ggml-cpu.c 468 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123
  1. #define _CRT_SECURE_NO_DEPRECATE // Disables "unsafe" warnings on Windows
  2. #define _USE_MATH_DEFINES // For M_PI on MSVC
  3. #include "ggml-backend-impl.h"
  4. #include "ggml-backend.h"
  5. #include "ggml-cpu-traits.h"
  6. #include "ggml-cpu-impl.h"
  7. #include "ggml-cpu.h"
  8. #include "ggml-impl.h"
  9. #include "ggml-quants.h"
  10. #include "ggml-cpu-quants.h"
  11. #include "ggml-threading.h"
  12. #include "amx/amx.h"
  13. #include "ggml.h"
  14. #if defined(_MSC_VER) || defined(__MINGW32__)
  15. #include <malloc.h> // using malloc.h with MSC/MINGW
  16. #elif !defined(__FreeBSD__) && !defined(__NetBSD__) && !defined(__OpenBSD__)
  17. #include <alloca.h>
  18. #endif
  19. #include <assert.h>
  20. #include <errno.h>
  21. #include <time.h>
  22. #include <math.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <stdint.h>
  26. #include <inttypes.h>
  27. #include <stdio.h>
  28. #include <float.h>
  29. #include <limits.h>
  30. #include <stdarg.h>
  31. #include <signal.h>
  32. #if defined(__gnu_linux__)
  33. #include <syscall.h>
  34. #endif
  35. #ifdef GGML_USE_OPENMP
  36. #include <omp.h>
  37. #endif
  38. #if defined(__ARM_FEATURE_SVE) || defined(__ARM_FEATURE_MATMUL_INT8)
  39. #undef GGML_USE_LLAMAFILE
  40. #endif
  41. #ifdef GGML_USE_LLAMAFILE
  42. #include "llamafile/sgemm.h"
  43. #endif
  44. #if defined(_MSC_VER)
  45. // disable "possible loss of data" to avoid hundreds of casts
  46. // we should just be careful :)
  47. #pragma warning(disable: 4244 4267)
  48. // disable POSIX deprecation warnings
  49. // these functions are never going away, anyway
  50. #pragma warning(disable: 4996)
  51. // unreachable code because of multiple instances of code after GGML_ABORT
  52. #pragma warning(disable: 4702)
  53. #endif
  54. // Note: once we move threading into a separate C++ file
  55. // will use std::hardware_destructive_interference_size instead of hardcoding it here
  56. // and we'll use C++ attribute syntax.
  57. #define GGML_CACHE_LINE 64
  58. #if defined(__clang__) || defined(__GNUC__)
  59. #define GGML_CACHE_ALIGN __attribute__((aligned(GGML_CACHE_LINE)))
  60. #endif
  61. #if defined(__has_feature)
  62. #if __has_feature(thread_sanitizer)
  63. #define GGML_TSAN_ENABLED 1
  64. #endif
  65. #else // __has_feature
  66. #if defined(__SANITIZE_THREAD__)
  67. #define GGML_TSAN_ENABLED 1
  68. #endif
  69. #endif // __has_feature
  70. #define UNUSED GGML_UNUSED
  71. #define SWAP(x, y, T) do { T SWAP = x; (x) = y; (y) = SWAP; } while (0)
  72. #if defined(GGML_USE_ACCELERATE)
  73. #include <Accelerate/Accelerate.h>
  74. #endif
  75. // floating point type used to accumulate sums
  76. typedef double ggml_float;
  77. #define GGML_GELU_FP16
  78. #define GGML_GELU_QUICK_FP16
  79. #define GGML_SOFT_MAX_UNROLL 4
  80. #define GGML_VEC_DOT_UNROLL 2
  81. #define GGML_VEC_MAD_UNROLL 32
  82. //
  83. // global data
  84. //
  85. // precomputed gelu table for f16 (128 KB)
  86. static ggml_fp16_t ggml_table_gelu_f16[1 << 16];
  87. // precomputed quick gelu table for f16 (128 KB)
  88. static ggml_fp16_t ggml_table_gelu_quick_f16[1 << 16];
  89. #if defined(__ARM_ARCH)
  90. struct ggml_arm_arch_features_type {
  91. int has_neon;
  92. int has_dotprod;
  93. int has_i8mm;
  94. int has_sve;
  95. int sve_cnt;
  96. } ggml_arm_arch_features = {-1, -1, -1, -1, 0};
  97. #endif
  98. #if defined(_WIN32)
  99. #define WIN32_LEAN_AND_MEAN
  100. #ifndef NOMINMAX
  101. #define NOMINMAX
  102. #endif
  103. #include <windows.h>
  104. #if defined(_MSC_VER) && !defined(__clang__)
  105. #define GGML_CACHE_ALIGN __declspec(align(GGML_CACHE_LINE))
  106. typedef volatile LONG atomic_int;
  107. typedef atomic_int atomic_bool;
  108. typedef atomic_int atomic_flag;
  109. #define ATOMIC_FLAG_INIT 0
  110. typedef enum {
  111. memory_order_relaxed,
  112. memory_order_consume,
  113. memory_order_acquire,
  114. memory_order_release,
  115. memory_order_acq_rel,
  116. memory_order_seq_cst
  117. } memory_order;
  118. static void atomic_store(atomic_int * ptr, LONG val) {
  119. InterlockedExchange(ptr, val);
  120. }
  121. static void atomic_store_explicit(atomic_int * ptr, LONG val, memory_order mo) {
  122. // TODO: add support for explicit memory order
  123. InterlockedExchange(ptr, val);
  124. }
  125. static LONG atomic_load(atomic_int * ptr) {
  126. return InterlockedCompareExchange(ptr, 0, 0);
  127. }
  128. static LONG atomic_load_explicit(atomic_int * ptr, memory_order mo) {
  129. // TODO: add support for explicit memory order
  130. return InterlockedCompareExchange(ptr, 0, 0);
  131. }
  132. static LONG atomic_fetch_add(atomic_int * ptr, LONG inc) {
  133. return InterlockedExchangeAdd(ptr, inc);
  134. }
  135. static LONG atomic_fetch_add_explicit(atomic_int * ptr, LONG inc, memory_order mo) {
  136. // TODO: add support for explicit memory order
  137. return InterlockedExchangeAdd(ptr, inc);
  138. }
  139. static atomic_bool atomic_flag_test_and_set(atomic_flag * ptr) {
  140. return InterlockedExchange(ptr, 1);
  141. }
  142. static void atomic_flag_clear(atomic_flag * ptr) {
  143. InterlockedExchange(ptr, 0);
  144. }
  145. static void atomic_thread_fence(memory_order mo) {
  146. MemoryBarrier();
  147. }
  148. #else // clang
  149. #include <stdatomic.h>
  150. #endif
  151. typedef HANDLE pthread_t;
  152. typedef DWORD thread_ret_t;
  153. static int pthread_create(pthread_t * out, void * unused, thread_ret_t(*func)(void *), void * arg) {
  154. (void) unused;
  155. HANDLE handle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) func, arg, 0, NULL);
  156. if (handle == NULL)
  157. {
  158. return EAGAIN;
  159. }
  160. *out = handle;
  161. return 0;
  162. }
  163. static int pthread_join(pthread_t thread, void * unused) {
  164. (void) unused;
  165. int ret = (int) WaitForSingleObject(thread, INFINITE);
  166. CloseHandle(thread);
  167. return ret;
  168. }
  169. static int sched_yield (void) {
  170. Sleep (0);
  171. return 0;
  172. }
  173. #else
  174. #include <pthread.h>
  175. #include <stdatomic.h>
  176. #include <sched.h>
  177. #if defined(__FreeBSD__)
  178. #include <pthread_np.h>
  179. #endif
  180. typedef void * thread_ret_t;
  181. #include <sys/types.h>
  182. #include <sys/stat.h>
  183. #include <unistd.h>
  184. #endif
  185. typedef pthread_t ggml_thread_t;
  186. #if defined(__APPLE__)
  187. #include <unistd.h>
  188. #include <mach/mach.h>
  189. #include <TargetConditionals.h>
  190. #endif
  191. //
  192. // cache line
  193. //
  194. #if defined(__cpp_lib_hardware_interference_size)
  195. #define CACHE_LINE_SIZE hardware_destructive_interference_size
  196. #else
  197. #if defined(__POWER9_VECTOR__)
  198. #define CACHE_LINE_SIZE 128
  199. #else
  200. #define CACHE_LINE_SIZE 64
  201. #endif
  202. #endif
  203. static const size_t CACHE_LINE_SIZE_F32 = CACHE_LINE_SIZE/sizeof(float);
  204. static void ggml_vec_dot_f32(int n, float * restrict s, size_t bs, const float * restrict x, size_t bx, const float * restrict y, size_t by, int nrc);
  205. static void ggml_vec_dot_f16(int n, float * restrict s, size_t bs, ggml_fp16_t * restrict x, size_t bx, ggml_fp16_t * restrict y, size_t by, int nrc);
  206. static void ggml_vec_dot_bf16(int n, float * restrict s, size_t bs, ggml_bf16_t * restrict x, size_t bx, ggml_bf16_t * restrict y, size_t by, int nrc);
  207. static const struct ggml_type_traits_cpu type_traits_cpu[GGML_TYPE_COUNT] = {
  208. [GGML_TYPE_F32] = {
  209. .vec_dot = (ggml_vec_dot_t) ggml_vec_dot_f32,
  210. .vec_dot_type = GGML_TYPE_F32,
  211. .nrows = 1,
  212. },
  213. [GGML_TYPE_F16] = {
  214. .from_float = (ggml_from_float_t) ggml_fp32_to_fp16_row,
  215. .vec_dot = (ggml_vec_dot_t) ggml_vec_dot_f16,
  216. .vec_dot_type = GGML_TYPE_F16,
  217. .nrows = 1,
  218. },
  219. [GGML_TYPE_Q4_0] = {
  220. .from_float = quantize_row_q4_0,
  221. .vec_dot = ggml_vec_dot_q4_0_q8_0,
  222. .vec_dot_type = GGML_TYPE_Q8_0,
  223. #if defined (__ARM_FEATURE_MATMUL_INT8)
  224. .nrows = 2,
  225. #else
  226. .nrows = 1,
  227. #endif
  228. },
  229. [GGML_TYPE_Q4_1] = {
  230. .from_float = quantize_row_q4_1,
  231. .vec_dot = ggml_vec_dot_q4_1_q8_1,
  232. .vec_dot_type = GGML_TYPE_Q8_1,
  233. #if defined (__ARM_FEATURE_MATMUL_INT8)
  234. .nrows = 2,
  235. #else
  236. .nrows = 1,
  237. #endif
  238. },
  239. [GGML_TYPE_Q5_0] = {
  240. .from_float = quantize_row_q5_0,
  241. .vec_dot = ggml_vec_dot_q5_0_q8_0,
  242. .vec_dot_type = GGML_TYPE_Q8_0,
  243. .nrows = 1,
  244. },
  245. [GGML_TYPE_Q5_1] = {
  246. .from_float = quantize_row_q5_1,
  247. .vec_dot = ggml_vec_dot_q5_1_q8_1,
  248. .vec_dot_type = GGML_TYPE_Q8_1,
  249. .nrows = 1,
  250. },
  251. [GGML_TYPE_Q8_0] = {
  252. .from_float = quantize_row_q8_0,
  253. .vec_dot = ggml_vec_dot_q8_0_q8_0,
  254. .vec_dot_type = GGML_TYPE_Q8_0,
  255. #if defined (__ARM_FEATURE_MATMUL_INT8)
  256. .nrows = 2,
  257. #else
  258. .nrows = 1,
  259. #endif
  260. },
  261. [GGML_TYPE_Q8_1] = {
  262. .from_float = quantize_row_q8_1,
  263. .vec_dot_type = GGML_TYPE_Q8_1,
  264. .nrows = 1,
  265. },
  266. [GGML_TYPE_Q2_K] = {
  267. .from_float = quantize_row_q2_K,
  268. .vec_dot = ggml_vec_dot_q2_K_q8_K,
  269. .vec_dot_type = GGML_TYPE_Q8_K,
  270. .nrows = 1,
  271. },
  272. [GGML_TYPE_Q3_K] = {
  273. .from_float = quantize_row_q3_K,
  274. .vec_dot = ggml_vec_dot_q3_K_q8_K,
  275. .vec_dot_type = GGML_TYPE_Q8_K,
  276. .nrows = 1,
  277. },
  278. [GGML_TYPE_Q4_K] = {
  279. .from_float = quantize_row_q4_K,
  280. .vec_dot = ggml_vec_dot_q4_K_q8_K,
  281. .vec_dot_type = GGML_TYPE_Q8_K,
  282. .nrows = 1,
  283. },
  284. [GGML_TYPE_Q5_K] = {
  285. .from_float = quantize_row_q5_K,
  286. .vec_dot = ggml_vec_dot_q5_K_q8_K,
  287. .vec_dot_type = GGML_TYPE_Q8_K,
  288. .nrows = 1,
  289. },
  290. [GGML_TYPE_Q6_K] = {
  291. .from_float = quantize_row_q6_K,
  292. .vec_dot = ggml_vec_dot_q6_K_q8_K,
  293. .vec_dot_type = GGML_TYPE_Q8_K,
  294. .nrows = 1,
  295. },
  296. [GGML_TYPE_IQ2_XXS] = {
  297. .from_float = NULL,
  298. .vec_dot = ggml_vec_dot_iq2_xxs_q8_K,
  299. .vec_dot_type = GGML_TYPE_Q8_K,
  300. .nrows = 1,
  301. },
  302. [GGML_TYPE_IQ2_XS] = {
  303. .from_float = NULL,
  304. .vec_dot = ggml_vec_dot_iq2_xs_q8_K,
  305. .vec_dot_type = GGML_TYPE_Q8_K,
  306. .nrows = 1,
  307. },
  308. [GGML_TYPE_IQ3_XXS] = {
  309. // NOTE: from_float for iq3 and iq2_s was removed because these quants require initialization in ggml_quantize_init
  310. //.from_float = quantize_row_iq3_xxs,
  311. .vec_dot = ggml_vec_dot_iq3_xxs_q8_K,
  312. .vec_dot_type = GGML_TYPE_Q8_K,
  313. .nrows = 1,
  314. },
  315. [GGML_TYPE_IQ3_S] = {
  316. //.from_float = quantize_row_iq3_s,
  317. .vec_dot = ggml_vec_dot_iq3_s_q8_K,
  318. .vec_dot_type = GGML_TYPE_Q8_K,
  319. .nrows = 1,
  320. },
  321. [GGML_TYPE_IQ2_S] = {
  322. //.from_float = quantize_row_iq2_s,
  323. .vec_dot = ggml_vec_dot_iq2_s_q8_K,
  324. .vec_dot_type = GGML_TYPE_Q8_K,
  325. .nrows = 1,
  326. },
  327. [GGML_TYPE_IQ1_S] = {
  328. .from_float = NULL,
  329. .vec_dot = ggml_vec_dot_iq1_s_q8_K,
  330. .vec_dot_type = GGML_TYPE_Q8_K,
  331. .nrows = 1,
  332. },
  333. [GGML_TYPE_IQ1_M] = {
  334. .from_float = NULL,
  335. .vec_dot = ggml_vec_dot_iq1_m_q8_K,
  336. .vec_dot_type = GGML_TYPE_Q8_K,
  337. .nrows = 1,
  338. },
  339. [GGML_TYPE_IQ4_NL] = {
  340. .from_float = quantize_row_iq4_nl,
  341. .vec_dot = ggml_vec_dot_iq4_nl_q8_0,
  342. .vec_dot_type = GGML_TYPE_Q8_0,
  343. .nrows = 1,
  344. },
  345. [GGML_TYPE_IQ4_XS] = {
  346. .from_float = quantize_row_iq4_xs,
  347. .vec_dot = ggml_vec_dot_iq4_xs_q8_K,
  348. .vec_dot_type = GGML_TYPE_Q8_K,
  349. .nrows = 1,
  350. },
  351. [GGML_TYPE_Q8_K] = {
  352. .from_float = quantize_row_q8_K,
  353. },
  354. [GGML_TYPE_BF16] = {
  355. .from_float = (ggml_from_float_t) ggml_fp32_to_bf16_row,
  356. .vec_dot = (ggml_vec_dot_t) ggml_vec_dot_bf16,
  357. .vec_dot_type = GGML_TYPE_BF16,
  358. .nrows = 1,
  359. },
  360. [GGML_TYPE_TQ1_0] = {
  361. .from_float = quantize_row_tq1_0,
  362. .vec_dot = ggml_vec_dot_tq1_0_q8_K,
  363. .vec_dot_type = GGML_TYPE_Q8_K,
  364. .nrows = 1,
  365. },
  366. [GGML_TYPE_TQ2_0] = {
  367. .from_float = quantize_row_tq2_0,
  368. .vec_dot = ggml_vec_dot_tq2_0_q8_K,
  369. .vec_dot_type = GGML_TYPE_Q8_K,
  370. .nrows = 1,
  371. },
  372. };
  373. const struct ggml_type_traits_cpu * ggml_get_type_traits_cpu(enum ggml_type type) {
  374. return &type_traits_cpu[type];
  375. }
  376. //
  377. // simd mappings
  378. //
  379. // we define a common set of C macros which map to specific intrinsics based on the current architecture
  380. // we then implement the fundamental computation operations below using only these macros
  381. // adding support for new architectures requires to define the corresponding SIMD macros
  382. //
  383. // GGML_F32_STEP / GGML_F16_STEP
  384. // number of elements to process in a single step
  385. //
  386. // GGML_F32_EPR / GGML_F16_EPR
  387. // number of elements to fit in a single register
  388. //
  389. #if defined(__ARM_NEON) && defined(__ARM_FEATURE_FMA)
  390. #define GGML_SIMD
  391. // F32 NEON
  392. #define GGML_F32_STEP 16
  393. #define GGML_F32_EPR 4
  394. #define GGML_F32x4 float32x4_t
  395. #define GGML_F32x4_ZERO vdupq_n_f32(0.0f)
  396. #define GGML_F32x4_SET1(x) vdupq_n_f32(x)
  397. #define GGML_F32x4_LOAD vld1q_f32
  398. #define GGML_F32x4_STORE vst1q_f32
  399. #define GGML_F32x4_FMA(a, b, c) vfmaq_f32(a, b, c)
  400. #define GGML_F32x4_ADD vaddq_f32
  401. #define GGML_F32x4_MUL vmulq_f32
  402. #define GGML_F32x4_REDUCE_ONE(x) vaddvq_f32(x)
  403. #define GGML_F32x4_REDUCE(res, x) \
  404. { \
  405. int offset = GGML_F32_ARR >> 1; \
  406. for (int i = 0; i < offset; ++i) { \
  407. (x)[i] = vaddq_f32((x)[i], (x)[offset+i]); \
  408. } \
  409. offset >>= 1; \
  410. for (int i = 0; i < offset; ++i) { \
  411. (x)[i] = vaddq_f32((x)[i], (x)[offset+i]); \
  412. } \
  413. offset >>= 1; \
  414. for (int i = 0; i < offset; ++i) { \
  415. (x)[i] = vaddq_f32((x)[i], (x)[offset+i]); \
  416. } \
  417. (res) = (ggml_float) GGML_F32x4_REDUCE_ONE((x)[0]); \
  418. }
  419. #define GGML_F32_VEC GGML_F32x4
  420. #define GGML_F32_VEC_ZERO GGML_F32x4_ZERO
  421. #define GGML_F32_VEC_SET1 GGML_F32x4_SET1
  422. #define GGML_F32_VEC_LOAD GGML_F32x4_LOAD
  423. #define GGML_F32_VEC_STORE GGML_F32x4_STORE
  424. #define GGML_F32_VEC_FMA GGML_F32x4_FMA
  425. #define GGML_F32_VEC_ADD GGML_F32x4_ADD
  426. #define GGML_F32_VEC_MUL GGML_F32x4_MUL
  427. #define GGML_F32_VEC_REDUCE GGML_F32x4_REDUCE
  428. // F16 NEON
  429. #if defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC)
  430. #define GGML_F16_STEP 32
  431. #define GGML_F16_EPR 8
  432. #define GGML_F16x8 float16x8_t
  433. #define GGML_F16x8_ZERO vdupq_n_f16(0.0f)
  434. #define GGML_F16x8_SET1(x) vdupq_n_f16(x)
  435. #define GGML_F16x8_LOAD(x) vld1q_f16((const ggml_fp16_internal_t *)(x))
  436. #define GGML_F16x8_STORE vst1q_f16
  437. #define GGML_F16x8_FMA(a, b, c) vfmaq_f16(a, b, c)
  438. #define GGML_F16x8_ADD vaddq_f16
  439. #define GGML_F16x8_MUL vmulq_f16
  440. #define GGML_F16x8_REDUCE(res, x) \
  441. do { \
  442. int offset = GGML_F16_ARR >> 1; \
  443. for (int i = 0; i < offset; ++i) { \
  444. (x)[i] = vaddq_f16((x)[i], (x)[offset+i]); \
  445. } \
  446. offset >>= 1; \
  447. for (int i = 0; i < offset; ++i) { \
  448. (x)[i] = vaddq_f16((x)[i], (x)[offset+i]); \
  449. } \
  450. offset >>= 1; \
  451. for (int i = 0; i < offset; ++i) { \
  452. (x)[i] = vaddq_f16((x)[i], (x)[offset+i]); \
  453. } \
  454. const float32x4_t t0 = vcvt_f32_f16(vget_low_f16 ((x)[0])); \
  455. const float32x4_t t1 = vcvt_f32_f16(vget_high_f16((x)[0])); \
  456. (res) = (ggml_float) vaddvq_f32(vaddq_f32(t0, t1)); \
  457. } while (0)
  458. #define GGML_F16_VEC GGML_F16x8
  459. #define GGML_F16_VEC_ZERO GGML_F16x8_ZERO
  460. #define GGML_F16_VEC_SET1 GGML_F16x8_SET1
  461. #define GGML_F16_VEC_LOAD(p, i) GGML_F16x8_LOAD(p)
  462. #define GGML_F16_VEC_STORE(p, r, i) GGML_F16x8_STORE((ggml_fp16_internal_t *)(p), (r)[i])
  463. #define GGML_F16_VEC_FMA GGML_F16x8_FMA
  464. #define GGML_F16_VEC_ADD GGML_F16x8_ADD
  465. #define GGML_F16_VEC_MUL GGML_F16x8_MUL
  466. #define GGML_F16_VEC_REDUCE GGML_F16x8_REDUCE
  467. #else
  468. // if FP16 vector arithmetic is not supported, we use FP32 instead
  469. // and take advantage of the vcvt_ functions to convert to/from FP16
  470. #define GGML_F16_STEP 16
  471. #define GGML_F16_EPR 4
  472. #define GGML_F32Cx4 float32x4_t
  473. #define GGML_F32Cx4_ZERO vdupq_n_f32(0.0f)
  474. #define GGML_F32Cx4_SET1(x) vdupq_n_f32(x)
  475. #define GGML_F32Cx4_LOAD(x) vcvt_f32_f16(vld1_f16((const ggml_fp16_internal_t *)(x)))
  476. #define GGML_F32Cx4_STORE(x, y) vst1_f16(x, vcvt_f16_f32(y))
  477. #define GGML_F32Cx4_FMA(a, b, c) vfmaq_f32(a, b, c)
  478. #define GGML_F32Cx4_ADD vaddq_f32
  479. #define GGML_F32Cx4_MUL vmulq_f32
  480. #define GGML_F32Cx4_REDUCE GGML_F32x4_REDUCE
  481. #define GGML_F16_VEC GGML_F32Cx4
  482. #define GGML_F16_VEC_ZERO GGML_F32Cx4_ZERO
  483. #define GGML_F16_VEC_SET1 GGML_F32Cx4_SET1
  484. #define GGML_F16_VEC_LOAD(p, i) GGML_F32Cx4_LOAD(p)
  485. #define GGML_F16_VEC_STORE(p, r, i) GGML_F32Cx4_STORE((ggml_fp16_internal_t *)(p), r[i])
  486. #define GGML_F16_VEC_FMA GGML_F32Cx4_FMA
  487. #define GGML_F16_VEC_ADD GGML_F32Cx4_ADD
  488. #define GGML_F16_VEC_MUL GGML_F32Cx4_MUL
  489. #define GGML_F16_VEC_REDUCE GGML_F32Cx4_REDUCE
  490. #endif
  491. #elif defined(__AVX512F__)
  492. #define GGML_SIMD
  493. // F32 AVX512
  494. #define GGML_F32_STEP 64
  495. #define GGML_F32_EPR 16
  496. #define GGML_F32x16 __m512
  497. #define GGML_F32x16_ZERO _mm512_setzero_ps()
  498. #define GGML_F32x16_SET1(x) _mm512_set1_ps(x)
  499. #define GGML_F32x16_LOAD _mm512_loadu_ps
  500. #define GGML_F32x16_STORE _mm512_storeu_ps
  501. // _mm512_fmadd_ps is defined in AVX512F so no guard is required
  502. #define GGML_F32x16_FMA(a, b, c) _mm512_fmadd_ps(b, c, a)
  503. #define GGML_F32x16_ADD _mm512_add_ps
  504. #define GGML_F32x16_MUL _mm512_mul_ps
  505. #define GGML_F32x16_REDUCE(res, x) \
  506. do { \
  507. int offset = GGML_F32_ARR >> 1; \
  508. for (int i = 0; i < offset; ++i) { \
  509. x[i] = _mm512_add_ps(x[i], x[offset+i]); \
  510. } \
  511. offset >>= 1; \
  512. for (int i = 0; i < offset; ++i) { \
  513. x[i] = _mm512_add_ps(x[i], x[offset+i]); \
  514. } \
  515. offset >>= 1; \
  516. for (int i = 0; i < offset; ++i) { \
  517. x[i] = _mm512_add_ps(x[i], x[offset+i]); \
  518. } \
  519. res = (ggml_float) _mm512_reduce_add_ps(x[0]); \
  520. } while (0)
  521. // TODO: is this optimal ?
  522. #define GGML_F32_VEC GGML_F32x16
  523. #define GGML_F32_VEC_ZERO GGML_F32x16_ZERO
  524. #define GGML_F32_VEC_SET1 GGML_F32x16_SET1
  525. #define GGML_F32_VEC_LOAD GGML_F32x16_LOAD
  526. #define GGML_F32_VEC_STORE GGML_F32x16_STORE
  527. #define GGML_F32_VEC_FMA GGML_F32x16_FMA
  528. #define GGML_F32_VEC_ADD GGML_F32x16_ADD
  529. #define GGML_F32_VEC_MUL GGML_F32x16_MUL
  530. #define GGML_F32_VEC_REDUCE GGML_F32x16_REDUCE
  531. // F16 AVX512
  532. // F16 AVX
  533. #define GGML_F16_STEP 64
  534. #define GGML_F16_EPR 16
  535. // AVX512 has FP16 extension (AVX512_FP16) but I don't have it on my machine so I use FP32 instead
  536. #define GGML_F32Cx16 __m512
  537. #define GGML_F32Cx16_ZERO _mm512_setzero_ps()
  538. #define GGML_F32Cx16_SET1(x) _mm512_set1_ps(x)
  539. // unlike _mm256_cvt intrinsics that require F16C, _mm512_cvt is defined in AVX512F
  540. // so F16C guard isn't required
  541. #define GGML_F32Cx16_LOAD(x) _mm512_cvtph_ps(_mm256_loadu_si256((const __m256i *)(x)))
  542. #define GGML_F32Cx16_STORE(x, y) _mm256_storeu_si256((__m256i *)(x), _mm512_cvtps_ph(y, 0))
  543. #define GGML_F32Cx16_FMA(a, b, c) _mm512_fmadd_ps(b, c, a)
  544. #define GGML_F32Cx16_ADD _mm512_add_ps
  545. #define GGML_F32Cx16_MUL _mm512_mul_ps
  546. #define GGML_F32Cx16_REDUCE(res, x) \
  547. do { \
  548. int offset = GGML_F32_ARR >> 1; \
  549. for (int i = 0; i < offset; ++i) { \
  550. x[i] = _mm512_add_ps(x[i], x[offset+i]); \
  551. } \
  552. offset >>= 1; \
  553. for (int i = 0; i < offset; ++i) { \
  554. x[i] = _mm512_add_ps(x[i], x[offset+i]); \
  555. } \
  556. offset >>= 1; \
  557. for (int i = 0; i < offset; ++i) { \
  558. x[i] = _mm512_add_ps(x[i], x[offset+i]); \
  559. } \
  560. res = (ggml_float) _mm512_reduce_add_ps(x[0]); \
  561. } while (0)
  562. #define GGML_F16_VEC GGML_F32Cx16
  563. #define GGML_F16_VEC_ZERO GGML_F32Cx16_ZERO
  564. #define GGML_F16_VEC_SET1 GGML_F32Cx16_SET1
  565. #define GGML_F16_VEC_LOAD(p, i) GGML_F32Cx16_LOAD(p)
  566. #define GGML_F16_VEC_STORE(p, r, i) GGML_F32Cx16_STORE(p, r[i])
  567. #define GGML_F16_VEC_FMA GGML_F32Cx16_FMA
  568. #define GGML_F16_VEC_ADD GGML_F32Cx16_ADD
  569. #define GGML_F16_VEC_MUL GGML_F32Cx16_MUL
  570. #define GGML_F16_VEC_REDUCE GGML_F32Cx16_REDUCE
  571. #elif defined(__AVX__)
  572. #define GGML_SIMD
  573. // F32 AVX
  574. #define GGML_F32_STEP 32
  575. #define GGML_F32_EPR 8
  576. #define GGML_F32x8 __m256
  577. #define GGML_F32x8_ZERO _mm256_setzero_ps()
  578. #define GGML_F32x8_SET1(x) _mm256_set1_ps(x)
  579. #define GGML_F32x8_LOAD _mm256_loadu_ps
  580. #define GGML_F32x8_STORE _mm256_storeu_ps
  581. #if defined(__FMA__)
  582. #define GGML_F32x8_FMA(a, b, c) _mm256_fmadd_ps(b, c, a)
  583. #else
  584. #define GGML_F32x8_FMA(a, b, c) _mm256_add_ps(_mm256_mul_ps(b, c), a)
  585. #endif
  586. #define GGML_F32x8_ADD _mm256_add_ps
  587. #define GGML_F32x8_MUL _mm256_mul_ps
  588. #define GGML_F32x8_REDUCE(res, x) \
  589. do { \
  590. int offset = GGML_F32_ARR >> 1; \
  591. for (int i = 0; i < offset; ++i) { \
  592. x[i] = _mm256_add_ps(x[i], x[offset+i]); \
  593. } \
  594. offset >>= 1; \
  595. for (int i = 0; i < offset; ++i) { \
  596. x[i] = _mm256_add_ps(x[i], x[offset+i]); \
  597. } \
  598. offset >>= 1; \
  599. for (int i = 0; i < offset; ++i) { \
  600. x[i] = _mm256_add_ps(x[i], x[offset+i]); \
  601. } \
  602. const __m128 t0 = _mm_add_ps(_mm256_castps256_ps128(x[0]), \
  603. _mm256_extractf128_ps(x[0], 1)); \
  604. const __m128 t1 = _mm_hadd_ps(t0, t0); \
  605. res = (ggml_float) _mm_cvtss_f32(_mm_hadd_ps(t1, t1)); \
  606. } while (0)
  607. // TODO: is this optimal ?
  608. #define GGML_F32_VEC GGML_F32x8
  609. #define GGML_F32_VEC_ZERO GGML_F32x8_ZERO
  610. #define GGML_F32_VEC_SET1 GGML_F32x8_SET1
  611. #define GGML_F32_VEC_LOAD GGML_F32x8_LOAD
  612. #define GGML_F32_VEC_STORE GGML_F32x8_STORE
  613. #define GGML_F32_VEC_FMA GGML_F32x8_FMA
  614. #define GGML_F32_VEC_ADD GGML_F32x8_ADD
  615. #define GGML_F32_VEC_MUL GGML_F32x8_MUL
  616. #define GGML_F32_VEC_REDUCE GGML_F32x8_REDUCE
  617. // F16 AVX
  618. #define GGML_F16_STEP 32
  619. #define GGML_F16_EPR 8
  620. // F16 arithmetic is not supported by AVX, so we use F32 instead
  621. #define GGML_F32Cx8 __m256
  622. #define GGML_F32Cx8_ZERO _mm256_setzero_ps()
  623. #define GGML_F32Cx8_SET1(x) _mm256_set1_ps(x)
  624. #if defined(__F16C__)
  625. // the _mm256_cvt intrinsics require F16C
  626. #define GGML_F32Cx8_LOAD(x) _mm256_cvtph_ps(_mm_loadu_si128((const __m128i *)(x)))
  627. #define GGML_F32Cx8_STORE(x, y) _mm_storeu_si128((__m128i *)(x), _mm256_cvtps_ph(y, 0))
  628. #else
  629. static inline __m256 __avx_f32cx8_load(const ggml_fp16_t * x) {
  630. float tmp[8];
  631. for (int i = 0; i < 8; i++) {
  632. tmp[i] = GGML_FP16_TO_FP32(x[i]);
  633. }
  634. return _mm256_loadu_ps(tmp);
  635. }
  636. static inline void __avx_f32cx8_store(ggml_fp16_t *x, __m256 y) {
  637. float arr[8];
  638. _mm256_storeu_ps(arr, y);
  639. for (int i = 0; i < 8; i++)
  640. x[i] = GGML_FP32_TO_FP16(arr[i]);
  641. }
  642. #define GGML_F32Cx8_LOAD(x) __avx_f32cx8_load(x)
  643. #define GGML_F32Cx8_STORE(x, y) __avx_f32cx8_store(x, y)
  644. #endif
  645. #define GGML_F32Cx8_FMA GGML_F32x8_FMA
  646. #define GGML_F32Cx8_ADD _mm256_add_ps
  647. #define GGML_F32Cx8_MUL _mm256_mul_ps
  648. #define GGML_F32Cx8_REDUCE GGML_F32x8_REDUCE
  649. #define GGML_F16_VEC GGML_F32Cx8
  650. #define GGML_F16_VEC_ZERO GGML_F32Cx8_ZERO
  651. #define GGML_F16_VEC_SET1 GGML_F32Cx8_SET1
  652. #define GGML_F16_VEC_LOAD(p, i) GGML_F32Cx8_LOAD(p)
  653. #define GGML_F16_VEC_STORE(p, r, i) GGML_F32Cx8_STORE(p, r[i])
  654. #define GGML_F16_VEC_FMA GGML_F32Cx8_FMA
  655. #define GGML_F16_VEC_ADD GGML_F32Cx8_ADD
  656. #define GGML_F16_VEC_MUL GGML_F32Cx8_MUL
  657. #define GGML_F16_VEC_REDUCE GGML_F32Cx8_REDUCE
  658. #elif defined(__POWER9_VECTOR__)
  659. #define GGML_SIMD
  660. // F32 POWER9
  661. #define GGML_F32_STEP 32
  662. #define GGML_F32_EPR 4
  663. #define GGML_F32x4 vector float
  664. #define GGML_F32x4_ZERO 0.0f
  665. #define GGML_F32x4_SET1 vec_splats
  666. #define GGML_F32x4_LOAD(p) vec_xl(0, p)
  667. #define GGML_F32x4_STORE(p, r) vec_xst(r, 0, p)
  668. #define GGML_F32x4_FMA(a, b, c) vec_madd(b, c, a)
  669. #define GGML_F32x4_ADD vec_add
  670. #define GGML_F32x4_MUL vec_mul
  671. #define GGML_F32x4_REDUCE(res, x) \
  672. { \
  673. int offset = GGML_F32_ARR >> 1; \
  674. for (int i = 0; i < offset; ++i) { \
  675. x[i] = vec_add(x[i], x[offset+i]); \
  676. } \
  677. offset >>= 1; \
  678. for (int i = 0; i < offset; ++i) { \
  679. x[i] = vec_add(x[i], x[offset+i]); \
  680. } \
  681. offset >>= 1; \
  682. for (int i = 0; i < offset; ++i) { \
  683. x[i] = vec_add(x[i], x[offset+i]); \
  684. } \
  685. res = vec_extract(x[0], 0) + \
  686. vec_extract(x[0], 1) + \
  687. vec_extract(x[0], 2) + \
  688. vec_extract(x[0], 3); \
  689. }
  690. #define GGML_F32_VEC GGML_F32x4
  691. #define GGML_F32_VEC_ZERO GGML_F32x4_ZERO
  692. #define GGML_F32_VEC_SET1 GGML_F32x4_SET1
  693. #define GGML_F32_VEC_LOAD GGML_F32x4_LOAD
  694. #define GGML_F32_VEC_STORE GGML_F32x4_STORE
  695. #define GGML_F32_VEC_FMA GGML_F32x4_FMA
  696. #define GGML_F32_VEC_ADD GGML_F32x4_ADD
  697. #define GGML_F32_VEC_MUL GGML_F32x4_MUL
  698. #define GGML_F32_VEC_REDUCE GGML_F32x4_REDUCE
  699. // F16 POWER9
  700. #define GGML_F16_STEP GGML_F32_STEP
  701. #define GGML_F16_EPR GGML_F32_EPR
  702. #define GGML_F16_VEC GGML_F32x4
  703. #define GGML_F16_VEC_ZERO GGML_F32x4_ZERO
  704. #define GGML_F16_VEC_SET1 GGML_F32x4_SET1
  705. #define GGML_F16_VEC_FMA GGML_F32x4_FMA
  706. #define GGML_F16_VEC_ADD GGML_F32x4_ADD
  707. #define GGML_F16_VEC_MUL GGML_F32x4_MUL
  708. #define GGML_F16_VEC_REDUCE GGML_F32x4_REDUCE
  709. // Use vec_xl, not vec_ld, in case the load address is not aligned.
  710. #define GGML_F16_VEC_LOAD(p, i) (i & 0x1) ? \
  711. vec_extract_fp32_from_shorth(vec_xl(0, p - GGML_F16_EPR)) : \
  712. vec_extract_fp32_from_shortl(vec_xl(0, p))
  713. #define GGML_ENDIAN_BYTE(i) ((unsigned char *)&(uint16_t){1})[i]
  714. #define GGML_F16_VEC_STORE(p, r, i) \
  715. if (i & 0x1) \
  716. vec_xst(vec_pack_to_short_fp32(r[i - GGML_ENDIAN_BYTE(1)], \
  717. r[i - GGML_ENDIAN_BYTE(0)]), \
  718. 0, p - GGML_F16_EPR)
  719. #elif defined(__wasm_simd128__)
  720. #define GGML_SIMD
  721. // F32 WASM
  722. #define GGML_F32_STEP 16
  723. #define GGML_F32_EPR 4
  724. #define GGML_F32x4 v128_t
  725. #define GGML_F32x4_ZERO wasm_f32x4_splat(0.0f)
  726. #define GGML_F32x4_SET1(x) wasm_f32x4_splat(x)
  727. #define GGML_F32x4_LOAD wasm_v128_load
  728. #define GGML_F32x4_STORE wasm_v128_store
  729. #define GGML_F32x4_FMA(a, b, c) wasm_f32x4_add(wasm_f32x4_mul(b, c), a)
  730. #define GGML_F32x4_ADD wasm_f32x4_add
  731. #define GGML_F32x4_MUL wasm_f32x4_mul
  732. #define GGML_F32x4_REDUCE(res, x) \
  733. { \
  734. int offset = GGML_F32_ARR >> 1; \
  735. for (int i = 0; i < offset; ++i) { \
  736. x[i] = wasm_f32x4_add(x[i], x[offset+i]); \
  737. } \
  738. offset >>= 1; \
  739. for (int i = 0; i < offset; ++i) { \
  740. x[i] = wasm_f32x4_add(x[i], x[offset+i]); \
  741. } \
  742. offset >>= 1; \
  743. for (int i = 0; i < offset; ++i) { \
  744. x[i] = wasm_f32x4_add(x[i], x[offset+i]); \
  745. } \
  746. res = wasm_f32x4_extract_lane(x[0], 0) + \
  747. wasm_f32x4_extract_lane(x[0], 1) + \
  748. wasm_f32x4_extract_lane(x[0], 2) + \
  749. wasm_f32x4_extract_lane(x[0], 3); \
  750. }
  751. #define GGML_F32_VEC GGML_F32x4
  752. #define GGML_F32_VEC_ZERO GGML_F32x4_ZERO
  753. #define GGML_F32_VEC_SET1 GGML_F32x4_SET1
  754. #define GGML_F32_VEC_LOAD GGML_F32x4_LOAD
  755. #define GGML_F32_VEC_STORE GGML_F32x4_STORE
  756. #define GGML_F32_VEC_FMA GGML_F32x4_FMA
  757. #define GGML_F32_VEC_ADD GGML_F32x4_ADD
  758. #define GGML_F32_VEC_MUL GGML_F32x4_MUL
  759. #define GGML_F32_VEC_REDUCE GGML_F32x4_REDUCE
  760. // F16 WASM
  761. #define GGML_F16_STEP 16
  762. #define GGML_F16_EPR 4
  763. inline static v128_t __wasm_f16x4_load(const ggml_fp16_t * p) {
  764. float tmp[4];
  765. tmp[0] = GGML_FP16_TO_FP32(p[0]);
  766. tmp[1] = GGML_FP16_TO_FP32(p[1]);
  767. tmp[2] = GGML_FP16_TO_FP32(p[2]);
  768. tmp[3] = GGML_FP16_TO_FP32(p[3]);
  769. return wasm_v128_load(tmp);
  770. }
  771. inline static void __wasm_f16x4_store(ggml_fp16_t * p, v128_t x) {
  772. float tmp[4];
  773. wasm_v128_store(tmp, x);
  774. p[0] = GGML_FP32_TO_FP16(tmp[0]);
  775. p[1] = GGML_FP32_TO_FP16(tmp[1]);
  776. p[2] = GGML_FP32_TO_FP16(tmp[2]);
  777. p[3] = GGML_FP32_TO_FP16(tmp[3]);
  778. }
  779. #define GGML_F16x4 v128_t
  780. #define GGML_F16x4_ZERO wasm_f32x4_splat(0.0f)
  781. #define GGML_F16x4_SET1(x) wasm_f32x4_splat(x)
  782. #define GGML_F16x4_LOAD(x) __wasm_f16x4_load(x)
  783. #define GGML_F16x4_STORE(x, y) __wasm_f16x4_store(x, y)
  784. #define GGML_F16x4_FMA GGML_F32x4_FMA
  785. #define GGML_F16x4_ADD wasm_f32x4_add
  786. #define GGML_F16x4_MUL wasm_f32x4_mul
  787. #define GGML_F16x4_REDUCE(res, x) \
  788. { \
  789. int offset = GGML_F16_ARR >> 1; \
  790. for (int i = 0; i < offset; ++i) { \
  791. x[i] = wasm_f32x4_add(x[i], x[offset+i]); \
  792. } \
  793. offset >>= 1; \
  794. for (int i = 0; i < offset; ++i) { \
  795. x[i] = wasm_f32x4_add(x[i], x[offset+i]); \
  796. } \
  797. offset >>= 1; \
  798. for (int i = 0; i < offset; ++i) { \
  799. x[i] = wasm_f32x4_add(x[i], x[offset+i]); \
  800. } \
  801. res = wasm_f32x4_extract_lane(x[0], 0) + \
  802. wasm_f32x4_extract_lane(x[0], 1) + \
  803. wasm_f32x4_extract_lane(x[0], 2) + \
  804. wasm_f32x4_extract_lane(x[0], 3); \
  805. }
  806. #define GGML_F16_VEC GGML_F16x4
  807. #define GGML_F16_VEC_ZERO GGML_F16x4_ZERO
  808. #define GGML_F16_VEC_SET1 GGML_F16x4_SET1
  809. #define GGML_F16_VEC_LOAD(p, i) GGML_F16x4_LOAD(p)
  810. #define GGML_F16_VEC_STORE(p, r, i) GGML_F16x4_STORE(p, r[i])
  811. #define GGML_F16_VEC_FMA GGML_F16x4_FMA
  812. #define GGML_F16_VEC_ADD GGML_F16x4_ADD
  813. #define GGML_F16_VEC_MUL GGML_F16x4_MUL
  814. #define GGML_F16_VEC_REDUCE GGML_F16x4_REDUCE
  815. #elif defined(__SSE3__)
  816. #define GGML_SIMD
  817. // F32 SSE
  818. #define GGML_F32_STEP 32
  819. #define GGML_F32_EPR 4
  820. #define GGML_F32x4 __m128
  821. #define GGML_F32x4_ZERO _mm_setzero_ps()
  822. #define GGML_F32x4_SET1(x) _mm_set1_ps(x)
  823. #define GGML_F32x4_LOAD _mm_loadu_ps
  824. #define GGML_F32x4_STORE _mm_storeu_ps
  825. #if defined(__FMA__)
  826. // TODO: Does this work?
  827. #define GGML_F32x4_FMA(a, b, c) _mm_fmadd_ps(b, c, a)
  828. #else
  829. #define GGML_F32x4_FMA(a, b, c) _mm_add_ps(_mm_mul_ps(b, c), a)
  830. #endif
  831. #define GGML_F32x4_ADD _mm_add_ps
  832. #define GGML_F32x4_MUL _mm_mul_ps
  833. #define GGML_F32x4_REDUCE(res, x) \
  834. { \
  835. int offset = GGML_F32_ARR >> 1; \
  836. for (int i = 0; i < offset; ++i) { \
  837. x[i] = _mm_add_ps(x[i], x[offset+i]); \
  838. } \
  839. offset >>= 1; \
  840. for (int i = 0; i < offset; ++i) { \
  841. x[i] = _mm_add_ps(x[i], x[offset+i]); \
  842. } \
  843. offset >>= 1; \
  844. for (int i = 0; i < offset; ++i) { \
  845. x[i] = _mm_add_ps(x[i], x[offset+i]); \
  846. } \
  847. const __m128 t0 = _mm_hadd_ps(x[0], x[0]); \
  848. res = (ggml_float) _mm_cvtss_f32(_mm_hadd_ps(t0, t0)); \
  849. }
  850. // TODO: is this optimal ?
  851. #define GGML_F32_VEC GGML_F32x4
  852. #define GGML_F32_VEC_ZERO GGML_F32x4_ZERO
  853. #define GGML_F32_VEC_SET1 GGML_F32x4_SET1
  854. #define GGML_F32_VEC_LOAD GGML_F32x4_LOAD
  855. #define GGML_F32_VEC_STORE GGML_F32x4_STORE
  856. #define GGML_F32_VEC_FMA GGML_F32x4_FMA
  857. #define GGML_F32_VEC_ADD GGML_F32x4_ADD
  858. #define GGML_F32_VEC_MUL GGML_F32x4_MUL
  859. #define GGML_F32_VEC_REDUCE GGML_F32x4_REDUCE
  860. // F16 SSE
  861. #define GGML_F16_STEP 32
  862. #define GGML_F16_EPR 4
  863. static inline __m128 __sse_f16x4_load(const ggml_fp16_t * x) {
  864. float tmp[4];
  865. tmp[0] = GGML_FP16_TO_FP32(x[0]);
  866. tmp[1] = GGML_FP16_TO_FP32(x[1]);
  867. tmp[2] = GGML_FP16_TO_FP32(x[2]);
  868. tmp[3] = GGML_FP16_TO_FP32(x[3]);
  869. return _mm_loadu_ps(tmp);
  870. }
  871. static inline void __sse_f16x4_store(ggml_fp16_t * x, __m128 y) {
  872. float arr[4];
  873. _mm_storeu_ps(arr, y);
  874. x[0] = GGML_FP32_TO_FP16(arr[0]);
  875. x[1] = GGML_FP32_TO_FP16(arr[1]);
  876. x[2] = GGML_FP32_TO_FP16(arr[2]);
  877. x[3] = GGML_FP32_TO_FP16(arr[3]);
  878. }
  879. #define GGML_F32Cx4 __m128
  880. #define GGML_F32Cx4_ZERO _mm_setzero_ps()
  881. #define GGML_F32Cx4_SET1(x) _mm_set1_ps(x)
  882. #define GGML_F32Cx4_LOAD(x) __sse_f16x4_load(x)
  883. #define GGML_F32Cx4_STORE(x, y) __sse_f16x4_store(x, y)
  884. #define GGML_F32Cx4_FMA GGML_F32x4_FMA
  885. #define GGML_F32Cx4_ADD _mm_add_ps
  886. #define GGML_F32Cx4_MUL _mm_mul_ps
  887. #define GGML_F32Cx4_REDUCE GGML_F32x4_REDUCE
  888. #define GGML_F16_VEC GGML_F32Cx4
  889. #define GGML_F16_VEC_ZERO GGML_F32Cx4_ZERO
  890. #define GGML_F16_VEC_SET1 GGML_F32Cx4_SET1
  891. #define GGML_F16_VEC_LOAD(p, i) GGML_F32Cx4_LOAD(p)
  892. #define GGML_F16_VEC_STORE(p, r, i) GGML_F32Cx4_STORE(p, r[i])
  893. #define GGML_F16_VEC_FMA GGML_F32Cx4_FMA
  894. #define GGML_F16_VEC_ADD GGML_F32Cx4_ADD
  895. #define GGML_F16_VEC_MUL GGML_F32Cx4_MUL
  896. #define GGML_F16_VEC_REDUCE GGML_F32Cx4_REDUCE
  897. #elif defined(__loongarch_asx)
  898. #define GGML_SIMD
  899. // F32 LASX
  900. #define GGML_F32_STEP 32
  901. #define GGML_F32_EPR 8
  902. #define GGML_F32x8 __m256
  903. #define GGML_F32x8_ZERO (__m256)__lasx_xvldi(0)
  904. #define GGML_F32x8_SET1(x) (__m256)__lasx_xvreplfr2vr_s((x))
  905. #define GGML_F32x8_LOAD(x) (__m256)__lasx_xvld((x), 0)
  906. #define GGML_F32x8_STORE(x,y) __lasx_xvst((y), (x), 0)
  907. #define GGML_F32x8_FMA(a, b, c) __lasx_xvfmadd_s(b, c, a)
  908. #define GGML_F32x8_ADD __lasx_xvfadd_s
  909. #define GGML_F32x8_MUL __lasx_xvfmul_s
  910. #define GGML_F32x8_REDUCE(res, x) \
  911. do { \
  912. int offset = GGML_F32_ARR >> 1; \
  913. for (int i = 0; i < offset; ++i) { \
  914. x[i] = __lasx_xvfadd_s(x[i], x[offset+i]); \
  915. } \
  916. offset >>= 1; \
  917. for (int i = 0; i < offset; ++i) { \
  918. x[i] = __lasx_xvfadd_s(x[i], x[offset+i]); \
  919. } \
  920. offset >>= 1; \
  921. for (int i = 0; i < offset; ++i) { \
  922. x[i] = __lasx_xvfadd_s(x[i], x[offset+i]); \
  923. } \
  924. float *tmp_p = (float *)&x[0]; \
  925. res = tmp_p[0] + tmp_p[1] + tmp_p[2] + tmp_p[3] + tmp_p[4] + tmp_p[5] + tmp_p[6] + tmp_p[7]; \
  926. } while (0)
  927. // TODO: is this optimal ?
  928. #define GGML_F32_VEC GGML_F32x8
  929. #define GGML_F32_VEC_ZERO GGML_F32x8_ZERO
  930. #define GGML_F32_VEC_SET1 GGML_F32x8_SET1
  931. #define GGML_F32_VEC_LOAD GGML_F32x8_LOAD
  932. #define GGML_F32_VEC_STORE GGML_F32x8_STORE
  933. #define GGML_F32_VEC_FMA GGML_F32x8_FMA
  934. #define GGML_F32_VEC_ADD GGML_F32x8_ADD
  935. #define GGML_F32_VEC_MUL GGML_F32x8_MUL
  936. #define GGML_F32_VEC_REDUCE GGML_F32x8_REDUCE
  937. // F16 LASX
  938. #define GGML_F16_STEP 32
  939. #define GGML_F16_EPR 8
  940. // F16 arithmetic is not supported by AVX, so we use F32 instead
  941. #define GGML_F32Cx8 __m256
  942. #define GGML_F32Cx8_ZERO (__m256)__lasx_xvldi(0)
  943. #define GGML_F32Cx8_SET1(x) (__m256)__lasx_xvreplgr2vr_w((x))
  944. static inline __m256 __lasx_f32cx8_load(const ggml_fp16_t * x) {
  945. float tmp[8];
  946. for (int i = 0; i < 8; i++) {
  947. tmp[i] = GGML_FP16_TO_FP32(x[i]);
  948. }
  949. return (__m256)__lasx_xvld(tmp, 0);
  950. }
  951. static inline void __lasx_f32cx8_store(ggml_fp16_t * x, __m256 y) {
  952. float arr[8];
  953. __lasx_xvst(y, arr, 0);
  954. for (int i = 0; i < 8; i++) {
  955. x[i] = GGML_FP32_TO_FP16(arr[i]);
  956. }
  957. }
  958. #define GGML_F32Cx8_LOAD(x) __lasx_f32cx8_load(x)
  959. #define GGML_F32Cx8_STORE(x, y) __lasx_f32cx8_store(x, y)
  960. #define GGML_F32Cx8_FMA GGML_F32x8_FMA
  961. #define GGML_F32Cx8_ADD __lasx_xvfadd_s
  962. #define GGML_F32Cx8_MUL __lasx_xvfmul_s
  963. #define GGML_F32Cx8_REDUCE GGML_F32x8_REDUCE
  964. #define GGML_F16_VEC GGML_F32Cx8
  965. #define GGML_F16_VEC_ZERO GGML_F32Cx8_ZERO
  966. #define GGML_F16_VEC_SET1 GGML_F32Cx8_SET1
  967. #define GGML_F16_VEC_LOAD(p, i) GGML_F32Cx8_LOAD(p)
  968. #define GGML_F16_VEC_STORE(p, r, i) GGML_F32Cx8_STORE(p, r[i])
  969. #define GGML_F16_VEC_FMA GGML_F32Cx8_FMA
  970. #define GGML_F16_VEC_ADD GGML_F32Cx8_ADD
  971. #define GGML_F16_VEC_MUL GGML_F32Cx8_MUL
  972. #define GGML_F16_VEC_REDUCE GGML_F32Cx8_REDUCE
  973. #elif defined(__loongarch_sx)
  974. #define GGML_SIMD
  975. // F32 LSX
  976. #define GGML_F32_STEP 32
  977. #define GGML_F32_EPR 4
  978. #define GGML_F32x4 __m128
  979. #define GGML_F32x4_ZERO __lsx_vldi(0)
  980. #define GGML_F32x4_SET1(x) __lsx_vinsgr2vr_w(__lsx_vldi(0),(x), 0)
  981. #define GGML_F32x4_LOAD(x) __lsx_vld((x), 0)
  982. #define GGML_F32x4_STORE((x),(y)) __lsx_vst((y), (x), 0)
  983. #define GGML_F32x4_FMA(a, b, c) __lsx_vfmadd_s(b, c, a)
  984. #define GGML_F32x4_ADD __lsx_vfadd_s
  985. #define GGML_F32x4_MUL __lsx_vfmul_s
  986. #define GGML_F32x4_REDUCE(res, x) \
  987. { \
  988. int offset = GGML_F32_ARR >> 1; \
  989. for (int i = 0; i < offset; ++i) { \
  990. x[i] = __lsx_vfadd_s(x[i], x[offset + i]); \
  991. } \
  992. offset >>= 1; \
  993. for (int i = 0; i < offset; ++i) { \
  994. x[i] = __lsx_vfadd_s(x[i], x[offset + i]); \
  995. } \
  996. offset >>= 1; \
  997. for (int i = 0; i < offset; ++i) { \
  998. x[i] = __lsx_vfadd_s(x[i], x[offset + i]); \
  999. } \
  1000. __m128i tmp = __lsx_vsrli_d((__m128i) x[0], 32); \
  1001. tmp = (__m128i) __lsx_vfadd_s((__m128) tmp, x[0]); \
  1002. tmp = __lsx_vpickev_w(__lsx_vldi(0), tmp); \
  1003. const __m128 t0 = __lsx_vshuf4i_w(tmp, 0x88); \
  1004. tmp = __lsx_vsrli_d((__m128i) t0, 32); \
  1005. tmp = (__m128i) __lsx_vfadd_s((__m128) tmp, t0); \
  1006. tmp = __lsx_vpickev_w(__lsx_vldi(0), tmp); \
  1007. res = (ggml_float) __lsx_vpickve2gr_w(__lsx_vshuf4i_w(tmp, 0x88), 0); \
  1008. }
  1009. #define GGML_F32_VEC GGML_F32x4
  1010. #define GGML_F32_VEC_ZERO GGML_F32x4_ZERO
  1011. #define GGML_F32_VEC_SET1 GGML_F32x4_SET1
  1012. #define GGML_F32_VEC_LOAD GGML_F32x4_LOAD
  1013. #define GGML_F32_VEC_STORE GGML_F32x4_STORE
  1014. #define GGML_F32_VEC_FMA GGML_F32x4_FMA
  1015. #define GGML_F32_VEC_ADD GGML_F32x4_ADD
  1016. #define GGML_F32_VEC_MUL GGML_F32x4_MUL
  1017. #define GGML_F32_VEC_REDUCE GGML_F32x4_REDUCE
  1018. // F16 LSX
  1019. #define GGML_F16_STEP 32
  1020. #define GGML_F16_EPR 4
  1021. static inline __m128 __lsx_f16x4_load(const ggml_fp16_t * x) {
  1022. float tmp[4];
  1023. tmp[0] = GGML_FP16_TO_FP32(x[0]);
  1024. tmp[1] = GGML_FP16_TO_FP32(x[1]);
  1025. tmp[2] = GGML_FP16_TO_FP32(x[2]);
  1026. tmp[3] = GGML_FP16_TO_FP32(x[3]);
  1027. return __lsx_vld(tmp, 0);
  1028. }
  1029. static inline void __lsx_f16x4_store(ggml_fp16_t * x, __m128 y) {
  1030. float arr[4];
  1031. __lsx_vst(y, arr, 0);
  1032. x[0] = GGML_FP32_TO_FP16(arr[0]);
  1033. x[1] = GGML_FP32_TO_FP16(arr[1]);
  1034. x[2] = GGML_FP32_TO_FP16(arr[2]);
  1035. x[3] = GGML_FP32_TO_FP16(arr[3]);
  1036. }
  1037. #define GGML_F32Cx4 __m128
  1038. #define GGML_F32Cx4_ZERO __lsx_vldi(0)
  1039. #define GGML_F32Cx4_SET1(x) __lsx_vinsgr2vr_w(__lsx_vldi(0),(x), 0)
  1040. #define GGML_F32Cx4_LOAD(x) __lsx_f16x4_load(x)
  1041. #define GGML_F32Cx4_STORE(x, y) __lsx_f16x4_store(x, y)
  1042. #define GGML_F32Cx4_FMA GGML_F32x4_FMA
  1043. #define GGML_F32Cx4_ADD __lsx_vfadd_s
  1044. #define GGML_F32Cx4_MUL __lsx_vfmul_s
  1045. #define GGML_F32Cx4_REDUCE GGML_F32x4_REDUCE
  1046. #define GGML_F16_VEC GGML_F32Cx4
  1047. #define GGML_F16_VEC_ZERO GGML_F32Cx4_ZERO
  1048. #define GGML_F16_VEC_SET1 GGML_F32Cx4_SET1
  1049. #define GGML_F16_VEC_LOAD(p, i) GGML_F32Cx4_LOAD(p)
  1050. #define GGML_F16_VEC_STORE(p, r, i) GGML_F32Cx4_STORE(p, r[i])
  1051. #define GGML_F16_VEC_FMA GGML_F32Cx4_FMA
  1052. #define GGML_F16_VEC_ADD GGML_F32Cx4_ADD
  1053. #define GGML_F16_VEC_MUL GGML_F32Cx4_MUL
  1054. #define GGML_F16_VEC_REDUCE GGML_F32Cx4_REDUCE
  1055. #endif
  1056. // GGML_F32_ARR / GGML_F16_ARR
  1057. // number of registers to use per step
  1058. #ifdef GGML_SIMD
  1059. #define GGML_F32_ARR (GGML_F32_STEP/GGML_F32_EPR)
  1060. #define GGML_F16_ARR (GGML_F16_STEP/GGML_F16_EPR)
  1061. #endif
  1062. //
  1063. // Threading defs
  1064. //
  1065. typedef pthread_t ggml_thread_t;
  1066. #if defined(_WIN32)
  1067. typedef CONDITION_VARIABLE ggml_cond_t;
  1068. typedef SRWLOCK ggml_mutex_t;
  1069. #define ggml_mutex_init(m) InitializeSRWLock(m)
  1070. #define ggml_mutex_destroy(m)
  1071. #define ggml_mutex_lock(m) AcquireSRWLockExclusive(m)
  1072. #define ggml_mutex_unlock(m) ReleaseSRWLockExclusive(m)
  1073. #define ggml_mutex_lock_shared(m) AcquireSRWLockShared(m)
  1074. #define ggml_mutex_unlock_shared(m) ReleaseSRWLockShared(m)
  1075. #define ggml_cond_init(c) InitializeConditionVariable(c)
  1076. #define ggml_cond_destroy(c)
  1077. #define ggml_cond_wait(c, m) SleepConditionVariableSRW(c, m, INFINITE, CONDITION_VARIABLE_LOCKMODE_SHARED)
  1078. #define ggml_cond_broadcast(c) WakeAllConditionVariable(c)
  1079. #define ggml_thread_create pthread_create
  1080. #define ggml_thread_join pthread_join
  1081. #else
  1082. typedef pthread_cond_t ggml_cond_t;
  1083. typedef pthread_mutex_t ggml_mutex_t;
  1084. #define ggml_mutex_init(m) pthread_mutex_init(m, NULL)
  1085. #define ggml_mutex_destroy(m) pthread_mutex_destroy(m)
  1086. #define ggml_mutex_lock(m) pthread_mutex_lock(m)
  1087. #define ggml_mutex_unlock(m) pthread_mutex_unlock(m)
  1088. #define ggml_mutex_lock_shared(m) pthread_mutex_lock(m)
  1089. #define ggml_mutex_unlock_shared(m) pthread_mutex_unlock(m)
  1090. #define ggml_lock_init(x) UNUSED(x)
  1091. #define ggml_lock_destroy(x) UNUSED(x)
  1092. #if defined(__x86_64__) || (defined(_MSC_VER) && defined(_M_AMD64))
  1093. #define ggml_lock_lock(x) _mm_pause()
  1094. #else
  1095. #define ggml_lock_lock(x) UNUSED(x)
  1096. #endif
  1097. #define ggml_lock_unlock(x) UNUSED(x)
  1098. #define GGML_LOCK_INITIALIZER 0
  1099. #define ggml_cond_init(c) pthread_cond_init(c, NULL)
  1100. #define ggml_cond_destroy(c) pthread_cond_destroy(c)
  1101. #define ggml_cond_wait(c, m) pthread_cond_wait(c, m)
  1102. #define ggml_cond_broadcast(c) pthread_cond_broadcast(c)
  1103. #define ggml_thread_create pthread_create
  1104. #define ggml_thread_join pthread_join
  1105. #endif
  1106. // Threadpool def
  1107. struct ggml_threadpool {
  1108. ggml_mutex_t mutex; // mutex for cond.var
  1109. ggml_cond_t cond; // cond.var for waiting for new work
  1110. struct ggml_cgraph * cgraph;
  1111. struct ggml_cplan * cplan;
  1112. // synchronization primitives
  1113. atomic_int n_graph; // incremented when there is work to be done (i.e each graph)
  1114. atomic_int GGML_CACHE_ALIGN n_barrier;
  1115. atomic_int GGML_CACHE_ALIGN n_barrier_passed;
  1116. atomic_int current_chunk; // currently processing chunk during Mat_Mul, shared between all the threads.
  1117. // these are atomic as an annotation for thread-sanitizer
  1118. atomic_bool stop; // Used for stopping the threadpool altogether
  1119. atomic_bool pause; // Used for pausing the threadpool or individual threads
  1120. atomic_bool abort; // Used for aborting processing of a graph
  1121. struct ggml_compute_state * workers; // per thread state
  1122. int n_threads_max; // number of threads in the pool
  1123. atomic_int n_threads_cur; // number of threads used in the current graph
  1124. int32_t prio; // Scheduling priority
  1125. uint32_t poll; // Polling level (0 - no polling)
  1126. enum ggml_status ec;
  1127. };
  1128. // Per-thread state
  1129. struct ggml_compute_state {
  1130. #ifndef GGML_USE_OPENMP
  1131. ggml_thread_t thrd;
  1132. bool cpumask[GGML_MAX_N_THREADS];
  1133. int last_graph;
  1134. bool pending;
  1135. #endif
  1136. struct ggml_threadpool * threadpool;
  1137. int ith;
  1138. };
  1139. //
  1140. // fundamental operations
  1141. //
  1142. inline static void ggml_vec_set_i8(const int n, int8_t * x, const int8_t v) { for (int i = 0; i < n; ++i) x[i] = v; }
  1143. inline static void ggml_vec_set_i16(const int n, int16_t * x, const int16_t v) { for (int i = 0; i < n; ++i) x[i] = v; }
  1144. inline static void ggml_vec_set_i32(const int n, int32_t * x, const int32_t v) { for (int i = 0; i < n; ++i) x[i] = v; }
  1145. inline static void ggml_vec_cpy_i32(const int n, int32_t * y, const int32_t * x) { for (int i = 0; i < n; ++i) y[i] = x[i]; }
  1146. inline static void ggml_vec_set_f16(const int n, ggml_fp16_t * x, const int32_t v) { for (int i = 0; i < n; ++i) x[i] = v; }
  1147. inline static void ggml_vec_set_bf16(const int n, ggml_bf16_t * x, const ggml_bf16_t v) { for (int i = 0; i < n; ++i) x[i] = v; }
  1148. inline static void ggml_vec_add_f32 (const int n, float * z, const float * x, const float * y) { for (int i = 0; i < n; ++i) z[i] = x[i] + y[i]; }
  1149. inline static void ggml_vec_add1_f32(const int n, float * z, const float * x, const float v) { for (int i = 0; i < n; ++i) z[i] = x[i] + v; }
  1150. inline static void ggml_vec_acc_f32 (const int n, float * y, const float * x) { for (int i = 0; i < n; ++i) y[i] += x[i]; }
  1151. inline static void ggml_vec_acc1_f32(const int n, float * y, const float v) { for (int i = 0; i < n; ++i) y[i] += v; }
  1152. inline static void ggml_vec_sub_f32 (const int n, float * z, const float * x, const float * y) { for (int i = 0; i < n; ++i) z[i] = x[i] - y[i]; }
  1153. inline static void ggml_vec_set_f32 (const int n, float * x, const float v) { for (int i = 0; i < n; ++i) x[i] = v; }
  1154. inline static void ggml_vec_cpy_f32 (const int n, float * y, const float * x) { for (int i = 0; i < n; ++i) y[i] = x[i]; }
  1155. inline static void ggml_vec_neg_f32 (const int n, float * y, const float * x) { for (int i = 0; i < n; ++i) y[i] = -x[i]; }
  1156. inline static void ggml_vec_mul_f32 (const int n, float * z, const float * x, const float * y) { for (int i = 0; i < n; ++i) z[i] = x[i]*y[i]; }
  1157. inline static void ggml_vec_div_f32 (const int n, float * z, const float * x, const float * y) { for (int i = 0; i < n; ++i) z[i] = x[i]/y[i]; }
  1158. static void ggml_vec_dot_f32(int n, float * restrict s, size_t bs, const float * restrict x, size_t bx, const float * restrict y, size_t by, int nrc) {
  1159. assert(nrc == 1);
  1160. UNUSED(nrc);
  1161. UNUSED(bx);
  1162. UNUSED(by);
  1163. UNUSED(bs);
  1164. #if defined(GGML_SIMD)
  1165. float sumf = 0.0f;
  1166. const int np = (n & ~(GGML_F32_STEP - 1));
  1167. GGML_F32_VEC sum[GGML_F32_ARR] = { GGML_F32_VEC_ZERO };
  1168. GGML_F32_VEC ax[GGML_F32_ARR];
  1169. GGML_F32_VEC ay[GGML_F32_ARR];
  1170. for (int i = 0; i < np; i += GGML_F32_STEP) {
  1171. for (int j = 0; j < GGML_F32_ARR; j++) {
  1172. ax[j] = GGML_F32_VEC_LOAD(x + i + j*GGML_F32_EPR);
  1173. ay[j] = GGML_F32_VEC_LOAD(y + i + j*GGML_F32_EPR);
  1174. sum[j] = GGML_F32_VEC_FMA(sum[j], ax[j], ay[j]);
  1175. }
  1176. }
  1177. // reduce sum0..sum3 to sum0
  1178. GGML_F32_VEC_REDUCE(sumf, sum);
  1179. // leftovers
  1180. for (int i = np; i < n; ++i) {
  1181. sumf += x[i]*y[i];
  1182. }
  1183. #else
  1184. // scalar
  1185. ggml_float sumf = 0.0;
  1186. for (int i = 0; i < n; ++i) {
  1187. sumf += (ggml_float)(x[i]*y[i]);
  1188. }
  1189. #endif
  1190. *s = sumf;
  1191. }
  1192. static void ggml_vec_dot_bf16(int n, float * restrict s, size_t bs, ggml_bf16_t * restrict x, size_t bx, ggml_bf16_t * restrict y, size_t by, int nrc) {
  1193. assert(nrc == 1);
  1194. UNUSED(nrc);
  1195. UNUSED(bx);
  1196. UNUSED(by);
  1197. UNUSED(bs);
  1198. int i = 0;
  1199. ggml_float sumf = 0;
  1200. #if defined(__AVX512BF16__)
  1201. __m512 c1 = _mm512_setzero_ps();
  1202. __m512 c2 = _mm512_setzero_ps();
  1203. for (; i + 64 <= n; i += 64) {
  1204. c1 = _mm512_dpbf16_ps(c1, m512bh(_mm512_loadu_si512((x + i))),
  1205. m512bh(_mm512_loadu_si512((y + i))));
  1206. c2 = _mm512_dpbf16_ps(c2, m512bh(_mm512_loadu_si512((x + i + 32))),
  1207. m512bh(_mm512_loadu_si512((y + i + 32))));
  1208. }
  1209. sumf += (ggml_float)_mm512_reduce_add_ps(c1);
  1210. sumf += (ggml_float)_mm512_reduce_add_ps(c2);
  1211. #elif defined(__AVX512F__)
  1212. #define LOAD(p) _mm512_castsi512_ps(_mm512_slli_epi32(_mm512_cvtepu16_epi32(_mm256_loadu_si256((const __m256i *)(p))), 16))
  1213. __m512 c1 = _mm512_setzero_ps();
  1214. __m512 c2 = _mm512_setzero_ps();
  1215. for (; i + 32 <= n; i += 32) {
  1216. c1 = _mm512_add_ps(_mm512_mul_ps(LOAD(x + i), LOAD(y + i)), c1);
  1217. c2 = _mm512_add_ps(_mm512_mul_ps(LOAD(x + i + 16), LOAD(y + i + 16)), c2);
  1218. }
  1219. sumf += (ggml_float)_mm512_reduce_add_ps(c1);
  1220. sumf += (ggml_float)_mm512_reduce_add_ps(c2);
  1221. #undef LOAD
  1222. #elif defined(__AVX2__) || defined(__AVX__)
  1223. #if defined(__AVX2__)
  1224. #define LOAD(p) _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_cvtepu16_epi32(_mm_loadu_si128((const __m128i *)(p))), 16))
  1225. #else
  1226. #define LOAD(p) _mm256_castsi256_ps(_mm256_insertf128_si256(_mm256_castsi128_si256(_mm_slli_epi32(_mm_cvtepu16_epi32(_mm_loadu_si128((const __m128i *)(p))), 16)), (_mm_slli_epi32(_mm_cvtepu16_epi32(_mm_bsrli_si128(_mm_loadu_si128((const __m128i *)(p)), 8)), 16)), 1))
  1227. #endif
  1228. __m256 c1 = _mm256_setzero_ps();
  1229. __m256 c2 = _mm256_setzero_ps();
  1230. __m256 c3 = _mm256_setzero_ps();
  1231. __m256 c4 = _mm256_setzero_ps();
  1232. for (; i + 32 <= n; i += 32) {
  1233. c1 = _mm256_add_ps(_mm256_mul_ps(LOAD(x + i), LOAD(y + i)), c1);
  1234. c2 = _mm256_add_ps(_mm256_mul_ps(LOAD(x + i + 8), LOAD(y + i + 8)), c2);
  1235. c3 = _mm256_add_ps(_mm256_mul_ps(LOAD(x + i + 16), LOAD(y + i + 16)), c3);
  1236. c4 = _mm256_add_ps(_mm256_mul_ps(LOAD(x + i + 24), LOAD(y + i + 24)), c4);
  1237. }
  1238. __m128 g;
  1239. c1 = _mm256_add_ps(_mm256_add_ps(c1, c3),
  1240. _mm256_add_ps(c2, c4));
  1241. g = _mm_add_ps(_mm256_extractf128_ps(c1, 1),
  1242. _mm256_castps256_ps128(c1));
  1243. g = _mm_add_ps(g, _mm_movehl_ps(g, g));
  1244. g = _mm_add_ss(g, _mm_movehdup_ps(g));
  1245. sumf += (ggml_float)_mm_cvtss_f32(g);
  1246. #undef LOAD
  1247. #endif
  1248. for (; i < n; ++i) {
  1249. sumf += (ggml_float)(GGML_BF16_TO_FP32(x[i]) *
  1250. GGML_BF16_TO_FP32(y[i]));
  1251. }
  1252. *s = sumf;
  1253. }
  1254. static void ggml_vec_dot_f16(int n, float * restrict s, size_t bs, ggml_fp16_t * restrict x, size_t bx, ggml_fp16_t * restrict y, size_t by, int nrc) {
  1255. assert(nrc == 1);
  1256. UNUSED(nrc);
  1257. UNUSED(bx);
  1258. UNUSED(by);
  1259. UNUSED(bs);
  1260. ggml_float sumf = 0.0;
  1261. #if defined(GGML_SIMD)
  1262. const int np = (n & ~(GGML_F16_STEP - 1));
  1263. GGML_F16_VEC sum[GGML_F16_ARR] = { GGML_F16_VEC_ZERO };
  1264. GGML_F16_VEC ax[GGML_F16_ARR];
  1265. GGML_F16_VEC ay[GGML_F16_ARR];
  1266. for (int i = 0; i < np; i += GGML_F16_STEP) {
  1267. for (int j = 0; j < GGML_F16_ARR; j++) {
  1268. ax[j] = GGML_F16_VEC_LOAD(x + i + j*GGML_F16_EPR, j);
  1269. ay[j] = GGML_F16_VEC_LOAD(y + i + j*GGML_F16_EPR, j);
  1270. sum[j] = GGML_F16_VEC_FMA(sum[j], ax[j], ay[j]);
  1271. }
  1272. }
  1273. // reduce sum0..sum3 to sum0
  1274. GGML_F16_VEC_REDUCE(sumf, sum);
  1275. // leftovers
  1276. for (int i = np; i < n; ++i) {
  1277. sumf += (ggml_float)(GGML_FP16_TO_FP32(x[i])*GGML_FP16_TO_FP32(y[i]));
  1278. }
  1279. #else
  1280. for (int i = 0; i < n; ++i) {
  1281. sumf += (ggml_float)(GGML_FP16_TO_FP32(x[i])*GGML_FP16_TO_FP32(y[i]));
  1282. }
  1283. #endif
  1284. *s = sumf;
  1285. }
  1286. // compute GGML_VEC_DOT_UNROLL dot products at once
  1287. // xs - x row stride in bytes
  1288. inline static void ggml_vec_dot_f16_unroll(const int n, const int xs, float * restrict s, void * restrict xv, ggml_fp16_t * restrict y) {
  1289. ggml_float sumf[GGML_VEC_DOT_UNROLL] = { 0.0 };
  1290. ggml_fp16_t * restrict x[GGML_VEC_DOT_UNROLL];
  1291. for (int i = 0; i < GGML_VEC_DOT_UNROLL; ++i) {
  1292. x[i] = (ggml_fp16_t *) ((char *) xv + i*xs);
  1293. }
  1294. #if defined(GGML_SIMD)
  1295. const int np = (n & ~(GGML_F16_STEP - 1));
  1296. GGML_F16_VEC sum[GGML_VEC_DOT_UNROLL][GGML_F16_ARR] = { { GGML_F16_VEC_ZERO } };
  1297. GGML_F16_VEC ax[GGML_F16_ARR];
  1298. GGML_F16_VEC ay[GGML_F16_ARR];
  1299. for (int i = 0; i < np; i += GGML_F16_STEP) {
  1300. for (int j = 0; j < GGML_F16_ARR; j++) {
  1301. ay[j] = GGML_F16_VEC_LOAD(y + i + j*GGML_F16_EPR, j);
  1302. for (int k = 0; k < GGML_VEC_DOT_UNROLL; ++k) {
  1303. ax[j] = GGML_F16_VEC_LOAD(x[k] + i + j*GGML_F16_EPR, j);
  1304. sum[k][j] = GGML_F16_VEC_FMA(sum[k][j], ax[j], ay[j]);
  1305. }
  1306. }
  1307. }
  1308. // reduce sum0..sum3 to sum0
  1309. for (int k = 0; k < GGML_VEC_DOT_UNROLL; ++k) {
  1310. GGML_F16_VEC_REDUCE(sumf[k], sum[k]);
  1311. }
  1312. // leftovers
  1313. for (int i = np; i < n; ++i) {
  1314. for (int j = 0; j < GGML_VEC_DOT_UNROLL; ++j) {
  1315. sumf[j] += (ggml_float)(GGML_FP16_TO_FP32(x[j][i])*GGML_FP16_TO_FP32(y[i]));
  1316. }
  1317. }
  1318. #else
  1319. for (int i = 0; i < n; ++i) {
  1320. for (int j = 0; j < GGML_VEC_DOT_UNROLL; ++j) {
  1321. sumf[j] += (ggml_float)(GGML_FP16_TO_FP32(x[j][i])*GGML_FP16_TO_FP32(y[i]));
  1322. }
  1323. }
  1324. #endif
  1325. for (int i = 0; i < GGML_VEC_DOT_UNROLL; ++i) {
  1326. s[i] = sumf[i];
  1327. }
  1328. }
  1329. inline static void ggml_vec_mad_f32(const int n, float * restrict y, const float * restrict x, const float v) {
  1330. #if defined(GGML_SIMD)
  1331. const int np = (n & ~(GGML_F32_STEP - 1));
  1332. GGML_F32_VEC vx = GGML_F32_VEC_SET1(v);
  1333. GGML_F32_VEC ax[GGML_F32_ARR];
  1334. GGML_F32_VEC ay[GGML_F32_ARR];
  1335. for (int i = 0; i < np; i += GGML_F32_STEP) {
  1336. for (int j = 0; j < GGML_F32_ARR; j++) {
  1337. ax[j] = GGML_F32_VEC_LOAD(x + i + j*GGML_F32_EPR);
  1338. ay[j] = GGML_F32_VEC_LOAD(y + i + j*GGML_F32_EPR);
  1339. ay[j] = GGML_F32_VEC_FMA(ay[j], ax[j], vx);
  1340. GGML_F32_VEC_STORE(y + i + j*GGML_F32_EPR, ay[j]);
  1341. }
  1342. }
  1343. // leftovers
  1344. for (int i = np; i < n; ++i) {
  1345. y[i] += x[i]*v;
  1346. }
  1347. #else
  1348. // scalar
  1349. for (int i = 0; i < n; ++i) {
  1350. y[i] += x[i]*v;
  1351. }
  1352. #endif
  1353. }
  1354. inline static void ggml_vec_mad_f16(const int n, ggml_fp16_t * restrict y, const ggml_fp16_t * restrict x, const float v) {
  1355. #if defined(GGML_SIMD)
  1356. const int np = (n & ~(GGML_F16_STEP - 1));
  1357. GGML_F16_VEC vx = GGML_F16_VEC_SET1(v);
  1358. GGML_F16_VEC ax[GGML_F16_ARR];
  1359. GGML_F16_VEC ay[GGML_F16_ARR];
  1360. for (int i = 0; i < np; i += GGML_F16_STEP) {
  1361. for (int j = 0; j < GGML_F16_ARR; j++) {
  1362. ax[j] = GGML_F16_VEC_LOAD(x + i + j*GGML_F16_EPR, j);
  1363. ay[j] = GGML_F16_VEC_LOAD(y + i + j*GGML_F16_EPR, j);
  1364. ay[j] = GGML_F16_VEC_FMA(ay[j], ax[j], vx);
  1365. GGML_F16_VEC_STORE(y + i + j*GGML_F16_EPR, ay, j);
  1366. }
  1367. }
  1368. // leftovers
  1369. for (int i = np; i < n; ++i) {
  1370. y[i] = GGML_FP32_TO_FP16(GGML_FP16_TO_FP32(y[i]) + GGML_FP16_TO_FP32(x[i])*v);
  1371. }
  1372. #else
  1373. // scalar
  1374. for (int i = 0; i < n; ++i) {
  1375. y[i] = GGML_FP32_TO_FP16(GGML_FP16_TO_FP32(y[i]) + GGML_FP16_TO_FP32(x[i])*v);
  1376. }
  1377. #endif
  1378. }
  1379. // xs and vs are byte strides of x and v
  1380. inline static void ggml_vec_mad_f32_unroll(const int n, const int xs, const int vs, float * restrict y, const float * restrict xv, const float * restrict vv) {
  1381. const float * restrict x[GGML_VEC_MAD_UNROLL];
  1382. const float * restrict v[GGML_VEC_MAD_UNROLL];
  1383. for (int i = 0; i < GGML_VEC_MAD_UNROLL; ++i) {
  1384. x[i] = (const float *) ((const char *) xv + i*xs);
  1385. v[i] = (const float *) ((const char *) vv + i*vs);
  1386. }
  1387. #if defined(GGML_SIMD)
  1388. const int np = (n & ~(GGML_F32_STEP - 1));
  1389. GGML_F32_VEC vx[GGML_VEC_MAD_UNROLL];
  1390. for (int k = 0; k < GGML_VEC_MAD_UNROLL; ++k) {
  1391. vx[k] = GGML_F32_VEC_SET1(v[k][0]);
  1392. }
  1393. GGML_F32_VEC ax[GGML_VEC_MAD_UNROLL][GGML_F32_ARR];
  1394. GGML_F32_VEC ay[GGML_F32_ARR];
  1395. for (int i = 0; i < np; i += GGML_F32_STEP) {
  1396. for (int j = 0; j < GGML_F32_ARR; j++) {
  1397. ay[j] = GGML_F32_VEC_LOAD(y + i + j*GGML_F32_EPR);
  1398. for (int k = 0; k < GGML_VEC_MAD_UNROLL; ++k) {
  1399. ax[k][j] = GGML_F32_VEC_LOAD(x[k] + i + j*GGML_F32_EPR);
  1400. ay[j] = GGML_F32_VEC_FMA(ay[j], ax[k][j], vx[k]);
  1401. }
  1402. GGML_F32_VEC_STORE(y + i + j*GGML_F32_EPR, ay[j]);
  1403. }
  1404. }
  1405. // leftovers
  1406. for (int k = 0; k < GGML_VEC_MAD_UNROLL; ++k) {
  1407. for (int i = np; i < n; ++i) {
  1408. y[i] += x[k][i]*v[k][0];
  1409. }
  1410. }
  1411. #else
  1412. // scalar
  1413. for (int k = 0; k < GGML_VEC_MAD_UNROLL; ++k) {
  1414. for (int i = 0; i < n; ++i) {
  1415. y[i] += x[k][i]*v[k][0];
  1416. }
  1417. }
  1418. #endif
  1419. }
  1420. //inline static void ggml_vec_scale_f32(const int n, float * y, const float v) { for (int i = 0; i < n; ++i) y[i] *= v; }
  1421. inline static void ggml_vec_scale_f32(const int n, float * y, const float v) {
  1422. #if defined(GGML_USE_ACCELERATE)
  1423. vDSP_vsmul(y, 1, &v, y, 1, n);
  1424. #elif defined(GGML_SIMD)
  1425. const int np = (n & ~(GGML_F32_STEP - 1));
  1426. GGML_F32_VEC vx = GGML_F32_VEC_SET1(v);
  1427. GGML_F32_VEC ay[GGML_F32_ARR];
  1428. for (int i = 0; i < np; i += GGML_F32_STEP) {
  1429. for (int j = 0; j < GGML_F32_ARR; j++) {
  1430. ay[j] = GGML_F32_VEC_LOAD(y + i + j*GGML_F32_EPR);
  1431. ay[j] = GGML_F32_VEC_MUL(ay[j], vx);
  1432. GGML_F32_VEC_STORE(y + i + j*GGML_F32_EPR, ay[j]);
  1433. }
  1434. }
  1435. // leftovers
  1436. for (int i = np; i < n; ++i) {
  1437. y[i] *= v;
  1438. }
  1439. #else
  1440. // scalar
  1441. for (int i = 0; i < n; ++i) {
  1442. y[i] *= v;
  1443. }
  1444. #endif
  1445. }
  1446. inline static void ggml_vec_scale_f16(const int n, ggml_fp16_t * y, const float v) {
  1447. #if defined(GGML_SIMD)
  1448. const int np = (n & ~(GGML_F16_STEP - 1));
  1449. GGML_F16_VEC vx = GGML_F16_VEC_SET1(v);
  1450. GGML_F16_VEC ay[GGML_F16_ARR];
  1451. for (int i = 0; i < np; i += GGML_F16_STEP) {
  1452. for (int j = 0; j < GGML_F16_ARR; j++) {
  1453. ay[j] = GGML_F16_VEC_LOAD(y + i + j*GGML_F16_EPR, j);
  1454. ay[j] = GGML_F16_VEC_MUL(ay[j], vx);
  1455. GGML_F16_VEC_STORE(y + i + j*GGML_F16_EPR, ay, j);
  1456. }
  1457. }
  1458. // leftovers
  1459. for (int i = np; i < n; ++i) {
  1460. y[i] = GGML_FP32_TO_FP16(GGML_FP16_TO_FP32(y[i])*v);
  1461. }
  1462. #else
  1463. // scalar
  1464. for (int i = 0; i < n; ++i) {
  1465. y[i] = GGML_FP32_TO_FP16(GGML_FP16_TO_FP32(y[i])*v);
  1466. }
  1467. #endif
  1468. }
  1469. inline static void ggml_vec_norm_f32 (const int n, float * s, const float * x) { ggml_vec_dot_f32(n, s, 0, x, 0, x, 0, 1); *s = sqrtf(*s); }
  1470. inline static void ggml_vec_sqr_f32 (const int n, float * y, const float * x) { for (int i = 0; i < n; ++i) y[i] = x[i]*x[i]; }
  1471. inline static void ggml_vec_sqrt_f32 (const int n, float * y, const float * x) { for (int i = 0; i < n; ++i) y[i] = sqrtf(x[i]); }
  1472. inline static void ggml_vec_log_f32 (const int n, float * y, const float * x) { for (int i = 0; i < n; ++i) y[i] = logf(x[i]); }
  1473. inline static void ggml_vec_sin_f32 (const int n, float * y, const float * x) { for (int i = 0; i < n; ++i) y[i] = sinf(x[i]); }
  1474. inline static void ggml_vec_cos_f32 (const int n, float * y, const float * x) { for (int i = 0; i < n; ++i) y[i] = cosf(x[i]); }
  1475. inline static void ggml_vec_abs_f32 (const int n, float * y, const float * x) { for (int i = 0; i < n; ++i) y[i] = fabsf(x[i]); }
  1476. inline static void ggml_vec_sgn_f32 (const int n, float * y, const float * x) { for (int i = 0; i < n; ++i) y[i] = (x[i] > 0.f) ? 1.f : ((x[i] < 0.f) ? -1.f : 0.f); }
  1477. inline static void ggml_vec_step_f32 (const int n, float * y, const float * x) { for (int i = 0; i < n; ++i) y[i] = (x[i] > 0.f) ? 1.f : 0.f; }
  1478. inline static void ggml_vec_tanh_f32 (const int n, float * y, const float * x) { for (int i = 0; i < n; ++i) y[i] = tanhf(x[i]); }
  1479. inline static void ggml_vec_elu_f32 (const int n, float * y, const float * x) { for (int i = 0; i < n; ++i) y[i] = (x[i] > 0.f) ? x[i] : expm1f(x[i]); }
  1480. inline static void ggml_vec_relu_f32 (const int n, float * y, const float * x) { for (int i = 0; i < n; ++i) y[i] = (x[i] > 0.f) ? x[i] : 0.f; }
  1481. inline static void ggml_vec_leaky_relu_f32 (const int n, float * y, const float * x, const float ns) { for (int i = 0; i < n; ++i) y[i] = ((x[i] > 0.f) ? x[i] : 0.f) + ns * ((x[i] < 0.0f) ? x[i] : 0.f); }
  1482. inline static void ggml_vec_sigmoid_f32 (const int n, float * y, const float * x) { for (int i = 0; i < n; ++i) y[i] = 1.f / (1.f + expf(-x[i])); }
  1483. // TODO: optimize performance
  1484. inline static void ggml_vec_hardswish_f32 (const int n, float * y, const float * x) { for (int i = 0; i < n; ++i) y[i] = x[i] * fminf(1.0f, fmaxf(0.0f, (x[i] + 3.0f) / 6.0f)); }
  1485. inline static void ggml_vec_hardsigmoid_f32 (const int n, float * y, const float * x) { for (int i = 0; i < n; ++i) y[i] = fminf(1.0f, fmaxf(0.0f, (x[i] + 3.0f) / 6.0f)); }
  1486. inline static void ggml_vec_exp_f32 (const int n, float * y, const float * x) { for (int i = 0; i < n; ++i) y[i] = expf(x[i]); }
  1487. static const float GELU_COEF_A = 0.044715f;
  1488. static const float GELU_QUICK_COEF = -1.702f;
  1489. static const float SQRT_2_OVER_PI = 0.79788456080286535587989211986876f;
  1490. inline static float ggml_gelu_f32(float x) {
  1491. return 0.5f*x*(1.0f + tanhf(SQRT_2_OVER_PI*x*(1.0f + GELU_COEF_A*x*x)));
  1492. }
  1493. inline static void ggml_vec_gelu_f16(const int n, ggml_fp16_t * y, const ggml_fp16_t * x) {
  1494. const uint16_t * i16 = (const uint16_t *) x;
  1495. for (int i = 0; i < n; ++i) {
  1496. y[i] = ggml_table_gelu_f16[i16[i]];
  1497. }
  1498. }
  1499. #ifdef GGML_GELU_FP16
  1500. inline static void ggml_vec_gelu_f32(const int n, float * y, const float * x) {
  1501. uint16_t t;
  1502. for (int i = 0; i < n; ++i) {
  1503. if (x[i] <= -10.0f) {
  1504. y[i] = 0.0f;
  1505. } else if (x[i] >= 10.0f) {
  1506. y[i] = x[i];
  1507. } else {
  1508. ggml_fp16_t fp16 = GGML_FP32_TO_FP16(x[i]);
  1509. memcpy(&t, &fp16, sizeof(uint16_t));
  1510. y[i] = GGML_FP16_TO_FP32(ggml_table_gelu_f16[t]);
  1511. }
  1512. }
  1513. }
  1514. #else
  1515. inline static void ggml_vec_gelu_f32(const int n, float * y, const float * x) {
  1516. for (int i = 0; i < n; ++i) {
  1517. y[i] = ggml_gelu_f32(x[i]);
  1518. }
  1519. }
  1520. #endif
  1521. inline static float ggml_gelu_quick_f32(float x) {
  1522. return x*(1.0f/(1.0f+expf(GELU_QUICK_COEF*x)));
  1523. }
  1524. //inline static void ggml_vec_gelu_quick_f16(const int n, ggml_fp16_t * y, const ggml_fp16_t * x) {
  1525. // const uint16_t * i16 = (const uint16_t *) x;
  1526. // for (int i = 0; i < n; ++i) {
  1527. // y[i] = ggml_table_gelu_quick_f16[i16[i]];
  1528. // }
  1529. //}
  1530. #ifdef GGML_GELU_QUICK_FP16
  1531. inline static void ggml_vec_gelu_quick_f32(const int n, float * y, const float * x) {
  1532. uint16_t t;
  1533. for (int i = 0; i < n; ++i) {
  1534. ggml_fp16_t fp16 = GGML_FP32_TO_FP16(x[i]);
  1535. memcpy(&t, &fp16, sizeof(uint16_t));
  1536. y[i] = GGML_FP16_TO_FP32(ggml_table_gelu_quick_f16[t]);
  1537. }
  1538. }
  1539. #else
  1540. inline static void ggml_vec_gelu_quick_f32(const int n, float * y, const float * x) {
  1541. for (int i = 0; i < n; ++i) {
  1542. y[i] = ggml_gelu_quick_f32(x[i]);
  1543. }
  1544. }
  1545. #endif
  1546. // Sigmoid Linear Unit (SiLU) function
  1547. inline static float ggml_silu_f32(float x) {
  1548. return x/(1.0f + expf(-x));
  1549. }
  1550. #if __FINITE_MATH_ONLY__
  1551. #error "some routines in ggml.c require non-finite math arithmetics -- pass -fno-finite-math-only to the compiler to fix"
  1552. #error "ref: https://github.com/ggerganov/llama.cpp/pull/7154#issuecomment-2143844461"
  1553. #endif
  1554. #if defined(__ARM_NEON) && defined(__aarch64__)
  1555. // adapted from arm limited optimized routine
  1556. // the maximum error is 1.45358 plus 0.5 ulps
  1557. // numbers above 88.38 will flush to infinity
  1558. // numbers beneath -103.97 will flush to zero
  1559. inline static float32x4_t ggml_v_expf(float32x4_t x) {
  1560. const float32x4_t r = vdupq_n_f32(0x1.8p23f);
  1561. const float32x4_t z = vfmaq_f32(r, x, vdupq_n_f32(0x1.715476p+0f));
  1562. const float32x4_t n = vsubq_f32(z, r);
  1563. const float32x4_t b = vfmsq_f32(vfmsq_f32(x, n, vdupq_n_f32(0x1.62e4p-1f)), n,
  1564. vdupq_n_f32(0x1.7f7d1cp-20f));
  1565. const uint32x4_t e = vshlq_n_u32(vreinterpretq_u32_f32(z), 23);
  1566. const float32x4_t k = vreinterpretq_f32_u32(vaddq_u32(e, vreinterpretq_u32_f32(vdupq_n_f32(1))));
  1567. const uint32x4_t c = vcagtq_f32(n, vdupq_n_f32(126));
  1568. const float32x4_t u = vmulq_f32(b, b);
  1569. const float32x4_t j = vfmaq_f32(
  1570. vmulq_f32(vdupq_n_f32(0x1.ffffecp-1f), b),
  1571. vfmaq_f32(vfmaq_f32(vdupq_n_f32(0x1.fffdb6p-2f), vdupq_n_f32(0x1.555e66p-3f), b),
  1572. vfmaq_f32(vdupq_n_f32(0x1.573e2ep-5f), vdupq_n_f32(0x1.0e4020p-7f), b), u), u);
  1573. if (!vpaddd_u64(vreinterpretq_u64_u32(c)))
  1574. return vfmaq_f32(k, j, k);
  1575. const uint32x4_t d = vandq_u32(vclezq_f32(n), vdupq_n_u32(0x82000000));
  1576. const float32x4_t s1 = vreinterpretq_f32_u32(vaddq_u32(d, vdupq_n_u32(0x7f000000)));
  1577. const float32x4_t s2 = vreinterpretq_f32_u32(vsubq_u32(e, d));
  1578. return vbslq_f32(vcagtq_f32(n, vdupq_n_f32(192)), vmulq_f32(s1, s1),
  1579. vbslq_f32(c, vmulq_f32(vfmaq_f32(s2, s2, j), s1), vfmaq_f32(k, k, j)));
  1580. }
  1581. // computes silu x/(1+exp(-x)) in single precision vector
  1582. inline static float32x4_t ggml_v_silu(float32x4_t x) {
  1583. const float32x4_t one = vdupq_n_f32(1.0f);
  1584. const float32x4_t zero = vdupq_n_f32(0.0f);
  1585. const float32x4_t neg_x = vsubq_f32(zero, x);
  1586. const float32x4_t exp_neg_x = ggml_v_expf(neg_x);
  1587. const float32x4_t one_plus_exp_neg_x = vaddq_f32(one, exp_neg_x);
  1588. return vdivq_f32(x, one_plus_exp_neg_x);
  1589. }
  1590. #elif defined(__AVX512F__) && defined(__AVX512DQ__)
  1591. // adapted from arm limited optimized routine
  1592. // the maximum error is 1.45358 plus 0.5 ulps
  1593. // numbers above 88.38 will flush to infinity
  1594. // numbers beneath -103.97 will flush to zero
  1595. inline static __m512 ggml_v_expf(__m512 x) {
  1596. const __m512 r = _mm512_set1_ps(0x1.8p23f);
  1597. const __m512 z = _mm512_fmadd_ps(x, _mm512_set1_ps(0x1.715476p+0f), r);
  1598. const __m512 n = _mm512_sub_ps(z, r);
  1599. const __m512 b =
  1600. _mm512_fnmadd_ps(n, _mm512_set1_ps(0x1.7f7d1cp-20f),
  1601. _mm512_fnmadd_ps(n, _mm512_set1_ps(0x1.62e4p-1f), x));
  1602. const __mmask16 d =
  1603. _mm512_cmp_ps_mask(_mm512_abs_ps(n), _mm512_set1_ps(192), _CMP_GT_OQ);
  1604. const __m512 u = _mm512_mul_ps(b, b);
  1605. const __m512 j = _mm512_fmadd_ps(
  1606. _mm512_fmadd_ps(_mm512_fmadd_ps(_mm512_set1_ps(0x1.0e4020p-7f), b,
  1607. _mm512_set1_ps(0x1.573e2ep-5f)),
  1608. u,
  1609. _mm512_fmadd_ps(_mm512_set1_ps(0x1.555e66p-3f), b,
  1610. _mm512_set1_ps(0x1.fffdb6p-2f))),
  1611. u,
  1612. _mm512_fmadd_ps(_mm512_set1_ps(0x1.ffffecp-1f), b, _mm512_set1_ps(1.0F)));
  1613. const __m512 res = _mm512_scalef_ps(j, n);
  1614. if (_mm512_kortestz(d, d))
  1615. return res;
  1616. const __m512 zero = _mm512_setzero_ps();
  1617. const __m512 alt = _mm512_mask_blend_ps(
  1618. _mm512_cmp_ps_mask(n, zero, _CMP_LE_OQ), _mm512_set1_ps(INFINITY), zero);
  1619. return _mm512_mask_blend_ps(d, res, alt);
  1620. }
  1621. // computes silu x/(1+exp(-x)) in single precision vector
  1622. inline static __m512 ggml_v_silu(__m512 x) {
  1623. const __m512 one = _mm512_set1_ps(1);
  1624. const __m512 zero = _mm512_setzero_ps();
  1625. const __m512 neg_x = _mm512_sub_ps(zero, x);
  1626. const __m512 exp_neg_x = ggml_v_expf(neg_x);
  1627. const __m512 one_plus_exp_neg_x = _mm512_add_ps(one, exp_neg_x);
  1628. return _mm512_div_ps(x, one_plus_exp_neg_x);
  1629. }
  1630. #elif defined(__AVX2__) && defined(__FMA__)
  1631. // adapted from arm limited optimized routine
  1632. // the maximum error is 1.45358 plus 0.5 ulps
  1633. // numbers above 88.38 will flush to infinity
  1634. // numbers beneath -103.97 will flush to zero
  1635. inline static __m256 ggml_v_expf(__m256 x) {
  1636. const __m256 r = _mm256_set1_ps(0x1.8p23f);
  1637. const __m256 z = _mm256_fmadd_ps(x, _mm256_set1_ps(0x1.715476p+0f), r);
  1638. const __m256 n = _mm256_sub_ps(z, r);
  1639. const __m256 b = _mm256_fnmadd_ps(n, _mm256_set1_ps(0x1.7f7d1cp-20f),
  1640. _mm256_fnmadd_ps(n, _mm256_set1_ps(0x1.62e4p-1f), x));
  1641. const __m256i e = _mm256_slli_epi32(_mm256_castps_si256(z), 23);
  1642. const __m256 k = _mm256_castsi256_ps(
  1643. _mm256_add_epi32(e, _mm256_castps_si256(_mm256_set1_ps(1))));
  1644. const __m256i c = _mm256_castps_si256(
  1645. _mm256_cmp_ps(_mm256_andnot_ps(_mm256_set1_ps(-0.f), n),
  1646. _mm256_set1_ps(126), _CMP_GT_OQ));
  1647. const __m256 u = _mm256_mul_ps(b, b);
  1648. const __m256 j = _mm256_fmadd_ps(_mm256_fmadd_ps(_mm256_fmadd_ps(_mm256_set1_ps(0x1.0e4020p-7f), b,
  1649. _mm256_set1_ps(0x1.573e2ep-5f)), u,
  1650. _mm256_fmadd_ps(_mm256_set1_ps(0x1.555e66p-3f), b,
  1651. _mm256_set1_ps(0x1.fffdb6p-2f))),
  1652. u, _mm256_mul_ps(_mm256_set1_ps(0x1.ffffecp-1f), b));
  1653. if (!_mm256_movemask_ps(_mm256_castsi256_ps(c)))
  1654. return _mm256_fmadd_ps(j, k, k);
  1655. const __m256i g = _mm256_and_si256(
  1656. _mm256_castps_si256(_mm256_cmp_ps(n, _mm256_setzero_ps(), _CMP_LE_OQ)),
  1657. _mm256_set1_epi32(0x82000000u));
  1658. const __m256 s1 =
  1659. _mm256_castsi256_ps(_mm256_add_epi32(g, _mm256_set1_epi32(0x7f000000u)));
  1660. const __m256 s2 = _mm256_castsi256_ps(_mm256_sub_epi32(e, g));
  1661. const __m256i d = _mm256_castps_si256(
  1662. _mm256_cmp_ps(_mm256_andnot_ps(_mm256_set1_ps(-0.f), n),
  1663. _mm256_set1_ps(192), _CMP_GT_OQ));
  1664. return _mm256_or_ps(
  1665. _mm256_and_ps(_mm256_castsi256_ps(d), _mm256_mul_ps(s1, s1)),
  1666. _mm256_andnot_ps(
  1667. _mm256_castsi256_ps(d),
  1668. _mm256_or_ps(
  1669. _mm256_and_ps(_mm256_castsi256_ps(c),
  1670. _mm256_mul_ps(_mm256_fmadd_ps(s2, j, s2), s1)),
  1671. _mm256_andnot_ps(_mm256_castsi256_ps(c), _mm256_fmadd_ps(k, j, k)))));
  1672. }
  1673. // computes silu x/(1+exp(-x)) in single precision vector
  1674. inline static __m256 ggml_v_silu(__m256 x) {
  1675. const __m256 one = _mm256_set1_ps(1);
  1676. const __m256 zero = _mm256_setzero_ps();
  1677. const __m256 neg_x = _mm256_sub_ps(zero, x);
  1678. const __m256 exp_neg_x = ggml_v_expf(neg_x);
  1679. const __m256 one_plus_exp_neg_x = _mm256_add_ps(one, exp_neg_x);
  1680. return _mm256_div_ps(x, one_plus_exp_neg_x);
  1681. }
  1682. #elif defined(__SSE2__) // __AVX2__ / __ARM_NEON
  1683. #if defined(__FMA__)
  1684. #define MADD128(x, y, z) _mm_fmadd_ps(x, y, z)
  1685. #define NMADD128(x, y, z) _mm_fnmadd_ps(x, y, z)
  1686. #else
  1687. #define MADD128(x, y, z) _mm_add_ps(_mm_mul_ps(x, y), z)
  1688. #define NMADD128(x, y, z) _mm_sub_ps(z, _mm_mul_ps(x, y))
  1689. #endif
  1690. // adapted from arm limited optimized routine
  1691. // the maximum error is 1.45358 plus 0.5 ulps
  1692. // numbers above 88.38 will flush to infinity
  1693. // numbers beneath -103.97 will flush to zero
  1694. inline static __m128 ggml_v_expf(__m128 x) {
  1695. const __m128 r = _mm_set1_ps(0x1.8p23f);
  1696. const __m128 z = MADD128(x, _mm_set1_ps(0x1.715476p+0f), r);
  1697. const __m128 n = _mm_sub_ps(z, r);
  1698. const __m128 b =
  1699. NMADD128(n, _mm_set1_ps(0x1.7f7d1cp-20f), NMADD128(n, _mm_set1_ps(0x1.62e4p-1f), x));
  1700. const __m128i e = _mm_slli_epi32(_mm_castps_si128(z), 23);
  1701. const __m128 k = _mm_castsi128_ps(_mm_add_epi32(e, _mm_castps_si128(_mm_set1_ps(1))));
  1702. const __m128i c =
  1703. _mm_castps_si128(_mm_cmpgt_ps(_mm_andnot_ps(_mm_set1_ps(-0.f), n), _mm_set1_ps(126)));
  1704. const __m128 u = _mm_mul_ps(b, b);
  1705. const __m128 j =
  1706. MADD128(MADD128(MADD128(_mm_set1_ps(0x1.0e4020p-7f), b, _mm_set1_ps(0x1.573e2ep-5f)), u,
  1707. MADD128(_mm_set1_ps(0x1.555e66p-3f), b, _mm_set1_ps(0x1.fffdb6p-2f))),
  1708. u, _mm_mul_ps(_mm_set1_ps(0x1.ffffecp-1f), b));
  1709. if (!_mm_movemask_epi8(c))
  1710. return MADD128(j, k, k);
  1711. const __m128i g = _mm_and_si128(_mm_castps_si128(_mm_cmple_ps(n, _mm_setzero_ps())),
  1712. _mm_set1_epi32(0x82000000u));
  1713. const __m128 s1 = _mm_castsi128_ps(_mm_add_epi32(g, _mm_set1_epi32(0x7f000000u)));
  1714. const __m128 s2 = _mm_castsi128_ps(_mm_sub_epi32(e, g));
  1715. const __m128i d =
  1716. _mm_castps_si128(_mm_cmpgt_ps(_mm_andnot_ps(_mm_set1_ps(-0.f), n), _mm_set1_ps(192)));
  1717. return _mm_or_ps(
  1718. _mm_and_ps(_mm_castsi128_ps(d), _mm_mul_ps(s1, s1)),
  1719. _mm_andnot_ps(_mm_castsi128_ps(d),
  1720. _mm_or_ps(_mm_and_ps(_mm_castsi128_ps(c), _mm_mul_ps(MADD128(s2, j, s2), s1)),
  1721. _mm_andnot_ps(_mm_castsi128_ps(c), MADD128(k, j, k)))));
  1722. }
  1723. // computes silu x/(1+exp(-x)) in single precision vector
  1724. inline static __m128 ggml_v_silu(__m128 x) {
  1725. const __m128 one = _mm_set1_ps(1);
  1726. const __m128 zero = _mm_setzero_ps();
  1727. const __m128 neg_x = _mm_sub_ps(zero, x);
  1728. const __m128 exp_neg_x = ggml_v_expf(neg_x);
  1729. const __m128 one_plus_exp_neg_x = _mm_add_ps(one, exp_neg_x);
  1730. return _mm_div_ps(x, one_plus_exp_neg_x);
  1731. }
  1732. #endif // __ARM_NEON / __AVX2__ / __SSE2__
  1733. static void ggml_vec_silu_f32(const int n, float * y, const float * x) {
  1734. int i = 0;
  1735. #if defined(__AVX512F__) && defined(__AVX512DQ__)
  1736. for (; i + 15 < n; i += 16) {
  1737. _mm512_storeu_ps(y + i, ggml_v_silu(_mm512_loadu_ps(x + i)));
  1738. }
  1739. #elif defined(__AVX2__) && defined(__FMA__)
  1740. for (; i + 7 < n; i += 8) {
  1741. _mm256_storeu_ps(y + i, ggml_v_silu(_mm256_loadu_ps(x + i)));
  1742. }
  1743. #elif defined(__SSE2__)
  1744. for (; i + 3 < n; i += 4) {
  1745. _mm_storeu_ps(y + i, ggml_v_silu(_mm_loadu_ps(x + i)));
  1746. }
  1747. #elif defined(__ARM_NEON) && defined(__aarch64__)
  1748. for (; i + 3 < n; i += 4) {
  1749. vst1q_f32(y + i, ggml_v_silu(vld1q_f32(x + i)));
  1750. }
  1751. #endif
  1752. for (; i < n; ++i) {
  1753. y[i] = ggml_silu_f32(x[i]);
  1754. }
  1755. }
  1756. static ggml_float ggml_vec_soft_max_f32(const int n, float * y, const float * x, float max) {
  1757. int i = 0;
  1758. ggml_float sum = 0;
  1759. #if defined(__AVX512F__) && defined(__AVX512DQ__)
  1760. for (; i + 15 < n; i += 16) {
  1761. __m512 val = ggml_v_expf(_mm512_sub_ps(_mm512_loadu_ps(x + i),
  1762. _mm512_set1_ps(max)));
  1763. _mm512_storeu_ps(y + i, val);
  1764. sum += (ggml_float)_mm512_reduce_add_ps(val);
  1765. }
  1766. #elif defined(__AVX2__) && defined(__FMA__)
  1767. for (; i + 7 < n; i += 8) {
  1768. __m256 val = ggml_v_expf(_mm256_sub_ps(_mm256_loadu_ps(x + i),
  1769. _mm256_set1_ps(max)));
  1770. _mm256_storeu_ps(y + i, val);
  1771. __m128 val2 = _mm_add_ps(_mm256_extractf128_ps(val, 1),
  1772. _mm256_castps256_ps128(val));
  1773. val2 = _mm_add_ps(val2, _mm_movehl_ps(val2, val2));
  1774. val2 = _mm_add_ss(val2, _mm_movehdup_ps(val2));
  1775. sum += (ggml_float)_mm_cvtss_f32(val2);
  1776. }
  1777. #elif defined(__SSE2__)
  1778. for (; i + 3 < n; i += 4) {
  1779. __m128 val = ggml_v_expf(_mm_sub_ps(_mm_loadu_ps(x + i),
  1780. _mm_set1_ps(max)));
  1781. _mm_storeu_ps(y + i, val);
  1782. #if defined(__AVX__) || defined(__AVX2__) || defined(__AVX512F__)
  1783. val = _mm_add_ps(val, _mm_movehl_ps(val, val));
  1784. val = _mm_add_ss(val, _mm_movehdup_ps(val));
  1785. #else
  1786. __m128 tmp = _mm_shuffle_ps(val, val, _MM_SHUFFLE(2, 3, 0, 1));
  1787. val = _mm_add_ps(val, tmp);
  1788. tmp = _mm_movehl_ps(tmp, val);
  1789. val = _mm_add_ss(val, tmp);
  1790. #endif
  1791. sum += (ggml_float)_mm_cvtss_f32(val);
  1792. }
  1793. #elif defined(__ARM_NEON) && defined(__aarch64__)
  1794. for (; i + 3 < n; i += 4) {
  1795. float32x4_t val = ggml_v_expf(vsubq_f32(vld1q_f32(x + i),
  1796. vdupq_n_f32(max)));
  1797. vst1q_f32(y + i, val);
  1798. sum += (ggml_float)vaddvq_f32(val);
  1799. }
  1800. #endif
  1801. for (; i < n; ++i) {
  1802. float val = expf(x[i] - max);
  1803. sum += (ggml_float)val;
  1804. y[i] = val;
  1805. }
  1806. return sum;
  1807. }
  1808. static ggml_float ggml_vec_log_soft_max_f32(const int n, float * y, const float * x, float max) {
  1809. // log(soft_max) = log(soft_max_i / soft_max_sum) = log(soft_max_i) - log(soft_max_sum) = (logit_i - max) - log(soft_max_i)
  1810. int i = 0;
  1811. ggml_float sum = 0;
  1812. for (; i < n; ++i) {
  1813. float val = x[i] - max;
  1814. y[i] = val;
  1815. sum += (ggml_float)expf(val);
  1816. }
  1817. return sum = (ggml_float)logf(sum);
  1818. }
  1819. inline static float ggml_silu_backward_f32(float x, float dy) {
  1820. const float s = 1.0f/(1.0f + expf(-x));
  1821. return dy*s*(1.0f + x*(1.0f - s));
  1822. }
  1823. inline static void ggml_vec_silu_backward_f32(const int n, float * dx, const float * x, const float * dy) {
  1824. for (int i = 0; i < n; ++i) {
  1825. dx[i] = ggml_silu_backward_f32(x[i], dy[i]);
  1826. }
  1827. }
  1828. inline static void ggml_vec_sum_f32(const int n, float * s, const float * x) {
  1829. #ifndef GGML_USE_ACCELERATE
  1830. ggml_float sum = 0.0;
  1831. for (int i = 0; i < n; ++i) {
  1832. sum += (ggml_float)x[i];
  1833. }
  1834. *s = sum;
  1835. #else
  1836. vDSP_sve(x, 1, s, n);
  1837. #endif
  1838. }
  1839. inline static void ggml_vec_sum_f32_ggf(const int n, ggml_float * s, const float * x) {
  1840. ggml_float sum = 0.0;
  1841. for (int i = 0; i < n; ++i) {
  1842. sum += (ggml_float)x[i];
  1843. }
  1844. *s = sum;
  1845. }
  1846. inline static void ggml_vec_sum_f16_ggf(const int n, float * s, const ggml_fp16_t * x) {
  1847. float sum = 0.0f;
  1848. for (int i = 0; i < n; ++i) {
  1849. sum += GGML_FP16_TO_FP32(x[i]);
  1850. }
  1851. *s = sum;
  1852. }
  1853. inline static void ggml_vec_sum_bf16_ggf(const int n, float * s, const ggml_bf16_t * x) {
  1854. float sum = 0.0f;
  1855. for (int i = 0; i < n; ++i) {
  1856. sum += GGML_BF16_TO_FP32(x[i]);
  1857. }
  1858. *s = sum;
  1859. }
  1860. inline static void ggml_vec_max_f32(const int n, float * s, const float * x) {
  1861. #ifndef GGML_USE_ACCELERATE
  1862. float max = -INFINITY;
  1863. for (int i = 0; i < n; ++i) {
  1864. max = MAX(max, x[i]);
  1865. }
  1866. *s = max;
  1867. #else
  1868. vDSP_maxv(x, 1, s, n);
  1869. #endif
  1870. }
  1871. inline static void ggml_vec_norm_inv_f32(const int n, float * s, const float * x) {
  1872. ggml_vec_norm_f32(n, s, x);
  1873. *s = 1.f/(*s);
  1874. }
  1875. inline static void ggml_vec_argmax_f32(const int n, int * s, const float * x) {
  1876. float max = -INFINITY;
  1877. int idx = 0;
  1878. for (int i = 0; i < n; ++i) {
  1879. max = MAX(max, x[i]);
  1880. if (max == x[i]) { idx = i; }
  1881. }
  1882. *s = idx;
  1883. }
  1884. // Helpers for polling loops
  1885. #if defined(__aarch64__) && ( defined(__clang__) || defined(__GNUC__) )
  1886. static inline void ggml_thread_cpu_relax(void) {
  1887. __asm__ volatile("yield" ::: "memory");
  1888. }
  1889. #elif defined(__x86_64__)
  1890. static inline void ggml_thread_cpu_relax(void) {
  1891. _mm_pause();
  1892. }
  1893. #else
  1894. static inline void ggml_thread_cpu_relax(void) {;}
  1895. #endif
  1896. //
  1897. // NUMA support
  1898. //
  1899. #define GGML_NUMA_MAX_NODES 8
  1900. #define GGML_NUMA_MAX_CPUS 512
  1901. struct ggml_numa_node {
  1902. uint32_t cpus[GGML_NUMA_MAX_CPUS]; // hardware threads on this node
  1903. uint32_t n_cpus;
  1904. };
  1905. struct ggml_numa_nodes {
  1906. enum ggml_numa_strategy numa_strategy;
  1907. struct ggml_numa_node nodes[GGML_NUMA_MAX_NODES];
  1908. uint32_t n_nodes;
  1909. uint32_t total_cpus; // hardware threads on system
  1910. uint32_t current_node; // node on which main process is execting
  1911. #if defined(__gnu_linux__)
  1912. cpu_set_t cpuset; // cpuset from numactl
  1913. #else
  1914. uint32_t cpuset; // no NUMA support outside of Linux at this time. Use a portable datatype
  1915. #endif
  1916. };
  1917. //
  1918. // ggml state
  1919. //
  1920. struct ggml_state {
  1921. struct ggml_numa_nodes numa;
  1922. };
  1923. static struct ggml_state g_state = {0};
  1924. void ggml_barrier(struct ggml_threadpool * tp) {
  1925. int n_threads = atomic_load_explicit(&tp->n_threads_cur, memory_order_relaxed);
  1926. if (n_threads == 1) {
  1927. return;
  1928. }
  1929. #ifdef GGML_USE_OPENMP
  1930. #pragma omp barrier
  1931. #else
  1932. int n_passed = atomic_load_explicit(&tp->n_barrier_passed, memory_order_relaxed);
  1933. // enter barrier (full seq-cst fence)
  1934. int n_barrier = atomic_fetch_add_explicit(&tp->n_barrier, 1, memory_order_seq_cst);
  1935. if (n_barrier == (n_threads - 1)) {
  1936. // last thread
  1937. atomic_store_explicit(&tp->n_barrier, 0, memory_order_relaxed);
  1938. // exit barrier (fill seq-cst fence)
  1939. atomic_fetch_add_explicit(&tp->n_barrier_passed, 1, memory_order_seq_cst);
  1940. return;
  1941. }
  1942. // wait for other threads
  1943. while (atomic_load_explicit(&tp->n_barrier_passed, memory_order_relaxed) == n_passed) {
  1944. ggml_thread_cpu_relax();
  1945. }
  1946. // exit barrier (full seq-cst fence)
  1947. // TSAN doesn't support standalone fence yet, we use a dummy read-modify-write instead
  1948. #ifdef GGML_TSAN_ENABLED
  1949. atomic_fetch_add_explicit(&tp->n_barrier_passed, 0, memory_order_seq_cst);
  1950. #else
  1951. atomic_thread_fence(memory_order_seq_cst);
  1952. #endif
  1953. #endif
  1954. }
  1955. #if defined(__gnu_linux__)
  1956. static cpu_set_t ggml_get_numa_affinity(void) {
  1957. cpu_set_t cpuset;
  1958. pthread_t thread;
  1959. thread = pthread_self();
  1960. CPU_ZERO(&cpuset);
  1961. pthread_getaffinity_np(thread, sizeof(cpu_set_t), &cpuset);
  1962. return cpuset;
  1963. }
  1964. #else
  1965. static uint32_t ggml_get_numa_affinity(void) {
  1966. return 0; // no NUMA support
  1967. }
  1968. #endif
  1969. void ggml_numa_init(enum ggml_numa_strategy numa_flag) {
  1970. if (g_state.numa.n_nodes > 0) {
  1971. fprintf(stderr, "ggml_numa_init: NUMA already initialized\n");
  1972. return;
  1973. }
  1974. #if defined(__gnu_linux__)
  1975. struct stat st;
  1976. char path[256];
  1977. int rv;
  1978. // set numa scheme
  1979. g_state.numa.numa_strategy = numa_flag;
  1980. GGML_PRINT_DEBUG("numa strategy %u\n",g_state.numa.numa_strategy);
  1981. g_state.numa.cpuset = ggml_get_numa_affinity();
  1982. // enumerate nodes
  1983. while (g_state.numa.n_nodes < GGML_NUMA_MAX_NODES) {
  1984. rv = snprintf(path, sizeof(path), "/sys/devices/system/node/node%u", g_state.numa.n_nodes);
  1985. GGML_ASSERT(rv > 0 && (unsigned)rv < sizeof(path));
  1986. if (stat(path, &st) != 0) { break; }
  1987. ++g_state.numa.n_nodes;
  1988. }
  1989. // enumerate CPUs
  1990. while (g_state.numa.total_cpus < GGML_NUMA_MAX_CPUS) {
  1991. rv = snprintf(path, sizeof(path), "/sys/devices/system/cpu/cpu%u", g_state.numa.total_cpus);
  1992. GGML_ASSERT(rv > 0 && (unsigned)rv < sizeof(path));
  1993. if (stat(path, &st) != 0) { break; }
  1994. ++g_state.numa.total_cpus;
  1995. }
  1996. GGML_PRINT_DEBUG("found %u numa nodes, %u CPUs\n", g_state.numa.n_nodes, g_state.numa.total_cpus);
  1997. // figure out which node we're on
  1998. uint current_cpu;
  1999. int getcpu_ret = 0;
  2000. #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 33) || defined(__COSMOPOLITAN__)
  2001. getcpu_ret = getcpu(&current_cpu, &g_state.numa.current_node);
  2002. #else
  2003. // old glibc doesn't have a wrapper for this call. Fall back on direct syscall
  2004. # if !defined(SYS_getcpu) && defined(SYS_get_cpu)
  2005. # define SYS_getcpu SYS_get_cpu // some older glibc versions use this name
  2006. # endif
  2007. getcpu_ret = syscall(SYS_getcpu, &current_cpu, &g_state.numa.current_node);
  2008. #endif
  2009. if (g_state.numa.n_nodes < 1 || g_state.numa.total_cpus < 1 || getcpu_ret != 0) {
  2010. g_state.numa.n_nodes = 0;
  2011. return;
  2012. }
  2013. GGML_PRINT_DEBUG("found our process on numa node %u, CPU %u\n", g_state.numa.current_node, current_cpu);
  2014. for (uint32_t n = 0; n < g_state.numa.n_nodes; ++n) {
  2015. struct ggml_numa_node * node = &g_state.numa.nodes[n];
  2016. GGML_PRINT_DEBUG("CPUs on node %u:", n);
  2017. node->n_cpus = 0;
  2018. for (uint32_t c = 0; c < g_state.numa.total_cpus; ++c) {
  2019. rv = snprintf(path, sizeof(path), "/sys/devices/system/node/node%u/cpu%u", n, c);
  2020. GGML_ASSERT(rv > 0 && (unsigned)rv < sizeof(path));
  2021. if (stat(path, &st) == 0) {
  2022. node->cpus[node->n_cpus++] = c;
  2023. GGML_PRINT_DEBUG(" %u", c);
  2024. }
  2025. }
  2026. GGML_PRINT_DEBUG("\n");
  2027. }
  2028. if (ggml_is_numa()) {
  2029. FILE *fptr = fopen("/proc/sys/kernel/numa_balancing", "r");
  2030. if (fptr != NULL) {
  2031. char buf[42];
  2032. if (fgets(buf, sizeof(buf), fptr) && strncmp(buf, "0\n", sizeof(buf)) != 0) {
  2033. GGML_LOG_WARN("/proc/sys/kernel/numa_balancing is enabled, this has been observed to impair performance\n");
  2034. }
  2035. fclose(fptr);
  2036. }
  2037. }
  2038. #else
  2039. UNUSED(numa_flag);
  2040. // TODO
  2041. #endif
  2042. }
  2043. bool ggml_is_numa(void) {
  2044. return g_state.numa.n_nodes > 1;
  2045. }
  2046. #if defined(__ARM_ARCH)
  2047. #if defined(__linux__) && defined(__aarch64__)
  2048. #include <sys/auxv.h>
  2049. #elif defined(__APPLE__)
  2050. #include <sys/sysctl.h>
  2051. #endif
  2052. #if !defined(HWCAP2_I8MM)
  2053. #define HWCAP2_I8MM (1 << 13)
  2054. #endif
  2055. static void ggml_init_arm_arch_features(void) {
  2056. #if defined(__linux__) && defined(__aarch64__)
  2057. uint32_t hwcap = getauxval(AT_HWCAP);
  2058. uint32_t hwcap2 = getauxval(AT_HWCAP2);
  2059. ggml_arm_arch_features.has_neon = !!(hwcap & HWCAP_ASIMD);
  2060. ggml_arm_arch_features.has_dotprod = !!(hwcap & HWCAP_ASIMDDP);
  2061. ggml_arm_arch_features.has_i8mm = !!(hwcap2 & HWCAP2_I8MM);
  2062. ggml_arm_arch_features.has_sve = !!(hwcap & HWCAP_SVE);
  2063. #if defined(__ARM_FEATURE_SVE)
  2064. ggml_arm_arch_features.sve_cnt = PR_SVE_VL_LEN_MASK & prctl(PR_SVE_GET_VL);
  2065. #endif
  2066. #elif defined(__APPLE__)
  2067. int oldp = 0;
  2068. size_t size = sizeof(oldp);
  2069. if (sysctlbyname("hw.optional.AdvSIMD", &oldp, &size, NULL, 0) != 0) {
  2070. oldp = 0;
  2071. }
  2072. ggml_arm_arch_features.has_neon = oldp;
  2073. if (sysctlbyname("hw.optional.arm.FEAT_DotProd", &oldp, &size, NULL, 0) != 0) {
  2074. oldp = 0;
  2075. }
  2076. ggml_arm_arch_features.has_dotprod = oldp;
  2077. if (sysctlbyname("hw.optional.arm.FEAT_I8MM", &oldp, &size, NULL, 0) != 0) {
  2078. oldp = 0;
  2079. }
  2080. ggml_arm_arch_features.has_i8mm = oldp;
  2081. ggml_arm_arch_features.has_sve = 0;
  2082. ggml_arm_arch_features.sve_cnt = 0;
  2083. #else
  2084. // Run-time CPU feature detection not implemented for this platform, fallback to compile time
  2085. #if defined(__ARM_NEON)
  2086. ggml_arm_arch_features.has_neon = 1;
  2087. #else
  2088. ggml_arm_arch_features.has_neon = 0;
  2089. #endif
  2090. #if defined(__ARM_FEATURE_MATMUL_INT8)
  2091. ggml_arm_arch_features.has_i8mm = 1;
  2092. #else
  2093. ggml_arm_arch_features.has_i8mm = 0;
  2094. #endif
  2095. #if defined(__ARM_FEATURE_SVE)
  2096. ggml_arm_arch_features.has_sve = 1;
  2097. ggml_arm_arch_features.sve_cnt = 16;
  2098. #else
  2099. ggml_arm_arch_features.has_sve = 0;
  2100. ggml_arm_arch_features.sve_cnt = 0;
  2101. #endif
  2102. #endif
  2103. }
  2104. #endif
  2105. struct ggml_tensor * ggml_new_i32(struct ggml_context * ctx, int32_t value) {
  2106. GGML_ASSERT(!ggml_get_no_alloc(ctx));
  2107. struct ggml_tensor * result = ggml_new_tensor_1d(ctx, GGML_TYPE_I32, 1);
  2108. ggml_set_i32(result, value);
  2109. return result;
  2110. }
  2111. struct ggml_tensor * ggml_new_f32(struct ggml_context * ctx, float value) {
  2112. GGML_ASSERT(!ggml_get_no_alloc(ctx));
  2113. struct ggml_tensor * result = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, 1);
  2114. ggml_set_f32(result, value);
  2115. return result;
  2116. }
  2117. struct ggml_tensor * ggml_set_i32 (struct ggml_tensor * tensor, int32_t value) {
  2118. const int n = ggml_nrows(tensor);
  2119. const int nc = tensor->ne[0];
  2120. const size_t n1 = tensor->nb[1];
  2121. char * const data = tensor->data;
  2122. switch (tensor->type) {
  2123. case GGML_TYPE_I8:
  2124. {
  2125. assert(tensor->nb[0] == sizeof(int8_t));
  2126. for (int i = 0; i < n; i++) {
  2127. ggml_vec_set_i8(nc, (int8_t *)(data + i*n1), value);
  2128. }
  2129. } break;
  2130. case GGML_TYPE_I16:
  2131. {
  2132. assert(tensor->nb[0] == sizeof(int16_t));
  2133. for (int i = 0; i < n; i++) {
  2134. ggml_vec_set_i16(nc, (int16_t *)(data + i*n1), value);
  2135. }
  2136. } break;
  2137. case GGML_TYPE_I32:
  2138. {
  2139. assert(tensor->nb[0] == sizeof(int32_t));
  2140. for (int i = 0; i < n; i++) {
  2141. ggml_vec_set_i32(nc, (int32_t *)(data + i*n1), value);
  2142. }
  2143. } break;
  2144. case GGML_TYPE_F16:
  2145. {
  2146. assert(tensor->nb[0] == sizeof(ggml_fp16_t));
  2147. for (int i = 0; i < n; i++) {
  2148. ggml_vec_set_f16(nc, (ggml_fp16_t *)(data + i*n1), GGML_FP32_TO_FP16(value));
  2149. }
  2150. } break;
  2151. case GGML_TYPE_BF16:
  2152. {
  2153. assert(tensor->nb[0] == sizeof(ggml_fp16_t));
  2154. for (int i = 0; i < n; i++) {
  2155. ggml_vec_set_bf16(nc, (ggml_bf16_t *)(data + i*n1), GGML_FP32_TO_BF16(value));
  2156. }
  2157. } break;
  2158. case GGML_TYPE_F32:
  2159. {
  2160. assert(tensor->nb[0] == sizeof(float));
  2161. for (int i = 0; i < n; i++) {
  2162. ggml_vec_set_f32(nc, (float *)(data + i*n1), value);
  2163. }
  2164. } break;
  2165. default:
  2166. {
  2167. GGML_ABORT("fatal error");
  2168. }
  2169. }
  2170. return tensor;
  2171. }
  2172. struct ggml_tensor * ggml_set_f32(struct ggml_tensor * tensor, float value) {
  2173. const int n = ggml_nrows(tensor);
  2174. const int nc = tensor->ne[0];
  2175. const size_t n1 = tensor->nb[1];
  2176. char * const data = tensor->data;
  2177. switch (tensor->type) {
  2178. case GGML_TYPE_I8:
  2179. {
  2180. assert(tensor->nb[0] == sizeof(int8_t));
  2181. for (int i = 0; i < n; i++) {
  2182. ggml_vec_set_i8(nc, (int8_t *)(data + i*n1), value);
  2183. }
  2184. } break;
  2185. case GGML_TYPE_I16:
  2186. {
  2187. assert(tensor->nb[0] == sizeof(int16_t));
  2188. for (int i = 0; i < n; i++) {
  2189. ggml_vec_set_i16(nc, (int16_t *)(data + i*n1), value);
  2190. }
  2191. } break;
  2192. case GGML_TYPE_I32:
  2193. {
  2194. assert(tensor->nb[0] == sizeof(int32_t));
  2195. for (int i = 0; i < n; i++) {
  2196. ggml_vec_set_i32(nc, (int32_t *)(data + i*n1), value);
  2197. }
  2198. } break;
  2199. case GGML_TYPE_F16:
  2200. {
  2201. assert(tensor->nb[0] == sizeof(ggml_fp16_t));
  2202. for (int i = 0; i < n; i++) {
  2203. ggml_vec_set_f16(nc, (ggml_fp16_t *)(data + i*n1), GGML_FP32_TO_FP16(value));
  2204. }
  2205. } break;
  2206. case GGML_TYPE_BF16:
  2207. {
  2208. assert(tensor->nb[0] == sizeof(ggml_bf16_t));
  2209. for (int i = 0; i < n; i++) {
  2210. ggml_vec_set_bf16(nc, (ggml_bf16_t *)(data + i*n1), GGML_FP32_TO_BF16(value));
  2211. }
  2212. } break;
  2213. case GGML_TYPE_F32:
  2214. {
  2215. assert(tensor->nb[0] == sizeof(float));
  2216. for (int i = 0; i < n; i++) {
  2217. ggml_vec_set_f32(nc, (float *)(data + i*n1), value);
  2218. }
  2219. } break;
  2220. default:
  2221. {
  2222. GGML_ABORT("fatal error");
  2223. }
  2224. }
  2225. return tensor;
  2226. }
  2227. int32_t ggml_get_i32_1d(const struct ggml_tensor * tensor, int i) {
  2228. if (!ggml_is_contiguous(tensor)) {
  2229. int64_t id[4] = { 0, 0, 0, 0 };
  2230. ggml_unravel_index(tensor, i, &id[0], &id[1], &id[2], &id[3]);
  2231. return ggml_get_i32_nd(tensor, id[0], id[1], id[2], id[3]);
  2232. }
  2233. switch (tensor->type) {
  2234. case GGML_TYPE_I8:
  2235. {
  2236. GGML_ASSERT(tensor->nb[0] == sizeof(int8_t));
  2237. return ((int8_t *)(tensor->data))[i];
  2238. }
  2239. case GGML_TYPE_I16:
  2240. {
  2241. GGML_ASSERT(tensor->nb[0] == sizeof(int16_t));
  2242. return ((int16_t *)(tensor->data))[i];
  2243. }
  2244. case GGML_TYPE_I32:
  2245. {
  2246. GGML_ASSERT(tensor->nb[0] == sizeof(int32_t));
  2247. return ((int32_t *)(tensor->data))[i];
  2248. }
  2249. case GGML_TYPE_F16:
  2250. {
  2251. GGML_ASSERT(tensor->nb[0] == sizeof(ggml_fp16_t));
  2252. return GGML_FP16_TO_FP32(((ggml_fp16_t *)(tensor->data))[i]);
  2253. }
  2254. case GGML_TYPE_BF16:
  2255. {
  2256. GGML_ASSERT(tensor->nb[0] == sizeof(ggml_bf16_t));
  2257. return GGML_BF16_TO_FP32(((ggml_bf16_t *)(tensor->data))[i]);
  2258. }
  2259. case GGML_TYPE_F32:
  2260. {
  2261. GGML_ASSERT(tensor->nb[0] == sizeof(float));
  2262. return ((float *)(tensor->data))[i];
  2263. }
  2264. default:
  2265. {
  2266. GGML_ABORT("fatal error");
  2267. }
  2268. }
  2269. }
  2270. void ggml_set_i32_1d(const struct ggml_tensor * tensor, int i, int32_t value) {
  2271. if (!ggml_is_contiguous(tensor)) {
  2272. int64_t id[4] = { 0, 0, 0, 0 };
  2273. ggml_unravel_index(tensor, i, &id[0], &id[1], &id[2], &id[3]);
  2274. ggml_set_i32_nd(tensor, id[0], id[1], id[2], id[3], value);
  2275. return;
  2276. }
  2277. switch (tensor->type) {
  2278. case GGML_TYPE_I8:
  2279. {
  2280. GGML_ASSERT(tensor->nb[0] == sizeof(int8_t));
  2281. ((int8_t *)(tensor->data))[i] = value;
  2282. } break;
  2283. case GGML_TYPE_I16:
  2284. {
  2285. GGML_ASSERT(tensor->nb[0] == sizeof(int16_t));
  2286. ((int16_t *)(tensor->data))[i] = value;
  2287. } break;
  2288. case GGML_TYPE_I32:
  2289. {
  2290. GGML_ASSERT(tensor->nb[0] == sizeof(int32_t));
  2291. ((int32_t *)(tensor->data))[i] = value;
  2292. } break;
  2293. case GGML_TYPE_F16:
  2294. {
  2295. GGML_ASSERT(tensor->nb[0] == sizeof(ggml_fp16_t));
  2296. ((ggml_fp16_t *)(tensor->data))[i] = GGML_FP32_TO_FP16(value);
  2297. } break;
  2298. case GGML_TYPE_BF16:
  2299. {
  2300. GGML_ASSERT(tensor->nb[0] == sizeof(ggml_bf16_t));
  2301. ((ggml_bf16_t *)(tensor->data))[i] = GGML_FP32_TO_BF16(value);
  2302. } break;
  2303. case GGML_TYPE_F32:
  2304. {
  2305. GGML_ASSERT(tensor->nb[0] == sizeof(float));
  2306. ((float *)(tensor->data))[i] = value;
  2307. } break;
  2308. default:
  2309. {
  2310. GGML_ABORT("fatal error");
  2311. }
  2312. }
  2313. }
  2314. int32_t ggml_get_i32_nd(const struct ggml_tensor * tensor, int i0, int i1, int i2, int i3) {
  2315. void * data = (char *) tensor->data + i0*tensor->nb[0] + i1*tensor->nb[1] + i2*tensor->nb[2] + i3*tensor->nb[3];
  2316. switch (tensor->type) {
  2317. case GGML_TYPE_I8:
  2318. return ((int8_t *) data)[0];
  2319. case GGML_TYPE_I16:
  2320. return ((int16_t *) data)[0];
  2321. case GGML_TYPE_I32:
  2322. return ((int32_t *) data)[0];
  2323. case GGML_TYPE_F16:
  2324. return GGML_FP16_TO_FP32(((ggml_fp16_t *) data)[0]);
  2325. case GGML_TYPE_BF16:
  2326. return GGML_BF16_TO_FP32(((ggml_bf16_t *) data)[0]);
  2327. case GGML_TYPE_F32:
  2328. return ((float *) data)[0];
  2329. default:
  2330. GGML_ABORT("fatal error");
  2331. }
  2332. }
  2333. void ggml_set_i32_nd(const struct ggml_tensor * tensor, int i0, int i1, int i2, int i3, int32_t value) {
  2334. void * data = (char *) tensor->data + i0*tensor->nb[0] + i1*tensor->nb[1] + i2*tensor->nb[2] + i3*tensor->nb[3];
  2335. switch (tensor->type) {
  2336. case GGML_TYPE_I8:
  2337. {
  2338. ((int8_t *)(data))[0] = value;
  2339. } break;
  2340. case GGML_TYPE_I16:
  2341. {
  2342. ((int16_t *)(data))[0] = value;
  2343. } break;
  2344. case GGML_TYPE_I32:
  2345. {
  2346. ((int32_t *)(data))[0] = value;
  2347. } break;
  2348. case GGML_TYPE_F16:
  2349. {
  2350. ((ggml_fp16_t *)(data))[0] = GGML_FP32_TO_FP16(value);
  2351. } break;
  2352. case GGML_TYPE_BF16:
  2353. {
  2354. ((ggml_bf16_t *)(data))[0] = GGML_FP32_TO_BF16(value);
  2355. } break;
  2356. case GGML_TYPE_F32:
  2357. {
  2358. ((float *)(data))[0] = value;
  2359. } break;
  2360. default:
  2361. {
  2362. GGML_ABORT("fatal error");
  2363. }
  2364. }
  2365. }
  2366. float ggml_get_f32_1d(const struct ggml_tensor * tensor, int i) {
  2367. if (!ggml_is_contiguous(tensor)) {
  2368. int64_t id[4] = { 0, 0, 0, 0 };
  2369. ggml_unravel_index(tensor, i, &id[0], &id[1], &id[2], &id[3]);
  2370. return ggml_get_f32_nd(tensor, id[0], id[1], id[2], id[3]);
  2371. }
  2372. switch (tensor->type) {
  2373. case GGML_TYPE_I8:
  2374. {
  2375. return ((int8_t *)(tensor->data))[i];
  2376. }
  2377. case GGML_TYPE_I16:
  2378. {
  2379. return ((int16_t *)(tensor->data))[i];
  2380. }
  2381. case GGML_TYPE_I32:
  2382. {
  2383. return ((int32_t *)(tensor->data))[i];
  2384. }
  2385. case GGML_TYPE_F16:
  2386. {
  2387. return GGML_FP16_TO_FP32(((ggml_fp16_t *)(tensor->data))[i]);
  2388. }
  2389. case GGML_TYPE_BF16:
  2390. {
  2391. return GGML_BF16_TO_FP32(((ggml_bf16_t *)(tensor->data))[i]);
  2392. }
  2393. case GGML_TYPE_F32:
  2394. {
  2395. return ((float *)(tensor->data))[i];
  2396. }
  2397. default:
  2398. {
  2399. GGML_ABORT("fatal error");
  2400. }
  2401. }
  2402. }
  2403. void ggml_set_f32_1d(const struct ggml_tensor * tensor, int i, float value) {
  2404. if (!ggml_is_contiguous(tensor)) {
  2405. int64_t id[4] = { 0, 0, 0, 0 };
  2406. ggml_unravel_index(tensor, i, &id[0], &id[1], &id[2], &id[3]);
  2407. ggml_set_f32_nd(tensor, id[0], id[1], id[2], id[3], value);
  2408. return;
  2409. }
  2410. switch (tensor->type) {
  2411. case GGML_TYPE_I8:
  2412. {
  2413. ((int8_t *)(tensor->data))[i] = value;
  2414. } break;
  2415. case GGML_TYPE_I16:
  2416. {
  2417. ((int16_t *)(tensor->data))[i] = value;
  2418. } break;
  2419. case GGML_TYPE_I32:
  2420. {
  2421. ((int32_t *)(tensor->data))[i] = value;
  2422. } break;
  2423. case GGML_TYPE_F16:
  2424. {
  2425. ((ggml_fp16_t *)(tensor->data))[i] = GGML_FP32_TO_FP16(value);
  2426. } break;
  2427. case GGML_TYPE_BF16:
  2428. {
  2429. ((ggml_bf16_t *)(tensor->data))[i] = GGML_FP32_TO_BF16(value);
  2430. } break;
  2431. case GGML_TYPE_F32:
  2432. {
  2433. ((float *)(tensor->data))[i] = value;
  2434. } break;
  2435. default:
  2436. {
  2437. GGML_ABORT("fatal error");
  2438. }
  2439. }
  2440. }
  2441. float ggml_get_f32_nd(const struct ggml_tensor * tensor, int i0, int i1, int i2, int i3) {
  2442. void * data = (char *) tensor->data + i0*tensor->nb[0] + i1*tensor->nb[1] + i2*tensor->nb[2] + i3*tensor->nb[3];
  2443. switch (tensor->type) {
  2444. case GGML_TYPE_I8:
  2445. return ((int8_t *) data)[0];
  2446. case GGML_TYPE_I16:
  2447. return ((int16_t *) data)[0];
  2448. case GGML_TYPE_I32:
  2449. return ((int32_t *) data)[0];
  2450. case GGML_TYPE_F16:
  2451. return GGML_FP16_TO_FP32(((ggml_fp16_t *) data)[0]);
  2452. case GGML_TYPE_BF16:
  2453. return GGML_BF16_TO_FP32(((ggml_bf16_t *) data)[0]);
  2454. case GGML_TYPE_F32:
  2455. return ((float *) data)[0];
  2456. default:
  2457. GGML_ABORT("fatal error");
  2458. }
  2459. }
  2460. void ggml_set_f32_nd(const struct ggml_tensor * tensor, int i0, int i1, int i2, int i3, float value) {
  2461. void * data = (char *) tensor->data + i0*tensor->nb[0] + i1*tensor->nb[1] + i2*tensor->nb[2] + i3*tensor->nb[3];
  2462. switch (tensor->type) {
  2463. case GGML_TYPE_I8:
  2464. {
  2465. ((int8_t *)(data))[0] = value;
  2466. } break;
  2467. case GGML_TYPE_I16:
  2468. {
  2469. ((int16_t *)(data))[0] = value;
  2470. } break;
  2471. case GGML_TYPE_I32:
  2472. {
  2473. ((int32_t *)(data))[0] = value;
  2474. } break;
  2475. case GGML_TYPE_F16:
  2476. {
  2477. ((ggml_fp16_t *)(data))[0] = GGML_FP32_TO_FP16(value);
  2478. } break;
  2479. case GGML_TYPE_BF16:
  2480. {
  2481. ((ggml_bf16_t *)(data))[0] = GGML_FP32_TO_BF16(value);
  2482. } break;
  2483. case GGML_TYPE_F32:
  2484. {
  2485. ((float *)(data))[0] = value;
  2486. } break;
  2487. default:
  2488. {
  2489. GGML_ABORT("fatal error");
  2490. }
  2491. }
  2492. }
  2493. ////////////////////////////////////////////////////////////////////////////////
  2494. // ggml_compute_forward_dup
  2495. static void ggml_compute_forward_dup_same_cont(
  2496. const struct ggml_compute_params * params,
  2497. struct ggml_tensor * dst) {
  2498. const struct ggml_tensor * src0 = dst->src[0];
  2499. GGML_ASSERT(ggml_nelements(dst) == ggml_nelements(src0));
  2500. GGML_ASSERT(ggml_is_contiguous(dst) && ggml_is_contiguous(src0));
  2501. GGML_ASSERT(src0->type == dst->type);
  2502. const size_t nb0 = ggml_type_size(src0->type);
  2503. const int ith = params->ith; // thread index
  2504. const int nth = params->nth; // number of threads
  2505. // parallelize by elements
  2506. const int ne = ggml_nelements(dst);
  2507. const int dr = (ne + nth - 1) / nth;
  2508. const int ie0 = dr * ith;
  2509. const int ie1 = MIN(ie0 + dr, ne);
  2510. if (ie0 < ie1) {
  2511. memcpy(
  2512. ((char *) dst->data + ie0*nb0),
  2513. ((char *) src0->data + ie0*nb0),
  2514. (ie1 - ie0) * nb0);
  2515. }
  2516. }
  2517. static void ggml_compute_forward_dup_f16(
  2518. const struct ggml_compute_params * params,
  2519. struct ggml_tensor * dst) {
  2520. const struct ggml_tensor * src0 = dst->src[0];
  2521. GGML_ASSERT(ggml_nelements(dst) == ggml_nelements(src0));
  2522. GGML_TENSOR_UNARY_OP_LOCALS
  2523. const int ith = params->ith; // thread index
  2524. const int nth = params->nth; // number of threads
  2525. // parallelize by rows
  2526. const int nr = ne01;
  2527. // number of rows per thread
  2528. const int dr = (nr + nth - 1) / nth;
  2529. // row range for this thread
  2530. const int ir0 = dr * ith;
  2531. const int ir1 = MIN(ir0 + dr, nr);
  2532. if (src0->type == dst->type &&
  2533. ne00 == ne0 &&
  2534. nb00 == ggml_type_size(src0->type) && nb0 == ggml_type_size(dst->type)) {
  2535. // copy by rows
  2536. const size_t rs = ne00*nb00;
  2537. for (int64_t i03 = 0; i03 < ne03; i03++) {
  2538. for (int64_t i02 = 0; i02 < ne02; i02++) {
  2539. for (int64_t i01 = ir0; i01 < ir1; i01++) {
  2540. memcpy(
  2541. ((char *) dst->data + i01*nb1 + i02*nb2 + i03*nb3),
  2542. ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03),
  2543. rs);
  2544. }
  2545. }
  2546. }
  2547. return;
  2548. }
  2549. // TODO: add more special-case implementations for tensor shapes/strides that can benefit from memcpy
  2550. if (ggml_is_contiguous(dst)) {
  2551. if (nb00 == sizeof(ggml_fp16_t)) {
  2552. if (dst->type == GGML_TYPE_F16) {
  2553. size_t id = 0;
  2554. const size_t rs = ne00 * nb00;
  2555. char * dst_ptr = (char *) dst->data;
  2556. for (int i03 = 0; i03 < ne03; i03++) {
  2557. for (int i02 = 0; i02 < ne02; i02++) {
  2558. id += rs * ir0;
  2559. for (int i01 = ir0; i01 < ir1; i01++) {
  2560. const char * src0_ptr = (char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03;
  2561. memcpy(dst_ptr + id, src0_ptr, rs);
  2562. id += rs;
  2563. }
  2564. id += rs * (ne01 - ir1);
  2565. }
  2566. }
  2567. } else if (dst->type == GGML_TYPE_F32) {
  2568. size_t id = 0;
  2569. float * dst_ptr = (float *) dst->data;
  2570. for (int i03 = 0; i03 < ne03; i03++) {
  2571. for (int i02 = 0; i02 < ne02; i02++) {
  2572. id += ne00 * ir0;
  2573. for (int i01 = ir0; i01 < ir1; i01++) {
  2574. const ggml_fp16_t * src0_ptr = (ggml_fp16_t *) ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03);
  2575. for (int i00 = 0; i00 < ne00; i00++) {
  2576. dst_ptr[id] = GGML_FP16_TO_FP32(src0_ptr[i00]);
  2577. id++;
  2578. }
  2579. }
  2580. id += ne00 * (ne01 - ir1);
  2581. }
  2582. }
  2583. } else if (ggml_get_type_traits_cpu(dst->type)->from_float) {
  2584. ggml_from_float_t const quantize_row_q = ggml_get_type_traits_cpu(dst->type)->from_float;
  2585. float * src0_f32 = (float *) params->wdata + (ne00 + CACHE_LINE_SIZE_F32) * ith;
  2586. size_t id = 0;
  2587. size_t rs = nb0 * (ne00 / ggml_blck_size(dst->type));
  2588. char * dst_ptr = (char *) dst->data;
  2589. for (int i03 = 0; i03 < ne03; i03++) {
  2590. for (int i02 = 0; i02 < ne02; i02++) {
  2591. id += rs * ir0;
  2592. for (int i01 = ir0; i01 < ir1; i01++) {
  2593. const ggml_fp16_t * src0_ptr = (ggml_fp16_t *) ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03);
  2594. for (int i00 = 0; i00 < ne00; i00++) {
  2595. src0_f32[i00] = GGML_FP16_TO_FP32(src0_ptr[i00]);
  2596. }
  2597. quantize_row_q(src0_f32, dst_ptr + id, ne00);
  2598. id += rs;
  2599. }
  2600. id += rs * (ne01 - ir1);
  2601. }
  2602. }
  2603. } else {
  2604. GGML_ABORT("fatal error"); // TODO: implement
  2605. }
  2606. } else {
  2607. //printf("%s: this is not optimal - fix me\n", __func__);
  2608. if (dst->type == GGML_TYPE_F32) {
  2609. size_t id = 0;
  2610. float * dst_ptr = (float *) dst->data;
  2611. for (int i03 = 0; i03 < ne03; i03++) {
  2612. for (int i02 = 0; i02 < ne02; i02++) {
  2613. id += ne00 * ir0;
  2614. for (int i01 = ir0; i01 < ir1; i01++) {
  2615. for (int i00 = 0; i00 < ne00; i00++) {
  2616. const ggml_fp16_t * src0_ptr = (ggml_fp16_t *) ((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03);
  2617. dst_ptr[id] = GGML_FP16_TO_FP32(*src0_ptr);
  2618. id++;
  2619. }
  2620. }
  2621. id += ne00 * (ne01 - ir1);
  2622. }
  2623. }
  2624. } else if (dst->type == GGML_TYPE_F16) {
  2625. size_t id = 0;
  2626. ggml_fp16_t * dst_ptr = (ggml_fp16_t *) dst->data;
  2627. for (int i03 = 0; i03 < ne03; i03++) {
  2628. for (int i02 = 0; i02 < ne02; i02++) {
  2629. id += ne00 * ir0;
  2630. for (int i01 = ir0; i01 < ir1; i01++) {
  2631. for (int i00 = 0; i00 < ne00; i00++) {
  2632. const ggml_fp16_t * src0_ptr = (ggml_fp16_t *) ((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03);
  2633. dst_ptr[id] = *src0_ptr;
  2634. id++;
  2635. }
  2636. }
  2637. id += ne00 * (ne01 - ir1);
  2638. }
  2639. }
  2640. } else {
  2641. GGML_ABORT("fatal error"); // TODO: implement
  2642. }
  2643. }
  2644. return;
  2645. }
  2646. // dst counters
  2647. int64_t i10 = 0;
  2648. int64_t i11 = 0;
  2649. int64_t i12 = 0;
  2650. int64_t i13 = 0;
  2651. if (dst->type == GGML_TYPE_F16) {
  2652. for (int64_t i03 = 0; i03 < ne03; i03++) {
  2653. for (int64_t i02 = 0; i02 < ne02; i02++) {
  2654. i10 += ne00 * ir0;
  2655. while (i10 >= ne0) {
  2656. i10 -= ne0;
  2657. if (++i11 == ne1) {
  2658. i11 = 0;
  2659. if (++i12 == ne2) {
  2660. i12 = 0;
  2661. if (++i13 == ne3) {
  2662. i13 = 0;
  2663. }
  2664. }
  2665. }
  2666. }
  2667. for (int64_t i01 = ir0; i01 < ir1; i01++) {
  2668. for (int64_t i00 = 0; i00 < ne00; i00++) {
  2669. const char * src0_ptr = ((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03);
  2670. char * dst_ptr = ((char *) dst->data + i10*nb0 + i11*nb1 + i12*nb2 + i13*nb3);
  2671. memcpy(dst_ptr, src0_ptr, sizeof(ggml_fp16_t));
  2672. if (++i10 == ne00) {
  2673. i10 = 0;
  2674. if (++i11 == ne01) {
  2675. i11 = 0;
  2676. if (++i12 == ne02) {
  2677. i12 = 0;
  2678. if (++i13 == ne03) {
  2679. i13 = 0;
  2680. }
  2681. }
  2682. }
  2683. }
  2684. }
  2685. }
  2686. i10 += ne00 * (ne01 - ir1);
  2687. while (i10 >= ne0) {
  2688. i10 -= ne0;
  2689. if (++i11 == ne1) {
  2690. i11 = 0;
  2691. if (++i12 == ne2) {
  2692. i12 = 0;
  2693. if (++i13 == ne3) {
  2694. i13 = 0;
  2695. }
  2696. }
  2697. }
  2698. }
  2699. }
  2700. }
  2701. } else if (dst->type == GGML_TYPE_F32) {
  2702. for (int64_t i03 = 0; i03 < ne03; i03++) {
  2703. for (int64_t i02 = 0; i02 < ne02; i02++) {
  2704. i10 += ne00 * ir0;
  2705. while (i10 >= ne0) {
  2706. i10 -= ne0;
  2707. if (++i11 == ne1) {
  2708. i11 = 0;
  2709. if (++i12 == ne2) {
  2710. i12 = 0;
  2711. if (++i13 == ne3) {
  2712. i13 = 0;
  2713. }
  2714. }
  2715. }
  2716. }
  2717. for (int64_t i01 = ir0; i01 < ir1; i01++) {
  2718. for (int64_t i00 = 0; i00 < ne00; i00++) {
  2719. const char * src0_ptr = ((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03);
  2720. char * dst_ptr = ((char *) dst->data + i10*nb0 + i11*nb1 + i12*nb2 + i13*nb3);
  2721. *(float *) dst_ptr = GGML_FP16_TO_FP32(*(const ggml_fp16_t *) src0_ptr);
  2722. if (++i10 == ne0) {
  2723. i10 = 0;
  2724. if (++i11 == ne1) {
  2725. i11 = 0;
  2726. if (++i12 == ne2) {
  2727. i12 = 0;
  2728. if (++i13 == ne3) {
  2729. i13 = 0;
  2730. }
  2731. }
  2732. }
  2733. }
  2734. }
  2735. }
  2736. i10 += ne00 * (ne01 - ir1);
  2737. while (i10 >= ne0) {
  2738. i10 -= ne0;
  2739. if (++i11 == ne1) {
  2740. i11 = 0;
  2741. if (++i12 == ne2) {
  2742. i12 = 0;
  2743. if (++i13 == ne3) {
  2744. i13 = 0;
  2745. }
  2746. }
  2747. }
  2748. }
  2749. }
  2750. }
  2751. } else {
  2752. GGML_ABORT("fatal error"); // TODO: implement
  2753. }
  2754. }
  2755. static void ggml_compute_forward_dup_bf16(
  2756. const struct ggml_compute_params * params,
  2757. struct ggml_tensor * dst) {
  2758. const struct ggml_tensor * src0 = dst->src[0];
  2759. GGML_ASSERT(ggml_nelements(dst) == ggml_nelements(src0));
  2760. GGML_TENSOR_UNARY_OP_LOCALS
  2761. const int ith = params->ith; // thread index
  2762. const int nth = params->nth; // number of threads
  2763. // parallelize by rows
  2764. const int nr = ne01;
  2765. // number of rows per thread
  2766. const int dr = (nr + nth - 1) / nth;
  2767. // row range for this thread
  2768. const int ir0 = dr * ith;
  2769. const int ir1 = MIN(ir0 + dr, nr);
  2770. if (src0->type == dst->type &&
  2771. ne00 == ne0 &&
  2772. nb00 == ggml_type_size(src0->type) && nb0 == ggml_type_size(dst->type)) {
  2773. // copy by rows
  2774. const size_t rs = ne00*nb00;
  2775. for (int64_t i03 = 0; i03 < ne03; i03++) {
  2776. for (int64_t i02 = 0; i02 < ne02; i02++) {
  2777. for (int64_t i01 = ir0; i01 < ir1; i01++) {
  2778. memcpy(
  2779. ((char *) dst->data + i01*nb1 + i02*nb2 + i03*nb3),
  2780. ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03),
  2781. rs);
  2782. }
  2783. }
  2784. }
  2785. return;
  2786. }
  2787. // TODO: add more special-case implementations for tensor shapes/strides that can benefit from memcpy
  2788. if (ggml_is_contiguous(dst)) {
  2789. if (nb00 == sizeof(ggml_bf16_t)) {
  2790. if (dst->type == GGML_TYPE_BF16) {
  2791. size_t id = 0;
  2792. const size_t rs = ne00 * nb00;
  2793. char * dst_ptr = (char *) dst->data;
  2794. for (int i03 = 0; i03 < ne03; i03++) {
  2795. for (int i02 = 0; i02 < ne02; i02++) {
  2796. id += rs * ir0;
  2797. for (int i01 = ir0; i01 < ir1; i01++) {
  2798. const char * src0_ptr = (char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03;
  2799. memcpy(dst_ptr + id, src0_ptr, rs);
  2800. id += rs;
  2801. }
  2802. id += rs * (ne01 - ir1);
  2803. }
  2804. }
  2805. } else if (dst->type == GGML_TYPE_F16) {
  2806. size_t id = 0;
  2807. ggml_fp16_t * dst_ptr = (ggml_fp16_t *) dst->data;
  2808. for (int i03 = 0; i03 < ne03; i03++) {
  2809. for (int i02 = 0; i02 < ne02; i02++) {
  2810. id += ne00 * ir0;
  2811. for (int i01 = ir0; i01 < ir1; i01++) {
  2812. const ggml_bf16_t * src0_ptr = (ggml_bf16_t *) ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03);
  2813. for (int i00 = 0; i00 < ne00; i00++) {
  2814. dst_ptr[id] = GGML_FP32_TO_FP16(GGML_BF16_TO_FP32(src0_ptr[i00]));
  2815. id++;
  2816. }
  2817. }
  2818. id += ne00 * (ne01 - ir1);
  2819. }
  2820. }
  2821. } else if (dst->type == GGML_TYPE_F32) {
  2822. size_t id = 0;
  2823. float * dst_ptr = (float *) dst->data;
  2824. for (int i03 = 0; i03 < ne03; i03++) {
  2825. for (int i02 = 0; i02 < ne02; i02++) {
  2826. id += ne00 * ir0;
  2827. for (int i01 = ir0; i01 < ir1; i01++) {
  2828. const ggml_bf16_t * src0_ptr = (ggml_bf16_t *) ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03);
  2829. for (int i00 = 0; i00 < ne00; i00++) {
  2830. dst_ptr[id] = GGML_BF16_TO_FP32(src0_ptr[i00]);
  2831. id++;
  2832. }
  2833. }
  2834. id += ne00 * (ne01 - ir1);
  2835. }
  2836. }
  2837. } else if (ggml_get_type_traits_cpu(dst->type)->from_float) {
  2838. ggml_from_float_t const quantize_row_q = ggml_get_type_traits_cpu(dst->type)->from_float;
  2839. float * src0_f32 = (float *) params->wdata + (ne00 + CACHE_LINE_SIZE_F32) * ith;
  2840. size_t id = 0;
  2841. size_t rs = nb0 * (ne00 / ggml_blck_size(dst->type));
  2842. char * dst_ptr = (char *) dst->data;
  2843. for (int i03 = 0; i03 < ne03; i03++) {
  2844. for (int i02 = 0; i02 < ne02; i02++) {
  2845. id += rs * ir0;
  2846. for (int i01 = ir0; i01 < ir1; i01++) {
  2847. const ggml_bf16_t * src0_ptr = (ggml_bf16_t *) ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03);
  2848. for (int i00 = 0; i00 < ne00; i00++) {
  2849. src0_f32[i00] = GGML_BF16_TO_FP32(src0_ptr[i00]);
  2850. }
  2851. quantize_row_q(src0_f32, dst_ptr + id, ne00);
  2852. id += rs;
  2853. }
  2854. id += rs * (ne01 - ir1);
  2855. }
  2856. }
  2857. } else {
  2858. GGML_ABORT("fatal error"); // TODO: implement
  2859. }
  2860. } else {
  2861. //printf("%s: this is not optimal - fix me\n", __func__);
  2862. if (dst->type == GGML_TYPE_F32) {
  2863. size_t id = 0;
  2864. float * dst_ptr = (float *) dst->data;
  2865. for (int i03 = 0; i03 < ne03; i03++) {
  2866. for (int i02 = 0; i02 < ne02; i02++) {
  2867. id += ne00 * ir0;
  2868. for (int i01 = ir0; i01 < ir1; i01++) {
  2869. for (int i00 = 0; i00 < ne00; i00++) {
  2870. const ggml_bf16_t * src0_ptr = (ggml_bf16_t *) ((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03);
  2871. dst_ptr[id] = GGML_BF16_TO_FP32(*src0_ptr);
  2872. id++;
  2873. }
  2874. }
  2875. id += ne00 * (ne01 - ir1);
  2876. }
  2877. }
  2878. } else if (dst->type == GGML_TYPE_BF16) {
  2879. size_t id = 0;
  2880. ggml_bf16_t * dst_ptr = (ggml_bf16_t *) dst->data;
  2881. for (int i03 = 0; i03 < ne03; i03++) {
  2882. for (int i02 = 0; i02 < ne02; i02++) {
  2883. id += ne00 * ir0;
  2884. for (int i01 = ir0; i01 < ir1; i01++) {
  2885. for (int i00 = 0; i00 < ne00; i00++) {
  2886. const ggml_bf16_t * src0_ptr = (ggml_bf16_t *) ((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03);
  2887. dst_ptr[id] = *src0_ptr;
  2888. id++;
  2889. }
  2890. }
  2891. id += ne00 * (ne01 - ir1);
  2892. }
  2893. }
  2894. } else if (dst->type == GGML_TYPE_F16) {
  2895. size_t id = 0;
  2896. ggml_fp16_t * dst_ptr = (ggml_fp16_t *) dst->data;
  2897. for (int i03 = 0; i03 < ne03; i03++) {
  2898. for (int i02 = 0; i02 < ne02; i02++) {
  2899. id += ne00 * ir0;
  2900. for (int i01 = ir0; i01 < ir1; i01++) {
  2901. for (int i00 = 0; i00 < ne00; i00++) {
  2902. const ggml_bf16_t * src0_ptr = (ggml_bf16_t *) ((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03);
  2903. dst_ptr[id] = GGML_FP32_TO_FP16(GGML_BF16_TO_FP32(*src0_ptr));
  2904. id++;
  2905. }
  2906. }
  2907. id += ne00 * (ne01 - ir1);
  2908. }
  2909. }
  2910. } else {
  2911. GGML_ABORT("fatal error"); // TODO: implement
  2912. }
  2913. }
  2914. return;
  2915. }
  2916. // dst counters
  2917. int64_t i10 = 0;
  2918. int64_t i11 = 0;
  2919. int64_t i12 = 0;
  2920. int64_t i13 = 0;
  2921. if (dst->type == GGML_TYPE_BF16) {
  2922. for (int64_t i03 = 0; i03 < ne03; i03++) {
  2923. for (int64_t i02 = 0; i02 < ne02; i02++) {
  2924. i10 += ne00 * ir0;
  2925. while (i10 >= ne0) {
  2926. i10 -= ne0;
  2927. if (++i11 == ne1) {
  2928. i11 = 0;
  2929. if (++i12 == ne2) {
  2930. i12 = 0;
  2931. if (++i13 == ne3) {
  2932. i13 = 0;
  2933. }
  2934. }
  2935. }
  2936. }
  2937. for (int64_t i01 = ir0; i01 < ir1; i01++) {
  2938. for (int64_t i00 = 0; i00 < ne00; i00++) {
  2939. const char * src0_ptr = ((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03);
  2940. char * dst_ptr = ((char *) dst->data + i10*nb0 + i11*nb1 + i12*nb2 + i13*nb3);
  2941. memcpy(dst_ptr, src0_ptr, sizeof(ggml_bf16_t));
  2942. if (++i10 == ne00) {
  2943. i10 = 0;
  2944. if (++i11 == ne01) {
  2945. i11 = 0;
  2946. if (++i12 == ne02) {
  2947. i12 = 0;
  2948. if (++i13 == ne03) {
  2949. i13 = 0;
  2950. }
  2951. }
  2952. }
  2953. }
  2954. }
  2955. }
  2956. i10 += ne00 * (ne01 - ir1);
  2957. while (i10 >= ne0) {
  2958. i10 -= ne0;
  2959. if (++i11 == ne1) {
  2960. i11 = 0;
  2961. if (++i12 == ne2) {
  2962. i12 = 0;
  2963. if (++i13 == ne3) {
  2964. i13 = 0;
  2965. }
  2966. }
  2967. }
  2968. }
  2969. }
  2970. }
  2971. } else if (dst->type == GGML_TYPE_F16) {
  2972. for (int64_t i03 = 0; i03 < ne03; i03++) {
  2973. for (int64_t i02 = 0; i02 < ne02; i02++) {
  2974. i10 += ne00 * ir0;
  2975. while (i10 >= ne0) {
  2976. i10 -= ne0;
  2977. if (++i11 == ne1) {
  2978. i11 = 0;
  2979. if (++i12 == ne2) {
  2980. i12 = 0;
  2981. if (++i13 == ne3) {
  2982. i13 = 0;
  2983. }
  2984. }
  2985. }
  2986. }
  2987. for (int64_t i01 = ir0; i01 < ir1; i01++) {
  2988. for (int64_t i00 = 0; i00 < ne00; i00++) {
  2989. const char * src0_ptr = ((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03);
  2990. char * dst_ptr = ((char *) dst->data + i10*nb0 + i11*nb1 + i12*nb2 + i13*nb3);
  2991. *(ggml_fp16_t *) dst_ptr = GGML_FP32_TO_FP16(GGML_BF16_TO_FP32(*(const ggml_bf16_t *) src0_ptr));
  2992. if (++i10 == ne0) {
  2993. i10 = 0;
  2994. if (++i11 == ne1) {
  2995. i11 = 0;
  2996. if (++i12 == ne2) {
  2997. i12 = 0;
  2998. if (++i13 == ne3) {
  2999. i13 = 0;
  3000. }
  3001. }
  3002. }
  3003. }
  3004. }
  3005. }
  3006. i10 += ne00 * (ne01 - ir1);
  3007. while (i10 >= ne0) {
  3008. i10 -= ne0;
  3009. if (++i11 == ne1) {
  3010. i11 = 0;
  3011. if (++i12 == ne2) {
  3012. i12 = 0;
  3013. if (++i13 == ne3) {
  3014. i13 = 0;
  3015. }
  3016. }
  3017. }
  3018. }
  3019. }
  3020. }
  3021. } else if (dst->type == GGML_TYPE_F32) {
  3022. for (int64_t i03 = 0; i03 < ne03; i03++) {
  3023. for (int64_t i02 = 0; i02 < ne02; i02++) {
  3024. i10 += ne00 * ir0;
  3025. while (i10 >= ne0) {
  3026. i10 -= ne0;
  3027. if (++i11 == ne1) {
  3028. i11 = 0;
  3029. if (++i12 == ne2) {
  3030. i12 = 0;
  3031. if (++i13 == ne3) {
  3032. i13 = 0;
  3033. }
  3034. }
  3035. }
  3036. }
  3037. for (int64_t i01 = ir0; i01 < ir1; i01++) {
  3038. for (int64_t i00 = 0; i00 < ne00; i00++) {
  3039. const char * src0_ptr = ((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03);
  3040. char * dst_ptr = ((char *) dst->data + i10*nb0 + i11*nb1 + i12*nb2 + i13*nb3);
  3041. *(float *) dst_ptr = GGML_BF16_TO_FP32(*(const ggml_bf16_t *) src0_ptr);
  3042. if (++i10 == ne0) {
  3043. i10 = 0;
  3044. if (++i11 == ne1) {
  3045. i11 = 0;
  3046. if (++i12 == ne2) {
  3047. i12 = 0;
  3048. if (++i13 == ne3) {
  3049. i13 = 0;
  3050. }
  3051. }
  3052. }
  3053. }
  3054. }
  3055. }
  3056. i10 += ne00 * (ne01 - ir1);
  3057. while (i10 >= ne0) {
  3058. i10 -= ne0;
  3059. if (++i11 == ne1) {
  3060. i11 = 0;
  3061. if (++i12 == ne2) {
  3062. i12 = 0;
  3063. if (++i13 == ne3) {
  3064. i13 = 0;
  3065. }
  3066. }
  3067. }
  3068. }
  3069. }
  3070. }
  3071. } else {
  3072. GGML_ABORT("fatal error"); // TODO: implement
  3073. }
  3074. }
  3075. static void ggml_compute_forward_dup_f32(
  3076. const struct ggml_compute_params * params,
  3077. struct ggml_tensor * dst) {
  3078. const struct ggml_tensor * src0 = dst->src[0];
  3079. GGML_ASSERT(ggml_nelements(dst) == ggml_nelements(src0));
  3080. GGML_TENSOR_UNARY_OP_LOCALS
  3081. const int ith = params->ith; // thread index
  3082. const int nth = params->nth; // number of threads
  3083. // parallelize by rows
  3084. const int nr = ne01;
  3085. // number of rows per thread
  3086. const int dr = (nr + nth - 1) / nth;
  3087. // row range for this thread
  3088. const int ir0 = dr * ith;
  3089. const int ir1 = MIN(ir0 + dr, nr);
  3090. if (src0->type == dst->type &&
  3091. ne00 == ne0 &&
  3092. nb00 == ggml_type_size(src0->type) && nb0 == ggml_type_size(dst->type)) {
  3093. // copy by rows
  3094. const size_t rs = ne00*nb00;
  3095. for (int64_t i03 = 0; i03 < ne03; i03++) {
  3096. for (int64_t i02 = 0; i02 < ne02; i02++) {
  3097. for (int64_t i01 = ir0; i01 < ir1; i01++) {
  3098. memcpy(
  3099. ((char *) dst->data + i01*nb1 + i02*nb2 + i03*nb3),
  3100. ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03),
  3101. rs);
  3102. }
  3103. }
  3104. }
  3105. return;
  3106. }
  3107. if (ggml_is_contiguous(dst)) {
  3108. // TODO: simplify
  3109. if (nb00 == sizeof(float)) {
  3110. if (dst->type == GGML_TYPE_F32) {
  3111. size_t id = 0;
  3112. const size_t rs = ne00 * nb00;
  3113. char * dst_ptr = (char *) dst->data;
  3114. for (int i03 = 0; i03 < ne03; i03++) {
  3115. for (int i02 = 0; i02 < ne02; i02++) {
  3116. id += rs * ir0;
  3117. for (int i01 = ir0; i01 < ir1; i01++) {
  3118. const char * src0_ptr = (char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03;
  3119. memcpy(dst_ptr + id, src0_ptr, rs);
  3120. id += rs;
  3121. }
  3122. id += rs * (ne01 - ir1);
  3123. }
  3124. }
  3125. } else if (ggml_get_type_traits_cpu(dst->type)->from_float) {
  3126. ggml_from_float_t const quantize_row_q = ggml_get_type_traits_cpu(dst->type)->from_float;
  3127. size_t id = 0;
  3128. size_t rs = nb0 * (ne00 / ggml_blck_size(dst->type));
  3129. char * dst_ptr = (char *) dst->data;
  3130. for (int i03 = 0; i03 < ne03; i03++) {
  3131. for (int i02 = 0; i02 < ne02; i02++) {
  3132. id += rs * ir0;
  3133. for (int i01 = ir0; i01 < ir1; i01++) {
  3134. const float * src0_ptr = (float *) ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03);
  3135. quantize_row_q(src0_ptr, dst_ptr + id, ne00);
  3136. id += rs;
  3137. }
  3138. id += rs * (ne01 - ir1);
  3139. }
  3140. }
  3141. } else {
  3142. GGML_ABORT("fatal error"); // TODO: implement
  3143. }
  3144. } else {
  3145. //printf("%s: this is not optimal - fix me\n", __func__);
  3146. if (dst->type == GGML_TYPE_F32) {
  3147. size_t id = 0;
  3148. float * dst_ptr = (float *) dst->data;
  3149. for (int i03 = 0; i03 < ne03; i03++) {
  3150. for (int i02 = 0; i02 < ne02; i02++) {
  3151. id += ne00 * ir0;
  3152. for (int i01 = ir0; i01 < ir1; i01++) {
  3153. for (int i00 = 0; i00 < ne00; i00++) {
  3154. const float * src0_ptr = (float *) ((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03);
  3155. dst_ptr[id] = *src0_ptr;
  3156. id++;
  3157. }
  3158. }
  3159. id += ne00 * (ne01 - ir1);
  3160. }
  3161. }
  3162. } else if (dst->type == GGML_TYPE_F16) {
  3163. size_t id = 0;
  3164. ggml_fp16_t * dst_ptr = (ggml_fp16_t *) dst->data;
  3165. for (int i03 = 0; i03 < ne03; i03++) {
  3166. for (int i02 = 0; i02 < ne02; i02++) {
  3167. id += ne00 * ir0;
  3168. for (int i01 = ir0; i01 < ir1; i01++) {
  3169. for (int i00 = 0; i00 < ne00; i00++) {
  3170. const float * src0_ptr = (float *) ((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03);
  3171. dst_ptr[id] = GGML_FP32_TO_FP16(*src0_ptr);
  3172. id++;
  3173. }
  3174. }
  3175. id += ne00 * (ne01 - ir1);
  3176. }
  3177. }
  3178. } else if (dst->type == GGML_TYPE_BF16) {
  3179. size_t id = 0;
  3180. ggml_bf16_t * dst_ptr = (ggml_bf16_t *) dst->data;
  3181. for (int i03 = 0; i03 < ne03; i03++) {
  3182. for (int i02 = 0; i02 < ne02; i02++) {
  3183. id += ne00 * ir0;
  3184. for (int i01 = ir0; i01 < ir1; i01++) {
  3185. for (int i00 = 0; i00 < ne00; i00++) {
  3186. const float * src0_ptr = (float *) ((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03);
  3187. dst_ptr[id] = GGML_FP32_TO_BF16(*src0_ptr);
  3188. id++;
  3189. }
  3190. }
  3191. id += ne00 * (ne01 - ir1);
  3192. }
  3193. }
  3194. } else {
  3195. GGML_ABORT("fatal error"); // TODO: implement
  3196. }
  3197. }
  3198. return;
  3199. }
  3200. // dst counters
  3201. int64_t i10 = 0;
  3202. int64_t i11 = 0;
  3203. int64_t i12 = 0;
  3204. int64_t i13 = 0;
  3205. if (dst->type == GGML_TYPE_F32) {
  3206. for (int64_t i03 = 0; i03 < ne03; i03++) {
  3207. for (int64_t i02 = 0; i02 < ne02; i02++) {
  3208. i10 += ne00 * ir0;
  3209. while (i10 >= ne0) {
  3210. i10 -= ne0;
  3211. if (++i11 == ne1) {
  3212. i11 = 0;
  3213. if (++i12 == ne2) {
  3214. i12 = 0;
  3215. if (++i13 == ne3) {
  3216. i13 = 0;
  3217. }
  3218. }
  3219. }
  3220. }
  3221. for (int64_t i01 = ir0; i01 < ir1; i01++) {
  3222. for (int64_t i00 = 0; i00 < ne00; i00++) {
  3223. const char * src0_ptr = ((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03);
  3224. char * dst_ptr = ((char *) dst->data + i10*nb0 + i11*nb1 + i12*nb2 + i13*nb3);
  3225. memcpy(dst_ptr, src0_ptr, sizeof(float));
  3226. if (++i10 == ne0) {
  3227. i10 = 0;
  3228. if (++i11 == ne1) {
  3229. i11 = 0;
  3230. if (++i12 == ne2) {
  3231. i12 = 0;
  3232. if (++i13 == ne3) {
  3233. i13 = 0;
  3234. }
  3235. }
  3236. }
  3237. }
  3238. }
  3239. }
  3240. i10 += ne00 * (ne01 - ir1);
  3241. while (i10 >= ne0) {
  3242. i10 -= ne0;
  3243. if (++i11 == ne1) {
  3244. i11 = 0;
  3245. if (++i12 == ne2) {
  3246. i12 = 0;
  3247. if (++i13 == ne3) {
  3248. i13 = 0;
  3249. }
  3250. }
  3251. }
  3252. }
  3253. }
  3254. }
  3255. } else if (dst->type == GGML_TYPE_F16) {
  3256. for (int64_t i03 = 0; i03 < ne03; i03++) {
  3257. for (int64_t i02 = 0; i02 < ne02; i02++) {
  3258. i10 += ne00 * ir0;
  3259. while (i10 >= ne0) {
  3260. i10 -= ne0;
  3261. if (++i11 == ne1) {
  3262. i11 = 0;
  3263. if (++i12 == ne2) {
  3264. i12 = 0;
  3265. if (++i13 == ne3) {
  3266. i13 = 0;
  3267. }
  3268. }
  3269. }
  3270. }
  3271. for (int64_t i01 = ir0; i01 < ir1; i01++) {
  3272. for (int64_t i00 = 0; i00 < ne00; i00++) {
  3273. const char * src0_ptr = ((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03);
  3274. char * dst_ptr = ((char *) dst->data + i10*nb0 + i11*nb1 + i12*nb2 + i13*nb3);
  3275. *(ggml_fp16_t *) dst_ptr = GGML_FP32_TO_FP16(*(const float *) src0_ptr);
  3276. if (++i10 == ne0) {
  3277. i10 = 0;
  3278. if (++i11 == ne1) {
  3279. i11 = 0;
  3280. if (++i12 == ne2) {
  3281. i12 = 0;
  3282. if (++i13 == ne3) {
  3283. i13 = 0;
  3284. }
  3285. }
  3286. }
  3287. }
  3288. }
  3289. }
  3290. i10 += ne00 * (ne01 - ir1);
  3291. while (i10 >= ne0) {
  3292. i10 -= ne0;
  3293. if (++i11 == ne1) {
  3294. i11 = 0;
  3295. if (++i12 == ne2) {
  3296. i12 = 0;
  3297. if (++i13 == ne3) {
  3298. i13 = 0;
  3299. }
  3300. }
  3301. }
  3302. }
  3303. }
  3304. }
  3305. } else if (dst->type == GGML_TYPE_BF16) {
  3306. for (int64_t i03 = 0; i03 < ne03; i03++) {
  3307. for (int64_t i02 = 0; i02 < ne02; i02++) {
  3308. i10 += ne00 * ir0;
  3309. while (i10 >= ne0) {
  3310. i10 -= ne0;
  3311. if (++i11 == ne1) {
  3312. i11 = 0;
  3313. if (++i12 == ne2) {
  3314. i12 = 0;
  3315. if (++i13 == ne3) {
  3316. i13 = 0;
  3317. }
  3318. }
  3319. }
  3320. }
  3321. for (int64_t i01 = ir0; i01 < ir1; i01++) {
  3322. for (int64_t i00 = 0; i00 < ne00; i00++) {
  3323. const char * src0_ptr = ((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03);
  3324. char * dst_ptr = ((char *) dst->data + i10*nb0 + i11*nb1 + i12*nb2 + i13*nb3);
  3325. *(ggml_bf16_t *) dst_ptr = GGML_FP32_TO_BF16(*(const float *) src0_ptr);
  3326. if (++i10 == ne0) {
  3327. i10 = 0;
  3328. if (++i11 == ne1) {
  3329. i11 = 0;
  3330. if (++i12 == ne2) {
  3331. i12 = 0;
  3332. if (++i13 == ne3) {
  3333. i13 = 0;
  3334. }
  3335. }
  3336. }
  3337. }
  3338. }
  3339. }
  3340. i10 += ne00 * (ne01 - ir1);
  3341. while (i10 >= ne0) {
  3342. i10 -= ne0;
  3343. if (++i11 == ne1) {
  3344. i11 = 0;
  3345. if (++i12 == ne2) {
  3346. i12 = 0;
  3347. if (++i13 == ne3) {
  3348. i13 = 0;
  3349. }
  3350. }
  3351. }
  3352. }
  3353. }
  3354. }
  3355. } else {
  3356. GGML_ABORT("fatal error"); // TODO: implement
  3357. }
  3358. }
  3359. // A simplified version of ggml_compute_forward_dup that doesn't do float upcasting, and just plain old memcpy.
  3360. static void ggml_compute_forward_dup_bytes(
  3361. const struct ggml_compute_params * params,
  3362. struct ggml_tensor * dst) {
  3363. const struct ggml_tensor * src0 = dst->src[0];
  3364. GGML_ASSERT(ggml_nelements(dst) == ggml_nelements(src0));
  3365. GGML_ASSERT(src0->type == dst->type);
  3366. GGML_TENSOR_UNARY_OP_LOCALS;
  3367. if (ggml_is_contiguous(src0) && ggml_is_contiguous(dst)) {
  3368. ggml_compute_forward_dup_same_cont(params, dst);
  3369. return;
  3370. }
  3371. const size_t type_size = ggml_type_size(src0->type);
  3372. const int ith = params->ith; // thread index
  3373. const int nth = params->nth; // number of threads
  3374. // parallelize by rows
  3375. const int nr = ne01;
  3376. // number of rows per thread
  3377. const int dr = (nr + nth - 1) / nth;
  3378. // row range for this thread
  3379. const int ir0 = dr * ith;
  3380. const int ir1 = MIN(ir0 + dr, nr);
  3381. if (src0->type == dst->type &&
  3382. ne00 == ne0 &&
  3383. nb00 == type_size && nb0 == type_size) {
  3384. // copy by rows
  3385. const size_t rs = ne00 * type_size;
  3386. for (int64_t i03 = 0; i03 < ne03; i03++) {
  3387. for (int64_t i02 = 0; i02 < ne02; i02++) {
  3388. for (int64_t i01 = ir0; i01 < ir1; i01++) {
  3389. memcpy(
  3390. ((char *) dst->data + i01*nb1 + i02*nb2 + i03*nb3),
  3391. ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03),
  3392. rs);
  3393. }
  3394. }
  3395. }
  3396. return;
  3397. }
  3398. if (ggml_is_contiguous(dst)) {
  3399. size_t id = 0;
  3400. char * dst_ptr = (char *) dst->data;
  3401. const size_t rs = ne00 * type_size;
  3402. if (nb00 == type_size) {
  3403. // src0 is contigous on first dimension, copy by rows
  3404. for (int64_t i03 = 0; i03 < ne03; i03++) {
  3405. for (int64_t i02 = 0; i02 < ne02; i02++) {
  3406. id += rs * ir0;
  3407. for (int64_t i01 = ir0; i01 < ir1; i01++) {
  3408. const char * src0_ptr = (char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03;
  3409. memcpy(dst_ptr + id, src0_ptr, rs);
  3410. id += rs;
  3411. }
  3412. id += rs * (ne01 - ir1);
  3413. }
  3414. }
  3415. } else {
  3416. //printf("%s: this is not optimal - fix me\n", __func__);
  3417. for (int64_t i03 = 0; i03 < ne03; i03++) {
  3418. for (int64_t i02 = 0; i02 < ne02; i02++) {
  3419. id += rs * ir0;
  3420. for (int64_t i01 = ir0; i01 < ir1; i01++) {
  3421. for (int64_t i00 = 0; i00 < ne00; i00++) {
  3422. const char * src0_ptr = (char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03;
  3423. memcpy(dst_ptr + id, src0_ptr, type_size);
  3424. id += type_size;
  3425. }
  3426. }
  3427. id += rs * (ne01 - ir1);
  3428. }
  3429. }
  3430. }
  3431. return;
  3432. }
  3433. // dst counters
  3434. int64_t i10 = 0;
  3435. int64_t i11 = 0;
  3436. int64_t i12 = 0;
  3437. int64_t i13 = 0;
  3438. for (int64_t i03 = 0; i03 < ne03; i03++) {
  3439. for (int64_t i02 = 0; i02 < ne02; i02++) {
  3440. i10 += ne00 * ir0;
  3441. while (i10 >= ne0) {
  3442. i10 -= ne0;
  3443. if (++i11 == ne1) {
  3444. i11 = 0;
  3445. if (++i12 == ne2) {
  3446. i12 = 0;
  3447. if (++i13 == ne3) {
  3448. i13 = 0;
  3449. }
  3450. }
  3451. }
  3452. }
  3453. for (int64_t i01 = ir0; i01 < ir1; i01++) {
  3454. for (int64_t i00 = 0; i00 < ne00; i00++) {
  3455. const char * src0_ptr = ((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03);
  3456. char * dst_ptr = ((char *) dst->data + i10*nb0 + i11*nb1 + i12*nb2 + i13*nb3);
  3457. memcpy(dst_ptr, src0_ptr, type_size);
  3458. if (++i10 == ne0) {
  3459. i10 = 0;
  3460. if (++i11 == ne1) {
  3461. i11 = 0;
  3462. if (++i12 == ne2) {
  3463. i12 = 0;
  3464. if (++i13 == ne3) {
  3465. i13 = 0;
  3466. }
  3467. }
  3468. }
  3469. }
  3470. }
  3471. }
  3472. i10 += ne00 * (ne01 - ir1);
  3473. while (i10 >= ne0) {
  3474. i10 -= ne0;
  3475. if (++i11 == ne1) {
  3476. i11 = 0;
  3477. if (++i12 == ne2) {
  3478. i12 = 0;
  3479. if (++i13 == ne3) {
  3480. i13 = 0;
  3481. }
  3482. }
  3483. }
  3484. }
  3485. }
  3486. }
  3487. }
  3488. static void ggml_compute_forward_dup(
  3489. const struct ggml_compute_params * params,
  3490. struct ggml_tensor * dst) {
  3491. const struct ggml_tensor * src0 = dst->src[0];
  3492. if (src0->type == dst->type) {
  3493. ggml_compute_forward_dup_bytes(params, dst);
  3494. return;
  3495. }
  3496. switch (src0->type) {
  3497. case GGML_TYPE_F16:
  3498. {
  3499. ggml_compute_forward_dup_f16(params, dst);
  3500. } break;
  3501. case GGML_TYPE_BF16:
  3502. {
  3503. ggml_compute_forward_dup_bf16(params, dst);
  3504. } break;
  3505. case GGML_TYPE_F32:
  3506. {
  3507. ggml_compute_forward_dup_f32(params, dst);
  3508. } break;
  3509. default:
  3510. {
  3511. GGML_ABORT("fatal error");
  3512. }
  3513. }
  3514. }
  3515. // ggml_compute_forward_add
  3516. static void ggml_compute_forward_add_f32(
  3517. const struct ggml_compute_params * params,
  3518. struct ggml_tensor * dst) {
  3519. const struct ggml_tensor * src0 = dst->src[0];
  3520. const struct ggml_tensor * src1 = dst->src[1];
  3521. GGML_ASSERT(ggml_can_repeat(src1, src0) && ggml_are_same_shape(src0, dst));
  3522. const int ith = params->ith;
  3523. const int nth = params->nth;
  3524. const int nr = ggml_nrows(src0);
  3525. GGML_TENSOR_BINARY_OP_LOCALS
  3526. GGML_ASSERT( nb0 == sizeof(float));
  3527. GGML_ASSERT(nb00 == sizeof(float));
  3528. // rows per thread
  3529. const int dr = (nr + nth - 1)/nth;
  3530. // row range for this thread
  3531. const int ir0 = dr*ith;
  3532. const int ir1 = MIN(ir0 + dr, nr);
  3533. if (nb10 == sizeof(float)) {
  3534. for (int ir = ir0; ir < ir1; ++ir) {
  3535. // src1 is broadcastable across src0 and dst in i1, i2, i3
  3536. const int64_t i03 = ir/(ne02*ne01);
  3537. const int64_t i02 = (ir - i03*ne02*ne01)/ne01;
  3538. const int64_t i01 = (ir - i03*ne02*ne01 - i02*ne01);
  3539. const int64_t i13 = i03 % ne13;
  3540. const int64_t i12 = i02 % ne12;
  3541. const int64_t i11 = i01 % ne11;
  3542. const int64_t nr0 = ne00 / ne10;
  3543. float * dst_ptr = (float *) ((char *) dst->data + i03*nb3 + i02*nb2 + i01*nb1 );
  3544. float * src0_ptr = (float *) ((char *) src0->data + i03*nb03 + i02*nb02 + i01*nb01);
  3545. float * src1_ptr = (float *) ((char *) src1->data + i13*nb13 + i12*nb12 + i11*nb11);
  3546. for (int64_t r = 0; r < nr0; ++r) {
  3547. #ifdef GGML_USE_ACCELERATE
  3548. vDSP_vadd(src0_ptr + r*ne10, 1, src1_ptr, 1, dst_ptr + r*ne10, 1, ne10);
  3549. #else
  3550. ggml_vec_add_f32(ne10, dst_ptr + r*ne10, src0_ptr + r*ne10, src1_ptr);
  3551. #endif
  3552. }
  3553. }
  3554. } else {
  3555. // src1 is not contiguous
  3556. for (int ir = ir0; ir < ir1; ++ir) {
  3557. // src1 is broadcastable across src0 and dst in i1, i2, i3
  3558. const int64_t i03 = ir/(ne02*ne01);
  3559. const int64_t i02 = (ir - i03*ne02*ne01)/ne01;
  3560. const int64_t i01 = (ir - i03*ne02*ne01 - i02*ne01);
  3561. const int64_t i13 = i03 % ne13;
  3562. const int64_t i12 = i02 % ne12;
  3563. const int64_t i11 = i01 % ne11;
  3564. float * dst_ptr = (float *) ((char *) dst->data + i03*nb3 + i02*nb2 + i01*nb1 );
  3565. float * src0_ptr = (float *) ((char *) src0->data + i03*nb03 + i02*nb02 + i01*nb01);
  3566. for (int64_t i0 = 0; i0 < ne0; ++i0) {
  3567. const int64_t i10 = i0 % ne10;
  3568. float * src1_ptr = (float *) ((char *) src1->data + i13*nb13 + i12*nb12 + i11*nb11 + i10*nb10);
  3569. dst_ptr[i0] = src0_ptr[i0] + *src1_ptr;
  3570. }
  3571. }
  3572. }
  3573. }
  3574. static void ggml_compute_forward_add_f16_f32(
  3575. const struct ggml_compute_params * params,
  3576. struct ggml_tensor * dst) {
  3577. const struct ggml_tensor * src0 = dst->src[0];
  3578. const struct ggml_tensor * src1 = dst->src[1];
  3579. GGML_ASSERT(ggml_are_same_shape(src0, src1) && ggml_are_same_shape(src0, dst));
  3580. const int ith = params->ith;
  3581. const int nth = params->nth;
  3582. const int nr = ggml_nrows(src0);
  3583. GGML_TENSOR_BINARY_OP_LOCALS
  3584. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  3585. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  3586. if (dst->type == GGML_TYPE_F32) {
  3587. GGML_ASSERT( nb0 == sizeof(float));
  3588. }
  3589. else {
  3590. GGML_ASSERT(dst->type == GGML_TYPE_F16);
  3591. GGML_ASSERT( nb0 == sizeof(ggml_fp16_t));
  3592. }
  3593. GGML_ASSERT(nb00 == sizeof(ggml_fp16_t));
  3594. // rows per thread
  3595. const int dr = (nr + nth - 1)/nth;
  3596. // row range for this thread
  3597. const int ir0 = dr*ith;
  3598. const int ir1 = MIN(ir0 + dr, nr);
  3599. if (nb10 == sizeof(float)) {
  3600. if (dst->type == GGML_TYPE_F16) {
  3601. for (int ir = ir0; ir < ir1; ++ir) {
  3602. // src0, src1 and dst are same shape => same indices
  3603. const int i3 = ir/(ne2*ne1);
  3604. const int i2 = (ir - i3*ne2*ne1)/ne1;
  3605. const int i1 = (ir - i3*ne2*ne1 - i2*ne1);
  3606. ggml_fp16_t * dst_ptr = (ggml_fp16_t *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1);
  3607. ggml_fp16_t * src0_ptr = (ggml_fp16_t *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01);
  3608. float * src1_ptr = (float *) ((char *) src1->data + i3*nb13 + i2*nb12 + i1*nb11);
  3609. for (int i = 0; i < ne0; i++) {
  3610. dst_ptr[i] = GGML_FP32_TO_FP16(GGML_FP16_TO_FP32(src0_ptr[i]) + src1_ptr[i]);
  3611. }
  3612. }
  3613. } else {
  3614. for (int ir = ir0; ir < ir1; ++ir) {
  3615. // src0, src1 and dst are same shape => same indices
  3616. const int i3 = ir/(ne2*ne1);
  3617. const int i2 = (ir - i3*ne2*ne1)/ne1;
  3618. const int i1 = (ir - i3*ne2*ne1 - i2*ne1);
  3619. float * dst_ptr = (float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1);
  3620. ggml_fp16_t * src0_ptr = (ggml_fp16_t *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01);
  3621. float * src1_ptr = (float *) ((char *) src1->data + i3*nb13 + i2*nb12 + i1*nb11);
  3622. for (int i = 0; i < ne0; i++) {
  3623. dst_ptr[i] = GGML_FP16_TO_FP32(src0_ptr[i]) + src1_ptr[i];
  3624. }
  3625. }
  3626. }
  3627. }
  3628. else {
  3629. // src1 is not contiguous
  3630. GGML_ABORT("fatal error");
  3631. }
  3632. }
  3633. static void ggml_compute_forward_add_bf16_f32(
  3634. const struct ggml_compute_params * params,
  3635. struct ggml_tensor * dst) {
  3636. const struct ggml_tensor * src0 = dst->src[0];
  3637. const struct ggml_tensor * src1 = dst->src[1];
  3638. GGML_ASSERT(ggml_are_same_shape(src0, src1) && ggml_are_same_shape(src0, dst));
  3639. const int ith = params->ith;
  3640. const int nth = params->nth;
  3641. const int nr = ggml_nrows(src0);
  3642. GGML_TENSOR_BINARY_OP_LOCALS
  3643. GGML_ASSERT(src0->type == GGML_TYPE_BF16);
  3644. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  3645. if (dst->type == GGML_TYPE_F32) {
  3646. GGML_ASSERT( nb0 == sizeof(float));
  3647. }
  3648. else {
  3649. GGML_ASSERT(dst->type == GGML_TYPE_BF16);
  3650. GGML_ASSERT( nb0 == sizeof(ggml_bf16_t));
  3651. }
  3652. GGML_ASSERT(nb00 == sizeof(ggml_bf16_t));
  3653. // rows per thread
  3654. const int dr = (nr + nth - 1)/nth;
  3655. // row range for this thread
  3656. const int ir0 = dr*ith;
  3657. const int ir1 = MIN(ir0 + dr, nr);
  3658. if (nb10 == sizeof(float)) {
  3659. if (dst->type == GGML_TYPE_BF16) {
  3660. for (int ir = ir0; ir < ir1; ++ir) {
  3661. // src0, src1 and dst are same shape => same indices
  3662. const int i3 = ir/(ne2*ne1);
  3663. const int i2 = (ir - i3*ne2*ne1)/ne1;
  3664. const int i1 = (ir - i3*ne2*ne1 - i2*ne1);
  3665. ggml_bf16_t * dst_ptr = (ggml_bf16_t *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1);
  3666. ggml_bf16_t * src0_ptr = (ggml_bf16_t *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01);
  3667. float * src1_ptr = (float *) ((char *) src1->data + i3*nb13 + i2*nb12 + i1*nb11);
  3668. for (int i = 0; i < ne0; i++) {
  3669. dst_ptr[i] = GGML_FP32_TO_BF16(GGML_BF16_TO_FP32(src0_ptr[i]) + src1_ptr[i]);
  3670. }
  3671. }
  3672. } else {
  3673. for (int ir = ir0; ir < ir1; ++ir) {
  3674. // src0, src1 and dst are same shape => same indices
  3675. const int i3 = ir/(ne2*ne1);
  3676. const int i2 = (ir - i3*ne2*ne1)/ne1;
  3677. const int i1 = (ir - i3*ne2*ne1 - i2*ne1);
  3678. float * dst_ptr = (float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1);
  3679. ggml_bf16_t * src0_ptr = (ggml_bf16_t *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01);
  3680. float * src1_ptr = (float *) ((char *) src1->data + i3*nb13 + i2*nb12 + i1*nb11);
  3681. for (int i = 0; i < ne0; i++) {
  3682. dst_ptr[i] = GGML_BF16_TO_FP32(src0_ptr[i]) + src1_ptr[i];
  3683. }
  3684. }
  3685. }
  3686. }
  3687. else {
  3688. // src1 is not contiguous
  3689. GGML_ABORT("fatal error");
  3690. }
  3691. }
  3692. static void ggml_compute_forward_add_f16_f16(
  3693. const struct ggml_compute_params * params,
  3694. struct ggml_tensor * dst) {
  3695. const struct ggml_tensor * src0 = dst->src[0];
  3696. const struct ggml_tensor * src1 = dst->src[1];
  3697. GGML_ASSERT(ggml_are_same_shape(src0, src1) && ggml_are_same_shape(src0, dst));
  3698. const int ith = params->ith;
  3699. const int nth = params->nth;
  3700. const int nr = ggml_nrows(src0);
  3701. GGML_TENSOR_BINARY_OP_LOCALS
  3702. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  3703. GGML_ASSERT(src1->type == GGML_TYPE_F16);
  3704. GGML_ASSERT(dst->type == GGML_TYPE_F16);
  3705. GGML_ASSERT( nb0 == sizeof(ggml_fp16_t));
  3706. GGML_ASSERT(nb00 == sizeof(ggml_fp16_t));
  3707. // rows per thread
  3708. const int dr = (nr + nth - 1)/nth;
  3709. // row range for this thread
  3710. const int ir0 = dr*ith;
  3711. const int ir1 = MIN(ir0 + dr, nr);
  3712. if (nb10 == sizeof(ggml_fp16_t)) {
  3713. for (int ir = ir0; ir < ir1; ++ir) {
  3714. // src0, src1 and dst are same shape => same indices
  3715. const int i3 = ir/(ne2*ne1);
  3716. const int i2 = (ir - i3*ne2*ne1)/ne1;
  3717. const int i1 = (ir - i3*ne2*ne1 - i2*ne1);
  3718. ggml_fp16_t * dst_ptr = (ggml_fp16_t *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1);
  3719. ggml_fp16_t * src0_ptr = (ggml_fp16_t *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01);
  3720. ggml_fp16_t * src1_ptr = (ggml_fp16_t *) ((char *) src1->data + i3*nb13 + i2*nb12 + i1*nb11);
  3721. for (int i = 0; i < ne0; i++) {
  3722. dst_ptr[i] = GGML_FP32_TO_FP16(GGML_FP16_TO_FP32(src0_ptr[i]) + GGML_FP16_TO_FP32(src1_ptr[i]));
  3723. }
  3724. }
  3725. }
  3726. else {
  3727. // src1 is not contiguous
  3728. GGML_ABORT("fatal error");
  3729. }
  3730. }
  3731. static void ggml_compute_forward_add_bf16_bf16(
  3732. const struct ggml_compute_params * params,
  3733. struct ggml_tensor * dst) {
  3734. const struct ggml_tensor * src0 = dst->src[0];
  3735. const struct ggml_tensor * src1 = dst->src[1];
  3736. GGML_ASSERT(ggml_are_same_shape(src0, src1) && ggml_are_same_shape(src0, dst));
  3737. const int ith = params->ith;
  3738. const int nth = params->nth;
  3739. const int nr = ggml_nrows(src0);
  3740. GGML_TENSOR_BINARY_OP_LOCALS
  3741. GGML_ASSERT(src0->type == GGML_TYPE_BF16);
  3742. GGML_ASSERT(src1->type == GGML_TYPE_BF16);
  3743. GGML_ASSERT(dst->type == GGML_TYPE_BF16);
  3744. GGML_ASSERT( nb0 == sizeof(ggml_bf16_t));
  3745. GGML_ASSERT(nb00 == sizeof(ggml_bf16_t));
  3746. // rows per thread
  3747. const int dr = (nr + nth - 1)/nth;
  3748. // row range for this thread
  3749. const int ir0 = dr*ith;
  3750. const int ir1 = MIN(ir0 + dr, nr);
  3751. if (nb10 == sizeof(ggml_bf16_t)) {
  3752. for (int ir = ir0; ir < ir1; ++ir) {
  3753. // src0, src1 and dst are same shape => same indices
  3754. const int i3 = ir/(ne2*ne1);
  3755. const int i2 = (ir - i3*ne2*ne1)/ne1;
  3756. const int i1 = (ir - i3*ne2*ne1 - i2*ne1);
  3757. ggml_bf16_t * dst_ptr = (ggml_bf16_t *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1);
  3758. ggml_bf16_t * src0_ptr = (ggml_bf16_t *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01);
  3759. ggml_bf16_t * src1_ptr = (ggml_bf16_t *) ((char *) src1->data + i3*nb13 + i2*nb12 + i1*nb11);
  3760. for (int i = 0; i < ne0; i++) {
  3761. dst_ptr[i] = GGML_FP32_TO_BF16(GGML_BF16_TO_FP32(src0_ptr[i]) + GGML_BF16_TO_FP32(src1_ptr[i]));
  3762. }
  3763. }
  3764. }
  3765. else {
  3766. // src1 is not contiguous
  3767. GGML_ABORT("fatal error");
  3768. }
  3769. }
  3770. static void ggml_compute_forward_add_q_f32(
  3771. const struct ggml_compute_params * params,
  3772. struct ggml_tensor * dst) {
  3773. const struct ggml_tensor * src0 = dst->src[0];
  3774. const struct ggml_tensor * src1 = dst->src[1];
  3775. GGML_ASSERT(ggml_are_same_shape(src0, src1) && ggml_are_same_shape(src0, dst));
  3776. const int nr = ggml_nrows(src0);
  3777. GGML_TENSOR_BINARY_OP_LOCALS
  3778. const int ith = params->ith;
  3779. const int nth = params->nth;
  3780. const enum ggml_type type = src0->type;
  3781. const enum ggml_type dtype = dst->type;
  3782. ggml_to_float_t const dequantize_row_q = ggml_get_type_traits(type)->to_float;
  3783. ggml_from_float_t const quantize_row_q = ggml_get_type_traits_cpu(dtype)->from_float;
  3784. // we don't support permuted src0 or src1
  3785. GGML_ASSERT(nb00 == ggml_type_size(type));
  3786. GGML_ASSERT(nb10 == sizeof(float));
  3787. // dst cannot be transposed or permuted
  3788. GGML_ASSERT(nb0 <= nb1);
  3789. GGML_ASSERT(nb1 <= nb2);
  3790. GGML_ASSERT(nb2 <= nb3);
  3791. GGML_ASSERT(ggml_is_quantized(src0->type));
  3792. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  3793. // rows per thread
  3794. const int dr = (nr + nth - 1)/nth;
  3795. // row range for this thread
  3796. const int ir0 = dr*ith;
  3797. const int ir1 = MIN(ir0 + dr, nr);
  3798. float * wdata = (float *) params->wdata + (ne00 + CACHE_LINE_SIZE_F32) * ith;
  3799. for (int ir = ir0; ir < ir1; ++ir) {
  3800. // src0 indices
  3801. const int i03 = ir/(ne02*ne01);
  3802. const int i02 = (ir - i03*ne02*ne01)/ne01;
  3803. const int i01 = (ir - i03*ne02*ne01 - i02*ne01);
  3804. // src1 and dst are same shape as src0 => same indices
  3805. const int i13 = i03;
  3806. const int i12 = i02;
  3807. const int i11 = i01;
  3808. const int i3 = i03;
  3809. const int i2 = i02;
  3810. const int i1 = i01;
  3811. void * src0_row = (void *) ((char *) src0->data + (i01*nb01 + i02*nb02 + i03*nb03));
  3812. float * src1_row = (float *)((char *) src1->data + (i11*nb11 + i12*nb12 + i13*nb13));
  3813. void * dst_row = (void *) ((char *) dst->data + ( i1*nb1 + i2*nb2 + i3*nb3));
  3814. assert(ne00 % 32 == 0);
  3815. // unquantize row from src0 to temp buffer
  3816. dequantize_row_q(src0_row, wdata, ne00);
  3817. // add src1
  3818. ggml_vec_acc_f32(ne00, wdata, src1_row);
  3819. // quantize row to dst
  3820. if (quantize_row_q != NULL) {
  3821. quantize_row_q(wdata, dst_row, ne00);
  3822. } else {
  3823. memcpy(dst_row, wdata, ne0*nb0);
  3824. }
  3825. }
  3826. }
  3827. static void ggml_compute_forward_add(
  3828. const struct ggml_compute_params * params,
  3829. struct ggml_tensor * dst) {
  3830. const struct ggml_tensor * src0 = dst->src[0];
  3831. const struct ggml_tensor * src1 = dst->src[1];
  3832. switch (src0->type) {
  3833. case GGML_TYPE_F32:
  3834. {
  3835. if (src1->type == GGML_TYPE_F32) {
  3836. ggml_compute_forward_add_f32(params, dst);
  3837. }
  3838. else {
  3839. GGML_ABORT("fatal error");
  3840. }
  3841. } break;
  3842. case GGML_TYPE_F16:
  3843. {
  3844. if (src1->type == GGML_TYPE_F16) {
  3845. ggml_compute_forward_add_f16_f16(params, dst);
  3846. }
  3847. else if (src1->type == GGML_TYPE_F32) {
  3848. ggml_compute_forward_add_f16_f32(params, dst);
  3849. }
  3850. else {
  3851. GGML_ABORT("fatal error");
  3852. }
  3853. } break;
  3854. case GGML_TYPE_BF16:
  3855. {
  3856. if (src1->type == GGML_TYPE_BF16) {
  3857. ggml_compute_forward_add_bf16_bf16(params, dst);
  3858. }
  3859. else if (src1->type == GGML_TYPE_F32) {
  3860. ggml_compute_forward_add_bf16_f32(params, dst);
  3861. }
  3862. else {
  3863. GGML_ABORT("fatal error");
  3864. }
  3865. } break;
  3866. case GGML_TYPE_Q4_0:
  3867. case GGML_TYPE_Q4_1:
  3868. case GGML_TYPE_Q5_0:
  3869. case GGML_TYPE_Q5_1:
  3870. case GGML_TYPE_Q8_0:
  3871. case GGML_TYPE_Q2_K:
  3872. case GGML_TYPE_Q3_K:
  3873. case GGML_TYPE_Q4_K:
  3874. case GGML_TYPE_Q5_K:
  3875. case GGML_TYPE_Q6_K:
  3876. case GGML_TYPE_TQ1_0:
  3877. case GGML_TYPE_TQ2_0:
  3878. case GGML_TYPE_IQ2_XXS:
  3879. case GGML_TYPE_IQ2_XS:
  3880. case GGML_TYPE_IQ3_XXS:
  3881. case GGML_TYPE_IQ1_S:
  3882. case GGML_TYPE_IQ1_M:
  3883. case GGML_TYPE_IQ4_NL:
  3884. case GGML_TYPE_IQ4_XS:
  3885. case GGML_TYPE_IQ3_S:
  3886. case GGML_TYPE_IQ2_S:
  3887. {
  3888. ggml_compute_forward_add_q_f32(params, dst);
  3889. } break;
  3890. default:
  3891. {
  3892. GGML_ABORT("fatal error");
  3893. }
  3894. }
  3895. }
  3896. // ggml_compute_forward_add1
  3897. static void ggml_compute_forward_add1_f32(
  3898. const struct ggml_compute_params * params,
  3899. struct ggml_tensor * dst) {
  3900. const struct ggml_tensor * src0 = dst->src[0];
  3901. const struct ggml_tensor * src1 = dst->src[1];
  3902. GGML_ASSERT(ggml_are_same_shape(src0, dst));
  3903. GGML_ASSERT(ggml_is_scalar(src1));
  3904. const int ith = params->ith;
  3905. const int nth = params->nth;
  3906. const int nr = ggml_nrows(src0);
  3907. GGML_TENSOR_UNARY_OP_LOCALS
  3908. GGML_ASSERT( nb0 == sizeof(float));
  3909. GGML_ASSERT(nb00 == sizeof(float));
  3910. // rows per thread
  3911. const int dr = (nr + nth - 1)/nth;
  3912. // row range for this thread
  3913. const int ir0 = dr*ith;
  3914. const int ir1 = MIN(ir0 + dr, nr);
  3915. for (int ir = ir0; ir < ir1; ++ir) {
  3916. // src0 and dst are same shape => same indices
  3917. const int i3 = ir/(ne2*ne1);
  3918. const int i2 = (ir - i3*ne2*ne1)/ne1;
  3919. const int i1 = (ir - i3*ne2*ne1 - i2*ne1);
  3920. #ifdef GGML_USE_ACCELERATE
  3921. UNUSED(ggml_vec_add1_f32);
  3922. vDSP_vadd(
  3923. (float *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01), 1,
  3924. (float *) ((char *) src1->data), 0,
  3925. (float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 ), 1,
  3926. ne0);
  3927. #else
  3928. ggml_vec_add1_f32(ne0,
  3929. (float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 ),
  3930. (float *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01),
  3931. *(float *) src1->data);
  3932. #endif
  3933. }
  3934. }
  3935. static void ggml_compute_forward_add1_f16_f32(
  3936. const struct ggml_compute_params * params,
  3937. struct ggml_tensor * dst) {
  3938. const struct ggml_tensor * src0 = dst->src[0];
  3939. const struct ggml_tensor * src1 = dst->src[1];
  3940. GGML_ASSERT(ggml_are_same_shape(src0, dst));
  3941. GGML_ASSERT(ggml_is_scalar(src1));
  3942. // scalar to add
  3943. const float v = *(float *) src1->data;
  3944. const int ith = params->ith;
  3945. const int nth = params->nth;
  3946. const int nr = ggml_nrows(src0);
  3947. GGML_TENSOR_UNARY_OP_LOCALS
  3948. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  3949. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  3950. GGML_ASSERT(dst->type == GGML_TYPE_F16);
  3951. GGML_ASSERT( nb0 == sizeof(ggml_fp16_t));
  3952. GGML_ASSERT(nb00 == sizeof(ggml_fp16_t));
  3953. // rows per thread
  3954. const int dr = (nr + nth - 1)/nth;
  3955. // row range for this thread
  3956. const int ir0 = dr*ith;
  3957. const int ir1 = MIN(ir0 + dr, nr);
  3958. for (int ir = ir0; ir < ir1; ++ir) {
  3959. // src0 and dst are same shape => same indices
  3960. const int i3 = ir/(ne2*ne1);
  3961. const int i2 = (ir - i3*ne2*ne1)/ne1;
  3962. const int i1 = (ir - i3*ne2*ne1 - i2*ne1);
  3963. ggml_fp16_t * dst_ptr = (ggml_fp16_t *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 );
  3964. ggml_fp16_t * src0_ptr = (ggml_fp16_t *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01);
  3965. for (int i = 0; i < ne0; i++) {
  3966. dst_ptr[i] = GGML_FP32_TO_FP16(GGML_FP16_TO_FP32(src0_ptr[i]) + v);
  3967. }
  3968. }
  3969. }
  3970. static void ggml_compute_forward_add1_f16_f16(
  3971. const struct ggml_compute_params * params,
  3972. struct ggml_tensor * dst) {
  3973. const struct ggml_tensor * src0 = dst->src[0];
  3974. const struct ggml_tensor * src1 = dst->src[1];
  3975. GGML_ASSERT(ggml_are_same_shape(src0, dst));
  3976. GGML_ASSERT(ggml_is_scalar(src1));
  3977. // scalar to add
  3978. const float v = GGML_FP16_TO_FP32(*(ggml_fp16_t *) src1->data);
  3979. const int ith = params->ith;
  3980. const int nth = params->nth;
  3981. const int nr = ggml_nrows(src0);
  3982. GGML_TENSOR_UNARY_OP_LOCALS
  3983. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  3984. GGML_ASSERT(src1->type == GGML_TYPE_F16);
  3985. GGML_ASSERT(dst->type == GGML_TYPE_F16);
  3986. GGML_ASSERT( nb0 == sizeof(ggml_fp16_t));
  3987. GGML_ASSERT(nb00 == sizeof(ggml_fp16_t));
  3988. // rows per thread
  3989. const int dr = (nr + nth - 1)/nth;
  3990. // row range for this thread
  3991. const int ir0 = dr*ith;
  3992. const int ir1 = MIN(ir0 + dr, nr);
  3993. for (int ir = ir0; ir < ir1; ++ir) {
  3994. // src0 and dst are same shape => same indices
  3995. const int i3 = ir/(ne2*ne1);
  3996. const int i2 = (ir - i3*ne2*ne1)/ne1;
  3997. const int i1 = (ir - i3*ne2*ne1 - i2*ne1);
  3998. ggml_fp16_t * dst_ptr = (ggml_fp16_t *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 );
  3999. ggml_fp16_t * src0_ptr = (ggml_fp16_t *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01);
  4000. for (int i = 0; i < ne0; i++) {
  4001. dst_ptr[i] = GGML_FP32_TO_FP16(GGML_FP16_TO_FP32(src0_ptr[i]) + v);
  4002. }
  4003. }
  4004. }
  4005. static void ggml_compute_forward_add1_q_f32(
  4006. const struct ggml_compute_params * params,
  4007. struct ggml_tensor * dst) {
  4008. const struct ggml_tensor * src0 = dst->src[0];
  4009. const struct ggml_tensor * src1 = dst->src[1];
  4010. GGML_ASSERT(ggml_are_same_shape(src0, dst));
  4011. GGML_ASSERT(ggml_is_scalar(src1));
  4012. // scalar to add
  4013. const float v = *(float *) src1->data;
  4014. const int ith = params->ith;
  4015. const int nth = params->nth;
  4016. const int nr = ggml_nrows(src0);
  4017. GGML_TENSOR_UNARY_OP_LOCALS
  4018. const enum ggml_type type = src0->type;
  4019. ggml_to_float_t const dequantize_row_q = ggml_get_type_traits(type)->to_float;
  4020. ggml_from_float_t const quantize_row_q = ggml_get_type_traits_cpu(type)->from_float;
  4021. // we don't support permuted src0
  4022. GGML_ASSERT(nb00 == ggml_type_size(type));
  4023. // dst cannot be transposed or permuted
  4024. GGML_ASSERT(nb0 <= nb1);
  4025. GGML_ASSERT(nb1 <= nb2);
  4026. GGML_ASSERT(nb2 <= nb3);
  4027. GGML_ASSERT(ggml_is_quantized(src0->type));
  4028. GGML_ASSERT(dst->type == src0->type);
  4029. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  4030. // rows per thread
  4031. const int dr = (nr + nth - 1)/nth;
  4032. // row range for this thread
  4033. const int ir0 = dr*ith;
  4034. const int ir1 = MIN(ir0 + dr, nr);
  4035. float * wdata = (float *) params->wdata + (ne0 + CACHE_LINE_SIZE_F32) * ith;
  4036. for (int ir = ir0; ir < ir1; ++ir) {
  4037. // src0 and dst are same shape => same indices
  4038. const int i3 = ir/(ne2*ne1);
  4039. const int i2 = (ir - i3*ne2*ne1)/ne1;
  4040. const int i1 = (ir - i3*ne2*ne1 - i2*ne1);
  4041. void * src0_row = (void *) ((char *) src0->data + (i1*nb01 + i2*nb02 + i3*nb03));
  4042. void * dst_row = (void *) ((char *) dst->data + (i1*nb1 + i2*nb2 + i3*nb0 ));
  4043. assert(ne0 % 32 == 0);
  4044. // unquantize row from src0 to temp buffer
  4045. dequantize_row_q(src0_row, wdata, ne0);
  4046. // add src1
  4047. ggml_vec_acc1_f32(ne0, wdata, v);
  4048. // quantize row to dst
  4049. quantize_row_q(wdata, dst_row, ne0);
  4050. }
  4051. }
  4052. static void ggml_compute_forward_add1_bf16_f32(
  4053. const struct ggml_compute_params * params,
  4054. struct ggml_tensor * dst) {
  4055. const struct ggml_tensor * src0 = dst->src[0];
  4056. const struct ggml_tensor * src1 = dst->src[1];
  4057. GGML_ASSERT(ggml_are_same_shape(src0, dst));
  4058. GGML_ASSERT(ggml_is_scalar(src1));
  4059. // scalar to add
  4060. const float v = *(float *) src1->data;
  4061. const int ith = params->ith;
  4062. const int nth = params->nth;
  4063. const int nr = ggml_nrows(src0);
  4064. GGML_TENSOR_UNARY_OP_LOCALS
  4065. GGML_ASSERT(src0->type == GGML_TYPE_BF16);
  4066. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  4067. GGML_ASSERT(dst->type == GGML_TYPE_BF16);
  4068. GGML_ASSERT( nb0 == sizeof(ggml_bf16_t));
  4069. GGML_ASSERT(nb00 == sizeof(ggml_bf16_t));
  4070. // rows per thread
  4071. const int dr = (nr + nth - 1)/nth;
  4072. // row range for this thread
  4073. const int ir0 = dr*ith;
  4074. const int ir1 = MIN(ir0 + dr, nr);
  4075. for (int ir = ir0; ir < ir1; ++ir) {
  4076. // src0 and dst are same shape => same indices
  4077. const int i3 = ir/(ne2*ne1);
  4078. const int i2 = (ir - i3*ne2*ne1)/ne1;
  4079. const int i1 = (ir - i3*ne2*ne1 - i2*ne1);
  4080. ggml_bf16_t * dst_ptr = (ggml_bf16_t *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 );
  4081. ggml_bf16_t * src0_ptr = (ggml_bf16_t *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01);
  4082. for (int i = 0; i < ne0; i++) {
  4083. dst_ptr[i] = GGML_FP32_TO_BF16(GGML_BF16_TO_FP32(src0_ptr[i]) + v);
  4084. }
  4085. }
  4086. }
  4087. static void ggml_compute_forward_add1_bf16_bf16(
  4088. const struct ggml_compute_params * params,
  4089. struct ggml_tensor * dst) {
  4090. const struct ggml_tensor * src0 = dst->src[0];
  4091. const struct ggml_tensor * src1 = dst->src[1];
  4092. GGML_ASSERT(ggml_are_same_shape(src0, dst));
  4093. GGML_ASSERT(ggml_is_scalar(src1));
  4094. // scalar to add
  4095. const float v = GGML_BF16_TO_FP32(*(ggml_bf16_t *) src1->data);
  4096. const int ith = params->ith;
  4097. const int nth = params->nth;
  4098. const int nr = ggml_nrows(src0);
  4099. GGML_TENSOR_UNARY_OP_LOCALS
  4100. GGML_ASSERT(src0->type == GGML_TYPE_BF16);
  4101. GGML_ASSERT(src1->type == GGML_TYPE_BF16);
  4102. GGML_ASSERT(dst->type == GGML_TYPE_BF16);
  4103. GGML_ASSERT( nb0 == sizeof(ggml_bf16_t));
  4104. GGML_ASSERT(nb00 == sizeof(ggml_bf16_t));
  4105. // rows per thread
  4106. const int dr = (nr + nth - 1)/nth;
  4107. // row range for this thread
  4108. const int ir0 = dr*ith;
  4109. const int ir1 = MIN(ir0 + dr, nr);
  4110. for (int ir = ir0; ir < ir1; ++ir) {
  4111. // src0 and dst are same shape => same indices
  4112. const int i3 = ir/(ne2*ne1);
  4113. const int i2 = (ir - i3*ne2*ne1)/ne1;
  4114. const int i1 = (ir - i3*ne2*ne1 - i2*ne1);
  4115. ggml_bf16_t * dst_ptr = (ggml_bf16_t *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 );
  4116. ggml_bf16_t * src0_ptr = (ggml_bf16_t *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01);
  4117. for (int i = 0; i < ne0; i++) {
  4118. dst_ptr[i] = GGML_FP32_TO_BF16(GGML_BF16_TO_FP32(src0_ptr[i]) + v);
  4119. }
  4120. }
  4121. }
  4122. static void ggml_compute_forward_add1(
  4123. const struct ggml_compute_params * params,
  4124. struct ggml_tensor * dst) {
  4125. const struct ggml_tensor * src0 = dst->src[0];
  4126. const struct ggml_tensor * src1 = dst->src[1];
  4127. switch (src0->type) {
  4128. case GGML_TYPE_F32:
  4129. {
  4130. ggml_compute_forward_add1_f32(params, dst);
  4131. } break;
  4132. case GGML_TYPE_F16:
  4133. {
  4134. if (src1->type == GGML_TYPE_F16) {
  4135. ggml_compute_forward_add1_f16_f16(params, dst);
  4136. }
  4137. else if (src1->type == GGML_TYPE_F32) {
  4138. ggml_compute_forward_add1_f16_f32(params, dst);
  4139. }
  4140. else {
  4141. GGML_ABORT("fatal error");
  4142. }
  4143. } break;
  4144. case GGML_TYPE_BF16:
  4145. {
  4146. if (src1->type == GGML_TYPE_BF16) {
  4147. ggml_compute_forward_add1_bf16_bf16(params, dst);
  4148. }
  4149. else if (src1->type == GGML_TYPE_F32) {
  4150. ggml_compute_forward_add1_bf16_f32(params, dst);
  4151. }
  4152. else {
  4153. GGML_ABORT("fatal error");
  4154. }
  4155. } break;
  4156. case GGML_TYPE_Q4_0:
  4157. case GGML_TYPE_Q4_1:
  4158. case GGML_TYPE_Q5_0:
  4159. case GGML_TYPE_Q5_1:
  4160. case GGML_TYPE_Q8_0:
  4161. case GGML_TYPE_Q8_1:
  4162. case GGML_TYPE_Q2_K:
  4163. case GGML_TYPE_Q3_K:
  4164. case GGML_TYPE_Q4_K:
  4165. case GGML_TYPE_Q5_K:
  4166. case GGML_TYPE_Q6_K:
  4167. case GGML_TYPE_TQ1_0:
  4168. case GGML_TYPE_TQ2_0:
  4169. case GGML_TYPE_IQ2_XXS:
  4170. case GGML_TYPE_IQ2_XS:
  4171. case GGML_TYPE_IQ3_XXS:
  4172. case GGML_TYPE_IQ1_S:
  4173. case GGML_TYPE_IQ1_M:
  4174. case GGML_TYPE_IQ4_NL:
  4175. case GGML_TYPE_IQ4_XS:
  4176. case GGML_TYPE_IQ3_S:
  4177. case GGML_TYPE_IQ2_S:
  4178. {
  4179. ggml_compute_forward_add1_q_f32(params, dst);
  4180. } break;
  4181. default:
  4182. {
  4183. GGML_ABORT("fatal error");
  4184. }
  4185. }
  4186. }
  4187. // ggml_compute_forward_acc
  4188. static void ggml_compute_forward_acc_f32(
  4189. const struct ggml_compute_params * params,
  4190. struct ggml_tensor * dst) {
  4191. const struct ggml_tensor * src0 = dst->src[0];
  4192. const struct ggml_tensor * src1 = dst->src[1];
  4193. GGML_ASSERT(ggml_are_same_shape(src0, dst));
  4194. GGML_ASSERT(ggml_is_contiguous(dst) && ggml_is_contiguous(src0));
  4195. // view src0 and dst with these strides and data offset inbytes during acc
  4196. // nb0 is implicitly element_size because src0 and dst are contiguous
  4197. size_t nb1 = ((int32_t *) dst->op_params)[0];
  4198. size_t nb2 = ((int32_t *) dst->op_params)[1];
  4199. size_t nb3 = ((int32_t *) dst->op_params)[2];
  4200. size_t offset = ((int32_t *) dst->op_params)[3];
  4201. bool inplace = (bool) ((int32_t *) dst->op_params)[4];
  4202. if (!inplace) {
  4203. if (params->ith == 0) {
  4204. // memcpy needs to be synchronized across threads to avoid race conditions.
  4205. // => do it in INIT phase
  4206. memcpy(
  4207. ((char *) dst->data),
  4208. ((char *) src0->data),
  4209. ggml_nbytes(dst));
  4210. }
  4211. ggml_barrier(params->threadpool);
  4212. }
  4213. const int ith = params->ith;
  4214. const int nth = params->nth;
  4215. const int nr = ggml_nrows(src1);
  4216. const int nc = src1->ne[0];
  4217. GGML_TENSOR_LOCALS(int64_t, ne1, src1, ne)
  4218. GGML_TENSOR_LOCALS(size_t, nb1, src1, nb)
  4219. // src0 and dst as viewed during acc
  4220. const size_t nb0 = ggml_element_size(src0);
  4221. const size_t nb00 = nb0;
  4222. const size_t nb01 = nb1;
  4223. const size_t nb02 = nb2;
  4224. const size_t nb03 = nb3;
  4225. GGML_ASSERT(offset + (ne10 == 0 ? 0 : ne10-1)*nb0 + (ne11 == 0 ? 0 : ne11-1)*nb1 + (ne12 == 0 ? 0 : ne12-1)*nb2 + (ne13 == 0 ? 0 : ne13-1)*nb3 < ggml_nbytes(dst));
  4226. GGML_ASSERT(offset + (ne10 == 0 ? 0 : ne10-1)*nb00 + (ne11 == 0 ? 0 : ne11-1)*nb01 + (ne12 == 0 ? 0 : ne12-1)*nb02 + (ne13 == 0 ? 0 : ne13-1)*nb03 < ggml_nbytes(src0));
  4227. GGML_ASSERT(nb10 == sizeof(float));
  4228. // rows per thread
  4229. const int dr = (nr + nth - 1)/nth;
  4230. // row range for this thread
  4231. const int ir0 = dr*ith;
  4232. const int ir1 = MIN(ir0 + dr, nr);
  4233. for (int ir = ir0; ir < ir1; ++ir) {
  4234. // src0 and dst are viewed with shape of src1 and offset
  4235. // => same indices
  4236. const int i3 = ir/(ne12*ne11);
  4237. const int i2 = (ir - i3*ne12*ne11)/ne11;
  4238. const int i1 = (ir - i3*ne12*ne11 - i2*ne11);
  4239. #ifdef GGML_USE_ACCELERATE
  4240. vDSP_vadd(
  4241. (float *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01 + offset), 1,
  4242. (float *) ((char *) src1->data + i3*nb13 + i2*nb12 + i1*nb11), 1,
  4243. (float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + offset), 1, nc);
  4244. #else
  4245. ggml_vec_add_f32(nc,
  4246. (float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + offset),
  4247. (float *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01 + offset),
  4248. (float *) ((char *) src1->data + i3*nb13 + i2*nb12 + i1*nb11));
  4249. #endif
  4250. }
  4251. }
  4252. static void ggml_compute_forward_acc(
  4253. const struct ggml_compute_params * params,
  4254. struct ggml_tensor * dst) {
  4255. const struct ggml_tensor * src0 = dst->src[0];
  4256. switch (src0->type) {
  4257. case GGML_TYPE_F32:
  4258. {
  4259. ggml_compute_forward_acc_f32(params, dst);
  4260. } break;
  4261. case GGML_TYPE_F16:
  4262. case GGML_TYPE_BF16:
  4263. case GGML_TYPE_Q4_0:
  4264. case GGML_TYPE_Q4_1:
  4265. case GGML_TYPE_Q5_0:
  4266. case GGML_TYPE_Q5_1:
  4267. case GGML_TYPE_Q8_0:
  4268. case GGML_TYPE_Q8_1:
  4269. case GGML_TYPE_Q2_K:
  4270. case GGML_TYPE_Q3_K:
  4271. case GGML_TYPE_Q4_K:
  4272. case GGML_TYPE_Q5_K:
  4273. case GGML_TYPE_Q6_K:
  4274. case GGML_TYPE_TQ1_0:
  4275. case GGML_TYPE_TQ2_0:
  4276. case GGML_TYPE_IQ2_XXS:
  4277. case GGML_TYPE_IQ2_XS:
  4278. case GGML_TYPE_IQ3_XXS:
  4279. case GGML_TYPE_IQ1_S:
  4280. case GGML_TYPE_IQ1_M:
  4281. case GGML_TYPE_IQ4_NL:
  4282. case GGML_TYPE_IQ4_XS:
  4283. case GGML_TYPE_IQ3_S:
  4284. case GGML_TYPE_IQ2_S:
  4285. default:
  4286. {
  4287. GGML_ABORT("fatal error");
  4288. }
  4289. }
  4290. }
  4291. // ggml_compute_forward_sub
  4292. static void ggml_compute_forward_sub_f32(
  4293. const struct ggml_compute_params * params,
  4294. struct ggml_tensor * dst) {
  4295. const struct ggml_tensor * src0 = dst->src[0];
  4296. const struct ggml_tensor * src1 = dst->src[1];
  4297. assert(ggml_can_repeat(src1, src0) && ggml_are_same_shape(src0, dst));
  4298. const int ith = params->ith;
  4299. const int nth = params->nth;
  4300. const int nr = ggml_nrows(src0);
  4301. GGML_TENSOR_BINARY_OP_LOCALS
  4302. GGML_ASSERT( nb0 == sizeof(float));
  4303. GGML_ASSERT(nb00 == sizeof(float));
  4304. // rows per thread
  4305. const int dr = (nr + nth - 1)/nth;
  4306. // row range for this thread
  4307. const int ir0 = dr*ith;
  4308. const int ir1 = MIN(ir0 + dr, nr);
  4309. if (nb10 == sizeof(float)) {
  4310. for (int ir = ir0; ir < ir1; ++ir) {
  4311. // src1 is broadcastable across src0 and dst in i1, i2, i3
  4312. const int64_t i03 = ir/(ne02*ne01);
  4313. const int64_t i02 = (ir - i03*ne02*ne01)/ne01;
  4314. const int64_t i01 = (ir - i03*ne02*ne01 - i02*ne01);
  4315. const int64_t i13 = i03 % ne13;
  4316. const int64_t i12 = i02 % ne12;
  4317. const int64_t i11 = i01 % ne11;
  4318. const int64_t nr0 = ne00 / ne10;
  4319. float * dst_ptr = (float *) ((char *) dst->data + i03*nb3 + i02*nb2 + i01*nb1 );
  4320. float * src0_ptr = (float *) ((char *) src0->data + i03*nb03 + i02*nb02 + i01*nb01);
  4321. float * src1_ptr = (float *) ((char *) src1->data + i13*nb13 + i12*nb12 + i11*nb11);
  4322. for (int64_t r = 0; r < nr0; ++r) {
  4323. #ifdef GGML_USE_ACCELERATE
  4324. vDSP_vsub(src1_ptr, 1, src0_ptr + r*ne10, 1, dst_ptr + r*ne10, 1, ne10);
  4325. #else
  4326. ggml_vec_sub_f32(ne10, dst_ptr + r*ne10, src0_ptr + r*ne10, src1_ptr);
  4327. #endif
  4328. }
  4329. }
  4330. } else {
  4331. // src1 is not contiguous
  4332. for (int ir = ir0; ir < ir1; ++ir) {
  4333. // src1 is broadcastable across src0 and dst in i1, i2, i3
  4334. const int64_t i03 = ir/(ne02*ne01);
  4335. const int64_t i02 = (ir - i03*ne02*ne01)/ne01;
  4336. const int64_t i01 = (ir - i03*ne02*ne01 - i02*ne01);
  4337. const int64_t i13 = i03 % ne13;
  4338. const int64_t i12 = i02 % ne12;
  4339. const int64_t i11 = i01 % ne11;
  4340. float * dst_ptr = (float *) ((char *) dst->data + i03*nb3 + i02*nb2 + i01*nb1 );
  4341. float * src0_ptr = (float *) ((char *) src0->data + i03*nb03 + i02*nb02 + i01*nb01);
  4342. for (int64_t i0 = 0; i0 < ne0; ++i0) {
  4343. const int64_t i10 = i0 % ne10;
  4344. float * src1_ptr = (float *) ((char *) src1->data + i13*nb13 + i12*nb12 + i11*nb11 + i10*nb10);
  4345. dst_ptr[i0] = src0_ptr[i0] - *src1_ptr;
  4346. }
  4347. }
  4348. }
  4349. }
  4350. static void ggml_compute_forward_sub(
  4351. const struct ggml_compute_params * params,
  4352. struct ggml_tensor * dst) {
  4353. const struct ggml_tensor * src0 = dst->src[0];
  4354. switch (src0->type) {
  4355. case GGML_TYPE_F32:
  4356. {
  4357. ggml_compute_forward_sub_f32(params, dst);
  4358. } break;
  4359. default:
  4360. {
  4361. GGML_ABORT("fatal error");
  4362. }
  4363. }
  4364. }
  4365. // ggml_compute_forward_mul
  4366. static void ggml_compute_forward_mul_f32(
  4367. const struct ggml_compute_params * params,
  4368. struct ggml_tensor * dst) {
  4369. const struct ggml_tensor * src0 = dst->src[0];
  4370. const struct ggml_tensor * src1 = dst->src[1];
  4371. GGML_ASSERT(ggml_can_repeat(src1, src0) && ggml_are_same_shape(src0, dst));
  4372. const int ith = params->ith;
  4373. const int nth = params->nth;
  4374. const int64_t nr = ggml_nrows(src0);
  4375. GGML_TENSOR_BINARY_OP_LOCALS
  4376. GGML_ASSERT( nb0 == sizeof(float));
  4377. GGML_ASSERT(nb00 == sizeof(float));
  4378. if (nb10 == sizeof(float)) {
  4379. for (int64_t ir = ith; ir < nr; ir += nth) {
  4380. // src0 and dst are same shape => same indices
  4381. const int64_t i03 = ir/(ne02*ne01);
  4382. const int64_t i02 = (ir - i03*ne02*ne01)/ne01;
  4383. const int64_t i01 = (ir - i03*ne02*ne01 - i02*ne01);
  4384. const int64_t i13 = i03 % ne13;
  4385. const int64_t i12 = i02 % ne12;
  4386. const int64_t i11 = i01 % ne11;
  4387. const int64_t nr0 = ne00 / ne10;
  4388. float * dst_ptr = (float *) ((char *) dst->data + i03*nb3 + i02*nb2 + i01*nb1 );
  4389. float * src0_ptr = (float *) ((char *) src0->data + i03*nb03 + i02*nb02 + i01*nb01);
  4390. float * src1_ptr = (float *) ((char *) src1->data + i13*nb13 + i12*nb12 + i11*nb11);
  4391. for (int64_t r = 0 ; r < nr0; ++r) {
  4392. #ifdef GGML_USE_ACCELERATE
  4393. UNUSED(ggml_vec_mul_f32);
  4394. vDSP_vmul(src0_ptr + r*ne10, 1, src1_ptr, 1, dst_ptr + r*ne10, 1, ne10);
  4395. #else
  4396. ggml_vec_mul_f32(ne10, dst_ptr + r*ne10, src0_ptr + r*ne10, src1_ptr);
  4397. #endif
  4398. }
  4399. }
  4400. } else {
  4401. // src1 is not contiguous
  4402. for (int64_t ir = ith; ir < nr; ir += nth) {
  4403. // src0 and dst are same shape => same indices
  4404. // src1 is broadcastable across src0 and dst in i1, i2, i3
  4405. const int64_t i03 = ir/(ne02*ne01);
  4406. const int64_t i02 = (ir - i03*ne02*ne01)/ne01;
  4407. const int64_t i01 = (ir - i03*ne02*ne01 - i02*ne01);
  4408. const int64_t i13 = i03 % ne13;
  4409. const int64_t i12 = i02 % ne12;
  4410. const int64_t i11 = i01 % ne11;
  4411. float * dst_ptr = (float *) ((char *) dst->data + i03*nb3 + i02*nb2 + i01*nb1 );
  4412. float * src0_ptr = (float *) ((char *) src0->data + i03*nb03 + i02*nb02 + i01*nb01);
  4413. for (int64_t i0 = 0; i0 < ne00; ++i0) {
  4414. const int64_t i10 = i0 % ne10;
  4415. float * src1_ptr = (float *) ((char *) src1->data + i13*nb13 + i12*nb12 + i11*nb11 + i10*nb10);
  4416. dst_ptr[i0] = src0_ptr[i0] * (*src1_ptr);
  4417. }
  4418. }
  4419. }
  4420. }
  4421. static void ggml_compute_forward_mul(
  4422. const struct ggml_compute_params * params,
  4423. struct ggml_tensor * dst) {
  4424. const struct ggml_tensor * src0 = dst->src[0];
  4425. const struct ggml_tensor * src1 = dst->src[1];
  4426. GGML_ASSERT(src1->type == GGML_TYPE_F32 && "only f32 src1 supported for now");
  4427. switch (src0->type) {
  4428. case GGML_TYPE_F32:
  4429. {
  4430. ggml_compute_forward_mul_f32(params, dst);
  4431. } break;
  4432. default:
  4433. {
  4434. GGML_ABORT("fatal error");
  4435. }
  4436. }
  4437. }
  4438. // ggml_compute_forward_div
  4439. static void ggml_compute_forward_div_f32(
  4440. const struct ggml_compute_params * params,
  4441. struct ggml_tensor * dst) {
  4442. const struct ggml_tensor * src0 = dst->src[0];
  4443. const struct ggml_tensor * src1 = dst->src[1];
  4444. GGML_ASSERT(ggml_can_repeat(src1, src0) && ggml_are_same_shape(src0, dst));
  4445. const int ith = params->ith;
  4446. const int nth = params->nth;
  4447. const int64_t nr = ggml_nrows(src0);
  4448. GGML_TENSOR_BINARY_OP_LOCALS
  4449. GGML_ASSERT( nb0 == sizeof(float));
  4450. GGML_ASSERT(nb00 == sizeof(float));
  4451. if (nb10 == sizeof(float)) {
  4452. for (int64_t ir = ith; ir < nr; ir += nth) {
  4453. // src0 and dst are same shape => same indices
  4454. const int64_t i03 = ir/(ne02*ne01);
  4455. const int64_t i02 = (ir - i03*ne02*ne01)/ne01;
  4456. const int64_t i01 = (ir - i03*ne02*ne01 - i02*ne01);
  4457. const int64_t i13 = i03 % ne13;
  4458. const int64_t i12 = i02 % ne12;
  4459. const int64_t i11 = i01 % ne11;
  4460. const int64_t nr0 = ne00 / ne10;
  4461. float * dst_ptr = (float *) ((char *) dst->data + i03*nb3 + i02*nb2 + i01*nb1 );
  4462. float * src0_ptr = (float *) ((char *) src0->data + i03*nb03 + i02*nb02 + i01*nb01);
  4463. float * src1_ptr = (float *) ((char *) src1->data + i13*nb13 + i12*nb12 + i11*nb11);
  4464. for (int64_t r = 0; r < nr0; ++r) {
  4465. #ifdef GGML_USE_ACCELERATE
  4466. UNUSED(ggml_vec_div_f32);
  4467. vDSP_vdiv(src1_ptr, 1, src0_ptr + r*ne10, 1, dst_ptr + r*ne10, 1, ne10);
  4468. #else
  4469. ggml_vec_div_f32(ne10, dst_ptr + r*ne10, src0_ptr + r*ne10, src1_ptr);
  4470. #endif
  4471. }
  4472. }
  4473. } else {
  4474. // src1 is not contiguous
  4475. for (int64_t ir = ith; ir < nr; ir += nth) {
  4476. // src0 and dst are same shape => same indices
  4477. // src1 is broadcastable across src0 and dst in i1, i2, i3
  4478. const int64_t i03 = ir/(ne02*ne01);
  4479. const int64_t i02 = (ir - i03*ne02*ne01)/ne01;
  4480. const int64_t i01 = (ir - i03*ne02*ne01 - i02*ne01);
  4481. const int64_t i13 = i03 % ne13;
  4482. const int64_t i12 = i02 % ne12;
  4483. const int64_t i11 = i01 % ne11;
  4484. float * dst_ptr = (float *) ((char *) dst->data + i03*nb3 + i02*nb2 + i01*nb1 );
  4485. float * src0_ptr = (float *) ((char *) src0->data + i03*nb03 + i02*nb02 + i01*nb01);
  4486. for (int64_t i0 = 0; i0 < ne00; ++i0) {
  4487. const int64_t i10 = i0 % ne10;
  4488. float * src1_ptr = (float *) ((char *) src1->data + i13*nb13 + i12*nb12 + i11*nb11 + i10*nb10);
  4489. dst_ptr[i0] = src0_ptr[i0] / (*src1_ptr);
  4490. }
  4491. }
  4492. }
  4493. }
  4494. static void ggml_compute_forward_div(
  4495. const struct ggml_compute_params * params,
  4496. struct ggml_tensor * dst) {
  4497. const struct ggml_tensor * src0 = dst->src[0];
  4498. switch (src0->type) {
  4499. case GGML_TYPE_F32:
  4500. {
  4501. ggml_compute_forward_div_f32(params, dst);
  4502. } break;
  4503. default:
  4504. {
  4505. GGML_ABORT("fatal error");
  4506. }
  4507. }
  4508. }
  4509. // ggml_compute_forward_sqr
  4510. static void ggml_compute_forward_sqr_f32(
  4511. const struct ggml_compute_params * params,
  4512. struct ggml_tensor * dst) {
  4513. const struct ggml_tensor * src0 = dst->src[0];
  4514. if (params->ith != 0) {
  4515. return;
  4516. }
  4517. assert(ggml_are_same_shape(src0, dst));
  4518. const int n = ggml_nrows(src0);
  4519. const int nc = src0->ne[0];
  4520. assert( dst->nb[0] == sizeof(float));
  4521. assert(src0->nb[0] == sizeof(float));
  4522. for (int i = 0; i < n; i++) {
  4523. ggml_vec_sqr_f32(nc,
  4524. (float *) ((char *) dst->data + i*( dst->nb[1])),
  4525. (float *) ((char *) src0->data + i*(src0->nb[1])));
  4526. }
  4527. }
  4528. static void ggml_compute_forward_sqr(
  4529. const struct ggml_compute_params * params,
  4530. struct ggml_tensor * dst) {
  4531. const struct ggml_tensor * src0 = dst->src[0];
  4532. switch (src0->type) {
  4533. case GGML_TYPE_F32:
  4534. {
  4535. ggml_compute_forward_sqr_f32(params, dst);
  4536. } break;
  4537. default:
  4538. {
  4539. GGML_ABORT("fatal error");
  4540. }
  4541. }
  4542. }
  4543. // ggml_compute_forward_sqrt
  4544. static void ggml_compute_forward_sqrt_f32(
  4545. const struct ggml_compute_params * params,
  4546. struct ggml_tensor * dst) {
  4547. const struct ggml_tensor * src0 = dst->src[0];
  4548. if (params->ith != 0) {
  4549. return;
  4550. }
  4551. assert(ggml_are_same_shape(src0, dst));
  4552. const int n = ggml_nrows(src0);
  4553. const int nc = src0->ne[0];
  4554. assert( dst->nb[0] == sizeof(float));
  4555. assert(src0->nb[0] == sizeof(float));
  4556. for (int i = 0; i < n; i++) {
  4557. ggml_vec_sqrt_f32(nc,
  4558. (float *) ((char *) dst->data + i*( dst->nb[1])),
  4559. (float *) ((char *) src0->data + i*(src0->nb[1])));
  4560. }
  4561. }
  4562. static void ggml_compute_forward_sqrt(
  4563. const struct ggml_compute_params * params,
  4564. struct ggml_tensor * dst) {
  4565. const struct ggml_tensor * src0 = dst->src[0];
  4566. switch (src0->type) {
  4567. case GGML_TYPE_F32:
  4568. {
  4569. ggml_compute_forward_sqrt_f32(params, dst);
  4570. } break;
  4571. default:
  4572. {
  4573. GGML_ABORT("fatal error");
  4574. }
  4575. }
  4576. }
  4577. // ggml_compute_forward_log
  4578. static void ggml_compute_forward_log_f32(
  4579. const struct ggml_compute_params * params,
  4580. struct ggml_tensor * dst) {
  4581. const struct ggml_tensor * src0 = dst->src[0];
  4582. if (params->ith != 0) {
  4583. return;
  4584. }
  4585. GGML_ASSERT(ggml_are_same_shape(src0, dst));
  4586. const int n = ggml_nrows(src0);
  4587. const int nc = src0->ne[0];
  4588. GGML_ASSERT( dst->nb[0] == sizeof(float));
  4589. GGML_ASSERT(src0->nb[0] == sizeof(float));
  4590. for (int i = 0; i < n; i++) {
  4591. ggml_vec_log_f32(nc,
  4592. (float *) ((char *) dst->data + i*( dst->nb[1])),
  4593. (float *) ((char *) src0->data + i*(src0->nb[1])));
  4594. }
  4595. }
  4596. static void ggml_compute_forward_log(
  4597. const struct ggml_compute_params * params,
  4598. struct ggml_tensor * dst) {
  4599. const struct ggml_tensor * src0 = dst->src[0];
  4600. switch (src0->type) {
  4601. case GGML_TYPE_F32:
  4602. {
  4603. ggml_compute_forward_log_f32(params, dst);
  4604. } break;
  4605. default:
  4606. {
  4607. GGML_ABORT("fatal error");
  4608. }
  4609. }
  4610. }
  4611. // ggml_compute_forward_sin
  4612. static void ggml_compute_forward_sin_f32(
  4613. const struct ggml_compute_params * params,
  4614. struct ggml_tensor * dst) {
  4615. const struct ggml_tensor * src0 = dst->src[0];
  4616. if (params->ith != 0) {
  4617. return;
  4618. }
  4619. GGML_ASSERT(ggml_are_same_shape(src0, dst));
  4620. const int n = ggml_nrows(src0);
  4621. const int nc = src0->ne[0];
  4622. GGML_ASSERT( dst->nb[0] == sizeof(float));
  4623. GGML_ASSERT(src0->nb[0] == sizeof(float));
  4624. for (int i = 0; i < n; i++) {
  4625. ggml_vec_sin_f32(nc,
  4626. (float *) ((char *) dst->data + i*( dst->nb[1])),
  4627. (float *) ((char *) src0->data + i*(src0->nb[1])));
  4628. }
  4629. }
  4630. static void ggml_compute_forward_sin(
  4631. const struct ggml_compute_params * params,
  4632. struct ggml_tensor * dst) {
  4633. const struct ggml_tensor * src0 = dst->src[0];
  4634. switch (src0->type) {
  4635. case GGML_TYPE_F32:
  4636. {
  4637. ggml_compute_forward_sin_f32(params, dst);
  4638. } break;
  4639. default:
  4640. {
  4641. GGML_ABORT("fatal error");
  4642. }
  4643. }
  4644. }
  4645. // ggml_compute_forward_cos
  4646. static void ggml_compute_forward_cos_f32(
  4647. const struct ggml_compute_params * params,
  4648. struct ggml_tensor * dst) {
  4649. const struct ggml_tensor * src0 = dst->src[0];
  4650. if (params->ith != 0) {
  4651. return;
  4652. }
  4653. GGML_ASSERT(ggml_are_same_shape(src0, dst));
  4654. const int n = ggml_nrows(src0);
  4655. const int nc = src0->ne[0];
  4656. GGML_ASSERT( dst->nb[0] == sizeof(float));
  4657. GGML_ASSERT(src0->nb[0] == sizeof(float));
  4658. for (int i = 0; i < n; i++) {
  4659. ggml_vec_cos_f32(nc,
  4660. (float *) ((char *) dst->data + i*( dst->nb[1])),
  4661. (float *) ((char *) src0->data + i*(src0->nb[1])));
  4662. }
  4663. }
  4664. static void ggml_compute_forward_cos(
  4665. const struct ggml_compute_params * params,
  4666. struct ggml_tensor * dst) {
  4667. const struct ggml_tensor * src0 = dst->src[0];
  4668. switch (src0->type) {
  4669. case GGML_TYPE_F32:
  4670. {
  4671. ggml_compute_forward_cos_f32(params, dst);
  4672. } break;
  4673. default:
  4674. {
  4675. GGML_ABORT("fatal error");
  4676. }
  4677. }
  4678. }
  4679. // ggml_compute_forward_sum
  4680. static void ggml_compute_forward_sum_f32(
  4681. const struct ggml_compute_params * params,
  4682. struct ggml_tensor * dst) {
  4683. const struct ggml_tensor * src0 = dst->src[0];
  4684. if (params->ith != 0) {
  4685. return;
  4686. }
  4687. assert(ggml_is_scalar(dst));
  4688. assert(src0->nb[0] == sizeof(float));
  4689. GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne)
  4690. GGML_TENSOR_LOCALS(size_t, nb0, src0, nb)
  4691. ggml_float sum = 0;
  4692. ggml_float row_sum = 0;
  4693. for (int64_t i03 = 0; i03 < ne03; i03++) {
  4694. for (int64_t i02 = 0; i02 < ne02; i02++) {
  4695. for (int64_t i01 = 0; i01 < ne01; i01++) {
  4696. ggml_vec_sum_f32_ggf(ne00,
  4697. &row_sum,
  4698. (float *) ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03));
  4699. sum += row_sum;
  4700. }
  4701. }
  4702. }
  4703. ((float *) dst->data)[0] = sum;
  4704. }
  4705. static void ggml_compute_forward_sum_f16(
  4706. const struct ggml_compute_params * params,
  4707. struct ggml_tensor * dst) {
  4708. const struct ggml_tensor * src0 = dst->src[0];
  4709. if (params->ith != 0) {
  4710. return;
  4711. }
  4712. assert(ggml_is_scalar(dst));
  4713. assert(src0->nb[0] == sizeof(ggml_fp16_t));
  4714. GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne)
  4715. GGML_TENSOR_LOCALS(size_t, nb0, src0, nb)
  4716. float sum = 0;
  4717. float row_sum = 0;
  4718. for (int64_t i03 = 0; i03 < ne03; i03++) {
  4719. for (int64_t i02 = 0; i02 < ne02; i02++) {
  4720. for (int64_t i01 = 0; i01 < ne01; i01++) {
  4721. ggml_vec_sum_f16_ggf(ne00,
  4722. &row_sum,
  4723. (ggml_fp16_t *) ((char *) src0->data + i01 * nb01 + i02 * nb02 + i03 * nb03));
  4724. sum += row_sum;
  4725. }
  4726. }
  4727. }
  4728. ((ggml_fp16_t *) dst->data)[0] = GGML_FP32_TO_FP16(sum);
  4729. }
  4730. static void ggml_compute_forward_sum_bf16(
  4731. const struct ggml_compute_params * params,
  4732. struct ggml_tensor * dst) {
  4733. const struct ggml_tensor * src0 = dst->src[0];
  4734. if (params->ith != 0) {
  4735. return;
  4736. }
  4737. assert(ggml_is_scalar(dst));
  4738. assert(src0->nb[0] == sizeof(ggml_bf16_t));
  4739. GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne)
  4740. GGML_TENSOR_LOCALS(size_t, nb0, src0, nb)
  4741. float sum = 0;
  4742. float row_sum = 0;
  4743. for (int64_t i03 = 0; i03 < ne03; i03++) {
  4744. for (int64_t i02 = 0; i02 < ne02; i02++) {
  4745. for (int64_t i01 = 0; i01 < ne01; i01++) {
  4746. ggml_vec_sum_bf16_ggf(ne00,
  4747. &row_sum,
  4748. (ggml_bf16_t *) ((char *) src0->data + i01 * nb01 + i02 * nb02 + i03 * nb03));
  4749. sum += row_sum;
  4750. }
  4751. }
  4752. }
  4753. ((ggml_bf16_t *) dst->data)[0] = GGML_FP32_TO_BF16(sum);
  4754. }
  4755. static void ggml_compute_forward_sum(
  4756. const struct ggml_compute_params * params,
  4757. struct ggml_tensor * dst) {
  4758. const struct ggml_tensor * src0 = dst->src[0];
  4759. switch (src0->type) {
  4760. case GGML_TYPE_F32:
  4761. {
  4762. ggml_compute_forward_sum_f32(params, dst);
  4763. } break;
  4764. case GGML_TYPE_F16:
  4765. {
  4766. ggml_compute_forward_sum_f16(params, dst);
  4767. } break;
  4768. case GGML_TYPE_BF16:
  4769. {
  4770. ggml_compute_forward_sum_bf16(params, dst);
  4771. } break;
  4772. default:
  4773. {
  4774. GGML_ABORT("fatal error");
  4775. }
  4776. }
  4777. }
  4778. // ggml_compute_forward_sum_rows
  4779. static void ggml_compute_forward_sum_rows_f32(
  4780. const struct ggml_compute_params * params,
  4781. struct ggml_tensor * dst) {
  4782. const struct ggml_tensor * src0 = dst->src[0];
  4783. if (params->ith != 0) {
  4784. return;
  4785. }
  4786. GGML_ASSERT(src0->nb[0] == sizeof(float));
  4787. GGML_ASSERT(dst->nb[0] == sizeof(float));
  4788. GGML_TENSOR_UNARY_OP_LOCALS
  4789. GGML_ASSERT(ne0 == 1);
  4790. GGML_ASSERT(ne1 == ne01);
  4791. GGML_ASSERT(ne2 == ne02);
  4792. GGML_ASSERT(ne3 == ne03);
  4793. for (int64_t i3 = 0; i3 < ne03; i3++) {
  4794. for (int64_t i2 = 0; i2 < ne02; i2++) {
  4795. for (int64_t i1 = 0; i1 < ne01; i1++) {
  4796. float * src_row = (float *) ((char *) src0->data + i1*nb01 + i2*nb02 + i3*nb03);
  4797. float * dst_row = (float *) ((char *) dst->data + i1*nb1 + i2*nb2 + i3*nb3);
  4798. float row_sum = 0;
  4799. ggml_vec_sum_f32(ne00, &row_sum, src_row);
  4800. dst_row[0] = row_sum;
  4801. }
  4802. }
  4803. }
  4804. }
  4805. static void ggml_compute_forward_sum_rows(
  4806. const struct ggml_compute_params * params,
  4807. struct ggml_tensor * dst) {
  4808. const struct ggml_tensor * src0 = dst->src[0];
  4809. switch (src0->type) {
  4810. case GGML_TYPE_F32:
  4811. {
  4812. ggml_compute_forward_sum_rows_f32(params, dst);
  4813. } break;
  4814. default:
  4815. {
  4816. GGML_ABORT("fatal error");
  4817. }
  4818. }
  4819. }
  4820. // ggml_compute_forward_mean
  4821. static void ggml_compute_forward_mean_f32(
  4822. const struct ggml_compute_params * params,
  4823. struct ggml_tensor * dst) {
  4824. const struct ggml_tensor * src0 = dst->src[0];
  4825. if (params->ith != 0) {
  4826. return;
  4827. }
  4828. assert(src0->nb[0] == sizeof(float));
  4829. GGML_TENSOR_UNARY_OP_LOCALS
  4830. assert(ne0 == 1);
  4831. assert(ne1 == ne01);
  4832. assert(ne2 == ne02);
  4833. assert(ne3 == ne03);
  4834. UNUSED(ne0);
  4835. UNUSED(ne1);
  4836. UNUSED(ne2);
  4837. UNUSED(ne3);
  4838. for (int64_t i03 = 0; i03 < ne03; i03++) {
  4839. for (int64_t i02 = 0; i02 < ne02; i02++) {
  4840. for (int64_t i01 = 0; i01 < ne01; i01++) {
  4841. ggml_vec_sum_f32(ne00,
  4842. (float *) ((char *) dst->data + i01*nb1 + i02*nb2 + i03*nb3),
  4843. (float *) ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03));
  4844. *(float *) ((char *) dst->data + i01*nb1 + i02*nb2 + i03*nb3) /= (float) ne00;
  4845. }
  4846. }
  4847. }
  4848. }
  4849. static void ggml_compute_forward_mean(
  4850. const struct ggml_compute_params * params,
  4851. struct ggml_tensor * dst) {
  4852. const struct ggml_tensor * src0 = dst->src[0];
  4853. switch (src0->type) {
  4854. case GGML_TYPE_F32:
  4855. {
  4856. ggml_compute_forward_mean_f32(params, dst);
  4857. } break;
  4858. default:
  4859. {
  4860. GGML_ABORT("fatal error");
  4861. }
  4862. }
  4863. }
  4864. // ggml_compute_forward_argmax
  4865. static void ggml_compute_forward_argmax_f32(
  4866. const struct ggml_compute_params * params,
  4867. struct ggml_tensor * dst) {
  4868. const struct ggml_tensor * src0 = dst->src[0];
  4869. if (params->ith != 0) {
  4870. return;
  4871. }
  4872. assert(src0->nb[0] == sizeof(float));
  4873. assert(dst->nb[0] == sizeof(float));
  4874. const int64_t ne00 = src0->ne[0];
  4875. const int64_t ne01 = src0->ne[1];
  4876. const size_t nb01 = src0->nb[1];
  4877. const size_t nb0 = dst->nb[0];
  4878. for (int64_t i1 = 0; i1 < ne01; i1++) {
  4879. float * src = (float *) ((char *) src0->data + i1*nb01);
  4880. int32_t * dst_ = (int32_t *) ((char *) dst->data + i1*nb0);
  4881. int v = 0;
  4882. ggml_vec_argmax_f32(ne00, &v, src);
  4883. dst_[0] = v;
  4884. }
  4885. }
  4886. static void ggml_compute_forward_argmax(
  4887. const struct ggml_compute_params * params,
  4888. struct ggml_tensor * dst) {
  4889. const struct ggml_tensor * src0 = dst->src[0];
  4890. switch (src0->type) {
  4891. case GGML_TYPE_F32:
  4892. {
  4893. ggml_compute_forward_argmax_f32(params, dst);
  4894. } break;
  4895. default:
  4896. {
  4897. GGML_ABORT("fatal error");
  4898. }
  4899. }
  4900. }
  4901. // ggml_compute_forward_count_equal
  4902. static void ggml_compute_forward_count_equal_i32(
  4903. const struct ggml_compute_params * params,
  4904. struct ggml_tensor * dst) {
  4905. const struct ggml_tensor * src0 = dst->src[0];
  4906. const struct ggml_tensor * src1 = dst->src[1];
  4907. GGML_TENSOR_BINARY_OP_LOCALS;
  4908. GGML_ASSERT(src0->type == GGML_TYPE_I32);
  4909. GGML_ASSERT(src1->type == GGML_TYPE_I32);
  4910. GGML_ASSERT(ggml_are_same_shape(src0, src1));
  4911. GGML_ASSERT(ggml_is_scalar(dst));
  4912. GGML_ASSERT(dst->type == GGML_TYPE_I64);
  4913. const int64_t nr = ggml_nrows(src0);
  4914. const int ith = params->ith;
  4915. const int nth = params->nth;
  4916. int64_t * sums = (int64_t *) params->wdata;
  4917. int64_t sum_thread = 0;
  4918. // rows per thread
  4919. const int64_t dr = (nr + nth - 1)/nth;
  4920. // row range for this thread
  4921. const int64_t ir0 = dr*ith;
  4922. const int64_t ir1 = MIN(ir0 + dr, nr);
  4923. for (int64_t ir = ir0; ir < ir1; ++ir) {
  4924. const int64_t i03 = ir / (ne02*ne01);
  4925. const int64_t i02 = (ir - i03*ne03) / ne01;
  4926. const int64_t i01 = ir - i03*ne03 - i02*ne02;
  4927. const char * data0 = (const char *) src0->data + i03*nb03 + i02*nb02 + i01*nb01;
  4928. const char * data1 = (const char *) src1->data + i03*nb13 + i02*nb12 + i01*nb11;
  4929. for (int64_t i00 = 0; i00 < ne00; ++i00) {
  4930. const int32_t val0 = *((const int32_t *) (data0 + i00*nb00));
  4931. const int32_t val1 = *((const int32_t *) (data1 + i00*nb10));
  4932. sum_thread += val0 == val1;
  4933. }
  4934. }
  4935. if (ith != 0) {
  4936. sums[ith] = sum_thread;
  4937. }
  4938. ggml_barrier(params->threadpool);
  4939. if (ith != 0) {
  4940. return;
  4941. }
  4942. for (int ith_other = 1; ith_other < nth; ++ith_other) {
  4943. sum_thread += sums[ith_other];
  4944. }
  4945. *((int64_t *) dst->data) = sum_thread;
  4946. }
  4947. static void ggml_compute_forward_count_equal(
  4948. const struct ggml_compute_params * params,
  4949. struct ggml_tensor * dst) {
  4950. const struct ggml_tensor * src0 = dst->src[0];
  4951. switch (src0->type) {
  4952. case GGML_TYPE_I32:
  4953. {
  4954. ggml_compute_forward_count_equal_i32(params, dst);
  4955. } break;
  4956. default:
  4957. {
  4958. GGML_ABORT("fatal error");
  4959. }
  4960. }
  4961. }
  4962. // ggml_compute_forward_repeat
  4963. static void ggml_compute_forward_repeat_f32(
  4964. const struct ggml_compute_params * params,
  4965. struct ggml_tensor * dst) {
  4966. const struct ggml_tensor * src0 = dst->src[0];
  4967. if (params->ith != 0) {
  4968. return;
  4969. }
  4970. GGML_ASSERT(ggml_can_repeat(src0, dst));
  4971. GGML_TENSOR_UNARY_OP_LOCALS
  4972. // guaranteed to be an integer due to the check in ggml_can_repeat
  4973. const int nr0 = (int)(ne0/ne00);
  4974. const int nr1 = (int)(ne1/ne01);
  4975. const int nr2 = (int)(ne2/ne02);
  4976. const int nr3 = (int)(ne3/ne03);
  4977. // TODO: support for transposed / permuted tensors
  4978. GGML_ASSERT(nb0 == sizeof(float));
  4979. GGML_ASSERT(nb00 == sizeof(float));
  4980. // TODO: maybe this is not optimal?
  4981. for (int i3 = 0; i3 < nr3; i3++) {
  4982. for (int k3 = 0; k3 < ne03; k3++) {
  4983. for (int i2 = 0; i2 < nr2; i2++) {
  4984. for (int k2 = 0; k2 < ne02; k2++) {
  4985. for (int i1 = 0; i1 < nr1; i1++) {
  4986. for (int k1 = 0; k1 < ne01; k1++) {
  4987. for (int i0 = 0; i0 < nr0; i0++) {
  4988. ggml_vec_cpy_f32(ne00,
  4989. (float *) ((char *) dst->data + (i3*ne03 + k3)*nb3 + (i2*ne02 + k2)*nb2 + (i1*ne01 + k1)*nb1 + (i0*ne00)*nb0),
  4990. (float *) ((char *) src0->data + ( k3)*nb03 + ( k2)*nb02 + ( k1)*nb01));
  4991. }
  4992. }
  4993. }
  4994. }
  4995. }
  4996. }
  4997. }
  4998. }
  4999. static void ggml_compute_forward_repeat_f16(
  5000. const struct ggml_compute_params * params,
  5001. struct ggml_tensor * dst) {
  5002. const struct ggml_tensor * src0 = dst->src[0];
  5003. if (params->ith != 0) {
  5004. return;
  5005. }
  5006. GGML_ASSERT(ggml_can_repeat(src0, dst));
  5007. GGML_TENSOR_UNARY_OP_LOCALS
  5008. // guaranteed to be an integer due to the check in ggml_can_repeat
  5009. const int nr0 = (int)(ne0/ne00);
  5010. const int nr1 = (int)(ne1/ne01);
  5011. const int nr2 = (int)(ne2/ne02);
  5012. const int nr3 = (int)(ne3/ne03);
  5013. // TODO: support for transposed / permuted tensors
  5014. GGML_ASSERT(nb0 == sizeof(ggml_fp16_t));
  5015. GGML_ASSERT(nb00 == sizeof(ggml_fp16_t));
  5016. // TODO: maybe this is not optimal?
  5017. for (int i3 = 0; i3 < nr3; i3++) {
  5018. for (int k3 = 0; k3 < ne03; k3++) {
  5019. for (int i2 = 0; i2 < nr2; i2++) {
  5020. for (int k2 = 0; k2 < ne02; k2++) {
  5021. for (int i1 = 0; i1 < nr1; i1++) {
  5022. for (int k1 = 0; k1 < ne01; k1++) {
  5023. for (int i0 = 0; i0 < nr0; i0++) {
  5024. ggml_fp16_t * y = (ggml_fp16_t *) ((char *) dst->data + (i3*ne03 + k3)*nb3 + (i2*ne02 + k2)*nb2 + (i1*ne01 + k1)*nb1 + (i0*ne00)*nb0);
  5025. ggml_fp16_t * x = (ggml_fp16_t *) ((char *) src0->data + ( k3)*nb03 + ( k2)*nb02 + ( k1)*nb01);
  5026. // ggml_vec_cpy_f16(ne00, y, x)
  5027. for (int i = 0; i < ne00; ++i) {
  5028. y[i] = x[i];
  5029. }
  5030. }
  5031. }
  5032. }
  5033. }
  5034. }
  5035. }
  5036. }
  5037. }
  5038. static void ggml_compute_forward_repeat(
  5039. const struct ggml_compute_params * params,
  5040. struct ggml_tensor * dst) {
  5041. const struct ggml_tensor * src0 = dst->src[0];
  5042. switch (src0->type) {
  5043. case GGML_TYPE_F16:
  5044. case GGML_TYPE_BF16:
  5045. case GGML_TYPE_I16:
  5046. {
  5047. ggml_compute_forward_repeat_f16(params, dst);
  5048. } break;
  5049. case GGML_TYPE_F32:
  5050. case GGML_TYPE_I32:
  5051. {
  5052. ggml_compute_forward_repeat_f32(params, dst);
  5053. } break;
  5054. default:
  5055. {
  5056. GGML_ABORT("fatal error");
  5057. }
  5058. }
  5059. }
  5060. // ggml_compute_forward_repeat_back
  5061. static void ggml_compute_forward_repeat_back_f32(
  5062. const struct ggml_compute_params * params,
  5063. struct ggml_tensor * dst) {
  5064. const struct ggml_tensor * src0 = dst->src[0];
  5065. if (params->ith != 0) {
  5066. return;
  5067. }
  5068. GGML_ASSERT(ggml_can_repeat(dst, src0));
  5069. GGML_TENSOR_UNARY_OP_LOCALS
  5070. // guaranteed to be an integer due to the check in ggml_can_repeat
  5071. const int nr0 = (int)(ne00/ne0);
  5072. const int nr1 = (int)(ne01/ne1);
  5073. const int nr2 = (int)(ne02/ne2);
  5074. const int nr3 = (int)(ne03/ne3);
  5075. // TODO: support for transposed / permuted tensors
  5076. GGML_ASSERT(nb0 == sizeof(float));
  5077. GGML_ASSERT(nb00 == sizeof(float));
  5078. if (ggml_is_contiguous(dst)) {
  5079. ggml_vec_set_f32(ne0*ne1*ne2*ne3, dst->data, 0);
  5080. } else {
  5081. for (int k3 = 0; k3 < ne3; k3++) {
  5082. for (int k2 = 0; k2 < ne2; k2++) {
  5083. for (int k1 = 0; k1 < ne1; k1++) {
  5084. ggml_vec_set_f32(ne0,
  5085. (float *) ((char *) dst->data + k1*nb1 + k2*nb2 + k3*nb3),
  5086. 0);
  5087. }
  5088. }
  5089. }
  5090. }
  5091. // TODO: maybe this is not optimal?
  5092. for (int i3 = 0; i3 < nr3; i3++) {
  5093. for (int k3 = 0; k3 < ne3; k3++) {
  5094. for (int i2 = 0; i2 < nr2; i2++) {
  5095. for (int k2 = 0; k2 < ne2; k2++) {
  5096. for (int i1 = 0; i1 < nr1; i1++) {
  5097. for (int k1 = 0; k1 < ne1; k1++) {
  5098. for (int i0 = 0; i0 < nr0; i0++) {
  5099. ggml_vec_acc_f32(ne0,
  5100. (float *) ((char *) dst->data + ( k3)*nb3 + ( k2)*nb2 + ( k1)*nb1),
  5101. (float *) ((char *) src0->data + (i3*ne3 + k3)*nb03 + (i2*ne2 + k2)*nb02 + (i1*ne1 + k1)*nb01 + (i0*ne0)*nb00));
  5102. }
  5103. }
  5104. }
  5105. }
  5106. }
  5107. }
  5108. }
  5109. }
  5110. static void ggml_compute_forward_repeat_back(
  5111. const struct ggml_compute_params * params,
  5112. struct ggml_tensor * dst) {
  5113. const struct ggml_tensor * src0 = dst->src[0];
  5114. switch (src0->type) {
  5115. case GGML_TYPE_F32:
  5116. {
  5117. ggml_compute_forward_repeat_back_f32(params, dst);
  5118. } break;
  5119. default:
  5120. {
  5121. GGML_ABORT("fatal error");
  5122. }
  5123. }
  5124. }
  5125. // ggml_compute_forward_concat
  5126. static void ggml_compute_forward_concat_f32(
  5127. const struct ggml_compute_params * params,
  5128. struct ggml_tensor * dst) {
  5129. const struct ggml_tensor * src0 = dst->src[0];
  5130. const struct ggml_tensor * src1 = dst->src[1];
  5131. GGML_ASSERT(src0->nb[0] == sizeof(float));
  5132. const int ith = params->ith;
  5133. const int nth = params->nth;
  5134. GGML_TENSOR_BINARY_OP_LOCALS
  5135. const int32_t dim = ggml_get_op_params_i32(dst, 0);
  5136. GGML_ASSERT(dim >= 0 && dim < 4);
  5137. int64_t o[4] = {0, 0, 0, 0};
  5138. o[dim] = src0->ne[dim];
  5139. const float * x;
  5140. // TODO: smarter multi-theading
  5141. for (int i3 = 0; i3 < ne3; i3++) {
  5142. for (int i2 = ith; i2 < ne2; i2 += nth) {
  5143. for (int i1 = 0; i1 < ne1; i1++) {
  5144. for (int i0 = 0; i0 < ne0; i0++) {
  5145. if (i0 < ne00 && i1 < ne01 && i2 < ne02 && i3 < ne03) {
  5146. x = (const float *) ((const char *)src0->data + (i0 )*nb00 + (i1 )*nb01 + (i2 )*nb02 + (i3 )*nb03);
  5147. } else {
  5148. x = (const float *) ((const char *)src1->data + (i0 - o[0])*nb10 + (i1 - o[1])*nb11 + (i2 - o[2])*nb12 + (i3 - o[3])*nb13);
  5149. }
  5150. float * y = (float *)((char *)dst->data + i0*nb0 + i1*nb1 + i2*nb2 + i3*nb3);
  5151. *y = *x;
  5152. }
  5153. }
  5154. }
  5155. }
  5156. }
  5157. static void ggml_compute_forward_concat(
  5158. const struct ggml_compute_params * params,
  5159. struct ggml_tensor * dst) {
  5160. const struct ggml_tensor * src0 = dst->src[0];
  5161. switch (src0->type) {
  5162. case GGML_TYPE_F32:
  5163. case GGML_TYPE_I32:
  5164. {
  5165. ggml_compute_forward_concat_f32(params, dst);
  5166. } break;
  5167. default:
  5168. {
  5169. GGML_ABORT("fatal error");
  5170. }
  5171. }
  5172. }
  5173. // ggml_compute_forward_abs
  5174. static void ggml_compute_forward_abs_f32(
  5175. const struct ggml_compute_params * params,
  5176. struct ggml_tensor * dst) {
  5177. const struct ggml_tensor * src0 = dst->src[0];
  5178. if (params->ith != 0) {
  5179. return;
  5180. }
  5181. assert(ggml_is_contiguous_1(src0));
  5182. assert(ggml_is_contiguous_1(dst));
  5183. assert(ggml_are_same_shape(src0, dst));
  5184. const int n = ggml_nrows(src0);
  5185. const int nc = src0->ne[0];
  5186. for (int i = 0; i < n; i++) {
  5187. ggml_vec_abs_f32(nc,
  5188. (float *) ((char *) dst->data + i*( dst->nb[1])),
  5189. (float *) ((char *) src0->data + i*(src0->nb[1])));
  5190. }
  5191. }
  5192. static void ggml_compute_forward_abs(
  5193. const struct ggml_compute_params * params,
  5194. struct ggml_tensor * dst) {
  5195. const struct ggml_tensor * src0 = dst->src[0];
  5196. switch (src0->type) {
  5197. case GGML_TYPE_F32:
  5198. {
  5199. ggml_compute_forward_abs_f32(params, dst);
  5200. } break;
  5201. default:
  5202. {
  5203. GGML_ABORT("fatal error");
  5204. }
  5205. }
  5206. }
  5207. // ggml_compute_forward_sgn
  5208. static void ggml_compute_forward_sgn_f32(
  5209. const struct ggml_compute_params * params,
  5210. struct ggml_tensor * dst) {
  5211. const struct ggml_tensor * src0 = dst->src[0];
  5212. if (params->ith != 0) {
  5213. return;
  5214. }
  5215. assert(ggml_is_contiguous_1(src0));
  5216. assert(ggml_is_contiguous_1(dst));
  5217. assert(ggml_are_same_shape(src0, dst));
  5218. const int n = ggml_nrows(src0);
  5219. const int nc = src0->ne[0];
  5220. for (int i = 0; i < n; i++) {
  5221. ggml_vec_sgn_f32(nc,
  5222. (float *) ((char *) dst->data + i*( dst->nb[1])),
  5223. (float *) ((char *) src0->data + i*(src0->nb[1])));
  5224. }
  5225. }
  5226. static void ggml_compute_forward_sgn(
  5227. const struct ggml_compute_params * params,
  5228. struct ggml_tensor * dst) {
  5229. const struct ggml_tensor * src0 = dst->src[0];
  5230. switch (src0->type) {
  5231. case GGML_TYPE_F32:
  5232. {
  5233. ggml_compute_forward_sgn_f32(params, dst);
  5234. } break;
  5235. default:
  5236. {
  5237. GGML_ABORT("fatal error");
  5238. }
  5239. }
  5240. }
  5241. // ggml_compute_forward_neg
  5242. static void ggml_compute_forward_neg_f32(
  5243. const struct ggml_compute_params * params,
  5244. struct ggml_tensor * dst) {
  5245. const struct ggml_tensor * src0 = dst->src[0];
  5246. if (params->ith != 0) {
  5247. return;
  5248. }
  5249. assert(ggml_is_contiguous_1(src0));
  5250. assert(ggml_is_contiguous_1(dst));
  5251. assert(ggml_are_same_shape(src0, dst));
  5252. const int n = ggml_nrows(src0);
  5253. const int nc = src0->ne[0];
  5254. for (int i = 0; i < n; i++) {
  5255. ggml_vec_neg_f32(nc,
  5256. (float *) ((char *) dst->data + i*( dst->nb[1])),
  5257. (float *) ((char *) src0->data + i*(src0->nb[1])));
  5258. }
  5259. }
  5260. static void ggml_compute_forward_neg(
  5261. const struct ggml_compute_params * params,
  5262. struct ggml_tensor * dst) {
  5263. const struct ggml_tensor * src0 = dst->src[0];
  5264. switch (src0->type) {
  5265. case GGML_TYPE_F32:
  5266. {
  5267. ggml_compute_forward_neg_f32(params, dst);
  5268. } break;
  5269. default:
  5270. {
  5271. GGML_ABORT("fatal error");
  5272. }
  5273. }
  5274. }
  5275. // ggml_compute_forward_step
  5276. static void ggml_compute_forward_step_f32(
  5277. const struct ggml_compute_params * params,
  5278. struct ggml_tensor * dst) {
  5279. const struct ggml_tensor * src0 = dst->src[0];
  5280. if (params->ith != 0) {
  5281. return;
  5282. }
  5283. assert(ggml_is_contiguous_1(src0));
  5284. assert(ggml_is_contiguous_1(dst));
  5285. assert(ggml_are_same_shape(src0, dst));
  5286. const int n = ggml_nrows(src0);
  5287. const int nc = src0->ne[0];
  5288. for (int i = 0; i < n; i++) {
  5289. ggml_vec_step_f32(nc,
  5290. (float *) ((char *) dst->data + i*( dst->nb[1])),
  5291. (float *) ((char *) src0->data + i*(src0->nb[1])));
  5292. }
  5293. }
  5294. static void ggml_compute_forward_step(
  5295. const struct ggml_compute_params * params,
  5296. struct ggml_tensor * dst) {
  5297. const struct ggml_tensor * src0 = dst->src[0];
  5298. switch (src0->type) {
  5299. case GGML_TYPE_F32:
  5300. {
  5301. ggml_compute_forward_step_f32(params, dst);
  5302. } break;
  5303. default:
  5304. {
  5305. GGML_ABORT("fatal error");
  5306. }
  5307. }
  5308. }
  5309. // ggml_compute_forward_tanh
  5310. static void ggml_compute_forward_tanh_f32(
  5311. const struct ggml_compute_params * params,
  5312. struct ggml_tensor * dst) {
  5313. const struct ggml_tensor * src0 = dst->src[0];
  5314. if (params->ith != 0) {
  5315. return;
  5316. }
  5317. assert(ggml_is_contiguous_1(src0));
  5318. assert(ggml_is_contiguous_1(dst));
  5319. assert(ggml_are_same_shape(src0, dst));
  5320. const int n = ggml_nrows(src0);
  5321. const int nc = src0->ne[0];
  5322. for (int i = 0; i < n; i++) {
  5323. ggml_vec_tanh_f32(nc,
  5324. (float *) ((char *) dst->data + i*( dst->nb[1])),
  5325. (float *) ((char *) src0->data + i*(src0->nb[1])));
  5326. }
  5327. }
  5328. static void ggml_compute_forward_tanh(
  5329. const struct ggml_compute_params * params,
  5330. struct ggml_tensor * dst) {
  5331. const struct ggml_tensor * src0 = dst->src[0];
  5332. switch (src0->type) {
  5333. case GGML_TYPE_F32:
  5334. {
  5335. ggml_compute_forward_tanh_f32(params, dst);
  5336. } break;
  5337. default:
  5338. {
  5339. GGML_ABORT("fatal error");
  5340. }
  5341. }
  5342. }
  5343. // ggml_compute_forward_elu
  5344. static void ggml_compute_forward_elu_f32(
  5345. const struct ggml_compute_params * params,
  5346. struct ggml_tensor * dst) {
  5347. const struct ggml_tensor * src0 = dst->src[0];
  5348. if (params->ith != 0) {
  5349. return;
  5350. }
  5351. assert(ggml_is_contiguous_1(src0));
  5352. assert(ggml_is_contiguous_1(dst));
  5353. assert(ggml_are_same_shape(src0, dst));
  5354. const int n = ggml_nrows(src0);
  5355. const int nc = src0->ne[0];
  5356. for (int i = 0; i < n; i++) {
  5357. ggml_vec_elu_f32(nc,
  5358. (float *) ((char *) dst->data + i*( dst->nb[1])),
  5359. (float *) ((char *) src0->data + i*(src0->nb[1])));
  5360. }
  5361. }
  5362. static void ggml_compute_forward_elu(
  5363. const struct ggml_compute_params * params,
  5364. struct ggml_tensor * dst) {
  5365. const struct ggml_tensor * src0 = dst->src[0];
  5366. switch (src0->type) {
  5367. case GGML_TYPE_F32:
  5368. {
  5369. ggml_compute_forward_elu_f32(params, dst);
  5370. } break;
  5371. default:
  5372. {
  5373. GGML_ABORT("fatal error");
  5374. }
  5375. }
  5376. }
  5377. // ggml_compute_forward_relu
  5378. static void ggml_compute_forward_relu_f32(
  5379. const struct ggml_compute_params * params,
  5380. struct ggml_tensor * dst) {
  5381. const struct ggml_tensor * src0 = dst->src[0];
  5382. if (params->ith != 0) {
  5383. return;
  5384. }
  5385. assert(ggml_is_contiguous_1(src0));
  5386. assert(ggml_is_contiguous_1(dst));
  5387. assert(ggml_are_same_shape(src0, dst));
  5388. const int n = ggml_nrows(src0);
  5389. const int nc = src0->ne[0];
  5390. for (int i = 0; i < n; i++) {
  5391. ggml_vec_relu_f32(nc,
  5392. (float *) ((char *) dst->data + i*( dst->nb[1])),
  5393. (float *) ((char *) src0->data + i*(src0->nb[1])));
  5394. }
  5395. }
  5396. static void ggml_compute_forward_relu(
  5397. const struct ggml_compute_params * params,
  5398. struct ggml_tensor * dst) {
  5399. const struct ggml_tensor * src0 = dst->src[0];
  5400. switch (src0->type) {
  5401. case GGML_TYPE_F32:
  5402. {
  5403. ggml_compute_forward_relu_f32(params, dst);
  5404. } break;
  5405. default:
  5406. {
  5407. GGML_ABORT("fatal error");
  5408. }
  5409. }
  5410. }
  5411. // ggml_compute_forward_sigmoid
  5412. static void ggml_compute_forward_sigmoid_f32(
  5413. const struct ggml_compute_params * params,
  5414. struct ggml_tensor * dst) {
  5415. const struct ggml_tensor * src0 = dst->src[0];
  5416. if (params->ith != 0) {
  5417. return;
  5418. }
  5419. assert(ggml_is_contiguous_1(src0));
  5420. assert(ggml_is_contiguous_1(dst));
  5421. assert(ggml_are_same_shape(src0, dst));
  5422. const int n = ggml_nrows(src0);
  5423. const int nc = src0->ne[0];
  5424. for (int i = 0; i < n; i++) {
  5425. ggml_vec_sigmoid_f32(nc,
  5426. (float *) ((char *) dst->data + i*( dst->nb[1])),
  5427. (float *) ((char *) src0->data + i*(src0->nb[1])));
  5428. }
  5429. }
  5430. static void ggml_compute_forward_sigmoid(
  5431. const struct ggml_compute_params * params,
  5432. struct ggml_tensor * dst) {
  5433. const struct ggml_tensor * src0 = dst->src[0];
  5434. switch (src0->type) {
  5435. case GGML_TYPE_F32:
  5436. {
  5437. ggml_compute_forward_sigmoid_f32(params, dst);
  5438. } break;
  5439. default:
  5440. {
  5441. GGML_ABORT("fatal error");
  5442. }
  5443. }
  5444. }
  5445. // ggml_compute_forward_gelu
  5446. static void ggml_compute_forward_gelu_f32(
  5447. const struct ggml_compute_params * params,
  5448. struct ggml_tensor * dst) {
  5449. const struct ggml_tensor * src0 = dst->src[0];
  5450. assert(ggml_is_contiguous_1(src0));
  5451. assert(ggml_is_contiguous_1(dst));
  5452. assert(ggml_are_same_shape(src0, dst));
  5453. const int ith = params->ith;
  5454. const int nth = params->nth;
  5455. const int nc = src0->ne[0];
  5456. const int nr = ggml_nrows(src0);
  5457. // rows per thread
  5458. const int dr = (nr + nth - 1)/nth;
  5459. // row range for this thread
  5460. const int ir0 = dr*ith;
  5461. const int ir1 = MIN(ir0 + dr, nr);
  5462. for (int i1 = ir0; i1 < ir1; i1++) {
  5463. ggml_vec_gelu_f32(nc,
  5464. (float *) ((char *) dst->data + i1*( dst->nb[1])),
  5465. (float *) ((char *) src0->data + i1*(src0->nb[1])));
  5466. #ifndef NDEBUG
  5467. for (int k = 0; k < nc; k++) {
  5468. const float x = ((float *) ((char *) dst->data + i1*( dst->nb[1])))[k];
  5469. UNUSED(x);
  5470. assert(!isnan(x));
  5471. assert(!isinf(x));
  5472. }
  5473. #endif
  5474. }
  5475. }
  5476. static void ggml_compute_forward_gelu(
  5477. const struct ggml_compute_params * params,
  5478. struct ggml_tensor * dst) {
  5479. const struct ggml_tensor * src0 = dst->src[0];
  5480. switch (src0->type) {
  5481. case GGML_TYPE_F32:
  5482. {
  5483. ggml_compute_forward_gelu_f32(params, dst);
  5484. } break;
  5485. default:
  5486. {
  5487. GGML_ABORT("fatal error");
  5488. }
  5489. }
  5490. }
  5491. // ggml_compute_forward_gelu_quick
  5492. static void ggml_compute_forward_gelu_quick_f32(
  5493. const struct ggml_compute_params * params,
  5494. struct ggml_tensor * dst) {
  5495. const struct ggml_tensor * src0 = dst->src[0];
  5496. assert(ggml_is_contiguous_1(src0));
  5497. assert(ggml_is_contiguous_1(dst));
  5498. assert(ggml_are_same_shape(src0, dst));
  5499. const int ith = params->ith;
  5500. const int nth = params->nth;
  5501. const int nc = src0->ne[0];
  5502. const int nr = ggml_nrows(src0);
  5503. // rows per thread
  5504. const int dr = (nr + nth - 1)/nth;
  5505. // row range for this thread
  5506. const int ir0 = dr*ith;
  5507. const int ir1 = MIN(ir0 + dr, nr);
  5508. for (int i1 = ir0; i1 < ir1; i1++) {
  5509. ggml_vec_gelu_quick_f32(nc,
  5510. (float *) ((char *) dst->data + i1*( dst->nb[1])),
  5511. (float *) ((char *) src0->data + i1*(src0->nb[1])));
  5512. #ifndef NDEBUG
  5513. for (int k = 0; k < nc; k++) {
  5514. const float x = ((float *) ((char *) dst->data + i1*( dst->nb[1])))[k];
  5515. UNUSED(x);
  5516. assert(!isnan(x));
  5517. assert(!isinf(x));
  5518. }
  5519. #endif
  5520. }
  5521. }
  5522. static void ggml_compute_forward_gelu_quick(
  5523. const struct ggml_compute_params * params,
  5524. struct ggml_tensor * dst) {
  5525. const struct ggml_tensor * src0 = dst->src[0];
  5526. switch (src0->type) {
  5527. case GGML_TYPE_F32:
  5528. {
  5529. ggml_compute_forward_gelu_quick_f32(params, dst);
  5530. } break;
  5531. default:
  5532. {
  5533. GGML_ABORT("fatal error");
  5534. }
  5535. }
  5536. }
  5537. // ggml_compute_forward_silu
  5538. static void ggml_compute_forward_silu_f32(
  5539. const struct ggml_compute_params * params,
  5540. struct ggml_tensor * dst) {
  5541. const struct ggml_tensor * src0 = dst->src[0];
  5542. assert(ggml_is_contiguous_1(src0));
  5543. assert(ggml_is_contiguous_1(dst));
  5544. assert(ggml_are_same_shape(src0, dst));
  5545. const int ith = params->ith;
  5546. const int nth = params->nth;
  5547. const int nc = src0->ne[0];
  5548. const int nr = ggml_nrows(src0);
  5549. // rows per thread
  5550. const int dr = (nr + nth - 1)/nth;
  5551. // row range for this thread
  5552. const int ir0 = dr*ith;
  5553. const int ir1 = MIN(ir0 + dr, nr);
  5554. for (int i1 = ir0; i1 < ir1; i1++) {
  5555. ggml_vec_silu_f32(nc,
  5556. (float *) ((char *) dst->data + i1*( dst->nb[1])),
  5557. (float *) ((char *) src0->data + i1*(src0->nb[1])));
  5558. #ifndef NDEBUG
  5559. for (int k = 0; k < nc; k++) {
  5560. const float x = ((float *) ((char *) dst->data + i1*(dst->nb[1])))[k];
  5561. UNUSED(x);
  5562. assert(!isnan(x));
  5563. assert(!isinf(x));
  5564. }
  5565. #endif
  5566. }
  5567. }
  5568. static void ggml_compute_forward_silu(
  5569. const struct ggml_compute_params * params,
  5570. struct ggml_tensor * dst) {
  5571. const struct ggml_tensor * src0 = dst->src[0];
  5572. switch (src0->type) {
  5573. case GGML_TYPE_F32:
  5574. {
  5575. ggml_compute_forward_silu_f32(params, dst);
  5576. } break;
  5577. default:
  5578. {
  5579. GGML_ABORT("fatal error");
  5580. }
  5581. }
  5582. }
  5583. // ggml_compute_forward_leaky_relu
  5584. static void ggml_compute_forward_leaky_relu_f32(
  5585. const struct ggml_compute_params * params,
  5586. struct ggml_tensor * dst) {
  5587. const struct ggml_tensor * src0 = dst->src[0];
  5588. if (params->ith != 0) {
  5589. return;
  5590. }
  5591. assert(ggml_is_contiguous_1(src0));
  5592. assert(ggml_is_contiguous_1(dst));
  5593. assert(ggml_are_same_shape(src0, dst));
  5594. const int n = ggml_nrows(src0);
  5595. const int nc = src0->ne[0];
  5596. float negative_slope;
  5597. memcpy(&negative_slope, dst->op_params, sizeof(float));
  5598. assert(dst->nb[0] == sizeof(float));
  5599. assert(src0->nb[0] == sizeof(float));
  5600. for (int i = 0; i < n; i++) {
  5601. ggml_vec_leaky_relu_f32(nc,
  5602. (float *) ((char *) dst->data + i*( dst->nb[1])),
  5603. (float *) ((char *) src0->data + i*(src0->nb[1])), negative_slope);
  5604. }
  5605. }
  5606. static void ggml_compute_forward_leaky_relu(
  5607. const struct ggml_compute_params * params,
  5608. struct ggml_tensor * dst) {
  5609. const struct ggml_tensor * src0 = dst->src[0];
  5610. switch (src0->type) {
  5611. case GGML_TYPE_F32:
  5612. {
  5613. ggml_compute_forward_leaky_relu_f32(params, dst);
  5614. } break;
  5615. default:
  5616. {
  5617. GGML_ABORT("fatal error");
  5618. }
  5619. }
  5620. }
  5621. // ggml_compute_forward_silu_back
  5622. static void ggml_compute_forward_silu_back_f32(
  5623. const struct ggml_compute_params * params,
  5624. struct ggml_tensor * dst) {
  5625. const struct ggml_tensor * src0 = dst->src[0];
  5626. const struct ggml_tensor * grad = dst->src[1];
  5627. assert(ggml_is_contiguous_1(grad));
  5628. assert(ggml_is_contiguous_1(src0));
  5629. assert(ggml_is_contiguous_1(dst));
  5630. assert(ggml_are_same_shape(src0, dst));
  5631. assert(ggml_are_same_shape(src0, grad));
  5632. const int ith = params->ith;
  5633. const int nth = params->nth;
  5634. const int nc = src0->ne[0];
  5635. const int nr = ggml_nrows(src0);
  5636. // rows per thread
  5637. const int dr = (nr + nth - 1)/nth;
  5638. // row range for this thread
  5639. const int ir0 = dr*ith;
  5640. const int ir1 = MIN(ir0 + dr, nr);
  5641. for (int i1 = ir0; i1 < ir1; i1++) {
  5642. ggml_vec_silu_backward_f32(nc,
  5643. (float *) ((char *) dst->data + i1*( dst->nb[1])),
  5644. (float *) ((char *) src0->data + i1*(src0->nb[1])),
  5645. (float *) ((char *) grad->data + i1*(grad->nb[1])));
  5646. #ifndef NDEBUG
  5647. for (int k = 0; k < nc; k++) {
  5648. const float x = ((float *) ((char *) dst->data + i1*( dst->nb[1])))[k];
  5649. UNUSED(x);
  5650. assert(!isnan(x));
  5651. assert(!isinf(x));
  5652. }
  5653. #endif
  5654. }
  5655. }
  5656. static void ggml_compute_forward_silu_back(
  5657. const struct ggml_compute_params * params,
  5658. struct ggml_tensor * dst) {
  5659. const struct ggml_tensor * src0 = dst->src[0];
  5660. switch (src0->type) {
  5661. case GGML_TYPE_F32:
  5662. {
  5663. ggml_compute_forward_silu_back_f32(params, dst);
  5664. } break;
  5665. default:
  5666. {
  5667. GGML_ABORT("fatal error");
  5668. }
  5669. }
  5670. }
  5671. static void ggml_compute_forward_hardswish_f32(
  5672. const struct ggml_compute_params * params,
  5673. struct ggml_tensor * dst) {
  5674. const struct ggml_tensor * src0 = dst->src[0];
  5675. if (params->ith != 0) {
  5676. return;
  5677. }
  5678. assert(ggml_is_contiguous_1(src0));
  5679. assert(ggml_is_contiguous_1(dst));
  5680. assert(ggml_are_same_shape(src0, dst));
  5681. const int n = ggml_nrows(src0);
  5682. const int nc = src0->ne[0];
  5683. for (int i = 0; i < n; i++) {
  5684. ggml_vec_hardswish_f32(nc,
  5685. (float *) ((char *) dst->data + i*( dst->nb[1])),
  5686. (float *) ((char *) src0->data + i*(src0->nb[1])));
  5687. }
  5688. }
  5689. static void ggml_compute_forward_hardswish(
  5690. const struct ggml_compute_params * params,
  5691. struct ggml_tensor * dst) {
  5692. const struct ggml_tensor * src0 = dst->src[0];
  5693. switch (src0->type) {
  5694. case GGML_TYPE_F32:
  5695. {
  5696. ggml_compute_forward_hardswish_f32(params, dst);
  5697. } break;
  5698. default:
  5699. {
  5700. GGML_ABORT("fatal error");
  5701. }
  5702. }
  5703. }
  5704. static void ggml_compute_forward_hardsigmoid_f32(
  5705. const struct ggml_compute_params * params,
  5706. struct ggml_tensor * dst) {
  5707. const struct ggml_tensor * src0 = dst->src[0];
  5708. if (params->ith != 0) {
  5709. return;
  5710. }
  5711. assert(ggml_is_contiguous_1(src0));
  5712. assert(ggml_is_contiguous_1(dst));
  5713. assert(ggml_are_same_shape(src0, dst));
  5714. const int n = ggml_nrows(src0);
  5715. const int nc = src0->ne[0];
  5716. for (int i = 0; i < n; i++) {
  5717. ggml_vec_hardsigmoid_f32(nc,
  5718. (float *) ((char *) dst->data + i*( dst->nb[1])),
  5719. (float *) ((char *) src0->data + i*(src0->nb[1])));
  5720. }
  5721. }
  5722. static void ggml_compute_forward_hardsigmoid(
  5723. const struct ggml_compute_params * params,
  5724. struct ggml_tensor * dst) {
  5725. const struct ggml_tensor * src0 = dst->src[0];
  5726. switch (src0->type) {
  5727. case GGML_TYPE_F32:
  5728. {
  5729. ggml_compute_forward_hardsigmoid_f32(params, dst);
  5730. } break;
  5731. default:
  5732. {
  5733. GGML_ABORT("fatal error");
  5734. }
  5735. }
  5736. }
  5737. static void ggml_compute_forward_exp_f32(
  5738. const struct ggml_compute_params * params,
  5739. struct ggml_tensor * dst) {
  5740. const struct ggml_tensor * src0 = dst->src[0];
  5741. if (params->ith != 0) {
  5742. return;
  5743. }
  5744. assert(ggml_is_contiguous_1(src0));
  5745. assert(ggml_is_contiguous_1(dst));
  5746. assert(ggml_are_same_shape(src0, dst));
  5747. const int n = ggml_nrows(src0);
  5748. const int nc = src0->ne[0];
  5749. for (int i = 0; i < n; i++) {
  5750. ggml_vec_exp_f32(nc,
  5751. (float *) ((char *) dst->data + i*( dst->nb[1])),
  5752. (float *) ((char *) src0->data + i*(src0->nb[1])));
  5753. }
  5754. }
  5755. static void ggml_compute_forward_exp(
  5756. const struct ggml_compute_params * params,
  5757. struct ggml_tensor * dst) {
  5758. const struct ggml_tensor * src0 = dst->src[0];
  5759. switch (src0->type) {
  5760. case GGML_TYPE_F32:
  5761. {
  5762. ggml_compute_forward_exp_f32(params, dst);
  5763. } break;
  5764. default:
  5765. {
  5766. GGML_ABORT("fatal error");
  5767. }
  5768. }
  5769. }
  5770. // ggml_compute_forward_norm
  5771. static void ggml_compute_forward_norm_f32(
  5772. const struct ggml_compute_params * params,
  5773. struct ggml_tensor * dst) {
  5774. const struct ggml_tensor * src0 = dst->src[0];
  5775. GGML_ASSERT(ggml_are_same_shape(src0, dst));
  5776. GGML_ASSERT(src0->nb[0] == sizeof(float));
  5777. const int ith = params->ith;
  5778. const int nth = params->nth;
  5779. GGML_TENSOR_UNARY_OP_LOCALS
  5780. float eps;
  5781. memcpy(&eps, dst->op_params, sizeof(float));
  5782. GGML_ASSERT(eps > 0.0f);
  5783. // TODO: optimize
  5784. for (int64_t i03 = 0; i03 < ne03; i03++) {
  5785. for (int64_t i02 = 0; i02 < ne02; i02++) {
  5786. for (int64_t i01 = ith; i01 < ne01; i01 += nth) {
  5787. const float * x = (float *) ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03);
  5788. ggml_float sum = 0.0;
  5789. for (int64_t i00 = 0; i00 < ne00; i00++) {
  5790. sum += (ggml_float)x[i00];
  5791. }
  5792. float mean = sum/ne00;
  5793. float * y = (float *) ((char *) dst->data + i01*nb1 + i02*nb2 + i03*nb3);
  5794. ggml_float sum2 = 0.0;
  5795. for (int64_t i00 = 0; i00 < ne00; i00++) {
  5796. float v = x[i00] - mean;
  5797. y[i00] = v;
  5798. sum2 += (ggml_float)(v*v);
  5799. }
  5800. float variance = sum2/ne00;
  5801. const float scale = 1.0f/sqrtf(variance + eps);
  5802. ggml_vec_scale_f32(ne00, y, scale);
  5803. }
  5804. }
  5805. }
  5806. }
  5807. static void ggml_compute_forward_norm(
  5808. const struct ggml_compute_params * params,
  5809. struct ggml_tensor * dst) {
  5810. const struct ggml_tensor * src0 = dst->src[0];
  5811. switch (src0->type) {
  5812. case GGML_TYPE_F32:
  5813. {
  5814. ggml_compute_forward_norm_f32(params, dst);
  5815. } break;
  5816. default:
  5817. {
  5818. GGML_ABORT("fatal error");
  5819. }
  5820. }
  5821. }
  5822. // ggml_compute_forward_group_rms_norm
  5823. static void ggml_compute_forward_rms_norm_f32(
  5824. const struct ggml_compute_params * params,
  5825. struct ggml_tensor * dst) {
  5826. const struct ggml_tensor * src0 = dst->src[0];
  5827. GGML_ASSERT(ggml_are_same_shape(src0, dst));
  5828. GGML_ASSERT(src0->nb[0] == sizeof(float));
  5829. const int ith = params->ith;
  5830. const int nth = params->nth;
  5831. GGML_TENSOR_UNARY_OP_LOCALS
  5832. float eps;
  5833. memcpy(&eps, dst->op_params, sizeof(float));
  5834. GGML_ASSERT(eps > 0.0f);
  5835. // TODO: optimize
  5836. for (int64_t i03 = 0; i03 < ne03; i03++) {
  5837. for (int64_t i02 = 0; i02 < ne02; i02++) {
  5838. for (int64_t i01 = ith; i01 < ne01; i01 += nth) {
  5839. const float * x = (float *) ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03);
  5840. ggml_float sum = 0.0;
  5841. for (int64_t i00 = 0; i00 < ne00; i00++) {
  5842. sum += (ggml_float)(x[i00] * x[i00]);
  5843. }
  5844. const float mean = sum/ne00;
  5845. float * y = (float *) ((char *) dst->data + i01*nb1 + i02*nb2 + i03*nb3);
  5846. memcpy(y, x, ne00 * sizeof(float));
  5847. // for (int i00 = 0; i00 < ne00; i00++) {
  5848. // y[i00] = x[i00];
  5849. // }
  5850. const float scale = 1.0f/sqrtf(mean + eps);
  5851. ggml_vec_scale_f32(ne00, y, scale);
  5852. }
  5853. }
  5854. }
  5855. }
  5856. static void ggml_compute_forward_rms_norm(
  5857. const struct ggml_compute_params * params,
  5858. struct ggml_tensor * dst) {
  5859. const struct ggml_tensor * src0 = dst->src[0];
  5860. switch (src0->type) {
  5861. case GGML_TYPE_F32:
  5862. {
  5863. ggml_compute_forward_rms_norm_f32(params, dst);
  5864. } break;
  5865. default:
  5866. {
  5867. GGML_ABORT("fatal error");
  5868. }
  5869. }
  5870. }
  5871. static void ggml_compute_forward_rms_norm_back_f32(
  5872. const struct ggml_compute_params * params,
  5873. struct ggml_tensor * dst) {
  5874. const struct ggml_tensor * src0 = dst->src[0];
  5875. const struct ggml_tensor * src1 = dst->src[1];
  5876. GGML_ASSERT(ggml_are_same_shape(src0, dst) && ggml_are_same_shape(src0, src1));
  5877. GGML_ASSERT(src0->nb[0] == sizeof(float));
  5878. const int ith = params->ith;
  5879. const int nth = params->nth;
  5880. GGML_TENSOR_BINARY_OP_LOCALS
  5881. float eps;
  5882. memcpy(&eps, dst->op_params, sizeof(float));
  5883. // TODO: optimize
  5884. for (int64_t i03 = 0; i03 < ne03; i03++) {
  5885. for (int64_t i02 = 0; i02 < ne02; i02++) {
  5886. for (int64_t i01 = ith; i01 < ne01; i01 += nth) {
  5887. // src1 is same shape as src0 => same indices
  5888. const int64_t i11 = i01;
  5889. const int64_t i12 = i02;
  5890. const int64_t i13 = i03;
  5891. const float * x = (float *) ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03);
  5892. const float * dz = (float *) ((char *) src1->data + i11*nb11 + i12*nb12 + i13*nb13);
  5893. ggml_float sum_xx = 0.0;
  5894. ggml_float sum_xdz = 0.0;
  5895. for (int64_t i00 = 0; i00 < ne00; i00++) {
  5896. sum_xx += (ggml_float)(x[i00] * x[i00]);
  5897. sum_xdz += (ggml_float)(x[i00] * dz[i00]);
  5898. }
  5899. //const float mean = (float)(sum_xx)/ne00;
  5900. const float mean_eps = (float)(sum_xx)/ne00 + eps;
  5901. const float sum_eps = (float)(sum_xx) + eps*ne00;
  5902. //const float mean_xdz = (float)(sum_xdz)/ne00;
  5903. // we could cache rms from forward pass to improve performance.
  5904. // to do this implement ggml_rms and compose ggml_rms_norm using ggml_rms.
  5905. //const float rms = sqrtf(mean_eps);
  5906. const float rrms = 1.0f / sqrtf(mean_eps);
  5907. //const float scale = -rrms/(ne00 * mean_eps); // -1/(n*rms**3)
  5908. {
  5909. // z = rms_norm(x)
  5910. //
  5911. // rms_norm(src0) =
  5912. // scale(
  5913. // src0,
  5914. // div(
  5915. // 1,
  5916. // sqrt(
  5917. // add(
  5918. // scale(
  5919. // sum(
  5920. // sqr(
  5921. // src0)),
  5922. // (1.0/N)),
  5923. // eps))));
  5924. // postorder:
  5925. // ## op args grad
  5926. // 00 param src0 grad[#00]
  5927. // 01 const 1
  5928. // 02 sqr (#00) grad[#02]
  5929. // 03 sum (#02) grad[#03]
  5930. // 04 const 1/N
  5931. // 05 scale (#03, #04) grad[#05]
  5932. // 06 const eps
  5933. // 07 add (#05, #06) grad[#07]
  5934. // 08 sqrt (#07) grad[#08]
  5935. // 09 div (#01,#08) grad[#09]
  5936. // 10 scale (#00,#09) grad[#10]
  5937. //
  5938. // backward pass, given grad[#10]
  5939. // #10: scale
  5940. // grad[#00] += scale(grad[#10],#09)
  5941. // grad[#09] += sum(mul(grad[#10],#00))
  5942. // #09: div
  5943. // grad[#08] += neg(mul(grad[#09], div(#09,#08)))
  5944. // #08: sqrt
  5945. // grad[#07] += mul(grad[#08], div(0.5, #08))
  5946. // #07: add
  5947. // grad[#05] += grad[#07]
  5948. // #05: scale
  5949. // grad[#03] += scale(grad[#05],#04)
  5950. // #03: sum
  5951. // grad[#02] += repeat(grad[#03], #02)
  5952. // #02:
  5953. // grad[#00] += scale(mul(#00, grad[#02]), 2.0)
  5954. //
  5955. // substitute and simplify:
  5956. // grad[#00] = scale(grad(#10), #09) + scale(mul(#00, grad[#02]), 2.0)
  5957. // grad[#02] = repeat(grad[#03], #02)
  5958. // grad[#02] = repeat(scale(grad[#05],#04), #02)
  5959. // grad[#02] = repeat(scale(grad[#07],#04), #02)
  5960. // grad[#02] = repeat(scale(mul(grad[#08], div(0.5, #08)),#04), #02)
  5961. // grad[#02] = repeat(scale(mul(neg(mul(grad[#09], div(#09,#08))), div(0.5, #08)),#04), #02)
  5962. // grad[#02] = repeat(scale(mul(neg(mul(sum(mul(grad[#10],#00)), div(#09,#08))), div(0.5, #08)),#04), #02)
  5963. // grad[#02] = repeat(-(sum(mul(grad[#10],#00)) * div(#09,#08) * div(0.5, #08) * (1/N)), #02)
  5964. // grad[#02] = repeat(-(sum(mul(grad[#10],#00)) * div(div(#01,#08),#08) * div(0.5, #08) * (1/N)), #02)
  5965. // grad[#02] = repeat(-(sum(mul(grad[#10],#00)) * div(1,#08*#08) * div(0.5, #08) * (1/N)), #02)
  5966. // grad[#02] = repeat(-(sum(mul(grad[#10],#00)) * div(1,#07) * div(0.5, #08) * (1/N)), #02)
  5967. // grad[#00] = scale(grad(#10), #09) + scale(mul(#00, grad[#02]), 2.0)
  5968. // 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)
  5969. // 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)
  5970. // grad[#00] = scale(grad(#10), #09) + scale(#00, -(sum(mul(grad[#10],#00)) * div(1,#07) * div(1,#08) * (1/N)))
  5971. // grad[#00] = scale(grad(#10), #09) + scale(#00, sum(mul(grad[#10],#00)) * div(1,#07*#08) * (-1/N))
  5972. // grad[#00] = scale(grad(#10), #09) + scale(#00, sum(mul(grad[#10],#00)) * div(1,#07*#08) * (-1/N))
  5973. // grad[#00] = scale(grad(#10), #09) + scale(#00, sum(mul(grad[#10],#00)) * div(1,mean_eps*rms) * (-1/N))
  5974. // grad[#00] = scale(grad(#10), #09) + scale(#00, sum(mul(grad[#10],#00)) * div(-1,rms*N*mean_eps))
  5975. // grad[#00] = scale(grad(#10), #09) + scale(#00, sum(mul(grad[#10],#00)) * div(-1,rms*N*(sum_xx/N+eps)))
  5976. // grad[#00] = scale(grad(#10), #09) + scale(#00, sum(mul(grad[#10],#00)) * div(-1,rms*N*sum_xx+rms*N*eps))
  5977. // grad[#00] = scale(dz, rrms) + scale(x, sum(mul(dz,x)) * div(-1,rms*N*mean_eps))
  5978. // grad[#00] = scale(dz, rrms) + scale(x, sum_xdz * div(-1,rms*N*mean_eps))
  5979. // a = b*c + d*e
  5980. // a = b*c*f/f + d*e*f/f
  5981. // a = (b*c*f + d*e*f)*(1/f)
  5982. // a = (b*c*(1/c) + d*e*(1/c))*(1/(1/c))
  5983. // a = (b + d*e/c)*c
  5984. // b = dz, c = rrms, d = x, e = sum_xdz * div(-1,rms*N*mean_eps)
  5985. // a = (dz + x*sum_xdz * div(-1,rms*N*mean_eps)/rrms)*rrms
  5986. // a = (dz + x*sum_xdz * div(-1,rms*N*mean_eps)*rms)*rrms
  5987. // a = (dz + x*sum_xdz * div(-rms,rms*N*mean_eps))*rrms
  5988. // a = (dz + x*sum_xdz * div(-1,N*mean_eps))*rrms
  5989. // a = (dz + x*div(-sum_xdz,N*mean_eps))*rrms
  5990. // a = (dz + x*div(-mean_xdz,mean_eps))*rrms
  5991. // grad[#00] = scale(dz + scale(x, div(-mean_xdz,mean_eps)),rrms)
  5992. // grad[#00] = scale(dz + scale(x, -mean_xdz/mean_eps),rrms)
  5993. // dx = scale(dz + scale(x, -mean_xdz/mean_eps),rrms)
  5994. }
  5995. // dx = scale(dz + scale(x, -mean_xdz/mean_eps),rrms)
  5996. // post-order:
  5997. // dx := x
  5998. // dx := scale(dx,-mean_xdz/mean_eps)
  5999. // dx := add(dx, dz)
  6000. // dx := scale(dx, rrms)
  6001. float * dx = (float *) ((char *) dst->data + i01*nb1 + i02*nb2 + i03*nb3);
  6002. ggml_vec_cpy_f32 (ne00, dx, x);
  6003. // ggml_vec_scale_f32(ne00, dx, -mean_xdz/mean_eps);
  6004. ggml_vec_scale_f32(ne00, dx, (float)(-sum_xdz)/sum_eps);
  6005. ggml_vec_acc_f32 (ne00, dx, dz);
  6006. ggml_vec_scale_f32(ne00, dx, rrms);
  6007. }
  6008. }
  6009. }
  6010. }
  6011. static void ggml_compute_forward_rms_norm_back(
  6012. const struct ggml_compute_params * params,
  6013. struct ggml_tensor * dst) {
  6014. const struct ggml_tensor * src0 = dst->src[0];
  6015. switch (src0->type) {
  6016. case GGML_TYPE_F32:
  6017. {
  6018. ggml_compute_forward_rms_norm_back_f32(params, dst);
  6019. } break;
  6020. default:
  6021. {
  6022. GGML_ABORT("fatal error");
  6023. }
  6024. }
  6025. }
  6026. // ggml_compute_forward_group_norm
  6027. static void ggml_compute_forward_group_norm_f32(
  6028. const struct ggml_compute_params * params,
  6029. struct ggml_tensor * dst) {
  6030. const struct ggml_tensor * src0 = dst->src[0];
  6031. GGML_ASSERT(ggml_are_same_shape(src0, dst));
  6032. GGML_ASSERT(src0->nb[0] == sizeof(float));
  6033. const int ith = params->ith;
  6034. const int nth = params->nth;
  6035. GGML_TENSOR_UNARY_OP_LOCALS
  6036. // TODO: optimize
  6037. float eps;
  6038. memcpy(&eps, dst->op_params + 1, sizeof(float));
  6039. int n_channels = src0->ne[2];
  6040. int n_groups = dst->op_params[0];
  6041. int n_channels_per_group = (n_channels + n_groups - 1) / n_groups;
  6042. for (int i = ith; i < n_groups; i += nth) {
  6043. int start = i * n_channels_per_group;
  6044. int end = start + n_channels_per_group;
  6045. if (end > n_channels) {
  6046. end = n_channels;
  6047. }
  6048. int step = end - start;
  6049. for (int64_t i03 = 0; i03 < ne03; i03++) {
  6050. ggml_float sum = 0.0;
  6051. for (int64_t i02 = start; i02 < end; i02++) {
  6052. for (int64_t i01 = 0; i01 < ne01; i01++) {
  6053. const float * x = (float *)((char *) src0->data + i01 * nb01 + i02 * nb02 + i03 * nb03);
  6054. ggml_float sumr = 0.0;
  6055. for (int64_t i00 = 0; i00 < ne00; i00++) {
  6056. sumr += (ggml_float)x[i00];
  6057. }
  6058. sum += sumr;
  6059. }
  6060. }
  6061. const float mean = sum / (ne00 * ne01 * step);
  6062. ggml_float sum2 = 0.0;
  6063. for (int64_t i02 = start; i02 < end; i02++) {
  6064. for (int64_t i01 = 0; i01 < ne01; i01++) {
  6065. const float * x = (float *)((char *) src0->data + i01 * nb01 + i02 * nb02 + i03 * nb03);
  6066. float * y = (float *)((char *) dst->data + i01 * nb1 + i02 * nb2 + i03 * nb3);
  6067. ggml_float sumr = 0.0;
  6068. for (int64_t i00 = 0; i00 < ne00; i00++) {
  6069. float v = x[i00] - mean;
  6070. y[i00] = v;
  6071. sumr += (ggml_float)(v * v);
  6072. }
  6073. sum2 += sumr;
  6074. }
  6075. }
  6076. const float variance = sum2 / (ne00 * ne01 * step);
  6077. const float scale = 1.0f / sqrtf(variance + eps);
  6078. for (int64_t i02 = start; i02 < end; i02++) {
  6079. for (int64_t i01 = 0; i01 < ne01; i01++) {
  6080. float * y = (float *)((char *) dst->data + i01 * nb1 + i02 * nb2 + i03 * nb3);
  6081. ggml_vec_scale_f32(ne00, y, scale);
  6082. }
  6083. }
  6084. }
  6085. }
  6086. }
  6087. static void ggml_compute_forward_group_norm(
  6088. const struct ggml_compute_params * params,
  6089. struct ggml_tensor * dst) {
  6090. const struct ggml_tensor * src0 = dst->src[0];
  6091. switch (src0->type) {
  6092. case GGML_TYPE_F32:
  6093. {
  6094. ggml_compute_forward_group_norm_f32(params, dst);
  6095. } break;
  6096. default:
  6097. {
  6098. GGML_ABORT("fatal error");
  6099. }
  6100. }
  6101. }
  6102. // ggml_compute_forward_mul_mat
  6103. static void ggml_compute_forward_mul_mat_one_chunk(
  6104. const struct ggml_compute_params * params,
  6105. struct ggml_tensor * dst,
  6106. const enum ggml_type type,
  6107. const int64_t num_rows_per_vec_dot,
  6108. const int64_t ir0_start,
  6109. const int64_t ir0_end,
  6110. const int64_t ir1_start,
  6111. const int64_t ir1_end) {
  6112. const struct ggml_tensor * src0 = dst->src[0];
  6113. const struct ggml_tensor * src1 = dst->src[1];
  6114. GGML_TENSOR_BINARY_OP_LOCALS
  6115. const bool src1_cont = ggml_is_contiguous(src1);
  6116. ggml_vec_dot_t const vec_dot = type_traits_cpu[type].vec_dot;
  6117. enum ggml_type const vec_dot_type = type_traits_cpu[type].vec_dot_type;
  6118. // broadcast factors
  6119. const int64_t r2 = ne12 / ne02;
  6120. const int64_t r3 = ne13 / ne03;
  6121. //printf("ir0_start = %6lld, ir0_end = %6lld, ir1_start = %6lld, ir1_end = %6lld\n", ir0_start, ir0_end, ir1_start, ir1_end);
  6122. // threads with no work simply yield (not sure if it helps)
  6123. if (ir0_start >= ir0_end || ir1_start >= ir1_end) {
  6124. return;
  6125. }
  6126. const void * wdata = (src1->type == vec_dot_type) ? src1->data : params->wdata;
  6127. const size_t row_size = ggml_row_size(vec_dot_type, ne10);
  6128. assert(ne12 % ne02 == 0);
  6129. assert(ne13 % ne03 == 0);
  6130. // block-tiling attempt
  6131. const int64_t blck_0 = 16;
  6132. const int64_t blck_1 = 16;
  6133. const size_t src1_col_stride = src1_cont || src1->type != vec_dot_type ? row_size : nb11;
  6134. // attempt to reduce false-sharing (does not seem to make a difference)
  6135. // 16 * 2, accounting for mmla kernels
  6136. float tmp[32];
  6137. for (int64_t iir1 = ir1_start; iir1 < ir1_end; iir1 += blck_1) {
  6138. for (int64_t iir0 = ir0_start; iir0 < ir0_end; iir0 += blck_0) {
  6139. for (int64_t ir1 = iir1; ir1 < iir1 + blck_1 && ir1 < ir1_end; ir1 += num_rows_per_vec_dot) {
  6140. const int64_t i13 = (ir1 / (ne12 * ne1));
  6141. const int64_t i12 = (ir1 - i13 * ne12 * ne1) / ne1;
  6142. const int64_t i11 = (ir1 - i13 * ne12 * ne1 - i12 * ne1);
  6143. // broadcast src0 into src1
  6144. const int64_t i03 = i13 / r3;
  6145. const int64_t i02 = i12 / r2;
  6146. const int64_t i1 = i11;
  6147. const int64_t i2 = i12;
  6148. const int64_t i3 = i13;
  6149. const char * src0_row = (const char*)src0->data + (0 + i02 * nb02 + i03 * nb03);
  6150. // desc: when src1 is not a contiguous memory block we have to calculate the offset using the strides
  6151. // if it is, then we have either copied the data to params->wdata and made it contiguous or we are using
  6152. // the original src1 data pointer, so we should index using the indices directly
  6153. // TODO: this is a bit of a hack, we should probably have a better way to handle this
  6154. const char * src1_col = (const char*)wdata +
  6155. (src1_cont || src1->type != vec_dot_type
  6156. ? (i11 + i12 * ne11 + i13 * ne12 * ne11) * row_size
  6157. : (i11 * nb11 + i12 * nb12 + i13 * nb13));
  6158. float * dst_col = (float*)((char*)dst->data + (i1 * nb1 + i2 * nb2 + i3 * nb3));
  6159. //for (int64_t ir0 = iir0; ir0 < iir0 + blck_0 && ir0 < ir0_end; ++ir0) {
  6160. // vec_dot(ne00, &dst_col[ir0], src0_row + ir0*nb01, src1_col);
  6161. //}
  6162. for (int64_t ir0 = iir0; ir0 < iir0 + blck_0 && ir0 < ir0_end; ir0 += num_rows_per_vec_dot) {
  6163. 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);
  6164. }
  6165. for (int cn = 0; cn < num_rows_per_vec_dot; ++cn) {
  6166. memcpy(&dst_col[iir0 + cn * nb1 / nb0], tmp + (cn * 16), (MIN(iir0 + blck_0, ir0_end) - iir0) * sizeof(float));
  6167. }
  6168. }
  6169. }
  6170. }
  6171. }
  6172. static void ggml_compute_forward_mul_mat(
  6173. const struct ggml_compute_params * params,
  6174. struct ggml_tensor * dst) {
  6175. const struct ggml_tensor * src0 = dst->src[0];
  6176. const struct ggml_tensor * src1 = dst->src[1];
  6177. GGML_TENSOR_BINARY_OP_LOCALS
  6178. const int ith = params->ith;
  6179. const int nth = params->nth;
  6180. enum ggml_type const vec_dot_type = type_traits_cpu[src0->type].vec_dot_type;
  6181. ggml_from_float_t const from_float = type_traits_cpu[vec_dot_type].from_float;
  6182. int64_t const vec_dot_num_rows = type_traits_cpu[src0->type].nrows;
  6183. GGML_ASSERT(ne0 == ne01);
  6184. GGML_ASSERT(ne1 == ne11);
  6185. GGML_ASSERT(ne2 == ne12);
  6186. GGML_ASSERT(ne3 == ne13);
  6187. // we don't support permuted src0 or src1
  6188. GGML_ASSERT(nb00 == ggml_type_size(src0->type));
  6189. GGML_ASSERT(nb10 == ggml_type_size(src1->type));
  6190. // dst cannot be transposed or permuted
  6191. GGML_ASSERT(nb0 == sizeof(float));
  6192. GGML_ASSERT(nb0 <= nb1);
  6193. GGML_ASSERT(nb1 <= nb2);
  6194. GGML_ASSERT(nb2 <= nb3);
  6195. // nb01 >= nb00 - src0 is not transposed
  6196. // compute by src0 rows
  6197. // TODO: extract to "extra_op"
  6198. #if GGML_USE_LLAMAFILE
  6199. // broadcast factors
  6200. const int64_t r2 = ne12 / ne02;
  6201. const int64_t r3 = ne13 / ne03;
  6202. const bool src1_cont = ggml_is_contiguous(src1);
  6203. if (src1_cont) {
  6204. for (int64_t i13 = 0; i13 < ne13; i13++)
  6205. for (int64_t i12 = 0; i12 < ne12; i12++)
  6206. if (!llamafile_sgemm(params,
  6207. ne01, ne11, ne00/ggml_blck_size(src0->type),
  6208. (const char *)src0->data + i12/r2*nb02 + i13/r3*nb03,
  6209. nb01/ggml_type_size(src0->type),
  6210. (const char *)src1->data + i12*nb12 + i13*nb13,
  6211. nb11/ggml_type_size(src1->type),
  6212. (char *)dst->data + i12*nb2 + i13*nb3,
  6213. nb1/ggml_type_size(dst->type),
  6214. src0->type,
  6215. src1->type,
  6216. dst->type))
  6217. goto UseGgmlGemm1;
  6218. return;
  6219. }
  6220. UseGgmlGemm1:;
  6221. #endif
  6222. if (src1->type != vec_dot_type) {
  6223. char * wdata = params->wdata;
  6224. const size_t nbw1 = ggml_row_size(vec_dot_type, ne10);
  6225. const size_t nbw2 = nbw1*ne11;
  6226. const size_t nbw3 = nbw2*ne12;
  6227. assert(params->wsize >= ne13*nbw3);
  6228. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  6229. for (int64_t i13 = 0; i13 < ne13; ++i13) {
  6230. for (int64_t i12 = 0; i12 < ne12; ++i12) {
  6231. for (int64_t i11 = ith; i11 < ne11; i11 += nth) {
  6232. from_float((float *)((char *) src1->data + i13*nb13 + i12*nb12 + i11*nb11),
  6233. (void *) (wdata + i13*nbw3 + i12*nbw2 + i11*nbw1),
  6234. ne10);
  6235. }
  6236. }
  6237. }
  6238. }
  6239. if (ith == 0) {
  6240. // Every thread starts at ith, so the first unprocessed chunk is nth. This save a bit of coordination right at the start.
  6241. atomic_store_explicit(&params->threadpool->current_chunk, nth, memory_order_relaxed);
  6242. }
  6243. ggml_barrier(params->threadpool);
  6244. #if GGML_USE_LLAMAFILE
  6245. if (src1->type != vec_dot_type) {
  6246. const void* wdata = (src1->type == vec_dot_type) ? src1->data : params->wdata;
  6247. const size_t row_size = ggml_row_size(vec_dot_type, ne10);
  6248. for (int64_t i13 = 0; i13 < ne13; i13++)
  6249. for (int64_t i12 = 0; i12 < ne12; i12++)
  6250. if (!llamafile_sgemm(params,
  6251. ne01, ne11, ne00/ggml_blck_size(src0->type),
  6252. (const char *)src0->data + i12/r2*nb02 + i13/r3*nb03,
  6253. nb01/ggml_type_size(src0->type),
  6254. (const char *)wdata + (i12*ne11 + i13*ne12*ne11)*row_size,
  6255. row_size/ggml_type_size(vec_dot_type),
  6256. (char *)dst->data + i12*nb2 + i13*nb3,
  6257. nb1/ggml_type_size(dst->type),
  6258. src0->type,
  6259. vec_dot_type,
  6260. dst->type))
  6261. goto UseGgmlGemm2;
  6262. return;
  6263. }
  6264. UseGgmlGemm2:;
  6265. #endif
  6266. // 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)
  6267. const int64_t nr0 = ne0;
  6268. // This is the size of the rest of the dimensions of the result
  6269. const int64_t nr1 = ne1 * ne2 * ne3;
  6270. // Now select a reasonable chunk size.
  6271. int chunk_size = 16;
  6272. // We need to step up the size if it's small
  6273. if (nr0 == 1 || nr1 == 1) {
  6274. chunk_size = 64;
  6275. }
  6276. // distribute the work across the inner or outer loop based on which one is larger
  6277. // The number of chunks in the 0/1 dim.
  6278. // CEIL(nr0/chunk_size)
  6279. int64_t nchunk0 = (nr0 + chunk_size - 1) / chunk_size;
  6280. int64_t nchunk1 = (nr1 + chunk_size - 1) / chunk_size;
  6281. // If the chunking is poor for the number of threads on this setup, scrap the whole plan. Re-chunk it by thread.
  6282. // Also, chunking by thread was measured to have perform better on NUMA systems. See https://github.com/ggerganov/llama.cpp/pull/6915
  6283. // In theory, chunking should be just as useful on NUMA and non NUMA systems, but testing disagreed with that.
  6284. if (nchunk0 * nchunk1 < nth * 4 || ggml_is_numa()) {
  6285. // distribute the thread work across the inner or outer loop based on which one is larger
  6286. nchunk0 = nr0 > nr1 ? nth : 1; // parallelize by src0 rows
  6287. nchunk1 = nr0 > nr1 ? 1 : nth; // parallelize by src1 rows
  6288. }
  6289. // The number of elements in each chunk
  6290. const int64_t dr0 = (nr0 + nchunk0 - 1) / nchunk0;
  6291. const int64_t dr1 = (nr1 + nchunk1 - 1) / nchunk1;
  6292. // The first chunk comes from our thread_id, the rest will get auto-assigned.
  6293. int current_chunk = ith;
  6294. while (current_chunk < nchunk0 * nchunk1) {
  6295. const int64_t ith0 = current_chunk % nchunk0;
  6296. const int64_t ith1 = current_chunk / nchunk0;
  6297. const int64_t ir0_start = dr0 * ith0;
  6298. const int64_t ir0_end = MIN(ir0_start + dr0, nr0);
  6299. const int64_t ir1_start = dr1 * ith1;
  6300. const int64_t ir1_end = MIN(ir1_start + dr1, nr1);
  6301. // dot kernels can handle 1 row and col at a time, but mmla kernels can process 2 rows and cols
  6302. int64_t num_rows_per_vec_dot = vec_dot_num_rows;
  6303. // these checks are needed to avoid crossing dim1 boundaries
  6304. // can be optimized, but the logic would become more complicated, so keeping it like this for simplicity
  6305. if ((nr0 % 2 != 0) || (ne11 % 2 != 0) || ((ir0_end - ir0_start) % 2 != 0) || ((ir1_end - ir1_start) % 2 != 0)) {
  6306. num_rows_per_vec_dot = 1;
  6307. }
  6308. ggml_compute_forward_mul_mat_one_chunk(params, dst, src0->type, num_rows_per_vec_dot, ir0_start, ir0_end, ir1_start, ir1_end);
  6309. if (nth >= nchunk0 * nchunk1) {
  6310. break;
  6311. }
  6312. current_chunk = atomic_fetch_add_explicit(&params->threadpool->current_chunk, 1, memory_order_relaxed);
  6313. }
  6314. }
  6315. // ggml_compute_forward_mul_mat_id
  6316. static void ggml_compute_forward_mul_mat_id(
  6317. const struct ggml_compute_params * params,
  6318. struct ggml_tensor * dst) {
  6319. const struct ggml_tensor * src0 = dst->src[0];
  6320. const struct ggml_tensor * src1 = dst->src[1];
  6321. const struct ggml_tensor * ids = dst->src[2];
  6322. GGML_TENSOR_BINARY_OP_LOCALS
  6323. const int ith = params->ith;
  6324. const int nth = params->nth;
  6325. const enum ggml_type type = src0->type;
  6326. const bool src1_cont = ggml_is_contiguous(src1);
  6327. ggml_vec_dot_t const vec_dot = type_traits_cpu[type].vec_dot;
  6328. enum ggml_type const vec_dot_type = type_traits_cpu[type].vec_dot_type;
  6329. ggml_from_float_t const from_float = type_traits_cpu[vec_dot_type].from_float;
  6330. // we don't support permuted src0 or src1
  6331. GGML_ASSERT(nb00 == ggml_type_size(type));
  6332. GGML_ASSERT(nb10 == ggml_type_size(src1->type));
  6333. // dst cannot be transposed or permuted
  6334. GGML_ASSERT(nb0 == sizeof(float));
  6335. GGML_ASSERT(nb0 <= nb1);
  6336. GGML_ASSERT(nb1 <= nb2);
  6337. GGML_ASSERT(nb2 <= nb3);
  6338. // row groups
  6339. const int n_ids = ids->ne[0]; // n_expert_used
  6340. const int n_as = ne02; // n_expert
  6341. char * wdata_src1_end = (src1->type == vec_dot_type) ?
  6342. (char *) params->wdata :
  6343. (char *) params->wdata + GGML_PAD(ggml_row_size(vec_dot_type, ggml_nelements(src1)), sizeof(int64_t));
  6344. struct mmid_row_mapping {
  6345. int32_t i1;
  6346. int32_t i2;
  6347. };
  6348. int64_t * matrix_row_counts = (int64_t *) (wdata_src1_end); // [n_as]
  6349. struct mmid_row_mapping * matrix_rows = (struct mmid_row_mapping *)(matrix_row_counts + n_as); // [n_as][ne11]
  6350. if (src1->type != vec_dot_type) {
  6351. char * wdata = params->wdata;
  6352. const size_t nbw1 = ggml_row_size(vec_dot_type, ne10);
  6353. const size_t nbw2 = nbw1*ne11;
  6354. const size_t nbw3 = nbw2*ne12;
  6355. assert(params->wsize >= ne13*nbw3);
  6356. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  6357. for (int64_t i13 = 0; i13 < ne13; ++i13) {
  6358. for (int64_t i12 = 0; i12 < ne12; ++i12) {
  6359. for (int64_t i11 = ith; i11 < ne11; i11 += nth) {
  6360. from_float((float *)((char *) src1->data + i13*nb13 + i12*nb12 + i11*nb11),
  6361. (void *) (wdata + i13*nbw3 + i12*nbw2 + i11*nbw1),
  6362. ne10);
  6363. }
  6364. }
  6365. }
  6366. }
  6367. #define MMID_MATRIX_ROW(row_id, i1) matrix_rows[(row_id)*ne12 + (i1)]
  6368. if (ith == 0) {
  6369. // initialize matrix_row_counts
  6370. memset(matrix_row_counts, 0, n_as*sizeof(int64_t));
  6371. // group rows by src0 matrix
  6372. for (int64_t iid1 = 0; iid1 < ids->ne[1]; ++iid1) {
  6373. for (int id = 0; id < n_ids; ++id) {
  6374. const int32_t i02 = *(const int32_t *) ((const char *) ids->data + iid1*ids->nb[1] + id*ids->nb[0]);
  6375. assert(i02 >= 0 && i02 < n_as);
  6376. MMID_MATRIX_ROW(i02, matrix_row_counts[i02]) = (struct mmid_row_mapping) {id, iid1};
  6377. matrix_row_counts[i02] += 1;
  6378. }
  6379. }
  6380. }
  6381. ggml_barrier(params->threadpool);
  6382. // compute each matrix multiplication in sequence
  6383. for (int cur_a = 0; cur_a < n_as; ++cur_a) {
  6384. const int64_t cne1 = matrix_row_counts[cur_a];
  6385. if (cne1 == 0) {
  6386. continue;
  6387. }
  6388. const char * src0_cur = (const char *) src0->data + cur_a*nb02;
  6389. const void * wdata = (src1->type == vec_dot_type) ? src1->data : params->wdata;
  6390. const size_t row_size = ggml_row_size(vec_dot_type, ne10);
  6391. const int64_t nr0 = ne01; // src0 rows
  6392. const int64_t nr1 = cne1; // src1 rows
  6393. // distribute the thread work across the inner or outer loop based on which one is larger
  6394. const int64_t nth0 = nr0 > nr1 ? nth : 1; // parallelize by src0 rows
  6395. const int64_t nth1 = nr0 > nr1 ? 1 : nth; // parallelize by src1 rows
  6396. const int64_t ith0 = ith % nth0;
  6397. const int64_t ith1 = ith / nth0;
  6398. const int64_t dr0 = (nr0 + nth0 - 1)/nth0;
  6399. const int64_t dr1 = (nr1 + nth1 - 1)/nth1;
  6400. const int64_t ir010 = dr0*ith0;
  6401. const int64_t ir011 = MIN(ir010 + dr0, nr0);
  6402. const int64_t ir110 = dr1*ith1;
  6403. const int64_t ir111 = MIN(ir110 + dr1, nr1);
  6404. // threads with no work simply yield (not sure if it helps)
  6405. //if (ir010 >= ir011 || ir110 >= ir111) {
  6406. // sched_yield();
  6407. // continue;
  6408. //}
  6409. // block-tiling attempt
  6410. const int64_t blck_0 = 16;
  6411. const int64_t blck_1 = 16;
  6412. // attempt to reduce false-sharing (does not seem to make a difference)
  6413. float tmp[16];
  6414. for (int64_t iir1 = ir110; iir1 < ir111; iir1 += blck_1) {
  6415. for (int64_t iir0 = ir010; iir0 < ir011; iir0 += blck_0) {
  6416. for (int64_t ir1 = iir1; ir1 < iir1 + blck_1 && ir1 < ir111; ++ir1) {
  6417. const int64_t _i12 = ir1; // logical row index for this expert
  6418. struct mmid_row_mapping row_mapping = MMID_MATRIX_ROW(cur_a, _i12);
  6419. const int id = row_mapping.i1; // selected expert index
  6420. const int64_t i11 = id % ne11;
  6421. const int64_t i12 = row_mapping.i2; // row index in src1
  6422. const int64_t i1 = id; // selected expert index
  6423. const int64_t i2 = i12; // row
  6424. // desc: when src1 is not a contiguous memory block we have to calculate the offset using the strides
  6425. // if it is, then we have either copied the data to params->wdata and made it contiguous or we are using
  6426. // the original src1 data pointer, so we should index using the indices directly
  6427. // TODO: this is a bit of a hack, we should probably have a better way to handle this
  6428. const char * src1_col = (const char *) wdata +
  6429. (src1_cont || src1->type != vec_dot_type
  6430. ? (i11 + i12*ne11)*row_size
  6431. : (i11*nb11 + i12*nb12));
  6432. float * dst_col = (float *) ((char *) dst->data + (i1*nb1 + i2*nb2));
  6433. //for (int64_t ir0 = iir0; ir0 < iir0 + blck_0 && ir0 < ir011; ++ir0) {
  6434. // vec_dot(ne00, &dst_col[ir0], src0_row + ir0*nb01, src1_col);
  6435. //}
  6436. for (int64_t ir0 = iir0; ir0 < iir0 + blck_0 && ir0 < ir011; ++ir0) {
  6437. vec_dot(ne00, &tmp[ir0 - iir0], 0, src0_cur + ir0*nb01, 0, src1_col, 0, 1);
  6438. }
  6439. memcpy(&dst_col[iir0], tmp, (MIN(iir0 + blck_0, ir011) - iir0)*sizeof(float));
  6440. }
  6441. }
  6442. }
  6443. }
  6444. #undef MMID_MATRIX_ROW
  6445. }
  6446. // ggml_compute_forward_out_prod
  6447. static void ggml_compute_forward_out_prod_f32(
  6448. const struct ggml_compute_params * params,
  6449. struct ggml_tensor * dst) {
  6450. const struct ggml_tensor * src0 = dst->src[0];
  6451. const struct ggml_tensor * src1 = dst->src[1];
  6452. GGML_TENSOR_BINARY_OP_LOCALS
  6453. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  6454. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  6455. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  6456. const int ith = params->ith;
  6457. const int nth = params->nth;
  6458. GGML_ASSERT(ne0 == ne00);
  6459. GGML_ASSERT(ne1 == ne10);
  6460. GGML_ASSERT(ne2 == ne02);
  6461. GGML_ASSERT(ne02 == ne12);
  6462. GGML_ASSERT(ne3 == ne13);
  6463. GGML_ASSERT(ne03 == ne13);
  6464. // we don't support permuted src0 or src1
  6465. GGML_ASSERT(nb00 == sizeof(float));
  6466. // dst cannot be transposed or permuted
  6467. GGML_ASSERT(nb0 == sizeof(float));
  6468. // GGML_ASSERT(nb0 <= nb1);
  6469. // GGML_ASSERT(nb1 <= nb2);
  6470. // GGML_ASSERT(nb2 <= nb3);
  6471. // nb01 >= nb00 - src0 is not transposed
  6472. // compute by src0 rows
  6473. if (ith == 0) {
  6474. ggml_vec_set_f32(ne0*ne1*ne2*ne3, dst->data, 0);
  6475. }
  6476. ggml_barrier(params->threadpool);
  6477. // dst[:,:,:,:] = 0
  6478. // for i2,i3:
  6479. // for i1:
  6480. // for i01:
  6481. // for i0:
  6482. // dst[i0,i1,i2,i3] += src0[i0,i01,i2,i3] * src1[i1,i01,i2,i3]
  6483. // parallelize by last three dimensions
  6484. // total rows in dst
  6485. const int64_t nr = ne1*ne2*ne3;
  6486. // rows per thread
  6487. const int64_t dr = (nr + nth - 1)/nth;
  6488. // row range for this thread
  6489. const int64_t ir0 = dr*ith;
  6490. const int64_t ir1 = MIN(ir0 + dr, nr);
  6491. // block-tiling attempt
  6492. const int64_t blck_0 = MAX(GGML_VEC_MAD_UNROLL, 32);
  6493. const int64_t blck_1 = 16;
  6494. for (int64_t bir = ir0; bir < ir1; bir += blck_1) {
  6495. const int64_t bir1 = MIN(bir + blck_1, ir1);
  6496. for (int64_t bi01 = 0; bi01 < ne01; bi01 += blck_0) {
  6497. const int64_t bne01 = MIN(bi01 + blck_0, ne01);
  6498. for (int64_t ir = bir; ir < bir1; ++ir) {
  6499. // dst indices
  6500. const int64_t i3 = ir/(ne2*ne1);
  6501. const int64_t i2 = (ir - i3*ne2*ne1)/ne1;
  6502. const int64_t i1 = (ir - i3*ne2*ne1 - i2*ne1);
  6503. const int64_t i02 = i2;
  6504. const int64_t i03 = i3;
  6505. //const int64_t i10 = i1;
  6506. const int64_t i12 = i2;
  6507. const int64_t i13 = i3;
  6508. #if GGML_VEC_MAD_UNROLL > 2
  6509. const int64_t bne01_unroll = bne01 - (bne01 % GGML_VEC_MAD_UNROLL);
  6510. for (int64_t i01 = bi01; i01 < bne01_unroll; i01 += GGML_VEC_MAD_UNROLL) {
  6511. const int64_t i11 = i01;
  6512. float * s0 = (float *) ((char *) src0->data + ( i01*nb01 + i02*nb02 + i03*nb03));
  6513. float * s1 = (float *) ((char *) src1->data + (i1*nb10 + i11*nb11 + i12*nb12 + i13*nb13));
  6514. float * d = (float *) ((char *) dst->data + ( i1*nb1 + i2*nb2 + i3*nb3));
  6515. ggml_vec_mad_f32_unroll(ne0, nb01, nb11, d, s0, s1);
  6516. }
  6517. for (int64_t i01 = bne01_unroll; i01 < bne01; ++i01) {
  6518. const int64_t i11 = i01;
  6519. float * s0 = (float *) ((char *) src0->data + ( i01*nb01 + i02*nb02 + i03*nb03));
  6520. float * s1 = (float *) ((char *) src1->data + (i1*nb10 + i11*nb11 + i12*nb12 + i13*nb13));
  6521. float * d = (float *) ((char *) dst->data + ( i1*nb1 + i2*nb2 + i3*nb3));
  6522. ggml_vec_mad_f32(ne0, d, s0, *s1);
  6523. }
  6524. #else
  6525. for (int64_t i01 = bi01; i01 < bne01; ++i01) {
  6526. const int64_t i11 = i01;
  6527. float * s0 = (float *) ((char *) src0->data + ( i01*nb01 + i02*nb02 + i03*nb03));
  6528. float * s1 = (float *) ((char *) src1->data + (i1*nb10 + i11*nb11 + i12*nb12 + i13*nb13));
  6529. float * d = (float *) ((char *) dst->data + ( i1*nb1 + i2*nb2 + i3*nb3));
  6530. ggml_vec_mad_f32(ne0, d, s0, *s1);
  6531. }
  6532. #endif
  6533. }
  6534. }
  6535. }
  6536. }
  6537. static void ggml_compute_forward_out_prod_q_f32(
  6538. const struct ggml_compute_params * params,
  6539. struct ggml_tensor * dst) {
  6540. const struct ggml_tensor * src0 = dst->src[0];
  6541. const struct ggml_tensor * src1 = dst->src[1];
  6542. GGML_TENSOR_BINARY_OP_LOCALS;
  6543. const int ith = params->ith;
  6544. const int nth = params->nth;
  6545. const enum ggml_type type = src0->type;
  6546. ggml_to_float_t const dequantize_row_q = ggml_get_type_traits(type)->to_float;
  6547. GGML_ASSERT(ne02 == ne12);
  6548. GGML_ASSERT(ne03 == ne13);
  6549. GGML_ASSERT(ne2 == ne12);
  6550. GGML_ASSERT(ne3 == ne13);
  6551. // we don't support permuted src0 dim0
  6552. GGML_ASSERT(nb00 == ggml_type_size(type));
  6553. // dst dim0 cannot be transposed or permuted
  6554. GGML_ASSERT(nb0 == sizeof(float));
  6555. // GGML_ASSERT(nb0 <= nb1);
  6556. // GGML_ASSERT(nb1 <= nb2);
  6557. // GGML_ASSERT(nb2 <= nb3);
  6558. GGML_ASSERT(ne0 == ne00);
  6559. GGML_ASSERT(ne1 == ne10);
  6560. GGML_ASSERT(ne2 == ne02);
  6561. GGML_ASSERT(ne3 == ne03);
  6562. // nb01 >= nb00 - src0 is not transposed
  6563. // compute by src0 rows
  6564. if (ith == 0) {
  6565. ggml_vec_set_f32(ne0*ne1*ne2*ne3, dst->data, 0);
  6566. }
  6567. ggml_barrier(params->threadpool);
  6568. // parallelize by last three dimensions
  6569. // total rows in dst
  6570. const int64_t nr = ne1*ne2*ne3;
  6571. // rows per thread
  6572. const int64_t dr = (nr + nth - 1)/nth;
  6573. // row range for this thread
  6574. const int64_t ir0 = dr*ith;
  6575. const int64_t ir1 = MIN(ir0 + dr, nr);
  6576. // dst[:,:,:,:] = 0
  6577. // for i2,i3:
  6578. // for i1:
  6579. // for i01:
  6580. // for i0:
  6581. // dst[i0,i1,i2,i3] += src0[i0,i01,i2,i3] * src1[i1,i01,i2,i3]
  6582. float * wdata = (float *) params->wdata + (ne0 + CACHE_LINE_SIZE_F32) * ith;
  6583. for (int64_t ir = ir0; ir < ir1; ++ir) {
  6584. // dst indices
  6585. const int64_t i3 = ir/(ne2*ne1);
  6586. const int64_t i2 = (ir - i3*ne2*ne1)/ne1;
  6587. const int64_t i1 = (ir - i3*ne2*ne1 - i2*ne1);
  6588. const int64_t i02 = i2;
  6589. const int64_t i03 = i3;
  6590. //const int64_t i10 = i1;
  6591. const int64_t i12 = i2;
  6592. const int64_t i13 = i3;
  6593. for (int64_t i01 = 0; i01 < ne01; ++i01) {
  6594. const int64_t i11 = i01;
  6595. float * s0 = (float *) ((char *) src0->data + ( i01*nb01 + i02*nb02 + i03*nb03));
  6596. float * s1 = (float *) ((char *) src1->data + (i1*nb10 + i11*nb11 + i12*nb12 + i13*nb13));
  6597. float * d = (float *) ((char *) dst->data + ( i1*nb1 + i2*nb2 + i3*nb3));
  6598. dequantize_row_q(s0, wdata, ne0);
  6599. ggml_vec_mad_f32(ne0, d, wdata, *s1);
  6600. }
  6601. }
  6602. }
  6603. static void ggml_compute_forward_out_prod(
  6604. const struct ggml_compute_params * params,
  6605. struct ggml_tensor * dst) {
  6606. const struct ggml_tensor * src0 = dst->src[0];
  6607. switch (src0->type) {
  6608. case GGML_TYPE_Q4_0:
  6609. case GGML_TYPE_Q4_1:
  6610. case GGML_TYPE_Q5_0:
  6611. case GGML_TYPE_Q5_1:
  6612. case GGML_TYPE_Q8_0:
  6613. case GGML_TYPE_Q2_K:
  6614. case GGML_TYPE_Q3_K:
  6615. case GGML_TYPE_Q4_K:
  6616. case GGML_TYPE_Q5_K:
  6617. case GGML_TYPE_Q6_K:
  6618. case GGML_TYPE_TQ1_0:
  6619. case GGML_TYPE_TQ2_0:
  6620. case GGML_TYPE_IQ2_XXS:
  6621. case GGML_TYPE_IQ2_XS:
  6622. case GGML_TYPE_IQ3_XXS:
  6623. case GGML_TYPE_IQ1_S:
  6624. case GGML_TYPE_IQ1_M:
  6625. case GGML_TYPE_IQ4_NL:
  6626. case GGML_TYPE_IQ4_XS:
  6627. case GGML_TYPE_IQ3_S:
  6628. case GGML_TYPE_IQ2_S:
  6629. {
  6630. ggml_compute_forward_out_prod_q_f32(params, dst);
  6631. } break;
  6632. case GGML_TYPE_F16:
  6633. {
  6634. GGML_ABORT("fatal error"); // todo
  6635. // ggml_compute_forward_out_prod_f16_f32(params, dst);
  6636. }
  6637. case GGML_TYPE_F32:
  6638. {
  6639. ggml_compute_forward_out_prod_f32(params, dst);
  6640. } break;
  6641. default:
  6642. {
  6643. GGML_ABORT("fatal error");
  6644. }
  6645. }
  6646. }
  6647. // ggml_compute_forward_scale
  6648. static void ggml_compute_forward_scale_f32(
  6649. const struct ggml_compute_params * params,
  6650. struct ggml_tensor * dst) {
  6651. const struct ggml_tensor * src0 = dst->src[0];
  6652. GGML_ASSERT(ggml_is_contiguous(src0));
  6653. GGML_ASSERT(ggml_is_contiguous(dst));
  6654. GGML_ASSERT(ggml_are_same_shape(src0, dst));
  6655. // scale factor
  6656. float v;
  6657. memcpy(&v, dst->op_params, sizeof(float));
  6658. const int ith = params->ith;
  6659. const int nth = params->nth;
  6660. const int nc = src0->ne[0];
  6661. const int nr = ggml_nrows(src0);
  6662. // rows per thread
  6663. const int dr = (nr + nth - 1)/nth;
  6664. // row range for this thread
  6665. const int ir0 = dr*ith;
  6666. const int ir1 = MIN(ir0 + dr, nr);
  6667. const size_t nb01 = src0->nb[1];
  6668. const size_t nb1 = dst->nb[1];
  6669. for (int i1 = ir0; i1 < ir1; i1++) {
  6670. if (dst->data != src0->data) {
  6671. // src0 is same shape as dst => same indices
  6672. memcpy((char *)dst->data + i1*nb1, (char *)src0->data + i1*nb01, nc * sizeof(float));
  6673. }
  6674. ggml_vec_scale_f32(nc, (float *) ((char *) dst->data + i1*nb1), v);
  6675. }
  6676. }
  6677. static void ggml_compute_forward_scale(
  6678. const struct ggml_compute_params * params,
  6679. struct ggml_tensor * dst) {
  6680. const struct ggml_tensor * src0 = dst->src[0];
  6681. switch (src0->type) {
  6682. case GGML_TYPE_F32:
  6683. {
  6684. ggml_compute_forward_scale_f32(params, dst);
  6685. } break;
  6686. default:
  6687. {
  6688. GGML_ABORT("fatal error");
  6689. }
  6690. }
  6691. }
  6692. // ggml_compute_forward_set
  6693. static void ggml_compute_forward_set_f32(
  6694. const struct ggml_compute_params * params,
  6695. struct ggml_tensor * dst) {
  6696. const struct ggml_tensor * src0 = dst->src[0];
  6697. const struct ggml_tensor * src1 = dst->src[1];
  6698. GGML_ASSERT(ggml_are_same_shape(src0, dst));
  6699. GGML_ASSERT(ggml_is_contiguous(dst) && ggml_is_contiguous(src0));
  6700. // view src0 and dst with these strides and data offset inbytes during set
  6701. // nb0 is implicitly element_size because src0 and dst are contiguous
  6702. size_t nb1 = ((int32_t *) dst->op_params)[0];
  6703. size_t nb2 = ((int32_t *) dst->op_params)[1];
  6704. size_t nb3 = ((int32_t *) dst->op_params)[2];
  6705. size_t offset = ((int32_t *) dst->op_params)[3];
  6706. bool inplace = (bool) ((int32_t *) dst->op_params)[4];
  6707. if (!inplace) {
  6708. if (params->ith == 0) {
  6709. // memcpy needs to be synchronized across threads to avoid race conditions.
  6710. // => do it in INIT phase
  6711. memcpy(
  6712. ((char *) dst->data),
  6713. ((char *) src0->data),
  6714. ggml_nbytes(dst));
  6715. }
  6716. ggml_barrier(params->threadpool);
  6717. }
  6718. const int ith = params->ith;
  6719. const int nth = params->nth;
  6720. const int nr = ggml_nrows(src1);
  6721. const int nc = src1->ne[0];
  6722. GGML_TENSOR_LOCALS(int64_t, ne1, src1, ne)
  6723. GGML_TENSOR_LOCALS(size_t, nb1, src1, nb)
  6724. // src0 and dst as viewed during set
  6725. const size_t nb0 = ggml_element_size(src0);
  6726. const int im0 = (ne10 == 0 ? 0 : ne10-1);
  6727. const int im1 = (ne11 == 0 ? 0 : ne11-1);
  6728. const int im2 = (ne12 == 0 ? 0 : ne12-1);
  6729. const int im3 = (ne13 == 0 ? 0 : ne13-1);
  6730. GGML_ASSERT(offset + im0*nb0 + im1*nb1 + im2*nb2 + im3*nb3 <= ggml_nbytes(dst));
  6731. GGML_ASSERT(nb10 == sizeof(float));
  6732. // rows per thread
  6733. const int dr = (nr + nth - 1)/nth;
  6734. // row range for this thread
  6735. const int ir0 = dr*ith;
  6736. const int ir1 = MIN(ir0 + dr, nr);
  6737. for (int ir = ir0; ir < ir1; ++ir) {
  6738. // src0 and dst are viewed with shape of src1 and offset
  6739. // => same indices
  6740. const int i3 = ir/(ne12*ne11);
  6741. const int i2 = (ir - i3*ne12*ne11)/ne11;
  6742. const int i1 = (ir - i3*ne12*ne11 - i2*ne11);
  6743. ggml_vec_cpy_f32(nc,
  6744. (float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + offset),
  6745. (float *) ((char *) src1->data + i3*nb13 + i2*nb12 + i1*nb11));
  6746. }
  6747. }
  6748. static void ggml_compute_forward_set_i32(
  6749. const struct ggml_compute_params * params,
  6750. struct ggml_tensor * dst) {
  6751. const struct ggml_tensor * src0 = dst->src[0];
  6752. const struct ggml_tensor * src1 = dst->src[1];
  6753. GGML_ASSERT(ggml_are_same_shape(src0, dst));
  6754. GGML_ASSERT(ggml_is_contiguous(dst) && ggml_is_contiguous(src0));
  6755. // view src0 and dst with these strides and data offset inbytes during set
  6756. // nb0 is implicitly element_size because src0 and dst are contiguous
  6757. size_t nb1 = ((int32_t *) dst->op_params)[0];
  6758. size_t nb2 = ((int32_t *) dst->op_params)[1];
  6759. size_t nb3 = ((int32_t *) dst->op_params)[2];
  6760. size_t offset = ((int32_t *) dst->op_params)[3];
  6761. bool inplace = (bool) ((int32_t *) dst->op_params)[4];
  6762. if (!inplace) {
  6763. if (params->ith == 0) {
  6764. // memcpy needs to be synchronized across threads to avoid race conditions.
  6765. // => do it in INIT phase
  6766. memcpy(
  6767. ((char *) dst->data),
  6768. ((char *) src0->data),
  6769. ggml_nbytes(dst));
  6770. }
  6771. ggml_barrier(params->threadpool);
  6772. }
  6773. const int ith = params->ith;
  6774. const int nth = params->nth;
  6775. const int nr = ggml_nrows(src1);
  6776. const int nc = src1->ne[0];
  6777. GGML_TENSOR_LOCALS(int64_t, ne1, src1, ne)
  6778. GGML_TENSOR_LOCALS(size_t, nb1, src1, nb)
  6779. // src0 and dst as viewed during set
  6780. const size_t nb0 = ggml_element_size(src0);
  6781. const int im0 = (ne10 == 0 ? 0 : ne10-1);
  6782. const int im1 = (ne11 == 0 ? 0 : ne11-1);
  6783. const int im2 = (ne12 == 0 ? 0 : ne12-1);
  6784. const int im3 = (ne13 == 0 ? 0 : ne13-1);
  6785. GGML_ASSERT(offset + im0*nb0 + im1*nb1 + im2*nb2 + im3*nb3 <= ggml_nbytes(dst));
  6786. GGML_ASSERT(nb10 == sizeof(int32_t));
  6787. // rows per thread
  6788. const int dr = (nr + nth - 1)/nth;
  6789. // row range for this thread
  6790. const int ir0 = dr*ith;
  6791. const int ir1 = MIN(ir0 + dr, nr);
  6792. for (int ir = ir0; ir < ir1; ++ir) {
  6793. // src0 and dst are viewed with shape of src1 and offset
  6794. // => same indices
  6795. const int i3 = ir/(ne12*ne11);
  6796. const int i2 = (ir - i3*ne12*ne11)/ne11;
  6797. const int i1 = (ir - i3*ne12*ne11 - i2*ne11);
  6798. ggml_vec_cpy_i32(nc,
  6799. (int32_t *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + offset),
  6800. (int32_t *) ((char *) src1->data + i3*nb13 + i2*nb12 + i1*nb11));
  6801. }
  6802. }
  6803. static void ggml_compute_forward_set(
  6804. const struct ggml_compute_params * params,
  6805. struct ggml_tensor * dst) {
  6806. const struct ggml_tensor * src0 = dst->src[0];
  6807. switch (src0->type) {
  6808. case GGML_TYPE_F32:
  6809. {
  6810. ggml_compute_forward_set_f32(params, dst);
  6811. } break;
  6812. case GGML_TYPE_I32:
  6813. {
  6814. ggml_compute_forward_set_i32(params, dst);
  6815. } break;
  6816. case GGML_TYPE_F16:
  6817. case GGML_TYPE_BF16:
  6818. case GGML_TYPE_Q4_0:
  6819. case GGML_TYPE_Q4_1:
  6820. case GGML_TYPE_Q5_0:
  6821. case GGML_TYPE_Q5_1:
  6822. case GGML_TYPE_Q8_0:
  6823. case GGML_TYPE_Q8_1:
  6824. case GGML_TYPE_Q2_K:
  6825. case GGML_TYPE_Q3_K:
  6826. case GGML_TYPE_Q4_K:
  6827. case GGML_TYPE_Q5_K:
  6828. case GGML_TYPE_Q6_K:
  6829. case GGML_TYPE_TQ1_0:
  6830. case GGML_TYPE_TQ2_0:
  6831. case GGML_TYPE_IQ2_XXS:
  6832. case GGML_TYPE_IQ2_XS:
  6833. case GGML_TYPE_IQ3_XXS:
  6834. case GGML_TYPE_IQ1_S:
  6835. case GGML_TYPE_IQ1_M:
  6836. case GGML_TYPE_IQ4_NL:
  6837. case GGML_TYPE_IQ4_XS:
  6838. case GGML_TYPE_IQ3_S:
  6839. case GGML_TYPE_IQ2_S:
  6840. default:
  6841. {
  6842. GGML_ABORT("fatal error");
  6843. }
  6844. }
  6845. }
  6846. // ggml_compute_forward_cpy
  6847. static void ggml_compute_forward_cpy(
  6848. const struct ggml_compute_params * params,
  6849. struct ggml_tensor * dst) {
  6850. ggml_compute_forward_dup(params, dst);
  6851. }
  6852. // ggml_compute_forward_cont
  6853. static void ggml_compute_forward_cont(
  6854. const struct ggml_compute_params * params,
  6855. struct ggml_tensor * dst) {
  6856. ggml_compute_forward_dup(params, dst);
  6857. }
  6858. // ggml_compute_forward_reshape
  6859. static void ggml_compute_forward_reshape(
  6860. const struct ggml_compute_params * params,
  6861. struct ggml_tensor * dst) {
  6862. // NOP
  6863. UNUSED(params);
  6864. UNUSED(dst);
  6865. }
  6866. // ggml_compute_forward_view
  6867. static void ggml_compute_forward_view(
  6868. const struct ggml_compute_params * params,
  6869. const struct ggml_tensor * dst) {
  6870. // NOP
  6871. UNUSED(params);
  6872. UNUSED(dst);
  6873. }
  6874. // ggml_compute_forward_permute
  6875. static void ggml_compute_forward_permute(
  6876. const struct ggml_compute_params * params,
  6877. const struct ggml_tensor * dst) {
  6878. // NOP
  6879. UNUSED(params);
  6880. UNUSED(dst);
  6881. }
  6882. // ggml_compute_forward_transpose
  6883. static void ggml_compute_forward_transpose(
  6884. const struct ggml_compute_params * params,
  6885. const struct ggml_tensor * dst) {
  6886. // NOP
  6887. UNUSED(params);
  6888. UNUSED(dst);
  6889. }
  6890. // ggml_compute_forward_get_rows
  6891. static void ggml_compute_forward_get_rows_q(
  6892. const struct ggml_compute_params * params,
  6893. struct ggml_tensor * dst) {
  6894. const struct ggml_tensor * src0 = dst->src[0];
  6895. const struct ggml_tensor * src1 = dst->src[1];
  6896. GGML_TENSOR_BINARY_OP_LOCALS
  6897. const int64_t nc = ne00;
  6898. const int64_t nr = ggml_nelements(src1);
  6899. const enum ggml_type type = src0->type;
  6900. ggml_to_float_t const dequantize_row_q = ggml_get_type_traits(type)->to_float;
  6901. assert(ne0 == nc);
  6902. assert(ne02 == ne11);
  6903. assert(nb00 == ggml_type_size(type));
  6904. assert(ggml_nrows(dst) == nr);
  6905. const int ith = params->ith;
  6906. const int nth = params->nth;
  6907. // rows per thread
  6908. const int dr = (nr + nth - 1)/nth;
  6909. // row range for this thread
  6910. const int ir0 = dr*ith;
  6911. const int ir1 = MIN(ir0 + dr, nr);
  6912. for (int64_t i = ir0; i < ir1; ++i) {
  6913. const int64_t i12 = i/(ne11*ne10);
  6914. const int64_t i11 = (i - i12*ne11*ne10)/ne10;
  6915. const int64_t i10 = (i - i12*ne11*ne10 - i11*ne10);
  6916. const int64_t i01 = *(int32_t *) ((char *) src1->data + i10*nb10 + i11*nb11 + i12*nb12);
  6917. GGML_ASSERT(i01 >= 0 && i01 < ne01);
  6918. dequantize_row_q(
  6919. (const void *) ((char *) src0->data + i01*nb01 + i11*nb02 + i12*nb03),
  6920. (float *) ((char *) dst->data + i10*nb1 + i11*nb2 + i12*nb3), nc);
  6921. }
  6922. }
  6923. static void ggml_compute_forward_get_rows_f16(
  6924. const struct ggml_compute_params * params,
  6925. struct ggml_tensor * dst) {
  6926. const struct ggml_tensor * src0 = dst->src[0];
  6927. const struct ggml_tensor * src1 = dst->src[1];
  6928. GGML_TENSOR_BINARY_OP_LOCALS
  6929. const int64_t nc = ne00;
  6930. const int64_t nr = ggml_nelements(src1);
  6931. assert(ne0 == nc);
  6932. assert(ne02 == ne11);
  6933. assert(nb00 == sizeof(ggml_fp16_t));
  6934. assert(ggml_nrows(dst) == nr);
  6935. const int ith = params->ith;
  6936. const int nth = params->nth;
  6937. // rows per thread
  6938. const int dr = (nr + nth - 1)/nth;
  6939. // row range for this thread
  6940. const int ir0 = dr*ith;
  6941. const int ir1 = MIN(ir0 + dr, nr);
  6942. for (int64_t i = ir0; i < ir1; ++i) {
  6943. const int64_t i12 = i/(ne11*ne10);
  6944. const int64_t i11 = (i - i12*ne11*ne10)/ne10;
  6945. const int64_t i10 = (i - i12*ne11*ne10 - i11*ne10);
  6946. const int64_t i01 = *(int32_t *) ((char *) src1->data + i10*nb10 + i11*nb11 + i12*nb12);
  6947. GGML_ASSERT(i01 >= 0 && i01 < ne01);
  6948. ggml_fp16_to_fp32_row(
  6949. (const void *) ((char *) src0->data + i01*nb01 + i11*nb02 + i12*nb03),
  6950. (float *) ((char *) dst->data + i10*nb1 + i11*nb2 + i12*nb3), nc);
  6951. }
  6952. }
  6953. static void ggml_compute_forward_get_rows_bf16(
  6954. const struct ggml_compute_params * params,
  6955. struct ggml_tensor * dst) {
  6956. const struct ggml_tensor * src0 = dst->src[0];
  6957. const struct ggml_tensor * src1 = dst->src[1];
  6958. GGML_TENSOR_BINARY_OP_LOCALS
  6959. const int64_t nc = ne00;
  6960. const int64_t nr = ggml_nelements(src1);
  6961. assert(ne0 == nc);
  6962. assert(ne02 == ne11);
  6963. assert(nb00 == sizeof(ggml_bf16_t));
  6964. assert(ggml_nrows(dst) == nr);
  6965. const int ith = params->ith;
  6966. const int nth = params->nth;
  6967. // rows per thread
  6968. const int dr = (nr + nth - 1)/nth;
  6969. // row range for this thread
  6970. const int ir0 = dr*ith;
  6971. const int ir1 = MIN(ir0 + dr, nr);
  6972. for (int64_t i = ir0; i < ir1; ++i) {
  6973. const int64_t i12 = i/(ne11*ne10);
  6974. const int64_t i11 = (i - i12*ne11*ne10)/ne10;
  6975. const int64_t i10 = (i - i12*ne11*ne10 - i11*ne10);
  6976. const int64_t i01 = *(int32_t *) ((char *) src1->data + i10*nb10 + i11*nb11 + i12*nb12);
  6977. GGML_ASSERT(i01 >= 0 && i01 < ne01);
  6978. ggml_bf16_to_fp32_row(
  6979. (const void *) ((char *) src0->data + i01*nb01 + i11*nb02 + i12*nb03),
  6980. (float *) ((char *) dst->data + i10*nb1 + i11*nb2 + i12*nb3), nc);
  6981. }
  6982. }
  6983. static void ggml_compute_forward_get_rows_f32(
  6984. const struct ggml_compute_params * params,
  6985. struct ggml_tensor * dst) {
  6986. const struct ggml_tensor * src0 = dst->src[0];
  6987. const struct ggml_tensor * src1 = dst->src[1];
  6988. GGML_TENSOR_BINARY_OP_LOCALS
  6989. const int64_t nc = ne00;
  6990. const int64_t nr = ggml_nelements(src1);
  6991. assert(ne0 == nc);
  6992. assert(ne02 == ne11);
  6993. assert(nb00 == sizeof(float));
  6994. assert(ggml_nrows(dst) == nr);
  6995. const int ith = params->ith;
  6996. const int nth = params->nth;
  6997. // rows per thread
  6998. const int dr = (nr + nth - 1)/nth;
  6999. // row range for this thread
  7000. const int ir0 = dr*ith;
  7001. const int ir1 = MIN(ir0 + dr, nr);
  7002. for (int64_t i = ir0; i < ir1; ++i) {
  7003. const int64_t i12 = i/(ne11*ne10);
  7004. const int64_t i11 = (i - i12*ne11*ne10)/ne10;
  7005. const int64_t i10 = (i - i12*ne11*ne10 - i11*ne10);
  7006. const int64_t i01 = *(int32_t *) ((char *) src1->data + i10*nb10 + i11*nb11 + i12*nb12);
  7007. GGML_ASSERT(i01 >= 0 && i01 < ne01);
  7008. ggml_vec_cpy_f32(nc,
  7009. (float *) ((char *) dst->data + i10*nb1 + i11*nb2 + i12*nb3),
  7010. (float *) ((char *) src0->data + i01*nb01 + i11*nb02 + i12*nb03));
  7011. }
  7012. }
  7013. static void ggml_compute_forward_get_rows(
  7014. const struct ggml_compute_params * params,
  7015. struct ggml_tensor * dst) {
  7016. const struct ggml_tensor * src0 = dst->src[0];
  7017. switch (src0->type) {
  7018. case GGML_TYPE_Q4_0:
  7019. case GGML_TYPE_Q4_1:
  7020. case GGML_TYPE_Q5_0:
  7021. case GGML_TYPE_Q5_1:
  7022. case GGML_TYPE_Q8_0:
  7023. case GGML_TYPE_Q8_1:
  7024. case GGML_TYPE_Q2_K:
  7025. case GGML_TYPE_Q3_K:
  7026. case GGML_TYPE_Q4_K:
  7027. case GGML_TYPE_Q5_K:
  7028. case GGML_TYPE_Q6_K:
  7029. case GGML_TYPE_TQ1_0:
  7030. case GGML_TYPE_TQ2_0:
  7031. case GGML_TYPE_IQ2_XXS:
  7032. case GGML_TYPE_IQ2_XS:
  7033. case GGML_TYPE_IQ3_XXS:
  7034. case GGML_TYPE_IQ1_S:
  7035. case GGML_TYPE_IQ1_M:
  7036. case GGML_TYPE_IQ4_NL:
  7037. case GGML_TYPE_IQ4_XS:
  7038. case GGML_TYPE_IQ3_S:
  7039. case GGML_TYPE_IQ2_S:
  7040. {
  7041. ggml_compute_forward_get_rows_q(params, dst);
  7042. } break;
  7043. case GGML_TYPE_F16:
  7044. {
  7045. ggml_compute_forward_get_rows_f16(params, dst);
  7046. } break;
  7047. case GGML_TYPE_BF16:
  7048. {
  7049. ggml_compute_forward_get_rows_bf16(params, dst);
  7050. } break;
  7051. case GGML_TYPE_F32:
  7052. case GGML_TYPE_I32:
  7053. {
  7054. ggml_compute_forward_get_rows_f32(params, dst);
  7055. } break;
  7056. default:
  7057. {
  7058. GGML_ABORT("fatal error");
  7059. }
  7060. }
  7061. //static bool first = true;
  7062. //printf("ne0 = %d, ne1 = %d, ne2 = %d\n", dst->ne[0], dst->ne[1], dst->ne[2]);
  7063. //if (first) {
  7064. // first = false;
  7065. //} else {
  7066. // for (int k = 0; k < dst->ne[1]; ++k) {
  7067. // for (int j = 0; j < dst->ne[0]/16; ++j) {
  7068. // for (int i = 0; i < 16; ++i) {
  7069. // printf("%8.4f ", ((float *) dst->data)[k*dst->ne[0] + j*16 + i]);
  7070. // }
  7071. // printf("\n");
  7072. // }
  7073. // printf("\n");
  7074. // }
  7075. // printf("\n");
  7076. // exit(0);
  7077. //}
  7078. }
  7079. // ggml_compute_forward_get_rows_back
  7080. static void ggml_compute_forward_get_rows_back_f32_f16(
  7081. const struct ggml_compute_params * params,
  7082. struct ggml_tensor * dst) {
  7083. const struct ggml_tensor * src0 = dst->src[0];
  7084. const struct ggml_tensor * src1 = dst->src[1];
  7085. if (params->ith != 0) {
  7086. return;
  7087. }
  7088. GGML_ASSERT(ggml_is_contiguous(dst));
  7089. // ggml_compute_forward_dup_same_cont(params, opt0, dst);
  7090. memset(dst->data, 0, ggml_nbytes(dst));
  7091. const int nc = src0->ne[0];
  7092. const int nr = ggml_nelements(src1);
  7093. GGML_ASSERT( dst->ne[0] == nc);
  7094. GGML_ASSERT(src0->nb[0] == sizeof(ggml_fp16_t));
  7095. for (int i = 0; i < nr; ++i) {
  7096. const int r = ((int32_t *) src1->data)[i];
  7097. for (int j = 0; j < nc; ++j) {
  7098. ggml_fp16_t v = ((ggml_fp16_t *) ((char *) src0->data + i*src0->nb[1]))[j];
  7099. ((float *) ((char *) dst->data + r*dst->nb[1]))[j] += GGML_FP16_TO_FP32(v);
  7100. }
  7101. }
  7102. }
  7103. static void ggml_compute_forward_get_rows_back_f32(
  7104. const struct ggml_compute_params * params,
  7105. struct ggml_tensor * dst) {
  7106. const struct ggml_tensor * src0 = dst->src[0];
  7107. const struct ggml_tensor * src1 = dst->src[1];
  7108. if (params->ith != 0) {
  7109. return;
  7110. }
  7111. GGML_ASSERT(ggml_is_contiguous(dst));
  7112. // ggml_compute_forward_dup_same_cont(params, opt0, dst);
  7113. memset(dst->data, 0, ggml_nbytes(dst));
  7114. const int nc = src0->ne[0];
  7115. const int nr = ggml_nelements(src1);
  7116. GGML_ASSERT( dst->ne[0] == nc);
  7117. GGML_ASSERT(src0->nb[0] == sizeof(float));
  7118. for (int i = 0; i < nr; ++i) {
  7119. const int r = ((int32_t *) src1->data)[i];
  7120. ggml_vec_add_f32(nc,
  7121. (float *) ((char *) dst->data + r*dst->nb[1]),
  7122. (float *) ((char *) dst->data + r*dst->nb[1]),
  7123. (float *) ((char *) src0->data + i*src0->nb[1]));
  7124. }
  7125. }
  7126. static void ggml_compute_forward_get_rows_back(
  7127. const struct ggml_compute_params * params,
  7128. struct ggml_tensor * dst) {
  7129. const struct ggml_tensor * src0 = dst->src[0];
  7130. switch (src0->type) {
  7131. case GGML_TYPE_F16:
  7132. {
  7133. ggml_compute_forward_get_rows_back_f32_f16(params, dst);
  7134. } break;
  7135. case GGML_TYPE_F32:
  7136. {
  7137. ggml_compute_forward_get_rows_back_f32(params, dst);
  7138. } break;
  7139. default:
  7140. {
  7141. GGML_ABORT("fatal error");
  7142. }
  7143. }
  7144. //static bool first = true;
  7145. //printf("ne0 = %d, ne1 = %d, ne2 = %d\n", dst->ne[0], dst->ne[1], dst->ne[2]);
  7146. //if (first) {
  7147. // first = false;
  7148. //} else {
  7149. // for (int k = 0; k < dst->ne[1]; ++k) {
  7150. // for (int j = 0; j < dst->ne[0]/16; ++j) {
  7151. // for (int i = 0; i < 16; ++i) {
  7152. // printf("%8.4f ", ((float *) dst->data)[k*dst->ne[0] + j*16 + i]);
  7153. // }
  7154. // printf("\n");
  7155. // }
  7156. // printf("\n");
  7157. // }
  7158. // printf("\n");
  7159. // exit(0);
  7160. //}
  7161. }
  7162. // ggml_compute_forward_diag
  7163. static void ggml_compute_forward_diag_f32(
  7164. const struct ggml_compute_params * params,
  7165. struct ggml_tensor * dst) {
  7166. const struct ggml_tensor * src0 = dst->src[0];
  7167. if (params->ith != 0) {
  7168. return;
  7169. }
  7170. // TODO: handle transposed/permuted matrices
  7171. GGML_TENSOR_UNARY_OP_LOCALS
  7172. GGML_ASSERT(ne00 == ne0);
  7173. GGML_ASSERT(ne00 == ne1);
  7174. GGML_ASSERT(ne01 == 1);
  7175. GGML_ASSERT(ne02 == ne2);
  7176. GGML_ASSERT(ne03 == ne3);
  7177. GGML_ASSERT(nb00 == sizeof(float));
  7178. GGML_ASSERT(nb0 == sizeof(float));
  7179. for (int i3 = 0; i3 < ne3; i3++) {
  7180. for (int i2 = 0; i2 < ne2; i2++) {
  7181. for (int i1 = 0; i1 < ne1; i1++) {
  7182. float * d = (float *)((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1);
  7183. float * s = (float *)((char *) src0->data + i3*nb03 + i2*nb02);
  7184. for (int i0 = 0; i0 < i1; i0++) {
  7185. d[i0] = 0;
  7186. }
  7187. d[i1] = s[i1];
  7188. for (int i0 = i1+1; i0 < ne0; i0++) {
  7189. d[i0] = 0;
  7190. }
  7191. }
  7192. }
  7193. }
  7194. }
  7195. static void ggml_compute_forward_diag(
  7196. const struct ggml_compute_params * params,
  7197. struct ggml_tensor * dst) {
  7198. const struct ggml_tensor * src0 = dst->src[0];
  7199. switch (src0->type) {
  7200. case GGML_TYPE_F32:
  7201. {
  7202. ggml_compute_forward_diag_f32(params, dst);
  7203. } break;
  7204. default:
  7205. {
  7206. GGML_ABORT("fatal error");
  7207. }
  7208. }
  7209. }
  7210. // ggml_compute_forward_diag_mask_inf
  7211. static void ggml_compute_forward_diag_mask_f32(
  7212. const struct ggml_compute_params * params,
  7213. struct ggml_tensor * dst,
  7214. const float value) {
  7215. const struct ggml_tensor * src0 = dst->src[0];
  7216. const int ith = params->ith;
  7217. const int nth = params->nth;
  7218. const int n_past = ((int32_t *) dst->op_params)[0];
  7219. const bool inplace = src0->data == dst->data;
  7220. GGML_ASSERT(n_past >= 0);
  7221. if (!inplace) {
  7222. if (ith == 0) {
  7223. // memcpy needs to be synchronized across threads to avoid race conditions.
  7224. // => do it in INIT phase
  7225. GGML_ASSERT(ggml_nelements(dst) == ggml_nelements(src0));
  7226. GGML_ASSERT(ggml_is_contiguous(dst) && ggml_is_contiguous(src0));
  7227. memcpy(
  7228. ((char *) dst->data),
  7229. ((char *) src0->data),
  7230. ggml_nbytes(dst));
  7231. }
  7232. ggml_barrier(params->threadpool);
  7233. }
  7234. // TODO: handle transposed/permuted matrices
  7235. const int n = ggml_nrows(src0);
  7236. const int nc = src0->ne[0];
  7237. const int nr = src0->ne[1];
  7238. const int nz = n/nr;
  7239. GGML_ASSERT( dst->nb[0] == sizeof(float));
  7240. GGML_ASSERT(src0->nb[0] == sizeof(float));
  7241. for (int k = 0; k < nz; k++) {
  7242. for (int j = ith; j < nr; j += nth) {
  7243. for (int i = n_past; i < nc; i++) {
  7244. if (i > n_past + j) {
  7245. *(float *)((char *) dst->data + k*dst->nb[2] + j*dst->nb[1] + i*dst->nb[0]) = value;
  7246. }
  7247. }
  7248. }
  7249. }
  7250. }
  7251. static void ggml_compute_forward_diag_mask_inf(
  7252. const struct ggml_compute_params * params,
  7253. struct ggml_tensor * dst) {
  7254. const struct ggml_tensor * src0 = dst->src[0];
  7255. switch (src0->type) {
  7256. case GGML_TYPE_F32:
  7257. {
  7258. ggml_compute_forward_diag_mask_f32(params, dst, -INFINITY);
  7259. } break;
  7260. default:
  7261. {
  7262. GGML_ABORT("fatal error");
  7263. }
  7264. }
  7265. }
  7266. static void ggml_compute_forward_diag_mask_zero(
  7267. const struct ggml_compute_params * params,
  7268. struct ggml_tensor * dst) {
  7269. const struct ggml_tensor * src0 = dst->src[0];
  7270. switch (src0->type) {
  7271. case GGML_TYPE_F32:
  7272. {
  7273. ggml_compute_forward_diag_mask_f32(params, dst, 0);
  7274. } break;
  7275. default:
  7276. {
  7277. GGML_ABORT("fatal error");
  7278. }
  7279. }
  7280. }
  7281. // ggml_compute_forward_soft_max
  7282. static void ggml_compute_forward_soft_max_f32(
  7283. const struct ggml_compute_params * params,
  7284. struct ggml_tensor * dst) {
  7285. const struct ggml_tensor * src0 = dst->src[0];
  7286. const struct ggml_tensor * src1 = dst->src[1];
  7287. assert(ggml_is_contiguous(dst));
  7288. assert(ggml_are_same_shape(src0, dst));
  7289. float scale = 1.0f;
  7290. float max_bias = 0.0f;
  7291. memcpy(&scale, (float *) dst->op_params + 0, sizeof(float));
  7292. memcpy(&max_bias, (float *) dst->op_params + 1, sizeof(float));
  7293. // TODO: handle transposed/permuted matrices
  7294. const int ith = params->ith;
  7295. const int nth = params->nth;
  7296. GGML_TENSOR_UNARY_OP_LOCALS
  7297. //const int64_t ne11 = src1 ? src1->ne[1] : 1;
  7298. // TODO: is this supposed to be ceil instead of floor?
  7299. // https://huggingface.co/mosaicml/mpt-7b/blob/main/attention.py#L370
  7300. const uint32_t n_head = ne02;
  7301. const uint32_t n_head_log2 = 1u << (uint32_t) floor(log2(n_head));
  7302. const float m0 = powf(2.0f, -(max_bias ) / n_head_log2);
  7303. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_head_log2);
  7304. const int nc = src0->ne[0];
  7305. const int nr = ggml_nrows(src0);
  7306. // rows per thread
  7307. const int dr = (nr + nth - 1)/nth;
  7308. // row range for this thread
  7309. const int ir0 = dr*ith;
  7310. const int ir1 = MIN(ir0 + dr, nr);
  7311. float * wp = (float *) params->wdata + (nc + CACHE_LINE_SIZE_F32) * ith;
  7312. const bool use_f16 = (src1 && src1->type == GGML_TYPE_F16);
  7313. for (int i1 = ir0; i1 < ir1; i1++) {
  7314. // ALiBi
  7315. const uint32_t h = (i1/ne01)%ne02; // head
  7316. 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;
  7317. float * sp = (float *)((char *) src0->data + i1*src0->nb[1]);
  7318. float * dp = (float *)((char *) dst->data + i1*dst->nb[1]);
  7319. // broadcast the mask across rows
  7320. ggml_fp16_t * mp_f16 = src1 ? (ggml_fp16_t *)((char *) src1->data) + (i1%ne01)*ne00 : NULL;
  7321. float * mp_f32 = src1 ? (float *)((char *) src1->data) + (i1%ne01)*ne00 : NULL;
  7322. ggml_vec_cpy_f32 (nc, wp, sp);
  7323. ggml_vec_scale_f32(nc, wp, scale);
  7324. if (mp_f32) {
  7325. if (use_f16) {
  7326. for (int i = 0; i < nc; ++i) {
  7327. wp[i] += slope*GGML_FP16_TO_FP32(mp_f16[i]);
  7328. }
  7329. } else {
  7330. for (int i = 0; i < nc; ++i) {
  7331. wp[i] += slope*mp_f32[i];
  7332. }
  7333. }
  7334. }
  7335. #ifndef NDEBUG
  7336. for (int i = 0; i < nc; ++i) {
  7337. //printf("p[%d] = %f\n", i, p[i]);
  7338. assert(!isnan(wp[i]));
  7339. }
  7340. #endif
  7341. float max = -INFINITY;
  7342. ggml_vec_max_f32(nc, &max, wp);
  7343. ggml_float sum = ggml_vec_soft_max_f32(nc, dp, wp, max);
  7344. assert(sum > 0.0);
  7345. sum = 1.0/sum;
  7346. ggml_vec_scale_f32(nc, dp, sum);
  7347. #ifndef NDEBUG
  7348. for (int i = 0; i < nc; ++i) {
  7349. assert(!isnan(dp[i]));
  7350. assert(!isinf(dp[i]));
  7351. }
  7352. #endif
  7353. }
  7354. }
  7355. static void ggml_compute_forward_soft_max(
  7356. const struct ggml_compute_params * params,
  7357. struct ggml_tensor * dst) {
  7358. const struct ggml_tensor * src0 = dst->src[0];
  7359. switch (src0->type) {
  7360. case GGML_TYPE_F32:
  7361. {
  7362. ggml_compute_forward_soft_max_f32(params, dst);
  7363. } break;
  7364. default:
  7365. {
  7366. GGML_ABORT("fatal error");
  7367. }
  7368. }
  7369. }
  7370. // ggml_compute_forward_soft_max_back
  7371. static void ggml_compute_forward_soft_max_back_f32(
  7372. const struct ggml_compute_params * params,
  7373. struct ggml_tensor * dst) {
  7374. const struct ggml_tensor * src0 = dst->src[0];
  7375. const struct ggml_tensor * src1 = dst->src[1];
  7376. GGML_ASSERT(ggml_is_contiguous(src0));
  7377. GGML_ASSERT(ggml_is_contiguous(src1));
  7378. GGML_ASSERT(ggml_is_contiguous(dst));
  7379. GGML_ASSERT(ggml_are_same_shape(src0, dst));
  7380. GGML_ASSERT(ggml_are_same_shape(src1, dst));
  7381. // TODO: handle transposed/permuted matrices
  7382. const int ith = params->ith;
  7383. const int nth = params->nth;
  7384. const int nc = src0->ne[0];
  7385. const int nr = ggml_nrows(src0);
  7386. // rows per thread
  7387. const int dr = (nr + nth - 1)/nth;
  7388. // row range for this thread
  7389. const int ir0 = dr*ith;
  7390. const int ir1 = MIN(ir0 + dr, nr);
  7391. for (int i1 = ir0; i1 < ir1; i1++) {
  7392. float *dy = (float *)((char *) src0->data + i1*src0->nb[1]);
  7393. float *y = (float *)((char *) src1->data + i1*src1->nb[1]);
  7394. float *dx = (float *)((char *) dst->data + i1*dst->nb[1]);
  7395. #ifndef NDEBUG
  7396. for (int i = 0; i < nc; ++i) {
  7397. //printf("p[%d] = %f\n", i, p[i]);
  7398. assert(!isnan(dy[i]));
  7399. assert(!isnan(y[i]));
  7400. }
  7401. #endif
  7402. // Jii = yi - yi*yi
  7403. // Jij = -yi*yj
  7404. // J = diag(y)-y.T*y
  7405. // dx = J * dy
  7406. // dxk = sum_i(Jki * dyi)
  7407. // dxk = sum_i(-yk*yi * dyi) - (-yk*yk)*dyk + (yk - yk*yk)*dyk
  7408. // dxk = sum_i(-yk*yi * dyi) + yk*yk*dyk + yk*dyk - yk*yk*dyk
  7409. // dxk = sum_i(-yk*yi * dyi) + yk*dyk
  7410. // dxk = -yk * sum_i(yi * dyi) + yk*dyk
  7411. // dxk = -yk * dot(y, dy) + yk*dyk
  7412. // dxk = yk * (- dot(y, dy) + dyk)
  7413. // dxk = yk * (dyk - dot(y, dy))
  7414. //
  7415. // post-order:
  7416. // dot_y_dy := dot(y, dy)
  7417. // dx := dy
  7418. // dx := dx - dot_y_dy
  7419. // dx := dx * y
  7420. // linear runtime, no additional memory
  7421. float dot_y_dy = 0;
  7422. ggml_vec_dot_f32 (nc, &dot_y_dy, 0, y, 0, dy, 0, 1);
  7423. ggml_vec_cpy_f32 (nc, dx, dy);
  7424. ggml_vec_acc1_f32(nc, dx, -dot_y_dy);
  7425. ggml_vec_mul_f32 (nc, dx, dx, y);
  7426. #ifndef NDEBUG
  7427. for (int i = 0; i < nc; ++i) {
  7428. assert(!isnan(dx[i]));
  7429. assert(!isinf(dx[i]));
  7430. }
  7431. #endif
  7432. }
  7433. }
  7434. static void ggml_compute_forward_soft_max_back(
  7435. const struct ggml_compute_params * params,
  7436. struct ggml_tensor * dst) {
  7437. const struct ggml_tensor * src0 = dst->src[0];
  7438. switch (src0->type) {
  7439. case GGML_TYPE_F32:
  7440. {
  7441. ggml_compute_forward_soft_max_back_f32(params, dst);
  7442. } break;
  7443. default:
  7444. {
  7445. GGML_ABORT("fatal error");
  7446. }
  7447. }
  7448. }
  7449. // ggml_compute_forward_clamp
  7450. static void ggml_compute_forward_clamp_f32(
  7451. const struct ggml_compute_params * params,
  7452. struct ggml_tensor * dst) {
  7453. const struct ggml_tensor * src0 = dst->src[0];
  7454. if (params->ith != 0) {
  7455. return;
  7456. }
  7457. float min;
  7458. float max;
  7459. memcpy(&min, (float *) dst->op_params + 0, sizeof(float));
  7460. memcpy(&max, (float *) dst->op_params + 1, sizeof(float));
  7461. const int ith = params->ith;
  7462. const int nth = params->nth;
  7463. const int n = ggml_nrows(src0);
  7464. const int nc = src0->ne[0];
  7465. const size_t nb00 = src0->nb[0];
  7466. const size_t nb01 = src0->nb[1];
  7467. const size_t nb0 = dst->nb[0];
  7468. const size_t nb1 = dst->nb[1];
  7469. GGML_ASSERT( nb0 == sizeof(float));
  7470. GGML_ASSERT(nb00 == sizeof(float));
  7471. for (int j = ith; j < n; j += nth) {
  7472. float * dst_ptr = (float *) ((char *) dst->data + j*nb1);
  7473. float * src0_ptr = (float *) ((char *) src0->data + j*nb01);
  7474. for (int i = 0; i < nc; i++) {
  7475. dst_ptr[i] = MAX(MIN(src0_ptr[i], max), min);
  7476. }
  7477. }
  7478. }
  7479. static void ggml_compute_forward_clamp(
  7480. const struct ggml_compute_params * params,
  7481. struct ggml_tensor * dst) {
  7482. const struct ggml_tensor * src0 = dst->src[0];
  7483. switch (src0->type) {
  7484. case GGML_TYPE_F32:
  7485. {
  7486. ggml_compute_forward_clamp_f32(params, dst);
  7487. } break;
  7488. case GGML_TYPE_F16:
  7489. case GGML_TYPE_BF16:
  7490. case GGML_TYPE_Q4_0:
  7491. case GGML_TYPE_Q4_1:
  7492. case GGML_TYPE_Q5_0:
  7493. case GGML_TYPE_Q5_1:
  7494. case GGML_TYPE_Q8_0:
  7495. case GGML_TYPE_Q8_1:
  7496. case GGML_TYPE_Q2_K:
  7497. case GGML_TYPE_Q3_K:
  7498. case GGML_TYPE_Q4_K:
  7499. case GGML_TYPE_Q5_K:
  7500. case GGML_TYPE_Q6_K:
  7501. case GGML_TYPE_TQ1_0:
  7502. case GGML_TYPE_TQ2_0:
  7503. case GGML_TYPE_IQ2_XXS:
  7504. case GGML_TYPE_IQ2_XS:
  7505. case GGML_TYPE_IQ3_XXS:
  7506. case GGML_TYPE_IQ1_S:
  7507. case GGML_TYPE_IQ1_M:
  7508. case GGML_TYPE_IQ4_NL:
  7509. case GGML_TYPE_IQ4_XS:
  7510. case GGML_TYPE_IQ3_S:
  7511. case GGML_TYPE_IQ2_S:
  7512. case GGML_TYPE_Q8_K:
  7513. case GGML_TYPE_I8:
  7514. case GGML_TYPE_I16:
  7515. case GGML_TYPE_I32:
  7516. case GGML_TYPE_I64:
  7517. case GGML_TYPE_F64:
  7518. case GGML_TYPE_COUNT:
  7519. {
  7520. GGML_ABORT("fatal error");
  7521. }
  7522. }
  7523. }
  7524. // ggml_compute_forward_rope
  7525. static float rope_yarn_ramp(const float low, const float high, const int i0) {
  7526. const float y = (i0 / 2 - low) / MAX(0.001f, high - low);
  7527. return 1 - MIN(1, MAX(0, y));
  7528. }
  7529. // YaRN algorithm based on LlamaYaRNScaledRotaryEmbedding.py from https://github.com/jquesnelle/yarn
  7530. // MIT licensed. Copyright (c) 2023 Jeffrey Quesnelle and Bowen Peng.
  7531. static void rope_yarn(
  7532. float theta_extrap, float freq_scale, float corr_dims[2], int64_t i0, float ext_factor, float mscale,
  7533. float * cos_theta, float * sin_theta) {
  7534. // Get n-d rotational scaling corrected for extrapolation
  7535. float theta_interp = freq_scale * theta_extrap;
  7536. float theta = theta_interp;
  7537. if (ext_factor != 0.0f) {
  7538. float ramp_mix = rope_yarn_ramp(corr_dims[0], corr_dims[1], i0) * ext_factor;
  7539. theta = theta_interp * (1 - ramp_mix) + theta_extrap * ramp_mix;
  7540. // Get n-d magnitude scaling corrected for interpolation
  7541. mscale *= 1.0f + 0.1f * logf(1.0f / freq_scale);
  7542. }
  7543. *cos_theta = cosf(theta) * mscale;
  7544. *sin_theta = sinf(theta) * mscale;
  7545. }
  7546. static void ggml_rope_cache_init(
  7547. float theta_base, float freq_scale, const float * freq_factors, float corr_dims[2], int64_t ne0, float ext_factor, float mscale,
  7548. float * cache, float sin_sign, float theta_scale) {
  7549. // ref: https://github.com/jquesnelle/yarn/blob/master/scaled_rope/LlamaYaRNScaledRotaryEmbedding.py
  7550. float theta = theta_base;
  7551. for (int64_t i0 = 0; i0 < ne0; i0 += 2) {
  7552. const float ff = freq_factors ? freq_factors[i0/2] : 1.0f;
  7553. rope_yarn(
  7554. theta/ff, freq_scale, corr_dims, i0, ext_factor, mscale, &cache[i0 + 0], &cache[i0 + 1]
  7555. );
  7556. cache[i0 + 1] *= sin_sign;
  7557. theta *= theta_scale;
  7558. }
  7559. }
  7560. static void ggml_mrope_cache_init(
  7561. float theta_base_t, float theta_base_h, float theta_base_w, float theta_base_e, int sections[4], bool indep_sects,
  7562. float freq_scale, const float * freq_factors, float corr_dims[2], int64_t ne0, float ext_factor, float mscale,
  7563. float * cache, float sin_sign, float theta_scale) {
  7564. // ref: https://github.com/jquesnelle/yarn/blob/master/scaled_rope/LlamaYaRNScaledRotaryEmbedding.py
  7565. float theta_t = theta_base_t;
  7566. float theta_h = theta_base_h;
  7567. float theta_w = theta_base_w;
  7568. float theta_e = theta_base_e; // extra position id for vision encoder
  7569. int sect_dims = sections[0] + sections[1] + sections[2] + sections[3];
  7570. int sec_w = sections[1] + sections[0];
  7571. int sec_e = sections[2] + sec_w;
  7572. GGML_ASSERT(sect_dims <= ne0);
  7573. for (int64_t i0 = 0; i0 < ne0; i0 += 2) {
  7574. const float ff = freq_factors ? freq_factors[i0/2] : 1.0f;
  7575. int sector = (i0 / 2) % sect_dims;
  7576. if (indep_sects) {
  7577. // compute theta independently for each dim sections
  7578. // (i.e. reset corresponding theta when `i0` go from one section to another)
  7579. if (sector == 0) {
  7580. theta_t = theta_base_t;
  7581. }
  7582. else if (sector == sections[0]) {
  7583. theta_h = theta_base_h;;
  7584. }
  7585. else if (sector == sec_w) {
  7586. theta_w = theta_base_w;
  7587. }
  7588. else if (sector == sec_e) {
  7589. theta_e = theta_base_e;
  7590. }
  7591. }
  7592. float theta = theta_t;
  7593. if (sector >= sections[0] && sector < sec_w) {
  7594. theta = theta_h;
  7595. }
  7596. else if (sector >= sec_w && sector < sec_w + sections[2]) {
  7597. theta = theta_w;
  7598. }
  7599. else if (sector >= sec_w + sections[2]) {
  7600. theta = theta_e;
  7601. }
  7602. rope_yarn(
  7603. theta/ff, freq_scale, corr_dims, i0, ext_factor, mscale, &cache[i0 + 0], &cache[i0 + 1]
  7604. );
  7605. cache[i0 + 1] *= sin_sign;
  7606. theta_t *= theta_scale;
  7607. theta_w *= theta_scale;
  7608. theta_h *= theta_scale;
  7609. theta_e *= theta_scale;
  7610. }
  7611. }
  7612. static void ggml_compute_forward_rope_f32(
  7613. const struct ggml_compute_params * params,
  7614. struct ggml_tensor * dst,
  7615. const bool forward) {
  7616. const struct ggml_tensor * src0 = dst->src[0];
  7617. const struct ggml_tensor * src1 = dst->src[1];
  7618. const struct ggml_tensor * src2 = dst->src[2];
  7619. float freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow;
  7620. int sections[4];
  7621. //const int n_past = ((int32_t *) dst->op_params)[0];
  7622. const int n_dims = ((int32_t *) dst->op_params)[1];
  7623. const int mode = ((int32_t *) dst->op_params)[2];
  7624. //const int n_ctx = ((int32_t *) dst->op_params)[3];
  7625. const int n_ctx_orig = ((int32_t *) dst->op_params)[4];
  7626. memcpy(&freq_base, (int32_t *) dst->op_params + 5, sizeof(float));
  7627. memcpy(&freq_scale, (int32_t *) dst->op_params + 6, sizeof(float));
  7628. memcpy(&ext_factor, (int32_t *) dst->op_params + 7, sizeof(float));
  7629. memcpy(&attn_factor, (int32_t *) dst->op_params + 8, sizeof(float));
  7630. memcpy(&beta_fast, (int32_t *) dst->op_params + 9, sizeof(float));
  7631. memcpy(&beta_slow, (int32_t *) dst->op_params + 10, sizeof(float));
  7632. memcpy(&sections, (int32_t *) dst->op_params + 11, sizeof(int)*4);
  7633. GGML_TENSOR_UNARY_OP_LOCALS
  7634. //printf("ne0: %d, ne1: %d, ne2: %d, ne3: %d\n", ne0, ne1, ne2, ne3);
  7635. //printf("n_past = %d, ne2 = %d\n", n_past, ne2);
  7636. GGML_ASSERT(nb00 == sizeof(float));
  7637. const int ith = params->ith;
  7638. const int nth = params->nth;
  7639. const int nr = ggml_nrows(dst);
  7640. GGML_ASSERT(n_dims <= ne0);
  7641. GGML_ASSERT(n_dims % 2 == 0);
  7642. // rows per thread
  7643. const int dr = (nr + nth - 1)/nth;
  7644. // row range for this thread
  7645. const int ir0 = dr*ith;
  7646. const int ir1 = MIN(ir0 + dr, nr);
  7647. // row index used to determine which thread to use
  7648. int ir = 0;
  7649. const float theta_scale = powf(freq_base, -2.0f/n_dims);
  7650. float corr_dims[2];
  7651. ggml_rope_yarn_corr_dims(n_dims, n_ctx_orig, freq_base, beta_fast, beta_slow, corr_dims);
  7652. const bool is_neox = mode & GGML_ROPE_TYPE_NEOX;
  7653. const bool is_mrope = mode & GGML_ROPE_TYPE_MROPE; // ggml_rope_multi, multimodal rotary position embedding
  7654. const bool is_vision = mode == GGML_ROPE_TYPE_VISION;
  7655. if (is_mrope) {
  7656. GGML_ASSERT(sections[0] > 0 || sections[1] > 0 || sections[2] > 0);
  7657. }
  7658. if (is_vision) {
  7659. GGML_ASSERT(n_dims == ne0/2);
  7660. }
  7661. const float * freq_factors = NULL;
  7662. if (src2 != NULL) {
  7663. GGML_ASSERT(src2->type == GGML_TYPE_F32);
  7664. GGML_ASSERT(src2->ne[0] >= n_dims / 2);
  7665. freq_factors = (const float *) src2->data;
  7666. }
  7667. // backward process uses inverse rotation by cos and sin.
  7668. // cos and sin build a rotation matrix, where the inverse is the transpose.
  7669. // this essentially just switches the sign of sin.
  7670. const float sin_sign = forward ? 1.0f : -1.0f;
  7671. const int32_t * pos = (const int32_t *) src1->data;
  7672. for (int64_t i3 = 0; i3 < ne3; i3++) { // batch
  7673. for (int64_t i2 = 0; i2 < ne2; i2++) { // seq-len
  7674. float * cache = (float *) params->wdata + (ne0 + CACHE_LINE_SIZE_F32)*ith;
  7675. if (!is_mrope) {
  7676. const int64_t p = pos[i2];
  7677. ggml_rope_cache_init(p, freq_scale, freq_factors, corr_dims, ne0, ext_factor, attn_factor, cache, sin_sign, theta_scale);
  7678. }
  7679. else {
  7680. const int64_t p_t = pos[i2];
  7681. const int64_t p_h = pos[i2 + ne2];
  7682. const int64_t p_w = pos[i2 + ne2 * 2];
  7683. const int64_t p_e = pos[i2 + ne2 * 3];
  7684. ggml_mrope_cache_init(
  7685. p_t, p_h, p_w, p_e, sections, is_vision,
  7686. freq_scale, freq_factors, corr_dims, ne0, ext_factor, attn_factor, cache, sin_sign, theta_scale);
  7687. }
  7688. for (int64_t i1 = 0; i1 < ne1; i1++) { // attn-heads
  7689. if (ir++ < ir0) continue;
  7690. if (ir > ir1) break;
  7691. if (is_neox || is_mrope) {
  7692. if (is_vision){
  7693. for (int64_t i0 = 0; i0 < n_dims; i0 += 2) {
  7694. const int64_t ic = i0/2;
  7695. const float cos_theta = cache[i0 + 0];
  7696. const float sin_theta = cache[i0 + 1];
  7697. const float * const src = (float *)((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01 + ic*nb00);
  7698. float * dst_data = (float *)((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + ic*nb0);
  7699. const float x0 = src[0];
  7700. const float x1 = src[n_dims];
  7701. dst_data[0] = x0*cos_theta - x1*sin_theta;
  7702. dst_data[n_dims] = x0*sin_theta + x1*cos_theta;
  7703. }
  7704. } else {
  7705. for (int64_t i0 = 0; i0 < n_dims; i0 += 2) {
  7706. const int64_t ic = i0/2;
  7707. const float cos_theta = cache[i0 + 0];
  7708. const float sin_theta = cache[i0 + 1];
  7709. const float * const src = (float *)((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01 + ic*nb00);
  7710. float * dst_data = (float *)((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + ic*nb0);
  7711. const float x0 = src[0];
  7712. const float x1 = src[n_dims/2];
  7713. dst_data[0] = x0*cos_theta - x1*sin_theta;
  7714. dst_data[n_dims/2] = x0*sin_theta + x1*cos_theta;
  7715. }
  7716. }
  7717. } else {
  7718. for (int64_t i0 = 0; i0 < n_dims; i0 += 2) {
  7719. const float cos_theta = cache[i0 + 0];
  7720. const float sin_theta = cache[i0 + 1];
  7721. const float * const src = (float *)((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01 + i0*nb00);
  7722. float * dst_data = (float *)((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  7723. const float x0 = src[0];
  7724. const float x1 = src[1];
  7725. dst_data[0] = x0*cos_theta - x1*sin_theta;
  7726. dst_data[1] = x0*sin_theta + x1*cos_theta;
  7727. }
  7728. }
  7729. if (is_vision) {
  7730. for (int64_t i0 = n_dims; i0 < ne0; 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];
  7738. dst_data[0] = x0*cos_theta - x1*sin_theta;
  7739. dst_data[n_dims] = x0*sin_theta + x1*cos_theta;
  7740. }
  7741. } else {
  7742. // fill the remain channels with data from src tensor
  7743. for (int64_t i0 = n_dims; i0 < ne0; i0 += 2) {
  7744. const float * const src = (float *)((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01 + i0*nb00);
  7745. float * dst_data = (float *)((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  7746. dst_data[0] = src[0];
  7747. dst_data[1] = src[1];
  7748. }
  7749. }
  7750. }
  7751. }
  7752. }
  7753. }
  7754. // TODO: deduplicate f16/f32 code
  7755. static void ggml_compute_forward_rope_f16(
  7756. const struct ggml_compute_params * params,
  7757. struct ggml_tensor * dst,
  7758. const bool forward) {
  7759. const struct ggml_tensor * src0 = dst->src[0];
  7760. const struct ggml_tensor * src1 = dst->src[1];
  7761. const struct ggml_tensor * src2 = dst->src[2];
  7762. float freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow;
  7763. int sections[4];
  7764. //const int n_past = ((int32_t *) dst->op_params)[0];
  7765. const int n_dims = ((int32_t *) dst->op_params)[1];
  7766. const int mode = ((int32_t *) dst->op_params)[2];
  7767. //const int n_ctx = ((int32_t *) dst->op_params)[3];
  7768. const int n_ctx_orig = ((int32_t *) dst->op_params)[4];
  7769. memcpy(&freq_base, (int32_t *) dst->op_params + 5, sizeof(float));
  7770. memcpy(&freq_scale, (int32_t *) dst->op_params + 6, sizeof(float));
  7771. memcpy(&ext_factor, (int32_t *) dst->op_params + 7, sizeof(float));
  7772. memcpy(&attn_factor, (int32_t *) dst->op_params + 8, sizeof(float));
  7773. memcpy(&beta_fast, (int32_t *) dst->op_params + 9, sizeof(float));
  7774. memcpy(&beta_slow, (int32_t *) dst->op_params + 10, sizeof(float));
  7775. memcpy(&sections, (int32_t *) dst->op_params + 11, sizeof(int)*4);
  7776. GGML_TENSOR_UNARY_OP_LOCALS
  7777. //printf("ne0: %d, ne1: %d, ne2: %d, ne3: %d\n", ne0, ne1, ne2, ne3);
  7778. //printf("n_past = %d, ne2 = %d\n", n_past, ne2);
  7779. GGML_ASSERT(nb0 == sizeof(ggml_fp16_t));
  7780. const int ith = params->ith;
  7781. const int nth = params->nth;
  7782. const int nr = ggml_nrows(dst);
  7783. GGML_ASSERT(n_dims <= ne0);
  7784. GGML_ASSERT(n_dims % 2 == 0);
  7785. // rows per thread
  7786. const int dr = (nr + nth - 1)/nth;
  7787. // row range for this thread
  7788. const int ir0 = dr*ith;
  7789. const int ir1 = MIN(ir0 + dr, nr);
  7790. // row index used to determine which thread to use
  7791. int ir = 0;
  7792. const float theta_scale = powf(freq_base, -2.0f/n_dims);
  7793. float corr_dims[2];
  7794. ggml_rope_yarn_corr_dims(n_dims, n_ctx_orig, freq_base, beta_fast, beta_slow, corr_dims);
  7795. const bool is_neox = mode & GGML_ROPE_TYPE_NEOX;
  7796. const bool is_mrope = mode & GGML_ROPE_TYPE_MROPE;
  7797. const bool is_vision = mode == GGML_ROPE_TYPE_VISION;
  7798. if (is_mrope) {
  7799. GGML_ASSERT(sections[0] > 0 || sections[1] > 0 || sections[2] > 0);
  7800. }
  7801. if (is_vision) {
  7802. GGML_ASSERT(n_dims == ne0/2);
  7803. }
  7804. const float * freq_factors = NULL;
  7805. if (src2 != NULL) {
  7806. GGML_ASSERT(src2->type == GGML_TYPE_F32);
  7807. GGML_ASSERT(src2->ne[0] >= n_dims / 2);
  7808. freq_factors = (const float *) src2->data;
  7809. }
  7810. // backward process uses inverse rotation by cos and sin.
  7811. // cos and sin build a rotation matrix, where the inverse is the transpose.
  7812. // this essentially just switches the sign of sin.
  7813. const float sin_sign = forward ? 1.0f : -1.0f;
  7814. const int32_t * pos = (const int32_t *) src1->data;
  7815. for (int64_t i3 = 0; i3 < ne3; i3++) {
  7816. for (int64_t i2 = 0; i2 < ne2; i2++) {
  7817. float * cache = (float *) params->wdata + (ne0 + CACHE_LINE_SIZE_F32)*ith;
  7818. if (!is_mrope) {
  7819. const int64_t p = pos[i2];
  7820. ggml_rope_cache_init(p, freq_scale, freq_factors, corr_dims, ne0, ext_factor, attn_factor, cache, sin_sign, theta_scale);
  7821. }
  7822. else {
  7823. const int64_t p_t = pos[i2];
  7824. const int64_t p_h = pos[i2 + ne2];
  7825. const int64_t p_w = pos[i2 + ne2 * 2];
  7826. const int64_t p_e = pos[i2 + ne2 * 3];
  7827. ggml_mrope_cache_init(
  7828. p_t, p_h, p_w, p_e, sections, is_vision,
  7829. freq_scale, freq_factors, corr_dims, ne0, ext_factor, attn_factor, cache, sin_sign, theta_scale);
  7830. }
  7831. for (int64_t i1 = 0; i1 < ne1; i1++) {
  7832. if (ir++ < ir0) continue;
  7833. if (ir > ir1) break;
  7834. if (is_neox || is_mrope) {
  7835. if (is_vision) {
  7836. for (int64_t i0 = 0; i0 < n_dims; i0 += 2) {
  7837. const int64_t ic = i0/2;
  7838. const float cos_theta = cache[i0 + 0];
  7839. const float sin_theta = cache[i0 + 1];
  7840. const ggml_fp16_t * const src = (ggml_fp16_t *)((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01 + ic*nb00);
  7841. ggml_fp16_t * dst_data = (ggml_fp16_t *)((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + ic*nb0);
  7842. const float x0 = GGML_FP16_TO_FP32(src[0]);
  7843. const float x1 = GGML_FP16_TO_FP32(src[n_dims]);
  7844. dst_data[0] = GGML_FP32_TO_FP16(x0*cos_theta - x1*sin_theta);
  7845. dst_data[n_dims] = GGML_FP32_TO_FP16(x0*sin_theta + x1*cos_theta);
  7846. }
  7847. } else {
  7848. for (int64_t i0 = 0; i0 < n_dims; i0 += 2) {
  7849. const int64_t ic = i0/2;
  7850. const float cos_theta = cache[i0 + 0];
  7851. const float sin_theta = cache[i0 + 1];
  7852. const ggml_fp16_t * const src = (ggml_fp16_t *)((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01 + ic*nb00);
  7853. ggml_fp16_t * dst_data = (ggml_fp16_t *)((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + ic*nb0);
  7854. const float x0 = GGML_FP16_TO_FP32(src[0]);
  7855. const float x1 = GGML_FP16_TO_FP32(src[n_dims/2]);
  7856. dst_data[0] = GGML_FP32_TO_FP16(x0*cos_theta - x1*sin_theta);
  7857. dst_data[n_dims/2] = GGML_FP32_TO_FP16(x0*sin_theta + x1*cos_theta);
  7858. }
  7859. }
  7860. } else {
  7861. for (int64_t i0 = 0; i0 < n_dims; i0 += 2) {
  7862. const float cos_theta = cache[i0 + 0];
  7863. const float sin_theta = cache[i0 + 1];
  7864. const ggml_fp16_t * const src = (ggml_fp16_t *)((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01 + i0*nb00);
  7865. ggml_fp16_t * dst_data = (ggml_fp16_t *)((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  7866. const float x0 = GGML_FP16_TO_FP32(src[0]);
  7867. const float x1 = GGML_FP16_TO_FP32(src[1]);
  7868. dst_data[0] = GGML_FP32_TO_FP16(x0*cos_theta - x1*sin_theta);
  7869. dst_data[1] = GGML_FP32_TO_FP16(x0*sin_theta + x1*cos_theta);
  7870. }
  7871. }
  7872. if (is_vision) {
  7873. for (int64_t i0 = n_dims; i0 < ne0; 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]);
  7881. dst_data[0] = GGML_FP32_TO_FP16(x0*cos_theta - x1*sin_theta);
  7882. dst_data[n_dims] = GGML_FP32_TO_FP16(x0*sin_theta + x1*cos_theta);
  7883. }
  7884. } else {
  7885. for (int64_t i0 = n_dims; i0 < ne0; i0 += 2) {
  7886. const ggml_fp16_t * const src = (ggml_fp16_t *)((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01 + i0*nb00);
  7887. ggml_fp16_t * dst_data = (ggml_fp16_t *)((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0);
  7888. dst_data[0] = src[0];
  7889. dst_data[1] = src[1];
  7890. }
  7891. }
  7892. }
  7893. }
  7894. }
  7895. }
  7896. static void ggml_compute_forward_rope(
  7897. const struct ggml_compute_params * params,
  7898. struct ggml_tensor * dst) {
  7899. const struct ggml_tensor * src0 = dst->src[0];
  7900. switch (src0->type) {
  7901. case GGML_TYPE_F16:
  7902. {
  7903. ggml_compute_forward_rope_f16(params, dst, true);
  7904. } break;
  7905. case GGML_TYPE_F32:
  7906. {
  7907. ggml_compute_forward_rope_f32(params, dst, true);
  7908. } break;
  7909. default:
  7910. {
  7911. GGML_ABORT("fatal error");
  7912. }
  7913. }
  7914. }
  7915. // ggml_compute_forward_rope_back
  7916. static void ggml_compute_forward_rope_back(
  7917. const struct ggml_compute_params * params,
  7918. struct ggml_tensor * dst) {
  7919. const struct ggml_tensor * src0 = dst->src[0];
  7920. switch (src0->type) {
  7921. case GGML_TYPE_F16:
  7922. {
  7923. ggml_compute_forward_rope_f16(params, dst, false);
  7924. } break;
  7925. case GGML_TYPE_F32:
  7926. {
  7927. ggml_compute_forward_rope_f32(params, dst, false);
  7928. } break;
  7929. default:
  7930. {
  7931. GGML_ABORT("fatal error");
  7932. }
  7933. }
  7934. }
  7935. // ggml_compute_forward_conv_transpose_1d
  7936. static void ggml_compute_forward_conv_transpose_1d_f16_f32(
  7937. const struct ggml_compute_params * params,
  7938. struct ggml_tensor * dst) {
  7939. const struct ggml_tensor * src0 = dst->src[0];
  7940. const struct ggml_tensor * src1 = dst->src[1];
  7941. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  7942. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  7943. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  7944. GGML_TENSOR_BINARY_OP_LOCALS
  7945. const int ith = params->ith;
  7946. const int nth = params->nth;
  7947. const int nk = ne00*ne01*ne02;
  7948. GGML_ASSERT(nb00 == sizeof(ggml_fp16_t));
  7949. GGML_ASSERT(nb10 == sizeof(float));
  7950. if (ith == 0) {
  7951. memset(params->wdata, 0, params->wsize);
  7952. // permute kernel data (src0) from (K x Cout x Cin) to (Cin x K x Cout)
  7953. {
  7954. ggml_fp16_t * const wdata = (ggml_fp16_t *) params->wdata + 0;
  7955. for (int64_t i02 = 0; i02 < ne02; i02++) {
  7956. for (int64_t i01 = 0; i01 < ne01; i01++) {
  7957. const ggml_fp16_t * const src = (ggml_fp16_t *)((char *) src0->data + i02*nb02 + i01*nb01);
  7958. ggml_fp16_t * dst_data = wdata + i01*ne00*ne02;
  7959. for (int64_t i00 = 0; i00 < ne00; i00++) {
  7960. dst_data[i00*ne02 + i02] = src[i00];
  7961. }
  7962. }
  7963. }
  7964. }
  7965. // permute source data (src1) from (L x Cin) to (Cin x L)
  7966. {
  7967. ggml_fp16_t * const wdata = (ggml_fp16_t *) params->wdata + nk;
  7968. ggml_fp16_t * dst_data = wdata;
  7969. for (int64_t i11 = 0; i11 < ne11; i11++) {
  7970. const float * const src = (float *)((char *) src1->data + i11*nb11);
  7971. for (int64_t i10 = 0; i10 < ne10; i10++) {
  7972. dst_data[i10*ne11 + i11] = GGML_FP32_TO_FP16(src[i10]);
  7973. }
  7974. }
  7975. }
  7976. // need to zero dst since we are accumulating into it
  7977. memset(dst->data, 0, ggml_nbytes(dst));
  7978. }
  7979. ggml_barrier(params->threadpool);
  7980. const int32_t s0 = ((const int32_t*)(dst->op_params))[0];
  7981. // total rows in dst
  7982. const int nr = ne1;
  7983. // rows per thread
  7984. const int dr = (nr + nth - 1)/nth;
  7985. // row range for this thread
  7986. const int ir0 = dr*ith;
  7987. const int ir1 = MIN(ir0 + dr, nr);
  7988. ggml_fp16_t * const wdata = (ggml_fp16_t *) params->wdata + 0;
  7989. ggml_fp16_t * const wdata_src = wdata + nk;
  7990. for (int i1 = ir0; i1 < ir1; i1++) {
  7991. float * dst_data = (float *)((char *) dst->data + i1*nb1);
  7992. ggml_fp16_t * wdata_kernel = wdata + i1*ne02*ne00;
  7993. for (int i10 = 0; i10 < ne10; i10++) {
  7994. const int i1n = i10*ne11;
  7995. for (int i00 = 0; i00 < ne00; i00++) {
  7996. float v = 0;
  7997. ggml_vec_dot_f16(ne02, &v, 0,
  7998. (ggml_fp16_t *) wdata_src + i1n, 0,
  7999. (ggml_fp16_t *) wdata_kernel + i00*ne02, 0, 1);
  8000. dst_data[i10*s0 + i00] += v;
  8001. }
  8002. }
  8003. }
  8004. }
  8005. static void ggml_compute_forward_conv_transpose_1d_f32(
  8006. const struct ggml_compute_params * params,
  8007. struct ggml_tensor * dst) {
  8008. const struct ggml_tensor * src0 = dst->src[0];
  8009. const struct ggml_tensor * src1 = dst->src[1];
  8010. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  8011. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  8012. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  8013. GGML_TENSOR_BINARY_OP_LOCALS
  8014. const int ith = params->ith;
  8015. const int nth = params->nth;
  8016. const int nk = ne00*ne01*ne02;
  8017. GGML_ASSERT(nb00 == sizeof(float));
  8018. GGML_ASSERT(nb10 == sizeof(float));
  8019. if (ith == 0) {
  8020. memset(params->wdata, 0, params->wsize);
  8021. // prepare kernel data (src0) from (K x Cout x Cin) to (Cin x K x Cout)
  8022. {
  8023. float * const wdata = (float *) params->wdata + 0;
  8024. for (int64_t i02 = 0; i02 < ne02; i02++) {
  8025. for (int64_t i01 = 0; i01 < ne01; i01++) {
  8026. const float * const src = (float *)((char *) src0->data + i02*nb02 + i01*nb01);
  8027. float * dst_data = wdata + i01*ne00*ne02;
  8028. for (int64_t i00 = 0; i00 < ne00; i00++) {
  8029. dst_data[i00*ne02 + i02] = src[i00];
  8030. }
  8031. }
  8032. }
  8033. }
  8034. // prepare source data (src1)
  8035. {
  8036. float * const wdata = (float *) params->wdata + nk;
  8037. float * dst_data = wdata;
  8038. for (int64_t i11 = 0; i11 < ne11; i11++) {
  8039. const float * const src = (float *)((char *) src1->data + i11*nb11);
  8040. for (int64_t i10 = 0; i10 < ne10; i10++) {
  8041. dst_data[i10*ne11 + i11] = src[i10];
  8042. }
  8043. }
  8044. }
  8045. // need to zero dst since we are accumulating into it
  8046. memset(dst->data, 0, ggml_nbytes(dst));
  8047. }
  8048. ggml_barrier(params->threadpool);
  8049. const int32_t s0 = ((const int32_t*)(dst->op_params))[0];
  8050. // total rows in dst
  8051. const int nr = ne1;
  8052. // rows per thread
  8053. const int dr = (nr + nth - 1)/nth;
  8054. // row range for this thread
  8055. const int ir0 = dr*ith;
  8056. const int ir1 = MIN(ir0 + dr, nr);
  8057. float * const wdata = (float *) params->wdata + 0;
  8058. float * const wdata_src = wdata + nk;
  8059. for (int i1 = ir0; i1 < ir1; i1++) {
  8060. float * dst_data = (float *)((char *) dst->data + i1*nb1);
  8061. float * wdata_kernel = wdata + i1*ne02*ne00;
  8062. for (int i10 = 0; i10 < ne10; i10++) {
  8063. const int i1n = i10*ne11;
  8064. for (int i00 = 0; i00 < ne00; i00++) {
  8065. float v = 0;
  8066. ggml_vec_dot_f32(ne02, &v, 0,
  8067. wdata_src + i1n, 0,
  8068. wdata_kernel + i00*ne02, 0, 1);
  8069. dst_data[i10*s0 + i00] += v;
  8070. }
  8071. }
  8072. }
  8073. }
  8074. static void ggml_compute_forward_conv_transpose_1d(
  8075. const struct ggml_compute_params * params,
  8076. struct ggml_tensor * dst) {
  8077. const struct ggml_tensor * src0 = dst->src[0];
  8078. switch (src0->type) {
  8079. case GGML_TYPE_F16:
  8080. {
  8081. ggml_compute_forward_conv_transpose_1d_f16_f32(params, dst);
  8082. } break;
  8083. case GGML_TYPE_F32:
  8084. {
  8085. ggml_compute_forward_conv_transpose_1d_f32(params, dst);
  8086. } break;
  8087. default:
  8088. {
  8089. GGML_ABORT("fatal error");
  8090. }
  8091. }
  8092. }
  8093. // ggml_compute_forward_im2col_f32
  8094. // src0: kernel [OC, IC, KH, KW]
  8095. // src1: image [N, IC, IH, IW]
  8096. // dst: result [N, OH, OW, IC*KH*KW]
  8097. static void ggml_compute_forward_im2col_f32(
  8098. const struct ggml_compute_params * params,
  8099. struct ggml_tensor * dst) {
  8100. const struct ggml_tensor * src0 = dst->src[0];
  8101. const struct ggml_tensor * src1 = dst->src[1];
  8102. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  8103. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  8104. GGML_TENSOR_BINARY_OP_LOCALS;
  8105. const int32_t s0 = ((const int32_t *)(dst->op_params))[0];
  8106. const int32_t s1 = ((const int32_t *)(dst->op_params))[1];
  8107. const int32_t p0 = ((const int32_t *)(dst->op_params))[2];
  8108. const int32_t p1 = ((const int32_t *)(dst->op_params))[3];
  8109. const int32_t d0 = ((const int32_t *)(dst->op_params))[4];
  8110. const int32_t d1 = ((const int32_t *)(dst->op_params))[5];
  8111. const bool is_2D = ((const int32_t *)(dst->op_params))[6] == 1;
  8112. const int ith = params->ith;
  8113. const int nth = params->nth;
  8114. const int64_t N = is_2D ? ne13 : ne12;
  8115. const int64_t IC = is_2D ? ne12 : ne11;
  8116. const int64_t IH = is_2D ? ne11 : 1;
  8117. const int64_t IW = ne10;
  8118. const int64_t KH = is_2D ? ne01 : 1;
  8119. const int64_t KW = ne00;
  8120. const int64_t OH = is_2D ? ne2 : 1;
  8121. const int64_t OW = ne1;
  8122. int ofs0 = is_2D ? nb13 : nb12;
  8123. int ofs1 = is_2D ? nb12 : nb11;
  8124. GGML_ASSERT(nb10 == sizeof(float));
  8125. // im2col: [N, IC, IH, IW] => [N, OH, OW, IC*KH*KW]
  8126. {
  8127. float * const wdata = (float *) dst->data;
  8128. for (int64_t in = 0; in < N; in++) {
  8129. for (int64_t ioh = 0; ioh < OH; ioh++) { // 1
  8130. for (int64_t iow = 0; iow < OW; iow++) {
  8131. for (int64_t iic = ith; iic < IC; iic += nth) {
  8132. // micro kernel
  8133. float * dst_data = wdata + (in*OH*OW + ioh*OW + iow)*(IC*KH*KW); // [IC, KH, KW]
  8134. const float * const src_data = (float *)((char *) src1->data + in*ofs0 + iic*ofs1); // [IH, IW]
  8135. for (int64_t ikh = 0; ikh < KH; ikh++) { // 1
  8136. for (int64_t ikw = 0; ikw < KW; ikw++) {
  8137. const int64_t iiw = iow*s0 + ikw*d0 - p0;
  8138. const int64_t iih = ioh*s1 + ikh*d1 - p1;
  8139. if (iih < 0 || iih >= IH || iiw < 0 || iiw >= IW) {
  8140. dst_data[iic*(KH*KW) + ikh*KW + ikw] = 0;
  8141. } else {
  8142. dst_data[iic*(KH*KW) + ikh*KW + ikw] = (src_data[iih*IW + iiw]);
  8143. }
  8144. }
  8145. }
  8146. }
  8147. }
  8148. }
  8149. }
  8150. }
  8151. }
  8152. // ggml_compute_forward_im2col_f16
  8153. // src0: kernel [OC, IC, KH, KW]
  8154. // src1: image [N, IC, IH, IW]
  8155. // dst: result [N, OH, OW, IC*KH*KW]
  8156. static void ggml_compute_forward_im2col_f16(
  8157. const struct ggml_compute_params * params,
  8158. struct ggml_tensor * dst) {
  8159. const struct ggml_tensor * src0 = dst->src[0];
  8160. const struct ggml_tensor * src1 = dst->src[1];
  8161. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  8162. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  8163. GGML_ASSERT( dst->type == GGML_TYPE_F16);
  8164. GGML_TENSOR_BINARY_OP_LOCALS;
  8165. const int32_t s0 = ((const int32_t *)(dst->op_params))[0];
  8166. const int32_t s1 = ((const int32_t *)(dst->op_params))[1];
  8167. const int32_t p0 = ((const int32_t *)(dst->op_params))[2];
  8168. const int32_t p1 = ((const int32_t *)(dst->op_params))[3];
  8169. const int32_t d0 = ((const int32_t *)(dst->op_params))[4];
  8170. const int32_t d1 = ((const int32_t *)(dst->op_params))[5];
  8171. const bool is_2D = ((const int32_t *)(dst->op_params))[6] == 1;
  8172. const int ith = params->ith;
  8173. const int nth = params->nth;
  8174. const int64_t N = is_2D ? ne13 : ne12;
  8175. const int64_t IC = is_2D ? ne12 : ne11;
  8176. const int64_t IH = is_2D ? ne11 : 1;
  8177. const int64_t IW = ne10;
  8178. const int64_t KH = is_2D ? ne01 : 1;
  8179. const int64_t KW = ne00;
  8180. const int64_t OH = is_2D ? ne2 : 1;
  8181. const int64_t OW = ne1;
  8182. int ofs0 = is_2D ? nb13 : nb12;
  8183. int ofs1 = is_2D ? nb12 : nb11;
  8184. GGML_ASSERT(nb00 == sizeof(ggml_fp16_t));
  8185. GGML_ASSERT(nb10 == sizeof(float));
  8186. // im2col: [N, IC, IH, IW] => [N, OH, OW, IC*KH*KW]
  8187. {
  8188. ggml_fp16_t * const wdata = (ggml_fp16_t *) dst->data;
  8189. for (int64_t in = 0; in < N; in++) {
  8190. for (int64_t ioh = 0; ioh < OH; ioh++) { // 1
  8191. for (int64_t iow = 0; iow < OW; iow++) {
  8192. for (int64_t iic = ith; iic < IC; iic += nth) {
  8193. // micro kernel
  8194. ggml_fp16_t * dst_data = wdata + (in*OH*OW + ioh*OW + iow)*(IC*KH*KW); // [IC, KH, KW]
  8195. const float * const src_data = (float *)((char *) src1->data + in*ofs0 + iic*ofs1); // [IH, IW]
  8196. for (int64_t ikh = 0; ikh < KH; ikh++) { // 1
  8197. for (int64_t ikw = 0; ikw < KW; ikw++) {
  8198. const int64_t iiw = iow*s0 + ikw*d0 - p0;
  8199. const int64_t iih = ioh*s1 + ikh*d1 - p1;
  8200. if (iih < 0 || iih >= IH || iiw < 0 || iiw >= IW) {
  8201. dst_data[iic*(KH*KW) + ikh*KW + ikw] = 0;
  8202. } else {
  8203. dst_data[iic*(KH*KW) + ikh*KW + ikw] = GGML_FP32_TO_FP16(src_data[iih*IW + iiw]);
  8204. }
  8205. }
  8206. }
  8207. }
  8208. }
  8209. }
  8210. }
  8211. }
  8212. }
  8213. static void ggml_compute_forward_im2col(
  8214. const struct ggml_compute_params * params,
  8215. struct ggml_tensor * dst) {
  8216. switch (dst->type) {
  8217. case GGML_TYPE_F16:
  8218. {
  8219. ggml_compute_forward_im2col_f16(params, dst);
  8220. } break;
  8221. case GGML_TYPE_F32:
  8222. {
  8223. ggml_compute_forward_im2col_f32(params, dst);
  8224. } break;
  8225. default:
  8226. {
  8227. GGML_ABORT("fatal error");
  8228. }
  8229. }
  8230. }
  8231. // ggml_compute_forward_im2col_back_f32
  8232. static void ggml_compute_forward_im2col_back_f32(
  8233. const struct ggml_compute_params * params,
  8234. struct ggml_tensor * dst) {
  8235. const struct ggml_tensor * src0 = dst->src[0];
  8236. const struct ggml_tensor * src1 = dst->src[1];
  8237. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  8238. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  8239. GGML_TENSOR_BINARY_OP_LOCALS;
  8240. const int32_t s0 = ((const int32_t *)(dst->op_params))[0];
  8241. const int32_t s1 = ((const int32_t *)(dst->op_params))[1];
  8242. const int32_t p0 = ((const int32_t *)(dst->op_params))[2];
  8243. const int32_t p1 = ((const int32_t *)(dst->op_params))[3];
  8244. const int32_t d0 = ((const int32_t *)(dst->op_params))[4];
  8245. const int32_t d1 = ((const int32_t *)(dst->op_params))[5];
  8246. const bool is_2D = ((const int32_t *)(dst->op_params))[6] == 1;
  8247. const int ith = params->ith;
  8248. const int nth = params->nth;
  8249. const int64_t N = is_2D ? ne3 : ne2;
  8250. const int64_t IC = is_2D ? ne2 : ne1;
  8251. const int64_t IH = is_2D ? ne1 : 1;
  8252. const int64_t IW = ne0;
  8253. const int64_t KH = is_2D ? ne01 : 1;
  8254. const int64_t KW = ne00;
  8255. const int64_t OH = is_2D ? ne12 : 1;
  8256. const int64_t OW = ne11;
  8257. int ofs0 = is_2D ? nb3 : nb2;
  8258. int ofs1 = is_2D ? nb2 : nb1;
  8259. GGML_ASSERT(nb0 == sizeof(float));
  8260. // im2col: [N, IC, IH, IW] => [N, OH, OW, IC*KH*KW]
  8261. {
  8262. float * const wdata = (float *) dst->data;
  8263. for (int64_t in = 0; in < N; in++) {
  8264. for (int64_t iic = ith; iic < IC; iic += nth) {
  8265. for (int64_t iih = 0; iih < IH; iih++) {
  8266. for (int64_t iiw = 0; iiw < IW; iiw++) {
  8267. // micro kernel
  8268. float grad = 0.0f;
  8269. for (int64_t ikh = 0; ikh < KH; ikh++) {
  8270. for (int64_t ikw = 0; ikw < KW; ikw++) {
  8271. // For s0 > 1 some values were skipped over in the forward pass.
  8272. // These values have tmpw % s0 != 0 and need to be skipped in the backwards pass as well.
  8273. const int64_t tmpw = (iiw + p0 - ikw*d0);
  8274. if (tmpw % s0 != 0) {
  8275. continue;
  8276. }
  8277. const int64_t iow = tmpw / s0;
  8278. // Equivalent logic as above except for s1.
  8279. int64_t ioh;
  8280. if (is_2D) {
  8281. const int64_t tmph = iih + p1 - ikh*d1;
  8282. if (tmph % s1 != 0) {
  8283. continue;
  8284. }
  8285. ioh = tmph / s1;
  8286. } else {
  8287. ioh = 0;
  8288. }
  8289. if (iow < 0 || iow >= OW || ioh < 0 || ioh >= OH) {
  8290. continue;
  8291. }
  8292. const float * const src_data = (const float *) src1->data
  8293. + (in*OH*OW + ioh*OW + iow)*(IC*KH*KW); // [IC, KH, KW]
  8294. grad += src_data[iic*(KH*KW) + ikh*KW + ikw];
  8295. }
  8296. }
  8297. float * dst_data = (float *)((char *) wdata + (in*ofs0 + iic*ofs1)); // [IH, IW]
  8298. dst_data[iih*IW + iiw] = grad;
  8299. }
  8300. }
  8301. }
  8302. }
  8303. }
  8304. }
  8305. // ggml_compute_forward_conv_transpose_2d
  8306. static void ggml_compute_forward_conv_transpose_2d(
  8307. const struct ggml_compute_params * params,
  8308. struct ggml_tensor * dst) {
  8309. const struct ggml_tensor * src0 = dst->src[0];
  8310. const struct ggml_tensor * src1 = dst->src[1];
  8311. GGML_ASSERT(src0->type == GGML_TYPE_F16);
  8312. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  8313. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  8314. GGML_TENSOR_BINARY_OP_LOCALS
  8315. const int ith = params->ith;
  8316. const int nth = params->nth;
  8317. const int nk = ne00*ne01*ne02*ne03;
  8318. GGML_ASSERT(nb00 == sizeof(ggml_fp16_t));
  8319. GGML_ASSERT(nb10 == sizeof(float));
  8320. if (ith == 0) {
  8321. memset(params->wdata, 0, params->wsize);
  8322. // permute kernel data (src0) from (Kw x Kh x Cout x Cin) to (Cin x Kw x Kh x Cout)
  8323. {
  8324. ggml_fp16_t * const wdata = (ggml_fp16_t *) params->wdata + 0;
  8325. for (int64_t i03 = 0; i03 < ne03; i03++) {
  8326. for (int64_t i02 = 0; i02 < ne02; i02++) {
  8327. const ggml_fp16_t * const src = (ggml_fp16_t *)((char *) src0->data + i03*nb03 + i02*nb02);
  8328. ggml_fp16_t * dst_data = wdata + i02*ne01*ne00*ne03;
  8329. for (int64_t i01 = 0; i01 < ne01; i01++) {
  8330. for (int64_t i00 = 0; i00 < ne00; i00++) {
  8331. dst_data[i01*ne00*ne03 + i00*ne03 + i03] = src[i01 * ne00 + i00];
  8332. }
  8333. }
  8334. }
  8335. }
  8336. }
  8337. // permute source data (src1) from (Sw x Sh x Cin) to (Cin x Sw x Sh)
  8338. {
  8339. ggml_fp16_t * const wdata = (ggml_fp16_t *) params->wdata + nk;
  8340. for (int i12 = 0; i12 < ne12; i12++) {
  8341. for (int i11 = 0; i11 < ne11; i11++) {
  8342. const float * const src = (float *)((char *) src1->data + i12*nb12 + i11*nb11);
  8343. ggml_fp16_t * dst_data = wdata + i11*ne10*ne12;
  8344. for (int i10 = 0; i10 < ne10; i10++) {
  8345. dst_data[i10*ne12 + i12] = GGML_FP32_TO_FP16(src[i10]);
  8346. }
  8347. }
  8348. }
  8349. }
  8350. memset(dst->data, 0, ggml_nbytes(dst));
  8351. }
  8352. ggml_barrier(params->threadpool);
  8353. const int32_t stride = ggml_get_op_params_i32(dst, 0);
  8354. // total patches in dst
  8355. const int np = ne2;
  8356. // patches per thread
  8357. const int dp = (np + nth - 1)/nth;
  8358. // patch range for this thread
  8359. const int ip0 = dp*ith;
  8360. const int ip1 = MIN(ip0 + dp, np);
  8361. ggml_fp16_t * const wdata = (ggml_fp16_t *) params->wdata + 0;
  8362. ggml_fp16_t * const wdata_src = wdata + nk;
  8363. for (int i2 = ip0; i2 < ip1; i2++) { // Cout
  8364. float * dst_data = (float *)((char *) dst->data + i2*nb2);
  8365. ggml_fp16_t * wdata_kernel = wdata + i2*ne01*ne00*ne03;
  8366. for (int i11 = 0; i11 < ne11; i11++) {
  8367. for (int i10 = 0; i10 < ne10; i10++) {
  8368. const int i1n = i11*ne10*ne12 + i10*ne12;
  8369. for (int i01 = 0; i01 < ne01; i01++) {
  8370. for (int i00 = 0; i00 < ne00; i00++) {
  8371. float v = 0;
  8372. ggml_vec_dot_f16(ne03, &v, 0,
  8373. wdata_src + i1n, 0,
  8374. wdata_kernel + i01*ne00*ne03 + i00*ne03, 0, 1);
  8375. dst_data[(i11*stride + i01)*ne0 + i10*stride + i00] += v;
  8376. }
  8377. }
  8378. }
  8379. }
  8380. }
  8381. }
  8382. // ggml_compute_forward_pool_1d_sk_p0
  8383. static void ggml_compute_forward_pool_1d_sk_p0(
  8384. const struct ggml_compute_params * params,
  8385. const enum ggml_op_pool op,
  8386. const int k,
  8387. struct ggml_tensor * dst) {
  8388. const struct ggml_tensor * src = dst->src[0];
  8389. assert(src->type == GGML_TYPE_F32 || src->type == GGML_TYPE_F16);
  8390. if (params->ith != 0) {
  8391. return;
  8392. }
  8393. const char * cdata = (const char *)src->data;
  8394. const char * const data_end = cdata + ggml_nbytes(src);
  8395. float * drow = (float *)dst->data;
  8396. const int64_t rs = dst->ne[0];
  8397. while (cdata < data_end) {
  8398. const void * srow = (const void *)cdata;
  8399. int j = 0;
  8400. for (int64_t i = 0; i < rs; ++i) {
  8401. switch (op) {
  8402. case GGML_OP_POOL_AVG: drow[i] = 0; break;
  8403. case GGML_OP_POOL_MAX: drow[i] = -FLT_MAX; break;
  8404. case GGML_OP_POOL_COUNT: GGML_ABORT("fatal error");
  8405. }
  8406. for (int ki = 0; ki < k; ++ki) {
  8407. const float srow_j = (src->type == GGML_TYPE_F32) ? ((const float*)srow)[j] : GGML_FP16_TO_FP32(((const ggml_fp16_t*)srow)[j]);
  8408. switch (op) {
  8409. case GGML_OP_POOL_AVG: drow[i] += srow_j; break;
  8410. case GGML_OP_POOL_MAX: if (srow_j > drow[i]) drow[i] = srow_j; break;
  8411. case GGML_OP_POOL_COUNT: GGML_ABORT("fatal error");
  8412. }
  8413. ++j;
  8414. }
  8415. switch (op) {
  8416. case GGML_OP_POOL_AVG: drow[i] /= k; break;
  8417. case GGML_OP_POOL_MAX: break;
  8418. case GGML_OP_POOL_COUNT: GGML_ABORT("fatal error");
  8419. }
  8420. }
  8421. cdata += src->nb[1];
  8422. drow += rs;
  8423. }
  8424. }
  8425. // ggml_compute_forward_pool_1d
  8426. static void ggml_compute_forward_pool_1d(
  8427. const struct ggml_compute_params * params,
  8428. struct ggml_tensor * dst) {
  8429. const int32_t * opts = (const int32_t *)dst->op_params;
  8430. enum ggml_op_pool op = opts[0];
  8431. const int k0 = opts[1];
  8432. const int s0 = opts[2];
  8433. const int p0 = opts[3];
  8434. GGML_ASSERT(p0 == 0); // padding not supported
  8435. GGML_ASSERT(k0 == s0); // only s = k supported
  8436. ggml_compute_forward_pool_1d_sk_p0(params, op, k0, dst);
  8437. }
  8438. // ggml_compute_forward_pool_2d
  8439. static void ggml_compute_forward_pool_2d(
  8440. const struct ggml_compute_params * params,
  8441. struct ggml_tensor * dst) {
  8442. const struct ggml_tensor * src = dst->src[0];
  8443. assert(src->type == GGML_TYPE_F32 || src->type == GGML_TYPE_F16);
  8444. if (params->ith != 0) {
  8445. return;
  8446. }
  8447. const int32_t * opts = (const int32_t *)dst->op_params;
  8448. enum ggml_op_pool op = opts[0];
  8449. const int k0 = opts[1];
  8450. const int k1 = opts[2];
  8451. const int s0 = opts[3];
  8452. const int s1 = opts[4];
  8453. const int p0 = opts[5];
  8454. const int p1 = opts[6];
  8455. const char * cdata = (const char*)src->data;
  8456. const char * const data_end = cdata + ggml_nbytes(src);
  8457. const int64_t px = dst->ne[0];
  8458. const int64_t py = dst->ne[1];
  8459. const int64_t pa = px * py;
  8460. float * dplane = (float *)dst->data;
  8461. const int ka = k0 * k1;
  8462. const int offset0 = -p0;
  8463. const int offset1 = -p1;
  8464. while (cdata < data_end) {
  8465. for (int oy = 0; oy < py; ++oy) {
  8466. float * const drow = dplane + oy * px;
  8467. for (int ox = 0; ox < px; ++ox) {
  8468. float * const out = drow + ox;
  8469. switch (op) {
  8470. case GGML_OP_POOL_AVG: *out = 0; break;
  8471. case GGML_OP_POOL_MAX: *out = -FLT_MAX; break;
  8472. case GGML_OP_POOL_COUNT: GGML_ABORT("fatal error");
  8473. }
  8474. const int ix = offset0 + ox * s0;
  8475. const int iy = offset1 + oy * s1;
  8476. for (int ky = 0; ky < k1; ++ky) {
  8477. if (iy + ky < 0 || iy + ky >= src->ne[1]) continue;
  8478. const void * srow = (const void *)(cdata + src->nb[1] * (iy + ky));
  8479. for (int kx = 0; kx < k0; ++kx) {
  8480. int j = ix + kx;
  8481. if (j < 0 || j >= src->ne[0]) continue;
  8482. const float srow_j = (src->type == GGML_TYPE_F32) ? ((const float*)srow)[j] : GGML_FP16_TO_FP32(((const ggml_fp16_t*)srow)[j]);
  8483. switch (op) {
  8484. case GGML_OP_POOL_AVG: *out += srow_j; break;
  8485. case GGML_OP_POOL_MAX: if (srow_j > *out) *out = srow_j; break;
  8486. case GGML_OP_POOL_COUNT: GGML_ABORT("fatal error");
  8487. }
  8488. }
  8489. }
  8490. switch (op) {
  8491. case GGML_OP_POOL_AVG: *out /= ka; break;
  8492. case GGML_OP_POOL_MAX: break;
  8493. case GGML_OP_POOL_COUNT: GGML_ABORT("fatal error");
  8494. }
  8495. }
  8496. }
  8497. cdata += src->nb[2];
  8498. dplane += pa;
  8499. }
  8500. }
  8501. // ggml_compute_forward_pool_2d_back
  8502. static void ggml_compute_forward_pool_2d_back(
  8503. const struct ggml_compute_params * params,
  8504. struct ggml_tensor * dst) {
  8505. const struct ggml_tensor * src = dst->src[0];
  8506. const struct ggml_tensor * dstf = dst->src[1]; // forward tensor of dst
  8507. assert(dst->type == GGML_TYPE_F32 || dst->type == GGML_TYPE_F16);
  8508. if (params->ith != 0) {
  8509. return;
  8510. }
  8511. const int32_t * opts = (const int32_t *)dst->op_params;
  8512. enum ggml_op_pool op = opts[0];
  8513. const int k0 = opts[1];
  8514. const int k1 = opts[2];
  8515. const int s0 = opts[3];
  8516. const int s1 = opts[4];
  8517. const int p0 = opts[5];
  8518. const int p1 = opts[6];
  8519. char * cdata = (char *) dst->data;
  8520. const char * cdataf = (const char *) dstf->data;
  8521. const char * const data_end = cdata + ggml_nbytes(dst);
  8522. GGML_ASSERT(params->ith == 0);
  8523. memset(cdata, 0, ggml_nbytes(dst));
  8524. const int64_t px = src->ne[0];
  8525. const int64_t py = src->ne[1];
  8526. const int64_t pa = px * py;
  8527. const float * splane = (const float *) src->data;
  8528. const int ka = k0 * k1;
  8529. const int offset0 = -p0;
  8530. const int offset1 = -p1;
  8531. while (cdata < data_end) {
  8532. for (int oy = 0; oy < py; ++oy) {
  8533. const float * const srow = splane + oy * px;
  8534. for (int ox = 0; ox < px; ++ox) {
  8535. const float grad0 = srow[ox];
  8536. const int ix = offset0 + ox * s0;
  8537. const int iy = offset1 + oy * s1;
  8538. if (op == GGML_OP_POOL_MAX) {
  8539. float maxval = -FLT_MAX;
  8540. int kxmax = -1;
  8541. int kymax = -1;
  8542. for (int ky = 0; ky < k1; ++ky) {
  8543. if (iy + ky < 0 || iy + ky >= dst->ne[1]) {
  8544. continue;
  8545. }
  8546. const void * drowf = (const void *)(cdataf + dst->nb[1] * (iy + ky));
  8547. for (int kx = 0; kx < k0; ++kx) {
  8548. int j = ix + kx;
  8549. if (j < 0 || j >= dst->ne[0]) {
  8550. continue;
  8551. }
  8552. const float val = dst->type == GGML_TYPE_F32 ?
  8553. ((const float *) drowf)[j] : GGML_FP16_TO_FP32(((const ggml_fp16_t *) drowf)[j]);
  8554. if (val <= maxval) {
  8555. continue;
  8556. }
  8557. maxval = val;
  8558. kxmax = kx;
  8559. kymax = ky;
  8560. }
  8561. }
  8562. if (kxmax == -1 || kymax == -1) {
  8563. continue;
  8564. }
  8565. void * drow = (void *)(cdata + dst->nb[1] * (iy + kymax));
  8566. const int j = ix + kxmax;
  8567. if (dst->type == GGML_TYPE_F32) {
  8568. ((float *) drow)[j] += grad0;
  8569. } else {
  8570. ((ggml_fp16_t *) drow)[j] = GGML_FP32_TO_FP16(grad0 + GGML_FP16_TO_FP32(((const ggml_fp16_t *) drow)[j]));
  8571. }
  8572. } else if (op == GGML_OP_POOL_AVG) {
  8573. const float grad = grad0 / ka;
  8574. for (int ky = 0; ky < k1; ++ky) {
  8575. if (iy + ky < 0 || iy + ky >= dst->ne[1]) {
  8576. continue;
  8577. }
  8578. void * drow = (void *)(cdata + dst->nb[1] * (iy + ky));
  8579. for (int kx = 0; kx < k0; ++kx) {
  8580. int j = ix + kx;
  8581. if (j < 0 || j >= dst->ne[0]) {
  8582. continue;
  8583. }
  8584. if (dst->type == GGML_TYPE_F32) {
  8585. ((float *) drow)[j] += grad;
  8586. } else {
  8587. ((ggml_fp16_t *) drow)[j] += GGML_FP32_TO_FP16(grad);
  8588. }
  8589. }
  8590. }
  8591. } else {
  8592. GGML_ASSERT(false);
  8593. }
  8594. }
  8595. }
  8596. cdata += dst->nb[2];
  8597. cdataf += dst->nb[2];
  8598. splane += pa;
  8599. }
  8600. }
  8601. // ggml_compute_forward_upscale
  8602. static void ggml_compute_forward_upscale_f32(
  8603. const struct ggml_compute_params * params,
  8604. struct ggml_tensor * dst) {
  8605. const struct ggml_tensor * src0 = dst->src[0];
  8606. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  8607. const int ith = params->ith;
  8608. const int nth = params->nth;
  8609. GGML_TENSOR_UNARY_OP_LOCALS
  8610. const float sf0 = (float)ne0/src0->ne[0];
  8611. const float sf1 = (float)ne1/src0->ne[1];
  8612. const float sf2 = (float)ne2/src0->ne[2];
  8613. const float sf3 = (float)ne3/src0->ne[3];
  8614. // TODO: optimize
  8615. for (int64_t i3 = 0; i3 < ne3; i3++) {
  8616. const int64_t i03 = i3 / sf3;
  8617. for (int64_t i2 = ith; i2 < ne2; i2 += nth) {
  8618. const int64_t i02 = i2 / sf2;
  8619. for (int64_t i1 = 0; i1 < ne1; i1++) {
  8620. const int64_t i01 = i1 / sf1;
  8621. for (int64_t i0 = 0; i0 < ne0; i0++) {
  8622. const int64_t i00 = i0 / sf0;
  8623. const float * x = (float *)((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03);
  8624. float * y = (float *)((char *) dst->data + i0*nb0 + i1*nb1 + i2*nb2 + i3*nb3);
  8625. *y = *x;
  8626. }
  8627. }
  8628. }
  8629. }
  8630. }
  8631. static void ggml_compute_forward_upscale(
  8632. const struct ggml_compute_params * params,
  8633. struct ggml_tensor * dst) {
  8634. const struct ggml_tensor * src0 = dst->src[0];
  8635. switch (src0->type) {
  8636. case GGML_TYPE_F32:
  8637. {
  8638. ggml_compute_forward_upscale_f32(params, dst);
  8639. } break;
  8640. default:
  8641. {
  8642. GGML_ABORT("fatal error");
  8643. }
  8644. }
  8645. }
  8646. // ggml_compute_forward_pad
  8647. static void ggml_compute_forward_pad_f32(
  8648. const struct ggml_compute_params * params,
  8649. struct ggml_tensor * dst) {
  8650. const struct ggml_tensor * src0 = dst->src[0];
  8651. GGML_ASSERT(src0->nb[0] == sizeof(float));
  8652. GGML_ASSERT( dst->nb[0] == sizeof(float));
  8653. const int ith = params->ith;
  8654. const int nth = params->nth;
  8655. GGML_TENSOR_UNARY_OP_LOCALS
  8656. float * dst_ptr = (float *) dst->data;
  8657. // TODO: optimize
  8658. for (int64_t i2 = 0; i2 < ne2; ++i2) {
  8659. for (int64_t i1 = ith; i1 < ne1; i1 += nth) {
  8660. for (int64_t i0 = 0; i0 < ne0; ++i0) {
  8661. for (int64_t i3 = 0; i3 < ne3; ++i3) {
  8662. const int64_t dst_idx = i3*(ne0*ne1*ne2) + i2*(ne0*ne1) + i1*ne0 + i0;
  8663. const float * src_ptr = (const float *)((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01 + i0*nb00);
  8664. if (i0 < ne00 && i1 < ne01 && i2 < ne02 && i3 < ne03) {
  8665. dst_ptr[dst_idx] = *src_ptr;
  8666. } else {
  8667. dst_ptr[dst_idx] = 0;
  8668. }
  8669. }
  8670. }
  8671. }
  8672. }
  8673. }
  8674. static void ggml_compute_forward_pad(
  8675. const struct ggml_compute_params * params,
  8676. struct ggml_tensor * dst) {
  8677. const struct ggml_tensor * src0 = dst->src[0];
  8678. switch (src0->type) {
  8679. case GGML_TYPE_F32:
  8680. {
  8681. ggml_compute_forward_pad_f32(params, dst);
  8682. } break;
  8683. default:
  8684. {
  8685. GGML_ABORT("fatal error");
  8686. }
  8687. }
  8688. }
  8689. // ggml_compute_forward_pad_reflect_1d
  8690. static void ggml_compute_forward_pad_reflect_1d(
  8691. const struct ggml_compute_params * params,
  8692. struct ggml_tensor * dst) {
  8693. const struct ggml_tensor * src0 = dst->src[0];
  8694. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  8695. GGML_ASSERT( dst->type == GGML_TYPE_F32);
  8696. const int ith = params->ith;
  8697. const int nth = params->nth;
  8698. const int32_t * opts = (const int32_t *) dst->op_params;
  8699. const int p0 = opts[0];
  8700. const int p1 = opts[1];
  8701. GGML_TENSOR_UNARY_OP_LOCALS
  8702. for (int64_t i3 = 0; i3 < ne3; i3++) {
  8703. for (int64_t i2 = 0; i2 < ne2; i2++) {
  8704. for (int64_t i1 = ith; i1 < ne1; i1 += nth) {
  8705. float * left = (float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + p0*nb0);
  8706. float * right = (float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + (ne0-p1-1)*nb0);
  8707. ggml_vec_cpy_f32(ne00, left, (float *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01));
  8708. for (int i0 = 1; i0 <= p0; i0++) { left[-i0] = left[i0]; }
  8709. for (int i0 = 1; i0 <= p1; i0++) { right[i0] = right[-i0]; }
  8710. }
  8711. }
  8712. }
  8713. }
  8714. // ggml_compute_forward_arange
  8715. static void ggml_compute_forward_arange_f32(
  8716. const struct ggml_compute_params * params,
  8717. struct ggml_tensor * dst) {
  8718. GGML_ASSERT(dst->nb[0] == sizeof(float));
  8719. const int ith = params->ith;
  8720. const int nth = params->nth;
  8721. const float start = ggml_get_op_params_f32(dst, 0);
  8722. const float stop = ggml_get_op_params_f32(dst, 1);
  8723. const float step = ggml_get_op_params_f32(dst, 2);
  8724. const int64_t steps = (int64_t) ceilf((stop - start) / step);
  8725. GGML_ASSERT(ggml_nelements(dst) == steps);
  8726. for (int64_t i = ith; i < steps; i+= nth) {
  8727. float value = start + step * i;
  8728. ((float *)dst->data)[i] = value;
  8729. }
  8730. }
  8731. static void ggml_compute_forward_arange(
  8732. const struct ggml_compute_params * params,
  8733. struct ggml_tensor * dst) {
  8734. switch (dst->type) {
  8735. case GGML_TYPE_F32:
  8736. {
  8737. ggml_compute_forward_arange_f32(params, dst);
  8738. } break;
  8739. default:
  8740. {
  8741. GGML_ABORT("fatal error");
  8742. }
  8743. }
  8744. }
  8745. static void ggml_compute_forward_timestep_embedding_f32(
  8746. const struct ggml_compute_params * params,
  8747. struct ggml_tensor * dst) {
  8748. const struct ggml_tensor * src0 = dst->src[0];
  8749. GGML_ASSERT(src0->nb[0] == sizeof(float));
  8750. const int ith = params->ith;
  8751. const int nth = params->nth;
  8752. GGML_TENSOR_UNARY_OP_LOCALS
  8753. const int dim = ggml_get_op_params_i32(dst, 0);
  8754. const int max_period = ggml_get_op_params_i32(dst, 1);
  8755. int half = dim / 2;
  8756. for (int64_t i = 0; i < ne00; i++) {
  8757. float * embed_data = (float *)((char *) dst->data + i*nb1);
  8758. for (int64_t j = ith; j < half; j += nth) {
  8759. float timestep = ((float *)src0->data)[i];
  8760. float freq = (float)expf(-logf(max_period) * j / half);
  8761. float arg = timestep * freq;
  8762. embed_data[j] = cosf(arg);
  8763. embed_data[j + half] = sinf(arg);
  8764. }
  8765. if (dim % 2 != 0 && ith == 0) {
  8766. embed_data[dim] = 0.f;
  8767. }
  8768. }
  8769. }
  8770. static void ggml_compute_forward_timestep_embedding(
  8771. const struct ggml_compute_params * params,
  8772. struct ggml_tensor * dst) {
  8773. const struct ggml_tensor * src0 = dst->src[0];
  8774. switch (src0->type) {
  8775. case GGML_TYPE_F32:
  8776. {
  8777. ggml_compute_forward_timestep_embedding_f32(params, dst);
  8778. } break;
  8779. default:
  8780. {
  8781. GGML_ABORT("fatal error");
  8782. }
  8783. }
  8784. }
  8785. // ggml_compute_forward_argsort
  8786. static void ggml_compute_forward_argsort_f32(
  8787. const struct ggml_compute_params * params,
  8788. struct ggml_tensor * dst) {
  8789. const struct ggml_tensor * src0 = dst->src[0];
  8790. GGML_TENSOR_UNARY_OP_LOCALS
  8791. GGML_ASSERT(nb0 == sizeof(float));
  8792. const int ith = params->ith;
  8793. const int nth = params->nth;
  8794. const int64_t nr = ggml_nrows(src0);
  8795. enum ggml_sort_order order = (enum ggml_sort_order) ggml_get_op_params_i32(dst, 0);
  8796. for (int64_t i = ith; i < nr; i += nth) {
  8797. int32_t * dst_data = (int32_t *)((char *) dst->data + i*nb1);
  8798. const float * src_data = (float *)((char *) src0->data + i*nb01);
  8799. for (int64_t j = 0; j < ne0; j++) {
  8800. dst_data[j] = j;
  8801. }
  8802. // C doesn't have a functional sort, so we do a bubble sort instead
  8803. for (int64_t j = 0; j < ne0; j++) {
  8804. for (int64_t k = j + 1; k < ne0; k++) {
  8805. if ((order == GGML_SORT_ORDER_ASC && src_data[dst_data[j]] > src_data[dst_data[k]]) ||
  8806. (order == GGML_SORT_ORDER_DESC && src_data[dst_data[j]] < src_data[dst_data[k]])) {
  8807. int32_t tmp = dst_data[j];
  8808. dst_data[j] = dst_data[k];
  8809. dst_data[k] = tmp;
  8810. }
  8811. }
  8812. }
  8813. }
  8814. }
  8815. static void ggml_compute_forward_argsort(
  8816. const struct ggml_compute_params * params,
  8817. struct ggml_tensor * dst) {
  8818. const struct ggml_tensor * src0 = dst->src[0];
  8819. switch (src0->type) {
  8820. case GGML_TYPE_F32:
  8821. {
  8822. ggml_compute_forward_argsort_f32(params, dst);
  8823. } break;
  8824. default:
  8825. {
  8826. GGML_ABORT("fatal error");
  8827. }
  8828. }
  8829. }
  8830. // ggml_compute_forward_flash_attn_ext
  8831. static void ggml_compute_forward_flash_attn_ext_f16(
  8832. const struct ggml_compute_params * params,
  8833. const struct ggml_tensor * q,
  8834. const struct ggml_tensor * k,
  8835. const struct ggml_tensor * v,
  8836. const struct ggml_tensor * mask,
  8837. struct ggml_tensor * dst) {
  8838. GGML_TENSOR_LOCALS(int64_t, neq, q, ne)
  8839. GGML_TENSOR_LOCALS(size_t, nbq, q, nb)
  8840. GGML_TENSOR_LOCALS(int64_t, nek, k, ne)
  8841. GGML_TENSOR_LOCALS(size_t, nbk, k, nb)
  8842. GGML_TENSOR_LOCALS(int64_t, nev, v, ne)
  8843. GGML_TENSOR_LOCALS(size_t, nbv, v, nb)
  8844. GGML_TENSOR_LOCALS(int64_t, ne, dst, ne)
  8845. GGML_TENSOR_LOCALS(size_t, nb, dst, nb)
  8846. const int ith = params->ith;
  8847. const int nth = params->nth;
  8848. const int64_t D = neq0;
  8849. const int64_t N = neq1;
  8850. GGML_ASSERT(ne0 == D);
  8851. GGML_ASSERT(ne2 == N);
  8852. // input tensor rows must be contiguous
  8853. GGML_ASSERT(nbq0 == ggml_type_size(q->type));
  8854. GGML_ASSERT(nbk0 == ggml_type_size(k->type));
  8855. GGML_ASSERT(nbv0 == ggml_type_size(v->type));
  8856. GGML_ASSERT(neq0 == D);
  8857. GGML_ASSERT(nek0 == D);
  8858. GGML_ASSERT(nev0 == D);
  8859. GGML_ASSERT(neq1 == N);
  8860. GGML_ASSERT(nev0 == D);
  8861. // dst cannot be transposed or permuted
  8862. GGML_ASSERT(nb0 == sizeof(float));
  8863. GGML_ASSERT(nb0 <= nb1);
  8864. GGML_ASSERT(nb1 <= nb2);
  8865. GGML_ASSERT(nb2 <= nb3);
  8866. // broadcast factors
  8867. const int64_t rk2 = neq2/nek2;
  8868. const int64_t rk3 = neq3/nek3;
  8869. const int64_t rv2 = neq2/nev2;
  8870. const int64_t rv3 = neq3/nev3;
  8871. // parallelize by q rows using ggml_vec_dot_f32
  8872. // total rows in q
  8873. const int nr = neq1*neq2*neq3;
  8874. // rows per thread
  8875. const int dr = (nr + nth - 1)/nth;
  8876. // row range for this thread
  8877. const int ir0 = dr*ith;
  8878. const int ir1 = MIN(ir0 + dr, nr);
  8879. float scale = 1.0f;
  8880. float max_bias = 0.0f;
  8881. float logit_softcap = 0.0f;
  8882. memcpy(&scale, (float *) dst->op_params + 0, sizeof(float));
  8883. memcpy(&max_bias, (float *) dst->op_params + 1, sizeof(float));
  8884. memcpy(&logit_softcap, (float *) dst->op_params + 2, sizeof(float));
  8885. if (logit_softcap != 0) {
  8886. scale /= logit_softcap;
  8887. }
  8888. const uint32_t n_head = neq2;
  8889. const uint32_t n_head_log2 = 1u << (uint32_t) floor(log2(n_head));
  8890. const float m0 = powf(2.0f, -(max_bias ) / n_head_log2);
  8891. const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_head_log2);
  8892. enum ggml_type const k_vec_dot_type = type_traits_cpu[k->type].vec_dot_type;
  8893. ggml_from_float_t const q_to_vec_dot = type_traits_cpu[k_vec_dot_type].from_float;
  8894. ggml_vec_dot_t const kq_vec_dot = type_traits_cpu[k->type].vec_dot;
  8895. ggml_to_float_t const v_to_float = ggml_get_type_traits(v->type)->to_float;
  8896. GGML_ASSERT(q_to_vec_dot && "fattn: unsupported K-type");
  8897. GGML_ASSERT(v_to_float && "fattn: unsupported V-type");
  8898. // loop over n_batch and n_head
  8899. for (int ir = ir0; ir < ir1; ++ir) {
  8900. // q indices
  8901. const int iq3 = ir/(neq2*neq1);
  8902. const int iq2 = (ir - iq3*neq2*neq1)/neq1;
  8903. const int iq1 = (ir - iq3*neq2*neq1 - iq2*neq1);
  8904. const uint32_t h = iq2; // head index
  8905. 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;
  8906. float S = 0.0f; // sum
  8907. float M = -INFINITY; // maximum KQ value
  8908. float * VKQ32 = (float *) params->wdata + ith*(3*D + CACHE_LINE_SIZE_F32); // FP32 VKQ accumulator
  8909. float * V32 = (VKQ32 + 1*D); // (temporary) FP32 V buffer
  8910. ggml_fp16_t * VKQ16 = (ggml_fp16_t *) (VKQ32 + 1*D); // (temporary) FP16 VKQ accumulator
  8911. ggml_fp16_t * Q_q = (ggml_fp16_t *) (VKQ32 + 2*D); // (temporary) buffer for Q converted to quantized/FP16
  8912. if (v->type == GGML_TYPE_F16) {
  8913. memset(VKQ16, 0, D*sizeof(ggml_fp16_t));
  8914. } else {
  8915. memset(VKQ32, 0, D*sizeof(float));
  8916. }
  8917. const ggml_fp16_t * mp = mask ? (ggml_fp16_t *)((char *) mask->data + iq1*mask->nb[1]) : NULL;
  8918. // k indices
  8919. const int ik3 = iq3 / rk3;
  8920. const int ik2 = iq2 / rk2;
  8921. // v indices
  8922. const int iv3 = iq3 / rv3;
  8923. const int iv2 = iq2 / rv2;
  8924. const float * pq = (const float *) ((char *) q->data + (iq1*nbq1 + iq2*nbq2 + iq3*nbq3));
  8925. q_to_vec_dot(pq, Q_q, D);
  8926. // online softmax / attention
  8927. // loop over n_kv and n_head_kv
  8928. // ref: https://arxiv.org/pdf/2112.05682.pdf
  8929. for (int64_t ic = 0; ic < nek1; ++ic) {
  8930. const float mv = mp ? slope*GGML_FP16_TO_FP32(mp[ic]) : 0.0f;
  8931. if (mv == -INFINITY) {
  8932. continue;
  8933. }
  8934. float s; // KQ value
  8935. const char * k_data = (const char *) k->data + ( ic*nbk1 + ik2*nbk2 + ik3*nbk3);
  8936. kq_vec_dot(D, &s, 0, k_data, 0, Q_q, 0, 1);
  8937. s = s*scale; // scale KQ value
  8938. if (logit_softcap != 0.0f) {
  8939. s = logit_softcap*tanhf(s);
  8940. }
  8941. s += mv; // apply mask
  8942. const float Mold = M;
  8943. float ms = 1.0f; // upon new higher max val, scale VKQ and KQ sum with this value
  8944. float vs = 1.0f; // post-softmax KQ value, expf(s - M)
  8945. const char * v_data = ((const char *) v->data + (ic*nbv1 + iv2*nbv2 + iv3*nbv3));
  8946. if (v->type == GGML_TYPE_F16) {
  8947. if (s > M) {
  8948. // s is new maximum, ms < 1.0f, vs == expf(s - s) == 1.0f
  8949. M = s;
  8950. ms = expf(Mold - M);
  8951. // V = V*expf(Mold - M)
  8952. ggml_vec_scale_f16(D, VKQ16, ms);
  8953. } else {
  8954. // no new maximum, ms == 1.0f, vs != 1.0f
  8955. vs = expf(s - M);
  8956. }
  8957. // V += v*expf(s - M)
  8958. ggml_vec_mad_f16(D, VKQ16, (const ggml_fp16_t *) v_data, vs);
  8959. } else {
  8960. if (s > M) {
  8961. // s is new maximum, ms < 1.0f, vs == expf(s - s) == 1.0f
  8962. M = s;
  8963. ms = expf(Mold - M);
  8964. // V = V*expf(Mold - M)
  8965. ggml_vec_scale_f32(D, VKQ32, ms);
  8966. } else {
  8967. // no new maximum, ms == 1.0f, vs != 1.0f
  8968. vs = expf(s - M);
  8969. }
  8970. v_to_float(v_data, V32, D);
  8971. // V += v*expf(s - M)
  8972. ggml_vec_mad_f32(D, VKQ32, V32, vs);
  8973. }
  8974. S = S*ms + vs; // scale and increment sum with partial sum
  8975. }
  8976. if (v->type == GGML_TYPE_F16) {
  8977. for (int64_t d = 0; d < D; ++d) {
  8978. VKQ32[d] = GGML_FP16_TO_FP32(VKQ16[d]);
  8979. }
  8980. }
  8981. // V /= S
  8982. const float S_inv = 1.0f/S;
  8983. ggml_vec_scale_f32(D, VKQ32, S_inv);
  8984. // dst indices
  8985. const int i1 = iq1;
  8986. const int i2 = iq2;
  8987. const int i3 = iq3;
  8988. // original
  8989. //memcpy((char *) dst->data + (i1*nb1 + i2*nb2 + i3*nb3), V, nev0*sizeof(float));
  8990. // permute(0, 2, 1, 3)
  8991. memcpy((char *) dst->data + (i3*ne2*ne1 + i2 + i1*ne1)*nb1, VKQ32, nb1);
  8992. }
  8993. }
  8994. static void ggml_compute_forward_flash_attn_ext(
  8995. const struct ggml_compute_params * params,
  8996. const struct ggml_tensor * q,
  8997. const struct ggml_tensor * k,
  8998. const struct ggml_tensor * v,
  8999. const struct ggml_tensor * mask,
  9000. struct ggml_tensor * dst) {
  9001. switch (dst->op_params[3]) {
  9002. case GGML_PREC_DEFAULT:
  9003. case GGML_PREC_F32:
  9004. {
  9005. // uses F32 accumulators
  9006. ggml_compute_forward_flash_attn_ext_f16(params, q, k, v, mask, dst);
  9007. } break;
  9008. default:
  9009. {
  9010. GGML_ABORT("fatal error");
  9011. }
  9012. }
  9013. }
  9014. // ggml_compute_forward_flash_attn_back
  9015. static void ggml_compute_forward_flash_attn_back_f32(
  9016. const struct ggml_compute_params * params,
  9017. const bool masked,
  9018. struct ggml_tensor * dst) {
  9019. const struct ggml_tensor * q = dst->src[0];
  9020. const struct ggml_tensor * k = dst->src[1];
  9021. const struct ggml_tensor * v = dst->src[2];
  9022. const struct ggml_tensor * d = dst->src[3];
  9023. GGML_TENSOR_LOCALS(int64_t, neq, q, ne)
  9024. GGML_TENSOR_LOCALS(size_t, nbq, q, nb)
  9025. GGML_TENSOR_LOCALS(int64_t, nek, k, ne)
  9026. GGML_TENSOR_LOCALS(size_t, nbk, k, nb)
  9027. GGML_TENSOR_LOCALS(int64_t, nev, v, ne)
  9028. GGML_TENSOR_LOCALS(size_t, nbv, v, nb)
  9029. GGML_TENSOR_LOCALS(int64_t, ned, d, ne)
  9030. GGML_TENSOR_LOCALS(size_t, nbd, d, nb)
  9031. GGML_TENSOR_LOCALS(int64_t, ne, dst, ne)
  9032. GGML_TENSOR_LOCALS(size_t, nb, dst, nb)
  9033. const int ith = params->ith;
  9034. const int nth = params->nth;
  9035. const int64_t D = neq0;
  9036. const int64_t N = neq1;
  9037. const int64_t P = nek1 - N;
  9038. const int64_t M = P + N;
  9039. const int Mup = ggml_up(M, GGML_SOFT_MAX_UNROLL);
  9040. const int mxDM = MAX(D, Mup);
  9041. // GGML_ASSERT(ne0 == D);
  9042. // GGML_ASSERT(ne1 == N);
  9043. GGML_ASSERT(P >= 0);
  9044. GGML_ASSERT(nbq0 == sizeof(float));
  9045. GGML_ASSERT(nbk0 == sizeof(float));
  9046. GGML_ASSERT(nbv0 == sizeof(float));
  9047. GGML_ASSERT(neq0 == D);
  9048. GGML_ASSERT(nek0 == D);
  9049. GGML_ASSERT(nev1 == D);
  9050. GGML_ASSERT(ned0 == D);
  9051. GGML_ASSERT(neq1 == N);
  9052. GGML_ASSERT(nek1 == N + P);
  9053. GGML_ASSERT(nev1 == D);
  9054. GGML_ASSERT(ned1 == N);
  9055. // dst cannot be transposed or permuted
  9056. GGML_ASSERT(nb0 == sizeof(float));
  9057. GGML_ASSERT(nb0 <= nb1);
  9058. GGML_ASSERT(nb1 <= nb2);
  9059. GGML_ASSERT(nb2 <= nb3);
  9060. if (ith == 0) {
  9061. memset(dst->data, 0, nb0*ne0*ne1*ne2*ne3);
  9062. }
  9063. ggml_barrier(params->threadpool);
  9064. const int64_t elem_q = ggml_nelements(q);
  9065. const int64_t elem_k = ggml_nelements(k);
  9066. enum ggml_type result_type = dst->type;
  9067. GGML_ASSERT(ggml_blck_size(result_type) == 1);
  9068. const size_t tsize = ggml_type_size(result_type);
  9069. const size_t offs_q = 0;
  9070. const size_t offs_k = offs_q + GGML_PAD(elem_q * tsize, GGML_MEM_ALIGN);
  9071. const size_t offs_v = offs_k + GGML_PAD(elem_k * tsize, GGML_MEM_ALIGN);
  9072. void * grad_q = (char *) dst->data;
  9073. void * grad_k = (char *) dst->data + offs_k;
  9074. void * grad_v = (char *) dst->data + offs_v;
  9075. const size_t nbgq1 = nb0*neq0;
  9076. const size_t nbgq2 = nb0*neq0*neq1;
  9077. const size_t nbgq3 = nb0*neq0*neq1*neq2;
  9078. const size_t nbgk1 = nb0*nek0;
  9079. const size_t nbgk2 = nb0*nek0*nek1;
  9080. const size_t nbgk3 = nb0*nek0*nek1*neq2;
  9081. const size_t nbgv1 = nb0*nev0;
  9082. const size_t nbgv2 = nb0*nev0*nev1;
  9083. const size_t nbgv3 = nb0*nev0*nev1*neq2;
  9084. // parallelize by k rows using ggml_vec_dot_f32
  9085. // total rows in k
  9086. const int nr = nek2*nek3;
  9087. // rows per thread
  9088. const int dr = (nr + nth - 1)/nth;
  9089. // row range for this thread
  9090. const int ir0 = dr*ith;
  9091. const int ir1 = MIN(ir0 + dr, nr);
  9092. const float scale = 1.0f/sqrtf(D);
  9093. //printf("P=%d N=%d D=%d ir0=%d ir1=%d scale = %f\n", P, N, D, ir0, ir1, scale);
  9094. // how often k2 (and v2) is repeated in q2
  9095. int nrep = neq2/nek2;
  9096. for (int ir = ir0; ir < ir1; ++ir) {
  9097. // q indices
  9098. const int ik3 = ir/(nek2);
  9099. const int ik2 = ir - ik3*nek2;
  9100. const int iq3 = ik3;
  9101. const int id3 = ik3;
  9102. const int iv3 = ik3;
  9103. const int iv2 = ik2;
  9104. for (int irep = 0; irep < nrep; ++irep) {
  9105. const int iq2 = ik2 + irep*nek2;
  9106. const int id2 = iq2;
  9107. // (ik2 + irep*nek2) % nek2 == ik2
  9108. for (int iq1 = 0; iq1 < neq1; ++iq1) {
  9109. const int id1 = iq1;
  9110. // not sure about CACHE_LINE_SIZE_F32..
  9111. // - maybe it must not be multiplied by 2 and excluded from .. in SM 1*(..) offset?
  9112. float * S = (float *) params->wdata + ith*2*(mxDM + CACHE_LINE_SIZE_F32) + 0*(mxDM+CACHE_LINE_SIZE_F32);
  9113. float * SM = (float *) params->wdata + ith*2*(mxDM + CACHE_LINE_SIZE_F32) + 1*(mxDM+CACHE_LINE_SIZE_F32);
  9114. for (int i = M; i < Mup; ++i) {
  9115. S[i] = -INFINITY;
  9116. }
  9117. const int64_t masked_begin = masked ? (P + iq1 + 1) : M;
  9118. for (int64_t ic = 0; ic < masked_begin; ++ic) {
  9119. // k indices
  9120. const int ik1 = ic;
  9121. // S indices
  9122. const int i1 = ik1;
  9123. ggml_vec_dot_f32(neq0,
  9124. S + i1, 0,
  9125. (float *) ((char *) k->data + (ik1*nbk1 + ik2*nbk2 + ik3*nbk3)), 0,
  9126. (float *) ((char *) q->data + (iq1*nbq1 + iq2*nbq2 + iq3*nbq3)), 0, 1);
  9127. }
  9128. // scale
  9129. ggml_vec_scale_f32(masked_begin, S, scale);
  9130. for (int64_t i = masked_begin; i < M; i++) {
  9131. S[i] = -INFINITY;
  9132. }
  9133. // softmax
  9134. // exclude known -INF S[..] values from max and loop
  9135. // dont forget to set their SM values to zero
  9136. {
  9137. float max = -INFINITY;
  9138. ggml_vec_max_f32(masked_begin, &max, S);
  9139. ggml_float sum = 0.0;
  9140. {
  9141. #ifdef GGML_SOFT_MAX_ACCELERATE
  9142. max = -max;
  9143. vDSP_vsadd(SM, 1, &max, SM, 1, Mup);
  9144. vvexpf(SM, SM, &Mup);
  9145. ggml_vec_sum_f32(Mup, &sum, SM);
  9146. #else
  9147. sum = ggml_vec_soft_max_f32(Mup, SM, S, max);
  9148. #endif
  9149. }
  9150. assert(sum > 0.0);
  9151. sum = 1.0/sum;
  9152. ggml_vec_scale_f32(masked_begin, SM, sum);
  9153. }
  9154. // step-by-step explanation
  9155. {
  9156. // forward-process shape grads from backward process
  9157. // parallel_for ik2,ik3:
  9158. // for irep:
  9159. // iq2 = ik2 + irep*nek2
  9160. // k[:D,:M,:,:] [D,M,:,:] grad[k][:D,:M,ik2,ik3] += grad[kcur]
  9161. // q[:D,:N,:,:] [D,N,:,:] grad[q][:D,iq1,iq2,iq3] += grad[qcur]
  9162. // v[:M,:D,:,:] [M,D,:,:] grad[v][:M,:D,iv2,iv3] += grad[vcur]
  9163. // for iq1:
  9164. // kcur = k[:D,:M,ik2,ik3] [D,M,1,1] grad[kcur] = grad[S1].T @ qcur
  9165. // qcur = q[:D,iq1,iq2,iq3] [D,1,1,1] grad[qcur] = grad[S1] @ kcur
  9166. // vcur = v[:M,:D,iv2,iv3] [M,D,1,1] grad[vcur] = grad[S5].T @ S4
  9167. // S0 = -Inf [D,1,1,1]
  9168. // ~S1[i] = dot(kcur[:D,i], qcur)
  9169. // S1 = qcur @ kcur.T [M,1,1,1] grad[S1] = grad[S2] * scale
  9170. // S2 = S1 * scale [M,1,1,1] grad[S2] = diag_mask_zero(grad[S3], P)
  9171. // S3 = diag_mask_inf(S2, P) [M,1,1,1] grad[S3] = S4 * (grad[S4] - dot(S4, grad[S4]))
  9172. // S4 = softmax(S3) [M,1,1,1] grad[S4] = grad[S5] @ vcur
  9173. // ~S5[i] = dot(vcur[:,i], S4)
  9174. // S5 = S4 @ vcur.T [D,1,1,1] grad[S5] = d[:D,id1,id2,id3]
  9175. // ~dst[i,iq1,iq2,iq3] = S5[i] ^
  9176. // dst[:D,iq1,iq2,iq3] = S5 | grad[dst[:D,iq1,iq2,iq3]] = d[:D,id1,id2,id3]
  9177. // dst backward-/ grad[dst] = d
  9178. //
  9179. // output gradients with their dependencies:
  9180. //
  9181. // grad[kcur] = grad[S1].T @ qcur
  9182. // grad[S1] = diag_mask_zero(grad[S3], P) * scale
  9183. // grad[S3] = S4 * (grad[S4] - dot(S4, grad[S4]))
  9184. // grad[S4] = grad[S5] @ vcur
  9185. // grad[S4] = d[:D,id1,id2,id3] @ vcur
  9186. // grad[qcur] = grad[S1] @ kcur
  9187. // grad[vcur] = grad[S5].T @ S4
  9188. // grad[vcur] = d[:D,id1,id2,id3].T @ S4
  9189. //
  9190. // in post-order:
  9191. //
  9192. // S1 = qcur @ kcur.T
  9193. // S2 = S1 * scale
  9194. // S3 = diag_mask_inf(S2, P)
  9195. // S4 = softmax(S3)
  9196. // grad[S4] = d[:D,id1,id2,id3] @ vcur
  9197. // grad[S3] = S4 * (grad[S4] - dot(S4, grad[S4]))
  9198. // grad[S1] = diag_mask_zero(grad[S3], P) * scale
  9199. // grad[qcur] = grad[S1] @ kcur
  9200. // grad[kcur] = grad[S1].T @ qcur
  9201. // grad[vcur] = d[:D,id1,id2,id3].T @ S4
  9202. //
  9203. // using less variables (SM=S4):
  9204. //
  9205. // S = diag_mask_inf(qcur @ kcur.T * scale, P)
  9206. // SM = softmax(S)
  9207. // S = d[:D,iq1,iq2,iq3] @ vcur
  9208. // dot_SM_gradSM = dot(SM, S)
  9209. // S = SM * (S - dot(SM, S))
  9210. // S = diag_mask_zero(S, P) * scale
  9211. //
  9212. // grad[q][:D,iq1,iq2,iq3] += S @ kcur
  9213. // grad[k][:D,:M,ik2,ik3] += S.T @ qcur
  9214. // grad[v][:M,:D,iv2,iv3] += d[:D,id1,id2,id3].T @ SM
  9215. }
  9216. // S = gradSM = d[:D,id1,id2,id3] @ vcur[:,:,iv2,iv3]
  9217. // S = d[:D,id1,id2,id3] @ vcur[:,:,iv2,iv3]
  9218. // for ic:
  9219. // S[:M] += vcur[:M,ic,iv2,iv3] * d[ic,id1,id2,id3]
  9220. // exclude known future zero S[..] values from operation
  9221. ggml_vec_set_f32(masked_begin, S, 0);
  9222. for (int64_t ic = 0; ic < D; ++ic) {
  9223. ggml_vec_mad_f32(masked_begin,
  9224. S,
  9225. (float *) ((char *) v->data + ( ic*nbv1 + iv2*nbv2 + iv3*nbv3)),
  9226. *(float *) ((char *) d->data + (ic*nbd0 + id1*nbd1 + id2*nbd2 + id3*nbd3)));
  9227. }
  9228. // S = SM * (S - dot(SM, S))
  9229. float dot_SM_gradSM = 0;
  9230. ggml_vec_dot_f32 (masked_begin, &dot_SM_gradSM, 0, SM, 0, S, 0, 1);
  9231. ggml_vec_acc1_f32(M, S, -dot_SM_gradSM);
  9232. ggml_vec_mul_f32 (masked_begin, S, S, SM);
  9233. // S = diag_mask_zero(S, P) * scale
  9234. // already done by above ggml_vec_set_f32
  9235. // exclude known zero S[..] values from operation
  9236. ggml_vec_scale_f32(masked_begin, S, scale);
  9237. // S shape [M,1]
  9238. // SM shape [M,1]
  9239. // kcur shape [D,M]
  9240. // qcur shape [D,1]
  9241. // vcur shape [M,D]
  9242. // grad[q][:D,iq1,iq2,iq3] += S @ kcur
  9243. // grad[q][:D,iq1,iq2,iq3] += shape[M,1] @ shape[D,M]
  9244. // for ic:
  9245. // grad[q][:D,iq1,iq2,iq3] += S[ic] * kcur[:D,ic,ik2,ik3]
  9246. // exclude known zero S[..] values from loop
  9247. for (int64_t ic = 0; ic < masked_begin; ++ic) {
  9248. ggml_vec_mad_f32(D,
  9249. (float *) ((char *) grad_q + (iq1*nbgq1 + iq2*nbgq2 + iq3*nbgq3)),
  9250. (float *) ((char *) k->data + (ic*nbk1 + ik2*nbk2 + ik3*nbk3)),
  9251. S[ic]);
  9252. }
  9253. // grad[k][:D,:M,iq2,iq3] += S.T @ qcur
  9254. // for ic:
  9255. // grad[k][:D,ic,iq2,iq3] += S.T[0,ic] * qcur[:D,0]
  9256. // grad[k][:D,ic,iq2,iq3] += S[ic] * qcur[:D,0]
  9257. // exclude known zero S[..] values from loop
  9258. for (int64_t ic = 0; ic < masked_begin; ++ic) {
  9259. ggml_vec_mad_f32(D,
  9260. (float *) ((char *) grad_k + (ic*nbgk1 + ik2*nbgk2 + ik3*nbgk3)),
  9261. (float *) ((char *) q->data + (iq1*nbq1 + iq2*nbq2 + iq3*nbq3)),
  9262. S[ic]);
  9263. }
  9264. // grad[v][:M,:D,iv2,iv3] += d[:D,id1,id2,id3].T @ SM
  9265. // for ic:
  9266. // grad[v][:M,ic,iv2,iv3] += d[:D,id1,id2,id3].T[0,ic] * SM[:M]
  9267. // grad[v][:M,ic,iv2,iv3] += d[ic,id1,id2,id3] * SM[:M]
  9268. // exclude known zero SM[..] values from mad
  9269. for (int64_t ic = 0; ic < D; ++ic) {
  9270. ggml_vec_mad_f32(masked_begin,
  9271. (float *) ((char *) grad_v + ( ic*nbgv1 + iv2*nbgv2 + iv3*nbgv3)),
  9272. SM,
  9273. *(float *) ((char *) d->data + (ic*nbd0 + id1*nbd1 + id2*nbd2 + id3*nbd3)));
  9274. }
  9275. }
  9276. }
  9277. }
  9278. }
  9279. static void ggml_compute_forward_flash_attn_back(
  9280. const struct ggml_compute_params * params,
  9281. const bool masked,
  9282. struct ggml_tensor * dst) {
  9283. const struct ggml_tensor * q = dst->src[0];
  9284. switch (q->type) {
  9285. case GGML_TYPE_F32:
  9286. {
  9287. ggml_compute_forward_flash_attn_back_f32(params, masked, dst);
  9288. } break;
  9289. default:
  9290. {
  9291. GGML_ABORT("fatal error");
  9292. }
  9293. }
  9294. }
  9295. // ggml_compute_forward_ssm_conv
  9296. static void ggml_compute_forward_ssm_conv_f32(
  9297. const struct ggml_compute_params * params,
  9298. struct ggml_tensor * dst) {
  9299. const struct ggml_tensor * src0 = dst->src[0]; // conv_x
  9300. const struct ggml_tensor * src1 = dst->src[1]; // conv1d.weight
  9301. const int ith = params->ith;
  9302. const int nth = params->nth;
  9303. const int nc = src1->ne[0]; // d_conv
  9304. const int ncs = src0->ne[0]; // d_conv - 1 + n_t
  9305. const int nr = src0->ne[1]; // d_inner
  9306. const int n_t = dst->ne[1]; // tokens per sequence
  9307. const int n_s = dst->ne[2]; // number of sequences in the batch
  9308. GGML_ASSERT( dst->ne[0] == nr);
  9309. GGML_ASSERT(src0->nb[0] == sizeof(float));
  9310. GGML_ASSERT(src1->nb[0] == sizeof(float));
  9311. GGML_ASSERT(src0->nb[1] == src0->ne[0]*sizeof(float));
  9312. // rows per thread
  9313. const int dr = (nr + nth - 1)/nth;
  9314. // row range for this thread
  9315. const int ir0 = dr*ith;
  9316. const int ir1 = MIN(ir0 + dr, nr);
  9317. const int ir = ir1 - ir0;
  9318. for (int i3 = 0; i3 < n_s; ++i3) {
  9319. for (int i2 = 0; i2 < n_t; ++i2) {
  9320. // {d_conv - 1 + n_t, d_inner, n_seqs}
  9321. // sliding window
  9322. 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}
  9323. const float * c = (const float *) ((const char *) src1->data + ir0*(src1->nb[1])); // {d_conv, d_inner}
  9324. float * x = (float *) ((char *) dst->data + ir0*(dst->nb[0]) + i2*(dst->nb[1]) + i3*(dst->nb[2])); // {d_inner, n_t, n_s}
  9325. // TODO: transpose the output for smaller strides for big batches?
  9326. // d_inner
  9327. for (int i1 = 0; i1 < ir; ++i1) {
  9328. // rowwise dot product
  9329. // NOTE: not using ggml_vec_dot_f32, because its sum is in double precision
  9330. float sumf = 0.0f;
  9331. // d_conv
  9332. for (int i0 = 0; i0 < nc; ++i0) {
  9333. sumf += s[i0 + i1*ncs] * c[i0 + i1*nc];
  9334. }
  9335. x[i1] = sumf;
  9336. }
  9337. }
  9338. }
  9339. }
  9340. static void ggml_compute_forward_ssm_conv(
  9341. const struct ggml_compute_params * params,
  9342. struct ggml_tensor * dst) {
  9343. switch (dst->src[0]->type) {
  9344. case GGML_TYPE_F32:
  9345. {
  9346. ggml_compute_forward_ssm_conv_f32(params, dst);
  9347. } break;
  9348. default:
  9349. {
  9350. GGML_ABORT("fatal error");
  9351. }
  9352. }
  9353. }
  9354. // ggml_compute_forward_ssm_scan
  9355. static void ggml_compute_forward_ssm_scan_f32(
  9356. const struct ggml_compute_params * params,
  9357. struct ggml_tensor * dst) {
  9358. const struct ggml_tensor * src0 = dst->src[0]; // s
  9359. const struct ggml_tensor * src1 = dst->src[1]; // x
  9360. const struct ggml_tensor * src2 = dst->src[2]; // dt
  9361. const struct ggml_tensor * src3 = dst->src[3]; // A
  9362. const struct ggml_tensor * src4 = dst->src[4]; // B
  9363. const struct ggml_tensor * src5 = dst->src[5]; // C
  9364. const int ith = params->ith;
  9365. const int nth = params->nth;
  9366. const int64_t nc = src0->ne[0]; // d_state
  9367. const int64_t nr = src0->ne[1]; // d_inner
  9368. const int64_t n_t = src1->ne[1]; // number of tokens per sequence
  9369. const int64_t n_s = src0->ne[2]; // number of sequences in the batch
  9370. GGML_ASSERT(ggml_nelements(src1) + ggml_nelements(src0) == ggml_nelements(dst));
  9371. GGML_ASSERT(src0->nb[0] == sizeof(float));
  9372. GGML_ASSERT(src1->nb[0] == sizeof(float));
  9373. GGML_ASSERT(src2->nb[0] == sizeof(float));
  9374. GGML_ASSERT(src3->nb[0] == sizeof(float));
  9375. GGML_ASSERT(src4->nb[0] == sizeof(float));
  9376. GGML_ASSERT(src5->nb[0] == sizeof(float));
  9377. // required for the dot product between s and C
  9378. GGML_ASSERT(src0->nb[1] == src0->ne[0]*sizeof(float));
  9379. // required for per-sequence offsets for states
  9380. GGML_ASSERT(src0->nb[2] == src0->ne[0]*src0->ne[1]*sizeof(float));
  9381. // required to get correct offset for state destination (i.e. src1->nb[3])
  9382. GGML_ASSERT(src1->nb[3] == src1->ne[0]*src1->ne[1]*src1->ne[2]*sizeof(float));
  9383. // rows per thread
  9384. const int dr = (nr + nth - 1)/nth;
  9385. // row range for this thread
  9386. const int ir0 = dr*ith;
  9387. const int ir1 = MIN(ir0 + dr, nr);
  9388. const int ir = ir1 - ir0;
  9389. for (int i3 = 0; i3 < n_s; ++i3) {
  9390. for (int i2 = 0; i2 < n_t; ++i2) {
  9391. const float * s0 = (const float *) ((const char *) src0->data + ir0*(src0->nb[1]) + i3*(src0->nb[2])); // {d_state, d_inner, n_s}
  9392. 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}
  9393. 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}
  9394. const float * A = (const float *) ((const char *) src3->data + ir0*(src3->nb[1])); // {d_state, d_inner}
  9395. const float * B = (const float *) ((const char *) src4->data + i2*(src4->nb[1]) + i3*(src4->nb[2])); // {d_state, n_t, n_s}
  9396. const float * C = (const float *) ((const char *) src5->data + i2*(src5->nb[1]) + i3*(src5->nb[2])); // {d_state, n_t, n_s}
  9397. float * y = ( float *) (( char *) dst->data + ir0*(src1->nb[0]) + i2*(src1->nb[1]) + i3*(src1->nb[2])); // {d_inner, n_t, n_s}
  9398. float * s = ( float *) (( char *) dst->data + ir0*(src0->nb[1]) + i3*(src0->nb[2]) + src1->nb[3]); // {d_state, d_inner, n_s}
  9399. // use the output as the source for the next token-wise iterations
  9400. if (i2 > 0) { s0 = s; }
  9401. // d_inner
  9402. for (int i1 = 0; i1 < ir; ++i1) {
  9403. // ref: https://github.com/state-spaces/mamba/blob/34076d664838588a3c97727b263478ab9f621a07/mamba_ssm/ops/triton/selective_state_update.py#L78
  9404. float dt_soft_plus = dt[i1] <= 20.0f ? log1pf(expf(dt[i1])) : dt[i1];
  9405. float x_dt = x[i1] * dt_soft_plus;
  9406. float sumf = 0.0f;
  9407. // d_state
  9408. for (int i0 = 0; i0 < nc; ++i0) {
  9409. int i = i0 + i1*nc;
  9410. // state = prev_state * dA + dB * x
  9411. float state = (s0[i] * expf(dt_soft_plus * A[i])) + (B[i0] * x_dt);
  9412. // y = rowwise_dotprod(state, C)
  9413. sumf += state * C[i0];
  9414. s[i] = state;
  9415. }
  9416. y[i1] = sumf;
  9417. }
  9418. }
  9419. }
  9420. }
  9421. static void ggml_compute_forward_ssm_scan(
  9422. const struct ggml_compute_params * params,
  9423. struct ggml_tensor * dst) {
  9424. switch (dst->src[0]->type) {
  9425. case GGML_TYPE_F32:
  9426. {
  9427. ggml_compute_forward_ssm_scan_f32(params, dst);
  9428. } break;
  9429. default:
  9430. {
  9431. GGML_ABORT("fatal error");
  9432. }
  9433. }
  9434. }
  9435. // ggml_compute_forward_win_part
  9436. static void ggml_compute_forward_win_part_f32(
  9437. const struct ggml_compute_params * params,
  9438. struct ggml_tensor * dst) {
  9439. UNUSED(params);
  9440. const struct ggml_tensor * src0 = dst->src[0];
  9441. GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne)
  9442. GGML_TENSOR_LOCALS(int64_t, ne, dst, ne)
  9443. const int32_t nep0 = ((const int32_t *)(dst->op_params))[0];
  9444. const int32_t nep1 = ((const int32_t *)(dst->op_params))[1];
  9445. const int32_t w = ((const int32_t *)(dst->op_params))[2];
  9446. assert(ne00 == ne0);
  9447. assert(ne3 == nep0*nep1);
  9448. // TODO: optimize / multi-thread
  9449. for (int py = 0; py < nep1; ++py) {
  9450. for (int px = 0; px < nep0; ++px) {
  9451. const int64_t i3 = py*nep0 + px;
  9452. for (int64_t i2 = 0; i2 < ne2; ++i2) {
  9453. for (int64_t i1 = 0; i1 < ne1; ++i1) {
  9454. for (int64_t i0 = 0; i0 < ne0; ++i0) {
  9455. const int64_t i02 = py*w + i2;
  9456. const int64_t i01 = px*w + i1;
  9457. const int64_t i00 = i0;
  9458. const int64_t i = i3*ne2*ne1*ne0 + i2*ne1*ne0 + i1*ne0 + i0;
  9459. const int64_t j = i02*ne01*ne00 + i01*ne00 + i00;
  9460. if (py*w + i2 >= ne02 || px*w + i1 >= ne01) {
  9461. ((float *) dst->data)[i] = 0.0f;
  9462. } else {
  9463. ((float *) dst->data)[i] = ((float *) src0->data)[j];
  9464. }
  9465. }
  9466. }
  9467. }
  9468. }
  9469. }
  9470. }
  9471. static void ggml_compute_forward_win_part(
  9472. const struct ggml_compute_params * params,
  9473. struct ggml_tensor * dst) {
  9474. const struct ggml_tensor * src0 = dst->src[0];
  9475. switch (src0->type) {
  9476. case GGML_TYPE_F32:
  9477. {
  9478. ggml_compute_forward_win_part_f32(params, dst);
  9479. } break;
  9480. default:
  9481. {
  9482. GGML_ABORT("fatal error");
  9483. }
  9484. }
  9485. }
  9486. // ggml_compute_forward_win_unpart
  9487. static void ggml_compute_forward_win_unpart_f32(
  9488. const struct ggml_compute_params * params,
  9489. struct ggml_tensor * dst) {
  9490. UNUSED(params);
  9491. const struct ggml_tensor * src0 = dst->src[0];
  9492. GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne)
  9493. GGML_TENSOR_LOCALS(int64_t, ne, dst, ne)
  9494. const int32_t w = ((const int32_t *)(dst->op_params))[0];
  9495. // padding
  9496. const int px = (w - ne1%w)%w;
  9497. //const int py = (w - ne2%w)%w;
  9498. const int npx = (px + ne1)/w;
  9499. //const int npy = (py + ne2)/w;
  9500. assert(ne0 == ne00);
  9501. // TODO: optimize / multi-thread
  9502. for (int64_t i2 = 0; i2 < ne2; ++i2) {
  9503. for (int64_t i1 = 0; i1 < ne1; ++i1) {
  9504. for (int64_t i0 = 0; i0 < ne0; ++i0) {
  9505. const int ip2 = i2/w;
  9506. const int ip1 = i1/w;
  9507. const int64_t i02 = i2%w;
  9508. const int64_t i01 = i1%w;
  9509. const int64_t i00 = i0;
  9510. const int64_t i = (ip2*npx + ip1)*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00 + i00;
  9511. const int64_t j = i2*ne1*ne0 + i1*ne0 + i0;
  9512. ((float *) dst->data)[j] = ((float *) src0->data)[i];
  9513. }
  9514. }
  9515. }
  9516. }
  9517. static void ggml_compute_forward_win_unpart(
  9518. const struct ggml_compute_params * params,
  9519. struct ggml_tensor * dst) {
  9520. const struct ggml_tensor * src0 = dst->src[0];
  9521. switch (src0->type) {
  9522. case GGML_TYPE_F32:
  9523. {
  9524. ggml_compute_forward_win_unpart_f32(params, dst);
  9525. } break;
  9526. default:
  9527. {
  9528. GGML_ABORT("fatal error");
  9529. }
  9530. }
  9531. }
  9532. //gmml_compute_forward_unary
  9533. static void ggml_compute_forward_unary(
  9534. const struct ggml_compute_params * params,
  9535. struct ggml_tensor * dst) {
  9536. const enum ggml_unary_op op = ggml_get_unary_op(dst);
  9537. switch (op) {
  9538. case GGML_UNARY_OP_ABS:
  9539. {
  9540. ggml_compute_forward_abs(params, dst);
  9541. } break;
  9542. case GGML_UNARY_OP_SGN:
  9543. {
  9544. ggml_compute_forward_sgn(params, dst);
  9545. } break;
  9546. case GGML_UNARY_OP_NEG:
  9547. {
  9548. ggml_compute_forward_neg(params, dst);
  9549. } break;
  9550. case GGML_UNARY_OP_STEP:
  9551. {
  9552. ggml_compute_forward_step(params, dst);
  9553. } break;
  9554. case GGML_UNARY_OP_TANH:
  9555. {
  9556. ggml_compute_forward_tanh(params, dst);
  9557. } break;
  9558. case GGML_UNARY_OP_ELU:
  9559. {
  9560. ggml_compute_forward_elu(params, dst);
  9561. } break;
  9562. case GGML_UNARY_OP_RELU:
  9563. {
  9564. ggml_compute_forward_relu(params, dst);
  9565. } break;
  9566. case GGML_UNARY_OP_SIGMOID:
  9567. {
  9568. ggml_compute_forward_sigmoid(params, dst);
  9569. } break;
  9570. case GGML_UNARY_OP_GELU:
  9571. {
  9572. ggml_compute_forward_gelu(params, dst);
  9573. } break;
  9574. case GGML_UNARY_OP_GELU_QUICK:
  9575. {
  9576. ggml_compute_forward_gelu_quick(params, dst);
  9577. } break;
  9578. case GGML_UNARY_OP_SILU:
  9579. {
  9580. ggml_compute_forward_silu(params, dst);
  9581. } break;
  9582. case GGML_UNARY_OP_HARDSWISH:
  9583. {
  9584. ggml_compute_forward_hardswish(params, dst);
  9585. } break;
  9586. case GGML_UNARY_OP_HARDSIGMOID:
  9587. {
  9588. ggml_compute_forward_hardsigmoid(params, dst);
  9589. } break;
  9590. case GGML_UNARY_OP_EXP:
  9591. {
  9592. ggml_compute_forward_exp(params, dst);
  9593. } break;
  9594. default:
  9595. {
  9596. GGML_ABORT("fatal error");
  9597. }
  9598. }
  9599. }
  9600. // ggml_compute_forward_get_rel_pos
  9601. static void ggml_compute_forward_get_rel_pos_f16(
  9602. const struct ggml_compute_params * params,
  9603. struct ggml_tensor * dst) {
  9604. UNUSED(params);
  9605. const struct ggml_tensor * src0 = dst->src[0];
  9606. // ref: https://github.com/facebookresearch/segment-anything/blob/main/segment_anything/modeling/image_encoder.py#L292-L322
  9607. GGML_TENSOR_UNARY_OP_LOCALS
  9608. const int64_t w = ne1;
  9609. ggml_fp16_t * src0_data = (ggml_fp16_t *) src0->data;
  9610. ggml_fp16_t * dst_data = (ggml_fp16_t *) dst->data;
  9611. for (int64_t i2 = 0; i2 < ne2; ++i2) {
  9612. for (int64_t i1 = 0; i1 < ne1; ++i1) {
  9613. const int64_t pos = (w - i1 - 1) + i2;
  9614. for (int64_t i0 = 0; i0 < ne0; ++i0) {
  9615. dst_data[i2*ne1*ne0 + i1*ne0 + i0] = src0_data[pos*ne00 + i0];
  9616. }
  9617. }
  9618. }
  9619. }
  9620. static void ggml_compute_forward_get_rel_pos(
  9621. const struct ggml_compute_params * params,
  9622. struct ggml_tensor * dst) {
  9623. const struct ggml_tensor * src0 = dst->src[0];
  9624. switch (src0->type) {
  9625. case GGML_TYPE_F16:
  9626. case GGML_TYPE_BF16:
  9627. {
  9628. ggml_compute_forward_get_rel_pos_f16(params, dst);
  9629. } break;
  9630. default:
  9631. {
  9632. GGML_ABORT("fatal error");
  9633. }
  9634. }
  9635. }
  9636. // ggml_compute_forward_add_rel_pos
  9637. static void ggml_compute_forward_add_rel_pos_f32(
  9638. const struct ggml_compute_params * params,
  9639. struct ggml_tensor * dst) {
  9640. const struct ggml_tensor * src0 = dst->src[0];
  9641. const struct ggml_tensor * src1 = dst->src[1];
  9642. const struct ggml_tensor * src2 = dst->src[2];
  9643. const bool inplace = (bool) ((int32_t *) dst->op_params)[0];
  9644. if (!inplace) {
  9645. if (params->ith == 0) {
  9646. memcpy((char *) dst->data, (char *) src0->data, ggml_nbytes(dst));
  9647. }
  9648. ggml_barrier(params->threadpool);
  9649. }
  9650. // ref: https://github.com/facebookresearch/segment-anything/blob/main/segment_anything/modeling/image_encoder.py#L357-L359
  9651. float * src1_data = (float *) src1->data;
  9652. float * src2_data = (float *) src2->data;
  9653. float * dst_data = (float *) dst->data;
  9654. const int64_t ne10 = src1->ne[0];
  9655. const int64_t ne11 = src1->ne[1];
  9656. const int64_t ne12 = src1->ne[2];
  9657. const int64_t ne13 = src1->ne[3];
  9658. const int ith = params->ith;
  9659. const int nth = params->nth;
  9660. // total patches in dst
  9661. const int np = ne13;
  9662. // patches per thread
  9663. const int dp = (np + nth - 1)/nth;
  9664. // patch range for this thread
  9665. const int ip0 = dp*ith;
  9666. const int ip1 = MIN(ip0 + dp, np);
  9667. for (int64_t i13 = ip0; i13 < ip1; ++i13) {
  9668. for (int64_t i12 = 0; i12 < ne12; ++i12) {
  9669. for (int64_t i11 = 0; i11 < ne11; ++i11) {
  9670. const int64_t jp1 = i13*ne12*ne11*ne10 + i12*ne11*ne10 + i11*ne10;
  9671. for (int64_t i10 = 0; i10 < ne10; ++i10) {
  9672. const int64_t jp0 = jp1 + i10;
  9673. const float src1_e = src1_data[jp0];
  9674. const float src2_e = src2_data[jp0];
  9675. const int64_t jdh = jp0 * ne10;
  9676. const int64_t jdw = jdh - (ne10 - 1) * i10;
  9677. for (int64_t j = 0; j < ne10; ++j) {
  9678. dst_data[jdh + j ] += src2_e;
  9679. dst_data[jdw + j*ne10] += src1_e;
  9680. }
  9681. }
  9682. }
  9683. }
  9684. }
  9685. }
  9686. static void ggml_compute_forward_add_rel_pos(
  9687. const struct ggml_compute_params * params,
  9688. struct ggml_tensor * dst) {
  9689. const struct ggml_tensor * src0 = dst->src[0];
  9690. switch (src0->type) {
  9691. case GGML_TYPE_F32:
  9692. {
  9693. ggml_compute_forward_add_rel_pos_f32(params, dst);
  9694. } break;
  9695. default:
  9696. {
  9697. GGML_ABORT("fatal error");
  9698. }
  9699. }
  9700. }
  9701. // ggml_compute_forward_rwkv_wkv6
  9702. static void ggml_compute_forward_rwkv_wkv6_f32(
  9703. const struct ggml_compute_params * params,
  9704. struct ggml_tensor * dst) {
  9705. const int64_t T = dst->src[1]->ne[3];
  9706. const int64_t C = dst->ne[0];
  9707. const int64_t HEADS = dst->src[1]->ne[2];
  9708. const int64_t n_seqs = dst->src[5]->ne[1];
  9709. const int64_t head_size = C / HEADS;
  9710. float * dst_data = (float *) dst->data;
  9711. float * state = ((float *) dst->data) + C * T;
  9712. const int ith = params->ith;
  9713. const int nth = params->nth;
  9714. if (ith >= HEADS) {
  9715. return;
  9716. }
  9717. const int h_start = (HEADS * ith) / nth;
  9718. const int h_end = ((HEADS * (ith + 1)) / nth < HEADS) ?
  9719. (HEADS * (ith + 1)) / nth : HEADS;
  9720. float * k = (float *) dst->src[0]->data;
  9721. float * v = (float *) dst->src[1]->data;
  9722. float * r = (float *) dst->src[2]->data;
  9723. float * time_faaaa = (float *) dst->src[3]->data;
  9724. float * time_decay = (float *) dst->src[4]->data;
  9725. size_t t_stride = HEADS * head_size; // Same to C
  9726. size_t h_stride = C / HEADS;
  9727. GGML_ASSERT(C % HEADS == 0); // C must be divisible by HEADS
  9728. size_t h_stride_2d = head_size * head_size;
  9729. if (ith == 0) {
  9730. memset(dst_data, 0, T * C * sizeof(float));
  9731. }
  9732. ggml_barrier(params->threadpool);
  9733. #if defined(__AVX__) && !defined(__AVX512F__)
  9734. #define GGML_F32X GGML_F32x8
  9735. #define GGML_F32X_SET1 GGML_F32x8_SET1
  9736. #define GGML_F32X_LOAD GGML_F32x8_LOAD
  9737. #define GGML_F32X_STORE GGML_F32x8_STORE
  9738. #define GGML_F32X_MUL GGML_F32x8_MUL
  9739. #define GGML_F32X_FMA GGML_F32x8_FMA
  9740. #define WKV_VECTOR_SIZE 8
  9741. #elif defined(__AVX512F__)
  9742. #define GGML_F32X GGML_F32x16
  9743. #define GGML_F32X_SET1 GGML_F32x16_SET1
  9744. #define GGML_F32X_LOAD GGML_F32x16_LOAD
  9745. #define GGML_F32X_STORE GGML_F32x16_STORE
  9746. #define GGML_F32X_MUL GGML_F32x16_MUL
  9747. #define GGML_F32X_FMA GGML_F32x16_FMA
  9748. #define WKV_VECTOR_SIZE 16
  9749. #elif defined(__ARM_NEON) && defined(__aarch64__)
  9750. #define GGML_F32X GGML_F32x4
  9751. #define GGML_F32X_SET1 GGML_F32x4_SET1
  9752. #define GGML_F32X_LOAD GGML_F32x4_LOAD
  9753. #define GGML_F32X_STORE GGML_F32x4_STORE
  9754. #define GGML_F32X_MUL GGML_F32x4_MUL
  9755. #define GGML_F32X_FMA GGML_F32x4_FMA
  9756. #define WKV_VECTOR_SIZE 4
  9757. #endif
  9758. #ifdef WKV_VECTOR_SIZE
  9759. const int64_t vec_count = head_size / WKV_VECTOR_SIZE;
  9760. for (int64_t t = 0; t < T; t++) {
  9761. size_t t_offset = t * t_stride;
  9762. size_t state_offset = head_size * C * (t / (T / n_seqs));
  9763. float * state_cur = state + state_offset;
  9764. float * state_prev = t % (T / n_seqs) ? state_cur : (float*)dst->src[5]->data + state_offset;
  9765. for (int64_t h = h_start; h < h_end; h++) {
  9766. size_t h_offset = h * h_stride;
  9767. size_t t_h_offset = t_offset + h_offset;
  9768. size_t h_2d_offset = h * h_stride_2d;
  9769. for (int64_t i = 0; i < head_size; i++) {
  9770. size_t t_h_i_offset = t_h_offset + i;
  9771. size_t h_i_offset = h_offset + i;
  9772. size_t h_2d_i_offset = h_2d_offset + i * h_stride;
  9773. float k_val = k[t_h_i_offset];
  9774. float r_val = r[t_h_i_offset];
  9775. float time_faaaa_val = time_faaaa[h_i_offset];
  9776. float time_decay_val = time_decay[t_h_i_offset];
  9777. // Broadcast scalar values to vectors
  9778. GGML_F32X k_vec = GGML_F32X_SET1(k_val);
  9779. GGML_F32X r_vec = GGML_F32X_SET1(r_val);
  9780. GGML_F32X time_faaaa_vec = GGML_F32X_SET1(time_faaaa_val);
  9781. GGML_F32X time_decay_vec = GGML_F32X_SET1(time_decay_val);
  9782. for (int64_t j = 0; j < vec_count; j++) {
  9783. size_t base_j = j * WKV_VECTOR_SIZE;
  9784. size_t t_h_j_offset = t_h_offset + base_j;
  9785. size_t h_2d_i_j_offset = h_2d_i_offset + base_j;
  9786. // Load x elements at once
  9787. GGML_F32X v_vec = GGML_F32X_LOAD(&v[t_h_j_offset]);
  9788. GGML_F32X prev_state_vec = GGML_F32X_LOAD(&state_prev[h_2d_i_j_offset]);
  9789. GGML_F32X dst_vec = GGML_F32X_LOAD(&dst_data[t_h_j_offset]);
  9790. // Compute kv = v * k
  9791. GGML_F32X kv_vec = GGML_F32X_MUL(v_vec, k_vec);
  9792. // Compute temp = kv * time_faaaa + prev_state
  9793. GGML_F32X temp_vec = GGML_F32X_FMA(prev_state_vec, kv_vec, time_faaaa_vec);
  9794. // Update dst: dst += temp * r
  9795. dst_vec = GGML_F32X_FMA(dst_vec, temp_vec, r_vec);
  9796. GGML_F32X_STORE(&dst_data[t_h_j_offset], dst_vec);
  9797. // Update state: state = prev_state * time_decay + kv
  9798. GGML_F32X new_state_vec = GGML_F32X_FMA(kv_vec, prev_state_vec, time_decay_vec);
  9799. GGML_F32X_STORE(&state_cur[h_2d_i_j_offset], new_state_vec);
  9800. }
  9801. // Handle remaining elements, this will not be used.
  9802. for (int64_t j = vec_count * WKV_VECTOR_SIZE; j < head_size; j++) {
  9803. size_t t_h_j_offset = t_h_offset + j;
  9804. size_t h_2d_i_j_offset = h_2d_i_offset + j;
  9805. float v_val = v[t_h_j_offset];
  9806. float kv_val = v_val * k_val;
  9807. float prev_state_val = state_prev[h_2d_i_j_offset];
  9808. float temp_val = kv_val * time_faaaa_val + prev_state_val;
  9809. dst_data[t_h_j_offset] += temp_val * r_val;
  9810. state_cur[h_2d_i_j_offset] = prev_state_val * time_decay_val + kv_val;
  9811. }
  9812. }
  9813. }
  9814. }
  9815. #else
  9816. // basically fused operations:
  9817. // dst = r @ (time_faaaa * (k @ v) + state),
  9818. // state = time_decay * state + (k @ v),
  9819. // recursive through each token
  9820. for (int64_t t = 0; t < T; t++) {
  9821. size_t t_offset = t * t_stride;
  9822. size_t state_offset = head_size * C * (t / (T / n_seqs));
  9823. float * state_cur = state + state_offset;
  9824. float * state_prev = t % (T / n_seqs) ? state_cur : (float*)dst->src[5]->data + state_offset;
  9825. for (int64_t h = h_start; h < h_end; h++) {
  9826. size_t h_offset = h * h_stride;
  9827. size_t t_h_offset = t_offset + h_offset;
  9828. size_t h_2d_offset = h * h_stride_2d;
  9829. for (int64_t i = 0; i < head_size; i++) {
  9830. size_t t_h_i_offset = t_h_offset + i;
  9831. size_t h_i_offset = h_offset + i;
  9832. size_t h_2d_i_offset = h_2d_offset + i * h_stride;
  9833. float k_val = k[t_h_i_offset];
  9834. float r_val = r[t_h_i_offset];
  9835. float time_faaaa_val = time_faaaa[h_i_offset];
  9836. // RWKV v6: different time_decay for each token.
  9837. float time_decay_val = time_decay[t_h_i_offset];
  9838. for (int64_t j = 0; j < head_size; j++) {
  9839. size_t t_h_j_offset = t_h_offset + j;
  9840. size_t h_2d_i_j_offset = h_2d_i_offset + j;
  9841. float v_val = v[t_h_j_offset];
  9842. float kv_val = v_val * k_val;
  9843. float prev_state_val = state_prev[h_2d_i_j_offset];
  9844. float temp_val = kv_val * time_faaaa_val + prev_state_val;
  9845. dst_data[t_h_j_offset] += temp_val * r_val;
  9846. state_cur[h_2d_i_j_offset] = prev_state_val * time_decay_val + kv_val;
  9847. }
  9848. }
  9849. }
  9850. }
  9851. #endif
  9852. }
  9853. static void ggml_compute_forward_rwkv_wkv6(
  9854. const struct ggml_compute_params * params,
  9855. struct ggml_tensor * dst) {
  9856. const struct ggml_tensor * src0 = dst->src[0];
  9857. switch (src0->type) {
  9858. case GGML_TYPE_F32:
  9859. {
  9860. ggml_compute_forward_rwkv_wkv6_f32(params, dst);
  9861. } break;
  9862. default:
  9863. {
  9864. GGML_ABORT("fatal error");
  9865. }
  9866. }
  9867. }
  9868. // ggml_compute_forward_map_unary
  9869. static void ggml_compute_forward_map_unary_f32(
  9870. const struct ggml_compute_params * params,
  9871. struct ggml_tensor * dst,
  9872. const ggml_unary_op_f32_t fun) {
  9873. const struct ggml_tensor * src0 = dst->src[0];
  9874. if (params->ith != 0) {
  9875. return;
  9876. }
  9877. assert(ggml_is_contiguous_1(src0));
  9878. assert(ggml_is_contiguous_1(dst));
  9879. assert(ggml_are_same_shape(src0, dst));
  9880. const int n = ggml_nrows(src0);
  9881. const int nc = src0->ne[0];
  9882. for (int i = 0; i < n; i++) {
  9883. fun(nc,
  9884. (float *) ((char *) dst->data + i*( dst->nb[1])),
  9885. (float *) ((char *) src0->data + i*(src0->nb[1])));
  9886. }
  9887. }
  9888. static void ggml_compute_forward_map_unary(
  9889. const struct ggml_compute_params * params,
  9890. struct ggml_tensor * dst,
  9891. const ggml_unary_op_f32_t fun) {
  9892. const struct ggml_tensor * src0 = dst->src[0];
  9893. switch (src0->type) {
  9894. case GGML_TYPE_F32:
  9895. {
  9896. ggml_compute_forward_map_unary_f32(params, dst, fun);
  9897. } break;
  9898. default:
  9899. {
  9900. GGML_ABORT("fatal error");
  9901. }
  9902. }
  9903. }
  9904. // ggml_compute_forward_map_binary
  9905. static void ggml_compute_forward_map_binary_f32(
  9906. const struct ggml_compute_params * params,
  9907. struct ggml_tensor * dst,
  9908. const ggml_binary_op_f32_t fun) {
  9909. const struct ggml_tensor * src0 = dst->src[0];
  9910. const struct ggml_tensor * src1 = dst->src[1];
  9911. if (params->ith != 0) {
  9912. return;
  9913. }
  9914. assert(ggml_is_contiguous_1(src0));
  9915. assert(ggml_is_contiguous_1(src1));
  9916. assert(ggml_is_contiguous_1(dst));
  9917. assert(ggml_are_same_shape(src0, src1) && ggml_are_same_shape(src0, dst));
  9918. const int n = ggml_nrows(src0);
  9919. const int nc = src0->ne[0];
  9920. for (int i = 0; i < n; i++) {
  9921. fun(nc,
  9922. (float *) ((char *) dst->data + i*( dst->nb[1])),
  9923. (float *) ((char *) src0->data + i*(src0->nb[1])),
  9924. (float *) ((char *) src1->data + i*(src1->nb[1])));
  9925. }
  9926. }
  9927. static void ggml_compute_forward_map_binary(
  9928. const struct ggml_compute_params * params,
  9929. struct ggml_tensor * dst,
  9930. const ggml_binary_op_f32_t fun) {
  9931. const struct ggml_tensor * src0 = dst->src[0];
  9932. switch (src0->type) {
  9933. case GGML_TYPE_F32:
  9934. {
  9935. ggml_compute_forward_map_binary_f32(params, dst, fun);
  9936. } break;
  9937. default:
  9938. {
  9939. GGML_ABORT("fatal error");
  9940. }
  9941. }
  9942. }
  9943. // ggml_compute_forward_map_custom1
  9944. static void ggml_compute_forward_map_custom1_f32(
  9945. const struct ggml_compute_params * params,
  9946. struct ggml_tensor * dst,
  9947. const ggml_custom1_op_f32_t fun) {
  9948. const struct ggml_tensor * a = dst->src[0];
  9949. if (params->ith != 0) {
  9950. return;
  9951. }
  9952. fun(dst, a);
  9953. }
  9954. // ggml_compute_forward_map_custom2
  9955. static void ggml_compute_forward_map_custom2_f32(
  9956. const struct ggml_compute_params * params,
  9957. struct ggml_tensor * dst,
  9958. const ggml_custom2_op_f32_t fun) {
  9959. const struct ggml_tensor * a = dst->src[0];
  9960. const struct ggml_tensor * b = dst->src[1];
  9961. if (params->ith != 0) {
  9962. return;
  9963. }
  9964. fun(dst, a, b);
  9965. }
  9966. // ggml_compute_forward_map_custom3
  9967. static void ggml_compute_forward_map_custom3_f32(
  9968. const struct ggml_compute_params * params,
  9969. struct ggml_tensor * dst,
  9970. const ggml_custom3_op_f32_t fun) {
  9971. const struct ggml_tensor * a = dst->src[0];
  9972. const struct ggml_tensor * b = dst->src[1];
  9973. const struct ggml_tensor * c = dst->src[1];
  9974. if (params->ith != 0) {
  9975. return;
  9976. }
  9977. fun(dst, a, b, c);
  9978. }
  9979. // ggml_compute_forward_map_custom1
  9980. static void ggml_compute_forward_map_custom1(
  9981. const struct ggml_compute_params * params,
  9982. struct ggml_tensor * dst) {
  9983. const struct ggml_tensor * a = dst->src[0];
  9984. struct ggml_map_custom1_op_params p;
  9985. memcpy(&p, dst->op_params, sizeof(p));
  9986. p.fun(dst, a, params->ith, params->nth, p.userdata);
  9987. }
  9988. // ggml_compute_forward_map_custom2
  9989. static void ggml_compute_forward_map_custom2(
  9990. const struct ggml_compute_params * params,
  9991. struct ggml_tensor * dst) {
  9992. const struct ggml_tensor * a = dst->src[0];
  9993. const struct ggml_tensor * b = dst->src[1];
  9994. struct ggml_map_custom2_op_params p;
  9995. memcpy(&p, dst->op_params, sizeof(p));
  9996. p.fun(dst, a, b, params->ith, params->nth, p.userdata);
  9997. }
  9998. // ggml_compute_forward_map_custom3
  9999. static void ggml_compute_forward_map_custom3(
  10000. const struct ggml_compute_params * params,
  10001. struct ggml_tensor * dst) {
  10002. const struct ggml_tensor * a = dst->src[0];
  10003. const struct ggml_tensor * b = dst->src[1];
  10004. const struct ggml_tensor * c = dst->src[2];
  10005. struct ggml_map_custom3_op_params p;
  10006. memcpy(&p, dst->op_params, sizeof(p));
  10007. p.fun(dst, a, b, c, params->ith, params->nth, p.userdata);
  10008. }
  10009. // ggml_compute_forward_cross_entropy_loss
  10010. static void ggml_compute_forward_cross_entropy_loss_f32(
  10011. const struct ggml_compute_params * params,
  10012. struct ggml_tensor * dst) {
  10013. const struct ggml_tensor * src0 = dst->src[0];
  10014. const struct ggml_tensor * src1 = dst->src[1];
  10015. GGML_ASSERT(src0->type == GGML_TYPE_F32);
  10016. GGML_ASSERT(src1->type == GGML_TYPE_F32);
  10017. GGML_ASSERT(src0->nb[0] == ggml_type_size(src0->type));
  10018. GGML_ASSERT(src1->nb[0] == ggml_type_size(src1->type));
  10019. GGML_ASSERT(ggml_are_same_shape(src0, src1));
  10020. GGML_ASSERT(ggml_is_scalar(dst));
  10021. GGML_ASSERT(dst->type == GGML_TYPE_F32);
  10022. // TODO: handle transposed/permuted matrices
  10023. const int64_t nc = src0->ne[0];
  10024. const int64_t nr = ggml_nrows(src0);
  10025. const int ith = params->ith;
  10026. const int nth = params->nth;
  10027. float * sums = (float *) params->wdata;
  10028. float * st = ((float *) params->wdata) + nth + ith*nc;
  10029. float sum_thread = 0.0f;
  10030. GGML_ASSERT(params->wsize >= sizeof(float) * (nth + nth * nc));
  10031. // rows per thread
  10032. const int64_t dr = (nr + nth - 1)/nth;
  10033. // row range for this thread
  10034. const int64_t ir0 = dr*ith;
  10035. const int64_t ir1 = MIN(ir0 + dr, nr);
  10036. for (int64_t i1 = ir0; i1 < ir1; ++i1) {
  10037. const float * s0 = (const float *)((const char *) src0->data + i1*src0->nb[1]);
  10038. const float * s1 = (const float *)((const char *) src1->data + i1*src1->nb[1]);
  10039. #ifndef NDEBUG
  10040. for (int64_t i = 0; i < nc; ++i) {
  10041. //printf("p[%d] = %f\n", i, p[i]);
  10042. assert(!isnan(s0[i]));
  10043. assert(!isnan(s1[i]));
  10044. }
  10045. #endif
  10046. float max = -INFINITY;
  10047. ggml_vec_max_f32(nc, &max, s0);
  10048. const ggml_float sum_softmax = ggml_vec_log_soft_max_f32(nc, st, s0, max);
  10049. assert(sum_softmax >= 0.0);
  10050. ggml_vec_add1_f32(nc, st, st, -sum_softmax);
  10051. ggml_vec_mul_f32(nc, st, st, s1);
  10052. float sum_st = 0.0f;
  10053. ggml_vec_sum_f32(nc, &sum_st, st);
  10054. sum_thread += sum_st;
  10055. #ifndef NDEBUG
  10056. for (int64_t i = 0; i < nc; ++i) {
  10057. assert(!isnan(st[i]));
  10058. assert(!isinf(st[i]));
  10059. }
  10060. #endif
  10061. }
  10062. sums[ith] = sum_thread;
  10063. ggml_barrier(params->threadpool);
  10064. if (ith == 0) {
  10065. float * dp = (float *) dst->data;
  10066. ggml_vec_sum_f32(nth, dp, sums);
  10067. dp[0] *= -1.0f / (float) nr;
  10068. }
  10069. }
  10070. static void ggml_compute_forward_cross_entropy_loss(
  10071. const struct ggml_compute_params * params,
  10072. struct ggml_tensor * dst) {
  10073. const struct ggml_tensor * src0 = dst->src[0];
  10074. switch (src0->type) {
  10075. case GGML_TYPE_F32:
  10076. {
  10077. ggml_compute_forward_cross_entropy_loss_f32(params, dst);
  10078. } break;
  10079. default:
  10080. {
  10081. GGML_ABORT("fatal error");
  10082. }
  10083. }
  10084. }
  10085. // ggml_compute_forward_cross_entropy_loss_back
  10086. static void ggml_compute_forward_cross_entropy_loss_back_f32(
  10087. const struct ggml_compute_params * params,
  10088. struct ggml_tensor * dst) {
  10089. const struct ggml_tensor * src0 = dst->src[0];
  10090. const struct ggml_tensor * src1 = dst->src[1];
  10091. const struct ggml_tensor * opt0 = dst->src[2];
  10092. GGML_ASSERT(ggml_is_contiguous(dst));
  10093. GGML_ASSERT(ggml_is_contiguous(src0));
  10094. GGML_ASSERT(ggml_is_contiguous(src1));
  10095. GGML_ASSERT(ggml_is_contiguous(opt0));
  10096. GGML_ASSERT(ggml_are_same_shape(src0, src1) && ggml_are_same_shape(src0, dst));
  10097. const int64_t ith = params->ith;
  10098. const int64_t nth = params->nth;
  10099. // TODO: handle transposed/permuted matrices
  10100. const int64_t nc = src0->ne[0];
  10101. const int64_t nr = ggml_nrows(src0);
  10102. // rows per thread
  10103. const int64_t dr = (nr + nth - 1)/nth;
  10104. // row range for this thread
  10105. const int64_t ir0 = dr*ith;
  10106. const int64_t ir1 = MIN(ir0 + dr, nr);
  10107. const float d_by_nr = ((const float *) opt0->data)[0] / (float) nr;
  10108. for (int64_t i1 = ir0; i1 < ir1; i1++) {
  10109. float * ds0 = (float *)((char *) dst->data + i1*dst->nb[1]);
  10110. float * s0 = (float *)((char *) src0->data + i1*src0->nb[1]);
  10111. float * s1 = (float *)((char *) src1->data + i1*src1->nb[1]);
  10112. #ifndef NDEBUG
  10113. for (int64_t i = 0; i < nc; ++i) {
  10114. //printf("p[%d] = %f\n", i, p[i]);
  10115. assert(!isnan(s0[i]));
  10116. assert(!isnan(s1[i]));
  10117. }
  10118. #endif
  10119. // soft_max
  10120. float max = -INFINITY;
  10121. ggml_vec_max_f32(nc, &max, s0);
  10122. ggml_float sum = ggml_vec_soft_max_f32(nc, ds0, s0, max);
  10123. assert(sum > 0.0);
  10124. ggml_vec_scale_f32(nc, ds0, 1.0/sum);
  10125. // grad(src0) = (softmax(src0) - src1) * grad(cross_entropy_loss(src0, src1)) / nr
  10126. ggml_vec_sub_f32(nc, ds0, ds0, s1);
  10127. ggml_vec_scale_f32(nc, ds0, d_by_nr);
  10128. #ifndef NDEBUG
  10129. for (int64_t i = 0; i < nc; ++i) {
  10130. assert(!isnan(ds0[i]));
  10131. assert(!isinf(ds0[i]));
  10132. }
  10133. #endif
  10134. }
  10135. }
  10136. static void ggml_compute_forward_cross_entropy_loss_back(
  10137. const struct ggml_compute_params * params,
  10138. struct ggml_tensor * dst) {
  10139. const struct ggml_tensor * src0 = dst->src[0];
  10140. switch (src0->type) {
  10141. case GGML_TYPE_F32:
  10142. {
  10143. ggml_compute_forward_cross_entropy_loss_back_f32(params, dst);
  10144. } break;
  10145. default:
  10146. {
  10147. GGML_ABORT("fatal error");
  10148. }
  10149. }
  10150. }
  10151. static void ggml_compute_forward_opt_step_adamw_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 * src0_grad = dst->src[1];
  10156. const struct ggml_tensor * src0_grad_m = dst->src[2];
  10157. const struct ggml_tensor * src0_grad_v = dst->src[3];
  10158. const struct ggml_tensor * adamw_params = dst->src[4];
  10159. GGML_ASSERT(ggml_are_same_shape(src0, src0_grad));
  10160. GGML_ASSERT(ggml_are_same_shape(src0, src0_grad_m));
  10161. GGML_ASSERT(ggml_are_same_shape(src0, src0_grad_v));
  10162. GGML_ASSERT(ggml_nelements(adamw_params) == 7);
  10163. const int ith = params->ith;
  10164. const int nth = params->nth;
  10165. const int nr = ggml_nrows(src0);
  10166. GGML_TENSOR_UNARY_OP_LOCALS
  10167. GGML_ASSERT(nb00 == sizeof(float));
  10168. // rows per thread
  10169. const int dr = (nr + nth - 1)/nth;
  10170. // row range for this thread
  10171. const int ir0 = dr*ith;
  10172. const int ir1 = MIN(ir0 + dr, nr);
  10173. const float * adamw_params_ptr = ggml_get_data_f32(adamw_params);
  10174. const float alpha = adamw_params_ptr[0];
  10175. const float beta1 = adamw_params_ptr[1];
  10176. const float beta2 = adamw_params_ptr[2];
  10177. const float eps = adamw_params_ptr[3];
  10178. const float wd = adamw_params_ptr[4];
  10179. const float beta1h = adamw_params_ptr[5];
  10180. const float beta2h = adamw_params_ptr[6];
  10181. for (int ir = ir0; ir < ir1; ++ir) {
  10182. const int64_t i03 = ir/(ne02*ne01);
  10183. const int64_t i02 = (ir - i03*ne02*ne01)/ne01;
  10184. const int64_t i01 = (ir - i03*ne02*ne01 - i02*ne01);
  10185. const size_t offset = i03*nb03 + i02*nb02 + i01*nb01;
  10186. float * w = (float *) ((char *) src0->data + offset); // weight
  10187. const float * g = (const float *) ((const char *) src0_grad->data + offset); // grad
  10188. float * m = (float *) ((char *) src0_grad_m->data + offset);
  10189. float * v = (float *) ((char *) src0_grad_v->data + offset);
  10190. for (int i00 = 0; i00 < ne00; ++i00) {
  10191. m[i00] = m[i00]*beta1 + g[i00]*(1.0f - beta1);
  10192. v[i00] = v[i00]*beta2 + g[i00]*g[i00]*(1.0f - beta2);
  10193. const float mh = m[i00]*beta1h;
  10194. const float vh = sqrtf(v[i00]*beta2h) + eps;
  10195. // The weight decay is applied independently of the Adam momenta m and v.
  10196. // This is NOT equivalent to l2 regularization that adds w[i00]*w[i00] to the loss.
  10197. // See: https://arxiv.org/pdf/1711.05101v3.pdf
  10198. w[i00] = w[i00]*(1.0f - alpha*wd) - alpha*mh/vh;
  10199. }
  10200. }
  10201. }
  10202. static void ggml_compute_forward_opt_step_adamw(
  10203. const struct ggml_compute_params * params,
  10204. struct ggml_tensor * dst) {
  10205. const struct ggml_tensor * src0 = dst->src[0];
  10206. switch (src0->type) {
  10207. case GGML_TYPE_F32:
  10208. {
  10209. ggml_compute_forward_opt_step_adamw_f32(params, dst);
  10210. } break;
  10211. default:
  10212. {
  10213. GGML_ABORT("fatal error");
  10214. }
  10215. }
  10216. }
  10217. /////////////////////////////////
  10218. static void ggml_compute_forward(struct ggml_compute_params * params, struct ggml_tensor * tensor) {
  10219. GGML_ASSERT(params);
  10220. if (tensor->op == GGML_OP_NONE || ggml_is_empty(tensor)) {
  10221. return;
  10222. }
  10223. // extra_buffer op?
  10224. if (ggml_cpu_extra_compute_forward(params, tensor)) return;
  10225. switch (tensor->op) {
  10226. case GGML_OP_DUP:
  10227. {
  10228. ggml_compute_forward_dup(params, tensor);
  10229. } break;
  10230. case GGML_OP_ADD:
  10231. {
  10232. ggml_compute_forward_add(params, tensor);
  10233. } break;
  10234. case GGML_OP_ADD1:
  10235. {
  10236. ggml_compute_forward_add1(params, tensor);
  10237. } break;
  10238. case GGML_OP_ACC:
  10239. {
  10240. ggml_compute_forward_acc(params, tensor);
  10241. } break;
  10242. case GGML_OP_SUB:
  10243. {
  10244. ggml_compute_forward_sub(params, tensor);
  10245. } break;
  10246. case GGML_OP_MUL:
  10247. {
  10248. ggml_compute_forward_mul(params, tensor);
  10249. } break;
  10250. case GGML_OP_DIV:
  10251. {
  10252. ggml_compute_forward_div(params, tensor);
  10253. } break;
  10254. case GGML_OP_SQR:
  10255. {
  10256. ggml_compute_forward_sqr(params, tensor);
  10257. } break;
  10258. case GGML_OP_SQRT:
  10259. {
  10260. ggml_compute_forward_sqrt(params, tensor);
  10261. } break;
  10262. case GGML_OP_LOG:
  10263. {
  10264. ggml_compute_forward_log(params, tensor);
  10265. } break;
  10266. case GGML_OP_SIN:
  10267. {
  10268. ggml_compute_forward_sin(params, tensor);
  10269. } break;
  10270. case GGML_OP_COS:
  10271. {
  10272. ggml_compute_forward_cos(params, tensor);
  10273. } break;
  10274. case GGML_OP_SUM:
  10275. {
  10276. ggml_compute_forward_sum(params, tensor);
  10277. } break;
  10278. case GGML_OP_SUM_ROWS:
  10279. {
  10280. ggml_compute_forward_sum_rows(params, tensor);
  10281. } break;
  10282. case GGML_OP_MEAN:
  10283. {
  10284. ggml_compute_forward_mean(params, tensor);
  10285. } break;
  10286. case GGML_OP_ARGMAX:
  10287. {
  10288. ggml_compute_forward_argmax(params, tensor);
  10289. } break;
  10290. case GGML_OP_COUNT_EQUAL:
  10291. {
  10292. ggml_compute_forward_count_equal(params, tensor);
  10293. } break;
  10294. case GGML_OP_REPEAT:
  10295. {
  10296. ggml_compute_forward_repeat(params, tensor);
  10297. } break;
  10298. case GGML_OP_REPEAT_BACK:
  10299. {
  10300. ggml_compute_forward_repeat_back(params, tensor);
  10301. } break;
  10302. case GGML_OP_CONCAT:
  10303. {
  10304. ggml_compute_forward_concat(params, tensor);
  10305. } break;
  10306. case GGML_OP_SILU_BACK:
  10307. {
  10308. ggml_compute_forward_silu_back(params, tensor);
  10309. } break;
  10310. case GGML_OP_NORM:
  10311. {
  10312. ggml_compute_forward_norm(params, tensor);
  10313. } break;
  10314. case GGML_OP_RMS_NORM:
  10315. {
  10316. ggml_compute_forward_rms_norm(params, tensor);
  10317. } break;
  10318. case GGML_OP_RMS_NORM_BACK:
  10319. {
  10320. ggml_compute_forward_rms_norm_back(params, tensor);
  10321. } break;
  10322. case GGML_OP_GROUP_NORM:
  10323. {
  10324. ggml_compute_forward_group_norm(params, tensor);
  10325. } break;
  10326. case GGML_OP_MUL_MAT:
  10327. {
  10328. ggml_compute_forward_mul_mat(params, tensor);
  10329. } break;
  10330. case GGML_OP_MUL_MAT_ID:
  10331. {
  10332. ggml_compute_forward_mul_mat_id(params, tensor);
  10333. } break;
  10334. case GGML_OP_OUT_PROD:
  10335. {
  10336. ggml_compute_forward_out_prod(params, tensor);
  10337. } break;
  10338. case GGML_OP_SCALE:
  10339. {
  10340. ggml_compute_forward_scale(params, tensor);
  10341. } break;
  10342. case GGML_OP_SET:
  10343. {
  10344. ggml_compute_forward_set(params, tensor);
  10345. } break;
  10346. case GGML_OP_CPY:
  10347. {
  10348. ggml_compute_forward_cpy(params, tensor);
  10349. } break;
  10350. case GGML_OP_CONT:
  10351. {
  10352. ggml_compute_forward_cont(params, tensor);
  10353. } break;
  10354. case GGML_OP_RESHAPE:
  10355. {
  10356. ggml_compute_forward_reshape(params, tensor);
  10357. } break;
  10358. case GGML_OP_VIEW:
  10359. {
  10360. ggml_compute_forward_view(params, tensor);
  10361. } break;
  10362. case GGML_OP_PERMUTE:
  10363. {
  10364. ggml_compute_forward_permute(params, tensor);
  10365. } break;
  10366. case GGML_OP_TRANSPOSE:
  10367. {
  10368. ggml_compute_forward_transpose(params, tensor);
  10369. } break;
  10370. case GGML_OP_GET_ROWS:
  10371. {
  10372. ggml_compute_forward_get_rows(params, tensor);
  10373. } break;
  10374. case GGML_OP_GET_ROWS_BACK:
  10375. {
  10376. ggml_compute_forward_get_rows_back(params, tensor);
  10377. } break;
  10378. case GGML_OP_DIAG:
  10379. {
  10380. ggml_compute_forward_diag(params, tensor);
  10381. } break;
  10382. case GGML_OP_DIAG_MASK_INF:
  10383. {
  10384. ggml_compute_forward_diag_mask_inf(params, tensor);
  10385. } break;
  10386. case GGML_OP_DIAG_MASK_ZERO:
  10387. {
  10388. ggml_compute_forward_diag_mask_zero(params, tensor);
  10389. } break;
  10390. case GGML_OP_SOFT_MAX:
  10391. {
  10392. ggml_compute_forward_soft_max(params, tensor);
  10393. } break;
  10394. case GGML_OP_SOFT_MAX_BACK:
  10395. {
  10396. ggml_compute_forward_soft_max_back(params, tensor);
  10397. } break;
  10398. case GGML_OP_ROPE:
  10399. {
  10400. ggml_compute_forward_rope(params, tensor);
  10401. } break;
  10402. case GGML_OP_ROPE_BACK:
  10403. {
  10404. ggml_compute_forward_rope_back(params, tensor);
  10405. } break;
  10406. case GGML_OP_CLAMP:
  10407. {
  10408. ggml_compute_forward_clamp(params, tensor);
  10409. } break;
  10410. case GGML_OP_CONV_TRANSPOSE_1D:
  10411. {
  10412. ggml_compute_forward_conv_transpose_1d(params, tensor);
  10413. } break;
  10414. case GGML_OP_IM2COL:
  10415. {
  10416. ggml_compute_forward_im2col(params, tensor);
  10417. } break;
  10418. case GGML_OP_IM2COL_BACK:
  10419. {
  10420. ggml_compute_forward_im2col_back_f32(params, tensor);
  10421. } break;
  10422. case GGML_OP_CONV_TRANSPOSE_2D:
  10423. {
  10424. ggml_compute_forward_conv_transpose_2d(params, tensor);
  10425. } break;
  10426. case GGML_OP_POOL_1D:
  10427. {
  10428. ggml_compute_forward_pool_1d(params, tensor);
  10429. } break;
  10430. case GGML_OP_POOL_2D:
  10431. {
  10432. ggml_compute_forward_pool_2d(params, tensor);
  10433. } break;
  10434. case GGML_OP_POOL_2D_BACK:
  10435. {
  10436. ggml_compute_forward_pool_2d_back(params, tensor);
  10437. } break;
  10438. case GGML_OP_UPSCALE:
  10439. {
  10440. ggml_compute_forward_upscale(params, tensor);
  10441. } break;
  10442. case GGML_OP_PAD:
  10443. {
  10444. ggml_compute_forward_pad(params, tensor);
  10445. } break;
  10446. case GGML_OP_PAD_REFLECT_1D:
  10447. {
  10448. ggml_compute_forward_pad_reflect_1d(params, tensor);
  10449. } break;
  10450. case GGML_OP_ARANGE:
  10451. {
  10452. ggml_compute_forward_arange(params, tensor);
  10453. } break;
  10454. case GGML_OP_TIMESTEP_EMBEDDING:
  10455. {
  10456. ggml_compute_forward_timestep_embedding(params, tensor);
  10457. } break;
  10458. case GGML_OP_ARGSORT:
  10459. {
  10460. ggml_compute_forward_argsort(params, tensor);
  10461. } break;
  10462. case GGML_OP_LEAKY_RELU:
  10463. {
  10464. ggml_compute_forward_leaky_relu(params, tensor);
  10465. } break;
  10466. case GGML_OP_FLASH_ATTN_EXT:
  10467. {
  10468. ggml_compute_forward_flash_attn_ext(params, tensor->src[0], tensor->src[1], tensor->src[2], tensor->src[3], tensor);
  10469. } break;
  10470. case GGML_OP_FLASH_ATTN_BACK:
  10471. {
  10472. int32_t t = ggml_get_op_params_i32(tensor, 0);
  10473. GGML_ASSERT(t == 0 || t == 1);
  10474. bool masked = t != 0;
  10475. ggml_compute_forward_flash_attn_back(params, masked, tensor);
  10476. } break;
  10477. case GGML_OP_SSM_CONV:
  10478. {
  10479. ggml_compute_forward_ssm_conv(params, tensor);
  10480. } break;
  10481. case GGML_OP_SSM_SCAN:
  10482. {
  10483. ggml_compute_forward_ssm_scan(params, tensor);
  10484. } break;
  10485. case GGML_OP_WIN_PART:
  10486. {
  10487. ggml_compute_forward_win_part(params, tensor);
  10488. } break;
  10489. case GGML_OP_WIN_UNPART:
  10490. {
  10491. ggml_compute_forward_win_unpart(params, tensor);
  10492. } break;
  10493. case GGML_OP_UNARY:
  10494. {
  10495. ggml_compute_forward_unary(params, tensor);
  10496. } break;
  10497. case GGML_OP_GET_REL_POS:
  10498. {
  10499. ggml_compute_forward_get_rel_pos(params, tensor);
  10500. } break;
  10501. case GGML_OP_ADD_REL_POS:
  10502. {
  10503. ggml_compute_forward_add_rel_pos(params, tensor);
  10504. } break;
  10505. case GGML_OP_RWKV_WKV6:
  10506. {
  10507. ggml_compute_forward_rwkv_wkv6(params, tensor);
  10508. } break;
  10509. case GGML_OP_MAP_UNARY:
  10510. {
  10511. ggml_unary_op_f32_t fun;
  10512. memcpy(&fun, tensor->op_params, sizeof(fun));
  10513. ggml_compute_forward_map_unary(params, tensor, fun);
  10514. }
  10515. break;
  10516. case GGML_OP_MAP_BINARY:
  10517. {
  10518. ggml_binary_op_f32_t fun;
  10519. memcpy(&fun, tensor->op_params, sizeof(fun));
  10520. ggml_compute_forward_map_binary(params, tensor, fun);
  10521. }
  10522. break;
  10523. case GGML_OP_MAP_CUSTOM1_F32:
  10524. {
  10525. ggml_custom1_op_f32_t fun;
  10526. memcpy(&fun, tensor->op_params, sizeof(fun));
  10527. ggml_compute_forward_map_custom1_f32(params, tensor, fun);
  10528. }
  10529. break;
  10530. case GGML_OP_MAP_CUSTOM2_F32:
  10531. {
  10532. ggml_custom2_op_f32_t fun;
  10533. memcpy(&fun, tensor->op_params, sizeof(fun));
  10534. ggml_compute_forward_map_custom2_f32(params, tensor, fun);
  10535. }
  10536. break;
  10537. case GGML_OP_MAP_CUSTOM3_F32:
  10538. {
  10539. ggml_custom3_op_f32_t fun;
  10540. memcpy(&fun, tensor->op_params, sizeof(fun));
  10541. ggml_compute_forward_map_custom3_f32(params, tensor, fun);
  10542. }
  10543. break;
  10544. case GGML_OP_MAP_CUSTOM1:
  10545. {
  10546. ggml_compute_forward_map_custom1(params, tensor);
  10547. }
  10548. break;
  10549. case GGML_OP_MAP_CUSTOM2:
  10550. {
  10551. ggml_compute_forward_map_custom2(params, tensor);
  10552. }
  10553. break;
  10554. case GGML_OP_MAP_CUSTOM3:
  10555. {
  10556. ggml_compute_forward_map_custom3(params, tensor);
  10557. }
  10558. break;
  10559. case GGML_OP_CROSS_ENTROPY_LOSS:
  10560. {
  10561. ggml_compute_forward_cross_entropy_loss(params, tensor);
  10562. }
  10563. break;
  10564. case GGML_OP_CROSS_ENTROPY_LOSS_BACK:
  10565. {
  10566. ggml_compute_forward_cross_entropy_loss_back(params, tensor);
  10567. }
  10568. break;
  10569. case GGML_OP_OPT_STEP_ADAMW:
  10570. {
  10571. ggml_compute_forward_opt_step_adamw(params, tensor);
  10572. }
  10573. break;
  10574. case GGML_OP_NONE:
  10575. {
  10576. // nop
  10577. } break;
  10578. case GGML_OP_COUNT:
  10579. {
  10580. GGML_ABORT("fatal error");
  10581. }
  10582. }
  10583. }
  10584. // Android's libc implementation "bionic" does not support setting affinity
  10585. #if defined(__gnu_linux__)
  10586. static void set_numa_thread_affinity(int thread_n) {
  10587. if (!ggml_is_numa()) {
  10588. return;
  10589. }
  10590. int node_num;
  10591. int rv;
  10592. size_t setsize = CPU_ALLOC_SIZE(g_state.numa.total_cpus);
  10593. switch(g_state.numa.numa_strategy) {
  10594. case GGML_NUMA_STRATEGY_DISTRIBUTE:
  10595. // run thread on node_num thread_n / (threads per node)
  10596. node_num = thread_n % g_state.numa.n_nodes;
  10597. break;
  10598. case GGML_NUMA_STRATEGY_ISOLATE:
  10599. // run thread on current_node
  10600. node_num = g_state.numa.current_node;
  10601. break;
  10602. case GGML_NUMA_STRATEGY_NUMACTL:
  10603. // use the cpuset that numactl gave us
  10604. rv = pthread_setaffinity_np(pthread_self(), setsize, &g_state.numa.cpuset);
  10605. if (rv) {
  10606. fprintf(stderr, "warning: pthread_setaffinity_np() failed: %s\n",strerror(rv));
  10607. }
  10608. return;
  10609. default:
  10610. return;
  10611. }
  10612. struct ggml_numa_node * node = &g_state.numa.nodes[node_num];
  10613. cpu_set_t * cpus = CPU_ALLOC(g_state.numa.total_cpus);
  10614. CPU_ZERO_S(setsize, cpus);
  10615. for (size_t i = 0; i < node->n_cpus; ++i) {
  10616. CPU_SET_S(node->cpus[i], setsize, cpus);
  10617. }
  10618. rv = pthread_setaffinity_np(pthread_self(), setsize, cpus);
  10619. if (rv) {
  10620. fprintf(stderr, "warning: pthread_setaffinity_np() failed: %s\n", strerror(rv));
  10621. }
  10622. CPU_FREE(cpus);
  10623. }
  10624. static void clear_numa_thread_affinity(void) {
  10625. if (!ggml_is_numa()) {
  10626. return;
  10627. }
  10628. size_t setsize = CPU_ALLOC_SIZE(g_state.numa.total_cpus);
  10629. cpu_set_t * cpus = CPU_ALLOC(g_state.numa.total_cpus);
  10630. CPU_ZERO_S(setsize, cpus);
  10631. for (unsigned i = 0; i < g_state.numa.total_cpus; ++i) {
  10632. CPU_SET_S(i, setsize, cpus);
  10633. }
  10634. int rv = pthread_setaffinity_np(pthread_self(), setsize, cpus);
  10635. if (rv) {
  10636. fprintf(stderr, "warning: pthread_setaffinity_np() failed: %s\n", strerror(rv));
  10637. }
  10638. CPU_FREE(cpus);
  10639. }
  10640. #else
  10641. // TODO: Windows etc.
  10642. // (the linux implementation may also work on BSD, someone should test)
  10643. static void set_numa_thread_affinity(int thread_n) { UNUSED(thread_n); }
  10644. static void clear_numa_thread_affinity(void) {}
  10645. #endif
  10646. static int ggml_get_n_tasks(struct ggml_tensor * node, int n_threads) {
  10647. int n_tasks = 0;
  10648. if (ggml_is_empty(node)) {
  10649. // no need to multi-thread a no-op
  10650. n_tasks = 1;
  10651. return n_tasks;
  10652. }
  10653. switch (node->op) {
  10654. case GGML_OP_CPY:
  10655. case GGML_OP_DUP:
  10656. case GGML_OP_CONT:
  10657. case GGML_OP_ADD:
  10658. case GGML_OP_ADD1:
  10659. case GGML_OP_ACC:
  10660. {
  10661. n_tasks = n_threads;
  10662. } break;
  10663. case GGML_OP_SUB:
  10664. case GGML_OP_SQR:
  10665. case GGML_OP_SQRT:
  10666. case GGML_OP_LOG:
  10667. case GGML_OP_SIN:
  10668. case GGML_OP_COS:
  10669. case GGML_OP_SUM:
  10670. case GGML_OP_SUM_ROWS:
  10671. case GGML_OP_MEAN:
  10672. case GGML_OP_ARGMAX:
  10673. {
  10674. n_tasks = 1;
  10675. } break;
  10676. case GGML_OP_COUNT_EQUAL:
  10677. {
  10678. n_tasks = n_threads;
  10679. } break;
  10680. case GGML_OP_REPEAT:
  10681. case GGML_OP_REPEAT_BACK:
  10682. case GGML_OP_LEAKY_RELU:
  10683. {
  10684. n_tasks = 1;
  10685. } break;
  10686. case GGML_OP_UNARY:
  10687. switch (ggml_get_unary_op(node)) {
  10688. case GGML_UNARY_OP_ABS:
  10689. case GGML_UNARY_OP_SGN:
  10690. case GGML_UNARY_OP_NEG:
  10691. case GGML_UNARY_OP_STEP:
  10692. case GGML_UNARY_OP_TANH:
  10693. case GGML_UNARY_OP_ELU:
  10694. case GGML_UNARY_OP_RELU:
  10695. case GGML_UNARY_OP_SIGMOID:
  10696. case GGML_UNARY_OP_HARDSWISH:
  10697. case GGML_UNARY_OP_HARDSIGMOID:
  10698. case GGML_UNARY_OP_EXP:
  10699. {
  10700. n_tasks = 1;
  10701. } break;
  10702. case GGML_UNARY_OP_GELU:
  10703. case GGML_UNARY_OP_GELU_QUICK:
  10704. case GGML_UNARY_OP_SILU:
  10705. {
  10706. n_tasks = n_threads;
  10707. } break;
  10708. default:
  10709. GGML_ABORT("fatal error");
  10710. }
  10711. break;
  10712. case GGML_OP_SILU_BACK:
  10713. case GGML_OP_MUL:
  10714. case GGML_OP_DIV:
  10715. case GGML_OP_NORM:
  10716. case GGML_OP_RMS_NORM:
  10717. case GGML_OP_RMS_NORM_BACK:
  10718. case GGML_OP_GROUP_NORM:
  10719. case GGML_OP_CONCAT:
  10720. case GGML_OP_MUL_MAT:
  10721. case GGML_OP_MUL_MAT_ID:
  10722. case GGML_OP_OUT_PROD:
  10723. {
  10724. n_tasks = n_threads;
  10725. } break;
  10726. case GGML_OP_GET_ROWS:
  10727. {
  10728. // FIXME: get_rows can use additional threads, but the cost of launching additional threads
  10729. // decreases performance with GPU offloading
  10730. //n_tasks = n_threads;
  10731. n_tasks = 1;
  10732. } break;
  10733. case GGML_OP_SCALE:
  10734. case GGML_OP_SET:
  10735. case GGML_OP_RESHAPE:
  10736. case GGML_OP_VIEW:
  10737. case GGML_OP_PERMUTE:
  10738. case GGML_OP_TRANSPOSE:
  10739. case GGML_OP_GET_ROWS_BACK:
  10740. case GGML_OP_DIAG:
  10741. {
  10742. n_tasks = 1;
  10743. } break;
  10744. case GGML_OP_DIAG_MASK_ZERO:
  10745. case GGML_OP_DIAG_MASK_INF:
  10746. case GGML_OP_SOFT_MAX_BACK:
  10747. case GGML_OP_ROPE:
  10748. case GGML_OP_ROPE_BACK:
  10749. case GGML_OP_ADD_REL_POS:
  10750. {
  10751. n_tasks = n_threads;
  10752. } break;
  10753. case GGML_OP_CLAMP:
  10754. {
  10755. n_tasks = 1; //TODO
  10756. } break;
  10757. case GGML_OP_SOFT_MAX:
  10758. {
  10759. n_tasks = MIN(n_threads, ggml_nrows(node->src[0]));
  10760. } break;
  10761. case GGML_OP_IM2COL:
  10762. case GGML_OP_IM2COL_BACK:
  10763. case GGML_OP_CONV_TRANSPOSE_1D:
  10764. case GGML_OP_CONV_TRANSPOSE_2D:
  10765. {
  10766. n_tasks = n_threads;
  10767. } break;
  10768. case GGML_OP_POOL_1D:
  10769. case GGML_OP_POOL_2D:
  10770. case GGML_OP_POOL_2D_BACK:
  10771. {
  10772. n_tasks = 1;
  10773. } break;
  10774. case GGML_OP_UPSCALE:
  10775. case GGML_OP_PAD:
  10776. case GGML_OP_PAD_REFLECT_1D:
  10777. case GGML_OP_ARANGE:
  10778. case GGML_OP_TIMESTEP_EMBEDDING:
  10779. case GGML_OP_ARGSORT:
  10780. case GGML_OP_FLASH_ATTN_EXT:
  10781. case GGML_OP_FLASH_ATTN_BACK:
  10782. case GGML_OP_SSM_CONV:
  10783. case GGML_OP_SSM_SCAN:
  10784. {
  10785. n_tasks = n_threads;
  10786. } break;
  10787. case GGML_OP_WIN_PART:
  10788. case GGML_OP_WIN_UNPART:
  10789. case GGML_OP_GET_REL_POS:
  10790. case GGML_OP_RWKV_WKV6:
  10791. case GGML_OP_MAP_UNARY:
  10792. case GGML_OP_MAP_BINARY:
  10793. case GGML_OP_MAP_CUSTOM1_F32:
  10794. case GGML_OP_MAP_CUSTOM2_F32:
  10795. case GGML_OP_MAP_CUSTOM3_F32:
  10796. {
  10797. n_tasks = 1;
  10798. } break;
  10799. case GGML_OP_MAP_CUSTOM1:
  10800. {
  10801. struct ggml_map_custom1_op_params p;
  10802. memcpy(&p, node->op_params, sizeof(p));
  10803. if (p.n_tasks == GGML_N_TASKS_MAX) {
  10804. n_tasks = n_threads;
  10805. } else {
  10806. n_tasks = MIN(p.n_tasks, n_threads);
  10807. }
  10808. } break;
  10809. case GGML_OP_MAP_CUSTOM2:
  10810. {
  10811. struct ggml_map_custom2_op_params p;
  10812. memcpy(&p, node->op_params, sizeof(p));
  10813. if (p.n_tasks == GGML_N_TASKS_MAX) {
  10814. n_tasks = n_threads;
  10815. } else {
  10816. n_tasks = MIN(p.n_tasks, n_threads);
  10817. }
  10818. } break;
  10819. case GGML_OP_MAP_CUSTOM3:
  10820. {
  10821. struct ggml_map_custom3_op_params p;
  10822. memcpy(&p, node->op_params, sizeof(p));
  10823. if (p.n_tasks == GGML_N_TASKS_MAX) {
  10824. n_tasks = n_threads;
  10825. } else {
  10826. n_tasks = MIN(p.n_tasks, n_threads);
  10827. }
  10828. } break;
  10829. case GGML_OP_CROSS_ENTROPY_LOSS:
  10830. case GGML_OP_CROSS_ENTROPY_LOSS_BACK:
  10831. case GGML_OP_OPT_STEP_ADAMW:
  10832. {
  10833. n_tasks = n_threads;
  10834. } break;
  10835. case GGML_OP_NONE:
  10836. {
  10837. n_tasks = 1;
  10838. } break;
  10839. case GGML_OP_COUNT:
  10840. {
  10841. GGML_ABORT("fatal error");
  10842. }
  10843. default:
  10844. {
  10845. fprintf(stderr, "%s: op not implemented: ", __func__);
  10846. if (node->op < GGML_OP_COUNT) {
  10847. fprintf(stderr, "%s\n", ggml_op_name(node->op));
  10848. } else {
  10849. fprintf(stderr, "%d\n", node->op);
  10850. }
  10851. GGML_ABORT("fatal error");
  10852. }
  10853. }
  10854. assert(n_tasks > 0);
  10855. return n_tasks;
  10856. }
  10857. static thread_ret_t ggml_graph_compute_secondary_thread(void* data);
  10858. #if defined(_WIN32)
  10859. #include "windows.h"
  10860. // TODO: support > 64 CPUs
  10861. static bool ggml_thread_apply_affinity(bool * mask) {
  10862. HANDLE h = GetCurrentThread();
  10863. uint64_t bitmask = 0ULL;
  10864. assert(GGML_MAX_N_THREADS >= 64);
  10865. for (int32_t i = 0; i < 8; i++) {
  10866. int32_t idx = i * 8;
  10867. uint8_t val = 0;
  10868. val |= mask[idx + 0] << 0;
  10869. val |= mask[idx + 1] << 1;
  10870. val |= mask[idx + 2] << 2;
  10871. val |= mask[idx + 3] << 3;
  10872. val |= mask[idx + 4] << 4;
  10873. val |= mask[idx + 5] << 5;
  10874. val |= mask[idx + 6] << 6;
  10875. val |= mask[idx + 7] << 7;
  10876. bitmask |= (uint64_t)val << idx;
  10877. }
  10878. for (int32_t i = 64; i < GGML_MAX_N_THREADS; i++) {
  10879. if (mask[i]) {
  10880. fprintf(stderr, "warn: setting thread-affinity for > 64 CPUs isn't supported on windows!\n");
  10881. break;
  10882. }
  10883. }
  10884. DWORD_PTR m = (DWORD_PTR)bitmask;
  10885. m = SetThreadAffinityMask(h, m);
  10886. return m != 0;
  10887. }
  10888. static bool ggml_thread_apply_priority(int32_t prio) {
  10889. // Note that on Windows the Process Priority Class must be updated in order to set Thread priority.
  10890. // This is up to the applications.
  10891. DWORD p = THREAD_PRIORITY_NORMAL;
  10892. switch (prio) {
  10893. case GGML_SCHED_PRIO_NORMAL: p = THREAD_PRIORITY_NORMAL; break;
  10894. case GGML_SCHED_PRIO_MEDIUM: p = THREAD_PRIORITY_ABOVE_NORMAL; break;
  10895. case GGML_SCHED_PRIO_HIGH: p = THREAD_PRIORITY_HIGHEST; break;
  10896. case GGML_SCHED_PRIO_REALTIME: p = THREAD_PRIORITY_TIME_CRITICAL; break;
  10897. }
  10898. if (prio == GGML_SCHED_PRIO_NORMAL) {
  10899. // Keep inherited policy/priority
  10900. return true;
  10901. }
  10902. if (!SetThreadPriority(GetCurrentThread(), p)) {
  10903. fprintf(stderr, "warn: failed to set thread priority %d : (%d)\n", prio, (int) GetLastError());
  10904. return false;
  10905. }
  10906. return true;
  10907. }
  10908. #elif defined(__APPLE__)
  10909. #include <sys/types.h>
  10910. #include <sys/resource.h>
  10911. static bool ggml_thread_apply_affinity(const bool * mask) {
  10912. // Not supported on Apple platforms
  10913. UNUSED(mask);
  10914. return true;
  10915. }
  10916. static bool ggml_thread_apply_priority(int32_t prio) {
  10917. struct sched_param p;
  10918. int32_t policy = SCHED_OTHER;
  10919. switch (prio) {
  10920. case GGML_SCHED_PRIO_NORMAL: policy = SCHED_OTHER; p.sched_priority = 0; break;
  10921. case GGML_SCHED_PRIO_MEDIUM: policy = SCHED_FIFO; p.sched_priority = 40; break;
  10922. case GGML_SCHED_PRIO_HIGH: policy = SCHED_FIFO; p.sched_priority = 80; break;
  10923. case GGML_SCHED_PRIO_REALTIME: policy = SCHED_FIFO; p.sched_priority = 90; break;
  10924. }
  10925. if (prio == GGML_SCHED_PRIO_NORMAL) {
  10926. // Keep inherited policy/priority
  10927. return true;
  10928. }
  10929. int32_t err = pthread_setschedparam(pthread_self(), policy, &p);
  10930. if (err != 0) {
  10931. fprintf(stderr, "warn: failed to set thread priority %d : %s (%d)\n", prio, strerror(err), err);
  10932. return false;
  10933. }
  10934. return true;
  10935. }
  10936. #elif defined(__gnu_linux__)
  10937. // TODO: this may not work on BSD, to be verified
  10938. static bool ggml_thread_apply_affinity(const bool * mask) {
  10939. cpu_set_t cpuset;
  10940. int err;
  10941. CPU_ZERO(&cpuset);
  10942. for (uint32_t i = 0; i < GGML_MAX_N_THREADS; i++) {
  10943. if (mask[i]) {
  10944. GGML_PRINT_DEBUG("Thread %lx: adding %d to cpuset\n", pthread_self(), i);
  10945. CPU_SET(i, &cpuset);
  10946. }
  10947. }
  10948. #ifdef __ANDROID__
  10949. err = sched_setaffinity(0, sizeof(cpuset), &cpuset);
  10950. if (err < 0) {
  10951. err = errno;
  10952. }
  10953. #else
  10954. err = pthread_setaffinity_np(pthread_self(), sizeof(cpuset), &cpuset);
  10955. #endif
  10956. if (err != 0) {
  10957. fprintf(stderr, "warn: failed to set affinity mask 0x%llx : %s (%d)\n", (unsigned long long)mask, strerror(err), err);
  10958. return false;
  10959. }
  10960. return true;
  10961. }
  10962. static bool ggml_thread_apply_priority(int32_t prio) {
  10963. struct sched_param p;
  10964. int32_t policy = SCHED_OTHER;
  10965. switch (prio) {
  10966. case GGML_SCHED_PRIO_NORMAL: policy = SCHED_OTHER; p.sched_priority = 0; break;
  10967. case GGML_SCHED_PRIO_MEDIUM: policy = SCHED_FIFO; p.sched_priority = 40; break;
  10968. case GGML_SCHED_PRIO_HIGH: policy = SCHED_FIFO; p.sched_priority = 80; break;
  10969. case GGML_SCHED_PRIO_REALTIME: policy = SCHED_FIFO; p.sched_priority = 90; break;
  10970. }
  10971. if (prio == GGML_SCHED_PRIO_NORMAL) {
  10972. // Keep inherited policy/priority
  10973. return true;
  10974. }
  10975. int32_t err = pthread_setschedparam(pthread_self(), policy, &p);
  10976. if (err != 0) {
  10977. fprintf(stderr, "warn: failed to set thread priority %d : %s (%d)\n", prio, strerror(err), err);
  10978. return false;
  10979. }
  10980. return true;
  10981. }
  10982. #else // unsupported platforms
  10983. static bool ggml_thread_apply_affinity(const bool * mask) {
  10984. UNUSED(mask);
  10985. return true;
  10986. }
  10987. static bool ggml_thread_apply_priority(int32_t prio) {
  10988. UNUSED(prio);
  10989. return true;
  10990. }
  10991. #endif
  10992. static bool ggml_thread_cpumask_is_valid(const bool * mask) {
  10993. for (int i = 0; i < GGML_MAX_N_THREADS; i++) {
  10994. if (mask[i]) { return true; }
  10995. }
  10996. return false;
  10997. }
  10998. static void ggml_thread_cpumask_next(const bool * global_mask, bool * local_mask, bool strict, int32_t* iter) {
  10999. if (!strict) {
  11000. memcpy(local_mask, global_mask, GGML_MAX_N_THREADS);
  11001. return;
  11002. } else {
  11003. memset(local_mask, 0, GGML_MAX_N_THREADS);
  11004. int32_t base_idx = *iter;
  11005. for (int32_t i = 0; i < GGML_MAX_N_THREADS; i++) {
  11006. int32_t idx = base_idx + i;
  11007. if (idx >= GGML_MAX_N_THREADS) {
  11008. // Just a cheaper modulo
  11009. idx -= GGML_MAX_N_THREADS;
  11010. }
  11011. if (global_mask[idx]) {
  11012. local_mask[idx] = 1;
  11013. *iter = idx + 1;
  11014. return;
  11015. }
  11016. }
  11017. }
  11018. }
  11019. void ggml_threadpool_free(struct ggml_threadpool* threadpool) {
  11020. if (!threadpool) return;
  11021. const int n_threads = threadpool->n_threads_max;
  11022. #ifndef GGML_USE_OPENMP
  11023. struct ggml_compute_state* workers = threadpool->workers;
  11024. ggml_mutex_lock(&threadpool->mutex);
  11025. threadpool->stop = true;
  11026. threadpool->pause = false;
  11027. ggml_cond_broadcast(&threadpool->cond);
  11028. ggml_mutex_unlock(&threadpool->mutex);
  11029. for (int j = 1; j < n_threads; j++) {
  11030. int32_t rc = ggml_thread_join(workers[j].thrd, NULL);
  11031. GGML_ASSERT(rc == GGML_EXIT_SUCCESS || rc == GGML_EXIT_ABORTED);
  11032. UNUSED(rc);
  11033. }
  11034. ggml_mutex_destroy(&threadpool->mutex);
  11035. ggml_cond_destroy(&threadpool->cond);
  11036. #endif // GGML_USE_OPENMP
  11037. const size_t workers_size = sizeof(struct ggml_compute_state) * n_threads;
  11038. ggml_aligned_free(threadpool->workers, workers_size);
  11039. ggml_aligned_free(threadpool, sizeof(struct ggml_threadpool));
  11040. }
  11041. #ifndef GGML_USE_OPENMP
  11042. // pause/resume must be called under mutex
  11043. static void ggml_threadpool_pause_locked(struct ggml_threadpool * threadpool) {
  11044. GGML_PRINT_DEBUG("Pausing threadpool\n");
  11045. threadpool->pause = true;
  11046. ggml_cond_broadcast(&threadpool->cond);
  11047. }
  11048. static void ggml_threadpool_resume_locked(struct ggml_threadpool * threadpool) {
  11049. GGML_PRINT_DEBUG("Resuming threadpool\n");
  11050. threadpool->pause = false;
  11051. ggml_cond_broadcast(&threadpool->cond);
  11052. }
  11053. #endif
  11054. void ggml_threadpool_pause(struct ggml_threadpool * threadpool) {
  11055. #ifndef GGML_USE_OPENMP
  11056. ggml_mutex_lock(&threadpool->mutex);
  11057. if (!threadpool->pause) {
  11058. ggml_threadpool_pause_locked(threadpool);
  11059. }
  11060. ggml_mutex_unlock(&threadpool->mutex);
  11061. #else
  11062. UNUSED(threadpool);
  11063. #endif
  11064. }
  11065. void ggml_threadpool_resume(struct ggml_threadpool * threadpool) {
  11066. #ifndef GGML_USE_OPENMP
  11067. ggml_mutex_lock(&threadpool->mutex);
  11068. if (threadpool->pause) {
  11069. ggml_threadpool_resume_locked(threadpool);
  11070. }
  11071. ggml_mutex_unlock(&threadpool->mutex);
  11072. #else
  11073. UNUSED(threadpool);
  11074. #endif
  11075. }
  11076. struct ggml_cplan ggml_graph_plan(
  11077. const struct ggml_cgraph * cgraph,
  11078. int n_threads,
  11079. struct ggml_threadpool * threadpool) {
  11080. if (threadpool == NULL) {
  11081. //GGML_PRINT_DEBUG("Threadpool is not specified. Will create a disposable threadpool : n_threads %d\n", n_threads);
  11082. }
  11083. if (n_threads <= 0) {
  11084. n_threads = threadpool ? threadpool->n_threads_max : GGML_DEFAULT_N_THREADS;
  11085. }
  11086. size_t work_size = 0;
  11087. struct ggml_cplan cplan;
  11088. memset(&cplan, 0, sizeof(struct ggml_cplan));
  11089. int max_tasks = 1;
  11090. // thread scheduling for the different operations + work buffer size estimation
  11091. for (int i = 0; i < cgraph->n_nodes; i++) {
  11092. struct ggml_tensor * node = cgraph->nodes[i];
  11093. const int n_tasks = ggml_get_n_tasks(node, n_threads);
  11094. max_tasks = MAX(max_tasks, n_tasks);
  11095. size_t cur = 0;
  11096. if (!ggml_cpu_extra_work_size(n_threads, node, &cur)) {
  11097. switch (node->op) {
  11098. case GGML_OP_CPY:
  11099. case GGML_OP_DUP:
  11100. {
  11101. if (ggml_is_quantized(node->type) ||
  11102. // F16 -> BF16 and BF16 -> F16 copies go through intermediate F32
  11103. (node->src[0]->type == GGML_TYPE_F16 && node->src[1] && node->src[1]->type == GGML_TYPE_BF16) ||
  11104. (node->src[0]->type == GGML_TYPE_BF16 && node->src[1] && node->src[1]->type == GGML_TYPE_F16)) {
  11105. cur = ggml_type_size(GGML_TYPE_F32) * node->ne[0] * n_tasks;
  11106. }
  11107. } break;
  11108. case GGML_OP_ADD:
  11109. case GGML_OP_ADD1:
  11110. {
  11111. if (ggml_is_quantized(node->src[0]->type)) {
  11112. cur = ggml_type_size(GGML_TYPE_F32) * node->src[0]->ne[0] * n_tasks;
  11113. }
  11114. } break;
  11115. case GGML_OP_ACC:
  11116. {
  11117. if (ggml_is_quantized(node->src[0]->type)) {
  11118. cur = ggml_type_size(GGML_TYPE_F32) * node->src[1]->ne[0] * n_tasks;
  11119. }
  11120. } break;
  11121. case GGML_OP_COUNT_EQUAL:
  11122. {
  11123. cur = ggml_type_size(node->type)*n_tasks;
  11124. } break;
  11125. case GGML_OP_MUL_MAT:
  11126. {
  11127. const enum ggml_type vec_dot_type = type_traits_cpu[node->src[0]->type].vec_dot_type;
  11128. if (node->src[1]->type != vec_dot_type) {
  11129. cur = ggml_row_size(vec_dot_type, ggml_nelements(node->src[1]));
  11130. }
  11131. } break;
  11132. case GGML_OP_MUL_MAT_ID:
  11133. {
  11134. cur = 0;
  11135. const struct ggml_tensor * src0 = node->src[0];
  11136. const struct ggml_tensor * src1 = node->src[1];
  11137. const enum ggml_type vec_dot_type = type_traits_cpu[src0->type].vec_dot_type;
  11138. if (src1->type != vec_dot_type) {
  11139. cur += ggml_row_size(vec_dot_type, ggml_nelements(src1));
  11140. }
  11141. const int n_as = src0->ne[2];
  11142. cur += GGML_PAD(cur, sizeof(int64_t)); // align
  11143. cur += n_as * sizeof(int64_t); // matrix_row_counts
  11144. cur += n_as * src1->ne[2] * sizeof(int64_t); // matrix_rows
  11145. } break;
  11146. case GGML_OP_OUT_PROD:
  11147. {
  11148. if (ggml_is_quantized(node->src[0]->type)) {
  11149. cur = ggml_type_size(GGML_TYPE_F32) * node->src[0]->ne[0] * n_tasks;
  11150. }
  11151. } break;
  11152. case GGML_OP_SOFT_MAX:
  11153. case GGML_OP_ROPE:
  11154. {
  11155. cur = ggml_type_size(GGML_TYPE_F32) * node->ne[0] * n_tasks;
  11156. } break;
  11157. case GGML_OP_CONV_TRANSPOSE_1D:
  11158. {
  11159. GGML_ASSERT(node->src[0]->ne[3] == 1);
  11160. GGML_ASSERT(node->src[1]->ne[2] == 1);
  11161. GGML_ASSERT(node->src[1]->ne[3] == 1);
  11162. const int64_t ne00 = node->src[0]->ne[0]; // K
  11163. const int64_t ne01 = node->src[0]->ne[1]; // Cout
  11164. const int64_t ne02 = node->src[0]->ne[2]; // Cin
  11165. const int64_t ne10 = node->src[1]->ne[0]; // L
  11166. const int64_t ne11 = node->src[1]->ne[1]; // Cin
  11167. if ((node->src[0]->type == GGML_TYPE_F16 ||
  11168. node->src[0]->type == GGML_TYPE_BF16) &&
  11169. node->src[1]->type == GGML_TYPE_F32) {
  11170. cur += sizeof(ggml_fp16_t)*ne00*ne01*ne02;
  11171. cur += sizeof(ggml_fp16_t)*ne10*ne11;
  11172. } else if (node->src[0]->type == GGML_TYPE_F32 &&
  11173. node->src[1]->type == GGML_TYPE_F32) {
  11174. cur += sizeof(float)*ne00*ne01*ne02;
  11175. cur += sizeof(float)*ne10*ne11;
  11176. } else {
  11177. GGML_ABORT("fatal error");
  11178. }
  11179. } break;
  11180. case GGML_OP_CONV_TRANSPOSE_2D:
  11181. {
  11182. const int64_t ne00 = node->src[0]->ne[0]; // W
  11183. const int64_t ne01 = node->src[0]->ne[1]; // H
  11184. const int64_t ne02 = node->src[0]->ne[2]; // Channels Out
  11185. const int64_t ne03 = node->src[0]->ne[3]; // Channels In
  11186. const int64_t ne10 = node->src[1]->ne[0]; // W
  11187. const int64_t ne11 = node->src[1]->ne[1]; // H
  11188. const int64_t ne12 = node->src[1]->ne[2]; // Channels In
  11189. cur += sizeof(ggml_fp16_t)*ne00*ne01*ne02*ne03;
  11190. cur += sizeof(ggml_fp16_t)*ne10*ne11*ne12;
  11191. } break;
  11192. case GGML_OP_FLASH_ATTN_EXT:
  11193. {
  11194. const int64_t ne00 = node->src[0]->ne[0]; // D
  11195. cur = 3*sizeof(float)*ne00*n_tasks; // 3x head size/thread
  11196. } break;
  11197. case GGML_OP_FLASH_ATTN_BACK:
  11198. {
  11199. const int64_t D = node->src[0]->ne[0];
  11200. const int64_t ne11 = ggml_up(node->src[1]->ne[1], GGML_SOFT_MAX_UNROLL);
  11201. const int64_t mxDn = MAX(D, ne11) * 2; // *2 because of S and SM in ggml_compute_forward_flash_attn_back
  11202. if (node->src[1]->type == GGML_TYPE_F32) {
  11203. cur = sizeof(float)*mxDn*n_tasks; // TODO: this can become (n_tasks-1)
  11204. cur += sizeof(float)*mxDn*n_tasks; // this is overestimated by x2
  11205. } else if (node->src[1]->type == GGML_TYPE_F16) {
  11206. cur = sizeof(float)*mxDn*n_tasks; // TODO: this can become (n_tasks-1)
  11207. cur += sizeof(float)*mxDn*n_tasks; // this is overestimated by x2
  11208. } else if (node->src[1]->type == GGML_TYPE_BF16) {
  11209. cur = sizeof(float)*mxDn*n_tasks; // TODO: this can become (n_tasks-1)
  11210. cur += sizeof(float)*mxDn*n_tasks; // this is overestimated by x2
  11211. }
  11212. } break;
  11213. case GGML_OP_CROSS_ENTROPY_LOSS:
  11214. {
  11215. cur = ggml_type_size(node->type)*(n_tasks + node->src[0]->ne[0]*n_tasks);
  11216. } break;
  11217. case GGML_OP_COUNT:
  11218. {
  11219. GGML_ABORT("fatal error");
  11220. }
  11221. default:
  11222. break;
  11223. }
  11224. }
  11225. work_size = MAX(work_size, cur);
  11226. }
  11227. if (work_size > 0) {
  11228. work_size += CACHE_LINE_SIZE*(n_threads);
  11229. }
  11230. cplan.threadpool = threadpool;
  11231. cplan.n_threads = MIN(max_tasks, n_threads);
  11232. cplan.work_size = work_size;
  11233. cplan.work_data = NULL;
  11234. return cplan;
  11235. }
  11236. static thread_ret_t ggml_graph_compute_thread(void * data) {
  11237. struct ggml_compute_state * state = (struct ggml_compute_state *) data;
  11238. struct ggml_threadpool * tp = state->threadpool;
  11239. const struct ggml_cgraph * cgraph = tp->cgraph;
  11240. const struct ggml_cplan * cplan = tp->cplan;
  11241. set_numa_thread_affinity(state->ith);
  11242. struct ggml_compute_params params = {
  11243. /*.ith =*/ state->ith,
  11244. /*.nth =*/ atomic_load_explicit(&tp->n_threads_cur, memory_order_relaxed),
  11245. /*.wsize =*/ cplan->work_size,
  11246. /*.wdata =*/ cplan->work_data,
  11247. /*.threadpool=*/ tp,
  11248. };
  11249. for (int node_n = 0; node_n < cgraph->n_nodes && !tp->abort; node_n++) {
  11250. struct ggml_tensor * node = cgraph->nodes[node_n];
  11251. ggml_compute_forward(&params, node);
  11252. if (state->ith == 0 && cplan->abort_callback &&
  11253. cplan->abort_callback(cplan->abort_callback_data)) {
  11254. tp->abort = true;
  11255. tp->ec = GGML_STATUS_ABORTED;
  11256. }
  11257. ggml_barrier(state->threadpool);
  11258. }
  11259. return 0;
  11260. }
  11261. #ifndef GGML_USE_OPENMP
  11262. // check if thread is active
  11263. static inline bool ggml_graph_compute_thread_active(struct ggml_compute_state * state) {
  11264. struct ggml_threadpool * threadpool = state->threadpool;
  11265. int n_threads = atomic_load_explicit(&threadpool->n_threads_cur, memory_order_relaxed);
  11266. return (state->ith < n_threads);
  11267. }
  11268. // check if thread is ready to proceed (exit from polling or sleeping)
  11269. static inline bool ggml_graph_compute_thread_ready(struct ggml_compute_state * state) {
  11270. struct ggml_threadpool * threadpool = state->threadpool;
  11271. if (state->pending || threadpool->stop || threadpool->pause) { return true; }
  11272. // check for new graph/work
  11273. int new_graph = atomic_load_explicit(&threadpool->n_graph, memory_order_relaxed);
  11274. if (new_graph != state->last_graph) {
  11275. state->pending = ggml_graph_compute_thread_active(state);
  11276. state->last_graph = new_graph;
  11277. }
  11278. return state->pending;
  11279. }
  11280. // sync thread state after polling
  11281. static inline void ggml_graph_compute_thread_sync(struct ggml_compute_state * state) {
  11282. // TSAN doesn't support standalone fence yet, we use a dummy read-modify-write instead
  11283. #ifdef GGML_TSAN_ENABLED
  11284. atomic_fetch_add_explicit(&state->threadpool->n_graph, 0, memory_order_seq_cst);
  11285. #else
  11286. atomic_thread_fence(memory_order_seq_cst);
  11287. #endif
  11288. UNUSED(state);
  11289. }
  11290. static inline bool ggml_graph_compute_poll_for_work(struct ggml_compute_state * state) {
  11291. struct ggml_threadpool * threadpool = state->threadpool;
  11292. // Skip polling for unused threads
  11293. if (!ggml_graph_compute_thread_active(state)) {
  11294. return state->pending;
  11295. }
  11296. // This seems to make 0 ... 100 a decent range for polling level across modern processors.
  11297. // Perhaps, we can adjust it dynamically based on load and things.
  11298. const uint64_t n_rounds = 1024UL * 128 * threadpool->poll;
  11299. for (uint64_t i=0; !ggml_graph_compute_thread_ready(state) && i < n_rounds; i++) {
  11300. // No new work. Keep polling.
  11301. ggml_thread_cpu_relax();
  11302. }
  11303. return state->pending;
  11304. }
  11305. static inline bool ggml_graph_compute_check_for_work(struct ggml_compute_state * state) {
  11306. struct ggml_threadpool * threadpool = state->threadpool;
  11307. if (ggml_graph_compute_poll_for_work(state)) {
  11308. ggml_graph_compute_thread_sync(state);
  11309. return state->pending;
  11310. }
  11311. ggml_mutex_lock_shared(&threadpool->mutex);
  11312. while (!ggml_graph_compute_thread_ready(state)) {
  11313. // No new work. Wait for the signal.
  11314. GGML_PRINT_DEBUG("thread #%d waiting for work (sleeping)\n", state->ith);
  11315. ggml_cond_wait(&threadpool->cond, &threadpool->mutex);
  11316. }
  11317. ggml_mutex_unlock_shared(&threadpool->mutex);
  11318. return state->pending;
  11319. }
  11320. static thread_ret_t ggml_graph_compute_secondary_thread(void* data) {
  11321. struct ggml_compute_state * state = (struct ggml_compute_state *) data;
  11322. struct ggml_threadpool * threadpool = state->threadpool;
  11323. ggml_thread_apply_priority(threadpool->prio);
  11324. if (ggml_thread_cpumask_is_valid(state->cpumask)) {
  11325. ggml_thread_apply_affinity(state->cpumask);
  11326. }
  11327. while (true) {
  11328. // Check if we need to sleep
  11329. while (threadpool->pause) {
  11330. GGML_PRINT_DEBUG("thread #%d inside pause loop\n", state->ith);
  11331. ggml_mutex_lock_shared(&threadpool->mutex);
  11332. if (threadpool->pause) {
  11333. ggml_cond_wait(&threadpool->cond, &threadpool->mutex);
  11334. }
  11335. GGML_PRINT_DEBUG("thread #%d resuming after wait\n", state->ith);
  11336. ggml_mutex_unlock_shared(&threadpool->mutex);
  11337. }
  11338. // This needs to be checked for after the cond_wait
  11339. if (threadpool->stop) break;
  11340. // Check if there is new work
  11341. // The main thread is the only one that can dispatch new work
  11342. ggml_graph_compute_check_for_work(state);
  11343. if (state->pending) {
  11344. state->pending = false;
  11345. ggml_graph_compute_thread(state);
  11346. }
  11347. }
  11348. return (thread_ret_t) 0;
  11349. }
  11350. // Start processing new graph
  11351. static void ggml_graph_compute_kickoff(struct ggml_threadpool * threadpool, int n_threads)
  11352. {
  11353. // Always take the mutex here because the worker threads are doing hybrid poll/wait
  11354. ggml_mutex_lock(&threadpool->mutex);
  11355. GGML_PRINT_DEBUG("threadpool: n_threads_cur %d n_threads %d\n", threadpool->n_threads_cur, n_threads);
  11356. // Update the number of active threads
  11357. atomic_store_explicit(&threadpool->n_threads_cur, n_threads, memory_order_relaxed);
  11358. // Indicate the graph is ready to be processed
  11359. // We need the full seq-cst fence here because of the polling threads (used in thread_sync)
  11360. atomic_fetch_add_explicit(&threadpool->n_graph, 1, memory_order_seq_cst);
  11361. if (threadpool->pause) {
  11362. // Update main thread prio and affinity to match the threadpool settings
  11363. ggml_thread_apply_priority(threadpool->prio);
  11364. if (ggml_thread_cpumask_is_valid(threadpool->workers[0].cpumask)) {
  11365. ggml_thread_apply_affinity(threadpool->workers[0].cpumask);
  11366. }
  11367. // resume does cond broadcast
  11368. ggml_threadpool_resume_locked(threadpool);
  11369. } else {
  11370. ggml_cond_broadcast(&threadpool->cond);
  11371. }
  11372. ggml_mutex_unlock(&threadpool->mutex);
  11373. }
  11374. #endif // GGML_USE_OPENMP
  11375. static struct ggml_threadpool * ggml_threadpool_new_impl(
  11376. struct ggml_threadpool_params * tpp,
  11377. struct ggml_cgraph * cgraph,
  11378. struct ggml_cplan * cplan) {
  11379. struct ggml_threadpool * threadpool =
  11380. ggml_aligned_malloc(sizeof(struct ggml_threadpool));
  11381. {
  11382. threadpool->cgraph = cgraph;
  11383. threadpool->cplan = cplan;
  11384. threadpool->n_graph = 0;
  11385. threadpool->n_barrier = 0;
  11386. threadpool->n_barrier_passed = 0;
  11387. threadpool->current_chunk = 0;
  11388. threadpool->stop = false;
  11389. threadpool->pause = tpp->paused;
  11390. threadpool->abort = false;
  11391. threadpool->workers = NULL;
  11392. threadpool->n_threads_max = tpp->n_threads;
  11393. threadpool->n_threads_cur = tpp->n_threads;
  11394. threadpool->poll = tpp->poll;
  11395. threadpool->prio = tpp->prio;
  11396. threadpool->ec = GGML_STATUS_SUCCESS;
  11397. }
  11398. // Allocate and init workers state
  11399. const size_t workers_size = sizeof(struct ggml_compute_state) * tpp->n_threads;
  11400. struct ggml_compute_state * workers = ggml_aligned_malloc(workers_size);
  11401. memset(workers, 0, workers_size);
  11402. for (int j = 0; j < tpp->n_threads; j++) {
  11403. workers[j].threadpool = threadpool;
  11404. workers[j].ith = j;
  11405. }
  11406. threadpool->workers = workers;
  11407. #ifndef GGML_USE_OPENMP
  11408. ggml_mutex_init(&threadpool->mutex);
  11409. ggml_cond_init(&threadpool->cond);
  11410. // Spin the threads for all workers, and update CPU placements.
  11411. // Place the main thread last (towards the higher numbered CPU cores).
  11412. int32_t cpumask_iter = 0;
  11413. for (int j = 1; j < tpp->n_threads; j++) {
  11414. ggml_thread_cpumask_next(tpp->cpumask, workers[j].cpumask, tpp->strict_cpu, &cpumask_iter);
  11415. int32_t rc = ggml_thread_create(&workers[j].thrd, NULL, ggml_graph_compute_secondary_thread, &workers[j]);
  11416. GGML_ASSERT(rc == 0);
  11417. }
  11418. ggml_thread_cpumask_next(tpp->cpumask, workers[0].cpumask, tpp->strict_cpu, &cpumask_iter);
  11419. if (!threadpool->pause) {
  11420. // Update main thread prio and affinity at the start, otherwise we'll do it in resume
  11421. ggml_thread_apply_priority(threadpool->prio);
  11422. if (ggml_thread_cpumask_is_valid(threadpool->workers[0].cpumask)) {
  11423. ggml_thread_apply_affinity(threadpool->workers[0].cpumask);
  11424. }
  11425. }
  11426. #endif // GGML_USE_OPENMP
  11427. return threadpool;
  11428. }
  11429. struct ggml_threadpool * ggml_threadpool_new(struct ggml_threadpool_params * tpp) {
  11430. return ggml_threadpool_new_impl(tpp, NULL, NULL);
  11431. }
  11432. enum ggml_status ggml_graph_compute(struct ggml_cgraph * cgraph, struct ggml_cplan * cplan) {
  11433. ggml_cpu_init();
  11434. GGML_ASSERT(cplan);
  11435. GGML_ASSERT(cplan->n_threads > 0);
  11436. GGML_ASSERT(cplan->work_size == 0 || cplan->work_data != NULL);
  11437. int n_threads = cplan->n_threads;
  11438. struct ggml_threadpool * threadpool = cplan->threadpool;
  11439. bool disposable_threadpool = false;
  11440. if (threadpool == NULL) {
  11441. //GGML_PRINT_DEBUG("Threadpool is not specified. Will create a disposable threadpool : n_threads %d\n", n_threads);
  11442. disposable_threadpool = true;
  11443. struct ggml_threadpool_params ttp = ggml_threadpool_params_default(n_threads);
  11444. threadpool = ggml_threadpool_new_impl(&ttp, cgraph, cplan);
  11445. } else {
  11446. // Reset some of the parameters that need resetting
  11447. // No worker threads should be accessing the parameters below at this stage
  11448. threadpool->cgraph = cgraph;
  11449. threadpool->cplan = cplan;
  11450. threadpool->current_chunk = 0;
  11451. threadpool->abort = false;
  11452. threadpool->ec = GGML_STATUS_SUCCESS;
  11453. }
  11454. #ifdef GGML_USE_OPENMP
  11455. if (n_threads > 1) {
  11456. #pragma omp parallel num_threads(n_threads)
  11457. {
  11458. #pragma omp single
  11459. {
  11460. // update the number of threads from the actual number of threads that we got from OpenMP
  11461. n_threads = omp_get_num_threads();
  11462. atomic_store_explicit(&threadpool->n_threads_cur, n_threads, memory_order_relaxed);
  11463. }
  11464. ggml_graph_compute_thread(&threadpool->workers[omp_get_thread_num()]);
  11465. }
  11466. } else {
  11467. atomic_store_explicit(&threadpool->n_threads_cur, 1, memory_order_relaxed);
  11468. ggml_graph_compute_thread(&threadpool->workers[0]);
  11469. }
  11470. #else
  11471. if (n_threads > threadpool->n_threads_max) {
  11472. GGML_LOG_WARN("cplan requested more threads (%d) than available (%d)\n", n_threads, threadpool->n_threads_max);
  11473. n_threads = threadpool->n_threads_max;
  11474. }
  11475. // Kick all threads to start the new graph
  11476. ggml_graph_compute_kickoff(threadpool, n_threads);
  11477. // This is a work thread too
  11478. ggml_graph_compute_thread(&threadpool->workers[0]);
  11479. #endif
  11480. // don't leave affinity set on the main thread
  11481. clear_numa_thread_affinity();
  11482. enum ggml_status ret = threadpool->ec;
  11483. if (disposable_threadpool) {
  11484. ggml_threadpool_free(threadpool);
  11485. }
  11486. return ret;
  11487. }
  11488. enum ggml_status ggml_graph_compute_with_ctx(struct ggml_context * ctx, struct ggml_cgraph * cgraph, int n_threads) {
  11489. struct ggml_cplan cplan = ggml_graph_plan(cgraph, n_threads, NULL);
  11490. cplan.work_data = (uint8_t *)ggml_new_buffer(ctx, cplan.work_size);
  11491. return ggml_graph_compute(cgraph, &cplan);
  11492. }
  11493. int ggml_cpu_has_avx(void) {
  11494. #if defined(__AVX__)
  11495. return 1;
  11496. #else
  11497. return 0;
  11498. #endif
  11499. }
  11500. int ggml_cpu_has_avx_vnni(void) {
  11501. #if defined(__AVXVNNI__)
  11502. return 1;
  11503. #else
  11504. return 0;
  11505. #endif
  11506. }
  11507. int ggml_cpu_has_avx2(void) {
  11508. #if defined(__AVX2__)
  11509. return 1;
  11510. #else
  11511. return 0;
  11512. #endif
  11513. }
  11514. int ggml_cpu_has_avx512(void) {
  11515. #if defined(__AVX512F__)
  11516. return 1;
  11517. #else
  11518. return 0;
  11519. #endif
  11520. }
  11521. int ggml_cpu_has_avx512_vbmi(void) {
  11522. #if defined(__AVX512VBMI__)
  11523. return 1;
  11524. #else
  11525. return 0;
  11526. #endif
  11527. }
  11528. int ggml_cpu_has_avx512_vnni(void) {
  11529. #if defined(__AVX512VNNI__)
  11530. return 1;
  11531. #else
  11532. return 0;
  11533. #endif
  11534. }
  11535. int ggml_cpu_has_avx512_bf16(void) {
  11536. #if defined(__AVX512BF16__)
  11537. return 1;
  11538. #else
  11539. return 0;
  11540. #endif
  11541. }
  11542. int ggml_cpu_has_amx_int8(void) {
  11543. #if defined(__AMX_INT8__)
  11544. return 1;
  11545. #else
  11546. return 0;
  11547. #endif
  11548. }
  11549. int ggml_cpu_has_fma(void) {
  11550. #if defined(__FMA__)
  11551. return 1;
  11552. #else
  11553. return 0;
  11554. #endif
  11555. }
  11556. int ggml_cpu_has_arm_fma(void) {
  11557. #if defined(__ARM_FEATURE_FMA)
  11558. return 1;
  11559. #else
  11560. return 0;
  11561. #endif
  11562. }
  11563. int ggml_cpu_has_riscv_v(void) {
  11564. #if defined(__riscv_v_intrinsic)
  11565. return 1;
  11566. #else
  11567. return 0;
  11568. #endif
  11569. }
  11570. int ggml_cpu_has_f16c(void) {
  11571. #if defined(__F16C__)
  11572. return 1;
  11573. #else
  11574. return 0;
  11575. #endif
  11576. }
  11577. int ggml_cpu_has_fp16_va(void) {
  11578. #if defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC)
  11579. return 1;
  11580. #else
  11581. return 0;
  11582. #endif
  11583. }
  11584. int ggml_cpu_has_wasm_simd(void) {
  11585. #if defined(__wasm_simd128__)
  11586. return 1;
  11587. #else
  11588. return 0;
  11589. #endif
  11590. }
  11591. int ggml_cpu_has_llamafile(void) {
  11592. #if defined(GGML_USE_LLAMAFILE)
  11593. return 1;
  11594. #else
  11595. return 0;
  11596. #endif
  11597. }
  11598. int ggml_cpu_has_sse3(void) {
  11599. #if defined(__SSE3__)
  11600. return 1;
  11601. #else
  11602. return 0;
  11603. #endif
  11604. }
  11605. int ggml_cpu_has_ssse3(void) {
  11606. #if defined(__SSSE3__)
  11607. return 1;
  11608. #else
  11609. return 0;
  11610. #endif
  11611. }
  11612. int ggml_cpu_has_vsx(void) {
  11613. #if defined(__POWER9_VECTOR__)
  11614. return 1;
  11615. #else
  11616. return 0;
  11617. #endif
  11618. }
  11619. int ggml_cpu_has_neon(void) {
  11620. #if defined(__ARM_ARCH) && defined(__ARM_NEON)
  11621. return ggml_arm_arch_features.has_neon;
  11622. #else
  11623. return 0;
  11624. #endif
  11625. }
  11626. int ggml_cpu_has_dotprod(void) {
  11627. #if defined(__ARM_ARCH) && defined(__ARM_FEATURE_DOTPROD)
  11628. return ggml_arm_arch_features.has_dotprod;
  11629. #else
  11630. return 0;
  11631. #endif
  11632. }
  11633. int ggml_cpu_has_sve(void) {
  11634. #if defined(__ARM_ARCH) && defined(__ARM_FEATURE_SVE)
  11635. return ggml_arm_arch_features.has_sve;
  11636. #else
  11637. return 0;
  11638. #endif
  11639. }
  11640. int ggml_cpu_has_matmul_int8(void) {
  11641. #if defined(__ARM_ARCH) && defined(__ARM_FEATURE_MATMUL_INT8)
  11642. return ggml_arm_arch_features.has_i8mm;
  11643. #else
  11644. return 0;
  11645. #endif
  11646. }
  11647. int ggml_cpu_get_sve_cnt(void) {
  11648. #if defined(__ARM_ARCH) && defined(__ARM_FEATURE_SVE)
  11649. return ggml_arm_arch_features.sve_cnt;
  11650. #else
  11651. return 0;
  11652. #endif
  11653. }
  11654. void ggml_cpu_init(void) {
  11655. // needed to initialize f16 tables
  11656. {
  11657. struct ggml_init_params params = { 0, NULL, false };
  11658. struct ggml_context * ctx = ggml_init(params);
  11659. ggml_free(ctx);
  11660. }
  11661. ggml_critical_section_start();
  11662. static bool is_first_call = true;
  11663. if (is_first_call) {
  11664. // initialize GELU, Quick GELU, SILU and EXP F32 tables
  11665. {
  11666. const uint64_t t_start = ggml_time_us(); UNUSED(t_start);
  11667. for (int i = 0; i < (1 << 16); ++i) {
  11668. union {
  11669. uint16_t u16;
  11670. ggml_fp16_t fp16;
  11671. } u = {i};
  11672. float f = GGML_FP16_TO_FP32(u.fp16);
  11673. ggml_table_gelu_f16[i] = GGML_FP32_TO_FP16(ggml_gelu_f32(f));
  11674. ggml_table_gelu_quick_f16[i] = GGML_FP32_TO_FP16(ggml_gelu_quick_f32(f));
  11675. }
  11676. const uint64_t t_end = ggml_time_us(); UNUSED(t_end);
  11677. GGML_PRINT_DEBUG("%s: GELU, Quick GELU, SILU and EXP tables initialized in %f ms\n", __func__, (t_end - t_start)/1000.0);
  11678. }
  11679. #if defined(__ARM_ARCH)
  11680. ggml_init_arm_arch_features();
  11681. #endif
  11682. is_first_call = false;
  11683. }
  11684. ggml_critical_section_end();
  11685. }