llama-model.cpp 816 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822
  1. #include "llama-model.h"
  2. #include "llama-impl.h"
  3. #include "llama-mmap.h"
  4. #include "llama-batch.h"
  5. #include "llama-cparams.h"
  6. #include "llama-model-loader.h"
  7. #include "llama-kv-cache-unified.h"
  8. #include "llama-kv-cache-unified-iswa.h"
  9. #include "llama-memory-hybrid.h"
  10. #include "llama-memory-recurrent.h"
  11. #include "ggml-cpp.h"
  12. #include <algorithm>
  13. #include <cassert>
  14. #include <cmath>
  15. #include <cfloat>
  16. #include <cstring>
  17. #include <cmath>
  18. #include <functional>
  19. #include <map>
  20. #include <regex>
  21. #include <sstream>
  22. #include <stdexcept>
  23. const char * llm_type_name(llm_type type) {
  24. switch (type) {
  25. case LLM_TYPE_14M: return "14M";
  26. case LLM_TYPE_17M: return "17M";
  27. case LLM_TYPE_22M: return "22M";
  28. case LLM_TYPE_33M: return "33M";
  29. case LLM_TYPE_60M: return "60M";
  30. case LLM_TYPE_70M: return "70M";
  31. case LLM_TYPE_80M: return "80M";
  32. case LLM_TYPE_109M: return "109M";
  33. case LLM_TYPE_137M: return "137M";
  34. case LLM_TYPE_160M: return "160M";
  35. case LLM_TYPE_190M: return "190M";
  36. case LLM_TYPE_220M: return "220M";
  37. case LLM_TYPE_250M: return "250M";
  38. case LLM_TYPE_256M: return "256M";
  39. case LLM_TYPE_270M: return "270M";
  40. case LLM_TYPE_335M: return "335M";
  41. case LLM_TYPE_350M: return "350M";
  42. case LLM_TYPE_410M: return "410M";
  43. case LLM_TYPE_450M: return "450M";
  44. case LLM_TYPE_475M: return "475M";
  45. case LLM_TYPE_700M: return "700M";
  46. case LLM_TYPE_770M: return "770M";
  47. case LLM_TYPE_780M: return "780M";
  48. case LLM_TYPE_0_3B: return "0.3B";
  49. case LLM_TYPE_0_5B: return "0.5B";
  50. case LLM_TYPE_0_6B: return "0.6B";
  51. case LLM_TYPE_1B: return "1B";
  52. case LLM_TYPE_1_2B: return "1.2B";
  53. case LLM_TYPE_1_3B: return "1.3B";
  54. case LLM_TYPE_1_4B: return "1.4B";
  55. case LLM_TYPE_1_5B: return "1.5B";
  56. case LLM_TYPE_1_6B: return "1.6B";
  57. case LLM_TYPE_1_7B: return "1.7B";
  58. case LLM_TYPE_1_8B: return "1.8B";
  59. case LLM_TYPE_2B: return "2B";
  60. case LLM_TYPE_2_8B: return "2.8B";
  61. case LLM_TYPE_2_9B: return "2.9B";
  62. case LLM_TYPE_3B: return "3B";
  63. case LLM_TYPE_4B: return "4B";
  64. case LLM_TYPE_6B: return "6B";
  65. case LLM_TYPE_6_9B: return "6.9B";
  66. case LLM_TYPE_7B: return "7B";
  67. case LLM_TYPE_8B: return "8B";
  68. case LLM_TYPE_9B: return "9B";
  69. case LLM_TYPE_11B: return "11B";
  70. case LLM_TYPE_12B: return "12B";
  71. case LLM_TYPE_13B: return "13B";
  72. case LLM_TYPE_14B: return "14B";
  73. case LLM_TYPE_15B: return "15B";
  74. case LLM_TYPE_16B: return "16B";
  75. case LLM_TYPE_20B: return "20B";
  76. case LLM_TYPE_27B: return "27B";
  77. case LLM_TYPE_30B: return "30B";
  78. case LLM_TYPE_32B: return "32B";
  79. case LLM_TYPE_34B: return "34B";
  80. case LLM_TYPE_35B: return "35B";
  81. case LLM_TYPE_40B: return "40B";
  82. case LLM_TYPE_65B: return "65B";
  83. case LLM_TYPE_70B: return "70B";
  84. case LLM_TYPE_142B: return "142B";
  85. case LLM_TYPE_236B: return "236B";
  86. case LLM_TYPE_290B: return "290B";
  87. case LLM_TYPE_314B: return "314B";
  88. case LLM_TYPE_405B: return "405B";
  89. case LLM_TYPE_671B: return "671B";
  90. case LLM_TYPE_SMALL: return "0.1B";
  91. case LLM_TYPE_MEDIUM: return "0.4B";
  92. case LLM_TYPE_LARGE: return "0.8B";
  93. case LLM_TYPE_XL: return "1.5B";
  94. case LLM_TYPE_A1_7B: return "A1.7B";
  95. case LLM_TYPE_A2_7B: return "A2.7B";
  96. case LLM_TYPE_8x7B: return "8x7B";
  97. case LLM_TYPE_8x22B: return "8x22B";
  98. case LLM_TYPE_16x12B: return "16x12B";
  99. case LLM_TYPE_16x3_8B: return "16x3.8B";
  100. case LLM_TYPE_10B_128x3_66B: return "10B+128x3.66B";
  101. case LLM_TYPE_57B_A14B: return "57B.A14B";
  102. case LLM_TYPE_17B_16E: return "17Bx16E (Scout)";
  103. case LLM_TYPE_17B_128E: return "17Bx128E (Maverick)";
  104. case LLM_TYPE_A13B: return "A13B";
  105. case LLM_TYPE_21B_A3B: return "21B.A3B";
  106. case LLM_TYPE_30B_A3B: return "30B.A3B";
  107. case LLM_TYPE_106B_A12B: return "106B.A12B";
  108. case LLM_TYPE_235B_A22B: return "235B.A22B";
  109. case LLM_TYPE_300B_A47B: return "300B.A47B";
  110. case LLM_TYPE_355B_A32B: return "355B.A32B";
  111. case LLM_TYPE_E2B: return "E2B";
  112. case LLM_TYPE_E4B: return "E4B";
  113. default: return "?B";
  114. }
  115. }
  116. static const char * llama_expert_gating_func_name(llama_expert_gating_func_type type) {
  117. switch (type) {
  118. case LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX: return "softmax";
  119. case LLAMA_EXPERT_GATING_FUNC_TYPE_SIGMOID: return "sigmoid";
  120. default: return "unknown";
  121. }
  122. }
  123. static const std::map<llama_rope_scaling_type, const char *> LLAMA_ROPE_SCALING_TYPES = {
  124. { LLAMA_ROPE_SCALING_TYPE_NONE, "none" },
  125. { LLAMA_ROPE_SCALING_TYPE_LINEAR, "linear" },
  126. { LLAMA_ROPE_SCALING_TYPE_YARN, "yarn" },
  127. { LLAMA_ROPE_SCALING_TYPE_LONGROPE, "longrope" },
  128. };
  129. std::string llama_rope_scaling_type_name(llama_rope_scaling_type rope_scaling_type) {
  130. return LLAMA_ROPE_SCALING_TYPES.at(rope_scaling_type);
  131. }
  132. static llama_rope_scaling_type llama_rope_scaling_type_from_string(const std::string & name) {
  133. for (const auto & kv : LLAMA_ROPE_SCALING_TYPES) {
  134. if (kv.second == name) {
  135. return (llama_rope_scaling_type) kv.first;
  136. }
  137. }
  138. return LLAMA_ROPE_SCALING_TYPE_UNSPECIFIED;
  139. }
  140. // checks if the weight tensor can be used with the specified buffer type and device
  141. static bool weight_buft_supported(const llama_hparams & hparams, ggml_tensor * w, ggml_op op, ggml_backend_buffer_type_t buft, ggml_backend_dev_t dev) {
  142. GGML_ASSERT(w != nullptr);
  143. if (op == GGML_OP_NONE) {
  144. return true;
  145. }
  146. ggml_init_params params = {
  147. /*.mem_size =*/ ggml_tensor_overhead()*8,
  148. /*.mem_buffer =*/ NULL,
  149. /*.no_alloc =*/ true,
  150. };
  151. ggml_context_ptr ctx_ptr { ggml_init(params) };
  152. if (!ctx_ptr) {
  153. throw std::runtime_error(format("failed to create ggml context"));
  154. }
  155. ggml_context * ctx = ctx_ptr.get();
  156. ggml_tensor * op_tensor = nullptr;
  157. switch (op) {
  158. case GGML_OP_GET_ROWS:
  159. {
  160. ggml_tensor * b = ggml_new_tensor_1d(ctx, GGML_TYPE_I32, 512);
  161. op_tensor = ggml_get_rows(ctx, w, b);
  162. } break;
  163. case GGML_OP_MUL_MAT:
  164. {
  165. ggml_tensor * b = ggml_new_tensor_4d(ctx, GGML_TYPE_F32, w->ne[0], 512, w->ne[2], w->ne[3]);
  166. op_tensor = ggml_mul_mat(ctx, w, b);
  167. } break;
  168. case GGML_OP_MUL_MAT_ID:
  169. {
  170. int n_expert_used = hparams.n_expert_used;
  171. ggml_tensor * b = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, w->ne[0], n_expert_used, 512);
  172. ggml_tensor * ids = ggml_new_tensor_2d(ctx, GGML_TYPE_I32, n_expert_used, 512);
  173. op_tensor = ggml_mul_mat_id(ctx, w, b, ids);
  174. } break;
  175. case GGML_OP_ADD:
  176. {
  177. ggml_tensor * a = ggml_new_tensor_4d(ctx, GGML_TYPE_F32, w->ne[0], w->ne[1], w->ne[2], w->ne[3]);
  178. op_tensor = ggml_add(ctx, a, w);
  179. } break;
  180. case GGML_OP_ADD_ID:
  181. {
  182. int n_expert_used = hparams.n_expert_used;
  183. ggml_tensor * a = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, w->ne[0], n_expert_used, 512);
  184. ggml_tensor * c = ggml_new_tensor_2d(ctx, GGML_TYPE_I32, n_expert_used, 512);
  185. op_tensor = ggml_add_id(ctx, a, w, c);
  186. } break;
  187. case GGML_OP_MUL:
  188. {
  189. ggml_tensor * a = ggml_new_tensor_4d(ctx, GGML_TYPE_F32, w->ne[0], w->ne[1], w->ne[2], w->ne[3]);
  190. op_tensor = ggml_mul(ctx, a, w);
  191. } break;
  192. case GGML_OP_DIV:
  193. {
  194. ggml_tensor * a = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, w->ne[0]);
  195. op_tensor = ggml_div(ctx, a, w);
  196. } break;
  197. case GGML_OP_ROPE:
  198. {
  199. int n_embd_head = hparams.n_embd_head_v;
  200. int n_head = hparams.n_head();
  201. ggml_tensor * a = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, n_embd_head, n_head, 512);
  202. ggml_tensor * b = ggml_new_tensor_1d(ctx, GGML_TYPE_I32, 512);
  203. op_tensor = ggml_rope_ext(
  204. ctx, a, b, w,
  205. 0, 0, 0, 0, 0,
  206. 0, 0, 0, 0
  207. );
  208. } break;
  209. case GGML_OP_SSM_CONV:
  210. {
  211. const int64_t n_seq_tokens = 512;
  212. const int64_t n_seqs = 3;
  213. ggml_tensor * conv_x = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, w->ne[0] - 1 + n_seq_tokens, w->ne[1], n_seqs);
  214. op_tensor = ggml_ssm_conv(ctx, conv_x, w);
  215. } break;
  216. case GGML_OP_SSM_SCAN:
  217. {
  218. // w is ssm_a, which is used to distinguish Mamba-1 and Mamba-2
  219. const int64_t d_state = w->ne[0] == 1 ? hparams.ssm_d_state : w->ne[0];
  220. const int64_t n_head = w->ne[1];
  221. const int64_t head_dim = hparams.ssm_d_inner / n_head;
  222. const int64_t n_group = hparams.ssm_n_group ? hparams.ssm_n_group : 1;
  223. const int64_t n_seq_tokens = 512;
  224. const int64_t n_seqs = 3;
  225. ggml_tensor * s = ggml_new_tensor_4d(ctx, GGML_TYPE_F32, d_state, head_dim, n_head, n_seqs);
  226. ggml_tensor * x = ggml_new_tensor_4d(ctx, GGML_TYPE_F32, head_dim, n_head, n_seq_tokens, n_seqs);
  227. ggml_tensor * dt = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, n_head, n_seq_tokens, n_seqs);
  228. ggml_tensor * B = ggml_new_tensor_4d(ctx, GGML_TYPE_F32, d_state, n_group, n_seq_tokens, n_seqs);
  229. ggml_tensor * C = ggml_new_tensor_4d(ctx, GGML_TYPE_F32, d_state, n_group, n_seq_tokens, n_seqs);
  230. ggml_tensor * ids = ggml_new_tensor_1d(ctx, GGML_TYPE_I32, n_seqs);
  231. op_tensor = ggml_ssm_scan(ctx, s, x, dt, w, B, C, ids);
  232. } break;
  233. case GGML_OP_RWKV_WKV6:
  234. {
  235. // FIXME
  236. const int64_t S = 123;
  237. const int64_t H = 123;
  238. const int64_t n_tokens = 123;
  239. const int64_t n_seqs = 123;
  240. ggml_tensor * k = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, S, H, n_tokens);
  241. ggml_tensor * v = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, S, H, n_tokens);
  242. ggml_tensor * r = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, S, H, n_tokens);
  243. ggml_tensor * tf = w;
  244. ggml_tensor * td = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, S, H, n_tokens);
  245. ggml_tensor * state = ggml_new_tensor_4d(ctx, GGML_TYPE_F32, S, n_seqs, S, H);
  246. op_tensor = ggml_rwkv_wkv6(ctx, k, v, r, tf, td, state);
  247. } break;
  248. case GGML_OP_IM2COL:
  249. {
  250. const int n_embd = hparams.n_embd;
  251. ggml_tensor * b = ggml_new_tensor_4d(ctx, GGML_TYPE_F32, n_embd, w->ne[1], 1, 1);
  252. op_tensor = ggml_im2col(ctx, w, b, 1, 0, 0, 0, 1, 0, false, GGML_TYPE_F16);
  253. } break;
  254. case GGML_OP_SCALE:
  255. {
  256. op_tensor = ggml_scale(ctx, w, 1.0f);
  257. } break;
  258. default:
  259. GGML_ABORT("%s: missing test for op %s for tensor %s", __func__, ggml_op_name(op), w->name);
  260. }
  261. // create a temporary dummy buffer for the weight so that supports_op can check the buffer type
  262. GGML_ASSERT(w->buffer == nullptr);
  263. w->buffer = ggml_backend_buft_alloc_buffer(buft, 0);
  264. bool op_supported = ggml_backend_dev_supports_op(dev, op_tensor);
  265. ggml_backend_buffer_free(w->buffer);
  266. w->buffer = nullptr;
  267. return op_supported;
  268. }
  269. // lists of buffer types used for each layer
  270. using buft_list_t = std::vector<std::pair<ggml_backend_dev_t, ggml_backend_buffer_type_t>>;
  271. // find the first buffer type in the list that can use the tensor
  272. static ggml_backend_buffer_type_t select_weight_buft(const llama_hparams & hparams, ggml_tensor * tensor, ggml_op op, const buft_list_t & buft_list) {
  273. GGML_ASSERT(!buft_list.empty());
  274. for (const auto & cur : buft_list) {
  275. ggml_backend_dev_t cur_dev = cur.first;
  276. ggml_backend_buffer_type_t cur_buft = cur.second;
  277. if (weight_buft_supported(hparams, tensor, op, cur_buft, cur_dev)) {
  278. return cur_buft;
  279. }
  280. }
  281. return nullptr;
  282. }
  283. // CPU: ACCEL -> GPU host -> CPU extra -> CPU
  284. static buft_list_t make_cpu_buft_list(const std::vector<ggml_backend_dev_t> & devices, bool use_extra_bufts) {
  285. buft_list_t buft_list;
  286. // add ACCEL buffer types
  287. for (size_t i = 0; i < ggml_backend_dev_count(); ++i) {
  288. ggml_backend_dev_t dev = ggml_backend_dev_get(i);
  289. if (ggml_backend_dev_type(dev) == GGML_BACKEND_DEVICE_TYPE_ACCEL) {
  290. auto * buft = ggml_backend_dev_buffer_type(dev);
  291. // skip
  292. if (buft != ggml_backend_cpu_buffer_type()) {
  293. buft_list.emplace_back(dev, buft);
  294. }
  295. }
  296. }
  297. // add a host buffer type
  298. // storing the tensors in a host buffer is useful when the processing of large batches
  299. // is offloaded to a GPU device, since it reduces the time spent on data transfers
  300. // generally, this will be done using the first device in the list
  301. // a better approach would be to handle this on a weight-by-weight basis using the offload_op
  302. // function of the device to determine if it would benefit from being stored in a host buffer
  303. for (auto * dev : devices) {
  304. ggml_backend_buffer_type_t buft = ggml_backend_dev_host_buffer_type(dev);
  305. if (buft) {
  306. buft_list.emplace_back(dev, buft);
  307. break;
  308. }
  309. }
  310. // add extra buffer types
  311. if (use_extra_bufts) {
  312. auto * cpu_dev = ggml_backend_dev_by_type(GGML_BACKEND_DEVICE_TYPE_CPU);
  313. if (cpu_dev == nullptr) {
  314. throw std::runtime_error(format("%s: no CPU backend found", __func__));
  315. }
  316. auto * cpu_reg = ggml_backend_dev_backend_reg(cpu_dev);
  317. auto ggml_backend_dev_get_extra_bufts_fn = (ggml_backend_dev_get_extra_bufts_t)
  318. ggml_backend_reg_get_proc_address(cpu_reg, "ggml_backend_dev_get_extra_bufts");
  319. if (ggml_backend_dev_get_extra_bufts_fn) {
  320. ggml_backend_buffer_type_t * extra_bufts = ggml_backend_dev_get_extra_bufts_fn(cpu_dev);
  321. while (extra_bufts && *extra_bufts) {
  322. buft_list.emplace_back(cpu_dev, *extra_bufts);
  323. ++extra_bufts;
  324. }
  325. }
  326. }
  327. // add the CPU buffer type
  328. for (size_t i = 0; i < ggml_backend_dev_count(); ++i) {
  329. ggml_backend_dev_t dev = ggml_backend_dev_get(i);
  330. if (ggml_backend_dev_type(dev) == GGML_BACKEND_DEVICE_TYPE_CPU) {
  331. buft_list.emplace_back(dev, ggml_backend_dev_buffer_type(dev));
  332. }
  333. }
  334. return buft_list;
  335. }
  336. // GPU: split if LLAMA_SPLIT_MODE_ROW -> GPU
  337. static buft_list_t make_gpu_buft_list(ggml_backend_dev_t dev, llama_split_mode split_mode, const float * tensor_split) {
  338. buft_list_t buft_list;
  339. // add the device split buffer type if requested and available
  340. if (split_mode == LLAMA_SPLIT_MODE_ROW) {
  341. ggml_backend_reg_t reg = ggml_backend_dev_backend_reg(dev);
  342. auto ggml_backend_split_buffer_type_fn = (ggml_backend_split_buffer_type_t)
  343. ggml_backend_reg_get_proc_address(reg, "ggml_backend_split_buffer_type");
  344. if (ggml_backend_split_buffer_type_fn) {
  345. size_t dev_index = [&]() {
  346. auto * reg = ggml_backend_dev_backend_reg(dev);
  347. for (size_t i = 0; i < ggml_backend_reg_dev_count(reg); ++i) {
  348. if (ggml_backend_reg_dev_get(reg, i) == dev) {
  349. return i;
  350. }
  351. }
  352. throw std::runtime_error(format("device %s not found in its backend reg", ggml_backend_dev_name(dev)));
  353. }();
  354. auto * buft = ggml_backend_split_buffer_type_fn(dev_index, tensor_split);
  355. if (buft != nullptr) {
  356. buft_list.emplace_back(dev, buft);
  357. }
  358. }
  359. }
  360. // add the device default buffer type
  361. buft_list.emplace_back(dev, ggml_backend_dev_buffer_type(dev));
  362. return buft_list;
  363. }
  364. struct llama_model::impl {
  365. impl() {}
  366. ~impl() {}
  367. uint64_t n_elements = 0;
  368. size_t n_bytes = 0;
  369. std::string desc_str;
  370. // model memory mapped files
  371. llama_mmaps mappings;
  372. // objects representing data potentially being locked in memory
  373. llama_mlocks mlock_bufs;
  374. llama_mlocks mlock_mmaps;
  375. // contexts where the model tensors metadata is stored
  376. std::vector<ggml_context_ptr> ctxs;
  377. // the model memory buffers for the tensor data
  378. std::vector<ggml_backend_buffer_ptr> bufs;
  379. buft_list_t cpu_buft_list;
  380. std::map<ggml_backend_dev_t, buft_list_t> gpu_buft_list;
  381. struct layer_dev {
  382. ggml_backend_dev_t dev;
  383. buft_list_t * buft_list;
  384. };
  385. layer_dev dev_input = {};
  386. layer_dev dev_output = {};
  387. std::vector<layer_dev> dev_layer;
  388. bool has_tensor_overrides;
  389. };
  390. llama_model::llama_model(const llama_model_params & params) : params(params), pimpl(std::make_unique<impl>()) {
  391. pimpl->has_tensor_overrides = params.tensor_buft_overrides && params.tensor_buft_overrides[0].pattern;
  392. }
  393. llama_model::~llama_model() {}
  394. void llama_model::load_stats(llama_model_loader & ml) {
  395. pimpl->n_elements = ml.n_elements;
  396. pimpl->n_bytes = ml.n_bytes;
  397. }
  398. void llama_model::load_arch(llama_model_loader & ml) {
  399. arch = ml.get_arch();
  400. if (arch == LLM_ARCH_UNKNOWN) {
  401. throw std::runtime_error("unknown model architecture: '" + ml.get_arch_name() + "'");
  402. }
  403. }
  404. void llama_model::load_hparams(llama_model_loader & ml) {
  405. const gguf_context * ctx = ml.meta.get();
  406. // get metadata as string
  407. for (int i = 0; i < gguf_get_n_kv(ctx); i++) {
  408. gguf_type type = gguf_get_kv_type(ctx, i);
  409. if (type == GGUF_TYPE_ARRAY) {
  410. continue;
  411. }
  412. const char * name = gguf_get_key(ctx, i);
  413. const std::string value = gguf_kv_to_str(ctx, i);
  414. gguf_kv.emplace(name, value);
  415. }
  416. // get general kv
  417. ml.get_key(LLM_KV_GENERAL_NAME, name, false);
  418. // everything past this point is not vocab-related
  419. if (hparams.vocab_only) {
  420. return;
  421. }
  422. ml.get_key(LLM_KV_CONTEXT_LENGTH, hparams.n_ctx_train);
  423. ml.get_key(LLM_KV_EMBEDDING_LENGTH, hparams.n_embd);
  424. ml.get_key(LLM_KV_BLOCK_COUNT, hparams.n_layer);
  425. ml.get_key(LLM_KV_EXPERT_COUNT, hparams.n_expert, false);
  426. ml.get_key(LLM_KV_EXPERT_USED_COUNT, hparams.n_expert_used, false);
  427. if (arch == LLM_ARCH_WAVTOKENIZER_DEC) {
  428. ml.get_key(LLM_KV_FEATURES_LENGTH, hparams.n_embd_features);
  429. ml.get_key(LLM_KV_POSNET_EMBEDDING_LENGTH, hparams.posnet.n_embd);
  430. ml.get_key(LLM_KV_POSNET_BLOCK_COUNT, hparams.posnet.n_layer);
  431. ml.get_key(LLM_KV_CONVNEXT_EMBEDDING_LENGTH, hparams.convnext.n_embd);
  432. ml.get_key(LLM_KV_CONVNEXT_BLOCK_COUNT, hparams.convnext.n_layer);
  433. }
  434. GGML_ASSERT(hparams.n_expert <= LLAMA_MAX_EXPERTS);
  435. GGML_ASSERT(hparams.n_expert_used <= hparams.n_expert);
  436. if (hparams.n_expert > 0) {
  437. GGML_ASSERT(hparams.n_expert_used > 0);
  438. } else {
  439. GGML_ASSERT(hparams.n_expert_used == 0);
  440. }
  441. std::fill(hparams.n_head_arr.begin(), hparams.n_head_arr.end(), 0);
  442. std::fill(hparams.n_head_kv_arr.begin(), hparams.n_head_kv_arr.end(), 0);
  443. std::fill(hparams.n_ff_arr.begin(), hparams.n_ff_arr.end(), 0);
  444. std::fill(
  445. hparams.recurrent_layer_arr.begin(),
  446. hparams.recurrent_layer_arr.end(),
  447. llm_arch_is_recurrent(ml.get_arch()));
  448. std::fill(hparams.rope_sections.begin(), hparams.rope_sections.end(), 0);
  449. std::fill(hparams.swa_layers.begin(), hparams.swa_layers.end(), 0);
  450. ml.get_key_or_arr(LLM_KV_FEED_FORWARD_LENGTH, hparams.n_ff_arr, hparams.n_layer, false);
  451. ml.get_key_or_arr(LLM_KV_ATTENTION_HEAD_COUNT, hparams.n_head_arr, hparams.n_layer, false);
  452. // n_head_kv is optional, default to n_head
  453. hparams.n_head_kv_arr = hparams.n_head_arr;
  454. ml.get_key_or_arr(LLM_KV_ATTENTION_HEAD_COUNT_KV, hparams.n_head_kv_arr, hparams.n_layer, false);
  455. bool rope_finetuned = false;
  456. ml.get_key(LLM_KV_ROPE_SCALING_FINETUNED, rope_finetuned, false);
  457. hparams.rope_finetuned = rope_finetuned;
  458. hparams.n_ctx_orig_yarn = hparams.n_ctx_train;
  459. ml.get_key(LLM_KV_ROPE_SCALING_ORIG_CTX_LEN, hparams.n_ctx_orig_yarn, false);
  460. // rope_freq_base (optional)
  461. hparams.rope_freq_base_train = 10000.0f;
  462. ml.get_key(LLM_KV_ROPE_FREQ_BASE, hparams.rope_freq_base_train, false);
  463. std::string rope_scaling("linear");
  464. ml.get_key(LLM_KV_ROPE_SCALING_TYPE, rope_scaling, false);
  465. hparams.rope_scaling_type_train = llama_rope_scaling_type_from_string(rope_scaling);
  466. GGML_ASSERT(hparams.rope_scaling_type_train != LLAMA_ROPE_SCALING_TYPE_UNSPECIFIED);
  467. // rope_freq_scale (inverse of the kv) is optional
  468. float ropescale = 0.0f;
  469. if (!ml.get_key(LLM_KV_ROPE_SCALING_FACTOR, ropescale, false)) {
  470. // try the old key name
  471. ml.get_key(LLM_KV_ROPE_SCALE_LINEAR, ropescale, false);
  472. }
  473. hparams.rope_freq_scale_train = ropescale == 0.0f ? 1.0f : 1.0f/ropescale;
  474. // by default assume that the sliding-window layers use the same scaling type as the non-sliding-window layers
  475. hparams.rope_freq_base_train_swa = hparams.rope_freq_base_train;
  476. hparams.rope_freq_scale_train_swa = hparams.rope_freq_scale_train;
  477. ml.get_key(LLM_KV_ROPE_SCALING_ATTN_FACTOR, hparams.rope_attn_factor, false);
  478. // non-transformer models do not have attention heads
  479. if (hparams.n_head() > 0) {
  480. // gpt-neox n_rot = rotary_pct * (n_embd / n_head)
  481. // gpt-j n_rot = rotary_dim
  482. hparams.n_embd_head_k = hparams.n_embd / hparams.n_head();
  483. ml.get_key(LLM_KV_ATTENTION_KEY_LENGTH, hparams.n_embd_head_k, false);
  484. hparams.n_embd_head_v = hparams.n_embd / hparams.n_head();
  485. ml.get_key(LLM_KV_ATTENTION_VALUE_LENGTH, hparams.n_embd_head_v, false);
  486. // sanity check for n_rot (optional)
  487. hparams.n_rot = hparams.n_embd_head_k;
  488. ml.get_key(LLM_KV_ROPE_DIMENSION_COUNT, hparams.n_rot, false);
  489. if (arch == LLM_ARCH_LLAMA || arch == LLM_ARCH_DECI || arch == LLM_ARCH_FALCON) {
  490. if (hparams.n_rot != hparams.n_embd_head_k) {
  491. throw std::runtime_error(format("invalid n_rot: %u, expected %u", hparams.n_rot, hparams.n_embd_head_k));
  492. }
  493. }
  494. } else {
  495. hparams.n_rot = 0;
  496. hparams.n_embd_head_k = 0;
  497. hparams.n_embd_head_v = 0;
  498. }
  499. // for differentiating model types
  500. uint32_t n_vocab = 0;
  501. ml.get_key(LLM_KV_VOCAB_SIZE, n_vocab, false) || ml.get_arr_n(LLM_KV_TOKENIZER_LIST, n_vocab, false);
  502. // for classifier models
  503. ml.get_arr(LLM_KV_CLASSIFIER_OUTPUT_LABELS, classifier_labels, false);
  504. if (!classifier_labels.empty()) {
  505. hparams.n_cls_out = classifier_labels.size();
  506. }
  507. // arch-specific KVs
  508. switch (arch) {
  509. case LLM_ARCH_LLAMA:
  510. {
  511. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  512. if (hparams.n_expert == 8) {
  513. switch (hparams.n_layer) {
  514. case 32: type = LLM_TYPE_8x7B; break;
  515. case 56: type = LLM_TYPE_8x22B; break;
  516. default: type = LLM_TYPE_UNKNOWN;
  517. }
  518. } else {
  519. switch (hparams.n_layer) {
  520. case 16: type = LLM_TYPE_1B; break; // Llama 3.2 1B
  521. case 22: type = LLM_TYPE_1B; break;
  522. case 26: type = LLM_TYPE_3B; break;
  523. case 28: type = LLM_TYPE_3B; break; // Llama 3.2 3B
  524. case 30: type = LLM_TYPE_256M; break; // smoldocling 256M
  525. // granite uses a vocab with len 49152
  526. case 32: type = n_vocab == 49152 ? LLM_TYPE_3B : (n_vocab < 40000 ? LLM_TYPE_7B : LLM_TYPE_8B); break;
  527. case 36: type = LLM_TYPE_8B; break; // granite
  528. case 40: type = LLM_TYPE_13B; break;
  529. case 48: type = LLM_TYPE_34B; break;
  530. case 60: type = LLM_TYPE_30B; break;
  531. case 80: type = hparams.n_head() == hparams.n_head_kv() ? LLM_TYPE_65B : LLM_TYPE_70B; break;
  532. default: type = LLM_TYPE_UNKNOWN;
  533. }
  534. }
  535. } break;
  536. case LLM_ARCH_LLAMA4:
  537. {
  538. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  539. ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp);
  540. ml.get_key(LLM_KV_INTERLEAVE_MOE_LAYER_STEP, hparams.n_moe_layer_step);
  541. hparams.swa_type = LLAMA_SWA_TYPE_CHUNKED;
  542. hparams.n_swa = 8192; // should this be a gguf kv? currently it's the same for Scout and Maverick
  543. hparams.set_swa_pattern(4); // pattern: 3 chunked - 1 full
  544. switch (hparams.n_expert) {
  545. case 16: type = LLM_TYPE_17B_16E; break;
  546. case 128: type = LLM_TYPE_17B_128E; break;
  547. default: type = LLM_TYPE_UNKNOWN;
  548. }
  549. if (type == LLM_TYPE_17B_128E) {
  550. hparams.use_kq_norm = false;
  551. }
  552. } break;
  553. case LLM_ARCH_ARCEE:
  554. {
  555. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  556. // Arcee uses the same structure as Llama
  557. switch (hparams.n_layer) {
  558. case 36: type = LLM_TYPE_4B; break;
  559. default: type = LLM_TYPE_UNKNOWN;
  560. }
  561. } break;
  562. case LLM_ARCH_DECI:
  563. {
  564. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  565. switch (hparams.n_layer) {
  566. case 32: type = LLM_TYPE_7B; break;
  567. case 80: type = LLM_TYPE_70B; break;
  568. case 162: type = LLM_TYPE_405B; break;
  569. default: type = LLM_TYPE_UNKNOWN;
  570. }
  571. } break;
  572. case LLM_ARCH_MINICPM:
  573. {
  574. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  575. ml.get_key(LLM_KV_EMBEDDING_SCALE, hparams.f_embedding_scale);
  576. ml.get_key(LLM_KV_RESIDUAL_SCALE, hparams.f_residual_scale);
  577. ml.get_key(LLM_KV_LOGIT_SCALE, hparams.f_logit_scale);
  578. // MiniCPM uses rope by default, unlike Granite which uses it as a switch
  579. hparams.rope_finetuned = true;
  580. switch (hparams.n_layer) {
  581. case 52: type = LLM_TYPE_1B; break;
  582. case 40: type = LLM_TYPE_2B; break;
  583. default: type = LLM_TYPE_UNKNOWN;
  584. }
  585. } break;
  586. case LLM_ARCH_MINICPM3:
  587. {
  588. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  589. ml.get_key(LLM_KV_ATTENTION_Q_LORA_RANK, hparams.n_lora_q);
  590. ml.get_key(LLM_KV_ATTENTION_KV_LORA_RANK, hparams.n_lora_kv);
  591. switch (hparams.n_layer) {
  592. case 62: type = LLM_TYPE_4B; break;
  593. default: type = LLM_TYPE_UNKNOWN;
  594. }
  595. } break;
  596. case LLM_ARCH_GROK:
  597. {
  598. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  599. switch (hparams.n_layer) {
  600. case 64: type = LLM_TYPE_314B; break;
  601. default: type = LLM_TYPE_UNKNOWN;
  602. }
  603. } break;
  604. case LLM_ARCH_FALCON:
  605. {
  606. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  607. switch (hparams.n_layer) {
  608. case 32: type = LLM_TYPE_7B; break;
  609. case 60: type = LLM_TYPE_40B; break;
  610. default: type = LLM_TYPE_UNKNOWN;
  611. }
  612. } break;
  613. case LLM_ARCH_BAICHUAN:
  614. {
  615. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  616. switch (hparams.n_layer) {
  617. case 32: type = LLM_TYPE_7B; break;
  618. case 40: type = LLM_TYPE_13B; break;
  619. default: type = LLM_TYPE_UNKNOWN;
  620. }
  621. if (type == LLM_TYPE_13B) {
  622. // TODO: become GGUF KV parameter
  623. hparams.f_max_alibi_bias = 8.0f;
  624. }
  625. } break;
  626. case LLM_ARCH_STARCODER:
  627. {
  628. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  629. switch (hparams.n_layer) {
  630. case 24: type = LLM_TYPE_1B; break;
  631. case 36: type = LLM_TYPE_3B; break;
  632. case 42: type = LLM_TYPE_7B; break;
  633. case 40: type = LLM_TYPE_15B; break;
  634. default: type = LLM_TYPE_UNKNOWN;
  635. }
  636. } break;
  637. case LLM_ARCH_REFACT:
  638. {
  639. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  640. switch (hparams.n_layer) {
  641. case 32: type = LLM_TYPE_1B; break;
  642. default: type = LLM_TYPE_UNKNOWN;
  643. }
  644. // TODO: become GGUF KV parameter
  645. hparams.f_max_alibi_bias = 8.0f;
  646. } break;
  647. case LLM_ARCH_BERT:
  648. {
  649. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  650. ml.get_key(LLM_KV_ATTENTION_CAUSAL, hparams.causal_attn);
  651. ml.get_key(LLM_KV_POOLING_TYPE, hparams.pooling_type, false);
  652. switch (hparams.n_layer) {
  653. case 3:
  654. type = LLM_TYPE_17M; break; // bge-micro
  655. case 6:
  656. type = LLM_TYPE_22M; break; // MiniLM-L6
  657. case 12:
  658. switch (hparams.n_embd) {
  659. case 384: type = LLM_TYPE_33M; break; // MiniLM-L12, bge-small
  660. case 768: type = LLM_TYPE_109M; break; // bge-base
  661. default: type = LLM_TYPE_UNKNOWN;
  662. } break;
  663. case 24:
  664. type = LLM_TYPE_335M; break; // bge-large
  665. default: type = LLM_TYPE_UNKNOWN;
  666. }
  667. } break;
  668. case LLM_ARCH_JINA_BERT_V2:
  669. {
  670. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  671. ml.get_key(LLM_KV_ATTENTION_CAUSAL, hparams.causal_attn);
  672. ml.get_key(LLM_KV_POOLING_TYPE, hparams.pooling_type, false);
  673. hparams.f_max_alibi_bias = 8.0f;
  674. switch (hparams.n_layer) {
  675. case 4: type = LLM_TYPE_33M; break; // jina-embeddings-small
  676. case 12: type = LLM_TYPE_137M; break; // jina-embeddings-base
  677. default: type = LLM_TYPE_UNKNOWN;
  678. }
  679. } break;
  680. case LLM_ARCH_NOMIC_BERT:
  681. case LLM_ARCH_NOMIC_BERT_MOE:
  682. {
  683. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  684. ml.get_key(LLM_KV_ATTENTION_CAUSAL, hparams.causal_attn);
  685. ml.get_key(LLM_KV_POOLING_TYPE, hparams.pooling_type);
  686. ml.get_key(LLM_KV_MOE_EVERY_N_LAYERS, hparams.moe_every_n_layers, 0);
  687. if (hparams.n_layer == 12 && hparams.n_embd == 768) {
  688. if (arch == LLM_ARCH_NOMIC_BERT) {
  689. type = LLM_TYPE_137M;
  690. } else if (arch == LLM_ARCH_NOMIC_BERT_MOE && hparams.moe_every_n_layers == 2) {
  691. type = LLM_TYPE_475M;
  692. }
  693. }
  694. } break;
  695. case LLM_ARCH_NEO_BERT:
  696. {
  697. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  698. ml.get_key(LLM_KV_ATTENTION_CAUSAL, hparams.causal_attn);
  699. ml.get_key(LLM_KV_POOLING_TYPE, hparams.pooling_type);
  700. if (hparams.n_layer == 28) {
  701. type = LLM_TYPE_250M;
  702. }
  703. } break;
  704. case LLM_ARCH_BLOOM:
  705. {
  706. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  707. switch (hparams.n_layer) {
  708. case 24: type = LLM_TYPE_1B; break;
  709. case 30:
  710. switch (hparams.n_embd) {
  711. case 2560: type = LLM_TYPE_3B; break;
  712. case 4096: type = LLM_TYPE_7B; break;
  713. default: type = LLM_TYPE_UNKNOWN;
  714. } break;
  715. default: type = LLM_TYPE_UNKNOWN;
  716. }
  717. // TODO: become GGUF KV parameter
  718. hparams.f_max_alibi_bias = 8.0f;
  719. } break;
  720. case LLM_ARCH_MPT:
  721. {
  722. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  723. ml.get_key(LLM_KV_ATTENTION_CLAMP_KQV, hparams.f_clamp_kqv, false);
  724. ml.get_key(LLM_KV_ATTENTION_MAX_ALIBI_BIAS, hparams.f_max_alibi_bias);
  725. switch (hparams.n_layer) {
  726. case 32: type = LLM_TYPE_7B; break;
  727. case 48: type = LLM_TYPE_30B; break;
  728. default: type = LLM_TYPE_UNKNOWN;
  729. }
  730. } break;
  731. case LLM_ARCH_STABLELM:
  732. {
  733. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  734. switch (hparams.n_layer) {
  735. case 24: type = LLM_TYPE_1B; break;
  736. case 32: type = LLM_TYPE_3B; break;
  737. case 40: type = LLM_TYPE_12B; break;
  738. default: type = LLM_TYPE_UNKNOWN;
  739. }
  740. } break;
  741. case LLM_ARCH_QWEN:
  742. {
  743. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  744. switch (hparams.n_layer) {
  745. case 32: type = LLM_TYPE_7B; break;
  746. case 40: type = LLM_TYPE_13B; break;
  747. default: type = LLM_TYPE_UNKNOWN;
  748. }
  749. } break;
  750. case LLM_ARCH_QWEN2VL:
  751. {
  752. ml.get_key_or_arr(LLM_KV_ROPE_DIMENSION_SECTIONS, hparams.rope_sections, 4, true);
  753. }
  754. // fall through
  755. case LLM_ARCH_QWEN2:
  756. {
  757. ml.get_key(LLM_KV_POOLING_TYPE, hparams.pooling_type, false);
  758. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  759. switch (hparams.n_layer) {
  760. case 24: type = hparams.n_embd == 1024 ? LLM_TYPE_0_5B : LLM_TYPE_1B; break;
  761. case 28: type = hparams.n_embd == 1536 ? LLM_TYPE_1_5B : LLM_TYPE_7B; break;
  762. case 32: type = LLM_TYPE_7B; break;
  763. case 36: type = LLM_TYPE_3B; break;
  764. case 40: type = hparams.n_head() == 20 ? LLM_TYPE_4B : LLM_TYPE_13B; break;
  765. case 48: type = LLM_TYPE_14B; break;
  766. case 64: type = LLM_TYPE_32B; break;
  767. case 80: type = LLM_TYPE_70B; break;
  768. default: type = LLM_TYPE_UNKNOWN;
  769. }
  770. } break;
  771. case LLM_ARCH_DREAM:
  772. {
  773. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  774. // Dream models are primarily 7B with 28 layers
  775. switch (hparams.n_layer) {
  776. case 28:
  777. type = LLM_TYPE_7B;
  778. break;
  779. default:
  780. type = LLM_TYPE_UNKNOWN;
  781. }
  782. // Set non-causal attention for diffusion models
  783. hparams.causal_attn = false;
  784. }
  785. break;
  786. case LLM_ARCH_LLADA:
  787. {
  788. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  789. // LLaDA-8B has 32 layers, similar to LLaMA but for diffusion
  790. switch (hparams.n_layer) {
  791. case 32:
  792. type = LLM_TYPE_8B;
  793. break;
  794. default:
  795. type = LLM_TYPE_UNKNOWN;
  796. }
  797. // Set non-causal attention for diffusion models
  798. hparams.causal_attn = false;
  799. }
  800. break;
  801. case LLM_ARCH_QWEN2MOE:
  802. {
  803. ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp, false);
  804. ml.get_key(LLM_KV_EXPERT_SHARED_FEED_FORWARD_LENGTH, hparams.n_ff_shexp, false);
  805. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  806. switch (hparams.n_layer) {
  807. case 24: type = LLM_TYPE_A2_7B; break;
  808. case 28: type = LLM_TYPE_57B_A14B; break;
  809. default: type = LLM_TYPE_UNKNOWN;
  810. }
  811. } break;
  812. case LLM_ARCH_QWEN3:
  813. {
  814. ml.get_key(LLM_KV_POOLING_TYPE, hparams.pooling_type, false);
  815. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  816. switch (hparams.n_layer) {
  817. case 28: type = hparams.n_embd == 1024 ? LLM_TYPE_0_6B : LLM_TYPE_1_7B; break;
  818. case 36: type = hparams.n_embd == 2560 ? LLM_TYPE_4B : LLM_TYPE_8B; break;
  819. case 40: type = LLM_TYPE_14B; break;
  820. case 64: type = LLM_TYPE_32B; break;
  821. default: type = LLM_TYPE_UNKNOWN;
  822. }
  823. } break;
  824. case LLM_ARCH_QWEN3MOE:
  825. {
  826. ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp, false);
  827. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  828. switch (hparams.n_layer) {
  829. case 48: type = LLM_TYPE_30B_A3B; break;
  830. case 94: type = LLM_TYPE_235B_A22B; break;
  831. default: type = LLM_TYPE_UNKNOWN;
  832. }
  833. } break;
  834. case LLM_ARCH_PHI2:
  835. {
  836. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  837. switch (hparams.n_layer) {
  838. case 24: type = LLM_TYPE_1B; break;
  839. case 32: type = LLM_TYPE_3B; break;
  840. default: type = LLM_TYPE_UNKNOWN;
  841. }
  842. } break;
  843. case LLM_ARCH_PHI3:
  844. {
  845. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  846. switch (hparams.n_layer) {
  847. case 24: type = LLM_TYPE_1B; break;
  848. case 32: type = LLM_TYPE_3B; break;
  849. case 40: type = LLM_TYPE_14B; break;
  850. default: type = LLM_TYPE_UNKNOWN;
  851. }
  852. const bool found_swa = ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa, false);
  853. if (found_swa && hparams.n_swa > 0) {
  854. LLAMA_LOG_WARN("%s: Phi SWA is currently disabled - results might be suboptimal for some models (see %s)\n",
  855. __func__, "https://github.com/ggml-org/llama.cpp/pull/13676");
  856. // TODO: fix conversion scripts to correctly populate `n_swa` and `n_swa_pattern`
  857. hparams.swa_type = LLAMA_SWA_TYPE_NONE;
  858. hparams.n_swa = 0;
  859. hparams.set_swa_pattern(1);
  860. }
  861. } break;
  862. case LLM_ARCH_PHIMOE:
  863. {
  864. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  865. switch (hparams.n_layer) {
  866. case 32: type = LLM_TYPE_16x3_8B; break;
  867. default: type = LLM_TYPE_UNKNOWN;
  868. }
  869. } break;
  870. case LLM_ARCH_PLAMO:
  871. {
  872. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  873. switch (hparams.n_layer) {
  874. case 40: type = LLM_TYPE_13B; break;
  875. default: type = LLM_TYPE_UNKNOWN;
  876. }
  877. } break;
  878. case LLM_ARCH_PLAMO2:
  879. {
  880. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  881. // Load Mamba SSM parameters
  882. ml.get_key(LLM_KV_SSM_CONV_KERNEL, hparams.ssm_d_conv);
  883. ml.get_key(LLM_KV_SSM_INNER_SIZE, hparams.ssm_d_inner);
  884. ml.get_key(LLM_KV_SSM_STATE_SIZE, hparams.ssm_d_state);
  885. ml.get_key(LLM_KV_SSM_TIME_STEP_RANK, hparams.ssm_dt_rank);
  886. ml.get_key(LLM_KV_SSM_GROUP_COUNT, hparams.ssm_n_group);
  887. for (uint32_t i = 0; i < hparams.n_layer; ++i) {
  888. hparams.recurrent_layer_arr[i] = hparams.n_head_kv(i) == 0;
  889. }
  890. switch (hparams.n_layer) {
  891. case 16: type = LLM_TYPE_1B; break;
  892. case 32:
  893. if (hparams.n_embd == 2048) {
  894. type = LLM_TYPE_2B;
  895. } else if (hparams.n_embd == 4096) {
  896. type = LLM_TYPE_8B;
  897. }
  898. break;
  899. default: type = LLM_TYPE_UNKNOWN;
  900. }
  901. } break;
  902. case LLM_ARCH_GPT2:
  903. {
  904. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  905. switch (hparams.n_layer) {
  906. case 12: type = LLM_TYPE_SMALL; break;
  907. case 24: type = LLM_TYPE_MEDIUM; break;
  908. case 36: type = LLM_TYPE_LARGE; break;
  909. case 48: type = LLM_TYPE_XL; break;
  910. default: type = LLM_TYPE_UNKNOWN;
  911. }
  912. } break;
  913. case LLM_ARCH_CODESHELL:
  914. {
  915. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  916. switch (hparams.n_layer) {
  917. case 42: type = LLM_TYPE_7B; break;
  918. default: type = LLM_TYPE_UNKNOWN;
  919. }
  920. } break;
  921. case LLM_ARCH_ORION:
  922. {
  923. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  924. switch (hparams.n_layer) {
  925. case 40: type = LLM_TYPE_14B; break;
  926. default: type = LLM_TYPE_UNKNOWN;
  927. }
  928. } break;
  929. case LLM_ARCH_INTERNLM2:
  930. {
  931. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  932. switch (hparams.n_layer) {
  933. case 32: type = LLM_TYPE_7B; break;
  934. case 48: type = LLM_TYPE_20B; break;
  935. default: type = LLM_TYPE_UNKNOWN;
  936. }
  937. } break;
  938. case LLM_ARCH_GEMMA:
  939. {
  940. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  941. switch (hparams.n_layer) {
  942. case 18: type = LLM_TYPE_2B; break;
  943. case 28: type = LLM_TYPE_7B; break;
  944. default: type = LLM_TYPE_UNKNOWN;
  945. }
  946. } break;
  947. case LLM_ARCH_GEMMA2:
  948. {
  949. hparams.swa_type = LLAMA_SWA_TYPE_STANDARD;
  950. hparams.n_swa = 4096; // default value of gemma 2
  951. hparams.set_swa_pattern(2);
  952. hparams.attn_soft_cap = true;
  953. ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa, false);
  954. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  955. ml.get_key(LLM_KV_ATTN_LOGIT_SOFTCAPPING, hparams.f_attn_logit_softcapping, false);
  956. ml.get_key(LLM_KV_FINAL_LOGIT_SOFTCAPPING, hparams.f_final_logit_softcapping, false);
  957. switch (hparams.n_layer) {
  958. case 26: type = LLM_TYPE_2B; break;
  959. case 42: type = LLM_TYPE_9B; break;
  960. case 46: type = LLM_TYPE_27B; break;
  961. default: type = LLM_TYPE_UNKNOWN;
  962. }
  963. // ref: https://github.com/google/gemma_pytorch/blob/014acb7ac4563a5f77c76d7ff98f31b568c16508/gemma/config.py#L173
  964. hparams.f_attention_scale = type == LLM_TYPE_27B
  965. ? 1.0f / std::sqrt(float(hparams.n_embd / hparams.n_head(0)))
  966. : 1.0f / std::sqrt(float(hparams.n_embd_head_k));
  967. } break;
  968. case LLM_ARCH_GEMMA3:
  969. {
  970. hparams.swa_type = LLAMA_SWA_TYPE_STANDARD;
  971. hparams.set_swa_pattern(6);
  972. hparams.rope_freq_base_train_swa = 10000.0f;
  973. hparams.rope_freq_scale_train_swa = 1.0f;
  974. ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa);
  975. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  976. switch (hparams.n_layer) {
  977. case 26: type = LLM_TYPE_1B; break;
  978. case 34: type = LLM_TYPE_4B; break;
  979. case 48: type = LLM_TYPE_12B; break;
  980. case 62: type = LLM_TYPE_27B; break;
  981. default: type = LLM_TYPE_UNKNOWN;
  982. }
  983. // ref: https://github.com/google/gemma_pytorch/blob/014acb7ac4563a5f77c76d7ff98f31b568c16508/gemma/config.py#L289
  984. hparams.f_attention_scale = type == LLM_TYPE_27B
  985. ? 1.0f / std::sqrt(float(hparams.n_embd / hparams.n_head(0)))
  986. : 1.0f / std::sqrt(float(hparams.n_embd_head_k));
  987. } break;
  988. case LLM_ARCH_GEMMA3N:
  989. {
  990. hparams.swa_type = LLAMA_SWA_TYPE_STANDARD;
  991. hparams.set_swa_pattern(5);
  992. hparams.rope_freq_base_train_swa = 10000.0f;
  993. hparams.rope_freq_scale_train_swa = 1.0f;
  994. hparams.f_attention_scale = 1.0f;
  995. ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa);
  996. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  997. switch (hparams.n_layer) {
  998. case 30: type = LLM_TYPE_E2B; break;
  999. case 35: type = LLM_TYPE_E4B; break;
  1000. default: type = LLM_TYPE_UNKNOWN;
  1001. }
  1002. } break;
  1003. case LLM_ARCH_STARCODER2:
  1004. {
  1005. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  1006. switch (hparams.n_layer) {
  1007. case 30: type = LLM_TYPE_3B; break;
  1008. case 32: type = LLM_TYPE_7B; break;
  1009. case 40: type = LLM_TYPE_15B; break;
  1010. case 52: type = LLM_TYPE_20B; break; // granite
  1011. case 88: type = LLM_TYPE_34B; break; // granite
  1012. default: type = LLM_TYPE_UNKNOWN;
  1013. }
  1014. } break;
  1015. case LLM_ARCH_MAMBA:
  1016. {
  1017. ml.get_key(LLM_KV_SSM_CONV_KERNEL, hparams.ssm_d_conv);
  1018. ml.get_key(LLM_KV_SSM_INNER_SIZE, hparams.ssm_d_inner);
  1019. ml.get_key(LLM_KV_SSM_STATE_SIZE, hparams.ssm_d_state);
  1020. ml.get_key(LLM_KV_SSM_TIME_STEP_RANK, hparams.ssm_dt_rank);
  1021. ml.get_key(LLM_KV_SSM_DT_B_C_RMS, hparams.ssm_dt_b_c_rms, false);
  1022. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1023. switch (hparams.n_layer) {
  1024. case 24:
  1025. switch (hparams.n_embd) {
  1026. case 768: type = LLM_TYPE_SMALL; break;
  1027. default: type = LLM_TYPE_UNKNOWN;
  1028. } break;
  1029. case 48:
  1030. switch (hparams.n_embd) {
  1031. case 1024: type = LLM_TYPE_MEDIUM; break;
  1032. case 1536: type = LLM_TYPE_LARGE; break;
  1033. case 2048: type = LLM_TYPE_XL; break;
  1034. default: type = LLM_TYPE_UNKNOWN;
  1035. } break;
  1036. case 64:
  1037. switch (hparams.n_embd) {
  1038. case 2560: type = LLM_TYPE_3B; break;
  1039. default: type = LLM_TYPE_UNKNOWN;
  1040. } break;
  1041. default: type = LLM_TYPE_UNKNOWN;
  1042. }
  1043. } break;
  1044. case LLM_ARCH_MAMBA2:
  1045. {
  1046. ml.get_key(LLM_KV_SSM_CONV_KERNEL, hparams.ssm_d_conv);
  1047. ml.get_key(LLM_KV_SSM_INNER_SIZE, hparams.ssm_d_inner);
  1048. ml.get_key(LLM_KV_SSM_STATE_SIZE, hparams.ssm_d_state);
  1049. ml.get_key(LLM_KV_SSM_TIME_STEP_RANK, hparams.ssm_dt_rank);
  1050. ml.get_key(LLM_KV_SSM_GROUP_COUNT, hparams.ssm_n_group);
  1051. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1052. switch (hparams.n_layer) {
  1053. case 24:
  1054. switch (hparams.n_embd) {
  1055. case 768: type = LLM_TYPE_SMALL; break;
  1056. default: type = LLM_TYPE_UNKNOWN;
  1057. } break;
  1058. case 48:
  1059. switch (hparams.n_embd) {
  1060. case 1024: type = LLM_TYPE_MEDIUM; break;
  1061. case 1536: type = LLM_TYPE_LARGE; break;
  1062. case 2048: type = LLM_TYPE_XL; break;
  1063. default: type = LLM_TYPE_UNKNOWN;
  1064. } break;
  1065. case 64:
  1066. switch (hparams.n_embd) {
  1067. case 2560: type = LLM_TYPE_3B; break;
  1068. case 4096: type = LLM_TYPE_7B; break;
  1069. default: type = LLM_TYPE_UNKNOWN;
  1070. } break;
  1071. default: type = LLM_TYPE_UNKNOWN;
  1072. }
  1073. } break;
  1074. case LLM_ARCH_JAMBA:
  1075. {
  1076. ml.get_key(LLM_KV_SSM_CONV_KERNEL, hparams.ssm_d_conv);
  1077. ml.get_key(LLM_KV_SSM_INNER_SIZE, hparams.ssm_d_inner);
  1078. ml.get_key(LLM_KV_SSM_STATE_SIZE, hparams.ssm_d_state);
  1079. ml.get_key(LLM_KV_SSM_TIME_STEP_RANK, hparams.ssm_dt_rank);
  1080. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1081. for (uint32_t i = 0; i < hparams.n_layer; ++i) {
  1082. hparams.recurrent_layer_arr[i] = hparams.n_head_kv(i) == 0;
  1083. }
  1084. switch (hparams.n_layer) {
  1085. // TODO: Jamba layers are a bit heterogenous, so naming this is hard.
  1086. case 12: // 900M 8x???M
  1087. case 32: // 51B 16x?B
  1088. default: type = LLM_TYPE_UNKNOWN;
  1089. }
  1090. } break;
  1091. case LLM_ARCH_XVERSE:
  1092. {
  1093. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1094. switch (hparams.n_layer) {
  1095. case 32: type = LLM_TYPE_7B; break;
  1096. case 40: type = LLM_TYPE_13B; break;
  1097. case 80: type = LLM_TYPE_65B; break;
  1098. default: type = LLM_TYPE_UNKNOWN;
  1099. }
  1100. } break;
  1101. case LLM_ARCH_COMMAND_R:
  1102. {
  1103. ml.get_key(LLM_KV_LOGIT_SCALE, hparams.f_logit_scale);
  1104. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  1105. switch (hparams.n_layer) {
  1106. case 40: type = LLM_TYPE_35B; break;
  1107. default: type = LLM_TYPE_UNKNOWN;
  1108. }
  1109. } break;
  1110. case LLM_ARCH_COHERE2:
  1111. {
  1112. hparams.swa_type = LLAMA_SWA_TYPE_STANDARD;
  1113. hparams.set_swa_pattern(4);
  1114. ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa);
  1115. ml.get_key(LLM_KV_LOGIT_SCALE, hparams.f_logit_scale);
  1116. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  1117. switch (hparams.n_layer) {
  1118. case 32: type = LLM_TYPE_8B; break;
  1119. default: type = LLM_TYPE_UNKNOWN;
  1120. }
  1121. } break;
  1122. case LLM_ARCH_DBRX:
  1123. {
  1124. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  1125. ml.get_key(LLM_KV_ATTENTION_CLAMP_KQV, hparams.f_clamp_kqv);
  1126. switch (hparams.n_layer) {
  1127. case 40: type = LLM_TYPE_16x12B; break;
  1128. default: type = LLM_TYPE_UNKNOWN;
  1129. }
  1130. } break;
  1131. case LLM_ARCH_OLMO:
  1132. {
  1133. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  1134. ml.get_key(LLM_KV_ATTENTION_CLAMP_KQV, hparams.f_clamp_kqv, false);
  1135. switch (hparams.n_layer) {
  1136. case 22: type = LLM_TYPE_1B; break;
  1137. case 32: type = LLM_TYPE_7B; break;
  1138. case 80: type = LLM_TYPE_70B; break;
  1139. default: type = LLM_TYPE_UNKNOWN;
  1140. }
  1141. } break;
  1142. case LLM_ARCH_OLMO2:
  1143. {
  1144. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1145. switch (hparams.n_layer) {
  1146. case 16: type = LLM_TYPE_1B; break;
  1147. case 32: type = LLM_TYPE_7B; break;
  1148. case 40: type = LLM_TYPE_13B; break;
  1149. case 64: type = LLM_TYPE_32B; break;
  1150. default: type = LLM_TYPE_UNKNOWN;
  1151. }
  1152. } break;
  1153. case LLM_ARCH_OLMOE:
  1154. {
  1155. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1156. switch (hparams.n_layer) {
  1157. case 16: type = LLM_TYPE_A1_7B; break;
  1158. default: type = LLM_TYPE_UNKNOWN;
  1159. }
  1160. } break;
  1161. case LLM_ARCH_OPENELM:
  1162. {
  1163. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1164. switch (hparams.n_layer) {
  1165. case 16: type = LLM_TYPE_270M; break;
  1166. case 20: type = LLM_TYPE_450M; break;
  1167. case 28: type = LLM_TYPE_1B; break;
  1168. case 36: type = LLM_TYPE_3B; break;
  1169. default: type = LLM_TYPE_UNKNOWN;
  1170. }
  1171. } break;
  1172. case LLM_ARCH_GPTNEOX:
  1173. {
  1174. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  1175. ml.get_key(LLM_KV_USE_PARALLEL_RESIDUAL, hparams.use_par_res);
  1176. switch (hparams.n_layer) {
  1177. case 6:
  1178. switch (hparams.n_ff()) {
  1179. case 512: type = LLM_TYPE_14M; break;
  1180. case 2048: type = LLM_TYPE_70M; break;
  1181. default: type = LLM_TYPE_UNKNOWN;
  1182. } break;
  1183. case 12:
  1184. switch (hparams.n_ff()) {
  1185. case 3072: type = LLM_TYPE_160M; break;
  1186. default: type = LLM_TYPE_UNKNOWN;
  1187. } break;
  1188. case 16:
  1189. switch (hparams.n_ff()) {
  1190. case 8192: type = LLM_TYPE_1B; break;
  1191. default: type = LLM_TYPE_UNKNOWN;
  1192. } break;
  1193. case 24:
  1194. switch (hparams.n_ff()) {
  1195. case 4096: type = LLM_TYPE_410M; break;
  1196. case 8192: type = LLM_TYPE_1_4B; break;
  1197. default: type = LLM_TYPE_UNKNOWN;
  1198. } break;
  1199. case 32:
  1200. switch (hparams.n_ff()) {
  1201. case 10240: type = LLM_TYPE_2_8B; break;
  1202. case 16384: type = LLM_TYPE_6_9B; break;
  1203. default: type = LLM_TYPE_UNKNOWN;
  1204. } break;
  1205. case 36:
  1206. switch (hparams.n_ff()) {
  1207. case 20480: type = LLM_TYPE_12B; break;
  1208. default: type = LLM_TYPE_UNKNOWN;
  1209. } break;
  1210. case 44:
  1211. switch (hparams.n_ff()) {
  1212. case 24576: type = LLM_TYPE_20B; break;
  1213. default: type = LLM_TYPE_UNKNOWN;
  1214. } break;
  1215. default: type = LLM_TYPE_UNKNOWN;
  1216. }
  1217. } break;
  1218. case LLM_ARCH_ARCTIC:
  1219. {
  1220. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1221. if (hparams.n_expert == 128) {
  1222. switch (hparams.n_layer) {
  1223. case 35: type = LLM_TYPE_10B_128x3_66B; break;
  1224. default: type = LLM_TYPE_UNKNOWN;
  1225. }
  1226. } else {
  1227. type = LLM_TYPE_UNKNOWN;
  1228. }
  1229. } break;
  1230. case LLM_ARCH_DEEPSEEK:
  1231. {
  1232. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1233. ml.get_key(LLM_KV_LEADING_DENSE_BLOCK_COUNT, hparams.n_layer_dense_lead);
  1234. ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp);
  1235. ml.get_key(LLM_KV_EXPERT_SHARED_COUNT, hparams.n_expert_shared);
  1236. ml.get_key(LLM_KV_EXPERT_WEIGHTS_SCALE, hparams.expert_weights_scale);
  1237. switch (hparams.n_layer) {
  1238. case 28: type = LLM_TYPE_20B; break;
  1239. default: type = LLM_TYPE_UNKNOWN;
  1240. }
  1241. } break;
  1242. case LLM_ARCH_DEEPSEEK2:
  1243. {
  1244. bool is_lite = (hparams.n_layer == 27);
  1245. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1246. ml.get_key(LLM_KV_LEADING_DENSE_BLOCK_COUNT, hparams.n_layer_dense_lead);
  1247. if (!is_lite) {
  1248. ml.get_key(LLM_KV_ATTENTION_Q_LORA_RANK, hparams.n_lora_q);
  1249. }
  1250. ml.get_key(LLM_KV_ATTENTION_KV_LORA_RANK, hparams.n_lora_kv);
  1251. ml.get_key(LLM_KV_ATTENTION_KEY_LENGTH_MLA, hparams.n_embd_head_k_mla, false);
  1252. ml.get_key(LLM_KV_ATTENTION_VALUE_LENGTH_MLA, hparams.n_embd_head_v_mla, false);
  1253. ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp);
  1254. ml.get_key(LLM_KV_EXPERT_SHARED_COUNT, hparams.n_expert_shared);
  1255. ml.get_key(LLM_KV_EXPERT_WEIGHTS_SCALE, hparams.expert_weights_scale);
  1256. ml.get_key(LLM_KV_EXPERT_WEIGHTS_NORM, hparams.expert_weights_norm, false);
  1257. ml.get_key(LLM_KV_EXPERT_GATING_FUNC, hparams.expert_gating_func, false);
  1258. if (hparams.expert_gating_func == LLAMA_EXPERT_GATING_FUNC_TYPE_NONE) {
  1259. // for compatibility with existing DeepSeek V2 and V2.5 GGUFs
  1260. // that have no expert_gating_func model parameter set
  1261. hparams.expert_gating_func = LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX;
  1262. }
  1263. ml.get_key(LLM_KV_ROPE_SCALING_YARN_LOG_MUL, hparams.rope_yarn_log_mul, false);
  1264. switch (hparams.n_layer) {
  1265. case 27: type = LLM_TYPE_16B; break;
  1266. case 60: type = LLM_TYPE_236B; break;
  1267. case 61: type = LLM_TYPE_671B; break;
  1268. default: type = LLM_TYPE_UNKNOWN;
  1269. }
  1270. } break;
  1271. case LLM_ARCH_PLM:
  1272. {
  1273. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1274. ml.get_key(LLM_KV_ATTENTION_KV_LORA_RANK, hparams.n_lora_kv);
  1275. switch (hparams.n_layer) {
  1276. case 32: type = LLM_TYPE_1_8B; break;
  1277. default: type = LLM_TYPE_UNKNOWN;
  1278. }
  1279. } break;
  1280. case LLM_ARCH_CHATGLM:
  1281. {
  1282. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1283. switch (hparams.n_layer) {
  1284. case 28: {
  1285. if (hparams.n_head(0) == 16) {
  1286. type = LLM_TYPE_1_5B;
  1287. } else {
  1288. type = LLM_TYPE_6B;
  1289. }
  1290. } break;
  1291. case 40: {
  1292. if (hparams.n_head(0) == 24) {
  1293. type = LLM_TYPE_4B;
  1294. } else {
  1295. type = LLM_TYPE_9B;
  1296. }
  1297. } break;
  1298. default: type = LLM_TYPE_UNKNOWN;
  1299. }
  1300. } break;
  1301. case LLM_ARCH_GLM4:
  1302. {
  1303. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1304. switch (hparams.n_layer) {
  1305. case 40: type = LLM_TYPE_9B; break;
  1306. case 61: type = LLM_TYPE_32B; break;
  1307. default: type = LLM_TYPE_UNKNOWN;
  1308. }
  1309. } break;
  1310. case LLM_ARCH_GLM4_MOE:
  1311. {
  1312. ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp);
  1313. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1314. // MoE parameters
  1315. ml.get_key(LLM_KV_EXPERT_COUNT, hparams.n_expert);
  1316. ml.get_key(LLM_KV_EXPERT_USED_COUNT, hparams.n_expert_used);
  1317. ml.get_key(LLM_KV_EXPERT_SHARED_COUNT, hparams.n_expert_shared);
  1318. ml.get_key(LLM_KV_LEADING_DENSE_BLOCK_COUNT, hparams.n_layer_dense_lead, false);
  1319. ml.get_key(LLM_KV_EXPERT_WEIGHTS_SCALE, hparams.expert_weights_scale);
  1320. ml.get_key(LLM_KV_EXPERT_WEIGHTS_NORM, hparams.expert_weights_norm, false);
  1321. // Expert gating function (GLM-4.5 uses sigmoid)
  1322. ml.get_key(LLM_KV_EXPERT_GATING_FUNC, hparams.expert_gating_func, false);
  1323. if (hparams.expert_gating_func == LLAMA_EXPERT_GATING_FUNC_TYPE_NONE) {
  1324. hparams.expert_gating_func = LLAMA_EXPERT_GATING_FUNC_TYPE_SIGMOID;
  1325. }
  1326. // NextN/MTP parameters
  1327. ml.get_key(LLM_KV_NEXTN_PREDICT_LAYERS, hparams.nextn_predict_layers, false);
  1328. switch (hparams.n_layer) {
  1329. case 47: type = LLM_TYPE_106B_A12B; break; // GLM-4.5-Air (46 layers + 1 NextN layer)
  1330. case 93: type = LLM_TYPE_355B_A32B; break; // GLM-4.5 (92 layers + 1 NextN layer)
  1331. default: type = LLM_TYPE_UNKNOWN;
  1332. }
  1333. } break;
  1334. case LLM_ARCH_BITNET:
  1335. {
  1336. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1337. switch (hparams.n_layer) {
  1338. case 26: type = LLM_TYPE_3B; break;
  1339. default: type = LLM_TYPE_UNKNOWN;
  1340. }
  1341. } break;
  1342. case LLM_ARCH_T5:
  1343. {
  1344. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1345. ml.get_key(LLM_KV_ATTENTION_RELATIVE_BUCKETS_COUNT, hparams.n_rel_attn_bkts);
  1346. uint32_t dec_start_token_id;
  1347. if (ml.get_key(LLM_KV_DECODER_START_TOKEN_ID, dec_start_token_id, false)) {
  1348. hparams.dec_start_token_id = dec_start_token_id;
  1349. }
  1350. switch (hparams.n_layer) {
  1351. case 6: type = LLM_TYPE_60M; break; // t5-small
  1352. case 8: type = LLM_TYPE_80M; break; // flan-t5-small
  1353. case 12:
  1354. switch (hparams.n_ff()) {
  1355. case 3072: type = LLM_TYPE_220M; break; // t5-base
  1356. case 2048: type = LLM_TYPE_250M; break; // flan-t5-base
  1357. default: type = LLM_TYPE_UNKNOWN;
  1358. } break;
  1359. case 24:
  1360. switch (hparams.n_ff()) {
  1361. case 4096: type = LLM_TYPE_770M; break; // t5-large
  1362. case 2816: type = LLM_TYPE_780M; break; // flan-t5-large
  1363. case 16384: type = LLM_TYPE_3B; break; // t5-3b
  1364. case 5120: type = LLM_TYPE_3B; break; // flan-t5-xl
  1365. case 65536: type = LLM_TYPE_11B; break; // t5-11b
  1366. case 10240: type = LLM_TYPE_11B; break; // flan-t5-xxl
  1367. default: type = LLM_TYPE_UNKNOWN;
  1368. } break;
  1369. default: type = LLM_TYPE_UNKNOWN;
  1370. }
  1371. } break;
  1372. case LLM_ARCH_T5ENCODER:
  1373. {
  1374. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1375. ml.get_key(LLM_KV_ATTENTION_RELATIVE_BUCKETS_COUNT, hparams.n_rel_attn_bkts);
  1376. type = LLM_TYPE_UNKNOWN;
  1377. } break;
  1378. case LLM_ARCH_JAIS:
  1379. {
  1380. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  1381. ml.get_key(LLM_KV_ATTENTION_MAX_ALIBI_BIAS, hparams.f_max_alibi_bias);
  1382. switch (hparams.n_layer) {
  1383. case 24: type = LLM_TYPE_1_3B; break;
  1384. case 40: type = LLM_TYPE_13B; break;
  1385. /* TODO: add variants */
  1386. default: type = LLM_TYPE_UNKNOWN;
  1387. }
  1388. } break;
  1389. case LLM_ARCH_NEMOTRON:
  1390. {
  1391. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  1392. switch (hparams.n_layer) {
  1393. case 32: type = LLM_TYPE_4B; break;
  1394. default: type = LLM_TYPE_UNKNOWN;
  1395. }
  1396. } break;
  1397. case LLM_ARCH_EXAONE:
  1398. {
  1399. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1400. switch (hparams.n_layer) {
  1401. case 32: type = LLM_TYPE_8B; break;
  1402. default: type = LLM_TYPE_UNKNOWN;
  1403. }
  1404. } break;
  1405. case LLM_ARCH_EXAONE4:
  1406. {
  1407. if (hparams.n_layer == 64) { // 32B
  1408. hparams.swa_type = LLAMA_SWA_TYPE_STANDARD;
  1409. hparams.n_swa = 4096;
  1410. hparams.set_swa_pattern(4);
  1411. }
  1412. ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa, false);
  1413. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1414. switch (hparams.n_layer) {
  1415. case 30: type = LLM_TYPE_1_2B; break;
  1416. case 64: type = LLM_TYPE_32B; break;
  1417. default: type = LLM_TYPE_UNKNOWN;
  1418. }
  1419. } break;
  1420. case LLM_ARCH_RWKV6:
  1421. case LLM_ARCH_RWKV6QWEN2:
  1422. {
  1423. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps, false);
  1424. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps, false);
  1425. ml.get_key(LLM_KV_WKV_HEAD_SIZE, hparams.wkv_head_size);
  1426. ml.get_key(LLM_KV_TIME_MIX_EXTRA_DIM, hparams.time_mix_extra_dim);
  1427. ml.get_key(LLM_KV_TIME_DECAY_EXTRA_DIM, hparams.time_decay_extra_dim);
  1428. ml.get_key(LLM_KV_RESCALE_EVERY_N_LAYERS, hparams.rescale_every_n_layers, false);
  1429. ml.get_key(LLM_KV_TOKEN_SHIFT_COUNT, hparams.token_shift_count, false);
  1430. switch (hparams.n_layer) {
  1431. case 24: type = LLM_TYPE_1_6B; break;
  1432. case 32:
  1433. switch (hparams.n_embd) {
  1434. case 2560: type = LLM_TYPE_3B; break;
  1435. case 4096: type = LLM_TYPE_7B; break;
  1436. default: type = LLM_TYPE_UNKNOWN;
  1437. } break;
  1438. case 61: type = LLM_TYPE_14B; break;
  1439. case 64: type = LLM_TYPE_32B; break;
  1440. default: type = LLM_TYPE_UNKNOWN;
  1441. }
  1442. } break;
  1443. case LLM_ARCH_RWKV7:
  1444. case LLM_ARCH_ARWKV7:
  1445. {
  1446. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps, false);
  1447. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps, false);
  1448. ml.get_key(LLM_KV_WKV_HEAD_SIZE, hparams.wkv_head_size);
  1449. ml.get_key(LLM_KV_ATTENTION_DECAY_LORA_RANK, hparams.n_lora_decay);
  1450. ml.get_key(LLM_KV_ATTENTION_ICLR_LORA_RANK, hparams.n_lora_iclr);
  1451. ml.get_key(LLM_KV_ATTENTION_VALUE_RESIDUAL_MIX_LORA_RANK, hparams.n_lora_value_res_mix);
  1452. ml.get_key(LLM_KV_ATTENTION_GATE_LORA_RANK, hparams.n_lora_gate, false);
  1453. ml.get_key(LLM_KV_TOKEN_SHIFT_COUNT, hparams.token_shift_count, false);
  1454. switch (hparams.n_layer) {
  1455. case 12:
  1456. switch (hparams.n_embd) {
  1457. case 768: type = LLM_TYPE_190M; break;
  1458. default: type = LLM_TYPE_UNKNOWN;
  1459. } break;
  1460. case 24:
  1461. switch (hparams.n_embd) {
  1462. case 1024: type = LLM_TYPE_450M; break;
  1463. case 2048: type = LLM_TYPE_1_5B; break;
  1464. default: type = LLM_TYPE_UNKNOWN;
  1465. } break;
  1466. case 28:
  1467. switch (hparams.n_embd) {
  1468. case 1536: type = LLM_TYPE_1_5B; break;
  1469. case 3584: type = LLM_TYPE_7B; break;
  1470. default: type = LLM_TYPE_UNKNOWN;
  1471. } break;
  1472. case 32:
  1473. switch (hparams.n_embd) {
  1474. case 2560: type = LLM_TYPE_2_9B; break;
  1475. case 4096: type = LLM_TYPE_7B; break;
  1476. default: type = LLM_TYPE_UNKNOWN;
  1477. } break;
  1478. case 61:
  1479. switch (hparams.n_embd) {
  1480. case 4096: type = LLM_TYPE_14B; break;
  1481. default: type = LLM_TYPE_UNKNOWN;
  1482. } break;
  1483. default: type = LLM_TYPE_UNKNOWN;
  1484. }
  1485. } break;
  1486. case LLM_ARCH_GRANITE:
  1487. case LLM_ARCH_GRANITE_MOE:
  1488. {
  1489. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1490. ml.get_key(LLM_KV_LOGIT_SCALE, hparams.f_logit_scale);
  1491. ml.get_key(LLM_KV_RESIDUAL_SCALE, hparams.f_residual_scale);
  1492. ml.get_key(LLM_KV_EMBEDDING_SCALE, hparams.f_embedding_scale);
  1493. ml.get_key(LLM_KV_ATTENTION_SCALE, hparams.f_attention_scale);
  1494. // Granite uses rope_finetuned as a switch for rope, so default to true
  1495. bool rope_finetuned = true;
  1496. ml.get_key(LLM_KV_ROPE_SCALING_FINETUNED, rope_finetuned, false);
  1497. hparams.rope_finetuned = rope_finetuned;
  1498. switch (hparams.n_layer) {
  1499. case 32: type = LLM_TYPE_3B; break;
  1500. case 40: type = LLM_TYPE_3B; break;
  1501. // Add additional layer/vocab/etc checks here for other model sizes
  1502. default: type = LLM_TYPE_UNKNOWN;
  1503. }
  1504. // For Granite MoE Shared
  1505. ml.get_key(LLM_KV_EXPERT_SHARED_FEED_FORWARD_LENGTH, hparams.n_ff_shexp, /* required */ false);
  1506. } break;
  1507. case LLM_ARCH_GRANITE_HYBRID:
  1508. {
  1509. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1510. ml.get_key(LLM_KV_LOGIT_SCALE, hparams.f_logit_scale, /* required */ false);
  1511. ml.get_key(LLM_KV_RESIDUAL_SCALE, hparams.f_residual_scale, /* required */ false);
  1512. ml.get_key(LLM_KV_EMBEDDING_SCALE, hparams.f_embedding_scale, /* required */ false);
  1513. ml.get_key(LLM_KV_ATTENTION_SCALE, hparams.f_attention_scale, /* required */ false);
  1514. ml.get_key(LLM_KV_SSM_CONV_KERNEL, hparams.ssm_d_conv);
  1515. ml.get_key(LLM_KV_SSM_INNER_SIZE, hparams.ssm_d_inner);
  1516. ml.get_key(LLM_KV_SSM_STATE_SIZE, hparams.ssm_d_state);
  1517. ml.get_key(LLM_KV_SSM_TIME_STEP_RANK, hparams.ssm_dt_rank);
  1518. ml.get_key(LLM_KV_SSM_GROUP_COUNT, hparams.ssm_n_group);
  1519. // Granite uses rope_finetuned as a switch for rope, so default to true
  1520. bool rope_finetuned = true;
  1521. ml.get_key(LLM_KV_ROPE_SCALING_FINETUNED, rope_finetuned, false);
  1522. hparams.rope_finetuned = rope_finetuned;
  1523. // A layer is recurrent IFF the n_head_kv value is set to 0
  1524. for (uint32_t i = 0; i < hparams.n_layer; ++i) {
  1525. hparams.recurrent_layer_arr[i] = hparams.n_head_kv(i) == 0;
  1526. }
  1527. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1528. switch (hparams.n_layer) {
  1529. // TODO: Add llm type label (not sure this is useful)
  1530. default: type = LLM_TYPE_UNKNOWN;
  1531. }
  1532. // For Granite MoE Shared
  1533. ml.get_key(LLM_KV_EXPERT_SHARED_FEED_FORWARD_LENGTH, hparams.n_ff_shexp, /* required */ false);
  1534. } break;
  1535. case LLM_ARCH_CHAMELEON:
  1536. {
  1537. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1538. hparams.f_norm_eps = 1e-5; // eps for qk-norm, torch default
  1539. ml.get_key(LLM_KV_SWIN_NORM, hparams.swin_norm);
  1540. switch (hparams.n_layer) {
  1541. case 32: type = LLM_TYPE_7B; break;
  1542. case 48: type = LLM_TYPE_34B; break;
  1543. default: type = LLM_TYPE_UNKNOWN;
  1544. }
  1545. } break;
  1546. case LLM_ARCH_WAVTOKENIZER_DEC:
  1547. {
  1548. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  1549. ml.get_key(LLM_KV_ATTENTION_GROUPNORM_EPS, hparams.f_norm_group_eps);
  1550. ml.get_key(LLM_KV_ATTENTION_GROUPNORM_GROUPS, hparams.n_norm_groups);
  1551. ml.get_key(LLM_KV_ATTENTION_CAUSAL, hparams.causal_attn);
  1552. } break;
  1553. case LLM_ARCH_BAILINGMOE:
  1554. {
  1555. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1556. ml.get_key(LLM_KV_LEADING_DENSE_BLOCK_COUNT, hparams.n_layer_dense_lead);
  1557. ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp);
  1558. ml.get_key(LLM_KV_EXPERT_SHARED_COUNT, hparams.n_expert_shared);
  1559. ml.get_key(LLM_KV_EXPERT_WEIGHTS_SCALE, hparams.expert_weights_scale);
  1560. ml.get_key(LLM_KV_EXPERT_WEIGHTS_NORM, hparams.expert_weights_norm, false);
  1561. switch (hparams.n_layer) {
  1562. case 28: type = LLM_TYPE_16B; break;
  1563. case 88: type = LLM_TYPE_290B; break;
  1564. default: type = LLM_TYPE_UNKNOWN;
  1565. }
  1566. } break;
  1567. case LLM_ARCH_DOTS1:
  1568. {
  1569. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1570. ml.get_key(LLM_KV_LEADING_DENSE_BLOCK_COUNT, hparams.n_layer_dense_lead);
  1571. ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp);
  1572. ml.get_key(LLM_KV_EXPERT_SHARED_COUNT, hparams.n_expert_shared);
  1573. ml.get_key(LLM_KV_EXPERT_WEIGHTS_SCALE, hparams.expert_weights_scale);
  1574. ml.get_key(LLM_KV_EXPERT_WEIGHTS_NORM, hparams.expert_weights_norm, false);
  1575. ml.get_key(LLM_KV_EXPERT_GATING_FUNC, hparams.expert_gating_func, false);
  1576. switch (hparams.n_layer) {
  1577. case 62: type = LLM_TYPE_142B; break;
  1578. default: type = LLM_TYPE_UNKNOWN;
  1579. }
  1580. } break;
  1581. case LLM_ARCH_ERNIE4_5:
  1582. case LLM_ARCH_ERNIE4_5_MOE:
  1583. {
  1584. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1585. if (arch == LLM_ARCH_ERNIE4_5_MOE) {
  1586. ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp);
  1587. ml.get_key(LLM_KV_EXPERT_SHARED_FEED_FORWARD_LENGTH, hparams.n_ff_shexp, false);
  1588. ml.get_key(LLM_KV_INTERLEAVE_MOE_LAYER_STEP, hparams.n_moe_layer_step);
  1589. ml.get_key(LLM_KV_LEADING_DENSE_BLOCK_COUNT, hparams.n_layer_dense_lead);
  1590. }
  1591. switch (hparams.n_layer) {
  1592. case 18: type = LLM_TYPE_0_3B; break;
  1593. case 28: type = LLM_TYPE_21B_A3B; break;
  1594. case 54: type = LLM_TYPE_300B_A47B; break;
  1595. default: type = LLM_TYPE_UNKNOWN;
  1596. }
  1597. } break;
  1598. case LLM_ARCH_FALCON_H1:
  1599. {
  1600. // Common parameters
  1601. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1602. // SSM parameters
  1603. ml.get_key(LLM_KV_SSM_CONV_KERNEL, hparams.ssm_d_conv);
  1604. ml.get_key(LLM_KV_SSM_INNER_SIZE, hparams.ssm_d_inner);
  1605. ml.get_key(LLM_KV_SSM_STATE_SIZE, hparams.ssm_d_state);
  1606. ml.get_key(LLM_KV_SSM_TIME_STEP_RANK, hparams.ssm_dt_rank);
  1607. ml.get_key(LLM_KV_SSM_GROUP_COUNT, hparams.ssm_n_group);
  1608. std::fill(hparams.recurrent_layer_arr.begin(), hparams.recurrent_layer_arr.end(), true);
  1609. switch (hparams.n_layer) {
  1610. case 36:
  1611. type = LLM_TYPE_0_5B; break;
  1612. case 24:
  1613. type = LLM_TYPE_1_5B; break;
  1614. case 66:
  1615. type = LLM_TYPE_1B; break;
  1616. case 32:
  1617. type = LLM_TYPE_3B; break;
  1618. case 44:
  1619. type = LLM_TYPE_7B; break;
  1620. case 72:
  1621. type = LLM_TYPE_34B; break;
  1622. default:
  1623. type = LLM_TYPE_UNKNOWN;
  1624. }
  1625. } break;
  1626. case LLM_ARCH_HUNYUAN_MOE:
  1627. {
  1628. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1629. ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp);
  1630. ml.get_key(LLM_KV_EXPERT_SHARED_FEED_FORWARD_LENGTH, hparams.n_ff_shexp);
  1631. switch (hparams.n_layer) {
  1632. case 32: type = LLM_TYPE_A13B; break;
  1633. default: type = LLM_TYPE_UNKNOWN;
  1634. }
  1635. } break;
  1636. case LLM_ARCH_HUNYUAN_DENSE:
  1637. {
  1638. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1639. switch (hparams.n_embd) {
  1640. case 1024: type = LLM_TYPE_0_5B; break;
  1641. case 2048: type = LLM_TYPE_1_8B; break;
  1642. case 3072: type = LLM_TYPE_4B; break;
  1643. case 4096: type = LLM_TYPE_7B; break;
  1644. default: type = LLM_TYPE_UNKNOWN;
  1645. }
  1646. } break;
  1647. case LLM_ARCH_SMOLLM3:
  1648. {
  1649. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1650. hparams.n_no_rope_layer_step = 4;
  1651. switch (hparams.n_layer) {
  1652. case 36: type = LLM_TYPE_3B; break;
  1653. default: type = LLM_TYPE_UNKNOWN;
  1654. }
  1655. } break;
  1656. case LLM_ARCH_OPENAI_MOE:
  1657. {
  1658. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1659. ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp);
  1660. ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa);
  1661. hparams.swa_type = LLAMA_SWA_TYPE_STANDARD;
  1662. hparams.set_swa_pattern(2);
  1663. // TODO: switch (hparams.n_layer)
  1664. } break;
  1665. case LLM_ARCH_LFM2:
  1666. {
  1667. ml.get_key(LLM_KV_SHORTCONV_L_CACHE, hparams.n_shortconv_l_cache);
  1668. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1669. for (uint32_t il = 0; il < hparams.n_layer; ++il) {
  1670. hparams.recurrent_layer_arr[il] = hparams.n_head_kv(il) == 0;
  1671. }
  1672. switch (hparams.n_embd) {
  1673. case 1024: type = LLM_TYPE_350M; break;
  1674. case 1536: type = LLM_TYPE_700M; break;
  1675. case 2048: type = LLM_TYPE_1_2B; break;
  1676. default: type = LLM_TYPE_UNKNOWN;
  1677. }
  1678. } break;
  1679. case LLM_ARCH_SMALLTHINKER:
  1680. {
  1681. const bool found_swa = ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa, false);
  1682. if (found_swa && hparams.n_swa > 0) {
  1683. hparams.swa_type = LLAMA_SWA_TYPE_STANDARD;
  1684. hparams.n_swa = 4096;
  1685. hparams.set_swa_pattern(4, true);
  1686. } else {
  1687. hparams.swa_type = LLAMA_SWA_TYPE_NONE;
  1688. hparams.n_no_rope_layer_step = hparams.n_layer;
  1689. }
  1690. ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp, false);
  1691. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1692. ml.get_key(LLM_KV_EXPERT_GATING_FUNC, hparams.expert_gating_func, false);
  1693. switch (hparams.n_layer) {
  1694. case 32: type = LLM_TYPE_4B; break;
  1695. case 52: type = LLM_TYPE_20B; break;
  1696. default: type = LLM_TYPE_UNKNOWN;
  1697. }
  1698. } break;
  1699. default: throw std::runtime_error("unsupported model architecture");
  1700. }
  1701. pimpl->n_bytes = ml.n_bytes;
  1702. pimpl->desc_str = arch_name() + " " + type_name() + " " + ml.ftype_name();
  1703. if (hparams.f_max_alibi_bias > 0.0f) {
  1704. hparams.use_alibi = true;
  1705. }
  1706. hparams.rope_type = llama_model_rope_type(this);
  1707. }
  1708. void llama_model::load_vocab(llama_model_loader & ml) {
  1709. const auto kv = LLM_KV(arch);
  1710. vocab.load(ml, kv);
  1711. }
  1712. bool llama_model::load_tensors(llama_model_loader & ml) {
  1713. const auto & split_mode = params.split_mode;
  1714. const auto & n_gpu_layers = params.n_gpu_layers;
  1715. const auto & use_mlock = params.use_mlock;
  1716. const auto & tensor_split = params.tensor_split;
  1717. const int n_layer = hparams.n_layer;
  1718. const bool use_mmap_buffer = true;
  1719. LLAMA_LOG_INFO("%s: loading model tensors, this can take a while... (mmap = %s)\n", __func__, ml.use_mmap ? "true" : "false");
  1720. // build a list of buffer types for the CPU and GPU devices
  1721. pimpl->cpu_buft_list = make_cpu_buft_list(devices, params.use_extra_bufts);
  1722. for (auto * dev : devices) {
  1723. buft_list_t buft_list = make_gpu_buft_list(dev, split_mode, tensor_split);
  1724. // add CPU buffer types as a fallback
  1725. buft_list.insert(buft_list.end(), pimpl->cpu_buft_list.begin(), pimpl->cpu_buft_list.end());
  1726. pimpl->gpu_buft_list.emplace(dev, std::move(buft_list));
  1727. }
  1728. // calculate the split points
  1729. bool all_zero = tensor_split == nullptr || std::all_of(tensor_split, tensor_split + n_devices(), [](float x) { return x == 0.0f; });
  1730. std::vector<float> splits(n_devices());
  1731. if (all_zero) {
  1732. // default split, by free memory
  1733. for (size_t i = 0; i < n_devices(); ++i) {
  1734. ggml_backend_dev_t dev = devices[i];
  1735. size_t total;
  1736. size_t free;
  1737. ggml_backend_dev_memory(dev, &free, &total);
  1738. splits[i] = free;
  1739. }
  1740. } else {
  1741. std::copy(tensor_split, tensor_split + n_devices(), splits.begin());
  1742. }
  1743. // sum and normalize the splits to get the split points
  1744. float split_sum = 0.0f;
  1745. for (size_t i = 0; i < n_devices(); ++i) {
  1746. split_sum += splits[i];
  1747. splits[i] = split_sum;
  1748. }
  1749. for (size_t i = 0; i < n_devices(); ++i) {
  1750. splits[i] /= split_sum;
  1751. }
  1752. ggml_backend_dev_t cpu_dev = ggml_backend_dev_by_type(GGML_BACKEND_DEVICE_TYPE_CPU);
  1753. if (cpu_dev == nullptr) {
  1754. throw std::runtime_error(format("%s: no CPU backend found", __func__));
  1755. }
  1756. const int i_gpu_start = std::max((int) hparams.n_layer - n_gpu_layers, (int) 0);
  1757. const int act_gpu_layers = devices.empty() ? 0 : std::min(n_gpu_layers, (int)n_layer + 1);
  1758. auto get_layer_buft_list = [&](int il) -> llama_model::impl::layer_dev {
  1759. const bool is_swa = il < (int) hparams.n_layer && hparams.is_swa(il);
  1760. if (il < i_gpu_start || (il - i_gpu_start) >= act_gpu_layers) {
  1761. LLAMA_LOG_DEBUG("load_tensors: layer %3d assigned to device %s, is_swa = %d\n", il, ggml_backend_dev_name(cpu_dev), is_swa);
  1762. return {cpu_dev, &pimpl->cpu_buft_list};
  1763. }
  1764. const int layer_gpu = std::upper_bound(splits.begin(), splits.begin() + n_devices(), float(il - i_gpu_start)/act_gpu_layers) - splits.begin();
  1765. auto * dev = devices.at(layer_gpu);
  1766. LLAMA_LOG_DEBUG("load_tensors: layer %3d assigned to device %s, is_swa = %d\n", il, ggml_backend_dev_name(dev), is_swa);
  1767. return {dev, &pimpl->gpu_buft_list.at(dev)};
  1768. };
  1769. // assign the input layer
  1770. // there is very little benefit to offloading the input layer, so always keep it on the CPU
  1771. pimpl->dev_input = { cpu_dev, &pimpl->cpu_buft_list };
  1772. // assign the repeating layers to the devices according to the splits
  1773. pimpl->dev_layer.resize(n_layer);
  1774. for (int il = 0; il < n_layer; ++il) {
  1775. pimpl->dev_layer[il] = get_layer_buft_list(il);
  1776. }
  1777. // assign the output layer
  1778. pimpl->dev_output = get_layer_buft_list(n_layer);
  1779. // one ggml context per buffer type
  1780. int max_n_tensors = ml.n_tensors;
  1781. max_n_tensors += 1; // duplicated output tensor
  1782. max_n_tensors += n_layer*2; // duplicated rope freq tensors
  1783. const size_t ctx_size = ggml_tensor_overhead()*max_n_tensors;
  1784. std::map<ggml_backend_buffer_type_t, ggml_context *> ctx_map;
  1785. auto ctx_for_buft = [&](ggml_backend_buffer_type_t buft) -> ggml_context * {
  1786. auto it = ctx_map.find(buft);
  1787. if (it == ctx_map.end()) {
  1788. ggml_init_params params = {
  1789. /*.mem_size =*/ ctx_size,
  1790. /*.mem_buffer =*/ NULL,
  1791. /*.no_alloc =*/ true,
  1792. };
  1793. ggml_context * ctx = ggml_init(params);
  1794. if (!ctx) {
  1795. throw std::runtime_error(format("failed to create ggml context"));
  1796. }
  1797. ctx_map[buft] = ctx;
  1798. pimpl->ctxs.emplace_back(ctx);
  1799. return ctx;
  1800. }
  1801. return it->second;
  1802. };
  1803. const auto TENSOR_DUPLICATED = llama_model_loader::TENSOR_DUPLICATED;
  1804. const auto TENSOR_NOT_REQUIRED = llama_model_loader::TENSOR_NOT_REQUIRED;
  1805. const auto TENSOR_SKIP = llama_model_loader::TENSOR_SKIP;
  1806. // create tensors for the weights
  1807. {
  1808. // note: cast to int64_t since we will use these for the tensor dimensions
  1809. const int64_t n_head = hparams.n_head();
  1810. const int64_t n_head_kv = hparams.n_head_kv();
  1811. const int64_t n_embd = hparams.n_embd;
  1812. const int64_t n_embd_k_gqa = hparams.n_embd_k_gqa();
  1813. const int64_t n_embd_v_gqa = hparams.n_embd_v_gqa();
  1814. const int64_t n_embd_head_k = hparams.n_embd_head_k;
  1815. const int64_t n_embd_head_v = hparams.n_embd_head_v;
  1816. const int64_t n_ff = hparams.n_ff();
  1817. const int64_t n_embd_gqa = n_embd_v_gqa;
  1818. const int64_t n_vocab = vocab.n_tokens();
  1819. const int64_t n_token_types = vocab.n_token_types();
  1820. const int64_t n_rot = hparams.n_rot;
  1821. const int64_t n_expert = hparams.n_expert;
  1822. const int64_t n_expert_used = hparams.n_expert_used;
  1823. const int64_t n_ctx_train = hparams.n_ctx_train;
  1824. if (n_expert > 0 && hparams.n_expert_used == 0) {
  1825. throw std::runtime_error("model has expert layers but no expert layers are used");
  1826. }
  1827. int n_moved_tensors = 0;
  1828. ggml_tensor * first_moved_tensor = nullptr;
  1829. ggml_backend_buffer_type_t first_moved_from_buft = nullptr;
  1830. ggml_backend_buffer_type_t first_moved_to_buft = nullptr;
  1831. auto create_tensor = [&](const LLM_TN_IMPL & tn, const std::initializer_list<int64_t> & ne, int flags) -> ggml_tensor * {
  1832. ggml_tensor * t_meta = ml.get_tensor_meta(tn.str().c_str());
  1833. if (!t_meta) {
  1834. if (flags & TENSOR_NOT_REQUIRED) {
  1835. return nullptr;
  1836. }
  1837. throw std::runtime_error(format("missing tensor '%s'", tn.str().c_str()));
  1838. }
  1839. // some models use the token embedding tensor as the output, but since these are used in different layers and with different ops
  1840. // the tensor is duplicated
  1841. // to handle this, we check if the tensor is duplicated, and if so, we assume that it is being loaded as the output tensor
  1842. llm_tensor tn_tensor = tn.tensor;
  1843. if (tn.tensor == LLM_TENSOR_TOKEN_EMBD && flags & TENSOR_DUPLICATED) {
  1844. tn_tensor = LLM_TENSOR_OUTPUT;
  1845. }
  1846. llm_tensor_info info;
  1847. try {
  1848. info = llm_tensor_info_for(tn_tensor);
  1849. } catch (const std::out_of_range & e) {
  1850. throw std::runtime_error(format("missing tensor info mapping for %s", tn.str().c_str()));
  1851. }
  1852. // skip unused tensors
  1853. if (info.op == GGML_OP_NONE || flags & TENSOR_SKIP) {
  1854. const size_t nbytes = ggml_nbytes(t_meta);
  1855. LLAMA_LOG_WARN("model has unused tensor %s (size = %zu bytes) -- ignoring\n", tn.str().c_str(), nbytes);
  1856. ml.size_data -= nbytes;
  1857. ml.n_created++;
  1858. return nullptr;
  1859. }
  1860. // tensors with "bias" suffix are always used with GGML_OP_ADD or GGML_OP_ADD_ID
  1861. ggml_op op;
  1862. bool bias = tn.suffix != nullptr && strcmp(tn.suffix, "bias") == 0;
  1863. if (bias) {
  1864. if (info.op == GGML_OP_MUL_MAT_ID) {
  1865. op = GGML_OP_ADD_ID;
  1866. } else {
  1867. op = GGML_OP_ADD;
  1868. }
  1869. } else {
  1870. op = info.op;
  1871. }
  1872. // sanity checks
  1873. if (info.layer == LLM_TENSOR_LAYER_INPUT || info.layer == LLM_TENSOR_LAYER_OUTPUT) {
  1874. if (tn.bid != -1) {
  1875. GGML_ABORT("input/output layer tensor %s used with a layer number", tn.str().c_str());
  1876. }
  1877. } else {
  1878. if (tn.bid == -1) {
  1879. GGML_ABORT("repeating layer tensor %s used without a layer number", tn.str().c_str());
  1880. }
  1881. }
  1882. // select the buffer type for this tensor
  1883. buft_list_t * buft_list;
  1884. switch (info.layer) {
  1885. case LLM_TENSOR_LAYER_INPUT:
  1886. buft_list = pimpl->dev_input.buft_list;
  1887. break;
  1888. case LLM_TENSOR_LAYER_OUTPUT:
  1889. buft_list = pimpl->dev_output.buft_list;
  1890. break;
  1891. case LLM_TENSOR_LAYER_REPEATING:
  1892. buft_list = pimpl->dev_layer.at(tn.bid).buft_list;
  1893. break;
  1894. default:
  1895. GGML_ABORT("invalid layer %d for tensor %s", info.layer, tn.str().c_str());
  1896. }
  1897. ggml_backend_buffer_type_t buft = nullptr;
  1898. // check overrides
  1899. if (ml.tensor_buft_overrides) {
  1900. std::string tensor_name = tn.str();
  1901. for (const auto * overrides = ml.tensor_buft_overrides; overrides->pattern != nullptr; ++overrides) {
  1902. std::regex pattern(overrides->pattern);
  1903. if (std::regex_search(tensor_name, pattern)) {
  1904. if (overrides->buft == ggml_backend_cpu_buffer_type()) {
  1905. // when overriding to a CPU buffer, consider the extra buffer types
  1906. buft = select_weight_buft(hparams, t_meta, op, pimpl->cpu_buft_list);
  1907. } else {
  1908. buft = overrides->buft;
  1909. }
  1910. LLAMA_LOG_DEBUG("tensor %s (%zu MiB %s) buffer type overridden to %s\n",
  1911. tensor_name.c_str(),
  1912. ggml_nbytes(t_meta) / 1024 / 1024, ggml_type_name(t_meta->type),
  1913. ggml_backend_buft_name(buft));
  1914. break;
  1915. }
  1916. }
  1917. }
  1918. if (!buft) {
  1919. buft = select_weight_buft(hparams, t_meta, op, *buft_list);
  1920. if (!buft) {
  1921. throw std::runtime_error(format("failed to find a compatible buffer type for tensor %s", tn.str().c_str()));
  1922. }
  1923. }
  1924. // avoid using a host buffer when using mmap
  1925. auto * buft_dev = ggml_backend_buft_get_device(buft);
  1926. if (ml.use_mmap && buft_dev && buft == ggml_backend_dev_host_buffer_type(buft_dev)) {
  1927. auto * cpu_dev = ggml_backend_dev_by_type(GGML_BACKEND_DEVICE_TYPE_CPU);
  1928. if (!cpu_dev) {
  1929. throw std::runtime_error("no CPU backend found");
  1930. }
  1931. buft = ggml_backend_dev_buffer_type(cpu_dev);
  1932. }
  1933. if (buft != buft_list->front().second) {
  1934. n_moved_tensors++;
  1935. if (!first_moved_tensor) {
  1936. first_moved_tensor = t_meta;
  1937. first_moved_from_buft = buft_list->front().second;
  1938. first_moved_to_buft = buft;
  1939. }
  1940. }
  1941. ggml_context * ctx = ctx_for_buft(buft);
  1942. // if duplicated, check if the original tensor was allocated in the same buffer type context and avoid creating a new one
  1943. if (flags & TENSOR_DUPLICATED) {
  1944. ggml_tensor * t = ggml_get_tensor(ctx, tn.str().c_str());
  1945. if (t) {
  1946. return t;
  1947. }
  1948. }
  1949. return ml.create_tensor(ctx, tn, ne, flags);
  1950. };
  1951. layers.resize(n_layer);
  1952. // TODO: move to a separate function
  1953. const auto tn = LLM_TN(arch);
  1954. switch (arch) {
  1955. case LLM_ARCH_LLAMA:
  1956. case LLM_ARCH_REFACT:
  1957. case LLM_ARCH_MINICPM:
  1958. case LLM_ARCH_GRANITE:
  1959. case LLM_ARCH_GRANITE_MOE:
  1960. {
  1961. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  1962. // output
  1963. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  1964. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  1965. // if output is NULL, init from the input tok embed
  1966. if (output == NULL) {
  1967. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  1968. }
  1969. for (int i = 0; i < n_layer; ++i) {
  1970. auto & layer = layers[i];
  1971. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  1972. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  1973. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  1974. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  1975. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  1976. // optional bias tensors
  1977. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  1978. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED);
  1979. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED);
  1980. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  1981. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  1982. if (hparams.rope_scaling_type_train == LLAMA_ROPE_SCALING_TYPE_LONGROPE) {
  1983. layer.rope_long = create_tensor(tn(LLM_TENSOR_ROPE_FACTORS_LONG, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  1984. layer.rope_short = create_tensor(tn(LLM_TENSOR_ROPE_FACTORS_SHORT, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  1985. }
  1986. else {
  1987. layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  1988. }
  1989. if (n_expert == 0) {
  1990. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  1991. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  1992. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  1993. // optional MLP bias
  1994. layer.ffn_gate_b = create_tensor(tn(LLM_TENSOR_FFN_GATE, "bias", i), {n_ff}, TENSOR_NOT_REQUIRED);
  1995. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  1996. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, TENSOR_NOT_REQUIRED);
  1997. } else {
  1998. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  1999. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff, n_expert}, TENSOR_NOT_REQUIRED);
  2000. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), { n_ff, n_embd, n_expert}, 0);
  2001. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0);
  2002. // For Granite MoE Shared
  2003. if (hparams.n_ff_shexp > 0) {
  2004. layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), {n_embd, hparams.n_ff_shexp}, 0);
  2005. layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), {n_embd, hparams.n_ff_shexp}, 0);
  2006. layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), {hparams.n_ff_shexp, n_embd}, 0);
  2007. }
  2008. }
  2009. }
  2010. } break;
  2011. case LLM_ARCH_LLADA:
  2012. {
  2013. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, 0);
  2014. // output
  2015. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), { n_embd }, 0);
  2016. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), { n_embd, n_vocab }, TENSOR_NOT_REQUIRED);
  2017. // if output is NULL, init from the input tok embed
  2018. if (output == NULL) {
  2019. output =
  2020. create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, TENSOR_DUPLICATED);
  2021. }
  2022. for (int i = 0; i < n_layer; ++i) {
  2023. auto & layer = layers[i];
  2024. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), { n_embd }, 0);
  2025. // Use separate Q, K, V projections without bias, matching LLaDALlamaBlock
  2026. layer.wq =
  2027. create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), { n_embd, n_embd_head_k * n_head }, 0);
  2028. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), { n_embd, n_embd_k_gqa }, 0);
  2029. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), { n_embd, n_embd_v_gqa }, 0);
  2030. // No bias for QKV projections as per config: include_bias=false, include_qkv_bias=false
  2031. layer.wo =
  2032. create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), { n_embd_head_k * n_head, n_embd }, 0);
  2033. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), { n_embd }, TENSOR_NOT_REQUIRED);
  2034. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), { n_embd }, 0);
  2035. layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), { n_rot / 2 },
  2036. TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  2037. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), { n_embd, n_ff }, 0);
  2038. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd }, 0);
  2039. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), { n_embd, n_ff }, 0);
  2040. // optional MLP bias
  2041. layer.ffn_gate_b =
  2042. create_tensor(tn(LLM_TENSOR_FFN_GATE, "bias", i), { n_ff }, TENSOR_NOT_REQUIRED);
  2043. layer.ffn_down_b =
  2044. create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), { n_embd }, TENSOR_NOT_REQUIRED);
  2045. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), { n_ff }, TENSOR_NOT_REQUIRED);
  2046. }
  2047. }
  2048. break;
  2049. case LLM_ARCH_LLAMA4:
  2050. {
  2051. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2052. // output
  2053. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2054. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  2055. // if output is NULL, init from the input tok embed
  2056. if (output == NULL) {
  2057. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  2058. }
  2059. GGML_ASSERT(hparams.n_moe_layer_step > 0 && "Llama 4 requires n_moe_layer_step > 0");
  2060. for (int i = 0; i < n_layer; ++i) {
  2061. bool is_moe_layer = (i + 1) % hparams.n_moe_layer_step == 0;
  2062. auto & layer = layers[i];
  2063. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2064. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  2065. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  2066. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  2067. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  2068. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2069. layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  2070. if (is_moe_layer) {
  2071. int n_ff_exp = hparams.n_ff_exp;
  2072. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  2073. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff_exp, n_expert}, 0);
  2074. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), { n_ff_exp, n_embd, n_expert}, 0);
  2075. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff_exp, n_expert}, 0);
  2076. // Shared expert
  2077. const int64_t n_ff_shexp = n_ff_exp;
  2078. layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), { n_embd, n_ff_shexp}, 0);
  2079. layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), {n_ff_shexp, n_embd }, 0);
  2080. layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), { n_embd, n_ff_shexp}, 0);
  2081. } else {
  2082. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  2083. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  2084. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2085. }
  2086. }
  2087. } break;
  2088. case LLM_ARCH_DECI:
  2089. {
  2090. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2091. // output
  2092. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2093. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  2094. // if output is NULL, init from the input tok embed
  2095. if (output == NULL) {
  2096. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  2097. }
  2098. for (int i = 0; i < n_layer; ++i) {
  2099. auto & layer = layers[i];
  2100. const int64_t n_embd_k_gqa = hparams.n_embd_k_gqa(i);
  2101. const int64_t n_embd_v_gqa = hparams.n_embd_v_gqa(i);
  2102. const int64_t n_embd_gqa = hparams.n_embd_v_gqa(i);
  2103. const int64_t n_ff = hparams.n_ff(i);
  2104. const int64_t n_head = hparams.n_head(i);
  2105. const int64_t n_head_kv = hparams.n_head_kv(i);
  2106. if (n_head_kv == 0 && n_head > 0) {
  2107. // linear attention for DeciLMCausalModel
  2108. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2109. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2110. }
  2111. else if (n_head_kv > 0) {
  2112. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2113. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  2114. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  2115. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  2116. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  2117. }
  2118. // optional bias tensors
  2119. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2120. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED);
  2121. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED);
  2122. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2123. if (n_ff > 0) {
  2124. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2125. }
  2126. if (hparams.rope_scaling_type_train == LLAMA_ROPE_SCALING_TYPE_LONGROPE) {
  2127. layer.rope_long = create_tensor(tn(LLM_TENSOR_ROPE_FACTORS_LONG, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  2128. layer.rope_short = create_tensor(tn(LLM_TENSOR_ROPE_FACTORS_SHORT, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  2129. }
  2130. else {
  2131. layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  2132. }
  2133. if (n_ff > 0) {
  2134. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  2135. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  2136. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2137. }
  2138. // optional MLP bias
  2139. layer.ffn_gate_b = create_tensor(tn(LLM_TENSOR_FFN_GATE, "bias", i), {n_ff}, TENSOR_NOT_REQUIRED);
  2140. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2141. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, TENSOR_NOT_REQUIRED);
  2142. }
  2143. } break;
  2144. case LLM_ARCH_MINICPM3:
  2145. {
  2146. const int64_t n_embd_head_qk_rope = hparams.n_rot;
  2147. const int64_t n_embd_head_qk_nope = hparams.n_embd_head_k - hparams.n_rot;
  2148. const int64_t q_lora_rank = hparams.n_lora_q;
  2149. const int64_t kv_lora_rank = hparams.n_lora_kv;
  2150. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2151. // output
  2152. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2153. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  2154. // if output is NULL, init from the input tok embed
  2155. if (output == NULL) {
  2156. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  2157. }
  2158. for (int i = 0; i < n_layer; ++i) {
  2159. auto & layer = layers[i];
  2160. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2161. layer.attn_q_a_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_A_NORM, "weight", i), {q_lora_rank}, 0);
  2162. layer.attn_kv_a_norm = create_tensor(tn(LLM_TENSOR_ATTN_KV_A_NORM, "weight", i), {kv_lora_rank}, 0);
  2163. layer.wq_a = create_tensor(tn(LLM_TENSOR_ATTN_Q_A, "weight", i), {n_embd, q_lora_rank}, 0);
  2164. layer.wq_b = create_tensor(tn(LLM_TENSOR_ATTN_Q_B, "weight", i), {q_lora_rank, n_head * n_embd_head_k}, 0);
  2165. layer.wkv_a_mqa = create_tensor(tn(LLM_TENSOR_ATTN_KV_A_MQA, "weight", i), {n_embd, kv_lora_rank + (n_embd_head_qk_rope)}, 0);
  2166. layer.wkv_b = create_tensor(tn(LLM_TENSOR_ATTN_KV_B, "weight", i), {kv_lora_rank, n_head * (n_embd_head_qk_nope + n_embd_head_v)}, 0);
  2167. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), { n_head * ( n_embd_head_v), n_embd}, 0);
  2168. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2169. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  2170. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  2171. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2172. layer.rope_long = create_tensor(tn(LLM_TENSOR_ROPE_FACTORS_LONG, "weight", i), { n_embd_head_qk_rope/2 }, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  2173. layer.rope_short = create_tensor(tn(LLM_TENSOR_ROPE_FACTORS_SHORT, "weight", i), { n_embd_head_qk_rope/2 }, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  2174. }
  2175. } break;
  2176. case LLM_ARCH_GROK:
  2177. {
  2178. if (n_expert == 0) {
  2179. throw std::runtime_error("Grok model cannot have zero experts");
  2180. }
  2181. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2182. // output
  2183. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2184. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  2185. // if output is NULL, init from the input tok embed
  2186. if (output == NULL) {
  2187. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  2188. }
  2189. for (int i = 0; i < n_layer; ++i) {
  2190. auto & layer = layers[i];
  2191. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2192. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  2193. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  2194. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  2195. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2196. layer.attn_out_norm = create_tensor(tn(LLM_TENSOR_ATTN_OUT_NORM, "weight", i), {n_embd}, 0);
  2197. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2198. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  2199. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff, n_expert}, TENSOR_NOT_REQUIRED);
  2200. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), { n_ff, n_embd, n_expert}, 0);
  2201. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0);
  2202. layer.layer_out_norm = create_tensor(tn(LLM_TENSOR_LAYER_OUT_NORM, "weight", i), {n_embd}, 0);
  2203. }
  2204. } break;
  2205. case LLM_ARCH_DBRX:
  2206. {
  2207. if (n_expert == 0) {
  2208. throw std::runtime_error("DBRX model cannot have zero experts");
  2209. }
  2210. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2211. // output
  2212. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2213. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  2214. for (int i = 0; i < n_layer; ++i) {
  2215. auto & layer = layers[i];
  2216. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2217. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, 0);
  2218. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2219. layer.attn_out_norm = create_tensor(tn(LLM_TENSOR_ATTN_OUT_NORM, "weight", i), {n_embd}, 0);
  2220. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  2221. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0);
  2222. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff, n_embd, n_expert}, 0);
  2223. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0);
  2224. }
  2225. } break;
  2226. case LLM_ARCH_BAICHUAN:
  2227. {
  2228. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2229. {
  2230. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2231. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  2232. }
  2233. for (int i = 0; i < n_layer; ++i) {
  2234. auto & layer = layers[i];
  2235. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2236. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  2237. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  2238. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  2239. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2240. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2241. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  2242. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  2243. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2244. }
  2245. } break;
  2246. case LLM_ARCH_FALCON:
  2247. {
  2248. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2249. // output
  2250. {
  2251. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2252. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
  2253. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  2254. if (!output) {
  2255. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); // needs to be on GPU
  2256. }
  2257. }
  2258. for (int i = 0; i < n_layer; ++i) {
  2259. auto & layer = layers[i];
  2260. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2261. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0);
  2262. layer.attn_norm_2 = create_tensor(tn(LLM_TENSOR_ATTN_NORM_2, "weight", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2263. layer.attn_norm_2_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM_2, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2264. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, 0);
  2265. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2266. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  2267. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2268. }
  2269. } break;
  2270. case LLM_ARCH_STARCODER:
  2271. {
  2272. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2273. pos_embd = create_tensor(tn(LLM_TENSOR_POS_EMBD, "weight"), {n_embd, n_ctx_train}, 0);
  2274. // output
  2275. {
  2276. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2277. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
  2278. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  2279. if (!output) {
  2280. // needs to be on GPU
  2281. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  2282. }
  2283. }
  2284. for (int i = 0; i < n_layer; ++i) {
  2285. auto & layer = layers[i];
  2286. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2287. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0);
  2288. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, 0);
  2289. layer.bqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd + 2*n_embd_gqa}, 0);
  2290. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2291. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0);
  2292. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2293. layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd}, 0);
  2294. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  2295. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, 0);
  2296. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2297. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, 0);
  2298. }
  2299. } break;
  2300. case LLM_ARCH_BERT:
  2301. case LLM_ARCH_NOMIC_BERT:
  2302. case LLM_ARCH_NOMIC_BERT_MOE:
  2303. {
  2304. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2305. type_embd = create_tensor(tn(LLM_TENSOR_TOKEN_TYPES, "weight"), {n_embd, n_token_types}, TENSOR_NOT_REQUIRED);
  2306. if (arch == LLM_ARCH_BERT) {
  2307. pos_embd = create_tensor(tn(LLM_TENSOR_POS_EMBD, "weight"), {n_embd, n_ctx_train}, 0);
  2308. cls = create_tensor(tn(LLM_TENSOR_CLS, "weight"), {n_embd, n_embd}, TENSOR_NOT_REQUIRED);
  2309. cls_b = create_tensor(tn(LLM_TENSOR_CLS, "bias"), {n_embd}, TENSOR_NOT_REQUIRED);
  2310. cls_out = create_tensor(tn(LLM_TENSOR_CLS_OUT, "weight"), {n_embd, hparams.n_cls_out}, TENSOR_NOT_REQUIRED);
  2311. cls_out_b = create_tensor(tn(LLM_TENSOR_CLS_OUT, "bias"), {hparams.n_cls_out}, TENSOR_NOT_REQUIRED);
  2312. }
  2313. tok_norm = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "weight"), {n_embd}, 0);
  2314. tok_norm_b = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "bias"), {n_embd}, 0);
  2315. for (int i = 0; i < n_layer; ++i) {
  2316. auto & layer = layers[i];
  2317. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, TENSOR_NOT_REQUIRED);
  2318. layer.bqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd + 2*n_embd_gqa}, TENSOR_NOT_REQUIRED);
  2319. if (!layer.wqkv) {
  2320. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  2321. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, 0);
  2322. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  2323. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, 0);
  2324. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  2325. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, 0);
  2326. }
  2327. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2328. layer.attn_out_norm = create_tensor(tn(LLM_TENSOR_ATTN_OUT_NORM, "weight", i), {n_embd}, 0);
  2329. layer.attn_out_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_OUT_NORM, "bias", i), {n_embd}, 0);
  2330. if (hparams.moe_every_n_layers > 0 && i % hparams.moe_every_n_layers == 1) {
  2331. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0);
  2332. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff, n_expert}, 0);
  2333. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), { n_ff, n_embd, n_expert}, 0);
  2334. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  2335. } else {
  2336. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2337. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  2338. if (arch == LLM_ARCH_BERT || arch == LLM_ARCH_NOMIC_BERT_MOE) {
  2339. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0);
  2340. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, 0);
  2341. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, 0);
  2342. } else {
  2343. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  2344. }
  2345. }
  2346. layer.layer_out_norm = create_tensor(tn(LLM_TENSOR_LAYER_OUT_NORM, "weight", i), {n_embd}, 0);
  2347. layer.layer_out_norm_b = create_tensor(tn(LLM_TENSOR_LAYER_OUT_NORM, "bias", i), {n_embd}, 0);
  2348. }
  2349. } break;
  2350. case LLM_ARCH_NEO_BERT:
  2351. {
  2352. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2353. cls = create_tensor(tn(LLM_TENSOR_CLS, "weight"), {n_embd, n_embd}, TENSOR_NOT_REQUIRED);
  2354. cls_b = create_tensor(tn(LLM_TENSOR_CLS, "bias"), {n_embd}, TENSOR_NOT_REQUIRED);
  2355. cls_out = create_tensor(tn(LLM_TENSOR_CLS_OUT, "weight"), {n_embd, hparams.n_cls_out}, TENSOR_NOT_REQUIRED);
  2356. cls_out_b = create_tensor(tn(LLM_TENSOR_CLS_OUT, "bias"), {hparams.n_cls_out}, TENSOR_NOT_REQUIRED);
  2357. output_norm_enc = create_tensor(tn(LLM_TENSOR_ENC_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2358. for (int i = 0; i < n_layer; ++i) {
  2359. auto & layer = layers[i];
  2360. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2361. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, 0);
  2362. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2363. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2364. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff*2}, 0);
  2365. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  2366. }
  2367. } break;
  2368. case LLM_ARCH_JINA_BERT_V2:
  2369. {
  2370. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); // word_embeddings
  2371. type_embd = create_tensor(tn(LLM_TENSOR_TOKEN_TYPES, "weight"), {n_embd, n_token_types}, 0); // token_type_embeddings
  2372. tok_norm = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "weight"), {n_embd}, 0); // LayerNorm
  2373. tok_norm_b = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "bias"), {n_embd}, 0); //LayerNorm bias
  2374. cls = create_tensor(tn(LLM_TENSOR_CLS, "weight"), {n_embd, 1}, TENSOR_NOT_REQUIRED);
  2375. cls_b = create_tensor(tn(LLM_TENSOR_CLS, "bias"), {1}, TENSOR_NOT_REQUIRED);
  2376. for (int i = 0; i < n_layer; ++i) {
  2377. auto & layer = layers[i]; // JinaBertLayer
  2378. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  2379. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, 0);
  2380. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2381. layer.attn_q_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2382. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  2383. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, 0);
  2384. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2385. layer.attn_k_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2386. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  2387. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, 0);
  2388. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0); //output_dens
  2389. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0); //output_dens
  2390. layer.attn_out_norm = create_tensor(tn(LLM_TENSOR_ATTN_OUT_NORM, "weight", i), {n_embd}, 0); //output_norm
  2391. layer.attn_out_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_OUT_NORM, "bias", i), {n_embd}, 0);
  2392. layer.attn_norm_2 = create_tensor(tn(LLM_TENSOR_ATTN_NORM_2, "weight", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2393. layer.attn_norm_2_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM_2, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2394. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, TENSOR_NOT_REQUIRED);
  2395. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, layer.ffn_gate ? n_ff : n_ff * 2}, 0);
  2396. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  2397. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, 0);
  2398. layer.layer_out_norm = create_tensor(tn(LLM_TENSOR_LAYER_OUT_NORM, "weight", i), {n_embd}, 0);
  2399. layer.layer_out_norm_b = create_tensor(tn(LLM_TENSOR_LAYER_OUT_NORM, "bias", i), {n_embd}, 0);
  2400. }
  2401. } break;
  2402. case LLM_ARCH_BLOOM:
  2403. {
  2404. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2405. tok_norm = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "weight"), {n_embd}, 0);
  2406. tok_norm_b = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "bias"), {n_embd}, 0);
  2407. // output
  2408. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2409. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
  2410. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  2411. // if output is NULL, init from the input tok embed
  2412. if (output == NULL) {
  2413. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  2414. }
  2415. for (int i = 0; i < n_layer; ++i) {
  2416. auto & layer = layers[i];
  2417. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2418. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0);
  2419. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, 0);
  2420. layer.bqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd + 2*n_embd_gqa}, 0);
  2421. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2422. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0);
  2423. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2424. layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd}, 0);
  2425. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  2426. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, 0);
  2427. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2428. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, 0);
  2429. }
  2430. } break;
  2431. case LLM_ARCH_MPT:
  2432. {
  2433. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2434. pos_embd = create_tensor(tn(LLM_TENSOR_POS_EMBD, "weight"), {n_embd, n_ctx_train}, TENSOR_NOT_REQUIRED);
  2435. // output
  2436. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2437. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, TENSOR_NOT_REQUIRED);
  2438. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  2439. if (!output) {
  2440. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); // needs to be on GPU
  2441. }
  2442. for (int i = 0; i < n_layer; ++i) {
  2443. auto & layer = layers[i];
  2444. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2445. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2446. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, 0);
  2447. layer.bqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd + 2*n_embd_gqa}, TENSOR_NOT_REQUIRED);
  2448. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2449. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2450. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2451. layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2452. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  2453. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2454. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2455. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, TENSOR_NOT_REQUIRED);
  2456. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2457. layer.attn_q_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2458. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2459. layer.attn_k_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2460. // AWQ ScaleActivation layer
  2461. layer.ffn_act = create_tensor(tn(LLM_TENSOR_FFN_ACT, "scales", i), {n_ff}, TENSOR_NOT_REQUIRED);
  2462. }
  2463. } break;
  2464. case LLM_ARCH_STABLELM:
  2465. {
  2466. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2467. // output
  2468. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
  2469. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2470. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  2471. for (int i = 0; i < n_layer; ++i) {
  2472. auto & layer = layers[i];
  2473. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2474. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0);
  2475. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  2476. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  2477. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  2478. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2479. // optional bias tensors, present in Stable LM 2 1.6B
  2480. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2481. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED);
  2482. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED);
  2483. // optional q and k layernorms, present in StableLM 2 12B
  2484. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k, n_head}, TENSOR_NOT_REQUIRED);
  2485. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k, n_head_kv}, TENSOR_NOT_REQUIRED);
  2486. // optional FFN norm, not present in StableLM 2 12B which uses parallel residual
  2487. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2488. layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2489. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  2490. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  2491. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2492. }
  2493. } break;
  2494. case LLM_ARCH_QWEN:
  2495. {
  2496. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2497. // output
  2498. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2499. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  2500. for (int i = 0; i < n_layer; ++i) {
  2501. auto & layer = layers[i];
  2502. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2503. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd*3}, 0);
  2504. layer.bqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd*3}, 0);
  2505. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2506. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2507. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff/2}, 0);
  2508. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff/2, n_embd}, 0);
  2509. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff/2}, 0);
  2510. }
  2511. } break;
  2512. case LLM_ARCH_QWEN2:
  2513. case LLM_ARCH_QWEN2VL:
  2514. case LLM_ARCH_DREAM:
  2515. {
  2516. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2517. // output
  2518. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2519. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  2520. output_b = create_tensor(tn(LLM_TENSOR_OUTPUT, "bias"), {n_vocab}, TENSOR_NOT_REQUIRED);
  2521. // if output is NULL, init from the input tok embed
  2522. if (output == NULL) {
  2523. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  2524. }
  2525. for (int i = 0; i < n_layer; ++i) {
  2526. auto & layer = layers[i];
  2527. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2528. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  2529. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  2530. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  2531. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2532. // optional bias tensors
  2533. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, 0);
  2534. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, 0);
  2535. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, 0);
  2536. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2537. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  2538. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  2539. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2540. }
  2541. } break;
  2542. case LLM_ARCH_QWEN2MOE:
  2543. {
  2544. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2545. // output
  2546. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2547. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  2548. for (int i = 0; i < n_layer; ++i) {
  2549. auto & layer = layers[i];
  2550. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2551. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  2552. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  2553. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  2554. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2555. // optional bias tensors
  2556. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2557. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED);
  2558. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED);
  2559. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2560. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  2561. if (n_expert == 0) {
  2562. throw std::runtime_error("n_expert must be > 0 for QWEN2MOE");
  2563. }
  2564. if (n_expert_used == 0) {
  2565. throw std::runtime_error("n_expert_used must be > 0 for QWEN2MOE");
  2566. }
  2567. // MoE branch
  2568. const int64_t n_ff_exp = hparams.n_ff_exp ? hparams.n_ff_exp : n_ff / n_expert_used;
  2569. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);
  2570. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp, n_embd, n_expert}, 0);
  2571. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);
  2572. // Shared expert branch
  2573. const int64_t n_ff_shexp = hparams.n_ff_shexp ? hparams.n_ff_shexp : n_ff;
  2574. layer.ffn_gate_inp_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP_SHEXP, "weight", i), {n_embd}, 0);
  2575. layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), { n_embd, n_ff_shexp}, 0);
  2576. layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), {n_ff_shexp, n_embd}, 0);
  2577. layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), { n_embd, n_ff_shexp}, 0);
  2578. }
  2579. } break;
  2580. case LLM_ARCH_QWEN3:
  2581. {
  2582. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2583. // output
  2584. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2585. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  2586. // if output is NULL, init from the input tok embed
  2587. if (output == NULL) {
  2588. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  2589. }
  2590. for (int i = 0; i < n_layer; ++i) {
  2591. auto & layer = layers[i];
  2592. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2593. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  2594. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  2595. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  2596. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  2597. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0);
  2598. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0);
  2599. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2600. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  2601. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  2602. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2603. }
  2604. } break;
  2605. case LLM_ARCH_QWEN3MOE:
  2606. {
  2607. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2608. // output
  2609. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2610. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  2611. // if output is NULL, init from the input tok embed
  2612. if (output == NULL) {
  2613. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  2614. }
  2615. for (int i = 0; i < n_layer; ++i) {
  2616. auto & layer = layers[i];
  2617. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2618. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  2619. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  2620. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  2621. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  2622. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0);
  2623. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0);
  2624. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2625. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  2626. if (n_expert == 0) {
  2627. throw std::runtime_error("n_expert must be > 0 for QWEN3MOE");
  2628. }
  2629. if (n_expert_used == 0) {
  2630. throw std::runtime_error("n_expert_used must be > 0 for QWEN3MOE");
  2631. }
  2632. // MoE branch
  2633. const int64_t n_ff_exp = hparams.n_ff_exp ? hparams.n_ff_exp : n_ff / n_expert_used;
  2634. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);
  2635. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp, n_embd, n_expert}, 0);
  2636. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);
  2637. }
  2638. } break;
  2639. case LLM_ARCH_PHI2:
  2640. {
  2641. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2642. // output
  2643. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2644. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
  2645. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  2646. output_b = create_tensor(tn(LLM_TENSOR_OUTPUT, "bias"), {n_vocab}, 0);
  2647. for (int i = 0; i < n_layer; ++i) {
  2648. auto & layer = layers[i];
  2649. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2650. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0);
  2651. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, TENSOR_NOT_REQUIRED);
  2652. layer.bqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd + 2*n_embd_gqa}, TENSOR_NOT_REQUIRED);
  2653. if (layer.wqkv == nullptr) {
  2654. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  2655. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, 0);
  2656. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  2657. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, 0);
  2658. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  2659. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, 0);
  2660. }
  2661. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2662. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0);
  2663. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  2664. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, 0);
  2665. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2666. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, 0);
  2667. }
  2668. } break;
  2669. case LLM_ARCH_PHI3:
  2670. {
  2671. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, 0);
  2672. // output
  2673. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), { n_embd }, 0);
  2674. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  2675. // if output is NULL, init from the input tok embed
  2676. if (output == NULL) {
  2677. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  2678. }
  2679. for (int i = 0; i < n_layer; ++i) {
  2680. auto & layer = layers[i];
  2681. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), { n_embd }, 0);
  2682. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), { n_embd, n_embd + 2 * n_embd_gqa }, TENSOR_NOT_REQUIRED);
  2683. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), { n_embd, n_embd }, 0);
  2684. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), { n_embd }, 0);
  2685. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd }, 0);
  2686. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), { n_embd, 2 * n_ff }, 0);
  2687. layer.rope_long = create_tensor(tn(LLM_TENSOR_ROPE_FACTORS_LONG, "weight", i), { n_rot/2 }, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  2688. layer.rope_short = create_tensor(tn(LLM_TENSOR_ROPE_FACTORS_SHORT, "weight", i), { n_rot/2 }, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  2689. }
  2690. } break;
  2691. case LLM_ARCH_PHIMOE:
  2692. {
  2693. const int64_t n_embd_head = n_embd / n_head;
  2694. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, 0);
  2695. // output
  2696. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), { n_embd }, 0);
  2697. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
  2698. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), { n_embd, n_vocab }, 0);
  2699. output_b = create_tensor(tn(LLM_TENSOR_OUTPUT, "bias"), { n_vocab }, 0);
  2700. for (int i = 0; i < n_layer; ++i) {
  2701. auto & layer = layers[i];
  2702. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), { n_embd }, 0);
  2703. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), { n_embd }, 0);
  2704. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), { n_embd, n_embd + 2 * n_embd_gqa }, TENSOR_NOT_REQUIRED);
  2705. if (layer.wqkv == nullptr) {
  2706. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  2707. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, 0);
  2708. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  2709. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, 0);
  2710. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  2711. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, 0);
  2712. }
  2713. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), { n_embd, n_embd }, 0);
  2714. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), { n_embd }, 0);
  2715. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), { n_embd }, 0);
  2716. layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), { n_embd }, 0);
  2717. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  2718. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0);
  2719. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff, n_embd, n_expert}, 0);
  2720. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0);
  2721. layer.rope_long = create_tensor(tn(LLM_TENSOR_ROPE_FACTORS_LONG, "weight", i), { n_embd_head/2 }, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  2722. layer.rope_short = create_tensor(tn(LLM_TENSOR_ROPE_FACTORS_SHORT, "weight", i), { n_embd_head/2 }, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  2723. }
  2724. } break;
  2725. case LLM_ARCH_PLAMO:
  2726. {
  2727. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2728. // output
  2729. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2730. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  2731. for (int i = 0; i < n_layer; ++i) {
  2732. auto & layer = layers[i];
  2733. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2734. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  2735. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  2736. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  2737. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2738. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  2739. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  2740. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2741. }
  2742. } break;
  2743. case LLM_ARCH_PLAMO2:
  2744. {
  2745. const uint32_t d_conv = hparams.ssm_d_conv;
  2746. const uint32_t d_state = hparams.ssm_d_state;
  2747. const uint32_t num_heads = hparams.ssm_dt_rank;
  2748. const uint32_t intermediate_size = hparams.ssm_d_inner;
  2749. const uint32_t head_dim = intermediate_size / num_heads;
  2750. const uint32_t qk_dim = head_dim;
  2751. const uint32_t v_dim = head_dim;
  2752. const int64_t num_attention_heads = hparams.n_head();
  2753. const int64_t q_num_heads = num_attention_heads;
  2754. const int64_t dt_dim = std::max(64, int(hparams.n_embd / 16));
  2755. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2756. // output
  2757. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2758. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  2759. // if output is NULL, init from the input tok embed
  2760. if (output == NULL) {
  2761. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  2762. }
  2763. for (int i = 0; i < n_layer; ++i) {
  2764. auto & layer = layers[i];
  2765. bool is_mamba_layer = hparams.is_recurrent(i);
  2766. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2767. if (is_mamba_layer) {
  2768. layer.ssm_in = create_tensor(tn(LLM_TENSOR_SSM_IN, "weight", i), {n_embd, 2 * intermediate_size}, 0);
  2769. layer.ssm_conv1d = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "weight", i), {d_conv, intermediate_size}, 0);
  2770. layer.ssm_x = create_tensor(tn(LLM_TENSOR_SSM_X, "weight", i), {intermediate_size, dt_dim + 2*d_state}, 0);
  2771. layer.ssm_dt = create_tensor(tn(LLM_TENSOR_SSM_DT, "weight", i), {dt_dim, num_heads}, 0);
  2772. layer.ssm_dt_b = create_tensor(tn(LLM_TENSOR_SSM_DT, "bias", i), {num_heads}, 0);
  2773. layer.ssm_a = create_tensor(tn(LLM_TENSOR_SSM_A, i), {num_heads}, 0);
  2774. layer.ssm_d = create_tensor(tn(LLM_TENSOR_SSM_D, i), {num_heads}, 0);
  2775. layer.ssm_out = create_tensor(tn(LLM_TENSOR_SSM_OUT, "weight", i), {intermediate_size, n_embd}, 0);
  2776. layer.ssm_dt_norm = create_tensor(tn(LLM_TENSOR_SSM_DT_NORM, i), {dt_dim}, 0);
  2777. layer.ssm_b_norm = create_tensor(tn(LLM_TENSOR_SSM_B_NORM, i), {d_state}, 0);
  2778. layer.ssm_c_norm = create_tensor(tn(LLM_TENSOR_SSM_C_NORM, i), {d_state}, 0);
  2779. } else {
  2780. const int64_t num_key_value_heads = hparams.n_head_kv(i);
  2781. const int64_t k_num_heads = num_key_value_heads;
  2782. const int64_t v_num_heads = num_key_value_heads;
  2783. const int64_t q_proj_dim = q_num_heads * qk_dim;
  2784. const int64_t k_proj_dim = k_num_heads * qk_dim;
  2785. const int64_t v_proj_dim = v_num_heads * v_dim;
  2786. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, q_proj_dim + k_proj_dim + v_proj_dim}, 0);
  2787. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {head_dim, num_attention_heads}, 0);
  2788. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {head_dim, k_num_heads}, 0);
  2789. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {q_num_heads * v_dim, n_embd}, 0);
  2790. }
  2791. // All layers have post-attention norm, FFN norm, and FFN tensors
  2792. layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, i), {n_embd}, 0);
  2793. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2794. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  2795. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff * 2}, 0);
  2796. layer.ffn_post_norm = create_tensor(tn(LLM_TENSOR_FFN_POST_NORM, i), {n_embd}, 0);
  2797. }
  2798. } break;
  2799. case LLM_ARCH_GPT2:
  2800. {
  2801. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2802. pos_embd = create_tensor(tn(LLM_TENSOR_POS_EMBD, "weight"), {n_embd, n_ctx_train}, 0);
  2803. // output
  2804. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2805. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
  2806. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  2807. // if output is NULL, init from the input tok embed
  2808. if (output == NULL) {
  2809. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  2810. }
  2811. for (int i = 0; i < n_layer; ++i) {
  2812. auto & layer = layers[i];
  2813. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2814. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0);
  2815. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, 0);
  2816. layer.bqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd + 2*n_embd_gqa}, 0);
  2817. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2818. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0);
  2819. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2820. layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd}, 0);
  2821. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  2822. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, 0);
  2823. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2824. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, 0);
  2825. }
  2826. } break;
  2827. case LLM_ARCH_CODESHELL:
  2828. {
  2829. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  2830. // if tok embd is NULL, init from output
  2831. if (tok_embd == NULL) {
  2832. tok_embd = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  2833. }
  2834. // output
  2835. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2836. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
  2837. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  2838. for (int i = 0; i < n_layer; ++i) {
  2839. auto & layer = layers[i];
  2840. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2841. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0);
  2842. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, 0);
  2843. layer.bqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd + 2*n_embd_gqa}, 0);
  2844. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2845. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0);
  2846. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2847. layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd}, 0);
  2848. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  2849. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, 0);
  2850. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2851. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, 0);
  2852. }
  2853. } break;
  2854. case LLM_ARCH_ORION:
  2855. {
  2856. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2857. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2858. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
  2859. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  2860. for (int i = 0; i < n_layer; ++i) {
  2861. auto & layer = layers[i];
  2862. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2863. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0);
  2864. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  2865. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  2866. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  2867. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2868. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2869. layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd}, 0);
  2870. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  2871. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  2872. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2873. }
  2874. } break;
  2875. case LLM_ARCH_INTERNLM2:
  2876. {
  2877. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2878. // output
  2879. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2880. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  2881. for (int i = 0; i < n_layer; ++i) {
  2882. auto & layer = layers[i];
  2883. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2884. // layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, 0);
  2885. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  2886. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  2887. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  2888. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2889. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2890. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  2891. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  2892. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2893. }
  2894. } break;
  2895. case LLM_ARCH_GEMMA:
  2896. {
  2897. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2898. // output
  2899. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2900. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); // same as tok_embd, duplicated to allow offloading
  2901. for (int i = 0; i < n_layer; ++i) {
  2902. auto & layer = layers[i];
  2903. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2904. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  2905. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  2906. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  2907. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  2908. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2909. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  2910. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2911. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  2912. }
  2913. } break;
  2914. case LLM_ARCH_GEMMA2:
  2915. {
  2916. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2917. // output
  2918. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2919. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); // same as tok_embd, duplicated to allow offloading
  2920. for (int i = 0; i < n_layer; ++i) {
  2921. auto & layer = layers[i];
  2922. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2923. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  2924. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  2925. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  2926. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  2927. layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), {n_embd}, 0);
  2928. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2929. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  2930. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2931. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  2932. layer.ffn_post_norm = create_tensor(tn(LLM_TENSOR_FFN_POST_NORM, "weight", i), {n_embd}, 0);
  2933. }
  2934. } break;
  2935. case LLM_ARCH_GEMMA3:
  2936. {
  2937. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2938. // output
  2939. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2940. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  2941. // if output is NULL, init from the input tok embed
  2942. if (output == NULL) {
  2943. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  2944. }
  2945. for (int i = 0; i < n_layer; ++i) {
  2946. auto & layer = layers[i];
  2947. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2948. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  2949. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  2950. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  2951. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  2952. layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), {n_embd}, 0);
  2953. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0);
  2954. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0);
  2955. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2956. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  2957. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2958. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  2959. layer.ffn_post_norm = create_tensor(tn(LLM_TENSOR_FFN_POST_NORM, "weight", i), {n_embd}, 0);
  2960. }
  2961. } break;
  2962. case LLM_ARCH_GEMMA3N:
  2963. {
  2964. const int64_t n_altup = hparams.n_altup;
  2965. const int64_t laurel_rank = hparams.laurel_rank;
  2966. const int64_t n_embd_altup = hparams.n_embd_altup;
  2967. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  2968. // if output is NULL, init from the input tok embed
  2969. if (output == NULL) {
  2970. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  2971. }
  2972. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2973. tok_embd_per_layer = create_tensor(tn(LLM_TENSOR_PER_LAYER_TOKEN_EMBD, "weight"), {n_embd_altup * n_layer, n_vocab}, 0);
  2974. altup_proj = create_tensor(tn(LLM_TENSOR_ALTUP_PROJ, "weight"), {n_embd, n_embd, n_altup - 1}, 0);
  2975. altup_unembd_proj = create_tensor(tn(LLM_TENSOR_ALTUP_UNEMBD_PROJ, "weight"), {n_embd, n_embd, n_altup - 1}, 0);
  2976. per_layer_model_proj = create_tensor(tn(LLM_TENSOR_PER_LAYER_MODEL_PROJ, "weight"), {n_embd, n_embd_altup * n_layer}, 0);
  2977. per_layer_proj_norm = create_tensor(tn(LLM_TENSOR_PER_LAYER_PROJ_NORM, "weight"), {n_embd_altup}, 0);
  2978. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2979. for (int i = 0; i < n_layer; ++i) {
  2980. auto & layer = layers[i];
  2981. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2982. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  2983. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  2984. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  2985. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  2986. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0);
  2987. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0);
  2988. layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), {n_embd}, 0);
  2989. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2990. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  2991. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2992. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  2993. layer.ffn_post_norm = create_tensor(tn(LLM_TENSOR_FFN_POST_NORM, "weight", i), {n_embd}, 0);
  2994. // altup & laurel
  2995. layer.per_layer_inp_gate = create_tensor(tn(LLM_TENSOR_PER_LAYER_INP_GATE, "weight", i), {n_embd, n_embd_altup}, 0);
  2996. layer.per_layer_proj = create_tensor(tn(LLM_TENSOR_PER_LAYER_PROJ, "weight", i), {n_embd_altup, n_embd}, 0);
  2997. layer.per_layer_post_norm = create_tensor(tn(LLM_TENSOR_PER_LAYER_POST_NORM, "weight", i), {n_embd}, 0);
  2998. layer.altup_correct_coef = create_tensor(tn(LLM_TENSOR_ALTUP_CORRECT_COEF, "weight", i), {n_altup, n_altup}, 0);
  2999. layer.altup_correct_scale = create_tensor(tn(LLM_TENSOR_ALTUP_CORRECT_SCALE, "weight", i), {n_embd}, 0);
  3000. layer.altup_predict_coef = create_tensor(tn(LLM_TENSOR_ALTUP_PREDICT_COEF, "weight", i), {n_altup, n_altup * n_altup}, 0);
  3001. layer.altup_router = create_tensor(tn(LLM_TENSOR_ALTUP_ROUTER, "weight", i), {n_embd, n_altup}, 0);
  3002. layer.altup_router_norm = create_tensor(tn(LLM_TENSOR_ALTUP_ROUTER_NORM, "weight", i), {n_embd}, 0);
  3003. layer.laurel_l = create_tensor(tn(LLM_TENSOR_LAUREL_L, "weight", i), {n_embd, laurel_rank}, 0);
  3004. layer.laurel_r = create_tensor(tn(LLM_TENSOR_LAUREL_R, "weight", i), {laurel_rank, n_embd}, 0);
  3005. layer.laurel_post_norm = create_tensor(tn(LLM_TENSOR_LAUREL_POST_NORM, "weight", i), {n_embd}, 0);
  3006. }
  3007. } break;
  3008. case LLM_ARCH_STARCODER2:
  3009. {
  3010. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3011. // output
  3012. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3013. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
  3014. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  3015. // if output is NULL, init from the input tok embed
  3016. if (output == NULL) {
  3017. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3018. }
  3019. for (int i = 0; i < n_layer; ++i) {
  3020. auto & layer = layers[i];
  3021. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3022. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0);
  3023. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  3024. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  3025. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  3026. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  3027. // optional bias tensors
  3028. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, 0);
  3029. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, 0);
  3030. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, 0);
  3031. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0);
  3032. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3033. layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd}, 0);
  3034. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3035. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3036. // optional bias tensors
  3037. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, 0);
  3038. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP , "bias", i), { n_ff}, 0);
  3039. }
  3040. } break;
  3041. case LLM_ARCH_MAMBA:
  3042. {
  3043. const int64_t d_conv = hparams.ssm_d_conv;
  3044. const int64_t d_inner = hparams.ssm_d_inner;
  3045. const int64_t d_state = hparams.ssm_d_state;
  3046. const int64_t dt_rank = hparams.ssm_dt_rank;
  3047. // only an expansion factor of 2 is supported for now
  3048. if (2 * n_embd != d_inner) {
  3049. throw std::runtime_error("only an expansion factor of 2 is supported for now");
  3050. }
  3051. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3052. // output
  3053. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3054. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  3055. // if output is NULL, init from the input tok embed, duplicated to allow offloading
  3056. if (output == NULL) {
  3057. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3058. }
  3059. for (int i = 0; i < n_layer; ++i) {
  3060. auto & layer = layers[i];
  3061. // norm
  3062. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3063. layer.ssm_in = create_tensor(tn(LLM_TENSOR_SSM_IN, "weight", i), {n_embd, 2*d_inner}, 0);
  3064. layer.ssm_conv1d = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "weight", i), {d_conv, d_inner}, 0);
  3065. layer.ssm_conv1d_b = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "bias", i), {d_inner}, 0);
  3066. layer.ssm_x = create_tensor(tn(LLM_TENSOR_SSM_X, "weight", i), {d_inner, dt_rank + 2*d_state}, 0);
  3067. layer.ssm_dt = create_tensor(tn(LLM_TENSOR_SSM_DT, "weight", i), {dt_rank, d_inner}, 0);
  3068. layer.ssm_dt_b = create_tensor(tn(LLM_TENSOR_SSM_DT, "bias", i), {d_inner}, 0);
  3069. // no "weight" suffix for these
  3070. layer.ssm_a = create_tensor(tn(LLM_TENSOR_SSM_A, i), {d_state, d_inner}, 0);
  3071. layer.ssm_d = create_tensor(tn(LLM_TENSOR_SSM_D, i), {d_inner}, 0);
  3072. // out_proj
  3073. layer.ssm_out = create_tensor(tn(LLM_TENSOR_SSM_OUT, "weight", i), {d_inner, n_embd}, 0);
  3074. }
  3075. } break;
  3076. case LLM_ARCH_MAMBA2:
  3077. {
  3078. const int64_t d_conv = hparams.ssm_d_conv;
  3079. const int64_t d_inner = hparams.ssm_d_inner;
  3080. const int64_t d_state = hparams.ssm_d_state;
  3081. const int64_t n_head = hparams.ssm_dt_rank;
  3082. const int64_t n_group = hparams.ssm_n_group;
  3083. const int64_t d_in_proj = 2*d_inner + 2*n_group*d_state + n_head;
  3084. // only an expansion factor of 2 is supported for now
  3085. GGML_ASSERT(2 * n_embd == d_inner);
  3086. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3087. // output
  3088. {
  3089. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3090. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  3091. // if output is NULL, init from the input tok embed, duplicated to allow offloading
  3092. if (output == NULL) {
  3093. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3094. }
  3095. }
  3096. for (int i = 0; i < n_layer; ++i) {
  3097. auto & layer = layers[i];
  3098. // norm
  3099. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3100. layer.ssm_in = create_tensor(tn(LLM_TENSOR_SSM_IN, "weight", i), {n_embd, d_in_proj}, 0);
  3101. layer.ssm_conv1d = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "weight", i), {d_conv, d_inner + 2*n_group*d_state}, 0);
  3102. layer.ssm_conv1d_b = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "bias", i), {d_inner + 2*n_group*d_state}, 0);
  3103. layer.ssm_dt_b = create_tensor(tn(LLM_TENSOR_SSM_DT, "bias", i), {n_head}, 0);
  3104. // no "weight" suffix for these
  3105. layer.ssm_a = create_tensor(tn(LLM_TENSOR_SSM_A, i), {1, n_head}, 0);
  3106. layer.ssm_d = create_tensor(tn(LLM_TENSOR_SSM_D, i), {1, n_head}, 0);
  3107. layer.ssm_norm = create_tensor(tn(LLM_TENSOR_SSM_NORM, "weight", i), {d_inner / n_group, n_group}, 0);
  3108. // out_proj
  3109. layer.ssm_out = create_tensor(tn(LLM_TENSOR_SSM_OUT, "weight", i), {d_inner, n_embd}, 0);
  3110. }
  3111. } break;
  3112. case LLM_ARCH_JAMBA:
  3113. {
  3114. const int64_t d_conv = hparams.ssm_d_conv;
  3115. const int64_t d_inner = hparams.ssm_d_inner;
  3116. const int64_t d_state = hparams.ssm_d_state;
  3117. const int64_t dt_rank = hparams.ssm_dt_rank;
  3118. // only an expansion factor of 2 is supported for now
  3119. GGML_ASSERT(2 * n_embd == d_inner);
  3120. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3121. // output
  3122. {
  3123. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3124. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  3125. // if output is NULL, init from the input tok embed, duplicated to allow offloading
  3126. if (output == NULL) {
  3127. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3128. }
  3129. }
  3130. for (int i = 0; i < n_layer; ++i) {
  3131. const int64_t n_head_kv = hparams.n_head_kv(i);
  3132. const int64_t n_embd_gqa = hparams.n_embd_v_gqa(i);
  3133. auto & layer = layers[i];
  3134. // norm
  3135. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3136. if (n_head_kv == 0) {
  3137. // Mamba layer
  3138. layer.ssm_in = create_tensor(tn(LLM_TENSOR_SSM_IN, "weight", i), {n_embd, 2*d_inner}, 0);
  3139. layer.ssm_conv1d = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "weight", i), {d_conv, d_inner}, 0);
  3140. layer.ssm_conv1d_b = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "bias", i), {d_inner}, 0);
  3141. layer.ssm_x = create_tensor(tn(LLM_TENSOR_SSM_X, "weight", i), {d_inner, dt_rank + 2*d_state}, 0);
  3142. layer.ssm_dt_norm = create_tensor(tn(LLM_TENSOR_SSM_DT_NORM, "weight", i), {dt_rank}, 0);
  3143. layer.ssm_dt = create_tensor(tn(LLM_TENSOR_SSM_DT, "weight", i), {dt_rank, d_inner}, 0);
  3144. layer.ssm_dt_b = create_tensor(tn(LLM_TENSOR_SSM_DT, "bias", i), {d_inner}, 0);
  3145. layer.ssm_b_norm = create_tensor(tn(LLM_TENSOR_SSM_B_NORM, "weight", i), {d_state}, 0);
  3146. layer.ssm_c_norm = create_tensor(tn(LLM_TENSOR_SSM_C_NORM, "weight", i), {d_state}, 0);
  3147. // no "weight" suffix for these
  3148. layer.ssm_a = create_tensor(tn(LLM_TENSOR_SSM_A, i), {d_state, d_inner}, 0);
  3149. layer.ssm_d = create_tensor(tn(LLM_TENSOR_SSM_D, i), {d_inner}, 0);
  3150. // out_proj
  3151. layer.ssm_out = create_tensor(tn(LLM_TENSOR_SSM_OUT, "weight", i), {d_inner, n_embd}, 0);
  3152. } else {
  3153. // Attention layers
  3154. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  3155. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  3156. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  3157. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  3158. }
  3159. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3160. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, TENSOR_NOT_REQUIRED);
  3161. if (layer.ffn_gate_inp) {
  3162. // MoE
  3163. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0);
  3164. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff, n_embd, n_expert}, 0);
  3165. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0);
  3166. } else {
  3167. // FFN (no MoE)
  3168. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  3169. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  3170. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3171. }
  3172. }
  3173. } break;
  3174. case LLM_ARCH_GRANITE_HYBRID:
  3175. {
  3176. // mamba2 Mixer SSM params
  3177. // NOTE: int64_t for tensor dimensions
  3178. const int64_t d_conv = hparams.ssm_d_conv;
  3179. const int64_t d_inner = hparams.ssm_d_inner;
  3180. const int64_t d_state = hparams.ssm_d_state;
  3181. const int64_t n_ssm_head = hparams.ssm_dt_rank;
  3182. const int64_t n_group = hparams.ssm_n_group;
  3183. const int64_t d_in_proj = 2*d_inner + 2*n_group*d_state + n_ssm_head;
  3184. // only an expansion factor of 2 is supported for now
  3185. GGML_ASSERT(2 * n_embd == d_inner);
  3186. // embeddings
  3187. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3188. // output
  3189. {
  3190. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3191. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  3192. // if output is NULL, init from the input tok embed, duplicated to allow offloading
  3193. if (output == NULL) {
  3194. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3195. }
  3196. }
  3197. for (int i = 0; i < n_layer; ++i) {
  3198. auto & layer = layers[i];
  3199. // norm
  3200. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3201. if (hparams.is_recurrent(i)) {
  3202. // ssm layers
  3203. layer.ssm_in = create_tensor(tn(LLM_TENSOR_SSM_IN, "weight", i), {n_embd, d_in_proj}, 0);
  3204. layer.ssm_conv1d = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "weight", i), {d_conv, d_inner + 2*n_group*d_state}, 0);
  3205. layer.ssm_conv1d_b = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "bias", i), {d_inner + 2*n_group*d_state}, TENSOR_NOT_REQUIRED);
  3206. layer.ssm_dt_b = create_tensor(tn(LLM_TENSOR_SSM_DT, "bias", i), {n_ssm_head}, 0);
  3207. // no "weight" suffix for these
  3208. layer.ssm_a = create_tensor(tn(LLM_TENSOR_SSM_A, i), {1, n_ssm_head}, 0);
  3209. layer.ssm_d = create_tensor(tn(LLM_TENSOR_SSM_D, i), {1, n_ssm_head}, 0);
  3210. layer.ssm_norm = create_tensor(tn(LLM_TENSOR_SSM_NORM, "weight", i), {d_inner / n_group, n_group}, 0);
  3211. // out_proj
  3212. layer.ssm_out = create_tensor(tn(LLM_TENSOR_SSM_OUT, "weight", i), {d_inner, n_embd}, 0);
  3213. } else {
  3214. // attention layers (with optional bias)
  3215. const int64_t n_head_i = hparams.n_head(i);
  3216. const int64_t n_embd_k_gqa_i = hparams.n_embd_k_gqa(i);
  3217. const int64_t n_embd_v_gqa_i = hparams.n_embd_v_gqa(i);
  3218. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head_i}, 0);
  3219. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa_i}, 0);
  3220. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa_i}, 0);
  3221. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head_i, n_embd}, 0);
  3222. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  3223. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_k_gqa_i}, TENSOR_NOT_REQUIRED);
  3224. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_v_gqa_i}, TENSOR_NOT_REQUIRED);
  3225. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  3226. }
  3227. // feed forward (w/ optional biases)
  3228. if (n_expert > 0) {
  3229. // MoE FFN
  3230. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3231. layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  3232. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  3233. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff, n_expert}, TENSOR_NOT_REQUIRED);
  3234. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), { n_ff, n_embd, n_expert}, 0);
  3235. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0);
  3236. // For Granite MoE Shared
  3237. if (hparams.n_ff_shexp > 0) {
  3238. layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), {n_embd, hparams.n_ff_shexp}, 0);
  3239. layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), {n_embd, hparams.n_ff_shexp}, 0);
  3240. layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), {hparams.n_ff_shexp, n_embd}, 0);
  3241. }
  3242. } else {
  3243. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3244. layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  3245. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  3246. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3247. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3248. layer.ffn_gate_b = create_tensor(tn(LLM_TENSOR_FFN_GATE, "bias", i), {n_ff}, TENSOR_NOT_REQUIRED);
  3249. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  3250. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, TENSOR_NOT_REQUIRED);
  3251. }
  3252. }
  3253. } break;
  3254. case LLM_ARCH_XVERSE:
  3255. {
  3256. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3257. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3258. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  3259. for (int i = 0; i < n_layer; ++i) {
  3260. auto & layer = layers[i];
  3261. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3262. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  3263. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  3264. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  3265. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  3266. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3267. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  3268. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3269. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3270. }
  3271. } break;
  3272. case LLM_ARCH_COMMAND_R:
  3273. {
  3274. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3275. // output
  3276. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3277. // init output from the input tok embed
  3278. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3279. for (int i = 0; i < n_layer; ++i) {
  3280. auto & layer = layers[i];
  3281. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3282. if (n_layer >= 64){
  3283. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k, n_head}, 0);
  3284. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k, n_head_kv}, 0);
  3285. }
  3286. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  3287. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  3288. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  3289. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  3290. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  3291. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3292. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3293. }
  3294. } break;
  3295. case LLM_ARCH_COHERE2:
  3296. {
  3297. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, 0);
  3298. // output
  3299. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), { n_embd }, 0);
  3300. // init output from the input tok embed
  3301. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab },
  3302. TENSOR_DUPLICATED);
  3303. for (int i = 0; i < n_layer; ++i) {
  3304. auto & layer = layers[i];
  3305. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), { n_embd }, 0);
  3306. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), { n_embd, n_embd }, 0);
  3307. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), { n_embd, n_embd_gqa }, 0);
  3308. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), { n_embd, n_embd_gqa }, 0);
  3309. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), { n_embd, n_embd }, 0);
  3310. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), { n_embd, n_ff }, 0);
  3311. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd }, 0);
  3312. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), { n_embd, n_ff }, 0);
  3313. }
  3314. }
  3315. break;
  3316. case LLM_ARCH_OLMO: // adapted from LLM_ARCH_LLAMA with norm params removed
  3317. {
  3318. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3319. // output
  3320. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  3321. // if output is NULL, init from the input tok embed
  3322. if (output == NULL) {
  3323. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3324. }
  3325. for (int i = 0; i < n_layer; ++i) {
  3326. auto & layer = layers[i];
  3327. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  3328. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  3329. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  3330. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  3331. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  3332. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3333. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3334. }
  3335. } break;
  3336. case LLM_ARCH_OLMO2:
  3337. {
  3338. const int64_t n_embd_head = n_embd / n_head;
  3339. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3340. // output
  3341. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3342. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  3343. for (int i = 0; i < n_layer; ++i) {
  3344. auto & layer = layers[i];
  3345. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  3346. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  3347. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  3348. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  3349. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd}, 0);
  3350. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_head_kv * n_embd_head}, 0);
  3351. layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), {n_embd}, 0);
  3352. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  3353. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3354. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3355. layer.ffn_post_norm = create_tensor(tn(LLM_TENSOR_FFN_POST_NORM, "weight", i), {n_embd}, 0);
  3356. }
  3357. } break;
  3358. case LLM_ARCH_OLMOE:
  3359. {
  3360. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3361. // output
  3362. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3363. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  3364. for (int i = 0; i < n_layer; ++i) {
  3365. auto & layer = layers[i];
  3366. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3367. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  3368. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  3369. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  3370. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  3371. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd}, 0);
  3372. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd}, 0);
  3373. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3374. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  3375. if (n_expert == 0) {
  3376. throw std::runtime_error("n_expert must be > 0");
  3377. }
  3378. if (n_expert_used == 0) {
  3379. throw std::runtime_error("n_expert_used must be > 0");
  3380. }
  3381. // MoE branch
  3382. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0);
  3383. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff, n_embd, n_expert}, 0);
  3384. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0);
  3385. }
  3386. } break;
  3387. case LLM_ARCH_OPENELM:
  3388. {
  3389. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3390. // output
  3391. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3392. // init output from the input tok embed
  3393. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3394. for (int i = 0; i < n_layer; ++i) {
  3395. const int64_t n_head = hparams.n_head(i);
  3396. const int64_t n_head_qkv = 2*hparams.n_head_kv(i) + n_head;
  3397. const int64_t n_ff = hparams.n_ff(i);
  3398. auto & layer = layers[i];
  3399. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3400. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_head_qkv*n_embd_head_k}, 0);
  3401. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0);
  3402. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0);
  3403. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_head*n_embd_head_k, n_embd}, 0);
  3404. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3405. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  3406. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  3407. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3408. }
  3409. } break;
  3410. case LLM_ARCH_GPTNEOX:
  3411. {
  3412. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3413. // output
  3414. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3415. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
  3416. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  3417. for (int i = 0; i < n_layer; ++i) {
  3418. auto & layer = layers[i];
  3419. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3420. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0);
  3421. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, 0);
  3422. layer.bqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd + 2*n_embd_gqa}, 0);
  3423. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  3424. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0);
  3425. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3426. layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd}, 0);
  3427. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  3428. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, 0);
  3429. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3430. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, 0);
  3431. }
  3432. } break;
  3433. case LLM_ARCH_ARCTIC:
  3434. {
  3435. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3436. // output
  3437. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3438. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  3439. // if output is NULL, init from the input tok embed
  3440. if (output == NULL) {
  3441. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3442. }
  3443. for (int i = 0; i < n_layer; ++i) {
  3444. auto & layer = layers[i];
  3445. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3446. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  3447. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  3448. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  3449. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  3450. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3451. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_embd}, 0);
  3452. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_embd, n_embd}, 0);
  3453. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_embd}, 0);
  3454. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  3455. layer.ffn_norm_exps = create_tensor(tn(LLM_TENSOR_FFN_NORM_EXPS, "weight", i), {n_embd}, 0);
  3456. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff, n_expert}, false);
  3457. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), { n_ff, n_embd, n_expert}, 0);
  3458. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0);
  3459. }
  3460. } break;
  3461. case LLM_ARCH_DEEPSEEK:
  3462. {
  3463. const int64_t n_ff_exp = hparams.n_ff_exp;
  3464. const int64_t n_expert_shared = hparams.n_expert_shared;
  3465. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3466. // output
  3467. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3468. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  3469. for (int i = 0; i < n_layer; ++i) {
  3470. auto & layer = layers[i];
  3471. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3472. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  3473. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  3474. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  3475. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  3476. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3477. if (i < (int) hparams.n_layer_dense_lead) {
  3478. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  3479. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3480. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3481. } else {
  3482. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  3483. if (n_expert == 0) {
  3484. throw std::runtime_error("n_expert must be > 0");
  3485. }
  3486. if (n_expert_used == 0) {
  3487. throw std::runtime_error("n_expert_used must be > 0");
  3488. }
  3489. // MoE branch
  3490. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);
  3491. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp, n_embd, n_expert}, 0);
  3492. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);
  3493. // Shared expert branch
  3494. layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), {n_embd, n_ff_exp * n_expert_shared}, 0);
  3495. layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), { n_ff_exp * n_expert_shared, n_embd}, 0);
  3496. layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), {n_embd, n_ff_exp * n_expert_shared}, 0);
  3497. }
  3498. }
  3499. } break;
  3500. case LLM_ARCH_DEEPSEEK2:
  3501. {
  3502. const bool is_lite = (hparams.n_layer == 27);
  3503. const bool is_mla = (hparams.n_embd_head_k_mla != 0 && hparams.n_embd_head_v_mla != 0);
  3504. // note: these are the actual head sizes you get when treating as MHA or after "decompression" using wv_b for MLA
  3505. const int64_t n_embd_head_k_mla = is_mla ? hparams.n_embd_head_k_mla : hparams.n_embd_head_k;
  3506. const int64_t n_embd_head_v_mla = is_mla ? hparams.n_embd_head_v_mla : hparams.n_embd_head_v;
  3507. const int64_t n_embd_head_qk_rope = hparams.n_rot;
  3508. const int64_t n_embd_head_qk_nope = n_embd_head_k_mla - n_embd_head_qk_rope;
  3509. const int64_t q_lora_rank = hparams.n_lora_q;
  3510. const int64_t kv_lora_rank = hparams.n_lora_kv;
  3511. const int64_t n_ff_exp = hparams.n_ff_exp;
  3512. const int64_t n_expert_shared = hparams.n_expert_shared;
  3513. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3514. // output
  3515. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3516. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  3517. for (int i = 0; i < n_layer; ++i) {
  3518. auto & layer = layers[i];
  3519. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3520. if (!is_lite) {
  3521. layer.attn_q_a_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_A_NORM, "weight", i), {q_lora_rank}, 0);
  3522. }
  3523. layer.attn_kv_a_norm = create_tensor(tn(LLM_TENSOR_ATTN_KV_A_NORM, "weight", i), {kv_lora_rank}, 0);
  3524. if (!is_lite) {
  3525. layer.wq_a = create_tensor(tn(LLM_TENSOR_ATTN_Q_A, "weight", i), {n_embd, q_lora_rank}, 0);
  3526. layer.wq_b = create_tensor(tn(LLM_TENSOR_ATTN_Q_B, "weight", i), {q_lora_rank, n_head * n_embd_head_k_mla}, 0);
  3527. } else {
  3528. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_head * n_embd_head_k_mla}, 0);
  3529. }
  3530. layer.wkv_a_mqa = create_tensor(tn(LLM_TENSOR_ATTN_KV_A_MQA, "weight", i), {n_embd, kv_lora_rank + n_embd_head_qk_rope}, 0);
  3531. // note: only old legacy GGUF files will have the unsplit wkv_b tensor in
  3532. if (is_mla) {
  3533. layer.wk_b = create_tensor(tn(LLM_TENSOR_ATTN_K_B, "weight", i), {n_embd_head_qk_nope, kv_lora_rank, n_head}, 0);
  3534. layer.wv_b = create_tensor(tn(LLM_TENSOR_ATTN_V_B, "weight", i), {kv_lora_rank, n_embd_head_v_mla, n_head}, 0);
  3535. } else {
  3536. layer.wkv_b = create_tensor(tn(LLM_TENSOR_ATTN_KV_B, "weight", i), {kv_lora_rank, n_head * (n_embd_head_qk_nope + n_embd_head_v_mla)}, 0);
  3537. }
  3538. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_head * n_embd_head_v_mla, n_embd}, 0);
  3539. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3540. if (i < (int) hparams.n_layer_dense_lead) {
  3541. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  3542. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3543. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3544. } else {
  3545. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  3546. layer.ffn_exp_probs_b = create_tensor(tn(LLM_TENSOR_FFN_EXP_PROBS_B, "bias", i), {n_expert}, TENSOR_NOT_REQUIRED);
  3547. if (n_expert == 0) {
  3548. throw std::runtime_error("n_expert must be > 0");
  3549. }
  3550. if (n_expert_used == 0) {
  3551. throw std::runtime_error("n_expert_used must be > 0");
  3552. }
  3553. // MoE branch
  3554. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);
  3555. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp, n_embd, n_expert}, 0);
  3556. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);
  3557. // Shared expert branch
  3558. layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), {n_embd, n_ff_exp * n_expert_shared}, 0);
  3559. layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), { n_ff_exp * n_expert_shared, n_embd}, 0);
  3560. layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), {n_embd, n_ff_exp * n_expert_shared}, 0);
  3561. }
  3562. }
  3563. } break;
  3564. case LLM_ARCH_PLM:
  3565. {
  3566. const int64_t n_embd_head_qk_rope = hparams.n_rot;
  3567. const int64_t n_embd_head_qk_nope = hparams.n_embd_head_k - hparams.n_rot;
  3568. const int64_t kv_lora_rank = hparams.n_lora_kv;
  3569. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3570. // output
  3571. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3572. // output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  3573. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3574. for (int i = 0; i < n_layer; ++i) {
  3575. auto & layer = layers[i];
  3576. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3577. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  3578. layer.wkv_a_mqa = create_tensor(tn(LLM_TENSOR_ATTN_KV_A_MQA, "weight", i), {n_embd, kv_lora_rank + (n_embd_head_qk_rope)}, 0);
  3579. layer.attn_kv_a_norm = create_tensor(tn(LLM_TENSOR_ATTN_KV_A_NORM, "weight", i), {kv_lora_rank}, 0);
  3580. layer.wkv_b = create_tensor(tn(LLM_TENSOR_ATTN_KV_B, "weight", i), {kv_lora_rank, n_head * (n_embd_head_qk_nope + n_embd_head_v)}, 0);
  3581. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), { n_head * ( n_embd_head_v), n_embd}, 0);
  3582. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3583. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3584. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3585. }
  3586. } break;
  3587. case LLM_ARCH_BITNET:
  3588. {
  3589. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3590. // output
  3591. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3592. for (int i = 0; i < n_layer; ++i) {
  3593. auto & layer = layers[i];
  3594. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3595. layer.attn_sub_norm = create_tensor(tn(LLM_TENSOR_ATTN_SUB_NORM, "weight", i), {n_embd}, 0);
  3596. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  3597. layer.wq_scale = create_tensor(tn(LLM_TENSOR_ATTN_Q, "scale", i), {1}, TENSOR_NOT_REQUIRED);
  3598. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  3599. layer.wk_scale = create_tensor(tn(LLM_TENSOR_ATTN_K, "scale", i), {1}, TENSOR_NOT_REQUIRED);
  3600. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  3601. layer.wv_scale = create_tensor(tn(LLM_TENSOR_ATTN_V, "scale", i), {1}, TENSOR_NOT_REQUIRED);
  3602. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  3603. layer.wo_scale = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "scale", i), {1}, TENSOR_NOT_REQUIRED);
  3604. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3605. layer.ffn_sub_norm = create_tensor(tn(LLM_TENSOR_FFN_SUB_NORM, "weight", i), {n_ff}, 0);
  3606. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  3607. layer.ffn_gate_scale = create_tensor(tn(LLM_TENSOR_FFN_GATE, "scale", i), {1}, TENSOR_NOT_REQUIRED);
  3608. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  3609. layer.ffn_down_scale = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "scale", i), {1}, TENSOR_NOT_REQUIRED);
  3610. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3611. layer.ffn_up_scale = create_tensor(tn(LLM_TENSOR_FFN_UP, "scale", i), {1}, TENSOR_NOT_REQUIRED);
  3612. }
  3613. } break;
  3614. case LLM_ARCH_T5:
  3615. {
  3616. const auto n_rel_attn_bkts = hparams.n_rel_attn_bkts;
  3617. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3618. // output
  3619. output_norm_enc = create_tensor(tn(LLM_TENSOR_ENC_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3620. output_norm = create_tensor(tn(LLM_TENSOR_DEC_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3621. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  3622. // if output is NULL, init from the input tok embed
  3623. if (output == NULL) {
  3624. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3625. }
  3626. for (int i = 0; i < n_layer; ++i) {
  3627. auto & layer = layers[i];
  3628. layer.attn_norm_enc = create_tensor(tn(LLM_TENSOR_ENC_ATTN_NORM, "weight", i), {n_embd}, 0);
  3629. layer.attn_rel_b_enc = create_tensor(tn(LLM_TENSOR_ENC_ATTN_REL_B, "weight", i), {n_head, n_rel_attn_bkts}, TENSOR_NOT_REQUIRED);
  3630. layer.wq_enc = create_tensor(tn(LLM_TENSOR_ENC_ATTN_Q, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  3631. layer.wk_enc = create_tensor(tn(LLM_TENSOR_ENC_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  3632. layer.wv_enc = create_tensor(tn(LLM_TENSOR_ENC_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  3633. layer.wo_enc = create_tensor(tn(LLM_TENSOR_ENC_ATTN_OUT, "weight", i), {n_embd_v_gqa, n_embd}, 0);
  3634. layer.ffn_norm_enc = create_tensor(tn(LLM_TENSOR_ENC_FFN_NORM, "weight", i), {n_embd}, 0);
  3635. layer.ffn_gate_enc = create_tensor(tn(LLM_TENSOR_ENC_FFN_GATE, "weight", i), {n_embd, n_ff}, TENSOR_NOT_REQUIRED);
  3636. layer.ffn_down_enc = create_tensor(tn(LLM_TENSOR_ENC_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3637. layer.ffn_up_enc = create_tensor(tn(LLM_TENSOR_ENC_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3638. layer.attn_norm = create_tensor(tn(LLM_TENSOR_DEC_ATTN_NORM, "weight", i), {n_embd}, 0);
  3639. layer.attn_rel_b = create_tensor(tn(LLM_TENSOR_DEC_ATTN_REL_B, "weight", i), {n_head, n_rel_attn_bkts}, TENSOR_NOT_REQUIRED);
  3640. layer.wq = create_tensor(tn(LLM_TENSOR_DEC_ATTN_Q, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  3641. layer.wk = create_tensor(tn(LLM_TENSOR_DEC_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  3642. layer.wv = create_tensor(tn(LLM_TENSOR_DEC_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  3643. layer.wo = create_tensor(tn(LLM_TENSOR_DEC_ATTN_OUT, "weight", i), {n_embd_v_gqa, n_embd}, 0);
  3644. layer.attn_norm_cross = create_tensor(tn(LLM_TENSOR_DEC_CROSS_ATTN_NORM, "weight", i), {n_embd}, 0);
  3645. // this tensor seems to be unused in HF transformers implementation
  3646. layer.attn_rel_b_cross = create_tensor(tn(LLM_TENSOR_DEC_CROSS_ATTN_REL_B, "weight", i), {n_head, n_rel_attn_bkts}, TENSOR_NOT_REQUIRED);
  3647. layer.wq_cross = create_tensor(tn(LLM_TENSOR_DEC_CROSS_ATTN_Q, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  3648. layer.wk_cross = create_tensor(tn(LLM_TENSOR_DEC_CROSS_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  3649. layer.wv_cross = create_tensor(tn(LLM_TENSOR_DEC_CROSS_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  3650. layer.wo_cross = create_tensor(tn(LLM_TENSOR_DEC_CROSS_ATTN_OUT, "weight", i), {n_embd_v_gqa, n_embd}, 0);
  3651. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_DEC_FFN_NORM, "weight", i), {n_embd}, 0);
  3652. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_DEC_FFN_GATE, "weight", i), {n_embd, n_ff}, TENSOR_NOT_REQUIRED);
  3653. layer.ffn_down = create_tensor(tn(LLM_TENSOR_DEC_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3654. layer.ffn_up = create_tensor(tn(LLM_TENSOR_DEC_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3655. }
  3656. } break;
  3657. case LLM_ARCH_T5ENCODER:
  3658. {
  3659. const auto n_rel_attn_bkts = hparams.n_rel_attn_bkts;
  3660. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3661. // output
  3662. output_norm_enc = create_tensor(tn(LLM_TENSOR_ENC_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3663. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  3664. // if output is NULL, init from the input tok embed
  3665. if (output == NULL) {
  3666. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3667. }
  3668. for (int i = 0; i < n_layer; ++i) {
  3669. auto & layer = layers[i];
  3670. layer.attn_norm_enc = create_tensor(tn(LLM_TENSOR_ENC_ATTN_NORM, "weight", i), {n_embd}, 0);
  3671. layer.attn_rel_b_enc = create_tensor(tn(LLM_TENSOR_ENC_ATTN_REL_B, "weight", i), {n_head, n_rel_attn_bkts}, TENSOR_NOT_REQUIRED);
  3672. layer.wq_enc = create_tensor(tn(LLM_TENSOR_ENC_ATTN_Q, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  3673. layer.wk_enc = create_tensor(tn(LLM_TENSOR_ENC_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  3674. layer.wv_enc = create_tensor(tn(LLM_TENSOR_ENC_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  3675. layer.wo_enc = create_tensor(tn(LLM_TENSOR_ENC_ATTN_OUT, "weight", i), {n_embd_v_gqa, n_embd}, 0);
  3676. layer.ffn_norm_enc = create_tensor(tn(LLM_TENSOR_ENC_FFN_NORM, "weight", i), {n_embd}, 0);
  3677. layer.ffn_gate_enc = create_tensor(tn(LLM_TENSOR_ENC_FFN_GATE, "weight", i), {n_embd, n_ff}, TENSOR_NOT_REQUIRED);
  3678. layer.ffn_down_enc = create_tensor(tn(LLM_TENSOR_ENC_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3679. layer.ffn_up_enc = create_tensor(tn(LLM_TENSOR_ENC_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3680. }
  3681. } break;
  3682. case LLM_ARCH_JAIS:
  3683. {
  3684. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3685. // output
  3686. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3687. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
  3688. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  3689. for (int i = 0; i < n_layer; ++i) {
  3690. auto & layer = layers[i];
  3691. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3692. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0);
  3693. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, 0);
  3694. layer.bqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd + 2*n_embd_gqa}, 0);
  3695. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  3696. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0);
  3697. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3698. layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd}, 0);
  3699. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  3700. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, 0);
  3701. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  3702. layer.ffn_gate_b = create_tensor(tn(LLM_TENSOR_FFN_GATE, "bias", i), {n_ff}, 0);
  3703. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3704. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, 0);
  3705. }
  3706. } break;
  3707. case LLM_ARCH_CHATGLM:
  3708. {
  3709. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3710. // output
  3711. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3712. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  3713. // if output is NULL, init from the input tok embed
  3714. if (output == NULL) {
  3715. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3716. }
  3717. for (int i = 0; i < n_layer; ++i) {
  3718. auto & layer = layers[i];
  3719. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3720. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, TENSOR_NOT_REQUIRED);
  3721. layer.bqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd + 2*n_embd_gqa}, TENSOR_NOT_REQUIRED);
  3722. if (layer.wqkv == nullptr) {
  3723. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  3724. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  3725. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  3726. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  3727. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED);
  3728. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED);
  3729. }
  3730. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  3731. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3732. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff * 2}, 0);
  3733. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  3734. }
  3735. } break;
  3736. case LLM_ARCH_GLM4:
  3737. {
  3738. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3739. // output
  3740. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3741. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  3742. // if output is NULL, init from the input tok embed
  3743. if (output == NULL) {
  3744. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3745. }
  3746. for (int i = 0; i < n_layer; ++i) {
  3747. auto & layer = layers[i];
  3748. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3749. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, TENSOR_NOT_REQUIRED);
  3750. layer.bqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd + 2*n_embd_gqa}, TENSOR_NOT_REQUIRED);
  3751. if (layer.wqkv == nullptr) {
  3752. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  3753. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  3754. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  3755. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  3756. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED);
  3757. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED);
  3758. }
  3759. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  3760. layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), {n_embd}, 0);
  3761. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3762. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3763. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff * 2}, 0);
  3764. layer.ffn_post_norm = create_tensor(tn(LLM_TENSOR_FFN_POST_NORM, "weight", i), {n_embd}, 0);
  3765. }
  3766. } break;
  3767. case LLM_ARCH_GLM4_MOE:
  3768. {
  3769. const int64_t n_expert = hparams.n_expert;
  3770. const int64_t n_expert_used = hparams.n_expert_used;
  3771. const int64_t n_expert_shared = hparams.n_expert_shared;
  3772. GGML_ASSERT(hparams.n_expert > 0 && "n_expert must be > 0 for GLM4_MOE MoE layers");
  3773. GGML_ASSERT(hparams.n_expert_used > 0 && "n_expert_used must be > 0 for GLM4_MOE MoE layers");
  3774. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, 0);
  3775. // output
  3776. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), { n_embd }, 0);
  3777. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), { n_embd, n_vocab }, TENSOR_NOT_REQUIRED);
  3778. // if output is NULL, init from the input tok embed
  3779. if (output == NULL) {
  3780. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, TENSOR_DUPLICATED);
  3781. }
  3782. // Load ALL tensors including NextN layer to satisfy total tensor count
  3783. // but only PROCESS up to last layer (skipping final NextN layer) in forward pass
  3784. for (int i = 0; i < n_layer; ++i) {
  3785. int flags = 0;
  3786. if (hparams.nextn_predict_layers > 0 && static_cast<uint32_t>(i) >= n_layer - hparams.nextn_predict_layers) {
  3787. // skip all tensors in the NextN layers
  3788. flags |= TENSOR_SKIP;
  3789. }
  3790. auto & layer = layers[i];
  3791. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), { n_embd }, flags);
  3792. // GLM-style attention with bias terms
  3793. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), { n_embd, n_embd_head_k * n_head }, flags);
  3794. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), { n_embd, n_embd_k_gqa }, flags);
  3795. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), { n_embd, n_embd_v_gqa }, flags);
  3796. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), { n_embd_head_k * n_head }, flags);
  3797. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), { n_embd_k_gqa }, flags);
  3798. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), { n_embd_v_gqa }, flags);
  3799. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), { n_embd_head_k * n_head, n_embd }, flags);
  3800. // K/Q norm tensors (optional for GLM-4.5 355B variant)
  3801. layer.attn_q_norm = create_tensor(
  3802. tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), { n_embd_head_k }, TENSOR_NOT_REQUIRED | flags);
  3803. layer.attn_k_norm = create_tensor(
  3804. tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), { n_embd_head_k }, TENSOR_NOT_REQUIRED | flags);
  3805. layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), { n_embd }, flags);
  3806. // Check if this layer uses MoE or dense FFN based on n_layer_dense_lead
  3807. // GLM 4.5 uses hybrid architecture: layer 0 is dense, layers 1+ are MoE
  3808. const bool use_moe = (static_cast<uint32_t>(i) >= hparams.n_layer_dense_lead);
  3809. if (use_moe) {
  3810. // MoE layers
  3811. layer.ffn_gate_inp =
  3812. create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), { n_embd, n_expert }, flags);
  3813. layer.ffn_exp_probs_b = create_tensor(tn(LLM_TENSOR_FFN_EXP_PROBS_B, "bias", i), { n_expert }, flags);
  3814. // MoE branch
  3815. const int64_t n_ff_exp = hparams.n_ff_exp ? hparams.n_ff_exp : n_ff / n_expert_used;
  3816. layer.ffn_gate_exps = create_tensor(
  3817. tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert }, flags);
  3818. layer.ffn_down_exps = create_tensor(
  3819. tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), { n_ff_exp, n_embd, n_expert }, flags);
  3820. layer.ffn_up_exps = create_tensor(
  3821. tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert }, flags);
  3822. // Shared expert
  3823. if (n_expert_shared > 0) {
  3824. const int64_t n_ff_shexp = n_ff_exp * n_expert_shared;
  3825. layer.ffn_gate_shexp = create_tensor(
  3826. tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), { n_embd, n_ff_shexp }, flags);
  3827. layer.ffn_down_shexp = create_tensor(
  3828. tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), { n_ff_shexp, n_embd }, flags);
  3829. layer.ffn_up_shexp = create_tensor(
  3830. tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), { n_embd, n_ff_shexp }, flags);
  3831. }
  3832. } else {
  3833. // Dense layers (first k layers) - GLM uses separate gate/up projections
  3834. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), { n_embd, n_ff }, flags);
  3835. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd }, flags);
  3836. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), { n_embd, n_ff }, flags);
  3837. }
  3838. // NextN/MTP tensors (preserved but unused) - conditionally load for last nextn_predict_layers
  3839. if (hparams.nextn_predict_layers > 0 && static_cast<uint32_t>(i) >= n_layer - hparams.nextn_predict_layers) {
  3840. layer.nextn.eh_proj = create_tensor(tn(LLM_TENSOR_NEXTN_EH_PROJ, "weight", i), { 2 * n_embd, n_embd }, flags);
  3841. layer.nextn.embed_tokens = create_tensor(tn(LLM_TENSOR_NEXTN_EMBED_TOKENS, "weight", i), { n_embd, n_vocab }, flags);
  3842. layer.nextn.enorm = create_tensor(tn(LLM_TENSOR_NEXTN_ENORM, "weight", i), { n_embd }, flags);
  3843. layer.nextn.hnorm = create_tensor(tn(LLM_TENSOR_NEXTN_HNORM, "weight", i), { n_embd }, flags);
  3844. layer.nextn.shared_head_head = create_tensor(tn(LLM_TENSOR_NEXTN_SHARED_HEAD_HEAD, "weight", i), { n_embd, n_vocab }, flags);
  3845. layer.nextn.shared_head_norm = create_tensor(tn(LLM_TENSOR_NEXTN_SHARED_HEAD_NORM, "weight", i), { n_embd }, flags);
  3846. }
  3847. }
  3848. }
  3849. break;
  3850. case LLM_ARCH_NEMOTRON:
  3851. {
  3852. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3853. // output
  3854. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3855. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
  3856. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  3857. for (int i = 0; i < n_layer; ++i) {
  3858. auto & layer = layers[i];
  3859. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3860. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0);
  3861. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  3862. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  3863. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  3864. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  3865. // optional bias tensors
  3866. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  3867. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED);
  3868. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED);
  3869. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  3870. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3871. layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd}, 0);
  3872. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3873. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3874. // optional MLP bias
  3875. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  3876. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, TENSOR_NOT_REQUIRED);
  3877. }
  3878. } break;
  3879. case LLM_ARCH_EXAONE:
  3880. {
  3881. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3882. // output
  3883. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3884. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  3885. // if output is NULL, init from the input tok embed
  3886. if (output == NULL) {
  3887. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3888. }
  3889. for (int i = 0; i < n_layer; ++i) {
  3890. auto & layer = layers[i];
  3891. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3892. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  3893. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  3894. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  3895. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  3896. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3897. layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  3898. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  3899. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3900. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3901. }
  3902. } break;
  3903. case LLM_ARCH_EXAONE4:
  3904. {
  3905. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3906. // output
  3907. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3908. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  3909. // if output is NULL, init from the input tok embed
  3910. if (output == NULL) {
  3911. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3912. }
  3913. for (int i = 0; i < n_layer; ++i) {
  3914. auto & layer = layers[i];
  3915. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  3916. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  3917. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  3918. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  3919. layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  3920. layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), {n_embd}, 0);
  3921. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0);
  3922. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0);
  3923. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  3924. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3925. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3926. layer.ffn_post_norm = create_tensor(tn(LLM_TENSOR_FFN_POST_NORM, "weight", i), {n_embd}, 0);
  3927. }
  3928. } break;
  3929. case LLM_ARCH_RWKV6:
  3930. {
  3931. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3932. // Block 0, LN0
  3933. tok_norm = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "weight"), {n_embd}, 0);
  3934. tok_norm_b = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "bias"), {n_embd}, 0);
  3935. // output
  3936. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3937. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
  3938. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  3939. const int time_mix_extra_dim = hparams.time_mix_extra_dim;
  3940. const int time_decay_extra_dim = hparams.time_decay_extra_dim;
  3941. const int head_size = hparams.wkv_head_size;
  3942. const int attn_hidden_size = n_embd;
  3943. const int ffn_size = hparams.n_ff_arr[0];
  3944. for (int i = 0; i < n_layer; ++i) {
  3945. auto & layer = layers[i];
  3946. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3947. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0);
  3948. layer.attn_norm_2 = create_tensor(tn(LLM_TENSOR_ATTN_NORM_2, "weight", i), {n_embd}, 0);
  3949. layer.attn_norm_2_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM_2, "bias", i), {n_embd}, 0);
  3950. layer.time_mix_w1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_W1, "weight", i), {n_embd, time_mix_extra_dim * 5}, 0);
  3951. layer.time_mix_w2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_W2, "weight", i), {time_mix_extra_dim, n_embd, 5}, 0);
  3952. layer.time_mix_lerp_x = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_X, "weight", i), {n_embd, 1, 1}, 0);
  3953. layer.time_mix_lerp_w = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_W, "weight", i), {n_embd, 1, 1}, TENSOR_NOT_REQUIRED);
  3954. layer.time_mix_lerp_k = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_K, "weight", i), {n_embd, 1, 1}, TENSOR_NOT_REQUIRED);
  3955. layer.time_mix_lerp_v = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_V, "weight", i), {n_embd, 1, 1}, TENSOR_NOT_REQUIRED);
  3956. layer.time_mix_lerp_r = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_R, "weight", i), {n_embd, 1, 1}, TENSOR_NOT_REQUIRED);
  3957. layer.time_mix_lerp_g = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_G, "weight", i), {n_embd, 1, 1}, TENSOR_NOT_REQUIRED);
  3958. layer.time_mix_lerp_fused = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_FUSED, "weight", i), {n_embd, 1, 1, 5}, TENSOR_NOT_REQUIRED);
  3959. GGML_ASSERT(!(layer.time_mix_lerp_fused == NULL && layer.time_mix_lerp_w == NULL));
  3960. layer.time_mix_first = create_tensor(tn(LLM_TENSOR_TIME_MIX_FIRST, "weight", i), {head_size, n_embd / head_size}, 0);
  3961. layer.time_mix_decay = create_tensor(tn(LLM_TENSOR_TIME_MIX_DECAY, "weight", i), {n_embd}, 0);
  3962. layer.time_mix_decay_w1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_DECAY_W1, "weight", i), {n_embd, time_decay_extra_dim}, 0);
  3963. layer.time_mix_decay_w2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_DECAY_W2, "weight", i), {time_decay_extra_dim, attn_hidden_size}, 0);
  3964. layer.time_mix_key = create_tensor(tn(LLM_TENSOR_TIME_MIX_KEY, "weight", i), {attn_hidden_size, n_embd}, 0);
  3965. layer.time_mix_value = create_tensor(tn(LLM_TENSOR_TIME_MIX_VALUE, "weight", i), {attn_hidden_size, n_embd}, 0);
  3966. layer.time_mix_receptance = create_tensor(tn(LLM_TENSOR_TIME_MIX_RECEPTANCE, "weight", i), {attn_hidden_size, n_embd}, 0);
  3967. layer.time_mix_gate = create_tensor(tn(LLM_TENSOR_TIME_MIX_GATE, "weight", i), {attn_hidden_size, n_embd}, 0);
  3968. layer.time_mix_ln = create_tensor(tn(LLM_TENSOR_TIME_MIX_LN, "weight", i), {n_embd}, 0);
  3969. layer.time_mix_ln_b = create_tensor(tn(LLM_TENSOR_TIME_MIX_LN, "bias", i), {n_embd}, 0);
  3970. layer.time_mix_output = create_tensor(tn(LLM_TENSOR_TIME_MIX_OUTPUT, "weight", i), {n_embd, attn_hidden_size}, 0);
  3971. layer.channel_mix_lerp_k = create_tensor(tn(LLM_TENSOR_CHANNEL_MIX_LERP_K, "weight", i), {n_embd, 1, 1}, 0);
  3972. layer.channel_mix_lerp_r = create_tensor(tn(LLM_TENSOR_CHANNEL_MIX_LERP_R, "weight", i), {n_embd, 1, 1}, 0);
  3973. layer.channel_mix_key = create_tensor(tn(LLM_TENSOR_CHANNEL_MIX_KEY, "weight", i), {n_embd, ffn_size}, 0);
  3974. layer.channel_mix_value = create_tensor(tn(LLM_TENSOR_CHANNEL_MIX_VALUE, "weight", i), {ffn_size, n_embd}, 0);
  3975. layer.channel_mix_receptance = create_tensor(tn(LLM_TENSOR_CHANNEL_MIX_RECEPTANCE, "weight", i), {n_embd, n_embd}, 0);
  3976. }
  3977. } break;
  3978. case LLM_ARCH_RWKV6QWEN2:
  3979. {
  3980. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3981. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3982. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, TENSOR_NOT_REQUIRED);
  3983. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  3984. const int time_mix_extra_dim = hparams.time_mix_extra_dim;
  3985. const int time_decay_extra_dim = hparams.time_decay_extra_dim;
  3986. const int head_size = hparams.wkv_head_size;
  3987. const int attn_hidden_size = n_embd;
  3988. const int n_head_kv = hparams.n_head_kv();
  3989. int attn_key_value_size;
  3990. if (n_head_kv == 0 || attn_hidden_size / head_size == n_head_kv) {
  3991. attn_key_value_size = attn_hidden_size;
  3992. } else {
  3993. attn_key_value_size = n_head_kv * head_size;
  3994. }
  3995. for (int i = 0; i < n_layer; ++i) {
  3996. auto & layer = layers[i];
  3997. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3998. layer.time_mix_w1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_W1, "weight", i), {n_embd, time_mix_extra_dim * 5}, 0);
  3999. layer.time_mix_w2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_W2, "weight", i), {time_mix_extra_dim, n_embd, 5}, 0);
  4000. layer.time_mix_lerp_x = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_X, "weight", i), {n_embd, 1, 1}, 0);
  4001. layer.time_mix_lerp_fused = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_FUSED, "weight", i), {n_embd, 1, 1, 5}, 0);
  4002. layer.time_mix_first = create_tensor(tn(LLM_TENSOR_TIME_MIX_FIRST, "weight", i), {head_size, n_embd / head_size}, TENSOR_NOT_REQUIRED);
  4003. layer.time_mix_decay = create_tensor(tn(LLM_TENSOR_TIME_MIX_DECAY, "weight", i), {n_embd}, 0);
  4004. layer.time_mix_decay_w1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_DECAY_W1, "weight", i), {n_embd, time_decay_extra_dim}, 0);
  4005. layer.time_mix_decay_w2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_DECAY_W2, "weight", i), {time_decay_extra_dim, attn_hidden_size}, 0);
  4006. layer.time_mix_key = create_tensor(tn(LLM_TENSOR_TIME_MIX_KEY, "weight", i), {n_embd, attn_key_value_size}, 0);
  4007. layer.time_mix_value = create_tensor(tn(LLM_TENSOR_TIME_MIX_VALUE, "weight", i), {n_embd, attn_key_value_size}, 0);
  4008. layer.time_mix_receptance = create_tensor(tn(LLM_TENSOR_TIME_MIX_RECEPTANCE, "weight", i), {attn_hidden_size, n_embd}, 0);
  4009. layer.time_mix_gate = create_tensor(tn(LLM_TENSOR_TIME_MIX_GATE, "weight", i), {attn_hidden_size, n_embd}, 0);
  4010. // optional bias tensors
  4011. layer.time_mix_key_b = create_tensor(tn(LLM_TENSOR_TIME_MIX_KEY, "bias", i), {attn_key_value_size}, TENSOR_NOT_REQUIRED);
  4012. layer.time_mix_value_b = create_tensor(tn(LLM_TENSOR_TIME_MIX_VALUE, "bias", i), {attn_key_value_size}, TENSOR_NOT_REQUIRED);
  4013. layer.time_mix_receptance_b = create_tensor(tn(LLM_TENSOR_TIME_MIX_RECEPTANCE, "bias", i), {attn_hidden_size}, TENSOR_NOT_REQUIRED);
  4014. layer.time_mix_output = create_tensor(tn(LLM_TENSOR_TIME_MIX_OUTPUT, "weight", i), {n_embd, attn_hidden_size}, 0);
  4015. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  4016. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  4017. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  4018. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  4019. }
  4020. } break;
  4021. case LLM_ARCH_RWKV7:
  4022. {
  4023. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  4024. // Block 0, LN0
  4025. tok_norm = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "weight"), {n_embd}, 0);
  4026. tok_norm_b = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "bias"), {n_embd}, 0);
  4027. // output
  4028. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  4029. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
  4030. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  4031. const int n_lora_decay = hparams.n_lora_decay;
  4032. const int n_lora_iclr = hparams.n_lora_iclr;
  4033. const int n_lora_value_res_mix = hparams.n_lora_value_res_mix;
  4034. const int n_lora_gate = hparams.n_lora_gate;
  4035. const int attn_hidden_size = n_embd;
  4036. const int ffn_size = hparams.n_ff_arr[0];
  4037. for (int i = 0; i < n_layer; ++i) {
  4038. auto & layer = layers[i];
  4039. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  4040. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0);
  4041. layer.attn_norm_2 = create_tensor(tn(LLM_TENSOR_ATTN_NORM_2, "weight", i), {n_embd}, 0);
  4042. layer.attn_norm_2_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM_2, "bias", i), {n_embd}, 0);
  4043. layer.time_mix_w0 = create_tensor(tn(LLM_TENSOR_TIME_MIX_W0, "weight", i), {n_embd}, 0);
  4044. layer.time_mix_w1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_W1, "weight", i), {n_embd, n_lora_decay}, 0);
  4045. layer.time_mix_w2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_W2, "weight", i), {n_lora_decay, n_embd}, 0);
  4046. layer.time_mix_a0 = create_tensor(tn(LLM_TENSOR_TIME_MIX_A0, "weight", i), {n_embd}, 0);
  4047. layer.time_mix_a1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_A1, "weight", i), {n_embd, n_lora_iclr}, 0);
  4048. layer.time_mix_a2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_A2, "weight", i), {n_lora_iclr, n_embd}, 0);
  4049. if (i == 0) {
  4050. // actually not used
  4051. layer.time_mix_v0 = create_tensor(tn(LLM_TENSOR_TIME_MIX_V0, "weight", i), {n_embd}, 0);
  4052. layer.time_mix_v1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_V1, "weight", i), {n_embd, n_lora_iclr}, 0);
  4053. layer.time_mix_v2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_V2, "weight", i), {n_lora_iclr, n_embd}, 0);
  4054. } else {
  4055. layer.time_mix_v0 = create_tensor(tn(LLM_TENSOR_TIME_MIX_V0, "weight", i), {n_embd}, 0);
  4056. layer.time_mix_v1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_V1, "weight", i), {n_embd, n_lora_value_res_mix}, 0);
  4057. layer.time_mix_v2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_V2, "weight", i), {n_lora_value_res_mix, n_embd}, 0);
  4058. }
  4059. layer.time_mix_g1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_G1, "weight", i), {n_embd, n_lora_gate}, 0);
  4060. layer.time_mix_g2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_G2, "weight", i), {n_lora_gate, n_embd}, 0);
  4061. layer.time_mix_lerp_fused = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_FUSED, "weight", i), {n_embd, 1, 1, 6}, 0);
  4062. layer.time_mix_k_k = create_tensor(tn(LLM_TENSOR_TIME_MIX_K_K, "weight", i), {attn_hidden_size}, 0);
  4063. layer.time_mix_k_a = create_tensor(tn(LLM_TENSOR_TIME_MIX_K_A, "weight", i), {attn_hidden_size}, 0);
  4064. layer.time_mix_r_k = create_tensor(tn(LLM_TENSOR_TIME_MIX_R_K, "weight", i), {attn_hidden_size}, 0);
  4065. layer.time_mix_key = create_tensor(tn(LLM_TENSOR_TIME_MIX_KEY, "weight", i), {attn_hidden_size, n_embd}, 0);
  4066. layer.time_mix_value = create_tensor(tn(LLM_TENSOR_TIME_MIX_VALUE, "weight", i), {attn_hidden_size, n_embd}, 0);
  4067. layer.time_mix_receptance = create_tensor(tn(LLM_TENSOR_TIME_MIX_RECEPTANCE, "weight", i), {attn_hidden_size, n_embd}, 0);
  4068. layer.time_mix_ln = create_tensor(tn(LLM_TENSOR_TIME_MIX_LN, "weight", i), {n_embd}, 0);
  4069. layer.time_mix_ln_b = create_tensor(tn(LLM_TENSOR_TIME_MIX_LN, "bias", i), {n_embd}, 0);
  4070. layer.time_mix_output = create_tensor(tn(LLM_TENSOR_TIME_MIX_OUTPUT, "weight", i), {n_embd, attn_hidden_size}, 0);
  4071. layer.channel_mix_lerp_k = create_tensor(tn(LLM_TENSOR_CHANNEL_MIX_LERP_K, "weight", i), {n_embd, 1, 1}, 0);
  4072. layer.channel_mix_key = create_tensor(tn(LLM_TENSOR_CHANNEL_MIX_KEY, "weight", i), {n_embd, ffn_size}, 0);
  4073. layer.channel_mix_value = create_tensor(tn(LLM_TENSOR_CHANNEL_MIX_VALUE, "weight", i), {ffn_size, n_embd}, 0);
  4074. }
  4075. } break;
  4076. case LLM_ARCH_ARWKV7:
  4077. {
  4078. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  4079. // output
  4080. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  4081. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  4082. const int n_lora_decay = hparams.n_lora_decay;
  4083. const int n_lora_iclr = hparams.n_lora_iclr;
  4084. const int n_lora_value_res_mix = hparams.n_lora_value_res_mix;
  4085. const int n_lora_gate = hparams.n_lora_gate;
  4086. const int attn_hidden_size = n_embd;
  4087. for (int i = 0; i < n_layer; ++i) {
  4088. auto & layer = layers[i];
  4089. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  4090. layer.time_mix_w0 = create_tensor(tn(LLM_TENSOR_TIME_MIX_W0, "weight", i), {n_embd}, 0);
  4091. layer.time_mix_w1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_W1, "weight", i), {n_embd, n_lora_decay}, 0);
  4092. layer.time_mix_w2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_W2, "weight", i), {n_lora_decay, n_embd}, 0);
  4093. layer.time_mix_a0 = create_tensor(tn(LLM_TENSOR_TIME_MIX_A0, "weight", i), {n_embd}, 0);
  4094. layer.time_mix_a1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_A1, "weight", i), {n_embd, n_lora_iclr}, 0);
  4095. layer.time_mix_a2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_A2, "weight", i), {n_lora_iclr, n_embd}, 0);
  4096. if (i == 0) {
  4097. // actually not used
  4098. layer.time_mix_v0 = create_tensor(tn(LLM_TENSOR_TIME_MIX_V0, "weight", i), {n_embd}, 0);
  4099. layer.time_mix_v1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_V1, "weight", i), {n_embd, n_lora_iclr}, 0);
  4100. layer.time_mix_v2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_V2, "weight", i), {n_lora_iclr, n_embd}, 0);
  4101. } else {
  4102. layer.time_mix_v0 = create_tensor(tn(LLM_TENSOR_TIME_MIX_V0, "weight", i), {n_embd}, 0);
  4103. layer.time_mix_v1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_V1, "weight", i), {n_embd, n_lora_value_res_mix}, 0);
  4104. layer.time_mix_v2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_V2, "weight", i), {n_lora_value_res_mix, n_embd}, 0);
  4105. }
  4106. layer.time_mix_g1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_G1, "weight", i), {n_embd, n_lora_gate}, TENSOR_NOT_REQUIRED);
  4107. layer.time_mix_g2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_G2, "weight", i), {n_lora_gate, n_embd}, TENSOR_NOT_REQUIRED);
  4108. try {
  4109. layer.time_mix_lerp_fused = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_FUSED, "weight", i), {n_embd, 1, 1, 6}, 0);
  4110. } catch(std::runtime_error & e) {
  4111. // ARWKV models may not have gate tensors
  4112. layer.time_mix_lerp_fused = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_FUSED, "weight", i), {n_embd, 1, 1, 5}, 0);
  4113. }
  4114. layer.time_mix_k_k = create_tensor(tn(LLM_TENSOR_TIME_MIX_K_K, "weight", i), {attn_hidden_size}, 0);
  4115. layer.time_mix_k_a = create_tensor(tn(LLM_TENSOR_TIME_MIX_K_A, "weight", i), {attn_hidden_size}, 0);
  4116. layer.time_mix_r_k = create_tensor(tn(LLM_TENSOR_TIME_MIX_R_K, "weight", i), {attn_hidden_size}, 0);
  4117. layer.time_mix_key = create_tensor(tn(LLM_TENSOR_TIME_MIX_KEY, "weight", i), {attn_hidden_size, n_embd}, 0);
  4118. layer.time_mix_value = create_tensor(tn(LLM_TENSOR_TIME_MIX_VALUE, "weight", i), {attn_hidden_size, n_embd}, 0);
  4119. layer.time_mix_receptance = create_tensor(tn(LLM_TENSOR_TIME_MIX_RECEPTANCE, "weight", i), {attn_hidden_size, n_embd}, 0);
  4120. layer.time_mix_ln = create_tensor(tn(LLM_TENSOR_TIME_MIX_LN, "weight", i), {n_embd}, TENSOR_NOT_REQUIRED);
  4121. layer.time_mix_ln_b = create_tensor(tn(LLM_TENSOR_TIME_MIX_LN, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  4122. layer.time_mix_output = create_tensor(tn(LLM_TENSOR_TIME_MIX_OUTPUT, "weight", i), {n_embd, attn_hidden_size}, 0);
  4123. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  4124. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  4125. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  4126. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  4127. }
  4128. } break;
  4129. case LLM_ARCH_CHAMELEON:
  4130. {
  4131. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  4132. // output
  4133. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  4134. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  4135. // if output is NULL, init from the input tok embed
  4136. if (output == NULL) {
  4137. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  4138. }
  4139. for (int i = 0; i < n_layer; ++i) {
  4140. auto & layer = layers[i];
  4141. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  4142. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k, n_head}, 0);
  4143. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k, n_head_kv}, 0);
  4144. layer.attn_q_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "bias", i), {n_embd_head_k, n_head}, TENSOR_NOT_REQUIRED);
  4145. layer.attn_k_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "bias", i), {n_embd_head_k, n_head_kv}, TENSOR_NOT_REQUIRED);
  4146. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  4147. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  4148. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  4149. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  4150. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  4151. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  4152. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  4153. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  4154. }
  4155. } break;
  4156. case LLM_ARCH_WAVTOKENIZER_DEC:
  4157. {
  4158. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {hparams.n_embd_features, n_vocab}, 0);
  4159. conv1d = create_tensor(tn(LLM_TENSOR_CONV1D, "weight"), {7, hparams.n_embd_features, hparams.posnet.n_embd}, 0);
  4160. conv1d_b = create_tensor(tn(LLM_TENSOR_CONV1D, "bias"), {1, hparams.posnet.n_embd}, 0);
  4161. // posnet
  4162. {
  4163. const int64_t n_embd = hparams.posnet.n_embd;
  4164. for (uint32_t i = 0; i < hparams.posnet.n_layer; ++i) {
  4165. auto & layer = layers[i].posnet;
  4166. // posnet:
  4167. //
  4168. // - resnet
  4169. // - resnet
  4170. // - attn
  4171. // - resnet
  4172. // - resnet
  4173. // - norm
  4174. //
  4175. switch (i) {
  4176. case 0:
  4177. case 1:
  4178. case 3:
  4179. case 4:
  4180. {
  4181. layer.norm1 = create_tensor(tn(LLM_TENSOR_POS_NET_NORM1, "weight", i), {1, n_embd}, 0);
  4182. layer.norm1_b = create_tensor(tn(LLM_TENSOR_POS_NET_NORM1, "bias", i), {1, n_embd}, 0);
  4183. layer.conv1 = create_tensor(tn(LLM_TENSOR_POS_NET_CONV1, "weight", i), {3, n_embd, n_embd}, 0);
  4184. layer.conv1_b = create_tensor(tn(LLM_TENSOR_POS_NET_CONV1, "bias", i), {1, n_embd}, 0);
  4185. layer.norm2 = create_tensor(tn(LLM_TENSOR_POS_NET_NORM2, "weight", i), {1, n_embd}, 0);
  4186. layer.norm2_b = create_tensor(tn(LLM_TENSOR_POS_NET_NORM2, "bias", i), {1, n_embd}, 0);
  4187. layer.conv2 = create_tensor(tn(LLM_TENSOR_POS_NET_CONV2, "weight", i), {3, n_embd, n_embd}, 0);
  4188. layer.conv2_b = create_tensor(tn(LLM_TENSOR_POS_NET_CONV2, "bias", i), {1, n_embd}, 0);
  4189. } break;
  4190. case 2:
  4191. {
  4192. layer.attn_norm = create_tensor(tn(LLM_TENSOR_POS_NET_ATTN_NORM, "weight", i), {1, n_embd}, 0);
  4193. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_POS_NET_ATTN_NORM, "bias", i), {1, n_embd}, 0);
  4194. layer.attn_q = create_tensor(tn(LLM_TENSOR_POS_NET_ATTN_Q, "weight", i), {1, n_embd, n_embd}, 0);
  4195. layer.attn_q_b = create_tensor(tn(LLM_TENSOR_POS_NET_ATTN_Q, "bias", i), {1, n_embd}, 0);
  4196. layer.attn_k = create_tensor(tn(LLM_TENSOR_POS_NET_ATTN_K, "weight", i), {1, n_embd, n_embd}, 0);
  4197. layer.attn_k_b = create_tensor(tn(LLM_TENSOR_POS_NET_ATTN_K, "bias", i), {1, n_embd}, 0);
  4198. layer.attn_v = create_tensor(tn(LLM_TENSOR_POS_NET_ATTN_V, "weight", i), {1, n_embd, n_embd}, 0);
  4199. layer.attn_v_b = create_tensor(tn(LLM_TENSOR_POS_NET_ATTN_V, "bias", i), {1, n_embd}, 0);
  4200. layer.attn_o = create_tensor(tn(LLM_TENSOR_POS_NET_ATTN_OUT, "weight", i), {1, n_embd, n_embd}, 0);
  4201. layer.attn_o_b = create_tensor(tn(LLM_TENSOR_POS_NET_ATTN_OUT, "bias", i), {1, n_embd}, 0);
  4202. } break;
  4203. case 5:
  4204. {
  4205. layer.norm = create_tensor(tn(LLM_TENSOR_POS_NET_ATTN_NORM, "weight", i), {1, n_embd}, 0);
  4206. layer.norm_b = create_tensor(tn(LLM_TENSOR_POS_NET_ATTN_NORM, "bias", i), {1, n_embd}, 0);
  4207. } break;
  4208. default: GGML_ABORT("unknown posnet layer");
  4209. };
  4210. }
  4211. }
  4212. GGML_ASSERT(hparams.posnet.n_embd == hparams.convnext.n_embd);
  4213. tok_norm = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "weight"), {hparams.posnet.n_embd}, 0);
  4214. tok_norm_b = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "bias"), {hparams.posnet.n_embd}, 0);
  4215. // convnext
  4216. {
  4217. const int64_t n_embd = hparams.convnext.n_embd;
  4218. for (uint32_t i = 0; i < hparams.convnext.n_layer; ++i) {
  4219. auto & layer = layers[i].convnext;
  4220. layer.dw = create_tensor(tn(LLM_TENSOR_CONVNEXT_DW, "weight", i), {7, 1, n_embd}, 0);
  4221. layer.dw_b = create_tensor(tn(LLM_TENSOR_CONVNEXT_DW, "bias", i), {1, n_embd}, 0);
  4222. layer.norm = create_tensor(tn(LLM_TENSOR_CONVNEXT_NORM, "weight", i), {n_embd}, 0);
  4223. layer.norm_b = create_tensor(tn(LLM_TENSOR_CONVNEXT_NORM, "bias", i), {n_embd}, 0);
  4224. layer.pw1 = create_tensor(tn(LLM_TENSOR_CONVNEXT_PW1, "weight", i), {n_embd, n_ff}, 0);
  4225. layer.pw1_b = create_tensor(tn(LLM_TENSOR_CONVNEXT_PW1, "bias", i), {n_ff}, 0);
  4226. layer.pw2 = create_tensor(tn(LLM_TENSOR_CONVNEXT_PW2, "weight", i), {n_ff, n_embd}, 0);
  4227. layer.pw2_b = create_tensor(tn(LLM_TENSOR_CONVNEXT_PW2, "bias", i), {n_embd}, 0);
  4228. layer.gamma = create_tensor(tn(LLM_TENSOR_CONVNEXT_GAMMA, "weight", i), {n_embd}, 0);
  4229. }
  4230. // output
  4231. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  4232. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
  4233. }
  4234. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {hparams.convnext.n_embd, n_embd}, 0);
  4235. output_b = create_tensor(tn(LLM_TENSOR_OUTPUT, "bias"), {n_embd}, 0);
  4236. } break;
  4237. case LLM_ARCH_BAILINGMOE:
  4238. {
  4239. const int64_t n_ff_exp = hparams.n_ff_exp;
  4240. const int64_t n_expert_shared = hparams.n_expert_shared;
  4241. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  4242. // output
  4243. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  4244. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  4245. for (int i = 0; i < n_layer; ++i) {
  4246. auto & layer = layers[i];
  4247. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  4248. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_head * n_rot}, 0);
  4249. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_head_kv * n_rot}, 0);
  4250. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_head_kv * n_rot}, 0);
  4251. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_head * n_rot, n_embd}, 0);
  4252. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  4253. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  4254. if (n_expert == 0) {
  4255. throw std::runtime_error("n_expert must be > 0");
  4256. }
  4257. if (n_expert_used == 0) {
  4258. throw std::runtime_error("n_expert_used must be > 0");
  4259. }
  4260. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);
  4261. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp, n_embd, n_expert}, 0);
  4262. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);
  4263. layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), {n_embd, n_ff_exp * n_expert_shared}, 0);
  4264. layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), { n_ff_exp * n_expert_shared, n_embd}, 0);
  4265. layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), {n_embd, n_ff_exp * n_expert_shared}, 0);
  4266. }
  4267. } break;
  4268. case LLM_ARCH_DOTS1:
  4269. {
  4270. const int64_t n_ff_exp = hparams.n_ff_exp;
  4271. const int64_t n_expert_shared = hparams.n_expert_shared;
  4272. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  4273. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  4274. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  4275. for (int i = 0; i < n_layer; ++i) {
  4276. auto & layer = layers[i];
  4277. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  4278. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  4279. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  4280. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  4281. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  4282. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0);
  4283. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0);
  4284. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  4285. if (i < (int) hparams.n_layer_dense_lead) {
  4286. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  4287. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  4288. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  4289. } else {
  4290. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  4291. layer.ffn_exp_probs_b = create_tensor(tn(LLM_TENSOR_FFN_EXP_PROBS_B, "bias", i), {n_expert}, TENSOR_NOT_REQUIRED);
  4292. if (n_expert == 0) {
  4293. throw std::runtime_error("n_expert must be > 0");
  4294. }
  4295. if (n_expert_used == 0) {
  4296. throw std::runtime_error("n_expert_used must be > 0");
  4297. }
  4298. // MoE branch
  4299. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);
  4300. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp, n_embd, n_expert}, 0);
  4301. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);
  4302. // Shared expert branch
  4303. layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), {n_embd, n_ff_exp * n_expert_shared}, 0);
  4304. layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), { n_ff_exp * n_expert_shared, n_embd}, 0);
  4305. layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), {n_embd, n_ff_exp * n_expert_shared}, 0);
  4306. }
  4307. }
  4308. } break;
  4309. case LLM_ARCH_ARCEE:
  4310. {
  4311. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  4312. // output
  4313. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  4314. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  4315. // if output is NULL, init from the input tok embed
  4316. if (output == NULL) {
  4317. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  4318. }
  4319. for (int i = 0; i < n_layer; ++i) {
  4320. auto & layer = layers[i];
  4321. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  4322. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  4323. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  4324. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  4325. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  4326. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  4327. layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  4328. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  4329. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  4330. }
  4331. } break;
  4332. case LLM_ARCH_ERNIE4_5:
  4333. case LLM_ARCH_ERNIE4_5_MOE:
  4334. {
  4335. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  4336. // output
  4337. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  4338. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  4339. // if output is NULL, init from the input tok embed
  4340. if (output == NULL) {
  4341. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  4342. }
  4343. for (int i = 0; i < n_layer; ++i) {
  4344. auto & layer = layers[i];
  4345. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  4346. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  4347. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  4348. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  4349. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  4350. // optional bias tensors
  4351. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  4352. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED);
  4353. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED);
  4354. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  4355. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  4356. if (arch == LLM_ARCH_ERNIE4_5_MOE && static_cast<uint32_t>(i) >= hparams.n_layer_dense_lead) { // MoE layers
  4357. int n_ff_exp = hparams.n_ff_exp;
  4358. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  4359. layer.ffn_exp_probs_b = create_tensor(tn(LLM_TENSOR_FFN_EXP_PROBS_B, "bias", i), {n_expert}, TENSOR_NOT_REQUIRED);
  4360. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff_exp, n_expert}, TENSOR_NOT_REQUIRED);
  4361. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), { n_ff_exp, n_embd, n_expert}, 0);
  4362. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff_exp, n_expert}, 0);
  4363. // Shared expert (if present)
  4364. if (hparams.n_ff_shexp > 0) {
  4365. layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), { n_embd, hparams.n_ff_shexp}, 0);
  4366. layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), {hparams.n_ff_shexp, n_embd }, 0);
  4367. layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), { n_embd, hparams.n_ff_shexp}, 0);
  4368. }
  4369. } else { // Dense layers
  4370. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  4371. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  4372. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  4373. }
  4374. }
  4375. } break;
  4376. case LLM_ARCH_FALCON_H1:
  4377. {
  4378. // Common
  4379. const int64_t hidden_size = hparams.n_embd; // hidden_size
  4380. // mamba2 Mixer SSM params
  4381. const int64_t ssm_conv_kernel_size = hparams.ssm_d_conv; // ssm_conv_kernel_size
  4382. const int64_t ssm_n_groups = hparams.ssm_n_group; // ssm_n_groups
  4383. const int64_t ssm_state_size = hparams.ssm_d_state; // ssm_state_size
  4384. const int64_t ssm_intermediate_size = hparams.ssm_d_inner; // TODO expand
  4385. const int64_t ssm_num_heads = hparams.ssm_dt_rank; // ssm_num_heads
  4386. const int64_t ssm_conv_dim = ssm_intermediate_size + 2 * ssm_n_groups * ssm_state_size;
  4387. const int64_t ssm_projection_size = ssm_intermediate_size + ssm_conv_dim + ssm_num_heads;
  4388. // attn params
  4389. const int64_t attn_num_attention_head = hparams.n_head(0); // rename to: attn_num_attention_head
  4390. const int64_t attn_num_key_value_head = hparams.n_head_kv(0);
  4391. // ffn params
  4392. const int64_t ffn_intermediate_size = hparams.n_ff(0);
  4393. // embeddings
  4394. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {hidden_size, n_vocab}, 0);
  4395. // output
  4396. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {hidden_size, n_vocab}, TENSOR_NOT_REQUIRED);
  4397. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {hidden_size}, 0);
  4398. // if output is NULL, init from the input tok embed
  4399. if (output == NULL) {
  4400. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {hidden_size, n_vocab}, TENSOR_DUPLICATED);
  4401. }
  4402. for (int i = 0; i < n_layer; ++i) {
  4403. auto & layer = layers[i];
  4404. /*SSM LAYERS*/
  4405. // ssm in
  4406. layer.ssm_in = create_tensor(tn(LLM_TENSOR_SSM_IN, "weight", i), {hidden_size, ssm_projection_size}, 0);
  4407. // ssm 1d conv
  4408. layer.ssm_conv1d = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "weight", i), {ssm_conv_kernel_size, ssm_conv_dim}, 0);
  4409. layer.ssm_conv1d_b = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "bias", i), {ssm_conv_dim}, TENSOR_NOT_REQUIRED);
  4410. // ssm_dt
  4411. layer.ssm_dt_b = create_tensor(tn(LLM_TENSOR_SSM_DT, "bias", i), {ssm_num_heads}, 0);
  4412. // no "weight" suffix for these
  4413. layer.ssm_a = create_tensor(tn(LLM_TENSOR_SSM_A, i), {1, ssm_num_heads}, 0);
  4414. layer.ssm_d = create_tensor(tn(LLM_TENSOR_SSM_D, i), {1, ssm_num_heads}, 0);
  4415. // ssm_norm
  4416. layer.ssm_norm = create_tensor(tn(LLM_TENSOR_SSM_NORM, "weight", i), {ssm_intermediate_size / ssm_n_groups, ssm_n_groups}, TENSOR_NOT_REQUIRED);
  4417. // out_proj
  4418. layer.ssm_out = create_tensor(tn(LLM_TENSOR_SSM_OUT, "weight", i), {ssm_intermediate_size, hidden_size}, 0);
  4419. /*ATTENTION LAYERS*/
  4420. // attention layers (with optional bias)
  4421. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {hidden_size, n_embd_head_k * attn_num_attention_head}, 0);
  4422. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {hidden_size, attn_num_key_value_head * n_embd_head_k}, 0);
  4423. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {hidden_size, attn_num_key_value_head * n_embd_head_v}, 0);
  4424. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * attn_num_attention_head, hidden_size}, 0);
  4425. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {hidden_size}, TENSOR_NOT_REQUIRED);
  4426. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {attn_num_key_value_head * n_embd_head_k}, TENSOR_NOT_REQUIRED);
  4427. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {attn_num_key_value_head * n_embd_head_v}, TENSOR_NOT_REQUIRED);
  4428. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {hidden_size}, TENSOR_NOT_REQUIRED);
  4429. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {hidden_size}, 0);
  4430. // feed forward (w/ optional biases)
  4431. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, i), {hidden_size}, 0);
  4432. layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  4433. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {hidden_size, ffn_intermediate_size}, 0);
  4434. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { ffn_intermediate_size, hidden_size}, 0);
  4435. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {hidden_size, ffn_intermediate_size}, 0);
  4436. layer.ffn_gate_b = create_tensor(tn(LLM_TENSOR_FFN_GATE, "bias", i), {ffn_intermediate_size}, TENSOR_NOT_REQUIRED);
  4437. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {hidden_size}, TENSOR_NOT_REQUIRED);
  4438. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {ffn_intermediate_size}, TENSOR_NOT_REQUIRED);
  4439. }
  4440. } break;
  4441. case LLM_ARCH_HUNYUAN_MOE:
  4442. {
  4443. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  4444. // output
  4445. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  4446. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  4447. // if output is NULL, init from the input tok embed
  4448. if (output == NULL) {
  4449. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  4450. }
  4451. for (int i = 0; i < n_layer; ++i) {
  4452. auto & layer = layers[i];
  4453. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  4454. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  4455. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  4456. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  4457. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  4458. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0);
  4459. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0);
  4460. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  4461. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  4462. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0);
  4463. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), { n_ff, n_embd, n_expert}, 0);
  4464. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0);
  4465. layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), {n_embd, hparams.n_ff_shexp}, 0);
  4466. layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), {n_embd, hparams.n_ff_shexp}, 0);
  4467. layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), {hparams.n_ff_shexp, n_embd}, 0);
  4468. }
  4469. } break;
  4470. case LLM_ARCH_HUNYUAN_DENSE:
  4471. {
  4472. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  4473. // output
  4474. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  4475. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  4476. // if output is NULL, init from the input tok embed
  4477. if (output == NULL) {
  4478. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  4479. }
  4480. for (int i = 0; i < n_layer; ++i) {
  4481. auto & layer = layers[i];
  4482. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  4483. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  4484. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  4485. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  4486. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  4487. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0);
  4488. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0);
  4489. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  4490. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  4491. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  4492. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  4493. }
  4494. } break;
  4495. case LLM_ARCH_SMOLLM3:
  4496. {
  4497. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  4498. // output
  4499. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  4500. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  4501. // if output is NULL, init from the input tok embed
  4502. if (output == NULL) {
  4503. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  4504. }
  4505. for (int i = 0; i < n_layer; ++i) {
  4506. auto & layer = layers[i];
  4507. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  4508. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  4509. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  4510. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  4511. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  4512. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  4513. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  4514. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  4515. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  4516. }
  4517. } break;
  4518. case LLM_ARCH_OPENAI_MOE:
  4519. {
  4520. const int64_t n_ff_exp = hparams.n_ff_exp;
  4521. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  4522. // output
  4523. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  4524. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  4525. for (int i = 0; i < n_layer; ++i) {
  4526. auto & layer = layers[i];
  4527. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  4528. layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), {n_embd}, 0);
  4529. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_head * n_rot}, 0);
  4530. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_head_kv * n_rot}, 0);
  4531. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_head_kv * n_rot}, 0);
  4532. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_head * n_rot, n_embd}, 0);
  4533. layer.attn_sinks = create_tensor(tn(LLM_TENSOR_ATTN_SINKS, "weight", i), {n_head}, 0);
  4534. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), { n_embd, n_expert}, 0);
  4535. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);
  4536. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp, n_embd, n_expert}, 0);
  4537. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);
  4538. // bias
  4539. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_head * n_rot}, 0);
  4540. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_head_kv * n_rot}, 0);
  4541. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_head_kv * n_rot}, 0);
  4542. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0);
  4543. layer.ffn_gate_inp_b = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "bias", i), {n_expert}, 0);
  4544. layer.ffn_gate_exps_b = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "bias", i), {n_ff_exp, n_expert}, 0);
  4545. layer.ffn_down_exps_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "bias", i), { n_embd, n_expert}, 0);
  4546. layer.ffn_up_exps_b = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "bias", i), {n_ff_exp, n_expert}, 0);
  4547. }
  4548. } break;
  4549. case LLM_ARCH_LFM2:
  4550. {
  4551. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  4552. tok_norm = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "weight"), {n_embd}, 0);
  4553. for (int i = 0; i < n_layer; ++i) {
  4554. auto & layer = layers[i];
  4555. // ffn is same for transformer and conv layers
  4556. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  4557. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  4558. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  4559. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  4560. // for operator_norm
  4561. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  4562. if (!hparams.is_recurrent(i)) {
  4563. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0);
  4564. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0);
  4565. GGML_ASSERT(n_embd_v_gqa == n_embd_k_gqa);
  4566. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  4567. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, hparams.n_embd_k_gqa(i)}, 0);
  4568. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, hparams.n_embd_v_gqa(i)}, 0);
  4569. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  4570. } else {
  4571. layer.shortconv.conv = create_tensor(tn(LLM_TENSOR_SHORTCONV_CONV, "weight", i), {hparams.n_shortconv_l_cache, n_embd}, 0);
  4572. layer.shortconv.in_proj = create_tensor(tn(LLM_TENSOR_SHORTCONV_INPROJ, "weight", i), {n_embd, 3 * n_embd}, 0);
  4573. layer.shortconv.out_proj = create_tensor(tn(LLM_TENSOR_SHORTCONV_OUTPROJ, "weight", i), {n_embd, n_embd}, 0);
  4574. }
  4575. }
  4576. } break;
  4577. case LLM_ARCH_SMALLTHINKER:
  4578. {
  4579. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, 0);
  4580. // output
  4581. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), { n_embd }, 0);
  4582. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  4583. // if output is NULL, init from the input tok embed
  4584. if (output == NULL) {
  4585. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  4586. }
  4587. for (int i = 0; i < n_layer; ++i) {
  4588. auto & layer = layers[i];
  4589. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), { n_embd }, 0);
  4590. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), { n_embd, n_embd_head_k * n_head }, 0);
  4591. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), { n_embd, n_embd_gqa }, 0);
  4592. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), { n_embd, n_embd_gqa }, 0);
  4593. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), { n_embd_head_k * n_head, n_embd }, 0);
  4594. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), { n_embd }, 0);
  4595. GGML_ASSERT(n_expert > 0 && "n_expert must be > 0 for SMALLTHINKER");
  4596. GGML_ASSERT(n_expert_used > 0 && "n_expert_used must be > 0 for SMALLTHINKER");
  4597. // MoE branch
  4598. const int64_t n_ff_exp = hparams.n_ff_exp;
  4599. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), { n_embd, n_expert }, 0);
  4600. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert }, 0);
  4601. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), { n_ff_exp, n_embd, n_expert }, 0);
  4602. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert }, 0);
  4603. }
  4604. } break;
  4605. default:
  4606. throw std::runtime_error("unknown architecture");
  4607. }
  4608. if (n_moved_tensors > 0) {
  4609. LLAMA_LOG_DEBUG("%s: tensor '%s' (%s) (and %d others) cannot be used with preferred buffer type %s, using %s instead\n",
  4610. __func__, first_moved_tensor->name, ggml_type_name(first_moved_tensor->type), n_moved_tensors - 1,
  4611. ggml_backend_buft_name(first_moved_from_buft), ggml_backend_buft_name(first_moved_to_buft));
  4612. }
  4613. }
  4614. ml.done_getting_tensors();
  4615. ml.init_mappings(true, use_mlock ? &pimpl->mlock_mmaps : nullptr);
  4616. pimpl->mappings.reserve(ml.mappings.size());
  4617. // create the backend buffers
  4618. std::vector<std::pair<ggml_context *, llama_buf_map>> ctx_bufs;
  4619. ctx_bufs.reserve(ctx_map.size());
  4620. // Ensure we have enough capacity for the maximum backend buffer we will potentially create
  4621. const size_t n_max_backend_buffer = ctx_map.size() * ml.files.size();
  4622. pimpl->bufs.reserve(n_max_backend_buffer);
  4623. for (auto & it : ctx_map) {
  4624. ggml_backend_buffer_type_t buft = it.first;
  4625. ggml_context * ctx = it.second;
  4626. // skip contexts without tensors
  4627. if (ggml_get_first_tensor(ctx) == nullptr) {
  4628. continue;
  4629. }
  4630. llama_buf_map buf_map;
  4631. buf_map.reserve(n_max_backend_buffer);
  4632. // check if it is possible to use buffer_from_host_ptr with this buffer type
  4633. ggml_backend_dev_t dev = ggml_backend_buft_get_device(buft);
  4634. if (!dev) {
  4635. // FIXME: workaround for CPU backend buft having a NULL device
  4636. dev = ggml_backend_dev_by_type(GGML_BACKEND_DEVICE_TYPE_CPU);
  4637. if (!dev) {
  4638. throw std::runtime_error(format("%s: no CPU backend found", __func__));
  4639. }
  4640. }
  4641. ggml_backend_dev_props props;
  4642. ggml_backend_dev_get_props(dev, &props);
  4643. bool buffer_from_host_ptr_supported = props.caps.buffer_from_host_ptr;
  4644. bool is_default_buft = buft == ggml_backend_dev_buffer_type(dev);
  4645. if (ml.use_mmap && use_mmap_buffer && buffer_from_host_ptr_supported && is_default_buft) {
  4646. for (uint32_t idx = 0; idx < ml.files.size(); idx++) {
  4647. // only the mmap region containing the tensors in the model is mapped to the backend buffer
  4648. // this is important for metal with apple silicon: if the entire model could be mapped to a metal buffer, then we could just use metal for all layers
  4649. // this allows using partial offloading when the model size exceeds the metal buffer size, but not the RAM size
  4650. void * addr = nullptr;
  4651. size_t first, last; // NOLINT
  4652. ml.get_mapping_range(&first, &last, &addr, idx, ctx);
  4653. if (first >= last) {
  4654. continue;
  4655. }
  4656. const size_t max_size = ggml_get_max_tensor_size(ctx);
  4657. ggml_backend_buffer_t buf = ggml_backend_dev_buffer_from_host_ptr(dev, (char *) addr + first, last - first, max_size);
  4658. if (buf == nullptr) {
  4659. throw std::runtime_error(format("unable to allocate %s buffer", ggml_backend_buft_name(buft)));
  4660. }
  4661. pimpl->bufs.emplace_back(buf);
  4662. buf_map.emplace(idx, buf);
  4663. }
  4664. }
  4665. else {
  4666. ggml_backend_buffer_t buf = ggml_backend_alloc_ctx_tensors_from_buft(ctx, buft);
  4667. if (buf == nullptr) {
  4668. throw std::runtime_error(format("unable to allocate %s buffer", ggml_backend_buft_name(buft)));
  4669. }
  4670. pimpl->bufs.emplace_back(buf);
  4671. if (use_mlock && ggml_backend_buffer_is_host(buf)) {
  4672. pimpl->mlock_bufs.emplace_back(new llama_mlock);
  4673. auto & mlock_buf = pimpl->mlock_bufs.back();
  4674. mlock_buf->init (ggml_backend_buffer_get_base(buf));
  4675. mlock_buf->grow_to(ggml_backend_buffer_get_size(buf));
  4676. }
  4677. for (uint32_t idx = 0; idx < ml.files.size(); idx++) {
  4678. buf_map.emplace(idx, buf);
  4679. }
  4680. }
  4681. if (pimpl->bufs.empty()) {
  4682. throw std::runtime_error("failed to allocate buffer");
  4683. }
  4684. for (auto & buf : buf_map) {
  4685. // indicate that this buffer contains weights
  4686. // this is used by ggml_backend_sched to improve op scheduling: ops that use a weight are preferably scheduled to the backend that contains the weight
  4687. ggml_backend_buffer_set_usage(buf.second, GGML_BACKEND_BUFFER_USAGE_WEIGHTS);
  4688. }
  4689. ctx_bufs.emplace_back(ctx, buf_map);
  4690. }
  4691. if (llama_supports_gpu_offload()) {
  4692. const int n_gpu = std::min(n_gpu_layers, int(hparams.n_layer));
  4693. LLAMA_LOG_INFO("%s: offloading %d repeating layers to GPU\n", __func__, n_gpu);
  4694. if (n_gpu_layers > (int) hparams.n_layer) {
  4695. LLAMA_LOG_INFO("%s: offloading output layer to GPU\n", __func__);
  4696. }
  4697. const int max_backend_supported_layers = hparams.n_layer + 1;
  4698. const int max_offloadable_layers = hparams.n_layer + 1;
  4699. LLAMA_LOG_INFO("%s: offloaded %d/%d layers to GPU\n", __func__, std::min(n_gpu_layers, max_offloadable_layers), max_backend_supported_layers);
  4700. }
  4701. // print memory requirements per buffer type
  4702. for (auto & buf : pimpl->bufs) {
  4703. LLAMA_LOG_INFO("%s: %12s model buffer size = %8.2f MiB\n", __func__, ggml_backend_buffer_name(buf.get()), ggml_backend_buffer_get_size(buf.get()) / 1024.0 / 1024.0);
  4704. }
  4705. // populate tensors_by_name
  4706. for (auto & ctx : pimpl->ctxs) {
  4707. for (auto * cur = ggml_get_first_tensor(ctx.get()); cur != NULL; cur = ggml_get_next_tensor(ctx.get(), cur)) {
  4708. tensors_by_name.emplace_back(ggml_get_name(cur), cur);
  4709. }
  4710. }
  4711. // load tensor data
  4712. for (auto & it : ctx_bufs) {
  4713. ggml_context * ctx = it.first;
  4714. auto & bufs = it.second;
  4715. if (!ml.load_all_data(ctx, bufs, use_mlock ? &pimpl->mlock_mmaps : NULL, params.progress_callback, params.progress_callback_user_data)) {
  4716. return false;
  4717. }
  4718. }
  4719. if (use_mmap_buffer) {
  4720. for (auto & mapping : ml.mappings) {
  4721. pimpl->mappings.emplace_back(std::move(mapping));
  4722. }
  4723. }
  4724. return true;
  4725. }
  4726. std::string llama_model::arch_name() const {
  4727. return llm_arch_name(arch);
  4728. }
  4729. std::string llama_model::type_name() const {
  4730. return llm_type_name(type);
  4731. }
  4732. std::string llama_model::desc() const {
  4733. return pimpl->desc_str;
  4734. }
  4735. size_t llama_model::size() const {
  4736. return pimpl->n_bytes;
  4737. }
  4738. size_t llama_model::n_tensors() const {
  4739. return tensors_by_name.size();
  4740. }
  4741. size_t llama_model::n_devices() const {
  4742. return devices.size();
  4743. }
  4744. uint64_t llama_model::n_elements() const {
  4745. return pimpl->n_elements;
  4746. }
  4747. void llama_model::print_info() const {
  4748. const std::string rope_scaling_type = llama_rope_scaling_type_name(hparams.rope_scaling_type_train);
  4749. auto print_f = [](const std::function<uint32_t(uint32_t)> & f, uint32_t n) {
  4750. bool is_var = false;
  4751. std::vector<uint32_t> v;
  4752. for (uint32_t i = 0; i < n; ++i) {
  4753. v.push_back(f(i));
  4754. if (v[i] != v[0]) {
  4755. is_var = true;
  4756. }
  4757. }
  4758. std::stringstream ss;
  4759. if (is_var) {
  4760. ss << "[";
  4761. for (uint32_t i = 0; i < n; ++i) {
  4762. ss << v[i];
  4763. if (i < n - 1) {
  4764. ss << ", ";
  4765. }
  4766. }
  4767. ss << "]";
  4768. } else {
  4769. ss << v[0];
  4770. }
  4771. return ss.str();
  4772. };
  4773. // hparams
  4774. LLAMA_LOG_INFO("%s: arch = %s\n", __func__, arch_name().c_str());
  4775. LLAMA_LOG_INFO("%s: vocab_only = %d\n", __func__, hparams.vocab_only);
  4776. if (!hparams.vocab_only) {
  4777. LLAMA_LOG_INFO("%s: n_ctx_train = %u\n", __func__, hparams.n_ctx_train);
  4778. LLAMA_LOG_INFO("%s: n_embd = %u\n", __func__, hparams.n_embd);
  4779. LLAMA_LOG_INFO("%s: n_layer = %u\n", __func__, hparams.n_layer);
  4780. LLAMA_LOG_INFO("%s: n_head = %s\n", __func__, print_f([&](uint32_t il) { return hparams.n_head(il); }, hparams.n_layer).c_str());
  4781. LLAMA_LOG_INFO("%s: n_head_kv = %s\n", __func__, print_f([&](uint32_t il) { return hparams.n_head_kv(il); }, hparams.n_layer).c_str());
  4782. LLAMA_LOG_INFO("%s: n_rot = %u\n", __func__, hparams.n_rot);
  4783. LLAMA_LOG_INFO("%s: n_swa = %u\n", __func__, hparams.n_swa);
  4784. LLAMA_LOG_INFO("%s: is_swa_any = %u\n", __func__, hparams.is_swa_any());
  4785. LLAMA_LOG_INFO("%s: n_embd_head_k = %u\n", __func__, hparams.n_embd_head_k);
  4786. LLAMA_LOG_INFO("%s: n_embd_head_v = %u\n", __func__, hparams.n_embd_head_v);
  4787. LLAMA_LOG_INFO("%s: n_gqa = %s\n", __func__, print_f([&](uint32_t il) { return hparams.n_gqa(il); }, hparams.n_layer).c_str());
  4788. LLAMA_LOG_INFO("%s: n_embd_k_gqa = %s\n", __func__, print_f([&](uint32_t il) { return hparams.n_embd_k_gqa(il); }, hparams.n_layer).c_str());
  4789. LLAMA_LOG_INFO("%s: n_embd_v_gqa = %s\n", __func__, print_f([&](uint32_t il) { return hparams.n_embd_v_gqa(il); }, hparams.n_layer).c_str());
  4790. LLAMA_LOG_INFO("%s: f_norm_eps = %.1e\n", __func__, hparams.f_norm_eps);
  4791. LLAMA_LOG_INFO("%s: f_norm_rms_eps = %.1e\n", __func__, hparams.f_norm_rms_eps);
  4792. LLAMA_LOG_INFO("%s: f_clamp_kqv = %.1e\n", __func__, hparams.f_clamp_kqv);
  4793. LLAMA_LOG_INFO("%s: f_max_alibi_bias = %.1e\n", __func__, hparams.f_max_alibi_bias);
  4794. LLAMA_LOG_INFO("%s: f_logit_scale = %.1e\n", __func__, hparams.f_logit_scale);
  4795. LLAMA_LOG_INFO("%s: f_attn_scale = %.1e\n", __func__, hparams.f_attention_scale);
  4796. LLAMA_LOG_INFO("%s: n_ff = %s\n", __func__, print_f([&](uint32_t il) { return hparams.n_ff(il); }, hparams.n_layer).c_str());
  4797. LLAMA_LOG_INFO("%s: n_expert = %u\n", __func__, hparams.n_expert);
  4798. LLAMA_LOG_INFO("%s: n_expert_used = %u\n", __func__, hparams.n_expert_used);
  4799. LLAMA_LOG_INFO("%s: causal attn = %d\n", __func__, hparams.causal_attn);
  4800. LLAMA_LOG_INFO("%s: pooling type = %d\n", __func__, hparams.pooling_type);
  4801. LLAMA_LOG_INFO("%s: rope type = %d\n", __func__, hparams.rope_type);
  4802. LLAMA_LOG_INFO("%s: rope scaling = %s\n", __func__, rope_scaling_type.c_str());
  4803. LLAMA_LOG_INFO("%s: freq_base_train = %.1f\n", __func__, hparams.rope_freq_base_train);
  4804. LLAMA_LOG_INFO("%s: freq_scale_train = %g\n", __func__, hparams.rope_freq_scale_train);
  4805. LLAMA_LOG_INFO("%s: n_ctx_orig_yarn = %u\n", __func__, hparams.n_ctx_orig_yarn);
  4806. LLAMA_LOG_INFO("%s: rope_finetuned = %s\n", __func__, hparams.rope_finetuned ? "yes" : "unknown");
  4807. if (!classifier_labels.empty()) {
  4808. LLAMA_LOG_INFO("%s: n_cls_out = %u\n", __func__, hparams.n_cls_out);
  4809. size_t i = 0;
  4810. for (auto label : classifier_labels) {
  4811. LLAMA_LOG_INFO("%s: cls_label[%2zu] = %s\n", __func__, i++, label.c_str());
  4812. }
  4813. }
  4814. }
  4815. if (arch == LLM_ARCH_MAMBA ||
  4816. arch == LLM_ARCH_MAMBA2 ||
  4817. arch == LLM_ARCH_JAMBA ||
  4818. arch == LLM_ARCH_FALCON_H1 ||
  4819. arch == LLM_ARCH_PLAMO2 ||
  4820. arch == LLM_ARCH_GRANITE_HYBRID) {
  4821. LLAMA_LOG_INFO("%s: ssm_d_conv = %u\n", __func__, hparams.ssm_d_conv);
  4822. LLAMA_LOG_INFO("%s: ssm_d_inner = %u\n", __func__, hparams.ssm_d_inner);
  4823. LLAMA_LOG_INFO("%s: ssm_d_state = %u\n", __func__, hparams.ssm_d_state);
  4824. LLAMA_LOG_INFO("%s: ssm_dt_rank = %u\n", __func__, hparams.ssm_dt_rank);
  4825. LLAMA_LOG_INFO("%s: ssm_n_group = %u\n", __func__, hparams.ssm_n_group);
  4826. LLAMA_LOG_INFO("%s: ssm_dt_b_c_rms = %d\n", __func__, hparams.ssm_dt_b_c_rms);
  4827. }
  4828. LLAMA_LOG_INFO("%s: model type = %s\n", __func__, type_name().c_str());
  4829. if (pimpl->n_elements >= 1e12) {
  4830. LLAMA_LOG_INFO("%s: model params = %.2f T\n", __func__, pimpl->n_elements*1e-12);
  4831. } else if (pimpl->n_elements >= 1e9) {
  4832. LLAMA_LOG_INFO("%s: model params = %.2f B\n", __func__, pimpl->n_elements*1e-9);
  4833. } else if (pimpl->n_elements >= 1e6) {
  4834. LLAMA_LOG_INFO("%s: model params = %.2f M\n", __func__, pimpl->n_elements*1e-6);
  4835. } else {
  4836. LLAMA_LOG_INFO("%s: model params = %.2f K\n", __func__, pimpl->n_elements*1e-3);
  4837. }
  4838. // general kv
  4839. LLAMA_LOG_INFO("%s: general.name = %s\n", __func__, name.c_str());
  4840. if (arch == LLM_ARCH_DEEPSEEK) {
  4841. LLAMA_LOG_INFO("%s: n_layer_dense_lead = %d\n", __func__, hparams.n_layer_dense_lead);
  4842. LLAMA_LOG_INFO("%s: n_ff_exp = %d\n", __func__, hparams.n_ff_exp);
  4843. LLAMA_LOG_INFO("%s: n_expert_shared = %d\n", __func__, hparams.n_expert_shared);
  4844. LLAMA_LOG_INFO("%s: expert_weights_scale = %.1f\n", __func__, hparams.expert_weights_scale);
  4845. }
  4846. if (arch == LLM_ARCH_DEEPSEEK2) {
  4847. LLAMA_LOG_INFO("%s: n_layer_dense_lead = %d\n", __func__, hparams.n_layer_dense_lead);
  4848. LLAMA_LOG_INFO("%s: n_lora_q = %d\n", __func__, hparams.n_lora_q);
  4849. LLAMA_LOG_INFO("%s: n_lora_kv = %d\n", __func__, hparams.n_lora_kv);
  4850. LLAMA_LOG_INFO("%s: n_embd_head_k_mla = %d\n", __func__, hparams.n_embd_head_k_mla);
  4851. LLAMA_LOG_INFO("%s: n_embd_head_v_mla = %d\n", __func__, hparams.n_embd_head_v_mla);
  4852. LLAMA_LOG_INFO("%s: n_ff_exp = %d\n", __func__, hparams.n_ff_exp);
  4853. LLAMA_LOG_INFO("%s: n_expert_shared = %d\n", __func__, hparams.n_expert_shared);
  4854. LLAMA_LOG_INFO("%s: expert_weights_scale = %.1f\n", __func__, hparams.expert_weights_scale);
  4855. LLAMA_LOG_INFO("%s: expert_weights_norm = %d\n", __func__, hparams.expert_weights_norm);
  4856. LLAMA_LOG_INFO("%s: expert_gating_func = %s\n", __func__, llama_expert_gating_func_name((llama_expert_gating_func_type) hparams.expert_gating_func));
  4857. LLAMA_LOG_INFO("%s: rope_yarn_log_mul = %.4f\n", __func__, hparams.rope_yarn_log_mul);
  4858. }
  4859. if (arch == LLM_ARCH_QWEN2MOE) {
  4860. LLAMA_LOG_INFO("%s: n_ff_exp = %d\n", __func__, hparams.n_ff_exp);
  4861. LLAMA_LOG_INFO("%s: n_ff_shexp = %d\n", __func__, hparams.n_ff_shexp);
  4862. }
  4863. if (arch == LLM_ARCH_QWEN3MOE || arch == LLM_ARCH_OPENAI_MOE) {
  4864. LLAMA_LOG_INFO("%s: n_ff_exp = %d\n", __func__, hparams.n_ff_exp);
  4865. }
  4866. if (arch == LLM_ARCH_MINICPM ||
  4867. arch == LLM_ARCH_GRANITE ||
  4868. arch == LLM_ARCH_GRANITE_MOE ||
  4869. arch == LLM_ARCH_GRANITE_HYBRID) {
  4870. LLAMA_LOG_INFO("%s: f_embedding_scale = %f\n", __func__, hparams.f_embedding_scale);
  4871. LLAMA_LOG_INFO("%s: f_residual_scale = %f\n", __func__, hparams.f_residual_scale);
  4872. LLAMA_LOG_INFO("%s: f_attention_scale = %f\n", __func__, hparams.f_attention_scale);
  4873. LLAMA_LOG_INFO("%s: n_ff_shexp = %d\n", __func__, hparams.n_ff_shexp);
  4874. }
  4875. if (arch == LLM_ARCH_BAILINGMOE) {
  4876. LLAMA_LOG_INFO("%s: n_layer_dense_lead = %d\n", __func__, hparams.n_layer_dense_lead);
  4877. LLAMA_LOG_INFO("%s: n_ff_exp = %d\n", __func__, hparams.n_ff_exp);
  4878. LLAMA_LOG_INFO("%s: n_expert_shared = %d\n", __func__, hparams.n_expert_shared);
  4879. LLAMA_LOG_INFO("%s: expert_weights_scale = %.1f\n", __func__, hparams.expert_weights_scale);
  4880. LLAMA_LOG_INFO("%s: expert_weights_norm = %d\n", __func__, hparams.expert_weights_norm);
  4881. }
  4882. if (arch == LLM_ARCH_SMALLTHINKER) {
  4883. LLAMA_LOG_INFO("%s: n_ff_exp = %d\n", __func__, hparams.n_ff_exp);
  4884. LLAMA_LOG_INFO("%s: expert_gating_func = %s\n", __func__, llama_expert_gating_func_name((llama_expert_gating_func_type) hparams.expert_gating_func));
  4885. }
  4886. vocab.print_info();
  4887. }
  4888. ggml_backend_dev_t llama_model::dev_layer(int il) const {
  4889. return pimpl->dev_layer.at(il).dev;
  4890. }
  4891. ggml_backend_dev_t llama_model::dev_output() const {
  4892. return pimpl->dev_output.dev;
  4893. }
  4894. template<typename F>
  4895. static bool buft_supported(ggml_backend_buffer_type_t buft, ggml_backend_dev_t dev, F & fn) {
  4896. ggml_init_params params = {
  4897. /*.mem_size =*/ ggml_tensor_overhead()*8,
  4898. /*.mem_buffer =*/ NULL,
  4899. /*.no_alloc =*/ true,
  4900. };
  4901. ggml_context_ptr ctx { ggml_init(params) };
  4902. if (!ctx) {
  4903. throw std::runtime_error(format("failed to create ggml context"));
  4904. }
  4905. ggml_backend_buffer_ptr buf { ggml_backend_buft_alloc_buffer(buft, 0) };
  4906. ggml_tensor * op_tensor = fn(ctx.get());
  4907. for (int i = 0; i < GGML_MAX_SRC; i++) {
  4908. if (op_tensor->src[i] != nullptr) {
  4909. assert(op_tensor->src[i]->buffer == nullptr);
  4910. op_tensor->src[i]->buffer = buf.get();
  4911. }
  4912. }
  4913. bool op_supported = ggml_backend_dev_supports_op(dev, op_tensor);
  4914. return op_supported;
  4915. }
  4916. template<typename F>
  4917. static ggml_backend_buffer_type_t select_buft(const buft_list_t & buft_list, const F & fn) {
  4918. for (const auto & cur : buft_list) {
  4919. ggml_backend_dev_t cur_dev = cur.first;
  4920. ggml_backend_buffer_type_t cur_buft = cur.second;
  4921. if (buft_supported(cur_buft, cur_dev, fn)) {
  4922. return cur_buft;
  4923. }
  4924. }
  4925. throw std::runtime_error(format("no suitable buffer type found"));
  4926. }
  4927. ggml_backend_buffer_type_t llama_model::select_buft(int il) const {
  4928. return ::select_buft(
  4929. *pimpl->dev_layer.at(il).buft_list,
  4930. [&](ggml_context * ctx) {
  4931. ggml_tensor * cur = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, hparams.n_embd);
  4932. ggml_tensor * layer_dir = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, hparams.n_embd);
  4933. return ggml_add(ctx, cur, layer_dir);
  4934. });
  4935. }
  4936. bool llama_model::has_tensor_overrides() const {
  4937. return pimpl->has_tensor_overrides;
  4938. }
  4939. const ggml_tensor * llama_model::get_tensor(const char * name) const {
  4940. auto it = std::find_if(tensors_by_name.begin(), tensors_by_name.end(),
  4941. [name](const std::pair<std::string, ggml_tensor *> & it) {
  4942. return it.first == name;
  4943. });
  4944. if (it == tensors_by_name.end()) {
  4945. return nullptr;
  4946. }
  4947. return it->second;
  4948. }
  4949. float llama_model::get_rope_freq_base (const llama_cparams & cparams, int il) const {
  4950. return hparams.is_swa(il) ? hparams.rope_freq_base_train_swa : cparams.rope_freq_base;
  4951. }
  4952. float llama_model::get_rope_freq_scale(const llama_cparams & cparams, int il) const {
  4953. return hparams.is_swa(il) ? hparams.rope_freq_scale_train_swa : cparams.rope_freq_scale;
  4954. }
  4955. ggml_tensor * llama_model::get_rope_factors(const llama_cparams & cparams, int il) const {
  4956. const uint32_t n_ctx_per_seq = cparams.n_ctx / cparams.n_seq_max;
  4957. // choose long/short freq factors based on the context size
  4958. if (layers[il].rope_freqs != nullptr) {
  4959. return layers[il].rope_freqs;
  4960. }
  4961. if (n_ctx_per_seq > hparams.n_ctx_orig_yarn) {
  4962. return layers[il].rope_long;
  4963. }
  4964. return layers[il].rope_short;
  4965. }
  4966. struct llm_build_llama : public llm_graph_context {
  4967. llm_build_llama(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  4968. const int64_t n_embd_head = hparams.n_embd_head_v;
  4969. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  4970. GGML_ASSERT(n_embd_head == hparams.n_rot);
  4971. ggml_tensor * cur;
  4972. ggml_tensor * inpL;
  4973. inpL = build_inp_embd(model.tok_embd);
  4974. // inp_pos - contains the positions
  4975. ggml_tensor * inp_pos = build_inp_pos();
  4976. auto * inp_attn = build_attn_inp_kv_unified();
  4977. const float kq_scale = hparams.f_attention_scale == 0.0f ? 1.0f/sqrtf(float(n_embd_head)) : hparams.f_attention_scale;
  4978. ggml_tensor * inp_out_ids = build_inp_out_ids();
  4979. for (int il = 0; il < n_layer; ++il) {
  4980. ggml_tensor * inpSA = inpL;
  4981. // norm
  4982. cur = build_norm(inpL,
  4983. model.layers[il].attn_norm, NULL,
  4984. LLM_NORM_RMS, il);
  4985. cb(cur, "attn_norm", il);
  4986. // self-attention
  4987. {
  4988. // rope freq factors for llama3; may return nullptr for llama2 and other models
  4989. ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
  4990. // compute Q and K and RoPE them
  4991. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  4992. cb(Qcur, "Qcur", il);
  4993. if (model.layers[il].bq) {
  4994. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  4995. cb(Qcur, "Qcur", il);
  4996. }
  4997. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  4998. cb(Kcur, "Kcur", il);
  4999. if (model.layers[il].bk) {
  5000. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  5001. cb(Kcur, "Kcur", il);
  5002. }
  5003. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  5004. cb(Vcur, "Vcur", il);
  5005. if (model.layers[il].bv) {
  5006. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  5007. cb(Vcur, "Vcur", il);
  5008. }
  5009. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  5010. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  5011. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  5012. Qcur = ggml_rope_ext(
  5013. ctx0, Qcur, inp_pos, rope_factors,
  5014. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  5015. ext_factor, attn_factor, beta_fast, beta_slow
  5016. );
  5017. Kcur = ggml_rope_ext(
  5018. ctx0, Kcur, inp_pos, rope_factors,
  5019. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  5020. ext_factor, attn_factor, beta_fast, beta_slow
  5021. );
  5022. cb(Qcur, "Qcur", il);
  5023. cb(Kcur, "Kcur", il);
  5024. cb(Vcur, "Vcur", il);
  5025. cur = build_attn(inp_attn,
  5026. model.layers[il].wo, model.layers[il].bo,
  5027. Qcur, Kcur, Vcur, nullptr, nullptr, kq_scale, il);
  5028. cb(cur, "attn_out", il);
  5029. }
  5030. if (il == n_layer - 1 && inp_out_ids) {
  5031. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  5032. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  5033. }
  5034. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  5035. cb(ffn_inp, "ffn_inp", il);
  5036. // feed-forward network (non-MoE)
  5037. if (model.layers[il].ffn_gate_inp == nullptr) {
  5038. cur = build_norm(ffn_inp,
  5039. model.layers[il].ffn_norm, NULL,
  5040. LLM_NORM_RMS, il);
  5041. cb(cur, "ffn_norm", il);
  5042. cur = build_ffn(cur,
  5043. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  5044. model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, NULL,
  5045. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  5046. NULL,
  5047. LLM_FFN_SILU, LLM_FFN_PAR, il);
  5048. cb(cur, "ffn_out", il);
  5049. } else {
  5050. // MoE branch
  5051. cur = build_norm(ffn_inp,
  5052. model.layers[il].ffn_norm, NULL,
  5053. LLM_NORM_RMS, il);
  5054. cb(cur, "ffn_norm", il);
  5055. cur = build_moe_ffn(cur,
  5056. model.layers[il].ffn_gate_inp,
  5057. model.layers[il].ffn_up_exps,
  5058. model.layers[il].ffn_gate_exps,
  5059. model.layers[il].ffn_down_exps,
  5060. nullptr,
  5061. n_expert, n_expert_used,
  5062. LLM_FFN_SILU, true,
  5063. false, 0.0,
  5064. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  5065. il);
  5066. cb(cur, "ffn_moe_out", il);
  5067. }
  5068. cur = ggml_add(ctx0, cur, ffn_inp);
  5069. cb(cur, "ffn_out", il);
  5070. cur = build_cvec(cur, il);
  5071. cb(cur, "l_out", il);
  5072. // input for next layer
  5073. inpL = cur;
  5074. }
  5075. cur = inpL;
  5076. cur = build_norm(cur,
  5077. model.output_norm, NULL,
  5078. LLM_NORM_RMS, -1);
  5079. cb(cur, "result_norm", -1);
  5080. res->t_embd = cur;
  5081. // lm_head
  5082. cur = build_lora_mm(model.output, cur);
  5083. cb(cur, "result_output", -1);
  5084. res->t_logits = cur;
  5085. ggml_build_forward_expand(gf, cur);
  5086. }
  5087. };
  5088. struct llm_build_llama_iswa : public llm_graph_context {
  5089. llm_build_llama_iswa(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  5090. const int64_t n_embd_head = hparams.n_embd_head_v;
  5091. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  5092. GGML_ASSERT(n_embd_head == hparams.n_rot);
  5093. ggml_tensor * cur;
  5094. ggml_tensor * inpL;
  5095. inpL = build_inp_embd(model.tok_embd);
  5096. // inp_pos - contains the positions
  5097. ggml_tensor * inp_pos = build_inp_pos();
  5098. // temperature tuning
  5099. ggml_tensor * inp_attn_scale = nullptr;
  5100. inp_attn_scale = build_inp_attn_scale();
  5101. auto * inp_attn = build_attn_inp_kv_unified_iswa();
  5102. const float kq_scale = hparams.f_attention_scale == 0.0f ? 1.0f/sqrtf(float(n_embd_head)) : hparams.f_attention_scale;
  5103. ggml_tensor * inp_out_ids = build_inp_out_ids();
  5104. for (int il = 0; il < n_layer; ++il) {
  5105. ggml_tensor * inpSA = inpL;
  5106. const bool use_rope = (il + 1) % hparams.n_no_rope_layer_step != 0;
  5107. // norm
  5108. cur = build_norm(inpL,
  5109. model.layers[il].attn_norm, NULL,
  5110. LLM_NORM_RMS, il);
  5111. cb(cur, "attn_norm", il);
  5112. // self-attention
  5113. {
  5114. // rope freq factors for llama3; may return nullptr for llama2 and other models
  5115. ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
  5116. // compute Q and K and RoPE them
  5117. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  5118. cb(Qcur, "Qcur", il);
  5119. if (model.layers[il].bq) {
  5120. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  5121. cb(Qcur, "Qcur", il);
  5122. }
  5123. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  5124. cb(Kcur, "Kcur", il);
  5125. if (model.layers[il].bk) {
  5126. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  5127. cb(Kcur, "Kcur", il);
  5128. }
  5129. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  5130. cb(Vcur, "Vcur", il);
  5131. if (model.layers[il].bv) {
  5132. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  5133. cb(Vcur, "Vcur", il);
  5134. }
  5135. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  5136. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  5137. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  5138. if (use_rope) {
  5139. Qcur = ggml_rope_ext(
  5140. ctx0, Qcur, inp_pos, rope_factors,
  5141. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  5142. ext_factor, attn_factor, beta_fast, beta_slow
  5143. );
  5144. Kcur = ggml_rope_ext(
  5145. ctx0, Kcur, inp_pos, rope_factors,
  5146. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  5147. ext_factor, attn_factor, beta_fast, beta_slow
  5148. );
  5149. } else if (inp_attn_scale) {
  5150. Qcur = ggml_mul(ctx0, Qcur, inp_attn_scale);
  5151. }
  5152. cb(Qcur, "Qcur", il);
  5153. cb(Kcur, "Kcur", il);
  5154. cb(Vcur, "Vcur", il);
  5155. if (use_rope && hparams.use_kq_norm) {
  5156. // Llama4TextL2Norm
  5157. Qcur = ggml_rms_norm(ctx0, Qcur, hparams.f_norm_rms_eps);
  5158. Kcur = ggml_rms_norm(ctx0, Kcur, hparams.f_norm_rms_eps);
  5159. cb(Qcur, "Qcur_normed", il);
  5160. cb(Kcur, "Kcur_normed", il);
  5161. }
  5162. cur = build_attn(inp_attn,
  5163. model.layers[il].wo, model.layers[il].bo,
  5164. Qcur, Kcur, Vcur, nullptr, nullptr, kq_scale, il);
  5165. cb(cur, "attn_out", il);
  5166. }
  5167. if (il == n_layer - 1 && inp_out_ids) {
  5168. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  5169. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  5170. }
  5171. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  5172. cb(ffn_inp, "ffn_inp", il);
  5173. // feed-forward network (non-MoE)
  5174. if (model.layers[il].ffn_gate_inp == nullptr) {
  5175. cur = build_norm(ffn_inp,
  5176. model.layers[il].ffn_norm, NULL,
  5177. LLM_NORM_RMS, il);
  5178. cb(cur, "ffn_norm", il);
  5179. cur = build_ffn(cur,
  5180. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  5181. model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, NULL,
  5182. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  5183. NULL,
  5184. LLM_FFN_SILU, LLM_FFN_PAR, il);
  5185. cb(cur, "ffn_out", il);
  5186. } else {
  5187. ggml_tensor * ffn_inp_normed = build_norm(ffn_inp,
  5188. model.layers[il].ffn_norm, NULL,
  5189. LLM_NORM_RMS, il);
  5190. cb(cur, "ffn_norm", il);
  5191. ggml_tensor * moe_out = build_moe_ffn(ffn_inp_normed,
  5192. model.layers[il].ffn_gate_inp,
  5193. model.layers[il].ffn_up_exps,
  5194. model.layers[il].ffn_gate_exps,
  5195. model.layers[il].ffn_down_exps,
  5196. nullptr,
  5197. n_expert, n_expert_used,
  5198. LLM_FFN_SILU, false,
  5199. false, 0.0,
  5200. LLAMA_EXPERT_GATING_FUNC_TYPE_SIGMOID,
  5201. il);
  5202. // Shared experts
  5203. ggml_tensor * shexp_out = build_ffn(ffn_inp_normed,
  5204. model.layers[il].ffn_up_shexp, NULL, NULL,
  5205. model.layers[il].ffn_gate_shexp, NULL, NULL,
  5206. model.layers[il].ffn_down_shexp, NULL, NULL,
  5207. NULL,
  5208. LLM_FFN_SILU, LLM_FFN_PAR, il);
  5209. cb(shexp_out, "ffn_moe_shexp", il);
  5210. cur = ggml_add(ctx0, moe_out, shexp_out);
  5211. cb(cur, "ffn_moe_out_merged", il);
  5212. }
  5213. cur = ggml_add(ctx0, cur, ffn_inp);
  5214. cb(cur, "ffn_out", il);
  5215. cur = build_cvec(cur, il);
  5216. cb(cur, "l_out", il);
  5217. // input for next layer
  5218. inpL = cur;
  5219. }
  5220. cur = inpL;
  5221. cur = build_norm(cur,
  5222. model.output_norm, NULL,
  5223. LLM_NORM_RMS, -1);
  5224. cb(cur, "result_norm", -1);
  5225. res->t_embd = cur;
  5226. // lm_head
  5227. cur = build_lora_mm(model.output, cur);
  5228. cb(cur, "result_output", -1);
  5229. res->t_logits = cur;
  5230. ggml_build_forward_expand(gf, cur);
  5231. }
  5232. };
  5233. struct llm_build_deci : public llm_graph_context {
  5234. llm_build_deci(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  5235. const int64_t n_embd_head = hparams.n_embd_head_v;
  5236. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  5237. GGML_ASSERT(n_embd_head == hparams.n_rot);
  5238. ggml_tensor * cur;
  5239. ggml_tensor * inpL;
  5240. inpL = build_inp_embd(model.tok_embd);
  5241. // inp_pos - contains the positions
  5242. ggml_tensor * inp_pos = build_inp_pos();
  5243. auto * inp_attn = build_attn_inp_kv_unified();
  5244. const float kq_scale = hparams.f_attention_scale == 0.0f ? 1.0f/sqrtf(float(n_embd_head)) : hparams.f_attention_scale;
  5245. ggml_tensor * inp_out_ids = build_inp_out_ids();
  5246. for (int il = 0; il < n_layer; ++il) {
  5247. ggml_tensor * inpSA = inpL;
  5248. const int64_t n_head_kv = hparams.n_head_kv(il);
  5249. const int64_t n_head = hparams.n_head(il);
  5250. const int64_t n_ff = hparams.n_ff(il);
  5251. if (n_head == 0) {
  5252. // attention-free layer of Llama-3_1-Nemotron-51B
  5253. cur = inpL;
  5254. } else {
  5255. // norm
  5256. cur = build_norm(inpL,
  5257. model.layers[il].attn_norm, NULL,
  5258. LLM_NORM_RMS, il);
  5259. cb(cur, "attn_norm", il);
  5260. }
  5261. if (n_head > 0 && n_head_kv == 0) {
  5262. // "linear attention" of Llama-3_1-Nemotron-51B
  5263. cur = build_lora_mm(model.layers[il].wo, cur);
  5264. cb(cur, "wo", il);
  5265. } else if (n_head > 0) {
  5266. // self-attention
  5267. // rope freq factors for llama3; may return nullptr for llama2 and other models
  5268. ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
  5269. // compute Q and K and RoPE them
  5270. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  5271. cb(Qcur, "Qcur", il);
  5272. if (model.layers[il].bq) {
  5273. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  5274. cb(Qcur, "Qcur", il);
  5275. }
  5276. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  5277. cb(Kcur, "Kcur", il);
  5278. if (model.layers[il].bk) {
  5279. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  5280. cb(Kcur, "Kcur", il);
  5281. }
  5282. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  5283. cb(Vcur, "Vcur", il);
  5284. if (model.layers[il].bv) {
  5285. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  5286. cb(Vcur, "Vcur", il);
  5287. }
  5288. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  5289. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  5290. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  5291. Qcur = ggml_rope_ext(
  5292. ctx0, Qcur, inp_pos, rope_factors,
  5293. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  5294. ext_factor, attn_factor, beta_fast, beta_slow
  5295. );
  5296. Kcur = ggml_rope_ext(
  5297. ctx0, Kcur, inp_pos, rope_factors,
  5298. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  5299. ext_factor, attn_factor, beta_fast, beta_slow
  5300. );
  5301. cb(Qcur, "Qcur", il);
  5302. cb(Kcur, "Kcur", il);
  5303. cb(Vcur, "Vcur", il);
  5304. cur = build_attn(inp_attn,
  5305. model.layers[il].wo, model.layers[il].bo,
  5306. Qcur, Kcur, Vcur, nullptr, nullptr, kq_scale, il);
  5307. }
  5308. if (il == n_layer - 1 && inp_out_ids) {
  5309. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  5310. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  5311. }
  5312. // FFN-free layer of Llama-3_1-Nemotron-Ultra-253B
  5313. if (n_ff == 0) {
  5314. continue;
  5315. }
  5316. // modified to support attention-free layer of Llama-3_1-Nemotron-51B
  5317. ggml_tensor * ffn_inp = cur;
  5318. if (n_head > 0) {
  5319. ffn_inp = ggml_add(ctx0, cur, inpSA);
  5320. cb(ffn_inp, "ffn_inp", il);
  5321. }
  5322. // feed-forward network
  5323. if (model.layers[il].ffn_gate_inp == nullptr) {
  5324. cur = build_norm(ffn_inp,
  5325. model.layers[il].ffn_norm, NULL,
  5326. LLM_NORM_RMS, il);
  5327. cb(cur, "ffn_norm", il);
  5328. cur = build_ffn(cur,
  5329. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  5330. model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, NULL,
  5331. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  5332. NULL,
  5333. LLM_FFN_SILU, LLM_FFN_PAR, il);
  5334. cb(cur, "ffn_out", il);
  5335. }
  5336. cur = ggml_add(ctx0, cur, ffn_inp);
  5337. cb(cur, "ffn_out", il);
  5338. cur = build_cvec(cur, il);
  5339. cb(cur, "l_out", il);
  5340. // input for next layer
  5341. inpL = cur;
  5342. }
  5343. cur = inpL;
  5344. cur = build_norm(cur,
  5345. model.output_norm, NULL,
  5346. LLM_NORM_RMS, -1);
  5347. cb(cur, "result_norm", -1);
  5348. res->t_embd = cur;
  5349. // lm_head
  5350. cur = build_lora_mm(model.output, cur);
  5351. cb(cur, "result_output", -1);
  5352. res->t_logits = cur;
  5353. ggml_build_forward_expand(gf, cur);
  5354. }
  5355. };
  5356. struct llm_build_baichuan : public llm_graph_context {
  5357. llm_build_baichuan(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  5358. const int64_t n_embd_head = hparams.n_embd_head_v;
  5359. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  5360. GGML_ASSERT(n_embd_head == hparams.n_rot);
  5361. ggml_tensor * cur;
  5362. ggml_tensor * inpL;
  5363. inpL = build_inp_embd(model.tok_embd);
  5364. // inp_pos - contains the positions
  5365. ggml_tensor * inp_pos = model.type == LLM_TYPE_7B ? build_inp_pos() : nullptr;
  5366. auto * inp_attn = build_attn_inp_kv_unified();
  5367. ggml_tensor * inp_out_ids = build_inp_out_ids();
  5368. for (int il = 0; il < n_layer; ++il) {
  5369. ggml_tensor * inpSA = inpL;
  5370. cur = build_norm(inpL,
  5371. model.layers[il].attn_norm, NULL,
  5372. LLM_NORM_RMS, il);
  5373. cb(cur, "attn_norm", il);
  5374. // self-attention
  5375. {
  5376. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  5377. cb(Qcur, "Qcur", il);
  5378. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  5379. cb(Kcur, "Kcur", il);
  5380. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  5381. cb(Vcur, "Vcur", il);
  5382. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  5383. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  5384. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  5385. switch (model.type) {
  5386. case LLM_TYPE_7B:
  5387. Qcur = ggml_rope_ext(
  5388. ctx0, Qcur, inp_pos, nullptr,
  5389. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  5390. ext_factor, attn_factor, beta_fast, beta_slow
  5391. );
  5392. Kcur = ggml_rope_ext(
  5393. ctx0, Kcur, inp_pos, nullptr,
  5394. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  5395. ext_factor, attn_factor, beta_fast, beta_slow
  5396. );
  5397. break;
  5398. case LLM_TYPE_13B:
  5399. break;
  5400. default:
  5401. GGML_ABORT("fatal error");
  5402. }
  5403. cb(Qcur, "Qcur", il);
  5404. cb(Kcur, "Kcur", il);
  5405. cb(Vcur, "Vcur", il);
  5406. cur = build_attn(inp_attn,
  5407. model.layers[il].wo, NULL,
  5408. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  5409. }
  5410. if (il == n_layer - 1 && inp_out_ids) {
  5411. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  5412. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  5413. }
  5414. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  5415. cb(ffn_inp, "ffn_inp", il);
  5416. // feed-forward network
  5417. {
  5418. cur = build_norm(ffn_inp,
  5419. model.layers[il].ffn_norm, NULL,
  5420. LLM_NORM_RMS, il);
  5421. cb(cur, "ffn_norm", il);
  5422. cur = build_ffn(cur,
  5423. model.layers[il].ffn_up, NULL, NULL,
  5424. model.layers[il].ffn_gate, NULL, NULL,
  5425. model.layers[il].ffn_down, NULL, NULL,
  5426. NULL,
  5427. LLM_FFN_SILU, LLM_FFN_PAR, il);
  5428. cb(cur, "ffn_out", il);
  5429. }
  5430. cur = ggml_add(ctx0, cur, ffn_inp);
  5431. cur = build_cvec(cur, il);
  5432. cb(cur, "l_out", il);
  5433. // input for next layer
  5434. inpL = cur;
  5435. }
  5436. cur = inpL;
  5437. cur = build_norm(cur,
  5438. model.output_norm, NULL,
  5439. LLM_NORM_RMS, -1);
  5440. cb(cur, "result_norm", -1);
  5441. res->t_embd = cur;
  5442. // lm_head
  5443. cur = build_lora_mm(model.output, cur);
  5444. cb(cur, "result_output", -1);
  5445. res->t_logits = cur;
  5446. ggml_build_forward_expand(gf, cur);
  5447. }
  5448. };
  5449. struct llm_build_xverse : public llm_graph_context {
  5450. llm_build_xverse(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  5451. const int64_t n_embd_head = hparams.n_embd_head_v;
  5452. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  5453. GGML_ASSERT(n_embd_head == hparams.n_rot);
  5454. ggml_tensor * cur;
  5455. ggml_tensor * inpL;
  5456. inpL = build_inp_embd(model.tok_embd);
  5457. // inp_pos - contains the positions
  5458. ggml_tensor * inp_pos = build_inp_pos();
  5459. auto * inp_attn = build_attn_inp_kv_unified();
  5460. ggml_tensor * inp_out_ids = build_inp_out_ids();
  5461. for (int il = 0; il < n_layer; ++il) {
  5462. ggml_tensor * inpSA = inpL;
  5463. cur = build_norm(inpL,
  5464. model.layers[il].attn_norm, NULL,
  5465. LLM_NORM_RMS, il);
  5466. cb(cur, "attn_norm", il);
  5467. // self-attention
  5468. {
  5469. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  5470. cb(Qcur, "Qcur", il);
  5471. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  5472. cb(Kcur, "Kcur", il);
  5473. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  5474. cb(Vcur, "Vcur", il);
  5475. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  5476. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  5477. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  5478. Qcur = ggml_rope_ext(
  5479. ctx0, Qcur, inp_pos, nullptr,
  5480. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  5481. ext_factor, attn_factor, beta_fast, beta_slow
  5482. );
  5483. Kcur = ggml_rope_ext(
  5484. ctx0, Kcur, inp_pos, nullptr,
  5485. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  5486. ext_factor, attn_factor, beta_fast, beta_slow
  5487. );
  5488. cb(Qcur, "Qcur", il);
  5489. cb(Kcur, "Kcur", il);
  5490. cb(Vcur, "Vcur", il);
  5491. cur = build_attn(inp_attn,
  5492. model.layers[il].wo, NULL,
  5493. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  5494. }
  5495. if (il == n_layer - 1 && inp_out_ids) {
  5496. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  5497. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  5498. }
  5499. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  5500. cb(ffn_inp, "ffn_inp", il);
  5501. // feed-forward network
  5502. {
  5503. cur = build_norm(ffn_inp,
  5504. model.layers[il].ffn_norm, NULL,
  5505. LLM_NORM_RMS, il);
  5506. cb(cur, "ffn_norm", il);
  5507. cur = build_ffn(cur,
  5508. model.layers[il].ffn_up, NULL, NULL,
  5509. model.layers[il].ffn_gate, NULL, NULL,
  5510. model.layers[il].ffn_down, NULL, NULL,
  5511. NULL,
  5512. LLM_FFN_SILU, LLM_FFN_PAR, il);
  5513. cb(cur, "ffn_out", il);
  5514. }
  5515. cur = ggml_add(ctx0, cur, ffn_inp);
  5516. cur = build_cvec(cur, il);
  5517. cb(cur, "l_out", il);
  5518. // input for next layer
  5519. inpL = cur;
  5520. }
  5521. cur = inpL;
  5522. cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1);
  5523. cb(cur, "result_norm", -1);
  5524. res->t_embd = cur;
  5525. // lm_head
  5526. cur = build_lora_mm(model.output, cur);
  5527. cb(cur, "result_output", -1);
  5528. res->t_logits = cur;
  5529. ggml_build_forward_expand(gf, cur);
  5530. }
  5531. };
  5532. struct llm_build_falcon : public llm_graph_context {
  5533. llm_build_falcon(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  5534. const int64_t n_embd_head = hparams.n_embd_head_v;
  5535. const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  5536. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  5537. GGML_ASSERT(n_embd_head == hparams.n_rot);
  5538. ggml_tensor * cur;
  5539. ggml_tensor * inpL;
  5540. inpL = build_inp_embd(model.tok_embd);
  5541. // inp_pos - contains the positions
  5542. ggml_tensor * inp_pos = build_inp_pos();
  5543. auto * inp_attn = build_attn_inp_kv_unified();
  5544. ggml_tensor * inp_out_ids = build_inp_out_ids();
  5545. for (int il = 0; il < n_layer; ++il) {
  5546. ggml_tensor * attn_norm;
  5547. attn_norm = build_norm(inpL,
  5548. model.layers[il].attn_norm,
  5549. model.layers[il].attn_norm_b,
  5550. LLM_NORM, il);
  5551. cb(attn_norm, "attn_norm", il);
  5552. // self-attention
  5553. {
  5554. if (model.layers[il].attn_norm_2) {
  5555. // Falcon-40B
  5556. cur = build_norm(inpL,
  5557. model.layers[il].attn_norm_2,
  5558. model.layers[il].attn_norm_2_b,
  5559. LLM_NORM, il);
  5560. cb(cur, "attn_norm_2", il);
  5561. } else {
  5562. cur = attn_norm;
  5563. }
  5564. cur = build_lora_mm(model.layers[il].wqkv, cur);
  5565. cb(cur, "wqkv", il);
  5566. ggml_tensor * Qcur = ggml_view_3d(ctx0, cur, n_embd_head, n_head, n_tokens, n_embd_head*sizeof(float), cur->nb[1], 0*sizeof(float)*(n_embd));
  5567. ggml_tensor * Kcur = ggml_view_3d(ctx0, cur, n_embd_head, n_head_kv, n_tokens, n_embd_head*sizeof(float), cur->nb[1], 1*sizeof(float)*(n_embd));
  5568. ggml_tensor * Vcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd + n_embd_gqa)));
  5569. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  5570. // using mode = 2 for neox mode
  5571. Qcur = ggml_rope_ext(
  5572. ctx0, Qcur, inp_pos, nullptr,
  5573. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  5574. ext_factor, attn_factor, beta_fast, beta_slow
  5575. );
  5576. Kcur = ggml_rope_ext(
  5577. ctx0, Kcur, inp_pos, nullptr,
  5578. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  5579. ext_factor, attn_factor, beta_fast, beta_slow
  5580. );
  5581. cb(Qcur, "Qcur", il);
  5582. cb(Kcur, "Kcur", il);
  5583. cb(Vcur, "Vcur", il);
  5584. cur = build_attn(inp_attn,
  5585. model.layers[il].wo, NULL,
  5586. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  5587. }
  5588. if (il == n_layer - 1 && inp_out_ids) {
  5589. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  5590. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  5591. attn_norm = ggml_get_rows(ctx0, attn_norm, inp_out_ids);
  5592. }
  5593. ggml_tensor * ffn_inp = cur;
  5594. // feed forward
  5595. {
  5596. cur = build_ffn(attn_norm, // !! use the attn norm, not the result
  5597. model.layers[il].ffn_up, NULL, NULL,
  5598. NULL, NULL, NULL,
  5599. model.layers[il].ffn_down, NULL, NULL,
  5600. NULL,
  5601. LLM_FFN_GELU, LLM_FFN_SEQ, il);
  5602. cb(cur, "ffn_out", il);
  5603. }
  5604. cur = ggml_add(ctx0, cur, ffn_inp);
  5605. cur = ggml_add(ctx0, cur, inpL);
  5606. cur = build_cvec(cur, il);
  5607. cb(cur, "l_out", il);
  5608. // input for next layer
  5609. inpL = cur;
  5610. }
  5611. cur = inpL;
  5612. // norm
  5613. cur = build_norm(cur,
  5614. model.output_norm,
  5615. model.output_norm_b,
  5616. LLM_NORM, -1);
  5617. cb(cur, "result_norm", -1);
  5618. res->t_embd = cur;
  5619. cur = build_lora_mm(model.output, cur);
  5620. cb(cur, "result_output", -1);
  5621. res->t_logits = cur;
  5622. ggml_build_forward_expand(gf, cur);
  5623. }
  5624. };
  5625. struct llm_build_grok : public llm_graph_context {
  5626. llm_build_grok(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  5627. const int64_t n_embd_head = hparams.n_embd_head_v;
  5628. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  5629. GGML_ASSERT(n_embd_head == hparams.n_rot);
  5630. ggml_tensor * cur;
  5631. ggml_tensor * inpL;
  5632. inpL = build_inp_embd(model.tok_embd);
  5633. // multiply by embedding_multiplier_scale of 78.38367176906169
  5634. inpL = ggml_scale(ctx0, inpL, 78.38367176906169f);
  5635. // inp_pos - contains the positions
  5636. ggml_tensor * inp_pos = build_inp_pos();
  5637. auto * inp_attn = build_attn_inp_kv_unified();
  5638. ggml_tensor * inp_out_ids = build_inp_out_ids();
  5639. for (int il = 0; il < n_layer; ++il) {
  5640. ggml_tensor * inpSA = inpL;
  5641. // norm
  5642. cur = build_norm(inpL,
  5643. model.layers[il].attn_norm, NULL,
  5644. LLM_NORM_RMS, il);
  5645. cb(cur, "attn_norm", il);
  5646. // self-attention
  5647. {
  5648. // compute Q and K and RoPE them
  5649. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  5650. cb(Qcur, "Qcur", il);
  5651. if (model.layers[il].bq) {
  5652. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  5653. cb(Qcur, "Qcur", il);
  5654. }
  5655. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  5656. cb(Kcur, "Kcur", il);
  5657. if (model.layers[il].bk) {
  5658. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  5659. cb(Kcur, "Kcur", il);
  5660. }
  5661. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  5662. cb(Vcur, "Vcur", il);
  5663. if (model.layers[il].bv) {
  5664. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  5665. cb(Vcur, "Vcur", il);
  5666. }
  5667. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  5668. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  5669. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  5670. Qcur = ggml_rope_ext(
  5671. ctx0, Qcur, inp_pos, nullptr,
  5672. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  5673. ext_factor, attn_factor, beta_fast, beta_slow
  5674. );
  5675. Kcur = ggml_rope_ext(
  5676. ctx0, Kcur, inp_pos, nullptr,
  5677. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  5678. ext_factor, attn_factor, beta_fast, beta_slow
  5679. );
  5680. cb(Qcur, "Qcur", il);
  5681. cb(Kcur, "Kcur", il);
  5682. cb(Vcur, "Vcur", il);
  5683. cur = build_attn(inp_attn,
  5684. model.layers[il].wo, model.layers[il].bo,
  5685. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f, il);
  5686. }
  5687. if (il == n_layer - 1 && inp_out_ids) {
  5688. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  5689. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  5690. }
  5691. // Grok
  5692. // if attn_out_norm is present then apply it before adding the input
  5693. if (model.layers[il].attn_out_norm) {
  5694. cur = build_norm(cur,
  5695. model.layers[il].attn_out_norm, NULL,
  5696. LLM_NORM_RMS, il);
  5697. cb(cur, "attn_out_norm", il);
  5698. }
  5699. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  5700. cb(ffn_inp, "ffn_inp", il);
  5701. // feed-forward network
  5702. // MoE branch
  5703. cur = build_norm(ffn_inp,
  5704. model.layers[il].ffn_norm, NULL,
  5705. LLM_NORM_RMS, il);
  5706. cb(cur, "ffn_norm", il);
  5707. cur = build_moe_ffn(cur,
  5708. model.layers[il].ffn_gate_inp,
  5709. model.layers[il].ffn_up_exps,
  5710. model.layers[il].ffn_gate_exps,
  5711. model.layers[il].ffn_down_exps,
  5712. nullptr,
  5713. n_expert, n_expert_used,
  5714. LLM_FFN_GELU, true,
  5715. false, 0.0,
  5716. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  5717. il);
  5718. cb(cur, "ffn_moe_out", il);
  5719. // Grok
  5720. // if layer_out_norm is present then apply it before adding the input
  5721. // Idea: maybe ffn_out_norm is a better name
  5722. if (model.layers[il].layer_out_norm) {
  5723. cur = build_norm(cur,
  5724. model.layers[il].layer_out_norm, NULL,
  5725. LLM_NORM_RMS, il);
  5726. cb(cur, "layer_out_norm", il);
  5727. }
  5728. cur = ggml_add(ctx0, cur, ffn_inp);
  5729. cb(cur, "ffn_out", il);
  5730. cur = build_cvec(cur, il);
  5731. cb(cur, "l_out", il);
  5732. // input for next layer
  5733. inpL = cur;
  5734. }
  5735. cur = inpL;
  5736. cur = build_norm(cur,
  5737. model.output_norm, NULL,
  5738. LLM_NORM_RMS, -1);
  5739. cb(cur, "result_norm", -1);
  5740. res->t_embd = cur;
  5741. // lm_head
  5742. cur = build_lora_mm(model.output, cur);
  5743. // Grok
  5744. // multiply logits by output_multiplier_scale of 0.5773502691896257
  5745. cur = ggml_scale(ctx0, cur, 0.5773502691896257f);
  5746. cb(cur, "result_output", -1);
  5747. res->t_logits = cur;
  5748. ggml_build_forward_expand(gf, cur);
  5749. }
  5750. };
  5751. struct llm_build_dbrx : public llm_graph_context {
  5752. llm_build_dbrx(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  5753. const int64_t n_embd_head = hparams.n_embd_head_v;
  5754. const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  5755. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  5756. GGML_ASSERT(n_embd_head == hparams.n_rot);
  5757. ggml_tensor * cur;
  5758. ggml_tensor * inpL;
  5759. inpL = build_inp_embd(model.tok_embd);
  5760. // inp_pos - contains the positions
  5761. ggml_tensor * inp_pos = build_inp_pos();
  5762. auto * inp_attn = build_attn_inp_kv_unified();
  5763. ggml_tensor * inp_out_ids = build_inp_out_ids();
  5764. for (int il = 0; il < n_layer; ++il) {
  5765. ggml_tensor * inpSA = inpL;
  5766. // norm
  5767. cur = build_norm(inpL,
  5768. model.layers[il].attn_norm, NULL,
  5769. LLM_NORM, il);
  5770. cb(cur, "attn_norm", il);
  5771. // self-attention
  5772. {
  5773. ggml_tensor * Qcur = nullptr;
  5774. ggml_tensor * Kcur = nullptr;
  5775. ggml_tensor * Vcur = nullptr;
  5776. cur = build_lora_mm(model.layers[il].wqkv, cur);
  5777. cb(cur, "wqkv", il);
  5778. cur = ggml_clamp(ctx0, cur, -hparams.f_clamp_kqv, hparams.f_clamp_kqv);
  5779. cb(cur, "wqkv_clamped", il);
  5780. Qcur = ggml_view_3d(ctx0, cur, n_embd_head, n_head, n_tokens, n_embd_head*sizeof(float), cur->nb[1], 0*sizeof(float)*(n_embd));
  5781. Kcur = ggml_view_3d(ctx0, cur, n_embd_head, n_head_kv, n_tokens, n_embd_head*sizeof(float), cur->nb[1], 1*sizeof(float)*(n_embd));
  5782. Vcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd + n_embd_gqa)));
  5783. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  5784. Qcur = ggml_rope_ext(
  5785. ctx0, Qcur, inp_pos, nullptr,
  5786. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  5787. ext_factor, attn_factor, beta_fast, beta_slow
  5788. );
  5789. Kcur = ggml_rope_ext(
  5790. ctx0, Kcur, inp_pos, nullptr,
  5791. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  5792. ext_factor, attn_factor, beta_fast, beta_slow
  5793. );
  5794. cb(Qcur, "Qcur", il);
  5795. cb(Kcur, "Kcur", il);
  5796. cb(Vcur, "Vcur", il);
  5797. cur = build_attn(inp_attn,
  5798. model.layers[il].wo, NULL,
  5799. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  5800. }
  5801. if (il == n_layer - 1 && inp_out_ids) {
  5802. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  5803. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  5804. }
  5805. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  5806. cb(ffn_inp, "ffn_inp", il);
  5807. // feed-forward network
  5808. // MoE branch
  5809. cur = build_norm(ffn_inp,
  5810. model.layers[il].attn_out_norm, NULL,
  5811. LLM_NORM, il);
  5812. cb(cur, "attn_out_norm", il);
  5813. cur = build_moe_ffn(cur,
  5814. model.layers[il].ffn_gate_inp,
  5815. model.layers[il].ffn_up_exps,
  5816. model.layers[il].ffn_gate_exps,
  5817. model.layers[il].ffn_down_exps,
  5818. nullptr,
  5819. n_expert, n_expert_used,
  5820. LLM_FFN_SILU, true,
  5821. false, 0.0,
  5822. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  5823. il);
  5824. cb(cur, "ffn_moe_out", il);
  5825. cur = ggml_add(ctx0, cur, ffn_inp);
  5826. cb(cur, "ffn_out", il);
  5827. cur = build_cvec(cur, il);
  5828. cb(cur, "l_out", il);
  5829. // input for next layer
  5830. inpL = cur;
  5831. }
  5832. cur = inpL;
  5833. cur = build_norm(cur,
  5834. model.output_norm, NULL,
  5835. LLM_NORM, -1);
  5836. cb(cur, "result_norm", -1);
  5837. res->t_embd = cur;
  5838. // lm_head
  5839. cur = build_lora_mm(model.output, cur);
  5840. cb(cur, "result_output", -1);
  5841. res->t_logits = cur;
  5842. ggml_build_forward_expand(gf, cur);
  5843. }
  5844. };
  5845. struct llm_build_starcoder : public llm_graph_context {
  5846. llm_build_starcoder(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  5847. const int64_t n_embd_head = hparams.n_embd_head_v;
  5848. const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  5849. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  5850. ggml_tensor * cur;
  5851. ggml_tensor * inpL;
  5852. inpL = build_inp_embd(model.tok_embd);
  5853. // inp_pos - contains the positions
  5854. ggml_tensor * inp_pos = build_inp_pos();
  5855. auto * inp_attn = build_attn_inp_kv_unified();
  5856. ggml_tensor * pos = ggml_get_rows(ctx0, model.pos_embd, inp_pos);
  5857. cb(pos, "pos_embd", -1);
  5858. inpL = ggml_add(ctx0, inpL, pos);
  5859. cb(inpL, "inpL", -1);
  5860. ggml_tensor * inp_out_ids = build_inp_out_ids();
  5861. for (int il = 0; il < n_layer; ++il) {
  5862. cur = build_norm(inpL,
  5863. model.layers[il].attn_norm,
  5864. model.layers[il].attn_norm_b,
  5865. LLM_NORM, il);
  5866. cb(cur, "attn_norm", il);
  5867. // self-attention
  5868. {
  5869. cur = build_lora_mm(model.layers[il].wqkv, cur);
  5870. cb(cur, "wqkv", il);
  5871. cur = ggml_add(ctx0, cur, model.layers[il].bqkv);
  5872. cb(cur, "bqkv", il);
  5873. ggml_tensor * Qcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd, n_tokens, cur->nb[1], 0*sizeof(float)*(n_embd)));
  5874. ggml_tensor * Kcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd)));
  5875. ggml_tensor * Vcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd + n_embd_gqa)));
  5876. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  5877. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  5878. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  5879. cb(Qcur, "Qcur", il);
  5880. cb(Kcur, "Kcur", il);
  5881. cb(Vcur, "Vcur", il);
  5882. cur = build_attn(inp_attn,
  5883. model.layers[il].wo, model.layers[il].bo,
  5884. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  5885. }
  5886. if (il == n_layer - 1 && inp_out_ids) {
  5887. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  5888. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  5889. }
  5890. // add the input
  5891. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
  5892. cb(ffn_inp, "ffn_inp", il);
  5893. // FF
  5894. {
  5895. cur = build_norm(ffn_inp,
  5896. model.layers[il].ffn_norm,
  5897. model.layers[il].ffn_norm_b,
  5898. LLM_NORM, il);
  5899. cb(cur, "ffn_norm", il);
  5900. cur = build_ffn(cur,
  5901. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  5902. NULL, NULL, NULL,
  5903. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  5904. NULL,
  5905. LLM_FFN_GELU, LLM_FFN_SEQ, il);
  5906. cb(cur, "ffn_out", il);
  5907. }
  5908. cur = ggml_add(ctx0, cur, ffn_inp);
  5909. cur = build_cvec(cur, il);
  5910. cb(cur, "l_out", il);
  5911. // input for next layer
  5912. inpL = cur;
  5913. }
  5914. cur = build_norm(inpL,
  5915. model.output_norm,
  5916. model.output_norm_b,
  5917. LLM_NORM, -1);
  5918. cb(cur, "result_norm", -1);
  5919. res->t_embd = cur;
  5920. cur = build_lora_mm(model.output, cur);
  5921. cb(cur, "result_output", -1);
  5922. res->t_logits = cur;
  5923. ggml_build_forward_expand(gf, cur);
  5924. }
  5925. };
  5926. struct llm_build_refact : public llm_graph_context {
  5927. llm_build_refact(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  5928. const int64_t n_embd_head = hparams.n_embd_head_v;
  5929. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  5930. ggml_tensor * cur;
  5931. ggml_tensor * inpL;
  5932. inpL = build_inp_embd(model.tok_embd);
  5933. auto * inp_attn = build_attn_inp_kv_unified();
  5934. ggml_tensor * inp_out_ids = build_inp_out_ids();
  5935. for (int il = 0; il < n_layer; ++il) {
  5936. ggml_tensor * inpSA = inpL;
  5937. cur = build_norm(inpL,
  5938. model.layers[il].attn_norm, NULL,
  5939. LLM_NORM_RMS, il);
  5940. cb(cur, "attn_norm", il);
  5941. // self-attention
  5942. {
  5943. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  5944. cb(Qcur, "Qcur", il);
  5945. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  5946. cb(Kcur, "Kcur", il);
  5947. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  5948. cb(Vcur, "Vcur", il);
  5949. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  5950. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  5951. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  5952. cb(Qcur, "Qcur", il);
  5953. cb(Kcur, "Kcur", il);
  5954. cb(Vcur, "Vcur", il);
  5955. cur = build_attn(inp_attn,
  5956. model.layers[il].wo, NULL,
  5957. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  5958. }
  5959. if (il == n_layer - 1 && inp_out_ids) {
  5960. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  5961. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  5962. }
  5963. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  5964. cb(ffn_inp, "ffn_inp", il);
  5965. // feed-forward network
  5966. {
  5967. cur = build_norm(ffn_inp,
  5968. model.layers[il].ffn_norm, NULL,
  5969. LLM_NORM_RMS, il);
  5970. cb(cur, "ffn_norm", il);
  5971. cur = build_ffn(cur,
  5972. model.layers[il].ffn_up, NULL, NULL,
  5973. model.layers[il].ffn_gate, NULL, NULL,
  5974. model.layers[il].ffn_down, NULL, NULL,
  5975. NULL,
  5976. LLM_FFN_SILU, LLM_FFN_PAR, il);
  5977. cb(cur, "ffn_out", il);
  5978. }
  5979. cur = ggml_add(ctx0, cur, ffn_inp);
  5980. cur = build_cvec(cur, il);
  5981. cb(cur, "l_out", il);
  5982. // input for next layer
  5983. inpL = cur;
  5984. }
  5985. cur = inpL;
  5986. cur = build_norm(cur,
  5987. model.output_norm, NULL,
  5988. LLM_NORM_RMS, -1);
  5989. cb(cur, "result_norm", -1);
  5990. res->t_embd = cur;
  5991. // lm_head
  5992. cur = build_lora_mm(model.output, cur);
  5993. cb(cur, "result_output", -1);
  5994. res->t_logits = cur;
  5995. ggml_build_forward_expand(gf, cur);
  5996. }
  5997. };
  5998. struct llm_build_bert : public llm_graph_context {
  5999. llm_build_bert(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  6000. const int64_t n_embd_head = hparams.n_embd_head_v;
  6001. const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  6002. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  6003. ggml_tensor * cur;
  6004. ggml_tensor * inpL;
  6005. ggml_tensor * inp_pos = nullptr;
  6006. if (model.arch != LLM_ARCH_JINA_BERT_V2) {
  6007. inp_pos = build_inp_pos();
  6008. }
  6009. // construct input embeddings (token, type, position)
  6010. inpL = build_inp_embd(model.tok_embd);
  6011. // token types are hardcoded to zero ("Sentence A")
  6012. if (model.type_embd) {
  6013. ggml_tensor * type_row0 = ggml_view_1d(ctx0, model.type_embd, n_embd, 0);
  6014. inpL = ggml_add(ctx0, inpL, type_row0);
  6015. }
  6016. if (model.arch == LLM_ARCH_BERT) {
  6017. inpL = ggml_add(ctx0, ggml_get_rows(ctx0, model.pos_embd, inp_pos), inpL);
  6018. }
  6019. cb(inpL, "inp_embd", -1);
  6020. // embed layer norm
  6021. inpL = build_norm(inpL, model.tok_norm, model.tok_norm_b, LLM_NORM, -1);
  6022. cb(inpL, "inp_norm", -1);
  6023. auto * inp_attn = build_attn_inp_no_cache();
  6024. ggml_tensor * inp_out_ids = build_inp_out_ids();
  6025. for (int il = 0; il < n_layer; ++il) {
  6026. ggml_tensor * cur = inpL;
  6027. {
  6028. ggml_tensor * Qcur;
  6029. ggml_tensor * Kcur;
  6030. ggml_tensor * Vcur;
  6031. // self-attention
  6032. if (model.layers[il].wqkv) {
  6033. cur = build_lora_mm(model.layers[il].wqkv, cur);
  6034. cb(cur, "wqkv", il);
  6035. if (model.layers[il].bqkv) {
  6036. cur = ggml_add(ctx0, cur, model.layers[il].bqkv);
  6037. cb(cur, "bqkv", il);
  6038. }
  6039. Qcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd, n_tokens, cur->nb[1], 0*sizeof(float)*(n_embd)));
  6040. Kcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd)));
  6041. Vcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd + n_embd_gqa)));
  6042. } else {
  6043. Qcur = ggml_add(ctx0, build_lora_mm(model.layers[il].wq, cur), model.layers[il].bq);
  6044. Kcur = ggml_add(ctx0, build_lora_mm(model.layers[il].wk, cur), model.layers[il].bk);
  6045. Vcur = ggml_add(ctx0, build_lora_mm(model.layers[il].wv, cur), model.layers[il].bv);
  6046. }
  6047. if (model.layers[il].attn_q_norm) {
  6048. Qcur = build_norm(Qcur,
  6049. model.layers[il].attn_q_norm,
  6050. model.layers[il].attn_q_norm_b,
  6051. LLM_NORM, il);
  6052. }
  6053. if (model.layers[il].attn_k_norm) {
  6054. Kcur = build_norm(Kcur,
  6055. model.layers[il].attn_k_norm,
  6056. model.layers[il].attn_k_norm_b,
  6057. LLM_NORM, il);
  6058. }
  6059. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  6060. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  6061. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  6062. // RoPE
  6063. if (model.arch == LLM_ARCH_NOMIC_BERT || model.arch == LLM_ARCH_NOMIC_BERT_MOE) {
  6064. Qcur = ggml_rope_ext(
  6065. ctx0, Qcur, inp_pos, nullptr,
  6066. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  6067. ext_factor, attn_factor, beta_fast, beta_slow
  6068. );
  6069. Kcur = ggml_rope_ext(
  6070. ctx0, Kcur, inp_pos, nullptr,
  6071. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  6072. ext_factor, attn_factor, beta_fast, beta_slow
  6073. );
  6074. }
  6075. cb(Qcur, "Qcur", il);
  6076. cb(Kcur, "Kcur", il);
  6077. cb(Vcur, "Vcur", il);
  6078. cur = build_attn(inp_attn,
  6079. model.layers[il].wo, model.layers[il].bo,
  6080. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  6081. cb(cur, "kqv_out", il);
  6082. }
  6083. if (il == n_layer - 1 && inp_out_ids) {
  6084. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  6085. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  6086. }
  6087. // re-add the layer input
  6088. cur = ggml_add(ctx0, cur, inpL);
  6089. // attention layer norm
  6090. cur = build_norm(cur, model.layers[il].attn_out_norm, model.layers[il].attn_out_norm_b, LLM_NORM, il);
  6091. if (model.layers[il].attn_norm_2 != nullptr) {
  6092. cur = ggml_add(ctx0, cur, inpL); // re-add the layer input
  6093. cur = build_norm(cur, model.layers[il].attn_norm_2, model.layers[il].attn_norm_2_b, LLM_NORM, il);
  6094. }
  6095. ggml_tensor * ffn_inp = cur;
  6096. cb(ffn_inp, "ffn_inp", il);
  6097. // feed-forward network
  6098. if (hparams.moe_every_n_layers > 0 && il % hparams.moe_every_n_layers == 1) {
  6099. // MoE branch
  6100. cur = build_moe_ffn(cur,
  6101. model.layers[il].ffn_gate_inp,
  6102. model.layers[il].ffn_up_exps,
  6103. nullptr,
  6104. model.layers[il].ffn_down_exps,
  6105. nullptr,
  6106. hparams.n_expert,
  6107. hparams.n_expert_used,
  6108. LLM_FFN_GELU,
  6109. false, false,
  6110. 0.0f,
  6111. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX, il);
  6112. cb(cur, "ffn_moe_out", il);
  6113. } else if (model.arch == LLM_ARCH_BERT || model.arch == LLM_ARCH_NOMIC_BERT_MOE) {
  6114. cur = build_ffn(cur,
  6115. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  6116. NULL, NULL, NULL,
  6117. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  6118. NULL,
  6119. LLM_FFN_GELU, LLM_FFN_SEQ, il);
  6120. cb(cur, "ffn_out", il);
  6121. } else if (model.arch == LLM_ARCH_JINA_BERT_V2) {
  6122. cur = build_ffn(cur,
  6123. model.layers[il].ffn_up, NULL, NULL,
  6124. model.layers[il].ffn_gate, NULL, NULL,
  6125. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  6126. NULL,
  6127. model.layers[il].ffn_gate ? LLM_FFN_GELU : LLM_FFN_GEGLU, LLM_FFN_PAR, il);
  6128. cb(cur, "ffn_out", il);
  6129. } else {
  6130. cur = build_ffn(cur,
  6131. model.layers[il].ffn_up, NULL, NULL,
  6132. model.layers[il].ffn_gate, NULL, NULL,
  6133. model.layers[il].ffn_down, NULL, NULL,
  6134. NULL,
  6135. LLM_FFN_SILU, LLM_FFN_PAR, il);
  6136. cb(cur, "ffn_out", il);
  6137. }
  6138. // attentions bypass the intermediate layer
  6139. cur = ggml_add(ctx0, cur, ffn_inp);
  6140. // output layer norm
  6141. cur = build_norm(cur, model.layers[il].layer_out_norm, model.layers[il].layer_out_norm_b, LLM_NORM, il);
  6142. // input for next layer
  6143. inpL = cur;
  6144. }
  6145. cur = inpL;
  6146. cb(cur, "result_embd", -1);
  6147. res->t_embd = cur;
  6148. ggml_build_forward_expand(gf, cur);
  6149. }
  6150. };
  6151. struct llm_build_neo_bert : public llm_graph_context {
  6152. llm_build_neo_bert(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  6153. const int64_t n_embd_head = hparams.n_embd_head_v;
  6154. const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  6155. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  6156. ggml_tensor * cur;
  6157. ggml_tensor * inpL;
  6158. ggml_tensor * inp_pos = build_inp_pos();
  6159. // construct input embeddings (token, type, position)
  6160. inpL = build_inp_embd(model.tok_embd);
  6161. cb(inpL, "inp_embd", -1);
  6162. auto * inp_attn = build_attn_inp_no_cache();
  6163. ggml_tensor * inp_out_ids = build_inp_out_ids();
  6164. for (int il = 0; il < n_layer; ++il) {
  6165. ggml_tensor * cur = inpL;
  6166. // pre-norm
  6167. cur = build_norm(inpL,
  6168. model.layers[il].attn_norm, NULL,
  6169. LLM_NORM_RMS, il);
  6170. {
  6171. ggml_tensor * Qcur;
  6172. ggml_tensor * Kcur;
  6173. ggml_tensor * Vcur;
  6174. // self-attention
  6175. cur = build_lora_mm(model.layers[il].wqkv, cur);
  6176. cb(cur, "wqkv", il);
  6177. Qcur = ggml_view_3d(ctx0, cur, n_embd_head, n_head, n_tokens, n_embd_head*sizeof(float), cur->nb[1], 0*sizeof(float)*(n_embd));
  6178. Kcur = ggml_view_3d(ctx0, cur, n_embd_head, n_head_kv, n_tokens, n_embd_head*sizeof(float), cur->nb[1], 1*sizeof(float)*(n_embd));
  6179. Vcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd + n_embd_gqa)));
  6180. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  6181. // RoPE
  6182. Qcur = ggml_rope_ext(
  6183. ctx0, Qcur, inp_pos, nullptr,
  6184. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  6185. ext_factor, attn_factor, beta_fast, beta_slow
  6186. );
  6187. Kcur = ggml_rope_ext(
  6188. ctx0, Kcur, inp_pos, nullptr,
  6189. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  6190. ext_factor, attn_factor, beta_fast, beta_slow
  6191. );
  6192. cb(Qcur, "Qcur", il);
  6193. cb(Kcur, "Kcur", il);
  6194. cb(Vcur, "Vcur", il);
  6195. cur = build_attn(inp_attn,
  6196. model.layers[il].wo, nullptr,
  6197. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  6198. cb(cur, "kqv_out", il);
  6199. }
  6200. if (il == n_layer - 1 && inp_out_ids) {
  6201. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  6202. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  6203. }
  6204. // re-add the layer input
  6205. cur = ggml_add(ctx0, cur, inpL);
  6206. ggml_tensor * ffn_inp = cur;
  6207. cb(ffn_inp, "ffn_inp", il);
  6208. // pre-norm
  6209. cur = build_norm(ffn_inp,
  6210. model.layers[il].ffn_norm, NULL,
  6211. LLM_NORM_RMS, il);
  6212. cb(cur, "ffn_norm", il);
  6213. // feed-forward network
  6214. cur = build_ffn(cur,
  6215. model.layers[il].ffn_up,
  6216. NULL, NULL, NULL, NULL, NULL,
  6217. model.layers[il].ffn_down,
  6218. NULL, NULL, NULL,
  6219. LLM_FFN_SWIGLU, LLM_FFN_SEQ, il);
  6220. // attentions bypass the intermediate layer
  6221. cur = ggml_add(ctx0, cur, ffn_inp);
  6222. // input for next layer
  6223. inpL = cur;
  6224. }
  6225. cur = inpL;
  6226. cur = build_norm(cur,
  6227. model.output_norm_enc, NULL,
  6228. LLM_NORM_RMS, -1);
  6229. cb(cur, "result_embd", -1);
  6230. res->t_embd = cur;
  6231. ggml_build_forward_expand(gf, cur);
  6232. }
  6233. };
  6234. struct llm_build_bloom : public llm_graph_context {
  6235. llm_build_bloom(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  6236. const int64_t n_embd_head = hparams.n_embd_head_v;
  6237. const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  6238. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  6239. ggml_tensor * cur;
  6240. ggml_tensor * inpL;
  6241. inpL = build_inp_embd(model.tok_embd);
  6242. auto * inp_attn = build_attn_inp_kv_unified();
  6243. inpL = build_norm(inpL,
  6244. model.tok_norm,
  6245. model.tok_norm_b,
  6246. LLM_NORM, -1);
  6247. cb(inpL, "inp_norm", -1);
  6248. ggml_tensor * inp_out_ids = build_inp_out_ids();
  6249. for (int il = 0; il < n_layer; ++il) {
  6250. cur = build_norm(inpL,
  6251. model.layers[il].attn_norm,
  6252. model.layers[il].attn_norm_b,
  6253. LLM_NORM, il);
  6254. cb(cur, "attn_norm", il);
  6255. // self-attention
  6256. {
  6257. cur = build_lora_mm(model.layers[il].wqkv, cur);
  6258. cb(cur, "wqkv", il);
  6259. cur = ggml_add(ctx0, cur, model.layers[il].bqkv);
  6260. cb(cur, "bqkv", il);
  6261. ggml_tensor * Qcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd, n_tokens, cur->nb[1], 0*sizeof(float)*(n_embd)));
  6262. ggml_tensor * Kcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd)));
  6263. ggml_tensor * Vcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd + n_embd_gqa)));
  6264. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  6265. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  6266. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  6267. cb(Qcur, "Qcur", il);
  6268. cb(Kcur, "Kcur", il);
  6269. cb(Vcur, "Vcur", il);
  6270. cur = build_attn(inp_attn,
  6271. model.layers[il].wo, model.layers[il].bo,
  6272. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  6273. }
  6274. if (il == n_layer - 1 && inp_out_ids) {
  6275. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  6276. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  6277. }
  6278. // Add the input
  6279. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
  6280. cb(ffn_inp, "ffn_inp", il);
  6281. // FF
  6282. {
  6283. cur = build_norm(ffn_inp,
  6284. model.layers[il].ffn_norm,
  6285. model.layers[il].ffn_norm_b,
  6286. LLM_NORM, il);
  6287. cb(cur, "ffn_norm", il);
  6288. cur = build_ffn(cur,
  6289. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  6290. NULL, NULL, NULL,
  6291. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  6292. NULL,
  6293. LLM_FFN_GELU, LLM_FFN_SEQ, il);
  6294. cb(cur, "ffn_out", il);
  6295. }
  6296. cur = ggml_add(ctx0, cur, ffn_inp);
  6297. cur = build_cvec(cur, il);
  6298. cb(cur, "l_out", il);
  6299. // input for next layer
  6300. inpL = cur;
  6301. }
  6302. cur = build_norm(inpL,
  6303. model.output_norm,
  6304. model.output_norm_b,
  6305. LLM_NORM, -1);
  6306. cb(cur, "result_norm", -1);
  6307. res->t_embd = cur;
  6308. cur = build_lora_mm(model.output, cur);
  6309. cb(cur, "result_output", -1);
  6310. res->t_logits = cur;
  6311. ggml_build_forward_expand(gf, cur);
  6312. }
  6313. };
  6314. struct llm_build_mpt : public llm_graph_context {
  6315. llm_build_mpt(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  6316. const int64_t n_embd_head = hparams.n_embd_head_v;
  6317. const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  6318. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  6319. ggml_tensor * cur;
  6320. ggml_tensor * pos;
  6321. ggml_tensor * inpL;
  6322. inpL = build_inp_embd(model.tok_embd);
  6323. auto * inp_attn = build_attn_inp_kv_unified();
  6324. if (model.pos_embd) {
  6325. // inp_pos - contains the positions
  6326. ggml_tensor * inp_pos = build_inp_pos();
  6327. pos = ggml_get_rows(ctx0, model.pos_embd, inp_pos);
  6328. cb(pos, "pos_embd", -1);
  6329. inpL = ggml_add(ctx0, inpL, pos);
  6330. cb(inpL, "inpL", -1);
  6331. }
  6332. ggml_tensor * inp_out_ids = build_inp_out_ids();
  6333. for (int il = 0; il < n_layer; ++il) {
  6334. ggml_tensor * attn_norm;
  6335. attn_norm = build_norm(inpL,
  6336. model.layers[il].attn_norm,
  6337. model.layers[il].attn_norm_b,
  6338. LLM_NORM, il);
  6339. cb(attn_norm, "attn_norm", il);
  6340. // self-attention
  6341. {
  6342. cur = attn_norm;
  6343. cur = build_lora_mm(model.layers[il].wqkv, cur);
  6344. cb(cur, "wqkv", il);
  6345. if (model.layers[il].bqkv){
  6346. cur = ggml_add(ctx0, cur, model.layers[il].bqkv);
  6347. cb(cur, "bqkv", il);
  6348. }
  6349. if (hparams.f_clamp_kqv > 0.0f) {
  6350. cur = ggml_clamp(ctx0, cur, -hparams.f_clamp_kqv, hparams.f_clamp_kqv);
  6351. cb(cur, "wqkv_clamped", il);
  6352. }
  6353. ggml_tensor * Qcur = ggml_view_2d(ctx0, cur, n_embd, n_tokens, cur->nb[1], 0*sizeof(float)*(n_embd));
  6354. ggml_tensor * Kcur = ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd));
  6355. ggml_tensor * Vcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd + n_embd_gqa)));
  6356. cb(Qcur, "Qcur", il);
  6357. cb(Kcur, "Kcur", il);
  6358. cb(Vcur, "Vcur", il);
  6359. // Q/K Layernorm
  6360. if (model.layers[il].attn_q_norm) {
  6361. Qcur = build_norm(Qcur,
  6362. model.layers[il].attn_q_norm,
  6363. model.layers[il].attn_q_norm_b,
  6364. LLM_NORM, il);
  6365. cb(Qcur, "Qcur", il);
  6366. Kcur = build_norm(Kcur,
  6367. model.layers[il].attn_k_norm,
  6368. model.layers[il].attn_k_norm_b,
  6369. LLM_NORM, il);
  6370. cb(Kcur, "Kcur", il);
  6371. } else {
  6372. Qcur = ggml_cont(ctx0, Qcur);
  6373. cb(Qcur, "Qcur", il);
  6374. Kcur = ggml_cont(ctx0, Kcur);
  6375. cb(Kcur, "Kcur", il);
  6376. }
  6377. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  6378. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  6379. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  6380. cb(Qcur, "Qcur", il);
  6381. cb(Kcur, "Kcur", il);
  6382. cb(Vcur, "Vcur", il);
  6383. cur = build_attn(inp_attn,
  6384. model.layers[il].wo, model.layers[il].bo,
  6385. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  6386. }
  6387. if (il == n_layer - 1 && inp_out_ids) {
  6388. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  6389. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  6390. }
  6391. // Add the input
  6392. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
  6393. cb(ffn_inp, "ffn_inp", il);
  6394. // feed forward
  6395. {
  6396. cur = build_norm(ffn_inp,
  6397. model.layers[il].ffn_norm,
  6398. model.layers[il].ffn_norm_b,
  6399. LLM_NORM, il);
  6400. cb(cur, "ffn_norm", il);
  6401. cur = build_ffn(cur,
  6402. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  6403. NULL, NULL, NULL,
  6404. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  6405. model.layers[il].ffn_act,
  6406. LLM_FFN_GELU, LLM_FFN_SEQ, il);
  6407. cb(cur, "ffn_out", il);
  6408. }
  6409. cur = ggml_add(ctx0, cur, ffn_inp);
  6410. cur = build_cvec(cur, il);
  6411. cb(cur, "l_out", il);
  6412. // input for next layer
  6413. inpL = cur;
  6414. }
  6415. cur = inpL;
  6416. cur = build_norm(cur,
  6417. model.output_norm,
  6418. model.output_norm_b,
  6419. LLM_NORM, -1);
  6420. cb(cur, "result_norm", -1);
  6421. res->t_embd = cur;
  6422. cur = build_lora_mm(model.output, cur);
  6423. cb(cur, "result_output", -1);
  6424. res->t_logits = cur;
  6425. ggml_build_forward_expand(gf, cur);
  6426. }
  6427. };
  6428. struct llm_build_stablelm : public llm_graph_context {
  6429. llm_build_stablelm(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  6430. const int64_t n_embd_head = hparams.n_embd_head_v;
  6431. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  6432. ggml_tensor * cur;
  6433. ggml_tensor * inpL;
  6434. inpL = build_inp_embd(model.tok_embd);
  6435. // inp_pos - contains the positions
  6436. ggml_tensor * inp_pos = build_inp_pos();
  6437. auto * inp_attn = build_attn_inp_kv_unified();
  6438. ggml_tensor * inp_out_ids = build_inp_out_ids();
  6439. for (int il = 0; il < n_layer; ++il) {
  6440. // norm
  6441. cur = build_norm(inpL,
  6442. model.layers[il].attn_norm,
  6443. model.layers[il].attn_norm_b,
  6444. LLM_NORM, il);
  6445. cb(cur, "attn_norm", il);
  6446. ggml_tensor * inpSA = cur;
  6447. // self-attention
  6448. {
  6449. // compute Q and K and RoPE them
  6450. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  6451. cb(Qcur, "Qcur", il);
  6452. if (model.layers[il].bq) {
  6453. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  6454. cb(Qcur, "Qcur", il);
  6455. }
  6456. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  6457. cb(Kcur, "Kcur", il);
  6458. if (model.layers[il].bk) {
  6459. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  6460. cb(Kcur, "Kcur", il);
  6461. }
  6462. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  6463. cb(Vcur, "Vcur", il);
  6464. if (model.layers[il].bv) {
  6465. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  6466. cb(Vcur, "Vcur", il);
  6467. }
  6468. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  6469. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  6470. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  6471. if (model.layers[il].attn_q_norm) {
  6472. Qcur = build_norm(Qcur,
  6473. model.layers[il].attn_q_norm,
  6474. NULL,
  6475. LLM_NORM, il);
  6476. cb(Qcur, "Qcur", il);
  6477. }
  6478. if (model.layers[il].attn_k_norm) {
  6479. Kcur = build_norm(Kcur,
  6480. model.layers[il].attn_k_norm,
  6481. NULL,
  6482. LLM_NORM, il);
  6483. cb(Kcur, "Kcur", il);
  6484. }
  6485. Qcur = ggml_rope_ext(
  6486. ctx0, Qcur, inp_pos, nullptr,
  6487. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  6488. ext_factor, attn_factor, beta_fast, beta_slow
  6489. );
  6490. Kcur = ggml_rope_ext(
  6491. ctx0, Kcur, inp_pos, nullptr,
  6492. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  6493. ext_factor, attn_factor, beta_fast, beta_slow
  6494. );
  6495. cb(Qcur, "Qcur", il);
  6496. cb(Kcur, "Kcur", il);
  6497. cb(Vcur, "Vcur", il);
  6498. cur = build_attn(inp_attn,
  6499. model.layers[il].wo, NULL,
  6500. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  6501. }
  6502. if (il == n_layer - 1 && inp_out_ids) {
  6503. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  6504. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  6505. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  6506. }
  6507. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
  6508. cb(ffn_inp, "ffn_inp", il);
  6509. // feed-forward network
  6510. {
  6511. if (model.layers[il].ffn_norm) {
  6512. cur = build_norm(ffn_inp,
  6513. model.layers[il].ffn_norm,
  6514. model.layers[il].ffn_norm_b,
  6515. LLM_NORM, il);
  6516. cb(cur, "ffn_norm", il);
  6517. } else {
  6518. // parallel residual
  6519. cur = inpSA;
  6520. }
  6521. cur = build_ffn(cur,
  6522. model.layers[il].ffn_up, NULL, NULL,
  6523. model.layers[il].ffn_gate, NULL, NULL,
  6524. model.layers[il].ffn_down, NULL, NULL,
  6525. NULL,
  6526. LLM_FFN_SILU, LLM_FFN_PAR, il);
  6527. cb(cur, "ffn_out", il);
  6528. }
  6529. cur = ggml_add(ctx0, cur, ffn_inp);
  6530. cur = build_cvec(cur, il);
  6531. cb(cur, "l_out", il);
  6532. // input for next layer
  6533. inpL = cur;
  6534. }
  6535. cur = inpL;
  6536. cur = build_norm(cur,
  6537. model.output_norm,
  6538. model.output_norm_b,
  6539. LLM_NORM, -1);
  6540. cb(cur, "result_norm", -1);
  6541. res->t_embd = cur;
  6542. // lm_head
  6543. cur = build_lora_mm(model.output, cur);
  6544. cb(cur, "result_output", -1);
  6545. res->t_logits = cur;
  6546. ggml_build_forward_expand(gf, cur);
  6547. }
  6548. };
  6549. struct llm_build_qwen : public llm_graph_context {
  6550. llm_build_qwen(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  6551. const int64_t n_embd_head = hparams.n_embd_head_v;
  6552. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  6553. ggml_tensor * cur;
  6554. ggml_tensor * inpL;
  6555. inpL = build_inp_embd(model.tok_embd);
  6556. // inp_pos - contains the positions
  6557. ggml_tensor * inp_pos = build_inp_pos();
  6558. auto * inp_attn = build_attn_inp_kv_unified();
  6559. ggml_tensor * inp_out_ids = build_inp_out_ids();
  6560. for (int il = 0; il < n_layer; ++il) {
  6561. ggml_tensor * inpSA = inpL;
  6562. cur = build_norm(inpL,
  6563. model.layers[il].attn_norm, NULL,
  6564. LLM_NORM_RMS, il);
  6565. cb(cur, "attn_norm", il);
  6566. // self-attention
  6567. {
  6568. cur = build_lora_mm(model.layers[il].wqkv, cur);
  6569. cb(cur, "wqkv", il);
  6570. cur = ggml_add(ctx0, cur, model.layers[il].bqkv);
  6571. cb(cur, "bqkv", il);
  6572. ggml_tensor * Qcur = ggml_view_3d(ctx0, cur, n_embd_head, n_head, n_tokens, n_embd_head*sizeof(float), cur->nb[1], 0*sizeof(float)*(n_embd));
  6573. ggml_tensor * Kcur = ggml_view_3d(ctx0, cur, n_embd_head, n_head_kv, n_tokens, n_embd_head*sizeof(float), cur->nb[1], 1*sizeof(float)*(n_embd));
  6574. ggml_tensor * Vcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd, n_tokens, cur->nb[1], 2*sizeof(float)*(n_embd)));
  6575. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  6576. // using mode = 2 for neox mode
  6577. Qcur = ggml_rope_ext(
  6578. ctx0, Qcur, inp_pos, nullptr,
  6579. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  6580. ext_factor, attn_factor, beta_fast, beta_slow
  6581. );
  6582. Kcur = ggml_rope_ext(
  6583. ctx0, Kcur, inp_pos, nullptr,
  6584. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  6585. ext_factor, attn_factor, beta_fast, beta_slow
  6586. );
  6587. cb(Qcur, "Qcur", il);
  6588. cb(Kcur, "Kcur", il);
  6589. cb(Vcur, "Vcur", il);
  6590. cur = build_attn(inp_attn,
  6591. model.layers[il].wo, NULL,
  6592. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  6593. }
  6594. if (il == n_layer - 1 && inp_out_ids) {
  6595. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  6596. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  6597. }
  6598. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  6599. cb(ffn_inp, "ffn_inp", il);
  6600. // feed-forward forward
  6601. {
  6602. cur = build_norm(ffn_inp,
  6603. model.layers[il].ffn_norm, NULL,
  6604. LLM_NORM_RMS, il);
  6605. cb(cur, "ffn_norm", il);
  6606. cur = build_ffn(cur,
  6607. model.layers[il].ffn_up, NULL, NULL,
  6608. model.layers[il].ffn_gate, NULL, NULL,
  6609. model.layers[il].ffn_down, NULL, NULL,
  6610. NULL,
  6611. LLM_FFN_SILU, LLM_FFN_PAR, il);
  6612. cb(cur, "ffn_out", il);
  6613. }
  6614. cur = ggml_add(ctx0, cur, ffn_inp);
  6615. cur = build_cvec(cur, il);
  6616. cb(cur, "l_out", il);
  6617. // input for next layer
  6618. inpL = cur;
  6619. }
  6620. cur = inpL;
  6621. cur = build_norm(cur,
  6622. model.output_norm, NULL,
  6623. LLM_NORM_RMS, -1);
  6624. cb(cur, "result_norm", -1);
  6625. res->t_embd = cur;
  6626. // lm_head
  6627. cur = build_lora_mm(model.output, cur);
  6628. cb(cur, "result_output", -1);
  6629. res->t_logits = cur;
  6630. ggml_build_forward_expand(gf, cur);
  6631. }
  6632. };
  6633. struct llm_build_qwen2 : public llm_graph_context {
  6634. llm_build_qwen2(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  6635. const int64_t n_embd_head = hparams.n_embd_head_v;
  6636. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  6637. GGML_ASSERT(n_embd_head == hparams.n_rot);
  6638. ggml_tensor * cur;
  6639. ggml_tensor * inpL;
  6640. inpL = build_inp_embd(model.tok_embd);
  6641. // inp_pos - contains the positions
  6642. ggml_tensor * inp_pos = build_inp_pos();
  6643. auto * inp_attn = build_attn_inp_kv_unified();
  6644. ggml_tensor * inp_out_ids = build_inp_out_ids();
  6645. for (int il = 0; il < n_layer; ++il) {
  6646. ggml_tensor * inpSA = inpL;
  6647. // norm
  6648. cur = build_norm(inpL,
  6649. model.layers[il].attn_norm, NULL,
  6650. LLM_NORM_RMS, il);
  6651. cb(cur, "attn_norm", il);
  6652. // self-attention
  6653. {
  6654. // compute Q and K and RoPE them
  6655. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  6656. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  6657. cb(Qcur, "Qcur", il);
  6658. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  6659. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  6660. cb(Kcur, "Kcur", il);
  6661. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  6662. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  6663. cb(Vcur, "Vcur", il);
  6664. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  6665. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  6666. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  6667. Qcur = ggml_rope_ext(
  6668. ctx0, Qcur, inp_pos, nullptr,
  6669. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  6670. ext_factor, attn_factor, beta_fast, beta_slow
  6671. );
  6672. Kcur = ggml_rope_ext(
  6673. ctx0, Kcur, inp_pos, nullptr,
  6674. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  6675. ext_factor, attn_factor, beta_fast, beta_slow
  6676. );
  6677. cb(Qcur, "Qcur", il);
  6678. cb(Kcur, "Kcur", il);
  6679. cb(Vcur, "Vcur", il);
  6680. cur = build_attn(inp_attn,
  6681. model.layers[il].wo, model.layers[il].bo,
  6682. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  6683. }
  6684. if (il == n_layer - 1 && inp_out_ids) {
  6685. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  6686. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  6687. }
  6688. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  6689. cb(ffn_inp, "ffn_inp", il);
  6690. // feed-forward network
  6691. cur = build_norm(ffn_inp,
  6692. model.layers[il].ffn_norm, NULL,
  6693. LLM_NORM_RMS, il);
  6694. cb(cur, "ffn_norm", il);
  6695. cur = build_ffn(cur,
  6696. model.layers[il].ffn_up, NULL, NULL,
  6697. model.layers[il].ffn_gate, NULL, NULL,
  6698. model.layers[il].ffn_down, NULL, NULL,
  6699. NULL,
  6700. LLM_FFN_SILU, LLM_FFN_PAR, il);
  6701. cb(cur, "ffn_out", il);
  6702. cur = ggml_add(ctx0, cur, ffn_inp);
  6703. cur = build_cvec(cur, il);
  6704. cb(cur, "l_out", il);
  6705. // input for next layer
  6706. inpL = cur;
  6707. }
  6708. cur = inpL;
  6709. cur = build_norm(cur,
  6710. model.output_norm, NULL,
  6711. LLM_NORM_RMS, -1);
  6712. cb(cur, "result_norm", -1);
  6713. res->t_embd = cur;
  6714. // lm_head
  6715. cur = build_lora_mm(model.output, cur);
  6716. if (model.output_b != nullptr) {
  6717. cur = ggml_add(ctx0, cur, model.output_b);
  6718. }
  6719. cb(cur, "result_output", -1);
  6720. res->t_logits = cur;
  6721. ggml_build_forward_expand(gf, cur);
  6722. }
  6723. };
  6724. struct llm_build_dream : public llm_graph_context {
  6725. llm_build_dream(const llama_model & model, const llm_graph_params & params) :
  6726. llm_graph_context(params) {
  6727. //copied from qwen2
  6728. const int64_t n_embd_head = hparams.n_embd_head_v;
  6729. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  6730. GGML_ASSERT(n_embd_head == hparams.n_rot);
  6731. ggml_tensor * cur;
  6732. ggml_tensor * inpL;
  6733. inpL = build_inp_embd(model.tok_embd);
  6734. // inp_pos - contains the positions
  6735. ggml_tensor * inp_pos = build_inp_pos();
  6736. auto * inp_attn = build_attn_inp_no_cache();
  6737. ggml_tensor * inp_out_ids = build_inp_out_ids();
  6738. for (int il = 0; il < n_layer; ++il) {
  6739. ggml_tensor * inpSA = inpL;
  6740. // norm
  6741. cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
  6742. cb(cur, "attn_norm", il);
  6743. // self-attention
  6744. {
  6745. // compute Q and K and RoPE them
  6746. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  6747. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  6748. cb(Qcur, "Qcur", il);
  6749. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  6750. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  6751. cb(Kcur, "Kcur", il);
  6752. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  6753. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  6754. cb(Vcur, "Vcur", il);
  6755. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  6756. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  6757. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  6758. Qcur = ggml_rope_ext(ctx0, Qcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  6759. ext_factor, attn_factor, beta_fast, beta_slow);
  6760. Kcur = ggml_rope_ext(ctx0, Kcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  6761. ext_factor, attn_factor, beta_fast, beta_slow);
  6762. cb(Qcur, "Qcur", il);
  6763. cb(Kcur, "Kcur", il);
  6764. cb(Vcur, "Vcur", il);
  6765. cur = build_attn(inp_attn, model.layers[il].wo, model.layers[il].bo, Qcur, Kcur, Vcur, nullptr,
  6766. nullptr, 1.0f / sqrtf(float(n_embd_head)), il);
  6767. }
  6768. if (il == n_layer - 1 && inp_out_ids) {
  6769. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  6770. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  6771. }
  6772. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  6773. cb(ffn_inp, "ffn_inp", il);
  6774. // feed-forward network
  6775. cur = build_norm(ffn_inp, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);
  6776. cb(cur, "ffn_norm", il);
  6777. cur = build_ffn(cur, model.layers[il].ffn_up, NULL, NULL, model.layers[il].ffn_gate, NULL, NULL,
  6778. model.layers[il].ffn_down, NULL, NULL, NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
  6779. cb(cur, "ffn_out", il);
  6780. cur = ggml_add(ctx0, cur, ffn_inp);
  6781. cur = build_cvec(cur, il);
  6782. cb(cur, "l_out", il);
  6783. // input for next layer
  6784. inpL = cur;
  6785. }
  6786. cur = inpL;
  6787. cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1);
  6788. cb(cur, "result_norm", -1);
  6789. res->t_embd = cur;
  6790. // lm_head
  6791. cur = build_lora_mm(model.output, cur);
  6792. cb(cur, "result_output", -1);
  6793. res->t_logits = cur;
  6794. ggml_build_forward_expand(gf, cur);
  6795. }
  6796. };
  6797. struct llm_build_llada : public llm_graph_context {
  6798. llm_build_llada(const llama_model & model, const llm_graph_params & params) :
  6799. llm_graph_context(params) {
  6800. // LLaDA is similar to LLaMA but uses non-causal attention for diffusion
  6801. const int64_t n_embd_head = hparams.n_embd_head_v;
  6802. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  6803. GGML_ASSERT(n_embd_head == hparams.n_rot);
  6804. ggml_tensor * cur;
  6805. ggml_tensor * inpL;
  6806. inpL = build_inp_embd(model.tok_embd);
  6807. // inp_pos - contains the positions
  6808. ggml_tensor * inp_pos = build_inp_pos();
  6809. // Non-causal attention for diffusion
  6810. auto * inp_attn = build_attn_inp_no_cache();
  6811. ggml_tensor * inp_out_ids = build_inp_out_ids();
  6812. for (int il = 0; il < n_layer; ++il) {
  6813. ggml_tensor * inpSA = inpL;
  6814. // norm
  6815. cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
  6816. cb(cur, "attn_norm", il);
  6817. // self-attention
  6818. {
  6819. // compute separate Q, K, V projections without bias, matching LLaDALlamaBlock
  6820. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  6821. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  6822. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  6823. cb(Qcur, "Qcur", il);
  6824. cb(Kcur, "Kcur", il);
  6825. cb(Vcur, "Vcur", il);
  6826. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  6827. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  6828. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  6829. Qcur = ggml_rope_ext(ctx0, Qcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  6830. ext_factor, attn_factor, beta_fast, beta_slow);
  6831. Kcur = ggml_rope_ext(ctx0, Kcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  6832. ext_factor, attn_factor, beta_fast, beta_slow);
  6833. cb(Qcur, "Qcur", il);
  6834. cb(Kcur, "Kcur", il);
  6835. cb(Vcur, "Vcur", il);
  6836. cur = build_attn(inp_attn, model.layers[il].wo, NULL, Qcur, Kcur, Vcur, nullptr, nullptr,
  6837. 1.0f / sqrtf(float(n_embd_head)), il);
  6838. }
  6839. if (il == n_layer - 1 && inp_out_ids) {
  6840. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  6841. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  6842. }
  6843. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  6844. cb(ffn_inp, "ffn_inp", il);
  6845. // feed-forward network
  6846. cur = build_norm(ffn_inp, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);
  6847. cb(cur, "ffn_norm", il);
  6848. cur = build_ffn(cur, model.layers[il].ffn_up, NULL, NULL, model.layers[il].ffn_gate, NULL, NULL,
  6849. model.layers[il].ffn_down, NULL, NULL, NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
  6850. cb(cur, "ffn_out", il);
  6851. cur = ggml_add(ctx0, cur, ffn_inp);
  6852. cur = build_cvec(cur, il);
  6853. cb(cur, "l_out", il);
  6854. // input for next layer
  6855. inpL = cur;
  6856. }
  6857. cur = inpL;
  6858. cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1);
  6859. cb(cur, "result_norm", -1);
  6860. res->t_embd = cur;
  6861. // lm_head
  6862. cur = build_lora_mm(model.output, cur);
  6863. cb(cur, "result_output", -1);
  6864. res->t_logits = cur;
  6865. ggml_build_forward_expand(gf, cur);
  6866. }
  6867. };
  6868. struct llm_build_qwen2vl : public llm_graph_context {
  6869. llm_build_qwen2vl(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  6870. const int64_t n_embd_head = hparams.n_embd_head_v;
  6871. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  6872. GGML_ASSERT(n_embd_head == hparams.n_rot);
  6873. ggml_tensor * cur;
  6874. ggml_tensor * inpL;
  6875. inpL = build_inp_embd(model.tok_embd);
  6876. // inp_pos - contains the positions
  6877. ggml_tensor * inp_pos = build_inp_pos();
  6878. auto * inp_attn = build_attn_inp_kv_unified();
  6879. int sections[4];
  6880. std::copy(std::begin(hparams.rope_sections), std::begin(hparams.rope_sections) + 4, sections);
  6881. ggml_tensor * inp_out_ids = build_inp_out_ids();
  6882. for (int il = 0; il < n_layer; ++il) {
  6883. ggml_tensor * inpSA = inpL;
  6884. // norm
  6885. cur = build_norm(inpL,
  6886. model.layers[il].attn_norm, NULL,
  6887. LLM_NORM_RMS, il);
  6888. cb(cur, "attn_norm", il);
  6889. // self-attention
  6890. {
  6891. // compute Q and K and RoPE them
  6892. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  6893. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  6894. cb(Qcur, "Qcur", il);
  6895. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  6896. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  6897. cb(Kcur, "Kcur", il);
  6898. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  6899. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  6900. cb(Vcur, "Vcur", il);
  6901. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  6902. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  6903. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  6904. Qcur = ggml_rope_multi(
  6905. ctx0, Qcur, inp_pos, nullptr,
  6906. n_rot, sections, rope_type, n_ctx_orig, freq_base, freq_scale,
  6907. ext_factor, attn_factor, beta_fast, beta_slow
  6908. );
  6909. Kcur = ggml_rope_multi(
  6910. ctx0, Kcur, inp_pos, nullptr,
  6911. n_rot, sections, rope_type, n_ctx_orig, freq_base, freq_scale,
  6912. ext_factor, attn_factor, beta_fast, beta_slow
  6913. );
  6914. cb(Qcur, "Qcur", il);
  6915. cb(Kcur, "Kcur", il);
  6916. cb(Vcur, "Vcur", il);
  6917. cur = build_attn(inp_attn,
  6918. model.layers[il].wo, model.layers[il].bo,
  6919. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  6920. }
  6921. if (il == n_layer - 1 && inp_out_ids) {
  6922. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  6923. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  6924. }
  6925. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  6926. cb(ffn_inp, "ffn_inp", il);
  6927. // feed-forward network
  6928. cur = build_norm(ffn_inp,
  6929. model.layers[il].ffn_norm, NULL,
  6930. LLM_NORM_RMS, il);
  6931. cb(cur, "ffn_norm", il);
  6932. cur = build_ffn(cur,
  6933. model.layers[il].ffn_up, NULL, NULL,
  6934. model.layers[il].ffn_gate, NULL, NULL,
  6935. model.layers[il].ffn_down, NULL, NULL,
  6936. NULL,
  6937. LLM_FFN_SILU, LLM_FFN_PAR, il);
  6938. cb(cur, "ffn_out", il);
  6939. cur = ggml_add(ctx0, cur, ffn_inp);
  6940. cur = build_cvec(cur, il);
  6941. cb(cur, "l_out", il);
  6942. // input for next layer
  6943. inpL = cur;
  6944. }
  6945. cur = inpL;
  6946. cur = build_norm(cur,
  6947. model.output_norm, NULL,
  6948. LLM_NORM_RMS, -1);
  6949. cb(cur, "result_norm", -1);
  6950. res->t_embd = cur;
  6951. // lm_head
  6952. cur = build_lora_mm(model.output, cur);
  6953. cb(cur, "result_output", -1);
  6954. res->t_logits = cur;
  6955. ggml_build_forward_expand(gf, cur);
  6956. }
  6957. };
  6958. struct llm_build_qwen2moe : public llm_graph_context {
  6959. llm_build_qwen2moe(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  6960. const int64_t n_embd_head = hparams.n_embd_head_v;
  6961. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  6962. GGML_ASSERT(n_embd_head == hparams.n_rot);
  6963. ggml_tensor * cur;
  6964. ggml_tensor * inpL;
  6965. inpL = build_inp_embd(model.tok_embd);
  6966. // inp_pos - contains the positions
  6967. ggml_tensor * inp_pos = build_inp_pos();
  6968. auto * inp_attn = build_attn_inp_kv_unified();
  6969. ggml_tensor * inp_out_ids = build_inp_out_ids();
  6970. for (int il = 0; il < n_layer; ++il) {
  6971. ggml_tensor * inpSA = inpL;
  6972. // norm
  6973. cur = build_norm(inpL,
  6974. model.layers[il].attn_norm, NULL,
  6975. LLM_NORM_RMS, il);
  6976. cb(cur, "attn_norm", il);
  6977. // self_attention
  6978. {
  6979. // compute Q and K and RoPE them
  6980. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  6981. cb(Qcur, "Qcur", il);
  6982. if (model.layers[il].bq) {
  6983. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  6984. cb(Qcur, "Qcur", il);
  6985. }
  6986. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  6987. cb(Kcur, "Kcur", il);
  6988. if (model.layers[il].bk) {
  6989. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  6990. cb(Kcur, "Kcur", il);
  6991. }
  6992. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  6993. cb(Vcur, "Vcur", il);
  6994. if (model.layers[il].bv) {
  6995. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  6996. cb(Vcur, "Vcur", il);
  6997. }
  6998. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  6999. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  7000. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  7001. Qcur = ggml_rope_ext(
  7002. ctx0, Qcur, inp_pos, nullptr,
  7003. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  7004. ext_factor, attn_factor, beta_fast, beta_slow
  7005. );
  7006. Kcur = ggml_rope_ext(
  7007. ctx0, Kcur, inp_pos, nullptr,
  7008. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  7009. ext_factor, attn_factor, beta_fast, beta_slow
  7010. );
  7011. cb(Qcur, "Qcur", il);
  7012. cb(Kcur, "Kcur", il);
  7013. cb(Vcur, "Vcur", il);
  7014. cur = build_attn(inp_attn,
  7015. model.layers[il].wo, model.layers[il].bo,
  7016. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  7017. }
  7018. if (il == n_layer - 1 && inp_out_ids) {
  7019. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  7020. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  7021. }
  7022. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  7023. cb(ffn_inp, "ffn_inp", il);
  7024. // MoE branch
  7025. cur = build_norm(ffn_inp,
  7026. model.layers[il].ffn_norm, NULL,
  7027. LLM_NORM_RMS, il);
  7028. cb(cur, "ffn_norm", il);
  7029. ggml_tensor * moe_out =
  7030. build_moe_ffn(cur,
  7031. model.layers[il].ffn_gate_inp,
  7032. model.layers[il].ffn_up_exps,
  7033. model.layers[il].ffn_gate_exps,
  7034. model.layers[il].ffn_down_exps,
  7035. nullptr,
  7036. n_expert, n_expert_used,
  7037. LLM_FFN_SILU, false,
  7038. false, 0.0,
  7039. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  7040. il);
  7041. cb(moe_out, "ffn_moe_out", il);
  7042. // FFN shared expert
  7043. {
  7044. ggml_tensor * cur_gate_inp = build_lora_mm(model.layers[il].ffn_gate_inp_shexp, cur);
  7045. cb(cur_gate_inp, "ffn_shexp_gate_inp", il);
  7046. // sigmoid
  7047. ggml_tensor * cur_gate = ggml_div(ctx0, ggml_silu(ctx0, cur_gate_inp), cur_gate_inp);
  7048. cb(cur_gate, "ffn_shexp_gate", il);
  7049. ggml_tensor * cur_ffn = build_ffn(cur,
  7050. model.layers[il].ffn_up_shexp, NULL, NULL,
  7051. model.layers[il].ffn_gate_shexp, NULL, NULL,
  7052. model.layers[il].ffn_down_shexp, NULL, NULL,
  7053. NULL,
  7054. LLM_FFN_SILU, LLM_FFN_PAR, il);
  7055. cb(cur_ffn, "ffn_shexp", il);
  7056. ggml_tensor * ffn_shexp_out = ggml_mul(ctx0, cur_ffn, cur_gate);
  7057. cb(ffn_shexp_out, "ffn_shexp_out", il);
  7058. moe_out = ggml_add(ctx0, moe_out, ffn_shexp_out);
  7059. cb(moe_out, "ffn_out", il);
  7060. cur = moe_out;
  7061. }
  7062. cur = ggml_add(ctx0, cur, ffn_inp);
  7063. cur = build_cvec(cur, il);
  7064. cb(cur, "l_out", il);
  7065. // input for next layer
  7066. inpL = cur;
  7067. }
  7068. cur = inpL;
  7069. cur = build_norm(cur,
  7070. model.output_norm, NULL,
  7071. LLM_NORM_RMS, -1);
  7072. cb(cur, "result_norm", -1);
  7073. res->t_embd = cur;
  7074. // lm_head
  7075. cur = build_lora_mm(model.output, cur);
  7076. cb(cur, "result_output", -1);
  7077. res->t_logits = cur;
  7078. ggml_build_forward_expand(gf, cur);
  7079. }
  7080. };
  7081. struct llm_build_qwen3 : public llm_graph_context {
  7082. llm_build_qwen3(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  7083. const int64_t n_embd_head = hparams.n_embd_head_v;
  7084. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  7085. GGML_ASSERT(n_embd_head == hparams.n_rot);
  7086. ggml_tensor * cur;
  7087. ggml_tensor * inpL;
  7088. inpL = build_inp_embd(model.tok_embd);
  7089. // inp_pos - contains the positions
  7090. ggml_tensor * inp_pos = build_inp_pos();
  7091. auto * inp_attn = build_attn_inp_kv_unified();
  7092. ggml_tensor * inp_out_ids = build_inp_out_ids();
  7093. for (int il = 0; il < n_layer; ++il) {
  7094. ggml_tensor * inpSA = inpL;
  7095. // norm
  7096. cur = build_norm(inpL,
  7097. model.layers[il].attn_norm, NULL,
  7098. LLM_NORM_RMS, il);
  7099. cb(cur, "attn_norm", il);
  7100. // self-attention
  7101. {
  7102. // compute Q and K and RoPE them
  7103. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  7104. cb(Qcur, "Qcur", il);
  7105. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  7106. cb(Kcur, "Kcur", il);
  7107. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  7108. cb(Vcur, "Vcur", il);
  7109. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  7110. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  7111. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  7112. Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);
  7113. cb(Qcur, "Qcur_normed", il);
  7114. Qcur = ggml_rope_ext(
  7115. ctx0, Qcur, inp_pos, nullptr,
  7116. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  7117. ext_factor, attn_factor, beta_fast, beta_slow
  7118. );
  7119. Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);
  7120. cb(Kcur, "Kcur_normed", il);
  7121. Kcur = ggml_rope_ext(
  7122. ctx0, Kcur, inp_pos, nullptr,
  7123. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  7124. ext_factor, attn_factor, beta_fast, beta_slow
  7125. );
  7126. cb(Qcur, "Qcur", il);
  7127. cb(Kcur, "Kcur", il);
  7128. cb(Vcur, "Vcur", il);
  7129. cur = build_attn(inp_attn,
  7130. model.layers[il].wo, model.layers[il].bo,
  7131. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  7132. }
  7133. if (il == n_layer - 1 && inp_out_ids) {
  7134. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  7135. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  7136. }
  7137. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  7138. cb(ffn_inp, "ffn_inp", il);
  7139. // feed-forward network
  7140. cur = build_norm(ffn_inp,
  7141. model.layers[il].ffn_norm, NULL,
  7142. LLM_NORM_RMS, il);
  7143. cb(cur, "ffn_norm", il);
  7144. cur = build_ffn(cur,
  7145. model.layers[il].ffn_up, NULL, NULL,
  7146. model.layers[il].ffn_gate, NULL, NULL,
  7147. model.layers[il].ffn_down, NULL, NULL,
  7148. NULL,
  7149. LLM_FFN_SILU, LLM_FFN_PAR, il);
  7150. cb(cur, "ffn_out", il);
  7151. cur = ggml_add(ctx0, cur, ffn_inp);
  7152. cur = build_cvec(cur, il);
  7153. cb(cur, "l_out", il);
  7154. // input for next layer
  7155. inpL = cur;
  7156. }
  7157. cur = inpL;
  7158. cur = build_norm(cur,
  7159. model.output_norm, NULL,
  7160. LLM_NORM_RMS, -1);
  7161. cb(cur, "result_norm", -1);
  7162. res->t_embd = cur;
  7163. // lm_head
  7164. cur = build_lora_mm(model.output, cur);
  7165. cb(cur, "result_output", -1);
  7166. res->t_logits = cur;
  7167. ggml_build_forward_expand(gf, cur);
  7168. }
  7169. };
  7170. struct llm_build_qwen3moe : public llm_graph_context {
  7171. llm_build_qwen3moe(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  7172. const int64_t n_embd_head = hparams.n_embd_head_v;
  7173. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  7174. GGML_ASSERT(n_embd_head == hparams.n_rot);
  7175. ggml_tensor * cur;
  7176. ggml_tensor * inpL;
  7177. inpL = build_inp_embd(model.tok_embd);
  7178. // inp_pos - contains the positions
  7179. ggml_tensor * inp_pos = build_inp_pos();
  7180. auto * inp_attn = build_attn_inp_kv_unified();
  7181. ggml_tensor * inp_out_ids = build_inp_out_ids();
  7182. for (int il = 0; il < n_layer; ++il) {
  7183. ggml_tensor * inpSA = inpL;
  7184. // norm
  7185. cur = build_norm(inpL,
  7186. model.layers[il].attn_norm, NULL,
  7187. LLM_NORM_RMS, il);
  7188. cb(cur, "attn_norm", il);
  7189. // self_attention
  7190. {
  7191. // compute Q and K and RoPE them
  7192. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  7193. cb(Qcur, "Qcur", il);
  7194. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  7195. cb(Kcur, "Kcur", il);
  7196. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  7197. cb(Vcur, "Vcur", il);
  7198. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  7199. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  7200. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  7201. Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);
  7202. cb(Qcur, "Qcur_normed", il);
  7203. Qcur = ggml_rope_ext(
  7204. ctx0, Qcur, inp_pos, nullptr,
  7205. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  7206. ext_factor, attn_factor, beta_fast, beta_slow
  7207. );
  7208. Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);
  7209. cb(Kcur, "Kcur_normed", il);
  7210. Kcur = ggml_rope_ext(
  7211. ctx0, Kcur, inp_pos, nullptr,
  7212. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  7213. ext_factor, attn_factor, beta_fast, beta_slow
  7214. );
  7215. cb(Qcur, "Qcur", il);
  7216. cb(Kcur, "Kcur", il);
  7217. cb(Vcur, "Vcur", il);
  7218. cur = build_attn(inp_attn,
  7219. model.layers[il].wo, model.layers[il].bo,
  7220. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  7221. }
  7222. if (il == n_layer - 1 && inp_out_ids) {
  7223. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  7224. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  7225. }
  7226. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  7227. cb(ffn_inp, "ffn_inp", il);
  7228. // MoE branch
  7229. cur = build_norm(ffn_inp,
  7230. model.layers[il].ffn_norm, NULL,
  7231. LLM_NORM_RMS, il);
  7232. cb(cur, "ffn_norm", il);
  7233. ggml_tensor * moe_out =
  7234. build_moe_ffn(cur,
  7235. model.layers[il].ffn_gate_inp,
  7236. model.layers[il].ffn_up_exps,
  7237. model.layers[il].ffn_gate_exps,
  7238. model.layers[il].ffn_down_exps,
  7239. nullptr,
  7240. n_expert, n_expert_used,
  7241. LLM_FFN_SILU, true,
  7242. false, 0.0,
  7243. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  7244. il);
  7245. cb(moe_out, "ffn_moe_out", il);
  7246. cur = moe_out;
  7247. cur = ggml_add(ctx0, cur, ffn_inp);
  7248. cur = build_cvec(cur, il);
  7249. cb(cur, "l_out", il);
  7250. // input for next layer
  7251. inpL = cur;
  7252. }
  7253. cur = inpL;
  7254. cur = build_norm(cur,
  7255. model.output_norm, NULL,
  7256. LLM_NORM_RMS, -1);
  7257. cb(cur, "result_norm", -1);
  7258. res->t_embd = cur;
  7259. // lm_head
  7260. cur = build_lora_mm(model.output, cur);
  7261. cb(cur, "result_output", -1);
  7262. res->t_logits = cur;
  7263. ggml_build_forward_expand(gf, cur);
  7264. }
  7265. };
  7266. struct llm_build_phi2 : public llm_graph_context {
  7267. llm_build_phi2(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  7268. const int64_t n_embd_head = hparams.n_embd_head_v;
  7269. const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  7270. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  7271. ggml_tensor * cur;
  7272. ggml_tensor * attn_norm_output;
  7273. ggml_tensor * ffn_output;
  7274. ggml_tensor * inpL;
  7275. inpL = build_inp_embd(model.tok_embd);
  7276. // inp_pos - contains the positions
  7277. ggml_tensor * inp_pos = build_inp_pos();
  7278. auto * inp_attn = build_attn_inp_kv_unified();
  7279. ggml_tensor * inp_out_ids = build_inp_out_ids();
  7280. for (int il = 0; il < n_layer; ++il) {
  7281. attn_norm_output = build_norm(inpL,
  7282. model.layers[il].attn_norm,
  7283. model.layers[il].attn_norm_b,
  7284. LLM_NORM, il);
  7285. cb(attn_norm_output, "attn_norm", il);
  7286. // self-attention
  7287. {
  7288. ggml_tensor * Qcur = nullptr;
  7289. ggml_tensor * Kcur = nullptr;
  7290. ggml_tensor * Vcur = nullptr;
  7291. if (model.layers[il].wqkv) {
  7292. cur = build_lora_mm(model.layers[il].wqkv, attn_norm_output);
  7293. cb(cur, "wqkv", il);
  7294. cur = ggml_add(ctx0, cur, model.layers[il].bqkv);
  7295. cb(cur, "bqkv", il);
  7296. Qcur = ggml_view_3d(ctx0, cur, n_embd_head, n_head, n_tokens, n_embd_head*sizeof(float), cur->nb[1], 0*sizeof(float)*(n_embd));
  7297. Kcur = ggml_view_3d(ctx0, cur, n_embd_head, n_head_kv, n_tokens, n_embd_head*sizeof(float), cur->nb[1], 1*sizeof(float)*(n_embd));
  7298. Vcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd + n_embd_gqa)));
  7299. } else {
  7300. Qcur = ggml_add(ctx0, build_lora_mm(model.layers[il].wq, attn_norm_output), model.layers[il].bq);
  7301. Kcur = ggml_add(ctx0, build_lora_mm(model.layers[il].wk, attn_norm_output), model.layers[il].bk);
  7302. Vcur = ggml_add(ctx0, build_lora_mm(model.layers[il].wv, attn_norm_output), model.layers[il].bv);
  7303. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  7304. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  7305. }
  7306. cb(Qcur, "Qcur", il);
  7307. cb(Kcur, "Kcur", il);
  7308. cb(Vcur, "Vcur", il);
  7309. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  7310. Qcur = ggml_rope_ext(
  7311. ctx0, Qcur, inp_pos, nullptr,
  7312. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  7313. ext_factor, attn_factor, beta_fast, beta_slow
  7314. );
  7315. Kcur = ggml_rope_ext(
  7316. ctx0, Kcur, inp_pos, nullptr,
  7317. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  7318. ext_factor, attn_factor, beta_fast, beta_slow
  7319. );
  7320. cb(Qcur, "Qcur", il);
  7321. cb(Kcur, "Kcur", il);
  7322. cb(Vcur, "Vcur", il);
  7323. // with phi2, we scale the Q to avoid precision issues
  7324. // ref: https://github.com/ml-explore/mlx-examples/blob/08e862336ade809bc37d1035f94b359e7d1a5152/phi2/phi2.py#L64-L66
  7325. Qcur = ggml_scale(ctx0, Qcur, 1.0f/sqrtf(float(n_embd_head)));
  7326. cur = build_attn(inp_attn,
  7327. model.layers[il].wo, model.layers[il].bo,
  7328. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f, il);
  7329. }
  7330. if (il == n_layer - 1 && inp_out_ids) {
  7331. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  7332. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  7333. attn_norm_output = ggml_get_rows(ctx0, attn_norm_output, inp_out_ids);
  7334. }
  7335. // FF
  7336. {
  7337. ffn_output = build_ffn(attn_norm_output,
  7338. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  7339. NULL, NULL, NULL,
  7340. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  7341. NULL,
  7342. LLM_FFN_GELU, LLM_FFN_SEQ, il);
  7343. cb(ffn_output, "ffn_out", il);
  7344. }
  7345. cur = ggml_add(ctx0, cur, ffn_output);
  7346. cur = ggml_add(ctx0, cur, inpL);
  7347. cur = build_cvec(cur, il);
  7348. cb(cur, "l_out", il);
  7349. // input for next layer
  7350. inpL = cur;
  7351. }
  7352. cur = build_norm(inpL,
  7353. model.output_norm,
  7354. model.output_norm_b,
  7355. LLM_NORM, -1);
  7356. cb(cur, "result_norm", -1);
  7357. res->t_embd = cur;
  7358. cur = build_lora_mm(model.output, cur);
  7359. cb(cur, "result_output_no_bias", -1);
  7360. cur = ggml_add(ctx0, cur, model.output_b);
  7361. cb(cur, "result_output", -1);
  7362. res->t_logits = cur;
  7363. ggml_build_forward_expand(gf, cur);
  7364. }
  7365. };
  7366. template<bool iswa>
  7367. struct llm_build_phi3 : public llm_graph_context {
  7368. llm_build_phi3(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  7369. const int64_t n_embd_head = hparams.n_embd_head_v;
  7370. const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  7371. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  7372. ggml_tensor * cur;
  7373. ggml_tensor * inpL;
  7374. inpL = build_inp_embd(model.tok_embd);
  7375. // inp_pos - contains the positions
  7376. ggml_tensor * inp_pos = build_inp_pos();
  7377. using inp_attn_type = std::conditional_t<iswa, llm_graph_input_attn_kv_unified_iswa, llm_graph_input_attn_kv_unified>;
  7378. inp_attn_type * inp_attn = nullptr;
  7379. if constexpr (iswa) {
  7380. inp_attn = build_attn_inp_kv_unified_iswa();
  7381. } else {
  7382. inp_attn = build_attn_inp_kv_unified();
  7383. }
  7384. ggml_tensor * inp_out_ids = build_inp_out_ids();
  7385. for (int il = 0; il < n_layer; ++il) {
  7386. auto * residual = inpL;
  7387. // self-attention
  7388. {
  7389. // rope freq factors for 128k context
  7390. ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
  7391. ggml_tensor* attn_norm_output = build_norm(inpL,
  7392. model.layers[il].attn_norm,
  7393. model.layers[il].attn_norm_b,
  7394. LLM_NORM_RMS, il);
  7395. cb(attn_norm_output, "attn_norm", il);
  7396. ggml_tensor * Qcur = nullptr;
  7397. ggml_tensor * Kcur = nullptr;
  7398. ggml_tensor * Vcur = nullptr;
  7399. if (model.layers[il].wqkv) {
  7400. cur = build_lora_mm(model.layers[il].wqkv, attn_norm_output);
  7401. cb(cur, "wqkv", il);
  7402. Qcur = ggml_view_3d(ctx0, cur, n_embd_head, n_head, n_tokens, n_embd_head * sizeof(float), cur->nb[1], 0 * sizeof(float) * (n_embd));
  7403. Kcur = ggml_view_3d(ctx0, cur, n_embd_head, n_head_kv, n_tokens, n_embd_head * sizeof(float), cur->nb[1], 1 * sizeof(float) * (n_embd));
  7404. Vcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1 * sizeof(float) * (n_embd + n_embd_gqa)));
  7405. } else {
  7406. Qcur = ggml_add(ctx0, build_lora_mm(model.layers[il].wq, attn_norm_output), model.layers[il].bq);
  7407. Kcur = ggml_add(ctx0, build_lora_mm(model.layers[il].wk, attn_norm_output), model.layers[il].bk);
  7408. Vcur = ggml_add(ctx0, build_lora_mm(model.layers[il].wv, attn_norm_output), model.layers[il].bv);
  7409. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  7410. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  7411. }
  7412. cb(Qcur, "Qcur", il);
  7413. cb(Kcur, "Kcur", il);
  7414. cb(Vcur, "Vcur", il);
  7415. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  7416. Qcur = ggml_rope_ext(
  7417. ctx0, Qcur, inp_pos, rope_factors,
  7418. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  7419. ext_factor, attn_factor, beta_fast, beta_slow
  7420. );
  7421. Kcur = ggml_rope_ext(
  7422. ctx0, Kcur, inp_pos, rope_factors,
  7423. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  7424. ext_factor, attn_factor, beta_fast, beta_slow
  7425. );
  7426. cb(Qcur, "Qcur", il);
  7427. cb(Kcur, "Kcur", il);
  7428. cb(Vcur, "Vcur", il);
  7429. Qcur = ggml_scale(ctx0, Qcur, 1.0f / sqrtf(float(n_embd_head)));
  7430. cb(Qcur, "Qcur", il);
  7431. cur = build_attn(inp_attn,
  7432. model.layers[il].wo, model.layers[il].bo,
  7433. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f, il);
  7434. }
  7435. if (il == n_layer - 1 && inp_out_ids) {
  7436. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  7437. residual = ggml_get_rows(ctx0, residual, inp_out_ids);
  7438. }
  7439. cur = ggml_add(ctx0, cur, residual);
  7440. residual = cur;
  7441. cur = build_norm(cur,
  7442. model.layers[il].ffn_norm, model.layers[il].ffn_norm_b,
  7443. LLM_NORM_RMS, il);
  7444. cb(cur, "ffn_norm", il);
  7445. // feed-forward network
  7446. if (model.layers[il].ffn_gate_inp == nullptr) {
  7447. cur = build_ffn(cur,
  7448. model.layers[il].ffn_up, NULL, NULL,
  7449. NULL, NULL, NULL,
  7450. model.layers[il].ffn_down, NULL, NULL,
  7451. NULL,
  7452. LLM_FFN_SWIGLU, LLM_FFN_SEQ, il);
  7453. cb(cur, "ffn_out", il);
  7454. } else {
  7455. // MoE branch
  7456. cur = build_moe_ffn(cur,
  7457. model.layers[il].ffn_gate_inp,
  7458. model.layers[il].ffn_up_exps,
  7459. model.layers[il].ffn_gate_exps,
  7460. model.layers[il].ffn_down_exps,
  7461. nullptr,
  7462. n_expert, n_expert_used,
  7463. LLM_FFN_SILU, true,
  7464. false, 0.0,
  7465. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  7466. il);
  7467. cb(cur, "ffn_moe_out", il);
  7468. }
  7469. cur = ggml_add(ctx0, residual, cur);
  7470. cur = build_cvec(cur, il);
  7471. cb(cur, "l_out", il);
  7472. // input for next layer
  7473. inpL = cur;
  7474. }
  7475. cur = build_norm(inpL,
  7476. model.output_norm,
  7477. model.output_norm_b,
  7478. LLM_NORM_RMS, -1);
  7479. cb(cur, "result_norm", -1);
  7480. res->t_embd = cur;
  7481. cur = build_lora_mm(model.output, cur);
  7482. if (model.output_b != nullptr) {
  7483. cb(cur, "result_output_no_bias", -1);
  7484. cur = ggml_add(ctx0, cur, model.output_b);
  7485. }
  7486. cb(cur, "result_output", -1);
  7487. res->t_logits = cur;
  7488. ggml_build_forward_expand(gf, cur);
  7489. }
  7490. };
  7491. struct llm_build_plamo : public llm_graph_context {
  7492. llm_build_plamo(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  7493. const int64_t n_embd_head = hparams.n_embd_head_v;
  7494. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  7495. GGML_ASSERT(n_embd_head == hparams.n_rot);
  7496. ggml_tensor * cur;
  7497. ggml_tensor * inpL;
  7498. inpL = build_inp_embd(model.tok_embd);
  7499. // inp_pos - contains the positions
  7500. ggml_tensor * inp_pos = build_inp_pos();
  7501. auto * inp_attn = build_attn_inp_kv_unified();
  7502. ggml_tensor * inp_out_ids = build_inp_out_ids();
  7503. for (int il = 0; il < n_layer; ++il) {
  7504. // norm
  7505. cur = build_norm(inpL,
  7506. model.layers[il].attn_norm, NULL,
  7507. LLM_NORM_RMS, il);
  7508. cb(cur, "attn_norm", il);
  7509. ggml_tensor * sa_inp = cur;
  7510. // self-attention
  7511. {
  7512. // compute Q and K and RoPE them
  7513. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  7514. cb(Qcur, "Qcur", il);
  7515. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  7516. cb(Kcur, "Kcur", il);
  7517. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  7518. cb(Vcur, "Vcur", il);
  7519. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  7520. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  7521. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  7522. Qcur = ggml_rope_ext(
  7523. ctx0, Qcur, inp_pos, nullptr,
  7524. n_embd_head, rope_type, n_ctx_orig, freq_base, freq_scale,
  7525. ext_factor, attn_factor, beta_fast, beta_slow
  7526. );
  7527. Kcur = ggml_rope_ext(
  7528. ctx0, Kcur, inp_pos, nullptr,
  7529. n_embd_head, rope_type, n_ctx_orig, freq_base, freq_scale,
  7530. ext_factor, attn_factor, beta_fast, beta_slow
  7531. );
  7532. cb(Qcur, "Qcur", il);
  7533. cb(Kcur, "Kcur", il);
  7534. cb(Vcur, "Vcur", il);
  7535. cur = build_attn(inp_attn,
  7536. model.layers[il].wo, NULL,
  7537. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  7538. }
  7539. if (il == n_layer - 1 && inp_out_ids) {
  7540. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  7541. sa_inp = ggml_get_rows(ctx0, sa_inp, inp_out_ids);
  7542. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  7543. }
  7544. ggml_tensor * sa_out = cur;
  7545. cur = sa_inp;
  7546. // feed-forward network
  7547. {
  7548. cur = build_ffn(cur,
  7549. model.layers[il].ffn_up, NULL, NULL,
  7550. model.layers[il].ffn_gate, NULL, NULL,
  7551. model.layers[il].ffn_down, NULL, NULL,
  7552. NULL,
  7553. LLM_FFN_SILU, LLM_FFN_PAR, il);
  7554. cb(cur, "ffn_out", il);
  7555. }
  7556. cur = ggml_add(ctx0, cur, sa_out);
  7557. cur = ggml_add(ctx0, cur, inpL);
  7558. cur = build_cvec(cur, il);
  7559. cb(cur, "l_out", il);
  7560. // input for next layer
  7561. inpL = cur;
  7562. }
  7563. cur = inpL;
  7564. cur = build_norm(cur,
  7565. model.output_norm, NULL,
  7566. LLM_NORM_RMS, -1);
  7567. cb(cur, "result_norm", -1);
  7568. res->t_embd = cur;
  7569. // lm_head
  7570. cur = build_lora_mm(model.output, cur);
  7571. cb(cur, "result_output", -1);
  7572. res->t_logits = cur;
  7573. ggml_build_forward_expand(gf, cur);
  7574. }
  7575. };
  7576. struct llm_build_gpt2 : public llm_graph_context {
  7577. llm_build_gpt2(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  7578. const int64_t n_embd_head = hparams.n_embd_head_v;
  7579. const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  7580. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  7581. ggml_tensor * cur;
  7582. ggml_tensor * pos;
  7583. ggml_tensor * inpL;
  7584. inpL = build_inp_embd(model.tok_embd);
  7585. // inp_pos - contains the positions
  7586. ggml_tensor * inp_pos = build_inp_pos();
  7587. auto * inp_attn = build_attn_inp_kv_unified();
  7588. pos = ggml_get_rows(ctx0, model.pos_embd, inp_pos);
  7589. cb(pos, "pos_embd", -1);
  7590. inpL = ggml_add(ctx0, inpL, pos);
  7591. cb(inpL, "inpL", -1);
  7592. ggml_tensor * inp_out_ids = build_inp_out_ids();
  7593. for (int il = 0; il < n_layer; ++il) {
  7594. cur = build_norm(inpL,
  7595. model.layers[il].attn_norm,
  7596. model.layers[il].attn_norm_b,
  7597. LLM_NORM, il);
  7598. cb(cur, "attn_norm", il);
  7599. // self-attention
  7600. {
  7601. cur = build_lora_mm(model.layers[il].wqkv, cur);
  7602. cb(cur, "wqkv", il);
  7603. cur = ggml_add(ctx0, cur, model.layers[il].bqkv);
  7604. cb(cur, "bqkv", il);
  7605. ggml_tensor * Qcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd, n_tokens, cur->nb[1], 0*sizeof(float)*(n_embd)));
  7606. ggml_tensor * Kcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd)));
  7607. ggml_tensor * Vcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd + n_embd_gqa)));
  7608. cb(Qcur, "Qcur", il);
  7609. cb(Kcur, "Kcur", il);
  7610. cb(Vcur, "Vcur", il);
  7611. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  7612. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  7613. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  7614. cur = build_attn(inp_attn,
  7615. model.layers[il].wo, model.layers[il].bo,
  7616. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  7617. }
  7618. if (il == n_layer - 1 && inp_out_ids) {
  7619. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  7620. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  7621. }
  7622. // add the input
  7623. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
  7624. cb(ffn_inp, "ffn_inp", il);
  7625. // FF
  7626. {
  7627. cur = build_norm(ffn_inp,
  7628. model.layers[il].ffn_norm,
  7629. model.layers[il].ffn_norm_b,
  7630. LLM_NORM, il);
  7631. cb(cur, "ffn_norm", il);
  7632. cur = build_ffn(cur,
  7633. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  7634. NULL, NULL, NULL,
  7635. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  7636. NULL,
  7637. LLM_FFN_GELU, LLM_FFN_SEQ, il);
  7638. cb(cur, "ffn_out", il);
  7639. }
  7640. cur = ggml_add(ctx0, cur, ffn_inp);
  7641. cur = build_cvec(cur, il);
  7642. cb(cur, "l_out", il);
  7643. // input for next layer
  7644. inpL = cur;
  7645. }
  7646. cur = build_norm(inpL,
  7647. model.output_norm,
  7648. model.output_norm_b,
  7649. LLM_NORM, -1);
  7650. cb(cur, "result_norm", -1);
  7651. res->t_embd = cur;
  7652. cur = build_lora_mm(model.output, cur);
  7653. cb(cur, "result_output", -1);
  7654. res->t_logits = cur;
  7655. ggml_build_forward_expand(gf, cur);
  7656. }
  7657. };
  7658. struct llm_build_codeshell : public llm_graph_context {
  7659. llm_build_codeshell(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  7660. const int64_t n_embd_head = hparams.n_embd_head_v;
  7661. const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  7662. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  7663. GGML_ASSERT(n_embd_head == hparams.n_rot);
  7664. ggml_tensor * cur;
  7665. ggml_tensor * inpL;
  7666. inpL = build_inp_embd(model.tok_embd);
  7667. // inp_pos - contains the positions
  7668. ggml_tensor * inp_pos = build_inp_pos();
  7669. auto * inp_attn = build_attn_inp_kv_unified();
  7670. ggml_tensor * inp_out_ids = build_inp_out_ids();
  7671. for (int il = 0; il < n_layer; ++il) {
  7672. cur = build_norm(inpL,
  7673. model.layers[il].attn_norm,
  7674. model.layers[il].attn_norm_b,
  7675. LLM_NORM, il);
  7676. cb(cur, "attn_norm", il);
  7677. // self-attention
  7678. {
  7679. cur = build_lora_mm(model.layers[il].wqkv, cur);
  7680. cb(cur, "wqkv", il);
  7681. cur = ggml_add(ctx0, cur, model.layers[il].bqkv);
  7682. cb(cur, "bqkv", il);
  7683. ggml_tensor * Qcur = ggml_view_3d(ctx0, cur, n_embd_head, n_head, n_tokens, n_embd_head*sizeof(float), cur->nb[1], 0*sizeof(float)*(n_embd));
  7684. ggml_tensor * Kcur = ggml_view_3d(ctx0, cur, n_embd_head, n_head_kv, n_tokens, n_embd_head*sizeof(float), cur->nb[1], 1*sizeof(float)*(n_embd));
  7685. ggml_tensor * Vcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd + n_embd_gqa)));
  7686. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  7687. Qcur = ggml_rope_ext(
  7688. ctx0, Qcur, inp_pos, nullptr,
  7689. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  7690. ext_factor, attn_factor, beta_fast, beta_slow
  7691. );
  7692. Kcur = ggml_rope_ext(
  7693. ctx0, Kcur, inp_pos, nullptr,
  7694. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  7695. ext_factor, attn_factor, beta_fast, beta_slow
  7696. );
  7697. cb(Qcur, "Qcur", il);
  7698. cb(Kcur, "Kcur", il);
  7699. cb(Vcur, "Vcur", il);
  7700. cur = build_attn(inp_attn,
  7701. model.layers[il].wo, model.layers[il].bo,
  7702. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  7703. }
  7704. if (il == n_layer - 1 && inp_out_ids) {
  7705. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  7706. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  7707. }
  7708. // add the input
  7709. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
  7710. cb(ffn_inp, "ffn_inp", il);
  7711. // FF
  7712. {
  7713. cur = build_norm(ffn_inp,
  7714. model.layers[il].ffn_norm,
  7715. model.layers[il].ffn_norm_b,
  7716. LLM_NORM, il);
  7717. cb(cur, "ffn_norm", il);
  7718. cur = build_ffn(cur,
  7719. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  7720. NULL, NULL, NULL,
  7721. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  7722. NULL,
  7723. LLM_FFN_GELU, LLM_FFN_SEQ, il);
  7724. cb(cur, "ffn_out", il);
  7725. }
  7726. cur = ggml_add(ctx0, cur, ffn_inp);
  7727. cur = build_cvec(cur, il);
  7728. cb(cur, "l_out", il);
  7729. // input for next layer
  7730. inpL = cur;
  7731. }
  7732. cur = build_norm(inpL,
  7733. model.output_norm,
  7734. model.output_norm_b,
  7735. LLM_NORM, -1);
  7736. cb(cur, "result_norm", -1);
  7737. res->t_embd = cur;
  7738. cur = build_lora_mm(model.output, cur);
  7739. cb(cur, "result_output", -1);
  7740. res->t_logits = cur;
  7741. ggml_build_forward_expand(gf, cur);
  7742. }
  7743. };
  7744. struct llm_build_orion : public llm_graph_context {
  7745. llm_build_orion(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  7746. const int64_t n_embd_head = hparams.n_embd_head_v;
  7747. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  7748. GGML_ASSERT(n_embd_head == hparams.n_rot);
  7749. ggml_tensor * cur;
  7750. ggml_tensor * inpL;
  7751. inpL = build_inp_embd(model.tok_embd);
  7752. // inp_pos - contains the positions
  7753. ggml_tensor * inp_pos = build_inp_pos();
  7754. auto * inp_attn = build_attn_inp_kv_unified();
  7755. ggml_tensor * inp_out_ids = build_inp_out_ids();
  7756. for (int il = 0; il < n_layer; ++il) {
  7757. ggml_tensor * inpSA = inpL;
  7758. // norm
  7759. cur = build_norm(inpL,
  7760. model.layers[il].attn_norm, model.layers[il].attn_norm_b,
  7761. LLM_NORM, il);
  7762. cb(cur, "attn_norm", il);
  7763. // self-attention
  7764. {
  7765. // compute Q and K and RoPE them
  7766. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  7767. cb(Qcur, "Qcur", il);
  7768. // if (model.layers[il].bq) {
  7769. // Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  7770. // cb(Qcur, "Qcur", il);
  7771. // }
  7772. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  7773. cb(Kcur, "Kcur", il);
  7774. // if (model.layers[il].bk) {
  7775. // Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  7776. // cb(Kcur, "Kcur", il);
  7777. // }
  7778. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  7779. cb(Vcur, "Vcur", il);
  7780. // if (model.layers[il].bv) {
  7781. // Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  7782. // cb(Vcur, "Vcur", il);
  7783. // }
  7784. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  7785. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  7786. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  7787. Qcur = ggml_rope_ext(
  7788. ctx0, Qcur, inp_pos, nullptr,
  7789. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  7790. ext_factor, attn_factor, beta_fast, beta_slow
  7791. );
  7792. Kcur = ggml_rope_ext(
  7793. ctx0, Kcur, inp_pos, nullptr,
  7794. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  7795. ext_factor, attn_factor, beta_fast, beta_slow
  7796. );
  7797. cb(Qcur, "Qcur", il);
  7798. cb(Kcur, "Kcur", il);
  7799. cb(Vcur, "Vcur", il);
  7800. cur = build_attn(inp_attn,
  7801. model.layers[il].wo, NULL,
  7802. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  7803. }
  7804. if (il == n_layer - 1 && inp_out_ids) {
  7805. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  7806. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  7807. }
  7808. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  7809. cb(ffn_inp, "ffn_inp", il);
  7810. // feed-forward network
  7811. cur = build_norm(ffn_inp,
  7812. model.layers[il].ffn_norm, model.layers[il].ffn_norm_b,
  7813. LLM_NORM, il);
  7814. cb(cur, "ffn_norm", il);
  7815. cur = build_ffn(cur,
  7816. model.layers[il].ffn_up, NULL, NULL,
  7817. model.layers[il].ffn_gate, NULL, NULL,
  7818. model.layers[il].ffn_down, NULL, NULL,
  7819. NULL,
  7820. LLM_FFN_SILU, LLM_FFN_PAR, il);
  7821. cb(cur, "ffn_out", il);
  7822. cur = ggml_add(ctx0, cur, ffn_inp);
  7823. cur = build_cvec(cur, il);
  7824. cb(cur, "l_out", il);
  7825. // input for next layer
  7826. inpL = cur;
  7827. }
  7828. cur = inpL;
  7829. cur = build_norm(cur,
  7830. model.output_norm, model.output_norm_b,
  7831. LLM_NORM, -1);
  7832. cb(cur, "result_norm", -1);
  7833. res->t_embd = cur;
  7834. // lm_head
  7835. cur = build_lora_mm(model.output, cur);
  7836. cb(cur, "result_output", -1);
  7837. res->t_logits = cur;
  7838. ggml_build_forward_expand(gf, cur);
  7839. }
  7840. };
  7841. struct llm_build_internlm2 : public llm_graph_context {
  7842. llm_build_internlm2(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  7843. const int64_t n_embd_head = hparams.n_embd_head_v;
  7844. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  7845. GGML_ASSERT(n_embd_head == hparams.n_rot);
  7846. ggml_tensor * cur;
  7847. ggml_tensor * inpL;
  7848. inpL = build_inp_embd(model.tok_embd);
  7849. // inp_pos - contains the positions
  7850. ggml_tensor * inp_pos = build_inp_pos();
  7851. auto * inp_attn = build_attn_inp_kv_unified();
  7852. ggml_tensor * inp_out_ids = build_inp_out_ids();
  7853. for (int il = 0; il < n_layer; ++il) {
  7854. ggml_tensor * inpSA = inpL;
  7855. // norm
  7856. cur = build_norm(inpL,
  7857. model.layers[il].attn_norm, NULL,
  7858. LLM_NORM_RMS, il);
  7859. cb(cur, "attn_norm", il);
  7860. // self-attention
  7861. {
  7862. // compute Q and K and RoPE them
  7863. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  7864. cb(Qcur, "Qcur", il);
  7865. if (model.layers[il].bq) {
  7866. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  7867. cb(Qcur, "Qcur", il);
  7868. }
  7869. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  7870. cb(Kcur, "Kcur", il);
  7871. if (model.layers[il].bk) {
  7872. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  7873. cb(Kcur, "Kcur", il);
  7874. }
  7875. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  7876. cb(Vcur, "Vcur", il);
  7877. if (model.layers[il].bv) {
  7878. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  7879. cb(Vcur, "Vcur", il);
  7880. }
  7881. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  7882. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  7883. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  7884. Qcur = ggml_rope_ext(
  7885. ctx0, Qcur, inp_pos, nullptr,
  7886. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  7887. ext_factor, attn_factor, beta_fast, beta_slow
  7888. );
  7889. Kcur = ggml_rope_ext(
  7890. ctx0, Kcur, inp_pos, nullptr,
  7891. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  7892. ext_factor, attn_factor, beta_fast, beta_slow
  7893. );
  7894. cb(Qcur, "Qcur", il);
  7895. cb(Kcur, "Kcur", il);
  7896. cb(Vcur, "Vcur", il);
  7897. cur = build_attn(inp_attn,
  7898. model.layers[il].wo, model.layers[il].bo,
  7899. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  7900. }
  7901. if (il == n_layer - 1 && inp_out_ids) {
  7902. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  7903. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  7904. }
  7905. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  7906. cb(ffn_inp, "ffn_inp", il);
  7907. // feed-forward network
  7908. cur = build_norm(ffn_inp,
  7909. model.layers[il].ffn_norm, NULL,
  7910. LLM_NORM_RMS, il);
  7911. cb(cur, "ffn_norm", il);
  7912. cur = build_ffn(cur,
  7913. model.layers[il].ffn_up, NULL, NULL,
  7914. model.layers[il].ffn_gate, NULL, NULL,
  7915. model.layers[il].ffn_down, NULL, NULL,
  7916. NULL,
  7917. LLM_FFN_SILU, LLM_FFN_PAR, il);
  7918. cb(cur, "ffn_out", il);
  7919. cur = ggml_add(ctx0, cur, ffn_inp);
  7920. cur = build_cvec(cur, il);
  7921. cb(cur, "l_out", il);
  7922. // input for next layer
  7923. inpL = cur;
  7924. }
  7925. cur = inpL;
  7926. cur = build_norm(cur,
  7927. model.output_norm, NULL,
  7928. LLM_NORM_RMS, -1);
  7929. cb(cur, "result_norm", -1);
  7930. res->t_embd = cur;
  7931. // lm_head
  7932. cur = build_lora_mm(model.output, cur);
  7933. cb(cur, "result_output", -1);
  7934. res->t_logits = cur;
  7935. ggml_build_forward_expand(gf, cur);
  7936. }
  7937. };
  7938. struct llm_build_minicpm3 : public llm_graph_context {
  7939. llm_build_minicpm3(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  7940. //TODO: if the model varies, these parameters need to be read from the model
  7941. const int64_t n_embd_base = 256;
  7942. const float scale_embd = 12.0f;
  7943. const float scale_depth = 1.4f;
  7944. const float kq_scale = 1.0f / sqrtf(float(hparams.n_embd_head_k));
  7945. const uint32_t n_embd_head_qk_rope = hparams.n_rot;
  7946. const uint32_t n_embd_head_qk_nope = hparams.n_embd_head_k - hparams.n_rot;
  7947. const uint32_t kv_lora_rank = hparams.n_lora_kv;
  7948. ggml_tensor * cur;
  7949. ggml_tensor * inpL;
  7950. inpL = build_inp_embd(model.tok_embd);
  7951. // scale the input embeddings
  7952. inpL = ggml_scale(ctx0, inpL, scale_embd);
  7953. cb(inpL, "inp_scaled", -1);
  7954. // inp_pos - contains the positions
  7955. ggml_tensor * inp_pos = build_inp_pos();
  7956. auto * inp_attn = build_attn_inp_kv_unified();
  7957. ggml_tensor * inp_out_ids = build_inp_out_ids();
  7958. for (int il = 0; il < n_layer; ++il) {
  7959. ggml_tensor * inpSA = inpL;
  7960. ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
  7961. // norm
  7962. cur = build_norm(inpL,
  7963. model.layers[il].attn_norm, NULL,
  7964. LLM_NORM_RMS, il);
  7965. cb(cur, "attn_norm", il);
  7966. // self_attention
  7967. {
  7968. ggml_tensor * q = NULL;
  7969. // {n_embd, q_lora_rank} * {n_embd, n_tokens} -> {q_lora_rank, n_tokens}
  7970. q = ggml_mul_mat(ctx0, model.layers[il].wq_a, cur);
  7971. cb(q, "q", il);
  7972. q = build_norm(q,
  7973. model.layers[il].attn_q_a_norm, NULL,
  7974. LLM_NORM_RMS, il);
  7975. cb(q, "q", il);
  7976. // {q_lora_rank, n_head * hparams.n_embd_head_k} * {q_lora_rank, n_tokens} -> {n_head * hparams.n_embd_head_k, n_tokens}
  7977. q = ggml_mul_mat(ctx0, model.layers[il].wq_b, q);
  7978. cb(q, "q", il);
  7979. // split into {n_head * n_embd_head_qk_nope, n_tokens}
  7980. ggml_tensor * q_nope = ggml_view_3d(ctx0, q, n_embd_head_qk_nope, n_head, n_tokens,
  7981. ggml_row_size(q->type, hparams.n_embd_head_k),
  7982. ggml_row_size(q->type, hparams.n_embd_head_k * n_head),
  7983. 0);
  7984. cb(q_nope, "q_nope", il);
  7985. // and {n_head * n_embd_head_qk_rope, n_tokens}
  7986. ggml_tensor * q_pe = ggml_view_3d(ctx0, q, n_embd_head_qk_rope, n_head, n_tokens,
  7987. ggml_row_size(q->type, hparams.n_embd_head_k),
  7988. ggml_row_size(q->type, hparams.n_embd_head_k * n_head),
  7989. ggml_row_size(q->type, n_embd_head_qk_nope));
  7990. cb(q_pe, "q_pe", il);
  7991. // {n_embd, kv_lora_rank + n_embd_head_qk_rope} * {n_embd, n_tokens} -> {kv_lora_rank + n_embd_head_qk_rope, n_tokens}
  7992. ggml_tensor * kv_pe_compresseed = ggml_mul_mat(ctx0, model.layers[il].wkv_a_mqa, cur);
  7993. cb(kv_pe_compresseed, "kv_pe_compresseed", il);
  7994. // split into {kv_lora_rank, n_tokens}
  7995. ggml_tensor * kv_compressed = ggml_view_2d(ctx0, kv_pe_compresseed, kv_lora_rank, n_tokens,
  7996. kv_pe_compresseed->nb[1],
  7997. 0);
  7998. cb(kv_compressed, "kv_compressed", il);
  7999. // and {n_embd_head_qk_rope, n_tokens}
  8000. ggml_tensor * k_pe = ggml_view_3d(ctx0, kv_pe_compresseed, n_embd_head_qk_rope, 1, n_tokens,
  8001. kv_pe_compresseed->nb[1],
  8002. kv_pe_compresseed->nb[1],
  8003. ggml_row_size(kv_pe_compresseed->type, kv_lora_rank));
  8004. cb(k_pe, "k_pe", il);
  8005. kv_compressed = build_norm(kv_compressed,
  8006. model.layers[il].attn_kv_a_norm, NULL,
  8007. LLM_NORM_RMS, il);
  8008. cb(kv_compressed, "kv_compressed", il);
  8009. // {kv_lora_rank, n_head * (n_embd_head_qk_nope + n_embd_head_v)} * {kv_lora_rank, n_tokens} -> {n_head * (n_embd_head_qk_nope + n_embd_head_v), n_tokens}
  8010. ggml_tensor * kv = ggml_mul_mat(ctx0, model.layers[il].wkv_b, kv_compressed);
  8011. cb(kv, "kv", il);
  8012. // split into {n_head * n_embd_head_qk_nope, n_tokens}
  8013. ggml_tensor * k_nope = ggml_view_3d(ctx0, kv, n_embd_head_qk_nope, n_head, n_tokens,
  8014. ggml_row_size(kv->type, n_embd_head_qk_nope + hparams.n_embd_head_v),
  8015. ggml_row_size(kv->type, n_head * (n_embd_head_qk_nope + hparams.n_embd_head_v)),
  8016. 0);
  8017. cb(k_nope, "k_nope", il);
  8018. // and {n_head * n_embd_head_v, n_tokens}
  8019. ggml_tensor * v_states = ggml_view_3d(ctx0, kv, hparams.n_embd_head_v, n_head, n_tokens,
  8020. ggml_row_size(kv->type, (n_embd_head_qk_nope + hparams.n_embd_head_v)),
  8021. ggml_row_size(kv->type, (n_embd_head_qk_nope + hparams.n_embd_head_v)*n_head),
  8022. ggml_row_size(kv->type, (n_embd_head_qk_nope)));
  8023. cb(v_states, "v_states", il);
  8024. v_states = ggml_cont(ctx0, v_states);
  8025. cb(v_states, "v_states", il);
  8026. q_pe = ggml_rope_ext(
  8027. ctx0, q_pe, inp_pos, rope_factors,
  8028. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  8029. ext_factor, attn_factor, beta_fast, beta_slow
  8030. );
  8031. cb(q_pe, "q_pe", il);
  8032. // shared RoPE key
  8033. k_pe = ggml_rope_ext(
  8034. ctx0, k_pe, inp_pos, rope_factors,
  8035. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  8036. ext_factor, attn_factor, beta_fast, beta_slow
  8037. );
  8038. cb(k_pe, "k_pe", il);
  8039. ggml_tensor * q_states = ggml_concat(ctx0, q_nope, q_pe, 0);
  8040. cb(q_states, "q_states", il);
  8041. ggml_tensor * k_states = ggml_concat(ctx0, k_nope, ggml_repeat(ctx0, k_pe, q_pe), 0);
  8042. cb(k_states, "k_states", il);
  8043. cur = build_attn(inp_attn,
  8044. model.layers[il].wo, NULL,
  8045. q_states, k_states, v_states, nullptr, nullptr, kq_scale, il);
  8046. }
  8047. if (il == n_layer - 1 && inp_out_ids) {
  8048. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  8049. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  8050. }
  8051. // scale_res - scale the hidden states for residual connection
  8052. const float scale_res = scale_depth/sqrtf(float(n_layer)); // TODO: is this correct?
  8053. cur = ggml_scale(ctx0, cur, scale_res);
  8054. cb(cur, "hidden_scaled", il);
  8055. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  8056. cb(ffn_inp, "ffn_inp", il);
  8057. // feed-forward network
  8058. {
  8059. cur = build_norm(ffn_inp,
  8060. model.layers[il].ffn_norm, NULL,
  8061. LLM_NORM_RMS, il);
  8062. cb(cur, "ffn_norm", il);
  8063. cur = build_ffn(cur,
  8064. model.layers[il].ffn_up, NULL, NULL,
  8065. model.layers[il].ffn_gate, NULL, NULL,
  8066. model.layers[il].ffn_down, NULL, NULL,
  8067. NULL,
  8068. LLM_FFN_SILU, LLM_FFN_PAR, il);
  8069. cb(cur, "ffn_out", il);
  8070. }
  8071. // scale the hidden states for residual connection
  8072. cur = ggml_scale(ctx0, cur, scale_res);
  8073. cb(cur, "hidden_scaled_ffn", il);
  8074. cur = ggml_add(ctx0, cur, ffn_inp);
  8075. cur = build_cvec(cur, il);
  8076. cb(cur, "l_out", il);
  8077. // input for next layer
  8078. inpL = cur;
  8079. }
  8080. cur = inpL;
  8081. cur = build_norm(cur,
  8082. model.output_norm, NULL,
  8083. LLM_NORM_RMS, -1);
  8084. cb(cur, "result_norm", -1);
  8085. res->t_embd = cur;
  8086. // lm_head scaling
  8087. const float scale_lmhead = float(n_embd_base)/float(n_embd);
  8088. cur = ggml_scale(ctx0, cur, scale_lmhead);
  8089. cb(cur, "lmhead_scaling", -1);
  8090. // lm_head
  8091. cur = build_lora_mm(model.output, cur);
  8092. cb(cur, "result_output", -1);
  8093. res->t_logits = cur;
  8094. ggml_build_forward_expand(gf, cur);
  8095. }
  8096. };
  8097. struct llm_build_gemma : public llm_graph_context {
  8098. llm_build_gemma(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  8099. const int64_t n_embd_head = hparams.n_embd_head_v;
  8100. ggml_tensor * cur;
  8101. ggml_tensor * inpL;
  8102. inpL = build_inp_embd(model.tok_embd);
  8103. inpL = ggml_scale(ctx0, inpL, sqrtf(n_embd));
  8104. cb(inpL, "inp_scaled", -1);
  8105. // inp_pos - contains the positions
  8106. ggml_tensor * inp_pos = build_inp_pos();
  8107. auto * inp_attn = build_attn_inp_kv_unified();
  8108. ggml_tensor * inp_out_ids = build_inp_out_ids();
  8109. for (int il = 0; il < n_layer; ++il) {
  8110. // norm
  8111. cur = build_norm(inpL,
  8112. model.layers[il].attn_norm, NULL,
  8113. LLM_NORM_RMS, il);
  8114. cb(cur, "attn_norm", il);
  8115. // self-attention
  8116. {
  8117. // compute Q and K and RoPE them
  8118. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  8119. cb(Qcur, "Qcur", il);
  8120. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  8121. cb(Kcur, "Kcur", il);
  8122. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  8123. cb(Vcur, "Vcur", il);
  8124. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  8125. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  8126. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  8127. Qcur = ggml_rope_ext(
  8128. ctx0, Qcur, inp_pos, nullptr,
  8129. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  8130. ext_factor, attn_factor, beta_fast, beta_slow);
  8131. Kcur = ggml_rope_ext(
  8132. ctx0, Kcur, inp_pos, nullptr,
  8133. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  8134. ext_factor, attn_factor, beta_fast, beta_slow);
  8135. cb(Qcur, "Qcur", il);
  8136. cb(Kcur, "Kcur", il);
  8137. cb(Vcur, "Vcur", il);
  8138. Qcur = ggml_scale(ctx0, Qcur, 1.0f / sqrtf(float(n_embd_head)));
  8139. cb(Qcur, "Qcur_scaled", il);
  8140. cur = build_attn(inp_attn,
  8141. model.layers[il].wo, NULL,
  8142. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f, il);
  8143. }
  8144. if (il == n_layer - 1 && inp_out_ids) {
  8145. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  8146. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  8147. }
  8148. ggml_tensor * sa_out = ggml_add(ctx0, cur, inpL);
  8149. cb(sa_out, "sa_out", il);
  8150. cur = build_norm(sa_out,
  8151. model.layers[il].ffn_norm, NULL,
  8152. LLM_NORM_RMS, il);
  8153. cb(cur, "ffn_norm", il);
  8154. // feed-forward network
  8155. {
  8156. cur = build_ffn(cur,
  8157. model.layers[il].ffn_up, NULL, NULL,
  8158. model.layers[il].ffn_gate, NULL, NULL,
  8159. model.layers[il].ffn_down, NULL, NULL,
  8160. NULL,
  8161. LLM_FFN_GELU, LLM_FFN_PAR, il);
  8162. cb(cur, "ffn_out", il);
  8163. }
  8164. cur = ggml_add(ctx0, cur, sa_out);
  8165. cur = build_cvec(cur, il);
  8166. cb(cur, "l_out", il);
  8167. // input for next layer
  8168. inpL = cur;
  8169. }
  8170. cur = inpL;
  8171. cur = build_norm(cur,
  8172. model.output_norm, NULL,
  8173. LLM_NORM_RMS, -1);
  8174. cb(cur, "result_norm", -1);
  8175. res->t_embd = cur;
  8176. // lm_head
  8177. cur = build_lora_mm(model.output, cur);
  8178. cb(cur, "result_output", -1);
  8179. res->t_logits = cur;
  8180. ggml_build_forward_expand(gf, cur);
  8181. }
  8182. };
  8183. struct llm_build_gemma2_iswa : public llm_graph_context {
  8184. llm_build_gemma2_iswa(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  8185. const int64_t n_embd_head = hparams.n_embd_head_k;
  8186. ggml_tensor * cur;
  8187. ggml_tensor * inpL;
  8188. inpL = build_inp_embd(model.tok_embd);
  8189. inpL = ggml_scale(ctx0, inpL, sqrtf(n_embd));
  8190. cb(inpL, "inp_scaled", -1);
  8191. // inp_pos - contains the positions
  8192. ggml_tensor * inp_pos = build_inp_pos();
  8193. auto * inp_attn = build_attn_inp_kv_unified_iswa();
  8194. ggml_tensor * inp_out_ids = build_inp_out_ids();
  8195. for (int il = 0; il < n_layer; ++il) {
  8196. // norm
  8197. cur = build_norm(inpL,
  8198. model.layers[il].attn_norm, NULL,
  8199. LLM_NORM_RMS, il);
  8200. cb(cur, "attn_norm", il);
  8201. // self-attention
  8202. {
  8203. // compute Q and K and RoPE them
  8204. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  8205. cb(Qcur, "Qcur", il);
  8206. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  8207. cb(Kcur, "Kcur", il);
  8208. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  8209. cb(Vcur, "Vcur", il);
  8210. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  8211. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  8212. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  8213. Qcur = ggml_rope_ext(
  8214. ctx0, Qcur, inp_pos, nullptr,
  8215. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  8216. ext_factor, attn_factor, beta_fast, beta_slow);
  8217. Kcur = ggml_rope_ext(
  8218. ctx0, Kcur, inp_pos, nullptr,
  8219. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  8220. ext_factor, attn_factor, beta_fast, beta_slow);
  8221. cb(Qcur, "Qcur", il);
  8222. cb(Kcur, "Kcur", il);
  8223. cb(Vcur, "Vcur", il);
  8224. Qcur = ggml_scale(ctx0, Qcur, hparams.f_attention_scale);
  8225. cur = build_attn(inp_attn,
  8226. model.layers[il].wo, NULL,
  8227. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f, il);
  8228. }
  8229. if (il == n_layer - 1 && inp_out_ids) {
  8230. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  8231. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  8232. }
  8233. cur = build_norm(cur,
  8234. model.layers[il].attn_post_norm, NULL,
  8235. LLM_NORM_RMS, il);
  8236. cb(cur, "attn_post_norm", il);
  8237. ggml_tensor * sa_out = ggml_add(ctx0, cur, inpL);
  8238. cb(sa_out, "sa_out", il);
  8239. cur = build_norm(sa_out,
  8240. model.layers[il].ffn_norm, NULL,
  8241. LLM_NORM_RMS, il);
  8242. cb(cur, "ffn_norm", il);
  8243. // feed-forward network
  8244. {
  8245. cur = build_ffn(cur,
  8246. model.layers[il].ffn_up, NULL, NULL,
  8247. model.layers[il].ffn_gate, NULL, NULL,
  8248. model.layers[il].ffn_down, NULL, NULL,
  8249. NULL,
  8250. LLM_FFN_GELU, LLM_FFN_PAR, il);
  8251. cb(cur, "ffn_out", il);
  8252. }
  8253. cur = build_norm(cur,
  8254. model.layers[il].ffn_post_norm, NULL,
  8255. LLM_NORM_RMS, -1);
  8256. cb(cur, "ffn_post_norm", -1);
  8257. cur = ggml_add(ctx0, cur, sa_out);
  8258. cur = build_cvec(cur, il);
  8259. cb(cur, "l_out", il);
  8260. // input for next layer
  8261. inpL = cur;
  8262. }
  8263. cur = inpL;
  8264. cur = build_norm(cur,
  8265. model.output_norm, NULL,
  8266. LLM_NORM_RMS, -1);
  8267. cb(cur, "result_norm", -1);
  8268. res->t_embd = cur;
  8269. // lm_head
  8270. cur = build_lora_mm(model.output, cur);
  8271. // final logit soft-capping
  8272. cur = ggml_scale(ctx0, cur, 1.0f / hparams.f_final_logit_softcapping);
  8273. cur = ggml_tanh(ctx0, cur);
  8274. cur = ggml_scale(ctx0, cur, hparams.f_final_logit_softcapping);
  8275. cb(cur, "result_output", -1);
  8276. res->t_logits = cur;
  8277. ggml_build_forward_expand(gf, cur);
  8278. }
  8279. };
  8280. struct llm_build_gemma3_iswa : public llm_graph_context {
  8281. llm_build_gemma3_iswa(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  8282. const int64_t n_embd_head = hparams.n_embd_head_k;
  8283. ggml_tensor * cur;
  8284. ggml_tensor * inpL;
  8285. inpL = build_inp_embd(model.tok_embd);
  8286. // important: do not normalize weights for raw embeddings input (i.e. encoded image emdeddings)
  8287. if (ubatch.token) {
  8288. inpL = ggml_scale(ctx0, inpL, sqrtf(n_embd));
  8289. cb(inpL, "inp_scaled", -1);
  8290. }
  8291. // inp_pos - contains the positions
  8292. ggml_tensor * inp_pos = build_inp_pos();
  8293. // TODO: is causal == true correct? might need some changes
  8294. auto * inp_attn = build_attn_inp_kv_unified_iswa();
  8295. ggml_tensor * inp_out_ids = build_inp_out_ids();
  8296. for (int il = 0; il < n_layer; ++il) {
  8297. const float freq_base_l = model.get_rope_freq_base (cparams, il);
  8298. const float freq_scale_l = model.get_rope_freq_scale(cparams, il);
  8299. // norm
  8300. cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
  8301. cb(cur, "attn_norm", il);
  8302. // self-attention
  8303. {
  8304. // compute Q and K and RoPE them
  8305. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  8306. cb(Qcur, "Qcur", il);
  8307. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  8308. cb(Kcur, "Kcur", il);
  8309. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  8310. cb(Vcur, "Vcur", il);
  8311. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  8312. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  8313. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  8314. Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);
  8315. cb(Qcur, "Qcur_normed", il);
  8316. Qcur = ggml_rope_ext(
  8317. ctx0, Qcur, inp_pos, nullptr,
  8318. n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l,
  8319. ext_factor, attn_factor, beta_fast, beta_slow);
  8320. Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);
  8321. cb(Kcur, "Kcur_normed", il);
  8322. Kcur = ggml_rope_ext(
  8323. ctx0, Kcur, inp_pos, nullptr,
  8324. n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l,
  8325. ext_factor, attn_factor, beta_fast, beta_slow);
  8326. cb(Qcur, "Qcur", il);
  8327. cb(Kcur, "Kcur", il);
  8328. cb(Vcur, "Vcur", il);
  8329. // ref: https://github.com/google/gemma_pytorch/blob/014acb7ac4563a5f77c76d7ff98f31b568c16508/gemma/model.py#L315
  8330. Qcur = ggml_scale(ctx0, Qcur, hparams.f_attention_scale);
  8331. cur = build_attn(inp_attn,
  8332. model.layers[il].wo, NULL,
  8333. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f, il);
  8334. }
  8335. if (il == n_layer - 1 && inp_out_ids) {
  8336. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  8337. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  8338. }
  8339. cur = build_norm(cur,
  8340. model.layers[il].attn_post_norm, NULL,
  8341. LLM_NORM_RMS, il);
  8342. cb(cur, "attn_post_norm", il);
  8343. ggml_tensor * sa_out = ggml_add(ctx0, cur, inpL);
  8344. cb(sa_out, "sa_out", il);
  8345. cur = build_norm(sa_out,
  8346. model.layers[il].ffn_norm, NULL,
  8347. LLM_NORM_RMS, il);
  8348. cb(cur, "ffn_norm", il);
  8349. // feed-forward network
  8350. {
  8351. cur = build_ffn(cur,
  8352. model.layers[il].ffn_up, NULL, NULL,
  8353. model.layers[il].ffn_gate, NULL, NULL,
  8354. model.layers[il].ffn_down, NULL, NULL,
  8355. NULL,
  8356. LLM_FFN_GELU, LLM_FFN_PAR, il);
  8357. cb(cur, "ffn_out", il);
  8358. }
  8359. cur = build_norm(cur,
  8360. model.layers[il].ffn_post_norm, NULL,
  8361. LLM_NORM_RMS, -1);
  8362. cb(cur, "ffn_post_norm", -1);
  8363. cur = ggml_add(ctx0, cur, sa_out);
  8364. cur = build_cvec(cur, il);
  8365. cb(cur, "l_out", il);
  8366. // input for next layer
  8367. inpL = cur;
  8368. }
  8369. cur = inpL;
  8370. cur = build_norm(cur,
  8371. model.output_norm, NULL,
  8372. LLM_NORM_RMS, -1);
  8373. cb(cur, "result_norm", -1);
  8374. res->t_embd = cur;
  8375. // lm_head
  8376. cur = build_lora_mm(model.output, cur);
  8377. cb(cur, "result_output", -1);
  8378. res->t_logits = cur;
  8379. ggml_build_forward_expand(gf, cur);
  8380. }
  8381. };
  8382. struct llm_build_gemma3n_iswa : public llm_graph_context {
  8383. const llama_model & model;
  8384. const int64_t n_embd_head;
  8385. const int64_t n_embd_altup;
  8386. const int64_t n_altup;
  8387. const int i_altup_act;
  8388. const int n_layer_kv = 20; // number of layers having KV [KV_REUSE]
  8389. const int n_layer_sparsity = 10; // number of layers using activation sparsity
  8390. const float f_sparsity_std_mul = 1.6448533535003662f; // std_multiplier = normal_dist.icdf(0.95)
  8391. llm_build_gemma3n_iswa(const llama_model & model, const llm_graph_params & params)
  8392. : llm_graph_context(params),
  8393. model(model),
  8394. n_embd_head(model.hparams.n_embd_head_k),
  8395. n_embd_altup(model.hparams.n_embd_altup),
  8396. n_altup(model.hparams.n_altup),
  8397. i_altup_act(model.hparams.i_altup_act) {
  8398. ggml_tensor * cur;
  8399. ggml_tensor * inpL;
  8400. inpL = build_inp_embd(model.tok_embd);
  8401. // important: do not normalize weights for raw embeddings input (i.e. encoded image emdeddings)
  8402. if (ubatch.token) {
  8403. inpL = ggml_scale(ctx0, inpL, sqrtf(n_embd));
  8404. cb(inpL, "inp_scaled", -1);
  8405. }
  8406. // inp_pos - contains the positions
  8407. ggml_tensor * inp_pos = build_inp_pos();
  8408. // TODO: is causal == true correct? might need some changes
  8409. auto * inp_attn = build_attn_inp_kv_unified_iswa();
  8410. // inp_per_layer shape: [n_embd_altup, n_tokens, n_layer]
  8411. ggml_tensor * inp_per_layer = project_per_layer_inputs(inpL, get_per_layer_inputs());
  8412. // inpL now has only 1 altup, project it to the rest of the altups
  8413. // these "added" altups will be concat to the last dim of inpL
  8414. {
  8415. ggml_tensor * target_magnitude = calc_magnitude(inpL);
  8416. ggml_tensor * inp_repeated = ggml_repeat_4d(ctx0, inpL, n_embd, n_tokens, n_altup - 1, 1);
  8417. ggml_tensor * altup_added = ggml_mul_mat(ctx0, model.altup_proj, inp_repeated); // shape: [n_embd, n_tokens, n_altup - 1]
  8418. ggml_tensor * new_magnitude = calc_magnitude(altup_added);
  8419. altup_added = ggml_div(ctx0,
  8420. ggml_mul(ctx0, altup_added, target_magnitude),
  8421. new_magnitude);
  8422. inpL = ggml_concat(ctx0, inpL, altup_added, 2); // shape: [n_embd, n_tokens, n_altup]
  8423. cb(inpL, "inp_stacked", -1);
  8424. }
  8425. // inpL now has shape: [n_embd, n_tokens, n_altup]
  8426. // inp_per_layer now has shape: [n_embd_altup, n_tokens, n_layer]
  8427. for (int il = 0; il < n_layer; ++il) {
  8428. // this block is made to be closely resemble Gemma3p5DecoderLayer on python code
  8429. const bool has_kv = (il < n_layer_kv);
  8430. const float freq_base_l = model.get_rope_freq_base (cparams, il);
  8431. const float freq_scale_l = model.get_rope_freq_scale(cparams, il);
  8432. ggml_tensor * cur = inpL; // [n_embd, n_tokens, n_altup]
  8433. ggml_tensor * predictions = altup_predict(cur, il); // [n_embd, n_tokens, n_altup]
  8434. // predicted value will go through self-attention and laurel
  8435. ggml_tensor * active_prediction = view_2d_slice(predictions, i_altup_act); // [n_embd, n_tokens]
  8436. cur = active_prediction;
  8437. cb(cur, "active_prediction", il);
  8438. // norm
  8439. cur = build_norm(cur, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
  8440. cb(cur, "attn_norm", il);
  8441. // laurel
  8442. ggml_tensor * laurel_out = laurel(cur, il); // [n_embd, n_tokens]
  8443. // self-attention
  8444. if (has_kv) {
  8445. // compute Q and K and RoPE them
  8446. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  8447. cb(Qcur, "Qcur", il);
  8448. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  8449. cb(Kcur, "Kcur", il);
  8450. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  8451. cb(Vcur, "Vcur", il);
  8452. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  8453. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  8454. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  8455. Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);
  8456. Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);
  8457. Vcur = ggml_rms_norm(ctx0, Vcur, hparams.f_norm_rms_eps);
  8458. cb(Qcur, "Qcur_normed", il);
  8459. cb(Kcur, "Kcur_normed", il);
  8460. cb(Vcur, "Vcur_normed", il);
  8461. Qcur = ggml_rope_ext(
  8462. ctx0, Qcur, inp_pos, nullptr,
  8463. n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l,
  8464. ext_factor, attn_factor, beta_fast, beta_slow);
  8465. Kcur = ggml_rope_ext(
  8466. ctx0, Kcur, inp_pos, nullptr,
  8467. n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l,
  8468. ext_factor, attn_factor, beta_fast, beta_slow);
  8469. cb(Qcur, "Qcur_pos", il);
  8470. cb(Kcur, "Kcur_pos", il);
  8471. cur = build_attn(inp_attn,
  8472. model.layers[il].wo, NULL,
  8473. Qcur, Kcur, Vcur, nullptr, nullptr, hparams.f_attention_scale, il);
  8474. } else {
  8475. // no KV layers
  8476. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  8477. cb(Qcur, "Qcur", il);
  8478. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  8479. Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);
  8480. cb(Qcur, "Qcur_normed", il);
  8481. Qcur = ggml_rope_ext(
  8482. ctx0, Qcur, inp_pos, nullptr,
  8483. n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l,
  8484. ext_factor, attn_factor, beta_fast, beta_slow);
  8485. cb(Qcur, "Qcur_pos", il);
  8486. cur = build_attn(inp_attn,
  8487. model.layers[il].wo, NULL,
  8488. Qcur, nullptr, nullptr, nullptr, nullptr, hparams.f_attention_scale, il);
  8489. }
  8490. cur = build_norm(cur,
  8491. model.layers[il].attn_post_norm, NULL,
  8492. LLM_NORM_RMS, il);
  8493. cb(cur, "attn_post_norm", il);
  8494. cur = ggml_add(ctx0, cur, active_prediction); // [n_embd, n_tokens]
  8495. cb(cur, "attn_gated", il);
  8496. ggml_tensor * attn_laurel = ggml_scale(ctx0,
  8497. ggml_add(ctx0, cur, laurel_out),
  8498. 1.0f / sqrtf(2.0f)); // [n_embd, n_tokens]
  8499. cb(attn_laurel, "attn_laurel", il);
  8500. cur = build_norm(attn_laurel,
  8501. model.layers[il].ffn_norm, NULL,
  8502. LLM_NORM_RMS, il);
  8503. cb(cur, "ffn_norm", il);
  8504. // feed-forward network
  8505. {
  8506. ggml_tensor * up_proj = build_lora_mm(model.layers[il].ffn_up, cur);
  8507. ggml_tensor * gate_proj = build_lora_mm(model.layers[il].ffn_gate, cur);
  8508. if (il < n_layer_sparsity) {
  8509. // apply activation sparsity
  8510. gate_proj = gaussian_topk(gate_proj);
  8511. }
  8512. gate_proj = ggml_gelu(ctx0, gate_proj);
  8513. cur = ggml_mul(ctx0, up_proj, gate_proj);
  8514. cur = build_lora_mm(model.layers[il].ffn_down, cur);
  8515. cb(cur, "ffn_out", il);
  8516. }
  8517. cur = build_norm(cur,
  8518. model.layers[il].ffn_post_norm, NULL,
  8519. LLM_NORM_RMS, -1);
  8520. cb(cur, "ffn_post_norm", il);
  8521. ggml_tensor * attn_ffw_laurel_gated = ggml_add(ctx0, cur, attn_laurel); // [n_embd, n_tokens]
  8522. cb(attn_ffw_laurel_gated, "attn_ffw_laurel_gated", il);
  8523. ggml_tensor * corrected = altup_correct(predictions, attn_ffw_laurel_gated, il); // [n_embd, n_tokens, n_altup]
  8524. ggml_tensor * first_prediction; // [n_embd, n_tokens]
  8525. {
  8526. first_prediction = view_2d_slice(corrected, i_altup_act); // [n_embd, n_tokens]
  8527. first_prediction = ggml_mul(ctx0, first_prediction, model.layers[il].altup_correct_scale);
  8528. first_prediction = build_lora_mm(model.layers[il].per_layer_inp_gate, first_prediction);
  8529. first_prediction = ggml_gelu(ctx0, first_prediction); // [n_embd_altup, n_tokens]
  8530. cb(first_prediction, "first_prediction_gated", il);
  8531. ggml_tensor * inp_this_layer = view_2d_slice(inp_per_layer, il); // [n_embd_altup, n_tokens]
  8532. first_prediction = ggml_mul(ctx0, first_prediction, inp_this_layer); // [n_embd_altup, n_tokens]
  8533. cb(first_prediction, "first_prediction_scaled", il);
  8534. first_prediction = build_lora_mm(model.layers[il].per_layer_proj, first_prediction); // [n_embd, n_tokens]
  8535. first_prediction = build_norm(first_prediction,
  8536. model.layers[il].per_layer_post_norm, NULL,
  8537. LLM_NORM_RMS, il);
  8538. cb(first_prediction, "first_prediction_out", il);
  8539. }
  8540. // equivalent to python code: corrected_predictions[1:] += first_prediction
  8541. {
  8542. ggml_tensor * slice_first = view_2d_slice(corrected, 0);
  8543. ggml_tensor * slice_rest = ggml_view_3d(ctx0, corrected, n_embd, n_tokens, n_altup - 1,
  8544. ggml_row_size(corrected->type, n_embd),
  8545. ggml_row_size(corrected->type, n_embd*n_tokens),
  8546. n_embd*n_tokens*ggml_element_size(corrected));
  8547. ggml_tensor * tmp = ggml_add(ctx0, slice_rest, first_prediction); // [n_embd, n_tokens, n_altup - 1]
  8548. corrected = ggml_concat(ctx0, slice_first, tmp, 2); // [n_embd, n_tokens, n_altup]
  8549. }
  8550. cur = corrected; // [n_embd, n_tokens, n_altup]
  8551. cur = build_cvec(cur, il);
  8552. cb(cur, "l_out", il);
  8553. // input for next layer
  8554. inpL = cur;
  8555. }
  8556. cur = inpL; // [n_embd, n_tokens, n_altup]
  8557. // cur now has multiple altup(s), we want to merge them back to 1 altup
  8558. {
  8559. ggml_tensor * target_magnitude = calc_magnitude(view_2d_slice(cur, i_altup_act)); // [n_embd, n_tokens]
  8560. // do a view to skip the first slice (active altup)
  8561. ggml_tensor * alt_slice = ggml_view_3d(ctx0, cur, n_embd, n_tokens, n_altup - 1,
  8562. ggml_row_size(cur->type, n_embd),
  8563. ggml_row_size(cur->type, n_embd*n_tokens),
  8564. n_embd*n_tokens*ggml_element_size(cur));
  8565. ggml_tensor * altup_unembd = ggml_mul_mat(ctx0, model.altup_unembd_proj, alt_slice); // shape: [n_embd, n_tokens, n_altup - 1]
  8566. ggml_tensor * new_magnitude = calc_magnitude(altup_unembd);
  8567. altup_unembd = ggml_div(ctx0,
  8568. ggml_mul(ctx0, altup_unembd, target_magnitude),
  8569. new_magnitude);
  8570. cb(altup_unembd, "altup_unembd", -1);
  8571. // equivalent to torch.mean(hidden_states, dim=0)
  8572. cur = view_2d_slice(cur, 0); // [n_embd, n_tokens]
  8573. for (int i = 0; i < n_altup - 1; ++i) {
  8574. cur = ggml_add(ctx0, cur, view_2d_slice(altup_unembd, i));
  8575. }
  8576. cur = ggml_scale(ctx0, cur, 1.0f / float(n_altup)); // [n_embd, n_tokens]
  8577. cb(cur, "unembd_merged", -1);
  8578. }
  8579. // cur now has shape: [n_embd, n_tokens]
  8580. // TODO: move this to right after the last KV layer
  8581. {
  8582. // skip computing output for unused tokens
  8583. ggml_tensor * inp_out_ids = build_inp_out_ids();
  8584. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  8585. }
  8586. cur = build_norm(cur,
  8587. model.output_norm, NULL,
  8588. LLM_NORM_RMS, -1);
  8589. cb(cur, "result_norm", -1);
  8590. res->t_embd = cur;
  8591. cur = build_lora_mm(model.output, cur);
  8592. {
  8593. // final logit soft-capping
  8594. cur = ggml_scale(ctx0, cur, 1.0f / hparams.f_final_logit_softcapping);
  8595. cur = ggml_tanh(ctx0, cur);
  8596. cur = ggml_scale(ctx0, cur, hparams.f_final_logit_softcapping);
  8597. }
  8598. cb(cur, "result_output", -1);
  8599. res->t_logits = cur;
  8600. ggml_build_forward_expand(gf, cur);
  8601. }
  8602. ggml_tensor * calc_magnitude(ggml_tensor * x) {
  8603. return ggml_sqrt(ctx0, ggml_sum_rows(ctx0, ggml_sqr(ctx0, x)));
  8604. }
  8605. // get 2D slice view from a 3D tensor, the idx corresponds to the 3rd dim
  8606. ggml_tensor * view_2d_slice(ggml_tensor * x, int idx) {
  8607. GGML_ASSERT(idx < (int)x->ne[2]);
  8608. return ggml_view_2d(ctx0, x, x->ne[0], x->ne[1],
  8609. ggml_row_size(x->type, x->ne[0]),
  8610. idx * x->ne[0] * x->ne[1] * ggml_element_size(x));
  8611. }
  8612. // equivalent to get_per_layer_inputs() in python code
  8613. // output shape: [n_embd_altup, n_layer, n_tokens]
  8614. ggml_tensor * get_per_layer_inputs() {
  8615. auto inp = std::make_unique<llm_graph_input_embd>();
  8616. ggml_tensor * inp_per_layer;
  8617. if (ubatch.token) {
  8618. inp->tokens = ggml_new_tensor_1d(ctx0, GGML_TYPE_I32, ubatch.n_tokens);
  8619. ggml_set_input(inp->tokens);
  8620. res->t_tokens = inp->tokens;
  8621. inp_per_layer = ggml_get_rows(ctx0, model.tok_embd_per_layer, inp->tokens);
  8622. inp_per_layer = ggml_reshape_3d(ctx0, inp_per_layer, n_embd_altup, n_layer, n_tokens);
  8623. inp_per_layer = ggml_scale(ctx0, inp_per_layer, sqrtf((float)n_embd_altup));
  8624. cb(inp_per_layer, "inp_per_layer_selected", -1);
  8625. } else {
  8626. GGML_ABORT("TODO: support embd input");
  8627. }
  8628. res->add_input(std::move(inp));
  8629. return inp_per_layer;
  8630. }
  8631. // equivalent to project_per_layer_inputs() in python code
  8632. // this calculates the per-layer inputs, so the final tensor shape will have n_layer as the last dim
  8633. // output shape: [n_embd_altup, n_tokens, n_layer]
  8634. ggml_tensor * project_per_layer_inputs(ggml_tensor * inputs_embeds, ggml_tensor * inp_per_layer) {
  8635. const float per_layer_projection_scale = 1.0f / sqrtf((float)n_embd);
  8636. const float per_layer_input_scale = 1.0f / sqrtf(2.0f);
  8637. ggml_tensor * per_layer_proj = ggml_mul_mat(ctx0, model.per_layer_model_proj, inputs_embeds);
  8638. per_layer_proj = ggml_scale(ctx0, per_layer_proj, per_layer_projection_scale);
  8639. per_layer_proj = ggml_reshape_3d(ctx0, per_layer_proj, n_embd_altup, n_layer, n_tokens);
  8640. per_layer_proj = build_norm(per_layer_proj,
  8641. model.per_layer_proj_norm, NULL,
  8642. LLM_NORM_RMS, -1); // [n_embd_altup, n_layer, n_tokens]
  8643. cb(per_layer_proj, "per_layer_proj", -1);
  8644. inp_per_layer = ggml_add(ctx0, inp_per_layer, per_layer_proj);
  8645. inp_per_layer = ggml_scale(ctx0, inp_per_layer, per_layer_input_scale);
  8646. cb(inp_per_layer, "inp_per_layer", -1);
  8647. // permute to shape: [n_embd_altup, n_tokens, n_layer]
  8648. inp_per_layer = ggml_cont(ctx0, ggml_permute(ctx0, inp_per_layer, 0, 2, 1, 3));
  8649. return inp_per_layer;
  8650. }
  8651. // input cur shape: [n_altup, n_tokens]
  8652. // output shape: [n_altup, n_tokens]
  8653. ggml_tensor * laurel(ggml_tensor * cur, int il) {
  8654. ggml_tensor * tmp = cur;
  8655. tmp = build_lora_mm(model.layers[il].laurel_l, tmp);
  8656. tmp = build_lora_mm(model.layers[il].laurel_r, tmp);
  8657. tmp = build_norm(tmp, model.layers[il].laurel_post_norm, NULL, LLM_NORM_RMS, il);
  8658. tmp = ggml_add(ctx0, tmp, cur);
  8659. cb(tmp, "laurel_out", il);
  8660. return tmp;
  8661. }
  8662. // input x shape: [n_embd, n_tokens]
  8663. // output shape: [n_embd, n_tokens]
  8664. ggml_tensor * gaussian_topk(ggml_tensor * x) {
  8665. ggml_tensor * mean = ggml_mean(ctx0, x);
  8666. ggml_tensor * std = ggml_sqrt(ctx0, ggml_scale(ctx0,
  8667. ggml_sum_rows(ctx0, ggml_sqr(ctx0, ggml_sub(ctx0, x, mean))),
  8668. 1.0f / (float)(x->ne[0] - 1)
  8669. ));
  8670. ggml_tensor * cutoff_x = ggml_add(ctx0, mean, ggml_scale(ctx0, std, f_sparsity_std_mul));
  8671. return ggml_relu(ctx0, ggml_sub(ctx0, x, cutoff_x));
  8672. }
  8673. //
  8674. // altup functions
  8675. //
  8676. // equivalent to compute_router_modalities() in python code
  8677. // input x shape: [n_embd, n_tokens]
  8678. // output shape: [n_altup, n_tokens]
  8679. ggml_tensor * altup_compute_router_modalities(ggml_tensor * x, int il) {
  8680. ggml_tensor * router_inputs = build_norm(x,
  8681. model.layers[il].altup_router_norm, NULL,
  8682. LLM_NORM_RMS, il);
  8683. // router_input_scale
  8684. router_inputs = ggml_scale(ctx0, router_inputs, 1.0f / (float)n_embd);
  8685. ggml_tensor * output = ggml_mul_mat(ctx0, model.layers[il].altup_router, router_inputs);
  8686. return ggml_tanh(ctx0, output); // [n_altup, n_tokens]
  8687. }
  8688. // input cur shape: [n_embd, n_tokens, n_altup]
  8689. // output shape: [n_embd, n_tokens, n_altup]
  8690. ggml_tensor * altup_predict(ggml_tensor * cur, int il) {
  8691. ggml_tensor * activated = view_2d_slice(cur, i_altup_act); // [n_embd, n_tokens]
  8692. ggml_tensor * modalities = altup_compute_router_modalities(activated, il); // [n_altup, n_tokens]
  8693. cb(modalities, "modalities", il);
  8694. ggml_tensor * all_coefs = build_lora_mm(model.layers[il].altup_predict_coef, modalities);
  8695. cb(all_coefs, "all_coefs", il);
  8696. // first dim now having n_altup^2 elements, we reshape it to 2D (so we end up with 3D tensor)
  8697. all_coefs = ggml_reshape_3d(ctx0, all_coefs, n_altup, n_altup, n_tokens);
  8698. // permute to [n_altup, n_embd, n_tokens]
  8699. ggml_tensor * cur_permuted = ggml_cont(ctx0, ggml_permute(ctx0, cur, 1, 2, 0, 3));
  8700. ggml_tensor * predictions = ggml_mul_mat(ctx0, cur_permuted, all_coefs); // [n_altup, n_embd, n_tokens]
  8701. // final shape must be the same as cur: [n_embd, n_tokens, n_altup]
  8702. predictions = ggml_cont(ctx0, ggml_permute(ctx0, predictions, 0, 2, 1, 3));
  8703. predictions = ggml_add(ctx0, predictions, cur);
  8704. cb(predictions, "predictions", il);
  8705. return predictions;
  8706. }
  8707. // input predictions shape: [n_embd, n_tokens, n_altup]
  8708. // input activated shape: [n_embd, n_tokens]
  8709. // output shape: [n_embd, n_tokens, n_altup]
  8710. ggml_tensor * altup_correct(ggml_tensor * predictions, ggml_tensor * activated, int il) {
  8711. ggml_tensor * modalities = altup_compute_router_modalities(activated, il); // [n_altup, n_tokens]
  8712. cb(modalities, "modalities", il);
  8713. ggml_tensor * active_prediction = view_2d_slice(predictions, i_altup_act);
  8714. ggml_tensor * innovation = ggml_sub(ctx0, activated, active_prediction); // [n_embd, n_tokens]
  8715. cb(innovation, "innovation", il);
  8716. ggml_tensor * all_coefs = build_lora_mm(model.layers[il].altup_correct_coef, modalities); // [n_altup, n_tokens]
  8717. all_coefs = ggml_scale_bias(ctx0, all_coefs, 1.0f, 1.0f); // + 1.0
  8718. cb(all_coefs, "all_coefs", il);
  8719. all_coefs = ggml_cont(ctx0, ggml_transpose(ctx0, all_coefs)); // [n_tokens, n_altup]
  8720. all_coefs = ggml_reshape_3d(ctx0, all_coefs, 1, n_tokens, n_altup); // [1, n_tokens, n_altup]
  8721. innovation = ggml_repeat_4d(ctx0, innovation, n_embd, n_tokens, n_altup, 1);
  8722. ggml_tensor * corrected = ggml_mul(ctx0, innovation, all_coefs); // [n_embd, n_tokens, n_altup]
  8723. corrected = ggml_add(ctx0, corrected, predictions); // [n_embd, n_tokens, n_altup]
  8724. cb(corrected, "corrected", il);
  8725. return corrected;
  8726. }
  8727. };
  8728. // TODO: move up next to build_starcoder
  8729. struct llm_build_starcoder2 : public llm_graph_context {
  8730. llm_build_starcoder2(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  8731. const int64_t n_embd_head = hparams.n_embd_head_v;
  8732. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  8733. GGML_ASSERT(n_embd_head == hparams.n_rot);
  8734. ggml_tensor * cur;
  8735. ggml_tensor * inpL;
  8736. inpL = build_inp_embd(model.tok_embd);
  8737. // inp_pos - contains the positions
  8738. ggml_tensor * inp_pos = build_inp_pos();
  8739. auto * inp_attn = build_attn_inp_kv_unified();
  8740. ggml_tensor * inp_out_ids = build_inp_out_ids();
  8741. for (int il = 0; il < n_layer; ++il) {
  8742. ggml_tensor * inpSA = inpL;
  8743. // norm
  8744. cur = build_norm(inpL,
  8745. model.layers[il].attn_norm, model.layers[il].attn_norm_b,
  8746. LLM_NORM, il);
  8747. cb(cur, "attn_norm", il);
  8748. // self-attention
  8749. {
  8750. // compute Q and K and RoPE them
  8751. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  8752. cb(Qcur, "Qcur", il);
  8753. if (model.layers[il].bq) {
  8754. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  8755. cb(Qcur, "Qcur", il);
  8756. }
  8757. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  8758. cb(Kcur, "Kcur", il);
  8759. if (model.layers[il].bk) {
  8760. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  8761. cb(Kcur, "Kcur", il);
  8762. }
  8763. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  8764. cb(Vcur, "Vcur", il);
  8765. if (model.layers[il].bv) {
  8766. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  8767. cb(Vcur, "Vcur", il);
  8768. }
  8769. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  8770. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  8771. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  8772. Qcur = ggml_rope_ext(
  8773. ctx0, Qcur, inp_pos, nullptr,
  8774. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  8775. ext_factor, attn_factor, beta_fast, beta_slow
  8776. );
  8777. Kcur = ggml_rope_ext(
  8778. ctx0, Kcur, inp_pos, nullptr,
  8779. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  8780. ext_factor, attn_factor, beta_fast, beta_slow
  8781. );
  8782. cb(Qcur, "Qcur", il);
  8783. cb(Kcur, "Kcur", il);
  8784. cb(Vcur, "Vcur", il);
  8785. cur = build_attn(inp_attn,
  8786. model.layers[il].wo, model.layers[il].bo,
  8787. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  8788. }
  8789. if (il == n_layer - 1 && inp_out_ids) {
  8790. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  8791. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  8792. }
  8793. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  8794. cb(ffn_inp, "ffn_inp", il);
  8795. // feed-forward network
  8796. cur = build_norm(ffn_inp,
  8797. model.layers[il].ffn_norm, model.layers[il].ffn_norm_b,
  8798. LLM_NORM, il);
  8799. cb(cur, "ffn_norm", il);
  8800. cur = build_ffn(cur,
  8801. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  8802. NULL, NULL, NULL,
  8803. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  8804. NULL,
  8805. LLM_FFN_GELU, LLM_FFN_SEQ, il);
  8806. cb(cur, "ffn_out", il);
  8807. cur = ggml_add(ctx0, cur, ffn_inp);
  8808. cur = build_cvec(cur, il);
  8809. cb(cur, "l_out", il);
  8810. // input for next layer
  8811. inpL = cur;
  8812. }
  8813. cur = inpL;
  8814. cur = build_norm(cur,
  8815. model.output_norm, model.output_norm_b,
  8816. LLM_NORM, -1);
  8817. cb(cur, "result_norm", -1);
  8818. res->t_embd = cur;
  8819. // lm_head
  8820. cur = build_lora_mm(model.output, cur);
  8821. cb(cur, "result_output", -1);
  8822. res->t_logits = cur;
  8823. ggml_build_forward_expand(gf, cur);
  8824. }
  8825. };
  8826. struct llm_graph_context_mamba : public llm_graph_context {
  8827. llm_graph_context_mamba(const llm_graph_params & params) : llm_graph_context(params) {}
  8828. ggml_tensor * build_mamba_layer(
  8829. llm_graph_input_rs * inp,
  8830. ggml_tensor * cur,
  8831. const llama_model & model,
  8832. const llama_ubatch & ubatch,
  8833. int il) {
  8834. const auto * mctx_cur = inp->mctx;
  8835. const auto kv_head = mctx_cur->get_head();
  8836. const auto & layer = model.layers[il];
  8837. const int64_t d_conv = hparams.ssm_d_conv;
  8838. const int64_t d_inner = hparams.ssm_d_inner;
  8839. const int64_t d_state = hparams.ssm_d_state;
  8840. const int64_t dt_rank = hparams.ssm_dt_rank;
  8841. const int64_t n_head = d_inner;
  8842. const int64_t head_dim = 1;
  8843. const int64_t n_seqs = ubatch.n_seqs;
  8844. // Some variants of Mamba arch (e.g. FalconMamba do apply layer norm on B and Dt layers)
  8845. const bool ssm_dt_b_c_rms = hparams.ssm_dt_b_c_rms;
  8846. const int64_t n_seq_tokens = ubatch.n_seq_tokens;
  8847. GGML_ASSERT(n_seqs != 0);
  8848. GGML_ASSERT(ubatch.equal_seqs());
  8849. GGML_ASSERT(ubatch.n_tokens == n_seq_tokens * n_seqs);
  8850. ggml_tensor * conv_states_all = mctx_cur->get_r_l(il);
  8851. ggml_tensor * ssm_states_all = mctx_cur->get_s_l(il);
  8852. ggml_tensor * conv = build_rs(inp, conv_states_all, hparams.n_embd_r(), n_seqs);
  8853. conv = ggml_reshape_3d(ctx0, conv, d_conv - 1, d_inner, n_seqs);
  8854. // {n_embd, n_tokens} => {n_embd, n_seq_tokens, n_seqs}
  8855. cur = ggml_reshape_3d(ctx0, cur, cur->ne[0], n_seq_tokens, n_seqs);
  8856. // {n_embd, 2*d_inner} @ {n_embd, n_seq_tokens, n_seqs} => {2*d_inner, n_seq_tokens, n_seqs}
  8857. ggml_tensor * xz = build_lora_mm(layer.ssm_in, cur);
  8858. // split the above in two
  8859. // => {d_inner, n_seq_tokens, n_seqs}
  8860. ggml_tensor * x = ggml_view_3d(ctx0, xz, d_inner, xz->ne[1], xz->ne[2], xz->nb[1], xz->nb[2], 0);
  8861. ggml_tensor * z = ggml_view_3d(ctx0, xz, d_inner, xz->ne[1], xz->ne[2], xz->nb[1], xz->nb[2], d_inner*ggml_element_size(xz));
  8862. // conv
  8863. {
  8864. // => {d_conv - 1 + n_seq_tokens, d_inner, n_seqs}
  8865. ggml_tensor * conv_x = ggml_concat(ctx0, conv, ggml_transpose(ctx0, x), 0);
  8866. // copy last (d_conv - 1) columns back into the state cache
  8867. ggml_tensor * last_conv = ggml_view_3d(ctx0, conv_x, d_conv - 1, d_inner, n_seqs, conv_x->nb[1], conv_x->nb[2], n_seq_tokens*(conv_x->nb[0]));
  8868. ggml_build_forward_expand(gf,
  8869. ggml_cpy(ctx0, last_conv,
  8870. ggml_view_1d(ctx0, conv_states_all,
  8871. (d_conv - 1)*(d_inner)*(n_seqs),
  8872. kv_head*(d_conv - 1)*(d_inner)*ggml_element_size(conv_states_all))));
  8873. // 1D convolution
  8874. // The equivalent is to make a self-overlapping view of conv_x
  8875. // over d_conv columns at each stride in the 3rd dimension,
  8876. // then element-wise multiply that with the conv1d weight,
  8877. // then sum the elements of each row,
  8878. // (the last two steps are a dot product over rows (also doable with mul_mat))
  8879. // then permute away the ne[0] dimension,
  8880. // and then you're left with the resulting x tensor.
  8881. // For simultaneous sequences, all sequences need to have the same length.
  8882. x = ggml_ssm_conv(ctx0, conv_x, layer.ssm_conv1d);
  8883. // bias
  8884. x = ggml_add(ctx0, x, layer.ssm_conv1d_b);
  8885. x = ggml_silu(ctx0, x);
  8886. }
  8887. // ssm
  8888. {
  8889. // {d_inner, dt_rank + 2*d_state} @ {d_inner, n_seq_tokens, n_seqs} => {dt_rank + 2*d_state, n_seq_tokens, n_seqs}
  8890. ggml_tensor * x_db = build_lora_mm(layer.ssm_x, x);
  8891. // split
  8892. ggml_tensor * dt = ggml_view_3d(ctx0, x_db, dt_rank, n_seq_tokens, n_seqs, x_db->nb[1], x_db->nb[2], 0);
  8893. ggml_tensor * B = ggml_view_4d(ctx0, x_db, d_state, /* n_group */ 1, n_seq_tokens, n_seqs, d_state*x_db->nb[0], x_db->nb[1], x_db->nb[2], ggml_element_size(x_db)*dt_rank);
  8894. ggml_tensor * C = ggml_view_4d(ctx0, x_db, d_state, /* n_group */ 1, n_seq_tokens, n_seqs, d_state*x_db->nb[0], x_db->nb[1], x_db->nb[2], ggml_element_size(x_db)*(dt_rank+d_state));
  8895. // Some Mamba variants (e.g. FalconMamba, Jamba) apply RMS norm in B, C & Dt layers
  8896. if (ssm_dt_b_c_rms || (layer.ssm_dt_norm && layer.ssm_b_norm && layer.ssm_c_norm)) {
  8897. dt = build_norm(dt, layer.ssm_dt_norm, NULL, LLM_NORM_RMS, il);
  8898. B = build_norm(B, layer.ssm_b_norm, NULL, LLM_NORM_RMS, il);
  8899. C = build_norm(C, layer.ssm_c_norm, NULL, LLM_NORM_RMS, il);
  8900. }
  8901. // {dt_rank, d_inner} @ {dt_rank, n_seq_tokens, n_seqs} => {d_inner, n_seq_tokens, n_seqs}
  8902. dt = build_lora_mm(layer.ssm_dt, dt);
  8903. dt = ggml_add(ctx0, dt, layer.ssm_dt_b);
  8904. cur = x;
  8905. x = ggml_reshape_4d(ctx0, x, head_dim, n_head, n_seq_tokens, n_seqs);
  8906. ggml_tensor * A = layer.ssm_a;
  8907. // use the states and the indices provided by build_recurrent_state
  8908. // (this is necessary in order to properly use the states before they are overwritten,
  8909. // while avoiding to make unnecessary copies of the states)
  8910. auto get_ssm_rows = [&](ggml_context * ctx, ggml_tensor * states, ggml_tensor * ids) {
  8911. ggml_tensor * ssm = ggml_reshape_4d(ctx, states, d_state, head_dim, n_head, mctx_cur->get_size());
  8912. // Custom operator to optimize the parallel associative scan
  8913. // as described in the Annex D of the Mamba paper.
  8914. // => {d_inner, n_seq_tokens, n_seqs} and {d_state, d_inner, n_seqs}
  8915. return ggml_ssm_scan(ctx, ssm, x, dt, A, B, C, ids);
  8916. };
  8917. ggml_tensor * y_ssm = build_rs(inp, ssm_states_all, hparams.n_embd_s(), ubatch.n_seqs, get_ssm_rows);
  8918. // store last states
  8919. ggml_build_forward_expand(gf,
  8920. ggml_cpy(ctx0,
  8921. ggml_view_1d(ctx0, y_ssm, d_state*d_inner*n_seqs, x->nb[3]*x->ne[3]),
  8922. ggml_view_1d(ctx0, ssm_states_all, d_state*d_inner*n_seqs, kv_head*d_state*d_inner*ggml_element_size(ssm_states_all))));
  8923. ggml_tensor * y = ggml_view_3d(ctx0, y_ssm, d_inner, n_seq_tokens, n_seqs, x->nb[2], x->nb[3], 0);
  8924. // TODO: skip computing output earlier for unused tokens
  8925. y = ggml_add(ctx0, y, ggml_mul(ctx0, cur, layer.ssm_d));
  8926. y = ggml_swiglu_split(ctx0, ggml_cont(ctx0, z), y);
  8927. // {d_inner, n_embd} @ {d_inner, n_seq_tokens, n_seqs} => {n_embd, n_seq_tokens, n_seqs}
  8928. cur = build_lora_mm(layer.ssm_out, y);
  8929. }
  8930. // {n_embd, n_seq_tokens, n_seqs} => {n_embd, n_tokens}
  8931. cur = ggml_reshape_2d(ctx0, cur, cur->ne[0], n_seq_tokens * n_seqs);
  8932. return cur;
  8933. }
  8934. ggml_tensor * build_mamba2_layer(
  8935. llm_graph_input_rs * inp,
  8936. ggml_tensor * cur,
  8937. const llama_model & model,
  8938. const llama_ubatch & ubatch,
  8939. int il) const {
  8940. const auto * mctx_cur = inp->mctx;
  8941. const auto kv_head = mctx_cur->get_head();
  8942. const int64_t d_conv = hparams.ssm_d_conv;
  8943. const int64_t d_inner = hparams.ssm_d_inner;
  8944. const int64_t d_state = hparams.ssm_d_state;
  8945. const int64_t n_head = hparams.ssm_dt_rank;
  8946. const int64_t head_dim = d_inner / n_head;
  8947. const int64_t n_group = hparams.ssm_n_group;
  8948. const int64_t n_seqs = ubatch.n_seqs;
  8949. const int64_t n_seq_tokens = ubatch.n_seq_tokens;
  8950. GGML_ASSERT(n_seqs != 0);
  8951. GGML_ASSERT(ubatch.equal_seqs());
  8952. GGML_ASSERT(ubatch.n_tokens == n_seq_tokens * n_seqs);
  8953. ggml_tensor * conv_states_all = mctx_cur->get_r_l(il);
  8954. ggml_tensor * ssm_states_all = mctx_cur->get_s_l(il);
  8955. ggml_tensor * conv = build_rs(inp, conv_states_all, hparams.n_embd_r(), n_seqs);
  8956. conv = ggml_reshape_3d(ctx0, conv, d_conv - 1, d_inner + 2*n_group*d_state, n_seqs);
  8957. // {n_embd, n_tokens} => {n_embd, n_seq_tokens, n_seqs}
  8958. cur = ggml_reshape_3d(ctx0, cur, cur->ne[0], n_seq_tokens, n_seqs);
  8959. // d_in_proj = 2 * self.d_inner + 2 * self.ngroups * self.d_state + self.nheads
  8960. // {n_embd, d_in_proj} @ {n_embd, n_seq_tokens, n_seqs} => {d_in_proj, n_seq_tokens, n_seqs}
  8961. ggml_tensor * zxBCdt = build_lora_mm(model.layers[il].ssm_in, cur);
  8962. // split the above in three
  8963. ggml_tensor * z = ggml_view_4d(ctx0, zxBCdt, head_dim, n_head, n_seq_tokens, n_seqs, head_dim*zxBCdt->nb[0], zxBCdt->nb[1], zxBCdt->nb[2], 0);
  8964. ggml_tensor * xBC = ggml_view_3d(ctx0, zxBCdt, d_inner + 2*n_group*d_state, n_seq_tokens, n_seqs, zxBCdt->nb[1], zxBCdt->nb[2], d_inner*ggml_element_size(zxBCdt));
  8965. ggml_tensor * dt = ggml_view_3d(ctx0, zxBCdt, n_head, n_seq_tokens, n_seqs, zxBCdt->nb[1], zxBCdt->nb[2], (2*d_inner + 2*n_group*d_state)*ggml_element_size(zxBCdt));
  8966. // conv
  8967. {
  8968. // => {d_conv - 1 + n_seq_tokens, d_inner + 2*n_group*d_state, n_seqs}
  8969. ggml_tensor * conv_x = ggml_concat(ctx0, conv, ggml_transpose(ctx0, xBC), 0);
  8970. // copy last (d_conv - 1) columns back into the state cache
  8971. ggml_tensor * last_conv = ggml_view_3d(ctx0, conv_x, d_conv - 1, d_inner + 2*n_group*d_state, n_seqs, conv_x->nb[1], conv_x->nb[2], n_seq_tokens*(conv_x->nb[0]));
  8972. ggml_build_forward_expand(gf,
  8973. ggml_cpy(ctx0, last_conv,
  8974. ggml_view_1d(ctx0, conv_states_all,
  8975. (d_conv - 1)*(d_inner + 2*n_group*d_state)*(n_seqs),
  8976. kv_head*(d_conv - 1)*(d_inner + 2*n_group*d_state)*ggml_element_size(conv_states_all))));
  8977. // 1D convolution
  8978. // The equivalent is to make a self-overlapping view of conv_x
  8979. // over d_conv columns at each stride in the 3rd dimension,
  8980. // then element-wise multiply that with the conv1d weight,
  8981. // then sum the elements of each row,
  8982. // (the last two steps are a dot product over rows (also doable with mul_mat))
  8983. // then permute away the ne[0] dimension,
  8984. // and then you're left with the resulting x tensor.
  8985. // For simultaneous sequences, all sequences need to have the same length.
  8986. xBC = ggml_ssm_conv(ctx0, conv_x, model.layers[il].ssm_conv1d);
  8987. // bias
  8988. xBC = ggml_add(ctx0, xBC, model.layers[il].ssm_conv1d_b);
  8989. xBC = ggml_silu(ctx0, xBC);
  8990. }
  8991. // ssm
  8992. {
  8993. // These correspond to V K Q in SSM/attention duality
  8994. ggml_tensor * x = ggml_view_4d(ctx0, xBC, head_dim, n_head, n_seq_tokens, n_seqs, head_dim*xBC->nb[0], xBC->nb[1], xBC->nb[2], 0);
  8995. ggml_tensor * B = ggml_view_4d(ctx0, xBC, d_state, n_group, n_seq_tokens, n_seqs, d_state*xBC->nb[0], xBC->nb[1], xBC->nb[2], d_inner*ggml_element_size(xBC));
  8996. ggml_tensor * C = ggml_view_4d(ctx0, xBC, d_state, n_group, n_seq_tokens, n_seqs, d_state*xBC->nb[0], xBC->nb[1], xBC->nb[2], (d_inner + n_group*d_state)*ggml_element_size(xBC));
  8997. // {n_head, n_seq_tokens, n_seqs}
  8998. dt = ggml_add(ctx0, ggml_cont(ctx0, dt), model.layers[il].ssm_dt_b);
  8999. ggml_tensor * A = model.layers[il].ssm_a;
  9000. // use the states and the indices provided by build_recurrent_state
  9001. // (this is necessary in order to properly use the states before they are overwritten,
  9002. // while avoiding to make unnecessary copies of the states)
  9003. auto get_ssm_rows = [&](ggml_context * ctx, ggml_tensor * states, ggml_tensor * ids) {
  9004. ggml_tensor * ssm = ggml_reshape_4d(ctx, states, d_state, head_dim, n_head, mctx_cur->get_size());
  9005. // TODO: use semistructured matrices to implement state-space duality
  9006. // => {d_inner, n_seq_tokens, n_seqs} and {d_state, d_inner, n_seqs}
  9007. return ggml_ssm_scan(ctx, ssm, x, dt, A, B, C, ids);
  9008. };
  9009. ggml_tensor * y_ssm = build_rs(inp, ssm_states_all, hparams.n_embd_s(), ubatch.n_seqs, get_ssm_rows);
  9010. // store last states
  9011. ggml_build_forward_expand(gf,
  9012. ggml_cpy(ctx0,
  9013. ggml_view_1d(ctx0, y_ssm, d_state*d_inner*n_seqs, ggml_nelements(x)*x->nb[0]),
  9014. ggml_view_1d(ctx0, ssm_states_all, d_state*d_inner*n_seqs, kv_head*d_state*d_inner*ggml_element_size(ssm_states_all))));
  9015. ggml_tensor * y = ggml_view_4d(ctx0, y_ssm, head_dim, n_head, n_seq_tokens, n_seqs, x->nb[1], n_head*x->nb[1], n_seq_tokens*n_head*x->nb[1], 0);
  9016. // TODO: skip computing output earlier for unused tokens
  9017. y = ggml_add(ctx0, y, ggml_mul(ctx0, x, model.layers[il].ssm_d));
  9018. y = ggml_swiglu_split(ctx0, ggml_cont(ctx0, z), y);
  9019. // grouped RMS norm
  9020. if (model.layers[il].ssm_norm) {
  9021. y = ggml_reshape_4d(ctx0, y, d_inner / n_group, n_group, n_seq_tokens, n_seqs);
  9022. y = build_norm(y, model.layers[il].ssm_norm, NULL, LLM_NORM_RMS, il);
  9023. }
  9024. y = ggml_reshape_3d(ctx0, y, d_inner, n_seq_tokens, n_seqs);
  9025. // {d_inner, n_embd} @ {d_inner, n_seq_tokens, n_seqs} => {n_embd, n_seq_tokens, n_seqs}
  9026. cur = build_lora_mm(model.layers[il].ssm_out, y);
  9027. }
  9028. // {n_embd, n_seq_tokens, n_seqs} => {n_embd, n_tokens}
  9029. cur = ggml_reshape_2d(ctx0, cur, cur->ne[0], n_seq_tokens * n_seqs);
  9030. cb(cur, "mamba_out", il);
  9031. return cur;
  9032. }
  9033. };
  9034. struct llm_build_mamba : public llm_graph_context_mamba {
  9035. llm_build_mamba(const llama_model & model, const llm_graph_params & params) : llm_graph_context_mamba(params) {
  9036. ggml_tensor * cur;
  9037. ggml_tensor * inpL;
  9038. // {n_embd, n_tokens}
  9039. inpL = build_inp_embd(model.tok_embd);
  9040. auto * rs_inp = build_rs_inp();
  9041. ggml_tensor * inp_out_ids = build_inp_out_ids();
  9042. for (int il = 0; il < n_layer; ++il) {
  9043. // norm
  9044. cur = build_norm(inpL,
  9045. model.layers[il].attn_norm, NULL,
  9046. LLM_NORM_RMS, il);
  9047. cb(cur, "attn_norm", il);
  9048. if (model.arch == LLM_ARCH_MAMBA2) {
  9049. cur = build_mamba2_layer(rs_inp, cur, model, ubatch, il);
  9050. } else {
  9051. cur = build_mamba_layer(rs_inp, cur, model, ubatch, il);
  9052. }
  9053. if (il == n_layer - 1 && inp_out_ids) {
  9054. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  9055. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  9056. }
  9057. // residual
  9058. cur = ggml_add(ctx0, cur, inpL);
  9059. cur = build_cvec(cur, il);
  9060. cb(cur, "l_out", il);
  9061. // input for next layer
  9062. inpL = cur;
  9063. }
  9064. // final rmsnorm
  9065. cur = build_norm(inpL, model.output_norm, NULL, LLM_NORM_RMS, -1);
  9066. cb(cur, "result_norm", -1);
  9067. res->t_embd = cur;
  9068. // lm_head
  9069. cur = build_lora_mm(model.output, cur);
  9070. cb(cur, "result_output", -1);
  9071. res->t_logits = cur;
  9072. ggml_build_forward_expand(gf, cur);
  9073. }
  9074. };
  9075. struct llm_build_jamba : public llm_graph_context_mamba {
  9076. llm_build_jamba(const llama_model & model, const llm_graph_params & params) : llm_graph_context_mamba(params) {
  9077. const int64_t n_embd_head = hparams.n_embd_head_v;
  9078. ggml_tensor * cur;
  9079. ggml_tensor * inpL;
  9080. // {n_embd, n_tokens}
  9081. inpL = build_inp_embd(model.tok_embd);
  9082. auto * inp_hybrid = build_inp_mem_hybrid();
  9083. ggml_tensor * inp_out_ids = build_inp_out_ids();
  9084. for (int il = 0; il < n_layer; ++il) {
  9085. const int64_t n_head_kv = hparams.n_head_kv(il);
  9086. cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
  9087. cb(cur, "attn_norm", il);
  9088. if (n_head_kv == 0) {
  9089. cur = build_mamba_layer(inp_hybrid->get_recr(), cur, model, ubatch, il);
  9090. } else {
  9091. // Attention
  9092. struct ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  9093. struct ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  9094. struct ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  9095. cb(Qcur, "Qcur", il);
  9096. cb(Kcur, "Kcur", il);
  9097. cb(Vcur, "Vcur", il);
  9098. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  9099. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  9100. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  9101. cb(Qcur, "Qcur", il);
  9102. cb(Kcur, "Kcur", il);
  9103. cb(Vcur, "Vcur", il);
  9104. // No RoPE :)
  9105. cur = build_attn(inp_hybrid->get_attn(), model.layers[il].wo, NULL, Qcur, Kcur, Vcur, NULL, NULL, 1.0f/sqrtf(float(n_embd_head)), il);
  9106. }
  9107. if (il == n_layer - 1 && inp_out_ids) {
  9108. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  9109. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  9110. }
  9111. // residual
  9112. struct ggml_tensor * ffn_inp = ggml_add(ctx0, inpL, cur);
  9113. cb(cur, "ffn_inp", il);
  9114. cur = build_norm(ffn_inp, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);
  9115. cb(cur, "ffn_norm", il);
  9116. // feed-forward network
  9117. if (model.layers[il].ffn_gate_inp == nullptr) {
  9118. // FFN
  9119. cur = build_ffn(cur,
  9120. model.layers[il].ffn_up, NULL, NULL,
  9121. model.layers[il].ffn_gate, NULL, NULL,
  9122. model.layers[il].ffn_down, NULL, NULL,
  9123. NULL,
  9124. LLM_FFN_SILU, LLM_FFN_PAR, il);
  9125. cb(cur, "ffn_out", il);
  9126. } else {
  9127. // MoE branch
  9128. cur = build_moe_ffn(cur,
  9129. model.layers[il].ffn_gate_inp,
  9130. model.layers[il].ffn_up_exps,
  9131. model.layers[il].ffn_gate_exps,
  9132. model.layers[il].ffn_down_exps,
  9133. nullptr,
  9134. n_expert, n_expert_used,
  9135. LLM_FFN_SILU, false,
  9136. false, 0.0,
  9137. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  9138. il);
  9139. cb(cur, "ffn_moe_out", il);
  9140. }
  9141. // residual
  9142. cur = ggml_add(ctx0, ffn_inp, cur);
  9143. cur = build_cvec(cur, il);
  9144. cb(cur, "l_out", il);
  9145. // input for next layer
  9146. inpL = cur;
  9147. }
  9148. // final rmsnorm
  9149. cur = build_norm(inpL, model.output_norm, NULL, LLM_NORM_RMS, -1);
  9150. cb(cur, "result_norm", -1);
  9151. res->t_embd = cur;
  9152. // lm_head
  9153. cur = build_lora_mm(model.output, cur);
  9154. cb(cur, "result_output", -1);
  9155. res->t_logits = cur;
  9156. ggml_build_forward_expand(gf, cur);
  9157. }
  9158. };
  9159. struct llm_build_command_r : public llm_graph_context {
  9160. llm_build_command_r(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  9161. const int64_t n_embd_head = hparams.n_embd_head_v;
  9162. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  9163. const float f_logit_scale = hparams.f_logit_scale;
  9164. ggml_tensor * cur;
  9165. ggml_tensor * inpL;
  9166. inpL = build_inp_embd(model.tok_embd);
  9167. // inp_pos - contains the positions
  9168. ggml_tensor * inp_pos = build_inp_pos();
  9169. auto * inp_attn = build_attn_inp_kv_unified();
  9170. ggml_tensor * inp_out_ids = build_inp_out_ids();
  9171. for (int il = 0; il < n_layer; ++il) {
  9172. // norm
  9173. cur = build_norm(inpL,
  9174. model.layers[il].attn_norm, NULL,
  9175. LLM_NORM, il);
  9176. cb(cur, "attn_norm", il);
  9177. ggml_tensor * ffn_inp = cur;
  9178. // self-attention
  9179. {
  9180. // compute Q and K and RoPE them
  9181. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  9182. cb(Qcur, "Qcur", il);
  9183. if (model.layers[il].bq) {
  9184. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  9185. cb(Qcur, "Qcur", il);
  9186. }
  9187. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  9188. cb(Kcur, "Kcur", il);
  9189. if (model.layers[il].bk) {
  9190. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  9191. cb(Kcur, "Kcur", il);
  9192. }
  9193. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  9194. cb(Vcur, "Vcur", il);
  9195. if (model.layers[il].bv) {
  9196. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  9197. cb(Vcur, "Vcur", il);
  9198. }
  9199. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  9200. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  9201. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  9202. if (model.layers[il].attn_q_norm) {
  9203. Qcur = build_norm(Qcur,
  9204. model.layers[il].attn_q_norm,
  9205. NULL,
  9206. LLM_NORM, il);
  9207. cb(Qcur, "Qcur", il);
  9208. }
  9209. Qcur = ggml_rope_ext(
  9210. ctx0, Qcur, inp_pos, nullptr,
  9211. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  9212. ext_factor, attn_factor, beta_fast, beta_slow
  9213. );
  9214. if (model.layers[il].attn_k_norm) {
  9215. Kcur = build_norm(Kcur,
  9216. model.layers[il].attn_k_norm,
  9217. NULL,
  9218. LLM_NORM, il);
  9219. cb(Kcur, "Kcur", il);
  9220. }
  9221. Kcur = ggml_rope_ext(
  9222. ctx0, Kcur, inp_pos, nullptr,
  9223. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  9224. ext_factor, attn_factor, beta_fast, beta_slow
  9225. );
  9226. cb(Qcur, "Qcur", il);
  9227. cb(Kcur, "Kcur", il);
  9228. cb(Vcur, "Vcur", il);
  9229. cur = build_attn(inp_attn,
  9230. model.layers[il].wo, model.layers[il].bo,
  9231. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  9232. }
  9233. if (il == n_layer - 1 && inp_out_ids) {
  9234. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  9235. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  9236. ffn_inp = ggml_get_rows(ctx0, ffn_inp, inp_out_ids);
  9237. }
  9238. ggml_tensor * attn_out = cur;
  9239. // feed-forward network
  9240. {
  9241. cur = build_ffn(ffn_inp,
  9242. model.layers[il].ffn_up, NULL, NULL,
  9243. model.layers[il].ffn_gate, NULL, NULL,
  9244. model.layers[il].ffn_down, NULL, NULL,
  9245. NULL,
  9246. LLM_FFN_SILU, LLM_FFN_PAR, il);
  9247. cb(cur, "ffn_out", il);
  9248. }
  9249. // add together residual + FFN + self-attention
  9250. cur = ggml_add(ctx0, cur, inpL);
  9251. cur = ggml_add(ctx0, cur, attn_out);
  9252. cur = build_cvec(cur, il);
  9253. cb(cur, "l_out", il);
  9254. // input for next layer
  9255. inpL = cur;
  9256. }
  9257. cur = inpL;
  9258. cur = build_norm(cur,
  9259. model.output_norm, NULL,
  9260. LLM_NORM, -1);
  9261. cb(cur, "result_norm", -1);
  9262. res->t_embd = cur;
  9263. // lm_head
  9264. cur = build_lora_mm(model.output, cur);
  9265. if (f_logit_scale) {
  9266. cur = ggml_scale(ctx0, cur, f_logit_scale);
  9267. }
  9268. cb(cur, "result_output", -1);
  9269. res->t_logits = cur;
  9270. ggml_build_forward_expand(gf, cur);
  9271. }
  9272. };
  9273. struct llm_build_cohere2_iswa : public llm_graph_context {
  9274. llm_build_cohere2_iswa(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  9275. const int64_t n_embd_head = hparams.n_embd_head_v;
  9276. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  9277. const float f_logit_scale = hparams.f_logit_scale;
  9278. ggml_tensor * cur;
  9279. ggml_tensor * inpL;
  9280. inpL = build_inp_embd(model.tok_embd);
  9281. // inp_pos - contains the positions
  9282. ggml_tensor * inp_pos = build_inp_pos();
  9283. auto * inp_attn = build_attn_inp_kv_unified_iswa();
  9284. ggml_tensor * inp_out_ids = build_inp_out_ids();
  9285. for (int il = 0; il < n_layer; ++il) {
  9286. const bool is_swa = hparams.is_swa(il);
  9287. // norm
  9288. cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM, il);
  9289. cb(cur, "attn_norm", il);
  9290. ggml_tensor * ffn_inp = cur;
  9291. // self-attention
  9292. {
  9293. // rope freq factors for 128k context
  9294. ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
  9295. // compute Q and K and RoPE them
  9296. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  9297. cb(Qcur, "Qcur", il);
  9298. if (model.layers[il].bq) {
  9299. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  9300. cb(Qcur, "Qcur", il);
  9301. }
  9302. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  9303. cb(Kcur, "Kcur", il);
  9304. if (model.layers[il].bk) {
  9305. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  9306. cb(Kcur, "Kcur", il);
  9307. }
  9308. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  9309. cb(Vcur, "Vcur", il);
  9310. if (model.layers[il].bv) {
  9311. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  9312. cb(Vcur, "Vcur", il);
  9313. }
  9314. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  9315. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  9316. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  9317. if (is_swa) {
  9318. Qcur = ggml_rope_ext(
  9319. ctx0, Qcur, inp_pos, rope_factors,
  9320. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  9321. ext_factor, attn_factor, beta_fast, beta_slow
  9322. );
  9323. Kcur = ggml_rope_ext(
  9324. ctx0, Kcur, inp_pos, rope_factors,
  9325. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  9326. ext_factor, attn_factor, beta_fast, beta_slow
  9327. );
  9328. }
  9329. cb(Qcur, "Qcur", il);
  9330. cb(Kcur, "Kcur", il);
  9331. cb(Vcur, "Vcur", il);
  9332. cur = build_attn(inp_attn,
  9333. model.layers[il].wo, model.layers[il].bo,
  9334. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  9335. }
  9336. if (il == n_layer - 1 && inp_out_ids) {
  9337. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  9338. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  9339. ffn_inp = ggml_get_rows(ctx0, ffn_inp, inp_out_ids);
  9340. }
  9341. ggml_tensor * attn_out = cur;
  9342. // feed-forward network
  9343. {
  9344. cur = build_ffn(ffn_inp, model.layers[il].ffn_up, NULL, NULL, model.layers[il].ffn_gate,
  9345. NULL, NULL, model.layers[il].ffn_down, NULL, NULL, NULL, LLM_FFN_SILU, LLM_FFN_PAR,
  9346. il);
  9347. cb(cur, "ffn_out", il);
  9348. }
  9349. // add together residual + FFN + self-attention
  9350. cur = ggml_add(ctx0, cur, inpL);
  9351. cur = ggml_add(ctx0, cur, attn_out);
  9352. cur = build_cvec(cur, il);
  9353. cb(cur, "l_out", il);
  9354. // input for next layer
  9355. inpL = cur;
  9356. }
  9357. cur = inpL;
  9358. cur = build_norm(cur, model.output_norm, NULL, LLM_NORM, -1);
  9359. cb(cur, "result_norm", -1);
  9360. res->t_embd = cur;
  9361. // lm_head
  9362. cur = build_lora_mm(model.output, cur);
  9363. if (f_logit_scale) {
  9364. cur = ggml_scale(ctx0, cur, f_logit_scale);
  9365. }
  9366. cb(cur, "result_output", -1);
  9367. res->t_logits = cur;
  9368. ggml_build_forward_expand(gf, cur);
  9369. }
  9370. };
  9371. // ref: https://allenai.org/olmo
  9372. // based on the original build_llama() function, changes:
  9373. // * non-parametric layer norm
  9374. // * clamp qkv
  9375. // * removed bias
  9376. // * removed MoE
  9377. struct llm_build_olmo : public llm_graph_context {
  9378. llm_build_olmo(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  9379. const int64_t n_embd_head = hparams.n_embd_head_v;
  9380. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  9381. GGML_ASSERT(n_embd_head == hparams.n_rot);
  9382. ggml_tensor * cur;
  9383. ggml_tensor * inpL;
  9384. inpL = build_inp_embd(model.tok_embd);
  9385. // inp_pos - contains the positions
  9386. ggml_tensor * inp_pos = build_inp_pos();
  9387. auto * inp_attn = build_attn_inp_kv_unified();
  9388. ggml_tensor * inp_out_ids = build_inp_out_ids();
  9389. for (int il = 0; il < n_layer; ++il) {
  9390. ggml_tensor * inpSA = inpL;
  9391. // norm
  9392. cur = build_norm(inpL,
  9393. NULL, NULL,
  9394. LLM_NORM, il);
  9395. cb(cur, "attn_norm", il);
  9396. // self-attention
  9397. {
  9398. // compute Q and K and RoPE them
  9399. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  9400. cb(Qcur, "Qcur", il);
  9401. if (hparams.f_clamp_kqv > 0.0f) {
  9402. Qcur = ggml_clamp(ctx0, Qcur, -hparams.f_clamp_kqv, hparams.f_clamp_kqv);
  9403. cb(Qcur, "Qcur", il);
  9404. }
  9405. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  9406. cb(Kcur, "Kcur", il);
  9407. if (hparams.f_clamp_kqv > 0.0f) {
  9408. Kcur = ggml_clamp(ctx0, Kcur, -hparams.f_clamp_kqv, hparams.f_clamp_kqv);
  9409. cb(Kcur, "Kcur", il);
  9410. }
  9411. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  9412. cb(Vcur, "Vcur", il);
  9413. if (hparams.f_clamp_kqv > 0.0f) {
  9414. Vcur = ggml_clamp(ctx0, Vcur, -hparams.f_clamp_kqv, hparams.f_clamp_kqv);
  9415. cb(Vcur, "Vcur", il);
  9416. }
  9417. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  9418. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  9419. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  9420. Qcur = ggml_rope_ext(
  9421. ctx0, Qcur, inp_pos, nullptr,
  9422. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  9423. ext_factor, attn_factor, beta_fast, beta_slow
  9424. );
  9425. Kcur = ggml_rope_ext(
  9426. ctx0, Kcur, inp_pos, nullptr,
  9427. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  9428. ext_factor, attn_factor, beta_fast, beta_slow
  9429. );
  9430. cb(Qcur, "Qcur", il);
  9431. cb(Kcur, "Kcur", il);
  9432. cb(Vcur, "Vcur", il);
  9433. cur = build_attn(inp_attn,
  9434. model.layers[il].wo, nullptr,
  9435. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  9436. }
  9437. if (il == n_layer - 1 && inp_out_ids) {
  9438. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  9439. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  9440. }
  9441. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  9442. cb(ffn_inp, "ffn_inp", il);
  9443. // feed-forward network
  9444. cur = build_norm(ffn_inp,
  9445. NULL, NULL,
  9446. LLM_NORM, il);
  9447. cb(cur, "ffn_norm", il);
  9448. cur = build_ffn(cur,
  9449. model.layers[il].ffn_up, NULL, NULL,
  9450. model.layers[il].ffn_gate, NULL, NULL,
  9451. model.layers[il].ffn_down, NULL, NULL,
  9452. NULL,
  9453. LLM_FFN_SILU, LLM_FFN_PAR, il);
  9454. cb(cur, "ffn_out", il);
  9455. cur = ggml_add(ctx0, cur, ffn_inp);
  9456. cb(cur, "ffn_out", il);
  9457. cur = build_cvec(cur, il);
  9458. cb(cur, "l_out", il);
  9459. // input for next layer
  9460. inpL = cur;
  9461. }
  9462. cur = inpL;
  9463. cur = build_norm(cur,
  9464. NULL, NULL,
  9465. LLM_NORM, -1);
  9466. cb(cur, "result_norm", -1);
  9467. res->t_embd = cur;
  9468. // lm_head
  9469. cur = build_lora_mm(model.output, cur);
  9470. cb(cur, "result_output", -1);
  9471. res->t_logits = cur;
  9472. ggml_build_forward_expand(gf, cur);
  9473. }
  9474. };
  9475. struct llm_build_olmo2 : public llm_graph_context {
  9476. llm_build_olmo2(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  9477. const int64_t n_embd_head = hparams.n_embd_head_v;
  9478. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  9479. GGML_ASSERT(n_embd_head == hparams.n_rot);
  9480. ggml_tensor * cur;
  9481. ggml_tensor * inpL;
  9482. inpL = build_inp_embd(model.tok_embd);
  9483. // inp_pos - contains the positions
  9484. ggml_tensor * inp_pos = build_inp_pos();
  9485. auto * inp_attn = build_attn_inp_kv_unified();
  9486. ggml_tensor * inp_out_ids = build_inp_out_ids();
  9487. for (int il = 0; il < n_layer; ++il) {
  9488. ggml_tensor * inpSA = inpL;
  9489. cur = inpL;
  9490. // self_attention
  9491. {
  9492. // compute Q and K and RoPE them
  9493. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  9494. cb(Qcur, "Qcur", il);
  9495. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  9496. cb(Kcur, "Kcur", il);
  9497. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  9498. cb(Vcur, "Vcur", il);
  9499. Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL,
  9500. LLM_NORM_RMS, il);
  9501. cb(Qcur, "Qcur_normed", il);
  9502. Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL,
  9503. LLM_NORM_RMS, il);
  9504. cb(Kcur, "Kcur_normed", il);
  9505. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  9506. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  9507. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  9508. Qcur = ggml_rope_ext(
  9509. ctx0, Qcur, inp_pos, nullptr,
  9510. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  9511. ext_factor, attn_factor, beta_fast, beta_slow
  9512. );
  9513. Kcur = ggml_rope_ext(
  9514. ctx0, Kcur, inp_pos, nullptr,
  9515. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  9516. ext_factor, attn_factor, beta_fast, beta_slow
  9517. );
  9518. cb(Qcur, "Qcur", il);
  9519. cb(Kcur, "Kcur", il);
  9520. cb(Vcur, "Vcur", il);
  9521. cur = build_attn(inp_attn,
  9522. model.layers[il].wo, NULL,
  9523. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  9524. }
  9525. if (il == n_layer - 1 && inp_out_ids) {
  9526. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  9527. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  9528. }
  9529. cur = build_norm(cur,
  9530. model.layers[il].attn_post_norm, NULL,
  9531. LLM_NORM_RMS, il);
  9532. cb(cur, "attn_post_norm", il);
  9533. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  9534. cb(ffn_inp, "ffn_inp", il);
  9535. // feed-forward network
  9536. cur = build_ffn(ffn_inp,
  9537. model.layers[il].ffn_up, NULL, NULL,
  9538. model.layers[il].ffn_gate, NULL, NULL,
  9539. model.layers[il].ffn_down, NULL, NULL,
  9540. NULL,
  9541. LLM_FFN_SILU, LLM_FFN_PAR, il);
  9542. cb(cur, "ffn_out", il);
  9543. cur = build_norm(cur,
  9544. model.layers[il].ffn_post_norm, NULL,
  9545. LLM_NORM_RMS, -1);
  9546. cb(cur, "ffn_post_norm", -1);
  9547. cur = ggml_add(ctx0, cur, ffn_inp);
  9548. cb(cur, "ffn_out", il);
  9549. cur = build_cvec(cur, il);
  9550. cb(cur, "l_out", il);
  9551. // input for next layer
  9552. inpL = cur;
  9553. }
  9554. cur = inpL;
  9555. cur = build_norm(cur,
  9556. model.output_norm, NULL,
  9557. LLM_NORM_RMS, -1);
  9558. cb(cur, "result_norm", -1);
  9559. res->t_embd = cur;
  9560. // lm_head
  9561. cur = build_lora_mm(model.output, cur);
  9562. cb(cur, "result_output", -1);
  9563. res->t_logits = cur;
  9564. ggml_build_forward_expand(gf, cur);
  9565. }
  9566. };
  9567. // based on the build_qwen2moe() function, changes:
  9568. // * removed shared experts
  9569. // * removed bias
  9570. // * added q, k norm
  9571. struct llm_build_olmoe : public llm_graph_context {
  9572. llm_build_olmoe(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  9573. const int64_t n_embd_head = hparams.n_embd_head_v;
  9574. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  9575. GGML_ASSERT(n_embd_head == hparams.n_rot);
  9576. ggml_tensor * cur;
  9577. ggml_tensor * inpL;
  9578. inpL = build_inp_embd(model.tok_embd);
  9579. // inp_pos - contains the positions
  9580. ggml_tensor * inp_pos = build_inp_pos();
  9581. auto * inp_attn = build_attn_inp_kv_unified();
  9582. ggml_tensor * inp_out_ids = build_inp_out_ids();
  9583. for (int il = 0; il < n_layer; ++il) {
  9584. ggml_tensor * inpSA = inpL;
  9585. // norm
  9586. cur = build_norm(inpL,
  9587. model.layers[il].attn_norm, NULL,
  9588. LLM_NORM_RMS, il);
  9589. cb(cur, "attn_norm", il);
  9590. // self_attention
  9591. {
  9592. // compute Q and K and RoPE them
  9593. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  9594. cb(Qcur, "Qcur", il);
  9595. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  9596. cb(Kcur, "Kcur", il);
  9597. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  9598. cb(Vcur, "Vcur", il);
  9599. Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL,
  9600. LLM_NORM_RMS, il);
  9601. cb(Qcur, "Qcur_normed", il);
  9602. Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL,
  9603. LLM_NORM_RMS, il);
  9604. cb(Kcur, "Kcur_normed", il);
  9605. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  9606. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  9607. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  9608. Qcur = ggml_rope_ext(
  9609. ctx0, Qcur, inp_pos, nullptr,
  9610. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  9611. ext_factor, attn_factor, beta_fast, beta_slow
  9612. );
  9613. Kcur = ggml_rope_ext(
  9614. ctx0, Kcur, inp_pos, nullptr,
  9615. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  9616. ext_factor, attn_factor, beta_fast, beta_slow
  9617. );
  9618. cb(Qcur, "Qcur", il);
  9619. cb(Kcur, "Kcur", il);
  9620. cb(Vcur, "Vcur", il);
  9621. cur = build_attn(inp_attn,
  9622. model.layers[il].wo, NULL,
  9623. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  9624. }
  9625. if (il == n_layer - 1 && inp_out_ids) {
  9626. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  9627. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  9628. }
  9629. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  9630. cb(ffn_inp, "ffn_inp", il);
  9631. // MoE branch
  9632. cur = build_norm(ffn_inp,
  9633. model.layers[il].ffn_norm, NULL,
  9634. LLM_NORM_RMS, il);
  9635. cb(cur, "ffn_norm", il);
  9636. cur = build_moe_ffn(cur,
  9637. model.layers[il].ffn_gate_inp,
  9638. model.layers[il].ffn_up_exps,
  9639. model.layers[il].ffn_gate_exps,
  9640. model.layers[il].ffn_down_exps,
  9641. nullptr,
  9642. n_expert, n_expert_used,
  9643. LLM_FFN_SILU, false,
  9644. false, 0.0,
  9645. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  9646. il);
  9647. cb(cur, "ffn_moe_out", il);
  9648. cur = ggml_add(ctx0, cur, ffn_inp);
  9649. cur = build_cvec(cur, il);
  9650. cb(cur, "l_out", il);
  9651. // input for next layer
  9652. inpL = cur;
  9653. }
  9654. cur = inpL;
  9655. cur = build_norm(cur,
  9656. model.output_norm, NULL,
  9657. LLM_NORM_RMS, -1);
  9658. cb(cur, "result_norm", -1);
  9659. res->t_embd = cur;
  9660. // lm_head
  9661. cur = build_lora_mm(model.output, cur);
  9662. cb(cur, "result_output", -1);
  9663. res->t_logits = cur;
  9664. ggml_build_forward_expand(gf, cur);
  9665. }
  9666. };
  9667. struct llm_build_openelm : public llm_graph_context {
  9668. llm_build_openelm(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  9669. const int64_t n_embd_head = hparams.n_embd_head_v;
  9670. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  9671. ggml_tensor * cur;
  9672. ggml_tensor * inpL;
  9673. inpL = build_inp_embd(model.tok_embd);
  9674. // inp_pos - contains the positions
  9675. ggml_tensor * inp_pos = build_inp_pos();
  9676. auto * inp_attn = build_attn_inp_kv_unified();
  9677. ggml_tensor * inp_out_ids = build_inp_out_ids();
  9678. for (int il = 0; il < n_layer; ++il) {
  9679. const int64_t n_head = hparams.n_head(il);
  9680. const int64_t n_head_kv = hparams.n_head_kv(il);
  9681. const int64_t n_head_qkv = 2*n_head_kv + n_head;
  9682. cur = inpL;
  9683. ggml_tensor * residual = cur;
  9684. // norm
  9685. cur = build_norm(inpL,
  9686. model.layers[il].attn_norm, NULL,
  9687. LLM_NORM_RMS, il);
  9688. cb(cur, "attn_norm", il);
  9689. // self-attention
  9690. {
  9691. cur = build_lora_mm(model.layers[il].wqkv, cur);
  9692. cb(cur, "wqkv", il);
  9693. cur = ggml_reshape_3d(ctx0, cur, n_embd_head_k, n_head_qkv, n_tokens);
  9694. ggml_tensor * Qcur = ggml_view_3d(ctx0, cur, n_embd_head, n_head, n_tokens, cur->nb[1], cur->nb[2], 0);
  9695. cb(Qcur, "Qcur", il);
  9696. ggml_tensor * Kcur = ggml_view_3d(ctx0, cur, n_embd_head, n_head_kv, n_tokens, cur->nb[1], cur->nb[2], cur->nb[1]*n_head);
  9697. cb(Kcur, "Kcur", il);
  9698. ggml_tensor * Vcur = ggml_cont(ctx0, ggml_view_3d(ctx0, cur, n_embd_head, n_head_kv, n_tokens, cur->nb[1], cur->nb[2], cur->nb[1]*(n_head+n_head_kv)));
  9699. cb(Vcur, "Vcur", il);
  9700. Qcur = build_norm(Qcur,
  9701. model.layers[il].attn_q_norm, NULL,
  9702. LLM_NORM_RMS, il);
  9703. cb(Qcur, "Qcur", il);
  9704. Kcur = build_norm(Kcur,
  9705. model.layers[il].attn_k_norm, NULL,
  9706. LLM_NORM_RMS, il);
  9707. cb(Kcur, "Kcur", il);
  9708. Qcur = ggml_rope_ext(
  9709. ctx0, Qcur, inp_pos, NULL,
  9710. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  9711. ext_factor, attn_factor, beta_fast, beta_slow
  9712. );
  9713. Kcur = ggml_rope_ext(
  9714. ctx0, Kcur, inp_pos, NULL,
  9715. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  9716. ext_factor, attn_factor, beta_fast, beta_slow
  9717. );
  9718. cb(Qcur, "Qcur", il);
  9719. cb(Kcur, "Kcur", il);
  9720. cb(Qcur, "Vcur", il);
  9721. cur = build_attn(inp_attn,
  9722. model.layers[il].wo, NULL,
  9723. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  9724. }
  9725. if (il == n_layer - 1 && inp_out_ids) {
  9726. residual = ggml_get_rows(ctx0, residual, inp_out_ids);
  9727. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  9728. }
  9729. ggml_tensor * ffn_inp = ggml_add(ctx0, residual, cur);
  9730. cb(ffn_inp, "ffn_inp", il);
  9731. // feed-forward network
  9732. {
  9733. cur = build_norm(ffn_inp,
  9734. model.layers[il].ffn_norm, NULL,
  9735. LLM_NORM_RMS, il);
  9736. cb(cur, "ffn_norm", il);
  9737. cur = build_ffn(cur,
  9738. model.layers[il].ffn_up, NULL, NULL,
  9739. model.layers[il].ffn_gate, NULL, NULL,
  9740. model.layers[il].ffn_down, NULL, NULL,
  9741. NULL,
  9742. LLM_FFN_SILU, LLM_FFN_PAR, il);
  9743. cb(cur, "ffn_out", il);
  9744. }
  9745. cur = ggml_add(ctx0, cur, ffn_inp);
  9746. cur = build_cvec(cur, il);
  9747. cb(cur, "l_out", il);
  9748. inpL = cur;
  9749. }
  9750. cur = inpL;
  9751. // norm
  9752. cur = build_norm(cur,
  9753. model.output_norm, NULL,
  9754. LLM_NORM_RMS, -1);
  9755. cb(cur, "result_norm", -1);
  9756. res->t_embd = cur;
  9757. cur = build_lora_mm(model.output, cur);
  9758. cb(cur, "result_output", -1);
  9759. res->t_logits = cur;
  9760. ggml_build_forward_expand(gf, cur);
  9761. }
  9762. };
  9763. struct llm_build_gptneox : public llm_graph_context {
  9764. llm_build_gptneox(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  9765. const int64_t n_embd_head = hparams.n_embd_head_v;
  9766. const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  9767. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  9768. ggml_tensor * cur;
  9769. ggml_tensor * inpL;
  9770. inpL = build_inp_embd(model.tok_embd);
  9771. // inp_pos - contains the positions
  9772. ggml_tensor * inp_pos = build_inp_pos();
  9773. auto * inp_attn = build_attn_inp_kv_unified();
  9774. ggml_tensor * inp_out_ids = build_inp_out_ids();
  9775. for (int il = 0; il < n_layer; ++il) {
  9776. cur = build_norm(inpL,
  9777. model.layers[il].attn_norm,
  9778. model.layers[il].attn_norm_b,
  9779. LLM_NORM, il);
  9780. cb(cur, "attn_norm", il);
  9781. // self-attention
  9782. {
  9783. cur = build_lora_mm(model.layers[il].wqkv, cur);
  9784. cb(cur, "wqkv", il);
  9785. cur = ggml_add(ctx0, cur, model.layers[il].bqkv);
  9786. cb(cur, "bqkv", il);
  9787. ggml_tensor * Qcur = ggml_view_3d(ctx0, cur, n_embd_head, n_head, n_tokens, n_embd_head*sizeof(float), cur->nb[1], 0*sizeof(float)*(n_embd));
  9788. ggml_tensor * Kcur = ggml_view_3d(ctx0, cur, n_embd_head, n_head_kv, n_tokens, n_embd_head*sizeof(float), cur->nb[1], 1*sizeof(float)*(n_embd));
  9789. ggml_tensor * Vcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd + n_embd_gqa)));
  9790. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  9791. Qcur = ggml_rope_ext(
  9792. ctx0, Qcur, inp_pos, nullptr,
  9793. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  9794. ext_factor, attn_factor, beta_fast, beta_slow
  9795. );
  9796. Kcur = ggml_rope_ext(
  9797. ctx0, Kcur, inp_pos, nullptr,
  9798. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  9799. ext_factor, attn_factor, beta_fast, beta_slow
  9800. );
  9801. cb(Qcur, "Qcur", il);
  9802. cb(Kcur, "Kcur", il);
  9803. cb(Vcur, "Vcur", il);
  9804. cur = build_attn(inp_attn,
  9805. model.layers[il].wo, model.layers[il].bo,
  9806. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  9807. }
  9808. if (il == n_layer - 1 && inp_out_ids) {
  9809. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  9810. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  9811. }
  9812. // ffn
  9813. if (hparams.use_par_res) {
  9814. // attention and ffn are computed in parallel
  9815. // x = x + attn(ln1(x)) + ffn(ln2(x))
  9816. ggml_tensor * attn_out = cur;
  9817. cur = build_norm(inpL,
  9818. model.layers[il].ffn_norm,
  9819. model.layers[il].ffn_norm_b,
  9820. LLM_NORM, il);
  9821. cb(cur, "ffn_norm", il);
  9822. cur = build_ffn(cur,
  9823. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  9824. NULL, NULL, NULL,
  9825. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  9826. NULL,
  9827. LLM_FFN_GELU, LLM_FFN_SEQ, il);
  9828. cb(cur, "ffn_out", il);
  9829. cur = ggml_add(ctx0, cur, inpL);
  9830. cb(cur, "ffn_out", il);
  9831. cur = ggml_add(ctx0, cur, attn_out);
  9832. cur = build_cvec(cur, il);
  9833. cb(cur, "l_out", il);
  9834. // input for next layer
  9835. inpL = cur;
  9836. } else {
  9837. // attention and ffn are computed sequentially
  9838. // x = x + attn(ln1(x))
  9839. // x = x + ffn(ln2(x))
  9840. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
  9841. cb(ffn_inp, "ffn_inp", il);
  9842. cur = build_norm(ffn_inp,
  9843. model.layers[il].ffn_norm,
  9844. model.layers[il].ffn_norm_b,
  9845. LLM_NORM, il);
  9846. cb(cur, "ffn_norm", il);
  9847. cur = build_ffn(cur,
  9848. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  9849. NULL, NULL, NULL,
  9850. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  9851. NULL,
  9852. LLM_FFN_GELU, LLM_FFN_SEQ, il);
  9853. cb(cur, "ffn_out", il);
  9854. cur = ggml_add(ctx0, cur, ffn_inp);
  9855. cur = build_cvec(cur, il);
  9856. cb(cur, "l_out", il);
  9857. // input for next layer
  9858. inpL = cur;
  9859. }
  9860. }
  9861. cur = build_norm(inpL,
  9862. model.output_norm,
  9863. model.output_norm_b,
  9864. LLM_NORM, -1);
  9865. cb(cur, "result_norm", -1);
  9866. res->t_embd = cur;
  9867. cur = build_lora_mm(model.output, cur);
  9868. cb(cur, "result_output", -1);
  9869. res->t_logits = cur;
  9870. ggml_build_forward_expand(gf, cur);
  9871. }
  9872. };
  9873. struct llm_build_arctic : public llm_graph_context {
  9874. llm_build_arctic(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  9875. const int64_t n_embd_head = hparams.n_embd_head_v;
  9876. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  9877. GGML_ASSERT(n_embd_head == hparams.n_rot);
  9878. ggml_tensor * cur;
  9879. ggml_tensor * inpL;
  9880. inpL = build_inp_embd(model.tok_embd);
  9881. // inp_pos - contains the positions
  9882. ggml_tensor * inp_pos = build_inp_pos();
  9883. auto * inp_attn = build_attn_inp_kv_unified();
  9884. ggml_tensor * inp_out_ids = build_inp_out_ids();
  9885. for (int il = 0; il < n_layer; ++il) {
  9886. ggml_tensor * inpSA = inpL;
  9887. // norm
  9888. cur = build_norm(inpL,
  9889. model.layers[il].attn_norm, NULL,
  9890. LLM_NORM_RMS, il);
  9891. cb(cur, "attn_norm", il);
  9892. // self-attention
  9893. {
  9894. // compute Q and K and RoPE them
  9895. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  9896. cb(Qcur, "Qcur", il);
  9897. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  9898. cb(Kcur, "Kcur", il);
  9899. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  9900. cb(Vcur, "Vcur", il);
  9901. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  9902. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  9903. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  9904. Qcur = ggml_rope_ext(
  9905. ctx0, Qcur, inp_pos, nullptr,
  9906. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  9907. ext_factor, attn_factor, beta_fast, beta_slow
  9908. );
  9909. Kcur = ggml_rope_ext(
  9910. ctx0, Kcur, inp_pos, nullptr,
  9911. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  9912. ext_factor, attn_factor, beta_fast, beta_slow
  9913. );
  9914. cb(Qcur, "Qcur", il);
  9915. cb(Kcur, "Kcur", il);
  9916. cb(Vcur, "Vcur", il);
  9917. cur = build_attn(inp_attn,
  9918. model.layers[il].wo, NULL,
  9919. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  9920. }
  9921. if (il == n_layer - 1 && inp_out_ids) {
  9922. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  9923. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  9924. }
  9925. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  9926. cb(ffn_inp, "ffn_inp", il);
  9927. // feed-forward network
  9928. cur = build_norm(ffn_inp,
  9929. model.layers[il].ffn_norm, NULL,
  9930. LLM_NORM_RMS, il);
  9931. cb(cur, "ffn_norm", il);
  9932. cur = build_ffn(cur,
  9933. model.layers[il].ffn_up, NULL, NULL,
  9934. model.layers[il].ffn_gate, NULL, NULL,
  9935. model.layers[il].ffn_down, NULL, NULL,
  9936. NULL,
  9937. LLM_FFN_SILU, LLM_FFN_PAR, il);
  9938. cb(cur, "ffn_out", il);
  9939. ggml_tensor * ffn_out = ggml_add(ctx0, cur, ffn_inp);
  9940. cb(ffn_out, "ffn_out", il);
  9941. // MoE
  9942. cur = build_norm(inpSA,
  9943. model.layers[il].ffn_norm_exps, NULL,
  9944. LLM_NORM_RMS, il);
  9945. cb(cur, "ffn_norm_exps", il);
  9946. cur = build_moe_ffn(cur,
  9947. model.layers[il].ffn_gate_inp,
  9948. model.layers[il].ffn_up_exps,
  9949. model.layers[il].ffn_gate_exps,
  9950. model.layers[il].ffn_down_exps,
  9951. nullptr,
  9952. n_expert, n_expert_used,
  9953. LLM_FFN_SILU, true,
  9954. false, 0.0,
  9955. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  9956. il);
  9957. cb(cur, "ffn_moe_out", il);
  9958. cur = ggml_add(ctx0, cur, ffn_out);
  9959. cb(cur, "ffn_out", il);
  9960. cur = build_cvec(cur, il);
  9961. cb(cur, "l_out", il);
  9962. // input for next layer
  9963. inpL = cur;
  9964. }
  9965. cur = inpL;
  9966. cur = build_norm(cur,
  9967. model.output_norm, NULL,
  9968. LLM_NORM_RMS, -1);
  9969. cb(cur, "result_norm", -1);
  9970. res->t_embd = cur;
  9971. // lm_head
  9972. cur = build_lora_mm(model.output, cur);
  9973. cb(cur, "result_output", -1);
  9974. res->t_logits = cur;
  9975. ggml_build_forward_expand(gf, cur);
  9976. }
  9977. };
  9978. struct llm_build_deepseek : public llm_graph_context {
  9979. llm_build_deepseek(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  9980. const int64_t n_embd_head = hparams.n_embd_head_v;
  9981. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  9982. GGML_ASSERT(n_embd_head == hparams.n_rot);
  9983. ggml_tensor * cur;
  9984. ggml_tensor * inpL;
  9985. inpL = build_inp_embd(model.tok_embd);
  9986. // inp_pos - contains the positions
  9987. ggml_tensor * inp_pos = build_inp_pos();
  9988. auto * inp_attn = build_attn_inp_kv_unified();
  9989. const float kq_scale = hparams.f_attention_scale == 0.0f ? 1.0f/sqrtf(float(n_embd_head)) : hparams.f_attention_scale;
  9990. ggml_tensor * inp_out_ids = build_inp_out_ids();
  9991. for (int il = 0; il < n_layer; ++il) {
  9992. ggml_tensor * inpSA = inpL;
  9993. // norm
  9994. cur = build_norm(inpL,
  9995. model.layers[il].attn_norm, NULL,
  9996. LLM_NORM_RMS, il);
  9997. cb(cur, "attn_norm", il);
  9998. // self-attention
  9999. {
  10000. // rope freq factors for llama3; may return nullptr for llama2 and other models
  10001. ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
  10002. // compute Q and K and RoPE them
  10003. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  10004. cb(Qcur, "Qcur", il);
  10005. if (model.layers[il].bq) {
  10006. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  10007. cb(Qcur, "Qcur", il);
  10008. }
  10009. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  10010. cb(Kcur, "Kcur", il);
  10011. if (model.layers[il].bk) {
  10012. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  10013. cb(Kcur, "Kcur", il);
  10014. }
  10015. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  10016. cb(Vcur, "Vcur", il);
  10017. if (model.layers[il].bv) {
  10018. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  10019. cb(Vcur, "Vcur", il);
  10020. }
  10021. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  10022. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  10023. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  10024. Qcur = ggml_rope_ext(
  10025. ctx0, Qcur, inp_pos, rope_factors,
  10026. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  10027. ext_factor, attn_factor, beta_fast, beta_slow
  10028. );
  10029. Kcur = ggml_rope_ext(
  10030. ctx0, Kcur, inp_pos, rope_factors,
  10031. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  10032. ext_factor, attn_factor, beta_fast, beta_slow
  10033. );
  10034. cb(Qcur, "Qcur", il);
  10035. cb(Kcur, "Kcur", il);
  10036. cb(Vcur, "Vcur", il);
  10037. cur = build_attn(inp_attn,
  10038. model.layers[il].wo, model.layers[il].bo,
  10039. Qcur, Kcur, Vcur, nullptr, nullptr, kq_scale, il);
  10040. }
  10041. if (il == n_layer - 1 && inp_out_ids) {
  10042. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  10043. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  10044. }
  10045. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  10046. cb(ffn_inp, "ffn_inp", il);
  10047. cur = build_norm(ffn_inp,
  10048. model.layers[il].ffn_norm, NULL,
  10049. LLM_NORM_RMS, il);
  10050. cb(cur, "ffn_norm", il);
  10051. if ((uint32_t) il < hparams.n_layer_dense_lead) {
  10052. cur = build_ffn(cur,
  10053. model.layers[il].ffn_up, NULL, NULL,
  10054. model.layers[il].ffn_gate, NULL, NULL,
  10055. model.layers[il].ffn_down, NULL, NULL,
  10056. NULL,
  10057. LLM_FFN_SILU, LLM_FFN_PAR, il);
  10058. cb(cur, "ffn_out", il);
  10059. } else {
  10060. // MoE branch
  10061. ggml_tensor * moe_out =
  10062. build_moe_ffn(cur,
  10063. model.layers[il].ffn_gate_inp,
  10064. model.layers[il].ffn_up_exps,
  10065. model.layers[il].ffn_gate_exps,
  10066. model.layers[il].ffn_down_exps,
  10067. nullptr,
  10068. n_expert, n_expert_used,
  10069. LLM_FFN_SILU, false,
  10070. false, hparams.expert_weights_scale,
  10071. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  10072. il);
  10073. cb(moe_out, "ffn_moe_out", il);
  10074. // FFN shared expert
  10075. {
  10076. ggml_tensor * ffn_shexp = build_ffn(cur,
  10077. model.layers[il].ffn_up_shexp, NULL, NULL,
  10078. model.layers[il].ffn_gate_shexp, NULL, NULL,
  10079. model.layers[il].ffn_down_shexp, NULL, NULL,
  10080. NULL,
  10081. LLM_FFN_SILU, LLM_FFN_PAR, il);
  10082. cb(ffn_shexp, "ffn_shexp", il);
  10083. cur = ggml_add(ctx0, moe_out, ffn_shexp);
  10084. cb(cur, "ffn_out", il);
  10085. }
  10086. }
  10087. cur = ggml_add(ctx0, cur, ffn_inp);
  10088. cur = build_cvec(cur, il);
  10089. cb(cur, "l_out", il);
  10090. // input for next layer
  10091. inpL = cur;
  10092. }
  10093. cur = inpL;
  10094. cur = build_norm(cur,
  10095. model.output_norm, NULL,
  10096. LLM_NORM_RMS, -1);
  10097. cb(cur, "result_norm", -1);
  10098. res->t_embd = cur;
  10099. // lm_head
  10100. cur = build_lora_mm(model.output, cur);
  10101. cb(cur, "result_output", -1);
  10102. res->t_logits = cur;
  10103. ggml_build_forward_expand(gf, cur);
  10104. }
  10105. };
  10106. struct llm_build_deepseek2 : public llm_graph_context {
  10107. llm_build_deepseek2(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  10108. bool is_lite = (hparams.n_layer == 27);
  10109. const bool is_mla = (hparams.n_embd_head_k_mla != 0 && hparams.n_embd_head_v_mla != 0);
  10110. // note: these are the actual head sizes you get when treating as MHA or after "decompression" using wv_b for MLA
  10111. const int64_t n_embd_head_k = is_mla ? hparams.n_embd_head_k_mla : hparams.n_embd_head_k;
  10112. const int64_t n_embd_head_v = is_mla ? hparams.n_embd_head_v_mla : hparams.n_embd_head_v;
  10113. const int64_t n_embd_head_qk_rope = hparams.n_rot;
  10114. const int64_t n_embd_head_qk_nope = n_embd_head_k - n_embd_head_qk_rope;
  10115. const uint32_t kv_lora_rank = hparams.n_lora_kv;
  10116. // We have to pre-scale kq_scale and attn_factor to make the YaRN RoPE work correctly.
  10117. // See https://github.com/ggerganov/llama.cpp/discussions/7416 for detailed explanation.
  10118. const float mscale = attn_factor * (1.0f + hparams.rope_yarn_log_mul * logf(1.0f / freq_scale));
  10119. const float kq_scale = 1.0f*mscale*mscale/sqrtf(float(n_embd_head_k));
  10120. const float attn_factor = 1.0f / (1.0f + 0.1f * logf(1.0f / freq_scale));
  10121. ggml_tensor * cur;
  10122. ggml_tensor * inpL;
  10123. // {n_embd, n_tokens}
  10124. inpL = build_inp_embd(model.tok_embd);
  10125. // inp_pos - contains the positions
  10126. ggml_tensor * inp_pos = build_inp_pos();
  10127. auto * inp_attn = build_attn_inp_kv_unified();
  10128. ggml_tensor * inp_out_ids = build_inp_out_ids();
  10129. for (int il = 0; il < n_layer; ++il) {
  10130. ggml_tensor * inpSA = inpL;
  10131. // norm
  10132. cur = build_norm(inpL,
  10133. model.layers[il].attn_norm, NULL,
  10134. LLM_NORM_RMS, il);
  10135. cb(cur, "attn_norm", il);
  10136. // self_attention
  10137. {
  10138. ggml_tensor * q = NULL;
  10139. if (!is_lite) {
  10140. q = ggml_mul_mat(ctx0, model.layers[il].wq_a, cur);
  10141. cb(q, "q", il);
  10142. q = build_norm(q,
  10143. model.layers[il].attn_q_a_norm, nullptr,
  10144. LLM_NORM_RMS, il);
  10145. cb(q, "q", il);
  10146. q = ggml_mul_mat(ctx0, model.layers[il].wq_b, q);
  10147. cb(q, "q", il);
  10148. } else {
  10149. q = ggml_mul_mat(ctx0, model.layers[il].wq, cur);
  10150. cb(q, "q", il);
  10151. }
  10152. // split into {n_embd_head_qk_nope, n_head, n_tokens}
  10153. ggml_tensor * q_nope = ggml_view_3d(ctx0, q,
  10154. n_embd_head_qk_nope, n_head, n_tokens,
  10155. ggml_row_size(q->type, n_embd_head_k),
  10156. ggml_row_size(q->type, n_embd_head_k) * n_head,
  10157. 0);
  10158. cb(q_nope, "q_nope", il);
  10159. // and {n_embd_head_qk_rope, n_head, n_tokens}
  10160. ggml_tensor * q_pe = ggml_view_3d(ctx0, q,
  10161. n_embd_head_qk_rope, n_head, n_tokens,
  10162. ggml_row_size(q->type, n_embd_head_k),
  10163. ggml_row_size(q->type, n_embd_head_k) * n_head,
  10164. ggml_row_size(q->type, n_embd_head_qk_nope));
  10165. cb(q_pe, "q_pe", il);
  10166. ggml_tensor * kv_cmpr_pe = ggml_mul_mat(ctx0, model.layers[il].wkv_a_mqa, cur);
  10167. cb(kv_cmpr_pe, "kv_cmpr_pe", il);
  10168. // split into {kv_lora_rank, n_tokens}
  10169. ggml_tensor * kv_cmpr = ggml_view_2d(ctx0, kv_cmpr_pe,
  10170. kv_lora_rank, n_tokens,
  10171. ggml_row_size(kv_cmpr_pe->type, kv_lora_rank + n_embd_head_qk_rope),
  10172. 0);
  10173. cb(kv_cmpr, "kv_cmpr", il);
  10174. // and {n_embd_head_qk_rope, 1, n_tokens}
  10175. ggml_tensor * k_pe = ggml_view_3d(ctx0, kv_cmpr_pe,
  10176. n_embd_head_qk_rope, 1, n_tokens,
  10177. ggml_row_size(kv_cmpr_pe->type, kv_lora_rank + n_embd_head_qk_rope),
  10178. ggml_row_size(kv_cmpr_pe->type, kv_lora_rank + n_embd_head_qk_rope),
  10179. ggml_row_size(kv_cmpr_pe->type, kv_lora_rank));
  10180. cb(k_pe, "k_pe", il);
  10181. q_pe = ggml_rope_ext(ctx0, q_pe, inp_pos, nullptr,
  10182. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  10183. ext_factor, attn_factor, beta_fast, beta_slow
  10184. );
  10185. cb(q_pe, "q_pe", il);
  10186. k_pe = ggml_rope_ext(ctx0, k_pe, inp_pos, nullptr,
  10187. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  10188. ext_factor, attn_factor, beta_fast, beta_slow
  10189. );
  10190. cb(k_pe, "k_pe", il);
  10191. kv_cmpr = build_norm(kv_cmpr,
  10192. model.layers[il].attn_kv_a_norm, nullptr,
  10193. LLM_NORM_RMS, il);
  10194. cb(kv_cmpr, "kv_cmpr", il);
  10195. if (is_mla) {
  10196. // {n_embd_head_qk_nope, n_tokens, n_head}
  10197. q_nope = ggml_permute(ctx0, q_nope, 0, 2, 1, 3);
  10198. cb(q_nope, "q_nope_perm", il);
  10199. // {n_embd_head_qk_nope, kv_lora_rank, n_head} x {n_embd_head_qk_nope, n_tokens, n_head}
  10200. ggml_tensor * q_nope_absorbed = ggml_mul_mat(ctx0, model.layers[il].wk_b, q_nope);
  10201. cb(q_nope_absorbed, "q_nope_absorbed", il);
  10202. // {kv_lora_rank, n_head, n_tokens}
  10203. q_nope_absorbed = ggml_permute(ctx0, q_nope_absorbed, 0, 2, 1, 3);
  10204. cb(q_nope_absorbed, "q_nope_absorbed_perm", il);
  10205. // {n_embd_head_qk_rope + kv_lora_rank, n_head, n_tokens}
  10206. // note: rope must go first for in-place context shifting in build_rope_shift()
  10207. ggml_tensor * Qcur = ggml_concat(ctx0, q_pe, q_nope_absorbed, 0);
  10208. cb(Qcur, "Qcur", il);
  10209. kv_cmpr = ggml_reshape_3d(ctx0, kv_cmpr, kv_lora_rank, 1, n_tokens);
  10210. cb(kv_cmpr, "kv_cmpr_reshape", il);
  10211. // {n_embd_head_qk_rope + kv_lora_rank, 1, n_tokens}
  10212. ggml_tensor * Kcur = ggml_concat(ctx0, k_pe, kv_cmpr, 0);
  10213. cb(Kcur, "Kcur", il);
  10214. // {kv_lora_rank, 1, n_tokens}
  10215. ggml_tensor * Vcur = kv_cmpr;
  10216. cb(Vcur, "Vcur", il);
  10217. // note: MLA with the absorption optimzation converts into MQA (ie: GQA with 1 group)
  10218. cur = build_attn(inp_attn,
  10219. model.layers[il].wo, NULL,
  10220. Qcur, Kcur, Vcur, nullptr, model.layers[il].wv_b, kq_scale, il);
  10221. } else {
  10222. ggml_tensor * kv = ggml_mul_mat(ctx0, model.layers[il].wkv_b, kv_cmpr);
  10223. cb(kv, "kv", il);
  10224. // split into {n_embd_head_qk_nope, n_head, n_tokens}
  10225. ggml_tensor * k_nope = ggml_view_3d(ctx0, kv,
  10226. n_embd_head_qk_nope, n_head, n_tokens,
  10227. ggml_row_size(kv->type, n_embd_head_qk_nope + n_embd_head_v),
  10228. ggml_row_size(kv->type, n_embd_head_qk_nope + n_embd_head_v) * n_head,
  10229. 0);
  10230. cb(k_nope, "k_nope_view", il);
  10231. // and {n_embd_head_v, n_head, n_tokens}
  10232. ggml_tensor * Vcur = ggml_view_3d(ctx0, kv,
  10233. n_embd_head_v, n_head, n_tokens,
  10234. ggml_row_size(kv->type, n_embd_head_qk_nope + n_embd_head_v),
  10235. ggml_row_size(kv->type, n_embd_head_qk_nope + n_embd_head_v) * n_head,
  10236. ggml_row_size(kv->type, n_embd_head_qk_nope));
  10237. cb(Vcur, "Vcur_view", il);
  10238. Vcur = ggml_cont(ctx0, Vcur);
  10239. cb(Vcur, "Vcur_cont", il);
  10240. // note: rope must go first for in-place context shifting in build_rope_shift()
  10241. ggml_tensor * Qcur = ggml_concat(ctx0, q_pe, q_nope, 0);
  10242. cb(Qcur, "Qcur", il);
  10243. ggml_tensor * Kcur = ggml_concat(ctx0, ggml_repeat(ctx0, k_pe, q_pe), k_nope, 0);
  10244. cb(Kcur, "Kcur", il);
  10245. // note: MLA without the absorption optimization converts into MHA (ie: GQA with full n_head groups)
  10246. cur = build_attn(inp_attn,
  10247. model.layers[il].wo, NULL,
  10248. Qcur, Kcur, Vcur, nullptr, nullptr, kq_scale, il);
  10249. }
  10250. }
  10251. if (il == n_layer - 1 && inp_out_ids) {
  10252. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  10253. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  10254. }
  10255. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  10256. cb(ffn_inp, "ffn_inp", il);
  10257. cur = build_norm(ffn_inp,
  10258. model.layers[il].ffn_norm, NULL,
  10259. LLM_NORM_RMS, il);
  10260. cb(cur, "ffn_norm", il);
  10261. if ((uint32_t) il < hparams.n_layer_dense_lead) {
  10262. cur = build_ffn(cur,
  10263. model.layers[il].ffn_up, NULL, NULL,
  10264. model.layers[il].ffn_gate, NULL, NULL,
  10265. model.layers[il].ffn_down, NULL, NULL,
  10266. NULL,
  10267. LLM_FFN_SILU, LLM_FFN_PAR, il);
  10268. cb(cur, "ffn_out", il);
  10269. } else {
  10270. // MoE branch
  10271. ggml_tensor * moe_out =
  10272. build_moe_ffn(cur,
  10273. model.layers[il].ffn_gate_inp,
  10274. model.layers[il].ffn_up_exps,
  10275. model.layers[il].ffn_gate_exps,
  10276. model.layers[il].ffn_down_exps,
  10277. model.layers[il].ffn_exp_probs_b,
  10278. n_expert, n_expert_used,
  10279. LLM_FFN_SILU, hparams.expert_weights_norm,
  10280. true, hparams.expert_weights_scale,
  10281. (llama_expert_gating_func_type) hparams.expert_gating_func,
  10282. il);
  10283. cb(moe_out, "ffn_moe_out", il);
  10284. // FFN shared expert
  10285. {
  10286. ggml_tensor * ffn_shexp = build_ffn(cur,
  10287. model.layers[il].ffn_up_shexp, NULL, NULL,
  10288. model.layers[il].ffn_gate_shexp, NULL, NULL,
  10289. model.layers[il].ffn_down_shexp, NULL, NULL,
  10290. NULL,
  10291. LLM_FFN_SILU, LLM_FFN_PAR, il);
  10292. cb(ffn_shexp, "ffn_shexp", il);
  10293. cur = ggml_add(ctx0, moe_out, ffn_shexp);
  10294. cb(cur, "ffn_out", il);
  10295. }
  10296. }
  10297. cur = ggml_add(ctx0, cur, ffn_inp);
  10298. cur = build_cvec(cur, il);
  10299. cb(cur, "l_out", il);
  10300. // input for next layer
  10301. inpL = cur;
  10302. }
  10303. cur = inpL;
  10304. cur = build_norm(cur,
  10305. model.output_norm, NULL,
  10306. LLM_NORM_RMS, -1);
  10307. cb(cur, "result_norm", -1);
  10308. res->t_embd = cur;
  10309. // lm_head
  10310. cur = ggml_mul_mat(ctx0, model.output, cur);
  10311. cb(cur, "result_output", -1);
  10312. res->t_logits = cur;
  10313. ggml_build_forward_expand(gf, cur);
  10314. }
  10315. };
  10316. struct llm_build_bitnet : public llm_graph_context {
  10317. llm_build_bitnet(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  10318. const int64_t n_embd_head = hparams.n_embd_head_v;
  10319. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  10320. ggml_tensor * cur;
  10321. ggml_tensor * inpL;
  10322. inpL = build_inp_embd(model.tok_embd);
  10323. // inp_pos - contains the positions
  10324. ggml_tensor * inp_pos = build_inp_pos();
  10325. auto * inp_attn = build_attn_inp_kv_unified();
  10326. ggml_tensor * inp_out_ids = build_inp_out_ids();
  10327. for (int il = 0; il < n_layer; ++il) {
  10328. ggml_tensor * inpSA = inpL;
  10329. cur = build_norm(inpL,
  10330. model.layers[il].attn_norm, NULL,
  10331. LLM_NORM_RMS, il);
  10332. cb(cur, "attn_norm", il);
  10333. // self-attention
  10334. {
  10335. // compute Q and K and RoPE them
  10336. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  10337. if (model.layers[il].wq_scale) {
  10338. Qcur = ggml_mul(ctx0, Qcur, model.layers[il].wq_scale);
  10339. }
  10340. cb(Qcur, "Qcur", il);
  10341. if (model.layers[il].bq) {
  10342. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  10343. cb(Qcur, "Qcur", il);
  10344. }
  10345. // B1.K
  10346. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  10347. if (model.layers[il].wk_scale) {
  10348. Kcur = ggml_mul(ctx0, Kcur, model.layers[il].wk_scale);
  10349. }
  10350. cb(Kcur, "Kcur", il);
  10351. if (model.layers[il].bk) {
  10352. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  10353. cb(Kcur, "Kcur", il);
  10354. }
  10355. // B1.V
  10356. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  10357. if (model.layers[il].wv_scale) {
  10358. Vcur = ggml_mul(ctx0, Vcur, model.layers[il].wv_scale);
  10359. }
  10360. cb(Vcur, "Vcur", il);
  10361. if (model.layers[il].bv) {
  10362. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  10363. cb(Vcur, "Vcur", il);
  10364. }
  10365. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  10366. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  10367. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  10368. Qcur = ggml_rope_ext(
  10369. ctx0, Qcur, inp_pos, nullptr,
  10370. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  10371. ext_factor, attn_factor, beta_fast, beta_slow
  10372. );
  10373. Kcur = ggml_rope_ext(
  10374. ctx0, Kcur, inp_pos, nullptr,
  10375. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  10376. ext_factor, attn_factor, beta_fast, beta_slow
  10377. );
  10378. cb(Qcur, "Qcur", il);
  10379. cb(Kcur, "Kcur", il);
  10380. cb(Vcur, "Vcur", il);
  10381. cur = build_attn(inp_attn,
  10382. NULL, NULL,
  10383. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  10384. cur = build_norm(cur,
  10385. model.layers[il].attn_sub_norm, NULL,
  10386. LLM_NORM_RMS, il);
  10387. cb(cur, "attn_sub_norm", il);
  10388. cur = build_lora_mm(model.layers[il].wo, cur);
  10389. if (model.layers[il].wo_scale) {
  10390. cur = ggml_mul(ctx0, cur, model.layers[il].wo_scale);
  10391. }
  10392. if (model.layers[il].bo) {
  10393. cur = ggml_add(ctx0, cur, model.layers[il].bo);
  10394. }
  10395. cb(cur, "attn_o_out", il);
  10396. }
  10397. if (il == n_layer - 1 && inp_out_ids) {
  10398. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  10399. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  10400. }
  10401. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  10402. cb(ffn_inp, "ffn_inp", il);
  10403. // feed-forward forward
  10404. cur = build_norm(ffn_inp,
  10405. model.layers[il].ffn_norm, NULL,
  10406. LLM_NORM_RMS, il);
  10407. cb(cur, "ffn_norm", il);
  10408. cur = build_ffn(cur,
  10409. model.layers[il].ffn_up, NULL, model.layers[il].ffn_up_scale,
  10410. model.layers[il].ffn_gate, NULL, model.layers[il].ffn_gate_scale,
  10411. NULL, NULL, NULL,
  10412. NULL,
  10413. LLM_FFN_SILU, LLM_FFN_PAR, il);
  10414. cb(cur, "ffn_sub_out", il);
  10415. cur = build_norm(cur,
  10416. model.layers[il].ffn_sub_norm, NULL,
  10417. LLM_NORM_RMS, il);
  10418. cb(cur, "ffn_sub_norm", il);
  10419. cur = build_lora_mm(model.layers[il].ffn_down, cur);
  10420. if (model.layers[il].ffn_down_scale) {
  10421. cur = ggml_mul(ctx0, cur, model.layers[il].ffn_down_scale);
  10422. }
  10423. cb(cur, "ffn_down", il);
  10424. cur = ggml_add(ctx0, cur, ffn_inp);
  10425. cb(cur, "l_out", il);
  10426. // input for next layer
  10427. inpL = cur;
  10428. }
  10429. cur = inpL;
  10430. cur = build_norm(cur,
  10431. model.output_norm, NULL,
  10432. LLM_NORM_RMS, -1);
  10433. cb(cur, "result_norm", -1);
  10434. res->t_embd = cur;
  10435. // lm_head
  10436. // FIXME: do not use model.tok_embd directly, duplicate as model.output
  10437. cur = build_lora_mm(model.tok_embd, cur);
  10438. cb(cur, "result_output", -1);
  10439. res->t_logits = cur;
  10440. ggml_build_forward_expand(gf, cur);
  10441. }
  10442. };
  10443. struct llm_build_t5_enc : public llm_graph_context {
  10444. llm_build_t5_enc(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  10445. const int64_t n_embd_head = hparams.n_embd_head_v;
  10446. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  10447. ggml_tensor * cur;
  10448. ggml_tensor * inpL;
  10449. inpL = build_inp_embd(model.tok_embd);
  10450. ggml_tensor * pos_bucket_enc = build_inp_pos_bucket_enc();
  10451. auto * inp_attn = build_attn_inp_no_cache();
  10452. ggml_tensor * inp_out_ids = build_inp_out_ids();
  10453. for (int il = 0; il < n_layer; ++il) {
  10454. ggml_tensor * inpSA = inpL;
  10455. // norm
  10456. cur = build_norm(inpL,
  10457. model.layers[il].attn_norm_enc, NULL,
  10458. LLM_NORM_RMS, il);
  10459. cb(cur, "attn_norm", il);
  10460. // self-attention
  10461. {
  10462. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq_enc, cur);
  10463. cb(Qcur, "Qcur", il);
  10464. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk_enc, cur);
  10465. cb(Kcur, "Kcur", il);
  10466. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv_enc, cur);
  10467. cb(Vcur, "Vcur", il);
  10468. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  10469. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  10470. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  10471. ggml_tensor * attn_rel_b = model.layers[il].attn_rel_b_enc ? model.layers[il].attn_rel_b_enc : model.layers[0].attn_rel_b_enc;
  10472. ggml_tensor * kq_b = build_pos_bias(pos_bucket_enc, attn_rel_b);
  10473. cur = build_attn(inp_attn,
  10474. model.layers[il].wo_enc, nullptr,
  10475. Qcur, Kcur, Vcur, kq_b, nullptr, 1.0f, il);
  10476. cb(cur, "kqv_out", il);
  10477. }
  10478. if (il == n_layer - 1 && inp_out_ids) {
  10479. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  10480. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  10481. }
  10482. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  10483. cb(ffn_inp, "ffn_inp", il);
  10484. // feed-forward network
  10485. {
  10486. cur = build_norm(ffn_inp,
  10487. model.layers[il].ffn_norm_enc, NULL,
  10488. LLM_NORM_RMS, il);
  10489. cb(cur, "ffn_norm", il);
  10490. // T5 uses relu, flan-T5 uses gelu-gated
  10491. cur = build_ffn(cur,
  10492. model.layers[il].ffn_up_enc, NULL, NULL,
  10493. model.layers[il].ffn_gate_enc, NULL, NULL,
  10494. model.layers[il].ffn_down_enc, NULL, NULL,
  10495. NULL,
  10496. model.layers[il].ffn_gate_enc ? LLM_FFN_GELU : LLM_FFN_RELU,
  10497. model.layers[il].ffn_gate_enc ? LLM_FFN_PAR : LLM_FFN_SEQ,
  10498. il);
  10499. cb(cur, "ffn_out", il);
  10500. }
  10501. cur = ggml_add(ctx0, cur, ffn_inp);
  10502. cb(cur, "ffn_out", il);
  10503. cur = build_cvec(cur, il);
  10504. cb(cur, "l_out", il);
  10505. // input for next layer
  10506. inpL = cur;
  10507. }
  10508. cur = inpL;
  10509. cb(cur, "result_embd", -1);
  10510. cur = build_norm(cur,
  10511. model.output_norm_enc, NULL,
  10512. LLM_NORM_RMS, -1);
  10513. cb(cur, "result_norm", -1);
  10514. res->t_embd = cur;
  10515. ggml_build_forward_expand(gf, cur);
  10516. }
  10517. };
  10518. struct llm_build_t5_dec : public llm_graph_context {
  10519. llm_build_t5_dec(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  10520. const int64_t n_embd_head = hparams.n_embd_head_v;
  10521. //const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  10522. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  10523. ggml_tensor * cur;
  10524. ggml_tensor * inpL;
  10525. inpL = build_inp_embd(model.tok_embd);
  10526. ggml_tensor * embd_enc = build_inp_cross_embd();
  10527. ggml_tensor * pos_bucket_dec = build_inp_pos_bucket_dec();
  10528. const int64_t n_outputs_enc = embd_enc->ne[1];
  10529. auto * inp_attn_self = build_attn_inp_kv_unified();
  10530. auto * inp_attn_cross = build_attn_inp_cross();
  10531. ggml_tensor * inp_out_ids = build_inp_out_ids();
  10532. for (int il = 0; il < n_layer; ++il) {
  10533. ggml_tensor * inpSA = inpL;
  10534. // norm
  10535. cur = build_norm(inpL,
  10536. model.layers[il].attn_norm, NULL,
  10537. LLM_NORM_RMS, il);
  10538. cb(cur, "attn_norm", il);
  10539. // self-attention
  10540. {
  10541. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  10542. cb(Qcur, "Qcur", il);
  10543. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  10544. cb(Kcur, "Kcur", il);
  10545. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  10546. cb(Vcur, "Vcur", il);
  10547. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  10548. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  10549. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  10550. ggml_tensor * attn_rel_b = model.layers[il].attn_rel_b ? model.layers[il].attn_rel_b : model.layers[0].attn_rel_b;
  10551. ggml_tensor * kq_b = build_pos_bias(pos_bucket_dec, attn_rel_b);
  10552. cur = build_attn(inp_attn_self,
  10553. model.layers[il].wo, model.layers[il].bo,
  10554. Qcur, Kcur, Vcur, kq_b, nullptr, 1.0f, il);
  10555. cb(cur, "kqv_out", il);
  10556. }
  10557. cur = ggml_add(ctx0, cur, inpSA);
  10558. cb(cur, "cross_inp", il);
  10559. ggml_tensor * inpCA = cur;
  10560. // norm
  10561. cur = build_norm(cur,
  10562. model.layers[il].attn_norm_cross, NULL,
  10563. LLM_NORM_RMS, il);
  10564. cb(cur, "attn_norm_cross", il);
  10565. // cross-attention
  10566. {
  10567. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq_cross, cur);
  10568. cb(Qcur, "Qcur", il);
  10569. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk_cross, embd_enc);
  10570. cb(Kcur, "Kcur", il);
  10571. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv_cross, embd_enc);
  10572. cb(Vcur, "Vcur", il);
  10573. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  10574. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_outputs_enc);
  10575. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_outputs_enc);
  10576. cur = build_attn(inp_attn_cross,
  10577. model.layers[il].wo_cross, nullptr,
  10578. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f, il);
  10579. cb(cur, "kqv_out", il);
  10580. //ggml_tensor * q = ggml_permute(ctx0, Qcur, 0, 2, 1, 3);
  10581. //ggml_tensor * k = ggml_cont(ctx0, ggml_permute(ctx0, Kcur, 0, 2, 1, 3));
  10582. //ggml_tensor * kq = ggml_mul_mat(ctx0, k, q);
  10583. //cb(kq, "kq", il);
  10584. //kq = ggml_soft_max_ext(ctx0, kq, KQ_mask_cross, 1.0f, hparams.f_max_alibi_bias);
  10585. //cb(kq, "kq_soft_max_ext", il);
  10586. //ggml_tensor * v = ggml_cont(ctx0, ggml_transpose(ctx0, ggml_reshape_2d(ctx0, Vcur, n_embd_gqa, n_outputs_enc)));
  10587. //cb(v, "v", il);
  10588. //ggml_tensor * kqv = ggml_mul_mat(ctx0, ggml_reshape_3d(ctx0, v, n_outputs_enc, n_embd_head, n_head_kv), kq);
  10589. //cb(kqv, "kqv", il);
  10590. //ggml_tensor * kqv_merged = ggml_permute(ctx0, kqv, 0, 2, 1, 3);
  10591. //cb(kqv_merged, "kqv_merged", il);
  10592. //cur = ggml_cont_2d(ctx0, kqv_merged, n_embd_gqa, n_tokens);
  10593. //cb(cur, "kqv_merged_cont", il);
  10594. //ggml_build_forward_expand(gf, cur);
  10595. //cur = build_lora_mm(model.layers[il].wo_cross, cur);
  10596. //cb(cur, "kqv_out", il);
  10597. }
  10598. if (il == n_layer - 1 && inp_out_ids) {
  10599. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  10600. inpCA = ggml_get_rows(ctx0, inpCA, inp_out_ids);
  10601. }
  10602. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpCA);
  10603. cb(ffn_inp, "ffn_inp", il);
  10604. // feed-forward network
  10605. {
  10606. cur = build_norm(ffn_inp,
  10607. model.layers[il].ffn_norm, NULL,
  10608. LLM_NORM_RMS, il);
  10609. cb(cur, "ffn_norm", il);
  10610. // T5 uses relu, flan-T5 uses gelu-gated
  10611. cur = build_ffn(cur,
  10612. model.layers[il].ffn_up, NULL, NULL,
  10613. model.layers[il].ffn_gate, NULL, NULL,
  10614. model.layers[il].ffn_down, NULL, NULL,
  10615. NULL,
  10616. model.layers[il].ffn_gate_enc ? LLM_FFN_GELU : LLM_FFN_RELU,
  10617. model.layers[il].ffn_gate_enc ? LLM_FFN_PAR : LLM_FFN_SEQ,
  10618. il);
  10619. cb(cur, "ffn_out", il);
  10620. }
  10621. cur = ggml_add(ctx0, cur, ffn_inp);
  10622. cb(cur, "ffn_out", il);
  10623. cur = build_cvec(cur, il);
  10624. cb(cur, "l_out", il);
  10625. // input for next layer
  10626. inpL = cur;
  10627. }
  10628. cur = inpL;
  10629. cb(cur, "result_embd", -1);
  10630. cur = build_norm(cur,
  10631. model.output_norm, NULL,
  10632. LLM_NORM_RMS, -1);
  10633. cb(cur, "result_norm", -1);
  10634. res->t_embd = cur;
  10635. // lm_head
  10636. cur = build_lora_mm(model.output, cur);
  10637. cb(cur, "result_output", -1);
  10638. res->t_logits = cur;
  10639. ggml_build_forward_expand(gf, cur);
  10640. }
  10641. };
  10642. struct llm_build_jais : public llm_graph_context {
  10643. llm_build_jais(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  10644. const int64_t n_embd_head = hparams.n_embd_head_v;
  10645. const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  10646. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  10647. ggml_tensor * cur;
  10648. ggml_tensor * inpL;
  10649. inpL = build_inp_embd(model.tok_embd);
  10650. auto * inp_attn = build_attn_inp_kv_unified();
  10651. ggml_tensor * inp_out_ids = build_inp_out_ids();
  10652. for (int il = 0; il < n_layer; ++il) {
  10653. cur = build_norm(inpL,
  10654. model.layers[il].attn_norm,
  10655. model.layers[il].attn_norm_b,
  10656. LLM_NORM, il);
  10657. cb(cur, "attn_norm", il);
  10658. // self-attention
  10659. {
  10660. cur = build_lora_mm(model.layers[il].wqkv, cur);
  10661. cb(cur, "wqkv", il);
  10662. cur = ggml_add(ctx0, cur, model.layers[il].bqkv);
  10663. cb(cur, "bqkv", il);
  10664. ggml_tensor * Qcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd, n_tokens, cur->nb[1], 0*cur->nb[0]*(n_embd)));
  10665. ggml_tensor * Kcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*cur->nb[0]*(n_embd)));
  10666. ggml_tensor * Vcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*cur->nb[0]*(n_embd + n_embd_gqa)));
  10667. cb(Qcur, "Qcur", il);
  10668. cb(Kcur, "Kcur", il);
  10669. cb(Vcur, "Vcur", il);
  10670. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  10671. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  10672. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  10673. cur = build_attn(inp_attn,
  10674. model.layers[il].wo, model.layers[il].bo,
  10675. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/float(n_embd_head), il);
  10676. }
  10677. if (il == n_layer - 1 && inp_out_ids) {
  10678. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  10679. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  10680. }
  10681. // add the input
  10682. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
  10683. cb(ffn_inp, "ffn_inp", il);
  10684. // FF
  10685. {
  10686. cur = build_norm(ffn_inp,
  10687. model.layers[il].ffn_norm,
  10688. model.layers[il].ffn_norm_b,
  10689. LLM_NORM, il);
  10690. cb(cur, "ffn_norm", il);
  10691. cur = build_ffn(cur,
  10692. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  10693. model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, NULL,
  10694. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  10695. NULL,
  10696. LLM_FFN_SILU, LLM_FFN_PAR, il);
  10697. cb(cur, "ffn_out", il);
  10698. }
  10699. inpL = ggml_add(ctx0, cur, ffn_inp);
  10700. cb(inpL, "l_out", il);
  10701. }
  10702. cur = build_norm(inpL,
  10703. model.output_norm,
  10704. model.output_norm_b,
  10705. LLM_NORM, -1);
  10706. cb(cur, "result_norm", -1);
  10707. res->t_embd = cur;
  10708. cur = build_lora_mm(model.output, cur);
  10709. cb(cur, "result_output", -1);
  10710. res->t_logits = cur;
  10711. ggml_build_forward_expand(gf, cur);
  10712. }
  10713. };
  10714. struct llm_build_chatglm : public llm_graph_context {
  10715. llm_build_chatglm(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  10716. const int64_t n_embd_head = hparams.n_embd_head_v;
  10717. const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  10718. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  10719. ggml_tensor * cur;
  10720. ggml_tensor * inpL;
  10721. inpL = build_inp_embd(model.tok_embd);
  10722. // inp_pos - contains the positions
  10723. ggml_tensor * inp_pos = build_inp_pos();
  10724. auto * inp_attn = build_attn_inp_kv_unified();
  10725. ggml_tensor * inp_out_ids = build_inp_out_ids();
  10726. for (int il = 0; il < n_layer; ++il) {
  10727. ggml_tensor * inpSA = inpL;
  10728. cur = build_norm(inpL,
  10729. model.layers[il].attn_norm,
  10730. NULL,
  10731. LLM_NORM_RMS, il);
  10732. cb(cur, "attn_norm", il);
  10733. // self-attention
  10734. {
  10735. ggml_tensor * Qcur = nullptr;
  10736. ggml_tensor * Kcur = nullptr;
  10737. ggml_tensor * Vcur = nullptr;
  10738. if (model.layers[il].wqkv == nullptr) {
  10739. Qcur = build_lora_mm(model.layers[il].wq, cur);
  10740. if (model.layers[il].bq) {
  10741. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  10742. }
  10743. Kcur = build_lora_mm(model.layers[il].wk, cur);
  10744. if (model.layers[il].bk) {
  10745. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  10746. }
  10747. Vcur = build_lora_mm(model.layers[il].wv, cur);
  10748. if (model.layers[il].bv) {
  10749. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  10750. }
  10751. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  10752. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  10753. } else {
  10754. cur = build_lora_mm(model.layers[il].wqkv, cur);
  10755. cb(cur, "wqkv", il);
  10756. if (model.layers[il].bqkv) {
  10757. cur = ggml_add(ctx0, cur, model.layers[il].bqkv);
  10758. cb(cur, "bqkv", il);
  10759. }
  10760. Qcur = ggml_view_3d(ctx0, cur, n_embd_head, n_head, n_tokens, n_embd_head*sizeof(float), cur->nb[1], 0*sizeof(float)*(n_embd));
  10761. Kcur = ggml_view_3d(ctx0, cur, n_embd_head, n_head_kv, n_tokens, n_embd_head*sizeof(float), cur->nb[1], 1*sizeof(float)*(n_embd));
  10762. Vcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd + n_embd_gqa)));
  10763. }
  10764. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  10765. //printf("freq_base: %f freq_scale: %f ext_factor: %f attn_factor: %f\n", freq_base, freq_scale, ext_factor, attn_factor);
  10766. Qcur = ggml_rope_ext(
  10767. ctx0, Qcur, inp_pos, nullptr,
  10768. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  10769. ext_factor, attn_factor, beta_fast, beta_slow
  10770. );
  10771. Kcur = ggml_rope_ext(
  10772. ctx0, Kcur, inp_pos, nullptr,
  10773. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  10774. ext_factor, attn_factor, beta_fast, beta_slow
  10775. );
  10776. cb(Qcur, "Qcur", il);
  10777. cb(Kcur, "Kcur", il);
  10778. cb(Vcur, "Vcur", il);
  10779. cur = build_attn(inp_attn,
  10780. model.layers[il].wo, NULL,
  10781. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  10782. }
  10783. if (il == n_layer - 1 && inp_out_ids) {
  10784. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  10785. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  10786. }
  10787. // Add the input
  10788. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  10789. cb(ffn_inp, "ffn_inp", il);
  10790. // FF
  10791. {
  10792. cur = build_norm(ffn_inp,
  10793. model.layers[il].ffn_norm,
  10794. NULL,
  10795. LLM_NORM_RMS, il);
  10796. cb(cur, "ffn_norm", il);
  10797. cur = build_ffn(cur,
  10798. model.layers[il].ffn_up, NULL, NULL,
  10799. NULL, NULL, NULL,
  10800. model.layers[il].ffn_down, NULL, NULL,
  10801. NULL,
  10802. LLM_FFN_SWIGLU, LLM_FFN_SEQ, il);
  10803. cb(cur, "ffn_out", il);
  10804. }
  10805. inpL = ggml_add(ctx0, cur, ffn_inp);
  10806. cb(inpL, "l_out", il);
  10807. }
  10808. cur = build_norm(inpL,
  10809. model.output_norm,
  10810. NULL,
  10811. LLM_NORM_RMS, -1);
  10812. cb(cur, "result_norm", -1);
  10813. res->t_embd = cur;
  10814. cur = build_lora_mm(model.output, cur);
  10815. cb(cur, "result_output", -1);
  10816. res->t_logits = cur;
  10817. ggml_build_forward_expand(gf, cur);
  10818. }
  10819. };
  10820. struct llm_build_glm4 : public llm_graph_context {
  10821. llm_build_glm4(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  10822. const int64_t n_embd_head = hparams.n_embd_head_v;
  10823. const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  10824. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  10825. ggml_tensor * cur;
  10826. ggml_tensor * inpL;
  10827. inpL = build_inp_embd(model.tok_embd);
  10828. // inp_pos - contains the positions
  10829. ggml_tensor * inp_pos = build_inp_pos();
  10830. auto * inp_attn = build_attn_inp_kv_unified();
  10831. ggml_tensor * inp_out_ids = build_inp_out_ids();
  10832. for (int il = 0; il < n_layer; ++il) {
  10833. ggml_tensor * inpSA = inpL;
  10834. // Pre-attention norm
  10835. cur = build_norm(inpL,
  10836. model.layers[il].attn_norm,
  10837. NULL,
  10838. LLM_NORM_RMS, il);
  10839. cb(cur, "attn_norm", il);
  10840. // self-attention
  10841. {
  10842. ggml_tensor * Qcur = nullptr;
  10843. ggml_tensor * Kcur = nullptr;
  10844. ggml_tensor * Vcur = nullptr;
  10845. if (model.layers[il].wqkv == nullptr) {
  10846. Qcur = build_lora_mm(model.layers[il].wq, cur);
  10847. if (model.layers[il].bq) {
  10848. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  10849. }
  10850. Kcur = build_lora_mm(model.layers[il].wk, cur);
  10851. if (model.layers[il].bk) {
  10852. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  10853. }
  10854. Vcur = build_lora_mm(model.layers[il].wv, cur);
  10855. if (model.layers[il].bv) {
  10856. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  10857. }
  10858. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  10859. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  10860. } else {
  10861. cur = build_lora_mm(model.layers[il].wqkv, cur);
  10862. cb(cur, "wqkv", il);
  10863. if (model.layers[il].bqkv) {
  10864. cur = ggml_add(ctx0, cur, model.layers[il].bqkv);
  10865. cb(cur, "bqkv", il);
  10866. }
  10867. Qcur = ggml_view_3d(ctx0, cur, n_embd_head, n_head, n_tokens, n_embd_head*sizeof(float), cur->nb[1], 0*sizeof(float)*(n_embd));
  10868. Kcur = ggml_view_3d(ctx0, cur, n_embd_head, n_head_kv, n_tokens, n_embd_head*sizeof(float), cur->nb[1], 1*sizeof(float)*(n_embd));
  10869. Vcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd + n_embd_gqa)));
  10870. }
  10871. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  10872. Qcur = ggml_rope_ext(
  10873. ctx0, Qcur, inp_pos, nullptr,
  10874. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  10875. ext_factor, attn_factor, beta_fast, beta_slow
  10876. );
  10877. Kcur = ggml_rope_ext(
  10878. ctx0, Kcur, inp_pos, nullptr,
  10879. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  10880. ext_factor, attn_factor, beta_fast, beta_slow
  10881. );
  10882. cb(Qcur, "Qcur", il);
  10883. cb(Kcur, "Kcur", il);
  10884. cb(Vcur, "Vcur", il);
  10885. cur = build_attn(inp_attn,
  10886. model.layers[il].wo, NULL,
  10887. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  10888. }
  10889. if (il == n_layer - 1 && inp_out_ids) {
  10890. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  10891. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  10892. }
  10893. // Post-attention norm (new!)
  10894. cur = build_norm(cur,
  10895. model.layers[il].attn_post_norm,
  10896. NULL,
  10897. LLM_NORM_RMS, il);
  10898. cb(cur, "post_attn_norm", il);
  10899. // Add the input (residual connection after post-attention norm)
  10900. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  10901. cb(ffn_inp, "ffn_inp", il);
  10902. // FF
  10903. {
  10904. // Pre-MLP norm
  10905. cur = build_norm(ffn_inp,
  10906. model.layers[il].ffn_norm,
  10907. NULL,
  10908. LLM_NORM_RMS, il);
  10909. cb(cur, "ffn_norm", il);
  10910. // MLP
  10911. cur = build_ffn(cur,
  10912. model.layers[il].ffn_up, NULL, NULL,
  10913. NULL, NULL, NULL,
  10914. model.layers[il].ffn_down, NULL, NULL,
  10915. NULL,
  10916. LLM_FFN_SWIGLU, LLM_FFN_SEQ, il);
  10917. cb(cur, "ffn_out", il);
  10918. // Post-MLP norm
  10919. cur = build_norm(cur,
  10920. model.layers[il].ffn_post_norm,
  10921. NULL,
  10922. LLM_NORM_RMS, il);
  10923. cb(cur, "post_mlp_norm", il);
  10924. }
  10925. // Add residual connection after post-MLP norm
  10926. inpL = ggml_add(ctx0, cur, ffn_inp);
  10927. cb(inpL, "l_out", il);
  10928. }
  10929. // Final norm
  10930. cur = build_norm(inpL,
  10931. model.output_norm,
  10932. NULL,
  10933. LLM_NORM_RMS, -1);
  10934. cb(cur, "result_norm", -1);
  10935. res->t_embd = cur;
  10936. // Output projection
  10937. cur = build_lora_mm(model.output, cur);
  10938. cb(cur, "result_output", -1);
  10939. res->t_logits = cur;
  10940. ggml_build_forward_expand(gf, cur);
  10941. }
  10942. };
  10943. struct llm_build_glm4_moe : public llm_graph_context {
  10944. llm_build_glm4_moe(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  10945. const int64_t n_embd_head = hparams.n_embd_head_v;
  10946. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  10947. ggml_tensor * cur;
  10948. ggml_tensor * inpL;
  10949. inpL = build_inp_embd(model.tok_embd);
  10950. // inp_pos - contains the positions
  10951. ggml_tensor * inp_pos = build_inp_pos();
  10952. auto * inp_attn = build_attn_inp_kv_unified();
  10953. ggml_tensor * inp_out_ids = build_inp_out_ids();
  10954. // Only process up to last layer (skip final NextN layer)
  10955. // Final layer tensors are loaded but not processed in forward pass
  10956. const int n_transformer_layers = n_layer - hparams.nextn_predict_layers;
  10957. for (int il = 0; il < n_transformer_layers; ++il) {
  10958. ggml_tensor * inpSA = inpL;
  10959. // Pre-attention norm
  10960. cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
  10961. cb(cur, "attn_norm", il);
  10962. // self-attention
  10963. {
  10964. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  10965. if (model.layers[il].bq) {
  10966. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  10967. }
  10968. cb(Qcur, "Qcur", il);
  10969. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  10970. if (model.layers[il].bk) {
  10971. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  10972. }
  10973. cb(Kcur, "Kcur", il);
  10974. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  10975. if (model.layers[il].bv) {
  10976. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  10977. }
  10978. cb(Vcur, "Vcur", il);
  10979. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  10980. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  10981. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  10982. // Apply Q/K norm if available (GLM-4.5 355B variant)
  10983. if (model.layers[il].attn_q_norm) {
  10984. Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);
  10985. cb(Qcur, "Qcur_normed", il);
  10986. }
  10987. if (model.layers[il].attn_k_norm) {
  10988. Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);
  10989. cb(Kcur, "Kcur_normed", il);
  10990. }
  10991. Qcur = ggml_rope_ext(
  10992. ctx0, Qcur, inp_pos, nullptr,
  10993. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  10994. ext_factor, attn_factor, beta_fast, beta_slow
  10995. );
  10996. Kcur = ggml_rope_ext(
  10997. ctx0, Kcur, inp_pos, nullptr,
  10998. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  10999. ext_factor, attn_factor, beta_fast, beta_slow
  11000. );
  11001. cb(Qcur, "Qcur", il);
  11002. cb(Kcur, "Kcur", il);
  11003. cb(Vcur, "Vcur", il);
  11004. cur = build_attn(inp_attn,
  11005. model.layers[il].wo, NULL,
  11006. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  11007. }
  11008. if (il == n_transformer_layers - 1 && inp_out_ids) {
  11009. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  11010. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  11011. }
  11012. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  11013. cb(ffn_inp, "ffn_inp", il);
  11014. // Post-attention norm
  11015. cur = build_norm(ffn_inp, model.layers[il].attn_post_norm, NULL, LLM_NORM_RMS, il);
  11016. cb(cur, "post_attn_norm", il);
  11017. // Check if this is a dense layer (n_layer_dense_lead=1, so layer 0 is dense)
  11018. if (static_cast<uint32_t>(il) < hparams.n_layer_dense_lead) {
  11019. // Dense FFN layer
  11020. cur = build_ffn(cur,
  11021. model.layers[il].ffn_up, NULL, NULL,
  11022. model.layers[il].ffn_gate, NULL, NULL,
  11023. model.layers[il].ffn_down, NULL, NULL,
  11024. NULL,
  11025. LLM_FFN_SILU, LLM_FFN_PAR, il);
  11026. cb(cur, "ffn_out", il);
  11027. } else {
  11028. // Process routed experts using existing MoE infrastructure
  11029. ggml_tensor * routed_out = build_moe_ffn(cur,
  11030. model.layers[il].ffn_gate_inp,
  11031. model.layers[il].ffn_up_exps,
  11032. model.layers[il].ffn_gate_exps,
  11033. model.layers[il].ffn_down_exps,
  11034. model.layers[il].ffn_exp_probs_b,
  11035. n_expert, n_expert_used,
  11036. LLM_FFN_SILU, hparams.expert_weights_norm,
  11037. true, hparams.expert_weights_scale,
  11038. (llama_expert_gating_func_type) hparams.expert_gating_func,
  11039. il);
  11040. cb(routed_out, "ffn_moe_out", il);
  11041. // Process shared expert on original input
  11042. ggml_tensor * shared_out = build_ffn(cur,
  11043. model.layers[il].ffn_up_shexp, NULL, NULL,
  11044. model.layers[il].ffn_gate_shexp, NULL, NULL,
  11045. model.layers[il].ffn_down_shexp, NULL, NULL,
  11046. NULL,
  11047. LLM_FFN_SILU, LLM_FFN_PAR, il);
  11048. cb(shared_out, "ffn_shexp_out", il);
  11049. // Final output: routed_output + shared_output
  11050. cur = ggml_add(ctx0, routed_out, shared_out);
  11051. cb(cur, "ffn_out", il);
  11052. }
  11053. cur = ggml_add(ctx0, cur, ffn_inp);
  11054. cur = build_cvec(cur, il);
  11055. cb(cur, "l_out", il);
  11056. // input for next layer
  11057. inpL = cur;
  11058. }
  11059. cur = inpL;
  11060. cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1);
  11061. cb(cur, "result_norm", -1);
  11062. res->t_embd = cur;
  11063. // lm_head
  11064. cur = build_lora_mm(model.output, cur);
  11065. cb(cur, "result_output", -1);
  11066. res->t_logits = cur;
  11067. ggml_build_forward_expand(gf, cur);
  11068. }
  11069. };
  11070. struct llm_build_nemotron : public llm_graph_context {
  11071. llm_build_nemotron(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  11072. const int64_t n_embd_head = hparams.n_embd_head_v;
  11073. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  11074. //GGML_ASSERT(n_embd_head == hparams.n_rot);
  11075. ggml_tensor * cur;
  11076. ggml_tensor * inpL;
  11077. inpL = build_inp_embd(model.tok_embd);
  11078. // inp_pos - contains the positions
  11079. ggml_tensor * inp_pos = build_inp_pos();
  11080. auto * inp_attn = build_attn_inp_kv_unified();
  11081. ggml_tensor * inp_out_ids = build_inp_out_ids();
  11082. for (int il = 0; il < n_layer; ++il) {
  11083. ggml_tensor * inpSA = inpL;
  11084. // norm
  11085. cur = build_norm(inpL,
  11086. model.layers[il].attn_norm,
  11087. model.layers[il].attn_norm_b,
  11088. LLM_NORM, il);
  11089. cb(cur, "attn_norm", il);
  11090. // self-attention
  11091. {
  11092. // compute Q and K and RoPE them
  11093. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  11094. cb(Qcur, "Qcur", il);
  11095. if (model.layers[il].bq) {
  11096. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  11097. cb(Qcur, "Qcur", il);
  11098. }
  11099. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  11100. cb(Kcur, "Kcur", il);
  11101. if (model.layers[il].bk) {
  11102. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  11103. cb(Kcur, "Kcur", il);
  11104. }
  11105. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  11106. cb(Vcur, "Vcur", il);
  11107. if (model.layers[il].bv) {
  11108. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  11109. cb(Vcur, "Vcur", il);
  11110. }
  11111. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  11112. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  11113. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  11114. Qcur = ggml_rope_ext(
  11115. ctx0, Qcur, inp_pos, nullptr,
  11116. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  11117. ext_factor, attn_factor, beta_fast, beta_slow
  11118. );
  11119. Kcur = ggml_rope_ext(
  11120. ctx0, Kcur, inp_pos, nullptr,
  11121. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  11122. ext_factor, attn_factor, beta_fast, beta_slow
  11123. );
  11124. cb(Qcur, "Qcur", il);
  11125. cb(Kcur, "Kcur", il);
  11126. cb(Vcur, "Vcur", il);
  11127. cur = build_attn(inp_attn,
  11128. model.layers[il].wo, model.layers[il].bo,
  11129. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  11130. }
  11131. if (il == n_layer - 1 && inp_out_ids) {
  11132. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  11133. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  11134. }
  11135. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  11136. cb(ffn_inp, "ffn_inp", il);
  11137. // feed-forward network
  11138. cur = build_norm(ffn_inp,
  11139. model.layers[il].ffn_norm,
  11140. model.layers[il].ffn_norm_b,
  11141. LLM_NORM, il);
  11142. cb(cur, "ffn_norm", il);
  11143. cur = build_ffn(cur,
  11144. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  11145. NULL, NULL, NULL,
  11146. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  11147. NULL,
  11148. LLM_FFN_RELU_SQR, LLM_FFN_SEQ, il);
  11149. cur = ggml_add(ctx0, cur, ffn_inp);
  11150. cb(cur, "ffn_out", il);
  11151. cur = build_cvec(cur, il);
  11152. cb(cur, "l_out", il);
  11153. // input for next layer
  11154. inpL = cur;
  11155. }
  11156. cur = inpL;
  11157. cur = build_norm(cur,
  11158. model.output_norm, model.output_norm_b,
  11159. LLM_NORM, -1);
  11160. cb(cur, "result_norm", -1);
  11161. res->t_embd = cur;
  11162. // lm_head
  11163. cur = build_lora_mm(model.output, cur);
  11164. cb(cur, "result_output", -1);
  11165. res->t_logits = cur;
  11166. ggml_build_forward_expand(gf, cur);
  11167. }
  11168. };
  11169. struct llm_build_exaone : public llm_graph_context {
  11170. llm_build_exaone(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  11171. const int64_t n_embd_head = hparams.n_embd_head_v;
  11172. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  11173. GGML_ASSERT(n_embd_head == hparams.n_rot);
  11174. ggml_tensor * cur;
  11175. ggml_tensor * inpL;
  11176. inpL = build_inp_embd(model.tok_embd);
  11177. // inp_pos - contains the positions
  11178. ggml_tensor * inp_pos = build_inp_pos();
  11179. auto * inp_attn = build_attn_inp_kv_unified();
  11180. ggml_tensor * inp_out_ids = build_inp_out_ids();
  11181. for (int il = 0; il < n_layer; ++il) {
  11182. ggml_tensor * inpSA = inpL;
  11183. // norm
  11184. cur = build_norm(inpL,
  11185. model.layers[il].attn_norm, NULL,
  11186. LLM_NORM_RMS, il);
  11187. cb(cur, "attn_norm", il);
  11188. // self-attention
  11189. {
  11190. // rope freq factors for llama3; may return nullptr for llama2 and other models
  11191. ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
  11192. // compute Q and K and RoPE them
  11193. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  11194. cb(Qcur, "Qcur", il);
  11195. if (model.layers[il].bq) {
  11196. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  11197. cb(Qcur, "Qcur", il);
  11198. }
  11199. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  11200. cb(Kcur, "Kcur", il);
  11201. if (model.layers[il].bk) {
  11202. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  11203. cb(Kcur, "Kcur", il);
  11204. }
  11205. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  11206. cb(Vcur, "Vcur", il);
  11207. if (model.layers[il].bv) {
  11208. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  11209. cb(Vcur, "Vcur", il);
  11210. }
  11211. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  11212. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  11213. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  11214. Qcur = ggml_rope_ext(
  11215. ctx0, Qcur, inp_pos, rope_factors,
  11216. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  11217. ext_factor, attn_factor, beta_fast, beta_slow
  11218. );
  11219. Kcur = ggml_rope_ext(
  11220. ctx0, Kcur, inp_pos, rope_factors,
  11221. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  11222. ext_factor, attn_factor, beta_fast, beta_slow
  11223. );
  11224. cb(Qcur, "Qcur", il);
  11225. cb(Kcur, "Kcur", il);
  11226. cb(Vcur, "Vcur", il);
  11227. cur = build_attn(inp_attn,
  11228. model.layers[il].wo, model.layers[il].bo,
  11229. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  11230. }
  11231. if (il == n_layer - 1 && inp_out_ids) {
  11232. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  11233. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  11234. }
  11235. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  11236. cb(ffn_inp, "ffn_inp", il);
  11237. // feed-forward network
  11238. cur = build_norm(ffn_inp,
  11239. model.layers[il].ffn_norm, NULL,
  11240. LLM_NORM_RMS, il);
  11241. cb(cur, "ffn_norm", il);
  11242. cur = build_ffn(cur,
  11243. model.layers[il].ffn_up, NULL, NULL,
  11244. model.layers[il].ffn_gate, NULL, NULL,
  11245. model.layers[il].ffn_down, NULL, NULL,
  11246. NULL,
  11247. LLM_FFN_SILU, LLM_FFN_PAR, il);
  11248. cb(cur, "ffn_out", il);
  11249. cur = ggml_add(ctx0, cur, ffn_inp);
  11250. cb(cur, "ffn_out", il);
  11251. cur = build_cvec(cur, il);
  11252. cb(cur, "l_out", il);
  11253. // input for next layer
  11254. inpL = cur;
  11255. }
  11256. cur = inpL;
  11257. cur = build_norm(cur,
  11258. model.output_norm, NULL,
  11259. LLM_NORM_RMS, -1);
  11260. cb(cur, "result_norm", -1);
  11261. res->t_embd = cur;
  11262. // lm_head
  11263. cur = build_lora_mm(model.output, cur);
  11264. cb(cur, "result_output", -1);
  11265. res->t_logits = cur;
  11266. ggml_build_forward_expand(gf, cur);
  11267. }
  11268. };
  11269. template <bool iswa>
  11270. struct llm_build_exaone4 : public llm_graph_context {
  11271. llm_build_exaone4(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  11272. const int64_t n_embd_head = hparams.n_embd_head_k;
  11273. GGML_ASSERT(n_embd_head == hparams.n_embd_head_v);
  11274. GGML_ASSERT(n_embd_head == hparams.n_rot);
  11275. ggml_tensor * cur;
  11276. ggml_tensor * inpL;
  11277. inpL = build_inp_embd(model.tok_embd);
  11278. // inp_pos - contains the positions
  11279. ggml_tensor * inp_pos = build_inp_pos();
  11280. using inp_attn_type = std::conditional_t<iswa, llm_graph_input_attn_kv_unified_iswa, llm_graph_input_attn_kv_unified>;
  11281. inp_attn_type * inp_attn = nullptr;
  11282. if constexpr (iswa) {
  11283. inp_attn = build_attn_inp_kv_unified_iswa();
  11284. } else {
  11285. inp_attn = build_attn_inp_kv_unified();
  11286. }
  11287. ggml_tensor * inp_out_ids = build_inp_out_ids();
  11288. for (int il = 0; il < n_layer; ++il) {
  11289. ggml_tensor * inpSA = inpL;
  11290. // use RoPE for SWA layers or non-SWA models
  11291. const bool use_rope = hparams.is_swa(il) || hparams.swa_type == LLAMA_SWA_TYPE_NONE;
  11292. cur = inpL;
  11293. // self-attention
  11294. {
  11295. ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
  11296. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  11297. cb(Qcur, "Qcur", il);
  11298. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  11299. cb(Kcur, "Kcur", il);
  11300. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  11301. cb(Vcur, "Vcur", il);
  11302. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  11303. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  11304. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  11305. Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);
  11306. Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);
  11307. cb(Qcur, "Qcur_normed", il);
  11308. cb(Kcur, "Kcur_normed", il);
  11309. if (use_rope) {
  11310. Qcur = ggml_rope_ext(
  11311. ctx0, Qcur, inp_pos, rope_factors,
  11312. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  11313. ext_factor, attn_factor, beta_fast, beta_slow
  11314. );
  11315. Kcur = ggml_rope_ext(
  11316. ctx0, Kcur, inp_pos, rope_factors,
  11317. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  11318. ext_factor, attn_factor, beta_fast, beta_slow
  11319. );
  11320. }
  11321. cb(Qcur, "Qcur", il);
  11322. cb(Kcur, "Kcur", il);
  11323. cb(Vcur, "Vcur", il);
  11324. cur = build_attn(inp_attn,
  11325. model.layers[il].wo, NULL,
  11326. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  11327. cb(cur, "attn_out", il);
  11328. }
  11329. if (il == n_layer - 1 && inp_out_ids) {
  11330. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  11331. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  11332. }
  11333. cur = build_norm(cur,
  11334. model.layers[il].attn_post_norm, NULL,
  11335. LLM_NORM_RMS, il);
  11336. cb(cur, "attn_post_norm", il);
  11337. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  11338. cb(ffn_inp, "ffn_inp", il);
  11339. // feed-forward network
  11340. cur = build_ffn(ffn_inp,
  11341. model.layers[il].ffn_up, NULL, NULL,
  11342. model.layers[il].ffn_gate, NULL, NULL,
  11343. model.layers[il].ffn_down, NULL, NULL,
  11344. NULL,
  11345. LLM_FFN_SILU, LLM_FFN_PAR, il);
  11346. cb(cur, "ffn_out", il);
  11347. cur = build_norm(cur,
  11348. model.layers[il].ffn_post_norm, NULL,
  11349. LLM_NORM_RMS, -1);
  11350. cb(cur, "ffn_post_norm", -1);
  11351. cur = ggml_add(ctx0, cur, ffn_inp);
  11352. cur = build_cvec(cur, il);
  11353. cb(cur, "l_out", il);
  11354. // input for next layer
  11355. inpL = cur;
  11356. }
  11357. cur = inpL;
  11358. cur = build_norm(cur,
  11359. model.output_norm, NULL,
  11360. LLM_NORM_RMS, -1);
  11361. cb(cur, "result_norm", -1);
  11362. res->t_embd = cur;
  11363. // lm_head
  11364. cur = build_lora_mm(model.output, cur);
  11365. cb(cur, "result_output", -1);
  11366. res->t_logits = cur;
  11367. ggml_build_forward_expand(gf, cur);
  11368. }
  11369. };
  11370. struct llm_build_rwkv6_base : public llm_graph_context {
  11371. const llama_model & model;
  11372. llm_build_rwkv6_base(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params), model(model) {
  11373. }
  11374. ggml_tensor * build_rwkv6_channel_mix(
  11375. const llama_layer * layer,
  11376. ggml_tensor * cur,
  11377. ggml_tensor * x_prev,
  11378. llm_arch arch) const {
  11379. ggml_tensor * sx = ggml_sub(ctx0, x_prev, cur);
  11380. switch (arch) {
  11381. case LLM_ARCH_RWKV6:
  11382. {
  11383. ggml_tensor * xk = ggml_add(ctx0, ggml_mul(ctx0, sx, layer->channel_mix_lerp_k), cur);
  11384. ggml_tensor * xr = ggml_add(ctx0, ggml_mul(ctx0, sx, layer->channel_mix_lerp_r), cur);
  11385. ggml_tensor * r = ggml_sigmoid(ctx0, build_lora_mm(layer->channel_mix_receptance, xr));
  11386. ggml_tensor * k = ggml_sqr(
  11387. ctx0,
  11388. ggml_relu(
  11389. ctx0,
  11390. build_lora_mm(layer->channel_mix_key, xk)
  11391. )
  11392. );
  11393. cur = ggml_mul(ctx0, r, build_lora_mm(layer->channel_mix_value, k));
  11394. } break;
  11395. default:
  11396. GGML_ABORT("fatal error");
  11397. }
  11398. return cur;
  11399. }
  11400. ggml_tensor * build_rwkv6_time_mix(
  11401. llm_graph_input_rs * inp,
  11402. ggml_tensor * cur,
  11403. ggml_tensor * x_prev,
  11404. const llama_ubatch & ubatch,
  11405. int il) const {
  11406. const auto * mctx_cur = static_cast<const llama_memory_recurrent_context *>(mctx);
  11407. const auto n_tokens = ubatch.n_tokens;
  11408. const auto n_seqs = ubatch.n_seqs;
  11409. const auto n_seq_tokens = ubatch.n_seq_tokens;
  11410. const auto n_embd = hparams.n_embd;
  11411. const auto head_size = hparams.wkv_head_size;
  11412. const auto n_head = n_embd / head_size;
  11413. const auto n_head_kv = hparams.n_head_kv(il);
  11414. const auto kv_head = mctx_cur->get_head();
  11415. const auto & layer = model.layers[il];
  11416. bool is_qrwkv = layer.time_mix_first == nullptr;
  11417. ggml_tensor * sx = ggml_sub(ctx0, x_prev, cur);
  11418. sx = ggml_reshape_2d(ctx0, sx, n_embd, n_tokens);
  11419. cur = ggml_reshape_2d(ctx0, cur, n_embd, n_tokens);
  11420. ggml_tensor * xxx = ggml_add(ctx0, ggml_mul(ctx0, sx, layer.time_mix_lerp_x), cur);
  11421. xxx = ggml_reshape_4d(
  11422. ctx0,
  11423. ggml_tanh(
  11424. ctx0,
  11425. ggml_mul_mat(ctx0, layer.time_mix_w1, xxx)
  11426. ),
  11427. layer.time_mix_w1->ne[1] / 5, 1, 5, n_tokens
  11428. );
  11429. xxx = ggml_cont(ctx0, ggml_permute(ctx0, xxx, 0, 1, 3, 2));
  11430. xxx = ggml_mul_mat(
  11431. ctx0,
  11432. ggml_reshape_4d(
  11433. ctx0,
  11434. layer.time_mix_w2,
  11435. layer.time_mix_w2->ne[0], layer.time_mix_w2->ne[1], 1, 5
  11436. ),
  11437. xxx
  11438. );
  11439. ggml_tensor *xw, *xk, *xv, *xr, *xg;
  11440. if (layer.time_mix_lerp_fused) {
  11441. // fusing these weights makes some performance improvement
  11442. sx = ggml_reshape_3d(ctx0, sx, n_embd, 1, n_tokens);
  11443. cur = ggml_reshape_3d(ctx0, cur, n_embd, 1, n_tokens);
  11444. xxx = ggml_add(ctx0, ggml_mul(ctx0, ggml_add(ctx0, xxx, layer.time_mix_lerp_fused), sx), cur);
  11445. xw = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], 0);
  11446. xk = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], n_embd * n_tokens * sizeof(float));
  11447. xv = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], n_embd * n_tokens * 2 * sizeof(float));
  11448. xr = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], n_embd * n_tokens * 3 * sizeof(float));
  11449. xg = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], n_embd * n_tokens * 4 * sizeof(float));
  11450. } else {
  11451. // for backward compatibility
  11452. xw = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], 0);
  11453. xk = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], n_embd * n_tokens * sizeof(float));
  11454. xv = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], n_embd * n_tokens * 2 * sizeof(float));
  11455. xr = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], n_embd * n_tokens * 3 * sizeof(float));
  11456. xg = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], n_embd * n_tokens * 4 * sizeof(float));
  11457. xw = ggml_add(ctx0, ggml_mul(ctx0, ggml_add(ctx0, xw, layer.time_mix_lerp_w), sx), cur);
  11458. xk = ggml_add(ctx0, ggml_mul(ctx0, ggml_add(ctx0, xk, layer.time_mix_lerp_k), sx), cur);
  11459. xv = ggml_add(ctx0, ggml_mul(ctx0, ggml_add(ctx0, xv, layer.time_mix_lerp_v), sx), cur);
  11460. xr = ggml_add(ctx0, ggml_mul(ctx0, ggml_add(ctx0, xr, layer.time_mix_lerp_r), sx), cur);
  11461. xg = ggml_add(ctx0, ggml_mul(ctx0, ggml_add(ctx0, xg, layer.time_mix_lerp_g), sx), cur);
  11462. }
  11463. ggml_tensor * r = build_lora_mm(layer.time_mix_receptance, xr);
  11464. ggml_tensor * k = build_lora_mm(layer.time_mix_key, xk);
  11465. ggml_tensor * v = build_lora_mm(layer.time_mix_value, xv);
  11466. if (layer.time_mix_receptance_b) {
  11467. r = ggml_add(ctx0, r, layer.time_mix_receptance_b);
  11468. }
  11469. if (layer.time_mix_key_b) {
  11470. k = ggml_add(ctx0, k, layer.time_mix_key_b);
  11471. }
  11472. if (layer.time_mix_value_b) {
  11473. v = ggml_add(ctx0, v, layer.time_mix_value_b);
  11474. }
  11475. ggml_tensor * g = build_lora_mm(layer.time_mix_gate, xg);
  11476. if (is_qrwkv) {
  11477. g = ggml_sigmoid(ctx0, g);
  11478. } else {
  11479. g = ggml_silu(ctx0, g);
  11480. }
  11481. if (n_head_kv != 0 && n_head_kv != n_head) {
  11482. GGML_ASSERT(n_head % n_head_kv == 0);
  11483. k = ggml_reshape_4d(ctx0, k, head_size, 1, n_head_kv, n_tokens);
  11484. v = ggml_reshape_4d(ctx0, v, head_size, 1, n_head_kv, n_tokens);
  11485. ggml_tensor * tmp = ggml_new_tensor_4d(ctx0, GGML_TYPE_F32, head_size, n_head / n_head_kv, n_head_kv, n_tokens);
  11486. k = ggml_repeat(ctx0, k, tmp);
  11487. v = ggml_repeat(ctx0, v, tmp);
  11488. }
  11489. k = ggml_reshape_3d(ctx0, k, head_size, n_head, n_tokens);
  11490. v = ggml_reshape_3d(ctx0, v, head_size, n_head, n_tokens);
  11491. r = ggml_reshape_3d(ctx0, r, head_size, n_head, n_tokens);
  11492. ggml_tensor * w = ggml_mul_mat(
  11493. ctx0,
  11494. layer.time_mix_decay_w2,
  11495. ggml_tanh(
  11496. ctx0,
  11497. ggml_mul_mat(ctx0, layer.time_mix_decay_w1, xw)
  11498. )
  11499. );
  11500. w = ggml_add(ctx0, w, layer.time_mix_decay);
  11501. w = ggml_exp(ctx0, ggml_neg(ctx0, ggml_exp(ctx0, w)));
  11502. w = ggml_reshape_3d(ctx0, w, head_size, n_head, n_tokens);
  11503. if (is_qrwkv) {
  11504. // k = k * (1 - w)
  11505. k = ggml_sub(ctx0, k, ggml_mul(ctx0, k, w));
  11506. }
  11507. ggml_tensor * wkv_state = build_rs(
  11508. inp, mctx_cur->get_s_l(il),
  11509. hparams.n_embd_s(), n_seqs);
  11510. ggml_tensor * wkv_output;
  11511. if (is_qrwkv) {
  11512. wkv_output = ggml_gated_linear_attn(ctx0, k, v, r, w, wkv_state, pow(head_size, -0.5f));
  11513. } else {
  11514. wkv_output = ggml_rwkv_wkv6(ctx0, k, v, r, layer.time_mix_first, w, wkv_state);
  11515. }
  11516. cur = ggml_view_1d(ctx0, wkv_output, n_embd * n_tokens, 0);
  11517. wkv_state = ggml_view_1d(ctx0, wkv_output, n_embd * head_size * n_seqs, n_embd * n_tokens * sizeof(float));
  11518. ggml_build_forward_expand(
  11519. gf,
  11520. ggml_cpy(
  11521. ctx0,
  11522. wkv_state,
  11523. ggml_view_1d(
  11524. ctx0,
  11525. mctx_cur->get_s_l(il),
  11526. hparams.n_embd_s() * n_seqs,
  11527. hparams.n_embd_s() * kv_head * ggml_element_size(mctx_cur->get_s_l(il))
  11528. )
  11529. )
  11530. );
  11531. if (!is_qrwkv) {
  11532. // group norm with head_count groups
  11533. cur = ggml_reshape_3d(ctx0, cur, n_embd / n_head, n_head, n_tokens);
  11534. cur = ggml_norm(ctx0, cur, 64e-5f);
  11535. // Convert back to regular vectors.
  11536. cur = ggml_reshape_2d(ctx0, cur, n_embd, n_tokens);
  11537. cur = ggml_add(ctx0, ggml_mul(ctx0, cur, layer.time_mix_ln), layer.time_mix_ln_b);
  11538. } else {
  11539. cur = ggml_reshape_2d(ctx0, cur, n_embd, n_tokens);
  11540. }
  11541. cur = ggml_mul(ctx0, cur, g);
  11542. cur = build_lora_mm(layer.time_mix_output, cur);
  11543. return ggml_reshape_3d(ctx0, cur, n_embd, n_seq_tokens, n_seqs);
  11544. }
  11545. };
  11546. struct llm_build_rwkv6 : public llm_build_rwkv6_base {
  11547. llm_build_rwkv6(const llama_model & model, const llm_graph_params & params) : llm_build_rwkv6_base(model, params) {
  11548. GGML_ASSERT(hparams.token_shift_count == 2);
  11549. ggml_tensor * cur;
  11550. ggml_tensor * inpL;
  11551. inpL = build_inp_embd(model.tok_embd);
  11552. inpL = build_norm(inpL, model.tok_norm, model.tok_norm_b, LLM_NORM, -1);
  11553. auto * rs_inp = build_rs_inp();
  11554. const auto n_embd = hparams.n_embd;
  11555. const auto n_seq_tokens = ubatch.n_seq_tokens;
  11556. const auto n_seqs = ubatch.n_seqs;
  11557. ggml_tensor * inp_out_ids = build_inp_out_ids();
  11558. for (int il = 0; il < n_layer; ++il) {
  11559. const llama_layer * layer = &model.layers[il];
  11560. inpL = ggml_reshape_3d(ctx0, inpL, n_embd, n_seq_tokens, n_seqs);
  11561. ggml_tensor * token_shift = build_rwkv_token_shift_load(rs_inp, ubatch, il);
  11562. ggml_tensor * att_shift = ggml_view_3d(ctx0, token_shift, n_embd, 1, n_seqs, token_shift->nb[1], token_shift->nb[2], 0);
  11563. ggml_tensor * ffn_shift = ggml_view_3d(ctx0, token_shift, n_embd, 1, n_seqs, token_shift->nb[1], token_shift->nb[2], n_embd * ggml_element_size(token_shift));
  11564. ggml_tensor * att_norm = build_norm(inpL, layer->attn_norm, layer->attn_norm_b, LLM_NORM, il);
  11565. cb(att_norm, "attn_norm", il);
  11566. ggml_tensor * x_prev = ggml_concat(
  11567. ctx0,
  11568. att_shift,
  11569. ggml_view_3d(ctx0, att_norm, n_embd, n_seq_tokens - 1, n_seqs, att_norm->nb[1], att_norm->nb[2], 0),
  11570. 1
  11571. );
  11572. cur = build_rwkv6_time_mix(rs_inp, att_norm, x_prev, ubatch, il);
  11573. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
  11574. cb(ffn_inp, "ffn_inp", il);
  11575. ggml_tensor * ffn_norm = build_norm(ffn_inp, layer->attn_norm_2, layer->attn_norm_2_b, LLM_NORM, il);
  11576. cb(ffn_norm, "ffn_norm", il);
  11577. x_prev = ggml_concat(
  11578. ctx0,
  11579. ffn_shift,
  11580. ggml_view_3d(ctx0, ffn_norm, n_embd, n_seq_tokens - 1, n_seqs, ffn_norm->nb[1], ffn_norm->nb[2], 0),
  11581. 1
  11582. );
  11583. token_shift = ggml_concat(ctx0,
  11584. ggml_view_3d(ctx0, att_norm, n_embd, 1, n_seqs, att_norm->nb[1], att_norm->nb[2], (n_seq_tokens-1)*n_embd*ggml_element_size(att_norm)),
  11585. ggml_view_3d(ctx0, ffn_norm, n_embd, 1, n_seqs, ffn_norm->nb[1], ffn_norm->nb[2], (n_seq_tokens-1)*n_embd*ggml_element_size(ffn_norm)),
  11586. 1
  11587. );
  11588. ggml_build_forward_expand(gf, build_rwkv_token_shift_store(token_shift, ubatch, il));
  11589. ffn_inp = ggml_reshape_2d(ctx0, ffn_inp, n_embd, n_tokens);
  11590. ffn_norm = ggml_reshape_2d(ctx0, ffn_norm, n_embd, n_tokens);
  11591. x_prev = ggml_reshape_2d(ctx0, x_prev, n_embd, n_tokens);
  11592. cur = ggml_reshape_2d(ctx0, cur, n_embd, n_tokens);
  11593. if (il == n_layer - 1 && inp_out_ids) {
  11594. ffn_inp = ggml_get_rows(ctx0, ffn_inp, inp_out_ids);
  11595. ffn_norm = ggml_get_rows(ctx0, ffn_norm, inp_out_ids);
  11596. x_prev = ggml_get_rows(ctx0, x_prev, inp_out_ids);
  11597. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  11598. }
  11599. cur = build_rwkv6_channel_mix(layer, ffn_norm, x_prev, LLM_ARCH_RWKV6);
  11600. cur = ggml_add(ctx0, cur, ffn_inp);
  11601. if (hparams.rescale_every_n_layers != 0 && (il + 1) % hparams.rescale_every_n_layers == 0) {
  11602. cur = ggml_scale(ctx0, cur, 0.5F);
  11603. }
  11604. cur = build_cvec(cur, il);
  11605. cb(cur, "l_out", il);
  11606. // input for next layer
  11607. inpL = cur;
  11608. }
  11609. cur = inpL;
  11610. cur = build_norm(cur, model.output_norm, model.output_norm_b, LLM_NORM, -1);
  11611. cb(cur, "result_norm", -1);
  11612. res->t_embd = cur;
  11613. cur = build_lora_mm(model.output, cur);
  11614. cb(cur, "result_output", -1);
  11615. res->t_logits = cur;
  11616. ggml_build_forward_expand(gf, cur);
  11617. }
  11618. };
  11619. // ref: https://huggingface.co/recursal/QRWKV6-32B-Instruct-Preview-v0.1/blob/main/modeling_rwkv6qwen2.py
  11620. struct llm_build_rwkv6qwen2 : public llm_build_rwkv6_base {
  11621. llm_build_rwkv6qwen2(const llama_model & model, const llm_graph_params & params) : llm_build_rwkv6_base(model, params) {
  11622. GGML_ASSERT(n_embd == hparams.n_embd_r());
  11623. ggml_tensor * cur;
  11624. ggml_tensor * inpL;
  11625. inpL = build_inp_embd(model.tok_embd);
  11626. auto * rs_inp = build_rs_inp();
  11627. const auto n_embd = hparams.n_embd;
  11628. const auto n_seq_tokens = ubatch.n_seq_tokens;
  11629. const auto n_seqs = ubatch.n_seqs;
  11630. ggml_tensor * inp_out_ids = build_inp_out_ids();
  11631. for (int il = 0; il < n_layer; ++il) {
  11632. const llama_layer * layer = &model.layers[il];
  11633. inpL = ggml_reshape_3d(ctx0, inpL, n_embd, n_seq_tokens, n_seqs);
  11634. ggml_tensor * token_shift = build_rwkv_token_shift_load(rs_inp, ubatch, il);
  11635. ggml_tensor * att_norm = build_norm(inpL, layer->attn_norm, layer->attn_norm_b, LLM_NORM_RMS, il);
  11636. cb(att_norm, "attn_norm", il);
  11637. ggml_tensor * x_prev = ggml_concat(
  11638. ctx0,
  11639. token_shift,
  11640. ggml_view_3d(ctx0, att_norm, n_embd, n_seq_tokens - 1, n_seqs, att_norm->nb[1], att_norm->nb[2], 0),
  11641. 1
  11642. );
  11643. cur = build_rwkv6_time_mix(rs_inp, att_norm, x_prev, ubatch, il);
  11644. token_shift = ggml_view_3d(ctx0, att_norm, n_embd, 1, n_seqs, att_norm->nb[1], att_norm->nb[2], (n_seq_tokens-1)*n_embd*ggml_element_size(att_norm));
  11645. ggml_build_forward_expand(gf, build_rwkv_token_shift_store(token_shift, ubatch, il));
  11646. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
  11647. cb(ffn_inp, "ffn_inp", il);
  11648. cur = ggml_reshape_2d(ctx0, cur, n_embd, n_tokens);
  11649. ffn_inp = ggml_reshape_2d(ctx0, ffn_inp, n_embd, n_tokens);
  11650. if (il == n_layer - 1 && inp_out_ids) {
  11651. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  11652. ffn_inp = ggml_get_rows(ctx0, ffn_inp, inp_out_ids);
  11653. }
  11654. // feed-forward network
  11655. cur = build_norm(ffn_inp,
  11656. model.layers[il].ffn_norm, NULL,
  11657. LLM_NORM_RMS, il);
  11658. cb(cur, "ffn_norm", il);
  11659. cur = build_ffn(cur,
  11660. model.layers[il].ffn_up, NULL, NULL,
  11661. model.layers[il].ffn_gate, NULL, NULL,
  11662. model.layers[il].ffn_down, NULL, NULL,
  11663. NULL,
  11664. LLM_FFN_SILU, LLM_FFN_PAR, il);
  11665. cb(cur, "ffn_out", il);
  11666. cur = ggml_add(ctx0, cur, ffn_inp);
  11667. cur = build_cvec(cur, il);
  11668. cb(cur, "l_out", il);
  11669. // input for next layer
  11670. inpL = cur;
  11671. }
  11672. cur = inpL;
  11673. cur = build_norm(cur, model.output_norm, model.output_norm_b, LLM_NORM_RMS, -1);
  11674. cb(cur, "result_norm", -1);
  11675. res->t_embd = cur;
  11676. cur = build_lora_mm(model.output, cur);
  11677. cb(cur, "result_output", -1);
  11678. res->t_logits = cur;
  11679. ggml_build_forward_expand(gf, cur);
  11680. }
  11681. };
  11682. struct llm_build_rwkv7_base : public llm_graph_context {
  11683. const llama_model & model;
  11684. llm_build_rwkv7_base(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params), model(model) {
  11685. }
  11686. ggml_tensor * build_rwkv7_channel_mix(
  11687. const llama_layer * layer,
  11688. ggml_tensor * cur,
  11689. ggml_tensor * x_prev,
  11690. llm_arch arch) const {
  11691. ggml_tensor * sx = ggml_sub(ctx0, x_prev, cur);
  11692. switch (arch) {
  11693. case LLM_ARCH_RWKV7:
  11694. {
  11695. ggml_tensor * xk = ggml_add(ctx0, ggml_mul(ctx0, sx, layer->channel_mix_lerp_k), cur);
  11696. ggml_tensor * k = ggml_sqr(
  11697. ctx0,
  11698. ggml_relu(
  11699. ctx0,
  11700. build_lora_mm(layer->channel_mix_key, xk)
  11701. )
  11702. );
  11703. cur = build_lora_mm(layer->channel_mix_value, k);
  11704. } break;
  11705. default:
  11706. GGML_ABORT("fatal error");
  11707. }
  11708. return cur;
  11709. }
  11710. ggml_tensor * build_rwkv7_time_mix(
  11711. llm_graph_input_rs * inp,
  11712. ggml_tensor * cur,
  11713. ggml_tensor * x_prev,
  11714. ggml_tensor *& first_layer_value,
  11715. const llama_ubatch & ubatch,
  11716. int il) const {
  11717. const auto * mctx_cur = static_cast<const llama_memory_recurrent_context *>(mctx);
  11718. const auto n_tokens = ubatch.n_tokens;
  11719. const auto n_seqs = ubatch.n_seqs;
  11720. const auto n_embd = hparams.n_embd;
  11721. const auto head_size = hparams.wkv_head_size;
  11722. const auto head_count = n_embd / head_size;
  11723. const auto n_seq_tokens = ubatch.n_seq_tokens;
  11724. const auto kv_head = mctx_cur->get_head();
  11725. const auto & layer = model.layers[il];
  11726. bool has_gating = layer.time_mix_g1 && layer.time_mix_g2;
  11727. ggml_tensor * sx = ggml_sub(ctx0, x_prev, cur);
  11728. ggml_tensor * dummy = ggml_new_tensor_4d(ctx0, GGML_TYPE_F32, n_embd, n_seq_tokens, n_seqs, has_gating ? 6 : 5);
  11729. sx = ggml_repeat(ctx0, sx, dummy);
  11730. ggml_tensor * xxx = ggml_add(ctx0, ggml_mul(ctx0, sx, layer.time_mix_lerp_fused), cur);
  11731. ggml_tensor * xr = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], 0);
  11732. ggml_tensor * xw = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], n_embd * n_tokens * sizeof(float));
  11733. ggml_tensor * xk = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], n_embd * n_tokens * 2 * sizeof(float));
  11734. ggml_tensor * xv = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], n_embd * n_tokens * 3 * sizeof(float));
  11735. ggml_tensor * xa = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], n_embd * n_tokens * 4 * sizeof(float));
  11736. ggml_tensor * xg = has_gating ? ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], n_embd * n_tokens * 5 * sizeof(float)) : nullptr;
  11737. ggml_tensor * r = build_lora_mm(layer.time_mix_receptance, xr);
  11738. ggml_tensor * w = ggml_add(
  11739. ctx0,
  11740. ggml_mul_mat(ctx0, layer.time_mix_w2, ggml_tanh(ctx0, ggml_mul_mat(ctx0, layer.time_mix_w1, xw))),
  11741. layer.time_mix_w0
  11742. );
  11743. w = ggml_exp(ctx0, ggml_scale(ctx0, ggml_sigmoid(ctx0, w), -0.606531));
  11744. ggml_tensor * k = build_lora_mm(layer.time_mix_key, xk);
  11745. ggml_tensor * v = build_lora_mm(layer.time_mix_value, xv);
  11746. if (first_layer_value == nullptr) {
  11747. first_layer_value = v;
  11748. } else {
  11749. // Add the first layer value as a residual connection.
  11750. v = ggml_add(ctx0, v,
  11751. ggml_mul(ctx0,
  11752. ggml_sub(ctx0, first_layer_value, v),
  11753. ggml_sigmoid(ctx0, ggml_add(ctx0,
  11754. ggml_mul_mat(ctx0, layer.time_mix_v2, ggml_mul_mat(ctx0, layer.time_mix_v1, xv)),
  11755. layer.time_mix_v0
  11756. )
  11757. )
  11758. )
  11759. );
  11760. }
  11761. ggml_tensor * g = nullptr;
  11762. if (layer.time_mix_g1 && layer.time_mix_g2) {
  11763. g = ggml_mul_mat(ctx0, layer.time_mix_g2, ggml_sigmoid(ctx0, ggml_mul_mat(ctx0, layer.time_mix_g1, xg)));
  11764. }
  11765. ggml_tensor * a = ggml_sigmoid(ctx0,
  11766. ggml_add(
  11767. ctx0,
  11768. ggml_mul_mat(ctx0, layer.time_mix_a2, ggml_mul_mat(ctx0, layer.time_mix_a1, xa)),
  11769. layer.time_mix_a0
  11770. )
  11771. );
  11772. ggml_tensor * kk = ggml_reshape_3d(ctx0, ggml_mul(ctx0, k, layer.time_mix_k_k), head_size, head_count, n_tokens);
  11773. kk = ggml_l2_norm(ctx0, kk, 1e-12);
  11774. ggml_tensor * ka = ggml_mul(ctx0, k, layer.time_mix_k_a);
  11775. k = ggml_add(ctx0, k, ggml_sub(ctx0, ggml_mul(ctx0, a, ka), ka));
  11776. r = ggml_reshape_3d(ctx0, r, head_size, head_count, n_tokens);
  11777. w = ggml_reshape_3d(ctx0, w, head_size, head_count, n_tokens);
  11778. k = ggml_reshape_3d(ctx0, k, head_size, head_count, n_tokens);
  11779. v = ggml_reshape_3d(ctx0, v, head_size, head_count, n_tokens);
  11780. a = ggml_reshape_3d(ctx0, a, head_size, head_count, n_tokens);
  11781. ggml_tensor * wkv_state = build_rs(
  11782. inp, mctx_cur->get_s_l(il),
  11783. hparams.n_embd_s(), n_seqs);
  11784. ggml_tensor * wkv_output = ggml_rwkv_wkv7(ctx0, r, w, k, v, ggml_neg(ctx0, kk), ggml_mul(ctx0, kk, a), wkv_state);
  11785. cur = ggml_view_1d(ctx0, wkv_output, n_embd * n_tokens, 0);
  11786. wkv_state = ggml_view_1d(ctx0, wkv_output, n_embd * head_size * n_seqs, n_embd * n_tokens * sizeof(float));
  11787. ggml_build_forward_expand(
  11788. gf,
  11789. ggml_cpy(
  11790. ctx0,
  11791. wkv_state,
  11792. ggml_view_1d(
  11793. ctx0,
  11794. mctx_cur->get_s_l(il),
  11795. hparams.n_embd_s() * n_seqs,
  11796. hparams.n_embd_s() * kv_head * ggml_element_size(mctx_cur->get_s_l(il))
  11797. )
  11798. )
  11799. );
  11800. if (layer.time_mix_ln && layer.time_mix_ln_b) {
  11801. // group norm with head_count groups
  11802. cur = ggml_reshape_3d(ctx0, cur, n_embd / head_count, head_count, n_tokens);
  11803. cur = ggml_norm(ctx0, cur, 64e-5f);
  11804. // Convert back to regular vectors.
  11805. cur = ggml_reshape_2d(ctx0, cur, n_embd, n_tokens);
  11806. cur = ggml_add(ctx0, ggml_mul(ctx0, cur, layer.time_mix_ln), layer.time_mix_ln_b);
  11807. } else {
  11808. cur = ggml_reshape_2d(ctx0, cur, n_embd, n_tokens);
  11809. }
  11810. ggml_tensor * rk = ggml_sum_rows(ctx0,
  11811. ggml_mul(ctx0, ggml_mul(ctx0, k, r), ggml_reshape_2d(ctx0, layer.time_mix_r_k, head_size, head_count)));
  11812. cur = ggml_add(ctx0, cur, ggml_reshape_2d(ctx0, ggml_mul(ctx0, v, rk), n_embd, n_tokens));
  11813. if (has_gating) {
  11814. cur = ggml_mul(ctx0, cur, g);
  11815. }
  11816. cur = build_lora_mm(layer.time_mix_output, cur);
  11817. return ggml_reshape_3d(ctx0, cur, n_embd, n_seq_tokens, n_seqs);
  11818. }
  11819. };
  11820. struct llm_build_rwkv7 : public llm_build_rwkv7_base {
  11821. llm_build_rwkv7(const llama_model & model, const llm_graph_params & params) : llm_build_rwkv7_base(model, params) {
  11822. GGML_ASSERT(hparams.token_shift_count == 2);
  11823. ggml_tensor * cur;
  11824. ggml_tensor * inpL;
  11825. ggml_tensor * v_first = nullptr;
  11826. inpL = build_inp_embd(model.tok_embd);
  11827. inpL = build_norm(inpL, model.tok_norm, model.tok_norm_b, LLM_NORM, -1);
  11828. auto * rs_inp = build_rs_inp();
  11829. const auto n_embd = hparams.n_embd;
  11830. const auto n_seq_tokens = ubatch.n_seq_tokens;
  11831. const auto n_seqs = ubatch.n_seqs;
  11832. ggml_tensor * inp_out_ids = build_inp_out_ids();
  11833. for (int il = 0; il < n_layer; ++il) {
  11834. const llama_layer * layer = &model.layers[il];
  11835. inpL = ggml_reshape_3d(ctx0, inpL, n_embd, n_seq_tokens, n_seqs);
  11836. ggml_tensor * token_shift = build_rwkv_token_shift_load(rs_inp, ubatch, il);
  11837. ggml_tensor * att_shift = ggml_view_3d(ctx0, token_shift, n_embd, 1, n_seqs, token_shift->nb[1], token_shift->nb[2], 0);
  11838. ggml_tensor * ffn_shift = ggml_view_3d(ctx0, token_shift, n_embd, 1, n_seqs, token_shift->nb[1], token_shift->nb[2], n_embd * ggml_element_size(token_shift));
  11839. ggml_tensor * att_norm = build_norm(inpL, layer->attn_norm, layer->attn_norm_b, LLM_NORM, il);
  11840. cb(att_norm, "attn_norm", il);
  11841. ggml_tensor * x_prev = ggml_concat(
  11842. ctx0,
  11843. att_shift,
  11844. ggml_view_3d(ctx0, att_norm, n_embd, n_seq_tokens - 1, n_seqs, att_norm->nb[1], att_norm->nb[2], 0),
  11845. 1
  11846. );
  11847. cur = build_rwkv7_time_mix(rs_inp, att_norm, x_prev, v_first, ubatch, il);
  11848. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
  11849. cb(ffn_inp, "ffn_inp", il);
  11850. ggml_tensor * ffn_norm = build_norm(ffn_inp, layer->attn_norm_2, layer->attn_norm_2_b, LLM_NORM, il);
  11851. cb(ffn_norm, "ffn_norm", il);
  11852. x_prev = ggml_concat(
  11853. ctx0,
  11854. ffn_shift,
  11855. ggml_view_3d(ctx0, ffn_norm, n_embd, n_seq_tokens - 1, n_seqs, ffn_norm->nb[1], ffn_norm->nb[2], 0),
  11856. 1
  11857. );
  11858. token_shift = ggml_concat(ctx0,
  11859. ggml_view_3d(ctx0, att_norm, n_embd, 1, n_seqs, att_norm->nb[1], att_norm->nb[2], (n_seq_tokens-1)*n_embd*ggml_element_size(att_norm)),
  11860. ggml_view_3d(ctx0, ffn_norm, n_embd, 1, n_seqs, ffn_norm->nb[1], ffn_norm->nb[2], (n_seq_tokens-1)*n_embd*ggml_element_size(ffn_norm)),
  11861. 1
  11862. );
  11863. ggml_build_forward_expand(gf, build_rwkv_token_shift_store(token_shift, ubatch, il));
  11864. ffn_inp = ggml_reshape_2d(ctx0, ffn_inp, n_embd, n_tokens);
  11865. ffn_norm = ggml_reshape_2d(ctx0, ffn_norm, n_embd, n_tokens);
  11866. x_prev = ggml_reshape_2d(ctx0, x_prev, n_embd, n_tokens);
  11867. if (il == n_layer - 1 && inp_out_ids) {
  11868. ffn_inp = ggml_get_rows(ctx0, ffn_inp, inp_out_ids);
  11869. ffn_norm = ggml_get_rows(ctx0, ffn_norm, inp_out_ids);
  11870. x_prev = ggml_get_rows(ctx0, x_prev, inp_out_ids);
  11871. }
  11872. cur = build_rwkv7_channel_mix(layer, ffn_norm, x_prev, LLM_ARCH_RWKV7);
  11873. cur = ggml_add(ctx0, cur, ffn_inp);
  11874. cur = build_cvec(cur, il);
  11875. cb(cur, "l_out", il);
  11876. // input for next layer
  11877. inpL = cur;
  11878. }
  11879. cur = inpL;
  11880. cur = build_norm(cur, model.output_norm, model.output_norm_b, LLM_NORM, -1);
  11881. cb(cur, "result_norm", -1);
  11882. res->t_embd = cur;
  11883. cur = build_lora_mm(model.output, cur);
  11884. cb(cur, "result_output", -1);
  11885. res->t_logits = cur;
  11886. ggml_build_forward_expand(gf, cur);
  11887. }
  11888. };
  11889. struct llm_build_arwkv7 : public llm_build_rwkv7_base {
  11890. llm_build_arwkv7(const llama_model & model, const llm_graph_params & params) : llm_build_rwkv7_base(model, params) {
  11891. GGML_ASSERT(n_embd == hparams.n_embd_r());
  11892. ggml_tensor * cur;
  11893. ggml_tensor * inpL;
  11894. ggml_tensor * v_first = nullptr;
  11895. inpL = build_inp_embd(model.tok_embd);
  11896. auto * rs_inp = build_rs_inp();
  11897. const auto n_embd = hparams.n_embd;
  11898. const auto n_seq_tokens = ubatch.n_seq_tokens;
  11899. const auto n_seqs = ubatch.n_seqs;
  11900. ggml_tensor * inp_out_ids = build_inp_out_ids();
  11901. for (int il = 0; il < n_layer; ++il) {
  11902. const llama_layer * layer = &model.layers[il];
  11903. inpL = ggml_reshape_3d(ctx0, inpL, n_embd, n_seq_tokens, n_seqs);
  11904. ggml_tensor * token_shift = build_rwkv_token_shift_load(rs_inp, ubatch, il);
  11905. ggml_tensor * att_norm = build_norm(inpL, layer->attn_norm, layer->attn_norm_b, LLM_NORM_RMS, il);
  11906. cb(att_norm, "attn_norm", il);
  11907. ggml_tensor * x_prev = ggml_concat(
  11908. ctx0,
  11909. token_shift,
  11910. ggml_view_3d(ctx0, att_norm, n_embd, n_seq_tokens - 1, n_seqs, att_norm->nb[1], att_norm->nb[2], 0),
  11911. 1
  11912. );
  11913. cur = build_rwkv7_time_mix(rs_inp, att_norm, x_prev, v_first, ubatch, il);
  11914. token_shift = ggml_view_3d(ctx0, att_norm, n_embd, 1, n_seqs, att_norm->nb[1], att_norm->nb[2], (n_seq_tokens-1)*n_embd*ggml_element_size(att_norm));
  11915. ggml_build_forward_expand(gf, build_rwkv_token_shift_store(token_shift, ubatch, il));
  11916. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
  11917. cb(ffn_inp, "ffn_inp", il);
  11918. cur = ggml_reshape_2d(ctx0, cur, n_embd, n_tokens);
  11919. ffn_inp = ggml_reshape_2d(ctx0, ffn_inp, n_embd, n_tokens);
  11920. if (il == n_layer - 1 && inp_out_ids) {
  11921. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  11922. ffn_inp = ggml_get_rows(ctx0, ffn_inp, inp_out_ids);
  11923. }
  11924. // feed-forward network
  11925. cur = build_norm(ffn_inp,
  11926. model.layers[il].ffn_norm, NULL,
  11927. LLM_NORM_RMS, il);
  11928. cb(cur, "ffn_norm", il);
  11929. cur = build_ffn(cur,
  11930. model.layers[il].ffn_up, NULL, NULL,
  11931. model.layers[il].ffn_gate, NULL, NULL,
  11932. model.layers[il].ffn_down, NULL, NULL,
  11933. NULL,
  11934. LLM_FFN_SILU, LLM_FFN_PAR, il);
  11935. cb(cur, "ffn_out", il);
  11936. cur = ggml_add(ctx0, cur, ffn_inp);
  11937. cur = build_cvec(cur, il);
  11938. cb(cur, "l_out", il);
  11939. // input for next layer
  11940. inpL = cur;
  11941. }
  11942. cur = inpL;
  11943. cur = build_norm(cur, model.output_norm, model.output_norm_b, LLM_NORM_RMS, -1);
  11944. cb(cur, "result_norm", -1);
  11945. res->t_embd = cur;
  11946. cur = build_lora_mm(model.output, cur);
  11947. cb(cur, "result_output", -1);
  11948. res->t_logits = cur;
  11949. ggml_build_forward_expand(gf, cur);
  11950. }
  11951. };
  11952. struct llm_build_granite : public llm_graph_context {
  11953. llm_build_granite(
  11954. const llama_model & model,
  11955. const llm_graph_params & params)
  11956. : llm_graph_context(params) {
  11957. const int64_t n_embd_head = hparams.n_embd_head_v;
  11958. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  11959. GGML_ASSERT(n_embd_head == hparams.n_rot);
  11960. ggml_tensor * cur;
  11961. ggml_tensor * inpL;
  11962. inpL = build_inp_embd(model.tok_embd);
  11963. // inp_pos - built only if rope enabled
  11964. ggml_tensor * inp_pos = nullptr;
  11965. if (hparams.rope_finetuned) {
  11966. inp_pos = build_inp_pos();
  11967. }
  11968. auto * inp_attn = build_attn_inp_kv_unified();
  11969. ggml_tensor * inp_out_ids = build_inp_out_ids();
  11970. for (int il = 0; il < n_layer; ++il) {
  11971. ggml_tensor * inpSA = inpL;
  11972. // norm
  11973. cur = build_norm(inpL,
  11974. model.layers[il].attn_norm, NULL,
  11975. LLM_NORM_RMS, il);
  11976. cb(cur, "attn_norm", il);
  11977. // self-attention
  11978. cur = build_attention_layer(
  11979. cur, inp_pos, inp_attn,
  11980. model, n_embd_head, il);
  11981. if (il == n_layer - 1 && inp_out_ids) {
  11982. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  11983. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  11984. }
  11985. // ffn
  11986. cur = build_layer_ffn(cur, inpSA, model, il);
  11987. // input for next layer
  11988. inpL = cur;
  11989. }
  11990. cur = inpL;
  11991. cur = build_norm(cur,
  11992. model.output_norm, NULL,
  11993. LLM_NORM_RMS, -1);
  11994. cb(cur, "result_norm", -1);
  11995. res->t_embd = cur;
  11996. // lm_head
  11997. cur = build_lora_mm(model.output, cur);
  11998. // For Granite architectures - scale logits
  11999. cur = ggml_scale(ctx0, cur, 1.0f / hparams.f_logit_scale);
  12000. cb(cur, "result_output", -1);
  12001. res->t_logits = cur;
  12002. ggml_build_forward_expand(gf, cur);
  12003. }
  12004. ggml_tensor * build_attention_layer(
  12005. ggml_tensor * cur,
  12006. ggml_tensor * inp_pos,
  12007. llm_graph_input_attn_kv_unified * inp_attn,
  12008. const llama_model & model,
  12009. const int64_t n_embd_head,
  12010. const int il) {
  12011. // compute Q and K and (optionally) RoPE them
  12012. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  12013. cb(Qcur, "Qcur", il);
  12014. if (model.layers[il].bq) {
  12015. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  12016. cb(Qcur, "Qcur", il);
  12017. }
  12018. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  12019. cb(Kcur, "Kcur", il);
  12020. if (model.layers[il].bk) {
  12021. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  12022. cb(Kcur, "Kcur", il);
  12023. }
  12024. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  12025. cb(Vcur, "Vcur", il);
  12026. if (model.layers[il].bv) {
  12027. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  12028. cb(Vcur, "Vcur", il);
  12029. }
  12030. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, hparams.n_head(il), n_tokens);
  12031. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, hparams.n_head_kv(il), n_tokens);
  12032. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, hparams.n_head_kv(il), n_tokens);
  12033. const bool use_rope = hparams.rope_finetuned;
  12034. if (use_rope) {
  12035. ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
  12036. Qcur = ggml_rope_ext(
  12037. ctx0, Qcur, inp_pos, rope_factors,
  12038. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  12039. ext_factor, attn_factor, beta_fast, beta_slow
  12040. );
  12041. Kcur = ggml_rope_ext(
  12042. ctx0, Kcur, inp_pos, rope_factors,
  12043. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  12044. ext_factor, attn_factor, beta_fast, beta_slow
  12045. );
  12046. }
  12047. cb(Qcur, "Qcur", il);
  12048. cb(Kcur, "Kcur", il);
  12049. cb(Vcur, "Vcur", il);
  12050. const float kq_scale = hparams.f_attention_scale == 0.0f ? 1.0f/sqrtf(float(n_embd_head)) : hparams.f_attention_scale;
  12051. cur = build_attn(inp_attn,
  12052. model.layers[il].wo, model.layers[il].bo,
  12053. Qcur, Kcur, Vcur, nullptr, nullptr, kq_scale, il);
  12054. cb(cur, "attn_out", il);
  12055. return cur;
  12056. }
  12057. ggml_tensor * build_layer_ffn(
  12058. ggml_tensor * cur,
  12059. ggml_tensor * inpSA,
  12060. const llama_model & model,
  12061. const int il) {
  12062. // For Granite architectures - scale residual
  12063. if (hparams.f_residual_scale) {
  12064. cur = ggml_scale(ctx0, cur, hparams.f_residual_scale);
  12065. }
  12066. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  12067. cb(ffn_inp, "ffn_inp", il);
  12068. // feed-forward network (non-MoE)
  12069. if (model.layers[il].ffn_gate_inp == nullptr) {
  12070. cur = build_norm(ffn_inp,
  12071. model.layers[il].ffn_norm, NULL,
  12072. LLM_NORM_RMS, il);
  12073. cb(cur, "ffn_norm", il);
  12074. cur = build_ffn(cur,
  12075. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  12076. model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, NULL,
  12077. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  12078. NULL,
  12079. LLM_FFN_SILU, LLM_FFN_PAR, il);
  12080. cb(cur, "ffn_out", il);
  12081. } else {
  12082. // MoE branch
  12083. cur = build_norm(ffn_inp,
  12084. model.layers[il].ffn_norm, NULL,
  12085. LLM_NORM_RMS, il);
  12086. cb(cur, "ffn_norm", il);
  12087. ggml_tensor * moe_out = build_moe_ffn(cur,
  12088. model.layers[il].ffn_gate_inp,
  12089. model.layers[il].ffn_up_exps,
  12090. model.layers[il].ffn_gate_exps,
  12091. model.layers[il].ffn_down_exps,
  12092. nullptr,
  12093. n_expert, n_expert_used,
  12094. LLM_FFN_SILU, true,
  12095. false, 0.0,
  12096. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  12097. il);
  12098. cb(moe_out, "ffn_moe_out", il);
  12099. // For Granite MoE Shared
  12100. if (hparams.n_ff_shexp > 0) {
  12101. ggml_tensor * ffn_shexp = build_ffn(cur,
  12102. model.layers[il].ffn_up_shexp, NULL, NULL,
  12103. model.layers[il].ffn_gate_shexp, NULL, NULL,
  12104. model.layers[il].ffn_down_shexp, NULL, NULL,
  12105. NULL,
  12106. LLM_FFN_SILU, LLM_FFN_PAR, il);
  12107. cb(ffn_shexp, "ffn_shexp", il);
  12108. cur = ggml_add(ctx0, moe_out, ffn_shexp);
  12109. cb(cur, "ffn_out", il);
  12110. } else {
  12111. cur = moe_out;
  12112. }
  12113. }
  12114. // For Granite architectures - scale residual
  12115. if (hparams.f_residual_scale) {
  12116. cur = ggml_scale(ctx0, cur, hparams.f_residual_scale);
  12117. }
  12118. cur = ggml_add(ctx0, cur, ffn_inp);
  12119. cb(cur, "ffn_out", il);
  12120. cur = build_cvec(cur, il);
  12121. cb(cur, "l_out", il);
  12122. return cur;
  12123. }
  12124. };
  12125. struct llm_build_granite_hybrid : public llm_graph_context_mamba {
  12126. llm_build_granite_hybrid(
  12127. const llama_model & model,
  12128. const llm_graph_params & params) :
  12129. llm_graph_context_mamba(params) {
  12130. const int64_t n_embd_head = hparams.n_embd_head_v;
  12131. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  12132. ggml_tensor * cur;
  12133. ggml_tensor * inpL;
  12134. inpL = build_inp_embd(model.tok_embd);
  12135. auto * inp = build_inp_mem_hybrid();
  12136. ggml_tensor * inp_out_ids = build_inp_out_ids();
  12137. // Positional embeddings populated if rope enabled
  12138. ggml_tensor * inp_pos = nullptr;
  12139. if (hparams.rope_finetuned) {
  12140. inp_pos = build_inp_pos();
  12141. }
  12142. for (int il = 0; il < n_layer; ++il) {
  12143. struct ggml_tensor * inpSA = inpL;
  12144. // norm
  12145. cur = build_norm(inpL,
  12146. model.layers[il].attn_norm, NULL,
  12147. LLM_NORM_RMS, il);
  12148. cb(cur, "attn_norm", il);
  12149. if (hparams.is_recurrent(il)) {
  12150. // ssm layer //
  12151. cur = build_mamba2_layer(inp->get_recr(), cur, model, ubatch, il);
  12152. } else {
  12153. // attention layer //
  12154. cur = build_attention_layer(
  12155. cur, inp_pos, inp->get_attn(), model,
  12156. n_embd_head, il);
  12157. }
  12158. if (il == n_layer - 1 && inp_out_ids) {
  12159. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  12160. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  12161. }
  12162. // ffn
  12163. cur = build_layer_ffn(cur, inpSA, model, il);
  12164. // input for next layer
  12165. inpL = cur;
  12166. }
  12167. cur = inpL;
  12168. cur = build_norm(cur,
  12169. model.output_norm, NULL,
  12170. LLM_NORM_RMS, -1);
  12171. cb(cur, "result_norm", -1);
  12172. res->t_embd = cur;
  12173. // lm_head
  12174. cur = build_lora_mm(model.output, cur);
  12175. // For Granite architectures - scale logits
  12176. if (hparams.f_logit_scale) {
  12177. cur = ggml_scale(ctx0, cur, 1.0f / hparams.f_logit_scale);
  12178. }
  12179. cb(cur, "result_output", -1);
  12180. res->t_logits = cur;
  12181. ggml_build_forward_expand(gf, cur);
  12182. }
  12183. ggml_tensor * build_attention_layer(
  12184. ggml_tensor * cur,
  12185. ggml_tensor * inp_pos,
  12186. llm_graph_input_attn_kv_unified * inp_attn,
  12187. const llama_model & model,
  12188. const int64_t n_embd_head,
  12189. const int il) {
  12190. // compute Q and K and (optionally) RoPE them
  12191. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  12192. cb(Qcur, "Qcur", il);
  12193. if (model.layers[il].bq) {
  12194. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  12195. cb(Qcur, "Qcur", il);
  12196. }
  12197. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  12198. cb(Kcur, "Kcur", il);
  12199. if (model.layers[il].bk) {
  12200. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  12201. cb(Kcur, "Kcur", il);
  12202. }
  12203. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  12204. cb(Vcur, "Vcur", il);
  12205. if (model.layers[il].bv) {
  12206. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  12207. cb(Vcur, "Vcur", il);
  12208. }
  12209. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, hparams.n_head(il), n_tokens);
  12210. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, hparams.n_head_kv(il), n_tokens);
  12211. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, hparams.n_head_kv(il), n_tokens);
  12212. const bool use_rope = hparams.rope_finetuned;
  12213. if (use_rope) {
  12214. ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
  12215. Qcur = ggml_rope_ext(
  12216. ctx0, Qcur, inp_pos, rope_factors,
  12217. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  12218. ext_factor, attn_factor, beta_fast, beta_slow
  12219. );
  12220. Kcur = ggml_rope_ext(
  12221. ctx0, Kcur, inp_pos, rope_factors,
  12222. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  12223. ext_factor, attn_factor, beta_fast, beta_slow
  12224. );
  12225. }
  12226. cb(Qcur, "Qcur", il);
  12227. cb(Kcur, "Kcur", il);
  12228. cb(Vcur, "Vcur", il);
  12229. const float kq_scale = hparams.f_attention_scale == 0.0f ? 1.0f/sqrtf(float(n_embd_head)) : hparams.f_attention_scale;
  12230. cur = build_attn(inp_attn,
  12231. model.layers[il].wo, model.layers[il].bo,
  12232. Qcur, Kcur, Vcur, nullptr, nullptr, kq_scale, il);
  12233. cb(cur, "attn_out", il);
  12234. return cur;
  12235. }
  12236. ggml_tensor * build_layer_ffn(
  12237. ggml_tensor * cur,
  12238. ggml_tensor * inpSA,
  12239. const llama_model & model,
  12240. const int il) {
  12241. // For Granite architectures - scale residual
  12242. if (hparams.f_residual_scale) {
  12243. cur = ggml_scale(ctx0, cur, hparams.f_residual_scale);
  12244. }
  12245. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  12246. cb(ffn_inp, "ffn_inp", il);
  12247. // feed-forward network (non-MoE)
  12248. if (model.layers[il].ffn_gate_inp == nullptr) {
  12249. cur = build_norm(ffn_inp,
  12250. model.layers[il].ffn_norm, NULL,
  12251. LLM_NORM_RMS, il);
  12252. cb(cur, "ffn_norm", il);
  12253. cur = build_ffn(cur,
  12254. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  12255. model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, NULL,
  12256. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  12257. NULL,
  12258. LLM_FFN_SILU, LLM_FFN_PAR, il);
  12259. cb(cur, "ffn_out", il);
  12260. } else {
  12261. // MoE branch
  12262. cur = build_norm(ffn_inp,
  12263. model.layers[il].ffn_norm, NULL,
  12264. LLM_NORM_RMS, il);
  12265. cb(cur, "ffn_norm", il);
  12266. ggml_tensor * moe_out = build_moe_ffn(cur,
  12267. model.layers[il].ffn_gate_inp,
  12268. model.layers[il].ffn_up_exps,
  12269. model.layers[il].ffn_gate_exps,
  12270. model.layers[il].ffn_down_exps,
  12271. nullptr,
  12272. n_expert, n_expert_used,
  12273. LLM_FFN_SILU, true,
  12274. false, 0.0,
  12275. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  12276. il);
  12277. cb(moe_out, "ffn_moe_out", il);
  12278. // For Granite MoE Shared
  12279. if (hparams.n_ff_shexp > 0) {
  12280. ggml_tensor * ffn_shexp = build_ffn(cur,
  12281. model.layers[il].ffn_up_shexp, NULL, NULL,
  12282. model.layers[il].ffn_gate_shexp, NULL, NULL,
  12283. model.layers[il].ffn_down_shexp, NULL, NULL,
  12284. NULL,
  12285. LLM_FFN_SILU, LLM_FFN_PAR, il);
  12286. cb(ffn_shexp, "ffn_shexp", il);
  12287. cur = ggml_add(ctx0, moe_out, ffn_shexp);
  12288. cb(cur, "ffn_out", il);
  12289. } else {
  12290. cur = moe_out;
  12291. }
  12292. }
  12293. // For Granite architectures - scale residual
  12294. if (hparams.f_residual_scale) {
  12295. cur = ggml_scale(ctx0, cur, hparams.f_residual_scale);
  12296. }
  12297. cur = ggml_add(ctx0, cur, ffn_inp);
  12298. cb(cur, "ffn_out", il);
  12299. cur = build_cvec(cur, il);
  12300. cb(cur, "l_out", il);
  12301. return cur;
  12302. }
  12303. };
  12304. // ref: https://github.com/facebookresearch/chameleon
  12305. // based on the original build_llama() function, changes:
  12306. // * qk-norm
  12307. // * swin-norm
  12308. // * removed bias
  12309. // * removed MoE
  12310. struct llm_build_chameleon : public llm_graph_context {
  12311. llm_build_chameleon(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  12312. const int64_t n_embd_head = hparams.n_embd_head_v;
  12313. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  12314. GGML_ASSERT(n_embd_head == hparams.n_rot);
  12315. ggml_tensor * cur;
  12316. ggml_tensor * inpL;
  12317. inpL = build_inp_embd(model.tok_embd);
  12318. // inp_pos - contains the positions
  12319. ggml_tensor * inp_pos = build_inp_pos();
  12320. auto * inp_attn = build_attn_inp_kv_unified();
  12321. ggml_tensor * inp_out_ids = build_inp_out_ids();
  12322. for (int il = 0; il < n_layer; ++il) {
  12323. ggml_tensor * inpSA = inpL;
  12324. // norm
  12325. if (hparams.swin_norm) {
  12326. cur = inpL;
  12327. } else {
  12328. cur = build_norm(inpL,
  12329. model.layers[il].attn_norm, NULL,
  12330. LLM_NORM_RMS, il);
  12331. cb(cur, "attn_norm", il);
  12332. }
  12333. // self-attention
  12334. {
  12335. // compute Q and K and RoPE them
  12336. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  12337. cb(Qcur, "Qcur", il);
  12338. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  12339. cb(Kcur, "Kcur", il);
  12340. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  12341. cb(Vcur, "Vcur", il);
  12342. if (model.layers[il].attn_q_norm) {
  12343. Qcur = ggml_view_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens,
  12344. ggml_element_size(Qcur) * n_embd_head,
  12345. ggml_element_size(Qcur) * n_embd_head * n_head,
  12346. 0);
  12347. cb(Qcur, "Qcur", il);
  12348. Qcur = build_norm(Qcur,
  12349. model.layers[il].attn_q_norm,
  12350. model.layers[il].attn_q_norm_b,
  12351. LLM_NORM, il);
  12352. cb(Qcur, "Qcur", il);
  12353. }
  12354. if (model.layers[il].attn_k_norm) {
  12355. Kcur = ggml_view_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens,
  12356. ggml_element_size(Kcur) * n_embd_head,
  12357. ggml_element_size(Kcur) * n_embd_head * n_head_kv,
  12358. 0);
  12359. cb(Kcur, "Kcur", il);
  12360. Kcur = build_norm(Kcur,
  12361. model.layers[il].attn_k_norm,
  12362. model.layers[il].attn_k_norm_b,
  12363. LLM_NORM, il);
  12364. cb(Kcur, "Kcur", il);
  12365. }
  12366. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  12367. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  12368. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  12369. Qcur = ggml_rope_ext(
  12370. ctx0, Qcur, inp_pos, nullptr,
  12371. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  12372. ext_factor, attn_factor, beta_fast, beta_slow
  12373. );
  12374. Kcur = ggml_rope_ext(
  12375. ctx0, Kcur, inp_pos, nullptr,
  12376. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  12377. ext_factor, attn_factor, beta_fast, beta_slow
  12378. );
  12379. cb(Qcur, "Qcur", il);
  12380. cb(Kcur, "Kcur", il);
  12381. cb(Vcur, "Vcur", il);
  12382. cur = build_attn(inp_attn,
  12383. model.layers[il].wo, nullptr,
  12384. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  12385. }
  12386. if (il == n_layer - 1 && inp_out_ids) {
  12387. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  12388. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  12389. }
  12390. if (hparams.swin_norm) {
  12391. cur = build_norm(cur,
  12392. model.layers[il].attn_norm, NULL,
  12393. LLM_NORM_RMS, il);
  12394. }
  12395. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  12396. cb(ffn_inp, "ffn_inp", il);
  12397. // feed-forward network
  12398. if (!hparams.swin_norm) {
  12399. cur = build_norm(ffn_inp,
  12400. model.layers[il].ffn_norm, NULL,
  12401. LLM_NORM_RMS, il);
  12402. cb(cur, "ffn_norm", il);
  12403. }
  12404. cur = build_ffn(cur,
  12405. model.layers[il].ffn_up, NULL, NULL,
  12406. model.layers[il].ffn_gate, NULL, NULL,
  12407. model.layers[il].ffn_down, NULL, NULL,
  12408. NULL,
  12409. LLM_FFN_SILU, LLM_FFN_PAR, il);
  12410. cb(cur, "ffn_out", il);
  12411. if (hparams.swin_norm) {
  12412. cur = build_norm(cur,
  12413. model.layers[il].ffn_norm, NULL,
  12414. LLM_NORM_RMS, il);
  12415. cb(cur, "ffn_norm", il);
  12416. }
  12417. cur = ggml_add(ctx0, cur, ffn_inp);
  12418. cb(cur, "ffn_out", il);
  12419. cur = build_cvec(cur, il);
  12420. cb(cur, "l_out", il);
  12421. // input for next layer
  12422. inpL = cur;
  12423. }
  12424. cur = inpL;
  12425. cur = build_norm(cur,
  12426. model.output_norm, NULL,
  12427. LLM_NORM_RMS, -1);
  12428. cb(cur, "result_norm", -1);
  12429. res->t_embd = cur;
  12430. // lm_head
  12431. cur = build_lora_mm(model.output, cur);
  12432. cb(cur, "result_output_with_img_logits", -1);
  12433. // TODO: this suppresses the output of image tokens, which is required to enable text-only outputs.
  12434. // Needs to be removed once image outputs are supported.
  12435. int img_token_end_idx = 8196;
  12436. int img_token_start_idx = 4;
  12437. int num_img_tokens = img_token_end_idx - img_token_start_idx;
  12438. // creates 1d tensor of size num_img_tokens and values -FLT_MAX,
  12439. // which ensures that text token values are always at least larger than image token values
  12440. ggml_tensor * img_logits = ggml_new_tensor_1d(ctx0, GGML_TYPE_F32, num_img_tokens);
  12441. img_logits = ggml_clamp(ctx0, img_logits, -FLT_MAX, -FLT_MAX);
  12442. cb(img_logits, "img_logits", -1);
  12443. cur = ggml_set_1d(ctx0, cur, img_logits, ggml_element_size(cur) * img_token_start_idx);
  12444. cb(cur, "result_output", -1);
  12445. res->t_logits = cur;
  12446. ggml_build_forward_expand(gf, cur);
  12447. }
  12448. };
  12449. struct llm_build_wavtokenizer_dec : public llm_graph_context {
  12450. llm_build_wavtokenizer_dec(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  12451. ggml_tensor * cur;
  12452. ggml_tensor * inpL;
  12453. inpL = build_inp_embd(model.tok_embd);
  12454. cur = ggml_cont(ctx0, ggml_transpose(ctx0, inpL));
  12455. cur = ggml_conv_1d_ph(ctx0, model.conv1d, cur, 1, 1);
  12456. cur = ggml_add(ctx0, cur, model.conv1d_b);
  12457. // posnet
  12458. for (uint32_t il = 0; il < hparams.posnet.n_layer; ++il) {
  12459. const auto & layer = model.layers[il].posnet;
  12460. inpL = cur;
  12461. switch (il) {
  12462. case 0:
  12463. case 1:
  12464. case 3:
  12465. case 4:
  12466. {
  12467. cur = build_norm(cur,
  12468. layer.norm1,
  12469. layer.norm1_b,
  12470. LLM_NORM_GROUP, 0);
  12471. cur = ggml_mul(ctx0, ggml_sigmoid(ctx0, cur), cur);
  12472. cur = ggml_conv_1d_ph(ctx0, layer.conv1, cur, 1, 1);
  12473. cur = ggml_add(ctx0, cur, layer.conv1_b);
  12474. cur = build_norm(cur,
  12475. layer.norm2,
  12476. layer.norm2_b,
  12477. LLM_NORM_GROUP, 0);
  12478. cur = ggml_mul(ctx0, ggml_sigmoid(ctx0, cur), cur);
  12479. cur = ggml_conv_1d_ph(ctx0, layer.conv2, cur, 1, 1);
  12480. cur = ggml_add(ctx0, cur, layer.conv2_b);
  12481. cur = ggml_add(ctx0, cur, inpL);
  12482. } break;
  12483. case 2:
  12484. {
  12485. cur = build_norm(cur,
  12486. layer.attn_norm,
  12487. layer.attn_norm_b,
  12488. LLM_NORM_GROUP, 0);
  12489. ggml_tensor * q;
  12490. ggml_tensor * k;
  12491. ggml_tensor * v;
  12492. q = ggml_conv_1d_ph(ctx0, layer.attn_q, cur, 1, 1);
  12493. k = ggml_conv_1d_ph(ctx0, layer.attn_k, cur, 1, 1);
  12494. v = ggml_conv_1d_ph(ctx0, layer.attn_v, cur, 1, 1);
  12495. q = ggml_add(ctx0, q, layer.attn_q_b);
  12496. k = ggml_add(ctx0, k, layer.attn_k_b);
  12497. v = ggml_add(ctx0, v, layer.attn_v_b);
  12498. q = ggml_cont(ctx0, ggml_transpose(ctx0, q));
  12499. k = ggml_cont(ctx0, ggml_transpose(ctx0, k));
  12500. ggml_tensor * kq = ggml_mul_mat(ctx0, k, q);
  12501. kq = ggml_soft_max_ext(ctx0, kq, nullptr, 1.0f/sqrtf(float(hparams.posnet.n_embd)), 0.0f);
  12502. cur = ggml_mul_mat(ctx0, kq, v);
  12503. cur = ggml_conv_1d_ph(ctx0, layer.attn_o, cur, 1, 1);
  12504. cur = ggml_add(ctx0, cur, layer.attn_o_b);
  12505. cur = ggml_add(ctx0, cur, inpL);
  12506. } break;
  12507. case 5:
  12508. {
  12509. cur = build_norm(cur,
  12510. layer.norm,
  12511. layer.norm_b,
  12512. LLM_NORM_GROUP, 0);
  12513. } break;
  12514. default: GGML_ABORT("unknown posnet layer");
  12515. };
  12516. }
  12517. cur = ggml_cont(ctx0, ggml_transpose(ctx0, cur));
  12518. cur = build_norm(cur,
  12519. model.tok_norm,
  12520. model.tok_norm_b,
  12521. LLM_NORM, -1);
  12522. cur = ggml_cont(ctx0, ggml_transpose(ctx0, cur));
  12523. inpL = cur;
  12524. // convnext
  12525. for (uint32_t il = 0; il < hparams.convnext.n_layer; ++il) {
  12526. const auto & layer = model.layers[il].convnext;
  12527. cur = inpL;
  12528. cur = ggml_conv_1d_dw_ph(ctx0, layer.dw, cur, 1, 1);
  12529. cur = ggml_add(ctx0, cur, layer.dw_b);
  12530. cur = ggml_cont(ctx0, ggml_transpose(ctx0, cur));
  12531. cur = build_norm(cur,
  12532. layer.norm,
  12533. layer.norm_b,
  12534. LLM_NORM, -1);
  12535. cur = build_ffn(cur,
  12536. layer.pw1, layer.pw1_b, NULL,
  12537. NULL, NULL, NULL,
  12538. layer.pw2, layer.pw2_b, NULL,
  12539. NULL,
  12540. LLM_FFN_GELU, LLM_FFN_SEQ, il);
  12541. cur = ggml_mul(ctx0, cur, layer.gamma);
  12542. cur = ggml_cont(ctx0, ggml_transpose(ctx0, cur));
  12543. inpL = ggml_add(ctx0, cur, inpL);
  12544. }
  12545. cur = inpL;
  12546. cur = ggml_cont(ctx0, ggml_transpose(ctx0, cur));
  12547. cur = build_norm(cur,
  12548. model.output_norm,
  12549. model.output_norm_b,
  12550. LLM_NORM, -1);
  12551. // lm_head
  12552. cur = build_lora_mm(model.output, cur);
  12553. cur = ggml_add(ctx0, cur, model.output_b);
  12554. cb(cur, "result_embd", -1);
  12555. res->t_embd = cur;
  12556. ggml_build_forward_expand(gf, cur);
  12557. }
  12558. };
  12559. struct llm_build_plm : public llm_graph_context {
  12560. llm_build_plm(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  12561. const float kq_scale = 1.0f/sqrtf(float(hparams.n_embd_head_k));
  12562. const uint32_t n_embd_head_qk_rope = hparams.n_rot;
  12563. const uint32_t n_embd_head_qk_nope = hparams.n_embd_head_k - hparams.n_rot;
  12564. const uint32_t kv_lora_rank = hparams.n_lora_kv;
  12565. ggml_tensor * cur;
  12566. ggml_tensor * inpL;
  12567. // {n_embd, n_tokens}
  12568. inpL = build_inp_embd(model.tok_embd);
  12569. // inp_pos - contains the positions
  12570. ggml_tensor * inp_pos = build_inp_pos();
  12571. auto * inp_attn = build_attn_inp_kv_unified();
  12572. ggml_tensor * inp_out_ids = build_inp_out_ids();
  12573. for (int il = 0; il < n_layer; ++il) {
  12574. ggml_tensor * inpSA = inpL;
  12575. // norm
  12576. cur = build_norm(inpL,
  12577. model.layers[il].attn_norm, NULL,
  12578. LLM_NORM_RMS, il);
  12579. cb(cur, "attn_norm", il);
  12580. // self_attention
  12581. {
  12582. ggml_tensor * q = NULL;
  12583. q = ggml_mul_mat(ctx0, model.layers[il].wq, cur);
  12584. cb(q, "q", il);
  12585. // split into {n_head * n_embd_head_qk_nope, n_tokens}
  12586. ggml_tensor * q_nope = ggml_view_3d(ctx0, q, n_embd_head_qk_nope, n_head, n_tokens,
  12587. ggml_row_size(q->type, hparams.n_embd_head_k),
  12588. ggml_row_size(q->type, hparams.n_embd_head_k * n_head),
  12589. 0);
  12590. cb(q_nope, "q_nope", il);
  12591. // and {n_head * n_embd_head_qk_rope, n_tokens}
  12592. ggml_tensor * q_pe = ggml_view_3d(ctx0, q, n_embd_head_qk_rope, n_head, n_tokens,
  12593. ggml_row_size(q->type, hparams.n_embd_head_k),
  12594. ggml_row_size(q->type, hparams.n_embd_head_k * n_head),
  12595. ggml_row_size(q->type, n_embd_head_qk_nope));
  12596. cb(q_pe, "q_pe", il);
  12597. // {n_embd, kv_lora_rank + n_embd_head_qk_rope} * {n_embd, n_tokens} -> {kv_lora_rank + n_embd_head_qk_rope, n_tokens}
  12598. ggml_tensor * kv_pe_compresseed = ggml_mul_mat(ctx0, model.layers[il].wkv_a_mqa, cur);
  12599. cb(kv_pe_compresseed, "kv_pe_compresseed", il);
  12600. // split into {kv_lora_rank, n_tokens}
  12601. ggml_tensor * kv_compressed = ggml_view_2d(ctx0, kv_pe_compresseed, kv_lora_rank, n_tokens,
  12602. kv_pe_compresseed->nb[1],
  12603. 0);
  12604. cb(kv_compressed, "kv_compressed", il);
  12605. // and {n_embd_head_qk_rope, n_tokens}
  12606. ggml_tensor * k_pe = ggml_view_3d(ctx0, kv_pe_compresseed, n_embd_head_qk_rope, 1, n_tokens,
  12607. kv_pe_compresseed->nb[1],
  12608. kv_pe_compresseed->nb[1],
  12609. ggml_row_size(kv_pe_compresseed->type, kv_lora_rank));
  12610. cb(k_pe, "k_pe", il);
  12611. kv_compressed = build_norm(kv_compressed,
  12612. model.layers[il].attn_kv_a_norm, NULL,
  12613. LLM_NORM_RMS, il);
  12614. cb(kv_compressed, "kv_compressed", il);
  12615. // {kv_lora_rank, n_head * (n_embd_head_qk_nope + n_embd_head_v)} * {kv_lora_rank, n_tokens} -> {n_head * (n_embd_head_qk_nope + n_embd_head_v), n_tokens}
  12616. ggml_tensor * kv = ggml_mul_mat(ctx0, model.layers[il].wkv_b, kv_compressed);
  12617. cb(kv, "kv", il);
  12618. // split into {n_head * n_embd_head_qk_nope, n_tokens}
  12619. ggml_tensor * k_nope = ggml_view_3d(ctx0, kv, n_embd_head_qk_nope, n_head, n_tokens,
  12620. ggml_row_size(kv->type, n_embd_head_qk_nope + hparams.n_embd_head_v),
  12621. ggml_row_size(kv->type, n_head * (n_embd_head_qk_nope + hparams.n_embd_head_v)),
  12622. 0);
  12623. cb(k_nope, "k_nope", il);
  12624. // and {n_head * n_embd_head_v, n_tokens}
  12625. ggml_tensor * v_states = ggml_view_3d(ctx0, kv, hparams.n_embd_head_v, n_head, n_tokens,
  12626. ggml_row_size(kv->type, (n_embd_head_qk_nope + hparams.n_embd_head_v)),
  12627. ggml_row_size(kv->type, (n_embd_head_qk_nope + hparams.n_embd_head_v)*n_head),
  12628. ggml_row_size(kv->type, (n_embd_head_qk_nope)));
  12629. cb(v_states, "v_states", il);
  12630. v_states = ggml_cont(ctx0, v_states);
  12631. cb(v_states, "v_states", il);
  12632. v_states = ggml_view_2d(ctx0, v_states, hparams.n_embd_head_v * n_head, n_tokens,
  12633. ggml_row_size(kv->type, hparams.n_embd_head_v * n_head),
  12634. 0);
  12635. cb(v_states, "v_states", il);
  12636. q_pe = ggml_rope_ext(
  12637. ctx0, q_pe, inp_pos, nullptr,
  12638. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  12639. ext_factor, attn_factor, beta_fast, beta_slow
  12640. );
  12641. cb(q_pe, "q_pe", il);
  12642. // shared RoPE key
  12643. k_pe = ggml_rope_ext(
  12644. ctx0, k_pe, inp_pos, nullptr,
  12645. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  12646. ext_factor, attn_factor, beta_fast, beta_slow
  12647. );
  12648. cb(k_pe, "k_pe", il);
  12649. ggml_tensor * q_states = ggml_concat(ctx0, q_nope, q_pe, 0);
  12650. cb(q_states, "q_states", il);
  12651. ggml_tensor * k_states = ggml_concat(ctx0, k_nope, ggml_repeat(ctx0, k_pe, q_pe), 0);
  12652. cb(k_states, "k_states", il);
  12653. cur = build_attn(inp_attn,
  12654. model.layers[il].wo, NULL,
  12655. q_states, k_states, v_states, nullptr, nullptr, kq_scale, il);
  12656. }
  12657. if (il == n_layer - 1 && inp_out_ids) {
  12658. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  12659. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  12660. }
  12661. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  12662. cb(ffn_inp, "ffn_inp", il);
  12663. cur = build_norm(ffn_inp,
  12664. model.layers[il].ffn_norm, NULL,
  12665. LLM_NORM_RMS, il);
  12666. cb(cur, "ffn_norm", il);
  12667. cur = build_ffn(cur,
  12668. model.layers[il].ffn_up, NULL, NULL,
  12669. NULL, NULL, NULL,
  12670. model.layers[il].ffn_down, NULL, NULL,
  12671. NULL,
  12672. LLM_FFN_RELU_SQR, LLM_FFN_SEQ, il);
  12673. cb(cur, "ffn_out", il);
  12674. cur = ggml_add(ctx0, cur, ffn_inp);
  12675. cur = build_cvec(cur, il);
  12676. cb(cur, "l_out", il);
  12677. // input for next layer
  12678. inpL = cur;
  12679. }
  12680. cur = inpL;
  12681. cur = build_norm(cur,
  12682. model.output_norm, NULL,
  12683. LLM_NORM_RMS, -1);
  12684. cb(cur, "result_norm", -1);
  12685. res->t_embd = cur;
  12686. cur = build_lora_mm(model.output, cur);
  12687. cb(cur, "result_output", -1);
  12688. res->t_logits = cur;
  12689. ggml_build_forward_expand(gf, cur);
  12690. }
  12691. };
  12692. struct llm_build_bailingmoe : public llm_graph_context {
  12693. llm_build_bailingmoe(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  12694. ggml_tensor * cur;
  12695. ggml_tensor * inpL;
  12696. inpL = build_inp_embd(model.tok_embd);
  12697. // inp_pos - contains the positions
  12698. ggml_tensor * inp_pos = build_inp_pos();
  12699. auto * inp_attn = build_attn_inp_kv_unified();
  12700. ggml_tensor * inp_out_ids = build_inp_out_ids();
  12701. for (int il = 0; il < n_layer; ++il) {
  12702. ggml_tensor * inpSA = inpL;
  12703. // norm
  12704. cur = build_norm(inpL,
  12705. model.layers[il].attn_norm, NULL,
  12706. LLM_NORM_RMS, il);
  12707. cb(cur, "attn_norm", il);
  12708. // self-attention
  12709. {
  12710. // rope freq factors for llama3; may return nullptr for llama2 and other models
  12711. ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
  12712. // compute Q and K and RoPE them
  12713. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  12714. cb(Qcur, "Qcur", il);
  12715. if (model.layers[il].bq) {
  12716. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  12717. cb(Qcur, "Qcur", il);
  12718. }
  12719. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  12720. cb(Kcur, "Kcur", il);
  12721. if (model.layers[il].bk) {
  12722. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  12723. cb(Kcur, "Kcur", il);
  12724. }
  12725. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  12726. cb(Vcur, "Vcur", il);
  12727. if (model.layers[il].bv) {
  12728. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  12729. cb(Vcur, "Vcur", il);
  12730. }
  12731. Qcur = ggml_reshape_3d(ctx0, Qcur, n_rot, n_head, n_tokens);
  12732. Kcur = ggml_reshape_3d(ctx0, Kcur, n_rot, n_head_kv, n_tokens);
  12733. Vcur = ggml_reshape_3d(ctx0, Vcur, n_rot, n_head_kv, n_tokens);
  12734. Qcur = ggml_rope_ext(
  12735. ctx0, Qcur, inp_pos, rope_factors,
  12736. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  12737. ext_factor, attn_factor, beta_fast, beta_slow
  12738. );
  12739. Kcur = ggml_rope_ext(
  12740. ctx0, Kcur, inp_pos, rope_factors,
  12741. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  12742. ext_factor, attn_factor, beta_fast, beta_slow
  12743. );
  12744. cb(Qcur, "Qcur", il);
  12745. cb(Kcur, "Kcur", il);
  12746. cb(Vcur, "Vcur", il);
  12747. cur = build_attn(inp_attn,
  12748. model.layers[il].wo, model.layers[il].bo,
  12749. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_rot)), il);
  12750. }
  12751. if (il == n_layer - 1 && inp_out_ids) {
  12752. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  12753. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  12754. }
  12755. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  12756. cb(ffn_inp, "ffn_inp", il);
  12757. cur = build_norm(ffn_inp,
  12758. model.layers[il].ffn_norm, NULL,
  12759. LLM_NORM_RMS, il);
  12760. cb(cur, "ffn_norm", il);
  12761. ggml_tensor * moe_out =
  12762. build_moe_ffn(cur,
  12763. model.layers[il].ffn_gate_inp,
  12764. model.layers[il].ffn_up_exps,
  12765. model.layers[il].ffn_gate_exps,
  12766. model.layers[il].ffn_down_exps,
  12767. nullptr,
  12768. n_expert, n_expert_used,
  12769. LLM_FFN_SILU, hparams.expert_weights_norm,
  12770. false, hparams.expert_weights_scale,
  12771. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  12772. il);
  12773. cb(moe_out, "ffn_moe_out", il);
  12774. // FFN shared expert
  12775. {
  12776. ggml_tensor * ffn_shexp = build_ffn(cur,
  12777. model.layers[il].ffn_up_shexp, NULL, NULL,
  12778. model.layers[il].ffn_gate_shexp, NULL, NULL,
  12779. model.layers[il].ffn_down_shexp, NULL, NULL,
  12780. NULL,
  12781. LLM_FFN_SILU, LLM_FFN_PAR, il);
  12782. cb(ffn_shexp, "ffn_shexp", il);
  12783. cur = ggml_add(ctx0, moe_out, ffn_shexp);
  12784. cb(cur, "ffn_out", il);
  12785. }
  12786. cur = ggml_add(ctx0, cur, ffn_inp);
  12787. cur = build_cvec(cur, il);
  12788. cb(cur, "l_out", il);
  12789. // input for next layer
  12790. inpL = cur;
  12791. }
  12792. cur = inpL;
  12793. cur = build_norm(cur,
  12794. model.output_norm, NULL,
  12795. LLM_NORM_RMS, -1);
  12796. cb(cur, "result_norm", -1);
  12797. res->t_embd = cur;
  12798. // lm_head
  12799. cur = build_lora_mm(model.output, cur);
  12800. cb(cur, "result_output", -1);
  12801. res->t_logits = cur;
  12802. ggml_build_forward_expand(gf, cur);
  12803. }
  12804. };
  12805. struct llm_build_dots1 : public llm_graph_context {
  12806. llm_build_dots1(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  12807. const int64_t n_embd_head = hparams.n_embd_head_v;
  12808. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  12809. GGML_ASSERT(n_embd_head == hparams.n_rot);
  12810. ggml_tensor * cur;
  12811. ggml_tensor * inpL;
  12812. inpL = build_inp_embd(model.tok_embd);
  12813. // inp_pos - contains the positions
  12814. ggml_tensor * inp_pos = build_inp_pos();
  12815. auto * inp_attn = build_attn_inp_kv_unified();
  12816. ggml_tensor * inp_out_ids = build_inp_out_ids();
  12817. for (int il = 0; il < n_layer; ++il) {
  12818. ggml_tensor * inpSA = inpL;
  12819. // norm
  12820. cur = build_norm(inpL,
  12821. model.layers[il].attn_norm, NULL,
  12822. LLM_NORM_RMS, il);
  12823. cb(cur, "attn_norm", il);
  12824. // self_attention
  12825. {
  12826. // compute Q and K and RoPE them
  12827. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  12828. cb(Qcur, "Qcur", il);
  12829. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  12830. cb(Kcur, "Kcur", il);
  12831. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  12832. cb(Vcur, "Vcur", il);
  12833. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  12834. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  12835. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  12836. Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);
  12837. cb(Qcur, "Qcur_normed", il);
  12838. Qcur = ggml_rope_ext(
  12839. ctx0, Qcur, inp_pos, nullptr,
  12840. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  12841. ext_factor, attn_factor, beta_fast, beta_slow
  12842. );
  12843. Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);
  12844. cb(Kcur, "Kcur_normed", il);
  12845. Kcur = ggml_rope_ext(
  12846. ctx0, Kcur, inp_pos, nullptr,
  12847. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  12848. ext_factor, attn_factor, beta_fast, beta_slow
  12849. );
  12850. cb(Qcur, "Qcur", il);
  12851. cb(Kcur, "Kcur", il);
  12852. cb(Vcur, "Vcur", il);
  12853. cur = build_attn(inp_attn,
  12854. model.layers[il].wo, model.layers[il].bo,
  12855. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  12856. }
  12857. if (il == n_layer - 1 && inp_out_ids) {
  12858. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  12859. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  12860. }
  12861. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  12862. cb(ffn_inp, "ffn_inp", il);
  12863. // MoE branch
  12864. cur = build_norm(ffn_inp,
  12865. model.layers[il].ffn_norm, NULL,
  12866. LLM_NORM_RMS, il);
  12867. cb(cur, "ffn_norm", il);
  12868. if ((uint32_t) il < hparams.n_layer_dense_lead) {
  12869. cur = build_ffn(cur,
  12870. model.layers[il].ffn_up, NULL, NULL,
  12871. model.layers[il].ffn_gate, NULL, NULL,
  12872. model.layers[il].ffn_down, NULL, NULL,
  12873. NULL,
  12874. LLM_FFN_SILU, LLM_FFN_PAR, il);
  12875. cb(cur, "ffn_out", il);
  12876. } else {
  12877. ggml_tensor * moe_out =
  12878. build_moe_ffn(cur,
  12879. model.layers[il].ffn_gate_inp,
  12880. model.layers[il].ffn_up_exps,
  12881. model.layers[il].ffn_gate_exps,
  12882. model.layers[il].ffn_down_exps,
  12883. model.layers[il].ffn_exp_probs_b,
  12884. n_expert, n_expert_used,
  12885. LLM_FFN_SILU, hparams.expert_weights_norm,
  12886. true, hparams.expert_weights_scale,
  12887. (llama_expert_gating_func_type) hparams.expert_gating_func,
  12888. il);
  12889. cb(moe_out, "ffn_moe_out", il);
  12890. {
  12891. ggml_tensor * ffn_shexp = build_ffn(cur,
  12892. model.layers[il].ffn_up_shexp, NULL, NULL,
  12893. model.layers[il].ffn_gate_shexp, NULL, NULL,
  12894. model.layers[il].ffn_down_shexp, NULL, NULL,
  12895. NULL,
  12896. LLM_FFN_SILU, LLM_FFN_PAR, il);
  12897. cb(ffn_shexp, "ffn_shexp", il);
  12898. cur = ggml_add(ctx0, moe_out, ffn_shexp);
  12899. cb(cur, "ffn_out", il);
  12900. }
  12901. }
  12902. cur = ggml_add(ctx0, cur, ffn_inp);
  12903. cur = build_cvec(cur, il);
  12904. cb(cur, "l_out", il);
  12905. // input for next layer
  12906. inpL = cur;
  12907. }
  12908. cur = inpL;
  12909. cur = build_norm(cur,
  12910. model.output_norm, NULL,
  12911. LLM_NORM_RMS, -1);
  12912. cb(cur, "result_norm", -1);
  12913. res->t_embd = cur;
  12914. // lm_head
  12915. cur = build_lora_mm(model.output, cur);
  12916. cb(cur, "result_output", -1);
  12917. res->t_logits = cur;
  12918. ggml_build_forward_expand(gf, cur);
  12919. }
  12920. };
  12921. struct llm_build_ernie4_5 : public llm_graph_context {
  12922. llm_build_ernie4_5(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  12923. const int64_t n_embd_head = hparams.n_embd_head_v;
  12924. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  12925. GGML_ASSERT(n_embd_head == hparams.n_rot);
  12926. ggml_tensor * cur;
  12927. ggml_tensor * inpL;
  12928. inpL = build_inp_embd(model.tok_embd);
  12929. // inp_pos - contains the positions
  12930. ggml_tensor * inp_pos = build_inp_pos();
  12931. auto * inp_attn = build_attn_inp_kv_unified();
  12932. for (int il = 0; il < n_layer; ++il) {
  12933. ggml_tensor * inpSA = inpL;
  12934. // norm
  12935. {
  12936. cur = build_norm(inpL,
  12937. model.layers[il].attn_norm, NULL,
  12938. LLM_NORM_RMS, il);
  12939. cb(cur, "attn_norm", il);
  12940. }
  12941. // self-attention
  12942. {
  12943. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  12944. cb(Qcur, "Qcur", il);
  12945. if (model.layers[il].bq) {
  12946. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  12947. cb(Qcur, "Qcur", il);
  12948. }
  12949. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  12950. cb(Kcur, "Kcur", il);
  12951. if (model.layers[il].bk) {
  12952. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  12953. cb(Kcur, "Kcur", il);
  12954. }
  12955. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  12956. cb(Vcur, "Vcur", il);
  12957. if (model.layers[il].bv) {
  12958. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  12959. cb(Vcur, "Vcur", il);
  12960. }
  12961. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  12962. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  12963. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  12964. Qcur = ggml_rope_ext(
  12965. ctx0, Qcur, inp_pos, nullptr,
  12966. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  12967. ext_factor, attn_factor, beta_fast, beta_slow
  12968. );
  12969. Kcur = ggml_rope_ext(
  12970. ctx0, Kcur, inp_pos, nullptr,
  12971. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  12972. ext_factor, attn_factor, beta_fast, beta_slow
  12973. );
  12974. cb(Qcur, "Qcur", il);
  12975. cb(Kcur, "Kcur", il);
  12976. cb(Vcur, "Vcur", il);
  12977. cur = build_attn(inp_attn,
  12978. model.layers[il].wo, NULL,
  12979. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  12980. }
  12981. if (il == n_layer - 1) {
  12982. // skip computing output for unused tokens
  12983. ggml_tensor * inp_out_ids = build_inp_out_ids();
  12984. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  12985. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  12986. }
  12987. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  12988. cb(ffn_inp, "ffn_inp", il);
  12989. // feed-forward network
  12990. {
  12991. cur = build_norm(ffn_inp,
  12992. model.layers[il].ffn_norm, NULL,
  12993. LLM_NORM_RMS, il);
  12994. cb(cur, "ffn_norm", il);
  12995. cur = build_ffn(cur,
  12996. model.layers[il].ffn_up, NULL, NULL,
  12997. model.layers[il].ffn_gate, NULL, NULL,
  12998. model.layers[il].ffn_down, NULL, NULL,
  12999. NULL,
  13000. LLM_FFN_SILU, LLM_FFN_PAR, il);
  13001. cb(cur, "ffn_out", il);
  13002. }
  13003. cur = ggml_add(ctx0, cur, ffn_inp);
  13004. cur = build_cvec(cur, il);
  13005. cb(cur, "l_out", il);
  13006. // input for next layer
  13007. inpL = cur;
  13008. }
  13009. cur = inpL;
  13010. cur = build_norm(cur,
  13011. model.output_norm, NULL,
  13012. LLM_NORM_RMS, -1);
  13013. cb(cur, "result_norm", -1);
  13014. res->t_embd = cur;
  13015. // lm_head
  13016. cur = build_lora_mm(model.output, cur);
  13017. cb(cur, "result_output", -1);
  13018. res->t_logits = cur;
  13019. ggml_build_forward_expand(gf, cur);
  13020. }
  13021. };
  13022. struct llm_build_ernie4_5_moe : public llm_graph_context {
  13023. llm_build_ernie4_5_moe(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  13024. const int64_t n_embd_head = hparams.n_embd_head_v;
  13025. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  13026. GGML_ASSERT(n_embd_head == hparams.n_rot);
  13027. ggml_tensor * cur;
  13028. ggml_tensor * inpL;
  13029. inpL = build_inp_embd(model.tok_embd);
  13030. // inp_pos - contains the positions
  13031. ggml_tensor * inp_pos = build_inp_pos();
  13032. auto * inp_attn = build_attn_inp_kv_unified();
  13033. ggml_tensor * inp_out_ids = build_inp_out_ids();
  13034. GGML_ASSERT(hparams.n_moe_layer_step > 0 && "Ernie 4.5 MoE requires n_moe_layer_step > 0");
  13035. for (int il = 0; il < n_layer; ++il) {
  13036. ggml_tensor * inpSA = inpL;
  13037. // norm
  13038. {
  13039. cur = build_norm(inpL,
  13040. model.layers[il].attn_norm, NULL,
  13041. LLM_NORM_RMS, il);
  13042. cb(cur, "attn_norm", il);
  13043. }
  13044. // self-attention
  13045. {
  13046. // compute Q and K and RoPE them
  13047. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  13048. cb(Qcur, "Qcur", il);
  13049. if (model.layers[il].bq) {
  13050. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  13051. cb(Qcur, "Qcur", il);
  13052. }
  13053. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  13054. cb(Kcur, "Kcur", il);
  13055. if (model.layers[il].bk) {
  13056. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  13057. cb(Kcur, "Kcur", il);
  13058. }
  13059. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  13060. cb(Vcur, "Vcur", il);
  13061. if (model.layers[il].bv) {
  13062. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  13063. cb(Vcur, "Vcur", il);
  13064. }
  13065. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  13066. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  13067. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  13068. Qcur = ggml_rope_ext(
  13069. ctx0, Qcur, inp_pos, nullptr,
  13070. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  13071. ext_factor, attn_factor, beta_fast, beta_slow
  13072. );
  13073. Kcur = ggml_rope_ext(
  13074. ctx0, Kcur, inp_pos, nullptr,
  13075. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  13076. ext_factor, attn_factor, beta_fast, beta_slow
  13077. );
  13078. cb(Qcur, "Qcur", il);
  13079. cb(Kcur, "Kcur", il);
  13080. cb(Vcur, "Vcur", il);
  13081. cur = build_attn(inp_attn,
  13082. model.layers[il].wo, NULL,
  13083. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  13084. cb(cur, "attn_out", il);
  13085. }
  13086. if (il == n_layer - 1 && inp_out_ids) {
  13087. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  13088. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  13089. }
  13090. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  13091. cb(ffn_inp, "ffn_inp", il);
  13092. // feed-forward network
  13093. bool is_moe_layer = static_cast<uint32_t>(il) >= hparams.n_layer_dense_lead && (il + 1) % hparams.n_moe_layer_step == 0;
  13094. if (!is_moe_layer) {
  13095. cur = build_norm(ffn_inp,
  13096. model.layers[il].ffn_norm, NULL,
  13097. LLM_NORM_RMS, il);
  13098. cb(cur, "ffn_norm", il);
  13099. cur = build_ffn(cur,
  13100. model.layers[il].ffn_up, NULL, NULL,
  13101. model.layers[il].ffn_gate, NULL, NULL,
  13102. model.layers[il].ffn_down, NULL, NULL,
  13103. NULL,
  13104. LLM_FFN_SILU, LLM_FFN_PAR, il);
  13105. cb(cur, "ffn_out", il);
  13106. } else {
  13107. // MoE branch
  13108. cur = build_norm(ffn_inp,
  13109. model.layers[il].ffn_norm, NULL,
  13110. LLM_NORM_RMS, il);
  13111. cb(cur, "ffn_norm", il);
  13112. ggml_tensor * moe_out = build_moe_ffn(cur,
  13113. model.layers[il].ffn_gate_inp,
  13114. model.layers[il].ffn_up_exps,
  13115. model.layers[il].ffn_gate_exps,
  13116. model.layers[il].ffn_down_exps,
  13117. model.layers[il].ffn_exp_probs_b,
  13118. n_expert, n_expert_used,
  13119. LLM_FFN_SILU, true,
  13120. false, 0.0,
  13121. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  13122. il);
  13123. cb(moe_out, "ffn_moe_out", il);
  13124. // Shared expert (if present)
  13125. if (hparams.n_ff_shexp > 0) {
  13126. ggml_tensor * ffn_shexp = build_ffn(cur,
  13127. model.layers[il].ffn_up_shexp, NULL, NULL,
  13128. model.layers[il].ffn_gate_shexp, NULL, NULL,
  13129. model.layers[il].ffn_down_shexp, NULL, NULL,
  13130. NULL,
  13131. LLM_FFN_SILU, LLM_FFN_PAR, il);
  13132. cb(ffn_shexp, "ffn_shexp", il);
  13133. cur = ggml_add(ctx0, moe_out, ffn_shexp);
  13134. } else {
  13135. cur = moe_out;
  13136. }
  13137. cb(cur, "ffn_out", il);
  13138. }
  13139. cur = ggml_add(ctx0, cur, ffn_inp);
  13140. cb(cur, "ffn_out", il);
  13141. cur = build_cvec(cur, il);
  13142. cb(cur, "l_out", il);
  13143. // input for next layer
  13144. inpL = cur;
  13145. }
  13146. cur = inpL;
  13147. cur = build_norm(cur,
  13148. model.output_norm, NULL,
  13149. LLM_NORM_RMS, -1);
  13150. cb(cur, "result_norm", -1);
  13151. res->t_embd = cur;
  13152. // lm_head
  13153. cur = build_lora_mm(model.output, cur);
  13154. cb(cur, "result_output", -1);
  13155. res->t_logits = cur;
  13156. ggml_build_forward_expand(gf, cur);
  13157. }
  13158. };
  13159. struct llm_build_falcon_h1 : public llm_graph_context_mamba {
  13160. llm_build_falcon_h1(const llama_model & model, const llm_graph_params & params) : llm_graph_context_mamba(params) {
  13161. const int64_t n_embd_head = hparams.n_embd_head_v;
  13162. ggml_tensor * cur;
  13163. ggml_tensor * inpL;
  13164. inpL = build_inp_embd(model.tok_embd);
  13165. // inp_pos - contains the positions
  13166. ggml_tensor * inp_pos = build_inp_pos();
  13167. // Build the inputs in the recurrent & kv cache
  13168. auto * inp = build_inp_mem_hybrid();
  13169. const float kq_scale = hparams.f_attention_scale == 0.0f ? 1.0f/sqrtf(float(n_embd_head)) : hparams.f_attention_scale;
  13170. ggml_tensor * inp_out_ids = build_inp_out_ids();
  13171. for (int il = 0; il < n_layer; ++il) {
  13172. ggml_tensor * inpSA = inpL;
  13173. cur = build_norm(inpL,
  13174. model.layers[il].attn_norm, NULL,
  13175. LLM_NORM_RMS, il);
  13176. cb(cur, "attn_norm", il);
  13177. // self-attention
  13178. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  13179. cb(Qcur, "Qcur", il);
  13180. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  13181. cb(Kcur, "Kcur", il);
  13182. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  13183. cb(Vcur, "Vcur", il);
  13184. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  13185. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  13186. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  13187. Qcur = ggml_rope_ext(
  13188. ctx0, Qcur, inp_pos, nullptr,
  13189. n_rot, hparams.rope_type, n_ctx_orig, freq_base, freq_scale,
  13190. ext_factor, attn_factor, beta_fast, beta_slow);
  13191. Kcur = ggml_rope_ext(
  13192. ctx0, Kcur, inp_pos, nullptr,
  13193. n_rot, hparams.rope_type, n_ctx_orig, freq_base, freq_scale,
  13194. ext_factor, attn_factor, beta_fast, beta_slow
  13195. );
  13196. cb(Qcur, "Qcur-post-rope", il);
  13197. cb(Kcur, "Kcur-post-rope", il);
  13198. cb(Vcur, "Vcur-post-rope", il);
  13199. ggml_tensor * attn_out = build_attn(inp->get_attn(),
  13200. model.layers[il].wo, NULL,
  13201. Qcur, Kcur, Vcur, nullptr, nullptr, kq_scale, il);
  13202. cb(attn_out, "attn_out", il);
  13203. cur = build_norm(inpL,
  13204. model.layers[il].attn_norm, NULL,
  13205. LLM_NORM_RMS, il);
  13206. // Mamba2 layer
  13207. cb(cur, "ssm_in", il);
  13208. ggml_tensor * ssm_out = build_mamba2_layer(inp->get_recr(), cur, model, ubatch, il);
  13209. cb(ssm_out, "ssm_out", il);
  13210. // // Aggregation
  13211. cur = ggml_add(ctx0, attn_out, ssm_out);
  13212. inpSA = ggml_add(ctx0, cur, inpSA);
  13213. cb(cur, "layer_out", il);
  13214. if (il == n_layer - 1 && inp_out_ids) {
  13215. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  13216. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  13217. }
  13218. ggml_tensor * ffn_inp = inpSA;
  13219. cb(ffn_inp, "ffn_inp", il);
  13220. // feed-forward network
  13221. cur = build_norm(ffn_inp,
  13222. model.layers[il].ffn_norm, NULL,
  13223. LLM_NORM_RMS, il);
  13224. cb(cur, "ffn_norm", il);
  13225. cur = build_ffn(cur,
  13226. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  13227. model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, NULL,
  13228. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  13229. NULL,
  13230. LLM_FFN_SILU, LLM_FFN_PAR, il);
  13231. cb(cur, "ffn_out", il);
  13232. cur = ggml_add(ctx0, cur, inpSA);
  13233. cur = build_cvec(cur, il);
  13234. cb(cur, "l_out", il);
  13235. // input for next layer
  13236. inpL = cur;
  13237. }
  13238. cur = inpL;
  13239. cur = build_norm(cur,
  13240. model.output_norm, NULL,
  13241. LLM_NORM_RMS, -1);
  13242. cb(cur, "result_norm", -1);
  13243. res->t_embd = cur;
  13244. // lm_head
  13245. cur = build_lora_mm(model.output, cur);
  13246. cb(cur, "result_output", -1);
  13247. res->t_logits = cur;
  13248. ggml_build_forward_expand(gf, cur);
  13249. }
  13250. };
  13251. struct llm_build_plamo2 : public llm_graph_context_mamba {
  13252. llm_build_plamo2(const llama_model & model, const llm_graph_params & params) : llm_graph_context_mamba(params) {
  13253. ggml_tensor * cur;
  13254. ggml_tensor * inpL;
  13255. // {n_embd, n_tokens}
  13256. inpL = build_inp_embd(model.tok_embd);
  13257. cb(inpL, "embedding_output", -1);
  13258. ggml_tensor * inp_pos = build_inp_pos();
  13259. auto * inp_hybrid = build_inp_mem_hybrid();
  13260. ggml_tensor * inp_out_ids = build_inp_out_ids();
  13261. for (int il = 0; il < n_layer; ++il) {
  13262. ggml_tensor * residual = inpL;
  13263. // ggml_graph_add_node(gf, model.layers[il].attn_norm);
  13264. // cb(model.layers[il].attn_norm, "attn_norm", il);
  13265. // pre_mixer_norm
  13266. cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
  13267. // check if this layer is Mamba or Attention
  13268. bool is_mamba_layer = hparams.is_recurrent(il);
  13269. if (is_mamba_layer) {
  13270. // PLaMo-2 Mamba layer
  13271. cur = build_plamo2_mamba_layer(inp_hybrid->get_recr(), cur, model, ubatch, il);
  13272. } else {
  13273. // PLaMo-2 Attention layer
  13274. cur = build_plamo2_attn_layer(inp_hybrid->get_attn(), inp_pos, cur, model, il);
  13275. }
  13276. // post_mixer_norm
  13277. cur = build_norm(cur, model.layers[il].attn_post_norm, NULL, LLM_NORM_RMS, il);
  13278. cb(cur, "attn_post_norm", il);
  13279. // residual connection
  13280. cur = ggml_add(ctx0, cur, residual);
  13281. cb(cur, "attn_residual", il);
  13282. residual = cur;
  13283. // pre-ffn norm
  13284. cur = build_norm(cur, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);
  13285. cb(cur, "ffn_pre_norm", il);
  13286. // feed-forward network
  13287. cur = build_ffn(cur,
  13288. model.layers[il].ffn_up, NULL, NULL,
  13289. NULL, NULL, NULL,
  13290. model.layers[il].ffn_down, NULL, NULL,
  13291. NULL,
  13292. LLM_FFN_SWIGLU, LLM_FFN_SEQ, il);
  13293. cb(cur, "ffn_out", il);
  13294. // post ffn norm
  13295. cur = build_norm(cur, model.layers[il].ffn_post_norm, NULL, LLM_NORM_RMS, il);
  13296. cb(cur, "ffn_post_norm", il);
  13297. if (il == n_layer - 1 && inp_out_ids) {
  13298. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  13299. residual = ggml_get_rows(ctx0, residual, inp_out_ids);
  13300. }
  13301. // residual connection
  13302. cur = ggml_add(ctx0, cur, residual);
  13303. cb(cur, "ffn_residual", il);
  13304. inpL = cur;
  13305. }
  13306. cur = inpL;
  13307. // final norm
  13308. cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1);
  13309. cb(cur, "result_norm", -1);
  13310. // lm_head
  13311. cur = build_lora_mm(model.output, cur);
  13312. cb(cur, "result_output", -1);
  13313. // Explicitly mark as output tensor to ensure proper backend assignment
  13314. ggml_set_output(cur);
  13315. res->t_logits = cur;
  13316. ggml_build_forward_expand(gf, cur);
  13317. }
  13318. private:
  13319. ggml_tensor * build_plamo2_attn_layer(
  13320. llm_graph_input_attn_kv_unified * inp,
  13321. ggml_tensor * inp_pos,
  13322. ggml_tensor * cur,
  13323. const llama_model & model,
  13324. int il) {
  13325. // self-attention
  13326. {
  13327. // PLaMo-2 uses combined QKV tensor
  13328. ggml_tensor * qkv = build_lora_mm(model.layers[il].wqkv, cur);
  13329. cb(qkv, "wqkv", il);
  13330. // split QKV tensor into Q, K, V
  13331. const int64_t n_embd_head_q = hparams.n_embd_head_k;
  13332. const int64_t n_embd_head_k = hparams.n_embd_head_k;
  13333. const int64_t n_embd_head_v = hparams.n_embd_head_v;
  13334. int32_t n_head_kv = hparams.n_head_kv(il);
  13335. const int64_t q_offset = 0;
  13336. const int64_t k_offset = n_embd_head_q * n_head;
  13337. const int64_t v_offset = k_offset + n_embd_head_k * n_head_kv;
  13338. ggml_tensor * Qcur = ggml_view_3d(ctx0, qkv, n_embd_head_q, n_head, n_tokens, n_embd_head_q * sizeof(float), qkv->nb[1], q_offset * ggml_element_size(qkv));
  13339. ggml_tensor * Kcur = ggml_view_3d(ctx0, qkv, n_embd_head_k, n_head_kv, n_tokens, n_embd_head_k * sizeof(float), qkv->nb[1], k_offset * ggml_element_size(qkv));
  13340. ggml_tensor * Vcur = ggml_cont(ctx0, ggml_view_2d(ctx0, qkv, n_embd_head_v * n_head_kv, n_tokens, qkv->nb[1], v_offset * ggml_element_size(qkv)));
  13341. cb(Qcur, "Qcur", il);
  13342. cb(Kcur, "Kcur", il);
  13343. cb(Vcur, "Vcur", il);
  13344. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head_v, n_head_kv, n_tokens);
  13345. Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);
  13346. cb(Qcur, "Qcur_normed", il);
  13347. Qcur = ggml_rope_ext(
  13348. ctx0, Qcur, inp_pos, nullptr,
  13349. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  13350. ext_factor, attn_factor, beta_fast, beta_slow
  13351. );
  13352. Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);
  13353. cb(Kcur, "Kcur_normed", il);
  13354. Kcur = ggml_rope_ext(
  13355. ctx0, Kcur, inp_pos, nullptr,
  13356. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  13357. ext_factor, attn_factor, beta_fast, beta_slow
  13358. );
  13359. cur = build_attn(inp, model.layers[il].wo, NULL, Qcur, Kcur, Vcur, NULL, NULL, 1.0f/sqrtf(float(n_embd_head_v)), il);
  13360. }
  13361. cb(cur, "attn_out", il);
  13362. return cur;
  13363. }
  13364. ggml_tensor * build_plamo2_mamba_layer(
  13365. llm_graph_input_rs * inp,
  13366. ggml_tensor * cur,
  13367. const llama_model & model,
  13368. const llama_ubatch & ubatch,
  13369. int il) {
  13370. const auto * mctx_cur = inp->mctx;
  13371. const auto kv_head = mctx_cur->get_head();
  13372. const int64_t d_conv = hparams.ssm_d_conv;
  13373. const int64_t d_inner = hparams.ssm_d_inner;
  13374. const int64_t d_state = hparams.ssm_d_state;
  13375. const int64_t n_heads = hparams.ssm_dt_rank;
  13376. const int64_t head_dim = d_inner / n_heads;
  13377. const int64_t n_group = hparams.ssm_n_group;
  13378. const int64_t n_seqs = ubatch.n_seqs;
  13379. const int64_t n_seq_tokens = ubatch.n_seq_tokens;
  13380. GGML_ASSERT(n_seqs != 0);
  13381. GGML_ASSERT(ubatch.equal_seqs());
  13382. GGML_ASSERT(ubatch.n_tokens == n_seq_tokens * n_seqs);
  13383. ggml_tensor * conv_states_all = mctx_cur->get_r_l(il);
  13384. ggml_tensor * ssm_states_all = mctx_cur->get_s_l(il);
  13385. ggml_tensor * conv = build_rs(inp, conv_states_all, hparams.n_embd_r(), n_seqs);
  13386. conv = ggml_reshape_3d(ctx0, conv, d_conv - 1, d_inner + 2*n_group*d_state, n_seqs);
  13387. // {n_embd, n_tokens} => {n_embd, n_seq_tokens, n_seqs}
  13388. cur = ggml_reshape_3d(ctx0, cur, cur->ne[0], n_seq_tokens, n_seqs);
  13389. // in_proj: {n_embd, 2*d_inner} @ {n_embd, n_seq_tokens, n_seqs} => {2*d_inner, n_seq_tokens, n_seqs}
  13390. ggml_tensor * zx = build_lora_mm(model.layers[il].ssm_in, cur);
  13391. cb(zx, "mamba_in_proj", il);
  13392. // {8192, 5, 1, 1} -> {8192, 1, 5, 1}
  13393. zx = ggml_permute(ctx0, zx, 0, 2, 1, 3);
  13394. zx = ggml_cont(ctx0, zx);
  13395. zx = ggml_reshape_4d(ctx0, zx, head_dim * 2, n_heads, n_seq_tokens, n_seqs);
  13396. cb(zx, "mamba_in_proj_out", il);
  13397. // split into z and x
  13398. // => {head_dim * n_heads, n_seq_tokens, n_seqs}
  13399. ggml_tensor * x = ggml_view_4d(ctx0, zx, head_dim, n_heads, n_seq_tokens, n_seqs, zx->nb[1], zx->nb[2], zx->nb[3], head_dim*ggml_element_size(zx));
  13400. x = ggml_cont(ctx0, x);
  13401. x = ggml_reshape_3d(ctx0, x, head_dim * n_heads, n_seq_tokens, n_seqs);
  13402. // x = ggml_permute(ctx0, x, 0, 2, 1, 3);
  13403. cb(x, "mamba_x_split", il);
  13404. ggml_tensor * z = ggml_view_4d(ctx0, zx, head_dim, n_heads, n_seq_tokens, n_seqs, zx->nb[1], zx->nb[2], zx->nb[3], 0);
  13405. cb(z, "mamba_z_split", il);
  13406. // conv1d
  13407. {
  13408. // => {d_conv - 1 + n_seq_tokens, d_inner, n_seqs}
  13409. ggml_tensor * conv_x = ggml_concat(ctx0, conv, ggml_transpose(ctx0, x), 0);
  13410. cb(conv_x, "mamba_conv1d_input", il);
  13411. // copy last (d_conv - 1) columns back into the state cache
  13412. ggml_tensor * last_conv = ggml_view_3d(ctx0, conv_x, d_conv - 1, d_inner, n_seqs,
  13413. conv_x->nb[1], conv_x->nb[2], n_seq_tokens*(conv_x->nb[0]));
  13414. ggml_build_forward_expand(gf,
  13415. ggml_cpy(ctx0, last_conv,
  13416. ggml_view_1d(ctx0, conv_states_all,
  13417. (d_conv - 1)*(d_inner + 2*n_group*d_state)*(n_seqs),
  13418. kv_head*(d_conv - 1)*(d_inner + 2*n_group*d_state)*ggml_element_size(conv_states_all))));
  13419. cb(conv_states_all, "mamba_conv1d_state", il);
  13420. // 1D convolution
  13421. x = ggml_ssm_conv(ctx0, conv_x, model.layers[il].ssm_conv1d);
  13422. cb(x, "mamba_conv1d", il);
  13423. x = ggml_silu(ctx0, x);
  13424. cb(x, "mamba_conv1d_silu", il);
  13425. }
  13426. // SSM
  13427. {
  13428. // bcdt_proj: {d_inner, dt_rank + 2*d_state} @ {d_inner, n_seq_tokens, n_seqs} => {dt_rank + 2*d_state, n_seq_tokens, n_seqs}
  13429. ggml_tensor * x_bcdt = build_lora_mm(model.layers[il].ssm_x, x);
  13430. cb(x_bcdt, "mamba_bcdt_proj", il);
  13431. // split into dt, B, C
  13432. const int64_t dt_dim = std::max(64, int(hparams.n_embd / 16));
  13433. ggml_tensor * B = ggml_view_3d(ctx0, x_bcdt, d_state, n_seq_tokens, n_seqs, x_bcdt->nb[1], x_bcdt->nb[2], 0);
  13434. ggml_tensor * C = ggml_view_3d(ctx0, x_bcdt, d_state, n_seq_tokens, n_seqs, x_bcdt->nb[1], x_bcdt->nb[2], ggml_element_size(x_bcdt)*d_state);
  13435. ggml_tensor * dt = ggml_view_3d(ctx0, x_bcdt, dt_dim, n_seq_tokens, n_seqs, x_bcdt->nb[1], x_bcdt->nb[2], ggml_element_size(x_bcdt)*(2*d_state));
  13436. cb(B, "mamba_B_raw", il);
  13437. cb(C, "mamba_C_raw", il);
  13438. cb(dt, "mamba_dt_raw", il);
  13439. // Apply RMS norm to dt, B, C (PLaMo-2 specific)
  13440. B = build_norm(B, model.layers[il].ssm_b_norm, NULL, LLM_NORM_RMS, il);
  13441. C = build_norm(C, model.layers[il].ssm_c_norm, NULL, LLM_NORM_RMS, il);
  13442. dt = build_norm(dt, model.layers[il].ssm_dt_norm, NULL, LLM_NORM_RMS, il);
  13443. cb(B, "mamba_B_normed", il);
  13444. cb(C, "mamba_C_normed", il);
  13445. cb(dt, "mamba_dt_normed", il);
  13446. // dt_proj: {dt_rank, d_inner} @ {dt_rank, n_seq_tokens, n_seqs} => {d_inner, n_seq_tokens, n_seqs}
  13447. dt = build_lora_mm(model.layers[il].ssm_dt, dt);
  13448. dt = ggml_add(ctx0, dt, model.layers[il].ssm_dt_b);
  13449. cb(dt, "mamba_dt_proj", il);
  13450. ggml_tensor * A = ggml_reshape_2d(ctx0, model.layers[il].ssm_a, 1, n_heads);
  13451. cb(A, "mamba_A", il);
  13452. x = ggml_view_4d(ctx0, x, head_dim, n_heads, n_seq_tokens, n_seqs, head_dim * ggml_element_size(x), head_dim * n_heads * ggml_element_size(x), head_dim * n_heads * n_seq_tokens * ggml_element_size(x), 0);
  13453. B = ggml_view_4d(ctx0, B, d_state, 1, n_seq_tokens, n_seqs, d_state * B->nb[0], B->nb[1], B->nb[2], 0);
  13454. C = ggml_view_4d(ctx0, C, d_state, 1, n_seq_tokens, n_seqs, d_state * C->nb[0], C->nb[1], C->nb[2], 0);
  13455. // use the states and the indices provided by build_recurrent_state
  13456. // (this is necessary in order to properly use the states before they are overwritten,
  13457. // while avoiding to make unnecessary copies of the states)
  13458. auto get_ssm_rows = [&](ggml_context * ctx, ggml_tensor * states, ggml_tensor * ids) {
  13459. ggml_tensor * ssm = ggml_reshape_4d(ctx, states, d_state, head_dim, n_heads, mctx_cur->get_size());
  13460. // Custom operator to optimize the parallel associative scan
  13461. // as described in the Annex D of the Mamba paper.
  13462. // => {d_inner, n_seq_tokens, n_seqs} and {d_state, d_inner, n_seqs}
  13463. return ggml_ssm_scan(ctx, ssm, x, dt, A, B, C, ids);
  13464. };
  13465. ggml_tensor * y_ssm = build_rs(inp, ssm_states_all, hparams.n_embd_s(), ubatch.n_seqs, get_ssm_rows);
  13466. cb(y_ssm, "mamba_ssm_scan", il);
  13467. // store last states
  13468. ggml_build_forward_expand(gf,
  13469. ggml_cpy(ctx0,
  13470. ggml_view_1d(ctx0, y_ssm, n_heads*head_dim*d_state*n_seqs, n_heads*head_dim*n_seq_tokens*n_seqs*ggml_element_size(y_ssm)),
  13471. ggml_view_1d(ctx0, ssm_states_all, n_heads*head_dim*d_state*n_seqs, kv_head*n_seqs*n_heads*head_dim*d_state*ggml_element_size(ssm_states_all))));
  13472. cb(ssm_states_all, "mamba_ssm_states", il);
  13473. ggml_tensor * y = ggml_view_4d(ctx0, y_ssm, head_dim, n_heads, n_seq_tokens, n_seqs, head_dim * ggml_element_size(x), head_dim * n_heads * ggml_element_size(x), head_dim * n_heads * n_seq_tokens * ggml_element_size(x), 0);
  13474. cb(y, "mamba_y_view", il);
  13475. // Add D parameter and apply gating with z
  13476. // {d_inner, n_seq_tokens, n_seqs} * {d_inner} => {d_inner, n_seq_tokens, n_seqs}
  13477. ggml_tensor * D = ggml_reshape_2d(ctx0, model.layers[il].ssm_d, 1, n_heads);
  13478. y = ggml_add(ctx0, y, ggml_mul(ctx0, x, D));
  13479. cb(y, "mamba_y_add_d", il);
  13480. y = ggml_swiglu_split(ctx0, ggml_cont(ctx0, z), y);
  13481. cb(y, "mamba_y_swiglu_z", il);
  13482. // out_proj: {d_inner, n_embd} @ {d_inner, n_seq_tokens, n_seqs} => {n_embd, n_seq_tokens, n_seqs}
  13483. y = ggml_view_3d(ctx0, y, head_dim * n_heads, n_seq_tokens, n_seqs, y->nb[2], y->nb[3], 0);
  13484. cur = build_lora_mm(model.layers[il].ssm_out, y);
  13485. cb(cur, "mamba_out_proj", il);
  13486. }
  13487. // {n_embd, n_seq_tokens, n_seqs} => {n_embd, n_tokens}
  13488. cur = ggml_reshape_2d(ctx0, cur, cur->ne[0], n_seq_tokens * n_seqs);
  13489. cb(cur, "mamba_out", il);
  13490. return cur;
  13491. }
  13492. };
  13493. struct llm_build_arcee : public llm_graph_context {
  13494. llm_build_arcee(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  13495. const int64_t n_embd_head = hparams.n_embd_head_v;
  13496. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  13497. GGML_ASSERT(n_embd_head == hparams.n_rot);
  13498. ggml_tensor * cur;
  13499. ggml_tensor * inpL;
  13500. inpL = build_inp_embd(model.tok_embd);
  13501. // inp_pos - contains the positions
  13502. ggml_tensor * inp_pos = build_inp_pos();
  13503. auto * inp_attn = build_attn_inp_kv_unified();
  13504. const float kq_scale = hparams.f_attention_scale == 0.0f ? 1.0f/sqrtf(float(n_embd_head)) : hparams.f_attention_scale;
  13505. ggml_tensor * inp_out_ids = build_inp_out_ids();
  13506. for (int il = 0; il < n_layer; ++il) {
  13507. ggml_tensor * inpSA = inpL;
  13508. // norm
  13509. cur = build_norm(inpL,
  13510. model.layers[il].attn_norm, NULL,
  13511. LLM_NORM_RMS, il);
  13512. cb(cur, "attn_norm", il);
  13513. // self-attention
  13514. {
  13515. // rope freq factors for llama3; may return nullptr for llama2 and other models
  13516. ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
  13517. // compute Q and K and RoPE them
  13518. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  13519. cb(Qcur, "Qcur", il);
  13520. if (model.layers[il].bq) {
  13521. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  13522. cb(Qcur, "Qcur", il);
  13523. }
  13524. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  13525. cb(Kcur, "Kcur", il);
  13526. if (model.layers[il].bk) {
  13527. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  13528. cb(Kcur, "Kcur", il);
  13529. }
  13530. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  13531. cb(Vcur, "Vcur", il);
  13532. if (model.layers[il].bv) {
  13533. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  13534. cb(Vcur, "Vcur", il);
  13535. }
  13536. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  13537. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  13538. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  13539. Qcur = ggml_rope_ext(
  13540. ctx0, Qcur, inp_pos, rope_factors,
  13541. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  13542. ext_factor, attn_factor, beta_fast, beta_slow
  13543. );
  13544. Kcur = ggml_rope_ext(
  13545. ctx0, Kcur, inp_pos, rope_factors,
  13546. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  13547. ext_factor, attn_factor, beta_fast, beta_slow
  13548. );
  13549. cb(Qcur, "Qcur", il);
  13550. cb(Kcur, "Kcur", il);
  13551. cb(Vcur, "Vcur", il);
  13552. cur = build_attn(inp_attn,
  13553. model.layers[il].wo, model.layers[il].bo,
  13554. Qcur, Kcur, Vcur, nullptr, nullptr, kq_scale, il);
  13555. cb(cur, "attn_out", il);
  13556. }
  13557. if (il == n_layer - 1 && inp_out_ids) {
  13558. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  13559. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  13560. }
  13561. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  13562. cb(ffn_inp, "ffn_inp", il);
  13563. // feed-forward network
  13564. // ARCEE uses relu^2 instead of silu
  13565. cur = build_norm(ffn_inp,
  13566. model.layers[il].ffn_norm, NULL,
  13567. LLM_NORM_RMS, il);
  13568. cb(cur, "ffn_norm", il);
  13569. cur = build_ffn(cur,
  13570. model.layers[il].ffn_up, NULL, NULL,
  13571. NULL, NULL, NULL,
  13572. model.layers[il].ffn_down, NULL, NULL,
  13573. NULL,
  13574. LLM_FFN_RELU_SQR, LLM_FFN_SEQ, il);
  13575. cb(cur, "ffn_out", il);
  13576. cur = ggml_add(ctx0, cur, ffn_inp);
  13577. cb(cur, "ffn_out", il);
  13578. cur = build_cvec(cur, il);
  13579. cb(cur, "l_out", il);
  13580. // input for next layer
  13581. inpL = cur;
  13582. }
  13583. cur = inpL;
  13584. cur = build_norm(cur,
  13585. model.output_norm, NULL,
  13586. LLM_NORM_RMS, -1);
  13587. cb(cur, "result_norm", -1);
  13588. res->t_embd = cur;
  13589. // lm_head
  13590. cur = build_lora_mm(model.output, cur);
  13591. cb(cur, "result_output", -1);
  13592. res->t_logits = cur;
  13593. ggml_build_forward_expand(gf, cur);
  13594. }
  13595. };
  13596. struct llm_build_hunyuan_moe : public llm_graph_context {
  13597. llm_build_hunyuan_moe(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  13598. const int64_t n_embd_head = hparams.n_embd_head_v;
  13599. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  13600. GGML_ASSERT(n_embd_head == hparams.n_rot);
  13601. ggml_tensor * cur;
  13602. ggml_tensor * inpL;
  13603. inpL = build_inp_embd(model.tok_embd);
  13604. // inp_pos - contains the positions
  13605. ggml_tensor * inp_pos = build_inp_pos();
  13606. auto * inp_attn = build_attn_inp_kv_unified();
  13607. const float kq_scale = 1.0f / sqrtf(float(n_embd_head));
  13608. ggml_tensor * inp_out_ids = build_inp_out_ids();
  13609. for (int il = 0; il < n_layer; ++il) {
  13610. ggml_tensor * inpSA = inpL;
  13611. // norm
  13612. cur = build_norm(inpL,
  13613. model.layers[il].attn_norm, NULL,
  13614. LLM_NORM_RMS, il);
  13615. cb(cur, "attn_norm", il);
  13616. // self-attention
  13617. {
  13618. // rope freq factors for llama3; may return nullptr for llama2 and other models
  13619. ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
  13620. // compute Q and K and RoPE them
  13621. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  13622. cb(Qcur, "Qcur", il);
  13623. if (model.layers[il].bq) {
  13624. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  13625. cb(Qcur, "Qcur", il);
  13626. }
  13627. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  13628. cb(Kcur, "Kcur", il);
  13629. if (model.layers[il].bk) {
  13630. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  13631. cb(Kcur, "Kcur", il);
  13632. }
  13633. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  13634. cb(Vcur, "Vcur", il);
  13635. if (model.layers[il].bv) {
  13636. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  13637. cb(Vcur, "Vcur", il);
  13638. }
  13639. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  13640. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  13641. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  13642. Qcur = ggml_rope_ext(
  13643. ctx0, Qcur, inp_pos, rope_factors,
  13644. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  13645. ext_factor, attn_factor, beta_fast, beta_slow
  13646. );
  13647. cb(Qcur, "Qcur", il);
  13648. cb(Kcur, "Kcur", il);
  13649. cb(Vcur, "Vcur", il);
  13650. Kcur = ggml_rope_ext(
  13651. ctx0, Kcur, inp_pos, rope_factors,
  13652. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  13653. ext_factor, attn_factor, beta_fast, beta_slow
  13654. );
  13655. Kcur = build_norm(Kcur,
  13656. model.layers[il].attn_k_norm, nullptr,
  13657. LLM_NORM_RMS, il);
  13658. cb(Kcur, "Kcur_norm", il);
  13659. Qcur = build_norm(Qcur,
  13660. model.layers[il].attn_q_norm, nullptr,
  13661. LLM_NORM_RMS, il);
  13662. cb(Qcur, "Qcur_norm", il);
  13663. cur = build_attn(inp_attn,
  13664. model.layers[il].wo, model.layers[il].bo,
  13665. Qcur, Kcur, Vcur, nullptr, nullptr, kq_scale, il);
  13666. cb(cur, "attn_out", il);
  13667. }
  13668. if (il == n_layer - 1 && inp_out_ids) {
  13669. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  13670. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  13671. }
  13672. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  13673. cb(ffn_inp, "ffn_inp", il);
  13674. cur = build_norm(ffn_inp,
  13675. model.layers[il].ffn_norm, NULL,
  13676. LLM_NORM_RMS, il);
  13677. cb(cur, "ffn_norm", il);
  13678. // feed-forward network (non-MoE)
  13679. ggml_tensor * cur_mlp = build_ffn(cur,
  13680. model.layers[il].ffn_up_shexp, NULL, NULL,
  13681. model.layers[il].ffn_gate_shexp, NULL, NULL,
  13682. model.layers[il].ffn_down_shexp, NULL, NULL,
  13683. NULL,
  13684. LLM_FFN_SILU, LLM_FFN_PAR, il);
  13685. cb(cur_mlp, "ffn_mlp", il);
  13686. // MoE branch
  13687. ggml_tensor * cur_moe = build_moe_ffn(cur,
  13688. model.layers[il].ffn_gate_inp,
  13689. model.layers[il].ffn_up_exps,
  13690. model.layers[il].ffn_gate_exps,
  13691. model.layers[il].ffn_down_exps,
  13692. nullptr,
  13693. n_expert, n_expert_used,
  13694. LLM_FFN_SILU,
  13695. true, // norm_topk_prob
  13696. false,
  13697. 0.0,
  13698. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  13699. il);
  13700. cb(cur_moe, "ffn_moe_out", il);
  13701. ggml_tensor * ffn_out = ggml_add(ctx0, cur_moe, cur_mlp);
  13702. cb(ffn_out, "ffn_out", il);
  13703. cur = ggml_add(ctx0, ffn_out, ffn_inp);
  13704. cur = build_cvec(cur, il);
  13705. cb(cur, "l_out", il);
  13706. // input for next layer
  13707. inpL = cur;
  13708. }
  13709. cur = inpL;
  13710. cur = build_norm(cur,
  13711. model.output_norm, NULL,
  13712. LLM_NORM_RMS, -1);
  13713. cb(cur, "result_norm", -1);
  13714. res->t_embd = cur;
  13715. // lm_head
  13716. cur = build_lora_mm(model.output, cur);
  13717. cb(cur, "result_output", -1);
  13718. res->t_logits = cur;
  13719. ggml_build_forward_expand(gf, cur);
  13720. }
  13721. };
  13722. struct llm_build_hunyuan_dense : public llm_graph_context {
  13723. llm_build_hunyuan_dense(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  13724. const int64_t n_embd_head = hparams.n_embd_head_v;
  13725. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  13726. GGML_ASSERT(n_embd_head == hparams.n_rot);
  13727. ggml_tensor * cur;
  13728. ggml_tensor * inpL;
  13729. inpL = build_inp_embd(model.tok_embd);
  13730. // inp_pos - contains the positions
  13731. ggml_tensor * inp_pos = build_inp_pos();
  13732. auto * inp_attn = build_attn_inp_kv_unified();
  13733. const float kq_scale = 1.0f / sqrtf(float(n_embd_head));
  13734. ggml_tensor * inp_out_ids = build_inp_out_ids();
  13735. for (int il = 0; il < n_layer; ++il) {
  13736. ggml_tensor * inpSA = inpL;
  13737. // norm
  13738. cur = build_norm(inpL,
  13739. model.layers[il].attn_norm, NULL,
  13740. LLM_NORM_RMS, il);
  13741. cb(cur, "attn_norm", il);
  13742. // self-attention
  13743. {
  13744. // rope freq factors for llama3; may return nullptr for llama2 and other models
  13745. ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
  13746. // compute Q and K and RoPE them
  13747. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  13748. cb(Qcur, "Qcur", il);
  13749. if (model.layers[il].bq) {
  13750. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  13751. cb(Qcur, "Qcur", il);
  13752. }
  13753. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  13754. cb(Kcur, "Kcur", il);
  13755. if (model.layers[il].bk) {
  13756. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  13757. cb(Kcur, "Kcur", il);
  13758. }
  13759. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  13760. cb(Vcur, "Vcur", il);
  13761. if (model.layers[il].bv) {
  13762. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  13763. cb(Vcur, "Vcur", il);
  13764. }
  13765. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  13766. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  13767. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  13768. Qcur = ggml_rope_ext(
  13769. ctx0, Qcur, inp_pos, rope_factors,
  13770. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  13771. ext_factor, attn_factor, beta_fast, beta_slow
  13772. );
  13773. cb(Qcur, "Qcur", il);
  13774. cb(Kcur, "Kcur", il);
  13775. cb(Vcur, "Vcur", il);
  13776. Kcur = ggml_rope_ext(
  13777. ctx0, Kcur, inp_pos, rope_factors,
  13778. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  13779. ext_factor, attn_factor, beta_fast, beta_slow
  13780. );
  13781. Kcur = build_norm(Kcur,
  13782. model.layers[il].attn_k_norm, nullptr,
  13783. LLM_NORM_RMS, il);
  13784. cb(Kcur, "Kcur_norm", il);
  13785. Qcur = build_norm(Qcur,
  13786. model.layers[il].attn_q_norm, nullptr,
  13787. LLM_NORM_RMS, il);
  13788. cb(Qcur, "Qcur_norm", il);
  13789. cur = build_attn(inp_attn,
  13790. model.layers[il].wo, model.layers[il].bo,
  13791. Qcur, Kcur, Vcur, nullptr, nullptr, kq_scale, il);
  13792. cb(cur, "attn_out", il);
  13793. }
  13794. if (il == n_layer - 1 && inp_out_ids) {
  13795. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  13796. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  13797. }
  13798. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  13799. cb(ffn_inp, "ffn_inp", il);
  13800. cur = build_norm(ffn_inp,
  13801. model.layers[il].ffn_norm, NULL,
  13802. LLM_NORM_RMS, il);
  13803. cb(cur, "ffn_norm", il);
  13804. // feed-forward network (non-MoE)
  13805. ggml_tensor * cur_mlp = build_ffn(cur,
  13806. model.layers[il].ffn_up, NULL, NULL,
  13807. model.layers[il].ffn_gate, NULL, NULL,
  13808. model.layers[il].ffn_down, NULL, NULL,
  13809. NULL,
  13810. LLM_FFN_SILU, LLM_FFN_PAR, il);
  13811. cb(cur_mlp, "ffn_out", il);
  13812. cur = ggml_add(ctx0, cur_mlp, ffn_inp);
  13813. cur = build_cvec(cur, il);
  13814. cb(cur, "l_out", il);
  13815. // input for next layer
  13816. inpL = cur;
  13817. }
  13818. cur = inpL;
  13819. cur = build_norm(cur,
  13820. model.output_norm, NULL,
  13821. LLM_NORM_RMS, -1);
  13822. cb(cur, "result_norm", -1);
  13823. res->t_embd = cur;
  13824. // lm_head
  13825. cur = build_lora_mm(model.output, cur);
  13826. cb(cur, "result_output", -1);
  13827. res->t_logits = cur;
  13828. ggml_build_forward_expand(gf, cur);
  13829. }
  13830. };
  13831. struct llm_build_smollm3 : public llm_graph_context {
  13832. llm_build_smollm3(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  13833. const int64_t n_embd_head = hparams.n_embd_head_v;
  13834. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  13835. GGML_ASSERT(n_embd_head == hparams.n_rot);
  13836. ggml_tensor * cur;
  13837. ggml_tensor * inpL;
  13838. inpL = build_inp_embd(model.tok_embd);
  13839. // inp_pos - contains the positions
  13840. ggml_tensor * inp_pos = build_inp_pos();
  13841. auto * inp_attn = build_attn_inp_kv_unified();
  13842. const float kq_scale = hparams.f_attention_scale == 0.0f ? 1.0f/sqrtf(float(n_embd_head)) : hparams.f_attention_scale;
  13843. ggml_tensor * inp_out_ids = build_inp_out_ids();
  13844. for (int il = 0; il < n_layer; ++il) {
  13845. ggml_tensor * inpSA = inpL;
  13846. const bool use_rope = (il + 1) % hparams.n_no_rope_layer_step != 0;
  13847. // norm
  13848. cur = build_norm(inpL,
  13849. model.layers[il].attn_norm, NULL,
  13850. LLM_NORM_RMS, il);
  13851. cb(cur, "attn_norm", il);
  13852. // self-attention
  13853. {
  13854. // compute Q and K and RoPE them
  13855. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  13856. cb(Qcur, "Qcur", il);
  13857. if (model.layers[il].bq) {
  13858. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  13859. cb(Qcur, "Qcur", il);
  13860. }
  13861. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  13862. cb(Kcur, "Kcur", il);
  13863. if (model.layers[il].bk) {
  13864. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  13865. cb(Kcur, "Kcur", il);
  13866. }
  13867. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  13868. cb(Vcur, "Vcur", il);
  13869. if (model.layers[il].bv) {
  13870. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  13871. cb(Vcur, "Vcur", il);
  13872. }
  13873. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  13874. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  13875. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  13876. if (use_rope) {
  13877. Qcur = ggml_rope_ext(
  13878. ctx0, Qcur, inp_pos, nullptr,
  13879. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  13880. ext_factor, attn_factor, beta_fast, beta_slow
  13881. );
  13882. Kcur = ggml_rope_ext(
  13883. ctx0, Kcur, inp_pos, nullptr,
  13884. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  13885. ext_factor, attn_factor, beta_fast, beta_slow
  13886. );
  13887. }
  13888. cb(Qcur, "Qcur", il);
  13889. cb(Kcur, "Kcur", il);
  13890. cb(Vcur, "Vcur", il);
  13891. cur = build_attn(inp_attn,
  13892. model.layers[il].wo, model.layers[il].bo,
  13893. Qcur, Kcur, Vcur, nullptr, nullptr, kq_scale, il);
  13894. cb(cur, "attn_out", il);
  13895. }
  13896. if (il == n_layer - 1 && inp_out_ids) {
  13897. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  13898. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  13899. }
  13900. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  13901. cb(ffn_inp, "ffn_inp", il);
  13902. // feed-forward network
  13903. {
  13904. cur = build_norm(ffn_inp,
  13905. model.layers[il].ffn_norm, NULL,
  13906. LLM_NORM_RMS, il);
  13907. cb(cur, "ffn_norm", il);
  13908. cur = build_ffn(cur,
  13909. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  13910. model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, NULL,
  13911. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  13912. NULL,
  13913. LLM_FFN_SILU, LLM_FFN_PAR, il);
  13914. cb(cur, "ffn_out", il);
  13915. }
  13916. cur = ggml_add(ctx0, cur, ffn_inp);
  13917. cb(cur, "ffn_out", il);
  13918. cur = build_cvec(cur, il);
  13919. cb(cur, "l_out", il);
  13920. // input for next layer
  13921. inpL = cur;
  13922. }
  13923. cur = inpL;
  13924. cur = build_norm(cur,
  13925. model.output_norm, NULL,
  13926. LLM_NORM_RMS, -1);
  13927. cb(cur, "result_norm", -1);
  13928. res->t_embd = cur;
  13929. // lm_head
  13930. cur = build_lora_mm(model.output, cur);
  13931. cb(cur, "result_output", -1);
  13932. res->t_logits = cur;
  13933. ggml_build_forward_expand(gf, cur);
  13934. }
  13935. };
  13936. struct llm_build_openai_moe_iswa : public llm_graph_context {
  13937. llm_build_openai_moe_iswa(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  13938. ggml_tensor * cur;
  13939. ggml_tensor * inpL;
  13940. inpL = build_inp_embd(model.tok_embd);
  13941. // inp_pos - contains the positions
  13942. ggml_tensor * inp_pos = build_inp_pos();
  13943. auto * inp_attn = build_attn_inp_kv_unified_iswa();
  13944. for (int il = 0; il < n_layer; ++il) {
  13945. ggml_tensor * inpSA = inpL;
  13946. // norm
  13947. cur = build_norm(inpL,
  13948. model.layers[il].attn_norm, nullptr,
  13949. LLM_NORM_RMS, il);
  13950. cb(cur, "attn_norm", il);
  13951. // self-attention
  13952. {
  13953. // compute Q and K and RoPE them
  13954. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  13955. cb(Qcur, "Qcur", il);
  13956. if (model.layers[il].bq) {
  13957. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  13958. cb(Qcur, "Qcur", il);
  13959. }
  13960. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  13961. cb(Kcur, "Kcur", il);
  13962. if (model.layers[il].bk) {
  13963. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  13964. cb(Kcur, "Kcur", il);
  13965. }
  13966. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  13967. cb(Vcur, "Vcur", il);
  13968. if (model.layers[il].bv) {
  13969. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  13970. cb(Vcur, "Vcur", il);
  13971. }
  13972. Qcur = ggml_reshape_3d(ctx0, Qcur, n_rot, n_head, n_tokens);
  13973. Kcur = ggml_reshape_3d(ctx0, Kcur, n_rot, n_head_kv, n_tokens);
  13974. Vcur = ggml_reshape_3d(ctx0, Vcur, n_rot, n_head_kv, n_tokens);
  13975. Qcur = ggml_rope_ext(
  13976. ctx0, Qcur, inp_pos, nullptr,
  13977. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  13978. ext_factor, attn_factor, beta_fast, beta_slow
  13979. );
  13980. Kcur = ggml_rope_ext(
  13981. ctx0, Kcur, inp_pos, nullptr,
  13982. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  13983. ext_factor, attn_factor, beta_fast, beta_slow
  13984. );
  13985. cb(Qcur, "Qcur", il);
  13986. cb(Kcur, "Kcur", il);
  13987. cb(Vcur, "Vcur", il);
  13988. cur = build_attn_with_sinks(inp_attn,
  13989. model.layers[il].wo, model.layers[il].bo,
  13990. Qcur, Kcur, Vcur, nullptr, nullptr, model.layers[il].attn_sinks, 1.0f/sqrtf(float(n_rot)), il);
  13991. cb(cur, "attn_out", il);
  13992. }
  13993. if (il == n_layer - 1) {
  13994. // skip computing output for unused tokens
  13995. ggml_tensor * inp_out_ids = build_inp_out_ids();
  13996. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  13997. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  13998. }
  13999. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  14000. cb(ffn_inp, "ffn_inp", il);
  14001. cur = ffn_inp;
  14002. cur = build_norm(cur,
  14003. model.layers[il].attn_post_norm, nullptr,
  14004. LLM_NORM_RMS, il);
  14005. cb(cur, "attn_post_norm", il);
  14006. // MoE branch
  14007. cur = build_moe_ffn(cur,
  14008. model.layers[il].ffn_gate_inp, model.layers[il].ffn_gate_inp_b,
  14009. model.layers[il].ffn_up_exps, model.layers[il].ffn_up_exps_b,
  14010. model.layers[il].ffn_gate_exps, model.layers[il].ffn_gate_exps_b,
  14011. model.layers[il].ffn_down_exps, model.layers[il].ffn_down_exps_b,
  14012. nullptr,
  14013. n_expert, n_expert_used,
  14014. LLM_FFN_SWIGLU_OAI_MOE, false,
  14015. false, 0.0,
  14016. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX_WEIGHT,
  14017. il);
  14018. cb(cur, "ffn_moe_out", il);
  14019. cur = ggml_add(ctx0, cur, ffn_inp);
  14020. cur = build_cvec(cur, il);
  14021. cb(cur, "l_out", il);
  14022. // input for next layer
  14023. inpL = cur;
  14024. }
  14025. cur = inpL;
  14026. cur = build_norm(cur,
  14027. model.output_norm, NULL,
  14028. LLM_NORM_RMS, -1);
  14029. cb(cur, "result_norm", -1);
  14030. res->t_embd = cur;
  14031. // lm_head
  14032. cur = build_lora_mm(model.output, cur);
  14033. cb(cur, "result_output", -1);
  14034. res->t_logits = cur;
  14035. ggml_build_forward_expand(gf, cur);
  14036. }
  14037. };
  14038. struct llm_build_lfm2 : public llm_graph_context {
  14039. const llama_model & model;
  14040. llm_build_lfm2(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params), model(model) {
  14041. ggml_tensor * cur = build_inp_embd(model.tok_embd);
  14042. cb(cur, "model.embed_tokens", -1);
  14043. ggml_tensor * inp_pos = build_inp_pos();
  14044. auto * inp_hybrid = build_inp_mem_hybrid();
  14045. ggml_tensor * inp_out_ids = build_inp_out_ids();
  14046. for (int il = 0; il < n_layer; ++il) {
  14047. auto * prev_cur = cur;
  14048. cur = build_norm(cur, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
  14049. cb(cur, "model.layers.{}.operator_norm", il);
  14050. cur = hparams.is_recurrent(il) ?
  14051. build_shortconv_block(cur, inp_hybrid->get_recr(), il) :
  14052. build_attn_block(cur, inp_pos, inp_hybrid->get_attn(), il) ;
  14053. if (il == n_layer - 1 && inp_out_ids) {
  14054. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  14055. prev_cur = ggml_get_rows(ctx0, prev_cur, inp_out_ids);
  14056. }
  14057. cur = ggml_add(ctx0, prev_cur, cur);
  14058. cur = ggml_add(ctx0, cur, build_feed_forward(cur, il));
  14059. }
  14060. cur = build_norm(cur, model.tok_norm, NULL, LLM_NORM_RMS, -1);
  14061. cb(cur, "model.embedding_norm", -1);
  14062. res->t_embd = cur;
  14063. // lm_head is tied with embeddings
  14064. cur = build_lora_mm(model.tok_embd, cur);
  14065. cb(cur, "lm_head", -1);
  14066. res->t_logits = cur;
  14067. ggml_build_forward_expand(gf, cur);
  14068. }
  14069. ggml_tensor * build_feed_forward(ggml_tensor * cur,
  14070. int il) const {
  14071. cur = build_norm(cur, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);
  14072. cb(cur, "model.layers.{}.ffn_norm", il);
  14073. GGML_ASSERT(!model.layers[il].ffn_up_b);
  14074. GGML_ASSERT(!model.layers[il].ffn_gate_b);
  14075. GGML_ASSERT(!model.layers[il].ffn_down_b);
  14076. cur = build_ffn(cur,
  14077. model.layers[il].ffn_up, NULL, NULL,
  14078. model.layers[il].ffn_gate, NULL, NULL,
  14079. model.layers[il].ffn_down, NULL, NULL,
  14080. NULL,
  14081. LLM_FFN_SILU, LLM_FFN_PAR, il);
  14082. cb(cur, "model.layers.{}.feed_forward.w2", il);
  14083. return cur;
  14084. }
  14085. ggml_tensor * build_attn_block(ggml_tensor * cur,
  14086. ggml_tensor * inp_pos,
  14087. llm_graph_input_attn_kv_unified * inp_attn,
  14088. int il) const {
  14089. GGML_ASSERT(hparams.n_embd_v_gqa(il) == hparams.n_embd_k_gqa(il));
  14090. auto const n_embd_head = hparams.n_embd_head_v;
  14091. auto const n_head_kv = hparams.n_head_kv(il);
  14092. auto * q = build_lora_mm(model.layers[il].wq, cur);
  14093. cb(q, "model.layers.{}.self_attn.q_proj", il);
  14094. auto * k = build_lora_mm(model.layers[il].wk, cur);
  14095. cb(k, "model.layers.{}.self_attn.k_proj", il);
  14096. auto * v = build_lora_mm(model.layers[il].wv, cur);
  14097. cb(v, "model.layers.{}.self_attn.v_proj", il);
  14098. q = ggml_reshape_3d(ctx0, q, n_embd_head, n_head, n_tokens);
  14099. k = ggml_reshape_3d(ctx0, k, n_embd_head, n_head_kv, n_tokens);
  14100. v = ggml_reshape_3d(ctx0, v, n_embd_head, n_head_kv, n_tokens);
  14101. // qk norm
  14102. q = build_norm(q, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);
  14103. cb(q, "model.layers.{}.self_attn.q_layernorm", il);
  14104. k = build_norm(k, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);
  14105. cb(k, "model.layers.{}.self_attn.k_layernorm", il);
  14106. // RoPE
  14107. q = ggml_rope_ext(
  14108. ctx0, q, inp_pos, nullptr,
  14109. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  14110. ext_factor, attn_factor, beta_fast, beta_slow
  14111. );
  14112. k = ggml_rope_ext(
  14113. ctx0, k, inp_pos, nullptr,
  14114. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  14115. ext_factor, attn_factor, beta_fast, beta_slow
  14116. );
  14117. cur = build_attn(inp_attn, model.layers[il].wo, NULL,
  14118. q, k, v, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  14119. cb(cur, "model.layers.{}.self_attn.out_proj", il);
  14120. return cur;
  14121. }
  14122. ggml_tensor * build_shortconv_block(ggml_tensor * cur,
  14123. llm_graph_input_rs * inp_recr,
  14124. int il) {
  14125. const auto * mctx_cur = static_cast<const llama_memory_hybrid_context *>(mctx)->get_recr();
  14126. const uint32_t kv_head = mctx_cur->get_head();
  14127. const int64_t n_seq_tokens = ubatch.n_seq_tokens;
  14128. const int64_t n_seqs = ubatch.n_seqs;
  14129. GGML_ASSERT(n_seqs != 0);
  14130. GGML_ASSERT(ubatch.equal_seqs());
  14131. GGML_ASSERT(ubatch.n_tokens == n_seq_tokens * n_seqs);
  14132. GGML_ASSERT(hparams.n_shortconv_l_cache > 1);
  14133. const uint32_t d_conv = hparams.n_shortconv_l_cache - 1;
  14134. // {n_embd, n_tokens} => {n_embd, n_seq_tokens, n_seqs}
  14135. cur = ggml_reshape_3d(ctx0, cur, cur->ne[0], n_seq_tokens, n_seqs);
  14136. auto * bcx = build_lora_mm(model.layers[il].shortconv.in_proj, cur);
  14137. cb(bcx, "model.layers.{}.conv.in_proj", il);
  14138. constexpr auto n_chunks = 3;
  14139. GGML_ASSERT(bcx->ne[0] % n_chunks == 0);
  14140. auto const chunk_size = bcx->ne[0] / n_chunks;
  14141. auto * b = ggml_view_3d(ctx0, bcx, chunk_size, bcx->ne[1], bcx->ne[2], bcx->nb[1], bcx->nb[2], 0*chunk_size*ggml_element_size(bcx));
  14142. auto * c = ggml_view_3d(ctx0, bcx, chunk_size, bcx->ne[1], bcx->ne[2], bcx->nb[1], bcx->nb[2], 1*chunk_size*ggml_element_size(bcx));
  14143. auto * x = ggml_view_3d(ctx0, bcx, chunk_size, bcx->ne[1], bcx->ne[2], bcx->nb[1], bcx->nb[2], 2*chunk_size*ggml_element_size(bcx));
  14144. auto * bx = ggml_transpose(ctx0, ggml_mul(ctx0, b, x));
  14145. // read conv state
  14146. auto * conv_state = mctx_cur->get_r_l(il);
  14147. auto * conv_rs = build_rs(inp_recr, conv_state, hparams.n_embd_r(), n_seqs);
  14148. auto * conv = ggml_reshape_3d(ctx0, conv_rs, d_conv, hparams.n_embd, n_seqs);
  14149. bx = ggml_concat(ctx0, conv, bx, 0);
  14150. GGML_ASSERT(bx->ne[0] > conv->ne[0]);
  14151. // last d_conv columns is a new conv state
  14152. auto * new_conv = ggml_view_3d(ctx0, bx, conv->ne[0], bx->ne[1], bx->ne[2], bx->nb[1], bx->nb[2], (bx->ne[0] - conv->ne[0])*ggml_element_size(bx));
  14153. GGML_ASSERT(ggml_are_same_shape(conv, new_conv));
  14154. // write new conv conv state
  14155. ggml_build_forward_expand(
  14156. gf,
  14157. ggml_cpy(
  14158. ctx0,
  14159. new_conv,
  14160. ggml_view_1d(
  14161. ctx0,
  14162. conv_state,
  14163. ggml_nelements(new_conv),
  14164. kv_head*d_conv*n_embd*ggml_element_size(new_conv)
  14165. )
  14166. )
  14167. );
  14168. auto * conv_kernel = model.layers[il].shortconv.conv;
  14169. auto * conv_out = ggml_ssm_conv(ctx0, bx, conv_kernel);
  14170. cb(conv_out, "model.layers.{}.conv.conv", il);
  14171. auto * y = ggml_mul(ctx0, c, conv_out);
  14172. y = build_lora_mm(model.layers[il].shortconv.out_proj, y);
  14173. cb(y, "model.layers.{}.conv.out_proj", il);
  14174. // {n_embd, n_seq_tokens, n_seqs} => {n_embd, n_tokens}
  14175. y = ggml_reshape_2d(ctx0, y, y->ne[0], n_seq_tokens * n_seqs);
  14176. return y;
  14177. }
  14178. };
  14179. template <bool iswa>
  14180. struct llm_build_smallthinker : public llm_graph_context{
  14181. llm_build_smallthinker(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params){
  14182. const int64_t n_embd_head = hparams.n_embd_head_v;
  14183. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  14184. GGML_ASSERT(n_embd_head == hparams.n_rot);
  14185. ggml_tensor * cur;
  14186. ggml_tensor * inpL;
  14187. inpL = build_inp_embd(model.tok_embd);
  14188. // inp_pos - contains the positions
  14189. ggml_tensor * inp_pos = build_inp_pos();
  14190. using inp_attn_type = std::conditional_t<iswa, llm_graph_input_attn_kv_unified_iswa, llm_graph_input_attn_kv_unified>;
  14191. inp_attn_type * inp_attn = nullptr;
  14192. if constexpr (iswa) {
  14193. inp_attn = build_attn_inp_kv_unified_iswa();
  14194. } else {
  14195. inp_attn = build_attn_inp_kv_unified();
  14196. }
  14197. ggml_tensor * inp_out_ids = build_inp_out_ids();
  14198. for (int il = 0; il < n_layer; ++il) {
  14199. ggml_tensor * inpSA = inpL;
  14200. ggml_tensor * probs = nullptr;
  14201. probs = build_lora_mm(model.layers[il].ffn_gate_inp, inpL); // [n_expert, n_tokens]
  14202. cb(probs, "ffn_moe_logits", il);
  14203. // norm
  14204. cur = build_norm(inpL,model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
  14205. cb(cur, "attn_norm", il);
  14206. // self_attention
  14207. {
  14208. // compute Q and K and RoPE them
  14209. struct ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  14210. cb(Qcur, "Qcur", il);
  14211. struct ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  14212. cb(Kcur, "Kcur", il);
  14213. struct ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  14214. cb(Vcur, "Vcur", il);
  14215. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  14216. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  14217. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  14218. if (hparams.n_no_rope_layer_step == n_layer || il % hparams.n_no_rope_layer_step != 0) {
  14219. Qcur = ggml_rope_ext(ctx0, Qcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  14220. ext_factor, attn_factor, beta_fast, beta_slow);
  14221. Kcur = ggml_rope_ext(ctx0, Kcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  14222. ext_factor, attn_factor, beta_fast, beta_slow);
  14223. }
  14224. cb(Qcur, "Qcur", il);
  14225. cb(Kcur, "Kcur", il);
  14226. cur = build_attn(inp_attn,
  14227. model.layers[il].wo, model.layers[il].bo,
  14228. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f / sqrtf(float(n_embd_head)), il);
  14229. }
  14230. if (il == n_layer - 1 && inp_out_ids) {
  14231. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  14232. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  14233. probs = ggml_get_rows(ctx0, probs, inp_out_ids);
  14234. }
  14235. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  14236. cb(ffn_inp, "ffn_inp", il);
  14237. // MoE branch
  14238. cur = build_norm(ffn_inp, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);
  14239. cb(cur, "ffn_norm", il);
  14240. ggml_tensor * ffn_out =
  14241. build_moe_ffn(cur,
  14242. nullptr,
  14243. model.layers[il].ffn_up_exps,
  14244. model.layers[il].ffn_gate_exps,
  14245. model.layers[il].ffn_down_exps,
  14246. nullptr,
  14247. n_expert, n_expert_used,
  14248. LLM_FFN_RELU, true,
  14249. false, 0.0,
  14250. static_cast<llama_expert_gating_func_type>(hparams.expert_gating_func),
  14251. il, probs);
  14252. cb(ffn_out, "ffn_out", il);
  14253. cur = ffn_out;
  14254. cur = ggml_add(ctx0, cur, ffn_inp);
  14255. cur = build_cvec(cur, il);
  14256. cb(cur, "l_out", il);
  14257. // input for next layer
  14258. inpL = cur;
  14259. }
  14260. cur = inpL;
  14261. cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1);
  14262. cb(cur, "result_norm", -1);
  14263. // lm_head
  14264. cur = build_lora_mm(model.output, cur);
  14265. cb(cur, "result_output", -1);
  14266. res->t_logits = cur;
  14267. ggml_build_forward_expand(gf, cur);
  14268. }
  14269. };
  14270. llama_memory_i * llama_model::create_memory(const llama_memory_params & params, llama_cparams & cparams) const {
  14271. llama_memory_i * res;
  14272. switch (arch) {
  14273. // Models that need specific instantiation should be handled in the
  14274. // switch statement
  14275. case LLM_ARCH_BERT:
  14276. case LLM_ARCH_JINA_BERT_V2:
  14277. case LLM_ARCH_NOMIC_BERT:
  14278. case LLM_ARCH_NOMIC_BERT_MOE:
  14279. case LLM_ARCH_NEO_BERT:
  14280. case LLM_ARCH_WAVTOKENIZER_DEC:
  14281. case LLM_ARCH_DREAM:
  14282. case LLM_ARCH_LLADA:
  14283. {
  14284. res = nullptr;
  14285. } break;
  14286. // Models that need standard caching should rely on recurrent/hybrid
  14287. // checks
  14288. default:
  14289. {
  14290. if (llm_arch_is_recurrent(arch)) {
  14291. res = new llama_memory_recurrent(
  14292. *this,
  14293. nullptr,
  14294. GGML_TYPE_F32,
  14295. GGML_TYPE_F32,
  14296. cparams.offload_kqv,
  14297. std::max((uint32_t) 1, cparams.n_seq_max),
  14298. cparams.n_seq_max);
  14299. } else if (llm_arch_is_hybrid(arch)) {
  14300. const auto padding = llama_kv_cache_unified::get_padding(cparams);
  14301. cparams.n_ctx = GGML_PAD(cparams.n_ctx, padding);
  14302. res = new llama_memory_hybrid(
  14303. /* model */ *this,
  14304. /* attn_type_k */ params.type_k,
  14305. /* attn_type_v */ params.type_v,
  14306. /* attn_v_trans */ !cparams.flash_attn,
  14307. /* attn_kv_size */ cparams.n_ctx,
  14308. /* attn_n_pad */ padding,
  14309. /* attn_n_swa */ hparams.n_swa,
  14310. /* attn_swa_type */ hparams.swa_type,
  14311. /* recurrent_type_k */ GGML_TYPE_F32,
  14312. /* recurrent_type_v */ GGML_TYPE_F32,
  14313. /* recurrent_kv_size */ std::max((uint32_t) 1, cparams.n_seq_max),
  14314. /* n_seq_max */ cparams.n_seq_max,
  14315. /* offload */ cparams.offload_kqv,
  14316. /* unified */ cparams.kv_unified,
  14317. /* filter_attn */ (arch == LLM_ARCH_FALCON_H1) ? [&](int32_t) { return true; } : (llama_memory_hybrid::layer_filter_cb)nullptr,
  14318. /* filter_recr */ (arch == LLM_ARCH_FALCON_H1) ? [&](int32_t) { return true; } : (llama_memory_hybrid::layer_filter_cb)nullptr);
  14319. } else {
  14320. const auto padding = llama_kv_cache_unified::get_padding(cparams);
  14321. uint32_t n_ctx_per_stream = cparams.n_ctx;
  14322. if (!cparams.kv_unified) {
  14323. n_ctx_per_stream = (cparams.n_ctx + cparams.n_seq_max - 1)/cparams.n_seq_max;
  14324. n_ctx_per_stream = GGML_PAD(n_ctx_per_stream, padding);
  14325. cparams.n_ctx = n_ctx_per_stream*cparams.n_seq_max;
  14326. } else {
  14327. n_ctx_per_stream = GGML_PAD(n_ctx_per_stream, padding);
  14328. cparams.n_ctx = n_ctx_per_stream;
  14329. }
  14330. LLAMA_LOG_DEBUG("%s: n_ctx = %u (padded)\n", __func__, cparams.n_ctx);
  14331. if (hparams.swa_type != LLAMA_SWA_TYPE_NONE) {
  14332. GGML_ASSERT(hparams.is_swa_any());
  14333. res = new llama_kv_cache_unified_iswa(
  14334. *this,
  14335. params.type_k,
  14336. params.type_v,
  14337. !cparams.flash_attn,
  14338. cparams.offload_kqv,
  14339. params.swa_full,
  14340. cparams.kv_unified,
  14341. n_ctx_per_stream,
  14342. cparams.n_seq_max,
  14343. cparams.n_ubatch,
  14344. padding);
  14345. } else {
  14346. GGML_ASSERT(!hparams.is_swa_any());
  14347. res = new llama_kv_cache_unified(
  14348. *this,
  14349. nullptr,
  14350. params.type_k,
  14351. params.type_v,
  14352. !cparams.flash_attn,
  14353. cparams.offload_kqv,
  14354. cparams.kv_unified,
  14355. n_ctx_per_stream,
  14356. cparams.n_seq_max,
  14357. padding,
  14358. hparams.n_swa,
  14359. hparams.swa_type);
  14360. }
  14361. }
  14362. }
  14363. }
  14364. return res;
  14365. }
  14366. ggml_cgraph * llama_model::build_graph(const llm_graph_params & params) const {
  14367. std::unique_ptr<llm_graph_context> llm;
  14368. switch (arch) {
  14369. case LLM_ARCH_LLAMA:
  14370. {
  14371. llm = std::make_unique<llm_build_llama>(*this, params);
  14372. } break;
  14373. case LLM_ARCH_LLAMA4:
  14374. {
  14375. llm = std::make_unique<llm_build_llama_iswa>(*this, params);
  14376. } break;
  14377. case LLM_ARCH_DECI:
  14378. {
  14379. llm = std::make_unique<llm_build_deci>(*this, params);
  14380. } break;
  14381. case LLM_ARCH_BAICHUAN:
  14382. {
  14383. llm = std::make_unique<llm_build_baichuan>(*this, params);
  14384. } break;
  14385. case LLM_ARCH_FALCON:
  14386. {
  14387. llm = std::make_unique<llm_build_falcon>(*this, params);
  14388. } break;
  14389. case LLM_ARCH_GROK:
  14390. {
  14391. llm = std::make_unique<llm_build_grok>(*this, params);
  14392. } break;
  14393. case LLM_ARCH_STARCODER:
  14394. {
  14395. llm = std::make_unique<llm_build_starcoder>(*this, params);
  14396. } break;
  14397. case LLM_ARCH_REFACT:
  14398. {
  14399. llm = std::make_unique<llm_build_refact>(*this, params);
  14400. } break;
  14401. case LLM_ARCH_BERT:
  14402. case LLM_ARCH_JINA_BERT_V2:
  14403. case LLM_ARCH_NOMIC_BERT:
  14404. case LLM_ARCH_NOMIC_BERT_MOE:
  14405. {
  14406. llm = std::make_unique<llm_build_bert>(*this, params);
  14407. } break;
  14408. case LLM_ARCH_NEO_BERT:
  14409. {
  14410. llm = std::make_unique<llm_build_neo_bert>(*this, params);
  14411. } break;
  14412. case LLM_ARCH_BLOOM:
  14413. {
  14414. llm = std::make_unique<llm_build_bloom>(*this, params);
  14415. } break;
  14416. case LLM_ARCH_MPT:
  14417. {
  14418. llm = std::make_unique<llm_build_mpt>(*this, params);
  14419. } break;
  14420. case LLM_ARCH_STABLELM:
  14421. {
  14422. llm = std::make_unique<llm_build_stablelm>(*this, params);
  14423. } break;
  14424. case LLM_ARCH_QWEN:
  14425. {
  14426. llm = std::make_unique<llm_build_qwen>(*this, params);
  14427. } break;
  14428. case LLM_ARCH_QWEN2:
  14429. {
  14430. llm = std::make_unique<llm_build_qwen2>(*this, params);
  14431. } break;
  14432. case LLM_ARCH_DREAM:
  14433. {
  14434. llm = std::make_unique<llm_build_dream>(*this, params);
  14435. }
  14436. break;
  14437. case LLM_ARCH_LLADA:
  14438. {
  14439. llm = std::make_unique<llm_build_llada>(*this, params);
  14440. }
  14441. break;
  14442. case LLM_ARCH_QWEN2VL:
  14443. {
  14444. llm = std::make_unique<llm_build_qwen2vl>(*this, params);
  14445. } break;
  14446. case LLM_ARCH_QWEN2MOE:
  14447. {
  14448. llm = std::make_unique<llm_build_qwen2moe>(*this, params);
  14449. } break;
  14450. case LLM_ARCH_QWEN3:
  14451. {
  14452. llm = std::make_unique<llm_build_qwen3>(*this, params);
  14453. } break;
  14454. case LLM_ARCH_QWEN3MOE:
  14455. {
  14456. llm = std::make_unique<llm_build_qwen3moe>(*this, params);
  14457. } break;
  14458. case LLM_ARCH_PHI2:
  14459. {
  14460. llm = std::make_unique<llm_build_phi2>(*this, params);
  14461. } break;
  14462. case LLM_ARCH_PHI3:
  14463. case LLM_ARCH_PHIMOE:
  14464. {
  14465. if (hparams.swa_type != LLAMA_SWA_TYPE_NONE) {
  14466. llm = std::make_unique<llm_build_phi3<true>> (*this, params);
  14467. } else {
  14468. llm = std::make_unique<llm_build_phi3<false>>(*this, params);
  14469. }
  14470. } break;
  14471. case LLM_ARCH_PLAMO:
  14472. {
  14473. llm = std::make_unique<llm_build_plamo>(*this, params);
  14474. } break;
  14475. case LLM_ARCH_PLAMO2:
  14476. {
  14477. llm = std::make_unique<llm_build_plamo2>(*this, params);
  14478. } break;
  14479. case LLM_ARCH_GPT2:
  14480. {
  14481. llm = std::make_unique<llm_build_gpt2>(*this, params);
  14482. } break;
  14483. case LLM_ARCH_CODESHELL:
  14484. {
  14485. llm = std::make_unique<llm_build_codeshell>(*this, params);
  14486. } break;
  14487. case LLM_ARCH_ORION:
  14488. {
  14489. llm = std::make_unique<llm_build_orion>(*this, params);
  14490. } break;
  14491. case LLM_ARCH_INTERNLM2:
  14492. {
  14493. llm = std::make_unique<llm_build_internlm2>(*this, params);
  14494. } break;
  14495. case LLM_ARCH_MINICPM3:
  14496. {
  14497. llm = std::make_unique<llm_build_minicpm3>(*this, params);
  14498. } break;
  14499. case LLM_ARCH_GEMMA:
  14500. {
  14501. llm = std::make_unique<llm_build_gemma>(*this, params);
  14502. } break;
  14503. case LLM_ARCH_GEMMA2:
  14504. {
  14505. llm = std::make_unique<llm_build_gemma2_iswa>(*this, params);
  14506. } break;
  14507. case LLM_ARCH_GEMMA3:
  14508. {
  14509. llm = std::make_unique<llm_build_gemma3_iswa>(*this, params);
  14510. } break;
  14511. case LLM_ARCH_GEMMA3N:
  14512. {
  14513. llm = std::make_unique<llm_build_gemma3n_iswa>(*this, params);
  14514. } break;
  14515. case LLM_ARCH_STARCODER2:
  14516. {
  14517. llm = std::make_unique<llm_build_starcoder2>(*this, params);
  14518. } break;
  14519. case LLM_ARCH_MAMBA:
  14520. case LLM_ARCH_MAMBA2:
  14521. {
  14522. llm = std::make_unique<llm_build_mamba>(*this, params);
  14523. } break;
  14524. case LLM_ARCH_JAMBA:
  14525. {
  14526. llm = std::make_unique<llm_build_jamba>(*this, params);
  14527. } break;
  14528. case LLM_ARCH_XVERSE:
  14529. {
  14530. llm = std::make_unique<llm_build_xverse>(*this, params);
  14531. } break;
  14532. case LLM_ARCH_COMMAND_R:
  14533. {
  14534. llm = std::make_unique<llm_build_command_r>(*this, params);
  14535. } break;
  14536. case LLM_ARCH_COHERE2:
  14537. {
  14538. llm = std::make_unique<llm_build_cohere2_iswa>(*this, params);
  14539. } break;
  14540. case LLM_ARCH_DBRX:
  14541. {
  14542. llm = std::make_unique<llm_build_dbrx>(*this, params);
  14543. } break;
  14544. case LLM_ARCH_OLMO:
  14545. {
  14546. llm = std::make_unique<llm_build_olmo>(*this, params);
  14547. } break;
  14548. case LLM_ARCH_OLMO2:
  14549. {
  14550. llm = std::make_unique<llm_build_olmo2>(*this, params);
  14551. } break;
  14552. case LLM_ARCH_OLMOE:
  14553. {
  14554. llm = std::make_unique<llm_build_olmoe>(*this, params);
  14555. } break;
  14556. case LLM_ARCH_OPENELM:
  14557. {
  14558. llm = std::make_unique<llm_build_openelm>(*this, params);
  14559. } break;
  14560. case LLM_ARCH_GPTNEOX:
  14561. {
  14562. llm = std::make_unique<llm_build_gptneox>(*this, params);
  14563. } break;
  14564. case LLM_ARCH_ARCTIC:
  14565. {
  14566. llm = std::make_unique<llm_build_arctic>(*this, params);
  14567. } break;
  14568. case LLM_ARCH_DEEPSEEK:
  14569. {
  14570. llm = std::make_unique<llm_build_deepseek>(*this, params);
  14571. } break;
  14572. case LLM_ARCH_DEEPSEEK2:
  14573. {
  14574. llm = std::make_unique<llm_build_deepseek2>(*this, params);
  14575. } break;
  14576. case LLM_ARCH_CHATGLM:
  14577. {
  14578. llm = std::make_unique<llm_build_chatglm>(*this, params);
  14579. } break;
  14580. case LLM_ARCH_GLM4:
  14581. {
  14582. llm = std::make_unique<llm_build_glm4>(*this, params);
  14583. } break;
  14584. case LLM_ARCH_GLM4_MOE:
  14585. {
  14586. llm = std::make_unique<llm_build_glm4_moe>(*this, params);
  14587. } break;
  14588. case LLM_ARCH_BITNET:
  14589. {
  14590. llm = std::make_unique<llm_build_bitnet>(*this, params);
  14591. } break;
  14592. case LLM_ARCH_T5:
  14593. {
  14594. switch (params.gtype) {
  14595. case LLM_GRAPH_TYPE_ENCODER:
  14596. llm = std::make_unique<llm_build_t5_enc>(*this, params);
  14597. break;
  14598. case LLM_GRAPH_TYPE_DEFAULT:
  14599. case LLM_GRAPH_TYPE_DECODER:
  14600. llm = std::make_unique<llm_build_t5_dec>(*this, params);
  14601. break;
  14602. default:
  14603. GGML_ABORT("invalid graph type");
  14604. };
  14605. } break;
  14606. case LLM_ARCH_T5ENCODER:
  14607. {
  14608. llm = std::make_unique<llm_build_t5_enc>(*this, params);
  14609. }
  14610. break;
  14611. case LLM_ARCH_JAIS:
  14612. {
  14613. llm = std::make_unique<llm_build_jais>(*this, params);
  14614. } break;
  14615. case LLM_ARCH_NEMOTRON:
  14616. {
  14617. llm = std::make_unique<llm_build_nemotron>(*this, params);
  14618. } break;
  14619. case LLM_ARCH_EXAONE:
  14620. {
  14621. llm = std::make_unique<llm_build_exaone>(*this, params);
  14622. } break;
  14623. case LLM_ARCH_EXAONE4:
  14624. {
  14625. if (hparams.swa_type == LLAMA_SWA_TYPE_STANDARD) {
  14626. llm = std::make_unique<llm_build_exaone4<true>>(*this, params);
  14627. } else {
  14628. llm = std::make_unique<llm_build_exaone4<false>>(*this, params);
  14629. }
  14630. } break;
  14631. case LLM_ARCH_RWKV6:
  14632. {
  14633. llm = std::make_unique<llm_build_rwkv6>(*this, params);
  14634. } break;
  14635. case LLM_ARCH_RWKV6QWEN2:
  14636. {
  14637. llm = std::make_unique<llm_build_rwkv6qwen2>(*this, params);
  14638. } break;
  14639. case LLM_ARCH_RWKV7:
  14640. {
  14641. llm = std::make_unique<llm_build_rwkv7>(*this, params);
  14642. } break;
  14643. case LLM_ARCH_ARWKV7:
  14644. {
  14645. llm = std::make_unique<llm_build_arwkv7>(*this, params);
  14646. } break;
  14647. case LLM_ARCH_GRANITE:
  14648. case LLM_ARCH_GRANITE_MOE:
  14649. case LLM_ARCH_MINICPM:
  14650. {
  14651. llm = std::make_unique<llm_build_granite>(*this, params);
  14652. } break;
  14653. case LLM_ARCH_GRANITE_HYBRID:
  14654. {
  14655. llm = std::make_unique<llm_build_granite_hybrid>(*this, params);
  14656. } break;
  14657. case LLM_ARCH_CHAMELEON:
  14658. {
  14659. llm = std::make_unique<llm_build_chameleon>(*this, params);
  14660. } break;
  14661. case LLM_ARCH_WAVTOKENIZER_DEC:
  14662. {
  14663. llm = std::make_unique<llm_build_wavtokenizer_dec>(*this, params);
  14664. } break;
  14665. case LLM_ARCH_PLM:
  14666. {
  14667. llm = std::make_unique<llm_build_plm>(*this, params);
  14668. } break;
  14669. case LLM_ARCH_BAILINGMOE:
  14670. {
  14671. llm = std::make_unique<llm_build_bailingmoe>(*this, params);
  14672. } break;
  14673. case LLM_ARCH_DOTS1:
  14674. {
  14675. llm = std::make_unique<llm_build_dots1>(*this, params);
  14676. } break;
  14677. case LLM_ARCH_ARCEE:
  14678. {
  14679. llm = std::make_unique<llm_build_arcee>(*this, params);
  14680. } break;
  14681. case LLM_ARCH_ERNIE4_5:
  14682. {
  14683. llm = std::make_unique<llm_build_ernie4_5>(*this, params);
  14684. } break;
  14685. case LLM_ARCH_ERNIE4_5_MOE:
  14686. {
  14687. llm = std::make_unique<llm_build_ernie4_5_moe>(*this, params);
  14688. } break;
  14689. case LLM_ARCH_HUNYUAN_MOE:
  14690. {
  14691. llm = std::make_unique<llm_build_hunyuan_moe>(*this, params);
  14692. } break;
  14693. case LLM_ARCH_HUNYUAN_DENSE:
  14694. {
  14695. llm = std::make_unique<llm_build_hunyuan_dense>(*this, params);
  14696. } break;
  14697. case LLM_ARCH_SMOLLM3:
  14698. {
  14699. llm = std::make_unique<llm_build_smollm3>(*this, params);
  14700. } break;
  14701. case LLM_ARCH_OPENAI_MOE:
  14702. {
  14703. llm = std::make_unique<llm_build_openai_moe_iswa>(*this, params);
  14704. } break;
  14705. case LLM_ARCH_FALCON_H1:
  14706. {
  14707. llm = std::make_unique<llm_build_falcon_h1>(*this, params);
  14708. } break;
  14709. case LLM_ARCH_LFM2:
  14710. {
  14711. llm = std::make_unique<llm_build_lfm2>(*this, params);
  14712. } break;
  14713. case LLM_ARCH_SMALLTHINKER:
  14714. {
  14715. if (hparams.swa_type == LLAMA_SWA_TYPE_STANDARD) {
  14716. llm = std::make_unique<llm_build_smallthinker<true>> (*this, params);
  14717. } else {
  14718. llm = std::make_unique<llm_build_smallthinker<false>>(*this, params);
  14719. }
  14720. } break;
  14721. default:
  14722. GGML_ABORT("fatal error");
  14723. }
  14724. // add on pooling layer
  14725. llm->build_pooling(cls, cls_b, cls_out, cls_out_b);
  14726. return llm->res->get_gf();
  14727. }
  14728. //
  14729. // interface implementation
  14730. //
  14731. llama_model_params llama_model_default_params() {
  14732. llama_model_params result = {
  14733. /*.devices =*/ nullptr,
  14734. /*.tensor_buft_overrides =*/ nullptr,
  14735. /*.n_gpu_layers =*/ 0,
  14736. /*.split_mode =*/ LLAMA_SPLIT_MODE_LAYER,
  14737. /*.main_gpu =*/ 0,
  14738. /*.tensor_split =*/ nullptr,
  14739. /*.progress_callback =*/ nullptr,
  14740. /*.progress_callback_user_data =*/ nullptr,
  14741. /*.kv_overrides =*/ nullptr,
  14742. /*.vocab_only =*/ false,
  14743. /*.use_mmap =*/ true,
  14744. /*.use_mlock =*/ false,
  14745. /*.check_tensors =*/ false,
  14746. /*.use_extra_bufts =*/ true,
  14747. };
  14748. #ifdef GGML_USE_METAL
  14749. // note: we usually have plenty of VRAM, so by default offload all layers to the GPU
  14750. result.n_gpu_layers = 999;
  14751. #endif
  14752. return result;
  14753. }
  14754. const llama_vocab * llama_model_get_vocab(const llama_model * model) {
  14755. return &model->vocab;
  14756. }
  14757. void llama_free_model(llama_model * model) {
  14758. llama_model_free(model);
  14759. }
  14760. void llama_model_free(llama_model * model) {
  14761. delete model;
  14762. }
  14763. int32_t llama_model_n_ctx_train(const llama_model * model) {
  14764. return model->hparams.n_ctx_train;
  14765. }
  14766. int32_t llama_model_n_embd(const llama_model * model) {
  14767. return model->hparams.n_embd;
  14768. }
  14769. int32_t llama_model_n_layer(const llama_model * model) {
  14770. return model->hparams.n_layer;
  14771. }
  14772. int32_t llama_model_n_head(const llama_model * model) {
  14773. return model->hparams.n_head();
  14774. }
  14775. int32_t llama_model_n_head_kv(const llama_model * model) {
  14776. return model->hparams.n_head_kv();
  14777. }
  14778. int32_t llama_model_n_swa(const llama_model * model) {
  14779. return model->hparams.n_swa;
  14780. }
  14781. uint32_t llama_model_n_cls_out(const struct llama_model * model) {
  14782. return model->hparams.n_cls_out;
  14783. }
  14784. const char * llama_model_cls_label(const struct llama_model * model, uint32_t i) {
  14785. if (i < model->classifier_labels.size()) {
  14786. return model->classifier_labels[i].c_str();
  14787. }
  14788. return nullptr;
  14789. }
  14790. // deprecated
  14791. int32_t llama_n_ctx_train(const llama_model * model) {
  14792. return llama_model_n_ctx_train(model);
  14793. }
  14794. // deprecated
  14795. int32_t llama_n_embd(const llama_model * model) {
  14796. return llama_model_n_embd(model);
  14797. }
  14798. // deprecated
  14799. int32_t llama_n_layer(const llama_model * model) {
  14800. return llama_model_n_layer(model);
  14801. }
  14802. // deprecated
  14803. int32_t llama_n_head(const llama_model * model) {
  14804. return llama_model_n_head(model);
  14805. }
  14806. llama_rope_type llama_model_rope_type(const llama_model * model) {
  14807. switch (model->arch) {
  14808. // these models do not use RoPE
  14809. case LLM_ARCH_GPT2:
  14810. case LLM_ARCH_GPTJ:
  14811. case LLM_ARCH_MPT:
  14812. case LLM_ARCH_REFACT:
  14813. case LLM_ARCH_BLOOM:
  14814. case LLM_ARCH_MAMBA:
  14815. case LLM_ARCH_MAMBA2:
  14816. case LLM_ARCH_JAMBA:
  14817. case LLM_ARCH_JINA_BERT_V2:
  14818. case LLM_ARCH_T5:
  14819. case LLM_ARCH_T5ENCODER:
  14820. case LLM_ARCH_JAIS:
  14821. case LLM_ARCH_RWKV6:
  14822. case LLM_ARCH_RWKV6QWEN2:
  14823. case LLM_ARCH_RWKV7:
  14824. case LLM_ARCH_ARWKV7:
  14825. case LLM_ARCH_WAVTOKENIZER_DEC:
  14826. return LLAMA_ROPE_TYPE_NONE;
  14827. // use what we call a normal RoPE, operating on pairs of consecutive head values
  14828. case LLM_ARCH_LLAMA:
  14829. case LLM_ARCH_LLADA:
  14830. case LLM_ARCH_LLAMA4:
  14831. case LLM_ARCH_DECI:
  14832. case LLM_ARCH_BAICHUAN:
  14833. case LLM_ARCH_STARCODER:
  14834. case LLM_ARCH_INTERNLM2:
  14835. case LLM_ARCH_MINICPM:
  14836. case LLM_ARCH_XVERSE:
  14837. case LLM_ARCH_COMMAND_R:
  14838. case LLM_ARCH_COHERE2:
  14839. case LLM_ARCH_OLMO:
  14840. case LLM_ARCH_ARCTIC:
  14841. case LLM_ARCH_DEEPSEEK:
  14842. case LLM_ARCH_DEEPSEEK2:
  14843. case LLM_ARCH_PLM:
  14844. case LLM_ARCH_CHATGLM:
  14845. case LLM_ARCH_GLM4:
  14846. case LLM_ARCH_GRANITE:
  14847. case LLM_ARCH_GRANITE_MOE:
  14848. case LLM_ARCH_GRANITE_HYBRID:
  14849. case LLM_ARCH_CHAMELEON:
  14850. case LLM_ARCH_BAILINGMOE:
  14851. case LLM_ARCH_NEO_BERT:
  14852. case LLM_ARCH_SMOLLM3:
  14853. case LLM_ARCH_ARCEE:
  14854. case LLM_ARCH_ERNIE4_5:
  14855. case LLM_ARCH_ERNIE4_5_MOE:
  14856. return LLAMA_ROPE_TYPE_NORM;
  14857. // the pairs of head values are offset by n_rot/2
  14858. case LLM_ARCH_FALCON:
  14859. case LLM_ARCH_FALCON_H1:
  14860. case LLM_ARCH_GROK:
  14861. case LLM_ARCH_DBRX:
  14862. case LLM_ARCH_BERT:
  14863. case LLM_ARCH_NOMIC_BERT:
  14864. case LLM_ARCH_NOMIC_BERT_MOE:
  14865. case LLM_ARCH_STABLELM:
  14866. case LLM_ARCH_BITNET:
  14867. case LLM_ARCH_QWEN:
  14868. case LLM_ARCH_QWEN2:
  14869. case LLM_ARCH_DREAM:
  14870. case LLM_ARCH_QWEN2MOE:
  14871. case LLM_ARCH_QWEN3:
  14872. case LLM_ARCH_QWEN3MOE:
  14873. case LLM_ARCH_OLMO2:
  14874. case LLM_ARCH_OLMOE:
  14875. case LLM_ARCH_PHI2:
  14876. case LLM_ARCH_PHI3:
  14877. case LLM_ARCH_PHIMOE:
  14878. case LLM_ARCH_PLAMO:
  14879. case LLM_ARCH_PLAMO2:
  14880. case LLM_ARCH_GEMMA:
  14881. case LLM_ARCH_GEMMA2:
  14882. case LLM_ARCH_GEMMA3:
  14883. case LLM_ARCH_GEMMA3N:
  14884. case LLM_ARCH_STARCODER2:
  14885. case LLM_ARCH_OPENELM:
  14886. case LLM_ARCH_GPTNEOX:
  14887. case LLM_ARCH_CODESHELL:
  14888. case LLM_ARCH_ORION:
  14889. case LLM_ARCH_NEMOTRON:
  14890. case LLM_ARCH_EXAONE:
  14891. case LLM_ARCH_EXAONE4:
  14892. case LLM_ARCH_MINICPM3:
  14893. case LLM_ARCH_DOTS1:
  14894. case LLM_ARCH_HUNYUAN_MOE:
  14895. case LLM_ARCH_OPENAI_MOE:
  14896. case LLM_ARCH_HUNYUAN_DENSE:
  14897. case LLM_ARCH_LFM2:
  14898. case LLM_ARCH_SMALLTHINKER:
  14899. case LLM_ARCH_GLM4_MOE:
  14900. return LLAMA_ROPE_TYPE_NEOX;
  14901. case LLM_ARCH_QWEN2VL:
  14902. return LLAMA_ROPE_TYPE_MROPE;
  14903. // all model arches should be listed explicitly here
  14904. case LLM_ARCH_UNKNOWN:
  14905. GGML_ABORT("unknown architecture");
  14906. }
  14907. return LLAMA_ROPE_TYPE_NONE;
  14908. }
  14909. float llama_model_rope_freq_scale_train(const llama_model * model) {
  14910. return model->hparams.rope_freq_scale_train;
  14911. }
  14912. int32_t llama_model_meta_val_str(const llama_model * model, const char * key, char * buf, size_t buf_size) {
  14913. const auto & it = model->gguf_kv.find(key);
  14914. if (it == model->gguf_kv.end()) {
  14915. if (buf_size > 0) {
  14916. buf[0] = '\0';
  14917. }
  14918. return -1;
  14919. }
  14920. return snprintf(buf, buf_size, "%s", it->second.c_str());
  14921. }
  14922. int32_t llama_model_meta_count(const llama_model * model) {
  14923. return (int)model->gguf_kv.size();
  14924. }
  14925. int32_t llama_model_meta_key_by_index(const llama_model * model, int i, char * buf, size_t buf_size) {
  14926. if (i < 0 || i >= (int)model->gguf_kv.size()) {
  14927. if (buf_size > 0) {
  14928. buf[0] = '\0';
  14929. }
  14930. return -1;
  14931. }
  14932. auto it = model->gguf_kv.begin();
  14933. std::advance(it, i);
  14934. return snprintf(buf, buf_size, "%s", it->first.c_str());
  14935. }
  14936. int32_t llama_model_meta_val_str_by_index(const llama_model * model, int32_t i, char * buf, size_t buf_size) {
  14937. if (i < 0 || i >= (int)model->gguf_kv.size()) {
  14938. if (buf_size > 0) {
  14939. buf[0] = '\0';
  14940. }
  14941. return -1;
  14942. }
  14943. auto it = model->gguf_kv.begin();
  14944. std::advance(it, i);
  14945. return snprintf(buf, buf_size, "%s", it->second.c_str());
  14946. }
  14947. int32_t llama_model_desc(const llama_model * model, char * buf, size_t buf_size) {
  14948. return snprintf(buf, buf_size, "%s", model->desc().c_str());
  14949. }
  14950. uint64_t llama_model_size(const llama_model * model) {
  14951. return model->size();
  14952. }
  14953. const char * llama_model_chat_template(const llama_model * model, const char * name) {
  14954. const auto key = name ? LLM_KV(model->arch, name)(LLM_KV_TOKENIZER_CHAT_TEMPLATE)
  14955. : LLM_KV(model->arch)(LLM_KV_TOKENIZER_CHAT_TEMPLATE);
  14956. const auto & it = model->gguf_kv.find(key);
  14957. if (it == model->gguf_kv.end()) {
  14958. // one-off fix for very popular models (so we are not flooded with issues)
  14959. // do not extend this list unless absolutely necessary
  14960. // Mistral-Small-2503 does not have built-in chat template
  14961. llama_vocab_pre_type pre_type = model->vocab.get_pre_type();
  14962. if (!name && pre_type == LLAMA_VOCAB_PRE_TYPE_TEKKEN && model->layers.size() == 40) {
  14963. return "mistral-v7-tekken";
  14964. }
  14965. return nullptr;
  14966. }
  14967. return it->second.c_str();
  14968. }
  14969. uint64_t llama_model_n_params(const llama_model * model) {
  14970. return model->n_elements();
  14971. }
  14972. bool llama_model_has_encoder(const llama_model * model) {
  14973. switch (model->arch) {
  14974. case LLM_ARCH_T5: return true;
  14975. case LLM_ARCH_T5ENCODER: return true;
  14976. default: return false;
  14977. }
  14978. }
  14979. bool llama_model_has_decoder(const llama_model * model) {
  14980. switch (model->arch) {
  14981. case LLM_ARCH_T5ENCODER: return false;
  14982. default: return true;
  14983. }
  14984. }
  14985. llama_token llama_model_decoder_start_token(const llama_model * model) {
  14986. return model->hparams.dec_start_token_id;
  14987. }
  14988. bool llama_model_is_recurrent(const llama_model * model) {
  14989. return llm_arch_is_recurrent(model->arch);
  14990. }
  14991. bool llama_model_is_diffusion(const llama_model * model) {
  14992. return llm_arch_is_diffusion(model->arch);
  14993. }
  14994. const std::vector<std::pair<std::string, ggml_tensor *>> & llama_internal_get_tensor_map(const llama_model * model) {
  14995. return model->tensors_by_name;
  14996. }