llama-model.cpp 880 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183
  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.h"
  8. #include "llama-kv-cache-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_140M: return "140M";
  35. case LLM_TYPE_160M: return "160M";
  36. case LLM_TYPE_190M: return "190M";
  37. case LLM_TYPE_220M: return "220M";
  38. case LLM_TYPE_250M: return "250M";
  39. case LLM_TYPE_256M: return "256M";
  40. case LLM_TYPE_270M: return "270M";
  41. case LLM_TYPE_335M: return "335M";
  42. case LLM_TYPE_350M: return "350M";
  43. case LLM_TYPE_360M: return "360M";
  44. case LLM_TYPE_410M: return "410M";
  45. case LLM_TYPE_450M: return "450M";
  46. case LLM_TYPE_475M: return "475M";
  47. case LLM_TYPE_558M: return "558M";
  48. case LLM_TYPE_700M: return "700M";
  49. case LLM_TYPE_770M: return "770M";
  50. case LLM_TYPE_780M: return "780M";
  51. case LLM_TYPE_950M: return "950M";
  52. case LLM_TYPE_0_3B: return "0.3B";
  53. case LLM_TYPE_0_5B: return "0.5B";
  54. case LLM_TYPE_0_6B: return "0.6B";
  55. case LLM_TYPE_1B: return "1B";
  56. case LLM_TYPE_1_2B: return "1.2B";
  57. case LLM_TYPE_1_3B: return "1.3B";
  58. case LLM_TYPE_1_4B: return "1.4B";
  59. case LLM_TYPE_1_5B: return "1.5B";
  60. case LLM_TYPE_1_6B: return "1.6B";
  61. case LLM_TYPE_1_7B: return "1.7B";
  62. case LLM_TYPE_1_8B: return "1.8B";
  63. case LLM_TYPE_2B: return "2B";
  64. case LLM_TYPE_2_8B: return "2.8B";
  65. case LLM_TYPE_2_9B: return "2.9B";
  66. case LLM_TYPE_3B: return "3B";
  67. case LLM_TYPE_4B: return "4B";
  68. case LLM_TYPE_6B: return "6B";
  69. case LLM_TYPE_6_9B: return "6.9B";
  70. case LLM_TYPE_7B: return "7B";
  71. case LLM_TYPE_8B: return "8B";
  72. case LLM_TYPE_9B: return "9B";
  73. case LLM_TYPE_11B: return "11B";
  74. case LLM_TYPE_12B: return "12B";
  75. case LLM_TYPE_13B: return "13B";
  76. case LLM_TYPE_14B: return "14B";
  77. case LLM_TYPE_15B: return "15B";
  78. case LLM_TYPE_16B: return "16B";
  79. case LLM_TYPE_20B: return "20B";
  80. case LLM_TYPE_27B: return "27B";
  81. case LLM_TYPE_30B: return "30B";
  82. case LLM_TYPE_32B: return "32B";
  83. case LLM_TYPE_34B: return "34B";
  84. case LLM_TYPE_35B: return "35B";
  85. case LLM_TYPE_36B: return "36B";
  86. case LLM_TYPE_40B: return "40B";
  87. case LLM_TYPE_65B: return "65B";
  88. case LLM_TYPE_70B: return "70B";
  89. case LLM_TYPE_120B: return "120B";
  90. case LLM_TYPE_142B: return "142B";
  91. case LLM_TYPE_236B: return "236B";
  92. case LLM_TYPE_290B: return "290B";
  93. case LLM_TYPE_314B: return "314B";
  94. case LLM_TYPE_405B: return "405B";
  95. case LLM_TYPE_671B: return "671B";
  96. case LLM_TYPE_SMALL: return "0.1B";
  97. case LLM_TYPE_MEDIUM: return "0.4B";
  98. case LLM_TYPE_LARGE: return "0.8B";
  99. case LLM_TYPE_XL: return "1.5B";
  100. case LLM_TYPE_A1_7B: return "A1.7B";
  101. case LLM_TYPE_A2_7B: return "A2.7B";
  102. case LLM_TYPE_8x7B: return "8x7B";
  103. case LLM_TYPE_8x22B: return "8x22B";
  104. case LLM_TYPE_16x12B: return "16x12B";
  105. case LLM_TYPE_16x3_8B: return "16x3.8B";
  106. case LLM_TYPE_10B_128x3_66B: return "10B+128x3.66B";
  107. case LLM_TYPE_57B_A14B: return "57B.A14B";
  108. case LLM_TYPE_17B_16E: return "17Bx16E (Scout)";
  109. case LLM_TYPE_17B_128E: return "17Bx128E (Maverick)";
  110. case LLM_TYPE_A13B: return "A13B";
  111. case LLM_TYPE_21B_A3B: return "21B.A3B";
  112. case LLM_TYPE_30B_A3B: return "30B.A3B";
  113. case LLM_TYPE_80B_A3B: return "80B.A3B";
  114. case LLM_TYPE_106B_A12B: return "106B.A12B";
  115. case LLM_TYPE_235B_A22B: return "235B.A22B";
  116. case LLM_TYPE_300B_A47B: return "300B.A47B";
  117. case LLM_TYPE_355B_A32B: return "355B.A32B";
  118. case LLM_TYPE_E2B: return "E2B";
  119. case LLM_TYPE_E4B: return "E4B";
  120. default: return "?B";
  121. }
  122. }
  123. static const char * llama_expert_gating_func_name(llama_expert_gating_func_type type) {
  124. switch (type) {
  125. case LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX: return "softmax";
  126. case LLAMA_EXPERT_GATING_FUNC_TYPE_SIGMOID: return "sigmoid";
  127. default: return "unknown";
  128. }
  129. }
  130. static const std::map<llama_rope_scaling_type, const char *> LLAMA_ROPE_SCALING_TYPES = {
  131. { LLAMA_ROPE_SCALING_TYPE_NONE, "none" },
  132. { LLAMA_ROPE_SCALING_TYPE_LINEAR, "linear" },
  133. { LLAMA_ROPE_SCALING_TYPE_YARN, "yarn" },
  134. { LLAMA_ROPE_SCALING_TYPE_LONGROPE, "longrope" },
  135. };
  136. std::string llama_rope_scaling_type_name(llama_rope_scaling_type rope_scaling_type) {
  137. return LLAMA_ROPE_SCALING_TYPES.at(rope_scaling_type);
  138. }
  139. static llama_rope_scaling_type llama_rope_scaling_type_from_string(const std::string & name) {
  140. for (const auto & kv : LLAMA_ROPE_SCALING_TYPES) {
  141. if (kv.second == name) {
  142. return (llama_rope_scaling_type) kv.first;
  143. }
  144. }
  145. return LLAMA_ROPE_SCALING_TYPE_UNSPECIFIED;
  146. }
  147. // checks if the weight tensor can be used with the specified buffer type and device
  148. 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) {
  149. GGML_ASSERT(w != nullptr);
  150. if (op == GGML_OP_NONE) {
  151. return true;
  152. }
  153. ggml_init_params params = {
  154. /*.mem_size =*/ ggml_tensor_overhead()*8,
  155. /*.mem_buffer =*/ NULL,
  156. /*.no_alloc =*/ true,
  157. };
  158. ggml_context_ptr ctx_ptr { ggml_init(params) };
  159. if (!ctx_ptr) {
  160. throw std::runtime_error(format("failed to create ggml context"));
  161. }
  162. ggml_context * ctx = ctx_ptr.get();
  163. ggml_tensor * op_tensor = nullptr;
  164. switch (op) {
  165. case GGML_OP_GET_ROWS:
  166. {
  167. ggml_tensor * b = ggml_new_tensor_1d(ctx, GGML_TYPE_I32, 512);
  168. op_tensor = ggml_get_rows(ctx, w, b);
  169. } break;
  170. case GGML_OP_MUL_MAT:
  171. {
  172. ggml_tensor * b = ggml_new_tensor_4d(ctx, GGML_TYPE_F32, w->ne[0], 512, w->ne[2], w->ne[3]);
  173. op_tensor = ggml_mul_mat(ctx, w, b);
  174. } break;
  175. case GGML_OP_MUL_MAT_ID:
  176. {
  177. int n_expert_used = hparams.n_expert_used;
  178. ggml_tensor * b = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, w->ne[0], n_expert_used, 512);
  179. ggml_tensor * ids = ggml_new_tensor_2d(ctx, GGML_TYPE_I32, n_expert_used, 512);
  180. op_tensor = ggml_mul_mat_id(ctx, w, b, ids);
  181. } break;
  182. case GGML_OP_ADD:
  183. {
  184. ggml_tensor * a = ggml_new_tensor_4d(ctx, GGML_TYPE_F32, w->ne[0], w->ne[1], w->ne[2], w->ne[3]);
  185. op_tensor = ggml_add(ctx, a, w);
  186. } break;
  187. case GGML_OP_ADD_ID:
  188. {
  189. int n_expert_used = hparams.n_expert_used;
  190. ggml_tensor * a = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, w->ne[0], n_expert_used, 512);
  191. ggml_tensor * c = ggml_new_tensor_2d(ctx, GGML_TYPE_I32, n_expert_used, 512);
  192. op_tensor = ggml_add_id(ctx, a, w, c);
  193. } break;
  194. case GGML_OP_MUL:
  195. {
  196. ggml_tensor * a = ggml_new_tensor_4d(ctx, GGML_TYPE_F32, w->ne[0], w->ne[1], w->ne[2], w->ne[3]);
  197. op_tensor = ggml_mul(ctx, a, w);
  198. } break;
  199. case GGML_OP_DIV:
  200. {
  201. ggml_tensor * a = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, w->ne[0]);
  202. op_tensor = ggml_div(ctx, a, w);
  203. } break;
  204. case GGML_OP_ROPE:
  205. {
  206. int n_embd_head = hparams.n_embd_head_v;
  207. int n_head = hparams.n_head();
  208. ggml_tensor * a = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, n_embd_head, n_head, 512);
  209. ggml_tensor * b = ggml_new_tensor_1d(ctx, GGML_TYPE_I32, 512);
  210. op_tensor = ggml_rope_ext(
  211. ctx, a, b, w,
  212. 0, 0, 0, 0, 0,
  213. 0, 0, 0, 0
  214. );
  215. } break;
  216. case GGML_OP_SSM_CONV:
  217. {
  218. const int64_t n_seq_tokens = 512;
  219. const int64_t n_seqs = 3;
  220. ggml_tensor * conv_x = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, w->ne[0] - 1 + n_seq_tokens, w->ne[1], n_seqs);
  221. op_tensor = ggml_ssm_conv(ctx, conv_x, w);
  222. } break;
  223. case GGML_OP_SSM_SCAN:
  224. {
  225. // w is ssm_a, which is used to distinguish Mamba-1 and Mamba-2
  226. const int64_t d_state = w->ne[0] == 1 ? hparams.ssm_d_state : w->ne[0];
  227. const int64_t n_head = w->ne[1];
  228. const int64_t head_dim = hparams.ssm_d_inner / n_head;
  229. const int64_t n_group = hparams.ssm_n_group ? hparams.ssm_n_group : 1;
  230. const int64_t n_seq_tokens = 512;
  231. const int64_t n_seqs = 3;
  232. ggml_tensor * s = ggml_new_tensor_4d(ctx, GGML_TYPE_F32, d_state, head_dim, n_head, n_seqs);
  233. ggml_tensor * x = ggml_new_tensor_4d(ctx, GGML_TYPE_F32, head_dim, n_head, n_seq_tokens, n_seqs);
  234. ggml_tensor * dt = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, n_head, n_seq_tokens, n_seqs);
  235. ggml_tensor * B = ggml_new_tensor_4d(ctx, GGML_TYPE_F32, d_state, n_group, n_seq_tokens, n_seqs);
  236. ggml_tensor * C = ggml_new_tensor_4d(ctx, GGML_TYPE_F32, d_state, n_group, n_seq_tokens, n_seqs);
  237. ggml_tensor * ids = ggml_new_tensor_1d(ctx, GGML_TYPE_I32, n_seqs);
  238. op_tensor = ggml_ssm_scan(ctx, s, x, dt, w, B, C, ids);
  239. } break;
  240. case GGML_OP_RWKV_WKV6:
  241. {
  242. // FIXME
  243. const int64_t S = 123;
  244. const int64_t H = 123;
  245. const int64_t n_tokens = 123;
  246. const int64_t n_seqs = 123;
  247. ggml_tensor * k = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, S, H, n_tokens);
  248. ggml_tensor * v = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, S, H, n_tokens);
  249. ggml_tensor * r = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, S, H, n_tokens);
  250. ggml_tensor * tf = w;
  251. ggml_tensor * td = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, S, H, n_tokens);
  252. ggml_tensor * state = ggml_new_tensor_4d(ctx, GGML_TYPE_F32, S, n_seqs, S, H);
  253. op_tensor = ggml_rwkv_wkv6(ctx, k, v, r, tf, td, state);
  254. } break;
  255. case GGML_OP_IM2COL:
  256. {
  257. const int n_embd = hparams.n_embd;
  258. ggml_tensor * b = ggml_new_tensor_4d(ctx, GGML_TYPE_F32, n_embd, w->ne[1], 1, 1);
  259. op_tensor = ggml_im2col(ctx, w, b, 1, 0, 0, 0, 1, 0, false, GGML_TYPE_F16);
  260. } break;
  261. case GGML_OP_SCALE:
  262. {
  263. op_tensor = ggml_scale(ctx, w, 1.0f);
  264. } break;
  265. default:
  266. GGML_ABORT("%s: missing test for op %s for tensor %s", __func__, ggml_op_name(op), w->name);
  267. }
  268. // create a temporary dummy buffer for the weight so that supports_op can check the buffer type
  269. GGML_ASSERT(w->buffer == nullptr);
  270. w->buffer = ggml_backend_buft_alloc_buffer(buft, 0);
  271. bool op_supported = ggml_backend_dev_supports_op(dev, op_tensor);
  272. ggml_backend_buffer_free(w->buffer);
  273. w->buffer = nullptr;
  274. return op_supported;
  275. }
  276. // lists of buffer types used for each layer
  277. using buft_list_t = std::vector<std::pair<ggml_backend_dev_t, ggml_backend_buffer_type_t>>;
  278. // find the first buffer type in the list that can use the tensor
  279. 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) {
  280. GGML_ASSERT(!buft_list.empty());
  281. for (const auto & cur : buft_list) {
  282. ggml_backend_dev_t cur_dev = cur.first;
  283. ggml_backend_buffer_type_t cur_buft = cur.second;
  284. if (weight_buft_supported(hparams, tensor, op, cur_buft, cur_dev)) {
  285. return cur_buft;
  286. }
  287. }
  288. return nullptr;
  289. }
  290. // CPU: ACCEL -> GPU host -> CPU extra -> CPU
  291. static buft_list_t make_cpu_buft_list(const std::vector<ggml_backend_dev_t> & devices, bool use_extra_bufts) {
  292. buft_list_t buft_list;
  293. // add ACCEL buffer types
  294. for (size_t i = 0; i < ggml_backend_dev_count(); ++i) {
  295. ggml_backend_dev_t dev = ggml_backend_dev_get(i);
  296. if (ggml_backend_dev_type(dev) == GGML_BACKEND_DEVICE_TYPE_ACCEL) {
  297. auto * buft = ggml_backend_dev_buffer_type(dev);
  298. // skip
  299. if (buft != ggml_backend_cpu_buffer_type()) {
  300. buft_list.emplace_back(dev, buft);
  301. }
  302. }
  303. }
  304. // add a host buffer type
  305. // storing the tensors in a host buffer is useful when the processing of large batches
  306. // is offloaded to a GPU device, since it reduces the time spent on data transfers
  307. // generally, this will be done using the first device in the list
  308. // a better approach would be to handle this on a weight-by-weight basis using the offload_op
  309. // function of the device to determine if it would benefit from being stored in a host buffer
  310. for (auto * dev : devices) {
  311. ggml_backend_buffer_type_t buft = ggml_backend_dev_host_buffer_type(dev);
  312. if (buft) {
  313. buft_list.emplace_back(dev, buft);
  314. break;
  315. }
  316. }
  317. // add extra buffer types
  318. if (use_extra_bufts) {
  319. auto * cpu_dev = ggml_backend_dev_by_type(GGML_BACKEND_DEVICE_TYPE_CPU);
  320. if (cpu_dev == nullptr) {
  321. throw std::runtime_error(format("%s: no CPU backend found", __func__));
  322. }
  323. auto * cpu_reg = ggml_backend_dev_backend_reg(cpu_dev);
  324. auto ggml_backend_dev_get_extra_bufts_fn = (ggml_backend_dev_get_extra_bufts_t)
  325. ggml_backend_reg_get_proc_address(cpu_reg, "ggml_backend_dev_get_extra_bufts");
  326. if (ggml_backend_dev_get_extra_bufts_fn) {
  327. ggml_backend_buffer_type_t * extra_bufts = ggml_backend_dev_get_extra_bufts_fn(cpu_dev);
  328. while (extra_bufts && *extra_bufts) {
  329. buft_list.emplace_back(cpu_dev, *extra_bufts);
  330. ++extra_bufts;
  331. }
  332. }
  333. }
  334. // add the CPU buffer type
  335. for (size_t i = 0; i < ggml_backend_dev_count(); ++i) {
  336. ggml_backend_dev_t dev = ggml_backend_dev_get(i);
  337. if (ggml_backend_dev_type(dev) == GGML_BACKEND_DEVICE_TYPE_CPU) {
  338. buft_list.emplace_back(dev, ggml_backend_dev_buffer_type(dev));
  339. }
  340. }
  341. return buft_list;
  342. }
  343. // GPU: split if LLAMA_SPLIT_MODE_ROW -> GPU
  344. static buft_list_t make_gpu_buft_list(ggml_backend_dev_t dev, llama_split_mode split_mode, const float * tensor_split) {
  345. buft_list_t buft_list;
  346. // add the device split buffer type if requested and available
  347. if (split_mode == LLAMA_SPLIT_MODE_ROW) {
  348. ggml_backend_reg_t reg = ggml_backend_dev_backend_reg(dev);
  349. auto ggml_backend_split_buffer_type_fn = (ggml_backend_split_buffer_type_t)
  350. ggml_backend_reg_get_proc_address(reg, "ggml_backend_split_buffer_type");
  351. if (ggml_backend_split_buffer_type_fn) {
  352. size_t dev_index = [&]() {
  353. auto * reg = ggml_backend_dev_backend_reg(dev);
  354. for (size_t i = 0; i < ggml_backend_reg_dev_count(reg); ++i) {
  355. if (ggml_backend_reg_dev_get(reg, i) == dev) {
  356. return i;
  357. }
  358. }
  359. throw std::runtime_error(format("device %s not found in its backend reg", ggml_backend_dev_name(dev)));
  360. }();
  361. auto * buft = ggml_backend_split_buffer_type_fn(dev_index, tensor_split);
  362. if (buft != nullptr) {
  363. buft_list.emplace_back(dev, buft);
  364. }
  365. }
  366. }
  367. // add the device default buffer type
  368. buft_list.emplace_back(dev, ggml_backend_dev_buffer_type(dev));
  369. return buft_list;
  370. }
  371. struct llama_model::impl {
  372. impl() {}
  373. ~impl() {}
  374. uint64_t n_elements = 0;
  375. size_t n_bytes = 0;
  376. std::string desc_str;
  377. // model memory mapped files
  378. llama_mmaps mappings;
  379. // objects representing data potentially being locked in memory
  380. llama_mlocks mlock_bufs;
  381. llama_mlocks mlock_mmaps;
  382. // contexts where the model tensors metadata is stored
  383. std::vector<ggml_context_ptr> ctxs;
  384. // the model memory buffers for the tensor data
  385. std::vector<ggml_backend_buffer_ptr> bufs;
  386. buft_list_t cpu_buft_list;
  387. std::map<ggml_backend_dev_t, buft_list_t> gpu_buft_list;
  388. struct layer_dev {
  389. ggml_backend_dev_t dev;
  390. buft_list_t * buft_list;
  391. };
  392. layer_dev dev_input = {};
  393. layer_dev dev_output = {};
  394. std::vector<layer_dev> dev_layer;
  395. bool has_tensor_overrides;
  396. };
  397. llama_model::llama_model(const llama_model_params & params) : params(params), pimpl(std::make_unique<impl>()) {
  398. pimpl->has_tensor_overrides = params.tensor_buft_overrides && params.tensor_buft_overrides[0].pattern;
  399. }
  400. llama_model::~llama_model() {}
  401. void llama_model::load_stats(llama_model_loader & ml) {
  402. pimpl->n_elements = ml.n_elements;
  403. pimpl->n_bytes = ml.n_bytes;
  404. }
  405. void llama_model::load_arch(llama_model_loader & ml) {
  406. arch = ml.get_arch();
  407. if (arch == LLM_ARCH_UNKNOWN) {
  408. throw std::runtime_error("unknown model architecture: '" + ml.get_arch_name() + "'");
  409. }
  410. }
  411. void llama_model::load_hparams(llama_model_loader & ml) {
  412. const gguf_context * ctx = ml.meta.get();
  413. // get metadata as string
  414. for (int i = 0; i < gguf_get_n_kv(ctx); i++) {
  415. gguf_type type = gguf_get_kv_type(ctx, i);
  416. if (type == GGUF_TYPE_ARRAY) {
  417. continue;
  418. }
  419. const char * name = gguf_get_key(ctx, i);
  420. const std::string value = gguf_kv_to_str(ctx, i);
  421. gguf_kv.emplace(name, value);
  422. }
  423. // get general kv
  424. ml.get_key(LLM_KV_GENERAL_NAME, name, false);
  425. // everything past this point is not vocab-related
  426. if (hparams.vocab_only) {
  427. return;
  428. }
  429. ml.get_key(LLM_KV_CONTEXT_LENGTH, hparams.n_ctx_train);
  430. ml.get_key(LLM_KV_EMBEDDING_LENGTH, hparams.n_embd);
  431. ml.get_key(LLM_KV_BLOCK_COUNT, hparams.n_layer);
  432. ml.get_key(LLM_KV_EXPERT_COUNT, hparams.n_expert, false);
  433. ml.get_key(LLM_KV_EXPERT_USED_COUNT, hparams.n_expert_used, false);
  434. if (arch == LLM_ARCH_WAVTOKENIZER_DEC) {
  435. ml.get_key(LLM_KV_FEATURES_LENGTH, hparams.n_embd_features);
  436. ml.get_key(LLM_KV_POSNET_EMBEDDING_LENGTH, hparams.posnet.n_embd);
  437. ml.get_key(LLM_KV_POSNET_BLOCK_COUNT, hparams.posnet.n_layer);
  438. ml.get_key(LLM_KV_CONVNEXT_EMBEDDING_LENGTH, hparams.convnext.n_embd);
  439. ml.get_key(LLM_KV_CONVNEXT_BLOCK_COUNT, hparams.convnext.n_layer);
  440. }
  441. GGML_ASSERT(hparams.n_expert <= LLAMA_MAX_EXPERTS);
  442. GGML_ASSERT(hparams.n_expert_used <= hparams.n_expert);
  443. if (hparams.n_expert > 0) {
  444. GGML_ASSERT(hparams.n_expert_used > 0);
  445. } else {
  446. GGML_ASSERT(hparams.n_expert_used == 0);
  447. }
  448. std::fill(hparams.n_head_arr.begin(), hparams.n_head_arr.end(), 0);
  449. std::fill(hparams.n_head_kv_arr.begin(), hparams.n_head_kv_arr.end(), 0);
  450. std::fill(hparams.n_ff_arr.begin(), hparams.n_ff_arr.end(), 0);
  451. std::fill(
  452. hparams.recurrent_layer_arr.begin(),
  453. hparams.recurrent_layer_arr.end(),
  454. llm_arch_is_recurrent(ml.get_arch()));
  455. std::fill(hparams.rope_sections.begin(), hparams.rope_sections.end(), 0);
  456. std::fill(hparams.swa_layers.begin(), hparams.swa_layers.end(), 0);
  457. ml.get_key_or_arr(LLM_KV_FEED_FORWARD_LENGTH, hparams.n_ff_arr, hparams.n_layer, false);
  458. ml.get_key_or_arr(LLM_KV_ATTENTION_HEAD_COUNT, hparams.n_head_arr, hparams.n_layer, false);
  459. // n_head_kv is optional, default to n_head
  460. hparams.n_head_kv_arr = hparams.n_head_arr;
  461. ml.get_key_or_arr(LLM_KV_ATTENTION_HEAD_COUNT_KV, hparams.n_head_kv_arr, hparams.n_layer, false);
  462. bool rope_finetuned = false;
  463. ml.get_key(LLM_KV_ROPE_SCALING_FINETUNED, rope_finetuned, false);
  464. hparams.rope_finetuned = rope_finetuned;
  465. hparams.n_ctx_orig_yarn = hparams.n_ctx_train;
  466. ml.get_key(LLM_KV_ROPE_SCALING_ORIG_CTX_LEN, hparams.n_ctx_orig_yarn, false);
  467. // rope_freq_base (optional)
  468. hparams.rope_freq_base_train = 10000.0f;
  469. ml.get_key(LLM_KV_ROPE_FREQ_BASE, hparams.rope_freq_base_train, false);
  470. std::string rope_scaling("linear");
  471. ml.get_key(LLM_KV_ROPE_SCALING_TYPE, rope_scaling, false);
  472. hparams.rope_scaling_type_train = llama_rope_scaling_type_from_string(rope_scaling);
  473. GGML_ASSERT(hparams.rope_scaling_type_train != LLAMA_ROPE_SCALING_TYPE_UNSPECIFIED);
  474. // rope_freq_scale (inverse of the kv) is optional
  475. float ropescale = 0.0f;
  476. if (!ml.get_key(LLM_KV_ROPE_SCALING_FACTOR, ropescale, false)) {
  477. // try the old key name
  478. ml.get_key(LLM_KV_ROPE_SCALE_LINEAR, ropescale, false);
  479. }
  480. hparams.rope_freq_scale_train = ropescale == 0.0f ? 1.0f : 1.0f/ropescale;
  481. // by default assume that the sliding-window layers use the same scaling type as the non-sliding-window layers
  482. hparams.rope_freq_base_train_swa = hparams.rope_freq_base_train;
  483. hparams.rope_freq_scale_train_swa = hparams.rope_freq_scale_train;
  484. ml.get_key(LLM_KV_ROPE_SCALING_ATTN_FACTOR, hparams.rope_attn_factor, false);
  485. // non-transformer models do not have attention heads
  486. if (hparams.n_head() > 0) {
  487. // gpt-neox n_rot = rotary_pct * (n_embd / n_head)
  488. // gpt-j n_rot = rotary_dim
  489. hparams.n_embd_head_k = hparams.n_embd / hparams.n_head();
  490. ml.get_key(LLM_KV_ATTENTION_KEY_LENGTH, hparams.n_embd_head_k, false);
  491. hparams.n_embd_head_v = hparams.n_embd / hparams.n_head();
  492. ml.get_key(LLM_KV_ATTENTION_VALUE_LENGTH, hparams.n_embd_head_v, false);
  493. // sanity check for n_rot (optional)
  494. hparams.n_rot = hparams.n_embd_head_k;
  495. ml.get_key(LLM_KV_ROPE_DIMENSION_COUNT, hparams.n_rot, false);
  496. if (arch == LLM_ARCH_LLAMA || arch == LLM_ARCH_DECI || arch == LLM_ARCH_FALCON) {
  497. if (hparams.n_rot != hparams.n_embd_head_k) {
  498. throw std::runtime_error(format("invalid n_rot: %u, expected %u", hparams.n_rot, hparams.n_embd_head_k));
  499. }
  500. }
  501. } else {
  502. hparams.n_rot = 0;
  503. hparams.n_embd_head_k = 0;
  504. hparams.n_embd_head_v = 0;
  505. }
  506. // for differentiating model types
  507. uint32_t n_vocab = 0;
  508. ml.get_key(LLM_KV_VOCAB_SIZE, n_vocab, false) || ml.get_arr_n(LLM_KV_TOKENIZER_LIST, n_vocab, false);
  509. // for classifier models
  510. ml.get_arr(LLM_KV_CLASSIFIER_OUTPUT_LABELS, classifier_labels, false);
  511. if (!classifier_labels.empty()) {
  512. hparams.n_cls_out = classifier_labels.size();
  513. }
  514. // arch-specific KVs
  515. switch (arch) {
  516. case LLM_ARCH_LLAMA:
  517. {
  518. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  519. if (hparams.n_expert == 8) {
  520. switch (hparams.n_layer) {
  521. case 32: type = LLM_TYPE_8x7B; break;
  522. case 56: type = LLM_TYPE_8x22B; break;
  523. default: type = LLM_TYPE_UNKNOWN;
  524. }
  525. } else {
  526. switch (hparams.n_layer) {
  527. case 16: type = LLM_TYPE_1B; break; // Llama 3.2 1B
  528. case 22: type = LLM_TYPE_1B; break;
  529. case 26: type = LLM_TYPE_3B; break;
  530. case 28: type = LLM_TYPE_3B; break; // Llama 3.2 3B
  531. case 30: type = LLM_TYPE_256M; break; // smoldocling 256M
  532. // granite uses a vocab with len 49152
  533. case 32: type = n_vocab == 49152 ? LLM_TYPE_3B : (n_vocab < 40000 ? LLM_TYPE_7B : LLM_TYPE_8B); break;
  534. case 36: type = LLM_TYPE_8B; break; // granite
  535. case 40: type = LLM_TYPE_13B; break;
  536. case 48: type = LLM_TYPE_34B; break;
  537. case 60: type = LLM_TYPE_30B; break;
  538. case 80: type = hparams.n_head() == hparams.n_head_kv() ? LLM_TYPE_65B : LLM_TYPE_70B; break;
  539. default: type = LLM_TYPE_UNKNOWN;
  540. }
  541. }
  542. } break;
  543. case LLM_ARCH_LLAMA4:
  544. {
  545. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  546. ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp);
  547. ml.get_key(LLM_KV_INTERLEAVE_MOE_LAYER_STEP, hparams.n_moe_layer_step);
  548. const bool found_swa = ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa, false);
  549. if (found_swa && hparams.n_swa == 0) {
  550. hparams.swa_type = LLAMA_SWA_TYPE_NONE;
  551. hparams.n_no_rope_layer_step = hparams.n_layer; // always use rope
  552. } else {
  553. hparams.swa_type = LLAMA_SWA_TYPE_CHUNKED;
  554. hparams.n_swa = 8192;
  555. hparams.set_swa_pattern(4); // pattern: 3 chunked - 1 full
  556. }
  557. switch (hparams.n_expert) {
  558. case 0: {
  559. // MobileLLM (no MoE)
  560. switch (hparams.n_embd) {
  561. case 2048: type = LLM_TYPE_140M; break;
  562. case 4096: type = LLM_TYPE_360M; break;
  563. case 6144: type = LLM_TYPE_950M; break;
  564. default: type = LLM_TYPE_UNKNOWN;
  565. }
  566. } break;
  567. case 16: type = LLM_TYPE_17B_16E; break;
  568. case 128: type = LLM_TYPE_17B_128E; break;
  569. default: type = LLM_TYPE_UNKNOWN;
  570. }
  571. hparams.use_kq_norm = type != LLM_TYPE_17B_128E;
  572. } break;
  573. case LLM_ARCH_ARCEE:
  574. {
  575. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  576. // Arcee uses the same structure as Llama
  577. switch (hparams.n_layer) {
  578. case 36: type = LLM_TYPE_4B; break;
  579. default: type = LLM_TYPE_UNKNOWN;
  580. }
  581. } break;
  582. case LLM_ARCH_DECI:
  583. {
  584. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  585. switch (hparams.n_layer) {
  586. case 32: type = LLM_TYPE_7B; break;
  587. case 80: type = LLM_TYPE_70B; break;
  588. case 162: type = LLM_TYPE_405B; break;
  589. default: type = LLM_TYPE_UNKNOWN;
  590. }
  591. } break;
  592. case LLM_ARCH_MINICPM:
  593. {
  594. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  595. ml.get_key(LLM_KV_EMBEDDING_SCALE, hparams.f_embedding_scale);
  596. ml.get_key(LLM_KV_RESIDUAL_SCALE, hparams.f_residual_scale);
  597. ml.get_key(LLM_KV_LOGIT_SCALE, hparams.f_logit_scale);
  598. // MiniCPM uses rope by default, unlike Granite which uses it as a switch
  599. hparams.rope_finetuned = true;
  600. switch (hparams.n_layer) {
  601. case 52: type = LLM_TYPE_1B; break;
  602. case 40: type = LLM_TYPE_2B; break;
  603. default: type = LLM_TYPE_UNKNOWN;
  604. }
  605. } break;
  606. case LLM_ARCH_MINICPM3:
  607. {
  608. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  609. ml.get_key(LLM_KV_ATTENTION_Q_LORA_RANK, hparams.n_lora_q);
  610. ml.get_key(LLM_KV_ATTENTION_KV_LORA_RANK, hparams.n_lora_kv);
  611. switch (hparams.n_layer) {
  612. case 62: type = LLM_TYPE_4B; break;
  613. default: type = LLM_TYPE_UNKNOWN;
  614. }
  615. } break;
  616. case LLM_ARCH_GROK:
  617. {
  618. // defaults for old GGUFs
  619. hparams.yarn_beta_fast = 8.0f;
  620. hparams.f_logit_scale = 0.5773502691896257f;
  621. hparams.f_embedding_scale = 78.38367176906169f;
  622. hparams.f_attn_out_scale = 0.08838834764831845f;
  623. hparams.f_attn_logit_softcapping = 30.0f;
  624. hparams.f_router_logit_softcapping = 30.0f;
  625. // no final_logit_softcapping in grok-1
  626. hparams.f_final_logit_softcapping = 0.0f;
  627. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  628. ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp, false);
  629. ml.get_key(LLM_KV_LOGIT_SCALE, hparams.f_logit_scale, false);
  630. ml.get_key(LLM_KV_EMBEDDING_SCALE, hparams.f_embedding_scale, false);
  631. ml.get_key(LLM_KV_ATTENTION_OUTPUT_SCALE, hparams.f_attn_out_scale, false);
  632. ml.get_key(LLM_KV_ATTN_LOGIT_SOFTCAPPING, hparams.f_attn_logit_softcapping, false);
  633. ml.get_key(LLM_KV_ROUTER_LOGIT_SOFTCAPPING, hparams.f_router_logit_softcapping, false);
  634. ml.get_key(LLM_KV_FINAL_LOGIT_SOFTCAPPING, hparams.f_final_logit_softcapping, false);
  635. ml.get_key(LLM_KV_ATTENTION_TEMPERATURE_LENGTH, hparams.attn_temp_length, false);
  636. ml.get_key(LLM_KV_ROPE_SCALING_YARN_EXT_FACTOR, hparams.yarn_ext_factor, false);
  637. ml.get_key(LLM_KV_ROPE_SCALING_YARN_ATTN_FACTOR, hparams.yarn_attn_factor, false);
  638. ml.get_key(LLM_KV_ROPE_SCALING_YARN_BETA_FAST, hparams.yarn_beta_fast, false);
  639. ml.get_key(LLM_KV_ROPE_SCALING_YARN_BETA_SLOW, hparams.yarn_beta_slow, false);
  640. switch (hparams.n_layer) {
  641. case 64: type = LLM_TYPE_314B; break;
  642. default: type = LLM_TYPE_UNKNOWN;
  643. }
  644. } break;
  645. case LLM_ARCH_FALCON:
  646. {
  647. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  648. switch (hparams.n_layer) {
  649. case 32: type = LLM_TYPE_7B; break;
  650. case 60: type = LLM_TYPE_40B; break;
  651. default: type = LLM_TYPE_UNKNOWN;
  652. }
  653. } break;
  654. case LLM_ARCH_BAICHUAN:
  655. {
  656. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  657. switch (hparams.n_layer) {
  658. case 32: type = LLM_TYPE_7B; break;
  659. case 40: type = LLM_TYPE_13B; break;
  660. default: type = LLM_TYPE_UNKNOWN;
  661. }
  662. if (type == LLM_TYPE_13B) {
  663. // TODO: become GGUF KV parameter
  664. hparams.f_max_alibi_bias = 8.0f;
  665. }
  666. } break;
  667. case LLM_ARCH_STARCODER:
  668. {
  669. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  670. switch (hparams.n_layer) {
  671. case 24: type = LLM_TYPE_1B; break;
  672. case 36: type = LLM_TYPE_3B; break;
  673. case 42: type = LLM_TYPE_7B; break;
  674. case 40: type = LLM_TYPE_15B; break;
  675. default: type = LLM_TYPE_UNKNOWN;
  676. }
  677. } break;
  678. case LLM_ARCH_REFACT:
  679. {
  680. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  681. switch (hparams.n_layer) {
  682. case 32: type = LLM_TYPE_1B; break;
  683. default: type = LLM_TYPE_UNKNOWN;
  684. }
  685. // TODO: become GGUF KV parameter
  686. hparams.f_max_alibi_bias = 8.0f;
  687. } break;
  688. case LLM_ARCH_BERT:
  689. {
  690. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  691. ml.get_key(LLM_KV_ATTENTION_CAUSAL, hparams.causal_attn);
  692. ml.get_key(LLM_KV_POOLING_TYPE, hparams.pooling_type, false);
  693. switch (hparams.n_layer) {
  694. case 3:
  695. type = LLM_TYPE_17M; break; // bge-micro
  696. case 6:
  697. type = LLM_TYPE_22M; break; // MiniLM-L6
  698. case 12:
  699. switch (hparams.n_embd) {
  700. case 384: type = LLM_TYPE_33M; break; // MiniLM-L12, bge-small
  701. case 768: type = LLM_TYPE_109M; break; // bge-base
  702. default: type = LLM_TYPE_UNKNOWN;
  703. } break;
  704. case 24:
  705. type = LLM_TYPE_335M; break; // bge-large
  706. default: type = LLM_TYPE_UNKNOWN;
  707. }
  708. } break;
  709. case LLM_ARCH_JINA_BERT_V2:
  710. {
  711. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  712. ml.get_key(LLM_KV_ATTENTION_CAUSAL, hparams.causal_attn);
  713. ml.get_key(LLM_KV_POOLING_TYPE, hparams.pooling_type, false);
  714. hparams.f_max_alibi_bias = 8.0f;
  715. switch (hparams.n_layer) {
  716. case 4: type = LLM_TYPE_33M; break; // jina-embeddings-small
  717. case 12: type = LLM_TYPE_137M; break; // jina-embeddings-base
  718. default: type = LLM_TYPE_UNKNOWN;
  719. }
  720. } break;
  721. case LLM_ARCH_JINA_BERT_V3:
  722. {
  723. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  724. ml.get_key(LLM_KV_ATTENTION_CAUSAL, hparams.causal_attn);
  725. ml.get_key(LLM_KV_POOLING_TYPE, hparams.pooling_type, false);
  726. switch (hparams.n_layer) {
  727. case 24:
  728. type = LLM_TYPE_558M; break;
  729. default: type = LLM_TYPE_UNKNOWN;
  730. }
  731. } break;
  732. case LLM_ARCH_NOMIC_BERT:
  733. case LLM_ARCH_NOMIC_BERT_MOE:
  734. {
  735. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  736. ml.get_key(LLM_KV_ATTENTION_CAUSAL, hparams.causal_attn);
  737. ml.get_key(LLM_KV_POOLING_TYPE, hparams.pooling_type);
  738. ml.get_key(LLM_KV_MOE_EVERY_N_LAYERS, hparams.moe_every_n_layers, 0);
  739. if (hparams.n_layer == 12 && hparams.n_embd == 768) {
  740. if (arch == LLM_ARCH_NOMIC_BERT) {
  741. type = LLM_TYPE_137M;
  742. } else if (arch == LLM_ARCH_NOMIC_BERT_MOE && hparams.moe_every_n_layers == 2) {
  743. type = LLM_TYPE_475M;
  744. }
  745. }
  746. } break;
  747. case LLM_ARCH_NEO_BERT:
  748. {
  749. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  750. ml.get_key(LLM_KV_ATTENTION_CAUSAL, hparams.causal_attn);
  751. ml.get_key(LLM_KV_POOLING_TYPE, hparams.pooling_type);
  752. if (hparams.n_layer == 28) {
  753. type = LLM_TYPE_250M;
  754. }
  755. } break;
  756. case LLM_ARCH_BLOOM:
  757. {
  758. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  759. switch (hparams.n_layer) {
  760. case 24: type = LLM_TYPE_1B; break;
  761. case 30:
  762. switch (hparams.n_embd) {
  763. case 2560: type = LLM_TYPE_3B; break;
  764. case 4096: type = LLM_TYPE_7B; break;
  765. default: type = LLM_TYPE_UNKNOWN;
  766. } break;
  767. default: type = LLM_TYPE_UNKNOWN;
  768. }
  769. // TODO: become GGUF KV parameter
  770. hparams.f_max_alibi_bias = 8.0f;
  771. } break;
  772. case LLM_ARCH_MPT:
  773. {
  774. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  775. ml.get_key(LLM_KV_ATTENTION_CLAMP_KQV, hparams.f_clamp_kqv, false);
  776. ml.get_key(LLM_KV_ATTENTION_MAX_ALIBI_BIAS, hparams.f_max_alibi_bias);
  777. switch (hparams.n_layer) {
  778. case 32: type = LLM_TYPE_7B; break;
  779. case 48: type = LLM_TYPE_30B; break;
  780. default: type = LLM_TYPE_UNKNOWN;
  781. }
  782. } break;
  783. case LLM_ARCH_STABLELM:
  784. {
  785. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  786. switch (hparams.n_layer) {
  787. case 24: type = LLM_TYPE_1B; break;
  788. case 32: type = LLM_TYPE_3B; break;
  789. case 40: type = LLM_TYPE_12B; break;
  790. default: type = LLM_TYPE_UNKNOWN;
  791. }
  792. } break;
  793. case LLM_ARCH_QWEN:
  794. {
  795. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  796. switch (hparams.n_layer) {
  797. case 32: type = LLM_TYPE_7B; break;
  798. case 40: type = LLM_TYPE_13B; break;
  799. default: type = LLM_TYPE_UNKNOWN;
  800. }
  801. } break;
  802. case LLM_ARCH_QWEN2VL:
  803. {
  804. ml.get_key_or_arr(LLM_KV_ROPE_DIMENSION_SECTIONS, hparams.rope_sections, 4, true);
  805. }
  806. // fall through
  807. case LLM_ARCH_QWEN2:
  808. {
  809. ml.get_key(LLM_KV_POOLING_TYPE, hparams.pooling_type, false);
  810. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  811. switch (hparams.n_layer) {
  812. case 24: type = hparams.n_embd == 1024 ? LLM_TYPE_0_5B : LLM_TYPE_1B; break;
  813. case 28: type = hparams.n_embd == 1536 ? LLM_TYPE_1_5B : LLM_TYPE_7B; break;
  814. case 32: type = LLM_TYPE_7B; break;
  815. case 36: type = LLM_TYPE_3B; break;
  816. case 40: type = hparams.n_head() == 20 ? LLM_TYPE_4B : LLM_TYPE_13B; break;
  817. case 48: type = LLM_TYPE_14B; break;
  818. case 64: type = LLM_TYPE_32B; break;
  819. case 80: type = LLM_TYPE_70B; break;
  820. default: type = LLM_TYPE_UNKNOWN;
  821. }
  822. } break;
  823. case LLM_ARCH_DREAM:
  824. {
  825. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  826. // Dream models are primarily 7B with 28 layers
  827. switch (hparams.n_layer) {
  828. case 28:
  829. type = LLM_TYPE_7B;
  830. break;
  831. default:
  832. type = LLM_TYPE_UNKNOWN;
  833. }
  834. // Set non-causal attention for diffusion models
  835. hparams.causal_attn = false;
  836. }
  837. break;
  838. case LLM_ARCH_LLADA:
  839. {
  840. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  841. // LLaDA-8B has 32 layers, similar to LLaMA but for diffusion
  842. switch (hparams.n_layer) {
  843. case 32:
  844. type = LLM_TYPE_8B;
  845. break;
  846. default:
  847. type = LLM_TYPE_UNKNOWN;
  848. }
  849. // Set non-causal attention for diffusion models
  850. hparams.causal_attn = false;
  851. }
  852. break;
  853. case LLM_ARCH_LLADA_MOE:
  854. {
  855. ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp, false);
  856. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  857. // diffusion language model uses non-causal attention
  858. hparams.causal_attn = false;
  859. switch (hparams.n_layer) {
  860. case 16: type = LLM_TYPE_A1_7B; break;
  861. default: type = LLM_TYPE_UNKNOWN;
  862. }
  863. } break;
  864. case LLM_ARCH_QWEN2MOE:
  865. {
  866. ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp, false);
  867. ml.get_key(LLM_KV_EXPERT_SHARED_FEED_FORWARD_LENGTH, hparams.n_ff_shexp, false);
  868. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  869. switch (hparams.n_layer) {
  870. case 24: type = LLM_TYPE_A2_7B; break;
  871. case 28: type = LLM_TYPE_57B_A14B; break;
  872. default: type = LLM_TYPE_UNKNOWN;
  873. }
  874. } break;
  875. case LLM_ARCH_QWEN3:
  876. {
  877. ml.get_key(LLM_KV_POOLING_TYPE, hparams.pooling_type, false);
  878. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  879. switch (hparams.n_layer) {
  880. case 28: type = hparams.n_embd == 1024 ? LLM_TYPE_0_6B : LLM_TYPE_1_7B; break;
  881. case 36: type = hparams.n_embd == 2560 ? LLM_TYPE_4B : LLM_TYPE_8B; break;
  882. case 40: type = LLM_TYPE_14B; break;
  883. case 64: type = LLM_TYPE_32B; break;
  884. default: type = LLM_TYPE_UNKNOWN;
  885. }
  886. } break;
  887. case LLM_ARCH_QWEN3MOE:
  888. {
  889. ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp, false);
  890. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  891. switch (hparams.n_layer) {
  892. case 48: type = LLM_TYPE_30B_A3B; break;
  893. case 94: type = LLM_TYPE_235B_A22B; break;
  894. default: type = LLM_TYPE_UNKNOWN;
  895. }
  896. } break;
  897. case LLM_ARCH_PHI2:
  898. {
  899. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  900. switch (hparams.n_layer) {
  901. case 24: type = LLM_TYPE_1B; break;
  902. case 32: type = LLM_TYPE_3B; break;
  903. default: type = LLM_TYPE_UNKNOWN;
  904. }
  905. } break;
  906. case LLM_ARCH_PHI3:
  907. {
  908. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  909. switch (hparams.n_layer) {
  910. case 24: type = LLM_TYPE_1B; break;
  911. case 32: type = LLM_TYPE_3B; break;
  912. case 40: type = LLM_TYPE_14B; break;
  913. default: type = LLM_TYPE_UNKNOWN;
  914. }
  915. const bool found_swa = ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa, false);
  916. if (found_swa && hparams.n_swa > 0) {
  917. LLAMA_LOG_WARN("%s: Phi SWA is currently disabled - results might be suboptimal for some models (see %s)\n",
  918. __func__, "https://github.com/ggml-org/llama.cpp/pull/13676");
  919. // TODO: fix conversion scripts to correctly populate `n_swa` and `n_swa_pattern`
  920. hparams.swa_type = LLAMA_SWA_TYPE_NONE;
  921. hparams.n_swa = 0;
  922. hparams.set_swa_pattern(1);
  923. }
  924. } break;
  925. case LLM_ARCH_PHIMOE:
  926. {
  927. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  928. switch (hparams.n_layer) {
  929. case 32: type = LLM_TYPE_16x3_8B; break;
  930. default: type = LLM_TYPE_UNKNOWN;
  931. }
  932. } break;
  933. case LLM_ARCH_PLAMO:
  934. {
  935. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  936. switch (hparams.n_layer) {
  937. case 40: type = LLM_TYPE_13B; break;
  938. default: type = LLM_TYPE_UNKNOWN;
  939. }
  940. } break;
  941. case LLM_ARCH_PLAMO2:
  942. {
  943. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  944. // Load Mamba SSM parameters
  945. ml.get_key(LLM_KV_SSM_CONV_KERNEL, hparams.ssm_d_conv);
  946. ml.get_key(LLM_KV_SSM_INNER_SIZE, hparams.ssm_d_inner);
  947. ml.get_key(LLM_KV_SSM_STATE_SIZE, hparams.ssm_d_state);
  948. ml.get_key(LLM_KV_SSM_TIME_STEP_RANK, hparams.ssm_dt_rank);
  949. ml.get_key(LLM_KV_SSM_GROUP_COUNT, hparams.ssm_n_group);
  950. for (uint32_t i = 0; i < hparams.n_layer; ++i) {
  951. hparams.recurrent_layer_arr[i] = hparams.n_head_kv(i) == 0;
  952. }
  953. switch (hparams.n_layer) {
  954. case 16: type = LLM_TYPE_1B; break;
  955. case 32:
  956. if (hparams.n_embd == 2048) {
  957. type = LLM_TYPE_2B;
  958. } else if (hparams.n_embd == 4096) {
  959. type = LLM_TYPE_8B;
  960. }
  961. break;
  962. default: type = LLM_TYPE_UNKNOWN;
  963. }
  964. } break;
  965. case LLM_ARCH_GPT2:
  966. {
  967. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  968. switch (hparams.n_layer) {
  969. case 12: type = LLM_TYPE_SMALL; break;
  970. case 24: type = LLM_TYPE_MEDIUM; break;
  971. case 36: type = LLM_TYPE_LARGE; break;
  972. case 48: type = LLM_TYPE_XL; break;
  973. default: type = LLM_TYPE_UNKNOWN;
  974. }
  975. } break;
  976. case LLM_ARCH_CODESHELL:
  977. {
  978. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  979. switch (hparams.n_layer) {
  980. case 42: type = LLM_TYPE_7B; break;
  981. default: type = LLM_TYPE_UNKNOWN;
  982. }
  983. } break;
  984. case LLM_ARCH_ORION:
  985. {
  986. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  987. switch (hparams.n_layer) {
  988. case 40: type = LLM_TYPE_14B; break;
  989. default: type = LLM_TYPE_UNKNOWN;
  990. }
  991. } break;
  992. case LLM_ARCH_INTERNLM2:
  993. {
  994. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  995. switch (hparams.n_layer) {
  996. case 32: type = LLM_TYPE_7B; break;
  997. case 48: type = LLM_TYPE_20B; break;
  998. default: type = LLM_TYPE_UNKNOWN;
  999. }
  1000. } break;
  1001. case LLM_ARCH_GEMMA:
  1002. {
  1003. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1004. switch (hparams.n_layer) {
  1005. case 18: type = LLM_TYPE_2B; break;
  1006. case 28: type = LLM_TYPE_7B; break;
  1007. default: type = LLM_TYPE_UNKNOWN;
  1008. }
  1009. } break;
  1010. case LLM_ARCH_GEMMA2:
  1011. {
  1012. hparams.swa_type = LLAMA_SWA_TYPE_STANDARD;
  1013. hparams.n_swa = 4096; // default value of gemma 2
  1014. hparams.set_swa_pattern(2);
  1015. hparams.attn_soft_cap = true;
  1016. ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa, false);
  1017. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1018. ml.get_key(LLM_KV_ATTN_LOGIT_SOFTCAPPING, hparams.f_attn_logit_softcapping, false);
  1019. ml.get_key(LLM_KV_FINAL_LOGIT_SOFTCAPPING, hparams.f_final_logit_softcapping, false);
  1020. switch (hparams.n_layer) {
  1021. case 26: type = LLM_TYPE_2B; break;
  1022. case 42: type = LLM_TYPE_9B; break;
  1023. case 46: type = LLM_TYPE_27B; break;
  1024. default: type = LLM_TYPE_UNKNOWN;
  1025. }
  1026. // ref: https://github.com/google/gemma_pytorch/blob/014acb7ac4563a5f77c76d7ff98f31b568c16508/gemma/config.py#L173
  1027. hparams.f_attention_scale = type == LLM_TYPE_27B
  1028. ? 1.0f / std::sqrt(float(hparams.n_embd / hparams.n_head(0)))
  1029. : 1.0f / std::sqrt(float(hparams.n_embd_head_k));
  1030. } break;
  1031. case LLM_ARCH_GEMMA3:
  1032. {
  1033. hparams.swa_type = LLAMA_SWA_TYPE_STANDARD;
  1034. hparams.set_swa_pattern(6);
  1035. hparams.rope_freq_base_train_swa = 10000.0f;
  1036. hparams.rope_freq_scale_train_swa = 1.0f;
  1037. ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa);
  1038. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1039. switch (hparams.n_layer) {
  1040. case 18: type = LLM_TYPE_270M; break;
  1041. case 26: type = LLM_TYPE_1B; break;
  1042. case 34: type = LLM_TYPE_4B; break;
  1043. case 48: type = LLM_TYPE_12B; break;
  1044. case 62: type = LLM_TYPE_27B; break;
  1045. default: type = LLM_TYPE_UNKNOWN;
  1046. }
  1047. // ref: https://github.com/google/gemma_pytorch/blob/014acb7ac4563a5f77c76d7ff98f31b568c16508/gemma/config.py#L289
  1048. hparams.f_attention_scale = type == LLM_TYPE_27B
  1049. ? 1.0f / std::sqrt(float(hparams.n_embd / hparams.n_head(0)))
  1050. : 1.0f / std::sqrt(float(hparams.n_embd_head_k));
  1051. } break;
  1052. case LLM_ARCH_GEMMA3N:
  1053. {
  1054. hparams.swa_type = LLAMA_SWA_TYPE_STANDARD;
  1055. hparams.set_swa_pattern(5);
  1056. hparams.n_layer_kv_from_start = 20;
  1057. hparams.rope_freq_base_train_swa = 10000.0f;
  1058. hparams.rope_freq_scale_train_swa = 1.0f;
  1059. hparams.f_attention_scale = 1.0f;
  1060. ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa);
  1061. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1062. switch (hparams.n_layer) {
  1063. case 30: type = LLM_TYPE_E2B; break;
  1064. case 35: type = LLM_TYPE_E4B; break;
  1065. default: type = LLM_TYPE_UNKNOWN;
  1066. }
  1067. } break;
  1068. case LLM_ARCH_GEMMA_EMBEDDING:
  1069. {
  1070. hparams.swa_type = LLAMA_SWA_TYPE_SYMMETRIC;
  1071. hparams.set_swa_pattern(6);
  1072. hparams.causal_attn = false; // embeddings do not use causal attention
  1073. hparams.rope_freq_base_train_swa = 10000.0f;
  1074. hparams.rope_freq_scale_train_swa = 1.0f;
  1075. ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa);
  1076. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1077. ml.get_key(LLM_KV_POOLING_TYPE, hparams.pooling_type);
  1078. switch (hparams.n_layer) {
  1079. case 24: type = LLM_TYPE_0_3B; break;
  1080. default: type = LLM_TYPE_UNKNOWN;
  1081. }
  1082. hparams.f_attention_scale = 1.0f / std::sqrt(float(hparams.n_embd_head_k));
  1083. } break;
  1084. case LLM_ARCH_STARCODER2:
  1085. {
  1086. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  1087. switch (hparams.n_layer) {
  1088. case 30: type = LLM_TYPE_3B; break;
  1089. case 32: type = LLM_TYPE_7B; break;
  1090. case 40: type = LLM_TYPE_15B; break;
  1091. case 52: type = LLM_TYPE_20B; break; // granite
  1092. case 88: type = LLM_TYPE_34B; break; // granite
  1093. default: type = LLM_TYPE_UNKNOWN;
  1094. }
  1095. } break;
  1096. case LLM_ARCH_MAMBA:
  1097. {
  1098. ml.get_key(LLM_KV_SSM_CONV_KERNEL, hparams.ssm_d_conv);
  1099. ml.get_key(LLM_KV_SSM_INNER_SIZE, hparams.ssm_d_inner);
  1100. ml.get_key(LLM_KV_SSM_STATE_SIZE, hparams.ssm_d_state);
  1101. ml.get_key(LLM_KV_SSM_TIME_STEP_RANK, hparams.ssm_dt_rank);
  1102. ml.get_key(LLM_KV_SSM_DT_B_C_RMS, hparams.ssm_dt_b_c_rms, false);
  1103. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1104. switch (hparams.n_layer) {
  1105. case 24:
  1106. switch (hparams.n_embd) {
  1107. case 768: type = LLM_TYPE_SMALL; break;
  1108. default: type = LLM_TYPE_UNKNOWN;
  1109. } break;
  1110. case 48:
  1111. switch (hparams.n_embd) {
  1112. case 1024: type = LLM_TYPE_MEDIUM; break;
  1113. case 1536: type = LLM_TYPE_LARGE; break;
  1114. case 2048: type = LLM_TYPE_XL; break;
  1115. default: type = LLM_TYPE_UNKNOWN;
  1116. } break;
  1117. case 64:
  1118. switch (hparams.n_embd) {
  1119. case 2560: type = LLM_TYPE_3B; break;
  1120. default: type = LLM_TYPE_UNKNOWN;
  1121. } break;
  1122. default: type = LLM_TYPE_UNKNOWN;
  1123. }
  1124. } break;
  1125. case LLM_ARCH_MAMBA2:
  1126. {
  1127. ml.get_key(LLM_KV_SSM_CONV_KERNEL, hparams.ssm_d_conv);
  1128. ml.get_key(LLM_KV_SSM_INNER_SIZE, hparams.ssm_d_inner);
  1129. ml.get_key(LLM_KV_SSM_STATE_SIZE, hparams.ssm_d_state);
  1130. ml.get_key(LLM_KV_SSM_TIME_STEP_RANK, hparams.ssm_dt_rank);
  1131. ml.get_key(LLM_KV_SSM_GROUP_COUNT, hparams.ssm_n_group);
  1132. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1133. switch (hparams.n_layer) {
  1134. case 24:
  1135. switch (hparams.n_embd) {
  1136. case 768: type = LLM_TYPE_SMALL; break;
  1137. default: type = LLM_TYPE_UNKNOWN;
  1138. } break;
  1139. case 48:
  1140. switch (hparams.n_embd) {
  1141. case 1024: type = LLM_TYPE_MEDIUM; break;
  1142. case 1536: type = LLM_TYPE_LARGE; break;
  1143. case 2048: type = LLM_TYPE_XL; break;
  1144. default: type = LLM_TYPE_UNKNOWN;
  1145. } break;
  1146. case 64:
  1147. switch (hparams.n_embd) {
  1148. case 2560: type = LLM_TYPE_3B; break;
  1149. case 4096: type = LLM_TYPE_7B; break;
  1150. default: type = LLM_TYPE_UNKNOWN;
  1151. } break;
  1152. default: type = LLM_TYPE_UNKNOWN;
  1153. }
  1154. } break;
  1155. case LLM_ARCH_JAMBA:
  1156. {
  1157. ml.get_key(LLM_KV_SSM_CONV_KERNEL, hparams.ssm_d_conv);
  1158. ml.get_key(LLM_KV_SSM_INNER_SIZE, hparams.ssm_d_inner);
  1159. ml.get_key(LLM_KV_SSM_STATE_SIZE, hparams.ssm_d_state);
  1160. ml.get_key(LLM_KV_SSM_TIME_STEP_RANK, hparams.ssm_dt_rank);
  1161. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1162. for (uint32_t i = 0; i < hparams.n_layer; ++i) {
  1163. hparams.recurrent_layer_arr[i] = hparams.n_head_kv(i) == 0;
  1164. }
  1165. switch (hparams.n_layer) {
  1166. // TODO: Jamba layers are a bit heterogenous, so naming this is hard.
  1167. case 12: // 900M 8x???M
  1168. case 32: // 51B 16x?B
  1169. default: type = LLM_TYPE_UNKNOWN;
  1170. }
  1171. } break;
  1172. case LLM_ARCH_XVERSE:
  1173. {
  1174. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1175. switch (hparams.n_layer) {
  1176. case 32: type = LLM_TYPE_7B; break;
  1177. case 40: type = LLM_TYPE_13B; break;
  1178. case 80: type = LLM_TYPE_65B; break;
  1179. default: type = LLM_TYPE_UNKNOWN;
  1180. }
  1181. } break;
  1182. case LLM_ARCH_COMMAND_R:
  1183. {
  1184. ml.get_key(LLM_KV_LOGIT_SCALE, hparams.f_logit_scale);
  1185. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  1186. switch (hparams.n_layer) {
  1187. case 40: type = LLM_TYPE_35B; break;
  1188. default: type = LLM_TYPE_UNKNOWN;
  1189. }
  1190. } break;
  1191. case LLM_ARCH_COHERE2:
  1192. {
  1193. hparams.swa_type = LLAMA_SWA_TYPE_STANDARD;
  1194. hparams.set_swa_pattern(4);
  1195. ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa);
  1196. ml.get_key(LLM_KV_LOGIT_SCALE, hparams.f_logit_scale);
  1197. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  1198. switch (hparams.n_layer) {
  1199. case 32: type = LLM_TYPE_8B; break;
  1200. default: type = LLM_TYPE_UNKNOWN;
  1201. }
  1202. } break;
  1203. case LLM_ARCH_DBRX:
  1204. {
  1205. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  1206. ml.get_key(LLM_KV_ATTENTION_CLAMP_KQV, hparams.f_clamp_kqv);
  1207. switch (hparams.n_layer) {
  1208. case 40: type = LLM_TYPE_16x12B; break;
  1209. default: type = LLM_TYPE_UNKNOWN;
  1210. }
  1211. } break;
  1212. case LLM_ARCH_OLMO:
  1213. {
  1214. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  1215. ml.get_key(LLM_KV_ATTENTION_CLAMP_KQV, hparams.f_clamp_kqv, false);
  1216. switch (hparams.n_layer) {
  1217. case 22: type = LLM_TYPE_1B; break;
  1218. case 32: type = LLM_TYPE_7B; break;
  1219. case 80: type = LLM_TYPE_70B; break;
  1220. default: type = LLM_TYPE_UNKNOWN;
  1221. }
  1222. } break;
  1223. case LLM_ARCH_OLMO2:
  1224. {
  1225. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1226. const bool found_swa = ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa, false);
  1227. if (found_swa && hparams.n_swa > 0) {
  1228. hparams.swa_type = LLAMA_SWA_TYPE_STANDARD;
  1229. hparams.set_swa_pattern(4);
  1230. } else {
  1231. hparams.swa_type = LLAMA_SWA_TYPE_NONE;
  1232. }
  1233. switch (hparams.n_layer) {
  1234. case 16: type = LLM_TYPE_1B; break;
  1235. case 32: type = LLM_TYPE_7B; break;
  1236. case 40: type = LLM_TYPE_13B; break;
  1237. case 64: type = LLM_TYPE_32B; break;
  1238. default: type = LLM_TYPE_UNKNOWN;
  1239. }
  1240. } break;
  1241. case LLM_ARCH_SEED_OSS:
  1242. {
  1243. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1244. switch (hparams.n_layer) {
  1245. case 64: type = LLM_TYPE_36B; break;
  1246. default: type = LLM_TYPE_UNKNOWN;
  1247. }
  1248. } break;
  1249. case LLM_ARCH_OLMOE:
  1250. {
  1251. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1252. switch (hparams.n_layer) {
  1253. case 16: type = LLM_TYPE_A1_7B; break;
  1254. default: type = LLM_TYPE_UNKNOWN;
  1255. }
  1256. } break;
  1257. case LLM_ARCH_OPENELM:
  1258. {
  1259. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1260. switch (hparams.n_layer) {
  1261. case 16: type = LLM_TYPE_270M; break;
  1262. case 20: type = LLM_TYPE_450M; break;
  1263. case 28: type = LLM_TYPE_1B; break;
  1264. case 36: type = LLM_TYPE_3B; break;
  1265. default: type = LLM_TYPE_UNKNOWN;
  1266. }
  1267. } break;
  1268. case LLM_ARCH_GPTNEOX:
  1269. {
  1270. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  1271. ml.get_key(LLM_KV_USE_PARALLEL_RESIDUAL, hparams.use_par_res);
  1272. switch (hparams.n_layer) {
  1273. case 6:
  1274. switch (hparams.n_ff()) {
  1275. case 512: type = LLM_TYPE_14M; break;
  1276. case 2048: type = LLM_TYPE_70M; break;
  1277. default: type = LLM_TYPE_UNKNOWN;
  1278. } break;
  1279. case 12:
  1280. switch (hparams.n_ff()) {
  1281. case 3072: type = LLM_TYPE_160M; break;
  1282. default: type = LLM_TYPE_UNKNOWN;
  1283. } break;
  1284. case 16:
  1285. switch (hparams.n_ff()) {
  1286. case 8192: type = LLM_TYPE_1B; break;
  1287. default: type = LLM_TYPE_UNKNOWN;
  1288. } break;
  1289. case 24:
  1290. switch (hparams.n_ff()) {
  1291. case 4096: type = LLM_TYPE_410M; break;
  1292. case 8192: type = LLM_TYPE_1_4B; break;
  1293. default: type = LLM_TYPE_UNKNOWN;
  1294. } break;
  1295. case 32:
  1296. switch (hparams.n_ff()) {
  1297. case 10240: type = LLM_TYPE_2_8B; break;
  1298. case 16384: type = LLM_TYPE_6_9B; break;
  1299. default: type = LLM_TYPE_UNKNOWN;
  1300. } break;
  1301. case 36:
  1302. switch (hparams.n_ff()) {
  1303. case 20480: type = LLM_TYPE_12B; break;
  1304. default: type = LLM_TYPE_UNKNOWN;
  1305. } break;
  1306. case 44:
  1307. switch (hparams.n_ff()) {
  1308. case 24576: type = LLM_TYPE_20B; break;
  1309. default: type = LLM_TYPE_UNKNOWN;
  1310. } break;
  1311. default: type = LLM_TYPE_UNKNOWN;
  1312. }
  1313. } break;
  1314. case LLM_ARCH_ARCTIC:
  1315. {
  1316. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1317. if (hparams.n_expert == 128) {
  1318. switch (hparams.n_layer) {
  1319. case 35: type = LLM_TYPE_10B_128x3_66B; break;
  1320. default: type = LLM_TYPE_UNKNOWN;
  1321. }
  1322. } else {
  1323. type = LLM_TYPE_UNKNOWN;
  1324. }
  1325. } break;
  1326. case LLM_ARCH_DEEPSEEK:
  1327. {
  1328. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1329. ml.get_key(LLM_KV_LEADING_DENSE_BLOCK_COUNT, hparams.n_layer_dense_lead);
  1330. ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp);
  1331. ml.get_key(LLM_KV_EXPERT_SHARED_COUNT, hparams.n_expert_shared);
  1332. ml.get_key(LLM_KV_EXPERT_WEIGHTS_SCALE, hparams.expert_weights_scale);
  1333. switch (hparams.n_layer) {
  1334. case 28: type = LLM_TYPE_20B; break;
  1335. default: type = LLM_TYPE_UNKNOWN;
  1336. }
  1337. } break;
  1338. case LLM_ARCH_DEEPSEEK2:
  1339. {
  1340. bool is_lite = (hparams.n_layer == 27);
  1341. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1342. ml.get_key(LLM_KV_LEADING_DENSE_BLOCK_COUNT, hparams.n_layer_dense_lead);
  1343. if (!is_lite) {
  1344. ml.get_key(LLM_KV_ATTENTION_Q_LORA_RANK, hparams.n_lora_q);
  1345. }
  1346. ml.get_key(LLM_KV_ATTENTION_KV_LORA_RANK, hparams.n_lora_kv);
  1347. ml.get_key(LLM_KV_ATTENTION_KEY_LENGTH_MLA, hparams.n_embd_head_k_mla, false);
  1348. ml.get_key(LLM_KV_ATTENTION_VALUE_LENGTH_MLA, hparams.n_embd_head_v_mla, false);
  1349. ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp);
  1350. ml.get_key(LLM_KV_EXPERT_SHARED_COUNT, hparams.n_expert_shared);
  1351. ml.get_key(LLM_KV_EXPERT_WEIGHTS_SCALE, hparams.expert_weights_scale);
  1352. ml.get_key(LLM_KV_EXPERT_WEIGHTS_NORM, hparams.expert_weights_norm, false);
  1353. ml.get_key(LLM_KV_EXPERT_GATING_FUNC, hparams.expert_gating_func, false);
  1354. if (hparams.expert_gating_func == LLAMA_EXPERT_GATING_FUNC_TYPE_NONE) {
  1355. // for compatibility with existing DeepSeek V2 and V2.5 GGUFs
  1356. // that have no expert_gating_func model parameter set
  1357. hparams.expert_gating_func = LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX;
  1358. }
  1359. ml.get_key(LLM_KV_ROPE_SCALING_YARN_LOG_MUL, hparams.rope_yarn_log_mul, false);
  1360. switch (hparams.n_layer) {
  1361. case 27: type = LLM_TYPE_16B; break;
  1362. case 60: type = LLM_TYPE_236B; break;
  1363. case 61: type = LLM_TYPE_671B; break;
  1364. default: type = LLM_TYPE_UNKNOWN;
  1365. }
  1366. } break;
  1367. case LLM_ARCH_PLM:
  1368. {
  1369. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1370. ml.get_key(LLM_KV_ATTENTION_KV_LORA_RANK, hparams.n_lora_kv);
  1371. switch (hparams.n_layer) {
  1372. case 32: type = LLM_TYPE_1_8B; break;
  1373. default: type = LLM_TYPE_UNKNOWN;
  1374. }
  1375. } break;
  1376. case LLM_ARCH_CHATGLM:
  1377. {
  1378. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1379. switch (hparams.n_layer) {
  1380. case 28: {
  1381. if (hparams.n_head(0) == 16) {
  1382. type = LLM_TYPE_1_5B;
  1383. } else {
  1384. type = LLM_TYPE_6B;
  1385. }
  1386. } break;
  1387. case 40: {
  1388. if (hparams.n_head(0) == 24) {
  1389. type = LLM_TYPE_4B;
  1390. } else {
  1391. type = LLM_TYPE_9B;
  1392. }
  1393. } break;
  1394. default: type = LLM_TYPE_UNKNOWN;
  1395. }
  1396. } break;
  1397. case LLM_ARCH_GLM4:
  1398. {
  1399. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1400. switch (hparams.n_layer) {
  1401. case 40: type = LLM_TYPE_9B; break;
  1402. case 61: type = LLM_TYPE_32B; break;
  1403. default: type = LLM_TYPE_UNKNOWN;
  1404. }
  1405. } break;
  1406. case LLM_ARCH_GLM4_MOE:
  1407. {
  1408. ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp);
  1409. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1410. // MoE parameters
  1411. ml.get_key(LLM_KV_EXPERT_COUNT, hparams.n_expert);
  1412. ml.get_key(LLM_KV_EXPERT_USED_COUNT, hparams.n_expert_used);
  1413. ml.get_key(LLM_KV_EXPERT_SHARED_COUNT, hparams.n_expert_shared);
  1414. ml.get_key(LLM_KV_LEADING_DENSE_BLOCK_COUNT, hparams.n_layer_dense_lead, false);
  1415. ml.get_key(LLM_KV_EXPERT_WEIGHTS_SCALE, hparams.expert_weights_scale);
  1416. ml.get_key(LLM_KV_EXPERT_WEIGHTS_NORM, hparams.expert_weights_norm, false);
  1417. // Expert gating function (GLM-4.5 uses sigmoid)
  1418. ml.get_key(LLM_KV_EXPERT_GATING_FUNC, hparams.expert_gating_func, false);
  1419. if (hparams.expert_gating_func == LLAMA_EXPERT_GATING_FUNC_TYPE_NONE) {
  1420. hparams.expert_gating_func = LLAMA_EXPERT_GATING_FUNC_TYPE_SIGMOID;
  1421. }
  1422. // NextN/MTP parameters
  1423. ml.get_key(LLM_KV_NEXTN_PREDICT_LAYERS, hparams.nextn_predict_layers, false);
  1424. // TODO: when MTP is implemented, this should probably be updated if needed
  1425. hparams.n_layer_kv_from_start = hparams.n_layer - hparams.nextn_predict_layers;
  1426. switch (hparams.n_layer) {
  1427. case 47: type = LLM_TYPE_106B_A12B; break; // GLM-4.5-Air (46 layers + 1 NextN layer)
  1428. case 93: type = LLM_TYPE_355B_A32B; break; // GLM-4.5 (92 layers + 1 NextN layer)
  1429. default: type = LLM_TYPE_UNKNOWN;
  1430. }
  1431. } break;
  1432. case LLM_ARCH_BITNET:
  1433. {
  1434. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1435. switch (hparams.n_layer) {
  1436. case 26: type = LLM_TYPE_3B; break;
  1437. default: type = LLM_TYPE_UNKNOWN;
  1438. }
  1439. } break;
  1440. case LLM_ARCH_T5:
  1441. {
  1442. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1443. ml.get_key(LLM_KV_ATTENTION_RELATIVE_BUCKETS_COUNT, hparams.n_rel_attn_bkts);
  1444. uint32_t dec_start_token_id;
  1445. if (ml.get_key(LLM_KV_DECODER_START_TOKEN_ID, dec_start_token_id, false)) {
  1446. hparams.dec_start_token_id = dec_start_token_id;
  1447. }
  1448. hparams.dec_n_layer = hparams.n_layer;
  1449. ml.get_key(LLM_KV_DECODER_BLOCK_COUNT, hparams.dec_n_layer, false);
  1450. switch (hparams.n_layer) {
  1451. case 6: type = LLM_TYPE_60M; break; // t5-small
  1452. case 8: type = LLM_TYPE_80M; break; // flan-t5-small
  1453. case 12:
  1454. switch (hparams.n_ff()) {
  1455. case 3072: type = LLM_TYPE_220M; break; // t5-base
  1456. case 2048: type = LLM_TYPE_250M; break; // flan-t5-base
  1457. default: type = LLM_TYPE_UNKNOWN;
  1458. } break;
  1459. case 24:
  1460. switch (hparams.n_ff()) {
  1461. case 4096: type = LLM_TYPE_770M; break; // t5-large
  1462. case 2816: type = LLM_TYPE_780M; break; // flan-t5-large
  1463. case 16384: type = LLM_TYPE_3B; break; // t5-3b
  1464. case 5120: type = LLM_TYPE_3B; break; // flan-t5-xl
  1465. case 65536: type = LLM_TYPE_11B; break; // t5-11b
  1466. case 10240: type = LLM_TYPE_11B; break; // flan-t5-xxl
  1467. default: type = LLM_TYPE_UNKNOWN;
  1468. } break;
  1469. default: type = LLM_TYPE_UNKNOWN;
  1470. }
  1471. } break;
  1472. case LLM_ARCH_T5ENCODER:
  1473. {
  1474. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1475. ml.get_key(LLM_KV_ATTENTION_RELATIVE_BUCKETS_COUNT, hparams.n_rel_attn_bkts);
  1476. type = LLM_TYPE_UNKNOWN;
  1477. } break;
  1478. case LLM_ARCH_JAIS:
  1479. {
  1480. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  1481. ml.get_key(LLM_KV_ATTENTION_MAX_ALIBI_BIAS, hparams.f_max_alibi_bias);
  1482. switch (hparams.n_layer) {
  1483. case 24: type = LLM_TYPE_1_3B; break;
  1484. case 40: type = LLM_TYPE_13B; break;
  1485. /* TODO: add variants */
  1486. default: type = LLM_TYPE_UNKNOWN;
  1487. }
  1488. } break;
  1489. case LLM_ARCH_NEMOTRON:
  1490. {
  1491. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  1492. switch (hparams.n_layer) {
  1493. case 32: type = LLM_TYPE_4B; break;
  1494. default: type = LLM_TYPE_UNKNOWN;
  1495. }
  1496. } break;
  1497. case LLM_ARCH_NEMOTRON_H:
  1498. {
  1499. ml.get_key(LLM_KV_SSM_CONV_KERNEL, hparams.ssm_d_conv);
  1500. ml.get_key(LLM_KV_SSM_INNER_SIZE, hparams.ssm_d_inner);
  1501. ml.get_key(LLM_KV_SSM_STATE_SIZE, hparams.ssm_d_state);
  1502. ml.get_key(LLM_KV_SSM_TIME_STEP_RANK, hparams.ssm_dt_rank);
  1503. ml.get_key(LLM_KV_SSM_GROUP_COUNT, hparams.ssm_n_group);
  1504. // A layer is recurrent IFF the n_head_kv value is set to 0 and
  1505. // the n_ff value is set to 0
  1506. for (uint32_t i = 0; i < hparams.n_layer; ++i) {
  1507. hparams.recurrent_layer_arr[i] = (hparams.n_head_kv(i) == 0 && hparams.n_ff(i) == 0);
  1508. }
  1509. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1510. switch (hparams.n_layer) {
  1511. case 56: type = LLM_TYPE_9B; break;
  1512. default: type = LLM_TYPE_UNKNOWN;
  1513. }
  1514. } break;
  1515. case LLM_ARCH_EXAONE:
  1516. {
  1517. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1518. switch (hparams.n_layer) {
  1519. case 32: type = LLM_TYPE_8B; break;
  1520. default: type = LLM_TYPE_UNKNOWN;
  1521. }
  1522. } break;
  1523. case LLM_ARCH_EXAONE4:
  1524. {
  1525. if (hparams.n_layer == 64) { // 32B
  1526. hparams.swa_type = LLAMA_SWA_TYPE_STANDARD;
  1527. hparams.n_swa = 4096;
  1528. hparams.set_swa_pattern(4);
  1529. }
  1530. ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa, false);
  1531. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1532. switch (hparams.n_layer) {
  1533. case 30: type = LLM_TYPE_1_2B; break;
  1534. case 64: type = LLM_TYPE_32B; break;
  1535. default: type = LLM_TYPE_UNKNOWN;
  1536. }
  1537. } break;
  1538. case LLM_ARCH_RWKV6:
  1539. case LLM_ARCH_RWKV6QWEN2:
  1540. {
  1541. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps, false);
  1542. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps, false);
  1543. ml.get_key(LLM_KV_WKV_HEAD_SIZE, hparams.wkv_head_size);
  1544. ml.get_key(LLM_KV_TIME_MIX_EXTRA_DIM, hparams.time_mix_extra_dim);
  1545. ml.get_key(LLM_KV_TIME_DECAY_EXTRA_DIM, hparams.time_decay_extra_dim);
  1546. ml.get_key(LLM_KV_RESCALE_EVERY_N_LAYERS, hparams.rescale_every_n_layers, false);
  1547. ml.get_key(LLM_KV_TOKEN_SHIFT_COUNT, hparams.token_shift_count, false);
  1548. switch (hparams.n_layer) {
  1549. case 24: type = LLM_TYPE_1_6B; break;
  1550. case 32:
  1551. switch (hparams.n_embd) {
  1552. case 2560: type = LLM_TYPE_3B; break;
  1553. case 4096: type = LLM_TYPE_7B; break;
  1554. default: type = LLM_TYPE_UNKNOWN;
  1555. } break;
  1556. case 61: type = LLM_TYPE_14B; break;
  1557. case 64: type = LLM_TYPE_32B; break;
  1558. default: type = LLM_TYPE_UNKNOWN;
  1559. }
  1560. } break;
  1561. case LLM_ARCH_RWKV7:
  1562. case LLM_ARCH_ARWKV7:
  1563. {
  1564. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps, false);
  1565. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps, false);
  1566. ml.get_key(LLM_KV_WKV_HEAD_SIZE, hparams.wkv_head_size);
  1567. ml.get_key(LLM_KV_ATTENTION_DECAY_LORA_RANK, hparams.n_lora_decay);
  1568. ml.get_key(LLM_KV_ATTENTION_ICLR_LORA_RANK, hparams.n_lora_iclr);
  1569. ml.get_key(LLM_KV_ATTENTION_VALUE_RESIDUAL_MIX_LORA_RANK, hparams.n_lora_value_res_mix);
  1570. ml.get_key(LLM_KV_ATTENTION_GATE_LORA_RANK, hparams.n_lora_gate, false);
  1571. ml.get_key(LLM_KV_TOKEN_SHIFT_COUNT, hparams.token_shift_count, false);
  1572. switch (hparams.n_layer) {
  1573. case 12:
  1574. switch (hparams.n_embd) {
  1575. case 768: type = LLM_TYPE_190M; break;
  1576. default: type = LLM_TYPE_UNKNOWN;
  1577. } break;
  1578. case 24:
  1579. switch (hparams.n_embd) {
  1580. case 1024: type = LLM_TYPE_450M; break;
  1581. case 2048: type = LLM_TYPE_1_5B; break;
  1582. default: type = LLM_TYPE_UNKNOWN;
  1583. } break;
  1584. case 28:
  1585. switch (hparams.n_embd) {
  1586. case 1536: type = LLM_TYPE_1_5B; break;
  1587. case 3584: type = LLM_TYPE_7B; break;
  1588. default: type = LLM_TYPE_UNKNOWN;
  1589. } break;
  1590. case 32:
  1591. switch (hparams.n_embd) {
  1592. case 2560: type = LLM_TYPE_2_9B; break;
  1593. case 4096: type = LLM_TYPE_7B; break;
  1594. default: type = LLM_TYPE_UNKNOWN;
  1595. } break;
  1596. case 61:
  1597. switch (hparams.n_embd) {
  1598. case 4096: type = LLM_TYPE_14B; break;
  1599. default: type = LLM_TYPE_UNKNOWN;
  1600. } break;
  1601. default: type = LLM_TYPE_UNKNOWN;
  1602. }
  1603. } break;
  1604. case LLM_ARCH_GRANITE:
  1605. case LLM_ARCH_GRANITE_MOE:
  1606. {
  1607. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1608. ml.get_key(LLM_KV_LOGIT_SCALE, hparams.f_logit_scale);
  1609. ml.get_key(LLM_KV_RESIDUAL_SCALE, hparams.f_residual_scale);
  1610. ml.get_key(LLM_KV_EMBEDDING_SCALE, hparams.f_embedding_scale);
  1611. ml.get_key(LLM_KV_ATTENTION_SCALE, hparams.f_attention_scale);
  1612. // Granite uses rope_finetuned as a switch for rope, so default to true
  1613. bool rope_finetuned = true;
  1614. ml.get_key(LLM_KV_ROPE_SCALING_FINETUNED, rope_finetuned, false);
  1615. hparams.rope_finetuned = rope_finetuned;
  1616. switch (hparams.n_layer) {
  1617. case 32: type = LLM_TYPE_3B; break;
  1618. case 40: type = LLM_TYPE_3B; break;
  1619. // Add additional layer/vocab/etc checks here for other model sizes
  1620. default: type = LLM_TYPE_UNKNOWN;
  1621. }
  1622. // For Granite MoE Shared
  1623. ml.get_key(LLM_KV_EXPERT_SHARED_FEED_FORWARD_LENGTH, hparams.n_ff_shexp, /* required */ false);
  1624. } break;
  1625. case LLM_ARCH_GRANITE_HYBRID:
  1626. {
  1627. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1628. ml.get_key(LLM_KV_LOGIT_SCALE, hparams.f_logit_scale, /* required */ false);
  1629. ml.get_key(LLM_KV_RESIDUAL_SCALE, hparams.f_residual_scale, /* required */ false);
  1630. ml.get_key(LLM_KV_EMBEDDING_SCALE, hparams.f_embedding_scale, /* required */ false);
  1631. ml.get_key(LLM_KV_ATTENTION_SCALE, hparams.f_attention_scale, /* required */ false);
  1632. ml.get_key(LLM_KV_SSM_CONV_KERNEL, hparams.ssm_d_conv);
  1633. ml.get_key(LLM_KV_SSM_INNER_SIZE, hparams.ssm_d_inner);
  1634. ml.get_key(LLM_KV_SSM_STATE_SIZE, hparams.ssm_d_state);
  1635. ml.get_key(LLM_KV_SSM_TIME_STEP_RANK, hparams.ssm_dt_rank);
  1636. ml.get_key(LLM_KV_SSM_GROUP_COUNT, hparams.ssm_n_group);
  1637. // Granite uses rope_finetuned as a switch for rope, so default to true
  1638. bool rope_finetuned = true;
  1639. ml.get_key(LLM_KV_ROPE_SCALING_FINETUNED, rope_finetuned, false);
  1640. hparams.rope_finetuned = rope_finetuned;
  1641. // A layer is recurrent IFF the n_head_kv value is set to 0
  1642. for (uint32_t i = 0; i < hparams.n_layer; ++i) {
  1643. hparams.recurrent_layer_arr[i] = hparams.n_head_kv(i) == 0;
  1644. }
  1645. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1646. switch (hparams.n_layer) {
  1647. // TODO: Add llm type label (not sure this is useful)
  1648. default: type = LLM_TYPE_UNKNOWN;
  1649. }
  1650. // For Granite MoE Shared
  1651. ml.get_key(LLM_KV_EXPERT_SHARED_FEED_FORWARD_LENGTH, hparams.n_ff_shexp, /* required */ false);
  1652. } break;
  1653. case LLM_ARCH_QWEN3NEXT:
  1654. {
  1655. ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp, false);
  1656. ml.get_key(LLM_KV_EXPERT_SHARED_FEED_FORWARD_LENGTH, hparams.n_ff_shexp, false);
  1657. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1658. // Load linear attention (gated delta net) parameters
  1659. ml.get_key(LLM_KV_SSM_CONV_KERNEL, hparams.ssm_d_conv);
  1660. ml.get_key(LLM_KV_SSM_INNER_SIZE, hparams.ssm_d_inner);
  1661. ml.get_key(LLM_KV_SSM_STATE_SIZE, hparams.ssm_d_state);
  1662. ml.get_key(LLM_KV_SSM_TIME_STEP_RANK, hparams.ssm_dt_rank);
  1663. ml.get_key(LLM_KV_SSM_GROUP_COUNT, hparams.ssm_n_group);
  1664. // Mark recurrent layers (linear attention layers)
  1665. for (uint32_t i = 0; i < hparams.n_layer; ++i) {
  1666. hparams.recurrent_layer_arr[i] = ((i + 1) % 4 != 0); // TODO: extract the magic 4 from "full_attention_interval"
  1667. }
  1668. switch (hparams.n_layer) {
  1669. case 80: type = LLM_TYPE_80B_A3B; break;
  1670. default: type = LLM_TYPE_UNKNOWN;
  1671. }
  1672. } break;
  1673. case LLM_ARCH_CHAMELEON:
  1674. {
  1675. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1676. hparams.f_norm_eps = 1e-5; // eps for qk-norm, torch default
  1677. ml.get_key(LLM_KV_SWIN_NORM, hparams.swin_norm);
  1678. switch (hparams.n_layer) {
  1679. case 32: type = LLM_TYPE_7B; break;
  1680. case 48: type = LLM_TYPE_34B; break;
  1681. default: type = LLM_TYPE_UNKNOWN;
  1682. }
  1683. } break;
  1684. case LLM_ARCH_WAVTOKENIZER_DEC:
  1685. {
  1686. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  1687. ml.get_key(LLM_KV_ATTENTION_GROUPNORM_EPS, hparams.f_norm_group_eps);
  1688. ml.get_key(LLM_KV_ATTENTION_GROUPNORM_GROUPS, hparams.n_norm_groups);
  1689. ml.get_key(LLM_KV_ATTENTION_CAUSAL, hparams.causal_attn);
  1690. } break;
  1691. case LLM_ARCH_BAILINGMOE:
  1692. {
  1693. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1694. ml.get_key(LLM_KV_LEADING_DENSE_BLOCK_COUNT, hparams.n_layer_dense_lead);
  1695. ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp);
  1696. ml.get_key(LLM_KV_EXPERT_SHARED_COUNT, hparams.n_expert_shared);
  1697. ml.get_key(LLM_KV_EXPERT_WEIGHTS_SCALE, hparams.expert_weights_scale);
  1698. ml.get_key(LLM_KV_EXPERT_WEIGHTS_NORM, hparams.expert_weights_norm, false);
  1699. switch (hparams.n_layer) {
  1700. case 28: type = LLM_TYPE_16B; break;
  1701. case 88: type = LLM_TYPE_290B; break;
  1702. default: type = LLM_TYPE_UNKNOWN;
  1703. }
  1704. } break;
  1705. case LLM_ARCH_DOTS1:
  1706. {
  1707. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1708. ml.get_key(LLM_KV_LEADING_DENSE_BLOCK_COUNT, hparams.n_layer_dense_lead);
  1709. ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp);
  1710. ml.get_key(LLM_KV_EXPERT_SHARED_COUNT, hparams.n_expert_shared);
  1711. ml.get_key(LLM_KV_EXPERT_WEIGHTS_SCALE, hparams.expert_weights_scale);
  1712. ml.get_key(LLM_KV_EXPERT_WEIGHTS_NORM, hparams.expert_weights_norm, false);
  1713. ml.get_key(LLM_KV_EXPERT_GATING_FUNC, hparams.expert_gating_func, false);
  1714. switch (hparams.n_layer) {
  1715. case 62: type = LLM_TYPE_142B; break;
  1716. default: type = LLM_TYPE_UNKNOWN;
  1717. }
  1718. } break;
  1719. case LLM_ARCH_ERNIE4_5:
  1720. case LLM_ARCH_ERNIE4_5_MOE:
  1721. {
  1722. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1723. if (arch == LLM_ARCH_ERNIE4_5_MOE) {
  1724. ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp);
  1725. ml.get_key(LLM_KV_EXPERT_SHARED_FEED_FORWARD_LENGTH, hparams.n_ff_shexp, false);
  1726. ml.get_key(LLM_KV_INTERLEAVE_MOE_LAYER_STEP, hparams.n_moe_layer_step);
  1727. ml.get_key(LLM_KV_LEADING_DENSE_BLOCK_COUNT, hparams.n_layer_dense_lead);
  1728. }
  1729. switch (hparams.n_layer) {
  1730. case 18: type = LLM_TYPE_0_3B; break;
  1731. case 28: type = LLM_TYPE_21B_A3B; break;
  1732. case 54: type = LLM_TYPE_300B_A47B; break;
  1733. default: type = LLM_TYPE_UNKNOWN;
  1734. }
  1735. } break;
  1736. case LLM_ARCH_FALCON_H1:
  1737. {
  1738. // Common parameters
  1739. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1740. // SSM parameters
  1741. ml.get_key(LLM_KV_SSM_CONV_KERNEL, hparams.ssm_d_conv);
  1742. ml.get_key(LLM_KV_SSM_INNER_SIZE, hparams.ssm_d_inner);
  1743. ml.get_key(LLM_KV_SSM_STATE_SIZE, hparams.ssm_d_state);
  1744. ml.get_key(LLM_KV_SSM_TIME_STEP_RANK, hparams.ssm_dt_rank);
  1745. ml.get_key(LLM_KV_SSM_GROUP_COUNT, hparams.ssm_n_group);
  1746. std::fill(hparams.recurrent_layer_arr.begin(), hparams.recurrent_layer_arr.end(), true);
  1747. switch (hparams.n_layer) {
  1748. case 36:
  1749. type = LLM_TYPE_0_5B; break;
  1750. case 24:
  1751. type = LLM_TYPE_1_5B; break;
  1752. case 66:
  1753. type = LLM_TYPE_1B; break;
  1754. case 32:
  1755. type = LLM_TYPE_3B; break;
  1756. case 44:
  1757. type = LLM_TYPE_7B; break;
  1758. case 72:
  1759. type = LLM_TYPE_34B; break;
  1760. default:
  1761. type = LLM_TYPE_UNKNOWN;
  1762. }
  1763. } break;
  1764. case LLM_ARCH_HUNYUAN_MOE:
  1765. {
  1766. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1767. ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp);
  1768. ml.get_key(LLM_KV_EXPERT_SHARED_FEED_FORWARD_LENGTH, hparams.n_ff_shexp);
  1769. switch (hparams.n_layer) {
  1770. case 32: type = LLM_TYPE_A13B; break;
  1771. default: type = LLM_TYPE_UNKNOWN;
  1772. }
  1773. } break;
  1774. case LLM_ARCH_HUNYUAN_DENSE:
  1775. {
  1776. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1777. switch (hparams.n_embd) {
  1778. case 1024: type = LLM_TYPE_0_5B; break;
  1779. case 2048: type = LLM_TYPE_1_8B; break;
  1780. case 3072: type = LLM_TYPE_4B; break;
  1781. case 4096: type = LLM_TYPE_7B; break;
  1782. default: type = LLM_TYPE_UNKNOWN;
  1783. }
  1784. } break;
  1785. case LLM_ARCH_SMOLLM3:
  1786. {
  1787. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1788. hparams.n_no_rope_layer_step = 4;
  1789. switch (hparams.n_layer) {
  1790. case 36: type = LLM_TYPE_3B; break;
  1791. default: type = LLM_TYPE_UNKNOWN;
  1792. }
  1793. } break;
  1794. case LLM_ARCH_OPENAI_MOE:
  1795. {
  1796. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1797. ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp);
  1798. ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa);
  1799. hparams.swa_type = LLAMA_SWA_TYPE_STANDARD;
  1800. hparams.set_swa_pattern(2);
  1801. switch (hparams.n_layer) {
  1802. case 24: type = LLM_TYPE_20B; break;
  1803. case 36: type = LLM_TYPE_120B; break;
  1804. default: type = LLM_TYPE_UNKNOWN;
  1805. }
  1806. } break;
  1807. case LLM_ARCH_LFM2:
  1808. {
  1809. ml.get_key(LLM_KV_SHORTCONV_L_CACHE, hparams.n_shortconv_l_cache);
  1810. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1811. for (uint32_t il = 0; il < hparams.n_layer; ++il) {
  1812. hparams.recurrent_layer_arr[il] = hparams.n_head_kv(il) == 0;
  1813. }
  1814. switch (hparams.n_embd) {
  1815. case 1024: type = LLM_TYPE_350M; break;
  1816. case 1536: type = LLM_TYPE_700M; break;
  1817. case 2048: type = LLM_TYPE_1_2B; break;
  1818. default: type = LLM_TYPE_UNKNOWN;
  1819. }
  1820. } break;
  1821. case LLM_ARCH_SMALLTHINKER:
  1822. {
  1823. const bool found_swa = ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa, false);
  1824. if (found_swa && hparams.n_swa > 0) {
  1825. hparams.swa_type = LLAMA_SWA_TYPE_STANDARD;
  1826. hparams.n_swa = 4096;
  1827. hparams.set_swa_pattern(4, true);
  1828. } else {
  1829. hparams.swa_type = LLAMA_SWA_TYPE_NONE;
  1830. hparams.n_no_rope_layer_step = hparams.n_layer;
  1831. }
  1832. ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp, false);
  1833. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1834. ml.get_key(LLM_KV_EXPERT_GATING_FUNC, hparams.expert_gating_func, false);
  1835. switch (hparams.n_layer) {
  1836. case 32: type = LLM_TYPE_4B; break;
  1837. case 52: type = LLM_TYPE_20B; break;
  1838. default: type = LLM_TYPE_UNKNOWN;
  1839. }
  1840. } break;
  1841. default: throw std::runtime_error("unsupported model architecture");
  1842. }
  1843. pimpl->n_bytes = ml.n_bytes;
  1844. pimpl->desc_str = arch_name() + " " + type_name() + " " + ml.ftype_name();
  1845. if (hparams.f_max_alibi_bias > 0.0f) {
  1846. hparams.use_alibi = true;
  1847. }
  1848. hparams.rope_type = llama_model_rope_type(this);
  1849. }
  1850. void llama_model::load_vocab(llama_model_loader & ml) {
  1851. const auto kv = LLM_KV(arch);
  1852. vocab.load(ml, kv);
  1853. }
  1854. bool llama_model::load_tensors(llama_model_loader & ml) {
  1855. const auto & split_mode = params.split_mode;
  1856. const auto & n_gpu_layers = params.n_gpu_layers;
  1857. const auto & use_mlock = params.use_mlock;
  1858. const auto & tensor_split = params.tensor_split;
  1859. const int n_layer = hparams.n_layer;
  1860. const bool use_mmap_buffer = true;
  1861. LLAMA_LOG_INFO("%s: loading model tensors, this can take a while... (mmap = %s)\n", __func__, ml.use_mmap ? "true" : "false");
  1862. // build a list of buffer types for the CPU and GPU devices
  1863. pimpl->cpu_buft_list = make_cpu_buft_list(devices, params.use_extra_bufts);
  1864. for (auto * dev : devices) {
  1865. buft_list_t buft_list = make_gpu_buft_list(dev, split_mode, tensor_split);
  1866. // add CPU buffer types as a fallback
  1867. buft_list.insert(buft_list.end(), pimpl->cpu_buft_list.begin(), pimpl->cpu_buft_list.end());
  1868. pimpl->gpu_buft_list.emplace(dev, std::move(buft_list));
  1869. }
  1870. // calculate the split points
  1871. bool all_zero = tensor_split == nullptr || std::all_of(tensor_split, tensor_split + n_devices(), [](float x) { return x == 0.0f; });
  1872. std::vector<float> splits(n_devices());
  1873. if (all_zero) {
  1874. // default split, by free memory
  1875. for (size_t i = 0; i < n_devices(); ++i) {
  1876. ggml_backend_dev_t dev = devices[i];
  1877. size_t total;
  1878. size_t free;
  1879. ggml_backend_dev_memory(dev, &free, &total);
  1880. splits[i] = free;
  1881. }
  1882. } else {
  1883. std::copy(tensor_split, tensor_split + n_devices(), splits.begin());
  1884. }
  1885. // sum and normalize the splits to get the split points
  1886. float split_sum = 0.0f;
  1887. for (size_t i = 0; i < n_devices(); ++i) {
  1888. split_sum += splits[i];
  1889. splits[i] = split_sum;
  1890. }
  1891. for (size_t i = 0; i < n_devices(); ++i) {
  1892. splits[i] /= split_sum;
  1893. }
  1894. ggml_backend_dev_t cpu_dev = ggml_backend_dev_by_type(GGML_BACKEND_DEVICE_TYPE_CPU);
  1895. if (cpu_dev == nullptr) {
  1896. throw std::runtime_error(format("%s: no CPU backend found", __func__));
  1897. }
  1898. const int i_gpu_start = std::max((int) hparams.n_layer - n_gpu_layers, (int) 0);
  1899. const int act_gpu_layers = devices.empty() ? 0 : std::min(n_gpu_layers, (int)n_layer + 1);
  1900. auto get_layer_buft_list = [&](int il) -> llama_model::impl::layer_dev {
  1901. const bool is_swa = il < (int) hparams.n_layer && hparams.is_swa(il);
  1902. if (il < i_gpu_start || (il - i_gpu_start) >= act_gpu_layers) {
  1903. LLAMA_LOG_DEBUG("load_tensors: layer %3d assigned to device %s, is_swa = %d\n", il, ggml_backend_dev_name(cpu_dev), is_swa);
  1904. return {cpu_dev, &pimpl->cpu_buft_list};
  1905. }
  1906. const int layer_gpu = std::upper_bound(splits.begin(), splits.begin() + n_devices(), float(il - i_gpu_start)/act_gpu_layers) - splits.begin();
  1907. auto * dev = devices.at(layer_gpu);
  1908. LLAMA_LOG_DEBUG("load_tensors: layer %3d assigned to device %s, is_swa = %d\n", il, ggml_backend_dev_name(dev), is_swa);
  1909. return {dev, &pimpl->gpu_buft_list.at(dev)};
  1910. };
  1911. // assign the input layer
  1912. // there is very little benefit to offloading the input layer, so always keep it on the CPU
  1913. pimpl->dev_input = { cpu_dev, &pimpl->cpu_buft_list };
  1914. // assign the repeating layers to the devices according to the splits
  1915. pimpl->dev_layer.resize(n_layer);
  1916. for (int il = 0; il < n_layer; ++il) {
  1917. pimpl->dev_layer[il] = get_layer_buft_list(il);
  1918. }
  1919. // assign the output layer
  1920. pimpl->dev_output = get_layer_buft_list(n_layer);
  1921. // one ggml context per buffer type
  1922. int max_n_tensors = ml.n_tensors;
  1923. max_n_tensors += 1; // duplicated output tensor
  1924. max_n_tensors += n_layer*2; // duplicated rope freq tensors
  1925. const size_t ctx_size = ggml_tensor_overhead()*max_n_tensors;
  1926. std::map<ggml_backend_buffer_type_t, ggml_context *> ctx_map;
  1927. auto ctx_for_buft = [&](ggml_backend_buffer_type_t buft) -> ggml_context * {
  1928. auto it = ctx_map.find(buft);
  1929. if (it == ctx_map.end()) {
  1930. ggml_init_params params = {
  1931. /*.mem_size =*/ ctx_size,
  1932. /*.mem_buffer =*/ NULL,
  1933. /*.no_alloc =*/ true,
  1934. };
  1935. ggml_context * ctx = ggml_init(params);
  1936. if (!ctx) {
  1937. throw std::runtime_error(format("failed to create ggml context"));
  1938. }
  1939. ctx_map[buft] = ctx;
  1940. pimpl->ctxs.emplace_back(ctx);
  1941. return ctx;
  1942. }
  1943. return it->second;
  1944. };
  1945. const auto TENSOR_DUPLICATED = llama_model_loader::TENSOR_DUPLICATED;
  1946. const auto TENSOR_NOT_REQUIRED = llama_model_loader::TENSOR_NOT_REQUIRED;
  1947. const auto TENSOR_SKIP = llama_model_loader::TENSOR_SKIP;
  1948. // create tensors for the weights
  1949. {
  1950. // note: cast to int64_t since we will use these for the tensor dimensions
  1951. const int64_t n_head = hparams.n_head();
  1952. const int64_t n_head_kv = hparams.n_head_kv();
  1953. const int64_t n_embd = hparams.n_embd;
  1954. const int64_t n_embd_k_gqa = hparams.n_embd_k_gqa();
  1955. const int64_t n_embd_v_gqa = hparams.n_embd_v_gqa();
  1956. const int64_t n_embd_head_k = hparams.n_embd_head_k;
  1957. const int64_t n_embd_head_v = hparams.n_embd_head_v;
  1958. const int64_t n_ff = hparams.n_ff();
  1959. const int64_t n_embd_gqa = n_embd_v_gqa;
  1960. const int64_t n_vocab = vocab.n_tokens();
  1961. const int64_t n_token_types = vocab.n_token_types();
  1962. const int64_t n_rot = hparams.n_rot;
  1963. const int64_t n_expert = hparams.n_expert;
  1964. const int64_t n_expert_used = hparams.n_expert_used;
  1965. const int64_t n_ctx_train = hparams.n_ctx_train;
  1966. if (n_expert > 0 && hparams.n_expert_used == 0) {
  1967. throw std::runtime_error("model has expert layers but no expert layers are used");
  1968. }
  1969. int n_moved_tensors = 0;
  1970. ggml_tensor * first_moved_tensor = nullptr;
  1971. ggml_backend_buffer_type_t first_moved_from_buft = nullptr;
  1972. ggml_backend_buffer_type_t first_moved_to_buft = nullptr;
  1973. auto create_tensor = [&](const LLM_TN_IMPL & tn, const std::initializer_list<int64_t> & ne, int flags) -> ggml_tensor * {
  1974. ggml_tensor * t_meta = ml.get_tensor_meta(tn.str().c_str());
  1975. if (!t_meta) {
  1976. if (flags & TENSOR_NOT_REQUIRED) {
  1977. return nullptr;
  1978. }
  1979. throw std::runtime_error(format("missing tensor '%s'", tn.str().c_str()));
  1980. }
  1981. // some models use the token embedding tensor as the output, but since these are used in different layers and with different ops
  1982. // the tensor is duplicated
  1983. // to handle this, we check if the tensor is duplicated, and if so, we assume that it is being loaded as the output tensor
  1984. llm_tensor tn_tensor = tn.tensor;
  1985. if (tn.tensor == LLM_TENSOR_TOKEN_EMBD && flags & TENSOR_DUPLICATED) {
  1986. tn_tensor = LLM_TENSOR_OUTPUT;
  1987. }
  1988. llm_tensor_info info;
  1989. try {
  1990. info = llm_tensor_info_for(tn_tensor);
  1991. } catch (const std::out_of_range & e) {
  1992. throw std::runtime_error(format("missing tensor info mapping for %s", tn.str().c_str()));
  1993. }
  1994. // skip unused tensors
  1995. if (info.op == GGML_OP_NONE || flags & TENSOR_SKIP) {
  1996. const size_t nbytes = ggml_nbytes(t_meta);
  1997. LLAMA_LOG_WARN("model has unused tensor %s (size = %zu bytes) -- ignoring\n", tn.str().c_str(), nbytes);
  1998. ml.size_data -= nbytes;
  1999. ml.n_created++;
  2000. return nullptr;
  2001. }
  2002. // tensors with "bias" suffix are always used with GGML_OP_ADD or GGML_OP_ADD_ID
  2003. ggml_op op;
  2004. bool bias = tn.suffix != nullptr && strcmp(tn.suffix, "bias") == 0;
  2005. if (bias) {
  2006. if (info.op == GGML_OP_MUL_MAT_ID) {
  2007. op = GGML_OP_ADD_ID;
  2008. } else {
  2009. op = GGML_OP_ADD;
  2010. }
  2011. } else {
  2012. op = info.op;
  2013. }
  2014. // sanity checks
  2015. if (info.layer == LLM_TENSOR_LAYER_INPUT || info.layer == LLM_TENSOR_LAYER_OUTPUT) {
  2016. if (tn.bid != -1) {
  2017. GGML_ABORT("input/output layer tensor %s used with a layer number", tn.str().c_str());
  2018. }
  2019. } else {
  2020. if (tn.bid == -1) {
  2021. GGML_ABORT("repeating layer tensor %s used without a layer number", tn.str().c_str());
  2022. }
  2023. }
  2024. // select the buffer type for this tensor
  2025. buft_list_t * buft_list;
  2026. switch (info.layer) {
  2027. case LLM_TENSOR_LAYER_INPUT:
  2028. buft_list = pimpl->dev_input.buft_list;
  2029. break;
  2030. case LLM_TENSOR_LAYER_OUTPUT:
  2031. buft_list = pimpl->dev_output.buft_list;
  2032. break;
  2033. case LLM_TENSOR_LAYER_REPEATING:
  2034. buft_list = pimpl->dev_layer.at(tn.bid).buft_list;
  2035. break;
  2036. default:
  2037. GGML_ABORT("invalid layer %d for tensor %s", info.layer, tn.str().c_str());
  2038. }
  2039. ggml_backend_buffer_type_t buft = nullptr;
  2040. // check overrides
  2041. if (ml.tensor_buft_overrides) {
  2042. std::string tensor_name = tn.str();
  2043. for (const auto * overrides = ml.tensor_buft_overrides; overrides->pattern != nullptr; ++overrides) {
  2044. std::regex pattern(overrides->pattern);
  2045. if (std::regex_search(tensor_name, pattern)) {
  2046. if (overrides->buft == ggml_backend_cpu_buffer_type()) {
  2047. // when overriding to a CPU buffer, consider the extra buffer types
  2048. buft = select_weight_buft(hparams, t_meta, op, pimpl->cpu_buft_list);
  2049. } else {
  2050. buft = overrides->buft;
  2051. }
  2052. LLAMA_LOG_DEBUG("tensor %s (%zu MiB %s) buffer type overridden to %s\n",
  2053. tensor_name.c_str(),
  2054. ggml_nbytes(t_meta) / 1024 / 1024, ggml_type_name(t_meta->type),
  2055. ggml_backend_buft_name(buft));
  2056. break;
  2057. }
  2058. }
  2059. }
  2060. if (!buft) {
  2061. buft = select_weight_buft(hparams, t_meta, op, *buft_list);
  2062. if (!buft) {
  2063. throw std::runtime_error(format("failed to find a compatible buffer type for tensor %s", tn.str().c_str()));
  2064. }
  2065. }
  2066. // avoid using a host buffer when using mmap
  2067. auto * buft_dev = ggml_backend_buft_get_device(buft);
  2068. if (ml.use_mmap && buft_dev && buft == ggml_backend_dev_host_buffer_type(buft_dev)) {
  2069. auto * cpu_dev = ggml_backend_dev_by_type(GGML_BACKEND_DEVICE_TYPE_CPU);
  2070. if (!cpu_dev) {
  2071. throw std::runtime_error("no CPU backend found");
  2072. }
  2073. buft = ggml_backend_dev_buffer_type(cpu_dev);
  2074. }
  2075. if (buft != buft_list->front().second) {
  2076. n_moved_tensors++;
  2077. if (!first_moved_tensor) {
  2078. first_moved_tensor = t_meta;
  2079. first_moved_from_buft = buft_list->front().second;
  2080. first_moved_to_buft = buft;
  2081. }
  2082. }
  2083. ggml_context * ctx = ctx_for_buft(buft);
  2084. // if duplicated, check if the original tensor was allocated in the same buffer type context and avoid creating a new one
  2085. if (flags & TENSOR_DUPLICATED) {
  2086. ggml_tensor * t = ggml_get_tensor(ctx, tn.str().c_str());
  2087. if (t) {
  2088. return t;
  2089. }
  2090. }
  2091. return ml.create_tensor(ctx, tn, ne, flags);
  2092. };
  2093. layers.resize(n_layer);
  2094. // TODO: move to a separate function
  2095. const auto tn = LLM_TN(arch);
  2096. switch (arch) {
  2097. case LLM_ARCH_LLAMA:
  2098. case LLM_ARCH_REFACT:
  2099. case LLM_ARCH_MINICPM:
  2100. case LLM_ARCH_GRANITE:
  2101. case LLM_ARCH_GRANITE_MOE:
  2102. {
  2103. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2104. // output
  2105. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2106. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  2107. // if output is NULL, init from the input tok embed
  2108. if (output == NULL) {
  2109. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  2110. }
  2111. for (int i = 0; i < n_layer; ++i) {
  2112. auto & layer = layers[i];
  2113. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2114. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  2115. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  2116. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  2117. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  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. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2124. if (hparams.rope_scaling_type_train == LLAMA_ROPE_SCALING_TYPE_LONGROPE) {
  2125. layer.rope_long = create_tensor(tn(LLM_TENSOR_ROPE_FACTORS_LONG, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  2126. layer.rope_short = create_tensor(tn(LLM_TENSOR_ROPE_FACTORS_SHORT, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  2127. }
  2128. else {
  2129. layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  2130. }
  2131. if (n_expert == 0) {
  2132. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  2133. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  2134. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2135. // optional MLP bias
  2136. layer.ffn_gate_b = create_tensor(tn(LLM_TENSOR_FFN_GATE, "bias", i), {n_ff}, TENSOR_NOT_REQUIRED);
  2137. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2138. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, TENSOR_NOT_REQUIRED);
  2139. } else {
  2140. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  2141. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff, n_expert}, TENSOR_NOT_REQUIRED);
  2142. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), { n_ff, n_embd, n_expert}, 0);
  2143. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0);
  2144. // For Granite MoE Shared
  2145. if (hparams.n_ff_shexp > 0) {
  2146. layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), {n_embd, hparams.n_ff_shexp}, 0);
  2147. layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), {n_embd, hparams.n_ff_shexp}, 0);
  2148. layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), {hparams.n_ff_shexp, n_embd}, 0);
  2149. }
  2150. }
  2151. }
  2152. } break;
  2153. case LLM_ARCH_QWEN3NEXT:
  2154. {
  2155. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, 0);
  2156. // output
  2157. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), { n_embd }, 0);
  2158. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), { n_embd, n_vocab }, TENSOR_NOT_REQUIRED);
  2159. // if output is NULL, init from the input tok embed
  2160. if (output == NULL) {
  2161. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, TENSOR_DUPLICATED);
  2162. }
  2163. const int64_t n_ff_exp = hparams.n_ff_exp ? hparams.n_ff_exp : n_ff / n_expert_used;
  2164. // Calculate dimensions from hyperparameters
  2165. const int64_t head_k_dim = hparams.ssm_d_state;
  2166. const int64_t head_v_dim = hparams.ssm_d_state;
  2167. const int64_t n_k_heads = hparams.ssm_n_group;
  2168. const int64_t n_v_heads = hparams.ssm_dt_rank;
  2169. const int64_t key_dim = head_k_dim * n_k_heads;
  2170. const int64_t value_dim = head_v_dim * n_v_heads;
  2171. const int64_t conv_dim = key_dim * 2 + value_dim;
  2172. // Calculate projection sizes
  2173. const int64_t qkvz_projection_size = key_dim * 2 + value_dim * 2;
  2174. const int64_t ba_projection_size = n_v_heads * 2;
  2175. for (int i = 0; i < n_layer; ++i) {
  2176. auto & layer = layers[i];
  2177. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), { n_embd }, 0);
  2178. layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), { n_embd }, 0);
  2179. if ((i + 1) % 4 == 0) { // TODO: magic 4
  2180. // Attention layers
  2181. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), { n_embd, n_ff }, 0);
  2182. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), { n_embd, n_embd_k_gqa }, 0);
  2183. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), { n_embd, n_embd_v_gqa }, 0);
  2184. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), { n_embd_head_k * n_head, n_embd }, 0);
  2185. // Q/K normalization for attention layers
  2186. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), { n_embd_head_k }, 0);
  2187. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), { n_embd_head_k }, 0);
  2188. } else {
  2189. // Linear attention (gated delta net) specific tensors
  2190. // Create tensors with calculated dimensions
  2191. layer.ssm_in = create_tensor(tn(LLM_TENSOR_SSM_IN, "weight", i), { n_embd, qkvz_projection_size }, 0);
  2192. layer.ssm_conv1d = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "weight", i), { hparams.ssm_d_conv, conv_dim }, 0);
  2193. layer.ssm_dt = create_tensor(tn(LLM_TENSOR_SSM_DT, "bias", i), { hparams.ssm_dt_rank }, 0);
  2194. layer.ssm_a = create_tensor(tn(LLM_TENSOR_SSM_A, i), { hparams.ssm_dt_rank }, 0);
  2195. layer.ssm_beta_alpha = create_tensor(tn(LLM_TENSOR_SSM_BETA_ALPHA, "weight", i), { n_embd, ba_projection_size }, 0);
  2196. layer.ssm_norm = create_tensor(tn(LLM_TENSOR_SSM_NORM, "weight", i), { head_v_dim }, 0);
  2197. layer.ssm_out = create_tensor(tn(LLM_TENSOR_SSM_OUT, "weight", i), { n_ff, n_embd }, 0);
  2198. }
  2199. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), { n_embd, n_expert }, 0);
  2200. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert }, 0);
  2201. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), { n_ff_exp, n_embd, n_expert }, 0);
  2202. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert }, 0);
  2203. // Shared experts
  2204. layer.ffn_gate_inp_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP_SHEXP, "weight", i), { n_embd }, 0);
  2205. layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), { n_embd, hparams.n_ff_shexp }, 0);
  2206. layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), { n_embd, hparams.n_ff_shexp }, 0);
  2207. layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), { hparams.n_ff_shexp, n_embd }, 0);
  2208. }
  2209. }
  2210. break;
  2211. case LLM_ARCH_LLADA:
  2212. {
  2213. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, 0);
  2214. // output
  2215. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), { n_embd }, 0);
  2216. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), { n_embd, n_vocab }, TENSOR_NOT_REQUIRED);
  2217. // if output is NULL, init from the input tok embed
  2218. if (output == NULL) {
  2219. output =
  2220. create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, TENSOR_DUPLICATED);
  2221. }
  2222. for (int i = 0; i < n_layer; ++i) {
  2223. auto & layer = layers[i];
  2224. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), { n_embd }, 0);
  2225. // Use separate Q, K, V projections without bias, matching LLaDALlamaBlock
  2226. layer.wq =
  2227. create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), { n_embd, n_embd_head_k * n_head }, 0);
  2228. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), { n_embd, n_embd_k_gqa }, 0);
  2229. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), { n_embd, n_embd_v_gqa }, 0);
  2230. // No bias for QKV projections as per config: include_bias=false, include_qkv_bias=false
  2231. layer.wo =
  2232. create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), { n_embd_head_k * n_head, n_embd }, 0);
  2233. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), { n_embd }, TENSOR_NOT_REQUIRED);
  2234. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), { n_embd }, 0);
  2235. layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), { n_rot / 2 },
  2236. TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  2237. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), { n_embd, n_ff }, 0);
  2238. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd }, 0);
  2239. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), { n_embd, n_ff }, 0);
  2240. // optional MLP bias
  2241. layer.ffn_gate_b =
  2242. create_tensor(tn(LLM_TENSOR_FFN_GATE, "bias", i), { n_ff }, TENSOR_NOT_REQUIRED);
  2243. layer.ffn_down_b =
  2244. create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), { n_embd }, TENSOR_NOT_REQUIRED);
  2245. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), { n_ff }, TENSOR_NOT_REQUIRED);
  2246. }
  2247. }
  2248. break;
  2249. case LLM_ARCH_LLADA_MOE:
  2250. {
  2251. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2252. // output
  2253. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2254. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  2255. GGML_ASSERT(n_expert > 0 && "n_expert must be > 0 for llada-moe");
  2256. GGML_ASSERT(n_expert_used > 0 && "n_expert_used must be > 0 for llada-moe");
  2257. for (int i = 0; i < n_layer; ++i) {
  2258. auto & layer = layers[i];
  2259. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2260. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  2261. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  2262. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  2263. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2264. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0);
  2265. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0);
  2266. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2267. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  2268. const int64_t n_ff_exp = hparams.n_ff_exp ? hparams.n_ff_exp : n_ff / n_expert_used;
  2269. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);
  2270. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp, n_embd, n_expert}, 0);
  2271. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);
  2272. }
  2273. } break;
  2274. case LLM_ARCH_LLAMA4:
  2275. {
  2276. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2277. // output
  2278. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2279. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  2280. // if output is NULL, init from the input tok embed
  2281. if (output == NULL) {
  2282. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  2283. }
  2284. for (int i = 0; i < n_layer; ++i) {
  2285. bool is_moe_layer = hparams.n_moe_layer_step > 0 && (i + 1) % hparams.n_moe_layer_step == 0;
  2286. auto & layer = layers[i];
  2287. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2288. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  2289. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  2290. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  2291. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  2292. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2293. layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  2294. if (is_moe_layer) {
  2295. int n_ff_exp = hparams.n_ff_exp;
  2296. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  2297. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff_exp, n_expert}, 0);
  2298. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), { n_ff_exp, n_embd, n_expert}, 0);
  2299. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff_exp, n_expert}, 0);
  2300. // Shared expert
  2301. const int64_t n_ff_shexp = n_ff_exp;
  2302. layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), { n_embd, n_ff_shexp}, 0);
  2303. layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), {n_ff_shexp, n_embd }, 0);
  2304. layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), { n_embd, n_ff_shexp}, 0);
  2305. } else {
  2306. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  2307. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  2308. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2309. }
  2310. }
  2311. } break;
  2312. case LLM_ARCH_DECI:
  2313. {
  2314. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2315. // output
  2316. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2317. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  2318. // if output is NULL, init from the input tok embed
  2319. if (output == NULL) {
  2320. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  2321. }
  2322. for (int i = 0; i < n_layer; ++i) {
  2323. auto & layer = layers[i];
  2324. const int64_t n_embd_k_gqa = hparams.n_embd_k_gqa(i);
  2325. const int64_t n_embd_v_gqa = hparams.n_embd_v_gqa(i);
  2326. const int64_t n_embd_gqa = hparams.n_embd_v_gqa(i);
  2327. const int64_t n_ff = hparams.n_ff(i);
  2328. const int64_t n_head = hparams.n_head(i);
  2329. const int64_t n_head_kv = hparams.n_head_kv(i);
  2330. if (n_head_kv == 0 && n_head > 0) {
  2331. // linear attention for DeciLMCausalModel
  2332. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2333. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2334. }
  2335. else if (n_head_kv > 0) {
  2336. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2337. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  2338. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  2339. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  2340. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  2341. }
  2342. // optional bias tensors
  2343. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2344. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED);
  2345. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED);
  2346. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2347. if (n_ff > 0) {
  2348. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2349. }
  2350. if (hparams.rope_scaling_type_train == LLAMA_ROPE_SCALING_TYPE_LONGROPE) {
  2351. layer.rope_long = create_tensor(tn(LLM_TENSOR_ROPE_FACTORS_LONG, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  2352. layer.rope_short = create_tensor(tn(LLM_TENSOR_ROPE_FACTORS_SHORT, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  2353. }
  2354. else {
  2355. layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  2356. }
  2357. if (n_ff > 0) {
  2358. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  2359. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  2360. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2361. }
  2362. // optional MLP bias
  2363. layer.ffn_gate_b = create_tensor(tn(LLM_TENSOR_FFN_GATE, "bias", i), {n_ff}, TENSOR_NOT_REQUIRED);
  2364. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2365. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, TENSOR_NOT_REQUIRED);
  2366. }
  2367. } break;
  2368. case LLM_ARCH_MINICPM3:
  2369. {
  2370. const int64_t n_embd_head_qk_rope = hparams.n_rot;
  2371. const int64_t n_embd_head_qk_nope = hparams.n_embd_head_k - hparams.n_rot;
  2372. const int64_t q_lora_rank = hparams.n_lora_q;
  2373. const int64_t kv_lora_rank = hparams.n_lora_kv;
  2374. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2375. // output
  2376. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2377. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  2378. // if output is NULL, init from the input tok embed
  2379. if (output == NULL) {
  2380. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  2381. }
  2382. for (int i = 0; i < n_layer; ++i) {
  2383. auto & layer = layers[i];
  2384. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2385. layer.attn_q_a_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_A_NORM, "weight", i), {q_lora_rank}, 0);
  2386. layer.attn_kv_a_norm = create_tensor(tn(LLM_TENSOR_ATTN_KV_A_NORM, "weight", i), {kv_lora_rank}, 0);
  2387. layer.wq_a = create_tensor(tn(LLM_TENSOR_ATTN_Q_A, "weight", i), {n_embd, q_lora_rank}, 0);
  2388. layer.wq_b = create_tensor(tn(LLM_TENSOR_ATTN_Q_B, "weight", i), {q_lora_rank, n_head * n_embd_head_k}, 0);
  2389. 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);
  2390. 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);
  2391. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), { n_head * ( n_embd_head_v), n_embd}, 0);
  2392. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2393. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  2394. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  2395. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2396. 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));
  2397. 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));
  2398. }
  2399. } break;
  2400. case LLM_ARCH_GROK:
  2401. {
  2402. if (n_expert == 0) {
  2403. throw std::runtime_error("Grok model cannot have zero experts");
  2404. }
  2405. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2406. // output
  2407. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2408. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  2409. // if output is NULL, init from the input tok embed
  2410. if (output == NULL) {
  2411. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  2412. }
  2413. const int64_t n_ff_exp = hparams.n_ff_exp ? hparams.n_ff_exp : n_ff/* / n_expert_used*/; // grok-1 n_ff_exp == n_ff
  2414. for (int i = 0; i < n_layer; ++i) {
  2415. auto & layer = layers[i];
  2416. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2417. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  2418. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  2419. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  2420. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2421. layer.attn_out_norm = create_tensor(tn(LLM_TENSOR_ATTN_OUT_NORM, "weight", i), {n_embd}, 0);
  2422. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2423. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, TENSOR_NOT_REQUIRED);
  2424. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, TENSOR_NOT_REQUIRED);
  2425. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, TENSOR_NOT_REQUIRED);
  2426. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  2427. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff_exp, n_expert}, TENSOR_NOT_REQUIRED);
  2428. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp, n_embd, n_expert}, 0);
  2429. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff_exp, n_expert}, 0);
  2430. layer.ffn_post_norm = create_tensor(tn(LLM_TENSOR_LAYER_OUT_NORM, "weight", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2431. if (!layer.ffn_post_norm) {
  2432. layer.ffn_post_norm = create_tensor(tn(LLM_TENSOR_FFN_POST_NORM, "weight", i), {n_embd}, 0);
  2433. }
  2434. }
  2435. } break;
  2436. case LLM_ARCH_DBRX:
  2437. {
  2438. if (n_expert == 0) {
  2439. throw std::runtime_error("DBRX model cannot have zero experts");
  2440. }
  2441. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2442. // output
  2443. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2444. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  2445. for (int i = 0; i < n_layer; ++i) {
  2446. auto & layer = layers[i];
  2447. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2448. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, 0);
  2449. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2450. layer.attn_out_norm = create_tensor(tn(LLM_TENSOR_ATTN_OUT_NORM, "weight", i), {n_embd}, 0);
  2451. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  2452. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0);
  2453. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff, n_embd, n_expert}, 0);
  2454. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0);
  2455. }
  2456. } break;
  2457. case LLM_ARCH_BAICHUAN:
  2458. {
  2459. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2460. {
  2461. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2462. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  2463. }
  2464. for (int i = 0; i < n_layer; ++i) {
  2465. auto & layer = layers[i];
  2466. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2467. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  2468. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  2469. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  2470. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2471. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2472. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  2473. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  2474. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2475. }
  2476. } break;
  2477. case LLM_ARCH_FALCON:
  2478. {
  2479. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2480. // output
  2481. {
  2482. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2483. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
  2484. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  2485. if (!output) {
  2486. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); // needs to be on GPU
  2487. }
  2488. }
  2489. for (int i = 0; i < n_layer; ++i) {
  2490. auto & layer = layers[i];
  2491. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2492. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0);
  2493. layer.attn_norm_2 = create_tensor(tn(LLM_TENSOR_ATTN_NORM_2, "weight", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2494. layer.attn_norm_2_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM_2, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2495. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, 0);
  2496. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2497. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  2498. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2499. }
  2500. } break;
  2501. case LLM_ARCH_STARCODER:
  2502. {
  2503. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2504. pos_embd = create_tensor(tn(LLM_TENSOR_POS_EMBD, "weight"), {n_embd, n_ctx_train}, 0);
  2505. // output
  2506. {
  2507. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2508. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
  2509. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  2510. if (!output) {
  2511. // needs to be on GPU
  2512. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  2513. }
  2514. }
  2515. for (int i = 0; i < n_layer; ++i) {
  2516. auto & layer = layers[i];
  2517. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2518. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0);
  2519. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, 0);
  2520. layer.bqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd + 2*n_embd_gqa}, 0);
  2521. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2522. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0);
  2523. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2524. layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd}, 0);
  2525. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  2526. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, 0);
  2527. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2528. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, 0);
  2529. }
  2530. } break;
  2531. case LLM_ARCH_BERT:
  2532. case LLM_ARCH_NOMIC_BERT:
  2533. case LLM_ARCH_NOMIC_BERT_MOE:
  2534. case LLM_ARCH_JINA_BERT_V3:
  2535. {
  2536. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2537. type_embd = create_tensor(tn(LLM_TENSOR_TOKEN_TYPES, "weight"), {n_embd, n_token_types}, TENSOR_NOT_REQUIRED);
  2538. if (arch == LLM_ARCH_BERT) {
  2539. pos_embd = create_tensor(tn(LLM_TENSOR_POS_EMBD, "weight"), {n_embd, n_ctx_train}, 0);
  2540. cls = create_tensor(tn(LLM_TENSOR_CLS, "weight"), {n_embd, n_embd}, TENSOR_NOT_REQUIRED);
  2541. cls_b = create_tensor(tn(LLM_TENSOR_CLS, "bias"), {n_embd}, TENSOR_NOT_REQUIRED);
  2542. cls_out = create_tensor(tn(LLM_TENSOR_CLS_OUT, "weight"), {n_embd, hparams.n_cls_out}, TENSOR_NOT_REQUIRED);
  2543. cls_out_b = create_tensor(tn(LLM_TENSOR_CLS_OUT, "bias"), {hparams.n_cls_out}, TENSOR_NOT_REQUIRED);
  2544. }
  2545. tok_norm = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "weight"), {n_embd}, 0);
  2546. tok_norm_b = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "bias"), {n_embd}, 0);
  2547. for (int i = 0; i < n_layer; ++i) {
  2548. auto & layer = layers[i];
  2549. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, TENSOR_NOT_REQUIRED);
  2550. layer.bqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd + 2*n_embd_gqa}, TENSOR_NOT_REQUIRED);
  2551. if (!layer.wqkv) {
  2552. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  2553. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, 0);
  2554. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  2555. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, 0);
  2556. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  2557. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, 0);
  2558. }
  2559. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2560. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2561. layer.attn_out_norm = create_tensor(tn(LLM_TENSOR_ATTN_OUT_NORM, "weight", i), {n_embd}, 0);
  2562. layer.attn_out_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_OUT_NORM, "bias", i), {n_embd}, 0);
  2563. if (hparams.moe_every_n_layers > 0 && i % hparams.moe_every_n_layers == 1) {
  2564. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff, n_expert}, 0);
  2565. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), { n_ff, n_embd, n_expert}, 0);
  2566. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  2567. } else {
  2568. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2569. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, TENSOR_NOT_REQUIRED);
  2570. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  2571. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2572. if (arch == LLM_ARCH_NOMIC_BERT) {
  2573. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  2574. }
  2575. }
  2576. layer.layer_out_norm = create_tensor(tn(LLM_TENSOR_LAYER_OUT_NORM, "weight", i), {n_embd}, 0);
  2577. layer.layer_out_norm_b = create_tensor(tn(LLM_TENSOR_LAYER_OUT_NORM, "bias", i), {n_embd}, 0);
  2578. }
  2579. } break;
  2580. case LLM_ARCH_NEO_BERT:
  2581. {
  2582. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2583. cls = create_tensor(tn(LLM_TENSOR_CLS, "weight"), {n_embd, n_embd}, TENSOR_NOT_REQUIRED);
  2584. cls_b = create_tensor(tn(LLM_TENSOR_CLS, "bias"), {n_embd}, TENSOR_NOT_REQUIRED);
  2585. cls_out = create_tensor(tn(LLM_TENSOR_CLS_OUT, "weight"), {n_embd, hparams.n_cls_out}, TENSOR_NOT_REQUIRED);
  2586. cls_out_b = create_tensor(tn(LLM_TENSOR_CLS_OUT, "bias"), {hparams.n_cls_out}, TENSOR_NOT_REQUIRED);
  2587. output_norm_enc = create_tensor(tn(LLM_TENSOR_ENC_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2588. for (int i = 0; i < n_layer; ++i) {
  2589. auto & layer = layers[i];
  2590. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2591. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, 0);
  2592. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2593. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2594. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff*2}, 0);
  2595. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  2596. }
  2597. } break;
  2598. case LLM_ARCH_JINA_BERT_V2:
  2599. {
  2600. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); // word_embeddings
  2601. type_embd = create_tensor(tn(LLM_TENSOR_TOKEN_TYPES, "weight"), {n_embd, n_token_types}, 0); // token_type_embeddings
  2602. tok_norm = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "weight"), {n_embd}, 0); // LayerNorm
  2603. tok_norm_b = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "bias"), {n_embd}, 0); //LayerNorm bias
  2604. cls = create_tensor(tn(LLM_TENSOR_CLS, "weight"), {n_embd, 1}, TENSOR_NOT_REQUIRED);
  2605. cls_b = create_tensor(tn(LLM_TENSOR_CLS, "bias"), {1}, TENSOR_NOT_REQUIRED);
  2606. for (int i = 0; i < n_layer; ++i) {
  2607. auto & layer = layers[i]; // JinaBertLayer
  2608. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  2609. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, 0);
  2610. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2611. layer.attn_q_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2612. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  2613. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, 0);
  2614. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2615. layer.attn_k_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2616. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  2617. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, 0);
  2618. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0); //output_dens
  2619. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0); //output_dens
  2620. layer.attn_out_norm = create_tensor(tn(LLM_TENSOR_ATTN_OUT_NORM, "weight", i), {n_embd}, 0); //output_norm
  2621. layer.attn_out_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_OUT_NORM, "bias", i), {n_embd}, 0);
  2622. layer.attn_norm_2 = create_tensor(tn(LLM_TENSOR_ATTN_NORM_2, "weight", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2623. layer.attn_norm_2_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM_2, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2624. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, TENSOR_NOT_REQUIRED);
  2625. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, layer.ffn_gate ? n_ff : n_ff * 2}, 0);
  2626. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  2627. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, 0);
  2628. layer.layer_out_norm = create_tensor(tn(LLM_TENSOR_LAYER_OUT_NORM, "weight", i), {n_embd}, 0);
  2629. layer.layer_out_norm_b = create_tensor(tn(LLM_TENSOR_LAYER_OUT_NORM, "bias", i), {n_embd}, 0);
  2630. }
  2631. } break;
  2632. case LLM_ARCH_BLOOM:
  2633. {
  2634. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2635. tok_norm = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "weight"), {n_embd}, 0);
  2636. tok_norm_b = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "bias"), {n_embd}, 0);
  2637. // output
  2638. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2639. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
  2640. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  2641. // if output is NULL, init from the input tok embed
  2642. if (output == NULL) {
  2643. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  2644. }
  2645. for (int i = 0; i < n_layer; ++i) {
  2646. auto & layer = layers[i];
  2647. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2648. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0);
  2649. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, 0);
  2650. layer.bqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd + 2*n_embd_gqa}, 0);
  2651. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2652. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0);
  2653. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2654. layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd}, 0);
  2655. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  2656. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, 0);
  2657. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2658. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, 0);
  2659. }
  2660. } break;
  2661. case LLM_ARCH_MPT:
  2662. {
  2663. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2664. pos_embd = create_tensor(tn(LLM_TENSOR_POS_EMBD, "weight"), {n_embd, n_ctx_train}, TENSOR_NOT_REQUIRED);
  2665. // output
  2666. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2667. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, TENSOR_NOT_REQUIRED);
  2668. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  2669. if (!output) {
  2670. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); // needs to be on GPU
  2671. }
  2672. for (int i = 0; i < n_layer; ++i) {
  2673. auto & layer = layers[i];
  2674. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2675. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2676. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, 0);
  2677. layer.bqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd + 2*n_embd_gqa}, TENSOR_NOT_REQUIRED);
  2678. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2679. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2680. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2681. layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2682. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  2683. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2684. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2685. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, TENSOR_NOT_REQUIRED);
  2686. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2687. layer.attn_q_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2688. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2689. layer.attn_k_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2690. // AWQ ScaleActivation layer
  2691. layer.ffn_act = create_tensor(tn(LLM_TENSOR_FFN_ACT, "scales", i), {n_ff}, TENSOR_NOT_REQUIRED);
  2692. }
  2693. } break;
  2694. case LLM_ARCH_STABLELM:
  2695. {
  2696. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2697. // output
  2698. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
  2699. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2700. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  2701. for (int i = 0; i < n_layer; ++i) {
  2702. auto & layer = layers[i];
  2703. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2704. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0);
  2705. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  2706. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  2707. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  2708. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2709. // optional bias tensors, present in Stable LM 2 1.6B
  2710. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2711. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED);
  2712. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED);
  2713. // optional q and k layernorms, present in StableLM 2 12B
  2714. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k, n_head}, TENSOR_NOT_REQUIRED);
  2715. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k, n_head_kv}, TENSOR_NOT_REQUIRED);
  2716. // optional FFN norm, not present in StableLM 2 12B which uses parallel residual
  2717. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2718. layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2719. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  2720. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  2721. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2722. }
  2723. } break;
  2724. case LLM_ARCH_QWEN:
  2725. {
  2726. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2727. // output
  2728. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2729. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  2730. for (int i = 0; i < n_layer; ++i) {
  2731. auto & layer = layers[i];
  2732. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2733. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd*3}, 0);
  2734. layer.bqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd*3}, 0);
  2735. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2736. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2737. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff/2}, 0);
  2738. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff/2, n_embd}, 0);
  2739. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff/2}, 0);
  2740. }
  2741. } break;
  2742. case LLM_ARCH_QWEN2:
  2743. case LLM_ARCH_QWEN2VL:
  2744. case LLM_ARCH_DREAM:
  2745. {
  2746. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2747. // output
  2748. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2749. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  2750. output_b = create_tensor(tn(LLM_TENSOR_OUTPUT, "bias"), {n_vocab}, TENSOR_NOT_REQUIRED);
  2751. // if output is NULL, init from the input tok embed
  2752. if (output == NULL) {
  2753. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  2754. }
  2755. for (int i = 0; i < n_layer; ++i) {
  2756. auto & layer = layers[i];
  2757. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2758. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  2759. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  2760. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  2761. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2762. // optional bias tensors
  2763. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, 0);
  2764. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, 0);
  2765. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, 0);
  2766. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2767. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  2768. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  2769. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2770. }
  2771. } break;
  2772. case LLM_ARCH_QWEN2MOE:
  2773. {
  2774. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2775. // output
  2776. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2777. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  2778. for (int i = 0; i < n_layer; ++i) {
  2779. auto & layer = layers[i];
  2780. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2781. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  2782. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  2783. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  2784. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2785. // optional bias tensors
  2786. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2787. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED);
  2788. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED);
  2789. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2790. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  2791. if (n_expert == 0) {
  2792. throw std::runtime_error("n_expert must be > 0 for QWEN2MOE");
  2793. }
  2794. if (n_expert_used == 0) {
  2795. throw std::runtime_error("n_expert_used must be > 0 for QWEN2MOE");
  2796. }
  2797. // MoE branch
  2798. const int64_t n_ff_exp = hparams.n_ff_exp ? hparams.n_ff_exp : n_ff / n_expert_used;
  2799. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);
  2800. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp, n_embd, n_expert}, 0);
  2801. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);
  2802. // Shared expert branch
  2803. const int64_t n_ff_shexp = hparams.n_ff_shexp ? hparams.n_ff_shexp : n_ff;
  2804. layer.ffn_gate_inp_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP_SHEXP, "weight", i), {n_embd}, 0);
  2805. layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), { n_embd, n_ff_shexp}, 0);
  2806. layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), {n_ff_shexp, n_embd}, 0);
  2807. layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), { n_embd, n_ff_shexp}, 0);
  2808. }
  2809. } break;
  2810. case LLM_ARCH_QWEN3:
  2811. {
  2812. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2813. // output
  2814. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2815. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  2816. // if output is NULL, init from the input tok embed
  2817. if (output == NULL) {
  2818. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  2819. }
  2820. for (int i = 0; i < n_layer; ++i) {
  2821. auto & layer = layers[i];
  2822. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2823. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  2824. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  2825. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  2826. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  2827. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0);
  2828. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0);
  2829. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2830. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  2831. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  2832. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2833. }
  2834. } break;
  2835. case LLM_ARCH_QWEN3MOE:
  2836. {
  2837. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2838. // output
  2839. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2840. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  2841. // if output is NULL, init from the input tok embed
  2842. if (output == NULL) {
  2843. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  2844. }
  2845. for (int i = 0; i < n_layer; ++i) {
  2846. auto & layer = layers[i];
  2847. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2848. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  2849. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  2850. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  2851. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  2852. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0);
  2853. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0);
  2854. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2855. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  2856. if (n_expert == 0) {
  2857. throw std::runtime_error("n_expert must be > 0 for QWEN3MOE");
  2858. }
  2859. if (n_expert_used == 0) {
  2860. throw std::runtime_error("n_expert_used must be > 0 for QWEN3MOE");
  2861. }
  2862. // MoE branch
  2863. const int64_t n_ff_exp = hparams.n_ff_exp ? hparams.n_ff_exp : n_ff / n_expert_used;
  2864. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);
  2865. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp, n_embd, n_expert}, 0);
  2866. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);
  2867. }
  2868. } break;
  2869. case LLM_ARCH_PHI2:
  2870. {
  2871. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2872. // output
  2873. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2874. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
  2875. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  2876. output_b = create_tensor(tn(LLM_TENSOR_OUTPUT, "bias"), {n_vocab}, 0);
  2877. for (int i = 0; i < n_layer; ++i) {
  2878. auto & layer = layers[i];
  2879. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2880. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0);
  2881. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, TENSOR_NOT_REQUIRED);
  2882. layer.bqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd + 2*n_embd_gqa}, TENSOR_NOT_REQUIRED);
  2883. if (layer.wqkv == nullptr) {
  2884. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  2885. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, 0);
  2886. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  2887. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, 0);
  2888. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  2889. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, 0);
  2890. }
  2891. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2892. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0);
  2893. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  2894. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, 0);
  2895. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2896. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, 0);
  2897. }
  2898. } break;
  2899. case LLM_ARCH_PHI3:
  2900. {
  2901. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, 0);
  2902. // output
  2903. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), { n_embd }, 0);
  2904. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  2905. // if output is NULL, init from the input tok embed
  2906. if (output == NULL) {
  2907. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  2908. }
  2909. for (int i = 0; i < n_layer; ++i) {
  2910. auto & layer = layers[i];
  2911. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), { n_embd }, 0);
  2912. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), { n_embd, n_embd + 2 * n_embd_gqa }, TENSOR_NOT_REQUIRED);
  2913. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), { n_embd, n_embd }, 0);
  2914. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), { n_embd }, 0);
  2915. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd }, 0);
  2916. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), { n_embd, 2 * n_ff }, 0);
  2917. layer.rope_long = create_tensor(tn(LLM_TENSOR_ROPE_FACTORS_LONG, "weight", i), { n_rot/2 }, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  2918. layer.rope_short = create_tensor(tn(LLM_TENSOR_ROPE_FACTORS_SHORT, "weight", i), { n_rot/2 }, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  2919. }
  2920. } break;
  2921. case LLM_ARCH_PHIMOE:
  2922. {
  2923. const int64_t n_embd_head = n_embd / n_head;
  2924. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, 0);
  2925. // output
  2926. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), { n_embd }, 0);
  2927. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
  2928. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), { n_embd, n_vocab }, 0);
  2929. output_b = create_tensor(tn(LLM_TENSOR_OUTPUT, "bias"), { n_vocab }, 0);
  2930. for (int i = 0; i < n_layer; ++i) {
  2931. auto & layer = layers[i];
  2932. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), { n_embd }, 0);
  2933. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), { n_embd }, 0);
  2934. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), { n_embd, n_embd + 2 * n_embd_gqa }, TENSOR_NOT_REQUIRED);
  2935. if (layer.wqkv == nullptr) {
  2936. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  2937. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, 0);
  2938. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  2939. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, 0);
  2940. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  2941. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, 0);
  2942. }
  2943. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), { n_embd, n_embd }, 0);
  2944. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), { n_embd }, 0);
  2945. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), { n_embd }, 0);
  2946. layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), { n_embd }, 0);
  2947. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  2948. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0);
  2949. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff, n_embd, n_expert}, 0);
  2950. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0);
  2951. 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));
  2952. 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));
  2953. }
  2954. } break;
  2955. case LLM_ARCH_PLAMO:
  2956. {
  2957. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2958. // output
  2959. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2960. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  2961. for (int i = 0; i < n_layer; ++i) {
  2962. auto & layer = layers[i];
  2963. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2964. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  2965. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  2966. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  2967. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2968. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  2969. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  2970. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2971. }
  2972. } break;
  2973. case LLM_ARCH_PLAMO2:
  2974. {
  2975. const uint32_t d_conv = hparams.ssm_d_conv;
  2976. const uint32_t d_state = hparams.ssm_d_state;
  2977. const uint32_t num_heads = hparams.ssm_dt_rank;
  2978. const uint32_t intermediate_size = hparams.ssm_d_inner;
  2979. const uint32_t head_dim = intermediate_size / num_heads;
  2980. const uint32_t qk_dim = head_dim;
  2981. const uint32_t v_dim = head_dim;
  2982. const int64_t num_attention_heads = hparams.n_head();
  2983. const int64_t q_num_heads = num_attention_heads;
  2984. const int64_t dt_dim = std::max(64, int(hparams.n_embd / 16));
  2985. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2986. // output
  2987. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2988. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  2989. // if output is NULL, init from the input tok embed
  2990. if (output == NULL) {
  2991. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  2992. }
  2993. for (int i = 0; i < n_layer; ++i) {
  2994. auto & layer = layers[i];
  2995. bool is_mamba_layer = hparams.is_recurrent(i);
  2996. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2997. if (is_mamba_layer) {
  2998. layer.ssm_in = create_tensor(tn(LLM_TENSOR_SSM_IN, "weight", i), {n_embd, 2 * intermediate_size}, 0);
  2999. layer.ssm_conv1d = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "weight", i), {d_conv, intermediate_size}, 0);
  3000. layer.ssm_x = create_tensor(tn(LLM_TENSOR_SSM_X, "weight", i), {intermediate_size, dt_dim + 2*d_state}, 0);
  3001. layer.ssm_dt = create_tensor(tn(LLM_TENSOR_SSM_DT, "weight", i), {dt_dim, num_heads}, 0);
  3002. layer.ssm_dt_b = create_tensor(tn(LLM_TENSOR_SSM_DT, "bias", i), {num_heads}, 0);
  3003. layer.ssm_a = create_tensor(tn(LLM_TENSOR_SSM_A, i), {num_heads}, 0);
  3004. layer.ssm_d = create_tensor(tn(LLM_TENSOR_SSM_D, i), {num_heads}, 0);
  3005. layer.ssm_out = create_tensor(tn(LLM_TENSOR_SSM_OUT, "weight", i), {intermediate_size, n_embd}, 0);
  3006. layer.ssm_dt_norm = create_tensor(tn(LLM_TENSOR_SSM_DT_NORM, i), {dt_dim}, 0);
  3007. layer.ssm_b_norm = create_tensor(tn(LLM_TENSOR_SSM_B_NORM, i), {d_state}, 0);
  3008. layer.ssm_c_norm = create_tensor(tn(LLM_TENSOR_SSM_C_NORM, i), {d_state}, 0);
  3009. } else {
  3010. const int64_t num_key_value_heads = hparams.n_head_kv(i);
  3011. const int64_t k_num_heads = num_key_value_heads;
  3012. const int64_t v_num_heads = num_key_value_heads;
  3013. const int64_t q_proj_dim = q_num_heads * qk_dim;
  3014. const int64_t k_proj_dim = k_num_heads * qk_dim;
  3015. const int64_t v_proj_dim = v_num_heads * v_dim;
  3016. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, q_proj_dim + k_proj_dim + v_proj_dim}, 0);
  3017. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {head_dim, num_attention_heads}, 0);
  3018. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {head_dim, k_num_heads}, 0);
  3019. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {q_num_heads * v_dim, n_embd}, 0);
  3020. }
  3021. // All layers have post-attention norm, FFN norm, and FFN tensors
  3022. layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, i), {n_embd}, 0);
  3023. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3024. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  3025. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff * 2}, 0);
  3026. layer.ffn_post_norm = create_tensor(tn(LLM_TENSOR_FFN_POST_NORM, i), {n_embd}, 0);
  3027. }
  3028. } break;
  3029. case LLM_ARCH_GPT2:
  3030. {
  3031. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3032. pos_embd = create_tensor(tn(LLM_TENSOR_POS_EMBD, "weight"), {n_embd, n_ctx_train}, 0);
  3033. // output
  3034. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3035. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
  3036. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  3037. // if output is NULL, init from the input tok embed
  3038. if (output == NULL) {
  3039. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3040. }
  3041. for (int i = 0; i < n_layer; ++i) {
  3042. auto & layer = layers[i];
  3043. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3044. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0);
  3045. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, 0);
  3046. layer.bqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd + 2*n_embd_gqa}, 0);
  3047. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  3048. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0);
  3049. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3050. layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd}, 0);
  3051. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  3052. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, 0);
  3053. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3054. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, 0);
  3055. }
  3056. } break;
  3057. case LLM_ARCH_CODESHELL:
  3058. {
  3059. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  3060. // if tok embd is NULL, init from output
  3061. if (tok_embd == NULL) {
  3062. tok_embd = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3063. }
  3064. // output
  3065. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3066. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
  3067. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  3068. for (int i = 0; i < n_layer; ++i) {
  3069. auto & layer = layers[i];
  3070. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3071. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0);
  3072. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, 0);
  3073. layer.bqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd + 2*n_embd_gqa}, 0);
  3074. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  3075. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0);
  3076. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3077. layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd}, 0);
  3078. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  3079. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, 0);
  3080. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3081. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, 0);
  3082. }
  3083. } break;
  3084. case LLM_ARCH_ORION:
  3085. {
  3086. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3087. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3088. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
  3089. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  3090. for (int i = 0; i < n_layer; ++i) {
  3091. auto & layer = layers[i];
  3092. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3093. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0);
  3094. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  3095. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  3096. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  3097. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  3098. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3099. layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd}, 0);
  3100. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  3101. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3102. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3103. }
  3104. } break;
  3105. case LLM_ARCH_INTERNLM2:
  3106. {
  3107. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3108. // output
  3109. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3110. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  3111. for (int i = 0; i < n_layer; ++i) {
  3112. auto & layer = layers[i];
  3113. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3114. // layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, 0);
  3115. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  3116. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  3117. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  3118. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  3119. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3120. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  3121. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3122. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3123. }
  3124. } break;
  3125. case LLM_ARCH_GEMMA:
  3126. {
  3127. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3128. // output
  3129. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3130. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); // same as tok_embd, duplicated to allow offloading
  3131. for (int i = 0; i < n_layer; ++i) {
  3132. auto & layer = layers[i];
  3133. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3134. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  3135. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  3136. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  3137. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  3138. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3139. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  3140. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3141. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3142. }
  3143. } break;
  3144. case LLM_ARCH_GEMMA2:
  3145. {
  3146. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3147. // output
  3148. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3149. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); // same as tok_embd, duplicated to allow offloading
  3150. for (int i = 0; i < n_layer; ++i) {
  3151. auto & layer = layers[i];
  3152. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3153. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  3154. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  3155. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  3156. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  3157. layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), {n_embd}, 0);
  3158. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3159. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  3160. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3161. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3162. layer.ffn_post_norm = create_tensor(tn(LLM_TENSOR_FFN_POST_NORM, "weight", i), {n_embd}, 0);
  3163. }
  3164. } break;
  3165. case LLM_ARCH_GEMMA3:
  3166. case LLM_ARCH_GEMMA_EMBEDDING:
  3167. {
  3168. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3169. // output
  3170. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3171. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  3172. // if output is NULL, init from the input tok embed
  3173. if (output == NULL) {
  3174. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3175. }
  3176. for (int i = 0; i < n_layer; ++i) {
  3177. auto & layer = layers[i];
  3178. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3179. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  3180. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  3181. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  3182. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  3183. layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), {n_embd}, 0);
  3184. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0);
  3185. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0);
  3186. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3187. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  3188. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3189. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3190. layer.ffn_post_norm = create_tensor(tn(LLM_TENSOR_FFN_POST_NORM, "weight", i), {n_embd}, 0);
  3191. }
  3192. } break;
  3193. case LLM_ARCH_GEMMA3N:
  3194. {
  3195. const int64_t n_altup = hparams.n_altup;
  3196. const int64_t laurel_rank = hparams.laurel_rank;
  3197. const int64_t n_embd_altup = hparams.n_embd_altup;
  3198. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  3199. // if output is NULL, init from the input tok embed
  3200. if (output == NULL) {
  3201. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3202. }
  3203. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3204. tok_embd_per_layer = create_tensor(tn(LLM_TENSOR_PER_LAYER_TOKEN_EMBD, "weight"), {n_embd_altup * n_layer, n_vocab}, 0);
  3205. altup_proj = create_tensor(tn(LLM_TENSOR_ALTUP_PROJ, "weight"), {n_embd, n_embd, n_altup - 1}, 0);
  3206. altup_unembd_proj = create_tensor(tn(LLM_TENSOR_ALTUP_UNEMBD_PROJ, "weight"), {n_embd, n_embd, n_altup - 1}, 0);
  3207. per_layer_model_proj = create_tensor(tn(LLM_TENSOR_PER_LAYER_MODEL_PROJ, "weight"), {n_embd, n_embd_altup * n_layer}, 0);
  3208. per_layer_proj_norm = create_tensor(tn(LLM_TENSOR_PER_LAYER_PROJ_NORM, "weight"), {n_embd_altup}, 0);
  3209. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3210. for (int i = 0; i < n_layer; ++i) {
  3211. auto & layer = layers[i];
  3212. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3213. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  3214. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  3215. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  3216. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  3217. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0);
  3218. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0);
  3219. layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), {n_embd}, 0);
  3220. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3221. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  3222. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3223. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3224. layer.ffn_post_norm = create_tensor(tn(LLM_TENSOR_FFN_POST_NORM, "weight", i), {n_embd}, 0);
  3225. // altup & laurel
  3226. layer.per_layer_inp_gate = create_tensor(tn(LLM_TENSOR_PER_LAYER_INP_GATE, "weight", i), {n_embd, n_embd_altup}, 0);
  3227. layer.per_layer_proj = create_tensor(tn(LLM_TENSOR_PER_LAYER_PROJ, "weight", i), {n_embd_altup, n_embd}, 0);
  3228. layer.per_layer_post_norm = create_tensor(tn(LLM_TENSOR_PER_LAYER_POST_NORM, "weight", i), {n_embd}, 0);
  3229. layer.altup_correct_coef = create_tensor(tn(LLM_TENSOR_ALTUP_CORRECT_COEF, "weight", i), {n_altup, n_altup}, 0);
  3230. layer.altup_correct_scale = create_tensor(tn(LLM_TENSOR_ALTUP_CORRECT_SCALE, "weight", i), {n_embd}, 0);
  3231. layer.altup_predict_coef = create_tensor(tn(LLM_TENSOR_ALTUP_PREDICT_COEF, "weight", i), {n_altup, n_altup * n_altup}, 0);
  3232. layer.altup_router = create_tensor(tn(LLM_TENSOR_ALTUP_ROUTER, "weight", i), {n_embd, n_altup}, 0);
  3233. layer.altup_router_norm = create_tensor(tn(LLM_TENSOR_ALTUP_ROUTER_NORM, "weight", i), {n_embd}, 0);
  3234. layer.laurel_l = create_tensor(tn(LLM_TENSOR_LAUREL_L, "weight", i), {n_embd, laurel_rank}, 0);
  3235. layer.laurel_r = create_tensor(tn(LLM_TENSOR_LAUREL_R, "weight", i), {laurel_rank, n_embd}, 0);
  3236. layer.laurel_post_norm = create_tensor(tn(LLM_TENSOR_LAUREL_POST_NORM, "weight", i), {n_embd}, 0);
  3237. }
  3238. } break;
  3239. case LLM_ARCH_STARCODER2:
  3240. {
  3241. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3242. // output
  3243. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3244. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
  3245. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  3246. // if output is NULL, init from the input tok embed
  3247. if (output == NULL) {
  3248. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3249. }
  3250. for (int i = 0; i < n_layer; ++i) {
  3251. auto & layer = layers[i];
  3252. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3253. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0);
  3254. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  3255. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  3256. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  3257. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  3258. // optional bias tensors
  3259. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, 0);
  3260. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, 0);
  3261. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, 0);
  3262. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0);
  3263. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3264. layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd}, 0);
  3265. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3266. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3267. // optional bias tensors
  3268. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, 0);
  3269. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP , "bias", i), { n_ff}, 0);
  3270. }
  3271. } break;
  3272. case LLM_ARCH_MAMBA:
  3273. {
  3274. const int64_t d_conv = hparams.ssm_d_conv;
  3275. const int64_t d_inner = hparams.ssm_d_inner;
  3276. const int64_t d_state = hparams.ssm_d_state;
  3277. const int64_t dt_rank = hparams.ssm_dt_rank;
  3278. // only an expansion factor of 2 is supported for now
  3279. if (2 * n_embd != d_inner) {
  3280. throw std::runtime_error("only an expansion factor of 2 is supported for now");
  3281. }
  3282. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3283. // output
  3284. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3285. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  3286. // if output is NULL, init from the input tok embed, duplicated to allow offloading
  3287. if (output == NULL) {
  3288. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3289. }
  3290. for (int i = 0; i < n_layer; ++i) {
  3291. auto & layer = layers[i];
  3292. // norm
  3293. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3294. layer.ssm_in = create_tensor(tn(LLM_TENSOR_SSM_IN, "weight", i), {n_embd, 2*d_inner}, 0);
  3295. layer.ssm_conv1d = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "weight", i), {d_conv, d_inner}, 0);
  3296. layer.ssm_conv1d_b = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "bias", i), {d_inner}, 0);
  3297. layer.ssm_x = create_tensor(tn(LLM_TENSOR_SSM_X, "weight", i), {d_inner, dt_rank + 2*d_state}, 0);
  3298. layer.ssm_dt = create_tensor(tn(LLM_TENSOR_SSM_DT, "weight", i), {dt_rank, d_inner}, 0);
  3299. layer.ssm_dt_b = create_tensor(tn(LLM_TENSOR_SSM_DT, "bias", i), {d_inner}, 0);
  3300. // no "weight" suffix for these
  3301. layer.ssm_a = create_tensor(tn(LLM_TENSOR_SSM_A, i), {d_state, d_inner}, 0);
  3302. layer.ssm_d = create_tensor(tn(LLM_TENSOR_SSM_D, i), {d_inner}, 0);
  3303. // out_proj
  3304. layer.ssm_out = create_tensor(tn(LLM_TENSOR_SSM_OUT, "weight", i), {d_inner, n_embd}, 0);
  3305. }
  3306. } break;
  3307. case LLM_ARCH_MAMBA2:
  3308. {
  3309. const int64_t d_conv = hparams.ssm_d_conv;
  3310. const int64_t d_inner = hparams.ssm_d_inner;
  3311. const int64_t d_state = hparams.ssm_d_state;
  3312. const int64_t n_head = hparams.ssm_dt_rank;
  3313. const int64_t n_group = hparams.ssm_n_group;
  3314. const int64_t d_in_proj = 2*d_inner + 2*n_group*d_state + n_head;
  3315. // only an expansion factor of 2 is supported for now
  3316. GGML_ASSERT(2 * n_embd == d_inner);
  3317. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3318. // output
  3319. {
  3320. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3321. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  3322. // if output is NULL, init from the input tok embed, duplicated to allow offloading
  3323. if (output == NULL) {
  3324. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3325. }
  3326. }
  3327. for (int i = 0; i < n_layer; ++i) {
  3328. auto & layer = layers[i];
  3329. // norm
  3330. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3331. layer.ssm_in = create_tensor(tn(LLM_TENSOR_SSM_IN, "weight", i), {n_embd, d_in_proj}, 0);
  3332. layer.ssm_conv1d = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "weight", i), {d_conv, d_inner + 2*n_group*d_state}, 0);
  3333. layer.ssm_conv1d_b = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "bias", i), {d_inner + 2*n_group*d_state}, 0);
  3334. layer.ssm_dt_b = create_tensor(tn(LLM_TENSOR_SSM_DT, "bias", i), {n_head}, 0);
  3335. // no "weight" suffix for these
  3336. layer.ssm_a = create_tensor(tn(LLM_TENSOR_SSM_A, i), {1, n_head}, 0);
  3337. layer.ssm_d = create_tensor(tn(LLM_TENSOR_SSM_D, i), {1, n_head}, 0);
  3338. layer.ssm_norm = create_tensor(tn(LLM_TENSOR_SSM_NORM, "weight", i), {d_inner / n_group, n_group}, 0);
  3339. // out_proj
  3340. layer.ssm_out = create_tensor(tn(LLM_TENSOR_SSM_OUT, "weight", i), {d_inner, n_embd}, 0);
  3341. }
  3342. } break;
  3343. case LLM_ARCH_JAMBA:
  3344. {
  3345. const int64_t d_conv = hparams.ssm_d_conv;
  3346. const int64_t d_inner = hparams.ssm_d_inner;
  3347. const int64_t d_state = hparams.ssm_d_state;
  3348. const int64_t dt_rank = hparams.ssm_dt_rank;
  3349. // only an expansion factor of 2 is supported for now
  3350. GGML_ASSERT(2 * n_embd == d_inner);
  3351. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3352. // output
  3353. {
  3354. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3355. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  3356. // if output is NULL, init from the input tok embed, duplicated to allow offloading
  3357. if (output == NULL) {
  3358. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3359. }
  3360. }
  3361. for (int i = 0; i < n_layer; ++i) {
  3362. const int64_t n_head_kv = hparams.n_head_kv(i);
  3363. const int64_t n_embd_gqa = hparams.n_embd_v_gqa(i);
  3364. auto & layer = layers[i];
  3365. // norm
  3366. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3367. if (n_head_kv == 0) {
  3368. // Mamba layer
  3369. layer.ssm_in = create_tensor(tn(LLM_TENSOR_SSM_IN, "weight", i), {n_embd, 2*d_inner}, 0);
  3370. layer.ssm_conv1d = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "weight", i), {d_conv, d_inner}, 0);
  3371. layer.ssm_conv1d_b = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "bias", i), {d_inner}, 0);
  3372. layer.ssm_x = create_tensor(tn(LLM_TENSOR_SSM_X, "weight", i), {d_inner, dt_rank + 2*d_state}, 0);
  3373. layer.ssm_dt_norm = create_tensor(tn(LLM_TENSOR_SSM_DT_NORM, "weight", i), {dt_rank}, 0);
  3374. layer.ssm_dt = create_tensor(tn(LLM_TENSOR_SSM_DT, "weight", i), {dt_rank, d_inner}, 0);
  3375. layer.ssm_dt_b = create_tensor(tn(LLM_TENSOR_SSM_DT, "bias", i), {d_inner}, 0);
  3376. layer.ssm_b_norm = create_tensor(tn(LLM_TENSOR_SSM_B_NORM, "weight", i), {d_state}, 0);
  3377. layer.ssm_c_norm = create_tensor(tn(LLM_TENSOR_SSM_C_NORM, "weight", i), {d_state}, 0);
  3378. // no "weight" suffix for these
  3379. layer.ssm_a = create_tensor(tn(LLM_TENSOR_SSM_A, i), {d_state, d_inner}, 0);
  3380. layer.ssm_d = create_tensor(tn(LLM_TENSOR_SSM_D, i), {d_inner}, 0);
  3381. // out_proj
  3382. layer.ssm_out = create_tensor(tn(LLM_TENSOR_SSM_OUT, "weight", i), {d_inner, n_embd}, 0);
  3383. } else {
  3384. // Attention layers
  3385. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  3386. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  3387. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  3388. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  3389. }
  3390. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3391. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, TENSOR_NOT_REQUIRED);
  3392. if (layer.ffn_gate_inp) {
  3393. // MoE
  3394. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0);
  3395. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff, n_embd, n_expert}, 0);
  3396. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0);
  3397. } else {
  3398. // FFN (no MoE)
  3399. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  3400. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  3401. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3402. }
  3403. }
  3404. } break;
  3405. case LLM_ARCH_GRANITE_HYBRID:
  3406. {
  3407. // mamba2 Mixer SSM params
  3408. // NOTE: int64_t for tensor dimensions
  3409. const int64_t d_conv = hparams.ssm_d_conv;
  3410. const int64_t d_inner = hparams.ssm_d_inner;
  3411. const int64_t d_state = hparams.ssm_d_state;
  3412. const int64_t n_ssm_head = hparams.ssm_dt_rank;
  3413. const int64_t n_group = hparams.ssm_n_group;
  3414. const int64_t d_in_proj = 2*d_inner + 2*n_group*d_state + n_ssm_head;
  3415. // only an expansion factor of 2 is supported for now
  3416. GGML_ASSERT(2 * n_embd == d_inner);
  3417. // embeddings
  3418. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3419. // output
  3420. {
  3421. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3422. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  3423. // if output is NULL, init from the input tok embed, duplicated to allow offloading
  3424. if (output == NULL) {
  3425. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3426. }
  3427. }
  3428. for (int i = 0; i < n_layer; ++i) {
  3429. auto & layer = layers[i];
  3430. // norm
  3431. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3432. if (hparams.is_recurrent(i)) {
  3433. // ssm layers
  3434. layer.ssm_in = create_tensor(tn(LLM_TENSOR_SSM_IN, "weight", i), {n_embd, d_in_proj}, 0);
  3435. layer.ssm_conv1d = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "weight", i), {d_conv, d_inner + 2*n_group*d_state}, 0);
  3436. layer.ssm_conv1d_b = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "bias", i), {d_inner + 2*n_group*d_state}, TENSOR_NOT_REQUIRED);
  3437. layer.ssm_dt_b = create_tensor(tn(LLM_TENSOR_SSM_DT, "bias", i), {n_ssm_head}, 0);
  3438. // no "weight" suffix for these
  3439. layer.ssm_a = create_tensor(tn(LLM_TENSOR_SSM_A, i), {1, n_ssm_head}, 0);
  3440. layer.ssm_d = create_tensor(tn(LLM_TENSOR_SSM_D, i), {1, n_ssm_head}, 0);
  3441. layer.ssm_norm = create_tensor(tn(LLM_TENSOR_SSM_NORM, "weight", i), {d_inner / n_group, n_group}, 0);
  3442. // out_proj
  3443. layer.ssm_out = create_tensor(tn(LLM_TENSOR_SSM_OUT, "weight", i), {d_inner, n_embd}, 0);
  3444. } else {
  3445. // attention layers (with optional bias)
  3446. const int64_t n_head_i = hparams.n_head(i);
  3447. const int64_t n_embd_k_gqa_i = hparams.n_embd_k_gqa(i);
  3448. const int64_t n_embd_v_gqa_i = hparams.n_embd_v_gqa(i);
  3449. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head_i}, 0);
  3450. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa_i}, 0);
  3451. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa_i}, 0);
  3452. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head_i, n_embd}, 0);
  3453. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  3454. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_k_gqa_i}, TENSOR_NOT_REQUIRED);
  3455. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_v_gqa_i}, TENSOR_NOT_REQUIRED);
  3456. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  3457. }
  3458. // feed forward (w/ optional biases)
  3459. if (n_expert > 0) {
  3460. // MoE FFN
  3461. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3462. layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  3463. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  3464. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff, n_expert}, TENSOR_NOT_REQUIRED);
  3465. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), { n_ff, n_embd, n_expert}, 0);
  3466. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0);
  3467. // For Granite MoE Shared
  3468. if (hparams.n_ff_shexp > 0) {
  3469. layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), {n_embd, hparams.n_ff_shexp}, 0);
  3470. layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), {n_embd, hparams.n_ff_shexp}, 0);
  3471. layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), {hparams.n_ff_shexp, n_embd}, 0);
  3472. }
  3473. } else {
  3474. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3475. layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  3476. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  3477. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3478. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3479. layer.ffn_gate_b = create_tensor(tn(LLM_TENSOR_FFN_GATE, "bias", i), {n_ff}, TENSOR_NOT_REQUIRED);
  3480. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  3481. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, TENSOR_NOT_REQUIRED);
  3482. }
  3483. }
  3484. } break;
  3485. case LLM_ARCH_XVERSE:
  3486. {
  3487. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3488. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3489. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  3490. for (int i = 0; i < n_layer; ++i) {
  3491. auto & layer = layers[i];
  3492. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3493. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  3494. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  3495. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  3496. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  3497. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3498. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  3499. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3500. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3501. }
  3502. } break;
  3503. case LLM_ARCH_COMMAND_R:
  3504. {
  3505. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3506. // output
  3507. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3508. // init output from the input tok embed
  3509. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3510. for (int i = 0; i < n_layer; ++i) {
  3511. auto & layer = layers[i];
  3512. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3513. if (n_layer >= 64){
  3514. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k, n_head}, 0);
  3515. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k, n_head_kv}, 0);
  3516. }
  3517. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  3518. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  3519. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  3520. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  3521. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  3522. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3523. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3524. }
  3525. } break;
  3526. case LLM_ARCH_COHERE2:
  3527. {
  3528. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, 0);
  3529. // output
  3530. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), { n_embd }, 0);
  3531. // init output from the input tok embed
  3532. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab },
  3533. TENSOR_DUPLICATED);
  3534. for (int i = 0; i < n_layer; ++i) {
  3535. auto & layer = layers[i];
  3536. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), { n_embd }, 0);
  3537. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), { n_embd, n_embd }, 0);
  3538. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), { n_embd, n_embd_gqa }, 0);
  3539. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), { n_embd, n_embd_gqa }, 0);
  3540. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), { n_embd, n_embd }, 0);
  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. }
  3545. }
  3546. break;
  3547. case LLM_ARCH_OLMO: // adapted from LLM_ARCH_LLAMA with norm params removed
  3548. {
  3549. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3550. // output
  3551. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  3552. // if output is NULL, init from the input tok embed
  3553. if (output == NULL) {
  3554. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3555. }
  3556. for (int i = 0; i < n_layer; ++i) {
  3557. auto & layer = layers[i];
  3558. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  3559. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  3560. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  3561. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  3562. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  3563. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3564. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3565. }
  3566. } break;
  3567. case LLM_ARCH_OLMO2:
  3568. {
  3569. const int64_t n_embd_head = n_embd / n_head;
  3570. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3571. // output
  3572. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3573. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  3574. for (int i = 0; i < n_layer; ++i) {
  3575. auto & layer = layers[i];
  3576. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  3577. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  3578. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  3579. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  3580. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd}, 0);
  3581. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_head_kv * n_embd_head}, 0);
  3582. layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), {n_embd}, 0);
  3583. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  3584. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3585. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3586. layer.ffn_post_norm = create_tensor(tn(LLM_TENSOR_FFN_POST_NORM, "weight", i), {n_embd}, 0);
  3587. }
  3588. } break;
  3589. case LLM_ARCH_SEED_OSS:
  3590. {
  3591. const uint32_t head_dim = hparams.n_embd_head_k;
  3592. const int64_t n_qo_dim = n_head * head_dim;
  3593. const int64_t n_kv_dim = n_head_kv * head_dim;
  3594. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3595. // output
  3596. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3597. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  3598. // if output is NULL, init from the input tok embed
  3599. if (output == NULL) {
  3600. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3601. }
  3602. for (int i = 0; i < n_layer; ++i) {
  3603. auto & layer = layers[i];
  3604. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_qo_dim}, 0);
  3605. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_kv_dim}, 0);
  3606. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_kv_dim}, 0);
  3607. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_qo_dim, n_embd}, 0);
  3608. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_qo_dim}, TENSOR_NOT_REQUIRED);
  3609. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_kv_dim}, TENSOR_NOT_REQUIRED);
  3610. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_kv_dim}, TENSOR_NOT_REQUIRED);
  3611. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3612. layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), {n_embd}, 0);
  3613. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  3614. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3615. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3616. }
  3617. } break;
  3618. case LLM_ARCH_OLMOE:
  3619. {
  3620. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3621. // output
  3622. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3623. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  3624. for (int i = 0; i < n_layer; ++i) {
  3625. auto & layer = layers[i];
  3626. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3627. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  3628. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  3629. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  3630. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  3631. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd}, 0);
  3632. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd}, 0);
  3633. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3634. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  3635. if (n_expert == 0) {
  3636. throw std::runtime_error("n_expert must be > 0");
  3637. }
  3638. if (n_expert_used == 0) {
  3639. throw std::runtime_error("n_expert_used must be > 0");
  3640. }
  3641. // MoE branch
  3642. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0);
  3643. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff, n_embd, n_expert}, 0);
  3644. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0);
  3645. }
  3646. } break;
  3647. case LLM_ARCH_OPENELM:
  3648. {
  3649. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3650. // output
  3651. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3652. // init output from the input tok embed
  3653. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3654. for (int i = 0; i < n_layer; ++i) {
  3655. const int64_t n_head = hparams.n_head(i);
  3656. const int64_t n_head_qkv = 2*hparams.n_head_kv(i) + n_head;
  3657. const int64_t n_ff = hparams.n_ff(i);
  3658. auto & layer = layers[i];
  3659. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3660. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_head_qkv*n_embd_head_k}, 0);
  3661. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0);
  3662. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0);
  3663. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_head*n_embd_head_k, n_embd}, 0);
  3664. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3665. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  3666. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  3667. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3668. }
  3669. } break;
  3670. case LLM_ARCH_GPTNEOX:
  3671. {
  3672. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3673. // output
  3674. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3675. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
  3676. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  3677. for (int i = 0; i < n_layer; ++i) {
  3678. auto & layer = layers[i];
  3679. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3680. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0);
  3681. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, 0);
  3682. layer.bqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd + 2*n_embd_gqa}, 0);
  3683. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  3684. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0);
  3685. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3686. layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd}, 0);
  3687. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  3688. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, 0);
  3689. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3690. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, 0);
  3691. }
  3692. } break;
  3693. case LLM_ARCH_ARCTIC:
  3694. {
  3695. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3696. // output
  3697. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3698. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  3699. // if output is NULL, init from the input tok embed
  3700. if (output == NULL) {
  3701. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3702. }
  3703. for (int i = 0; i < n_layer; ++i) {
  3704. auto & layer = layers[i];
  3705. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3706. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  3707. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  3708. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  3709. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  3710. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3711. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_embd}, 0);
  3712. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_embd, n_embd}, 0);
  3713. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_embd}, 0);
  3714. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  3715. layer.ffn_norm_exps = create_tensor(tn(LLM_TENSOR_FFN_NORM_EXPS, "weight", i), {n_embd}, 0);
  3716. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff, n_expert}, false);
  3717. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), { n_ff, n_embd, n_expert}, 0);
  3718. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0);
  3719. }
  3720. } break;
  3721. case LLM_ARCH_DEEPSEEK:
  3722. {
  3723. const int64_t n_ff_exp = hparams.n_ff_exp;
  3724. const int64_t n_expert_shared = hparams.n_expert_shared;
  3725. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3726. // output
  3727. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3728. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  3729. for (int i = 0; i < n_layer; ++i) {
  3730. auto & layer = layers[i];
  3731. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3732. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  3733. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  3734. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  3735. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  3736. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3737. if (i < (int) hparams.n_layer_dense_lead) {
  3738. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  3739. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3740. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3741. } else {
  3742. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  3743. if (n_expert == 0) {
  3744. throw std::runtime_error("n_expert must be > 0");
  3745. }
  3746. if (n_expert_used == 0) {
  3747. throw std::runtime_error("n_expert_used must be > 0");
  3748. }
  3749. // MoE branch
  3750. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);
  3751. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp, n_embd, n_expert}, 0);
  3752. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);
  3753. // Shared expert branch
  3754. layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), {n_embd, n_ff_exp * n_expert_shared}, 0);
  3755. layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), { n_ff_exp * n_expert_shared, n_embd}, 0);
  3756. layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), {n_embd, n_ff_exp * n_expert_shared}, 0);
  3757. }
  3758. }
  3759. } break;
  3760. case LLM_ARCH_DEEPSEEK2:
  3761. {
  3762. const bool is_lite = (hparams.n_layer == 27);
  3763. const bool is_mla = (hparams.n_embd_head_k_mla != 0 && hparams.n_embd_head_v_mla != 0);
  3764. // note: these are the actual head sizes you get when treating as MHA or after "decompression" using wv_b for MLA
  3765. const int64_t n_embd_head_k_mla = is_mla ? hparams.n_embd_head_k_mla : hparams.n_embd_head_k;
  3766. const int64_t n_embd_head_v_mla = is_mla ? hparams.n_embd_head_v_mla : hparams.n_embd_head_v;
  3767. const int64_t n_embd_head_qk_rope = hparams.n_rot;
  3768. const int64_t n_embd_head_qk_nope = n_embd_head_k_mla - n_embd_head_qk_rope;
  3769. const int64_t q_lora_rank = hparams.n_lora_q;
  3770. const int64_t kv_lora_rank = hparams.n_lora_kv;
  3771. const int64_t n_ff_exp = hparams.n_ff_exp;
  3772. const int64_t n_expert_shared = hparams.n_expert_shared;
  3773. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3774. // output
  3775. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3776. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  3777. for (int i = 0; i < n_layer; ++i) {
  3778. auto & layer = layers[i];
  3779. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3780. if (!is_lite) {
  3781. layer.attn_q_a_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_A_NORM, "weight", i), {q_lora_rank}, 0);
  3782. }
  3783. layer.attn_kv_a_norm = create_tensor(tn(LLM_TENSOR_ATTN_KV_A_NORM, "weight", i), {kv_lora_rank}, 0);
  3784. if (!is_lite) {
  3785. layer.wq_a = create_tensor(tn(LLM_TENSOR_ATTN_Q_A, "weight", i), {n_embd, q_lora_rank}, 0);
  3786. layer.wq_b = create_tensor(tn(LLM_TENSOR_ATTN_Q_B, "weight", i), {q_lora_rank, n_head * n_embd_head_k_mla}, 0);
  3787. } else {
  3788. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_head * n_embd_head_k_mla}, 0);
  3789. }
  3790. 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);
  3791. // note: only old legacy GGUF files will have the unsplit wkv_b tensor in
  3792. if (is_mla) {
  3793. layer.wk_b = create_tensor(tn(LLM_TENSOR_ATTN_K_B, "weight", i), {n_embd_head_qk_nope, kv_lora_rank, n_head}, 0);
  3794. layer.wv_b = create_tensor(tn(LLM_TENSOR_ATTN_V_B, "weight", i), {kv_lora_rank, n_embd_head_v_mla, n_head}, 0);
  3795. } else {
  3796. 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);
  3797. }
  3798. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_head * n_embd_head_v_mla, n_embd}, 0);
  3799. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3800. if (i < (int) hparams.n_layer_dense_lead) {
  3801. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  3802. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3803. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3804. } else {
  3805. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  3806. layer.ffn_exp_probs_b = create_tensor(tn(LLM_TENSOR_FFN_EXP_PROBS_B, "bias", i), {n_expert}, TENSOR_NOT_REQUIRED);
  3807. if (n_expert == 0) {
  3808. throw std::runtime_error("n_expert must be > 0");
  3809. }
  3810. if (n_expert_used == 0) {
  3811. throw std::runtime_error("n_expert_used must be > 0");
  3812. }
  3813. // MoE branch
  3814. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);
  3815. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp, n_embd, n_expert}, 0);
  3816. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);
  3817. // Shared expert branch
  3818. layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), {n_embd, n_ff_exp * n_expert_shared}, 0);
  3819. layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), { n_ff_exp * n_expert_shared, n_embd}, 0);
  3820. layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), {n_embd, n_ff_exp * n_expert_shared}, 0);
  3821. }
  3822. }
  3823. } break;
  3824. case LLM_ARCH_PLM:
  3825. {
  3826. const int64_t n_embd_head_qk_rope = hparams.n_rot;
  3827. const int64_t n_embd_head_qk_nope = hparams.n_embd_head_k - hparams.n_rot;
  3828. const int64_t kv_lora_rank = hparams.n_lora_kv;
  3829. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3830. // output
  3831. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3832. // output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  3833. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3834. for (int i = 0; i < n_layer; ++i) {
  3835. auto & layer = layers[i];
  3836. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3837. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  3838. 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);
  3839. layer.attn_kv_a_norm = create_tensor(tn(LLM_TENSOR_ATTN_KV_A_NORM, "weight", i), {kv_lora_rank}, 0);
  3840. 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);
  3841. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), { n_head * ( n_embd_head_v), n_embd}, 0);
  3842. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3843. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3844. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3845. }
  3846. } break;
  3847. case LLM_ARCH_BITNET:
  3848. {
  3849. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3850. // output
  3851. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3852. for (int i = 0; i < n_layer; ++i) {
  3853. auto & layer = layers[i];
  3854. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3855. layer.attn_sub_norm = create_tensor(tn(LLM_TENSOR_ATTN_SUB_NORM, "weight", i), {n_embd}, 0);
  3856. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  3857. layer.wq_scale = create_tensor(tn(LLM_TENSOR_ATTN_Q, "scale", i), {1}, TENSOR_NOT_REQUIRED);
  3858. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  3859. layer.wk_scale = create_tensor(tn(LLM_TENSOR_ATTN_K, "scale", i), {1}, TENSOR_NOT_REQUIRED);
  3860. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  3861. layer.wv_scale = create_tensor(tn(LLM_TENSOR_ATTN_V, "scale", i), {1}, TENSOR_NOT_REQUIRED);
  3862. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  3863. layer.wo_scale = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "scale", i), {1}, TENSOR_NOT_REQUIRED);
  3864. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3865. layer.ffn_sub_norm = create_tensor(tn(LLM_TENSOR_FFN_SUB_NORM, "weight", i), {n_ff}, 0);
  3866. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  3867. layer.ffn_gate_scale = create_tensor(tn(LLM_TENSOR_FFN_GATE, "scale", i), {1}, TENSOR_NOT_REQUIRED);
  3868. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  3869. layer.ffn_down_scale = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "scale", i), {1}, TENSOR_NOT_REQUIRED);
  3870. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3871. layer.ffn_up_scale = create_tensor(tn(LLM_TENSOR_FFN_UP, "scale", i), {1}, TENSOR_NOT_REQUIRED);
  3872. }
  3873. } break;
  3874. case LLM_ARCH_T5:
  3875. {
  3876. const auto n_rel_attn_bkts = hparams.n_rel_attn_bkts;
  3877. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3878. // output
  3879. output_norm_enc = create_tensor(tn(LLM_TENSOR_ENC_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3880. output_norm = create_tensor(tn(LLM_TENSOR_DEC_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3881. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  3882. // if output is NULL, init from the input tok embed
  3883. if (output == NULL) {
  3884. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3885. }
  3886. // n_layer: number of encoder_layers
  3887. // dec_n_layer: number of decoder_layers
  3888. const int dec_n_layer = hparams.dec_n_layer;
  3889. if (dec_n_layer > n_layer) {
  3890. layers.resize(dec_n_layer);
  3891. }
  3892. // load encoder layers
  3893. for (int i = 0; i < n_layer; ++i) {
  3894. auto & layer = layers[i];
  3895. layer.attn_norm_enc = create_tensor(tn(LLM_TENSOR_ENC_ATTN_NORM, "weight", i), {n_embd}, 0);
  3896. 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);
  3897. layer.wq_enc = create_tensor(tn(LLM_TENSOR_ENC_ATTN_Q, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  3898. layer.wk_enc = create_tensor(tn(LLM_TENSOR_ENC_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  3899. layer.wv_enc = create_tensor(tn(LLM_TENSOR_ENC_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  3900. layer.wo_enc = create_tensor(tn(LLM_TENSOR_ENC_ATTN_OUT, "weight", i), {n_embd_v_gqa, n_embd}, 0);
  3901. layer.ffn_norm_enc = create_tensor(tn(LLM_TENSOR_ENC_FFN_NORM, "weight", i), {n_embd}, 0);
  3902. layer.ffn_gate_enc = create_tensor(tn(LLM_TENSOR_ENC_FFN_GATE, "weight", i), {n_embd, n_ff}, TENSOR_NOT_REQUIRED);
  3903. layer.ffn_down_enc = create_tensor(tn(LLM_TENSOR_ENC_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3904. layer.ffn_up_enc = create_tensor(tn(LLM_TENSOR_ENC_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3905. }
  3906. // load decoder layers
  3907. for (int i = 0; i < dec_n_layer; ++i) {
  3908. auto & layer = layers[i];
  3909. layer.attn_norm = create_tensor(tn(LLM_TENSOR_DEC_ATTN_NORM, "weight", i), {n_embd}, 0);
  3910. layer.attn_rel_b = create_tensor(tn(LLM_TENSOR_DEC_ATTN_REL_B, "weight", i), {n_head, n_rel_attn_bkts}, TENSOR_NOT_REQUIRED);
  3911. layer.wq = create_tensor(tn(LLM_TENSOR_DEC_ATTN_Q, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  3912. layer.wk = create_tensor(tn(LLM_TENSOR_DEC_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  3913. layer.wv = create_tensor(tn(LLM_TENSOR_DEC_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  3914. layer.wo = create_tensor(tn(LLM_TENSOR_DEC_ATTN_OUT, "weight", i), {n_embd_v_gqa, n_embd}, 0);
  3915. layer.attn_norm_cross = create_tensor(tn(LLM_TENSOR_DEC_CROSS_ATTN_NORM, "weight", i), {n_embd}, 0);
  3916. // this tensor seems to be unused in HF transformers implementation
  3917. 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);
  3918. layer.wq_cross = create_tensor(tn(LLM_TENSOR_DEC_CROSS_ATTN_Q, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  3919. layer.wk_cross = create_tensor(tn(LLM_TENSOR_DEC_CROSS_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  3920. layer.wv_cross = create_tensor(tn(LLM_TENSOR_DEC_CROSS_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  3921. layer.wo_cross = create_tensor(tn(LLM_TENSOR_DEC_CROSS_ATTN_OUT, "weight", i), {n_embd_v_gqa, n_embd}, 0);
  3922. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_DEC_FFN_NORM, "weight", i), {n_embd}, 0);
  3923. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_DEC_FFN_GATE, "weight", i), {n_embd, n_ff}, TENSOR_NOT_REQUIRED);
  3924. layer.ffn_down = create_tensor(tn(LLM_TENSOR_DEC_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3925. layer.ffn_up = create_tensor(tn(LLM_TENSOR_DEC_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3926. }
  3927. } break;
  3928. case LLM_ARCH_T5ENCODER:
  3929. {
  3930. const auto n_rel_attn_bkts = hparams.n_rel_attn_bkts;
  3931. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3932. // output
  3933. output_norm_enc = create_tensor(tn(LLM_TENSOR_ENC_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3934. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  3935. // if output is NULL, init from the input tok embed
  3936. if (output == NULL) {
  3937. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3938. }
  3939. for (int i = 0; i < n_layer; ++i) {
  3940. auto & layer = layers[i];
  3941. layer.attn_norm_enc = create_tensor(tn(LLM_TENSOR_ENC_ATTN_NORM, "weight", i), {n_embd}, 0);
  3942. 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);
  3943. layer.wq_enc = create_tensor(tn(LLM_TENSOR_ENC_ATTN_Q, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  3944. layer.wk_enc = create_tensor(tn(LLM_TENSOR_ENC_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  3945. layer.wv_enc = create_tensor(tn(LLM_TENSOR_ENC_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  3946. layer.wo_enc = create_tensor(tn(LLM_TENSOR_ENC_ATTN_OUT, "weight", i), {n_embd_v_gqa, n_embd}, 0);
  3947. layer.ffn_norm_enc = create_tensor(tn(LLM_TENSOR_ENC_FFN_NORM, "weight", i), {n_embd}, 0);
  3948. layer.ffn_gate_enc = create_tensor(tn(LLM_TENSOR_ENC_FFN_GATE, "weight", i), {n_embd, n_ff}, TENSOR_NOT_REQUIRED);
  3949. layer.ffn_down_enc = create_tensor(tn(LLM_TENSOR_ENC_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3950. layer.ffn_up_enc = create_tensor(tn(LLM_TENSOR_ENC_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3951. }
  3952. } break;
  3953. case LLM_ARCH_JAIS:
  3954. {
  3955. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3956. // output
  3957. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3958. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
  3959. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  3960. for (int i = 0; i < n_layer; ++i) {
  3961. auto & layer = layers[i];
  3962. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3963. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0);
  3964. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, 0);
  3965. layer.bqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd + 2*n_embd_gqa}, 0);
  3966. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  3967. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0);
  3968. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3969. layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd}, 0);
  3970. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  3971. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, 0);
  3972. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  3973. layer.ffn_gate_b = create_tensor(tn(LLM_TENSOR_FFN_GATE, "bias", i), {n_ff}, 0);
  3974. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3975. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, 0);
  3976. }
  3977. } break;
  3978. case LLM_ARCH_CHATGLM:
  3979. {
  3980. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3981. // output
  3982. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3983. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  3984. // if output is NULL, init from the input tok embed
  3985. if (output == NULL) {
  3986. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3987. }
  3988. for (int i = 0; i < n_layer; ++i) {
  3989. auto & layer = layers[i];
  3990. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3991. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, TENSOR_NOT_REQUIRED);
  3992. layer.bqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd + 2*n_embd_gqa}, TENSOR_NOT_REQUIRED);
  3993. if (layer.wqkv == nullptr) {
  3994. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  3995. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  3996. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  3997. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  3998. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED);
  3999. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED);
  4000. }
  4001. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  4002. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  4003. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff * 2}, 0);
  4004. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  4005. }
  4006. } break;
  4007. case LLM_ARCH_GLM4:
  4008. {
  4009. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  4010. // output
  4011. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  4012. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  4013. // if output is NULL, init from the input tok embed
  4014. if (output == NULL) {
  4015. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  4016. }
  4017. for (int i = 0; i < n_layer; ++i) {
  4018. auto & layer = layers[i];
  4019. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  4020. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, TENSOR_NOT_REQUIRED);
  4021. layer.bqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd + 2*n_embd_gqa}, TENSOR_NOT_REQUIRED);
  4022. if (layer.wqkv == nullptr) {
  4023. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  4024. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  4025. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  4026. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  4027. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED);
  4028. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED);
  4029. }
  4030. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  4031. layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), {n_embd}, 0);
  4032. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  4033. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  4034. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff * 2}, 0);
  4035. layer.ffn_post_norm = create_tensor(tn(LLM_TENSOR_FFN_POST_NORM, "weight", i), {n_embd}, 0);
  4036. }
  4037. } break;
  4038. case LLM_ARCH_GLM4_MOE:
  4039. {
  4040. const int64_t n_expert = hparams.n_expert;
  4041. const int64_t n_expert_used = hparams.n_expert_used;
  4042. const int64_t n_expert_shared = hparams.n_expert_shared;
  4043. GGML_ASSERT(hparams.n_expert > 0 && "n_expert must be > 0 for GLM4_MOE MoE layers");
  4044. GGML_ASSERT(hparams.n_expert_used > 0 && "n_expert_used must be > 0 for GLM4_MOE MoE layers");
  4045. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, 0);
  4046. // output
  4047. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), { n_embd }, 0);
  4048. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), { n_embd, n_vocab }, TENSOR_NOT_REQUIRED);
  4049. // if output is NULL, init from the input tok embed
  4050. if (output == NULL) {
  4051. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, TENSOR_DUPLICATED);
  4052. }
  4053. // Load ALL tensors including NextN layer to satisfy total tensor count
  4054. // but only PROCESS up to last layer (skipping final NextN layer) in forward pass
  4055. for (int i = 0; i < n_layer; ++i) {
  4056. int flags = 0;
  4057. if (hparams.nextn_predict_layers > 0 && static_cast<uint32_t>(i) >= n_layer - hparams.nextn_predict_layers) {
  4058. // skip all tensors in the NextN layers
  4059. flags |= TENSOR_SKIP;
  4060. }
  4061. auto & layer = layers[i];
  4062. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), { n_embd }, flags);
  4063. // GLM-style attention with bias terms
  4064. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), { n_embd, n_embd_head_k * n_head }, flags);
  4065. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), { n_embd, n_embd_k_gqa }, flags);
  4066. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), { n_embd, n_embd_v_gqa }, flags);
  4067. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), { n_embd_head_k * n_head }, flags);
  4068. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), { n_embd_k_gqa }, flags);
  4069. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), { n_embd_v_gqa }, flags);
  4070. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), { n_embd_head_k * n_head, n_embd }, flags);
  4071. // K/Q norm tensors (optional for GLM-4.5 355B variant)
  4072. layer.attn_q_norm = create_tensor(
  4073. tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), { n_embd_head_k }, TENSOR_NOT_REQUIRED | flags);
  4074. layer.attn_k_norm = create_tensor(
  4075. tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), { n_embd_head_k }, TENSOR_NOT_REQUIRED | flags);
  4076. layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), { n_embd }, flags);
  4077. // Check if this layer uses MoE or dense FFN based on n_layer_dense_lead
  4078. // GLM 4.5 uses hybrid architecture: layer 0 is dense, layers 1+ are MoE
  4079. const bool use_moe = (static_cast<uint32_t>(i) >= hparams.n_layer_dense_lead);
  4080. if (use_moe) {
  4081. // MoE layers
  4082. layer.ffn_gate_inp =
  4083. create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), { n_embd, n_expert }, flags);
  4084. layer.ffn_exp_probs_b = create_tensor(tn(LLM_TENSOR_FFN_EXP_PROBS_B, "bias", i), { n_expert }, flags);
  4085. // MoE branch
  4086. const int64_t n_ff_exp = hparams.n_ff_exp ? hparams.n_ff_exp : n_ff / n_expert_used;
  4087. layer.ffn_gate_exps = create_tensor(
  4088. tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert }, flags);
  4089. layer.ffn_down_exps = create_tensor(
  4090. tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), { n_ff_exp, n_embd, n_expert }, flags);
  4091. layer.ffn_up_exps = create_tensor(
  4092. tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert }, flags);
  4093. // Shared expert
  4094. if (n_expert_shared > 0) {
  4095. const int64_t n_ff_shexp = n_ff_exp * n_expert_shared;
  4096. layer.ffn_gate_shexp = create_tensor(
  4097. tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), { n_embd, n_ff_shexp }, flags);
  4098. layer.ffn_down_shexp = create_tensor(
  4099. tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), { n_ff_shexp, n_embd }, flags);
  4100. layer.ffn_up_shexp = create_tensor(
  4101. tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), { n_embd, n_ff_shexp }, flags);
  4102. }
  4103. } else {
  4104. // Dense layers (first k layers) - GLM uses separate gate/up projections
  4105. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), { n_embd, n_ff }, flags);
  4106. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd }, flags);
  4107. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), { n_embd, n_ff }, flags);
  4108. }
  4109. // NextN/MTP tensors (preserved but unused) - conditionally load for last nextn_predict_layers
  4110. if (hparams.nextn_predict_layers > 0 && static_cast<uint32_t>(i) >= n_layer - hparams.nextn_predict_layers) {
  4111. layer.nextn.eh_proj = create_tensor(tn(LLM_TENSOR_NEXTN_EH_PROJ, "weight", i), { 2 * n_embd, n_embd }, flags);
  4112. layer.nextn.embed_tokens = create_tensor(tn(LLM_TENSOR_NEXTN_EMBED_TOKENS, "weight", i), { n_embd, n_vocab }, flags);
  4113. layer.nextn.enorm = create_tensor(tn(LLM_TENSOR_NEXTN_ENORM, "weight", i), { n_embd }, flags);
  4114. layer.nextn.hnorm = create_tensor(tn(LLM_TENSOR_NEXTN_HNORM, "weight", i), { n_embd }, flags);
  4115. layer.nextn.shared_head_head = create_tensor(tn(LLM_TENSOR_NEXTN_SHARED_HEAD_HEAD, "weight", i), { n_embd, n_vocab }, flags);
  4116. layer.nextn.shared_head_norm = create_tensor(tn(LLM_TENSOR_NEXTN_SHARED_HEAD_NORM, "weight", i), { n_embd }, flags);
  4117. }
  4118. }
  4119. }
  4120. break;
  4121. case LLM_ARCH_NEMOTRON:
  4122. {
  4123. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  4124. // output
  4125. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  4126. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
  4127. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  4128. for (int i = 0; i < n_layer; ++i) {
  4129. auto & layer = layers[i];
  4130. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  4131. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0);
  4132. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  4133. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  4134. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  4135. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  4136. // optional bias tensors
  4137. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  4138. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED);
  4139. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED);
  4140. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  4141. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  4142. layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd}, 0);
  4143. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  4144. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  4145. // optional MLP bias
  4146. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  4147. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, TENSOR_NOT_REQUIRED);
  4148. }
  4149. } break;
  4150. case LLM_ARCH_NEMOTRON_H:
  4151. {
  4152. // mamba2 Mixer SSM params
  4153. // NOTE: int64_t for tensor dimensions
  4154. const int64_t d_conv = hparams.ssm_d_conv;
  4155. const int64_t d_inner = hparams.ssm_d_inner;
  4156. const int64_t d_state = hparams.ssm_d_state;
  4157. const int64_t n_ssm_head = hparams.ssm_dt_rank;
  4158. const int64_t n_group = hparams.ssm_n_group;
  4159. const int64_t d_in_proj = 2*d_inner + 2*n_group*d_state + n_ssm_head;
  4160. // embeddings
  4161. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  4162. // output
  4163. {
  4164. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  4165. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  4166. // if output is NULL, init from the input tok embed, duplicated to allow offloading
  4167. if (output == NULL) {
  4168. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  4169. }
  4170. }
  4171. for (int i = 0; i < n_layer; ++i) {
  4172. auto & layer = layers[i];
  4173. // all blocks use the attn norm
  4174. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  4175. if (hparams.is_recurrent(i)) {
  4176. // ssm layers
  4177. layer.ssm_in = create_tensor(tn(LLM_TENSOR_SSM_IN, "weight", i), {n_embd, d_in_proj}, 0);
  4178. layer.ssm_conv1d = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "weight", i), {d_conv, d_inner + 2*n_group*d_state}, 0);
  4179. layer.ssm_conv1d_b = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "bias", i), {d_inner + 2*n_group*d_state}, TENSOR_NOT_REQUIRED);
  4180. layer.ssm_dt_b = create_tensor(tn(LLM_TENSOR_SSM_DT, "bias", i), {n_ssm_head}, 0);
  4181. // no "weight" suffix for these
  4182. layer.ssm_a = create_tensor(tn(LLM_TENSOR_SSM_A, i), {1, n_ssm_head}, 0);
  4183. layer.ssm_d = create_tensor(tn(LLM_TENSOR_SSM_D, i), {1, n_ssm_head}, 0);
  4184. layer.ssm_norm = create_tensor(tn(LLM_TENSOR_SSM_NORM, "weight", i), {d_inner / n_group, n_group}, 0);
  4185. // out_proj
  4186. layer.ssm_out = create_tensor(tn(LLM_TENSOR_SSM_OUT, "weight", i), {d_inner, n_embd}, 0);
  4187. } else if (hparams.n_ff(i) == 0) {
  4188. // attention layers (with optional bias)
  4189. const int64_t n_head_i = hparams.n_head(i);
  4190. const int64_t n_embd_k_gqa_i = hparams.n_embd_k_gqa(i);
  4191. const int64_t n_embd_v_gqa_i = hparams.n_embd_v_gqa(i);
  4192. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head_i}, 0);
  4193. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa_i}, 0);
  4194. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa_i}, 0);
  4195. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head_i, n_embd}, 0);
  4196. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  4197. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_k_gqa_i}, TENSOR_NOT_REQUIRED);
  4198. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_v_gqa_i}, TENSOR_NOT_REQUIRED);
  4199. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  4200. } else {
  4201. // mlp layers
  4202. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { hparams.n_ff(i), n_embd}, 0);
  4203. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, hparams.n_ff(i)}, 0);
  4204. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  4205. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {hparams.n_ff(i)}, TENSOR_NOT_REQUIRED);
  4206. }
  4207. }
  4208. } break;
  4209. case LLM_ARCH_EXAONE:
  4210. {
  4211. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  4212. // output
  4213. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  4214. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  4215. // if output is NULL, init from the input tok embed
  4216. if (output == NULL) {
  4217. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  4218. }
  4219. for (int i = 0; i < n_layer; ++i) {
  4220. auto & layer = layers[i];
  4221. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  4222. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  4223. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  4224. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  4225. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  4226. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  4227. layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  4228. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  4229. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  4230. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  4231. }
  4232. } break;
  4233. case LLM_ARCH_EXAONE4:
  4234. {
  4235. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  4236. // output
  4237. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  4238. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  4239. // if output is NULL, init from the input tok embed
  4240. if (output == NULL) {
  4241. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  4242. }
  4243. for (int i = 0; i < n_layer; ++i) {
  4244. auto & layer = layers[i];
  4245. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  4246. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  4247. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  4248. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  4249. layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  4250. layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), {n_embd}, 0);
  4251. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0);
  4252. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0);
  4253. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  4254. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  4255. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  4256. layer.ffn_post_norm = create_tensor(tn(LLM_TENSOR_FFN_POST_NORM, "weight", i), {n_embd}, 0);
  4257. }
  4258. } break;
  4259. case LLM_ARCH_RWKV6:
  4260. {
  4261. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  4262. // Block 0, LN0
  4263. tok_norm = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "weight"), {n_embd}, 0);
  4264. tok_norm_b = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "bias"), {n_embd}, 0);
  4265. // output
  4266. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  4267. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
  4268. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  4269. const int time_mix_extra_dim = hparams.time_mix_extra_dim;
  4270. const int time_decay_extra_dim = hparams.time_decay_extra_dim;
  4271. const int head_size = hparams.wkv_head_size;
  4272. const int attn_hidden_size = n_embd;
  4273. const int ffn_size = hparams.n_ff_arr[0];
  4274. for (int i = 0; i < n_layer; ++i) {
  4275. auto & layer = layers[i];
  4276. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  4277. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0);
  4278. layer.attn_norm_2 = create_tensor(tn(LLM_TENSOR_ATTN_NORM_2, "weight", i), {n_embd}, 0);
  4279. layer.attn_norm_2_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM_2, "bias", i), {n_embd}, 0);
  4280. layer.time_mix_w1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_W1, "weight", i), {n_embd, time_mix_extra_dim * 5}, 0);
  4281. layer.time_mix_w2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_W2, "weight", i), {time_mix_extra_dim, n_embd, 5}, 0);
  4282. layer.time_mix_lerp_x = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_X, "weight", i), {n_embd, 1, 1}, 0);
  4283. layer.time_mix_lerp_w = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_W, "weight", i), {n_embd, 1, 1}, TENSOR_NOT_REQUIRED);
  4284. layer.time_mix_lerp_k = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_K, "weight", i), {n_embd, 1, 1}, TENSOR_NOT_REQUIRED);
  4285. layer.time_mix_lerp_v = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_V, "weight", i), {n_embd, 1, 1}, TENSOR_NOT_REQUIRED);
  4286. layer.time_mix_lerp_r = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_R, "weight", i), {n_embd, 1, 1}, TENSOR_NOT_REQUIRED);
  4287. layer.time_mix_lerp_g = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_G, "weight", i), {n_embd, 1, 1}, TENSOR_NOT_REQUIRED);
  4288. layer.time_mix_lerp_fused = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_FUSED, "weight", i), {n_embd, 1, 1, 5}, TENSOR_NOT_REQUIRED);
  4289. GGML_ASSERT(!(layer.time_mix_lerp_fused == NULL && layer.time_mix_lerp_w == NULL));
  4290. layer.time_mix_first = create_tensor(tn(LLM_TENSOR_TIME_MIX_FIRST, "weight", i), {head_size, n_embd / head_size}, 0);
  4291. layer.time_mix_decay = create_tensor(tn(LLM_TENSOR_TIME_MIX_DECAY, "weight", i), {n_embd}, 0);
  4292. layer.time_mix_decay_w1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_DECAY_W1, "weight", i), {n_embd, time_decay_extra_dim}, 0);
  4293. layer.time_mix_decay_w2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_DECAY_W2, "weight", i), {time_decay_extra_dim, attn_hidden_size}, 0);
  4294. layer.time_mix_key = create_tensor(tn(LLM_TENSOR_TIME_MIX_KEY, "weight", i), {attn_hidden_size, n_embd}, 0);
  4295. layer.time_mix_value = create_tensor(tn(LLM_TENSOR_TIME_MIX_VALUE, "weight", i), {attn_hidden_size, n_embd}, 0);
  4296. layer.time_mix_receptance = create_tensor(tn(LLM_TENSOR_TIME_MIX_RECEPTANCE, "weight", i), {attn_hidden_size, n_embd}, 0);
  4297. layer.time_mix_gate = create_tensor(tn(LLM_TENSOR_TIME_MIX_GATE, "weight", i), {attn_hidden_size, n_embd}, 0);
  4298. layer.time_mix_ln = create_tensor(tn(LLM_TENSOR_TIME_MIX_LN, "weight", i), {n_embd}, 0);
  4299. layer.time_mix_ln_b = create_tensor(tn(LLM_TENSOR_TIME_MIX_LN, "bias", i), {n_embd}, 0);
  4300. layer.time_mix_output = create_tensor(tn(LLM_TENSOR_TIME_MIX_OUTPUT, "weight", i), {n_embd, attn_hidden_size}, 0);
  4301. layer.channel_mix_lerp_k = create_tensor(tn(LLM_TENSOR_CHANNEL_MIX_LERP_K, "weight", i), {n_embd, 1, 1}, 0);
  4302. layer.channel_mix_lerp_r = create_tensor(tn(LLM_TENSOR_CHANNEL_MIX_LERP_R, "weight", i), {n_embd, 1, 1}, 0);
  4303. layer.channel_mix_key = create_tensor(tn(LLM_TENSOR_CHANNEL_MIX_KEY, "weight", i), {n_embd, ffn_size}, 0);
  4304. layer.channel_mix_value = create_tensor(tn(LLM_TENSOR_CHANNEL_MIX_VALUE, "weight", i), {ffn_size, n_embd}, 0);
  4305. layer.channel_mix_receptance = create_tensor(tn(LLM_TENSOR_CHANNEL_MIX_RECEPTANCE, "weight", i), {n_embd, n_embd}, 0);
  4306. }
  4307. } break;
  4308. case LLM_ARCH_RWKV6QWEN2:
  4309. {
  4310. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  4311. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  4312. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, TENSOR_NOT_REQUIRED);
  4313. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  4314. const int time_mix_extra_dim = hparams.time_mix_extra_dim;
  4315. const int time_decay_extra_dim = hparams.time_decay_extra_dim;
  4316. const int head_size = hparams.wkv_head_size;
  4317. const int attn_hidden_size = n_embd;
  4318. const int n_head_kv = hparams.n_head_kv();
  4319. int attn_key_value_size;
  4320. if (n_head_kv == 0 || attn_hidden_size / head_size == n_head_kv) {
  4321. attn_key_value_size = attn_hidden_size;
  4322. } else {
  4323. attn_key_value_size = n_head_kv * head_size;
  4324. }
  4325. for (int i = 0; i < n_layer; ++i) {
  4326. auto & layer = layers[i];
  4327. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  4328. layer.time_mix_w1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_W1, "weight", i), {n_embd, time_mix_extra_dim * 5}, 0);
  4329. layer.time_mix_w2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_W2, "weight", i), {time_mix_extra_dim, n_embd, 5}, 0);
  4330. layer.time_mix_lerp_x = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_X, "weight", i), {n_embd, 1, 1}, 0);
  4331. layer.time_mix_lerp_fused = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_FUSED, "weight", i), {n_embd, 1, 1, 5}, 0);
  4332. layer.time_mix_first = create_tensor(tn(LLM_TENSOR_TIME_MIX_FIRST, "weight", i), {head_size, n_embd / head_size}, TENSOR_NOT_REQUIRED);
  4333. layer.time_mix_decay = create_tensor(tn(LLM_TENSOR_TIME_MIX_DECAY, "weight", i), {n_embd}, 0);
  4334. layer.time_mix_decay_w1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_DECAY_W1, "weight", i), {n_embd, time_decay_extra_dim}, 0);
  4335. layer.time_mix_decay_w2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_DECAY_W2, "weight", i), {time_decay_extra_dim, attn_hidden_size}, 0);
  4336. layer.time_mix_key = create_tensor(tn(LLM_TENSOR_TIME_MIX_KEY, "weight", i), {n_embd, attn_key_value_size}, 0);
  4337. layer.time_mix_value = create_tensor(tn(LLM_TENSOR_TIME_MIX_VALUE, "weight", i), {n_embd, attn_key_value_size}, 0);
  4338. layer.time_mix_receptance = create_tensor(tn(LLM_TENSOR_TIME_MIX_RECEPTANCE, "weight", i), {attn_hidden_size, n_embd}, 0);
  4339. layer.time_mix_gate = create_tensor(tn(LLM_TENSOR_TIME_MIX_GATE, "weight", i), {attn_hidden_size, n_embd}, 0);
  4340. // optional bias tensors
  4341. layer.time_mix_key_b = create_tensor(tn(LLM_TENSOR_TIME_MIX_KEY, "bias", i), {attn_key_value_size}, TENSOR_NOT_REQUIRED);
  4342. layer.time_mix_value_b = create_tensor(tn(LLM_TENSOR_TIME_MIX_VALUE, "bias", i), {attn_key_value_size}, TENSOR_NOT_REQUIRED);
  4343. layer.time_mix_receptance_b = create_tensor(tn(LLM_TENSOR_TIME_MIX_RECEPTANCE, "bias", i), {attn_hidden_size}, TENSOR_NOT_REQUIRED);
  4344. layer.time_mix_output = create_tensor(tn(LLM_TENSOR_TIME_MIX_OUTPUT, "weight", i), {n_embd, attn_hidden_size}, 0);
  4345. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  4346. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  4347. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  4348. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  4349. }
  4350. } break;
  4351. case LLM_ARCH_RWKV7:
  4352. {
  4353. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  4354. // Block 0, LN0
  4355. tok_norm = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "weight"), {n_embd}, 0);
  4356. tok_norm_b = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "bias"), {n_embd}, 0);
  4357. // output
  4358. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  4359. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
  4360. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  4361. const int n_lora_decay = hparams.n_lora_decay;
  4362. const int n_lora_iclr = hparams.n_lora_iclr;
  4363. const int n_lora_value_res_mix = hparams.n_lora_value_res_mix;
  4364. const int n_lora_gate = hparams.n_lora_gate;
  4365. const int attn_hidden_size = n_embd;
  4366. const int ffn_size = hparams.n_ff_arr[0];
  4367. for (int i = 0; i < n_layer; ++i) {
  4368. auto & layer = layers[i];
  4369. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  4370. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0);
  4371. layer.attn_norm_2 = create_tensor(tn(LLM_TENSOR_ATTN_NORM_2, "weight", i), {n_embd}, 0);
  4372. layer.attn_norm_2_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM_2, "bias", i), {n_embd}, 0);
  4373. layer.time_mix_w0 = create_tensor(tn(LLM_TENSOR_TIME_MIX_W0, "weight", i), {n_embd}, 0);
  4374. layer.time_mix_w1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_W1, "weight", i), {n_embd, n_lora_decay}, 0);
  4375. layer.time_mix_w2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_W2, "weight", i), {n_lora_decay, n_embd}, 0);
  4376. layer.time_mix_a0 = create_tensor(tn(LLM_TENSOR_TIME_MIX_A0, "weight", i), {n_embd}, 0);
  4377. layer.time_mix_a1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_A1, "weight", i), {n_embd, n_lora_iclr}, 0);
  4378. layer.time_mix_a2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_A2, "weight", i), {n_lora_iclr, n_embd}, 0);
  4379. if (i == 0) {
  4380. // actually not used
  4381. layer.time_mix_v0 = create_tensor(tn(LLM_TENSOR_TIME_MIX_V0, "weight", i), {n_embd}, 0);
  4382. layer.time_mix_v1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_V1, "weight", i), {n_embd, n_lora_iclr}, 0);
  4383. layer.time_mix_v2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_V2, "weight", i), {n_lora_iclr, n_embd}, 0);
  4384. } else {
  4385. layer.time_mix_v0 = create_tensor(tn(LLM_TENSOR_TIME_MIX_V0, "weight", i), {n_embd}, 0);
  4386. layer.time_mix_v1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_V1, "weight", i), {n_embd, n_lora_value_res_mix}, 0);
  4387. layer.time_mix_v2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_V2, "weight", i), {n_lora_value_res_mix, n_embd}, 0);
  4388. }
  4389. layer.time_mix_g1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_G1, "weight", i), {n_embd, n_lora_gate}, 0);
  4390. layer.time_mix_g2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_G2, "weight", i), {n_lora_gate, n_embd}, 0);
  4391. layer.time_mix_lerp_fused = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_FUSED, "weight", i), {n_embd, 1, 1, 6}, 0);
  4392. layer.time_mix_k_k = create_tensor(tn(LLM_TENSOR_TIME_MIX_K_K, "weight", i), {attn_hidden_size}, 0);
  4393. layer.time_mix_k_a = create_tensor(tn(LLM_TENSOR_TIME_MIX_K_A, "weight", i), {attn_hidden_size}, 0);
  4394. layer.time_mix_r_k = create_tensor(tn(LLM_TENSOR_TIME_MIX_R_K, "weight", i), {attn_hidden_size}, 0);
  4395. layer.time_mix_key = create_tensor(tn(LLM_TENSOR_TIME_MIX_KEY, "weight", i), {attn_hidden_size, n_embd}, 0);
  4396. layer.time_mix_value = create_tensor(tn(LLM_TENSOR_TIME_MIX_VALUE, "weight", i), {attn_hidden_size, n_embd}, 0);
  4397. layer.time_mix_receptance = create_tensor(tn(LLM_TENSOR_TIME_MIX_RECEPTANCE, "weight", i), {attn_hidden_size, n_embd}, 0);
  4398. layer.time_mix_ln = create_tensor(tn(LLM_TENSOR_TIME_MIX_LN, "weight", i), {n_embd}, 0);
  4399. layer.time_mix_ln_b = create_tensor(tn(LLM_TENSOR_TIME_MIX_LN, "bias", i), {n_embd}, 0);
  4400. layer.time_mix_output = create_tensor(tn(LLM_TENSOR_TIME_MIX_OUTPUT, "weight", i), {n_embd, attn_hidden_size}, 0);
  4401. layer.channel_mix_lerp_k = create_tensor(tn(LLM_TENSOR_CHANNEL_MIX_LERP_K, "weight", i), {n_embd, 1, 1}, 0);
  4402. layer.channel_mix_key = create_tensor(tn(LLM_TENSOR_CHANNEL_MIX_KEY, "weight", i), {n_embd, ffn_size}, 0);
  4403. layer.channel_mix_value = create_tensor(tn(LLM_TENSOR_CHANNEL_MIX_VALUE, "weight", i), {ffn_size, n_embd}, 0);
  4404. }
  4405. } break;
  4406. case LLM_ARCH_ARWKV7:
  4407. {
  4408. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  4409. // output
  4410. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  4411. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  4412. const int n_lora_decay = hparams.n_lora_decay;
  4413. const int n_lora_iclr = hparams.n_lora_iclr;
  4414. const int n_lora_value_res_mix = hparams.n_lora_value_res_mix;
  4415. const int n_lora_gate = hparams.n_lora_gate;
  4416. const int attn_hidden_size = n_embd;
  4417. for (int i = 0; i < n_layer; ++i) {
  4418. auto & layer = layers[i];
  4419. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  4420. layer.time_mix_w0 = create_tensor(tn(LLM_TENSOR_TIME_MIX_W0, "weight", i), {n_embd}, 0);
  4421. layer.time_mix_w1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_W1, "weight", i), {n_embd, n_lora_decay}, 0);
  4422. layer.time_mix_w2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_W2, "weight", i), {n_lora_decay, n_embd}, 0);
  4423. layer.time_mix_a0 = create_tensor(tn(LLM_TENSOR_TIME_MIX_A0, "weight", i), {n_embd}, 0);
  4424. layer.time_mix_a1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_A1, "weight", i), {n_embd, n_lora_iclr}, 0);
  4425. layer.time_mix_a2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_A2, "weight", i), {n_lora_iclr, n_embd}, 0);
  4426. if (i == 0) {
  4427. // actually not used
  4428. layer.time_mix_v0 = create_tensor(tn(LLM_TENSOR_TIME_MIX_V0, "weight", i), {n_embd}, 0);
  4429. layer.time_mix_v1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_V1, "weight", i), {n_embd, n_lora_iclr}, 0);
  4430. layer.time_mix_v2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_V2, "weight", i), {n_lora_iclr, n_embd}, 0);
  4431. } else {
  4432. layer.time_mix_v0 = create_tensor(tn(LLM_TENSOR_TIME_MIX_V0, "weight", i), {n_embd}, 0);
  4433. layer.time_mix_v1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_V1, "weight", i), {n_embd, n_lora_value_res_mix}, 0);
  4434. layer.time_mix_v2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_V2, "weight", i), {n_lora_value_res_mix, n_embd}, 0);
  4435. }
  4436. layer.time_mix_g1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_G1, "weight", i), {n_embd, n_lora_gate}, TENSOR_NOT_REQUIRED);
  4437. layer.time_mix_g2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_G2, "weight", i), {n_lora_gate, n_embd}, TENSOR_NOT_REQUIRED);
  4438. try {
  4439. layer.time_mix_lerp_fused = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_FUSED, "weight", i), {n_embd, 1, 1, 6}, 0);
  4440. } catch(std::runtime_error & e) {
  4441. // ARWKV models may not have gate tensors
  4442. layer.time_mix_lerp_fused = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_FUSED, "weight", i), {n_embd, 1, 1, 5}, 0);
  4443. }
  4444. layer.time_mix_k_k = create_tensor(tn(LLM_TENSOR_TIME_MIX_K_K, "weight", i), {attn_hidden_size}, 0);
  4445. layer.time_mix_k_a = create_tensor(tn(LLM_TENSOR_TIME_MIX_K_A, "weight", i), {attn_hidden_size}, 0);
  4446. layer.time_mix_r_k = create_tensor(tn(LLM_TENSOR_TIME_MIX_R_K, "weight", i), {attn_hidden_size}, 0);
  4447. layer.time_mix_key = create_tensor(tn(LLM_TENSOR_TIME_MIX_KEY, "weight", i), {attn_hidden_size, n_embd}, 0);
  4448. layer.time_mix_value = create_tensor(tn(LLM_TENSOR_TIME_MIX_VALUE, "weight", i), {attn_hidden_size, n_embd}, 0);
  4449. layer.time_mix_receptance = create_tensor(tn(LLM_TENSOR_TIME_MIX_RECEPTANCE, "weight", i), {attn_hidden_size, n_embd}, 0);
  4450. layer.time_mix_ln = create_tensor(tn(LLM_TENSOR_TIME_MIX_LN, "weight", i), {n_embd}, TENSOR_NOT_REQUIRED);
  4451. layer.time_mix_ln_b = create_tensor(tn(LLM_TENSOR_TIME_MIX_LN, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  4452. layer.time_mix_output = create_tensor(tn(LLM_TENSOR_TIME_MIX_OUTPUT, "weight", i), {n_embd, attn_hidden_size}, 0);
  4453. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  4454. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  4455. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  4456. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  4457. }
  4458. } break;
  4459. case LLM_ARCH_CHAMELEON:
  4460. {
  4461. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  4462. // output
  4463. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  4464. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  4465. // if output is NULL, init from the input tok embed
  4466. if (output == NULL) {
  4467. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  4468. }
  4469. for (int i = 0; i < n_layer; ++i) {
  4470. auto & layer = layers[i];
  4471. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  4472. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k, n_head}, 0);
  4473. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k, n_head_kv}, 0);
  4474. layer.attn_q_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "bias", i), {n_embd_head_k, n_head}, TENSOR_NOT_REQUIRED);
  4475. 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);
  4476. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  4477. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  4478. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  4479. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  4480. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  4481. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  4482. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  4483. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  4484. }
  4485. } break;
  4486. case LLM_ARCH_WAVTOKENIZER_DEC:
  4487. {
  4488. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {hparams.n_embd_features, n_vocab}, 0);
  4489. conv1d = create_tensor(tn(LLM_TENSOR_CONV1D, "weight"), {7, hparams.n_embd_features, hparams.posnet.n_embd}, 0);
  4490. conv1d_b = create_tensor(tn(LLM_TENSOR_CONV1D, "bias"), {1, hparams.posnet.n_embd}, 0);
  4491. // posnet
  4492. {
  4493. const int64_t n_embd = hparams.posnet.n_embd;
  4494. for (uint32_t i = 0; i < hparams.posnet.n_layer; ++i) {
  4495. auto & layer = layers[i].posnet;
  4496. // posnet:
  4497. //
  4498. // - resnet
  4499. // - resnet
  4500. // - attn
  4501. // - resnet
  4502. // - resnet
  4503. // - norm
  4504. //
  4505. switch (i) {
  4506. case 0:
  4507. case 1:
  4508. case 3:
  4509. case 4:
  4510. {
  4511. layer.norm1 = create_tensor(tn(LLM_TENSOR_POS_NET_NORM1, "weight", i), {1, n_embd}, 0);
  4512. layer.norm1_b = create_tensor(tn(LLM_TENSOR_POS_NET_NORM1, "bias", i), {1, n_embd}, 0);
  4513. layer.conv1 = create_tensor(tn(LLM_TENSOR_POS_NET_CONV1, "weight", i), {3, n_embd, n_embd}, 0);
  4514. layer.conv1_b = create_tensor(tn(LLM_TENSOR_POS_NET_CONV1, "bias", i), {1, n_embd}, 0);
  4515. layer.norm2 = create_tensor(tn(LLM_TENSOR_POS_NET_NORM2, "weight", i), {1, n_embd}, 0);
  4516. layer.norm2_b = create_tensor(tn(LLM_TENSOR_POS_NET_NORM2, "bias", i), {1, n_embd}, 0);
  4517. layer.conv2 = create_tensor(tn(LLM_TENSOR_POS_NET_CONV2, "weight", i), {3, n_embd, n_embd}, 0);
  4518. layer.conv2_b = create_tensor(tn(LLM_TENSOR_POS_NET_CONV2, "bias", i), {1, n_embd}, 0);
  4519. } break;
  4520. case 2:
  4521. {
  4522. layer.attn_norm = create_tensor(tn(LLM_TENSOR_POS_NET_ATTN_NORM, "weight", i), {1, n_embd}, 0);
  4523. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_POS_NET_ATTN_NORM, "bias", i), {1, n_embd}, 0);
  4524. layer.attn_q = create_tensor(tn(LLM_TENSOR_POS_NET_ATTN_Q, "weight", i), {1, n_embd, n_embd}, 0);
  4525. layer.attn_q_b = create_tensor(tn(LLM_TENSOR_POS_NET_ATTN_Q, "bias", i), {1, n_embd}, 0);
  4526. layer.attn_k = create_tensor(tn(LLM_TENSOR_POS_NET_ATTN_K, "weight", i), {1, n_embd, n_embd}, 0);
  4527. layer.attn_k_b = create_tensor(tn(LLM_TENSOR_POS_NET_ATTN_K, "bias", i), {1, n_embd}, 0);
  4528. layer.attn_v = create_tensor(tn(LLM_TENSOR_POS_NET_ATTN_V, "weight", i), {1, n_embd, n_embd}, 0);
  4529. layer.attn_v_b = create_tensor(tn(LLM_TENSOR_POS_NET_ATTN_V, "bias", i), {1, n_embd}, 0);
  4530. layer.attn_o = create_tensor(tn(LLM_TENSOR_POS_NET_ATTN_OUT, "weight", i), {1, n_embd, n_embd}, 0);
  4531. layer.attn_o_b = create_tensor(tn(LLM_TENSOR_POS_NET_ATTN_OUT, "bias", i), {1, n_embd}, 0);
  4532. } break;
  4533. case 5:
  4534. {
  4535. layer.norm = create_tensor(tn(LLM_TENSOR_POS_NET_ATTN_NORM, "weight", i), {1, n_embd}, 0);
  4536. layer.norm_b = create_tensor(tn(LLM_TENSOR_POS_NET_ATTN_NORM, "bias", i), {1, n_embd}, 0);
  4537. } break;
  4538. default: GGML_ABORT("unknown posnet layer");
  4539. };
  4540. }
  4541. }
  4542. GGML_ASSERT(hparams.posnet.n_embd == hparams.convnext.n_embd);
  4543. tok_norm = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "weight"), {hparams.posnet.n_embd}, 0);
  4544. tok_norm_b = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "bias"), {hparams.posnet.n_embd}, 0);
  4545. // convnext
  4546. {
  4547. const int64_t n_embd = hparams.convnext.n_embd;
  4548. for (uint32_t i = 0; i < hparams.convnext.n_layer; ++i) {
  4549. auto & layer = layers[i].convnext;
  4550. layer.dw = create_tensor(tn(LLM_TENSOR_CONVNEXT_DW, "weight", i), {7, 1, n_embd}, 0);
  4551. layer.dw_b = create_tensor(tn(LLM_TENSOR_CONVNEXT_DW, "bias", i), {1, n_embd}, 0);
  4552. layer.norm = create_tensor(tn(LLM_TENSOR_CONVNEXT_NORM, "weight", i), {n_embd}, 0);
  4553. layer.norm_b = create_tensor(tn(LLM_TENSOR_CONVNEXT_NORM, "bias", i), {n_embd}, 0);
  4554. layer.pw1 = create_tensor(tn(LLM_TENSOR_CONVNEXT_PW1, "weight", i), {n_embd, n_ff}, 0);
  4555. layer.pw1_b = create_tensor(tn(LLM_TENSOR_CONVNEXT_PW1, "bias", i), {n_ff}, 0);
  4556. layer.pw2 = create_tensor(tn(LLM_TENSOR_CONVNEXT_PW2, "weight", i), {n_ff, n_embd}, 0);
  4557. layer.pw2_b = create_tensor(tn(LLM_TENSOR_CONVNEXT_PW2, "bias", i), {n_embd}, 0);
  4558. layer.gamma = create_tensor(tn(LLM_TENSOR_CONVNEXT_GAMMA, "weight", i), {n_embd}, 0);
  4559. }
  4560. // output
  4561. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  4562. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
  4563. }
  4564. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {hparams.convnext.n_embd, n_embd}, 0);
  4565. output_b = create_tensor(tn(LLM_TENSOR_OUTPUT, "bias"), {n_embd}, 0);
  4566. } break;
  4567. case LLM_ARCH_BAILINGMOE:
  4568. {
  4569. const int64_t n_ff_exp = hparams.n_ff_exp;
  4570. const int64_t n_expert_shared = hparams.n_expert_shared;
  4571. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  4572. // output
  4573. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  4574. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  4575. for (int i = 0; i < n_layer; ++i) {
  4576. auto & layer = layers[i];
  4577. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  4578. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_head * n_rot}, 0);
  4579. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_head_kv * n_rot}, 0);
  4580. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_head_kv * n_rot}, 0);
  4581. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_head * n_rot, n_embd}, 0);
  4582. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  4583. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  4584. if (n_expert == 0) {
  4585. throw std::runtime_error("n_expert must be > 0");
  4586. }
  4587. if (n_expert_used == 0) {
  4588. throw std::runtime_error("n_expert_used must be > 0");
  4589. }
  4590. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);
  4591. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp, n_embd, n_expert}, 0);
  4592. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);
  4593. layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), {n_embd, n_ff_exp * n_expert_shared}, 0);
  4594. layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), { n_ff_exp * n_expert_shared, n_embd}, 0);
  4595. layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), {n_embd, n_ff_exp * n_expert_shared}, 0);
  4596. }
  4597. } break;
  4598. case LLM_ARCH_DOTS1:
  4599. {
  4600. const int64_t n_ff_exp = hparams.n_ff_exp;
  4601. const int64_t n_expert_shared = hparams.n_expert_shared;
  4602. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  4603. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  4604. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  4605. for (int i = 0; i < n_layer; ++i) {
  4606. auto & layer = layers[i];
  4607. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  4608. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  4609. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  4610. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  4611. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  4612. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0);
  4613. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0);
  4614. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  4615. if (i < (int) hparams.n_layer_dense_lead) {
  4616. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  4617. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  4618. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  4619. } else {
  4620. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  4621. layer.ffn_exp_probs_b = create_tensor(tn(LLM_TENSOR_FFN_EXP_PROBS_B, "bias", i), {n_expert}, TENSOR_NOT_REQUIRED);
  4622. if (n_expert == 0) {
  4623. throw std::runtime_error("n_expert must be > 0");
  4624. }
  4625. if (n_expert_used == 0) {
  4626. throw std::runtime_error("n_expert_used must be > 0");
  4627. }
  4628. // MoE branch
  4629. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);
  4630. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp, n_embd, n_expert}, 0);
  4631. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);
  4632. // Shared expert branch
  4633. layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), {n_embd, n_ff_exp * n_expert_shared}, 0);
  4634. layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), { n_ff_exp * n_expert_shared, n_embd}, 0);
  4635. layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), {n_embd, n_ff_exp * n_expert_shared}, 0);
  4636. }
  4637. }
  4638. } break;
  4639. case LLM_ARCH_ARCEE:
  4640. {
  4641. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  4642. // output
  4643. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  4644. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  4645. // if output is NULL, init from the input tok embed
  4646. if (output == NULL) {
  4647. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  4648. }
  4649. for (int i = 0; i < n_layer; ++i) {
  4650. auto & layer = layers[i];
  4651. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  4652. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  4653. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  4654. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  4655. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  4656. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  4657. layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  4658. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  4659. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  4660. }
  4661. } break;
  4662. case LLM_ARCH_ERNIE4_5:
  4663. case LLM_ARCH_ERNIE4_5_MOE:
  4664. {
  4665. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  4666. // output
  4667. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  4668. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  4669. // if output is NULL, init from the input tok embed
  4670. if (output == NULL) {
  4671. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  4672. }
  4673. for (int i = 0; i < n_layer; ++i) {
  4674. auto & layer = layers[i];
  4675. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  4676. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  4677. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  4678. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  4679. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  4680. // optional bias tensors
  4681. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  4682. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED);
  4683. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED);
  4684. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  4685. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  4686. if (arch == LLM_ARCH_ERNIE4_5_MOE && static_cast<uint32_t>(i) >= hparams.n_layer_dense_lead) { // MoE layers
  4687. int n_ff_exp = hparams.n_ff_exp;
  4688. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  4689. layer.ffn_exp_probs_b = create_tensor(tn(LLM_TENSOR_FFN_EXP_PROBS_B, "bias", i), {n_expert}, TENSOR_NOT_REQUIRED);
  4690. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff_exp, n_expert}, TENSOR_NOT_REQUIRED);
  4691. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), { n_ff_exp, n_embd, n_expert}, 0);
  4692. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff_exp, n_expert}, 0);
  4693. // Shared expert (if present)
  4694. if (hparams.n_ff_shexp > 0) {
  4695. layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), { n_embd, hparams.n_ff_shexp}, 0);
  4696. layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), {hparams.n_ff_shexp, n_embd }, 0);
  4697. layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), { n_embd, hparams.n_ff_shexp}, 0);
  4698. }
  4699. } else { // Dense layers
  4700. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  4701. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  4702. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  4703. }
  4704. }
  4705. } break;
  4706. case LLM_ARCH_FALCON_H1:
  4707. {
  4708. // Common
  4709. const int64_t hidden_size = hparams.n_embd; // hidden_size
  4710. // mamba2 Mixer SSM params
  4711. const int64_t ssm_conv_kernel_size = hparams.ssm_d_conv; // ssm_conv_kernel_size
  4712. const int64_t ssm_n_groups = hparams.ssm_n_group; // ssm_n_groups
  4713. const int64_t ssm_state_size = hparams.ssm_d_state; // ssm_state_size
  4714. const int64_t ssm_intermediate_size = hparams.ssm_d_inner; // TODO expand
  4715. const int64_t ssm_num_heads = hparams.ssm_dt_rank; // ssm_num_heads
  4716. const int64_t ssm_conv_dim = ssm_intermediate_size + 2 * ssm_n_groups * ssm_state_size;
  4717. const int64_t ssm_projection_size = ssm_intermediate_size + ssm_conv_dim + ssm_num_heads;
  4718. // attn params
  4719. const int64_t attn_num_attention_head = hparams.n_head(0); // rename to: attn_num_attention_head
  4720. const int64_t attn_num_key_value_head = hparams.n_head_kv(0);
  4721. // ffn params
  4722. const int64_t ffn_intermediate_size = hparams.n_ff(0);
  4723. // embeddings
  4724. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {hidden_size, n_vocab}, 0);
  4725. // output
  4726. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {hidden_size, n_vocab}, TENSOR_NOT_REQUIRED);
  4727. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {hidden_size}, 0);
  4728. // if output is NULL, init from the input tok embed
  4729. if (output == NULL) {
  4730. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {hidden_size, n_vocab}, TENSOR_DUPLICATED);
  4731. }
  4732. for (int i = 0; i < n_layer; ++i) {
  4733. auto & layer = layers[i];
  4734. /*SSM LAYERS*/
  4735. // ssm in
  4736. layer.ssm_in = create_tensor(tn(LLM_TENSOR_SSM_IN, "weight", i), {hidden_size, ssm_projection_size}, 0);
  4737. // ssm 1d conv
  4738. layer.ssm_conv1d = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "weight", i), {ssm_conv_kernel_size, ssm_conv_dim}, 0);
  4739. layer.ssm_conv1d_b = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "bias", i), {ssm_conv_dim}, TENSOR_NOT_REQUIRED);
  4740. // ssm_dt
  4741. layer.ssm_dt_b = create_tensor(tn(LLM_TENSOR_SSM_DT, "bias", i), {ssm_num_heads}, 0);
  4742. // no "weight" suffix for these
  4743. layer.ssm_a = create_tensor(tn(LLM_TENSOR_SSM_A, i), {1, ssm_num_heads}, 0);
  4744. layer.ssm_d = create_tensor(tn(LLM_TENSOR_SSM_D, i), {1, ssm_num_heads}, 0);
  4745. // ssm_norm
  4746. layer.ssm_norm = create_tensor(tn(LLM_TENSOR_SSM_NORM, "weight", i), {ssm_intermediate_size / ssm_n_groups, ssm_n_groups}, TENSOR_NOT_REQUIRED);
  4747. // out_proj
  4748. layer.ssm_out = create_tensor(tn(LLM_TENSOR_SSM_OUT, "weight", i), {ssm_intermediate_size, hidden_size}, 0);
  4749. /*ATTENTION LAYERS*/
  4750. // attention layers (with optional bias)
  4751. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {hidden_size, n_embd_head_k * attn_num_attention_head}, 0);
  4752. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {hidden_size, attn_num_key_value_head * n_embd_head_k}, 0);
  4753. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {hidden_size, attn_num_key_value_head * n_embd_head_v}, 0);
  4754. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * attn_num_attention_head, hidden_size}, 0);
  4755. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {hidden_size}, TENSOR_NOT_REQUIRED);
  4756. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {attn_num_key_value_head * n_embd_head_k}, TENSOR_NOT_REQUIRED);
  4757. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {attn_num_key_value_head * n_embd_head_v}, TENSOR_NOT_REQUIRED);
  4758. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {hidden_size}, TENSOR_NOT_REQUIRED);
  4759. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {hidden_size}, 0);
  4760. // feed forward (w/ optional biases)
  4761. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, i), {hidden_size}, 0);
  4762. layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  4763. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {hidden_size, ffn_intermediate_size}, 0);
  4764. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { ffn_intermediate_size, hidden_size}, 0);
  4765. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {hidden_size, ffn_intermediate_size}, 0);
  4766. layer.ffn_gate_b = create_tensor(tn(LLM_TENSOR_FFN_GATE, "bias", i), {ffn_intermediate_size}, TENSOR_NOT_REQUIRED);
  4767. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {hidden_size}, TENSOR_NOT_REQUIRED);
  4768. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {ffn_intermediate_size}, TENSOR_NOT_REQUIRED);
  4769. }
  4770. } break;
  4771. case LLM_ARCH_HUNYUAN_MOE:
  4772. {
  4773. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  4774. // output
  4775. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  4776. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  4777. // if output is NULL, init from the input tok embed
  4778. if (output == NULL) {
  4779. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  4780. }
  4781. for (int i = 0; i < n_layer; ++i) {
  4782. auto & layer = layers[i];
  4783. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  4784. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  4785. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  4786. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  4787. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  4788. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0);
  4789. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0);
  4790. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  4791. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  4792. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0);
  4793. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), { n_ff, n_embd, n_expert}, 0);
  4794. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0);
  4795. layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), {n_embd, hparams.n_ff_shexp}, 0);
  4796. layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), {n_embd, hparams.n_ff_shexp}, 0);
  4797. layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), {hparams.n_ff_shexp, n_embd}, 0);
  4798. }
  4799. } break;
  4800. case LLM_ARCH_HUNYUAN_DENSE:
  4801. {
  4802. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  4803. // output
  4804. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  4805. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  4806. // if output is NULL, init from the input tok embed
  4807. if (output == NULL) {
  4808. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  4809. }
  4810. for (int i = 0; i < n_layer; ++i) {
  4811. auto & layer = layers[i];
  4812. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  4813. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  4814. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  4815. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  4816. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  4817. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0);
  4818. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0);
  4819. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  4820. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  4821. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  4822. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  4823. }
  4824. } break;
  4825. case LLM_ARCH_SMOLLM3:
  4826. {
  4827. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  4828. // output
  4829. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  4830. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  4831. // if output is NULL, init from the input tok embed
  4832. if (output == NULL) {
  4833. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  4834. }
  4835. for (int i = 0; i < n_layer; ++i) {
  4836. auto & layer = layers[i];
  4837. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  4838. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  4839. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  4840. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  4841. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  4842. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  4843. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  4844. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  4845. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  4846. }
  4847. } break;
  4848. case LLM_ARCH_OPENAI_MOE:
  4849. {
  4850. const int64_t n_ff_exp = hparams.n_ff_exp;
  4851. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  4852. // output
  4853. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  4854. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  4855. for (int i = 0; i < n_layer; ++i) {
  4856. auto & layer = layers[i];
  4857. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  4858. layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), {n_embd}, 0);
  4859. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_head * n_rot}, 0);
  4860. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_head_kv * n_rot}, 0);
  4861. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_head_kv * n_rot}, 0);
  4862. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_head * n_rot, n_embd}, 0);
  4863. layer.attn_sinks = create_tensor(tn(LLM_TENSOR_ATTN_SINKS, "weight", i), {n_head}, 0);
  4864. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), { n_embd, n_expert}, 0);
  4865. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);
  4866. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp, n_embd, n_expert}, 0);
  4867. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);
  4868. // bias
  4869. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_head * n_rot}, 0);
  4870. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_head_kv * n_rot}, 0);
  4871. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_head_kv * n_rot}, 0);
  4872. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0);
  4873. layer.ffn_gate_inp_b = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "bias", i), {n_expert}, 0);
  4874. layer.ffn_gate_exps_b = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "bias", i), {n_ff_exp, n_expert}, 0);
  4875. layer.ffn_down_exps_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "bias", i), { n_embd, n_expert}, 0);
  4876. layer.ffn_up_exps_b = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "bias", i), {n_ff_exp, n_expert}, 0);
  4877. }
  4878. } break;
  4879. case LLM_ARCH_LFM2:
  4880. {
  4881. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  4882. tok_norm = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "weight"), {n_embd}, 0);
  4883. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  4884. if (output == NULL) {
  4885. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  4886. }
  4887. for (int i = 0; i < n_layer; ++i) {
  4888. auto & layer = layers[i];
  4889. // ffn is same for transformer and conv layers
  4890. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  4891. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  4892. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  4893. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  4894. // for operator_norm
  4895. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  4896. if (!hparams.is_recurrent(i)) {
  4897. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0);
  4898. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0);
  4899. GGML_ASSERT(n_embd_v_gqa == n_embd_k_gqa);
  4900. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  4901. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, hparams.n_embd_k_gqa(i)}, 0);
  4902. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, hparams.n_embd_v_gqa(i)}, 0);
  4903. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  4904. } else {
  4905. layer.shortconv.conv = create_tensor(tn(LLM_TENSOR_SHORTCONV_CONV, "weight", i), {hparams.n_shortconv_l_cache, n_embd}, 0);
  4906. layer.shortconv.in_proj = create_tensor(tn(LLM_TENSOR_SHORTCONV_INPROJ, "weight", i), {n_embd, 3 * n_embd}, 0);
  4907. layer.shortconv.out_proj = create_tensor(tn(LLM_TENSOR_SHORTCONV_OUTPROJ, "weight", i), {n_embd, n_embd}, 0);
  4908. }
  4909. }
  4910. } break;
  4911. case LLM_ARCH_SMALLTHINKER:
  4912. {
  4913. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, 0);
  4914. // output
  4915. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), { n_embd }, 0);
  4916. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  4917. // if output is NULL, init from the input tok embed
  4918. if (output == NULL) {
  4919. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  4920. }
  4921. for (int i = 0; i < n_layer; ++i) {
  4922. auto & layer = layers[i];
  4923. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), { n_embd }, 0);
  4924. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), { n_embd, n_embd_head_k * n_head }, 0);
  4925. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), { n_embd, n_embd_gqa }, 0);
  4926. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), { n_embd, n_embd_gqa }, 0);
  4927. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), { n_embd_head_k * n_head, n_embd }, 0);
  4928. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), { n_embd }, 0);
  4929. GGML_ASSERT(n_expert > 0 && "n_expert must be > 0 for SMALLTHINKER");
  4930. GGML_ASSERT(n_expert_used > 0 && "n_expert_used must be > 0 for SMALLTHINKER");
  4931. // MoE branch
  4932. const int64_t n_ff_exp = hparams.n_ff_exp;
  4933. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), { n_embd, n_expert }, 0);
  4934. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert }, 0);
  4935. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), { n_ff_exp, n_embd, n_expert }, 0);
  4936. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert }, 0);
  4937. }
  4938. } break;
  4939. default:
  4940. throw std::runtime_error("unknown architecture");
  4941. }
  4942. if (n_moved_tensors > 0) {
  4943. LLAMA_LOG_DEBUG("%s: tensor '%s' (%s) (and %d others) cannot be used with preferred buffer type %s, using %s instead\n",
  4944. __func__, first_moved_tensor->name, ggml_type_name(first_moved_tensor->type), n_moved_tensors - 1,
  4945. ggml_backend_buft_name(first_moved_from_buft), ggml_backend_buft_name(first_moved_to_buft));
  4946. }
  4947. }
  4948. ml.done_getting_tensors();
  4949. ml.init_mappings(true, use_mlock ? &pimpl->mlock_mmaps : nullptr);
  4950. pimpl->mappings.reserve(ml.mappings.size());
  4951. // create the backend buffers
  4952. std::vector<std::pair<ggml_context *, llama_buf_map>> ctx_bufs;
  4953. ctx_bufs.reserve(ctx_map.size());
  4954. // Ensure we have enough capacity for the maximum backend buffer we will potentially create
  4955. const size_t n_max_backend_buffer = ctx_map.size() * ml.files.size();
  4956. pimpl->bufs.reserve(n_max_backend_buffer);
  4957. for (auto & it : ctx_map) {
  4958. ggml_backend_buffer_type_t buft = it.first;
  4959. ggml_context * ctx = it.second;
  4960. // skip contexts without tensors
  4961. if (ggml_get_first_tensor(ctx) == nullptr) {
  4962. continue;
  4963. }
  4964. llama_buf_map buf_map;
  4965. buf_map.reserve(n_max_backend_buffer);
  4966. // check if it is possible to use buffer_from_host_ptr with this buffer type
  4967. ggml_backend_dev_t dev = ggml_backend_buft_get_device(buft);
  4968. if (!dev) {
  4969. // FIXME: workaround for CPU backend buft having a NULL device
  4970. dev = ggml_backend_dev_by_type(GGML_BACKEND_DEVICE_TYPE_CPU);
  4971. if (!dev) {
  4972. throw std::runtime_error(format("%s: no CPU backend found", __func__));
  4973. }
  4974. }
  4975. ggml_backend_dev_props props;
  4976. ggml_backend_dev_get_props(dev, &props);
  4977. bool buffer_from_host_ptr_supported = props.caps.buffer_from_host_ptr;
  4978. bool is_default_buft = buft == ggml_backend_dev_buffer_type(dev);
  4979. if (ml.use_mmap && use_mmap_buffer && buffer_from_host_ptr_supported && is_default_buft) {
  4980. for (uint32_t idx = 0; idx < ml.files.size(); idx++) {
  4981. // only the mmap region containing the tensors in the model is mapped to the backend buffer
  4982. // 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
  4983. // this allows using partial offloading when the model size exceeds the metal buffer size, but not the RAM size
  4984. void * addr = nullptr;
  4985. size_t first, last; // NOLINT
  4986. ml.get_mapping_range(&first, &last, &addr, idx, ctx);
  4987. if (first >= last) {
  4988. continue;
  4989. }
  4990. const size_t max_size = ggml_get_max_tensor_size(ctx);
  4991. ggml_backend_buffer_t buf = ggml_backend_dev_buffer_from_host_ptr(dev, (char *) addr + first, last - first, max_size);
  4992. if (buf == nullptr) {
  4993. throw std::runtime_error(format("unable to allocate %s buffer", ggml_backend_buft_name(buft)));
  4994. }
  4995. pimpl->bufs.emplace_back(buf);
  4996. buf_map.emplace(idx, buf);
  4997. }
  4998. }
  4999. else {
  5000. ggml_backend_buffer_t buf = ggml_backend_alloc_ctx_tensors_from_buft(ctx, buft);
  5001. if (buf == nullptr) {
  5002. throw std::runtime_error(format("unable to allocate %s buffer", ggml_backend_buft_name(buft)));
  5003. }
  5004. pimpl->bufs.emplace_back(buf);
  5005. if (use_mlock && ggml_backend_buffer_is_host(buf)) {
  5006. pimpl->mlock_bufs.emplace_back(new llama_mlock);
  5007. auto & mlock_buf = pimpl->mlock_bufs.back();
  5008. mlock_buf->init (ggml_backend_buffer_get_base(buf));
  5009. mlock_buf->grow_to(ggml_backend_buffer_get_size(buf));
  5010. }
  5011. for (uint32_t idx = 0; idx < ml.files.size(); idx++) {
  5012. buf_map.emplace(idx, buf);
  5013. }
  5014. }
  5015. if (pimpl->bufs.empty()) {
  5016. throw std::runtime_error("failed to allocate buffer");
  5017. }
  5018. for (auto & buf : buf_map) {
  5019. // indicate that this buffer contains weights
  5020. // 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
  5021. ggml_backend_buffer_set_usage(buf.second, GGML_BACKEND_BUFFER_USAGE_WEIGHTS);
  5022. }
  5023. ctx_bufs.emplace_back(ctx, buf_map);
  5024. }
  5025. if (llama_supports_gpu_offload()) {
  5026. const int n_gpu = std::min(n_gpu_layers, int(hparams.n_layer));
  5027. LLAMA_LOG_INFO("%s: offloading %d repeating layers to GPU\n", __func__, n_gpu);
  5028. if (n_gpu_layers > (int) hparams.n_layer) {
  5029. LLAMA_LOG_INFO("%s: offloading output layer to GPU\n", __func__);
  5030. }
  5031. const int max_backend_supported_layers = hparams.n_layer + 1;
  5032. const int max_offloadable_layers = hparams.n_layer + 1;
  5033. LLAMA_LOG_INFO("%s: offloaded %d/%d layers to GPU\n", __func__, std::min(n_gpu_layers, max_offloadable_layers), max_backend_supported_layers);
  5034. }
  5035. // print memory requirements per buffer type
  5036. for (auto & buf : pimpl->bufs) {
  5037. 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);
  5038. }
  5039. // populate tensors_by_name
  5040. for (auto & ctx : pimpl->ctxs) {
  5041. for (auto * cur = ggml_get_first_tensor(ctx.get()); cur != NULL; cur = ggml_get_next_tensor(ctx.get(), cur)) {
  5042. tensors_by_name.emplace_back(ggml_get_name(cur), cur);
  5043. }
  5044. }
  5045. // load tensor data
  5046. for (auto & it : ctx_bufs) {
  5047. ggml_context * ctx = it.first;
  5048. auto & bufs = it.second;
  5049. if (!ml.load_all_data(ctx, bufs, use_mlock ? &pimpl->mlock_mmaps : NULL, params.progress_callback, params.progress_callback_user_data)) {
  5050. return false;
  5051. }
  5052. }
  5053. if (use_mmap_buffer) {
  5054. for (auto & mapping : ml.mappings) {
  5055. pimpl->mappings.emplace_back(std::move(mapping));
  5056. }
  5057. }
  5058. return true;
  5059. }
  5060. std::string llama_model::arch_name() const {
  5061. return llm_arch_name(arch);
  5062. }
  5063. std::string llama_model::type_name() const {
  5064. return llm_type_name(type);
  5065. }
  5066. std::string llama_model::desc() const {
  5067. return pimpl->desc_str;
  5068. }
  5069. size_t llama_model::size() const {
  5070. return pimpl->n_bytes;
  5071. }
  5072. size_t llama_model::n_tensors() const {
  5073. return tensors_by_name.size();
  5074. }
  5075. size_t llama_model::n_devices() const {
  5076. return devices.size();
  5077. }
  5078. uint64_t llama_model::n_elements() const {
  5079. return pimpl->n_elements;
  5080. }
  5081. void llama_model::print_info() const {
  5082. const std::string rope_scaling_type = llama_rope_scaling_type_name(hparams.rope_scaling_type_train);
  5083. auto print_f = [](const std::function<uint32_t(uint32_t)> & f, uint32_t n) {
  5084. bool is_var = false;
  5085. std::vector<uint32_t> v;
  5086. for (uint32_t i = 0; i < n; ++i) {
  5087. v.push_back(f(i));
  5088. if (v[i] != v[0]) {
  5089. is_var = true;
  5090. }
  5091. }
  5092. std::stringstream ss;
  5093. if (is_var) {
  5094. ss << "[";
  5095. for (uint32_t i = 0; i < n; ++i) {
  5096. ss << v[i];
  5097. if (i < n - 1) {
  5098. ss << ", ";
  5099. }
  5100. }
  5101. ss << "]";
  5102. } else {
  5103. ss << v[0];
  5104. }
  5105. return ss.str();
  5106. };
  5107. // hparams
  5108. LLAMA_LOG_INFO("%s: arch = %s\n", __func__, arch_name().c_str());
  5109. LLAMA_LOG_INFO("%s: vocab_only = %d\n", __func__, hparams.vocab_only);
  5110. if (!hparams.vocab_only) {
  5111. LLAMA_LOG_INFO("%s: n_ctx_train = %u\n", __func__, hparams.n_ctx_train);
  5112. LLAMA_LOG_INFO("%s: n_embd = %u\n", __func__, hparams.n_embd);
  5113. LLAMA_LOG_INFO("%s: n_layer = %u\n", __func__, hparams.n_layer);
  5114. LLAMA_LOG_INFO("%s: n_head = %s\n", __func__, print_f([&](uint32_t il) { return hparams.n_head(il); }, hparams.n_layer).c_str());
  5115. 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());
  5116. LLAMA_LOG_INFO("%s: n_rot = %u\n", __func__, hparams.n_rot);
  5117. LLAMA_LOG_INFO("%s: n_swa = %u\n", __func__, hparams.n_swa);
  5118. LLAMA_LOG_INFO("%s: is_swa_any = %u\n", __func__, hparams.is_swa_any());
  5119. LLAMA_LOG_INFO("%s: n_embd_head_k = %u\n", __func__, hparams.n_embd_head_k);
  5120. LLAMA_LOG_INFO("%s: n_embd_head_v = %u\n", __func__, hparams.n_embd_head_v);
  5121. LLAMA_LOG_INFO("%s: n_gqa = %s\n", __func__, print_f([&](uint32_t il) { return hparams.n_gqa(il); }, hparams.n_layer).c_str());
  5122. 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());
  5123. 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());
  5124. LLAMA_LOG_INFO("%s: f_norm_eps = %.1e\n", __func__, hparams.f_norm_eps);
  5125. LLAMA_LOG_INFO("%s: f_norm_rms_eps = %.1e\n", __func__, hparams.f_norm_rms_eps);
  5126. LLAMA_LOG_INFO("%s: f_clamp_kqv = %.1e\n", __func__, hparams.f_clamp_kqv);
  5127. LLAMA_LOG_INFO("%s: f_max_alibi_bias = %.1e\n", __func__, hparams.f_max_alibi_bias);
  5128. LLAMA_LOG_INFO("%s: f_logit_scale = %.1e\n", __func__, hparams.f_logit_scale);
  5129. LLAMA_LOG_INFO("%s: f_attn_scale = %.1e\n", __func__, hparams.f_attention_scale);
  5130. LLAMA_LOG_INFO("%s: n_ff = %s\n", __func__, print_f([&](uint32_t il) { return hparams.n_ff(il); }, hparams.n_layer).c_str());
  5131. LLAMA_LOG_INFO("%s: n_expert = %u\n", __func__, hparams.n_expert);
  5132. LLAMA_LOG_INFO("%s: n_expert_used = %u\n", __func__, hparams.n_expert_used);
  5133. LLAMA_LOG_INFO("%s: causal attn = %d\n", __func__, hparams.causal_attn);
  5134. LLAMA_LOG_INFO("%s: pooling type = %d\n", __func__, hparams.pooling_type);
  5135. LLAMA_LOG_INFO("%s: rope type = %d\n", __func__, hparams.rope_type);
  5136. LLAMA_LOG_INFO("%s: rope scaling = %s\n", __func__, rope_scaling_type.c_str());
  5137. LLAMA_LOG_INFO("%s: freq_base_train = %.1f\n", __func__, hparams.rope_freq_base_train);
  5138. LLAMA_LOG_INFO("%s: freq_scale_train = %g\n", __func__, hparams.rope_freq_scale_train);
  5139. LLAMA_LOG_INFO("%s: n_ctx_orig_yarn = %u\n", __func__, hparams.n_ctx_orig_yarn);
  5140. LLAMA_LOG_INFO("%s: rope_finetuned = %s\n", __func__, hparams.rope_finetuned ? "yes" : "unknown");
  5141. if (!classifier_labels.empty()) {
  5142. LLAMA_LOG_INFO("%s: n_cls_out = %u\n", __func__, hparams.n_cls_out);
  5143. size_t i = 0;
  5144. for (auto label : classifier_labels) {
  5145. LLAMA_LOG_INFO("%s: cls_label[%2zu] = %s\n", __func__, i++, label.c_str());
  5146. }
  5147. }
  5148. }
  5149. if (arch == LLM_ARCH_MAMBA ||
  5150. arch == LLM_ARCH_MAMBA2 ||
  5151. arch == LLM_ARCH_JAMBA ||
  5152. arch == LLM_ARCH_FALCON_H1 ||
  5153. arch == LLM_ARCH_PLAMO2 ||
  5154. arch == LLM_ARCH_GRANITE_HYBRID ||
  5155. arch == LLM_ARCH_NEMOTRON_H ||
  5156. arch == LLM_ARCH_QWEN3NEXT) {
  5157. LLAMA_LOG_INFO("%s: ssm_d_conv = %u\n", __func__, hparams.ssm_d_conv);
  5158. LLAMA_LOG_INFO("%s: ssm_d_inner = %u\n", __func__, hparams.ssm_d_inner);
  5159. LLAMA_LOG_INFO("%s: ssm_d_state = %u\n", __func__, hparams.ssm_d_state);
  5160. LLAMA_LOG_INFO("%s: ssm_dt_rank = %u\n", __func__, hparams.ssm_dt_rank);
  5161. LLAMA_LOG_INFO("%s: ssm_n_group = %u\n", __func__, hparams.ssm_n_group);
  5162. LLAMA_LOG_INFO("%s: ssm_dt_b_c_rms = %d\n", __func__, hparams.ssm_dt_b_c_rms);
  5163. }
  5164. LLAMA_LOG_INFO("%s: model type = %s\n", __func__, type_name().c_str());
  5165. if (pimpl->n_elements >= 1e12) {
  5166. LLAMA_LOG_INFO("%s: model params = %.2f T\n", __func__, pimpl->n_elements*1e-12);
  5167. } else if (pimpl->n_elements >= 1e9) {
  5168. LLAMA_LOG_INFO("%s: model params = %.2f B\n", __func__, pimpl->n_elements*1e-9);
  5169. } else if (pimpl->n_elements >= 1e6) {
  5170. LLAMA_LOG_INFO("%s: model params = %.2f M\n", __func__, pimpl->n_elements*1e-6);
  5171. } else {
  5172. LLAMA_LOG_INFO("%s: model params = %.2f K\n", __func__, pimpl->n_elements*1e-3);
  5173. }
  5174. // general kv
  5175. LLAMA_LOG_INFO("%s: general.name = %s\n", __func__, name.c_str());
  5176. if (arch == LLM_ARCH_DEEPSEEK) {
  5177. LLAMA_LOG_INFO("%s: n_layer_dense_lead = %d\n", __func__, hparams.n_layer_dense_lead);
  5178. LLAMA_LOG_INFO("%s: n_ff_exp = %d\n", __func__, hparams.n_ff_exp);
  5179. LLAMA_LOG_INFO("%s: n_expert_shared = %d\n", __func__, hparams.n_expert_shared);
  5180. LLAMA_LOG_INFO("%s: expert_weights_scale = %.1f\n", __func__, hparams.expert_weights_scale);
  5181. }
  5182. if (arch == LLM_ARCH_DEEPSEEK2) {
  5183. LLAMA_LOG_INFO("%s: n_layer_dense_lead = %d\n", __func__, hparams.n_layer_dense_lead);
  5184. LLAMA_LOG_INFO("%s: n_lora_q = %d\n", __func__, hparams.n_lora_q);
  5185. LLAMA_LOG_INFO("%s: n_lora_kv = %d\n", __func__, hparams.n_lora_kv);
  5186. LLAMA_LOG_INFO("%s: n_embd_head_k_mla = %d\n", __func__, hparams.n_embd_head_k_mla);
  5187. LLAMA_LOG_INFO("%s: n_embd_head_v_mla = %d\n", __func__, hparams.n_embd_head_v_mla);
  5188. LLAMA_LOG_INFO("%s: n_ff_exp = %d\n", __func__, hparams.n_ff_exp);
  5189. LLAMA_LOG_INFO("%s: n_expert_shared = %d\n", __func__, hparams.n_expert_shared);
  5190. LLAMA_LOG_INFO("%s: expert_weights_scale = %.1f\n", __func__, hparams.expert_weights_scale);
  5191. LLAMA_LOG_INFO("%s: expert_weights_norm = %d\n", __func__, hparams.expert_weights_norm);
  5192. LLAMA_LOG_INFO("%s: expert_gating_func = %s\n", __func__, llama_expert_gating_func_name((llama_expert_gating_func_type) hparams.expert_gating_func));
  5193. LLAMA_LOG_INFO("%s: rope_yarn_log_mul = %.4f\n", __func__, hparams.rope_yarn_log_mul);
  5194. }
  5195. if (arch == LLM_ARCH_QWEN2MOE) {
  5196. LLAMA_LOG_INFO("%s: n_ff_exp = %d\n", __func__, hparams.n_ff_exp);
  5197. LLAMA_LOG_INFO("%s: n_ff_shexp = %d\n", __func__, hparams.n_ff_shexp);
  5198. }
  5199. if (arch == LLM_ARCH_QWEN3MOE || arch == LLM_ARCH_OPENAI_MOE) {
  5200. LLAMA_LOG_INFO("%s: n_ff_exp = %d\n", __func__, hparams.n_ff_exp);
  5201. }
  5202. if (arch == LLM_ARCH_MINICPM ||
  5203. arch == LLM_ARCH_GRANITE ||
  5204. arch == LLM_ARCH_GRANITE_MOE ||
  5205. arch == LLM_ARCH_GRANITE_HYBRID) {
  5206. LLAMA_LOG_INFO("%s: f_embedding_scale = %f\n", __func__, hparams.f_embedding_scale);
  5207. LLAMA_LOG_INFO("%s: f_residual_scale = %f\n", __func__, hparams.f_residual_scale);
  5208. LLAMA_LOG_INFO("%s: f_attention_scale = %f\n", __func__, hparams.f_attention_scale);
  5209. LLAMA_LOG_INFO("%s: n_ff_shexp = %d\n", __func__, hparams.n_ff_shexp);
  5210. }
  5211. if (arch == LLM_ARCH_BAILINGMOE) {
  5212. LLAMA_LOG_INFO("%s: n_layer_dense_lead = %d\n", __func__, hparams.n_layer_dense_lead);
  5213. LLAMA_LOG_INFO("%s: n_ff_exp = %d\n", __func__, hparams.n_ff_exp);
  5214. LLAMA_LOG_INFO("%s: n_expert_shared = %d\n", __func__, hparams.n_expert_shared);
  5215. LLAMA_LOG_INFO("%s: expert_weights_scale = %.1f\n", __func__, hparams.expert_weights_scale);
  5216. LLAMA_LOG_INFO("%s: expert_weights_norm = %d\n", __func__, hparams.expert_weights_norm);
  5217. }
  5218. if (arch == LLM_ARCH_SMALLTHINKER) {
  5219. LLAMA_LOG_INFO("%s: n_ff_exp = %d\n", __func__, hparams.n_ff_exp);
  5220. LLAMA_LOG_INFO("%s: expert_gating_func = %s\n", __func__, llama_expert_gating_func_name((llama_expert_gating_func_type) hparams.expert_gating_func));
  5221. }
  5222. vocab.print_info();
  5223. }
  5224. ggml_backend_dev_t llama_model::dev_layer(int il) const {
  5225. return pimpl->dev_layer.at(il).dev;
  5226. }
  5227. ggml_backend_dev_t llama_model::dev_output() const {
  5228. return pimpl->dev_output.dev;
  5229. }
  5230. template<typename F>
  5231. static bool buft_supported(ggml_backend_buffer_type_t buft, ggml_backend_dev_t dev, F & fn) {
  5232. ggml_init_params params = {
  5233. /*.mem_size =*/ ggml_tensor_overhead()*8,
  5234. /*.mem_buffer =*/ NULL,
  5235. /*.no_alloc =*/ true,
  5236. };
  5237. ggml_context_ptr ctx { ggml_init(params) };
  5238. if (!ctx) {
  5239. throw std::runtime_error(format("failed to create ggml context"));
  5240. }
  5241. ggml_backend_buffer_ptr buf { ggml_backend_buft_alloc_buffer(buft, 0) };
  5242. ggml_tensor * op_tensor = fn(ctx.get());
  5243. for (int i = 0; i < GGML_MAX_SRC; i++) {
  5244. if (op_tensor->src[i] != nullptr) {
  5245. assert(op_tensor->src[i]->buffer == nullptr);
  5246. op_tensor->src[i]->buffer = buf.get();
  5247. }
  5248. }
  5249. bool op_supported = ggml_backend_dev_supports_op(dev, op_tensor);
  5250. return op_supported;
  5251. }
  5252. template<typename F>
  5253. static ggml_backend_buffer_type_t select_buft(const buft_list_t & buft_list, const F & fn) {
  5254. for (const auto & cur : buft_list) {
  5255. ggml_backend_dev_t cur_dev = cur.first;
  5256. ggml_backend_buffer_type_t cur_buft = cur.second;
  5257. if (buft_supported(cur_buft, cur_dev, fn)) {
  5258. return cur_buft;
  5259. }
  5260. }
  5261. throw std::runtime_error(format("no suitable buffer type found"));
  5262. }
  5263. ggml_backend_buffer_type_t llama_model::select_buft(int il) const {
  5264. return ::select_buft(
  5265. *pimpl->dev_layer.at(il).buft_list,
  5266. [&](ggml_context * ctx) {
  5267. ggml_tensor * cur = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, hparams.n_embd);
  5268. ggml_tensor * layer_dir = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, hparams.n_embd);
  5269. return ggml_add(ctx, cur, layer_dir);
  5270. });
  5271. }
  5272. bool llama_model::has_tensor_overrides() const {
  5273. return pimpl->has_tensor_overrides;
  5274. }
  5275. const ggml_tensor * llama_model::get_tensor(const char * name) const {
  5276. auto it = std::find_if(tensors_by_name.begin(), tensors_by_name.end(),
  5277. [name](const std::pair<std::string, ggml_tensor *> & it) {
  5278. return it.first == name;
  5279. });
  5280. if (it == tensors_by_name.end()) {
  5281. return nullptr;
  5282. }
  5283. return it->second;
  5284. }
  5285. float llama_model::get_rope_freq_base (const llama_cparams & cparams, int il) const {
  5286. return hparams.is_swa(il) ? hparams.rope_freq_base_train_swa : cparams.rope_freq_base;
  5287. }
  5288. float llama_model::get_rope_freq_scale(const llama_cparams & cparams, int il) const {
  5289. return hparams.is_swa(il) ? hparams.rope_freq_scale_train_swa : cparams.rope_freq_scale;
  5290. }
  5291. ggml_tensor * llama_model::get_rope_factors(const llama_cparams & cparams, int il) const {
  5292. const uint32_t n_ctx_per_seq = cparams.n_ctx / cparams.n_seq_max;
  5293. // choose long/short freq factors based on the context size
  5294. if (layers[il].rope_freqs != nullptr) {
  5295. return layers[il].rope_freqs;
  5296. }
  5297. if (n_ctx_per_seq > hparams.n_ctx_orig_yarn) {
  5298. return layers[il].rope_long;
  5299. }
  5300. return layers[il].rope_short;
  5301. }
  5302. struct llm_build_llama : public llm_graph_context {
  5303. llm_build_llama(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  5304. const int64_t n_embd_head = hparams.n_embd_head_v;
  5305. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  5306. GGML_ASSERT(n_embd_head == hparams.n_rot);
  5307. ggml_tensor * cur;
  5308. ggml_tensor * inpL;
  5309. inpL = build_inp_embd(model.tok_embd);
  5310. // inp_pos - contains the positions
  5311. ggml_tensor * inp_pos = build_inp_pos();
  5312. auto * inp_attn = build_attn_inp_kv();
  5313. const float kq_scale = hparams.f_attention_scale == 0.0f ? 1.0f/sqrtf(float(n_embd_head)) : hparams.f_attention_scale;
  5314. ggml_tensor * inp_out_ids = build_inp_out_ids();
  5315. for (int il = 0; il < n_layer; ++il) {
  5316. ggml_tensor * inpSA = inpL;
  5317. // norm
  5318. cur = build_norm(inpL,
  5319. model.layers[il].attn_norm, NULL,
  5320. LLM_NORM_RMS, il);
  5321. cb(cur, "attn_norm", il);
  5322. // self-attention
  5323. {
  5324. // rope freq factors for llama3; may return nullptr for llama2 and other models
  5325. ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
  5326. // compute Q and K and RoPE them
  5327. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  5328. cb(Qcur, "Qcur", il);
  5329. if (model.layers[il].bq) {
  5330. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  5331. cb(Qcur, "Qcur", il);
  5332. }
  5333. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  5334. cb(Kcur, "Kcur", il);
  5335. if (model.layers[il].bk) {
  5336. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  5337. cb(Kcur, "Kcur", il);
  5338. }
  5339. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  5340. cb(Vcur, "Vcur", il);
  5341. if (model.layers[il].bv) {
  5342. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  5343. cb(Vcur, "Vcur", il);
  5344. }
  5345. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  5346. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  5347. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  5348. Qcur = ggml_rope_ext(
  5349. ctx0, Qcur, inp_pos, rope_factors,
  5350. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  5351. ext_factor, attn_factor, beta_fast, beta_slow
  5352. );
  5353. Kcur = ggml_rope_ext(
  5354. ctx0, Kcur, inp_pos, rope_factors,
  5355. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  5356. ext_factor, attn_factor, beta_fast, beta_slow
  5357. );
  5358. cb(Qcur, "Qcur", il);
  5359. cb(Kcur, "Kcur", il);
  5360. cb(Vcur, "Vcur", il);
  5361. if (hparams.use_kq_norm) {
  5362. // Llama4TextL2Norm
  5363. Qcur = ggml_rms_norm(ctx0, Qcur, hparams.f_norm_rms_eps);
  5364. Kcur = ggml_rms_norm(ctx0, Kcur, hparams.f_norm_rms_eps);
  5365. cb(Qcur, "Qcur_normed", il);
  5366. cb(Kcur, "Kcur_normed", il);
  5367. }
  5368. cur = build_attn(inp_attn,
  5369. model.layers[il].wo, model.layers[il].bo,
  5370. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
  5371. cb(cur, "attn_out", il);
  5372. }
  5373. if (il == n_layer - 1 && inp_out_ids) {
  5374. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  5375. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  5376. }
  5377. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  5378. cb(ffn_inp, "ffn_inp", il);
  5379. // feed-forward network (non-MoE)
  5380. if (model.layers[il].ffn_gate_inp == nullptr) {
  5381. cur = build_norm(ffn_inp,
  5382. model.layers[il].ffn_norm, NULL,
  5383. LLM_NORM_RMS, il);
  5384. cb(cur, "ffn_norm", il);
  5385. cur = build_ffn(cur,
  5386. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  5387. model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, NULL,
  5388. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  5389. NULL,
  5390. LLM_FFN_SILU, LLM_FFN_PAR, il);
  5391. cb(cur, "ffn_out", il);
  5392. } else {
  5393. // MoE branch
  5394. cur = build_norm(ffn_inp,
  5395. model.layers[il].ffn_norm, NULL,
  5396. LLM_NORM_RMS, il);
  5397. cb(cur, "ffn_norm", il);
  5398. cur = build_moe_ffn(cur,
  5399. model.layers[il].ffn_gate_inp,
  5400. model.layers[il].ffn_up_exps,
  5401. model.layers[il].ffn_gate_exps,
  5402. model.layers[il].ffn_down_exps,
  5403. nullptr,
  5404. n_expert, n_expert_used,
  5405. LLM_FFN_SILU, true,
  5406. false, 0.0,
  5407. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  5408. il);
  5409. cb(cur, "ffn_moe_out", il);
  5410. }
  5411. cur = ggml_add(ctx0, cur, ffn_inp);
  5412. cb(cur, "ffn_out", il);
  5413. cur = build_cvec(cur, il);
  5414. cb(cur, "l_out", il);
  5415. // input for next layer
  5416. inpL = cur;
  5417. }
  5418. cur = inpL;
  5419. cur = build_norm(cur,
  5420. model.output_norm, NULL,
  5421. LLM_NORM_RMS, -1);
  5422. cb(cur, "result_norm", -1);
  5423. res->t_embd = cur;
  5424. // lm_head
  5425. cur = build_lora_mm(model.output, cur);
  5426. cb(cur, "result_output", -1);
  5427. res->t_logits = cur;
  5428. ggml_build_forward_expand(gf, cur);
  5429. }
  5430. };
  5431. struct llm_build_llama_iswa : public llm_graph_context {
  5432. llm_build_llama_iswa(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  5433. const int64_t n_embd_head = hparams.n_embd_head_v;
  5434. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  5435. GGML_ASSERT(n_embd_head == hparams.n_rot);
  5436. ggml_tensor * cur;
  5437. ggml_tensor * inpL;
  5438. inpL = build_inp_embd(model.tok_embd);
  5439. // inp_pos - contains the positions
  5440. ggml_tensor * inp_pos = build_inp_pos();
  5441. // temperature tuning
  5442. ggml_tensor * inp_attn_scale = nullptr;
  5443. inp_attn_scale = build_inp_attn_scale();
  5444. auto * inp_attn = build_attn_inp_kv_iswa();
  5445. const float kq_scale = hparams.f_attention_scale == 0.0f ? 1.0f/sqrtf(float(n_embd_head)) : hparams.f_attention_scale;
  5446. ggml_tensor * inp_out_ids = build_inp_out_ids();
  5447. for (int il = 0; il < n_layer; ++il) {
  5448. ggml_tensor * inpSA = inpL;
  5449. const bool use_rope = hparams.n_no_rope_layer_step > 0 &&
  5450. (il + 1) % hparams.n_no_rope_layer_step != 0;
  5451. // norm
  5452. cur = build_norm(inpL,
  5453. model.layers[il].attn_norm, NULL,
  5454. LLM_NORM_RMS, il);
  5455. cb(cur, "attn_norm", il);
  5456. // self-attention
  5457. {
  5458. // rope freq factors for llama3; may return nullptr for llama2 and other models
  5459. ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
  5460. // compute Q and K and RoPE them
  5461. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  5462. cb(Qcur, "Qcur", il);
  5463. if (model.layers[il].bq) {
  5464. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  5465. cb(Qcur, "Qcur", il);
  5466. }
  5467. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  5468. cb(Kcur, "Kcur", il);
  5469. if (model.layers[il].bk) {
  5470. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  5471. cb(Kcur, "Kcur", il);
  5472. }
  5473. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  5474. cb(Vcur, "Vcur", il);
  5475. if (model.layers[il].bv) {
  5476. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  5477. cb(Vcur, "Vcur", il);
  5478. }
  5479. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  5480. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  5481. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  5482. if (use_rope) {
  5483. Qcur = ggml_rope_ext(
  5484. ctx0, Qcur, inp_pos, rope_factors,
  5485. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  5486. ext_factor, attn_factor, beta_fast, beta_slow
  5487. );
  5488. Kcur = ggml_rope_ext(
  5489. ctx0, Kcur, inp_pos, rope_factors,
  5490. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  5491. ext_factor, attn_factor, beta_fast, beta_slow
  5492. );
  5493. } else if (inp_attn_scale) {
  5494. Qcur = ggml_mul(ctx0, Qcur, inp_attn_scale);
  5495. }
  5496. cb(Qcur, "Qcur", il);
  5497. cb(Kcur, "Kcur", il);
  5498. cb(Vcur, "Vcur", il);
  5499. if (use_rope && hparams.use_kq_norm) {
  5500. // Llama4TextL2Norm
  5501. Qcur = ggml_rms_norm(ctx0, Qcur, hparams.f_norm_rms_eps);
  5502. Kcur = ggml_rms_norm(ctx0, Kcur, hparams.f_norm_rms_eps);
  5503. cb(Qcur, "Qcur_normed", il);
  5504. cb(Kcur, "Kcur_normed", il);
  5505. }
  5506. cur = build_attn(inp_attn,
  5507. model.layers[il].wo, model.layers[il].bo,
  5508. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
  5509. cb(cur, "attn_out", il);
  5510. }
  5511. if (il == n_layer - 1 && inp_out_ids) {
  5512. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  5513. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  5514. }
  5515. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  5516. cb(ffn_inp, "ffn_inp", il);
  5517. // feed-forward network (non-MoE)
  5518. if (model.layers[il].ffn_gate_inp == nullptr) {
  5519. cur = build_norm(ffn_inp,
  5520. model.layers[il].ffn_norm, NULL,
  5521. LLM_NORM_RMS, il);
  5522. cb(cur, "ffn_norm", il);
  5523. cur = build_ffn(cur,
  5524. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  5525. model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, NULL,
  5526. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  5527. NULL,
  5528. LLM_FFN_SILU, LLM_FFN_PAR, il);
  5529. cb(cur, "ffn_out", il);
  5530. } else {
  5531. ggml_tensor * ffn_inp_normed = build_norm(ffn_inp,
  5532. model.layers[il].ffn_norm, NULL,
  5533. LLM_NORM_RMS, il);
  5534. cb(cur, "ffn_norm", il);
  5535. ggml_tensor * moe_out = build_moe_ffn(ffn_inp_normed,
  5536. model.layers[il].ffn_gate_inp,
  5537. model.layers[il].ffn_up_exps,
  5538. model.layers[il].ffn_gate_exps,
  5539. model.layers[il].ffn_down_exps,
  5540. nullptr,
  5541. n_expert, n_expert_used,
  5542. LLM_FFN_SILU, false,
  5543. false, 0.0,
  5544. LLAMA_EXPERT_GATING_FUNC_TYPE_SIGMOID,
  5545. il);
  5546. // Shared experts
  5547. ggml_tensor * shexp_out = build_ffn(ffn_inp_normed,
  5548. model.layers[il].ffn_up_shexp, NULL, NULL,
  5549. model.layers[il].ffn_gate_shexp, NULL, NULL,
  5550. model.layers[il].ffn_down_shexp, NULL, NULL,
  5551. NULL,
  5552. LLM_FFN_SILU, LLM_FFN_PAR, il);
  5553. cb(shexp_out, "ffn_moe_shexp", il);
  5554. cur = ggml_add(ctx0, moe_out, shexp_out);
  5555. cb(cur, "ffn_moe_out_merged", il);
  5556. }
  5557. cur = ggml_add(ctx0, cur, ffn_inp);
  5558. cb(cur, "ffn_out", il);
  5559. cur = build_cvec(cur, il);
  5560. cb(cur, "l_out", il);
  5561. // input for next layer
  5562. inpL = cur;
  5563. }
  5564. cur = inpL;
  5565. cur = build_norm(cur,
  5566. model.output_norm, NULL,
  5567. LLM_NORM_RMS, -1);
  5568. cb(cur, "result_norm", -1);
  5569. res->t_embd = cur;
  5570. // lm_head
  5571. cur = build_lora_mm(model.output, cur);
  5572. cb(cur, "result_output", -1);
  5573. res->t_logits = cur;
  5574. ggml_build_forward_expand(gf, cur);
  5575. }
  5576. };
  5577. struct llm_build_deci : public llm_graph_context {
  5578. llm_build_deci(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  5579. const int64_t n_embd_head = hparams.n_embd_head_v;
  5580. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  5581. GGML_ASSERT(n_embd_head == hparams.n_rot);
  5582. ggml_tensor * cur;
  5583. ggml_tensor * inpL;
  5584. inpL = build_inp_embd(model.tok_embd);
  5585. // inp_pos - contains the positions
  5586. ggml_tensor * inp_pos = build_inp_pos();
  5587. auto * inp_attn = build_attn_inp_kv();
  5588. const float kq_scale = hparams.f_attention_scale == 0.0f ? 1.0f/sqrtf(float(n_embd_head)) : hparams.f_attention_scale;
  5589. ggml_tensor * inp_out_ids = build_inp_out_ids();
  5590. for (int il = 0; il < n_layer; ++il) {
  5591. ggml_tensor * inpSA = inpL;
  5592. const int64_t n_head_kv = hparams.n_head_kv(il);
  5593. const int64_t n_head = hparams.n_head(il);
  5594. const int64_t n_ff = hparams.n_ff(il);
  5595. if (n_head == 0) {
  5596. // attention-free layer of Llama-3_1-Nemotron-51B
  5597. cur = inpL;
  5598. } else {
  5599. // norm
  5600. cur = build_norm(inpL,
  5601. model.layers[il].attn_norm, NULL,
  5602. LLM_NORM_RMS, il);
  5603. cb(cur, "attn_norm", il);
  5604. }
  5605. if (n_head > 0 && n_head_kv == 0) {
  5606. // "linear attention" of Llama-3_1-Nemotron-51B
  5607. cur = build_lora_mm(model.layers[il].wo, cur);
  5608. cb(cur, "wo", il);
  5609. } else if (n_head > 0) {
  5610. // self-attention
  5611. // rope freq factors for llama3; may return nullptr for llama2 and other models
  5612. ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
  5613. // compute Q and K and RoPE them
  5614. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  5615. cb(Qcur, "Qcur", il);
  5616. if (model.layers[il].bq) {
  5617. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  5618. cb(Qcur, "Qcur", il);
  5619. }
  5620. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  5621. cb(Kcur, "Kcur", il);
  5622. if (model.layers[il].bk) {
  5623. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  5624. cb(Kcur, "Kcur", il);
  5625. }
  5626. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  5627. cb(Vcur, "Vcur", il);
  5628. if (model.layers[il].bv) {
  5629. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  5630. cb(Vcur, "Vcur", il);
  5631. }
  5632. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  5633. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  5634. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  5635. Qcur = ggml_rope_ext(
  5636. ctx0, Qcur, inp_pos, rope_factors,
  5637. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  5638. ext_factor, attn_factor, beta_fast, beta_slow
  5639. );
  5640. Kcur = ggml_rope_ext(
  5641. ctx0, Kcur, inp_pos, rope_factors,
  5642. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  5643. ext_factor, attn_factor, beta_fast, beta_slow
  5644. );
  5645. cb(Qcur, "Qcur", il);
  5646. cb(Kcur, "Kcur", il);
  5647. cb(Vcur, "Vcur", il);
  5648. cur = build_attn(inp_attn,
  5649. model.layers[il].wo, model.layers[il].bo,
  5650. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
  5651. }
  5652. if (il == n_layer - 1 && inp_out_ids) {
  5653. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  5654. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  5655. }
  5656. // FFN-free layer of Llama-3_1-Nemotron-Ultra-253B
  5657. if (n_ff == 0) {
  5658. continue;
  5659. }
  5660. // modified to support attention-free layer of Llama-3_1-Nemotron-51B
  5661. ggml_tensor * ffn_inp = cur;
  5662. if (n_head > 0) {
  5663. ffn_inp = ggml_add(ctx0, cur, inpSA);
  5664. cb(ffn_inp, "ffn_inp", il);
  5665. }
  5666. // feed-forward network
  5667. if (model.layers[il].ffn_gate_inp == nullptr) {
  5668. cur = build_norm(ffn_inp,
  5669. model.layers[il].ffn_norm, NULL,
  5670. LLM_NORM_RMS, il);
  5671. cb(cur, "ffn_norm", il);
  5672. cur = build_ffn(cur,
  5673. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  5674. model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, NULL,
  5675. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  5676. NULL,
  5677. LLM_FFN_SILU, LLM_FFN_PAR, il);
  5678. cb(cur, "ffn_out", il);
  5679. }
  5680. cur = ggml_add(ctx0, cur, ffn_inp);
  5681. cb(cur, "ffn_out", il);
  5682. cur = build_cvec(cur, il);
  5683. cb(cur, "l_out", il);
  5684. // input for next layer
  5685. inpL = cur;
  5686. }
  5687. cur = inpL;
  5688. cur = build_norm(cur,
  5689. model.output_norm, NULL,
  5690. LLM_NORM_RMS, -1);
  5691. cb(cur, "result_norm", -1);
  5692. res->t_embd = cur;
  5693. // lm_head
  5694. cur = build_lora_mm(model.output, cur);
  5695. cb(cur, "result_output", -1);
  5696. res->t_logits = cur;
  5697. ggml_build_forward_expand(gf, cur);
  5698. }
  5699. };
  5700. struct llm_build_baichuan : public llm_graph_context {
  5701. llm_build_baichuan(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  5702. const int64_t n_embd_head = hparams.n_embd_head_v;
  5703. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  5704. GGML_ASSERT(n_embd_head == hparams.n_rot);
  5705. ggml_tensor * cur;
  5706. ggml_tensor * inpL;
  5707. inpL = build_inp_embd(model.tok_embd);
  5708. // inp_pos - contains the positions
  5709. ggml_tensor * inp_pos = model.type == LLM_TYPE_7B ? build_inp_pos() : nullptr;
  5710. auto * inp_attn = build_attn_inp_kv();
  5711. ggml_tensor * inp_out_ids = build_inp_out_ids();
  5712. for (int il = 0; il < n_layer; ++il) {
  5713. ggml_tensor * inpSA = inpL;
  5714. cur = build_norm(inpL,
  5715. model.layers[il].attn_norm, NULL,
  5716. LLM_NORM_RMS, il);
  5717. cb(cur, "attn_norm", il);
  5718. // self-attention
  5719. {
  5720. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  5721. cb(Qcur, "Qcur", il);
  5722. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  5723. cb(Kcur, "Kcur", il);
  5724. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  5725. cb(Vcur, "Vcur", il);
  5726. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  5727. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  5728. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  5729. switch (model.type) {
  5730. case LLM_TYPE_7B:
  5731. Qcur = ggml_rope_ext(
  5732. ctx0, Qcur, inp_pos, nullptr,
  5733. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  5734. ext_factor, attn_factor, beta_fast, beta_slow
  5735. );
  5736. Kcur = ggml_rope_ext(
  5737. ctx0, Kcur, inp_pos, nullptr,
  5738. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  5739. ext_factor, attn_factor, beta_fast, beta_slow
  5740. );
  5741. break;
  5742. case LLM_TYPE_13B:
  5743. break;
  5744. default:
  5745. GGML_ABORT("fatal error");
  5746. }
  5747. cb(Qcur, "Qcur", il);
  5748. cb(Kcur, "Kcur", il);
  5749. cb(Vcur, "Vcur", il);
  5750. cur = build_attn(inp_attn,
  5751. model.layers[il].wo, NULL,
  5752. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  5753. }
  5754. if (il == n_layer - 1 && inp_out_ids) {
  5755. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  5756. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  5757. }
  5758. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  5759. cb(ffn_inp, "ffn_inp", il);
  5760. // feed-forward network
  5761. {
  5762. cur = build_norm(ffn_inp,
  5763. model.layers[il].ffn_norm, NULL,
  5764. LLM_NORM_RMS, il);
  5765. cb(cur, "ffn_norm", il);
  5766. cur = build_ffn(cur,
  5767. model.layers[il].ffn_up, NULL, NULL,
  5768. model.layers[il].ffn_gate, NULL, NULL,
  5769. model.layers[il].ffn_down, NULL, NULL,
  5770. NULL,
  5771. LLM_FFN_SILU, LLM_FFN_PAR, il);
  5772. cb(cur, "ffn_out", il);
  5773. }
  5774. cur = ggml_add(ctx0, cur, ffn_inp);
  5775. cur = build_cvec(cur, il);
  5776. cb(cur, "l_out", il);
  5777. // input for next layer
  5778. inpL = cur;
  5779. }
  5780. cur = inpL;
  5781. cur = build_norm(cur,
  5782. model.output_norm, NULL,
  5783. LLM_NORM_RMS, -1);
  5784. cb(cur, "result_norm", -1);
  5785. res->t_embd = cur;
  5786. // lm_head
  5787. cur = build_lora_mm(model.output, cur);
  5788. cb(cur, "result_output", -1);
  5789. res->t_logits = cur;
  5790. ggml_build_forward_expand(gf, cur);
  5791. }
  5792. };
  5793. struct llm_build_xverse : public llm_graph_context {
  5794. llm_build_xverse(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  5795. const int64_t n_embd_head = hparams.n_embd_head_v;
  5796. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  5797. GGML_ASSERT(n_embd_head == hparams.n_rot);
  5798. ggml_tensor * cur;
  5799. ggml_tensor * inpL;
  5800. inpL = build_inp_embd(model.tok_embd);
  5801. // inp_pos - contains the positions
  5802. ggml_tensor * inp_pos = build_inp_pos();
  5803. auto * inp_attn = build_attn_inp_kv();
  5804. ggml_tensor * inp_out_ids = build_inp_out_ids();
  5805. for (int il = 0; il < n_layer; ++il) {
  5806. ggml_tensor * inpSA = inpL;
  5807. cur = build_norm(inpL,
  5808. model.layers[il].attn_norm, NULL,
  5809. LLM_NORM_RMS, il);
  5810. cb(cur, "attn_norm", il);
  5811. // self-attention
  5812. {
  5813. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  5814. cb(Qcur, "Qcur", il);
  5815. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  5816. cb(Kcur, "Kcur", il);
  5817. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  5818. cb(Vcur, "Vcur", il);
  5819. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  5820. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  5821. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  5822. Qcur = ggml_rope_ext(
  5823. ctx0, Qcur, inp_pos, nullptr,
  5824. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  5825. ext_factor, attn_factor, beta_fast, beta_slow
  5826. );
  5827. Kcur = ggml_rope_ext(
  5828. ctx0, Kcur, inp_pos, nullptr,
  5829. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  5830. ext_factor, attn_factor, beta_fast, beta_slow
  5831. );
  5832. cb(Qcur, "Qcur", il);
  5833. cb(Kcur, "Kcur", il);
  5834. cb(Vcur, "Vcur", il);
  5835. cur = build_attn(inp_attn,
  5836. model.layers[il].wo, NULL,
  5837. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  5838. }
  5839. if (il == n_layer - 1 && inp_out_ids) {
  5840. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  5841. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  5842. }
  5843. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  5844. cb(ffn_inp, "ffn_inp", il);
  5845. // feed-forward network
  5846. {
  5847. cur = build_norm(ffn_inp,
  5848. model.layers[il].ffn_norm, NULL,
  5849. LLM_NORM_RMS, il);
  5850. cb(cur, "ffn_norm", il);
  5851. cur = build_ffn(cur,
  5852. model.layers[il].ffn_up, NULL, NULL,
  5853. model.layers[il].ffn_gate, NULL, NULL,
  5854. model.layers[il].ffn_down, NULL, NULL,
  5855. NULL,
  5856. LLM_FFN_SILU, LLM_FFN_PAR, il);
  5857. cb(cur, "ffn_out", il);
  5858. }
  5859. cur = ggml_add(ctx0, cur, ffn_inp);
  5860. cur = build_cvec(cur, il);
  5861. cb(cur, "l_out", il);
  5862. // input for next layer
  5863. inpL = cur;
  5864. }
  5865. cur = inpL;
  5866. cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1);
  5867. cb(cur, "result_norm", -1);
  5868. res->t_embd = cur;
  5869. // lm_head
  5870. cur = build_lora_mm(model.output, cur);
  5871. cb(cur, "result_output", -1);
  5872. res->t_logits = cur;
  5873. ggml_build_forward_expand(gf, cur);
  5874. }
  5875. };
  5876. struct llm_build_falcon : public llm_graph_context {
  5877. llm_build_falcon(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  5878. const int64_t n_embd_head = hparams.n_embd_head_v;
  5879. const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  5880. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  5881. GGML_ASSERT(n_embd_head == hparams.n_rot);
  5882. ggml_tensor * cur;
  5883. ggml_tensor * inpL;
  5884. inpL = build_inp_embd(model.tok_embd);
  5885. // inp_pos - contains the positions
  5886. ggml_tensor * inp_pos = build_inp_pos();
  5887. auto * inp_attn = build_attn_inp_kv();
  5888. ggml_tensor * inp_out_ids = build_inp_out_ids();
  5889. for (int il = 0; il < n_layer; ++il) {
  5890. ggml_tensor * attn_norm;
  5891. attn_norm = build_norm(inpL,
  5892. model.layers[il].attn_norm,
  5893. model.layers[il].attn_norm_b,
  5894. LLM_NORM, il);
  5895. cb(attn_norm, "attn_norm", il);
  5896. // self-attention
  5897. {
  5898. if (model.layers[il].attn_norm_2) {
  5899. // Falcon-40B
  5900. cur = build_norm(inpL,
  5901. model.layers[il].attn_norm_2,
  5902. model.layers[il].attn_norm_2_b,
  5903. LLM_NORM, il);
  5904. cb(cur, "attn_norm_2", il);
  5905. } else {
  5906. cur = attn_norm;
  5907. }
  5908. cur = build_lora_mm(model.layers[il].wqkv, cur);
  5909. cb(cur, "wqkv", il);
  5910. 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));
  5911. 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));
  5912. ggml_tensor * Vcur = 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 + n_embd_gqa));
  5913. // using mode = 2 for neox mode
  5914. Qcur = ggml_rope_ext(
  5915. ctx0, Qcur, inp_pos, nullptr,
  5916. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  5917. ext_factor, attn_factor, beta_fast, beta_slow
  5918. );
  5919. Kcur = ggml_rope_ext(
  5920. ctx0, Kcur, inp_pos, nullptr,
  5921. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  5922. ext_factor, attn_factor, beta_fast, beta_slow
  5923. );
  5924. cb(Qcur, "Qcur", il);
  5925. cb(Kcur, "Kcur", il);
  5926. cb(Vcur, "Vcur", il);
  5927. cur = build_attn(inp_attn,
  5928. model.layers[il].wo, NULL,
  5929. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  5930. }
  5931. if (il == n_layer - 1 && inp_out_ids) {
  5932. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  5933. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  5934. attn_norm = ggml_get_rows(ctx0, attn_norm, inp_out_ids);
  5935. }
  5936. ggml_tensor * ffn_inp = cur;
  5937. // feed forward
  5938. {
  5939. cur = build_ffn(attn_norm, // !! use the attn norm, not the result
  5940. model.layers[il].ffn_up, NULL, NULL,
  5941. NULL, NULL, NULL,
  5942. model.layers[il].ffn_down, NULL, NULL,
  5943. NULL,
  5944. LLM_FFN_GELU, LLM_FFN_SEQ, il);
  5945. cb(cur, "ffn_out", il);
  5946. }
  5947. cur = ggml_add(ctx0, cur, ffn_inp);
  5948. cur = ggml_add(ctx0, cur, inpL);
  5949. cur = build_cvec(cur, il);
  5950. cb(cur, "l_out", il);
  5951. // input for next layer
  5952. inpL = cur;
  5953. }
  5954. cur = inpL;
  5955. // norm
  5956. cur = build_norm(cur,
  5957. model.output_norm,
  5958. model.output_norm_b,
  5959. LLM_NORM, -1);
  5960. cb(cur, "result_norm", -1);
  5961. res->t_embd = cur;
  5962. cur = build_lora_mm(model.output, cur);
  5963. cb(cur, "result_output", -1);
  5964. res->t_logits = cur;
  5965. ggml_build_forward_expand(gf, cur);
  5966. }
  5967. };
  5968. struct llm_build_grok : public llm_graph_context {
  5969. llm_build_grok(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  5970. const int64_t n_embd_head = hparams.n_embd_head_v;
  5971. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  5972. GGML_ASSERT(n_embd_head == hparams.n_rot);
  5973. ggml_tensor * cur;
  5974. ggml_tensor * inpL;
  5975. inpL = build_inp_embd(model.tok_embd);
  5976. // inp_pos - contains the positions
  5977. ggml_tensor * inp_pos = build_inp_pos();
  5978. auto * inp_attn = build_attn_inp_kv();
  5979. ggml_tensor * inp_out_ids = build_inp_out_ids();
  5980. for (int il = 0; il < n_layer; ++il) {
  5981. ggml_tensor * inpSA = inpL;
  5982. // norm
  5983. cur = build_norm(inpL,
  5984. model.layers[il].attn_norm, NULL,
  5985. LLM_NORM_RMS, il);
  5986. cb(cur, "attn_norm", il);
  5987. // self-attention
  5988. {
  5989. // compute Q and K and RoPE them
  5990. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  5991. cb(Qcur, "Qcur", il);
  5992. if (model.layers[il].bq) {
  5993. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  5994. cb(Qcur, "Qcur", il);
  5995. }
  5996. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  5997. cb(Kcur, "Kcur", il);
  5998. if (model.layers[il].bk) {
  5999. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  6000. cb(Kcur, "Kcur", il);
  6001. }
  6002. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  6003. cb(Vcur, "Vcur", il);
  6004. if (model.layers[il].bv) {
  6005. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  6006. cb(Vcur, "Vcur", il);
  6007. }
  6008. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  6009. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  6010. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  6011. Qcur = ggml_rope_ext(
  6012. ctx0, Qcur, inp_pos, nullptr,
  6013. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  6014. ext_factor, attn_factor, beta_fast, beta_slow
  6015. );
  6016. Kcur = ggml_rope_ext(
  6017. ctx0, Kcur, inp_pos, nullptr,
  6018. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  6019. ext_factor, attn_factor, beta_fast, beta_slow
  6020. );
  6021. cb(Qcur, "Qcur", il);
  6022. cb(Kcur, "Kcur", il);
  6023. cb(Vcur, "Vcur", il);
  6024. cur = build_attn(inp_attn,
  6025. model.layers[il].wo, model.layers[il].bo,
  6026. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f, il);
  6027. }
  6028. if (il == n_layer - 1 && inp_out_ids) {
  6029. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  6030. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  6031. }
  6032. cur = build_norm(cur,
  6033. model.layers[il].attn_out_norm, NULL,
  6034. LLM_NORM_RMS, il);
  6035. cb(cur, "attn_out_norm", il);
  6036. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  6037. cb(ffn_inp, "ffn_inp", il);
  6038. // feed-forward network
  6039. cur = build_norm(ffn_inp,
  6040. model.layers[il].ffn_norm, NULL,
  6041. LLM_NORM_RMS, il);
  6042. cb(cur, "ffn_norm", il);
  6043. // MoE branch
  6044. ggml_tensor * moe_out = build_moe_ffn(cur,
  6045. model.layers[il].ffn_gate_inp,
  6046. model.layers[il].ffn_up_exps,
  6047. model.layers[il].ffn_gate_exps,
  6048. model.layers[il].ffn_down_exps,
  6049. nullptr,
  6050. n_expert, n_expert_used,
  6051. LLM_FFN_GELU, true,
  6052. false, 0.0,
  6053. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  6054. il);
  6055. cb(moe_out, "ffn_moe_out", il);
  6056. if (model.layers[il].ffn_up) {
  6057. ggml_tensor * ffn_out = build_ffn(cur,
  6058. model.layers[il].ffn_up, NULL, NULL,
  6059. model.layers[il].ffn_gate, NULL, NULL,
  6060. model.layers[il].ffn_down, NULL, NULL,
  6061. NULL,
  6062. LLM_FFN_GELU, LLM_FFN_PAR, il);
  6063. cb(ffn_out, "ffn_out", il);
  6064. cur = ggml_scale(ctx0, ggml_add(ctx0, ffn_out, moe_out), std::sqrt(2) / 2);
  6065. cb(cur, "ffn_out", il);
  6066. } else {
  6067. cur = moe_out;
  6068. }
  6069. cur = build_norm(cur,
  6070. model.layers[il].ffn_post_norm, NULL,
  6071. LLM_NORM_RMS, il);
  6072. cb(cur, "ffn_post_norm", il);
  6073. cur = ggml_add(ctx0, cur, ffn_inp);
  6074. cb(cur, "ffn_out", il);
  6075. cur = build_cvec(cur, il);
  6076. cb(cur, "l_out", il);
  6077. // input for next layer
  6078. inpL = cur;
  6079. }
  6080. cur = inpL;
  6081. cur = build_norm(cur,
  6082. model.output_norm, NULL,
  6083. LLM_NORM_RMS, -1);
  6084. cb(cur, "result_norm", -1);
  6085. res->t_embd = cur;
  6086. // lm_head
  6087. cur = build_lora_mm(model.output, cur);
  6088. cur = ggml_scale(ctx0, cur, hparams.f_logit_scale);
  6089. // final logit soft-capping
  6090. if (hparams.f_final_logit_softcapping) {
  6091. cur = ggml_scale(ctx0, cur, 1.0f / hparams.f_final_logit_softcapping);
  6092. cur = ggml_tanh(ctx0, cur);
  6093. cur = ggml_scale(ctx0, cur, hparams.f_final_logit_softcapping);
  6094. }
  6095. cb(cur, "result_output", -1);
  6096. res->t_logits = cur;
  6097. ggml_build_forward_expand(gf, cur);
  6098. }
  6099. };
  6100. struct llm_build_dbrx : public llm_graph_context {
  6101. llm_build_dbrx(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  6102. const int64_t n_embd_head = hparams.n_embd_head_v;
  6103. const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  6104. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  6105. GGML_ASSERT(n_embd_head == hparams.n_rot);
  6106. ggml_tensor * cur;
  6107. ggml_tensor * inpL;
  6108. inpL = build_inp_embd(model.tok_embd);
  6109. // inp_pos - contains the positions
  6110. ggml_tensor * inp_pos = build_inp_pos();
  6111. auto * inp_attn = build_attn_inp_kv();
  6112. ggml_tensor * inp_out_ids = build_inp_out_ids();
  6113. for (int il = 0; il < n_layer; ++il) {
  6114. ggml_tensor * inpSA = inpL;
  6115. // norm
  6116. cur = build_norm(inpL,
  6117. model.layers[il].attn_norm, NULL,
  6118. LLM_NORM, il);
  6119. cb(cur, "attn_norm", il);
  6120. // self-attention
  6121. {
  6122. ggml_tensor * Qcur = nullptr;
  6123. ggml_tensor * Kcur = nullptr;
  6124. ggml_tensor * Vcur = nullptr;
  6125. cur = build_lora_mm(model.layers[il].wqkv, cur);
  6126. cb(cur, "wqkv", il);
  6127. cur = ggml_clamp(ctx0, cur, -hparams.f_clamp_kqv, hparams.f_clamp_kqv);
  6128. cb(cur, "wqkv_clamped", il);
  6129. 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));
  6130. 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));
  6131. Vcur = 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 + n_embd_gqa));
  6132. Qcur = ggml_rope_ext(
  6133. ctx0, Qcur, inp_pos, nullptr,
  6134. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  6135. ext_factor, attn_factor, beta_fast, beta_slow
  6136. );
  6137. Kcur = ggml_rope_ext(
  6138. ctx0, Kcur, inp_pos, nullptr,
  6139. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  6140. ext_factor, attn_factor, beta_fast, beta_slow
  6141. );
  6142. cb(Qcur, "Qcur", il);
  6143. cb(Kcur, "Kcur", il);
  6144. cb(Vcur, "Vcur", il);
  6145. cur = build_attn(inp_attn,
  6146. model.layers[il].wo, NULL,
  6147. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  6148. }
  6149. if (il == n_layer - 1 && inp_out_ids) {
  6150. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  6151. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  6152. }
  6153. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  6154. cb(ffn_inp, "ffn_inp", il);
  6155. // feed-forward network
  6156. // MoE branch
  6157. cur = build_norm(ffn_inp,
  6158. model.layers[il].attn_out_norm, NULL,
  6159. LLM_NORM, il);
  6160. cb(cur, "attn_out_norm", il);
  6161. cur = build_moe_ffn(cur,
  6162. model.layers[il].ffn_gate_inp,
  6163. model.layers[il].ffn_up_exps,
  6164. model.layers[il].ffn_gate_exps,
  6165. model.layers[il].ffn_down_exps,
  6166. nullptr,
  6167. n_expert, n_expert_used,
  6168. LLM_FFN_SILU, true,
  6169. false, 0.0,
  6170. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  6171. il);
  6172. cb(cur, "ffn_moe_out", il);
  6173. cur = ggml_add(ctx0, cur, ffn_inp);
  6174. cb(cur, "ffn_out", il);
  6175. cur = build_cvec(cur, il);
  6176. cb(cur, "l_out", il);
  6177. // input for next layer
  6178. inpL = cur;
  6179. }
  6180. cur = inpL;
  6181. cur = build_norm(cur,
  6182. model.output_norm, NULL,
  6183. LLM_NORM, -1);
  6184. cb(cur, "result_norm", -1);
  6185. res->t_embd = cur;
  6186. // lm_head
  6187. cur = build_lora_mm(model.output, cur);
  6188. cb(cur, "result_output", -1);
  6189. res->t_logits = cur;
  6190. ggml_build_forward_expand(gf, cur);
  6191. }
  6192. };
  6193. struct llm_build_starcoder : public llm_graph_context {
  6194. llm_build_starcoder(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  6195. const int64_t n_embd_head = hparams.n_embd_head_v;
  6196. const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  6197. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  6198. ggml_tensor * cur;
  6199. ggml_tensor * inpL;
  6200. inpL = build_inp_embd(model.tok_embd);
  6201. // inp_pos - contains the positions
  6202. ggml_tensor * inp_pos = build_inp_pos();
  6203. auto * inp_attn = build_attn_inp_kv();
  6204. ggml_tensor * pos = ggml_get_rows(ctx0, model.pos_embd, inp_pos);
  6205. cb(pos, "pos_embd", -1);
  6206. inpL = ggml_add(ctx0, inpL, pos);
  6207. cb(inpL, "inpL", -1);
  6208. ggml_tensor * inp_out_ids = build_inp_out_ids();
  6209. for (int il = 0; il < n_layer; ++il) {
  6210. cur = build_norm(inpL,
  6211. model.layers[il].attn_norm,
  6212. model.layers[il].attn_norm_b,
  6213. LLM_NORM, il);
  6214. cb(cur, "attn_norm", il);
  6215. // self-attention
  6216. {
  6217. cur = build_lora_mm(model.layers[il].wqkv, cur);
  6218. cb(cur, "wqkv", il);
  6219. cur = ggml_add(ctx0, cur, model.layers[il].bqkv);
  6220. cb(cur, "bqkv", il);
  6221. 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));
  6222. 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));
  6223. ggml_tensor * Vcur = 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 + n_embd_gqa));
  6224. cb(Qcur, "Qcur", il);
  6225. cb(Kcur, "Kcur", il);
  6226. cb(Vcur, "Vcur", il);
  6227. cur = build_attn(inp_attn,
  6228. model.layers[il].wo, model.layers[il].bo,
  6229. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  6230. }
  6231. if (il == n_layer - 1 && inp_out_ids) {
  6232. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  6233. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  6234. }
  6235. // add the input
  6236. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
  6237. cb(ffn_inp, "ffn_inp", il);
  6238. // FF
  6239. {
  6240. cur = build_norm(ffn_inp,
  6241. model.layers[il].ffn_norm,
  6242. model.layers[il].ffn_norm_b,
  6243. LLM_NORM, il);
  6244. cb(cur, "ffn_norm", il);
  6245. cur = build_ffn(cur,
  6246. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  6247. NULL, NULL, NULL,
  6248. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  6249. NULL,
  6250. LLM_FFN_GELU, LLM_FFN_SEQ, il);
  6251. cb(cur, "ffn_out", il);
  6252. }
  6253. cur = ggml_add(ctx0, cur, ffn_inp);
  6254. cur = build_cvec(cur, il);
  6255. cb(cur, "l_out", il);
  6256. // input for next layer
  6257. inpL = cur;
  6258. }
  6259. cur = build_norm(inpL,
  6260. model.output_norm,
  6261. model.output_norm_b,
  6262. LLM_NORM, -1);
  6263. cb(cur, "result_norm", -1);
  6264. res->t_embd = cur;
  6265. cur = build_lora_mm(model.output, cur);
  6266. cb(cur, "result_output", -1);
  6267. res->t_logits = cur;
  6268. ggml_build_forward_expand(gf, cur);
  6269. }
  6270. };
  6271. struct llm_build_refact : public llm_graph_context {
  6272. llm_build_refact(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  6273. const int64_t n_embd_head = hparams.n_embd_head_v;
  6274. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  6275. ggml_tensor * cur;
  6276. ggml_tensor * inpL;
  6277. inpL = build_inp_embd(model.tok_embd);
  6278. auto * inp_attn = build_attn_inp_kv();
  6279. ggml_tensor * inp_out_ids = build_inp_out_ids();
  6280. for (int il = 0; il < n_layer; ++il) {
  6281. ggml_tensor * inpSA = inpL;
  6282. cur = build_norm(inpL,
  6283. model.layers[il].attn_norm, NULL,
  6284. LLM_NORM_RMS, il);
  6285. cb(cur, "attn_norm", il);
  6286. // self-attention
  6287. {
  6288. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  6289. cb(Qcur, "Qcur", il);
  6290. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  6291. cb(Kcur, "Kcur", il);
  6292. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  6293. cb(Vcur, "Vcur", il);
  6294. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  6295. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  6296. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  6297. cb(Qcur, "Qcur", il);
  6298. cb(Kcur, "Kcur", il);
  6299. cb(Vcur, "Vcur", il);
  6300. cur = build_attn(inp_attn,
  6301. model.layers[il].wo, NULL,
  6302. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  6303. }
  6304. if (il == n_layer - 1 && inp_out_ids) {
  6305. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  6306. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  6307. }
  6308. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  6309. cb(ffn_inp, "ffn_inp", il);
  6310. // feed-forward network
  6311. {
  6312. cur = build_norm(ffn_inp,
  6313. model.layers[il].ffn_norm, NULL,
  6314. LLM_NORM_RMS, il);
  6315. cb(cur, "ffn_norm", il);
  6316. cur = build_ffn(cur,
  6317. model.layers[il].ffn_up, NULL, NULL,
  6318. model.layers[il].ffn_gate, NULL, NULL,
  6319. model.layers[il].ffn_down, NULL, NULL,
  6320. NULL,
  6321. LLM_FFN_SILU, LLM_FFN_PAR, il);
  6322. cb(cur, "ffn_out", il);
  6323. }
  6324. cur = ggml_add(ctx0, cur, ffn_inp);
  6325. cur = build_cvec(cur, il);
  6326. cb(cur, "l_out", il);
  6327. // input for next layer
  6328. inpL = cur;
  6329. }
  6330. cur = inpL;
  6331. cur = build_norm(cur,
  6332. model.output_norm, NULL,
  6333. LLM_NORM_RMS, -1);
  6334. cb(cur, "result_norm", -1);
  6335. res->t_embd = cur;
  6336. // lm_head
  6337. cur = build_lora_mm(model.output, cur);
  6338. cb(cur, "result_output", -1);
  6339. res->t_logits = cur;
  6340. ggml_build_forward_expand(gf, cur);
  6341. }
  6342. };
  6343. struct llm_build_bert : public llm_graph_context {
  6344. llm_build_bert(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  6345. const int64_t n_embd_head = hparams.n_embd_head_v;
  6346. const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  6347. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  6348. ggml_tensor * cur;
  6349. ggml_tensor * inpL;
  6350. ggml_tensor * inp_pos = nullptr;
  6351. if (model.arch != LLM_ARCH_JINA_BERT_V2) {
  6352. inp_pos = build_inp_pos();
  6353. }
  6354. // construct input embeddings (token, type, position)
  6355. inpL = build_inp_embd(model.tok_embd);
  6356. // token types are hardcoded to zero ("Sentence A")
  6357. if (model.type_embd) {
  6358. ggml_tensor * type_row0 = ggml_view_1d(ctx0, model.type_embd, n_embd, 0);
  6359. inpL = ggml_add(ctx0, inpL, type_row0);
  6360. }
  6361. if (model.arch == LLM_ARCH_BERT) {
  6362. inpL = ggml_add(ctx0, ggml_get_rows(ctx0, model.pos_embd, inp_pos), inpL);
  6363. }
  6364. cb(inpL, "inp_embd", -1);
  6365. // embed layer norm
  6366. inpL = build_norm(inpL, model.tok_norm, model.tok_norm_b, LLM_NORM, -1);
  6367. cb(inpL, "inp_norm", -1);
  6368. auto * inp_attn = build_attn_inp_no_cache();
  6369. ggml_tensor * inp_out_ids = build_inp_out_ids();
  6370. for (int il = 0; il < n_layer; ++il) {
  6371. ggml_tensor * cur = inpL;
  6372. {
  6373. ggml_tensor * Qcur;
  6374. ggml_tensor * Kcur;
  6375. ggml_tensor * Vcur;
  6376. // self-attention
  6377. if (model.layers[il].wqkv) {
  6378. cur = build_lora_mm(model.layers[il].wqkv, cur);
  6379. cb(cur, "wqkv", il);
  6380. if (model.layers[il].bqkv) {
  6381. cur = ggml_add(ctx0, cur, model.layers[il].bqkv);
  6382. cb(cur, "bqkv", il);
  6383. }
  6384. 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));
  6385. 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));
  6386. Vcur = 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 + n_embd_gqa));
  6387. } else {
  6388. Qcur = ggml_add(ctx0, build_lora_mm(model.layers[il].wq, cur), model.layers[il].bq);
  6389. Kcur = ggml_add(ctx0, build_lora_mm(model.layers[il].wk, cur), model.layers[il].bk);
  6390. Vcur = ggml_add(ctx0, build_lora_mm(model.layers[il].wv, cur), model.layers[il].bv);
  6391. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  6392. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  6393. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  6394. }
  6395. if (model.layers[il].attn_q_norm) {
  6396. Qcur = build_norm(Qcur,
  6397. model.layers[il].attn_q_norm,
  6398. model.layers[il].attn_q_norm_b,
  6399. LLM_NORM, il);
  6400. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  6401. }
  6402. if (model.layers[il].attn_k_norm) {
  6403. Kcur = build_norm(Kcur,
  6404. model.layers[il].attn_k_norm,
  6405. model.layers[il].attn_k_norm_b,
  6406. LLM_NORM, il);
  6407. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  6408. }
  6409. // RoPE
  6410. if (model.arch == LLM_ARCH_NOMIC_BERT || model.arch == LLM_ARCH_NOMIC_BERT_MOE || model.arch == LLM_ARCH_JINA_BERT_V3) {
  6411. Qcur = ggml_rope_ext(
  6412. ctx0, Qcur, inp_pos, nullptr,
  6413. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  6414. ext_factor, attn_factor, beta_fast, beta_slow
  6415. );
  6416. Kcur = ggml_rope_ext(
  6417. ctx0, Kcur, inp_pos, nullptr,
  6418. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  6419. ext_factor, attn_factor, beta_fast, beta_slow
  6420. );
  6421. }
  6422. cb(Qcur, "Qcur", il);
  6423. cb(Kcur, "Kcur", il);
  6424. cb(Vcur, "Vcur", il);
  6425. cur = build_attn(inp_attn,
  6426. model.layers[il].wo, model.layers[il].bo,
  6427. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  6428. cb(cur, "kqv_out", il);
  6429. }
  6430. if (il == n_layer - 1 && inp_out_ids) {
  6431. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  6432. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  6433. }
  6434. // re-add the layer input
  6435. cur = ggml_add(ctx0, cur, inpL);
  6436. // attention layer norm
  6437. cur = build_norm(cur, model.layers[il].attn_out_norm, model.layers[il].attn_out_norm_b, LLM_NORM, il);
  6438. if (model.layers[il].attn_norm_2 != nullptr) {
  6439. cur = ggml_add(ctx0, cur, inpL); // re-add the layer input
  6440. cur = build_norm(cur, model.layers[il].attn_norm_2, model.layers[il].attn_norm_2_b, LLM_NORM, il);
  6441. }
  6442. ggml_tensor * ffn_inp = cur;
  6443. cb(ffn_inp, "ffn_inp", il);
  6444. // feed-forward network
  6445. if (hparams.moe_every_n_layers > 0 && il % hparams.moe_every_n_layers == 1) {
  6446. // MoE branch
  6447. cur = build_moe_ffn(cur,
  6448. model.layers[il].ffn_gate_inp,
  6449. model.layers[il].ffn_up_exps,
  6450. nullptr,
  6451. model.layers[il].ffn_down_exps,
  6452. nullptr,
  6453. hparams.n_expert,
  6454. hparams.n_expert_used,
  6455. LLM_FFN_GELU,
  6456. false, false,
  6457. 0.0f,
  6458. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX, il);
  6459. cb(cur, "ffn_moe_out", il);
  6460. } else if (model.arch == LLM_ARCH_BERT || model.arch == LLM_ARCH_NOMIC_BERT_MOE || model.arch == LLM_ARCH_JINA_BERT_V3) {
  6461. cur = build_ffn(cur,
  6462. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  6463. NULL, NULL, NULL,
  6464. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  6465. NULL,
  6466. LLM_FFN_GELU, LLM_FFN_SEQ, il);
  6467. cb(cur, "ffn_out", il);
  6468. } else if (model.arch == LLM_ARCH_JINA_BERT_V2) {
  6469. cur = build_ffn(cur,
  6470. model.layers[il].ffn_up, NULL, NULL,
  6471. model.layers[il].ffn_gate, NULL, NULL,
  6472. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  6473. NULL,
  6474. model.layers[il].ffn_gate ? LLM_FFN_GELU : LLM_FFN_GEGLU, LLM_FFN_PAR, il);
  6475. cb(cur, "ffn_out", il);
  6476. } else {
  6477. cur = build_ffn(cur,
  6478. model.layers[il].ffn_up, NULL, NULL,
  6479. model.layers[il].ffn_gate, NULL, NULL,
  6480. model.layers[il].ffn_down, NULL, NULL,
  6481. NULL,
  6482. LLM_FFN_SILU, LLM_FFN_PAR, il);
  6483. cb(cur, "ffn_out", il);
  6484. }
  6485. // attentions bypass the intermediate layer
  6486. cur = ggml_add(ctx0, cur, ffn_inp);
  6487. // output layer norm
  6488. cur = build_norm(cur, model.layers[il].layer_out_norm, model.layers[il].layer_out_norm_b, LLM_NORM, il);
  6489. // input for next layer
  6490. inpL = cur;
  6491. }
  6492. cur = inpL;
  6493. cb(cur, "result_embd", -1);
  6494. res->t_embd = cur;
  6495. ggml_build_forward_expand(gf, cur);
  6496. }
  6497. };
  6498. struct llm_build_neo_bert : public llm_graph_context {
  6499. llm_build_neo_bert(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  6500. const int64_t n_embd_head = hparams.n_embd_head_v;
  6501. const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  6502. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  6503. ggml_tensor * cur;
  6504. ggml_tensor * inpL;
  6505. ggml_tensor * inp_pos = build_inp_pos();
  6506. // construct input embeddings (token, type, position)
  6507. inpL = build_inp_embd(model.tok_embd);
  6508. cb(inpL, "inp_embd", -1);
  6509. auto * inp_attn = build_attn_inp_no_cache();
  6510. ggml_tensor * inp_out_ids = build_inp_out_ids();
  6511. for (int il = 0; il < n_layer; ++il) {
  6512. ggml_tensor * cur = inpL;
  6513. // pre-norm
  6514. cur = build_norm(inpL,
  6515. model.layers[il].attn_norm, NULL,
  6516. LLM_NORM_RMS, il);
  6517. {
  6518. ggml_tensor * Qcur;
  6519. ggml_tensor * Kcur;
  6520. ggml_tensor * Vcur;
  6521. // self-attention
  6522. cur = build_lora_mm(model.layers[il].wqkv, cur);
  6523. cb(cur, "wqkv", il);
  6524. 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));
  6525. 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));
  6526. Vcur = 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 + n_embd_gqa));
  6527. // RoPE
  6528. Qcur = ggml_rope_ext(
  6529. ctx0, Qcur, inp_pos, nullptr,
  6530. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  6531. ext_factor, attn_factor, beta_fast, beta_slow
  6532. );
  6533. Kcur = ggml_rope_ext(
  6534. ctx0, Kcur, inp_pos, nullptr,
  6535. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  6536. ext_factor, attn_factor, beta_fast, beta_slow
  6537. );
  6538. cb(Qcur, "Qcur", il);
  6539. cb(Kcur, "Kcur", il);
  6540. cb(Vcur, "Vcur", il);
  6541. cur = build_attn(inp_attn,
  6542. model.layers[il].wo, nullptr,
  6543. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  6544. cb(cur, "kqv_out", il);
  6545. }
  6546. if (il == n_layer - 1 && inp_out_ids) {
  6547. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  6548. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  6549. }
  6550. // re-add the layer input
  6551. cur = ggml_add(ctx0, cur, inpL);
  6552. ggml_tensor * ffn_inp = cur;
  6553. cb(ffn_inp, "ffn_inp", il);
  6554. // pre-norm
  6555. cur = build_norm(ffn_inp,
  6556. model.layers[il].ffn_norm, NULL,
  6557. LLM_NORM_RMS, il);
  6558. cb(cur, "ffn_norm", il);
  6559. // feed-forward network
  6560. cur = build_ffn(cur,
  6561. model.layers[il].ffn_up,
  6562. NULL, NULL, NULL, NULL, NULL,
  6563. model.layers[il].ffn_down,
  6564. NULL, NULL, NULL,
  6565. LLM_FFN_SWIGLU, LLM_FFN_SEQ, il);
  6566. // attentions bypass the intermediate layer
  6567. cur = ggml_add(ctx0, cur, ffn_inp);
  6568. // input for next layer
  6569. inpL = cur;
  6570. }
  6571. cur = inpL;
  6572. cur = build_norm(cur,
  6573. model.output_norm_enc, NULL,
  6574. LLM_NORM_RMS, -1);
  6575. cb(cur, "result_embd", -1);
  6576. res->t_embd = cur;
  6577. ggml_build_forward_expand(gf, cur);
  6578. }
  6579. };
  6580. struct llm_build_bloom : public llm_graph_context {
  6581. llm_build_bloom(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  6582. const int64_t n_embd_head = hparams.n_embd_head_v;
  6583. const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  6584. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  6585. ggml_tensor * cur;
  6586. ggml_tensor * inpL;
  6587. inpL = build_inp_embd(model.tok_embd);
  6588. auto * inp_attn = build_attn_inp_kv();
  6589. inpL = build_norm(inpL,
  6590. model.tok_norm,
  6591. model.tok_norm_b,
  6592. LLM_NORM, -1);
  6593. cb(inpL, "inp_norm", -1);
  6594. ggml_tensor * inp_out_ids = build_inp_out_ids();
  6595. for (int il = 0; il < n_layer; ++il) {
  6596. cur = build_norm(inpL,
  6597. model.layers[il].attn_norm,
  6598. model.layers[il].attn_norm_b,
  6599. LLM_NORM, il);
  6600. cb(cur, "attn_norm", il);
  6601. // self-attention
  6602. {
  6603. cur = build_lora_mm(model.layers[il].wqkv, cur);
  6604. cb(cur, "wqkv", il);
  6605. cur = ggml_add(ctx0, cur, model.layers[il].bqkv);
  6606. cb(cur, "bqkv", il);
  6607. 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));
  6608. 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));
  6609. ggml_tensor * Vcur = 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 + n_embd_gqa));
  6610. cb(Qcur, "Qcur", il);
  6611. cb(Kcur, "Kcur", il);
  6612. cb(Vcur, "Vcur", il);
  6613. cur = build_attn(inp_attn,
  6614. model.layers[il].wo, model.layers[il].bo,
  6615. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  6616. }
  6617. if (il == n_layer - 1 && inp_out_ids) {
  6618. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  6619. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  6620. }
  6621. // Add the input
  6622. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
  6623. cb(ffn_inp, "ffn_inp", il);
  6624. // FF
  6625. {
  6626. cur = build_norm(ffn_inp,
  6627. model.layers[il].ffn_norm,
  6628. model.layers[il].ffn_norm_b,
  6629. LLM_NORM, il);
  6630. cb(cur, "ffn_norm", il);
  6631. cur = build_ffn(cur,
  6632. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  6633. NULL, NULL, NULL,
  6634. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  6635. NULL,
  6636. LLM_FFN_GELU, LLM_FFN_SEQ, il);
  6637. cb(cur, "ffn_out", il);
  6638. }
  6639. cur = ggml_add(ctx0, cur, ffn_inp);
  6640. cur = build_cvec(cur, il);
  6641. cb(cur, "l_out", il);
  6642. // input for next layer
  6643. inpL = cur;
  6644. }
  6645. cur = build_norm(inpL,
  6646. model.output_norm,
  6647. model.output_norm_b,
  6648. LLM_NORM, -1);
  6649. cb(cur, "result_norm", -1);
  6650. res->t_embd = cur;
  6651. cur = build_lora_mm(model.output, cur);
  6652. cb(cur, "result_output", -1);
  6653. res->t_logits = cur;
  6654. ggml_build_forward_expand(gf, cur);
  6655. }
  6656. };
  6657. struct llm_build_mpt : public llm_graph_context {
  6658. llm_build_mpt(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  6659. const int64_t n_embd_head = hparams.n_embd_head_v;
  6660. const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  6661. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  6662. ggml_tensor * cur;
  6663. ggml_tensor * pos;
  6664. ggml_tensor * inpL;
  6665. inpL = build_inp_embd(model.tok_embd);
  6666. auto * inp_attn = build_attn_inp_kv();
  6667. if (model.pos_embd) {
  6668. // inp_pos - contains the positions
  6669. ggml_tensor * inp_pos = build_inp_pos();
  6670. pos = ggml_get_rows(ctx0, model.pos_embd, inp_pos);
  6671. cb(pos, "pos_embd", -1);
  6672. inpL = ggml_add(ctx0, inpL, pos);
  6673. cb(inpL, "inpL", -1);
  6674. }
  6675. ggml_tensor * inp_out_ids = build_inp_out_ids();
  6676. for (int il = 0; il < n_layer; ++il) {
  6677. ggml_tensor * attn_norm;
  6678. attn_norm = build_norm(inpL,
  6679. model.layers[il].attn_norm,
  6680. model.layers[il].attn_norm_b,
  6681. LLM_NORM, il);
  6682. cb(attn_norm, "attn_norm", il);
  6683. // self-attention
  6684. {
  6685. cur = attn_norm;
  6686. cur = build_lora_mm(model.layers[il].wqkv, cur);
  6687. cb(cur, "wqkv", il);
  6688. if (model.layers[il].bqkv){
  6689. cur = ggml_add(ctx0, cur, model.layers[il].bqkv);
  6690. cb(cur, "bqkv", il);
  6691. }
  6692. if (hparams.f_clamp_kqv > 0.0f) {
  6693. cur = ggml_clamp(ctx0, cur, -hparams.f_clamp_kqv, hparams.f_clamp_kqv);
  6694. cb(cur, "wqkv_clamped", il);
  6695. }
  6696. 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));
  6697. 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));
  6698. ggml_tensor * Vcur = 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 + n_embd_gqa));
  6699. // Q/K Layernorm
  6700. if (model.layers[il].attn_q_norm) {
  6701. Qcur = build_norm(Qcur,
  6702. model.layers[il].attn_q_norm,
  6703. model.layers[il].attn_q_norm_b,
  6704. LLM_NORM, il);
  6705. Kcur = build_norm(Kcur,
  6706. model.layers[il].attn_k_norm,
  6707. model.layers[il].attn_k_norm_b,
  6708. LLM_NORM, il);
  6709. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  6710. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  6711. }
  6712. cb(Qcur, "Qcur", il);
  6713. cb(Kcur, "Kcur", il);
  6714. cb(Vcur, "Vcur", il);
  6715. cur = build_attn(inp_attn,
  6716. model.layers[il].wo, model.layers[il].bo,
  6717. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  6718. }
  6719. if (il == n_layer - 1 && inp_out_ids) {
  6720. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  6721. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  6722. }
  6723. // Add the input
  6724. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
  6725. cb(ffn_inp, "ffn_inp", il);
  6726. // feed forward
  6727. {
  6728. cur = build_norm(ffn_inp,
  6729. model.layers[il].ffn_norm,
  6730. model.layers[il].ffn_norm_b,
  6731. LLM_NORM, il);
  6732. cb(cur, "ffn_norm", il);
  6733. cur = build_ffn(cur,
  6734. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  6735. NULL, NULL, NULL,
  6736. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  6737. model.layers[il].ffn_act,
  6738. LLM_FFN_GELU, LLM_FFN_SEQ, il);
  6739. cb(cur, "ffn_out", il);
  6740. }
  6741. cur = ggml_add(ctx0, cur, ffn_inp);
  6742. cur = build_cvec(cur, il);
  6743. cb(cur, "l_out", il);
  6744. // input for next layer
  6745. inpL = cur;
  6746. }
  6747. cur = inpL;
  6748. cur = build_norm(cur,
  6749. model.output_norm,
  6750. model.output_norm_b,
  6751. LLM_NORM, -1);
  6752. cb(cur, "result_norm", -1);
  6753. res->t_embd = cur;
  6754. cur = build_lora_mm(model.output, cur);
  6755. cb(cur, "result_output", -1);
  6756. res->t_logits = cur;
  6757. ggml_build_forward_expand(gf, cur);
  6758. }
  6759. };
  6760. struct llm_build_stablelm : public llm_graph_context {
  6761. llm_build_stablelm(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  6762. const int64_t n_embd_head = hparams.n_embd_head_v;
  6763. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  6764. ggml_tensor * cur;
  6765. ggml_tensor * inpL;
  6766. inpL = build_inp_embd(model.tok_embd);
  6767. // inp_pos - contains the positions
  6768. ggml_tensor * inp_pos = build_inp_pos();
  6769. auto * inp_attn = build_attn_inp_kv();
  6770. ggml_tensor * inp_out_ids = build_inp_out_ids();
  6771. for (int il = 0; il < n_layer; ++il) {
  6772. // norm
  6773. cur = build_norm(inpL,
  6774. model.layers[il].attn_norm,
  6775. model.layers[il].attn_norm_b,
  6776. LLM_NORM, il);
  6777. cb(cur, "attn_norm", il);
  6778. ggml_tensor * inpSA = cur;
  6779. // self-attention
  6780. {
  6781. // compute Q and K and RoPE them
  6782. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  6783. cb(Qcur, "Qcur", il);
  6784. if (model.layers[il].bq) {
  6785. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  6786. cb(Qcur, "Qcur", il);
  6787. }
  6788. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  6789. cb(Kcur, "Kcur", il);
  6790. if (model.layers[il].bk) {
  6791. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  6792. cb(Kcur, "Kcur", il);
  6793. }
  6794. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  6795. cb(Vcur, "Vcur", il);
  6796. if (model.layers[il].bv) {
  6797. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  6798. cb(Vcur, "Vcur", il);
  6799. }
  6800. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  6801. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  6802. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  6803. if (model.layers[il].attn_q_norm) {
  6804. Qcur = build_norm(Qcur,
  6805. model.layers[il].attn_q_norm,
  6806. NULL,
  6807. LLM_NORM, il);
  6808. cb(Qcur, "Qcur", il);
  6809. }
  6810. if (model.layers[il].attn_k_norm) {
  6811. Kcur = build_norm(Kcur,
  6812. model.layers[il].attn_k_norm,
  6813. NULL,
  6814. LLM_NORM, il);
  6815. cb(Kcur, "Kcur", il);
  6816. }
  6817. Qcur = ggml_rope_ext(
  6818. ctx0, Qcur, inp_pos, nullptr,
  6819. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  6820. ext_factor, attn_factor, beta_fast, beta_slow
  6821. );
  6822. Kcur = ggml_rope_ext(
  6823. ctx0, Kcur, inp_pos, nullptr,
  6824. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  6825. ext_factor, attn_factor, beta_fast, beta_slow
  6826. );
  6827. cb(Qcur, "Qcur", il);
  6828. cb(Kcur, "Kcur", il);
  6829. cb(Vcur, "Vcur", il);
  6830. cur = build_attn(inp_attn,
  6831. model.layers[il].wo, NULL,
  6832. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  6833. }
  6834. if (il == n_layer - 1 && inp_out_ids) {
  6835. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  6836. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  6837. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  6838. }
  6839. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
  6840. cb(ffn_inp, "ffn_inp", il);
  6841. // feed-forward network
  6842. {
  6843. if (model.layers[il].ffn_norm) {
  6844. cur = build_norm(ffn_inp,
  6845. model.layers[il].ffn_norm,
  6846. model.layers[il].ffn_norm_b,
  6847. LLM_NORM, il);
  6848. cb(cur, "ffn_norm", il);
  6849. } else {
  6850. // parallel residual
  6851. cur = inpSA;
  6852. }
  6853. cur = build_ffn(cur,
  6854. model.layers[il].ffn_up, NULL, NULL,
  6855. model.layers[il].ffn_gate, NULL, NULL,
  6856. model.layers[il].ffn_down, NULL, NULL,
  6857. NULL,
  6858. LLM_FFN_SILU, LLM_FFN_PAR, il);
  6859. cb(cur, "ffn_out", il);
  6860. }
  6861. cur = ggml_add(ctx0, cur, ffn_inp);
  6862. cur = build_cvec(cur, il);
  6863. cb(cur, "l_out", il);
  6864. // input for next layer
  6865. inpL = cur;
  6866. }
  6867. cur = inpL;
  6868. cur = build_norm(cur,
  6869. model.output_norm,
  6870. model.output_norm_b,
  6871. LLM_NORM, -1);
  6872. cb(cur, "result_norm", -1);
  6873. res->t_embd = cur;
  6874. // lm_head
  6875. cur = build_lora_mm(model.output, cur);
  6876. cb(cur, "result_output", -1);
  6877. res->t_logits = cur;
  6878. ggml_build_forward_expand(gf, cur);
  6879. }
  6880. };
  6881. struct llm_build_qwen : public llm_graph_context {
  6882. llm_build_qwen(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  6883. const int64_t n_embd_head = hparams.n_embd_head_v;
  6884. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  6885. ggml_tensor * cur;
  6886. ggml_tensor * inpL;
  6887. inpL = build_inp_embd(model.tok_embd);
  6888. // inp_pos - contains the positions
  6889. ggml_tensor * inp_pos = build_inp_pos();
  6890. auto * inp_attn = build_attn_inp_kv();
  6891. ggml_tensor * inp_out_ids = build_inp_out_ids();
  6892. for (int il = 0; il < n_layer; ++il) {
  6893. ggml_tensor * inpSA = inpL;
  6894. cur = build_norm(inpL,
  6895. model.layers[il].attn_norm, NULL,
  6896. LLM_NORM_RMS, il);
  6897. cb(cur, "attn_norm", il);
  6898. // self-attention
  6899. {
  6900. cur = build_lora_mm(model.layers[il].wqkv, cur);
  6901. cb(cur, "wqkv", il);
  6902. cur = ggml_add(ctx0, cur, model.layers[il].bqkv);
  6903. cb(cur, "bqkv", il);
  6904. 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));
  6905. 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));
  6906. ggml_tensor * Vcur = ggml_view_3d(ctx0, cur, n_embd_head, n_head_kv, n_tokens, n_embd_head*sizeof(float), cur->nb[1], 2*sizeof(float)*(n_embd));
  6907. // using mode = 2 for neox mode
  6908. Qcur = ggml_rope_ext(
  6909. ctx0, Qcur, inp_pos, nullptr,
  6910. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  6911. ext_factor, attn_factor, beta_fast, beta_slow
  6912. );
  6913. Kcur = ggml_rope_ext(
  6914. ctx0, Kcur, inp_pos, nullptr,
  6915. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  6916. ext_factor, attn_factor, beta_fast, beta_slow
  6917. );
  6918. cb(Qcur, "Qcur", il);
  6919. cb(Kcur, "Kcur", il);
  6920. cb(Vcur, "Vcur", il);
  6921. cur = build_attn(inp_attn,
  6922. model.layers[il].wo, NULL,
  6923. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  6924. }
  6925. if (il == n_layer - 1 && inp_out_ids) {
  6926. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  6927. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  6928. }
  6929. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  6930. cb(ffn_inp, "ffn_inp", il);
  6931. // feed-forward forward
  6932. {
  6933. cur = build_norm(ffn_inp,
  6934. model.layers[il].ffn_norm, NULL,
  6935. LLM_NORM_RMS, il);
  6936. cb(cur, "ffn_norm", il);
  6937. cur = build_ffn(cur,
  6938. model.layers[il].ffn_up, NULL, NULL,
  6939. model.layers[il].ffn_gate, NULL, NULL,
  6940. model.layers[il].ffn_down, NULL, NULL,
  6941. NULL,
  6942. LLM_FFN_SILU, LLM_FFN_PAR, il);
  6943. cb(cur, "ffn_out", il);
  6944. }
  6945. cur = ggml_add(ctx0, cur, ffn_inp);
  6946. cur = build_cvec(cur, il);
  6947. cb(cur, "l_out", il);
  6948. // input for next layer
  6949. inpL = cur;
  6950. }
  6951. cur = inpL;
  6952. cur = build_norm(cur,
  6953. model.output_norm, NULL,
  6954. LLM_NORM_RMS, -1);
  6955. cb(cur, "result_norm", -1);
  6956. res->t_embd = cur;
  6957. // lm_head
  6958. cur = build_lora_mm(model.output, cur);
  6959. cb(cur, "result_output", -1);
  6960. res->t_logits = cur;
  6961. ggml_build_forward_expand(gf, cur);
  6962. }
  6963. };
  6964. struct llm_build_qwen2 : public llm_graph_context {
  6965. llm_build_qwen2(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  6966. const int64_t n_embd_head = hparams.n_embd_head_v;
  6967. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  6968. GGML_ASSERT(n_embd_head == hparams.n_rot);
  6969. ggml_tensor * cur;
  6970. ggml_tensor * inpL;
  6971. inpL = build_inp_embd(model.tok_embd);
  6972. // inp_pos - contains the positions
  6973. ggml_tensor * inp_pos = build_inp_pos();
  6974. auto * inp_attn = build_attn_inp_kv();
  6975. ggml_tensor * inp_out_ids = build_inp_out_ids();
  6976. for (int il = 0; il < n_layer; ++il) {
  6977. ggml_tensor * inpSA = inpL;
  6978. // norm
  6979. cur = build_norm(inpL,
  6980. model.layers[il].attn_norm, NULL,
  6981. LLM_NORM_RMS, il);
  6982. cb(cur, "attn_norm", il);
  6983. // self-attention
  6984. {
  6985. // compute Q and K and RoPE them
  6986. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  6987. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  6988. cb(Qcur, "Qcur", il);
  6989. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  6990. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  6991. cb(Kcur, "Kcur", il);
  6992. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  6993. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  6994. cb(Vcur, "Vcur", il);
  6995. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  6996. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  6997. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  6998. Qcur = ggml_rope_ext(
  6999. ctx0, Qcur, inp_pos, nullptr,
  7000. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  7001. ext_factor, attn_factor, beta_fast, beta_slow
  7002. );
  7003. Kcur = ggml_rope_ext(
  7004. ctx0, Kcur, inp_pos, nullptr,
  7005. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  7006. ext_factor, attn_factor, beta_fast, beta_slow
  7007. );
  7008. cb(Qcur, "Qcur", il);
  7009. cb(Kcur, "Kcur", il);
  7010. cb(Vcur, "Vcur", il);
  7011. cur = build_attn(inp_attn,
  7012. model.layers[il].wo, model.layers[il].bo,
  7013. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  7014. }
  7015. if (il == n_layer - 1 && inp_out_ids) {
  7016. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  7017. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  7018. }
  7019. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  7020. cb(ffn_inp, "ffn_inp", il);
  7021. // feed-forward network
  7022. cur = build_norm(ffn_inp,
  7023. model.layers[il].ffn_norm, NULL,
  7024. LLM_NORM_RMS, il);
  7025. cb(cur, "ffn_norm", il);
  7026. cur = build_ffn(cur,
  7027. model.layers[il].ffn_up, NULL, NULL,
  7028. model.layers[il].ffn_gate, NULL, NULL,
  7029. model.layers[il].ffn_down, NULL, NULL,
  7030. NULL,
  7031. LLM_FFN_SILU, LLM_FFN_PAR, il);
  7032. cb(cur, "ffn_out", il);
  7033. cur = ggml_add(ctx0, cur, ffn_inp);
  7034. cur = build_cvec(cur, il);
  7035. cb(cur, "l_out", il);
  7036. // input for next layer
  7037. inpL = cur;
  7038. }
  7039. cur = inpL;
  7040. cur = build_norm(cur,
  7041. model.output_norm, NULL,
  7042. LLM_NORM_RMS, -1);
  7043. cb(cur, "result_norm", -1);
  7044. res->t_embd = cur;
  7045. // lm_head
  7046. cur = build_lora_mm(model.output, cur);
  7047. if (model.output_b != nullptr) {
  7048. cur = ggml_add(ctx0, cur, model.output_b);
  7049. }
  7050. cb(cur, "result_output", -1);
  7051. res->t_logits = cur;
  7052. ggml_build_forward_expand(gf, cur);
  7053. }
  7054. };
  7055. struct llm_build_dream : public llm_graph_context {
  7056. llm_build_dream(const llama_model & model, const llm_graph_params & params) :
  7057. llm_graph_context(params) {
  7058. //copied from qwen2
  7059. const int64_t n_embd_head = hparams.n_embd_head_v;
  7060. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  7061. GGML_ASSERT(n_embd_head == hparams.n_rot);
  7062. ggml_tensor * cur;
  7063. ggml_tensor * inpL;
  7064. inpL = build_inp_embd(model.tok_embd);
  7065. // inp_pos - contains the positions
  7066. ggml_tensor * inp_pos = build_inp_pos();
  7067. auto * inp_attn = build_attn_inp_no_cache();
  7068. ggml_tensor * inp_out_ids = build_inp_out_ids();
  7069. for (int il = 0; il < n_layer; ++il) {
  7070. ggml_tensor * inpSA = inpL;
  7071. // norm
  7072. cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
  7073. cb(cur, "attn_norm", il);
  7074. // self-attention
  7075. {
  7076. // compute Q and K and RoPE them
  7077. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  7078. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  7079. cb(Qcur, "Qcur", il);
  7080. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  7081. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  7082. cb(Kcur, "Kcur", il);
  7083. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  7084. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  7085. cb(Vcur, "Vcur", il);
  7086. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  7087. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  7088. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  7089. Qcur = ggml_rope_ext(ctx0, Qcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  7090. ext_factor, attn_factor, beta_fast, beta_slow);
  7091. Kcur = ggml_rope_ext(ctx0, Kcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  7092. ext_factor, attn_factor, beta_fast, beta_slow);
  7093. cb(Qcur, "Qcur", il);
  7094. cb(Kcur, "Kcur", il);
  7095. cb(Vcur, "Vcur", il);
  7096. cur = build_attn(inp_attn,
  7097. model.layers[il].wo, model.layers[il].bo,
  7098. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f / sqrtf(float(n_embd_head)), il);
  7099. }
  7100. if (il == n_layer - 1 && inp_out_ids) {
  7101. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  7102. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  7103. }
  7104. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  7105. cb(ffn_inp, "ffn_inp", il);
  7106. // feed-forward network
  7107. cur = build_norm(ffn_inp, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);
  7108. cb(cur, "ffn_norm", il);
  7109. cur = build_ffn(cur, model.layers[il].ffn_up, NULL, NULL, model.layers[il].ffn_gate, NULL, NULL,
  7110. model.layers[il].ffn_down, NULL, NULL, NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
  7111. cb(cur, "ffn_out", il);
  7112. cur = ggml_add(ctx0, cur, ffn_inp);
  7113. cur = build_cvec(cur, il);
  7114. cb(cur, "l_out", il);
  7115. // input for next layer
  7116. inpL = cur;
  7117. }
  7118. cur = inpL;
  7119. cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1);
  7120. cb(cur, "result_norm", -1);
  7121. res->t_embd = cur;
  7122. // lm_head
  7123. cur = build_lora_mm(model.output, cur);
  7124. cb(cur, "result_output", -1);
  7125. res->t_logits = cur;
  7126. ggml_build_forward_expand(gf, cur);
  7127. }
  7128. };
  7129. struct llm_build_llada : public llm_graph_context {
  7130. llm_build_llada(const llama_model & model, const llm_graph_params & params) :
  7131. llm_graph_context(params) {
  7132. // LLaDA is similar to LLaMA but uses non-causal attention for diffusion
  7133. const int64_t n_embd_head = hparams.n_embd_head_v;
  7134. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  7135. GGML_ASSERT(n_embd_head == hparams.n_rot);
  7136. ggml_tensor * cur;
  7137. ggml_tensor * inpL;
  7138. inpL = build_inp_embd(model.tok_embd);
  7139. // inp_pos - contains the positions
  7140. ggml_tensor * inp_pos = build_inp_pos();
  7141. // Non-causal attention for diffusion
  7142. auto * inp_attn = build_attn_inp_no_cache();
  7143. ggml_tensor * inp_out_ids = build_inp_out_ids();
  7144. for (int il = 0; il < n_layer; ++il) {
  7145. ggml_tensor * inpSA = inpL;
  7146. // norm
  7147. cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
  7148. cb(cur, "attn_norm", il);
  7149. // self-attention
  7150. {
  7151. // compute separate Q, K, V projections without bias, matching LLaDALlamaBlock
  7152. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  7153. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  7154. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  7155. cb(Qcur, "Qcur", il);
  7156. cb(Kcur, "Kcur", il);
  7157. cb(Vcur, "Vcur", il);
  7158. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  7159. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  7160. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  7161. Qcur = ggml_rope_ext(ctx0, Qcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  7162. ext_factor, attn_factor, beta_fast, beta_slow);
  7163. Kcur = ggml_rope_ext(ctx0, Kcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  7164. ext_factor, attn_factor, beta_fast, beta_slow);
  7165. cb(Qcur, "Qcur", il);
  7166. cb(Kcur, "Kcur", il);
  7167. cb(Vcur, "Vcur", il);
  7168. cur = build_attn(inp_attn,
  7169. model.layers[il].wo, NULL,
  7170. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f / sqrtf(float(n_embd_head)), il);
  7171. }
  7172. if (il == n_layer - 1 && inp_out_ids) {
  7173. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  7174. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  7175. }
  7176. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  7177. cb(ffn_inp, "ffn_inp", il);
  7178. // feed-forward network
  7179. cur = build_norm(ffn_inp, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);
  7180. cb(cur, "ffn_norm", il);
  7181. cur = build_ffn(cur, model.layers[il].ffn_up, NULL, NULL, model.layers[il].ffn_gate, NULL, NULL,
  7182. model.layers[il].ffn_down, NULL, NULL, NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
  7183. cb(cur, "ffn_out", il);
  7184. cur = ggml_add(ctx0, cur, ffn_inp);
  7185. cur = build_cvec(cur, il);
  7186. cb(cur, "l_out", il);
  7187. // input for next layer
  7188. inpL = cur;
  7189. }
  7190. cur = inpL;
  7191. cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1);
  7192. cb(cur, "result_norm", -1);
  7193. res->t_embd = cur;
  7194. // lm_head
  7195. cur = build_lora_mm(model.output, cur);
  7196. cb(cur, "result_output", -1);
  7197. res->t_logits = cur;
  7198. ggml_build_forward_expand(gf, cur);
  7199. }
  7200. };
  7201. struct llm_build_qwen2vl : public llm_graph_context {
  7202. llm_build_qwen2vl(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  7203. const int64_t n_embd_head = hparams.n_embd_head_v;
  7204. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  7205. GGML_ASSERT(n_embd_head == hparams.n_rot);
  7206. ggml_tensor * cur;
  7207. ggml_tensor * inpL;
  7208. inpL = build_inp_embd(model.tok_embd);
  7209. // inp_pos - contains the positions
  7210. ggml_tensor * inp_pos = build_inp_pos();
  7211. auto * inp_attn = build_attn_inp_kv();
  7212. int sections[4];
  7213. std::copy(std::begin(hparams.rope_sections), std::begin(hparams.rope_sections) + 4, sections);
  7214. ggml_tensor * inp_out_ids = build_inp_out_ids();
  7215. for (int il = 0; il < n_layer; ++il) {
  7216. ggml_tensor * inpSA = inpL;
  7217. // norm
  7218. cur = build_norm(inpL,
  7219. model.layers[il].attn_norm, NULL,
  7220. LLM_NORM_RMS, il);
  7221. cb(cur, "attn_norm", il);
  7222. // self-attention
  7223. {
  7224. // compute Q and K and RoPE them
  7225. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  7226. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  7227. cb(Qcur, "Qcur", il);
  7228. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  7229. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  7230. cb(Kcur, "Kcur", il);
  7231. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  7232. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  7233. cb(Vcur, "Vcur", il);
  7234. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  7235. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  7236. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  7237. Qcur = ggml_rope_multi(
  7238. ctx0, Qcur, inp_pos, nullptr,
  7239. n_rot, sections, rope_type, n_ctx_orig, freq_base, freq_scale,
  7240. ext_factor, attn_factor, beta_fast, beta_slow
  7241. );
  7242. Kcur = ggml_rope_multi(
  7243. ctx0, Kcur, inp_pos, nullptr,
  7244. n_rot, sections, rope_type, n_ctx_orig, freq_base, freq_scale,
  7245. ext_factor, attn_factor, beta_fast, beta_slow
  7246. );
  7247. cb(Qcur, "Qcur", il);
  7248. cb(Kcur, "Kcur", il);
  7249. cb(Vcur, "Vcur", il);
  7250. cur = build_attn(inp_attn,
  7251. model.layers[il].wo, model.layers[il].bo,
  7252. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  7253. }
  7254. if (il == n_layer - 1 && inp_out_ids) {
  7255. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  7256. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  7257. }
  7258. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  7259. cb(ffn_inp, "ffn_inp", il);
  7260. // feed-forward network
  7261. cur = build_norm(ffn_inp,
  7262. model.layers[il].ffn_norm, NULL,
  7263. LLM_NORM_RMS, il);
  7264. cb(cur, "ffn_norm", il);
  7265. cur = build_ffn(cur,
  7266. model.layers[il].ffn_up, NULL, NULL,
  7267. model.layers[il].ffn_gate, NULL, NULL,
  7268. model.layers[il].ffn_down, NULL, NULL,
  7269. NULL,
  7270. LLM_FFN_SILU, LLM_FFN_PAR, il);
  7271. cb(cur, "ffn_out", il);
  7272. cur = ggml_add(ctx0, cur, ffn_inp);
  7273. cur = build_cvec(cur, il);
  7274. cb(cur, "l_out", il);
  7275. // input for next layer
  7276. inpL = cur;
  7277. }
  7278. cur = inpL;
  7279. cur = build_norm(cur,
  7280. model.output_norm, NULL,
  7281. LLM_NORM_RMS, -1);
  7282. cb(cur, "result_norm", -1);
  7283. res->t_embd = cur;
  7284. // lm_head
  7285. cur = build_lora_mm(model.output, cur);
  7286. cb(cur, "result_output", -1);
  7287. res->t_logits = cur;
  7288. ggml_build_forward_expand(gf, cur);
  7289. }
  7290. };
  7291. struct llm_build_qwen2moe : public llm_graph_context {
  7292. llm_build_qwen2moe(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  7293. const int64_t n_embd_head = hparams.n_embd_head_v;
  7294. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  7295. GGML_ASSERT(n_embd_head == hparams.n_rot);
  7296. ggml_tensor * cur;
  7297. ggml_tensor * inpL;
  7298. inpL = build_inp_embd(model.tok_embd);
  7299. // inp_pos - contains the positions
  7300. ggml_tensor * inp_pos = build_inp_pos();
  7301. auto * inp_attn = build_attn_inp_kv();
  7302. ggml_tensor * inp_out_ids = build_inp_out_ids();
  7303. for (int il = 0; il < n_layer; ++il) {
  7304. ggml_tensor * inpSA = inpL;
  7305. // norm
  7306. cur = build_norm(inpL,
  7307. model.layers[il].attn_norm, NULL,
  7308. LLM_NORM_RMS, il);
  7309. cb(cur, "attn_norm", il);
  7310. // self_attention
  7311. {
  7312. // compute Q and K and RoPE them
  7313. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  7314. cb(Qcur, "Qcur", il);
  7315. if (model.layers[il].bq) {
  7316. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  7317. cb(Qcur, "Qcur", il);
  7318. }
  7319. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  7320. cb(Kcur, "Kcur", il);
  7321. if (model.layers[il].bk) {
  7322. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  7323. cb(Kcur, "Kcur", il);
  7324. }
  7325. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  7326. cb(Vcur, "Vcur", il);
  7327. if (model.layers[il].bv) {
  7328. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  7329. cb(Vcur, "Vcur", il);
  7330. }
  7331. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  7332. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  7333. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  7334. Qcur = ggml_rope_ext(
  7335. ctx0, Qcur, inp_pos, nullptr,
  7336. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  7337. ext_factor, attn_factor, beta_fast, beta_slow
  7338. );
  7339. Kcur = ggml_rope_ext(
  7340. ctx0, Kcur, inp_pos, nullptr,
  7341. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  7342. ext_factor, attn_factor, beta_fast, beta_slow
  7343. );
  7344. cb(Qcur, "Qcur", il);
  7345. cb(Kcur, "Kcur", il);
  7346. cb(Vcur, "Vcur", il);
  7347. cur = build_attn(inp_attn,
  7348. model.layers[il].wo, model.layers[il].bo,
  7349. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  7350. }
  7351. if (il == n_layer - 1 && inp_out_ids) {
  7352. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  7353. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  7354. }
  7355. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  7356. cb(ffn_inp, "ffn_inp", il);
  7357. // MoE branch
  7358. cur = build_norm(ffn_inp,
  7359. model.layers[il].ffn_norm, NULL,
  7360. LLM_NORM_RMS, il);
  7361. cb(cur, "ffn_norm", il);
  7362. ggml_tensor * moe_out =
  7363. build_moe_ffn(cur,
  7364. model.layers[il].ffn_gate_inp,
  7365. model.layers[il].ffn_up_exps,
  7366. model.layers[il].ffn_gate_exps,
  7367. model.layers[il].ffn_down_exps,
  7368. nullptr,
  7369. n_expert, n_expert_used,
  7370. LLM_FFN_SILU, false,
  7371. false, 0.0,
  7372. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  7373. il);
  7374. cb(moe_out, "ffn_moe_out", il);
  7375. // FFN shared expert
  7376. {
  7377. ggml_tensor * cur_gate_inp = build_lora_mm(model.layers[il].ffn_gate_inp_shexp, cur);
  7378. cb(cur_gate_inp, "ffn_shexp_gate_inp", il);
  7379. // sigmoid
  7380. ggml_tensor * cur_gate = ggml_div(ctx0, ggml_silu(ctx0, cur_gate_inp), cur_gate_inp);
  7381. cb(cur_gate, "ffn_shexp_gate", il);
  7382. ggml_tensor * cur_ffn = build_ffn(cur,
  7383. model.layers[il].ffn_up_shexp, NULL, NULL,
  7384. model.layers[il].ffn_gate_shexp, NULL, NULL,
  7385. model.layers[il].ffn_down_shexp, NULL, NULL,
  7386. NULL,
  7387. LLM_FFN_SILU, LLM_FFN_PAR, il);
  7388. cb(cur_ffn, "ffn_shexp", il);
  7389. ggml_tensor * ffn_shexp_out = ggml_mul(ctx0, cur_ffn, cur_gate);
  7390. cb(ffn_shexp_out, "ffn_shexp_out", il);
  7391. moe_out = ggml_add(ctx0, moe_out, ffn_shexp_out);
  7392. cb(moe_out, "ffn_out", il);
  7393. cur = moe_out;
  7394. }
  7395. cur = ggml_add(ctx0, cur, ffn_inp);
  7396. cur = build_cvec(cur, il);
  7397. cb(cur, "l_out", il);
  7398. // input for next layer
  7399. inpL = cur;
  7400. }
  7401. cur = inpL;
  7402. cur = build_norm(cur,
  7403. model.output_norm, NULL,
  7404. LLM_NORM_RMS, -1);
  7405. cb(cur, "result_norm", -1);
  7406. res->t_embd = cur;
  7407. // lm_head
  7408. cur = build_lora_mm(model.output, cur);
  7409. cb(cur, "result_output", -1);
  7410. res->t_logits = cur;
  7411. ggml_build_forward_expand(gf, cur);
  7412. }
  7413. };
  7414. struct llm_build_qwen3 : public llm_graph_context {
  7415. llm_build_qwen3(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  7416. const int64_t n_embd_head = hparams.n_embd_head_v;
  7417. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  7418. GGML_ASSERT(n_embd_head == hparams.n_rot);
  7419. ggml_tensor * cur;
  7420. ggml_tensor * inpL;
  7421. inpL = build_inp_embd(model.tok_embd);
  7422. // inp_pos - contains the positions
  7423. ggml_tensor * inp_pos = build_inp_pos();
  7424. auto * inp_attn = build_attn_inp_kv();
  7425. ggml_tensor * inp_out_ids = build_inp_out_ids();
  7426. for (int il = 0; il < n_layer; ++il) {
  7427. ggml_tensor * inpSA = inpL;
  7428. // norm
  7429. cur = build_norm(inpL,
  7430. model.layers[il].attn_norm, NULL,
  7431. LLM_NORM_RMS, il);
  7432. cb(cur, "attn_norm", il);
  7433. // self-attention
  7434. {
  7435. // compute Q and K and RoPE them
  7436. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  7437. cb(Qcur, "Qcur", il);
  7438. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  7439. cb(Kcur, "Kcur", il);
  7440. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  7441. cb(Vcur, "Vcur", il);
  7442. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  7443. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  7444. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  7445. Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);
  7446. cb(Qcur, "Qcur_normed", il);
  7447. Qcur = ggml_rope_ext(
  7448. ctx0, Qcur, inp_pos, nullptr,
  7449. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  7450. ext_factor, attn_factor, beta_fast, beta_slow
  7451. );
  7452. Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);
  7453. cb(Kcur, "Kcur_normed", il);
  7454. Kcur = ggml_rope_ext(
  7455. ctx0, Kcur, inp_pos, nullptr,
  7456. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  7457. ext_factor, attn_factor, beta_fast, beta_slow
  7458. );
  7459. cb(Qcur, "Qcur", il);
  7460. cb(Kcur, "Kcur", il);
  7461. cb(Vcur, "Vcur", il);
  7462. cur = build_attn(inp_attn,
  7463. model.layers[il].wo, model.layers[il].bo,
  7464. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  7465. }
  7466. if (il == n_layer - 1 && inp_out_ids) {
  7467. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  7468. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  7469. }
  7470. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  7471. cb(ffn_inp, "ffn_inp", il);
  7472. // feed-forward network
  7473. cur = build_norm(ffn_inp,
  7474. model.layers[il].ffn_norm, NULL,
  7475. LLM_NORM_RMS, il);
  7476. cb(cur, "ffn_norm", il);
  7477. cur = build_ffn(cur,
  7478. model.layers[il].ffn_up, NULL, NULL,
  7479. model.layers[il].ffn_gate, NULL, NULL,
  7480. model.layers[il].ffn_down, NULL, NULL,
  7481. NULL,
  7482. LLM_FFN_SILU, LLM_FFN_PAR, il);
  7483. cb(cur, "ffn_out", il);
  7484. cur = ggml_add(ctx0, cur, ffn_inp);
  7485. cur = build_cvec(cur, il);
  7486. cb(cur, "l_out", il);
  7487. // input for next layer
  7488. inpL = cur;
  7489. }
  7490. cur = inpL;
  7491. cur = build_norm(cur,
  7492. model.output_norm, NULL,
  7493. LLM_NORM_RMS, -1);
  7494. cb(cur, "result_norm", -1);
  7495. res->t_embd = cur;
  7496. // lm_head
  7497. cur = build_lora_mm(model.output, cur);
  7498. cb(cur, "result_output", -1);
  7499. res->t_logits = cur;
  7500. ggml_build_forward_expand(gf, cur);
  7501. }
  7502. };
  7503. struct llm_build_qwen3moe : public llm_graph_context {
  7504. llm_build_qwen3moe(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  7505. const int64_t n_embd_head = hparams.n_embd_head_v;
  7506. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  7507. GGML_ASSERT(n_embd_head == hparams.n_rot);
  7508. ggml_tensor * cur;
  7509. ggml_tensor * inpL;
  7510. inpL = build_inp_embd(model.tok_embd);
  7511. // inp_pos - contains the positions
  7512. ggml_tensor * inp_pos = build_inp_pos();
  7513. auto * inp_attn = build_attn_inp_kv();
  7514. ggml_tensor * inp_out_ids = build_inp_out_ids();
  7515. for (int il = 0; il < n_layer; ++il) {
  7516. ggml_tensor * inpSA = inpL;
  7517. // norm
  7518. cur = build_norm(inpL,
  7519. model.layers[il].attn_norm, NULL,
  7520. LLM_NORM_RMS, il);
  7521. cb(cur, "attn_norm", il);
  7522. // self_attention
  7523. {
  7524. // compute Q and K and RoPE them
  7525. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  7526. cb(Qcur, "Qcur", il);
  7527. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  7528. cb(Kcur, "Kcur", il);
  7529. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  7530. cb(Vcur, "Vcur", il);
  7531. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  7532. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  7533. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  7534. Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);
  7535. cb(Qcur, "Qcur_normed", il);
  7536. Qcur = ggml_rope_ext(
  7537. ctx0, Qcur, inp_pos, nullptr,
  7538. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  7539. ext_factor, attn_factor, beta_fast, beta_slow
  7540. );
  7541. Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);
  7542. cb(Kcur, "Kcur_normed", il);
  7543. Kcur = ggml_rope_ext(
  7544. ctx0, Kcur, inp_pos, nullptr,
  7545. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  7546. ext_factor, attn_factor, beta_fast, beta_slow
  7547. );
  7548. cb(Qcur, "Qcur", il);
  7549. cb(Kcur, "Kcur", il);
  7550. cb(Vcur, "Vcur", il);
  7551. cur = build_attn(inp_attn,
  7552. model.layers[il].wo, model.layers[il].bo,
  7553. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  7554. }
  7555. if (il == n_layer - 1 && inp_out_ids) {
  7556. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  7557. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  7558. }
  7559. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  7560. cb(ffn_inp, "ffn_inp", il);
  7561. // MoE branch
  7562. cur = build_norm(ffn_inp,
  7563. model.layers[il].ffn_norm, NULL,
  7564. LLM_NORM_RMS, il);
  7565. cb(cur, "ffn_norm", il);
  7566. ggml_tensor * moe_out =
  7567. build_moe_ffn(cur,
  7568. model.layers[il].ffn_gate_inp,
  7569. model.layers[il].ffn_up_exps,
  7570. model.layers[il].ffn_gate_exps,
  7571. model.layers[il].ffn_down_exps,
  7572. nullptr,
  7573. n_expert, n_expert_used,
  7574. LLM_FFN_SILU, true,
  7575. false, 0.0,
  7576. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  7577. il);
  7578. cb(moe_out, "ffn_moe_out", il);
  7579. cur = moe_out;
  7580. cur = ggml_add(ctx0, cur, ffn_inp);
  7581. cur = build_cvec(cur, il);
  7582. cb(cur, "l_out", il);
  7583. // input for next layer
  7584. inpL = cur;
  7585. }
  7586. cur = inpL;
  7587. cur = build_norm(cur,
  7588. model.output_norm, NULL,
  7589. LLM_NORM_RMS, -1);
  7590. cb(cur, "result_norm", -1);
  7591. res->t_embd = cur;
  7592. // lm_head
  7593. cur = build_lora_mm(model.output, cur);
  7594. cb(cur, "result_output", -1);
  7595. res->t_logits = cur;
  7596. ggml_build_forward_expand(gf, cur);
  7597. }
  7598. };
  7599. struct llm_build_phi2 : public llm_graph_context {
  7600. llm_build_phi2(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  7601. const int64_t n_embd_head = hparams.n_embd_head_v;
  7602. const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  7603. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  7604. ggml_tensor * cur;
  7605. ggml_tensor * attn_norm_output;
  7606. ggml_tensor * ffn_output;
  7607. ggml_tensor * inpL;
  7608. inpL = build_inp_embd(model.tok_embd);
  7609. // inp_pos - contains the positions
  7610. ggml_tensor * inp_pos = build_inp_pos();
  7611. auto * inp_attn = build_attn_inp_kv();
  7612. ggml_tensor * inp_out_ids = build_inp_out_ids();
  7613. for (int il = 0; il < n_layer; ++il) {
  7614. attn_norm_output = build_norm(inpL,
  7615. model.layers[il].attn_norm,
  7616. model.layers[il].attn_norm_b,
  7617. LLM_NORM, il);
  7618. cb(attn_norm_output, "attn_norm", il);
  7619. // self-attention
  7620. {
  7621. ggml_tensor * Qcur = nullptr;
  7622. ggml_tensor * Kcur = nullptr;
  7623. ggml_tensor * Vcur = nullptr;
  7624. if (model.layers[il].wqkv) {
  7625. cur = build_lora_mm(model.layers[il].wqkv, attn_norm_output);
  7626. cb(cur, "wqkv", il);
  7627. cur = ggml_add(ctx0, cur, model.layers[il].bqkv);
  7628. cb(cur, "bqkv", il);
  7629. 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));
  7630. 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));
  7631. Vcur = 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 + n_embd_gqa));
  7632. } else {
  7633. Qcur = ggml_add(ctx0, build_lora_mm(model.layers[il].wq, attn_norm_output), model.layers[il].bq);
  7634. Kcur = ggml_add(ctx0, build_lora_mm(model.layers[il].wk, attn_norm_output), model.layers[il].bk);
  7635. Vcur = ggml_add(ctx0, build_lora_mm(model.layers[il].wv, attn_norm_output), model.layers[il].bv);
  7636. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  7637. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  7638. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  7639. }
  7640. Qcur = ggml_rope_ext(
  7641. ctx0, Qcur, inp_pos, nullptr,
  7642. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  7643. ext_factor, attn_factor, beta_fast, beta_slow
  7644. );
  7645. Kcur = ggml_rope_ext(
  7646. ctx0, Kcur, inp_pos, nullptr,
  7647. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  7648. ext_factor, attn_factor, beta_fast, beta_slow
  7649. );
  7650. cb(Qcur, "Qcur", il);
  7651. cb(Kcur, "Kcur", il);
  7652. cb(Vcur, "Vcur", il);
  7653. // with phi2, we scale the Q to avoid precision issues
  7654. // ref: https://github.com/ml-explore/mlx-examples/blob/08e862336ade809bc37d1035f94b359e7d1a5152/phi2/phi2.py#L64-L66
  7655. Qcur = ggml_scale(ctx0, Qcur, 1.0f/sqrtf(float(n_embd_head)));
  7656. cur = build_attn(inp_attn,
  7657. model.layers[il].wo, model.layers[il].bo,
  7658. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f, il);
  7659. }
  7660. if (il == n_layer - 1 && inp_out_ids) {
  7661. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  7662. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  7663. attn_norm_output = ggml_get_rows(ctx0, attn_norm_output, inp_out_ids);
  7664. }
  7665. // FF
  7666. {
  7667. ffn_output = build_ffn(attn_norm_output,
  7668. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  7669. NULL, NULL, NULL,
  7670. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  7671. NULL,
  7672. LLM_FFN_GELU, LLM_FFN_SEQ, il);
  7673. cb(ffn_output, "ffn_out", il);
  7674. }
  7675. cur = ggml_add(ctx0, cur, ffn_output);
  7676. cur = ggml_add(ctx0, cur, inpL);
  7677. cur = build_cvec(cur, il);
  7678. cb(cur, "l_out", il);
  7679. // input for next layer
  7680. inpL = cur;
  7681. }
  7682. cur = build_norm(inpL,
  7683. model.output_norm,
  7684. model.output_norm_b,
  7685. LLM_NORM, -1);
  7686. cb(cur, "result_norm", -1);
  7687. res->t_embd = cur;
  7688. cur = build_lora_mm(model.output, cur);
  7689. cb(cur, "result_output_no_bias", -1);
  7690. cur = ggml_add(ctx0, cur, model.output_b);
  7691. cb(cur, "result_output", -1);
  7692. res->t_logits = cur;
  7693. ggml_build_forward_expand(gf, cur);
  7694. }
  7695. };
  7696. template<bool iswa>
  7697. struct llm_build_phi3 : public llm_graph_context {
  7698. llm_build_phi3(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  7699. const int64_t n_embd_head = hparams.n_embd_head_v;
  7700. const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  7701. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  7702. ggml_tensor * cur;
  7703. ggml_tensor * inpL;
  7704. inpL = build_inp_embd(model.tok_embd);
  7705. // inp_pos - contains the positions
  7706. ggml_tensor * inp_pos = build_inp_pos();
  7707. using inp_attn_type = std::conditional_t<iswa, llm_graph_input_attn_kv_iswa, llm_graph_input_attn_kv>;
  7708. inp_attn_type * inp_attn = nullptr;
  7709. if constexpr (iswa) {
  7710. inp_attn = build_attn_inp_kv_iswa();
  7711. } else {
  7712. inp_attn = build_attn_inp_kv();
  7713. }
  7714. ggml_tensor * inp_out_ids = build_inp_out_ids();
  7715. for (int il = 0; il < n_layer; ++il) {
  7716. auto * residual = inpL;
  7717. // self-attention
  7718. {
  7719. // rope freq factors for 128k context
  7720. ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
  7721. ggml_tensor* attn_norm_output = build_norm(inpL,
  7722. model.layers[il].attn_norm,
  7723. model.layers[il].attn_norm_b,
  7724. LLM_NORM_RMS, il);
  7725. cb(attn_norm_output, "attn_norm", il);
  7726. ggml_tensor * Qcur = nullptr;
  7727. ggml_tensor * Kcur = nullptr;
  7728. ggml_tensor * Vcur = nullptr;
  7729. if (model.layers[il].wqkv) {
  7730. cur = build_lora_mm(model.layers[il].wqkv, attn_norm_output);
  7731. cb(cur, "wqkv", il);
  7732. 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));
  7733. 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));
  7734. Vcur = 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 + n_embd_gqa));
  7735. } else {
  7736. Qcur = ggml_add(ctx0, build_lora_mm(model.layers[il].wq, attn_norm_output), model.layers[il].bq);
  7737. Kcur = ggml_add(ctx0, build_lora_mm(model.layers[il].wk, attn_norm_output), model.layers[il].bk);
  7738. Vcur = ggml_add(ctx0, build_lora_mm(model.layers[il].wv, attn_norm_output), model.layers[il].bv);
  7739. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  7740. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  7741. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  7742. }
  7743. Qcur = ggml_rope_ext(
  7744. ctx0, Qcur, inp_pos, rope_factors,
  7745. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  7746. ext_factor, attn_factor, beta_fast, beta_slow
  7747. );
  7748. Kcur = ggml_rope_ext(
  7749. ctx0, Kcur, inp_pos, rope_factors,
  7750. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  7751. ext_factor, attn_factor, beta_fast, beta_slow
  7752. );
  7753. cb(Qcur, "Qcur", il);
  7754. cb(Kcur, "Kcur", il);
  7755. cb(Vcur, "Vcur", il);
  7756. Qcur = ggml_scale(ctx0, Qcur, 1.0f / sqrtf(float(n_embd_head)));
  7757. cb(Qcur, "Qcur", il);
  7758. cur = build_attn(inp_attn,
  7759. model.layers[il].wo, model.layers[il].bo,
  7760. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f, il);
  7761. }
  7762. if (il == n_layer - 1 && inp_out_ids) {
  7763. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  7764. residual = ggml_get_rows(ctx0, residual, inp_out_ids);
  7765. }
  7766. cur = ggml_add(ctx0, cur, residual);
  7767. residual = cur;
  7768. cur = build_norm(cur,
  7769. model.layers[il].ffn_norm, model.layers[il].ffn_norm_b,
  7770. LLM_NORM_RMS, il);
  7771. cb(cur, "ffn_norm", il);
  7772. // feed-forward network
  7773. if (model.layers[il].ffn_gate_inp == nullptr) {
  7774. cur = build_ffn(cur,
  7775. model.layers[il].ffn_up, NULL, NULL,
  7776. NULL, NULL, NULL,
  7777. model.layers[il].ffn_down, NULL, NULL,
  7778. NULL,
  7779. LLM_FFN_SWIGLU, LLM_FFN_SEQ, il);
  7780. cb(cur, "ffn_out", il);
  7781. } else {
  7782. // MoE branch
  7783. cur = build_moe_ffn(cur,
  7784. model.layers[il].ffn_gate_inp,
  7785. model.layers[il].ffn_up_exps,
  7786. model.layers[il].ffn_gate_exps,
  7787. model.layers[il].ffn_down_exps,
  7788. nullptr,
  7789. n_expert, n_expert_used,
  7790. LLM_FFN_SILU, true,
  7791. false, 0.0,
  7792. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  7793. il);
  7794. cb(cur, "ffn_moe_out", il);
  7795. }
  7796. cur = ggml_add(ctx0, residual, cur);
  7797. cur = build_cvec(cur, il);
  7798. cb(cur, "l_out", il);
  7799. // input for next layer
  7800. inpL = cur;
  7801. }
  7802. cur = build_norm(inpL,
  7803. model.output_norm,
  7804. model.output_norm_b,
  7805. LLM_NORM_RMS, -1);
  7806. cb(cur, "result_norm", -1);
  7807. res->t_embd = cur;
  7808. cur = build_lora_mm(model.output, cur);
  7809. if (model.output_b != nullptr) {
  7810. cb(cur, "result_output_no_bias", -1);
  7811. cur = ggml_add(ctx0, cur, model.output_b);
  7812. }
  7813. cb(cur, "result_output", -1);
  7814. res->t_logits = cur;
  7815. ggml_build_forward_expand(gf, cur);
  7816. }
  7817. };
  7818. struct llm_build_plamo : public llm_graph_context {
  7819. llm_build_plamo(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  7820. const int64_t n_embd_head = hparams.n_embd_head_v;
  7821. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  7822. GGML_ASSERT(n_embd_head == hparams.n_rot);
  7823. ggml_tensor * cur;
  7824. ggml_tensor * inpL;
  7825. inpL = build_inp_embd(model.tok_embd);
  7826. // inp_pos - contains the positions
  7827. ggml_tensor * inp_pos = build_inp_pos();
  7828. auto * inp_attn = build_attn_inp_kv();
  7829. ggml_tensor * inp_out_ids = build_inp_out_ids();
  7830. for (int il = 0; il < n_layer; ++il) {
  7831. // norm
  7832. cur = build_norm(inpL,
  7833. model.layers[il].attn_norm, NULL,
  7834. LLM_NORM_RMS, il);
  7835. cb(cur, "attn_norm", il);
  7836. ggml_tensor * sa_inp = cur;
  7837. // self-attention
  7838. {
  7839. // compute Q and K and RoPE them
  7840. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  7841. cb(Qcur, "Qcur", il);
  7842. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  7843. cb(Kcur, "Kcur", il);
  7844. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  7845. cb(Vcur, "Vcur", il);
  7846. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  7847. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  7848. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  7849. Qcur = ggml_rope_ext(
  7850. ctx0, Qcur, inp_pos, nullptr,
  7851. n_embd_head, rope_type, n_ctx_orig, freq_base, freq_scale,
  7852. ext_factor, attn_factor, beta_fast, beta_slow
  7853. );
  7854. Kcur = ggml_rope_ext(
  7855. ctx0, Kcur, inp_pos, nullptr,
  7856. n_embd_head, rope_type, n_ctx_orig, freq_base, freq_scale,
  7857. ext_factor, attn_factor, beta_fast, beta_slow
  7858. );
  7859. cb(Qcur, "Qcur", il);
  7860. cb(Kcur, "Kcur", il);
  7861. cb(Vcur, "Vcur", il);
  7862. cur = build_attn(inp_attn,
  7863. model.layers[il].wo, NULL,
  7864. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  7865. }
  7866. if (il == n_layer - 1 && inp_out_ids) {
  7867. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  7868. sa_inp = ggml_get_rows(ctx0, sa_inp, inp_out_ids);
  7869. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  7870. }
  7871. ggml_tensor * sa_out = cur;
  7872. cur = sa_inp;
  7873. // feed-forward network
  7874. {
  7875. cur = build_ffn(cur,
  7876. model.layers[il].ffn_up, NULL, NULL,
  7877. model.layers[il].ffn_gate, NULL, NULL,
  7878. model.layers[il].ffn_down, NULL, NULL,
  7879. NULL,
  7880. LLM_FFN_SILU, LLM_FFN_PAR, il);
  7881. cb(cur, "ffn_out", il);
  7882. }
  7883. cur = ggml_add(ctx0, cur, sa_out);
  7884. cur = ggml_add(ctx0, cur, inpL);
  7885. cur = build_cvec(cur, il);
  7886. cb(cur, "l_out", il);
  7887. // input for next layer
  7888. inpL = cur;
  7889. }
  7890. cur = inpL;
  7891. cur = build_norm(cur,
  7892. model.output_norm, NULL,
  7893. LLM_NORM_RMS, -1);
  7894. cb(cur, "result_norm", -1);
  7895. res->t_embd = cur;
  7896. // lm_head
  7897. cur = build_lora_mm(model.output, cur);
  7898. cb(cur, "result_output", -1);
  7899. res->t_logits = cur;
  7900. ggml_build_forward_expand(gf, cur);
  7901. }
  7902. };
  7903. struct llm_build_gpt2 : public llm_graph_context {
  7904. llm_build_gpt2(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  7905. const int64_t n_embd_head = hparams.n_embd_head_v;
  7906. const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  7907. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  7908. ggml_tensor * cur;
  7909. ggml_tensor * pos;
  7910. ggml_tensor * inpL;
  7911. inpL = build_inp_embd(model.tok_embd);
  7912. // inp_pos - contains the positions
  7913. ggml_tensor * inp_pos = build_inp_pos();
  7914. auto * inp_attn = build_attn_inp_kv();
  7915. pos = ggml_get_rows(ctx0, model.pos_embd, inp_pos);
  7916. cb(pos, "pos_embd", -1);
  7917. inpL = ggml_add(ctx0, inpL, pos);
  7918. cb(inpL, "inpL", -1);
  7919. ggml_tensor * inp_out_ids = build_inp_out_ids();
  7920. for (int il = 0; il < n_layer; ++il) {
  7921. cur = build_norm(inpL,
  7922. model.layers[il].attn_norm,
  7923. model.layers[il].attn_norm_b,
  7924. LLM_NORM, il);
  7925. cb(cur, "attn_norm", il);
  7926. // self-attention
  7927. {
  7928. cur = build_lora_mm(model.layers[il].wqkv, cur);
  7929. cb(cur, "wqkv", il);
  7930. cur = ggml_add(ctx0, cur, model.layers[il].bqkv);
  7931. cb(cur, "bqkv", il);
  7932. 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));
  7933. 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));
  7934. ggml_tensor * Vcur = 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 + n_embd_gqa));
  7935. cb(Qcur, "Qcur", il);
  7936. cb(Kcur, "Kcur", il);
  7937. cb(Vcur, "Vcur", il);
  7938. cur = build_attn(inp_attn,
  7939. model.layers[il].wo, model.layers[il].bo,
  7940. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  7941. }
  7942. if (il == n_layer - 1 && inp_out_ids) {
  7943. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  7944. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  7945. }
  7946. // add the input
  7947. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
  7948. cb(ffn_inp, "ffn_inp", il);
  7949. // FF
  7950. {
  7951. cur = build_norm(ffn_inp,
  7952. model.layers[il].ffn_norm,
  7953. model.layers[il].ffn_norm_b,
  7954. LLM_NORM, il);
  7955. cb(cur, "ffn_norm", il);
  7956. cur = build_ffn(cur,
  7957. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  7958. NULL, NULL, NULL,
  7959. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  7960. NULL,
  7961. LLM_FFN_GELU, LLM_FFN_SEQ, il);
  7962. cb(cur, "ffn_out", il);
  7963. }
  7964. cur = ggml_add(ctx0, cur, ffn_inp);
  7965. cur = build_cvec(cur, il);
  7966. cb(cur, "l_out", il);
  7967. // input for next layer
  7968. inpL = cur;
  7969. }
  7970. cur = build_norm(inpL,
  7971. model.output_norm,
  7972. model.output_norm_b,
  7973. LLM_NORM, -1);
  7974. cb(cur, "result_norm", -1);
  7975. res->t_embd = cur;
  7976. cur = build_lora_mm(model.output, cur);
  7977. cb(cur, "result_output", -1);
  7978. res->t_logits = cur;
  7979. ggml_build_forward_expand(gf, cur);
  7980. }
  7981. };
  7982. struct llm_build_codeshell : public llm_graph_context {
  7983. llm_build_codeshell(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  7984. const int64_t n_embd_head = hparams.n_embd_head_v;
  7985. const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  7986. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  7987. GGML_ASSERT(n_embd_head == hparams.n_rot);
  7988. ggml_tensor * cur;
  7989. ggml_tensor * inpL;
  7990. inpL = build_inp_embd(model.tok_embd);
  7991. // inp_pos - contains the positions
  7992. ggml_tensor * inp_pos = build_inp_pos();
  7993. auto * inp_attn = build_attn_inp_kv();
  7994. ggml_tensor * inp_out_ids = build_inp_out_ids();
  7995. for (int il = 0; il < n_layer; ++il) {
  7996. cur = build_norm(inpL,
  7997. model.layers[il].attn_norm,
  7998. model.layers[il].attn_norm_b,
  7999. LLM_NORM, il);
  8000. cb(cur, "attn_norm", il);
  8001. // self-attention
  8002. {
  8003. cur = build_lora_mm(model.layers[il].wqkv, cur);
  8004. cb(cur, "wqkv", il);
  8005. cur = ggml_add(ctx0, cur, model.layers[il].bqkv);
  8006. cb(cur, "bqkv", il);
  8007. 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));
  8008. 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));
  8009. ggml_tensor * Vcur = 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 + n_embd_gqa));
  8010. Qcur = ggml_rope_ext(
  8011. ctx0, Qcur, inp_pos, nullptr,
  8012. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  8013. ext_factor, attn_factor, beta_fast, beta_slow
  8014. );
  8015. Kcur = ggml_rope_ext(
  8016. ctx0, Kcur, inp_pos, nullptr,
  8017. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  8018. ext_factor, attn_factor, beta_fast, beta_slow
  8019. );
  8020. cb(Qcur, "Qcur", il);
  8021. cb(Kcur, "Kcur", il);
  8022. cb(Vcur, "Vcur", il);
  8023. cur = build_attn(inp_attn,
  8024. model.layers[il].wo, model.layers[il].bo,
  8025. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  8026. }
  8027. if (il == n_layer - 1 && inp_out_ids) {
  8028. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  8029. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  8030. }
  8031. // add the input
  8032. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
  8033. cb(ffn_inp, "ffn_inp", il);
  8034. // FF
  8035. {
  8036. cur = build_norm(ffn_inp,
  8037. model.layers[il].ffn_norm,
  8038. model.layers[il].ffn_norm_b,
  8039. LLM_NORM, il);
  8040. cb(cur, "ffn_norm", il);
  8041. cur = build_ffn(cur,
  8042. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  8043. NULL, NULL, NULL,
  8044. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  8045. NULL,
  8046. LLM_FFN_GELU, LLM_FFN_SEQ, il);
  8047. cb(cur, "ffn_out", il);
  8048. }
  8049. cur = ggml_add(ctx0, cur, ffn_inp);
  8050. cur = build_cvec(cur, il);
  8051. cb(cur, "l_out", il);
  8052. // input for next layer
  8053. inpL = cur;
  8054. }
  8055. cur = build_norm(inpL,
  8056. model.output_norm,
  8057. model.output_norm_b,
  8058. LLM_NORM, -1);
  8059. cb(cur, "result_norm", -1);
  8060. res->t_embd = cur;
  8061. cur = build_lora_mm(model.output, cur);
  8062. cb(cur, "result_output", -1);
  8063. res->t_logits = cur;
  8064. ggml_build_forward_expand(gf, cur);
  8065. }
  8066. };
  8067. struct llm_build_orion : public llm_graph_context {
  8068. llm_build_orion(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  8069. const int64_t n_embd_head = hparams.n_embd_head_v;
  8070. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  8071. GGML_ASSERT(n_embd_head == hparams.n_rot);
  8072. ggml_tensor * cur;
  8073. ggml_tensor * inpL;
  8074. inpL = build_inp_embd(model.tok_embd);
  8075. // inp_pos - contains the positions
  8076. ggml_tensor * inp_pos = build_inp_pos();
  8077. auto * inp_attn = build_attn_inp_kv();
  8078. ggml_tensor * inp_out_ids = build_inp_out_ids();
  8079. for (int il = 0; il < n_layer; ++il) {
  8080. ggml_tensor * inpSA = inpL;
  8081. // norm
  8082. cur = build_norm(inpL,
  8083. model.layers[il].attn_norm, model.layers[il].attn_norm_b,
  8084. LLM_NORM, il);
  8085. cb(cur, "attn_norm", il);
  8086. // self-attention
  8087. {
  8088. // compute Q and K and RoPE them
  8089. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  8090. cb(Qcur, "Qcur", il);
  8091. // if (model.layers[il].bq) {
  8092. // Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  8093. // cb(Qcur, "Qcur", il);
  8094. // }
  8095. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  8096. cb(Kcur, "Kcur", il);
  8097. // if (model.layers[il].bk) {
  8098. // Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  8099. // cb(Kcur, "Kcur", il);
  8100. // }
  8101. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  8102. cb(Vcur, "Vcur", il);
  8103. // if (model.layers[il].bv) {
  8104. // Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  8105. // cb(Vcur, "Vcur", il);
  8106. // }
  8107. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  8108. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  8109. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  8110. Qcur = ggml_rope_ext(
  8111. ctx0, Qcur, inp_pos, nullptr,
  8112. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  8113. ext_factor, attn_factor, beta_fast, beta_slow
  8114. );
  8115. Kcur = ggml_rope_ext(
  8116. ctx0, Kcur, inp_pos, nullptr,
  8117. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  8118. ext_factor, attn_factor, beta_fast, beta_slow
  8119. );
  8120. cb(Qcur, "Qcur", il);
  8121. cb(Kcur, "Kcur", il);
  8122. cb(Vcur, "Vcur", il);
  8123. cur = build_attn(inp_attn,
  8124. model.layers[il].wo, NULL,
  8125. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  8126. }
  8127. if (il == n_layer - 1 && inp_out_ids) {
  8128. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  8129. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  8130. }
  8131. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  8132. cb(ffn_inp, "ffn_inp", il);
  8133. // feed-forward network
  8134. cur = build_norm(ffn_inp,
  8135. model.layers[il].ffn_norm, model.layers[il].ffn_norm_b,
  8136. LLM_NORM, il);
  8137. cb(cur, "ffn_norm", il);
  8138. cur = build_ffn(cur,
  8139. model.layers[il].ffn_up, NULL, NULL,
  8140. model.layers[il].ffn_gate, NULL, NULL,
  8141. model.layers[il].ffn_down, NULL, NULL,
  8142. NULL,
  8143. LLM_FFN_SILU, LLM_FFN_PAR, il);
  8144. cb(cur, "ffn_out", il);
  8145. cur = ggml_add(ctx0, cur, ffn_inp);
  8146. cur = build_cvec(cur, il);
  8147. cb(cur, "l_out", il);
  8148. // input for next layer
  8149. inpL = cur;
  8150. }
  8151. cur = inpL;
  8152. cur = build_norm(cur,
  8153. model.output_norm, model.output_norm_b,
  8154. LLM_NORM, -1);
  8155. cb(cur, "result_norm", -1);
  8156. res->t_embd = cur;
  8157. // lm_head
  8158. cur = build_lora_mm(model.output, cur);
  8159. cb(cur, "result_output", -1);
  8160. res->t_logits = cur;
  8161. ggml_build_forward_expand(gf, cur);
  8162. }
  8163. };
  8164. struct llm_build_internlm2 : public llm_graph_context {
  8165. llm_build_internlm2(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  8166. const int64_t n_embd_head = hparams.n_embd_head_v;
  8167. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  8168. GGML_ASSERT(n_embd_head == hparams.n_rot);
  8169. ggml_tensor * cur;
  8170. ggml_tensor * inpL;
  8171. inpL = build_inp_embd(model.tok_embd);
  8172. // inp_pos - contains the positions
  8173. ggml_tensor * inp_pos = build_inp_pos();
  8174. auto * inp_attn = build_attn_inp_kv();
  8175. ggml_tensor * inp_out_ids = build_inp_out_ids();
  8176. for (int il = 0; il < n_layer; ++il) {
  8177. ggml_tensor * inpSA = inpL;
  8178. // norm
  8179. cur = build_norm(inpL,
  8180. model.layers[il].attn_norm, NULL,
  8181. LLM_NORM_RMS, il);
  8182. cb(cur, "attn_norm", il);
  8183. // self-attention
  8184. {
  8185. // compute Q and K and RoPE them
  8186. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  8187. cb(Qcur, "Qcur", il);
  8188. if (model.layers[il].bq) {
  8189. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  8190. cb(Qcur, "Qcur", il);
  8191. }
  8192. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  8193. cb(Kcur, "Kcur", il);
  8194. if (model.layers[il].bk) {
  8195. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  8196. cb(Kcur, "Kcur", il);
  8197. }
  8198. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  8199. cb(Vcur, "Vcur", il);
  8200. if (model.layers[il].bv) {
  8201. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  8202. cb(Vcur, "Vcur", il);
  8203. }
  8204. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  8205. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  8206. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  8207. Qcur = ggml_rope_ext(
  8208. ctx0, Qcur, inp_pos, nullptr,
  8209. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  8210. ext_factor, attn_factor, beta_fast, beta_slow
  8211. );
  8212. Kcur = ggml_rope_ext(
  8213. ctx0, Kcur, inp_pos, nullptr,
  8214. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  8215. ext_factor, attn_factor, beta_fast, beta_slow
  8216. );
  8217. cb(Qcur, "Qcur", il);
  8218. cb(Kcur, "Kcur", il);
  8219. cb(Vcur, "Vcur", il);
  8220. cur = build_attn(inp_attn,
  8221. model.layers[il].wo, model.layers[il].bo,
  8222. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  8223. }
  8224. if (il == n_layer - 1 && inp_out_ids) {
  8225. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  8226. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  8227. }
  8228. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  8229. cb(ffn_inp, "ffn_inp", il);
  8230. // feed-forward network
  8231. cur = build_norm(ffn_inp,
  8232. model.layers[il].ffn_norm, NULL,
  8233. LLM_NORM_RMS, il);
  8234. cb(cur, "ffn_norm", il);
  8235. cur = build_ffn(cur,
  8236. model.layers[il].ffn_up, NULL, NULL,
  8237. model.layers[il].ffn_gate, NULL, NULL,
  8238. model.layers[il].ffn_down, NULL, NULL,
  8239. NULL,
  8240. LLM_FFN_SILU, LLM_FFN_PAR, il);
  8241. cb(cur, "ffn_out", il);
  8242. cur = ggml_add(ctx0, cur, ffn_inp);
  8243. cur = build_cvec(cur, il);
  8244. cb(cur, "l_out", il);
  8245. // input for next layer
  8246. inpL = cur;
  8247. }
  8248. cur = inpL;
  8249. cur = build_norm(cur,
  8250. model.output_norm, NULL,
  8251. LLM_NORM_RMS, -1);
  8252. cb(cur, "result_norm", -1);
  8253. res->t_embd = cur;
  8254. // lm_head
  8255. cur = build_lora_mm(model.output, cur);
  8256. cb(cur, "result_output", -1);
  8257. res->t_logits = cur;
  8258. ggml_build_forward_expand(gf, cur);
  8259. }
  8260. };
  8261. struct llm_build_minicpm3 : public llm_graph_context {
  8262. llm_build_minicpm3(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  8263. //TODO: if the model varies, these parameters need to be read from the model
  8264. const int64_t n_embd_base = 256;
  8265. const float scale_embd = 12.0f;
  8266. const float scale_depth = 1.4f;
  8267. const float kq_scale = 1.0f / sqrtf(float(hparams.n_embd_head_k));
  8268. const uint32_t n_embd_head_qk_rope = hparams.n_rot;
  8269. const uint32_t n_embd_head_qk_nope = hparams.n_embd_head_k - hparams.n_rot;
  8270. const uint32_t kv_lora_rank = hparams.n_lora_kv;
  8271. ggml_tensor * cur;
  8272. ggml_tensor * inpL;
  8273. inpL = build_inp_embd(model.tok_embd);
  8274. // scale the input embeddings
  8275. inpL = ggml_scale(ctx0, inpL, scale_embd);
  8276. cb(inpL, "inp_scaled", -1);
  8277. // inp_pos - contains the positions
  8278. ggml_tensor * inp_pos = build_inp_pos();
  8279. auto * inp_attn = build_attn_inp_kv();
  8280. ggml_tensor * inp_out_ids = build_inp_out_ids();
  8281. for (int il = 0; il < n_layer; ++il) {
  8282. ggml_tensor * inpSA = inpL;
  8283. ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
  8284. // norm
  8285. cur = build_norm(inpL,
  8286. model.layers[il].attn_norm, NULL,
  8287. LLM_NORM_RMS, il);
  8288. cb(cur, "attn_norm", il);
  8289. // self_attention
  8290. {
  8291. ggml_tensor * q = NULL;
  8292. // {n_embd, q_lora_rank} * {n_embd, n_tokens} -> {q_lora_rank, n_tokens}
  8293. q = ggml_mul_mat(ctx0, model.layers[il].wq_a, cur);
  8294. cb(q, "q", il);
  8295. q = build_norm(q,
  8296. model.layers[il].attn_q_a_norm, NULL,
  8297. LLM_NORM_RMS, il);
  8298. cb(q, "q", il);
  8299. // {q_lora_rank, n_head * hparams.n_embd_head_k} * {q_lora_rank, n_tokens} -> {n_head * hparams.n_embd_head_k, n_tokens}
  8300. q = ggml_mul_mat(ctx0, model.layers[il].wq_b, q);
  8301. cb(q, "q", il);
  8302. // split into {n_head * n_embd_head_qk_nope, n_tokens}
  8303. ggml_tensor * q_nope = ggml_view_3d(ctx0, q, n_embd_head_qk_nope, n_head, n_tokens,
  8304. ggml_row_size(q->type, hparams.n_embd_head_k),
  8305. ggml_row_size(q->type, hparams.n_embd_head_k * n_head),
  8306. 0);
  8307. cb(q_nope, "q_nope", il);
  8308. // and {n_head * n_embd_head_qk_rope, n_tokens}
  8309. ggml_tensor * q_pe = ggml_view_3d(ctx0, q, n_embd_head_qk_rope, n_head, n_tokens,
  8310. ggml_row_size(q->type, hparams.n_embd_head_k),
  8311. ggml_row_size(q->type, hparams.n_embd_head_k * n_head),
  8312. ggml_row_size(q->type, n_embd_head_qk_nope));
  8313. cb(q_pe, "q_pe", il);
  8314. // {n_embd, kv_lora_rank + n_embd_head_qk_rope} * {n_embd, n_tokens} -> {kv_lora_rank + n_embd_head_qk_rope, n_tokens}
  8315. ggml_tensor * kv_pe_compresseed = ggml_mul_mat(ctx0, model.layers[il].wkv_a_mqa, cur);
  8316. cb(kv_pe_compresseed, "kv_pe_compresseed", il);
  8317. // split into {kv_lora_rank, n_tokens}
  8318. ggml_tensor * kv_compressed = ggml_view_2d(ctx0, kv_pe_compresseed, kv_lora_rank, n_tokens,
  8319. kv_pe_compresseed->nb[1],
  8320. 0);
  8321. cb(kv_compressed, "kv_compressed", il);
  8322. // and {n_embd_head_qk_rope, n_tokens}
  8323. ggml_tensor * k_pe = ggml_view_3d(ctx0, kv_pe_compresseed, n_embd_head_qk_rope, 1, n_tokens,
  8324. kv_pe_compresseed->nb[1],
  8325. kv_pe_compresseed->nb[1],
  8326. ggml_row_size(kv_pe_compresseed->type, kv_lora_rank));
  8327. cb(k_pe, "k_pe", il);
  8328. kv_compressed = build_norm(kv_compressed,
  8329. model.layers[il].attn_kv_a_norm, NULL,
  8330. LLM_NORM_RMS, il);
  8331. cb(kv_compressed, "kv_compressed", il);
  8332. // {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}
  8333. ggml_tensor * kv = ggml_mul_mat(ctx0, model.layers[il].wkv_b, kv_compressed);
  8334. cb(kv, "kv", il);
  8335. // split into {n_head * n_embd_head_qk_nope, n_tokens}
  8336. ggml_tensor * k_nope = ggml_view_3d(ctx0, kv, n_embd_head_qk_nope, n_head, n_tokens,
  8337. ggml_row_size(kv->type, n_embd_head_qk_nope + hparams.n_embd_head_v),
  8338. ggml_row_size(kv->type, n_head * (n_embd_head_qk_nope + hparams.n_embd_head_v)),
  8339. 0);
  8340. cb(k_nope, "k_nope", il);
  8341. // and {n_head * n_embd_head_v, n_tokens}
  8342. ggml_tensor * v_states = ggml_view_3d(ctx0, kv, hparams.n_embd_head_v, n_head, n_tokens,
  8343. ggml_row_size(kv->type, (n_embd_head_qk_nope + hparams.n_embd_head_v)),
  8344. ggml_row_size(kv->type, (n_embd_head_qk_nope + hparams.n_embd_head_v)*n_head),
  8345. ggml_row_size(kv->type, (n_embd_head_qk_nope)));
  8346. cb(v_states, "v_states", il);
  8347. v_states = ggml_cont(ctx0, v_states);
  8348. cb(v_states, "v_states", il);
  8349. q_pe = ggml_rope_ext(
  8350. ctx0, q_pe, inp_pos, rope_factors,
  8351. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  8352. ext_factor, attn_factor, beta_fast, beta_slow
  8353. );
  8354. cb(q_pe, "q_pe", il);
  8355. // shared RoPE key
  8356. k_pe = ggml_rope_ext(
  8357. ctx0, k_pe, inp_pos, rope_factors,
  8358. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  8359. ext_factor, attn_factor, beta_fast, beta_slow
  8360. );
  8361. cb(k_pe, "k_pe", il);
  8362. ggml_tensor * q_states = ggml_concat(ctx0, q_nope, q_pe, 0);
  8363. cb(q_states, "q_states", il);
  8364. ggml_tensor * k_states = ggml_concat(ctx0, k_nope, ggml_repeat(ctx0, k_pe, q_pe), 0);
  8365. cb(k_states, "k_states", il);
  8366. cur = build_attn(inp_attn,
  8367. model.layers[il].wo, NULL,
  8368. q_states, k_states, v_states, nullptr, nullptr, nullptr, kq_scale, il);
  8369. }
  8370. if (il == n_layer - 1 && inp_out_ids) {
  8371. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  8372. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  8373. }
  8374. // scale_res - scale the hidden states for residual connection
  8375. const float scale_res = scale_depth/sqrtf(float(n_layer)); // TODO: is this correct?
  8376. cur = ggml_scale(ctx0, cur, scale_res);
  8377. cb(cur, "hidden_scaled", il);
  8378. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  8379. cb(ffn_inp, "ffn_inp", il);
  8380. // feed-forward network
  8381. {
  8382. cur = build_norm(ffn_inp,
  8383. model.layers[il].ffn_norm, NULL,
  8384. LLM_NORM_RMS, il);
  8385. cb(cur, "ffn_norm", il);
  8386. cur = build_ffn(cur,
  8387. model.layers[il].ffn_up, NULL, NULL,
  8388. model.layers[il].ffn_gate, NULL, NULL,
  8389. model.layers[il].ffn_down, NULL, NULL,
  8390. NULL,
  8391. LLM_FFN_SILU, LLM_FFN_PAR, il);
  8392. cb(cur, "ffn_out", il);
  8393. }
  8394. // scale the hidden states for residual connection
  8395. cur = ggml_scale(ctx0, cur, scale_res);
  8396. cb(cur, "hidden_scaled_ffn", il);
  8397. cur = ggml_add(ctx0, cur, ffn_inp);
  8398. cur = build_cvec(cur, il);
  8399. cb(cur, "l_out", il);
  8400. // input for next layer
  8401. inpL = cur;
  8402. }
  8403. cur = inpL;
  8404. cur = build_norm(cur,
  8405. model.output_norm, NULL,
  8406. LLM_NORM_RMS, -1);
  8407. cb(cur, "result_norm", -1);
  8408. res->t_embd = cur;
  8409. // lm_head scaling
  8410. const float scale_lmhead = float(n_embd_base)/float(n_embd);
  8411. cur = ggml_scale(ctx0, cur, scale_lmhead);
  8412. cb(cur, "lmhead_scaling", -1);
  8413. // lm_head
  8414. cur = build_lora_mm(model.output, cur);
  8415. cb(cur, "result_output", -1);
  8416. res->t_logits = cur;
  8417. ggml_build_forward_expand(gf, cur);
  8418. }
  8419. };
  8420. struct llm_build_gemma : public llm_graph_context {
  8421. llm_build_gemma(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  8422. const int64_t n_embd_head = hparams.n_embd_head_v;
  8423. ggml_tensor * cur;
  8424. ggml_tensor * inpL;
  8425. inpL = build_inp_embd(model.tok_embd);
  8426. inpL = ggml_scale(ctx0, inpL, sqrtf(n_embd));
  8427. cb(inpL, "inp_scaled", -1);
  8428. // inp_pos - contains the positions
  8429. ggml_tensor * inp_pos = build_inp_pos();
  8430. auto * inp_attn = build_attn_inp_kv();
  8431. ggml_tensor * inp_out_ids = build_inp_out_ids();
  8432. for (int il = 0; il < n_layer; ++il) {
  8433. // norm
  8434. cur = build_norm(inpL,
  8435. model.layers[il].attn_norm, NULL,
  8436. LLM_NORM_RMS, il);
  8437. cb(cur, "attn_norm", il);
  8438. // self-attention
  8439. {
  8440. // compute Q and K and RoPE them
  8441. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  8442. cb(Qcur, "Qcur", il);
  8443. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  8444. cb(Kcur, "Kcur", il);
  8445. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  8446. cb(Vcur, "Vcur", il);
  8447. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  8448. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  8449. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  8450. Qcur = ggml_rope_ext(
  8451. ctx0, Qcur, inp_pos, nullptr,
  8452. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  8453. ext_factor, attn_factor, beta_fast, beta_slow);
  8454. Kcur = ggml_rope_ext(
  8455. ctx0, Kcur, inp_pos, nullptr,
  8456. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  8457. ext_factor, attn_factor, beta_fast, beta_slow);
  8458. cb(Qcur, "Qcur", il);
  8459. cb(Kcur, "Kcur", il);
  8460. cb(Vcur, "Vcur", il);
  8461. Qcur = ggml_scale(ctx0, Qcur, 1.0f / sqrtf(float(n_embd_head)));
  8462. cb(Qcur, "Qcur_scaled", il);
  8463. cur = build_attn(inp_attn,
  8464. model.layers[il].wo, NULL,
  8465. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f, il);
  8466. }
  8467. if (il == n_layer - 1 && inp_out_ids) {
  8468. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  8469. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  8470. }
  8471. ggml_tensor * sa_out = ggml_add(ctx0, cur, inpL);
  8472. cb(sa_out, "sa_out", il);
  8473. cur = build_norm(sa_out,
  8474. model.layers[il].ffn_norm, NULL,
  8475. LLM_NORM_RMS, il);
  8476. cb(cur, "ffn_norm", il);
  8477. // feed-forward network
  8478. {
  8479. cur = build_ffn(cur,
  8480. model.layers[il].ffn_up, NULL, NULL,
  8481. model.layers[il].ffn_gate, NULL, NULL,
  8482. model.layers[il].ffn_down, NULL, NULL,
  8483. NULL,
  8484. LLM_FFN_GELU, LLM_FFN_PAR, il);
  8485. cb(cur, "ffn_out", il);
  8486. }
  8487. cur = ggml_add(ctx0, cur, sa_out);
  8488. cur = build_cvec(cur, il);
  8489. cb(cur, "l_out", il);
  8490. // input for next layer
  8491. inpL = cur;
  8492. }
  8493. cur = inpL;
  8494. cur = build_norm(cur,
  8495. model.output_norm, NULL,
  8496. LLM_NORM_RMS, -1);
  8497. cb(cur, "result_norm", -1);
  8498. res->t_embd = cur;
  8499. // lm_head
  8500. cur = build_lora_mm(model.output, cur);
  8501. cb(cur, "result_output", -1);
  8502. res->t_logits = cur;
  8503. ggml_build_forward_expand(gf, cur);
  8504. }
  8505. };
  8506. struct llm_build_gemma2_iswa : public llm_graph_context {
  8507. llm_build_gemma2_iswa(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  8508. const int64_t n_embd_head = hparams.n_embd_head_k;
  8509. ggml_tensor * cur;
  8510. ggml_tensor * inpL;
  8511. inpL = build_inp_embd(model.tok_embd);
  8512. inpL = ggml_scale(ctx0, inpL, sqrtf(n_embd));
  8513. cb(inpL, "inp_scaled", -1);
  8514. // inp_pos - contains the positions
  8515. ggml_tensor * inp_pos = build_inp_pos();
  8516. auto * inp_attn = build_attn_inp_kv_iswa();
  8517. ggml_tensor * inp_out_ids = build_inp_out_ids();
  8518. for (int il = 0; il < n_layer; ++il) {
  8519. // norm
  8520. cur = build_norm(inpL,
  8521. model.layers[il].attn_norm, NULL,
  8522. LLM_NORM_RMS, il);
  8523. cb(cur, "attn_norm", il);
  8524. // self-attention
  8525. {
  8526. // compute Q and K and RoPE them
  8527. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  8528. cb(Qcur, "Qcur", il);
  8529. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  8530. cb(Kcur, "Kcur", il);
  8531. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  8532. cb(Vcur, "Vcur", il);
  8533. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  8534. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  8535. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  8536. Qcur = ggml_rope_ext(
  8537. ctx0, Qcur, inp_pos, nullptr,
  8538. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  8539. ext_factor, attn_factor, beta_fast, beta_slow);
  8540. Kcur = ggml_rope_ext(
  8541. ctx0, Kcur, inp_pos, nullptr,
  8542. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  8543. ext_factor, attn_factor, beta_fast, beta_slow);
  8544. cb(Qcur, "Qcur", il);
  8545. cb(Kcur, "Kcur", il);
  8546. cb(Vcur, "Vcur", il);
  8547. Qcur = ggml_scale(ctx0, Qcur, hparams.f_attention_scale);
  8548. cur = build_attn(inp_attn,
  8549. model.layers[il].wo, NULL,
  8550. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f, il);
  8551. }
  8552. if (il == n_layer - 1 && inp_out_ids) {
  8553. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  8554. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  8555. }
  8556. cur = build_norm(cur,
  8557. model.layers[il].attn_post_norm, NULL,
  8558. LLM_NORM_RMS, il);
  8559. cb(cur, "attn_post_norm", il);
  8560. ggml_tensor * sa_out = ggml_add(ctx0, cur, inpL);
  8561. cb(sa_out, "sa_out", il);
  8562. cur = build_norm(sa_out,
  8563. model.layers[il].ffn_norm, NULL,
  8564. LLM_NORM_RMS, il);
  8565. cb(cur, "ffn_norm", il);
  8566. // feed-forward network
  8567. {
  8568. cur = build_ffn(cur,
  8569. model.layers[il].ffn_up, NULL, NULL,
  8570. model.layers[il].ffn_gate, NULL, NULL,
  8571. model.layers[il].ffn_down, NULL, NULL,
  8572. NULL,
  8573. LLM_FFN_GELU, LLM_FFN_PAR, il);
  8574. cb(cur, "ffn_out", il);
  8575. }
  8576. cur = build_norm(cur,
  8577. model.layers[il].ffn_post_norm, NULL,
  8578. LLM_NORM_RMS, -1);
  8579. cb(cur, "ffn_post_norm", -1);
  8580. cur = ggml_add(ctx0, cur, sa_out);
  8581. cur = build_cvec(cur, il);
  8582. cb(cur, "l_out", il);
  8583. // input for next layer
  8584. inpL = cur;
  8585. }
  8586. cur = inpL;
  8587. cur = build_norm(cur,
  8588. model.output_norm, NULL,
  8589. LLM_NORM_RMS, -1);
  8590. cb(cur, "result_norm", -1);
  8591. res->t_embd = cur;
  8592. // lm_head
  8593. cur = build_lora_mm(model.output, cur);
  8594. // final logit soft-capping
  8595. cur = ggml_scale(ctx0, cur, 1.0f / hparams.f_final_logit_softcapping);
  8596. cur = ggml_tanh(ctx0, cur);
  8597. cur = ggml_scale(ctx0, cur, hparams.f_final_logit_softcapping);
  8598. cb(cur, "result_output", -1);
  8599. res->t_logits = cur;
  8600. ggml_build_forward_expand(gf, cur);
  8601. }
  8602. };
  8603. struct llm_build_gemma3_iswa : public llm_graph_context {
  8604. llm_build_gemma3_iswa(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  8605. const int64_t n_embd_head = hparams.n_embd_head_k;
  8606. ggml_tensor * cur;
  8607. ggml_tensor * inpL;
  8608. inpL = build_inp_embd(model.tok_embd);
  8609. // important: do not normalize weights for raw embeddings input (i.e. encoded image emdeddings)
  8610. if (ubatch.token) {
  8611. inpL = ggml_scale(ctx0, inpL, sqrtf(n_embd));
  8612. cb(inpL, "inp_scaled", -1);
  8613. }
  8614. // inp_pos - contains the positions
  8615. ggml_tensor * inp_pos = build_inp_pos();
  8616. // TODO: is causal == true correct? might need some changes
  8617. auto * inp_attn = build_attn_inp_kv_iswa();
  8618. ggml_tensor * inp_out_ids = build_inp_out_ids();
  8619. for (int il = 0; il < n_layer; ++il) {
  8620. const float freq_base_l = model.get_rope_freq_base (cparams, il);
  8621. const float freq_scale_l = model.get_rope_freq_scale(cparams, il);
  8622. // norm
  8623. cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
  8624. cb(cur, "attn_norm", il);
  8625. // self-attention
  8626. {
  8627. // compute Q and K and RoPE them
  8628. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  8629. cb(Qcur, "Qcur", il);
  8630. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  8631. cb(Kcur, "Kcur", il);
  8632. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  8633. cb(Vcur, "Vcur", il);
  8634. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  8635. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  8636. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  8637. Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);
  8638. cb(Qcur, "Qcur_normed", il);
  8639. Qcur = ggml_rope_ext(
  8640. ctx0, Qcur, inp_pos, nullptr,
  8641. n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l,
  8642. ext_factor, attn_factor, beta_fast, beta_slow);
  8643. Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);
  8644. cb(Kcur, "Kcur_normed", il);
  8645. Kcur = ggml_rope_ext(
  8646. ctx0, Kcur, inp_pos, nullptr,
  8647. n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l,
  8648. ext_factor, attn_factor, beta_fast, beta_slow);
  8649. cb(Qcur, "Qcur", il);
  8650. cb(Kcur, "Kcur", il);
  8651. cb(Vcur, "Vcur", il);
  8652. // ref: https://github.com/google/gemma_pytorch/blob/014acb7ac4563a5f77c76d7ff98f31b568c16508/gemma/model.py#L315
  8653. Qcur = ggml_scale(ctx0, Qcur, hparams.f_attention_scale);
  8654. cur = build_attn(inp_attn,
  8655. model.layers[il].wo, NULL,
  8656. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f, il);
  8657. }
  8658. if (il == n_layer - 1 && inp_out_ids) {
  8659. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  8660. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  8661. }
  8662. cur = build_norm(cur,
  8663. model.layers[il].attn_post_norm, NULL,
  8664. LLM_NORM_RMS, il);
  8665. cb(cur, "attn_post_norm", il);
  8666. ggml_tensor * sa_out = ggml_add(ctx0, cur, inpL);
  8667. cb(sa_out, "sa_out", il);
  8668. cur = build_norm(sa_out,
  8669. model.layers[il].ffn_norm, NULL,
  8670. LLM_NORM_RMS, il);
  8671. cb(cur, "ffn_norm", il);
  8672. // feed-forward network
  8673. {
  8674. cur = build_ffn(cur,
  8675. model.layers[il].ffn_up, NULL, NULL,
  8676. model.layers[il].ffn_gate, NULL, NULL,
  8677. model.layers[il].ffn_down, NULL, NULL,
  8678. NULL,
  8679. LLM_FFN_GELU, LLM_FFN_PAR, il);
  8680. cb(cur, "ffn_out", il);
  8681. }
  8682. cur = build_norm(cur,
  8683. model.layers[il].ffn_post_norm, NULL,
  8684. LLM_NORM_RMS, -1);
  8685. cb(cur, "ffn_post_norm", -1);
  8686. cur = ggml_add(ctx0, cur, sa_out);
  8687. cur = build_cvec(cur, il);
  8688. cb(cur, "l_out", il);
  8689. // input for next layer
  8690. inpL = cur;
  8691. }
  8692. cur = inpL;
  8693. cur = build_norm(cur,
  8694. model.output_norm, NULL,
  8695. LLM_NORM_RMS, -1);
  8696. cb(cur, "result_norm", -1);
  8697. res->t_embd = cur;
  8698. // lm_head
  8699. cur = build_lora_mm(model.output, cur);
  8700. cb(cur, "result_output", -1);
  8701. res->t_logits = cur;
  8702. ggml_build_forward_expand(gf, cur);
  8703. }
  8704. };
  8705. struct llm_build_gemma3n_iswa : public llm_graph_context {
  8706. const llama_model & model;
  8707. const int64_t n_embd_head;
  8708. const int64_t n_embd_altup;
  8709. const int64_t n_altup;
  8710. const int i_altup_act;
  8711. const int n_layer_sparsity = 10; // number of layers using activation sparsity
  8712. const float f_sparsity_std_mul = 1.6448533535003662f; // std_multiplier = normal_dist.icdf(0.95)
  8713. llm_build_gemma3n_iswa(const llama_model & model, const llm_graph_params & params)
  8714. : llm_graph_context(params),
  8715. model(model),
  8716. n_embd_head(model.hparams.n_embd_head_k),
  8717. n_embd_altup(model.hparams.n_embd_altup),
  8718. n_altup(model.hparams.n_altup),
  8719. i_altup_act(model.hparams.i_altup_act) {
  8720. ggml_tensor * cur;
  8721. ggml_tensor * inpL;
  8722. inpL = build_inp_embd(model.tok_embd);
  8723. // important: do not normalize weights for raw embeddings input (i.e. encoded image emdeddings)
  8724. if (ubatch.token) {
  8725. inpL = ggml_scale(ctx0, inpL, sqrtf(n_embd));
  8726. cb(inpL, "inp_scaled", -1);
  8727. }
  8728. // inp_pos - contains the positions
  8729. ggml_tensor * inp_pos = build_inp_pos();
  8730. // TODO: is causal == true correct? might need some changes
  8731. auto * inp_attn = build_attn_inp_kv_iswa();
  8732. // inp_per_layer shape: [n_embd_altup, n_tokens, n_layer]
  8733. ggml_tensor * inp_per_layer = project_per_layer_inputs(inpL, get_per_layer_inputs());
  8734. // inpL now has only 1 altup, project it to the rest of the altups
  8735. // these "added" altups will be concat to the last dim of inpL
  8736. {
  8737. ggml_tensor * target_magnitude = calc_magnitude(inpL);
  8738. ggml_tensor * inp_repeated = ggml_repeat_4d(ctx0, inpL, n_embd, n_tokens, n_altup - 1, 1);
  8739. ggml_tensor * altup_added = ggml_mul_mat(ctx0, model.altup_proj, inp_repeated); // shape: [n_embd, n_tokens, n_altup - 1]
  8740. ggml_tensor * new_magnitude = calc_magnitude(altup_added);
  8741. altup_added = ggml_div(ctx0,
  8742. ggml_mul(ctx0, altup_added, target_magnitude),
  8743. new_magnitude);
  8744. inpL = ggml_concat(ctx0, inpL, altup_added, 2); // shape: [n_embd, n_tokens, n_altup]
  8745. cb(inpL, "inp_stacked", -1);
  8746. }
  8747. // inpL now has shape: [n_embd, n_tokens, n_altup]
  8748. // inp_per_layer now has shape: [n_embd_altup, n_tokens, n_layer]
  8749. for (int il = 0; il < n_layer; ++il) {
  8750. // this block is made to be closely resemble Gemma3p5DecoderLayer on python code
  8751. const float freq_base_l = model.get_rope_freq_base (cparams, il);
  8752. const float freq_scale_l = model.get_rope_freq_scale(cparams, il);
  8753. ggml_tensor * cur = inpL; // [n_embd, n_tokens, n_altup]
  8754. ggml_tensor * predictions = altup_predict(cur, il); // [n_embd, n_tokens, n_altup]
  8755. // predicted value will go through self-attention and laurel
  8756. ggml_tensor * active_prediction = view_2d_slice(predictions, i_altup_act); // [n_embd, n_tokens]
  8757. cur = active_prediction;
  8758. cb(cur, "active_prediction", il);
  8759. // norm
  8760. cur = build_norm(cur, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
  8761. cb(cur, "attn_norm", il);
  8762. // laurel
  8763. ggml_tensor * laurel_out = laurel(cur, il); // [n_embd, n_tokens]
  8764. // self-attention
  8765. if (hparams.has_kv(il)) {
  8766. // compute Q and K and RoPE them
  8767. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  8768. cb(Qcur, "Qcur", il);
  8769. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  8770. cb(Kcur, "Kcur", il);
  8771. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  8772. cb(Vcur, "Vcur", il);
  8773. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  8774. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  8775. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  8776. Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);
  8777. Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);
  8778. Vcur = ggml_rms_norm(ctx0, Vcur, hparams.f_norm_rms_eps);
  8779. cb(Qcur, "Qcur_normed", il);
  8780. cb(Kcur, "Kcur_normed", il);
  8781. cb(Vcur, "Vcur_normed", il);
  8782. Qcur = ggml_rope_ext(
  8783. ctx0, Qcur, inp_pos, nullptr,
  8784. n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l,
  8785. ext_factor, attn_factor, beta_fast, beta_slow);
  8786. Kcur = ggml_rope_ext(
  8787. ctx0, Kcur, inp_pos, nullptr,
  8788. n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l,
  8789. ext_factor, attn_factor, beta_fast, beta_slow);
  8790. cb(Qcur, "Qcur_pos", il);
  8791. cb(Kcur, "Kcur_pos", il);
  8792. cur = build_attn(inp_attn,
  8793. model.layers[il].wo, NULL,
  8794. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, hparams.f_attention_scale, il);
  8795. } else {
  8796. // reuse KV cache of earlier layers
  8797. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  8798. cb(Qcur, "Qcur", il);
  8799. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  8800. Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);
  8801. cb(Qcur, "Qcur_normed", il);
  8802. Qcur = ggml_rope_ext(
  8803. ctx0, Qcur, inp_pos, nullptr,
  8804. n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l,
  8805. ext_factor, attn_factor, beta_fast, beta_slow);
  8806. cb(Qcur, "Qcur_pos", il);
  8807. cur = build_attn(inp_attn,
  8808. model.layers[il].wo, NULL,
  8809. Qcur, nullptr, nullptr, nullptr, nullptr, nullptr, hparams.f_attention_scale, il);
  8810. }
  8811. cur = build_norm(cur,
  8812. model.layers[il].attn_post_norm, NULL,
  8813. LLM_NORM_RMS, il);
  8814. cb(cur, "attn_post_norm", il);
  8815. cur = ggml_add(ctx0, cur, active_prediction); // [n_embd, n_tokens]
  8816. cb(cur, "attn_gated", il);
  8817. ggml_tensor * attn_laurel = ggml_scale(ctx0,
  8818. ggml_add(ctx0, cur, laurel_out),
  8819. 1.0f / sqrtf(2.0f)); // [n_embd, n_tokens]
  8820. cb(attn_laurel, "attn_laurel", il);
  8821. cur = build_norm(attn_laurel,
  8822. model.layers[il].ffn_norm, NULL,
  8823. LLM_NORM_RMS, il);
  8824. cb(cur, "ffn_norm", il);
  8825. // feed-forward network
  8826. {
  8827. ggml_tensor * up_proj = build_lora_mm(model.layers[il].ffn_up, cur);
  8828. ggml_tensor * gate_proj = build_lora_mm(model.layers[il].ffn_gate, cur);
  8829. if (il < n_layer_sparsity) {
  8830. // apply activation sparsity
  8831. gate_proj = gaussian_topk(gate_proj);
  8832. }
  8833. gate_proj = ggml_gelu(ctx0, gate_proj);
  8834. cur = ggml_mul(ctx0, up_proj, gate_proj);
  8835. cur = build_lora_mm(model.layers[il].ffn_down, cur);
  8836. cb(cur, "ffn_out", il);
  8837. }
  8838. cur = build_norm(cur,
  8839. model.layers[il].ffn_post_norm, NULL,
  8840. LLM_NORM_RMS, -1);
  8841. cb(cur, "ffn_post_norm", il);
  8842. ggml_tensor * attn_ffw_laurel_gated = ggml_add(ctx0, cur, attn_laurel); // [n_embd, n_tokens]
  8843. cb(attn_ffw_laurel_gated, "attn_ffw_laurel_gated", il);
  8844. ggml_tensor * corrected = altup_correct(predictions, attn_ffw_laurel_gated, il); // [n_embd, n_tokens, n_altup]
  8845. ggml_tensor * first_prediction; // [n_embd, n_tokens]
  8846. {
  8847. first_prediction = view_2d_slice(corrected, i_altup_act); // [n_embd, n_tokens]
  8848. first_prediction = ggml_mul(ctx0, first_prediction, model.layers[il].altup_correct_scale);
  8849. first_prediction = build_lora_mm(model.layers[il].per_layer_inp_gate, first_prediction);
  8850. first_prediction = ggml_gelu(ctx0, first_prediction); // [n_embd_altup, n_tokens]
  8851. cb(first_prediction, "first_prediction_gated", il);
  8852. ggml_tensor * inp_this_layer = view_2d_slice(inp_per_layer, il); // [n_embd_altup, n_tokens]
  8853. first_prediction = ggml_mul(ctx0, first_prediction, inp_this_layer); // [n_embd_altup, n_tokens]
  8854. cb(first_prediction, "first_prediction_scaled", il);
  8855. first_prediction = build_lora_mm(model.layers[il].per_layer_proj, first_prediction); // [n_embd, n_tokens]
  8856. first_prediction = build_norm(first_prediction,
  8857. model.layers[il].per_layer_post_norm, NULL,
  8858. LLM_NORM_RMS, il);
  8859. cb(first_prediction, "first_prediction_out", il);
  8860. }
  8861. // equivalent to python code: corrected_predictions[1:] += first_prediction
  8862. {
  8863. ggml_tensor * slice_first = view_2d_slice(corrected, 0);
  8864. ggml_tensor * slice_rest = ggml_view_3d(ctx0, corrected, n_embd, n_tokens, n_altup - 1,
  8865. ggml_row_size(corrected->type, n_embd),
  8866. ggml_row_size(corrected->type, n_embd*n_tokens),
  8867. n_embd*n_tokens*ggml_element_size(corrected));
  8868. ggml_tensor * tmp = ggml_add(ctx0, slice_rest, first_prediction); // [n_embd, n_tokens, n_altup - 1]
  8869. corrected = ggml_concat(ctx0, slice_first, tmp, 2); // [n_embd, n_tokens, n_altup]
  8870. }
  8871. cur = corrected; // [n_embd, n_tokens, n_altup]
  8872. cur = build_cvec(cur, il);
  8873. cb(cur, "l_out", il);
  8874. // input for next layer
  8875. inpL = cur;
  8876. }
  8877. cur = inpL; // [n_embd, n_tokens, n_altup]
  8878. // cur now has multiple altup(s), we want to merge them back to 1 altup
  8879. {
  8880. ggml_tensor * target_magnitude = calc_magnitude(view_2d_slice(cur, i_altup_act)); // [n_embd, n_tokens]
  8881. // do a view to skip the first slice (active altup)
  8882. ggml_tensor * alt_slice = ggml_view_3d(ctx0, cur, n_embd, n_tokens, n_altup - 1,
  8883. ggml_row_size(cur->type, n_embd),
  8884. ggml_row_size(cur->type, n_embd*n_tokens),
  8885. n_embd*n_tokens*ggml_element_size(cur));
  8886. ggml_tensor * altup_unembd = ggml_mul_mat(ctx0, model.altup_unembd_proj, alt_slice); // shape: [n_embd, n_tokens, n_altup - 1]
  8887. ggml_tensor * new_magnitude = calc_magnitude(altup_unembd);
  8888. altup_unembd = ggml_div(ctx0,
  8889. ggml_mul(ctx0, altup_unembd, target_magnitude),
  8890. new_magnitude);
  8891. cb(altup_unembd, "altup_unembd", -1);
  8892. // equivalent to torch.mean(hidden_states, dim=0)
  8893. cur = view_2d_slice(cur, 0); // [n_embd, n_tokens]
  8894. for (int i = 0; i < n_altup - 1; ++i) {
  8895. cur = ggml_add(ctx0, cur, view_2d_slice(altup_unembd, i));
  8896. }
  8897. cur = ggml_scale(ctx0, cur, 1.0f / float(n_altup)); // [n_embd, n_tokens]
  8898. cb(cur, "unembd_merged", -1);
  8899. }
  8900. // cur now has shape: [n_embd, n_tokens]
  8901. // TODO: move this to right after the last KV layer
  8902. {
  8903. // skip computing output for unused tokens
  8904. ggml_tensor * inp_out_ids = build_inp_out_ids();
  8905. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  8906. }
  8907. cur = build_norm(cur,
  8908. model.output_norm, NULL,
  8909. LLM_NORM_RMS, -1);
  8910. cb(cur, "result_norm", -1);
  8911. res->t_embd = cur;
  8912. cur = build_lora_mm(model.output, cur);
  8913. {
  8914. // final logit soft-capping
  8915. cur = ggml_scale(ctx0, cur, 1.0f / hparams.f_final_logit_softcapping);
  8916. cur = ggml_tanh(ctx0, cur);
  8917. cur = ggml_scale(ctx0, cur, hparams.f_final_logit_softcapping);
  8918. }
  8919. cb(cur, "result_output", -1);
  8920. res->t_logits = cur;
  8921. ggml_build_forward_expand(gf, cur);
  8922. }
  8923. ggml_tensor * calc_magnitude(ggml_tensor * x) {
  8924. return ggml_sqrt(ctx0, ggml_sum_rows(ctx0, ggml_sqr(ctx0, x)));
  8925. }
  8926. // get 2D slice view from a 3D tensor, the idx corresponds to the 3rd dim
  8927. ggml_tensor * view_2d_slice(ggml_tensor * x, int idx) {
  8928. GGML_ASSERT(idx < (int)x->ne[2]);
  8929. return ggml_view_2d(ctx0, x, x->ne[0], x->ne[1],
  8930. ggml_row_size(x->type, x->ne[0]),
  8931. idx * x->ne[0] * x->ne[1] * ggml_element_size(x));
  8932. }
  8933. // equivalent to get_per_layer_inputs() in python code
  8934. // output shape: [n_embd_altup, n_layer, n_tokens]
  8935. ggml_tensor * get_per_layer_inputs() {
  8936. auto inp = std::make_unique<llm_graph_input_embd>();
  8937. ggml_tensor * inp_per_layer;
  8938. if (ubatch.token) {
  8939. inp->tokens = ggml_new_tensor_1d(ctx0, GGML_TYPE_I32, ubatch.n_tokens);
  8940. ggml_set_input(inp->tokens);
  8941. res->t_tokens = inp->tokens;
  8942. inp_per_layer = ggml_get_rows(ctx0, model.tok_embd_per_layer, inp->tokens);
  8943. inp_per_layer = ggml_reshape_3d(ctx0, inp_per_layer, n_embd_altup, n_layer, n_tokens);
  8944. inp_per_layer = ggml_scale(ctx0, inp_per_layer, sqrtf((float)n_embd_altup));
  8945. cb(inp_per_layer, "inp_per_layer_selected", -1);
  8946. } else {
  8947. GGML_ABORT("TODO: support embd input");
  8948. }
  8949. res->add_input(std::move(inp));
  8950. return inp_per_layer;
  8951. }
  8952. // equivalent to project_per_layer_inputs() in python code
  8953. // this calculates the per-layer inputs, so the final tensor shape will have n_layer as the last dim
  8954. // output shape: [n_embd_altup, n_tokens, n_layer]
  8955. ggml_tensor * project_per_layer_inputs(ggml_tensor * inputs_embeds, ggml_tensor * inp_per_layer) {
  8956. const float per_layer_projection_scale = 1.0f / sqrtf((float)n_embd);
  8957. const float per_layer_input_scale = 1.0f / sqrtf(2.0f);
  8958. ggml_tensor * per_layer_proj = ggml_mul_mat(ctx0, model.per_layer_model_proj, inputs_embeds);
  8959. per_layer_proj = ggml_scale(ctx0, per_layer_proj, per_layer_projection_scale);
  8960. per_layer_proj = ggml_reshape_3d(ctx0, per_layer_proj, n_embd_altup, n_layer, n_tokens);
  8961. per_layer_proj = build_norm(per_layer_proj,
  8962. model.per_layer_proj_norm, NULL,
  8963. LLM_NORM_RMS, -1); // [n_embd_altup, n_layer, n_tokens]
  8964. cb(per_layer_proj, "per_layer_proj", -1);
  8965. inp_per_layer = ggml_add(ctx0, inp_per_layer, per_layer_proj);
  8966. inp_per_layer = ggml_scale(ctx0, inp_per_layer, per_layer_input_scale);
  8967. cb(inp_per_layer, "inp_per_layer", -1);
  8968. // permute to shape: [n_embd_altup, n_tokens, n_layer]
  8969. inp_per_layer = ggml_cont(ctx0, ggml_permute(ctx0, inp_per_layer, 0, 2, 1, 3));
  8970. return inp_per_layer;
  8971. }
  8972. // input cur shape: [n_altup, n_tokens]
  8973. // output shape: [n_altup, n_tokens]
  8974. ggml_tensor * laurel(ggml_tensor * cur, int il) {
  8975. ggml_tensor * tmp = cur;
  8976. tmp = build_lora_mm(model.layers[il].laurel_l, tmp);
  8977. tmp = build_lora_mm(model.layers[il].laurel_r, tmp);
  8978. tmp = build_norm(tmp, model.layers[il].laurel_post_norm, NULL, LLM_NORM_RMS, il);
  8979. tmp = ggml_add(ctx0, tmp, cur);
  8980. cb(tmp, "laurel_out", il);
  8981. return tmp;
  8982. }
  8983. // input x shape: [n_embd, n_tokens]
  8984. // output shape: [n_embd, n_tokens]
  8985. ggml_tensor * gaussian_topk(ggml_tensor * x) {
  8986. ggml_tensor * mean = ggml_mean(ctx0, x);
  8987. ggml_tensor * std = ggml_sqrt(ctx0, ggml_scale(ctx0,
  8988. ggml_sum_rows(ctx0, ggml_sqr(ctx0, ggml_sub(ctx0, x, mean))),
  8989. 1.0f / (float)(x->ne[0] - 1)
  8990. ));
  8991. ggml_tensor * cutoff_x = ggml_add(ctx0, mean, ggml_scale(ctx0, std, f_sparsity_std_mul));
  8992. return ggml_relu(ctx0, ggml_sub(ctx0, x, cutoff_x));
  8993. }
  8994. //
  8995. // altup functions
  8996. //
  8997. // equivalent to compute_router_modalities() in python code
  8998. // input x shape: [n_embd, n_tokens]
  8999. // output shape: [n_altup, n_tokens]
  9000. ggml_tensor * altup_compute_router_modalities(ggml_tensor * x, int il) {
  9001. ggml_tensor * router_inputs = build_norm(x,
  9002. model.layers[il].altup_router_norm, NULL,
  9003. LLM_NORM_RMS, il);
  9004. // router_input_scale
  9005. router_inputs = ggml_scale(ctx0, router_inputs, 1.0f / (float)n_embd);
  9006. ggml_tensor * output = ggml_mul_mat(ctx0, model.layers[il].altup_router, router_inputs);
  9007. return ggml_tanh(ctx0, output); // [n_altup, n_tokens]
  9008. }
  9009. // input cur shape: [n_embd, n_tokens, n_altup]
  9010. // output shape: [n_embd, n_tokens, n_altup]
  9011. ggml_tensor * altup_predict(ggml_tensor * cur, int il) {
  9012. ggml_tensor * activated = view_2d_slice(cur, i_altup_act); // [n_embd, n_tokens]
  9013. ggml_tensor * modalities = altup_compute_router_modalities(activated, il); // [n_altup, n_tokens]
  9014. cb(modalities, "modalities", il);
  9015. ggml_tensor * all_coefs = build_lora_mm(model.layers[il].altup_predict_coef, modalities);
  9016. cb(all_coefs, "all_coefs", il);
  9017. // first dim now having n_altup^2 elements, we reshape it to 2D (so we end up with 3D tensor)
  9018. all_coefs = ggml_reshape_3d(ctx0, all_coefs, n_altup, n_altup, n_tokens);
  9019. // permute to [n_altup, n_embd, n_tokens]
  9020. ggml_tensor * cur_permuted = ggml_cont(ctx0, ggml_permute(ctx0, cur, 1, 2, 0, 3));
  9021. ggml_tensor * predictions = ggml_mul_mat(ctx0, cur_permuted, all_coefs); // [n_altup, n_embd, n_tokens]
  9022. // final shape must be the same as cur: [n_embd, n_tokens, n_altup]
  9023. predictions = ggml_cont(ctx0, ggml_permute(ctx0, predictions, 0, 2, 1, 3));
  9024. predictions = ggml_add(ctx0, predictions, cur);
  9025. cb(predictions, "predictions", il);
  9026. return predictions;
  9027. }
  9028. // input predictions shape: [n_embd, n_tokens, n_altup]
  9029. // input activated shape: [n_embd, n_tokens]
  9030. // output shape: [n_embd, n_tokens, n_altup]
  9031. ggml_tensor * altup_correct(ggml_tensor * predictions, ggml_tensor * activated, int il) {
  9032. ggml_tensor * modalities = altup_compute_router_modalities(activated, il); // [n_altup, n_tokens]
  9033. cb(modalities, "modalities", il);
  9034. ggml_tensor * active_prediction = view_2d_slice(predictions, i_altup_act);
  9035. ggml_tensor * innovation = ggml_sub(ctx0, activated, active_prediction); // [n_embd, n_tokens]
  9036. cb(innovation, "innovation", il);
  9037. ggml_tensor * all_coefs = build_lora_mm(model.layers[il].altup_correct_coef, modalities); // [n_altup, n_tokens]
  9038. all_coefs = ggml_scale_bias(ctx0, all_coefs, 1.0f, 1.0f); // + 1.0
  9039. cb(all_coefs, "all_coefs", il);
  9040. all_coefs = ggml_transpose(ctx0, all_coefs); // [n_tokens, n_altup]
  9041. all_coefs = ggml_cont_3d(ctx0, all_coefs, 1, n_tokens, n_altup); // [1, n_tokens, n_altup]
  9042. innovation = ggml_repeat_4d(ctx0, innovation, n_embd, n_tokens, n_altup, 1);
  9043. ggml_tensor * corrected = ggml_mul(ctx0, innovation, all_coefs); // [n_embd, n_tokens, n_altup]
  9044. corrected = ggml_add(ctx0, corrected, predictions); // [n_embd, n_tokens, n_altup]
  9045. cb(corrected, "corrected", il);
  9046. return corrected;
  9047. }
  9048. };
  9049. struct llm_build_gemma_embedding_iswa : public llm_graph_context {
  9050. llm_build_gemma_embedding_iswa(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  9051. const int64_t n_embd_head = hparams.n_embd_head_k;
  9052. ggml_tensor * cur;
  9053. ggml_tensor * inpL;
  9054. inpL = build_inp_embd(model.tok_embd);
  9055. // important: do not normalize weights for raw embeddings input (i.e. encoded image emdeddings)
  9056. if (ubatch.token) {
  9057. inpL = ggml_scale(ctx0, inpL, sqrtf(n_embd));
  9058. cb(inpL, "inp_scaled", -1);
  9059. }
  9060. // inp_pos - contains the positions
  9061. ggml_tensor * inp_pos = build_inp_pos();
  9062. // TODO: support cacheless iSWA embeddings [TAG_NO_CACHE_ISWA]
  9063. auto * inp_attn = build_attn_inp_kv_iswa();
  9064. ggml_tensor * inp_out_ids = build_inp_out_ids();
  9065. for (int il = 0; il < n_layer; ++il) {
  9066. const float freq_base_l = model.get_rope_freq_base (cparams, il);
  9067. const float freq_scale_l = model.get_rope_freq_scale(cparams, il);
  9068. // norm
  9069. cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
  9070. cb(cur, "attn_norm", il);
  9071. // self-attention
  9072. {
  9073. // compute Q and K and RoPE them
  9074. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  9075. cb(Qcur, "Qcur", il);
  9076. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  9077. cb(Kcur, "Kcur", il);
  9078. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  9079. cb(Vcur, "Vcur", il);
  9080. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  9081. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  9082. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  9083. Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);
  9084. cb(Qcur, "Qcur_normed", il);
  9085. Qcur = ggml_rope_ext(
  9086. ctx0, Qcur, inp_pos, nullptr,
  9087. n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l,
  9088. ext_factor, attn_factor, beta_fast, beta_slow);
  9089. Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);
  9090. cb(Kcur, "Kcur_normed", il);
  9091. Kcur = ggml_rope_ext(
  9092. ctx0, Kcur, inp_pos, nullptr,
  9093. n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l,
  9094. ext_factor, attn_factor, beta_fast, beta_slow);
  9095. cb(Qcur, "Qcur", il);
  9096. cb(Kcur, "Kcur", il);
  9097. cb(Vcur, "Vcur", il);
  9098. // ref: https://github.com/google/gemma_pytorch/blob/014acb7ac4563a5f77c76d7ff98f31b568c16508/gemma/model.py#L315
  9099. Qcur = ggml_scale(ctx0, Qcur, hparams.f_attention_scale);
  9100. cur = build_attn(inp_attn,
  9101. model.layers[il].wo, NULL,
  9102. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f, il);
  9103. }
  9104. if (il == n_layer - 1 && inp_out_ids) {
  9105. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  9106. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  9107. }
  9108. cur = build_norm(cur,
  9109. model.layers[il].attn_post_norm, NULL,
  9110. LLM_NORM_RMS, il);
  9111. cb(cur, "attn_post_norm", il);
  9112. ggml_tensor * sa_out = ggml_add(ctx0, cur, inpL);
  9113. cb(sa_out, "sa_out", il);
  9114. cur = build_norm(sa_out,
  9115. model.layers[il].ffn_norm, NULL,
  9116. LLM_NORM_RMS, il);
  9117. cb(cur, "ffn_norm", il);
  9118. // feed-forward network
  9119. {
  9120. cur = build_ffn(cur,
  9121. model.layers[il].ffn_up, NULL, NULL,
  9122. model.layers[il].ffn_gate, NULL, NULL,
  9123. model.layers[il].ffn_down, NULL, NULL,
  9124. NULL,
  9125. LLM_FFN_GELU, LLM_FFN_PAR, il);
  9126. cb(cur, "ffn_out", il);
  9127. }
  9128. cur = build_norm(cur,
  9129. model.layers[il].ffn_post_norm, NULL,
  9130. LLM_NORM_RMS, -1);
  9131. cb(cur, "ffn_post_norm", -1);
  9132. cur = ggml_add(ctx0, cur, sa_out);
  9133. cur = build_cvec(cur, il);
  9134. cb(cur, "l_out", il);
  9135. // input for next layer
  9136. inpL = cur;
  9137. }
  9138. cur = inpL;
  9139. cur = build_norm(cur,
  9140. model.output_norm, NULL,
  9141. LLM_NORM_RMS, -1);
  9142. cb(cur, "result_norm", -1);
  9143. res->t_embd = cur;
  9144. ggml_build_forward_expand(gf, cur);
  9145. }
  9146. };
  9147. // TODO: move up next to build_starcoder
  9148. struct llm_build_starcoder2 : public llm_graph_context {
  9149. llm_build_starcoder2(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  9150. const int64_t n_embd_head = hparams.n_embd_head_v;
  9151. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  9152. GGML_ASSERT(n_embd_head == hparams.n_rot);
  9153. ggml_tensor * cur;
  9154. ggml_tensor * inpL;
  9155. inpL = build_inp_embd(model.tok_embd);
  9156. // inp_pos - contains the positions
  9157. ggml_tensor * inp_pos = build_inp_pos();
  9158. auto * inp_attn = build_attn_inp_kv();
  9159. ggml_tensor * inp_out_ids = build_inp_out_ids();
  9160. for (int il = 0; il < n_layer; ++il) {
  9161. ggml_tensor * inpSA = inpL;
  9162. // norm
  9163. cur = build_norm(inpL,
  9164. model.layers[il].attn_norm, model.layers[il].attn_norm_b,
  9165. LLM_NORM, il);
  9166. cb(cur, "attn_norm", il);
  9167. // self-attention
  9168. {
  9169. // compute Q and K and RoPE them
  9170. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  9171. cb(Qcur, "Qcur", il);
  9172. if (model.layers[il].bq) {
  9173. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  9174. cb(Qcur, "Qcur", il);
  9175. }
  9176. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  9177. cb(Kcur, "Kcur", il);
  9178. if (model.layers[il].bk) {
  9179. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  9180. cb(Kcur, "Kcur", il);
  9181. }
  9182. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  9183. cb(Vcur, "Vcur", il);
  9184. if (model.layers[il].bv) {
  9185. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  9186. cb(Vcur, "Vcur", il);
  9187. }
  9188. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  9189. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  9190. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  9191. Qcur = ggml_rope_ext(
  9192. ctx0, Qcur, inp_pos, nullptr,
  9193. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  9194. ext_factor, attn_factor, beta_fast, beta_slow
  9195. );
  9196. Kcur = ggml_rope_ext(
  9197. ctx0, Kcur, inp_pos, nullptr,
  9198. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  9199. ext_factor, attn_factor, beta_fast, beta_slow
  9200. );
  9201. cb(Qcur, "Qcur", il);
  9202. cb(Kcur, "Kcur", il);
  9203. cb(Vcur, "Vcur", il);
  9204. cur = build_attn(inp_attn,
  9205. model.layers[il].wo, model.layers[il].bo,
  9206. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  9207. }
  9208. if (il == n_layer - 1 && inp_out_ids) {
  9209. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  9210. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  9211. }
  9212. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  9213. cb(ffn_inp, "ffn_inp", il);
  9214. // feed-forward network
  9215. cur = build_norm(ffn_inp,
  9216. model.layers[il].ffn_norm, model.layers[il].ffn_norm_b,
  9217. LLM_NORM, il);
  9218. cb(cur, "ffn_norm", il);
  9219. cur = build_ffn(cur,
  9220. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  9221. NULL, NULL, NULL,
  9222. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  9223. NULL,
  9224. LLM_FFN_GELU, LLM_FFN_SEQ, il);
  9225. cb(cur, "ffn_out", il);
  9226. cur = ggml_add(ctx0, cur, ffn_inp);
  9227. cur = build_cvec(cur, il);
  9228. cb(cur, "l_out", il);
  9229. // input for next layer
  9230. inpL = cur;
  9231. }
  9232. cur = inpL;
  9233. cur = build_norm(cur,
  9234. model.output_norm, model.output_norm_b,
  9235. LLM_NORM, -1);
  9236. cb(cur, "result_norm", -1);
  9237. res->t_embd = cur;
  9238. // lm_head
  9239. cur = build_lora_mm(model.output, cur);
  9240. cb(cur, "result_output", -1);
  9241. res->t_logits = cur;
  9242. ggml_build_forward_expand(gf, cur);
  9243. }
  9244. };
  9245. struct llm_graph_context_mamba : public llm_graph_context {
  9246. llm_graph_context_mamba(const llm_graph_params & params) : llm_graph_context(params) {}
  9247. ggml_tensor * build_mamba_layer(
  9248. llm_graph_input_rs * inp,
  9249. ggml_tensor * cur,
  9250. const llama_model & model,
  9251. const llama_ubatch & ubatch,
  9252. int il) {
  9253. const auto * mctx_cur = inp->mctx;
  9254. const auto kv_head = mctx_cur->get_head();
  9255. const auto & layer = model.layers[il];
  9256. const int64_t d_conv = hparams.ssm_d_conv;
  9257. const int64_t d_inner = hparams.ssm_d_inner;
  9258. const int64_t d_state = hparams.ssm_d_state;
  9259. const int64_t dt_rank = hparams.ssm_dt_rank;
  9260. const int64_t n_head = d_inner;
  9261. const int64_t head_dim = 1;
  9262. const int64_t n_seqs = ubatch.n_seqs;
  9263. // Some variants of Mamba arch (e.g. FalconMamba do apply layer norm on B and Dt layers)
  9264. const bool ssm_dt_b_c_rms = hparams.ssm_dt_b_c_rms;
  9265. const int64_t n_seq_tokens = ubatch.n_seq_tokens;
  9266. GGML_ASSERT(n_seqs != 0);
  9267. GGML_ASSERT(ubatch.equal_seqs());
  9268. GGML_ASSERT(ubatch.n_tokens == n_seq_tokens * n_seqs);
  9269. ggml_tensor * conv_states_all = mctx_cur->get_r_l(il);
  9270. ggml_tensor * ssm_states_all = mctx_cur->get_s_l(il);
  9271. ggml_tensor * conv = build_rs(inp, conv_states_all, hparams.n_embd_r(), n_seqs);
  9272. conv = ggml_reshape_3d(ctx0, conv, d_conv - 1, d_inner, n_seqs);
  9273. // {n_embd, n_tokens} => {n_embd, n_seq_tokens, n_seqs}
  9274. cur = ggml_reshape_3d(ctx0, cur, cur->ne[0], n_seq_tokens, n_seqs);
  9275. // {n_embd, 2*d_inner} @ {n_embd, n_seq_tokens, n_seqs} => {2*d_inner, n_seq_tokens, n_seqs}
  9276. ggml_tensor * xz = build_lora_mm(layer.ssm_in, cur);
  9277. // split the above in two
  9278. // => {d_inner, n_seq_tokens, n_seqs}
  9279. ggml_tensor * x = ggml_view_3d(ctx0, xz, d_inner, xz->ne[1], xz->ne[2], xz->nb[1], xz->nb[2], 0);
  9280. 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));
  9281. // conv
  9282. {
  9283. // => {d_conv - 1 + n_seq_tokens, d_inner, n_seqs}
  9284. ggml_tensor * conv_x = ggml_concat(ctx0, conv, ggml_transpose(ctx0, x), 0);
  9285. // copy last (d_conv - 1) columns back into the state cache
  9286. 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]));
  9287. ggml_build_forward_expand(gf,
  9288. ggml_cpy(ctx0, last_conv,
  9289. ggml_view_1d(ctx0, conv_states_all,
  9290. (d_conv - 1)*(d_inner)*(n_seqs),
  9291. kv_head*(d_conv - 1)*(d_inner)*ggml_element_size(conv_states_all))));
  9292. // 1D convolution
  9293. // The equivalent is to make a self-overlapping view of conv_x
  9294. // over d_conv columns at each stride in the 3rd dimension,
  9295. // then element-wise multiply that with the conv1d weight,
  9296. // then sum the elements of each row,
  9297. // (the last two steps are a dot product over rows (also doable with mul_mat))
  9298. // then permute away the ne[0] dimension,
  9299. // and then you're left with the resulting x tensor.
  9300. // For simultaneous sequences, all sequences need to have the same length.
  9301. x = ggml_ssm_conv(ctx0, conv_x, layer.ssm_conv1d);
  9302. // bias
  9303. x = ggml_add(ctx0, x, layer.ssm_conv1d_b);
  9304. x = ggml_silu(ctx0, x);
  9305. }
  9306. // ssm
  9307. {
  9308. // {d_inner, dt_rank + 2*d_state} @ {d_inner, n_seq_tokens, n_seqs} => {dt_rank + 2*d_state, n_seq_tokens, n_seqs}
  9309. ggml_tensor * x_db = build_lora_mm(layer.ssm_x, x);
  9310. // split
  9311. 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);
  9312. 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);
  9313. 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));
  9314. // Some Mamba variants (e.g. FalconMamba, Jamba) apply RMS norm in B, C & Dt layers
  9315. if (ssm_dt_b_c_rms || (layer.ssm_dt_norm && layer.ssm_b_norm && layer.ssm_c_norm)) {
  9316. dt = build_norm(dt, layer.ssm_dt_norm, NULL, LLM_NORM_RMS, il);
  9317. B = build_norm(B, layer.ssm_b_norm, NULL, LLM_NORM_RMS, il);
  9318. C = build_norm(C, layer.ssm_c_norm, NULL, LLM_NORM_RMS, il);
  9319. }
  9320. // {dt_rank, d_inner} @ {dt_rank, n_seq_tokens, n_seqs} => {d_inner, n_seq_tokens, n_seqs}
  9321. dt = build_lora_mm(layer.ssm_dt, dt);
  9322. dt = ggml_add(ctx0, dt, layer.ssm_dt_b);
  9323. cur = x;
  9324. x = ggml_reshape_4d(ctx0, x, head_dim, n_head, n_seq_tokens, n_seqs);
  9325. ggml_tensor * A = layer.ssm_a;
  9326. // use the states and the indices provided by build_recurrent_state
  9327. // (this is necessary in order to properly use the states before they are overwritten,
  9328. // while avoiding to make unnecessary copies of the states)
  9329. auto get_ssm_rows = [&](ggml_context * ctx, ggml_tensor * states, ggml_tensor * ids) {
  9330. ggml_tensor * ssm = ggml_reshape_4d(ctx, states, d_state, head_dim, n_head, mctx_cur->get_size());
  9331. // Custom operator to optimize the parallel associative scan
  9332. // as described in the Annex D of the Mamba paper.
  9333. // => {d_inner, n_seq_tokens, n_seqs} and {d_state, d_inner, n_seqs}
  9334. return ggml_ssm_scan(ctx, ssm, x, dt, A, B, C, ids);
  9335. };
  9336. ggml_tensor * y_ssm = build_rs(inp, ssm_states_all, hparams.n_embd_s(), ubatch.n_seqs, get_ssm_rows);
  9337. // store last states
  9338. ggml_build_forward_expand(gf,
  9339. ggml_cpy(ctx0,
  9340. ggml_view_1d(ctx0, y_ssm, d_state*d_inner*n_seqs, x->nb[3]*x->ne[3]),
  9341. 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))));
  9342. ggml_tensor * y = ggml_view_3d(ctx0, y_ssm, d_inner, n_seq_tokens, n_seqs, x->nb[2], x->nb[3], 0);
  9343. // TODO: skip computing output earlier for unused tokens
  9344. y = ggml_add(ctx0, y, ggml_mul(ctx0, cur, layer.ssm_d));
  9345. y = ggml_swiglu_split(ctx0, ggml_cont(ctx0, z), y);
  9346. // {d_inner, n_embd} @ {d_inner, n_seq_tokens, n_seqs} => {n_embd, n_seq_tokens, n_seqs}
  9347. cur = build_lora_mm(layer.ssm_out, y);
  9348. }
  9349. // {n_embd, n_seq_tokens, n_seqs} => {n_embd, n_tokens}
  9350. cur = ggml_reshape_2d(ctx0, cur, cur->ne[0], n_seq_tokens * n_seqs);
  9351. return cur;
  9352. }
  9353. ggml_tensor * build_mamba2_layer(
  9354. llm_graph_input_rs * inp,
  9355. ggml_tensor * cur,
  9356. const llama_model & model,
  9357. const llama_ubatch & ubatch,
  9358. int il) const {
  9359. const auto * mctx_cur = inp->mctx;
  9360. const auto kv_head = mctx_cur->get_head();
  9361. const int64_t d_conv = hparams.ssm_d_conv;
  9362. const int64_t d_inner = hparams.ssm_d_inner;
  9363. const int64_t d_state = hparams.ssm_d_state;
  9364. const int64_t n_head = hparams.ssm_dt_rank;
  9365. const int64_t head_dim = d_inner / n_head;
  9366. const int64_t n_group = hparams.ssm_n_group;
  9367. const int64_t n_seqs = ubatch.n_seqs;
  9368. const int64_t n_seq_tokens = ubatch.n_seq_tokens;
  9369. GGML_ASSERT(n_seqs != 0);
  9370. GGML_ASSERT(ubatch.equal_seqs());
  9371. GGML_ASSERT(ubatch.n_tokens == n_seq_tokens * n_seqs);
  9372. ggml_tensor * conv_states_all = mctx_cur->get_r_l(il);
  9373. ggml_tensor * ssm_states_all = mctx_cur->get_s_l(il);
  9374. ggml_tensor * conv = build_rs(inp, conv_states_all, hparams.n_embd_r(), n_seqs);
  9375. conv = ggml_reshape_3d(ctx0, conv, d_conv - 1, d_inner + 2*n_group*d_state, n_seqs);
  9376. // {n_embd, n_tokens} => {n_embd, n_seq_tokens, n_seqs}
  9377. cur = ggml_reshape_3d(ctx0, cur, cur->ne[0], n_seq_tokens, n_seqs);
  9378. // d_in_proj = 2 * self.d_inner + 2 * self.ngroups * self.d_state + self.nheads
  9379. // {n_embd, d_in_proj} @ {n_embd, n_seq_tokens, n_seqs} => {d_in_proj, n_seq_tokens, n_seqs}
  9380. ggml_tensor * zxBCdt = build_lora_mm(model.layers[il].ssm_in, cur);
  9381. // split the above in three
  9382. 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);
  9383. 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));
  9384. 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));
  9385. // conv
  9386. {
  9387. // => {d_conv - 1 + n_seq_tokens, d_inner + 2*n_group*d_state, n_seqs}
  9388. ggml_tensor * conv_x = ggml_concat(ctx0, conv, ggml_transpose(ctx0, xBC), 0);
  9389. // copy last (d_conv - 1) columns back into the state cache
  9390. 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]));
  9391. ggml_build_forward_expand(gf,
  9392. ggml_cpy(ctx0, last_conv,
  9393. ggml_view_1d(ctx0, conv_states_all,
  9394. (d_conv - 1)*(d_inner + 2*n_group*d_state)*(n_seqs),
  9395. kv_head*(d_conv - 1)*(d_inner + 2*n_group*d_state)*ggml_element_size(conv_states_all))));
  9396. // 1D convolution
  9397. // The equivalent is to make a self-overlapping view of conv_x
  9398. // over d_conv columns at each stride in the 3rd dimension,
  9399. // then element-wise multiply that with the conv1d weight,
  9400. // then sum the elements of each row,
  9401. // (the last two steps are a dot product over rows (also doable with mul_mat))
  9402. // then permute away the ne[0] dimension,
  9403. // and then you're left with the resulting x tensor.
  9404. // For simultaneous sequences, all sequences need to have the same length.
  9405. xBC = ggml_ssm_conv(ctx0, conv_x, model.layers[il].ssm_conv1d);
  9406. // bias
  9407. xBC = ggml_add(ctx0, xBC, model.layers[il].ssm_conv1d_b);
  9408. xBC = ggml_silu(ctx0, xBC);
  9409. }
  9410. // ssm
  9411. {
  9412. // These correspond to V K Q in SSM/attention duality
  9413. 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);
  9414. 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));
  9415. 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));
  9416. // {n_head, n_seq_tokens, n_seqs}
  9417. dt = ggml_add(ctx0, ggml_cont(ctx0, dt), model.layers[il].ssm_dt_b);
  9418. ggml_tensor * A = model.layers[il].ssm_a;
  9419. // use the states and the indices provided by build_recurrent_state
  9420. // (this is necessary in order to properly use the states before they are overwritten,
  9421. // while avoiding to make unnecessary copies of the states)
  9422. auto get_ssm_rows = [&](ggml_context * ctx, ggml_tensor * states, ggml_tensor * ids) {
  9423. ggml_tensor * ssm = ggml_reshape_4d(ctx, states, d_state, head_dim, n_head, mctx_cur->get_size());
  9424. // TODO: use semistructured matrices to implement state-space duality
  9425. // => {d_inner, n_seq_tokens, n_seqs} and {d_state, d_inner, n_seqs}
  9426. return ggml_ssm_scan(ctx, ssm, x, dt, A, B, C, ids);
  9427. };
  9428. ggml_tensor * y_ssm = build_rs(inp, ssm_states_all, hparams.n_embd_s(), ubatch.n_seqs, get_ssm_rows);
  9429. // store last states
  9430. ggml_build_forward_expand(gf,
  9431. ggml_cpy(ctx0,
  9432. ggml_view_1d(ctx0, y_ssm, d_state*d_inner*n_seqs, ggml_nelements(x)*x->nb[0]),
  9433. 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))));
  9434. 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);
  9435. // TODO: skip computing output earlier for unused tokens
  9436. y = ggml_add(ctx0, y, ggml_mul(ctx0, x, model.layers[il].ssm_d));
  9437. y = ggml_swiglu_split(ctx0, ggml_cont(ctx0, z), y);
  9438. // grouped RMS norm
  9439. if (model.layers[il].ssm_norm) {
  9440. y = ggml_reshape_4d(ctx0, y, d_inner / n_group, n_group, n_seq_tokens, n_seqs);
  9441. y = build_norm(y, model.layers[il].ssm_norm, NULL, LLM_NORM_RMS, il);
  9442. }
  9443. y = ggml_reshape_3d(ctx0, y, d_inner, n_seq_tokens, n_seqs);
  9444. // {d_inner, n_embd} @ {d_inner, n_seq_tokens, n_seqs} => {n_embd, n_seq_tokens, n_seqs}
  9445. cur = build_lora_mm(model.layers[il].ssm_out, y);
  9446. }
  9447. // {n_embd, n_seq_tokens, n_seqs} => {n_embd, n_tokens}
  9448. cur = ggml_reshape_2d(ctx0, cur, cur->ne[0], n_seq_tokens * n_seqs);
  9449. cb(cur, "mamba_out", il);
  9450. return cur;
  9451. }
  9452. };
  9453. struct llm_build_mamba : public llm_graph_context_mamba {
  9454. llm_build_mamba(const llama_model & model, const llm_graph_params & params) : llm_graph_context_mamba(params) {
  9455. ggml_tensor * cur;
  9456. ggml_tensor * inpL;
  9457. // {n_embd, n_tokens}
  9458. inpL = build_inp_embd(model.tok_embd);
  9459. auto * rs_inp = build_rs_inp();
  9460. ggml_tensor * inp_out_ids = build_inp_out_ids();
  9461. for (int il = 0; il < n_layer; ++il) {
  9462. // norm
  9463. cur = build_norm(inpL,
  9464. model.layers[il].attn_norm, NULL,
  9465. LLM_NORM_RMS, il);
  9466. cb(cur, "attn_norm", il);
  9467. if (model.arch == LLM_ARCH_MAMBA2) {
  9468. cur = build_mamba2_layer(rs_inp, cur, model, ubatch, il);
  9469. } else {
  9470. cur = build_mamba_layer(rs_inp, cur, model, ubatch, il);
  9471. }
  9472. if (il == n_layer - 1 && inp_out_ids) {
  9473. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  9474. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  9475. }
  9476. // residual
  9477. cur = ggml_add(ctx0, cur, inpL);
  9478. cur = build_cvec(cur, il);
  9479. cb(cur, "l_out", il);
  9480. // input for next layer
  9481. inpL = cur;
  9482. }
  9483. // final rmsnorm
  9484. cur = build_norm(inpL, model.output_norm, NULL, LLM_NORM_RMS, -1);
  9485. cb(cur, "result_norm", -1);
  9486. res->t_embd = cur;
  9487. // lm_head
  9488. cur = build_lora_mm(model.output, cur);
  9489. cb(cur, "result_output", -1);
  9490. res->t_logits = cur;
  9491. ggml_build_forward_expand(gf, cur);
  9492. }
  9493. };
  9494. struct llm_build_jamba : public llm_graph_context_mamba {
  9495. llm_build_jamba(const llama_model & model, const llm_graph_params & params) : llm_graph_context_mamba(params) {
  9496. const int64_t n_embd_head = hparams.n_embd_head_v;
  9497. ggml_tensor * cur;
  9498. ggml_tensor * inpL;
  9499. // {n_embd, n_tokens}
  9500. inpL = build_inp_embd(model.tok_embd);
  9501. auto * inp_hybrid = build_inp_mem_hybrid();
  9502. ggml_tensor * inp_out_ids = build_inp_out_ids();
  9503. for (int il = 0; il < n_layer; ++il) {
  9504. const int64_t n_head_kv = hparams.n_head_kv(il);
  9505. cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
  9506. cb(cur, "attn_norm", il);
  9507. if (n_head_kv == 0) {
  9508. cur = build_mamba_layer(inp_hybrid->get_recr(), cur, model, ubatch, il);
  9509. } else {
  9510. // Attention
  9511. struct ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  9512. struct ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  9513. struct ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  9514. cb(Qcur, "Qcur", il);
  9515. cb(Kcur, "Kcur", il);
  9516. cb(Vcur, "Vcur", il);
  9517. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  9518. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  9519. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  9520. cb(Qcur, "Qcur", il);
  9521. cb(Kcur, "Kcur", il);
  9522. cb(Vcur, "Vcur", il);
  9523. // No RoPE :)
  9524. cur = build_attn(inp_hybrid->get_attn(),
  9525. model.layers[il].wo, NULL,
  9526. Qcur, Kcur, Vcur, NULL, NULL, NULL, 1.0f/sqrtf(float(n_embd_head)), il);
  9527. }
  9528. if (il == n_layer - 1 && inp_out_ids) {
  9529. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  9530. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  9531. }
  9532. // residual
  9533. struct ggml_tensor * ffn_inp = ggml_add(ctx0, inpL, cur);
  9534. cb(cur, "ffn_inp", il);
  9535. cur = build_norm(ffn_inp, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);
  9536. cb(cur, "ffn_norm", il);
  9537. // feed-forward network
  9538. if (model.layers[il].ffn_gate_inp == nullptr) {
  9539. // FFN
  9540. cur = build_ffn(cur,
  9541. model.layers[il].ffn_up, NULL, NULL,
  9542. model.layers[il].ffn_gate, NULL, NULL,
  9543. model.layers[il].ffn_down, NULL, NULL,
  9544. NULL,
  9545. LLM_FFN_SILU, LLM_FFN_PAR, il);
  9546. cb(cur, "ffn_out", il);
  9547. } else {
  9548. // MoE branch
  9549. cur = build_moe_ffn(cur,
  9550. model.layers[il].ffn_gate_inp,
  9551. model.layers[il].ffn_up_exps,
  9552. model.layers[il].ffn_gate_exps,
  9553. model.layers[il].ffn_down_exps,
  9554. nullptr,
  9555. n_expert, n_expert_used,
  9556. LLM_FFN_SILU, false,
  9557. false, 0.0,
  9558. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  9559. il);
  9560. cb(cur, "ffn_moe_out", il);
  9561. }
  9562. // residual
  9563. cur = ggml_add(ctx0, ffn_inp, cur);
  9564. cur = build_cvec(cur, il);
  9565. cb(cur, "l_out", il);
  9566. // input for next layer
  9567. inpL = cur;
  9568. }
  9569. // final rmsnorm
  9570. cur = build_norm(inpL, model.output_norm, NULL, LLM_NORM_RMS, -1);
  9571. cb(cur, "result_norm", -1);
  9572. res->t_embd = cur;
  9573. // lm_head
  9574. cur = build_lora_mm(model.output, cur);
  9575. cb(cur, "result_output", -1);
  9576. res->t_logits = cur;
  9577. ggml_build_forward_expand(gf, cur);
  9578. }
  9579. };
  9580. struct llm_build_command_r : public llm_graph_context {
  9581. llm_build_command_r(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  9582. const int64_t n_embd_head = hparams.n_embd_head_v;
  9583. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  9584. const float f_logit_scale = hparams.f_logit_scale;
  9585. ggml_tensor * cur;
  9586. ggml_tensor * inpL;
  9587. inpL = build_inp_embd(model.tok_embd);
  9588. // inp_pos - contains the positions
  9589. ggml_tensor * inp_pos = build_inp_pos();
  9590. auto * inp_attn = build_attn_inp_kv();
  9591. ggml_tensor * inp_out_ids = build_inp_out_ids();
  9592. for (int il = 0; il < n_layer; ++il) {
  9593. // norm
  9594. cur = build_norm(inpL,
  9595. model.layers[il].attn_norm, NULL,
  9596. LLM_NORM, il);
  9597. cb(cur, "attn_norm", il);
  9598. ggml_tensor * ffn_inp = cur;
  9599. // self-attention
  9600. {
  9601. // compute Q and K and RoPE them
  9602. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  9603. cb(Qcur, "Qcur", il);
  9604. if (model.layers[il].bq) {
  9605. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  9606. cb(Qcur, "Qcur", il);
  9607. }
  9608. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  9609. cb(Kcur, "Kcur", il);
  9610. if (model.layers[il].bk) {
  9611. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  9612. cb(Kcur, "Kcur", il);
  9613. }
  9614. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  9615. cb(Vcur, "Vcur", il);
  9616. if (model.layers[il].bv) {
  9617. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  9618. cb(Vcur, "Vcur", il);
  9619. }
  9620. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  9621. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  9622. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  9623. if (model.layers[il].attn_q_norm) {
  9624. Qcur = build_norm(Qcur,
  9625. model.layers[il].attn_q_norm,
  9626. NULL,
  9627. LLM_NORM, il);
  9628. cb(Qcur, "Qcur", il);
  9629. }
  9630. Qcur = ggml_rope_ext(
  9631. ctx0, Qcur, inp_pos, nullptr,
  9632. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  9633. ext_factor, attn_factor, beta_fast, beta_slow
  9634. );
  9635. if (model.layers[il].attn_k_norm) {
  9636. Kcur = build_norm(Kcur,
  9637. model.layers[il].attn_k_norm,
  9638. NULL,
  9639. LLM_NORM, il);
  9640. cb(Kcur, "Kcur", il);
  9641. }
  9642. Kcur = ggml_rope_ext(
  9643. ctx0, Kcur, inp_pos, nullptr,
  9644. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  9645. ext_factor, attn_factor, beta_fast, beta_slow
  9646. );
  9647. cb(Qcur, "Qcur", il);
  9648. cb(Kcur, "Kcur", il);
  9649. cb(Vcur, "Vcur", il);
  9650. cur = build_attn(inp_attn,
  9651. model.layers[il].wo, model.layers[il].bo,
  9652. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  9653. }
  9654. if (il == n_layer - 1 && inp_out_ids) {
  9655. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  9656. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  9657. ffn_inp = ggml_get_rows(ctx0, ffn_inp, inp_out_ids);
  9658. }
  9659. ggml_tensor * attn_out = cur;
  9660. // feed-forward network
  9661. {
  9662. cur = build_ffn(ffn_inp,
  9663. model.layers[il].ffn_up, NULL, NULL,
  9664. model.layers[il].ffn_gate, NULL, NULL,
  9665. model.layers[il].ffn_down, NULL, NULL,
  9666. NULL,
  9667. LLM_FFN_SILU, LLM_FFN_PAR, il);
  9668. cb(cur, "ffn_out", il);
  9669. }
  9670. // add together residual + FFN + self-attention
  9671. cur = ggml_add(ctx0, cur, inpL);
  9672. cur = ggml_add(ctx0, cur, attn_out);
  9673. cur = build_cvec(cur, il);
  9674. cb(cur, "l_out", il);
  9675. // input for next layer
  9676. inpL = cur;
  9677. }
  9678. cur = inpL;
  9679. cur = build_norm(cur,
  9680. model.output_norm, NULL,
  9681. LLM_NORM, -1);
  9682. cb(cur, "result_norm", -1);
  9683. res->t_embd = cur;
  9684. // lm_head
  9685. cur = build_lora_mm(model.output, cur);
  9686. if (f_logit_scale) {
  9687. cur = ggml_scale(ctx0, cur, f_logit_scale);
  9688. }
  9689. cb(cur, "result_output", -1);
  9690. res->t_logits = cur;
  9691. ggml_build_forward_expand(gf, cur);
  9692. }
  9693. };
  9694. struct llm_build_cohere2_iswa : public llm_graph_context {
  9695. llm_build_cohere2_iswa(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  9696. const int64_t n_embd_head = hparams.n_embd_head_v;
  9697. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  9698. const float f_logit_scale = hparams.f_logit_scale;
  9699. ggml_tensor * cur;
  9700. ggml_tensor * inpL;
  9701. inpL = build_inp_embd(model.tok_embd);
  9702. // inp_pos - contains the positions
  9703. ggml_tensor * inp_pos = build_inp_pos();
  9704. auto * inp_attn = build_attn_inp_kv_iswa();
  9705. ggml_tensor * inp_out_ids = build_inp_out_ids();
  9706. for (int il = 0; il < n_layer; ++il) {
  9707. const bool is_swa = hparams.is_swa(il);
  9708. // norm
  9709. cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM, il);
  9710. cb(cur, "attn_norm", il);
  9711. ggml_tensor * ffn_inp = cur;
  9712. // self-attention
  9713. {
  9714. // rope freq factors for 128k context
  9715. ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
  9716. // compute Q and K and RoPE them
  9717. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  9718. cb(Qcur, "Qcur", il);
  9719. if (model.layers[il].bq) {
  9720. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  9721. cb(Qcur, "Qcur", il);
  9722. }
  9723. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  9724. cb(Kcur, "Kcur", il);
  9725. if (model.layers[il].bk) {
  9726. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  9727. cb(Kcur, "Kcur", il);
  9728. }
  9729. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  9730. cb(Vcur, "Vcur", il);
  9731. if (model.layers[il].bv) {
  9732. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  9733. cb(Vcur, "Vcur", il);
  9734. }
  9735. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  9736. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  9737. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  9738. if (is_swa) {
  9739. Qcur = ggml_rope_ext(
  9740. ctx0, Qcur, inp_pos, rope_factors,
  9741. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  9742. ext_factor, attn_factor, beta_fast, beta_slow
  9743. );
  9744. Kcur = ggml_rope_ext(
  9745. ctx0, Kcur, inp_pos, rope_factors,
  9746. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  9747. ext_factor, attn_factor, beta_fast, beta_slow
  9748. );
  9749. }
  9750. cb(Qcur, "Qcur", il);
  9751. cb(Kcur, "Kcur", il);
  9752. cb(Vcur, "Vcur", il);
  9753. cur = build_attn(inp_attn,
  9754. model.layers[il].wo, model.layers[il].bo,
  9755. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  9756. }
  9757. if (il == n_layer - 1 && inp_out_ids) {
  9758. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  9759. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  9760. ffn_inp = ggml_get_rows(ctx0, ffn_inp, inp_out_ids);
  9761. }
  9762. ggml_tensor * attn_out = cur;
  9763. // feed-forward network
  9764. {
  9765. cur = build_ffn(ffn_inp, model.layers[il].ffn_up, NULL, NULL, model.layers[il].ffn_gate,
  9766. NULL, NULL, model.layers[il].ffn_down, NULL, NULL, NULL, LLM_FFN_SILU, LLM_FFN_PAR,
  9767. il);
  9768. cb(cur, "ffn_out", il);
  9769. }
  9770. // add together residual + FFN + self-attention
  9771. cur = ggml_add(ctx0, cur, inpL);
  9772. cur = ggml_add(ctx0, cur, attn_out);
  9773. cur = build_cvec(cur, il);
  9774. cb(cur, "l_out", il);
  9775. // input for next layer
  9776. inpL = cur;
  9777. }
  9778. cur = inpL;
  9779. cur = build_norm(cur, model.output_norm, NULL, LLM_NORM, -1);
  9780. cb(cur, "result_norm", -1);
  9781. res->t_embd = cur;
  9782. // lm_head
  9783. cur = build_lora_mm(model.output, cur);
  9784. if (f_logit_scale) {
  9785. cur = ggml_scale(ctx0, cur, f_logit_scale);
  9786. }
  9787. cb(cur, "result_output", -1);
  9788. res->t_logits = cur;
  9789. ggml_build_forward_expand(gf, cur);
  9790. }
  9791. };
  9792. // ref: https://allenai.org/olmo
  9793. // based on the original build_llama() function, changes:
  9794. // * non-parametric layer norm
  9795. // * clamp qkv
  9796. // * removed bias
  9797. // * removed MoE
  9798. struct llm_build_olmo : public llm_graph_context {
  9799. llm_build_olmo(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  9800. const int64_t n_embd_head = hparams.n_embd_head_v;
  9801. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  9802. GGML_ASSERT(n_embd_head == hparams.n_rot);
  9803. ggml_tensor * cur;
  9804. ggml_tensor * inpL;
  9805. inpL = build_inp_embd(model.tok_embd);
  9806. // inp_pos - contains the positions
  9807. ggml_tensor * inp_pos = build_inp_pos();
  9808. auto * inp_attn = build_attn_inp_kv();
  9809. ggml_tensor * inp_out_ids = build_inp_out_ids();
  9810. for (int il = 0; il < n_layer; ++il) {
  9811. ggml_tensor * inpSA = inpL;
  9812. // norm
  9813. cur = build_norm(inpL,
  9814. NULL, NULL,
  9815. LLM_NORM, il);
  9816. cb(cur, "attn_norm", il);
  9817. // self-attention
  9818. {
  9819. // compute Q and K and RoPE them
  9820. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  9821. cb(Qcur, "Qcur", il);
  9822. if (hparams.f_clamp_kqv > 0.0f) {
  9823. Qcur = ggml_clamp(ctx0, Qcur, -hparams.f_clamp_kqv, hparams.f_clamp_kqv);
  9824. cb(Qcur, "Qcur", il);
  9825. }
  9826. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  9827. cb(Kcur, "Kcur", il);
  9828. if (hparams.f_clamp_kqv > 0.0f) {
  9829. Kcur = ggml_clamp(ctx0, Kcur, -hparams.f_clamp_kqv, hparams.f_clamp_kqv);
  9830. cb(Kcur, "Kcur", il);
  9831. }
  9832. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  9833. cb(Vcur, "Vcur", il);
  9834. if (hparams.f_clamp_kqv > 0.0f) {
  9835. Vcur = ggml_clamp(ctx0, Vcur, -hparams.f_clamp_kqv, hparams.f_clamp_kqv);
  9836. cb(Vcur, "Vcur", il);
  9837. }
  9838. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  9839. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  9840. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  9841. Qcur = ggml_rope_ext(
  9842. ctx0, Qcur, inp_pos, nullptr,
  9843. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  9844. ext_factor, attn_factor, beta_fast, beta_slow
  9845. );
  9846. Kcur = ggml_rope_ext(
  9847. ctx0, Kcur, inp_pos, nullptr,
  9848. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  9849. ext_factor, attn_factor, beta_fast, beta_slow
  9850. );
  9851. cb(Qcur, "Qcur", il);
  9852. cb(Kcur, "Kcur", il);
  9853. cb(Vcur, "Vcur", il);
  9854. cur = build_attn(inp_attn,
  9855. model.layers[il].wo, nullptr,
  9856. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  9857. }
  9858. if (il == n_layer - 1 && inp_out_ids) {
  9859. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  9860. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  9861. }
  9862. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  9863. cb(ffn_inp, "ffn_inp", il);
  9864. // feed-forward network
  9865. cur = build_norm(ffn_inp,
  9866. NULL, NULL,
  9867. LLM_NORM, il);
  9868. cb(cur, "ffn_norm", il);
  9869. cur = build_ffn(cur,
  9870. model.layers[il].ffn_up, NULL, NULL,
  9871. model.layers[il].ffn_gate, NULL, NULL,
  9872. model.layers[il].ffn_down, NULL, NULL,
  9873. NULL,
  9874. LLM_FFN_SILU, LLM_FFN_PAR, il);
  9875. cb(cur, "ffn_out", il);
  9876. cur = ggml_add(ctx0, cur, ffn_inp);
  9877. cb(cur, "ffn_out", il);
  9878. cur = build_cvec(cur, il);
  9879. cb(cur, "l_out", il);
  9880. // input for next layer
  9881. inpL = cur;
  9882. }
  9883. cur = inpL;
  9884. cur = build_norm(cur,
  9885. NULL, NULL,
  9886. LLM_NORM, -1);
  9887. cb(cur, "result_norm", -1);
  9888. res->t_embd = cur;
  9889. // lm_head
  9890. cur = build_lora_mm(model.output, cur);
  9891. cb(cur, "result_output", -1);
  9892. res->t_logits = cur;
  9893. ggml_build_forward_expand(gf, cur);
  9894. }
  9895. };
  9896. template <bool iswa>
  9897. struct llm_build_olmo2 : public llm_graph_context {
  9898. llm_build_olmo2(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  9899. const int64_t n_embd_head = hparams.n_embd_head_v;
  9900. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  9901. GGML_ASSERT(n_embd_head == hparams.n_rot);
  9902. ggml_tensor * cur;
  9903. ggml_tensor * inpL;
  9904. inpL = build_inp_embd(model.tok_embd);
  9905. // inp_pos - contains the positions
  9906. ggml_tensor * inp_pos = build_inp_pos();
  9907. using inp_attn_type = std::conditional_t<iswa, llm_graph_input_attn_kv_iswa, llm_graph_input_attn_kv>;
  9908. inp_attn_type * inp_attn = nullptr;
  9909. if constexpr (iswa) {
  9910. inp_attn = build_attn_inp_kv_iswa();
  9911. } else {
  9912. inp_attn = build_attn_inp_kv();
  9913. }
  9914. ggml_tensor * inp_out_ids = build_inp_out_ids();
  9915. for (int il = 0; il < n_layer; ++il) {
  9916. ggml_tensor * inpSA = inpL;
  9917. cur = inpL;
  9918. // self_attention
  9919. {
  9920. // compute Q and K and RoPE them
  9921. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  9922. cb(Qcur, "Qcur", il);
  9923. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  9924. cb(Kcur, "Kcur", il);
  9925. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  9926. cb(Vcur, "Vcur", il);
  9927. Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL,
  9928. LLM_NORM_RMS, il);
  9929. cb(Qcur, "Qcur_normed", il);
  9930. Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL,
  9931. LLM_NORM_RMS, il);
  9932. cb(Kcur, "Kcur_normed", il);
  9933. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  9934. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  9935. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  9936. const bool is_swa = hparams.is_swa(il);
  9937. if (is_swa) {
  9938. // For sliding window layers, Olmo3 use regular rope with no yarn rope scaling.
  9939. // This is achieved here by setting freq_scale and attn_factor to 1.
  9940. // We also set ext_factor to 0 to avoid a few unnecessary computations.
  9941. Qcur = ggml_rope_ext(
  9942. ctx0, Qcur, inp_pos, nullptr,
  9943. n_rot, rope_type, n_ctx_orig, freq_base, 1.0,
  9944. 0.0, 1.0, beta_fast, beta_slow
  9945. );
  9946. Kcur = ggml_rope_ext(
  9947. ctx0, Kcur, inp_pos, nullptr,
  9948. n_rot, rope_type, n_ctx_orig, freq_base, 1.0,
  9949. 0.0, 1.0, beta_fast, beta_slow
  9950. );
  9951. } else {
  9952. Qcur = ggml_rope_ext(
  9953. ctx0, Qcur, inp_pos, nullptr,
  9954. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  9955. ext_factor, attn_factor, beta_fast, beta_slow
  9956. );
  9957. Kcur = ggml_rope_ext(
  9958. ctx0, Kcur, inp_pos, nullptr,
  9959. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  9960. ext_factor, attn_factor, beta_fast, beta_slow
  9961. );
  9962. }
  9963. cb(Qcur, "Qcur", il);
  9964. cb(Kcur, "Kcur", il);
  9965. cb(Vcur, "Vcur", il);
  9966. cur = build_attn(inp_attn,
  9967. model.layers[il].wo, NULL,
  9968. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  9969. }
  9970. if (il == n_layer - 1 && inp_out_ids) {
  9971. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  9972. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  9973. }
  9974. cur = build_norm(cur,
  9975. model.layers[il].attn_post_norm, NULL,
  9976. LLM_NORM_RMS, il);
  9977. cb(cur, "attn_post_norm", il);
  9978. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  9979. cb(ffn_inp, "ffn_inp", il);
  9980. // feed-forward network
  9981. cur = build_ffn(ffn_inp,
  9982. model.layers[il].ffn_up, NULL, NULL,
  9983. model.layers[il].ffn_gate, NULL, NULL,
  9984. model.layers[il].ffn_down, NULL, NULL,
  9985. NULL,
  9986. LLM_FFN_SILU, LLM_FFN_PAR, il);
  9987. cb(cur, "ffn_out", il);
  9988. cur = build_norm(cur,
  9989. model.layers[il].ffn_post_norm, NULL,
  9990. LLM_NORM_RMS, -1);
  9991. cb(cur, "ffn_post_norm", -1);
  9992. cur = ggml_add(ctx0, cur, ffn_inp);
  9993. cb(cur, "ffn_out", il);
  9994. cur = build_cvec(cur, il);
  9995. cb(cur, "l_out", il);
  9996. // input for next layer
  9997. inpL = cur;
  9998. }
  9999. cur = inpL;
  10000. cur = build_norm(cur,
  10001. model.output_norm, NULL,
  10002. LLM_NORM_RMS, -1);
  10003. cb(cur, "result_norm", -1);
  10004. res->t_embd = cur;
  10005. // lm_head
  10006. cur = build_lora_mm(model.output, cur);
  10007. cb(cur, "result_output", -1);
  10008. res->t_logits = cur;
  10009. ggml_build_forward_expand(gf, cur);
  10010. }
  10011. };
  10012. // based on the build_qwen2moe() function, changes:
  10013. // * removed shared experts
  10014. // * removed bias
  10015. // * added q, k norm
  10016. struct llm_build_olmoe : public llm_graph_context {
  10017. llm_build_olmoe(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  10018. const int64_t n_embd_head = hparams.n_embd_head_v;
  10019. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  10020. GGML_ASSERT(n_embd_head == hparams.n_rot);
  10021. ggml_tensor * cur;
  10022. ggml_tensor * inpL;
  10023. inpL = build_inp_embd(model.tok_embd);
  10024. // inp_pos - contains the positions
  10025. ggml_tensor * inp_pos = build_inp_pos();
  10026. auto * inp_attn = build_attn_inp_kv();
  10027. ggml_tensor * inp_out_ids = build_inp_out_ids();
  10028. for (int il = 0; il < n_layer; ++il) {
  10029. ggml_tensor * inpSA = inpL;
  10030. // norm
  10031. cur = build_norm(inpL,
  10032. model.layers[il].attn_norm, NULL,
  10033. LLM_NORM_RMS, il);
  10034. cb(cur, "attn_norm", il);
  10035. // self_attention
  10036. {
  10037. // compute Q and K and RoPE them
  10038. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  10039. cb(Qcur, "Qcur", il);
  10040. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  10041. cb(Kcur, "Kcur", il);
  10042. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  10043. cb(Vcur, "Vcur", il);
  10044. Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL,
  10045. LLM_NORM_RMS, il);
  10046. cb(Qcur, "Qcur_normed", il);
  10047. Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL,
  10048. LLM_NORM_RMS, il);
  10049. cb(Kcur, "Kcur_normed", il);
  10050. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  10051. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  10052. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  10053. Qcur = ggml_rope_ext(
  10054. ctx0, Qcur, inp_pos, nullptr,
  10055. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  10056. ext_factor, attn_factor, beta_fast, beta_slow
  10057. );
  10058. Kcur = ggml_rope_ext(
  10059. ctx0, Kcur, inp_pos, nullptr,
  10060. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  10061. ext_factor, attn_factor, beta_fast, beta_slow
  10062. );
  10063. cb(Qcur, "Qcur", il);
  10064. cb(Kcur, "Kcur", il);
  10065. cb(Vcur, "Vcur", il);
  10066. cur = build_attn(inp_attn,
  10067. model.layers[il].wo, NULL,
  10068. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  10069. }
  10070. if (il == n_layer - 1 && inp_out_ids) {
  10071. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  10072. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  10073. }
  10074. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  10075. cb(ffn_inp, "ffn_inp", il);
  10076. // MoE branch
  10077. cur = build_norm(ffn_inp,
  10078. model.layers[il].ffn_norm, NULL,
  10079. LLM_NORM_RMS, il);
  10080. cb(cur, "ffn_norm", il);
  10081. cur = build_moe_ffn(cur,
  10082. model.layers[il].ffn_gate_inp,
  10083. model.layers[il].ffn_up_exps,
  10084. model.layers[il].ffn_gate_exps,
  10085. model.layers[il].ffn_down_exps,
  10086. nullptr,
  10087. n_expert, n_expert_used,
  10088. LLM_FFN_SILU, false,
  10089. false, 0.0,
  10090. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  10091. il);
  10092. cb(cur, "ffn_moe_out", il);
  10093. cur = ggml_add(ctx0, cur, ffn_inp);
  10094. cur = build_cvec(cur, il);
  10095. cb(cur, "l_out", il);
  10096. // input for next layer
  10097. inpL = cur;
  10098. }
  10099. cur = inpL;
  10100. cur = build_norm(cur,
  10101. model.output_norm, NULL,
  10102. LLM_NORM_RMS, -1);
  10103. cb(cur, "result_norm", -1);
  10104. res->t_embd = cur;
  10105. // lm_head
  10106. cur = build_lora_mm(model.output, cur);
  10107. cb(cur, "result_output", -1);
  10108. res->t_logits = cur;
  10109. ggml_build_forward_expand(gf, cur);
  10110. }
  10111. };
  10112. struct llm_build_llada_moe : public llm_graph_context {
  10113. llm_build_llada_moe(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  10114. const int64_t n_embd_head = hparams.n_embd_head_v;
  10115. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  10116. GGML_ASSERT(n_embd_head == hparams.n_rot);
  10117. ggml_tensor * cur;
  10118. ggml_tensor * inpL;
  10119. inpL = build_inp_embd(model.tok_embd);
  10120. // inp_pos - contains the positions
  10121. ggml_tensor * inp_pos = build_inp_pos();
  10122. auto * inp_attn = build_attn_inp_no_cache();
  10123. ggml_tensor * inp_out_ids = build_inp_out_ids();
  10124. for (int il = 0; il < n_layer; ++il) {
  10125. ggml_tensor * inpSA = inpL;
  10126. // norm
  10127. cur = build_norm(inpL,
  10128. model.layers[il].attn_norm, NULL,
  10129. LLM_NORM_RMS, il);
  10130. cb(cur, "attn_norm", il);
  10131. // self_attention
  10132. {
  10133. // compute Q and K and RoPE them
  10134. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  10135. cb(Qcur, "Qcur", il);
  10136. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  10137. cb(Kcur, "Kcur", il);
  10138. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  10139. cb(Vcur, "Vcur", il);
  10140. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  10141. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  10142. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  10143. Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);
  10144. cb(Qcur, "Qcur_normed", il);
  10145. Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);
  10146. cb(Kcur, "Kcur_normed", il);
  10147. Qcur = ggml_rope_ext(
  10148. ctx0, Qcur, inp_pos, nullptr,
  10149. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  10150. ext_factor, attn_factor, beta_fast, beta_slow
  10151. );
  10152. Kcur = ggml_rope_ext(
  10153. ctx0, Kcur, inp_pos, nullptr,
  10154. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  10155. ext_factor, attn_factor, beta_fast, beta_slow
  10156. );
  10157. cb(Qcur, "Qcur", il);
  10158. cb(Kcur, "Kcur", il);
  10159. cb(Vcur, "Vcur", il);
  10160. cur = build_attn(inp_attn,
  10161. model.layers[il].wo, NULL,
  10162. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  10163. }
  10164. if (il == n_layer - 1 && inp_out_ids) {
  10165. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  10166. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  10167. }
  10168. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  10169. cb(ffn_inp, "ffn_inp", il);
  10170. // MoE branch
  10171. cur = build_norm(ffn_inp,
  10172. model.layers[il].ffn_norm, NULL,
  10173. LLM_NORM_RMS, il);
  10174. cb(cur, "ffn_norm", il);
  10175. cur = build_moe_ffn(cur,
  10176. model.layers[il].ffn_gate_inp,
  10177. model.layers[il].ffn_up_exps,
  10178. model.layers[il].ffn_gate_exps,
  10179. model.layers[il].ffn_down_exps,
  10180. nullptr,
  10181. n_expert, n_expert_used,
  10182. LLM_FFN_SILU, false,
  10183. false, 0.0,
  10184. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  10185. il);
  10186. cb(cur, "ffn_moe_out", il);
  10187. cur = ggml_add(ctx0, cur, ffn_inp);
  10188. cur = build_cvec(cur, il);
  10189. cb(cur, "l_out", il);
  10190. // input for next layer
  10191. inpL = cur;
  10192. }
  10193. cur = inpL;
  10194. cur = build_norm(cur,
  10195. model.output_norm, NULL,
  10196. LLM_NORM_RMS, -1);
  10197. cb(cur, "result_norm", -1);
  10198. res->t_embd = cur;
  10199. // lm_head
  10200. cur = build_lora_mm(model.output, cur);
  10201. cb(cur, "result_output", -1);
  10202. res->t_logits = cur;
  10203. ggml_build_forward_expand(gf, cur);
  10204. }
  10205. };
  10206. struct llm_build_openelm : public llm_graph_context {
  10207. llm_build_openelm(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  10208. const int64_t n_embd_head = hparams.n_embd_head_v;
  10209. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  10210. ggml_tensor * cur;
  10211. ggml_tensor * inpL;
  10212. inpL = build_inp_embd(model.tok_embd);
  10213. // inp_pos - contains the positions
  10214. ggml_tensor * inp_pos = build_inp_pos();
  10215. auto * inp_attn = build_attn_inp_kv();
  10216. ggml_tensor * inp_out_ids = build_inp_out_ids();
  10217. for (int il = 0; il < n_layer; ++il) {
  10218. const int64_t n_head = hparams.n_head(il);
  10219. const int64_t n_head_kv = hparams.n_head_kv(il);
  10220. const int64_t n_head_qkv = 2*n_head_kv + n_head;
  10221. cur = inpL;
  10222. ggml_tensor * residual = cur;
  10223. // norm
  10224. cur = build_norm(inpL,
  10225. model.layers[il].attn_norm, NULL,
  10226. LLM_NORM_RMS, il);
  10227. cb(cur, "attn_norm", il);
  10228. // self-attention
  10229. {
  10230. cur = build_lora_mm(model.layers[il].wqkv, cur);
  10231. cb(cur, "wqkv", il);
  10232. cur = ggml_reshape_3d(ctx0, cur, n_embd_head_k, n_head_qkv, n_tokens);
  10233. ggml_tensor * Qcur = ggml_view_3d(ctx0, cur, n_embd_head, n_head, n_tokens, cur->nb[1], cur->nb[2], 0);
  10234. cb(Qcur, "Qcur", il);
  10235. 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);
  10236. cb(Kcur, "Kcur", il);
  10237. 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)));
  10238. cb(Vcur, "Vcur", il);
  10239. Qcur = build_norm(Qcur,
  10240. model.layers[il].attn_q_norm, NULL,
  10241. LLM_NORM_RMS, il);
  10242. cb(Qcur, "Qcur", il);
  10243. Kcur = build_norm(Kcur,
  10244. model.layers[il].attn_k_norm, NULL,
  10245. LLM_NORM_RMS, il);
  10246. cb(Kcur, "Kcur", il);
  10247. Qcur = ggml_rope_ext(
  10248. ctx0, Qcur, inp_pos, NULL,
  10249. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  10250. ext_factor, attn_factor, beta_fast, beta_slow
  10251. );
  10252. Kcur = ggml_rope_ext(
  10253. ctx0, Kcur, inp_pos, NULL,
  10254. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  10255. ext_factor, attn_factor, beta_fast, beta_slow
  10256. );
  10257. cb(Qcur, "Qcur", il);
  10258. cb(Kcur, "Kcur", il);
  10259. cb(Qcur, "Vcur", il);
  10260. cur = build_attn(inp_attn,
  10261. model.layers[il].wo, NULL,
  10262. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  10263. }
  10264. if (il == n_layer - 1 && inp_out_ids) {
  10265. residual = ggml_get_rows(ctx0, residual, inp_out_ids);
  10266. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  10267. }
  10268. ggml_tensor * ffn_inp = ggml_add(ctx0, residual, cur);
  10269. cb(ffn_inp, "ffn_inp", il);
  10270. // feed-forward network
  10271. {
  10272. cur = build_norm(ffn_inp,
  10273. model.layers[il].ffn_norm, NULL,
  10274. LLM_NORM_RMS, il);
  10275. cb(cur, "ffn_norm", il);
  10276. cur = build_ffn(cur,
  10277. model.layers[il].ffn_up, NULL, NULL,
  10278. model.layers[il].ffn_gate, NULL, NULL,
  10279. model.layers[il].ffn_down, NULL, NULL,
  10280. NULL,
  10281. LLM_FFN_SILU, LLM_FFN_PAR, il);
  10282. cb(cur, "ffn_out", il);
  10283. }
  10284. cur = ggml_add(ctx0, cur, ffn_inp);
  10285. cur = build_cvec(cur, il);
  10286. cb(cur, "l_out", il);
  10287. inpL = cur;
  10288. }
  10289. cur = inpL;
  10290. // norm
  10291. cur = build_norm(cur,
  10292. model.output_norm, NULL,
  10293. LLM_NORM_RMS, -1);
  10294. cb(cur, "result_norm", -1);
  10295. res->t_embd = cur;
  10296. cur = build_lora_mm(model.output, cur);
  10297. cb(cur, "result_output", -1);
  10298. res->t_logits = cur;
  10299. ggml_build_forward_expand(gf, cur);
  10300. }
  10301. };
  10302. struct llm_build_gptneox : public llm_graph_context {
  10303. llm_build_gptneox(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  10304. const int64_t n_embd_head = hparams.n_embd_head_v;
  10305. const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  10306. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  10307. ggml_tensor * cur;
  10308. ggml_tensor * inpL;
  10309. inpL = build_inp_embd(model.tok_embd);
  10310. // inp_pos - contains the positions
  10311. ggml_tensor * inp_pos = build_inp_pos();
  10312. auto * inp_attn = build_attn_inp_kv();
  10313. ggml_tensor * inp_out_ids = build_inp_out_ids();
  10314. for (int il = 0; il < n_layer; ++il) {
  10315. cur = build_norm(inpL,
  10316. model.layers[il].attn_norm,
  10317. model.layers[il].attn_norm_b,
  10318. LLM_NORM, il);
  10319. cb(cur, "attn_norm", il);
  10320. // self-attention
  10321. {
  10322. cur = build_lora_mm(model.layers[il].wqkv, cur);
  10323. cb(cur, "wqkv", il);
  10324. cur = ggml_add(ctx0, cur, model.layers[il].bqkv);
  10325. cb(cur, "bqkv", il);
  10326. 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));
  10327. 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));
  10328. ggml_tensor * Vcur = 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 + n_embd_gqa));
  10329. Qcur = ggml_rope_ext(
  10330. ctx0, Qcur, inp_pos, nullptr,
  10331. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  10332. ext_factor, attn_factor, beta_fast, beta_slow
  10333. );
  10334. Kcur = ggml_rope_ext(
  10335. ctx0, Kcur, inp_pos, nullptr,
  10336. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  10337. ext_factor, attn_factor, beta_fast, beta_slow
  10338. );
  10339. cb(Qcur, "Qcur", il);
  10340. cb(Kcur, "Kcur", il);
  10341. cb(Vcur, "Vcur", il);
  10342. cur = build_attn(inp_attn,
  10343. model.layers[il].wo, model.layers[il].bo,
  10344. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  10345. }
  10346. if (il == n_layer - 1 && inp_out_ids) {
  10347. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  10348. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  10349. }
  10350. // ffn
  10351. if (hparams.use_par_res) {
  10352. // attention and ffn are computed in parallel
  10353. // x = x + attn(ln1(x)) + ffn(ln2(x))
  10354. ggml_tensor * attn_out = cur;
  10355. cur = build_norm(inpL,
  10356. model.layers[il].ffn_norm,
  10357. model.layers[il].ffn_norm_b,
  10358. LLM_NORM, il);
  10359. cb(cur, "ffn_norm", il);
  10360. cur = build_ffn(cur,
  10361. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  10362. NULL, NULL, NULL,
  10363. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  10364. NULL,
  10365. LLM_FFN_GELU, LLM_FFN_SEQ, il);
  10366. cb(cur, "ffn_out", il);
  10367. cur = ggml_add(ctx0, cur, inpL);
  10368. cb(cur, "ffn_out", il);
  10369. cur = ggml_add(ctx0, cur, attn_out);
  10370. cur = build_cvec(cur, il);
  10371. cb(cur, "l_out", il);
  10372. // input for next layer
  10373. inpL = cur;
  10374. } else {
  10375. // attention and ffn are computed sequentially
  10376. // x = x + attn(ln1(x))
  10377. // x = x + ffn(ln2(x))
  10378. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
  10379. cb(ffn_inp, "ffn_inp", il);
  10380. cur = build_norm(ffn_inp,
  10381. model.layers[il].ffn_norm,
  10382. model.layers[il].ffn_norm_b,
  10383. LLM_NORM, il);
  10384. cb(cur, "ffn_norm", il);
  10385. cur = build_ffn(cur,
  10386. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  10387. NULL, NULL, NULL,
  10388. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  10389. NULL,
  10390. LLM_FFN_GELU, LLM_FFN_SEQ, il);
  10391. cb(cur, "ffn_out", il);
  10392. cur = ggml_add(ctx0, cur, ffn_inp);
  10393. cur = build_cvec(cur, il);
  10394. cb(cur, "l_out", il);
  10395. // input for next layer
  10396. inpL = cur;
  10397. }
  10398. }
  10399. cur = build_norm(inpL,
  10400. model.output_norm,
  10401. model.output_norm_b,
  10402. LLM_NORM, -1);
  10403. cb(cur, "result_norm", -1);
  10404. res->t_embd = cur;
  10405. cur = build_lora_mm(model.output, cur);
  10406. cb(cur, "result_output", -1);
  10407. res->t_logits = cur;
  10408. ggml_build_forward_expand(gf, cur);
  10409. }
  10410. };
  10411. struct llm_build_arctic : public llm_graph_context {
  10412. llm_build_arctic(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  10413. const int64_t n_embd_head = hparams.n_embd_head_v;
  10414. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  10415. GGML_ASSERT(n_embd_head == hparams.n_rot);
  10416. ggml_tensor * cur;
  10417. ggml_tensor * inpL;
  10418. inpL = build_inp_embd(model.tok_embd);
  10419. // inp_pos - contains the positions
  10420. ggml_tensor * inp_pos = build_inp_pos();
  10421. auto * inp_attn = build_attn_inp_kv();
  10422. ggml_tensor * inp_out_ids = build_inp_out_ids();
  10423. for (int il = 0; il < n_layer; ++il) {
  10424. ggml_tensor * inpSA = inpL;
  10425. // norm
  10426. cur = build_norm(inpL,
  10427. model.layers[il].attn_norm, NULL,
  10428. LLM_NORM_RMS, il);
  10429. cb(cur, "attn_norm", il);
  10430. // self-attention
  10431. {
  10432. // compute Q and K and RoPE them
  10433. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  10434. cb(Qcur, "Qcur", il);
  10435. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  10436. cb(Kcur, "Kcur", il);
  10437. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  10438. cb(Vcur, "Vcur", il);
  10439. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  10440. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  10441. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  10442. Qcur = ggml_rope_ext(
  10443. ctx0, Qcur, inp_pos, nullptr,
  10444. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  10445. ext_factor, attn_factor, beta_fast, beta_slow
  10446. );
  10447. Kcur = ggml_rope_ext(
  10448. ctx0, Kcur, inp_pos, nullptr,
  10449. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  10450. ext_factor, attn_factor, beta_fast, beta_slow
  10451. );
  10452. cb(Qcur, "Qcur", il);
  10453. cb(Kcur, "Kcur", il);
  10454. cb(Vcur, "Vcur", il);
  10455. cur = build_attn(inp_attn,
  10456. model.layers[il].wo, NULL,
  10457. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  10458. }
  10459. if (il == n_layer - 1 && inp_out_ids) {
  10460. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  10461. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  10462. }
  10463. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  10464. cb(ffn_inp, "ffn_inp", il);
  10465. // feed-forward network
  10466. cur = build_norm(ffn_inp,
  10467. model.layers[il].ffn_norm, NULL,
  10468. LLM_NORM_RMS, il);
  10469. cb(cur, "ffn_norm", il);
  10470. cur = build_ffn(cur,
  10471. model.layers[il].ffn_up, NULL, NULL,
  10472. model.layers[il].ffn_gate, NULL, NULL,
  10473. model.layers[il].ffn_down, NULL, NULL,
  10474. NULL,
  10475. LLM_FFN_SILU, LLM_FFN_PAR, il);
  10476. cb(cur, "ffn_out", il);
  10477. ggml_tensor * ffn_out = ggml_add(ctx0, cur, ffn_inp);
  10478. cb(ffn_out, "ffn_out", il);
  10479. // MoE
  10480. cur = build_norm(inpSA,
  10481. model.layers[il].ffn_norm_exps, NULL,
  10482. LLM_NORM_RMS, il);
  10483. cb(cur, "ffn_norm_exps", il);
  10484. cur = build_moe_ffn(cur,
  10485. model.layers[il].ffn_gate_inp,
  10486. model.layers[il].ffn_up_exps,
  10487. model.layers[il].ffn_gate_exps,
  10488. model.layers[il].ffn_down_exps,
  10489. nullptr,
  10490. n_expert, n_expert_used,
  10491. LLM_FFN_SILU, true,
  10492. false, 0.0,
  10493. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  10494. il);
  10495. cb(cur, "ffn_moe_out", il);
  10496. cur = ggml_add(ctx0, cur, ffn_out);
  10497. cb(cur, "ffn_out", il);
  10498. cur = build_cvec(cur, il);
  10499. cb(cur, "l_out", il);
  10500. // input for next layer
  10501. inpL = cur;
  10502. }
  10503. cur = inpL;
  10504. cur = build_norm(cur,
  10505. model.output_norm, NULL,
  10506. LLM_NORM_RMS, -1);
  10507. cb(cur, "result_norm", -1);
  10508. res->t_embd = cur;
  10509. // lm_head
  10510. cur = build_lora_mm(model.output, cur);
  10511. cb(cur, "result_output", -1);
  10512. res->t_logits = cur;
  10513. ggml_build_forward_expand(gf, cur);
  10514. }
  10515. };
  10516. struct llm_build_deepseek : public llm_graph_context {
  10517. llm_build_deepseek(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  10518. const int64_t n_embd_head = hparams.n_embd_head_v;
  10519. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  10520. GGML_ASSERT(n_embd_head == hparams.n_rot);
  10521. ggml_tensor * cur;
  10522. ggml_tensor * inpL;
  10523. inpL = build_inp_embd(model.tok_embd);
  10524. // inp_pos - contains the positions
  10525. ggml_tensor * inp_pos = build_inp_pos();
  10526. auto * inp_attn = build_attn_inp_kv();
  10527. const float kq_scale = hparams.f_attention_scale == 0.0f ? 1.0f/sqrtf(float(n_embd_head)) : hparams.f_attention_scale;
  10528. ggml_tensor * inp_out_ids = build_inp_out_ids();
  10529. for (int il = 0; il < n_layer; ++il) {
  10530. ggml_tensor * inpSA = inpL;
  10531. // norm
  10532. cur = build_norm(inpL,
  10533. model.layers[il].attn_norm, NULL,
  10534. LLM_NORM_RMS, il);
  10535. cb(cur, "attn_norm", il);
  10536. // self-attention
  10537. {
  10538. // rope freq factors for llama3; may return nullptr for llama2 and other models
  10539. ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
  10540. // compute Q and K and RoPE them
  10541. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  10542. cb(Qcur, "Qcur", il);
  10543. if (model.layers[il].bq) {
  10544. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  10545. cb(Qcur, "Qcur", il);
  10546. }
  10547. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  10548. cb(Kcur, "Kcur", il);
  10549. if (model.layers[il].bk) {
  10550. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  10551. cb(Kcur, "Kcur", il);
  10552. }
  10553. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  10554. cb(Vcur, "Vcur", il);
  10555. if (model.layers[il].bv) {
  10556. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  10557. cb(Vcur, "Vcur", il);
  10558. }
  10559. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  10560. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  10561. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  10562. Qcur = ggml_rope_ext(
  10563. ctx0, Qcur, inp_pos, rope_factors,
  10564. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  10565. ext_factor, attn_factor, beta_fast, beta_slow
  10566. );
  10567. Kcur = ggml_rope_ext(
  10568. ctx0, Kcur, inp_pos, rope_factors,
  10569. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  10570. ext_factor, attn_factor, beta_fast, beta_slow
  10571. );
  10572. cb(Qcur, "Qcur", il);
  10573. cb(Kcur, "Kcur", il);
  10574. cb(Vcur, "Vcur", il);
  10575. cur = build_attn(inp_attn,
  10576. model.layers[il].wo, model.layers[il].bo,
  10577. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
  10578. }
  10579. if (il == n_layer - 1 && inp_out_ids) {
  10580. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  10581. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  10582. }
  10583. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  10584. cb(ffn_inp, "ffn_inp", il);
  10585. cur = build_norm(ffn_inp,
  10586. model.layers[il].ffn_norm, NULL,
  10587. LLM_NORM_RMS, il);
  10588. cb(cur, "ffn_norm", il);
  10589. if ((uint32_t) il < hparams.n_layer_dense_lead) {
  10590. cur = build_ffn(cur,
  10591. model.layers[il].ffn_up, NULL, NULL,
  10592. model.layers[il].ffn_gate, NULL, NULL,
  10593. model.layers[il].ffn_down, NULL, NULL,
  10594. NULL,
  10595. LLM_FFN_SILU, LLM_FFN_PAR, il);
  10596. cb(cur, "ffn_out", il);
  10597. } else {
  10598. // MoE branch
  10599. ggml_tensor * moe_out =
  10600. build_moe_ffn(cur,
  10601. model.layers[il].ffn_gate_inp,
  10602. model.layers[il].ffn_up_exps,
  10603. model.layers[il].ffn_gate_exps,
  10604. model.layers[il].ffn_down_exps,
  10605. nullptr,
  10606. n_expert, n_expert_used,
  10607. LLM_FFN_SILU, false,
  10608. false, hparams.expert_weights_scale,
  10609. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  10610. il);
  10611. cb(moe_out, "ffn_moe_out", il);
  10612. // FFN shared expert
  10613. {
  10614. ggml_tensor * ffn_shexp = build_ffn(cur,
  10615. model.layers[il].ffn_up_shexp, NULL, NULL,
  10616. model.layers[il].ffn_gate_shexp, NULL, NULL,
  10617. model.layers[il].ffn_down_shexp, NULL, NULL,
  10618. NULL,
  10619. LLM_FFN_SILU, LLM_FFN_PAR, il);
  10620. cb(ffn_shexp, "ffn_shexp", il);
  10621. cur = ggml_add(ctx0, moe_out, ffn_shexp);
  10622. cb(cur, "ffn_out", il);
  10623. }
  10624. }
  10625. cur = ggml_add(ctx0, cur, ffn_inp);
  10626. cur = build_cvec(cur, il);
  10627. cb(cur, "l_out", il);
  10628. // input for next layer
  10629. inpL = cur;
  10630. }
  10631. cur = inpL;
  10632. cur = build_norm(cur,
  10633. model.output_norm, NULL,
  10634. LLM_NORM_RMS, -1);
  10635. cb(cur, "result_norm", -1);
  10636. res->t_embd = cur;
  10637. // lm_head
  10638. cur = build_lora_mm(model.output, cur);
  10639. cb(cur, "result_output", -1);
  10640. res->t_logits = cur;
  10641. ggml_build_forward_expand(gf, cur);
  10642. }
  10643. };
  10644. struct llm_build_deepseek2 : public llm_graph_context {
  10645. llm_build_deepseek2(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  10646. bool is_lite = (hparams.n_layer == 27);
  10647. const bool is_mla = (hparams.n_embd_head_k_mla != 0 && hparams.n_embd_head_v_mla != 0);
  10648. // note: these are the actual head sizes you get when treating as MHA or after "decompression" using wv_b for MLA
  10649. const int64_t n_embd_head_k = is_mla ? hparams.n_embd_head_k_mla : hparams.n_embd_head_k;
  10650. const int64_t n_embd_head_v = is_mla ? hparams.n_embd_head_v_mla : hparams.n_embd_head_v;
  10651. const int64_t n_embd_head_qk_rope = hparams.n_rot;
  10652. const int64_t n_embd_head_qk_nope = n_embd_head_k - n_embd_head_qk_rope;
  10653. const uint32_t kv_lora_rank = hparams.n_lora_kv;
  10654. // We have to pre-scale kq_scale and attn_factor to make the YaRN RoPE work correctly.
  10655. // See https://github.com/ggerganov/llama.cpp/discussions/7416 for detailed explanation.
  10656. const float mscale = attn_factor * (1.0f + hparams.rope_yarn_log_mul * logf(1.0f / freq_scale));
  10657. const float kq_scale = 1.0f*mscale*mscale/sqrtf(float(n_embd_head_k));
  10658. const float attn_factor = 1.0f / (1.0f + 0.1f * logf(1.0f / freq_scale));
  10659. ggml_tensor * cur;
  10660. ggml_tensor * inpL;
  10661. // {n_embd, n_tokens}
  10662. inpL = build_inp_embd(model.tok_embd);
  10663. // inp_pos - contains the positions
  10664. ggml_tensor * inp_pos = build_inp_pos();
  10665. auto * inp_attn = build_attn_inp_kv();
  10666. ggml_tensor * inp_out_ids = build_inp_out_ids();
  10667. for (int il = 0; il < n_layer; ++il) {
  10668. ggml_tensor * inpSA = inpL;
  10669. // norm
  10670. cur = build_norm(inpL,
  10671. model.layers[il].attn_norm, NULL,
  10672. LLM_NORM_RMS, il);
  10673. cb(cur, "attn_norm", il);
  10674. // self_attention
  10675. {
  10676. ggml_tensor * q = NULL;
  10677. if (!is_lite) {
  10678. q = ggml_mul_mat(ctx0, model.layers[il].wq_a, cur);
  10679. cb(q, "q", il);
  10680. q = build_norm(q,
  10681. model.layers[il].attn_q_a_norm, nullptr,
  10682. LLM_NORM_RMS, il);
  10683. cb(q, "q", il);
  10684. q = ggml_mul_mat(ctx0, model.layers[il].wq_b, q);
  10685. cb(q, "q", il);
  10686. } else {
  10687. q = ggml_mul_mat(ctx0, model.layers[il].wq, cur);
  10688. cb(q, "q", il);
  10689. }
  10690. // split into {n_embd_head_qk_nope, n_head, n_tokens}
  10691. ggml_tensor * q_nope = ggml_view_3d(ctx0, q,
  10692. n_embd_head_qk_nope, n_head, n_tokens,
  10693. ggml_row_size(q->type, n_embd_head_k),
  10694. ggml_row_size(q->type, n_embd_head_k) * n_head,
  10695. 0);
  10696. cb(q_nope, "q_nope", il);
  10697. // and {n_embd_head_qk_rope, n_head, n_tokens}
  10698. ggml_tensor * q_pe = ggml_view_3d(ctx0, q,
  10699. n_embd_head_qk_rope, n_head, n_tokens,
  10700. ggml_row_size(q->type, n_embd_head_k),
  10701. ggml_row_size(q->type, n_embd_head_k) * n_head,
  10702. ggml_row_size(q->type, n_embd_head_qk_nope));
  10703. cb(q_pe, "q_pe", il);
  10704. ggml_tensor * kv_cmpr_pe = ggml_mul_mat(ctx0, model.layers[il].wkv_a_mqa, cur);
  10705. cb(kv_cmpr_pe, "kv_cmpr_pe", il);
  10706. // split into {kv_lora_rank, n_tokens}
  10707. ggml_tensor * kv_cmpr = ggml_view_2d(ctx0, kv_cmpr_pe,
  10708. kv_lora_rank, n_tokens,
  10709. ggml_row_size(kv_cmpr_pe->type, kv_lora_rank + n_embd_head_qk_rope),
  10710. 0);
  10711. cb(kv_cmpr, "kv_cmpr", il);
  10712. // and {n_embd_head_qk_rope, 1, n_tokens}
  10713. ggml_tensor * k_pe = ggml_view_3d(ctx0, kv_cmpr_pe,
  10714. n_embd_head_qk_rope, 1, n_tokens,
  10715. ggml_row_size(kv_cmpr_pe->type, kv_lora_rank + n_embd_head_qk_rope),
  10716. ggml_row_size(kv_cmpr_pe->type, kv_lora_rank + n_embd_head_qk_rope),
  10717. ggml_row_size(kv_cmpr_pe->type, kv_lora_rank));
  10718. cb(k_pe, "k_pe", il);
  10719. q_pe = ggml_rope_ext(ctx0, q_pe, inp_pos, nullptr,
  10720. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  10721. ext_factor, attn_factor, beta_fast, beta_slow
  10722. );
  10723. cb(q_pe, "q_pe", il);
  10724. k_pe = ggml_rope_ext(ctx0, k_pe, inp_pos, nullptr,
  10725. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  10726. ext_factor, attn_factor, beta_fast, beta_slow
  10727. );
  10728. cb(k_pe, "k_pe", il);
  10729. kv_cmpr = build_norm(kv_cmpr,
  10730. model.layers[il].attn_kv_a_norm, nullptr,
  10731. LLM_NORM_RMS, il);
  10732. cb(kv_cmpr, "kv_cmpr", il);
  10733. if (is_mla) {
  10734. // {n_embd_head_qk_nope, n_tokens, n_head}
  10735. q_nope = ggml_permute(ctx0, q_nope, 0, 2, 1, 3);
  10736. cb(q_nope, "q_nope_perm", il);
  10737. // {n_embd_head_qk_nope, kv_lora_rank, n_head} x {n_embd_head_qk_nope, n_tokens, n_head}
  10738. ggml_tensor * q_nope_absorbed = ggml_mul_mat(ctx0, model.layers[il].wk_b, q_nope);
  10739. cb(q_nope_absorbed, "q_nope_absorbed", il);
  10740. // {kv_lora_rank, n_head, n_tokens}
  10741. q_nope_absorbed = ggml_permute(ctx0, q_nope_absorbed, 0, 2, 1, 3);
  10742. cb(q_nope_absorbed, "q_nope_absorbed_perm", il);
  10743. // {n_embd_head_qk_rope + kv_lora_rank, n_head, n_tokens}
  10744. // note: rope must go first for in-place context shifting in build_rope_shift()
  10745. ggml_tensor * Qcur = ggml_concat(ctx0, q_pe, q_nope_absorbed, 0);
  10746. cb(Qcur, "Qcur", il);
  10747. kv_cmpr = ggml_reshape_3d(ctx0, kv_cmpr, kv_lora_rank, 1, n_tokens);
  10748. cb(kv_cmpr, "kv_cmpr_reshape", il);
  10749. // {n_embd_head_qk_rope + kv_lora_rank, 1, n_tokens}
  10750. ggml_tensor * Kcur = ggml_concat(ctx0, k_pe, kv_cmpr, 0);
  10751. cb(Kcur, "Kcur", il);
  10752. // {kv_lora_rank, 1, n_tokens}
  10753. ggml_tensor * Vcur = kv_cmpr;
  10754. cb(Vcur, "Vcur", il);
  10755. // note: MLA with the absorption optimzation converts into MQA (ie: GQA with 1 group)
  10756. cur = build_attn(inp_attn,
  10757. model.layers[il].wo, NULL,
  10758. Qcur, Kcur, Vcur, nullptr, nullptr, model.layers[il].wv_b, kq_scale, il);
  10759. } else {
  10760. ggml_tensor * kv = ggml_mul_mat(ctx0, model.layers[il].wkv_b, kv_cmpr);
  10761. cb(kv, "kv", il);
  10762. // split into {n_embd_head_qk_nope, n_head, n_tokens}
  10763. ggml_tensor * k_nope = ggml_view_3d(ctx0, kv,
  10764. n_embd_head_qk_nope, n_head, n_tokens,
  10765. ggml_row_size(kv->type, n_embd_head_qk_nope + n_embd_head_v),
  10766. ggml_row_size(kv->type, n_embd_head_qk_nope + n_embd_head_v) * n_head,
  10767. 0);
  10768. cb(k_nope, "k_nope_view", il);
  10769. // and {n_embd_head_v, n_head, n_tokens}
  10770. ggml_tensor * Vcur = ggml_view_3d(ctx0, kv,
  10771. n_embd_head_v, n_head, n_tokens,
  10772. ggml_row_size(kv->type, n_embd_head_qk_nope + n_embd_head_v),
  10773. ggml_row_size(kv->type, n_embd_head_qk_nope + n_embd_head_v) * n_head,
  10774. ggml_row_size(kv->type, n_embd_head_qk_nope));
  10775. cb(Vcur, "Vcur_view", il);
  10776. Vcur = ggml_cont(ctx0, Vcur);
  10777. cb(Vcur, "Vcur_cont", il);
  10778. // note: rope must go first for in-place context shifting in build_rope_shift()
  10779. ggml_tensor * Qcur = ggml_concat(ctx0, q_pe, q_nope, 0);
  10780. cb(Qcur, "Qcur", il);
  10781. ggml_tensor * Kcur = ggml_concat(ctx0, ggml_repeat(ctx0, k_pe, q_pe), k_nope, 0);
  10782. cb(Kcur, "Kcur", il);
  10783. // note: MLA without the absorption optimization converts into MHA (ie: GQA with full n_head groups)
  10784. cur = build_attn(inp_attn,
  10785. model.layers[il].wo, NULL,
  10786. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
  10787. }
  10788. }
  10789. if (il == n_layer - 1 && inp_out_ids) {
  10790. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  10791. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  10792. }
  10793. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  10794. cb(ffn_inp, "ffn_inp", il);
  10795. cur = build_norm(ffn_inp,
  10796. model.layers[il].ffn_norm, NULL,
  10797. LLM_NORM_RMS, il);
  10798. cb(cur, "ffn_norm", il);
  10799. if ((uint32_t) il < hparams.n_layer_dense_lead) {
  10800. cur = build_ffn(cur,
  10801. model.layers[il].ffn_up, NULL, NULL,
  10802. model.layers[il].ffn_gate, NULL, NULL,
  10803. model.layers[il].ffn_down, NULL, NULL,
  10804. NULL,
  10805. LLM_FFN_SILU, LLM_FFN_PAR, il);
  10806. cb(cur, "ffn_out", il);
  10807. } else {
  10808. // MoE branch
  10809. ggml_tensor * moe_out =
  10810. build_moe_ffn(cur,
  10811. model.layers[il].ffn_gate_inp,
  10812. model.layers[il].ffn_up_exps,
  10813. model.layers[il].ffn_gate_exps,
  10814. model.layers[il].ffn_down_exps,
  10815. model.layers[il].ffn_exp_probs_b,
  10816. n_expert, n_expert_used,
  10817. LLM_FFN_SILU, hparams.expert_weights_norm,
  10818. true, hparams.expert_weights_scale,
  10819. (llama_expert_gating_func_type) hparams.expert_gating_func,
  10820. il);
  10821. cb(moe_out, "ffn_moe_out", il);
  10822. // FFN shared expert
  10823. {
  10824. ggml_tensor * ffn_shexp = build_ffn(cur,
  10825. model.layers[il].ffn_up_shexp, NULL, NULL,
  10826. model.layers[il].ffn_gate_shexp, NULL, NULL,
  10827. model.layers[il].ffn_down_shexp, NULL, NULL,
  10828. NULL,
  10829. LLM_FFN_SILU, LLM_FFN_PAR, il);
  10830. cb(ffn_shexp, "ffn_shexp", il);
  10831. cur = ggml_add(ctx0, moe_out, ffn_shexp);
  10832. cb(cur, "ffn_out", il);
  10833. }
  10834. }
  10835. cur = ggml_add(ctx0, cur, ffn_inp);
  10836. cur = build_cvec(cur, il);
  10837. cb(cur, "l_out", il);
  10838. // input for next layer
  10839. inpL = cur;
  10840. }
  10841. cur = inpL;
  10842. cur = build_norm(cur,
  10843. model.output_norm, NULL,
  10844. LLM_NORM_RMS, -1);
  10845. cb(cur, "result_norm", -1);
  10846. res->t_embd = cur;
  10847. // lm_head
  10848. cur = ggml_mul_mat(ctx0, model.output, cur);
  10849. cb(cur, "result_output", -1);
  10850. res->t_logits = cur;
  10851. ggml_build_forward_expand(gf, cur);
  10852. }
  10853. };
  10854. struct llm_build_bitnet : public llm_graph_context {
  10855. llm_build_bitnet(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  10856. const int64_t n_embd_head = hparams.n_embd_head_v;
  10857. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  10858. ggml_tensor * cur;
  10859. ggml_tensor * inpL;
  10860. inpL = build_inp_embd(model.tok_embd);
  10861. // inp_pos - contains the positions
  10862. ggml_tensor * inp_pos = build_inp_pos();
  10863. auto * inp_attn = build_attn_inp_kv();
  10864. ggml_tensor * inp_out_ids = build_inp_out_ids();
  10865. for (int il = 0; il < n_layer; ++il) {
  10866. ggml_tensor * inpSA = inpL;
  10867. cur = build_norm(inpL,
  10868. model.layers[il].attn_norm, NULL,
  10869. LLM_NORM_RMS, il);
  10870. cb(cur, "attn_norm", il);
  10871. // self-attention
  10872. {
  10873. // compute Q and K and RoPE them
  10874. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  10875. if (model.layers[il].wq_scale) {
  10876. Qcur = ggml_mul(ctx0, Qcur, model.layers[il].wq_scale);
  10877. }
  10878. cb(Qcur, "Qcur", il);
  10879. if (model.layers[il].bq) {
  10880. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  10881. cb(Qcur, "Qcur", il);
  10882. }
  10883. // B1.K
  10884. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  10885. if (model.layers[il].wk_scale) {
  10886. Kcur = ggml_mul(ctx0, Kcur, model.layers[il].wk_scale);
  10887. }
  10888. cb(Kcur, "Kcur", il);
  10889. if (model.layers[il].bk) {
  10890. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  10891. cb(Kcur, "Kcur", il);
  10892. }
  10893. // B1.V
  10894. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  10895. if (model.layers[il].wv_scale) {
  10896. Vcur = ggml_mul(ctx0, Vcur, model.layers[il].wv_scale);
  10897. }
  10898. cb(Vcur, "Vcur", il);
  10899. if (model.layers[il].bv) {
  10900. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  10901. cb(Vcur, "Vcur", il);
  10902. }
  10903. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  10904. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  10905. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  10906. Qcur = ggml_rope_ext(
  10907. ctx0, Qcur, inp_pos, nullptr,
  10908. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  10909. ext_factor, attn_factor, beta_fast, beta_slow
  10910. );
  10911. Kcur = ggml_rope_ext(
  10912. ctx0, Kcur, inp_pos, nullptr,
  10913. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  10914. ext_factor, attn_factor, beta_fast, beta_slow
  10915. );
  10916. cb(Qcur, "Qcur", il);
  10917. cb(Kcur, "Kcur", il);
  10918. cb(Vcur, "Vcur", il);
  10919. cur = build_attn(inp_attn,
  10920. NULL, NULL,
  10921. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  10922. cur = build_norm(cur,
  10923. model.layers[il].attn_sub_norm, NULL,
  10924. LLM_NORM_RMS, il);
  10925. cb(cur, "attn_sub_norm", il);
  10926. cur = build_lora_mm(model.layers[il].wo, cur);
  10927. if (model.layers[il].wo_scale) {
  10928. cur = ggml_mul(ctx0, cur, model.layers[il].wo_scale);
  10929. }
  10930. if (model.layers[il].bo) {
  10931. cur = ggml_add(ctx0, cur, model.layers[il].bo);
  10932. }
  10933. cb(cur, "attn_o_out", il);
  10934. }
  10935. if (il == n_layer - 1 && inp_out_ids) {
  10936. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  10937. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  10938. }
  10939. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  10940. cb(ffn_inp, "ffn_inp", il);
  10941. // feed-forward forward
  10942. cur = build_norm(ffn_inp,
  10943. model.layers[il].ffn_norm, NULL,
  10944. LLM_NORM_RMS, il);
  10945. cb(cur, "ffn_norm", il);
  10946. cur = build_ffn(cur,
  10947. model.layers[il].ffn_up, NULL, model.layers[il].ffn_up_scale,
  10948. model.layers[il].ffn_gate, NULL, model.layers[il].ffn_gate_scale,
  10949. NULL, NULL, NULL,
  10950. NULL,
  10951. LLM_FFN_SILU, LLM_FFN_PAR, il);
  10952. cb(cur, "ffn_sub_out", il);
  10953. cur = build_norm(cur,
  10954. model.layers[il].ffn_sub_norm, NULL,
  10955. LLM_NORM_RMS, il);
  10956. cb(cur, "ffn_sub_norm", il);
  10957. cur = build_lora_mm(model.layers[il].ffn_down, cur);
  10958. if (model.layers[il].ffn_down_scale) {
  10959. cur = ggml_mul(ctx0, cur, model.layers[il].ffn_down_scale);
  10960. }
  10961. cb(cur, "ffn_down", il);
  10962. cur = ggml_add(ctx0, cur, ffn_inp);
  10963. cb(cur, "l_out", il);
  10964. // input for next layer
  10965. inpL = cur;
  10966. }
  10967. cur = inpL;
  10968. cur = build_norm(cur,
  10969. model.output_norm, NULL,
  10970. LLM_NORM_RMS, -1);
  10971. cb(cur, "result_norm", -1);
  10972. res->t_embd = cur;
  10973. // lm_head
  10974. // FIXME: do not use model.tok_embd directly, duplicate as model.output
  10975. cur = build_lora_mm(model.tok_embd, cur);
  10976. cb(cur, "result_output", -1);
  10977. res->t_logits = cur;
  10978. ggml_build_forward_expand(gf, cur);
  10979. }
  10980. };
  10981. struct llm_build_t5_enc : public llm_graph_context {
  10982. llm_build_t5_enc(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  10983. const int64_t n_embd_head = hparams.n_embd_head_v;
  10984. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  10985. ggml_tensor * cur;
  10986. ggml_tensor * inpL;
  10987. inpL = build_inp_embd(model.tok_embd);
  10988. ggml_tensor * pos_bucket_enc = build_inp_pos_bucket_enc();
  10989. auto * inp_attn = build_attn_inp_no_cache();
  10990. ggml_tensor * inp_out_ids = build_inp_out_ids();
  10991. for (int il = 0; il < n_layer; ++il) {
  10992. ggml_tensor * inpSA = inpL;
  10993. // norm
  10994. cur = build_norm(inpL,
  10995. model.layers[il].attn_norm_enc, NULL,
  10996. LLM_NORM_RMS, il);
  10997. cb(cur, "attn_norm", il);
  10998. // self-attention
  10999. {
  11000. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq_enc, cur);
  11001. cb(Qcur, "Qcur", il);
  11002. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk_enc, cur);
  11003. cb(Kcur, "Kcur", il);
  11004. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv_enc, cur);
  11005. cb(Vcur, "Vcur", il);
  11006. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  11007. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  11008. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  11009. 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;
  11010. ggml_tensor * kq_b = build_pos_bias(pos_bucket_enc, attn_rel_b);
  11011. cur = build_attn(inp_attn,
  11012. model.layers[il].wo_enc, nullptr,
  11013. Qcur, Kcur, Vcur, kq_b, nullptr, nullptr, 1.0f, il);
  11014. cb(cur, "kqv_out", il);
  11015. }
  11016. if (il == n_layer - 1 && inp_out_ids) {
  11017. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  11018. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  11019. }
  11020. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  11021. cb(ffn_inp, "ffn_inp", il);
  11022. // feed-forward network
  11023. {
  11024. cur = build_norm(ffn_inp,
  11025. model.layers[il].ffn_norm_enc, NULL,
  11026. LLM_NORM_RMS, il);
  11027. cb(cur, "ffn_norm", il);
  11028. // T5 uses relu, flan-T5 uses gelu-gated
  11029. cur = build_ffn(cur,
  11030. model.layers[il].ffn_up_enc, NULL, NULL,
  11031. model.layers[il].ffn_gate_enc, NULL, NULL,
  11032. model.layers[il].ffn_down_enc, NULL, NULL,
  11033. NULL,
  11034. model.layers[il].ffn_gate_enc ? LLM_FFN_GELU : LLM_FFN_RELU,
  11035. model.layers[il].ffn_gate_enc ? LLM_FFN_PAR : LLM_FFN_SEQ,
  11036. il);
  11037. cb(cur, "ffn_out", il);
  11038. }
  11039. cur = ggml_add(ctx0, cur, ffn_inp);
  11040. cb(cur, "ffn_out", il);
  11041. cur = build_cvec(cur, il);
  11042. cb(cur, "l_out", il);
  11043. // input for next layer
  11044. inpL = cur;
  11045. }
  11046. cur = inpL;
  11047. cb(cur, "result_embd", -1);
  11048. cur = build_norm(cur,
  11049. model.output_norm_enc, NULL,
  11050. LLM_NORM_RMS, -1);
  11051. cb(cur, "result_norm", -1);
  11052. res->t_embd = cur;
  11053. ggml_build_forward_expand(gf, cur);
  11054. }
  11055. };
  11056. struct llm_build_t5_dec : public llm_graph_context {
  11057. llm_build_t5_dec(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  11058. const int64_t n_embd_head = hparams.n_embd_head_v;
  11059. //const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  11060. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  11061. ggml_tensor * cur;
  11062. ggml_tensor * inpL;
  11063. inpL = build_inp_embd(model.tok_embd);
  11064. ggml_tensor * embd_enc = build_inp_cross_embd();
  11065. ggml_tensor * pos_bucket_dec = build_inp_pos_bucket_dec();
  11066. const int64_t n_outputs_enc = embd_enc->ne[1];
  11067. auto * inp_attn_self = build_attn_inp_kv();
  11068. auto * inp_attn_cross = build_attn_inp_cross();
  11069. ggml_tensor * inp_out_ids = build_inp_out_ids();
  11070. const int64_t dec_n_layer = hparams.dec_n_layer;
  11071. for (int il = 0; il < dec_n_layer; ++il) {
  11072. ggml_tensor * inpSA = inpL;
  11073. // norm
  11074. cur = build_norm(inpL,
  11075. model.layers[il].attn_norm, NULL,
  11076. LLM_NORM_RMS, il);
  11077. cb(cur, "attn_norm", il);
  11078. // self-attention
  11079. {
  11080. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  11081. cb(Qcur, "Qcur", il);
  11082. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  11083. cb(Kcur, "Kcur", il);
  11084. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  11085. cb(Vcur, "Vcur", il);
  11086. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  11087. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  11088. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  11089. ggml_tensor * attn_rel_b = model.layers[il].attn_rel_b ? model.layers[il].attn_rel_b : model.layers[0].attn_rel_b;
  11090. ggml_tensor * kq_b = build_pos_bias(pos_bucket_dec, attn_rel_b);
  11091. cur = build_attn(inp_attn_self,
  11092. model.layers[il].wo, model.layers[il].bo,
  11093. Qcur, Kcur, Vcur, kq_b, nullptr, nullptr, 1.0f, il);
  11094. cb(cur, "kqv_out", il);
  11095. }
  11096. cur = ggml_add(ctx0, cur, inpSA);
  11097. cb(cur, "cross_inp", il);
  11098. ggml_tensor * inpCA = cur;
  11099. // norm
  11100. cur = build_norm(cur,
  11101. model.layers[il].attn_norm_cross, NULL,
  11102. LLM_NORM_RMS, il);
  11103. cb(cur, "attn_norm_cross", il);
  11104. // cross-attention
  11105. {
  11106. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq_cross, cur);
  11107. cb(Qcur, "Qcur", il);
  11108. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk_cross, embd_enc);
  11109. cb(Kcur, "Kcur", il);
  11110. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv_cross, embd_enc);
  11111. cb(Vcur, "Vcur", il);
  11112. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  11113. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_outputs_enc);
  11114. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_outputs_enc);
  11115. cur = build_attn(inp_attn_cross,
  11116. model.layers[il].wo_cross, nullptr,
  11117. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f, il);
  11118. cb(cur, "kqv_out", il);
  11119. //ggml_tensor * q = ggml_permute(ctx0, Qcur, 0, 2, 1, 3);
  11120. //ggml_tensor * k = ggml_cont(ctx0, ggml_permute(ctx0, Kcur, 0, 2, 1, 3));
  11121. //ggml_tensor * kq = ggml_mul_mat(ctx0, k, q);
  11122. //cb(kq, "kq", il);
  11123. //kq = ggml_soft_max_ext(ctx0, kq, KQ_mask_cross, 1.0f, hparams.f_max_alibi_bias);
  11124. //cb(kq, "kq_soft_max_ext", il);
  11125. //ggml_tensor * v = ggml_cont(ctx0, ggml_transpose(ctx0, ggml_reshape_2d(ctx0, Vcur, n_embd_gqa, n_outputs_enc)));
  11126. //cb(v, "v", il);
  11127. //ggml_tensor * kqv = ggml_mul_mat(ctx0, ggml_reshape_3d(ctx0, v, n_outputs_enc, n_embd_head, n_head_kv), kq);
  11128. //cb(kqv, "kqv", il);
  11129. //ggml_tensor * kqv_merged = ggml_permute(ctx0, kqv, 0, 2, 1, 3);
  11130. //cb(kqv_merged, "kqv_merged", il);
  11131. //cur = ggml_cont_2d(ctx0, kqv_merged, n_embd_gqa, n_tokens);
  11132. //cb(cur, "kqv_merged_cont", il);
  11133. //ggml_build_forward_expand(gf, cur);
  11134. //cur = build_lora_mm(model.layers[il].wo_cross, cur);
  11135. //cb(cur, "kqv_out", il);
  11136. }
  11137. if (il == dec_n_layer - 1 && inp_out_ids) {
  11138. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  11139. inpCA = ggml_get_rows(ctx0, inpCA, inp_out_ids);
  11140. }
  11141. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpCA);
  11142. cb(ffn_inp, "ffn_inp", il);
  11143. // feed-forward network
  11144. {
  11145. cur = build_norm(ffn_inp,
  11146. model.layers[il].ffn_norm, NULL,
  11147. LLM_NORM_RMS, il);
  11148. cb(cur, "ffn_norm", il);
  11149. // T5 uses relu, flan-T5 uses gelu-gated
  11150. cur = build_ffn(cur,
  11151. model.layers[il].ffn_up, NULL, NULL,
  11152. model.layers[il].ffn_gate, NULL, NULL,
  11153. model.layers[il].ffn_down, NULL, NULL,
  11154. NULL,
  11155. model.layers[il].ffn_gate ? LLM_FFN_GELU : LLM_FFN_RELU,
  11156. model.layers[il].ffn_gate ? LLM_FFN_PAR : LLM_FFN_SEQ,
  11157. il);
  11158. cb(cur, "ffn_out", il);
  11159. }
  11160. cur = ggml_add(ctx0, cur, ffn_inp);
  11161. cb(cur, "ffn_out", il);
  11162. cur = build_cvec(cur, il);
  11163. cb(cur, "l_out", il);
  11164. // input for next layer
  11165. inpL = cur;
  11166. }
  11167. cur = inpL;
  11168. cb(cur, "result_embd", -1);
  11169. cur = build_norm(cur,
  11170. model.output_norm, NULL,
  11171. LLM_NORM_RMS, -1);
  11172. cb(cur, "result_norm", -1);
  11173. res->t_embd = cur;
  11174. // lm_head
  11175. cur = build_lora_mm(model.output, cur);
  11176. cb(cur, "result_output", -1);
  11177. res->t_logits = cur;
  11178. ggml_build_forward_expand(gf, cur);
  11179. }
  11180. };
  11181. struct llm_build_jais : public llm_graph_context {
  11182. llm_build_jais(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  11183. const int64_t n_embd_head = hparams.n_embd_head_v;
  11184. const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  11185. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  11186. ggml_tensor * cur;
  11187. ggml_tensor * inpL;
  11188. inpL = build_inp_embd(model.tok_embd);
  11189. auto * inp_attn = build_attn_inp_kv();
  11190. ggml_tensor * inp_out_ids = build_inp_out_ids();
  11191. for (int il = 0; il < n_layer; ++il) {
  11192. cur = build_norm(inpL,
  11193. model.layers[il].attn_norm,
  11194. model.layers[il].attn_norm_b,
  11195. LLM_NORM, il);
  11196. cb(cur, "attn_norm", il);
  11197. // self-attention
  11198. {
  11199. cur = build_lora_mm(model.layers[il].wqkv, cur);
  11200. cb(cur, "wqkv", il);
  11201. cur = ggml_add(ctx0, cur, model.layers[il].bqkv);
  11202. cb(cur, "bqkv", il);
  11203. ggml_tensor * Qcur = ggml_view_3d(ctx0, cur, n_embd_head, n_head, n_tokens, n_embd_head*sizeof(float), cur->nb[1], 0*cur->nb[0]*(n_embd));
  11204. 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*cur->nb[0]*(n_embd));
  11205. ggml_tensor * Vcur = ggml_view_3d(ctx0, cur, n_embd_head, n_head_kv, n_tokens, n_embd_head*sizeof(float), cur->nb[1], 1*cur->nb[0]*(n_embd + n_embd_gqa));
  11206. cb(Qcur, "Qcur", il);
  11207. cb(Kcur, "Kcur", il);
  11208. cb(Vcur, "Vcur", il);
  11209. cur = build_attn(inp_attn,
  11210. model.layers[il].wo, model.layers[il].bo,
  11211. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/float(n_embd_head), il);
  11212. }
  11213. if (il == n_layer - 1 && inp_out_ids) {
  11214. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  11215. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  11216. }
  11217. // add the input
  11218. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
  11219. cb(ffn_inp, "ffn_inp", il);
  11220. // FF
  11221. {
  11222. cur = build_norm(ffn_inp,
  11223. model.layers[il].ffn_norm,
  11224. model.layers[il].ffn_norm_b,
  11225. LLM_NORM, il);
  11226. cb(cur, "ffn_norm", il);
  11227. cur = build_ffn(cur,
  11228. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  11229. model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, NULL,
  11230. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  11231. NULL,
  11232. LLM_FFN_SILU, LLM_FFN_PAR, il);
  11233. cb(cur, "ffn_out", il);
  11234. }
  11235. inpL = ggml_add(ctx0, cur, ffn_inp);
  11236. cb(inpL, "l_out", il);
  11237. }
  11238. cur = build_norm(inpL,
  11239. model.output_norm,
  11240. model.output_norm_b,
  11241. LLM_NORM, -1);
  11242. cb(cur, "result_norm", -1);
  11243. res->t_embd = cur;
  11244. cur = build_lora_mm(model.output, cur);
  11245. cb(cur, "result_output", -1);
  11246. res->t_logits = cur;
  11247. ggml_build_forward_expand(gf, cur);
  11248. }
  11249. };
  11250. struct llm_build_chatglm : public llm_graph_context {
  11251. llm_build_chatglm(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  11252. const int64_t n_embd_head = hparams.n_embd_head_v;
  11253. const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  11254. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  11255. ggml_tensor * cur;
  11256. ggml_tensor * inpL;
  11257. inpL = build_inp_embd(model.tok_embd);
  11258. // inp_pos - contains the positions
  11259. ggml_tensor * inp_pos = build_inp_pos();
  11260. auto * inp_attn = build_attn_inp_kv();
  11261. ggml_tensor * inp_out_ids = build_inp_out_ids();
  11262. for (int il = 0; il < n_layer; ++il) {
  11263. ggml_tensor * inpSA = inpL;
  11264. cur = build_norm(inpL,
  11265. model.layers[il].attn_norm,
  11266. NULL,
  11267. LLM_NORM_RMS, il);
  11268. cb(cur, "attn_norm", il);
  11269. // self-attention
  11270. {
  11271. ggml_tensor * Qcur = nullptr;
  11272. ggml_tensor * Kcur = nullptr;
  11273. ggml_tensor * Vcur = nullptr;
  11274. if (model.layers[il].wqkv == nullptr) {
  11275. Qcur = build_lora_mm(model.layers[il].wq, cur);
  11276. if (model.layers[il].bq) {
  11277. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  11278. }
  11279. Kcur = build_lora_mm(model.layers[il].wk, cur);
  11280. if (model.layers[il].bk) {
  11281. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  11282. }
  11283. Vcur = build_lora_mm(model.layers[il].wv, cur);
  11284. if (model.layers[il].bv) {
  11285. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  11286. }
  11287. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  11288. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  11289. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  11290. } else {
  11291. cur = build_lora_mm(model.layers[il].wqkv, cur);
  11292. cb(cur, "wqkv", il);
  11293. if (model.layers[il].bqkv) {
  11294. cur = ggml_add(ctx0, cur, model.layers[il].bqkv);
  11295. cb(cur, "bqkv", il);
  11296. }
  11297. 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));
  11298. 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));
  11299. Vcur = 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 + n_embd_gqa));
  11300. }
  11301. //printf("freq_base: %f freq_scale: %f ext_factor: %f attn_factor: %f\n", freq_base, freq_scale, ext_factor, attn_factor);
  11302. Qcur = ggml_rope_ext(
  11303. ctx0, Qcur, inp_pos, nullptr,
  11304. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  11305. ext_factor, attn_factor, beta_fast, beta_slow
  11306. );
  11307. Kcur = ggml_rope_ext(
  11308. ctx0, Kcur, inp_pos, nullptr,
  11309. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  11310. ext_factor, attn_factor, beta_fast, beta_slow
  11311. );
  11312. cb(Qcur, "Qcur", il);
  11313. cb(Kcur, "Kcur", il);
  11314. cb(Vcur, "Vcur", il);
  11315. cur = build_attn(inp_attn,
  11316. model.layers[il].wo, NULL,
  11317. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  11318. }
  11319. if (il == n_layer - 1 && inp_out_ids) {
  11320. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  11321. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  11322. }
  11323. // Add the input
  11324. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  11325. cb(ffn_inp, "ffn_inp", il);
  11326. // FF
  11327. {
  11328. cur = build_norm(ffn_inp,
  11329. model.layers[il].ffn_norm,
  11330. NULL,
  11331. LLM_NORM_RMS, il);
  11332. cb(cur, "ffn_norm", il);
  11333. cur = build_ffn(cur,
  11334. model.layers[il].ffn_up, NULL, NULL,
  11335. NULL, NULL, NULL,
  11336. model.layers[il].ffn_down, NULL, NULL,
  11337. NULL,
  11338. LLM_FFN_SWIGLU, LLM_FFN_SEQ, il);
  11339. cb(cur, "ffn_out", il);
  11340. }
  11341. inpL = ggml_add(ctx0, cur, ffn_inp);
  11342. cb(inpL, "l_out", il);
  11343. }
  11344. cur = build_norm(inpL,
  11345. model.output_norm,
  11346. NULL,
  11347. LLM_NORM_RMS, -1);
  11348. cb(cur, "result_norm", -1);
  11349. res->t_embd = cur;
  11350. cur = build_lora_mm(model.output, cur);
  11351. cb(cur, "result_output", -1);
  11352. res->t_logits = cur;
  11353. ggml_build_forward_expand(gf, cur);
  11354. }
  11355. };
  11356. struct llm_build_glm4 : public llm_graph_context {
  11357. llm_build_glm4(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  11358. const int64_t n_embd_head = hparams.n_embd_head_v;
  11359. const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  11360. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  11361. ggml_tensor * cur;
  11362. ggml_tensor * inpL;
  11363. inpL = build_inp_embd(model.tok_embd);
  11364. // inp_pos - contains the positions
  11365. ggml_tensor * inp_pos = build_inp_pos();
  11366. auto * inp_attn = build_attn_inp_kv();
  11367. ggml_tensor * inp_out_ids = build_inp_out_ids();
  11368. for (int il = 0; il < n_layer; ++il) {
  11369. ggml_tensor * inpSA = inpL;
  11370. // Pre-attention norm
  11371. cur = build_norm(inpL,
  11372. model.layers[il].attn_norm,
  11373. NULL,
  11374. LLM_NORM_RMS, il);
  11375. cb(cur, "attn_norm", il);
  11376. // self-attention
  11377. {
  11378. ggml_tensor * Qcur = nullptr;
  11379. ggml_tensor * Kcur = nullptr;
  11380. ggml_tensor * Vcur = nullptr;
  11381. if (model.layers[il].wqkv == nullptr) {
  11382. Qcur = build_lora_mm(model.layers[il].wq, cur);
  11383. if (model.layers[il].bq) {
  11384. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  11385. }
  11386. Kcur = build_lora_mm(model.layers[il].wk, cur);
  11387. if (model.layers[il].bk) {
  11388. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  11389. }
  11390. Vcur = build_lora_mm(model.layers[il].wv, cur);
  11391. if (model.layers[il].bv) {
  11392. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  11393. }
  11394. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  11395. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  11396. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  11397. } else {
  11398. cur = build_lora_mm(model.layers[il].wqkv, cur);
  11399. cb(cur, "wqkv", il);
  11400. if (model.layers[il].bqkv) {
  11401. cur = ggml_add(ctx0, cur, model.layers[il].bqkv);
  11402. cb(cur, "bqkv", il);
  11403. }
  11404. 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));
  11405. 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));
  11406. Vcur = 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 + n_embd_gqa));
  11407. }
  11408. Qcur = ggml_rope_ext(
  11409. ctx0, Qcur, inp_pos, nullptr,
  11410. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  11411. ext_factor, attn_factor, beta_fast, beta_slow
  11412. );
  11413. Kcur = ggml_rope_ext(
  11414. ctx0, Kcur, inp_pos, nullptr,
  11415. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  11416. ext_factor, attn_factor, beta_fast, beta_slow
  11417. );
  11418. cb(Qcur, "Qcur", il);
  11419. cb(Kcur, "Kcur", il);
  11420. cb(Vcur, "Vcur", il);
  11421. cur = build_attn(inp_attn,
  11422. model.layers[il].wo, NULL,
  11423. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  11424. }
  11425. if (il == n_layer - 1 && inp_out_ids) {
  11426. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  11427. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  11428. }
  11429. // Post-attention norm (new!)
  11430. cur = build_norm(cur,
  11431. model.layers[il].attn_post_norm,
  11432. NULL,
  11433. LLM_NORM_RMS, il);
  11434. cb(cur, "post_attn_norm", il);
  11435. // Add the input (residual connection after post-attention norm)
  11436. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  11437. cb(ffn_inp, "ffn_inp", il);
  11438. // FF
  11439. {
  11440. // Pre-MLP norm
  11441. cur = build_norm(ffn_inp,
  11442. model.layers[il].ffn_norm,
  11443. NULL,
  11444. LLM_NORM_RMS, il);
  11445. cb(cur, "ffn_norm", il);
  11446. // MLP
  11447. cur = build_ffn(cur,
  11448. model.layers[il].ffn_up, NULL, NULL,
  11449. NULL, NULL, NULL,
  11450. model.layers[il].ffn_down, NULL, NULL,
  11451. NULL,
  11452. LLM_FFN_SWIGLU, LLM_FFN_SEQ, il);
  11453. cb(cur, "ffn_out", il);
  11454. // Post-MLP norm
  11455. cur = build_norm(cur,
  11456. model.layers[il].ffn_post_norm,
  11457. NULL,
  11458. LLM_NORM_RMS, il);
  11459. cb(cur, "post_mlp_norm", il);
  11460. }
  11461. // Add residual connection after post-MLP norm
  11462. inpL = ggml_add(ctx0, cur, ffn_inp);
  11463. cb(inpL, "l_out", il);
  11464. }
  11465. // Final norm
  11466. cur = build_norm(inpL,
  11467. model.output_norm,
  11468. NULL,
  11469. LLM_NORM_RMS, -1);
  11470. cb(cur, "result_norm", -1);
  11471. res->t_embd = cur;
  11472. // Output projection
  11473. cur = build_lora_mm(model.output, cur);
  11474. cb(cur, "result_output", -1);
  11475. res->t_logits = cur;
  11476. ggml_build_forward_expand(gf, cur);
  11477. }
  11478. };
  11479. struct llm_build_glm4_moe : public llm_graph_context {
  11480. llm_build_glm4_moe(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  11481. const int64_t n_embd_head = hparams.n_embd_head_v;
  11482. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  11483. ggml_tensor * cur;
  11484. ggml_tensor * inpL;
  11485. inpL = build_inp_embd(model.tok_embd);
  11486. // inp_pos - contains the positions
  11487. ggml_tensor * inp_pos = build_inp_pos();
  11488. auto * inp_attn = build_attn_inp_kv();
  11489. ggml_tensor * inp_out_ids = build_inp_out_ids();
  11490. // Only process up to last layer (skip final NextN layer)
  11491. // Final layer tensors are loaded but not processed in forward pass
  11492. const int n_transformer_layers = n_layer - hparams.nextn_predict_layers;
  11493. for (int il = 0; il < n_transformer_layers; ++il) {
  11494. ggml_tensor * inpSA = inpL;
  11495. // Pre-attention norm
  11496. cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
  11497. cb(cur, "attn_norm", il);
  11498. // self-attention
  11499. {
  11500. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  11501. if (model.layers[il].bq) {
  11502. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  11503. }
  11504. cb(Qcur, "Qcur", il);
  11505. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  11506. if (model.layers[il].bk) {
  11507. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  11508. }
  11509. cb(Kcur, "Kcur", il);
  11510. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  11511. if (model.layers[il].bv) {
  11512. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  11513. }
  11514. cb(Vcur, "Vcur", il);
  11515. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  11516. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  11517. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  11518. // Apply Q/K norm if available (GLM-4.5 355B variant)
  11519. if (model.layers[il].attn_q_norm) {
  11520. Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);
  11521. cb(Qcur, "Qcur_normed", il);
  11522. }
  11523. if (model.layers[il].attn_k_norm) {
  11524. Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);
  11525. cb(Kcur, "Kcur_normed", il);
  11526. }
  11527. Qcur = ggml_rope_ext(
  11528. ctx0, Qcur, inp_pos, nullptr,
  11529. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  11530. ext_factor, attn_factor, beta_fast, beta_slow
  11531. );
  11532. Kcur = ggml_rope_ext(
  11533. ctx0, Kcur, inp_pos, nullptr,
  11534. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  11535. ext_factor, attn_factor, beta_fast, beta_slow
  11536. );
  11537. cb(Qcur, "Qcur", il);
  11538. cb(Kcur, "Kcur", il);
  11539. cb(Vcur, "Vcur", il);
  11540. cur = build_attn(inp_attn,
  11541. model.layers[il].wo, NULL,
  11542. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  11543. }
  11544. if (il == n_transformer_layers - 1 && inp_out_ids) {
  11545. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  11546. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  11547. }
  11548. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  11549. cb(ffn_inp, "ffn_inp", il);
  11550. // Post-attention norm
  11551. cur = build_norm(ffn_inp, model.layers[il].attn_post_norm, NULL, LLM_NORM_RMS, il);
  11552. cb(cur, "post_attn_norm", il);
  11553. // Check if this is a dense layer (n_layer_dense_lead=1, so layer 0 is dense)
  11554. if (static_cast<uint32_t>(il) < hparams.n_layer_dense_lead) {
  11555. // Dense FFN layer
  11556. cur = build_ffn(cur,
  11557. model.layers[il].ffn_up, NULL, NULL,
  11558. model.layers[il].ffn_gate, NULL, NULL,
  11559. model.layers[il].ffn_down, NULL, NULL,
  11560. NULL,
  11561. LLM_FFN_SILU, LLM_FFN_PAR, il);
  11562. cb(cur, "ffn_out", il);
  11563. } else {
  11564. // Process routed experts using existing MoE infrastructure
  11565. ggml_tensor * routed_out = build_moe_ffn(cur,
  11566. model.layers[il].ffn_gate_inp,
  11567. model.layers[il].ffn_up_exps,
  11568. model.layers[il].ffn_gate_exps,
  11569. model.layers[il].ffn_down_exps,
  11570. model.layers[il].ffn_exp_probs_b,
  11571. n_expert, n_expert_used,
  11572. LLM_FFN_SILU, hparams.expert_weights_norm,
  11573. true, hparams.expert_weights_scale,
  11574. (llama_expert_gating_func_type) hparams.expert_gating_func,
  11575. il);
  11576. cb(routed_out, "ffn_moe_out", il);
  11577. // Process shared expert on original input
  11578. ggml_tensor * shared_out = build_ffn(cur,
  11579. model.layers[il].ffn_up_shexp, NULL, NULL,
  11580. model.layers[il].ffn_gate_shexp, NULL, NULL,
  11581. model.layers[il].ffn_down_shexp, NULL, NULL,
  11582. NULL,
  11583. LLM_FFN_SILU, LLM_FFN_PAR, il);
  11584. cb(shared_out, "ffn_shexp_out", il);
  11585. // Final output: routed_output + shared_output
  11586. cur = ggml_add(ctx0, routed_out, shared_out);
  11587. cb(cur, "ffn_out", il);
  11588. }
  11589. cur = ggml_add(ctx0, cur, ffn_inp);
  11590. cur = build_cvec(cur, il);
  11591. cb(cur, "l_out", il);
  11592. // input for next layer
  11593. inpL = cur;
  11594. }
  11595. cur = inpL;
  11596. cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1);
  11597. cb(cur, "result_norm", -1);
  11598. res->t_embd = cur;
  11599. // lm_head
  11600. cur = build_lora_mm(model.output, cur);
  11601. cb(cur, "result_output", -1);
  11602. res->t_logits = cur;
  11603. ggml_build_forward_expand(gf, cur);
  11604. }
  11605. };
  11606. struct llm_build_nemotron : public llm_graph_context {
  11607. llm_build_nemotron(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  11608. const int64_t n_embd_head = hparams.n_embd_head_v;
  11609. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  11610. //GGML_ASSERT(n_embd_head == hparams.n_rot);
  11611. ggml_tensor * cur;
  11612. ggml_tensor * inpL;
  11613. inpL = build_inp_embd(model.tok_embd);
  11614. // inp_pos - contains the positions
  11615. ggml_tensor * inp_pos = build_inp_pos();
  11616. auto * inp_attn = build_attn_inp_kv();
  11617. ggml_tensor * inp_out_ids = build_inp_out_ids();
  11618. for (int il = 0; il < n_layer; ++il) {
  11619. ggml_tensor * inpSA = inpL;
  11620. // norm
  11621. cur = build_norm(inpL,
  11622. model.layers[il].attn_norm,
  11623. model.layers[il].attn_norm_b,
  11624. LLM_NORM, il);
  11625. cb(cur, "attn_norm", il);
  11626. // self-attention
  11627. {
  11628. // compute Q and K and RoPE them
  11629. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  11630. cb(Qcur, "Qcur", il);
  11631. if (model.layers[il].bq) {
  11632. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  11633. cb(Qcur, "Qcur", il);
  11634. }
  11635. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  11636. cb(Kcur, "Kcur", il);
  11637. if (model.layers[il].bk) {
  11638. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  11639. cb(Kcur, "Kcur", il);
  11640. }
  11641. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  11642. cb(Vcur, "Vcur", il);
  11643. if (model.layers[il].bv) {
  11644. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  11645. cb(Vcur, "Vcur", il);
  11646. }
  11647. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  11648. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  11649. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  11650. Qcur = ggml_rope_ext(
  11651. ctx0, Qcur, inp_pos, nullptr,
  11652. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  11653. ext_factor, attn_factor, beta_fast, beta_slow
  11654. );
  11655. Kcur = ggml_rope_ext(
  11656. ctx0, Kcur, inp_pos, nullptr,
  11657. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  11658. ext_factor, attn_factor, beta_fast, beta_slow
  11659. );
  11660. cb(Qcur, "Qcur", il);
  11661. cb(Kcur, "Kcur", il);
  11662. cb(Vcur, "Vcur", il);
  11663. cur = build_attn(inp_attn,
  11664. model.layers[il].wo, model.layers[il].bo,
  11665. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  11666. }
  11667. if (il == n_layer - 1 && inp_out_ids) {
  11668. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  11669. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  11670. }
  11671. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  11672. cb(ffn_inp, "ffn_inp", il);
  11673. // feed-forward network
  11674. cur = build_norm(ffn_inp,
  11675. model.layers[il].ffn_norm,
  11676. model.layers[il].ffn_norm_b,
  11677. LLM_NORM, il);
  11678. cb(cur, "ffn_norm", il);
  11679. cur = build_ffn(cur,
  11680. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  11681. NULL, NULL, NULL,
  11682. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  11683. NULL,
  11684. LLM_FFN_RELU_SQR, LLM_FFN_SEQ, il);
  11685. cur = ggml_add(ctx0, cur, ffn_inp);
  11686. cb(cur, "ffn_out", il);
  11687. cur = build_cvec(cur, il);
  11688. cb(cur, "l_out", il);
  11689. // input for next layer
  11690. inpL = cur;
  11691. }
  11692. cur = inpL;
  11693. cur = build_norm(cur,
  11694. model.output_norm, model.output_norm_b,
  11695. LLM_NORM, -1);
  11696. cb(cur, "result_norm", -1);
  11697. res->t_embd = cur;
  11698. // lm_head
  11699. cur = build_lora_mm(model.output, cur);
  11700. cb(cur, "result_output", -1);
  11701. res->t_logits = cur;
  11702. ggml_build_forward_expand(gf, cur);
  11703. }
  11704. };
  11705. struct llm_build_nemotron_h : public llm_graph_context_mamba {
  11706. llm_build_nemotron_h(
  11707. const llama_model & model,
  11708. const llm_graph_params & params) :
  11709. llm_graph_context_mamba(params) {
  11710. const int64_t n_embd_head = hparams.n_embd_head_v;
  11711. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  11712. ggml_tensor * cur;
  11713. ggml_tensor * inpL;
  11714. inpL = build_inp_embd(model.tok_embd);
  11715. auto * inp = build_inp_mem_hybrid();
  11716. ggml_tensor * inp_out_ids = build_inp_out_ids();
  11717. for (int il = 0; il < n_layer; ++il) {
  11718. struct ggml_tensor * inpSA = inpL;
  11719. // norm
  11720. cur = build_norm(inpL,
  11721. model.layers[il].attn_norm, NULL,
  11722. LLM_NORM_RMS, il);
  11723. cb(cur, "attn_norm", il);
  11724. if (hparams.is_recurrent(il)) {
  11725. // ssm layer //
  11726. cur = build_mamba2_layer(inp->get_recr(), cur, model, ubatch, il);
  11727. } else if (hparams.n_ff(il) == 0) {
  11728. // attention layer //
  11729. cur = build_attention_layer(cur, inp->get_attn(), model, n_embd_head, il);
  11730. } else {
  11731. cur = build_ffn_layer(cur, model, il);
  11732. }
  11733. if (il == n_layer - 1 && inp_out_ids) {
  11734. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  11735. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  11736. }
  11737. // add residual
  11738. cur = ggml_add(ctx0, cur, inpSA);
  11739. cb(cur, "block_out", il);
  11740. // input for next layer
  11741. inpL = cur;
  11742. }
  11743. cur = inpL;
  11744. cur = build_norm(cur,
  11745. model.output_norm, NULL,
  11746. LLM_NORM_RMS, -1);
  11747. cb(cur, "result_norm", -1);
  11748. res->t_embd = cur;
  11749. // lm_head
  11750. cur = build_lora_mm(model.output, cur);
  11751. cb(cur, "result_output", -1);
  11752. res->t_logits = cur;
  11753. ggml_build_forward_expand(gf, cur);
  11754. }
  11755. ggml_tensor * build_attention_layer(
  11756. ggml_tensor * cur,
  11757. llm_graph_input_attn_kv * inp_attn,
  11758. const llama_model & model,
  11759. const int64_t n_embd_head,
  11760. const int il) {
  11761. // compute Q and K and (optionally) RoPE them
  11762. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  11763. cb(Qcur, "Qcur", il);
  11764. if (model.layers[il].bq) {
  11765. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  11766. cb(Qcur, "Qcur", il);
  11767. }
  11768. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  11769. cb(Kcur, "Kcur", il);
  11770. if (model.layers[il].bk) {
  11771. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  11772. cb(Kcur, "Kcur", il);
  11773. }
  11774. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  11775. cb(Vcur, "Vcur", il);
  11776. if (model.layers[il].bv) {
  11777. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  11778. cb(Vcur, "Vcur", il);
  11779. }
  11780. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, hparams.n_head(il), n_tokens);
  11781. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, hparams.n_head_kv(il), n_tokens);
  11782. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, hparams.n_head_kv(il), n_tokens);
  11783. cb(Qcur, "Qcur", il);
  11784. cb(Kcur, "Kcur", il);
  11785. cb(Vcur, "Vcur", il);
  11786. const float kq_scale = hparams.f_attention_scale == 0.0f ? 1.0f/sqrtf(float(n_embd_head)) : hparams.f_attention_scale;
  11787. cur = build_attn(inp_attn,
  11788. model.layers[il].wo, model.layers[il].bo,
  11789. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
  11790. cb(cur, "attn_out", il);
  11791. return cur;
  11792. }
  11793. ggml_tensor * build_ffn_layer(
  11794. ggml_tensor * cur,
  11795. const llama_model & model,
  11796. const int il) {
  11797. cur = build_ffn(cur,
  11798. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  11799. NULL, NULL, NULL,
  11800. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  11801. NULL,
  11802. LLM_FFN_RELU_SQR, LLM_FFN_PAR, il);
  11803. cb(cur, "ffn_out", il);
  11804. cur = build_cvec(cur, il);
  11805. cb(cur, "l_out", il);
  11806. return cur;
  11807. }
  11808. };
  11809. struct llm_build_exaone : public llm_graph_context {
  11810. llm_build_exaone(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  11811. const int64_t n_embd_head = hparams.n_embd_head_v;
  11812. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  11813. GGML_ASSERT(n_embd_head == hparams.n_rot);
  11814. ggml_tensor * cur;
  11815. ggml_tensor * inpL;
  11816. inpL = build_inp_embd(model.tok_embd);
  11817. // inp_pos - contains the positions
  11818. ggml_tensor * inp_pos = build_inp_pos();
  11819. auto * inp_attn = build_attn_inp_kv();
  11820. ggml_tensor * inp_out_ids = build_inp_out_ids();
  11821. for (int il = 0; il < n_layer; ++il) {
  11822. ggml_tensor * inpSA = inpL;
  11823. // norm
  11824. cur = build_norm(inpL,
  11825. model.layers[il].attn_norm, NULL,
  11826. LLM_NORM_RMS, il);
  11827. cb(cur, "attn_norm", il);
  11828. // self-attention
  11829. {
  11830. // rope freq factors for llama3; may return nullptr for llama2 and other models
  11831. ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
  11832. // compute Q and K and RoPE them
  11833. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  11834. cb(Qcur, "Qcur", il);
  11835. if (model.layers[il].bq) {
  11836. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  11837. cb(Qcur, "Qcur", il);
  11838. }
  11839. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  11840. cb(Kcur, "Kcur", il);
  11841. if (model.layers[il].bk) {
  11842. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  11843. cb(Kcur, "Kcur", il);
  11844. }
  11845. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  11846. cb(Vcur, "Vcur", il);
  11847. if (model.layers[il].bv) {
  11848. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  11849. cb(Vcur, "Vcur", il);
  11850. }
  11851. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  11852. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  11853. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  11854. Qcur = ggml_rope_ext(
  11855. ctx0, Qcur, inp_pos, rope_factors,
  11856. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  11857. ext_factor, attn_factor, beta_fast, beta_slow
  11858. );
  11859. Kcur = ggml_rope_ext(
  11860. ctx0, Kcur, inp_pos, rope_factors,
  11861. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  11862. ext_factor, attn_factor, beta_fast, beta_slow
  11863. );
  11864. cb(Qcur, "Qcur", il);
  11865. cb(Kcur, "Kcur", il);
  11866. cb(Vcur, "Vcur", il);
  11867. cur = build_attn(inp_attn,
  11868. model.layers[il].wo, model.layers[il].bo,
  11869. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  11870. }
  11871. if (il == n_layer - 1 && inp_out_ids) {
  11872. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  11873. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  11874. }
  11875. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  11876. cb(ffn_inp, "ffn_inp", il);
  11877. // feed-forward network
  11878. cur = build_norm(ffn_inp,
  11879. model.layers[il].ffn_norm, NULL,
  11880. LLM_NORM_RMS, il);
  11881. cb(cur, "ffn_norm", il);
  11882. cur = build_ffn(cur,
  11883. model.layers[il].ffn_up, NULL, NULL,
  11884. model.layers[il].ffn_gate, NULL, NULL,
  11885. model.layers[il].ffn_down, NULL, NULL,
  11886. NULL,
  11887. LLM_FFN_SILU, LLM_FFN_PAR, il);
  11888. cb(cur, "ffn_out", il);
  11889. cur = ggml_add(ctx0, cur, ffn_inp);
  11890. cb(cur, "ffn_out", il);
  11891. cur = build_cvec(cur, il);
  11892. cb(cur, "l_out", il);
  11893. // input for next layer
  11894. inpL = cur;
  11895. }
  11896. cur = inpL;
  11897. cur = build_norm(cur,
  11898. model.output_norm, NULL,
  11899. LLM_NORM_RMS, -1);
  11900. cb(cur, "result_norm", -1);
  11901. res->t_embd = cur;
  11902. // lm_head
  11903. cur = build_lora_mm(model.output, cur);
  11904. cb(cur, "result_output", -1);
  11905. res->t_logits = cur;
  11906. ggml_build_forward_expand(gf, cur);
  11907. }
  11908. };
  11909. template <bool iswa>
  11910. struct llm_build_exaone4 : public llm_graph_context {
  11911. llm_build_exaone4(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  11912. const int64_t n_embd_head = hparams.n_embd_head_k;
  11913. GGML_ASSERT(n_embd_head == hparams.n_embd_head_v);
  11914. GGML_ASSERT(n_embd_head == hparams.n_rot);
  11915. ggml_tensor * cur;
  11916. ggml_tensor * inpL;
  11917. inpL = build_inp_embd(model.tok_embd);
  11918. // inp_pos - contains the positions
  11919. ggml_tensor * inp_pos = build_inp_pos();
  11920. using inp_attn_type = std::conditional_t<iswa, llm_graph_input_attn_kv_iswa, llm_graph_input_attn_kv>;
  11921. inp_attn_type * inp_attn = nullptr;
  11922. if constexpr (iswa) {
  11923. inp_attn = build_attn_inp_kv_iswa();
  11924. } else {
  11925. inp_attn = build_attn_inp_kv();
  11926. }
  11927. ggml_tensor * inp_out_ids = build_inp_out_ids();
  11928. for (int il = 0; il < n_layer; ++il) {
  11929. ggml_tensor * inpSA = inpL;
  11930. // use RoPE for SWA layers or non-SWA models
  11931. const bool use_rope = hparams.is_swa(il) || hparams.swa_type == LLAMA_SWA_TYPE_NONE;
  11932. cur = inpL;
  11933. // self-attention
  11934. {
  11935. ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
  11936. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  11937. cb(Qcur, "Qcur", il);
  11938. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  11939. cb(Kcur, "Kcur", il);
  11940. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  11941. cb(Vcur, "Vcur", il);
  11942. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  11943. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  11944. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  11945. Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);
  11946. Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);
  11947. cb(Qcur, "Qcur_normed", il);
  11948. cb(Kcur, "Kcur_normed", il);
  11949. if (use_rope) {
  11950. Qcur = ggml_rope_ext(
  11951. ctx0, Qcur, inp_pos, rope_factors,
  11952. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  11953. ext_factor, attn_factor, beta_fast, beta_slow
  11954. );
  11955. Kcur = ggml_rope_ext(
  11956. ctx0, Kcur, inp_pos, rope_factors,
  11957. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  11958. ext_factor, attn_factor, beta_fast, beta_slow
  11959. );
  11960. }
  11961. cb(Qcur, "Qcur", il);
  11962. cb(Kcur, "Kcur", il);
  11963. cb(Vcur, "Vcur", il);
  11964. cur = build_attn(inp_attn,
  11965. model.layers[il].wo, NULL,
  11966. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  11967. cb(cur, "attn_out", il);
  11968. }
  11969. if (il == n_layer - 1 && inp_out_ids) {
  11970. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  11971. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  11972. }
  11973. cur = build_norm(cur,
  11974. model.layers[il].attn_post_norm, NULL,
  11975. LLM_NORM_RMS, il);
  11976. cb(cur, "attn_post_norm", il);
  11977. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  11978. cb(ffn_inp, "ffn_inp", il);
  11979. // feed-forward network
  11980. cur = build_ffn(ffn_inp,
  11981. model.layers[il].ffn_up, NULL, NULL,
  11982. model.layers[il].ffn_gate, NULL, NULL,
  11983. model.layers[il].ffn_down, NULL, NULL,
  11984. NULL,
  11985. LLM_FFN_SILU, LLM_FFN_PAR, il);
  11986. cb(cur, "ffn_out", il);
  11987. cur = build_norm(cur,
  11988. model.layers[il].ffn_post_norm, NULL,
  11989. LLM_NORM_RMS, -1);
  11990. cb(cur, "ffn_post_norm", -1);
  11991. cur = ggml_add(ctx0, cur, ffn_inp);
  11992. cur = build_cvec(cur, il);
  11993. cb(cur, "l_out", il);
  11994. // input for next layer
  11995. inpL = cur;
  11996. }
  11997. cur = inpL;
  11998. cur = build_norm(cur,
  11999. model.output_norm, NULL,
  12000. LLM_NORM_RMS, -1);
  12001. cb(cur, "result_norm", -1);
  12002. res->t_embd = cur;
  12003. // lm_head
  12004. cur = build_lora_mm(model.output, cur);
  12005. cb(cur, "result_output", -1);
  12006. res->t_logits = cur;
  12007. ggml_build_forward_expand(gf, cur);
  12008. }
  12009. };
  12010. struct llm_build_rwkv6_base : public llm_graph_context {
  12011. const llama_model & model;
  12012. llm_build_rwkv6_base(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params), model(model) {
  12013. }
  12014. ggml_tensor * build_rwkv6_channel_mix(
  12015. const llama_layer * layer,
  12016. ggml_tensor * cur,
  12017. ggml_tensor * x_prev,
  12018. llm_arch arch) const {
  12019. ggml_tensor * sx = ggml_sub(ctx0, x_prev, cur);
  12020. switch (arch) {
  12021. case LLM_ARCH_RWKV6:
  12022. {
  12023. ggml_tensor * xk = ggml_add(ctx0, ggml_mul(ctx0, sx, layer->channel_mix_lerp_k), cur);
  12024. ggml_tensor * xr = ggml_add(ctx0, ggml_mul(ctx0, sx, layer->channel_mix_lerp_r), cur);
  12025. ggml_tensor * r = ggml_sigmoid(ctx0, build_lora_mm(layer->channel_mix_receptance, xr));
  12026. ggml_tensor * k = ggml_sqr(
  12027. ctx0,
  12028. ggml_relu(
  12029. ctx0,
  12030. build_lora_mm(layer->channel_mix_key, xk)
  12031. )
  12032. );
  12033. cur = ggml_mul(ctx0, r, build_lora_mm(layer->channel_mix_value, k));
  12034. } break;
  12035. default:
  12036. GGML_ABORT("fatal error");
  12037. }
  12038. return cur;
  12039. }
  12040. ggml_tensor * build_rwkv6_time_mix(
  12041. llm_graph_input_rs * inp,
  12042. ggml_tensor * cur,
  12043. ggml_tensor * x_prev,
  12044. const llama_ubatch & ubatch,
  12045. int il) const {
  12046. const auto * mctx_cur = static_cast<const llama_memory_recurrent_context *>(mctx);
  12047. const auto n_tokens = ubatch.n_tokens;
  12048. const auto n_seqs = ubatch.n_seqs;
  12049. const auto n_seq_tokens = ubatch.n_seq_tokens;
  12050. const auto n_embd = hparams.n_embd;
  12051. const auto head_size = hparams.wkv_head_size;
  12052. const auto n_head = n_embd / head_size;
  12053. const auto n_head_kv = hparams.n_head_kv(il);
  12054. const auto kv_head = mctx_cur->get_head();
  12055. const auto & layer = model.layers[il];
  12056. bool is_qrwkv = layer.time_mix_first == nullptr;
  12057. ggml_tensor * sx = ggml_sub(ctx0, x_prev, cur);
  12058. sx = ggml_reshape_2d(ctx0, sx, n_embd, n_tokens);
  12059. cur = ggml_reshape_2d(ctx0, cur, n_embd, n_tokens);
  12060. ggml_tensor * xxx = ggml_add(ctx0, ggml_mul(ctx0, sx, layer.time_mix_lerp_x), cur);
  12061. xxx = ggml_reshape_4d(
  12062. ctx0,
  12063. ggml_tanh(
  12064. ctx0,
  12065. ggml_mul_mat(ctx0, layer.time_mix_w1, xxx)
  12066. ),
  12067. layer.time_mix_w1->ne[1] / 5, 1, 5, n_tokens
  12068. );
  12069. xxx = ggml_cont(ctx0, ggml_permute(ctx0, xxx, 0, 1, 3, 2));
  12070. xxx = ggml_mul_mat(
  12071. ctx0,
  12072. ggml_reshape_4d(
  12073. ctx0,
  12074. layer.time_mix_w2,
  12075. layer.time_mix_w2->ne[0], layer.time_mix_w2->ne[1], 1, 5
  12076. ),
  12077. xxx
  12078. );
  12079. ggml_tensor *xw, *xk, *xv, *xr, *xg;
  12080. if (layer.time_mix_lerp_fused) {
  12081. // fusing these weights makes some performance improvement
  12082. sx = ggml_reshape_3d(ctx0, sx, n_embd, 1, n_tokens);
  12083. cur = ggml_reshape_3d(ctx0, cur, n_embd, 1, n_tokens);
  12084. xxx = ggml_add(ctx0, ggml_mul(ctx0, ggml_add(ctx0, xxx, layer.time_mix_lerp_fused), sx), cur);
  12085. xw = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], 0);
  12086. xk = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], n_embd * n_tokens * sizeof(float));
  12087. xv = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], n_embd * n_tokens * 2 * sizeof(float));
  12088. xr = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], n_embd * n_tokens * 3 * sizeof(float));
  12089. xg = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], n_embd * n_tokens * 4 * sizeof(float));
  12090. } else {
  12091. // for backward compatibility
  12092. xw = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], 0);
  12093. xk = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], n_embd * n_tokens * sizeof(float));
  12094. xv = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], n_embd * n_tokens * 2 * sizeof(float));
  12095. xr = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], n_embd * n_tokens * 3 * sizeof(float));
  12096. xg = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], n_embd * n_tokens * 4 * sizeof(float));
  12097. xw = ggml_add(ctx0, ggml_mul(ctx0, ggml_add(ctx0, xw, layer.time_mix_lerp_w), sx), cur);
  12098. xk = ggml_add(ctx0, ggml_mul(ctx0, ggml_add(ctx0, xk, layer.time_mix_lerp_k), sx), cur);
  12099. xv = ggml_add(ctx0, ggml_mul(ctx0, ggml_add(ctx0, xv, layer.time_mix_lerp_v), sx), cur);
  12100. xr = ggml_add(ctx0, ggml_mul(ctx0, ggml_add(ctx0, xr, layer.time_mix_lerp_r), sx), cur);
  12101. xg = ggml_add(ctx0, ggml_mul(ctx0, ggml_add(ctx0, xg, layer.time_mix_lerp_g), sx), cur);
  12102. }
  12103. ggml_tensor * r = build_lora_mm(layer.time_mix_receptance, xr);
  12104. ggml_tensor * k = build_lora_mm(layer.time_mix_key, xk);
  12105. ggml_tensor * v = build_lora_mm(layer.time_mix_value, xv);
  12106. if (layer.time_mix_receptance_b) {
  12107. r = ggml_add(ctx0, r, layer.time_mix_receptance_b);
  12108. }
  12109. if (layer.time_mix_key_b) {
  12110. k = ggml_add(ctx0, k, layer.time_mix_key_b);
  12111. }
  12112. if (layer.time_mix_value_b) {
  12113. v = ggml_add(ctx0, v, layer.time_mix_value_b);
  12114. }
  12115. ggml_tensor * g = build_lora_mm(layer.time_mix_gate, xg);
  12116. if (is_qrwkv) {
  12117. g = ggml_sigmoid(ctx0, g);
  12118. } else {
  12119. g = ggml_silu(ctx0, g);
  12120. }
  12121. if (n_head_kv != 0 && n_head_kv != n_head) {
  12122. GGML_ASSERT(n_head % n_head_kv == 0);
  12123. k = ggml_reshape_4d(ctx0, k, head_size, 1, n_head_kv, n_tokens);
  12124. v = ggml_reshape_4d(ctx0, v, head_size, 1, n_head_kv, n_tokens);
  12125. ggml_tensor * tmp = ggml_new_tensor_4d(ctx0, GGML_TYPE_F32, head_size, n_head / n_head_kv, n_head_kv, n_tokens);
  12126. k = ggml_repeat(ctx0, k, tmp);
  12127. v = ggml_repeat(ctx0, v, tmp);
  12128. }
  12129. k = ggml_reshape_3d(ctx0, k, head_size, n_head, n_tokens);
  12130. v = ggml_reshape_3d(ctx0, v, head_size, n_head, n_tokens);
  12131. r = ggml_reshape_3d(ctx0, r, head_size, n_head, n_tokens);
  12132. ggml_tensor * w = ggml_mul_mat(
  12133. ctx0,
  12134. layer.time_mix_decay_w2,
  12135. ggml_tanh(
  12136. ctx0,
  12137. ggml_mul_mat(ctx0, layer.time_mix_decay_w1, xw)
  12138. )
  12139. );
  12140. w = ggml_add(ctx0, w, layer.time_mix_decay);
  12141. w = ggml_exp(ctx0, ggml_neg(ctx0, ggml_exp(ctx0, w)));
  12142. w = ggml_reshape_3d(ctx0, w, head_size, n_head, n_tokens);
  12143. if (is_qrwkv) {
  12144. // k = k * (1 - w)
  12145. k = ggml_sub(ctx0, k, ggml_mul(ctx0, k, w));
  12146. }
  12147. ggml_tensor * wkv_state = build_rs(
  12148. inp, mctx_cur->get_s_l(il),
  12149. hparams.n_embd_s(), n_seqs);
  12150. ggml_tensor * wkv_output;
  12151. if (is_qrwkv) {
  12152. wkv_output = ggml_gated_linear_attn(ctx0, k, v, r, w, wkv_state, pow(head_size, -0.5f));
  12153. } else {
  12154. wkv_output = ggml_rwkv_wkv6(ctx0, k, v, r, layer.time_mix_first, w, wkv_state);
  12155. }
  12156. cur = ggml_view_1d(ctx0, wkv_output, n_embd * n_tokens, 0);
  12157. wkv_state = ggml_view_1d(ctx0, wkv_output, n_embd * head_size * n_seqs, n_embd * n_tokens * sizeof(float));
  12158. ggml_build_forward_expand(
  12159. gf,
  12160. ggml_cpy(
  12161. ctx0,
  12162. wkv_state,
  12163. ggml_view_1d(
  12164. ctx0,
  12165. mctx_cur->get_s_l(il),
  12166. hparams.n_embd_s() * n_seqs,
  12167. hparams.n_embd_s() * kv_head * ggml_element_size(mctx_cur->get_s_l(il))
  12168. )
  12169. )
  12170. );
  12171. if (!is_qrwkv) {
  12172. // group norm with head_count groups
  12173. cur = ggml_reshape_3d(ctx0, cur, n_embd / n_head, n_head, n_tokens);
  12174. cur = ggml_norm(ctx0, cur, 64e-5f);
  12175. // Convert back to regular vectors.
  12176. cur = ggml_reshape_2d(ctx0, cur, n_embd, n_tokens);
  12177. cur = ggml_add(ctx0, ggml_mul(ctx0, cur, layer.time_mix_ln), layer.time_mix_ln_b);
  12178. } else {
  12179. cur = ggml_reshape_2d(ctx0, cur, n_embd, n_tokens);
  12180. }
  12181. cur = ggml_mul(ctx0, cur, g);
  12182. cur = build_lora_mm(layer.time_mix_output, cur);
  12183. return ggml_reshape_3d(ctx0, cur, n_embd, n_seq_tokens, n_seqs);
  12184. }
  12185. };
  12186. struct llm_build_rwkv6 : public llm_build_rwkv6_base {
  12187. llm_build_rwkv6(const llama_model & model, const llm_graph_params & params) : llm_build_rwkv6_base(model, params) {
  12188. GGML_ASSERT(hparams.token_shift_count == 2);
  12189. ggml_tensor * cur;
  12190. ggml_tensor * inpL;
  12191. inpL = build_inp_embd(model.tok_embd);
  12192. inpL = build_norm(inpL, model.tok_norm, model.tok_norm_b, LLM_NORM, -1);
  12193. auto * rs_inp = build_rs_inp();
  12194. const auto n_embd = hparams.n_embd;
  12195. const auto n_seq_tokens = ubatch.n_seq_tokens;
  12196. const auto n_seqs = ubatch.n_seqs;
  12197. ggml_tensor * inp_out_ids = build_inp_out_ids();
  12198. for (int il = 0; il < n_layer; ++il) {
  12199. const llama_layer * layer = &model.layers[il];
  12200. inpL = ggml_reshape_3d(ctx0, inpL, n_embd, n_seq_tokens, n_seqs);
  12201. ggml_tensor * token_shift = build_rwkv_token_shift_load(rs_inp, ubatch, il);
  12202. ggml_tensor * att_shift = ggml_view_3d(ctx0, token_shift, n_embd, 1, n_seqs, token_shift->nb[1], token_shift->nb[2], 0);
  12203. 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));
  12204. ggml_tensor * att_norm = build_norm(inpL, layer->attn_norm, layer->attn_norm_b, LLM_NORM, il);
  12205. cb(att_norm, "attn_norm", il);
  12206. ggml_tensor * x_prev = ggml_concat(
  12207. ctx0,
  12208. att_shift,
  12209. ggml_view_3d(ctx0, att_norm, n_embd, n_seq_tokens - 1, n_seqs, att_norm->nb[1], att_norm->nb[2], 0),
  12210. 1
  12211. );
  12212. cur = build_rwkv6_time_mix(rs_inp, att_norm, x_prev, ubatch, il);
  12213. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
  12214. cb(ffn_inp, "ffn_inp", il);
  12215. ggml_tensor * ffn_norm = build_norm(ffn_inp, layer->attn_norm_2, layer->attn_norm_2_b, LLM_NORM, il);
  12216. cb(ffn_norm, "ffn_norm", il);
  12217. x_prev = ggml_concat(
  12218. ctx0,
  12219. ffn_shift,
  12220. ggml_view_3d(ctx0, ffn_norm, n_embd, n_seq_tokens - 1, n_seqs, ffn_norm->nb[1], ffn_norm->nb[2], 0),
  12221. 1
  12222. );
  12223. token_shift = ggml_concat(ctx0,
  12224. 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)),
  12225. 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)),
  12226. 1
  12227. );
  12228. ggml_build_forward_expand(gf, build_rwkv_token_shift_store(token_shift, ubatch, il));
  12229. ffn_inp = ggml_reshape_2d(ctx0, ffn_inp, n_embd, n_tokens);
  12230. ffn_norm = ggml_reshape_2d(ctx0, ffn_norm, n_embd, n_tokens);
  12231. x_prev = ggml_reshape_2d(ctx0, x_prev, n_embd, n_tokens);
  12232. cur = ggml_reshape_2d(ctx0, cur, n_embd, n_tokens);
  12233. if (il == n_layer - 1 && inp_out_ids) {
  12234. ffn_inp = ggml_get_rows(ctx0, ffn_inp, inp_out_ids);
  12235. ffn_norm = ggml_get_rows(ctx0, ffn_norm, inp_out_ids);
  12236. x_prev = ggml_get_rows(ctx0, x_prev, inp_out_ids);
  12237. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  12238. }
  12239. cur = build_rwkv6_channel_mix(layer, ffn_norm, x_prev, LLM_ARCH_RWKV6);
  12240. cur = ggml_add(ctx0, cur, ffn_inp);
  12241. if (hparams.rescale_every_n_layers != 0 && (il + 1) % hparams.rescale_every_n_layers == 0) {
  12242. cur = ggml_scale(ctx0, cur, 0.5F);
  12243. }
  12244. cur = build_cvec(cur, il);
  12245. cb(cur, "l_out", il);
  12246. // input for next layer
  12247. inpL = cur;
  12248. }
  12249. cur = inpL;
  12250. cur = build_norm(cur, model.output_norm, model.output_norm_b, LLM_NORM, -1);
  12251. cb(cur, "result_norm", -1);
  12252. res->t_embd = cur;
  12253. cur = build_lora_mm(model.output, cur);
  12254. cb(cur, "result_output", -1);
  12255. res->t_logits = cur;
  12256. ggml_build_forward_expand(gf, cur);
  12257. }
  12258. };
  12259. // ref: https://huggingface.co/recursal/QRWKV6-32B-Instruct-Preview-v0.1/blob/main/modeling_rwkv6qwen2.py
  12260. struct llm_build_rwkv6qwen2 : public llm_build_rwkv6_base {
  12261. llm_build_rwkv6qwen2(const llama_model & model, const llm_graph_params & params) : llm_build_rwkv6_base(model, params) {
  12262. GGML_ASSERT(n_embd == hparams.n_embd_r());
  12263. ggml_tensor * cur;
  12264. ggml_tensor * inpL;
  12265. inpL = build_inp_embd(model.tok_embd);
  12266. auto * rs_inp = build_rs_inp();
  12267. const auto n_embd = hparams.n_embd;
  12268. const auto n_seq_tokens = ubatch.n_seq_tokens;
  12269. const auto n_seqs = ubatch.n_seqs;
  12270. ggml_tensor * inp_out_ids = build_inp_out_ids();
  12271. for (int il = 0; il < n_layer; ++il) {
  12272. const llama_layer * layer = &model.layers[il];
  12273. inpL = ggml_reshape_3d(ctx0, inpL, n_embd, n_seq_tokens, n_seqs);
  12274. ggml_tensor * token_shift = build_rwkv_token_shift_load(rs_inp, ubatch, il);
  12275. ggml_tensor * att_norm = build_norm(inpL, layer->attn_norm, layer->attn_norm_b, LLM_NORM_RMS, il);
  12276. cb(att_norm, "attn_norm", il);
  12277. ggml_tensor * x_prev = ggml_concat(
  12278. ctx0,
  12279. token_shift,
  12280. ggml_view_3d(ctx0, att_norm, n_embd, n_seq_tokens - 1, n_seqs, att_norm->nb[1], att_norm->nb[2], 0),
  12281. 1
  12282. );
  12283. cur = build_rwkv6_time_mix(rs_inp, att_norm, x_prev, ubatch, il);
  12284. 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));
  12285. ggml_build_forward_expand(gf, build_rwkv_token_shift_store(token_shift, ubatch, il));
  12286. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
  12287. cb(ffn_inp, "ffn_inp", il);
  12288. cur = ggml_reshape_2d(ctx0, cur, n_embd, n_tokens);
  12289. ffn_inp = ggml_reshape_2d(ctx0, ffn_inp, n_embd, n_tokens);
  12290. if (il == n_layer - 1 && inp_out_ids) {
  12291. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  12292. ffn_inp = ggml_get_rows(ctx0, ffn_inp, inp_out_ids);
  12293. }
  12294. // feed-forward network
  12295. cur = build_norm(ffn_inp,
  12296. model.layers[il].ffn_norm, NULL,
  12297. LLM_NORM_RMS, il);
  12298. cb(cur, "ffn_norm", il);
  12299. cur = build_ffn(cur,
  12300. model.layers[il].ffn_up, NULL, NULL,
  12301. model.layers[il].ffn_gate, NULL, NULL,
  12302. model.layers[il].ffn_down, NULL, NULL,
  12303. NULL,
  12304. LLM_FFN_SILU, LLM_FFN_PAR, il);
  12305. cb(cur, "ffn_out", il);
  12306. cur = ggml_add(ctx0, cur, ffn_inp);
  12307. cur = build_cvec(cur, il);
  12308. cb(cur, "l_out", il);
  12309. // input for next layer
  12310. inpL = cur;
  12311. }
  12312. cur = inpL;
  12313. cur = build_norm(cur, model.output_norm, model.output_norm_b, LLM_NORM_RMS, -1);
  12314. cb(cur, "result_norm", -1);
  12315. res->t_embd = cur;
  12316. cur = build_lora_mm(model.output, cur);
  12317. cb(cur, "result_output", -1);
  12318. res->t_logits = cur;
  12319. ggml_build_forward_expand(gf, cur);
  12320. }
  12321. };
  12322. struct llm_build_rwkv7_base : public llm_graph_context {
  12323. const llama_model & model;
  12324. llm_build_rwkv7_base(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params), model(model) {
  12325. }
  12326. ggml_tensor * build_rwkv7_channel_mix(
  12327. const llama_layer * layer,
  12328. ggml_tensor * cur,
  12329. ggml_tensor * x_prev,
  12330. llm_arch arch) const {
  12331. ggml_tensor * sx = ggml_sub(ctx0, x_prev, cur);
  12332. switch (arch) {
  12333. case LLM_ARCH_RWKV7:
  12334. {
  12335. ggml_tensor * xk = ggml_add(ctx0, ggml_mul(ctx0, sx, layer->channel_mix_lerp_k), cur);
  12336. ggml_tensor * k = ggml_sqr(
  12337. ctx0,
  12338. ggml_relu(
  12339. ctx0,
  12340. build_lora_mm(layer->channel_mix_key, xk)
  12341. )
  12342. );
  12343. cur = build_lora_mm(layer->channel_mix_value, k);
  12344. } break;
  12345. default:
  12346. GGML_ABORT("fatal error");
  12347. }
  12348. return cur;
  12349. }
  12350. ggml_tensor * build_rwkv7_time_mix(
  12351. llm_graph_input_rs * inp,
  12352. ggml_tensor * cur,
  12353. ggml_tensor * x_prev,
  12354. ggml_tensor *& first_layer_value,
  12355. const llama_ubatch & ubatch,
  12356. int il) const {
  12357. const auto * mctx_cur = static_cast<const llama_memory_recurrent_context *>(mctx);
  12358. const auto n_tokens = ubatch.n_tokens;
  12359. const auto n_seqs = ubatch.n_seqs;
  12360. const auto n_embd = hparams.n_embd;
  12361. const auto head_size = hparams.wkv_head_size;
  12362. const auto head_count = n_embd / head_size;
  12363. const auto n_seq_tokens = ubatch.n_seq_tokens;
  12364. const auto kv_head = mctx_cur->get_head();
  12365. const auto & layer = model.layers[il];
  12366. bool has_gating = layer.time_mix_g1 && layer.time_mix_g2;
  12367. ggml_tensor * sx = ggml_sub(ctx0, x_prev, cur);
  12368. ggml_tensor * dummy = ggml_new_tensor_4d(ctx0, GGML_TYPE_F32, n_embd, n_seq_tokens, n_seqs, has_gating ? 6 : 5);
  12369. sx = ggml_repeat(ctx0, sx, dummy);
  12370. ggml_tensor * xxx = ggml_add(ctx0, ggml_mul(ctx0, sx, layer.time_mix_lerp_fused), cur);
  12371. ggml_tensor * xr = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], 0);
  12372. ggml_tensor * xw = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], n_embd * n_tokens * sizeof(float));
  12373. ggml_tensor * xk = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], n_embd * n_tokens * 2 * sizeof(float));
  12374. ggml_tensor * xv = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], n_embd * n_tokens * 3 * sizeof(float));
  12375. ggml_tensor * xa = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], n_embd * n_tokens * 4 * sizeof(float));
  12376. 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;
  12377. ggml_tensor * r = build_lora_mm(layer.time_mix_receptance, xr);
  12378. ggml_tensor * w = ggml_add(
  12379. ctx0,
  12380. ggml_mul_mat(ctx0, layer.time_mix_w2, ggml_tanh(ctx0, ggml_mul_mat(ctx0, layer.time_mix_w1, xw))),
  12381. layer.time_mix_w0
  12382. );
  12383. w = ggml_exp(ctx0, ggml_scale(ctx0, ggml_sigmoid(ctx0, w), -0.606531));
  12384. ggml_tensor * k = build_lora_mm(layer.time_mix_key, xk);
  12385. ggml_tensor * v = build_lora_mm(layer.time_mix_value, xv);
  12386. if (first_layer_value == nullptr) {
  12387. first_layer_value = v;
  12388. } else {
  12389. // Add the first layer value as a residual connection.
  12390. v = ggml_add(ctx0, v,
  12391. ggml_mul(ctx0,
  12392. ggml_sub(ctx0, first_layer_value, v),
  12393. ggml_sigmoid(ctx0, ggml_add(ctx0,
  12394. ggml_mul_mat(ctx0, layer.time_mix_v2, ggml_mul_mat(ctx0, layer.time_mix_v1, xv)),
  12395. layer.time_mix_v0
  12396. )
  12397. )
  12398. )
  12399. );
  12400. }
  12401. ggml_tensor * g = nullptr;
  12402. if (layer.time_mix_g1 && layer.time_mix_g2) {
  12403. g = ggml_mul_mat(ctx0, layer.time_mix_g2, ggml_sigmoid(ctx0, ggml_mul_mat(ctx0, layer.time_mix_g1, xg)));
  12404. }
  12405. ggml_tensor * a = ggml_sigmoid(ctx0,
  12406. ggml_add(
  12407. ctx0,
  12408. ggml_mul_mat(ctx0, layer.time_mix_a2, ggml_mul_mat(ctx0, layer.time_mix_a1, xa)),
  12409. layer.time_mix_a0
  12410. )
  12411. );
  12412. ggml_tensor * kk = ggml_reshape_3d(ctx0, ggml_mul(ctx0, k, layer.time_mix_k_k), head_size, head_count, n_tokens);
  12413. kk = ggml_l2_norm(ctx0, kk, 1e-12);
  12414. ggml_tensor * ka = ggml_mul(ctx0, k, layer.time_mix_k_a);
  12415. k = ggml_add(ctx0, k, ggml_sub(ctx0, ggml_mul(ctx0, a, ka), ka));
  12416. r = ggml_reshape_3d(ctx0, r, head_size, head_count, n_tokens);
  12417. w = ggml_reshape_3d(ctx0, w, head_size, head_count, n_tokens);
  12418. k = ggml_reshape_3d(ctx0, k, head_size, head_count, n_tokens);
  12419. v = ggml_reshape_3d(ctx0, v, head_size, head_count, n_tokens);
  12420. a = ggml_reshape_3d(ctx0, a, head_size, head_count, n_tokens);
  12421. ggml_tensor * wkv_state = build_rs(
  12422. inp, mctx_cur->get_s_l(il),
  12423. hparams.n_embd_s(), n_seqs);
  12424. ggml_tensor * wkv_output = ggml_rwkv_wkv7(ctx0, r, w, k, v, ggml_neg(ctx0, kk), ggml_mul(ctx0, kk, a), wkv_state);
  12425. cur = ggml_view_1d(ctx0, wkv_output, n_embd * n_tokens, 0);
  12426. wkv_state = ggml_view_1d(ctx0, wkv_output, n_embd * head_size * n_seqs, n_embd * n_tokens * sizeof(float));
  12427. ggml_build_forward_expand(
  12428. gf,
  12429. ggml_cpy(
  12430. ctx0,
  12431. wkv_state,
  12432. ggml_view_1d(
  12433. ctx0,
  12434. mctx_cur->get_s_l(il),
  12435. hparams.n_embd_s() * n_seqs,
  12436. hparams.n_embd_s() * kv_head * ggml_element_size(mctx_cur->get_s_l(il))
  12437. )
  12438. )
  12439. );
  12440. if (layer.time_mix_ln && layer.time_mix_ln_b) {
  12441. // group norm with head_count groups
  12442. cur = ggml_reshape_3d(ctx0, cur, n_embd / head_count, head_count, n_tokens);
  12443. cur = ggml_norm(ctx0, cur, 64e-5f);
  12444. // Convert back to regular vectors.
  12445. cur = ggml_reshape_2d(ctx0, cur, n_embd, n_tokens);
  12446. cur = ggml_add(ctx0, ggml_mul(ctx0, cur, layer.time_mix_ln), layer.time_mix_ln_b);
  12447. } else {
  12448. cur = ggml_reshape_2d(ctx0, cur, n_embd, n_tokens);
  12449. }
  12450. ggml_tensor * rk = ggml_sum_rows(ctx0,
  12451. ggml_mul(ctx0, ggml_mul(ctx0, k, r), ggml_reshape_2d(ctx0, layer.time_mix_r_k, head_size, head_count)));
  12452. cur = ggml_add(ctx0, cur, ggml_reshape_2d(ctx0, ggml_mul(ctx0, v, rk), n_embd, n_tokens));
  12453. if (has_gating) {
  12454. cur = ggml_mul(ctx0, cur, g);
  12455. }
  12456. cur = build_lora_mm(layer.time_mix_output, cur);
  12457. return ggml_reshape_3d(ctx0, cur, n_embd, n_seq_tokens, n_seqs);
  12458. }
  12459. };
  12460. struct llm_build_rwkv7 : public llm_build_rwkv7_base {
  12461. llm_build_rwkv7(const llama_model & model, const llm_graph_params & params) : llm_build_rwkv7_base(model, params) {
  12462. GGML_ASSERT(hparams.token_shift_count == 2);
  12463. ggml_tensor * cur;
  12464. ggml_tensor * inpL;
  12465. ggml_tensor * v_first = nullptr;
  12466. inpL = build_inp_embd(model.tok_embd);
  12467. inpL = build_norm(inpL, model.tok_norm, model.tok_norm_b, LLM_NORM, -1);
  12468. auto * rs_inp = build_rs_inp();
  12469. const auto n_embd = hparams.n_embd;
  12470. const auto n_seq_tokens = ubatch.n_seq_tokens;
  12471. const auto n_seqs = ubatch.n_seqs;
  12472. ggml_tensor * inp_out_ids = build_inp_out_ids();
  12473. for (int il = 0; il < n_layer; ++il) {
  12474. const llama_layer * layer = &model.layers[il];
  12475. inpL = ggml_reshape_3d(ctx0, inpL, n_embd, n_seq_tokens, n_seqs);
  12476. ggml_tensor * token_shift = build_rwkv_token_shift_load(rs_inp, ubatch, il);
  12477. ggml_tensor * att_shift = ggml_view_3d(ctx0, token_shift, n_embd, 1, n_seqs, token_shift->nb[1], token_shift->nb[2], 0);
  12478. 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));
  12479. ggml_tensor * att_norm = build_norm(inpL, layer->attn_norm, layer->attn_norm_b, LLM_NORM, il);
  12480. cb(att_norm, "attn_norm", il);
  12481. ggml_tensor * x_prev = ggml_concat(
  12482. ctx0,
  12483. att_shift,
  12484. ggml_view_3d(ctx0, att_norm, n_embd, n_seq_tokens - 1, n_seqs, att_norm->nb[1], att_norm->nb[2], 0),
  12485. 1
  12486. );
  12487. cur = build_rwkv7_time_mix(rs_inp, att_norm, x_prev, v_first, ubatch, il);
  12488. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
  12489. cb(ffn_inp, "ffn_inp", il);
  12490. ggml_tensor * ffn_norm = build_norm(ffn_inp, layer->attn_norm_2, layer->attn_norm_2_b, LLM_NORM, il);
  12491. cb(ffn_norm, "ffn_norm", il);
  12492. x_prev = ggml_concat(
  12493. ctx0,
  12494. ffn_shift,
  12495. ggml_view_3d(ctx0, ffn_norm, n_embd, n_seq_tokens - 1, n_seqs, ffn_norm->nb[1], ffn_norm->nb[2], 0),
  12496. 1
  12497. );
  12498. token_shift = ggml_concat(ctx0,
  12499. 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)),
  12500. 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)),
  12501. 1
  12502. );
  12503. ggml_build_forward_expand(gf, build_rwkv_token_shift_store(token_shift, ubatch, il));
  12504. ffn_inp = ggml_reshape_2d(ctx0, ffn_inp, n_embd, n_tokens);
  12505. ffn_norm = ggml_reshape_2d(ctx0, ffn_norm, n_embd, n_tokens);
  12506. x_prev = ggml_reshape_2d(ctx0, x_prev, n_embd, n_tokens);
  12507. if (il == n_layer - 1 && inp_out_ids) {
  12508. ffn_inp = ggml_get_rows(ctx0, ffn_inp, inp_out_ids);
  12509. ffn_norm = ggml_get_rows(ctx0, ffn_norm, inp_out_ids);
  12510. x_prev = ggml_get_rows(ctx0, x_prev, inp_out_ids);
  12511. }
  12512. cur = build_rwkv7_channel_mix(layer, ffn_norm, x_prev, LLM_ARCH_RWKV7);
  12513. cur = ggml_add(ctx0, cur, ffn_inp);
  12514. cur = build_cvec(cur, il);
  12515. cb(cur, "l_out", il);
  12516. // input for next layer
  12517. inpL = cur;
  12518. }
  12519. cur = inpL;
  12520. cur = build_norm(cur, model.output_norm, model.output_norm_b, LLM_NORM, -1);
  12521. cb(cur, "result_norm", -1);
  12522. res->t_embd = cur;
  12523. cur = build_lora_mm(model.output, cur);
  12524. cb(cur, "result_output", -1);
  12525. res->t_logits = cur;
  12526. ggml_build_forward_expand(gf, cur);
  12527. }
  12528. };
  12529. struct llm_build_arwkv7 : public llm_build_rwkv7_base {
  12530. llm_build_arwkv7(const llama_model & model, const llm_graph_params & params) : llm_build_rwkv7_base(model, params) {
  12531. GGML_ASSERT(n_embd == hparams.n_embd_r());
  12532. ggml_tensor * cur;
  12533. ggml_tensor * inpL;
  12534. ggml_tensor * v_first = nullptr;
  12535. inpL = build_inp_embd(model.tok_embd);
  12536. auto * rs_inp = build_rs_inp();
  12537. const auto n_embd = hparams.n_embd;
  12538. const auto n_seq_tokens = ubatch.n_seq_tokens;
  12539. const auto n_seqs = ubatch.n_seqs;
  12540. ggml_tensor * inp_out_ids = build_inp_out_ids();
  12541. for (int il = 0; il < n_layer; ++il) {
  12542. const llama_layer * layer = &model.layers[il];
  12543. inpL = ggml_reshape_3d(ctx0, inpL, n_embd, n_seq_tokens, n_seqs);
  12544. ggml_tensor * token_shift = build_rwkv_token_shift_load(rs_inp, ubatch, il);
  12545. ggml_tensor * att_norm = build_norm(inpL, layer->attn_norm, layer->attn_norm_b, LLM_NORM_RMS, il);
  12546. cb(att_norm, "attn_norm", il);
  12547. ggml_tensor * x_prev = ggml_concat(
  12548. ctx0,
  12549. token_shift,
  12550. ggml_view_3d(ctx0, att_norm, n_embd, n_seq_tokens - 1, n_seqs, att_norm->nb[1], att_norm->nb[2], 0),
  12551. 1
  12552. );
  12553. cur = build_rwkv7_time_mix(rs_inp, att_norm, x_prev, v_first, ubatch, il);
  12554. 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));
  12555. ggml_build_forward_expand(gf, build_rwkv_token_shift_store(token_shift, ubatch, il));
  12556. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
  12557. cb(ffn_inp, "ffn_inp", il);
  12558. cur = ggml_reshape_2d(ctx0, cur, n_embd, n_tokens);
  12559. ffn_inp = ggml_reshape_2d(ctx0, ffn_inp, n_embd, n_tokens);
  12560. if (il == n_layer - 1 && inp_out_ids) {
  12561. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  12562. ffn_inp = ggml_get_rows(ctx0, ffn_inp, inp_out_ids);
  12563. }
  12564. // feed-forward network
  12565. cur = build_norm(ffn_inp,
  12566. model.layers[il].ffn_norm, NULL,
  12567. LLM_NORM_RMS, il);
  12568. cb(cur, "ffn_norm", il);
  12569. cur = build_ffn(cur,
  12570. model.layers[il].ffn_up, NULL, NULL,
  12571. model.layers[il].ffn_gate, NULL, NULL,
  12572. model.layers[il].ffn_down, NULL, NULL,
  12573. NULL,
  12574. LLM_FFN_SILU, LLM_FFN_PAR, il);
  12575. cb(cur, "ffn_out", il);
  12576. cur = ggml_add(ctx0, cur, ffn_inp);
  12577. cur = build_cvec(cur, il);
  12578. cb(cur, "l_out", il);
  12579. // input for next layer
  12580. inpL = cur;
  12581. }
  12582. cur = inpL;
  12583. cur = build_norm(cur, model.output_norm, model.output_norm_b, LLM_NORM_RMS, -1);
  12584. cb(cur, "result_norm", -1);
  12585. res->t_embd = cur;
  12586. cur = build_lora_mm(model.output, cur);
  12587. cb(cur, "result_output", -1);
  12588. res->t_logits = cur;
  12589. ggml_build_forward_expand(gf, cur);
  12590. }
  12591. };
  12592. struct llm_build_granite : public llm_graph_context {
  12593. llm_build_granite(
  12594. const llama_model & model,
  12595. const llm_graph_params & params)
  12596. : llm_graph_context(params) {
  12597. const int64_t n_embd_head = hparams.n_embd_head_v;
  12598. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  12599. GGML_ASSERT(n_embd_head == hparams.n_rot);
  12600. ggml_tensor * cur;
  12601. ggml_tensor * inpL;
  12602. inpL = build_inp_embd(model.tok_embd);
  12603. // inp_pos - built only if rope enabled
  12604. ggml_tensor * inp_pos = nullptr;
  12605. if (hparams.rope_finetuned) {
  12606. inp_pos = build_inp_pos();
  12607. }
  12608. auto * inp_attn = build_attn_inp_kv();
  12609. ggml_tensor * inp_out_ids = build_inp_out_ids();
  12610. for (int il = 0; il < n_layer; ++il) {
  12611. ggml_tensor * inpSA = inpL;
  12612. // norm
  12613. cur = build_norm(inpL,
  12614. model.layers[il].attn_norm, NULL,
  12615. LLM_NORM_RMS, il);
  12616. cb(cur, "attn_norm", il);
  12617. // self-attention
  12618. cur = build_attention_layer(
  12619. cur, inp_pos, inp_attn,
  12620. model, n_embd_head, il);
  12621. if (il == n_layer - 1 && inp_out_ids) {
  12622. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  12623. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  12624. }
  12625. // ffn
  12626. cur = build_layer_ffn(cur, inpSA, model, il);
  12627. // input for next layer
  12628. inpL = cur;
  12629. }
  12630. cur = inpL;
  12631. cur = build_norm(cur,
  12632. model.output_norm, NULL,
  12633. LLM_NORM_RMS, -1);
  12634. cb(cur, "result_norm", -1);
  12635. res->t_embd = cur;
  12636. // lm_head
  12637. cur = build_lora_mm(model.output, cur);
  12638. // For Granite architectures - scale logits
  12639. cur = ggml_scale(ctx0, cur, 1.0f / hparams.f_logit_scale);
  12640. cb(cur, "result_output", -1);
  12641. res->t_logits = cur;
  12642. ggml_build_forward_expand(gf, cur);
  12643. }
  12644. ggml_tensor * build_attention_layer(
  12645. ggml_tensor * cur,
  12646. ggml_tensor * inp_pos,
  12647. llm_graph_input_attn_kv * inp_attn,
  12648. const llama_model & model,
  12649. const int64_t n_embd_head,
  12650. const int il) {
  12651. // compute Q and K and (optionally) RoPE them
  12652. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  12653. cb(Qcur, "Qcur", il);
  12654. if (model.layers[il].bq) {
  12655. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  12656. cb(Qcur, "Qcur", il);
  12657. }
  12658. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  12659. cb(Kcur, "Kcur", il);
  12660. if (model.layers[il].bk) {
  12661. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  12662. cb(Kcur, "Kcur", il);
  12663. }
  12664. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  12665. cb(Vcur, "Vcur", il);
  12666. if (model.layers[il].bv) {
  12667. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  12668. cb(Vcur, "Vcur", il);
  12669. }
  12670. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, hparams.n_head(il), n_tokens);
  12671. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, hparams.n_head_kv(il), n_tokens);
  12672. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, hparams.n_head_kv(il), n_tokens);
  12673. const bool use_rope = hparams.rope_finetuned;
  12674. if (use_rope) {
  12675. ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
  12676. Qcur = ggml_rope_ext(
  12677. ctx0, Qcur, inp_pos, rope_factors,
  12678. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  12679. ext_factor, attn_factor, beta_fast, beta_slow
  12680. );
  12681. Kcur = ggml_rope_ext(
  12682. ctx0, Kcur, inp_pos, rope_factors,
  12683. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  12684. ext_factor, attn_factor, beta_fast, beta_slow
  12685. );
  12686. }
  12687. cb(Qcur, "Qcur", il);
  12688. cb(Kcur, "Kcur", il);
  12689. cb(Vcur, "Vcur", il);
  12690. const float kq_scale = hparams.f_attention_scale == 0.0f ? 1.0f/sqrtf(float(n_embd_head)) : hparams.f_attention_scale;
  12691. cur = build_attn(inp_attn,
  12692. model.layers[il].wo, model.layers[il].bo,
  12693. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
  12694. cb(cur, "attn_out", il);
  12695. return cur;
  12696. }
  12697. ggml_tensor * build_layer_ffn(
  12698. ggml_tensor * cur,
  12699. ggml_tensor * inpSA,
  12700. const llama_model & model,
  12701. const int il) {
  12702. // For Granite architectures - scale residual
  12703. if (hparams.f_residual_scale) {
  12704. cur = ggml_scale(ctx0, cur, hparams.f_residual_scale);
  12705. }
  12706. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  12707. cb(ffn_inp, "ffn_inp", il);
  12708. // feed-forward network (non-MoE)
  12709. if (model.layers[il].ffn_gate_inp == nullptr) {
  12710. cur = build_norm(ffn_inp,
  12711. model.layers[il].ffn_norm, NULL,
  12712. LLM_NORM_RMS, il);
  12713. cb(cur, "ffn_norm", il);
  12714. cur = build_ffn(cur,
  12715. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  12716. model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, NULL,
  12717. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  12718. NULL,
  12719. LLM_FFN_SILU, LLM_FFN_PAR, il);
  12720. cb(cur, "ffn_out", il);
  12721. } else {
  12722. // MoE branch
  12723. cur = build_norm(ffn_inp,
  12724. model.layers[il].ffn_norm, NULL,
  12725. LLM_NORM_RMS, il);
  12726. cb(cur, "ffn_norm", il);
  12727. ggml_tensor * moe_out = build_moe_ffn(cur,
  12728. model.layers[il].ffn_gate_inp,
  12729. model.layers[il].ffn_up_exps,
  12730. model.layers[il].ffn_gate_exps,
  12731. model.layers[il].ffn_down_exps,
  12732. nullptr,
  12733. n_expert, n_expert_used,
  12734. LLM_FFN_SILU, true,
  12735. false, 0.0,
  12736. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  12737. il);
  12738. cb(moe_out, "ffn_moe_out", il);
  12739. // For Granite MoE Shared
  12740. if (hparams.n_ff_shexp > 0) {
  12741. ggml_tensor * ffn_shexp = build_ffn(cur,
  12742. model.layers[il].ffn_up_shexp, NULL, NULL,
  12743. model.layers[il].ffn_gate_shexp, NULL, NULL,
  12744. model.layers[il].ffn_down_shexp, NULL, NULL,
  12745. NULL,
  12746. LLM_FFN_SILU, LLM_FFN_PAR, il);
  12747. cb(ffn_shexp, "ffn_shexp", il);
  12748. cur = ggml_add(ctx0, moe_out, ffn_shexp);
  12749. cb(cur, "ffn_out", il);
  12750. } else {
  12751. cur = moe_out;
  12752. }
  12753. }
  12754. // For Granite architectures - scale residual
  12755. if (hparams.f_residual_scale) {
  12756. cur = ggml_scale(ctx0, cur, hparams.f_residual_scale);
  12757. }
  12758. cur = ggml_add(ctx0, cur, ffn_inp);
  12759. cb(cur, "ffn_out", il);
  12760. cur = build_cvec(cur, il);
  12761. cb(cur, "l_out", il);
  12762. return cur;
  12763. }
  12764. };
  12765. struct llm_build_granite_hybrid : public llm_graph_context_mamba {
  12766. llm_build_granite_hybrid(
  12767. const llama_model & model,
  12768. const llm_graph_params & params) :
  12769. llm_graph_context_mamba(params) {
  12770. const int64_t n_embd_head = hparams.n_embd_head_v;
  12771. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  12772. ggml_tensor * cur;
  12773. ggml_tensor * inpL;
  12774. inpL = build_inp_embd(model.tok_embd);
  12775. auto * inp = build_inp_mem_hybrid();
  12776. ggml_tensor * inp_out_ids = build_inp_out_ids();
  12777. // Positional embeddings populated if rope enabled
  12778. ggml_tensor * inp_pos = nullptr;
  12779. if (hparams.rope_finetuned) {
  12780. inp_pos = build_inp_pos();
  12781. }
  12782. for (int il = 0; il < n_layer; ++il) {
  12783. struct ggml_tensor * inpSA = inpL;
  12784. // norm
  12785. cur = build_norm(inpL,
  12786. model.layers[il].attn_norm, NULL,
  12787. LLM_NORM_RMS, il);
  12788. cb(cur, "attn_norm", il);
  12789. if (hparams.is_recurrent(il)) {
  12790. // ssm layer //
  12791. cur = build_mamba2_layer(inp->get_recr(), cur, model, ubatch, il);
  12792. } else {
  12793. // attention layer //
  12794. cur = build_attention_layer(
  12795. cur, inp_pos, inp->get_attn(), model,
  12796. n_embd_head, il);
  12797. }
  12798. if (il == n_layer - 1 && inp_out_ids) {
  12799. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  12800. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  12801. }
  12802. // ffn
  12803. cur = build_layer_ffn(cur, inpSA, model, il);
  12804. // input for next layer
  12805. inpL = cur;
  12806. }
  12807. cur = inpL;
  12808. cur = build_norm(cur,
  12809. model.output_norm, NULL,
  12810. LLM_NORM_RMS, -1);
  12811. cb(cur, "result_norm", -1);
  12812. res->t_embd = cur;
  12813. // lm_head
  12814. cur = build_lora_mm(model.output, cur);
  12815. // For Granite architectures - scale logits
  12816. if (hparams.f_logit_scale) {
  12817. cur = ggml_scale(ctx0, cur, 1.0f / hparams.f_logit_scale);
  12818. }
  12819. cb(cur, "result_output", -1);
  12820. res->t_logits = cur;
  12821. ggml_build_forward_expand(gf, cur);
  12822. }
  12823. ggml_tensor * build_attention_layer(
  12824. ggml_tensor * cur,
  12825. ggml_tensor * inp_pos,
  12826. llm_graph_input_attn_kv * inp_attn,
  12827. const llama_model & model,
  12828. const int64_t n_embd_head,
  12829. const int il) {
  12830. // compute Q and K and (optionally) RoPE them
  12831. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  12832. cb(Qcur, "Qcur", il);
  12833. if (model.layers[il].bq) {
  12834. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  12835. cb(Qcur, "Qcur", il);
  12836. }
  12837. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  12838. cb(Kcur, "Kcur", il);
  12839. if (model.layers[il].bk) {
  12840. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  12841. cb(Kcur, "Kcur", il);
  12842. }
  12843. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  12844. cb(Vcur, "Vcur", il);
  12845. if (model.layers[il].bv) {
  12846. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  12847. cb(Vcur, "Vcur", il);
  12848. }
  12849. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, hparams.n_head(il), n_tokens);
  12850. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, hparams.n_head_kv(il), n_tokens);
  12851. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, hparams.n_head_kv(il), n_tokens);
  12852. const bool use_rope = hparams.rope_finetuned;
  12853. if (use_rope) {
  12854. ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
  12855. Qcur = ggml_rope_ext(
  12856. ctx0, Qcur, inp_pos, rope_factors,
  12857. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  12858. ext_factor, attn_factor, beta_fast, beta_slow
  12859. );
  12860. Kcur = ggml_rope_ext(
  12861. ctx0, Kcur, inp_pos, rope_factors,
  12862. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  12863. ext_factor, attn_factor, beta_fast, beta_slow
  12864. );
  12865. }
  12866. cb(Qcur, "Qcur", il);
  12867. cb(Kcur, "Kcur", il);
  12868. cb(Vcur, "Vcur", il);
  12869. const float kq_scale = hparams.f_attention_scale == 0.0f ? 1.0f/sqrtf(float(n_embd_head)) : hparams.f_attention_scale;
  12870. cur = build_attn(inp_attn,
  12871. model.layers[il].wo, model.layers[il].bo,
  12872. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
  12873. cb(cur, "attn_out", il);
  12874. return cur;
  12875. }
  12876. ggml_tensor * build_layer_ffn(
  12877. ggml_tensor * cur,
  12878. ggml_tensor * inpSA,
  12879. const llama_model & model,
  12880. const int il) {
  12881. // For Granite architectures - scale residual
  12882. if (hparams.f_residual_scale) {
  12883. cur = ggml_scale(ctx0, cur, hparams.f_residual_scale);
  12884. }
  12885. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  12886. cb(ffn_inp, "ffn_inp", il);
  12887. // feed-forward network (non-MoE)
  12888. if (model.layers[il].ffn_gate_inp == nullptr) {
  12889. cur = build_norm(ffn_inp,
  12890. model.layers[il].ffn_norm, NULL,
  12891. LLM_NORM_RMS, il);
  12892. cb(cur, "ffn_norm", il);
  12893. cur = build_ffn(cur,
  12894. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  12895. model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, NULL,
  12896. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  12897. NULL,
  12898. LLM_FFN_SILU, LLM_FFN_PAR, il);
  12899. cb(cur, "ffn_out", il);
  12900. } else {
  12901. // MoE branch
  12902. cur = build_norm(ffn_inp,
  12903. model.layers[il].ffn_norm, NULL,
  12904. LLM_NORM_RMS, il);
  12905. cb(cur, "ffn_norm", il);
  12906. ggml_tensor * moe_out = build_moe_ffn(cur,
  12907. model.layers[il].ffn_gate_inp,
  12908. model.layers[il].ffn_up_exps,
  12909. model.layers[il].ffn_gate_exps,
  12910. model.layers[il].ffn_down_exps,
  12911. nullptr,
  12912. n_expert, n_expert_used,
  12913. LLM_FFN_SILU, true,
  12914. false, 0.0,
  12915. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  12916. il);
  12917. cb(moe_out, "ffn_moe_out", il);
  12918. // For Granite MoE Shared
  12919. if (hparams.n_ff_shexp > 0) {
  12920. ggml_tensor * ffn_shexp = build_ffn(cur,
  12921. model.layers[il].ffn_up_shexp, NULL, NULL,
  12922. model.layers[il].ffn_gate_shexp, NULL, NULL,
  12923. model.layers[il].ffn_down_shexp, NULL, NULL,
  12924. NULL,
  12925. LLM_FFN_SILU, LLM_FFN_PAR, il);
  12926. cb(ffn_shexp, "ffn_shexp", il);
  12927. cur = ggml_add(ctx0, moe_out, ffn_shexp);
  12928. cb(cur, "ffn_out", il);
  12929. } else {
  12930. cur = moe_out;
  12931. }
  12932. }
  12933. // For Granite architectures - scale residual
  12934. if (hparams.f_residual_scale) {
  12935. cur = ggml_scale(ctx0, cur, hparams.f_residual_scale);
  12936. }
  12937. cur = ggml_add(ctx0, cur, ffn_inp);
  12938. cb(cur, "ffn_out", il);
  12939. cur = build_cvec(cur, il);
  12940. cb(cur, "l_out", il);
  12941. return cur;
  12942. }
  12943. };
  12944. // ref: https://github.com/facebookresearch/chameleon
  12945. // based on the original build_llama() function, changes:
  12946. // * qk-norm
  12947. // * swin-norm
  12948. // * removed bias
  12949. // * removed MoE
  12950. struct llm_build_chameleon : public llm_graph_context {
  12951. llm_build_chameleon(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  12952. const int64_t n_embd_head = hparams.n_embd_head_v;
  12953. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  12954. GGML_ASSERT(n_embd_head == hparams.n_rot);
  12955. ggml_tensor * cur;
  12956. ggml_tensor * inpL;
  12957. inpL = build_inp_embd(model.tok_embd);
  12958. // inp_pos - contains the positions
  12959. ggml_tensor * inp_pos = build_inp_pos();
  12960. auto * inp_attn = build_attn_inp_kv();
  12961. ggml_tensor * inp_out_ids = build_inp_out_ids();
  12962. for (int il = 0; il < n_layer; ++il) {
  12963. ggml_tensor * inpSA = inpL;
  12964. // norm
  12965. if (hparams.swin_norm) {
  12966. cur = inpL;
  12967. } else {
  12968. cur = build_norm(inpL,
  12969. model.layers[il].attn_norm, NULL,
  12970. LLM_NORM_RMS, il);
  12971. cb(cur, "attn_norm", il);
  12972. }
  12973. // self-attention
  12974. {
  12975. // compute Q and K and RoPE them
  12976. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  12977. cb(Qcur, "Qcur", il);
  12978. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  12979. cb(Kcur, "Kcur", il);
  12980. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  12981. cb(Vcur, "Vcur", il);
  12982. if (model.layers[il].attn_q_norm) {
  12983. Qcur = ggml_view_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens,
  12984. ggml_element_size(Qcur) * n_embd_head,
  12985. ggml_element_size(Qcur) * n_embd_head * n_head,
  12986. 0);
  12987. cb(Qcur, "Qcur", il);
  12988. Qcur = build_norm(Qcur,
  12989. model.layers[il].attn_q_norm,
  12990. model.layers[il].attn_q_norm_b,
  12991. LLM_NORM, il);
  12992. cb(Qcur, "Qcur", il);
  12993. }
  12994. if (model.layers[il].attn_k_norm) {
  12995. Kcur = ggml_view_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens,
  12996. ggml_element_size(Kcur) * n_embd_head,
  12997. ggml_element_size(Kcur) * n_embd_head * n_head_kv,
  12998. 0);
  12999. cb(Kcur, "Kcur", il);
  13000. Kcur = build_norm(Kcur,
  13001. model.layers[il].attn_k_norm,
  13002. model.layers[il].attn_k_norm_b,
  13003. LLM_NORM, il);
  13004. cb(Kcur, "Kcur", il);
  13005. }
  13006. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  13007. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  13008. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  13009. Qcur = ggml_rope_ext(
  13010. ctx0, Qcur, inp_pos, nullptr,
  13011. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  13012. ext_factor, attn_factor, beta_fast, beta_slow
  13013. );
  13014. Kcur = ggml_rope_ext(
  13015. ctx0, Kcur, inp_pos, nullptr,
  13016. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  13017. ext_factor, attn_factor, beta_fast, beta_slow
  13018. );
  13019. cb(Qcur, "Qcur", il);
  13020. cb(Kcur, "Kcur", il);
  13021. cb(Vcur, "Vcur", il);
  13022. cur = build_attn(inp_attn,
  13023. model.layers[il].wo, nullptr,
  13024. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  13025. }
  13026. if (il == n_layer - 1 && inp_out_ids) {
  13027. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  13028. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  13029. }
  13030. if (hparams.swin_norm) {
  13031. cur = build_norm(cur,
  13032. model.layers[il].attn_norm, NULL,
  13033. LLM_NORM_RMS, il);
  13034. }
  13035. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  13036. cb(ffn_inp, "ffn_inp", il);
  13037. // feed-forward network
  13038. if (!hparams.swin_norm) {
  13039. cur = build_norm(ffn_inp,
  13040. model.layers[il].ffn_norm, NULL,
  13041. LLM_NORM_RMS, il);
  13042. cb(cur, "ffn_norm", il);
  13043. }
  13044. cur = build_ffn(cur,
  13045. model.layers[il].ffn_up, NULL, NULL,
  13046. model.layers[il].ffn_gate, NULL, NULL,
  13047. model.layers[il].ffn_down, NULL, NULL,
  13048. NULL,
  13049. LLM_FFN_SILU, LLM_FFN_PAR, il);
  13050. cb(cur, "ffn_out", il);
  13051. if (hparams.swin_norm) {
  13052. cur = build_norm(cur,
  13053. model.layers[il].ffn_norm, NULL,
  13054. LLM_NORM_RMS, il);
  13055. cb(cur, "ffn_norm", il);
  13056. }
  13057. cur = ggml_add(ctx0, cur, ffn_inp);
  13058. cb(cur, "ffn_out", il);
  13059. cur = build_cvec(cur, il);
  13060. cb(cur, "l_out", il);
  13061. // input for next layer
  13062. inpL = cur;
  13063. }
  13064. cur = inpL;
  13065. cur = build_norm(cur,
  13066. model.output_norm, NULL,
  13067. LLM_NORM_RMS, -1);
  13068. cb(cur, "result_norm", -1);
  13069. res->t_embd = cur;
  13070. // lm_head
  13071. cur = build_lora_mm(model.output, cur);
  13072. cb(cur, "result_output_with_img_logits", -1);
  13073. // TODO: this suppresses the output of image tokens, which is required to enable text-only outputs.
  13074. // Needs to be removed once image outputs are supported.
  13075. int img_token_end_idx = 8196;
  13076. int img_token_start_idx = 4;
  13077. int num_img_tokens = img_token_end_idx - img_token_start_idx;
  13078. // creates 1d tensor of size num_img_tokens and values -FLT_MAX,
  13079. // which ensures that text token values are always at least larger than image token values
  13080. ggml_tensor * img_logits = ggml_new_tensor_1d(ctx0, GGML_TYPE_F32, num_img_tokens);
  13081. img_logits = ggml_clamp(ctx0, img_logits, -FLT_MAX, -FLT_MAX);
  13082. cb(img_logits, "img_logits", -1);
  13083. cur = ggml_set_1d(ctx0, cur, img_logits, ggml_element_size(cur) * img_token_start_idx);
  13084. cb(cur, "result_output", -1);
  13085. res->t_logits = cur;
  13086. ggml_build_forward_expand(gf, cur);
  13087. }
  13088. };
  13089. struct llm_build_wavtokenizer_dec : public llm_graph_context {
  13090. llm_build_wavtokenizer_dec(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  13091. ggml_tensor * cur;
  13092. ggml_tensor * inpL;
  13093. inpL = build_inp_embd(model.tok_embd);
  13094. cur = ggml_cont(ctx0, ggml_transpose(ctx0, inpL));
  13095. cur = ggml_conv_1d_ph(ctx0, model.conv1d, cur, 1, 1);
  13096. cur = ggml_add(ctx0, cur, model.conv1d_b);
  13097. // posnet
  13098. for (uint32_t il = 0; il < hparams.posnet.n_layer; ++il) {
  13099. const auto & layer = model.layers[il].posnet;
  13100. inpL = cur;
  13101. switch (il) {
  13102. case 0:
  13103. case 1:
  13104. case 3:
  13105. case 4:
  13106. {
  13107. cur = build_norm(cur,
  13108. layer.norm1,
  13109. layer.norm1_b,
  13110. LLM_NORM_GROUP, 0);
  13111. cur = ggml_mul(ctx0, ggml_sigmoid(ctx0, cur), cur);
  13112. cur = ggml_conv_1d_ph(ctx0, layer.conv1, cur, 1, 1);
  13113. cur = ggml_add(ctx0, cur, layer.conv1_b);
  13114. cur = build_norm(cur,
  13115. layer.norm2,
  13116. layer.norm2_b,
  13117. LLM_NORM_GROUP, 0);
  13118. cur = ggml_mul(ctx0, ggml_sigmoid(ctx0, cur), cur);
  13119. cur = ggml_conv_1d_ph(ctx0, layer.conv2, cur, 1, 1);
  13120. cur = ggml_add(ctx0, cur, layer.conv2_b);
  13121. cur = ggml_add(ctx0, cur, inpL);
  13122. } break;
  13123. case 2:
  13124. {
  13125. cur = build_norm(cur,
  13126. layer.attn_norm,
  13127. layer.attn_norm_b,
  13128. LLM_NORM_GROUP, 0);
  13129. ggml_tensor * q;
  13130. ggml_tensor * k;
  13131. ggml_tensor * v;
  13132. q = ggml_conv_1d_ph(ctx0, layer.attn_q, cur, 1, 1);
  13133. k = ggml_conv_1d_ph(ctx0, layer.attn_k, cur, 1, 1);
  13134. v = ggml_conv_1d_ph(ctx0, layer.attn_v, cur, 1, 1);
  13135. q = ggml_add(ctx0, q, layer.attn_q_b);
  13136. k = ggml_add(ctx0, k, layer.attn_k_b);
  13137. v = ggml_add(ctx0, v, layer.attn_v_b);
  13138. q = ggml_cont(ctx0, ggml_transpose(ctx0, q));
  13139. k = ggml_cont(ctx0, ggml_transpose(ctx0, k));
  13140. ggml_tensor * kq = ggml_mul_mat(ctx0, k, q);
  13141. kq = ggml_soft_max_ext(ctx0, kq, nullptr, 1.0f/sqrtf(float(hparams.posnet.n_embd)), 0.0f);
  13142. cur = ggml_mul_mat(ctx0, kq, v);
  13143. cur = ggml_conv_1d_ph(ctx0, layer.attn_o, cur, 1, 1);
  13144. cur = ggml_add(ctx0, cur, layer.attn_o_b);
  13145. cur = ggml_add(ctx0, cur, inpL);
  13146. } break;
  13147. case 5:
  13148. {
  13149. cur = build_norm(cur,
  13150. layer.norm,
  13151. layer.norm_b,
  13152. LLM_NORM_GROUP, 0);
  13153. } break;
  13154. default: GGML_ABORT("unknown posnet layer");
  13155. };
  13156. }
  13157. cur = ggml_cont(ctx0, ggml_transpose(ctx0, cur));
  13158. cur = build_norm(cur,
  13159. model.tok_norm,
  13160. model.tok_norm_b,
  13161. LLM_NORM, -1);
  13162. cur = ggml_cont(ctx0, ggml_transpose(ctx0, cur));
  13163. inpL = cur;
  13164. // convnext
  13165. for (uint32_t il = 0; il < hparams.convnext.n_layer; ++il) {
  13166. const auto & layer = model.layers[il].convnext;
  13167. cur = inpL;
  13168. cur = ggml_conv_1d_dw_ph(ctx0, layer.dw, cur, 1, 1);
  13169. cur = ggml_add(ctx0, cur, layer.dw_b);
  13170. cur = ggml_cont(ctx0, ggml_transpose(ctx0, cur));
  13171. cur = build_norm(cur,
  13172. layer.norm,
  13173. layer.norm_b,
  13174. LLM_NORM, -1);
  13175. cur = build_ffn(cur,
  13176. layer.pw1, layer.pw1_b, NULL,
  13177. NULL, NULL, NULL,
  13178. layer.pw2, layer.pw2_b, NULL,
  13179. NULL,
  13180. LLM_FFN_GELU, LLM_FFN_SEQ, il);
  13181. cur = ggml_mul(ctx0, cur, layer.gamma);
  13182. cur = ggml_cont(ctx0, ggml_transpose(ctx0, cur));
  13183. inpL = ggml_add(ctx0, cur, inpL);
  13184. }
  13185. cur = inpL;
  13186. cur = ggml_cont(ctx0, ggml_transpose(ctx0, cur));
  13187. cur = build_norm(cur,
  13188. model.output_norm,
  13189. model.output_norm_b,
  13190. LLM_NORM, -1);
  13191. // lm_head
  13192. cur = build_lora_mm(model.output, cur);
  13193. cur = ggml_add(ctx0, cur, model.output_b);
  13194. cb(cur, "result_embd", -1);
  13195. res->t_embd = cur;
  13196. ggml_build_forward_expand(gf, cur);
  13197. }
  13198. };
  13199. struct llm_build_plm : public llm_graph_context {
  13200. llm_build_plm(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  13201. const float kq_scale = 1.0f/sqrtf(float(hparams.n_embd_head_k));
  13202. const uint32_t n_embd_head_qk_rope = hparams.n_rot;
  13203. const uint32_t n_embd_head_qk_nope = hparams.n_embd_head_k - hparams.n_rot;
  13204. const uint32_t kv_lora_rank = hparams.n_lora_kv;
  13205. ggml_tensor * cur;
  13206. ggml_tensor * inpL;
  13207. // {n_embd, n_tokens}
  13208. inpL = build_inp_embd(model.tok_embd);
  13209. // inp_pos - contains the positions
  13210. ggml_tensor * inp_pos = build_inp_pos();
  13211. auto * inp_attn = build_attn_inp_kv();
  13212. ggml_tensor * inp_out_ids = build_inp_out_ids();
  13213. for (int il = 0; il < n_layer; ++il) {
  13214. ggml_tensor * inpSA = inpL;
  13215. // norm
  13216. cur = build_norm(inpL,
  13217. model.layers[il].attn_norm, NULL,
  13218. LLM_NORM_RMS, il);
  13219. cb(cur, "attn_norm", il);
  13220. // self_attention
  13221. {
  13222. ggml_tensor * q = NULL;
  13223. q = ggml_mul_mat(ctx0, model.layers[il].wq, cur);
  13224. cb(q, "q", il);
  13225. // split into {n_head * n_embd_head_qk_nope, n_tokens}
  13226. ggml_tensor * q_nope = ggml_view_3d(ctx0, q, n_embd_head_qk_nope, n_head, n_tokens,
  13227. ggml_row_size(q->type, hparams.n_embd_head_k),
  13228. ggml_row_size(q->type, hparams.n_embd_head_k * n_head),
  13229. 0);
  13230. cb(q_nope, "q_nope", il);
  13231. // and {n_head * n_embd_head_qk_rope, n_tokens}
  13232. ggml_tensor * q_pe = ggml_view_3d(ctx0, q, n_embd_head_qk_rope, n_head, n_tokens,
  13233. ggml_row_size(q->type, hparams.n_embd_head_k),
  13234. ggml_row_size(q->type, hparams.n_embd_head_k * n_head),
  13235. ggml_row_size(q->type, n_embd_head_qk_nope));
  13236. cb(q_pe, "q_pe", il);
  13237. // {n_embd, kv_lora_rank + n_embd_head_qk_rope} * {n_embd, n_tokens} -> {kv_lora_rank + n_embd_head_qk_rope, n_tokens}
  13238. ggml_tensor * kv_pe_compresseed = ggml_mul_mat(ctx0, model.layers[il].wkv_a_mqa, cur);
  13239. cb(kv_pe_compresseed, "kv_pe_compresseed", il);
  13240. // split into {kv_lora_rank, n_tokens}
  13241. ggml_tensor * kv_compressed = ggml_view_2d(ctx0, kv_pe_compresseed, kv_lora_rank, n_tokens,
  13242. kv_pe_compresseed->nb[1],
  13243. 0);
  13244. cb(kv_compressed, "kv_compressed", il);
  13245. // and {n_embd_head_qk_rope, n_tokens}
  13246. ggml_tensor * k_pe = ggml_view_3d(ctx0, kv_pe_compresseed, n_embd_head_qk_rope, 1, n_tokens,
  13247. kv_pe_compresseed->nb[1],
  13248. kv_pe_compresseed->nb[1],
  13249. ggml_row_size(kv_pe_compresseed->type, kv_lora_rank));
  13250. cb(k_pe, "k_pe", il);
  13251. kv_compressed = build_norm(kv_compressed,
  13252. model.layers[il].attn_kv_a_norm, NULL,
  13253. LLM_NORM_RMS, il);
  13254. cb(kv_compressed, "kv_compressed", il);
  13255. // {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}
  13256. ggml_tensor * kv = ggml_mul_mat(ctx0, model.layers[il].wkv_b, kv_compressed);
  13257. cb(kv, "kv", il);
  13258. // split into {n_head * n_embd_head_qk_nope, n_tokens}
  13259. ggml_tensor * k_nope = ggml_view_3d(ctx0, kv, n_embd_head_qk_nope, n_head, n_tokens,
  13260. ggml_row_size(kv->type, n_embd_head_qk_nope + hparams.n_embd_head_v),
  13261. ggml_row_size(kv->type, n_head * (n_embd_head_qk_nope + hparams.n_embd_head_v)),
  13262. 0);
  13263. cb(k_nope, "k_nope", il);
  13264. // and {n_head * n_embd_head_v, n_tokens}
  13265. ggml_tensor * v_states = ggml_view_3d(ctx0, kv, hparams.n_embd_head_v, n_head, n_tokens,
  13266. ggml_row_size(kv->type, (n_embd_head_qk_nope + hparams.n_embd_head_v)),
  13267. ggml_row_size(kv->type, (n_embd_head_qk_nope + hparams.n_embd_head_v)*n_head),
  13268. ggml_row_size(kv->type, (n_embd_head_qk_nope)));
  13269. cb(v_states, "v_states", il);
  13270. v_states = ggml_cont(ctx0, v_states);
  13271. cb(v_states, "v_states", il);
  13272. v_states = ggml_view_2d(ctx0, v_states, hparams.n_embd_head_v * n_head, n_tokens,
  13273. ggml_row_size(kv->type, hparams.n_embd_head_v * n_head),
  13274. 0);
  13275. cb(v_states, "v_states", il);
  13276. q_pe = ggml_rope_ext(
  13277. ctx0, q_pe, inp_pos, nullptr,
  13278. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  13279. ext_factor, attn_factor, beta_fast, beta_slow
  13280. );
  13281. cb(q_pe, "q_pe", il);
  13282. // shared RoPE key
  13283. k_pe = ggml_rope_ext(
  13284. ctx0, k_pe, inp_pos, nullptr,
  13285. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  13286. ext_factor, attn_factor, beta_fast, beta_slow
  13287. );
  13288. cb(k_pe, "k_pe", il);
  13289. ggml_tensor * q_states = ggml_concat(ctx0, q_nope, q_pe, 0);
  13290. cb(q_states, "q_states", il);
  13291. ggml_tensor * k_states = ggml_concat(ctx0, k_nope, ggml_repeat(ctx0, k_pe, q_pe), 0);
  13292. cb(k_states, "k_states", il);
  13293. cur = build_attn(inp_attn,
  13294. model.layers[il].wo, NULL,
  13295. q_states, k_states, v_states, nullptr, nullptr, nullptr, kq_scale, il);
  13296. }
  13297. if (il == n_layer - 1 && inp_out_ids) {
  13298. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  13299. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  13300. }
  13301. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  13302. cb(ffn_inp, "ffn_inp", il);
  13303. cur = build_norm(ffn_inp,
  13304. model.layers[il].ffn_norm, NULL,
  13305. LLM_NORM_RMS, il);
  13306. cb(cur, "ffn_norm", il);
  13307. cur = build_ffn(cur,
  13308. model.layers[il].ffn_up, NULL, NULL,
  13309. NULL, NULL, NULL,
  13310. model.layers[il].ffn_down, NULL, NULL,
  13311. NULL,
  13312. LLM_FFN_RELU_SQR, LLM_FFN_SEQ, il);
  13313. cb(cur, "ffn_out", il);
  13314. cur = ggml_add(ctx0, cur, ffn_inp);
  13315. cur = build_cvec(cur, il);
  13316. cb(cur, "l_out", il);
  13317. // input for next layer
  13318. inpL = cur;
  13319. }
  13320. cur = inpL;
  13321. cur = build_norm(cur,
  13322. model.output_norm, NULL,
  13323. LLM_NORM_RMS, -1);
  13324. cb(cur, "result_norm", -1);
  13325. res->t_embd = cur;
  13326. cur = build_lora_mm(model.output, cur);
  13327. cb(cur, "result_output", -1);
  13328. res->t_logits = cur;
  13329. ggml_build_forward_expand(gf, cur);
  13330. }
  13331. };
  13332. struct llm_build_bailingmoe : public llm_graph_context {
  13333. llm_build_bailingmoe(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  13334. ggml_tensor * cur;
  13335. ggml_tensor * inpL;
  13336. inpL = build_inp_embd(model.tok_embd);
  13337. // inp_pos - contains the positions
  13338. ggml_tensor * inp_pos = build_inp_pos();
  13339. auto * inp_attn = build_attn_inp_kv();
  13340. ggml_tensor * inp_out_ids = build_inp_out_ids();
  13341. for (int il = 0; il < n_layer; ++il) {
  13342. ggml_tensor * inpSA = inpL;
  13343. // norm
  13344. cur = build_norm(inpL,
  13345. model.layers[il].attn_norm, NULL,
  13346. LLM_NORM_RMS, il);
  13347. cb(cur, "attn_norm", il);
  13348. // self-attention
  13349. {
  13350. // rope freq factors for llama3; may return nullptr for llama2 and other models
  13351. ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
  13352. // compute Q and K and RoPE them
  13353. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  13354. cb(Qcur, "Qcur", il);
  13355. if (model.layers[il].bq) {
  13356. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  13357. cb(Qcur, "Qcur", il);
  13358. }
  13359. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  13360. cb(Kcur, "Kcur", il);
  13361. if (model.layers[il].bk) {
  13362. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  13363. cb(Kcur, "Kcur", il);
  13364. }
  13365. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  13366. cb(Vcur, "Vcur", il);
  13367. if (model.layers[il].bv) {
  13368. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  13369. cb(Vcur, "Vcur", il);
  13370. }
  13371. Qcur = ggml_reshape_3d(ctx0, Qcur, n_rot, n_head, n_tokens);
  13372. Kcur = ggml_reshape_3d(ctx0, Kcur, n_rot, n_head_kv, n_tokens);
  13373. Vcur = ggml_reshape_3d(ctx0, Vcur, n_rot, n_head_kv, n_tokens);
  13374. Qcur = ggml_rope_ext(
  13375. ctx0, Qcur, inp_pos, rope_factors,
  13376. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  13377. ext_factor, attn_factor, beta_fast, beta_slow
  13378. );
  13379. Kcur = ggml_rope_ext(
  13380. ctx0, Kcur, inp_pos, rope_factors,
  13381. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  13382. ext_factor, attn_factor, beta_fast, beta_slow
  13383. );
  13384. cb(Qcur, "Qcur", il);
  13385. cb(Kcur, "Kcur", il);
  13386. cb(Vcur, "Vcur", il);
  13387. cur = build_attn(inp_attn,
  13388. model.layers[il].wo, model.layers[il].bo,
  13389. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_rot)), il);
  13390. }
  13391. if (il == n_layer - 1 && inp_out_ids) {
  13392. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  13393. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  13394. }
  13395. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  13396. cb(ffn_inp, "ffn_inp", il);
  13397. cur = build_norm(ffn_inp,
  13398. model.layers[il].ffn_norm, NULL,
  13399. LLM_NORM_RMS, il);
  13400. cb(cur, "ffn_norm", il);
  13401. ggml_tensor * moe_out =
  13402. build_moe_ffn(cur,
  13403. model.layers[il].ffn_gate_inp,
  13404. model.layers[il].ffn_up_exps,
  13405. model.layers[il].ffn_gate_exps,
  13406. model.layers[il].ffn_down_exps,
  13407. nullptr,
  13408. n_expert, n_expert_used,
  13409. LLM_FFN_SILU, hparams.expert_weights_norm,
  13410. false, hparams.expert_weights_scale,
  13411. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  13412. il);
  13413. cb(moe_out, "ffn_moe_out", il);
  13414. // FFN shared expert
  13415. {
  13416. ggml_tensor * ffn_shexp = build_ffn(cur,
  13417. model.layers[il].ffn_up_shexp, NULL, NULL,
  13418. model.layers[il].ffn_gate_shexp, NULL, NULL,
  13419. model.layers[il].ffn_down_shexp, NULL, NULL,
  13420. NULL,
  13421. LLM_FFN_SILU, LLM_FFN_PAR, il);
  13422. cb(ffn_shexp, "ffn_shexp", il);
  13423. cur = ggml_add(ctx0, moe_out, ffn_shexp);
  13424. cb(cur, "ffn_out", il);
  13425. }
  13426. cur = ggml_add(ctx0, cur, ffn_inp);
  13427. cur = build_cvec(cur, il);
  13428. cb(cur, "l_out", il);
  13429. // input for next layer
  13430. inpL = cur;
  13431. }
  13432. cur = inpL;
  13433. cur = build_norm(cur,
  13434. model.output_norm, NULL,
  13435. LLM_NORM_RMS, -1);
  13436. cb(cur, "result_norm", -1);
  13437. res->t_embd = cur;
  13438. // lm_head
  13439. cur = build_lora_mm(model.output, cur);
  13440. cb(cur, "result_output", -1);
  13441. res->t_logits = cur;
  13442. ggml_build_forward_expand(gf, cur);
  13443. }
  13444. };
  13445. struct llm_build_dots1 : public llm_graph_context {
  13446. llm_build_dots1(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  13447. const int64_t n_embd_head = hparams.n_embd_head_v;
  13448. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  13449. GGML_ASSERT(n_embd_head == hparams.n_rot);
  13450. ggml_tensor * cur;
  13451. ggml_tensor * inpL;
  13452. inpL = build_inp_embd(model.tok_embd);
  13453. // inp_pos - contains the positions
  13454. ggml_tensor * inp_pos = build_inp_pos();
  13455. auto * inp_attn = build_attn_inp_kv();
  13456. ggml_tensor * inp_out_ids = build_inp_out_ids();
  13457. for (int il = 0; il < n_layer; ++il) {
  13458. ggml_tensor * inpSA = inpL;
  13459. // norm
  13460. cur = build_norm(inpL,
  13461. model.layers[il].attn_norm, NULL,
  13462. LLM_NORM_RMS, il);
  13463. cb(cur, "attn_norm", il);
  13464. // self_attention
  13465. {
  13466. // compute Q and K and RoPE them
  13467. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  13468. cb(Qcur, "Qcur", il);
  13469. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  13470. cb(Kcur, "Kcur", il);
  13471. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  13472. cb(Vcur, "Vcur", il);
  13473. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  13474. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  13475. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  13476. Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);
  13477. cb(Qcur, "Qcur_normed", il);
  13478. Qcur = ggml_rope_ext(
  13479. ctx0, Qcur, inp_pos, nullptr,
  13480. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  13481. ext_factor, attn_factor, beta_fast, beta_slow
  13482. );
  13483. Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);
  13484. cb(Kcur, "Kcur_normed", il);
  13485. Kcur = ggml_rope_ext(
  13486. ctx0, Kcur, inp_pos, nullptr,
  13487. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  13488. ext_factor, attn_factor, beta_fast, beta_slow
  13489. );
  13490. cb(Qcur, "Qcur", il);
  13491. cb(Kcur, "Kcur", il);
  13492. cb(Vcur, "Vcur", il);
  13493. cur = build_attn(inp_attn,
  13494. model.layers[il].wo, model.layers[il].bo,
  13495. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  13496. }
  13497. if (il == n_layer - 1 && inp_out_ids) {
  13498. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  13499. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  13500. }
  13501. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  13502. cb(ffn_inp, "ffn_inp", il);
  13503. // MoE branch
  13504. cur = build_norm(ffn_inp,
  13505. model.layers[il].ffn_norm, NULL,
  13506. LLM_NORM_RMS, il);
  13507. cb(cur, "ffn_norm", il);
  13508. if ((uint32_t) il < hparams.n_layer_dense_lead) {
  13509. cur = build_ffn(cur,
  13510. model.layers[il].ffn_up, NULL, NULL,
  13511. model.layers[il].ffn_gate, NULL, NULL,
  13512. model.layers[il].ffn_down, NULL, NULL,
  13513. NULL,
  13514. LLM_FFN_SILU, LLM_FFN_PAR, il);
  13515. cb(cur, "ffn_out", il);
  13516. } else {
  13517. ggml_tensor * moe_out =
  13518. build_moe_ffn(cur,
  13519. model.layers[il].ffn_gate_inp,
  13520. model.layers[il].ffn_up_exps,
  13521. model.layers[il].ffn_gate_exps,
  13522. model.layers[il].ffn_down_exps,
  13523. model.layers[il].ffn_exp_probs_b,
  13524. n_expert, n_expert_used,
  13525. LLM_FFN_SILU, hparams.expert_weights_norm,
  13526. true, hparams.expert_weights_scale,
  13527. (llama_expert_gating_func_type) hparams.expert_gating_func,
  13528. il);
  13529. cb(moe_out, "ffn_moe_out", il);
  13530. {
  13531. ggml_tensor * ffn_shexp = build_ffn(cur,
  13532. model.layers[il].ffn_up_shexp, NULL, NULL,
  13533. model.layers[il].ffn_gate_shexp, NULL, NULL,
  13534. model.layers[il].ffn_down_shexp, NULL, NULL,
  13535. NULL,
  13536. LLM_FFN_SILU, LLM_FFN_PAR, il);
  13537. cb(ffn_shexp, "ffn_shexp", il);
  13538. cur = ggml_add(ctx0, moe_out, ffn_shexp);
  13539. cb(cur, "ffn_out", il);
  13540. }
  13541. }
  13542. cur = ggml_add(ctx0, cur, ffn_inp);
  13543. cur = build_cvec(cur, il);
  13544. cb(cur, "l_out", il);
  13545. // input for next layer
  13546. inpL = cur;
  13547. }
  13548. cur = inpL;
  13549. cur = build_norm(cur,
  13550. model.output_norm, NULL,
  13551. LLM_NORM_RMS, -1);
  13552. cb(cur, "result_norm", -1);
  13553. res->t_embd = cur;
  13554. // lm_head
  13555. cur = build_lora_mm(model.output, cur);
  13556. cb(cur, "result_output", -1);
  13557. res->t_logits = cur;
  13558. ggml_build_forward_expand(gf, cur);
  13559. }
  13560. };
  13561. struct llm_build_ernie4_5 : public llm_graph_context {
  13562. llm_build_ernie4_5(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  13563. const int64_t n_embd_head = hparams.n_embd_head_v;
  13564. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  13565. GGML_ASSERT(n_embd_head == hparams.n_rot);
  13566. ggml_tensor * cur;
  13567. ggml_tensor * inpL;
  13568. inpL = build_inp_embd(model.tok_embd);
  13569. // inp_pos - contains the positions
  13570. ggml_tensor * inp_pos = build_inp_pos();
  13571. auto * inp_attn = build_attn_inp_kv();
  13572. for (int il = 0; il < n_layer; ++il) {
  13573. ggml_tensor * inpSA = inpL;
  13574. // norm
  13575. {
  13576. cur = build_norm(inpL,
  13577. model.layers[il].attn_norm, NULL,
  13578. LLM_NORM_RMS, il);
  13579. cb(cur, "attn_norm", il);
  13580. }
  13581. // self-attention
  13582. {
  13583. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  13584. cb(Qcur, "Qcur", il);
  13585. if (model.layers[il].bq) {
  13586. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  13587. cb(Qcur, "Qcur", il);
  13588. }
  13589. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  13590. cb(Kcur, "Kcur", il);
  13591. if (model.layers[il].bk) {
  13592. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  13593. cb(Kcur, "Kcur", il);
  13594. }
  13595. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  13596. cb(Vcur, "Vcur", il);
  13597. if (model.layers[il].bv) {
  13598. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  13599. cb(Vcur, "Vcur", il);
  13600. }
  13601. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  13602. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  13603. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  13604. Qcur = ggml_rope_ext(
  13605. ctx0, Qcur, inp_pos, nullptr,
  13606. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  13607. ext_factor, attn_factor, beta_fast, beta_slow
  13608. );
  13609. Kcur = ggml_rope_ext(
  13610. ctx0, Kcur, inp_pos, nullptr,
  13611. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  13612. ext_factor, attn_factor, beta_fast, beta_slow
  13613. );
  13614. cb(Qcur, "Qcur", il);
  13615. cb(Kcur, "Kcur", il);
  13616. cb(Vcur, "Vcur", il);
  13617. cur = build_attn(inp_attn,
  13618. model.layers[il].wo, NULL,
  13619. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  13620. }
  13621. if (il == n_layer - 1) {
  13622. // skip computing output for unused tokens
  13623. ggml_tensor * inp_out_ids = build_inp_out_ids();
  13624. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  13625. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  13626. }
  13627. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  13628. cb(ffn_inp, "ffn_inp", il);
  13629. // feed-forward network
  13630. {
  13631. cur = build_norm(ffn_inp,
  13632. model.layers[il].ffn_norm, NULL,
  13633. LLM_NORM_RMS, il);
  13634. cb(cur, "ffn_norm", il);
  13635. cur = build_ffn(cur,
  13636. model.layers[il].ffn_up, NULL, NULL,
  13637. model.layers[il].ffn_gate, NULL, NULL,
  13638. model.layers[il].ffn_down, NULL, NULL,
  13639. NULL,
  13640. LLM_FFN_SILU, LLM_FFN_PAR, il);
  13641. cb(cur, "ffn_out", il);
  13642. }
  13643. cur = ggml_add(ctx0, cur, ffn_inp);
  13644. cur = build_cvec(cur, il);
  13645. cb(cur, "l_out", il);
  13646. // input for next layer
  13647. inpL = cur;
  13648. }
  13649. cur = inpL;
  13650. cur = build_norm(cur,
  13651. model.output_norm, NULL,
  13652. LLM_NORM_RMS, -1);
  13653. cb(cur, "result_norm", -1);
  13654. res->t_embd = cur;
  13655. // lm_head
  13656. cur = build_lora_mm(model.output, cur);
  13657. cb(cur, "result_output", -1);
  13658. res->t_logits = cur;
  13659. ggml_build_forward_expand(gf, cur);
  13660. }
  13661. };
  13662. struct llm_build_ernie4_5_moe : public llm_graph_context {
  13663. llm_build_ernie4_5_moe(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  13664. const int64_t n_embd_head = hparams.n_embd_head_v;
  13665. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  13666. GGML_ASSERT(n_embd_head == hparams.n_rot);
  13667. ggml_tensor * cur;
  13668. ggml_tensor * inpL;
  13669. inpL = build_inp_embd(model.tok_embd);
  13670. // inp_pos - contains the positions
  13671. ggml_tensor * inp_pos = build_inp_pos();
  13672. auto * inp_attn = build_attn_inp_kv();
  13673. ggml_tensor * inp_out_ids = build_inp_out_ids();
  13674. GGML_ASSERT(hparams.n_moe_layer_step > 0 && "Ernie 4.5 MoE requires n_moe_layer_step > 0");
  13675. for (int il = 0; il < n_layer; ++il) {
  13676. ggml_tensor * inpSA = inpL;
  13677. // norm
  13678. {
  13679. cur = build_norm(inpL,
  13680. model.layers[il].attn_norm, NULL,
  13681. LLM_NORM_RMS, il);
  13682. cb(cur, "attn_norm", il);
  13683. }
  13684. // self-attention
  13685. {
  13686. // compute Q and K and RoPE them
  13687. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  13688. cb(Qcur, "Qcur", il);
  13689. if (model.layers[il].bq) {
  13690. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  13691. cb(Qcur, "Qcur", il);
  13692. }
  13693. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  13694. cb(Kcur, "Kcur", il);
  13695. if (model.layers[il].bk) {
  13696. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  13697. cb(Kcur, "Kcur", il);
  13698. }
  13699. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  13700. cb(Vcur, "Vcur", il);
  13701. if (model.layers[il].bv) {
  13702. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  13703. cb(Vcur, "Vcur", il);
  13704. }
  13705. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  13706. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  13707. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  13708. Qcur = ggml_rope_ext(
  13709. ctx0, Qcur, inp_pos, nullptr,
  13710. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  13711. ext_factor, attn_factor, beta_fast, beta_slow
  13712. );
  13713. Kcur = ggml_rope_ext(
  13714. ctx0, Kcur, inp_pos, nullptr,
  13715. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  13716. ext_factor, attn_factor, beta_fast, beta_slow
  13717. );
  13718. cb(Qcur, "Qcur", il);
  13719. cb(Kcur, "Kcur", il);
  13720. cb(Vcur, "Vcur", il);
  13721. cur = build_attn(inp_attn,
  13722. model.layers[il].wo, NULL,
  13723. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  13724. cb(cur, "attn_out", il);
  13725. }
  13726. if (il == n_layer - 1 && inp_out_ids) {
  13727. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  13728. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  13729. }
  13730. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  13731. cb(ffn_inp, "ffn_inp", il);
  13732. // feed-forward network
  13733. bool is_moe_layer = static_cast<uint32_t>(il) >= hparams.n_layer_dense_lead && (il + 1) % hparams.n_moe_layer_step == 0;
  13734. if (!is_moe_layer) {
  13735. cur = build_norm(ffn_inp,
  13736. model.layers[il].ffn_norm, NULL,
  13737. LLM_NORM_RMS, il);
  13738. cb(cur, "ffn_norm", il);
  13739. cur = build_ffn(cur,
  13740. model.layers[il].ffn_up, NULL, NULL,
  13741. model.layers[il].ffn_gate, NULL, NULL,
  13742. model.layers[il].ffn_down, NULL, NULL,
  13743. NULL,
  13744. LLM_FFN_SILU, LLM_FFN_PAR, il);
  13745. cb(cur, "ffn_out", il);
  13746. } else {
  13747. // MoE branch
  13748. cur = build_norm(ffn_inp,
  13749. model.layers[il].ffn_norm, NULL,
  13750. LLM_NORM_RMS, il);
  13751. cb(cur, "ffn_norm", il);
  13752. ggml_tensor * moe_out = build_moe_ffn(cur,
  13753. model.layers[il].ffn_gate_inp,
  13754. model.layers[il].ffn_up_exps,
  13755. model.layers[il].ffn_gate_exps,
  13756. model.layers[il].ffn_down_exps,
  13757. model.layers[il].ffn_exp_probs_b,
  13758. n_expert, n_expert_used,
  13759. LLM_FFN_SILU, true,
  13760. false, 0.0,
  13761. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  13762. il);
  13763. cb(moe_out, "ffn_moe_out", il);
  13764. // Shared expert (if present)
  13765. if (hparams.n_ff_shexp > 0) {
  13766. ggml_tensor * ffn_shexp = build_ffn(cur,
  13767. model.layers[il].ffn_up_shexp, NULL, NULL,
  13768. model.layers[il].ffn_gate_shexp, NULL, NULL,
  13769. model.layers[il].ffn_down_shexp, NULL, NULL,
  13770. NULL,
  13771. LLM_FFN_SILU, LLM_FFN_PAR, il);
  13772. cb(ffn_shexp, "ffn_shexp", il);
  13773. cur = ggml_add(ctx0, moe_out, ffn_shexp);
  13774. } else {
  13775. cur = moe_out;
  13776. }
  13777. cb(cur, "ffn_out", il);
  13778. }
  13779. cur = ggml_add(ctx0, cur, ffn_inp);
  13780. cb(cur, "ffn_out", il);
  13781. cur = build_cvec(cur, il);
  13782. cb(cur, "l_out", il);
  13783. // input for next layer
  13784. inpL = cur;
  13785. }
  13786. cur = inpL;
  13787. cur = build_norm(cur,
  13788. model.output_norm, NULL,
  13789. LLM_NORM_RMS, -1);
  13790. cb(cur, "result_norm", -1);
  13791. res->t_embd = cur;
  13792. // lm_head
  13793. cur = build_lora_mm(model.output, cur);
  13794. cb(cur, "result_output", -1);
  13795. res->t_logits = cur;
  13796. ggml_build_forward_expand(gf, cur);
  13797. }
  13798. };
  13799. struct llm_build_falcon_h1 : public llm_graph_context_mamba {
  13800. llm_build_falcon_h1(const llama_model & model, const llm_graph_params & params) : llm_graph_context_mamba(params) {
  13801. const int64_t n_embd_head = hparams.n_embd_head_v;
  13802. ggml_tensor * cur;
  13803. ggml_tensor * inpL;
  13804. inpL = build_inp_embd(model.tok_embd);
  13805. // inp_pos - contains the positions
  13806. ggml_tensor * inp_pos = build_inp_pos();
  13807. // Build the inputs in the recurrent & kv cache
  13808. auto * inp = build_inp_mem_hybrid();
  13809. const float kq_scale = hparams.f_attention_scale == 0.0f ? 1.0f/sqrtf(float(n_embd_head)) : hparams.f_attention_scale;
  13810. ggml_tensor * inp_out_ids = build_inp_out_ids();
  13811. for (int il = 0; il < n_layer; ++il) {
  13812. ggml_tensor * inpSA = inpL;
  13813. cur = build_norm(inpL,
  13814. model.layers[il].attn_norm, NULL,
  13815. LLM_NORM_RMS, il);
  13816. cb(cur, "attn_norm", il);
  13817. // self-attention
  13818. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  13819. cb(Qcur, "Qcur", il);
  13820. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  13821. cb(Kcur, "Kcur", il);
  13822. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  13823. cb(Vcur, "Vcur", il);
  13824. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  13825. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  13826. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  13827. Qcur = ggml_rope_ext(
  13828. ctx0, Qcur, inp_pos, nullptr,
  13829. n_rot, hparams.rope_type, n_ctx_orig, freq_base, freq_scale,
  13830. ext_factor, attn_factor, beta_fast, beta_slow);
  13831. Kcur = ggml_rope_ext(
  13832. ctx0, Kcur, inp_pos, nullptr,
  13833. n_rot, hparams.rope_type, n_ctx_orig, freq_base, freq_scale,
  13834. ext_factor, attn_factor, beta_fast, beta_slow
  13835. );
  13836. cb(Qcur, "Qcur-post-rope", il);
  13837. cb(Kcur, "Kcur-post-rope", il);
  13838. cb(Vcur, "Vcur-post-rope", il);
  13839. ggml_tensor * attn_out = build_attn(inp->get_attn(),
  13840. model.layers[il].wo, NULL,
  13841. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
  13842. cb(attn_out, "attn_out", il);
  13843. cur = build_norm(inpL,
  13844. model.layers[il].attn_norm, NULL,
  13845. LLM_NORM_RMS, il);
  13846. // Mamba2 layer
  13847. cb(cur, "ssm_in", il);
  13848. ggml_tensor * ssm_out = build_mamba2_layer(inp->get_recr(), cur, model, ubatch, il);
  13849. cb(ssm_out, "ssm_out", il);
  13850. // // Aggregation
  13851. cur = ggml_add(ctx0, attn_out, ssm_out);
  13852. inpSA = ggml_add(ctx0, cur, inpSA);
  13853. cb(cur, "layer_out", il);
  13854. if (il == n_layer - 1 && inp_out_ids) {
  13855. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  13856. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  13857. }
  13858. ggml_tensor * ffn_inp = inpSA;
  13859. cb(ffn_inp, "ffn_inp", il);
  13860. // feed-forward network
  13861. cur = build_norm(ffn_inp,
  13862. model.layers[il].ffn_norm, NULL,
  13863. LLM_NORM_RMS, il);
  13864. cb(cur, "ffn_norm", il);
  13865. cur = build_ffn(cur,
  13866. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  13867. model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, NULL,
  13868. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  13869. NULL,
  13870. LLM_FFN_SILU, LLM_FFN_PAR, il);
  13871. cb(cur, "ffn_out", il);
  13872. cur = ggml_add(ctx0, cur, inpSA);
  13873. cur = build_cvec(cur, il);
  13874. cb(cur, "l_out", il);
  13875. // input for next layer
  13876. inpL = cur;
  13877. }
  13878. cur = inpL;
  13879. cur = build_norm(cur,
  13880. model.output_norm, NULL,
  13881. LLM_NORM_RMS, -1);
  13882. cb(cur, "result_norm", -1);
  13883. res->t_embd = cur;
  13884. // lm_head
  13885. cur = build_lora_mm(model.output, cur);
  13886. cb(cur, "result_output", -1);
  13887. res->t_logits = cur;
  13888. ggml_build_forward_expand(gf, cur);
  13889. }
  13890. };
  13891. struct llm_build_plamo2 : public llm_graph_context_mamba {
  13892. llm_build_plamo2(const llama_model & model, const llm_graph_params & params) : llm_graph_context_mamba(params) {
  13893. ggml_tensor * cur;
  13894. ggml_tensor * inpL;
  13895. // {n_embd, n_tokens}
  13896. inpL = build_inp_embd(model.tok_embd);
  13897. cb(inpL, "embedding_output", -1);
  13898. ggml_tensor * inp_pos = build_inp_pos();
  13899. auto * inp_hybrid = build_inp_mem_hybrid();
  13900. ggml_tensor * inp_out_ids = build_inp_out_ids();
  13901. for (int il = 0; il < n_layer; ++il) {
  13902. ggml_tensor * residual = inpL;
  13903. // ggml_graph_add_node(gf, model.layers[il].attn_norm);
  13904. // cb(model.layers[il].attn_norm, "attn_norm", il);
  13905. // pre_mixer_norm
  13906. cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
  13907. // check if this layer is Mamba or Attention
  13908. bool is_mamba_layer = hparams.is_recurrent(il);
  13909. if (is_mamba_layer) {
  13910. // PLaMo-2 Mamba layer
  13911. cur = build_plamo2_mamba_layer(inp_hybrid->get_recr(), cur, model, ubatch, il);
  13912. } else {
  13913. // PLaMo-2 Attention layer
  13914. cur = build_plamo2_attn_layer(inp_hybrid->get_attn(), inp_pos, cur, model, il);
  13915. }
  13916. // post_mixer_norm
  13917. cur = build_norm(cur, model.layers[il].attn_post_norm, NULL, LLM_NORM_RMS, il);
  13918. cb(cur, "attn_post_norm", il);
  13919. // residual connection
  13920. cur = ggml_add(ctx0, cur, residual);
  13921. cb(cur, "attn_residual", il);
  13922. residual = cur;
  13923. // pre-ffn norm
  13924. cur = build_norm(cur, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);
  13925. cb(cur, "ffn_pre_norm", il);
  13926. // feed-forward network
  13927. cur = build_ffn(cur,
  13928. model.layers[il].ffn_up, NULL, NULL,
  13929. NULL, NULL, NULL,
  13930. model.layers[il].ffn_down, NULL, NULL,
  13931. NULL,
  13932. LLM_FFN_SWIGLU, LLM_FFN_SEQ, il);
  13933. cb(cur, "ffn_out", il);
  13934. // post ffn norm
  13935. cur = build_norm(cur, model.layers[il].ffn_post_norm, NULL, LLM_NORM_RMS, il);
  13936. cb(cur, "ffn_post_norm", il);
  13937. if (il == n_layer - 1 && inp_out_ids) {
  13938. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  13939. residual = ggml_get_rows(ctx0, residual, inp_out_ids);
  13940. }
  13941. // residual connection
  13942. cur = ggml_add(ctx0, cur, residual);
  13943. cb(cur, "ffn_residual", il);
  13944. inpL = cur;
  13945. }
  13946. cur = inpL;
  13947. // final norm
  13948. cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1);
  13949. cb(cur, "result_norm", -1);
  13950. // lm_head
  13951. cur = build_lora_mm(model.output, cur);
  13952. cb(cur, "result_output", -1);
  13953. // Explicitly mark as output tensor to ensure proper backend assignment
  13954. ggml_set_output(cur);
  13955. res->t_logits = cur;
  13956. ggml_build_forward_expand(gf, cur);
  13957. }
  13958. private:
  13959. ggml_tensor * build_plamo2_attn_layer(
  13960. llm_graph_input_attn_kv * inp,
  13961. ggml_tensor * inp_pos,
  13962. ggml_tensor * cur,
  13963. const llama_model & model,
  13964. int il) {
  13965. // self-attention
  13966. {
  13967. // PLaMo-2 uses combined QKV tensor
  13968. ggml_tensor * qkv = build_lora_mm(model.layers[il].wqkv, cur);
  13969. cb(qkv, "wqkv", il);
  13970. // split QKV tensor into Q, K, V
  13971. const int64_t n_embd_head_q = hparams.n_embd_head_k;
  13972. const int64_t n_embd_head_k = hparams.n_embd_head_k;
  13973. const int64_t n_embd_head_v = hparams.n_embd_head_v;
  13974. int32_t n_head_kv = hparams.n_head_kv(il);
  13975. const int64_t q_offset = 0;
  13976. const int64_t k_offset = n_embd_head_q * n_head;
  13977. const int64_t v_offset = k_offset + n_embd_head_k * n_head_kv;
  13978. 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));
  13979. 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));
  13980. ggml_tensor * Vcur = ggml_view_3d(ctx0, qkv, n_embd_head_v, n_head_kv, n_tokens, n_embd_head_v * sizeof(float), qkv->nb[1], v_offset * ggml_element_size(qkv));
  13981. cb(Qcur, "Qcur", il);
  13982. cb(Kcur, "Kcur", il);
  13983. cb(Vcur, "Vcur", il);
  13984. Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);
  13985. cb(Qcur, "Qcur_normed", il);
  13986. Qcur = ggml_rope_ext(
  13987. ctx0, Qcur, inp_pos, nullptr,
  13988. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  13989. ext_factor, attn_factor, beta_fast, beta_slow
  13990. );
  13991. Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);
  13992. cb(Kcur, "Kcur_normed", il);
  13993. Kcur = ggml_rope_ext(
  13994. ctx0, Kcur, inp_pos, nullptr,
  13995. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  13996. ext_factor, attn_factor, beta_fast, beta_slow
  13997. );
  13998. cur = build_attn(inp,
  13999. model.layers[il].wo, NULL,
  14000. Qcur, Kcur, Vcur, NULL, NULL, NULL, 1.0f/sqrtf(float(n_embd_head_v)), il);
  14001. }
  14002. cb(cur, "attn_out", il);
  14003. return cur;
  14004. }
  14005. ggml_tensor * build_plamo2_mamba_layer(
  14006. llm_graph_input_rs * inp,
  14007. ggml_tensor * cur,
  14008. const llama_model & model,
  14009. const llama_ubatch & ubatch,
  14010. int il) {
  14011. const auto * mctx_cur = inp->mctx;
  14012. const auto kv_head = mctx_cur->get_head();
  14013. const int64_t d_conv = hparams.ssm_d_conv;
  14014. const int64_t d_inner = hparams.ssm_d_inner;
  14015. const int64_t d_state = hparams.ssm_d_state;
  14016. const int64_t n_heads = hparams.ssm_dt_rank;
  14017. const int64_t head_dim = d_inner / n_heads;
  14018. const int64_t n_group = hparams.ssm_n_group;
  14019. const int64_t n_seqs = ubatch.n_seqs;
  14020. const int64_t n_seq_tokens = ubatch.n_seq_tokens;
  14021. GGML_ASSERT(n_seqs != 0);
  14022. GGML_ASSERT(ubatch.equal_seqs());
  14023. GGML_ASSERT(ubatch.n_tokens == n_seq_tokens * n_seqs);
  14024. ggml_tensor * conv_states_all = mctx_cur->get_r_l(il);
  14025. ggml_tensor * ssm_states_all = mctx_cur->get_s_l(il);
  14026. ggml_tensor * conv = build_rs(inp, conv_states_all, hparams.n_embd_r(), n_seqs);
  14027. conv = ggml_reshape_3d(ctx0, conv, d_conv - 1, d_inner + 2*n_group*d_state, n_seqs);
  14028. // {n_embd, n_tokens} => {n_embd, n_seq_tokens, n_seqs}
  14029. cur = ggml_reshape_3d(ctx0, cur, cur->ne[0], n_seq_tokens, n_seqs);
  14030. // in_proj: {n_embd, 2*d_inner} @ {n_embd, n_seq_tokens, n_seqs} => {2*d_inner, n_seq_tokens, n_seqs}
  14031. ggml_tensor * zx = build_lora_mm(model.layers[il].ssm_in, cur);
  14032. cb(zx, "mamba_in_proj", il);
  14033. // {8192, 5, 1, 1} -> {8192, 1, 5, 1}
  14034. zx = ggml_permute(ctx0, zx, 0, 2, 1, 3);
  14035. zx = ggml_cont_4d(ctx0, zx, head_dim * 2, n_heads, n_seq_tokens, n_seqs);
  14036. cb(zx, "mamba_in_proj_out", il);
  14037. // split into z and x
  14038. // => {head_dim * n_heads, n_seq_tokens, n_seqs}
  14039. 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));
  14040. x = ggml_cont_3d(ctx0, x, head_dim * n_heads, n_seq_tokens, n_seqs);
  14041. // x = ggml_permute(ctx0, x, 0, 2, 1, 3);
  14042. cb(x, "mamba_x_split", il);
  14043. 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);
  14044. cb(z, "mamba_z_split", il);
  14045. // conv1d
  14046. {
  14047. // => {d_conv - 1 + n_seq_tokens, d_inner, n_seqs}
  14048. ggml_tensor * conv_x = ggml_concat(ctx0, conv, ggml_transpose(ctx0, x), 0);
  14049. cb(conv_x, "mamba_conv1d_input", il);
  14050. // copy last (d_conv - 1) columns back into the state cache
  14051. ggml_tensor * last_conv = ggml_view_3d(ctx0, conv_x, d_conv - 1, d_inner, n_seqs,
  14052. conv_x->nb[1], conv_x->nb[2], n_seq_tokens*(conv_x->nb[0]));
  14053. ggml_build_forward_expand(gf,
  14054. ggml_cpy(ctx0, last_conv,
  14055. ggml_view_1d(ctx0, conv_states_all,
  14056. (d_conv - 1)*(d_inner + 2*n_group*d_state)*(n_seqs),
  14057. kv_head*(d_conv - 1)*(d_inner + 2*n_group*d_state)*ggml_element_size(conv_states_all))));
  14058. cb(conv_states_all, "mamba_conv1d_state", il);
  14059. // 1D convolution
  14060. x = ggml_ssm_conv(ctx0, conv_x, model.layers[il].ssm_conv1d);
  14061. cb(x, "mamba_conv1d", il);
  14062. x = ggml_silu(ctx0, x);
  14063. cb(x, "mamba_conv1d_silu", il);
  14064. }
  14065. // SSM
  14066. {
  14067. // 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}
  14068. ggml_tensor * x_bcdt = build_lora_mm(model.layers[il].ssm_x, x);
  14069. cb(x_bcdt, "mamba_bcdt_proj", il);
  14070. // split into dt, B, C
  14071. const int64_t dt_dim = std::max(64, int(hparams.n_embd / 16));
  14072. 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);
  14073. 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);
  14074. 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));
  14075. cb(B, "mamba_B_raw", il);
  14076. cb(C, "mamba_C_raw", il);
  14077. cb(dt, "mamba_dt_raw", il);
  14078. // Apply RMS norm to dt, B, C (PLaMo-2 specific)
  14079. B = build_norm(B, model.layers[il].ssm_b_norm, NULL, LLM_NORM_RMS, il);
  14080. C = build_norm(C, model.layers[il].ssm_c_norm, NULL, LLM_NORM_RMS, il);
  14081. dt = build_norm(dt, model.layers[il].ssm_dt_norm, NULL, LLM_NORM_RMS, il);
  14082. cb(B, "mamba_B_normed", il);
  14083. cb(C, "mamba_C_normed", il);
  14084. cb(dt, "mamba_dt_normed", il);
  14085. // dt_proj: {dt_rank, d_inner} @ {dt_rank, n_seq_tokens, n_seqs} => {d_inner, n_seq_tokens, n_seqs}
  14086. dt = build_lora_mm(model.layers[il].ssm_dt, dt);
  14087. dt = ggml_add(ctx0, dt, model.layers[il].ssm_dt_b);
  14088. cb(dt, "mamba_dt_proj", il);
  14089. ggml_tensor * A = ggml_reshape_2d(ctx0, model.layers[il].ssm_a, 1, n_heads);
  14090. cb(A, "mamba_A", il);
  14091. 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);
  14092. 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);
  14093. 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);
  14094. // use the states and the indices provided by build_recurrent_state
  14095. // (this is necessary in order to properly use the states before they are overwritten,
  14096. // while avoiding to make unnecessary copies of the states)
  14097. auto get_ssm_rows = [&](ggml_context * ctx, ggml_tensor * states, ggml_tensor * ids) {
  14098. ggml_tensor * ssm = ggml_reshape_4d(ctx, states, d_state, head_dim, n_heads, mctx_cur->get_size());
  14099. // Custom operator to optimize the parallel associative scan
  14100. // as described in the Annex D of the Mamba paper.
  14101. // => {d_inner, n_seq_tokens, n_seqs} and {d_state, d_inner, n_seqs}
  14102. return ggml_ssm_scan(ctx, ssm, x, dt, A, B, C, ids);
  14103. };
  14104. ggml_tensor * y_ssm = build_rs(inp, ssm_states_all, hparams.n_embd_s(), ubatch.n_seqs, get_ssm_rows);
  14105. cb(y_ssm, "mamba_ssm_scan", il);
  14106. // store last states
  14107. ggml_build_forward_expand(gf,
  14108. ggml_cpy(ctx0,
  14109. 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)),
  14110. 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))));
  14111. cb(ssm_states_all, "mamba_ssm_states", il);
  14112. 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);
  14113. cb(y, "mamba_y_view", il);
  14114. // Add D parameter and apply gating with z
  14115. // {d_inner, n_seq_tokens, n_seqs} * {d_inner} => {d_inner, n_seq_tokens, n_seqs}
  14116. ggml_tensor * D = ggml_reshape_2d(ctx0, model.layers[il].ssm_d, 1, n_heads);
  14117. y = ggml_add(ctx0, y, ggml_mul(ctx0, x, D));
  14118. cb(y, "mamba_y_add_d", il);
  14119. y = ggml_swiglu_split(ctx0, ggml_cont(ctx0, z), y);
  14120. cb(y, "mamba_y_swiglu_z", il);
  14121. // out_proj: {d_inner, n_embd} @ {d_inner, n_seq_tokens, n_seqs} => {n_embd, n_seq_tokens, n_seqs}
  14122. y = ggml_view_3d(ctx0, y, head_dim * n_heads, n_seq_tokens, n_seqs, y->nb[2], y->nb[3], 0);
  14123. cur = build_lora_mm(model.layers[il].ssm_out, y);
  14124. cb(cur, "mamba_out_proj", il);
  14125. }
  14126. // {n_embd, n_seq_tokens, n_seqs} => {n_embd, n_tokens}
  14127. cur = ggml_reshape_2d(ctx0, cur, cur->ne[0], n_seq_tokens * n_seqs);
  14128. cb(cur, "mamba_out", il);
  14129. return cur;
  14130. }
  14131. };
  14132. struct llm_build_arcee : public llm_graph_context {
  14133. llm_build_arcee(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  14134. const int64_t n_embd_head = hparams.n_embd_head_v;
  14135. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  14136. GGML_ASSERT(n_embd_head == hparams.n_rot);
  14137. ggml_tensor * cur;
  14138. ggml_tensor * inpL;
  14139. inpL = build_inp_embd(model.tok_embd);
  14140. // inp_pos - contains the positions
  14141. ggml_tensor * inp_pos = build_inp_pos();
  14142. auto * inp_attn = build_attn_inp_kv();
  14143. const float kq_scale = hparams.f_attention_scale == 0.0f ? 1.0f/sqrtf(float(n_embd_head)) : hparams.f_attention_scale;
  14144. ggml_tensor * inp_out_ids = build_inp_out_ids();
  14145. for (int il = 0; il < n_layer; ++il) {
  14146. ggml_tensor * inpSA = inpL;
  14147. // norm
  14148. cur = build_norm(inpL,
  14149. model.layers[il].attn_norm, NULL,
  14150. LLM_NORM_RMS, il);
  14151. cb(cur, "attn_norm", il);
  14152. // self-attention
  14153. {
  14154. // rope freq factors for llama3; may return nullptr for llama2 and other models
  14155. ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
  14156. // compute Q and K and RoPE them
  14157. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  14158. cb(Qcur, "Qcur", il);
  14159. if (model.layers[il].bq) {
  14160. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  14161. cb(Qcur, "Qcur", il);
  14162. }
  14163. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  14164. cb(Kcur, "Kcur", il);
  14165. if (model.layers[il].bk) {
  14166. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  14167. cb(Kcur, "Kcur", il);
  14168. }
  14169. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  14170. cb(Vcur, "Vcur", il);
  14171. if (model.layers[il].bv) {
  14172. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  14173. cb(Vcur, "Vcur", il);
  14174. }
  14175. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  14176. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  14177. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  14178. Qcur = ggml_rope_ext(
  14179. ctx0, Qcur, inp_pos, rope_factors,
  14180. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  14181. ext_factor, attn_factor, beta_fast, beta_slow
  14182. );
  14183. Kcur = ggml_rope_ext(
  14184. ctx0, Kcur, inp_pos, rope_factors,
  14185. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  14186. ext_factor, attn_factor, beta_fast, beta_slow
  14187. );
  14188. cb(Qcur, "Qcur", il);
  14189. cb(Kcur, "Kcur", il);
  14190. cb(Vcur, "Vcur", il);
  14191. cur = build_attn(inp_attn,
  14192. model.layers[il].wo, model.layers[il].bo,
  14193. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
  14194. cb(cur, "attn_out", il);
  14195. }
  14196. if (il == n_layer - 1 && inp_out_ids) {
  14197. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  14198. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  14199. }
  14200. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  14201. cb(ffn_inp, "ffn_inp", il);
  14202. // feed-forward network
  14203. // ARCEE uses relu^2 instead of silu
  14204. cur = build_norm(ffn_inp,
  14205. model.layers[il].ffn_norm, NULL,
  14206. LLM_NORM_RMS, il);
  14207. cb(cur, "ffn_norm", il);
  14208. cur = build_ffn(cur,
  14209. model.layers[il].ffn_up, NULL, NULL,
  14210. NULL, NULL, NULL,
  14211. model.layers[il].ffn_down, NULL, NULL,
  14212. NULL,
  14213. LLM_FFN_RELU_SQR, LLM_FFN_SEQ, il);
  14214. cb(cur, "ffn_out", il);
  14215. cur = ggml_add(ctx0, cur, ffn_inp);
  14216. cb(cur, "ffn_out", il);
  14217. cur = build_cvec(cur, il);
  14218. cb(cur, "l_out", il);
  14219. // input for next layer
  14220. inpL = cur;
  14221. }
  14222. cur = inpL;
  14223. cur = build_norm(cur,
  14224. model.output_norm, NULL,
  14225. LLM_NORM_RMS, -1);
  14226. cb(cur, "result_norm", -1);
  14227. res->t_embd = cur;
  14228. // lm_head
  14229. cur = build_lora_mm(model.output, cur);
  14230. cb(cur, "result_output", -1);
  14231. res->t_logits = cur;
  14232. ggml_build_forward_expand(gf, cur);
  14233. }
  14234. };
  14235. struct llm_build_hunyuan_moe : public llm_graph_context {
  14236. llm_build_hunyuan_moe(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  14237. const int64_t n_embd_head = hparams.n_embd_head_v;
  14238. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  14239. GGML_ASSERT(n_embd_head == hparams.n_rot);
  14240. ggml_tensor * cur;
  14241. ggml_tensor * inpL;
  14242. inpL = build_inp_embd(model.tok_embd);
  14243. // inp_pos - contains the positions
  14244. ggml_tensor * inp_pos = build_inp_pos();
  14245. auto * inp_attn = build_attn_inp_kv();
  14246. const float kq_scale = 1.0f / sqrtf(float(n_embd_head));
  14247. ggml_tensor * inp_out_ids = build_inp_out_ids();
  14248. for (int il = 0; il < n_layer; ++il) {
  14249. ggml_tensor * inpSA = inpL;
  14250. // norm
  14251. cur = build_norm(inpL,
  14252. model.layers[il].attn_norm, NULL,
  14253. LLM_NORM_RMS, il);
  14254. cb(cur, "attn_norm", il);
  14255. // self-attention
  14256. {
  14257. // rope freq factors for llama3; may return nullptr for llama2 and other models
  14258. ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
  14259. // compute Q and K and RoPE them
  14260. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  14261. cb(Qcur, "Qcur", il);
  14262. if (model.layers[il].bq) {
  14263. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  14264. cb(Qcur, "Qcur", il);
  14265. }
  14266. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  14267. cb(Kcur, "Kcur", il);
  14268. if (model.layers[il].bk) {
  14269. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  14270. cb(Kcur, "Kcur", il);
  14271. }
  14272. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  14273. cb(Vcur, "Vcur", il);
  14274. if (model.layers[il].bv) {
  14275. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  14276. cb(Vcur, "Vcur", il);
  14277. }
  14278. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  14279. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  14280. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  14281. Qcur = ggml_rope_ext(
  14282. ctx0, Qcur, inp_pos, rope_factors,
  14283. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  14284. ext_factor, attn_factor, beta_fast, beta_slow
  14285. );
  14286. cb(Qcur, "Qcur", il);
  14287. cb(Kcur, "Kcur", il);
  14288. cb(Vcur, "Vcur", il);
  14289. Kcur = ggml_rope_ext(
  14290. ctx0, Kcur, inp_pos, rope_factors,
  14291. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  14292. ext_factor, attn_factor, beta_fast, beta_slow
  14293. );
  14294. Kcur = build_norm(Kcur,
  14295. model.layers[il].attn_k_norm, nullptr,
  14296. LLM_NORM_RMS, il);
  14297. cb(Kcur, "Kcur_norm", il);
  14298. Qcur = build_norm(Qcur,
  14299. model.layers[il].attn_q_norm, nullptr,
  14300. LLM_NORM_RMS, il);
  14301. cb(Qcur, "Qcur_norm", il);
  14302. cur = build_attn(inp_attn,
  14303. model.layers[il].wo, model.layers[il].bo,
  14304. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
  14305. cb(cur, "attn_out", il);
  14306. }
  14307. if (il == n_layer - 1 && inp_out_ids) {
  14308. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  14309. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  14310. }
  14311. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  14312. cb(ffn_inp, "ffn_inp", il);
  14313. cur = build_norm(ffn_inp,
  14314. model.layers[il].ffn_norm, NULL,
  14315. LLM_NORM_RMS, il);
  14316. cb(cur, "ffn_norm", il);
  14317. // feed-forward network (non-MoE)
  14318. ggml_tensor * cur_mlp = build_ffn(cur,
  14319. model.layers[il].ffn_up_shexp, NULL, NULL,
  14320. model.layers[il].ffn_gate_shexp, NULL, NULL,
  14321. model.layers[il].ffn_down_shexp, NULL, NULL,
  14322. NULL,
  14323. LLM_FFN_SILU, LLM_FFN_PAR, il);
  14324. cb(cur_mlp, "ffn_mlp", il);
  14325. // MoE branch
  14326. ggml_tensor * cur_moe = build_moe_ffn(cur,
  14327. model.layers[il].ffn_gate_inp,
  14328. model.layers[il].ffn_up_exps,
  14329. model.layers[il].ffn_gate_exps,
  14330. model.layers[il].ffn_down_exps,
  14331. nullptr,
  14332. n_expert, n_expert_used,
  14333. LLM_FFN_SILU,
  14334. true, // norm_topk_prob
  14335. false,
  14336. 0.0,
  14337. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  14338. il);
  14339. cb(cur_moe, "ffn_moe_out", il);
  14340. ggml_tensor * ffn_out = ggml_add(ctx0, cur_moe, cur_mlp);
  14341. cb(ffn_out, "ffn_out", il);
  14342. cur = ggml_add(ctx0, ffn_out, ffn_inp);
  14343. cur = build_cvec(cur, il);
  14344. cb(cur, "l_out", il);
  14345. // input for next layer
  14346. inpL = cur;
  14347. }
  14348. cur = inpL;
  14349. cur = build_norm(cur,
  14350. model.output_norm, NULL,
  14351. LLM_NORM_RMS, -1);
  14352. cb(cur, "result_norm", -1);
  14353. res->t_embd = cur;
  14354. // lm_head
  14355. cur = build_lora_mm(model.output, cur);
  14356. cb(cur, "result_output", -1);
  14357. res->t_logits = cur;
  14358. ggml_build_forward_expand(gf, cur);
  14359. }
  14360. };
  14361. struct llm_build_hunyuan_dense : public llm_graph_context {
  14362. llm_build_hunyuan_dense(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  14363. const int64_t n_embd_head = hparams.n_embd_head_v;
  14364. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  14365. GGML_ASSERT(n_embd_head == hparams.n_rot);
  14366. ggml_tensor * cur;
  14367. ggml_tensor * inpL;
  14368. inpL = build_inp_embd(model.tok_embd);
  14369. // inp_pos - contains the positions
  14370. ggml_tensor * inp_pos = build_inp_pos();
  14371. auto * inp_attn = build_attn_inp_kv();
  14372. const float kq_scale = 1.0f / sqrtf(float(n_embd_head));
  14373. ggml_tensor * inp_out_ids = build_inp_out_ids();
  14374. for (int il = 0; il < n_layer; ++il) {
  14375. ggml_tensor * inpSA = inpL;
  14376. // norm
  14377. cur = build_norm(inpL,
  14378. model.layers[il].attn_norm, NULL,
  14379. LLM_NORM_RMS, il);
  14380. cb(cur, "attn_norm", il);
  14381. // self-attention
  14382. {
  14383. // rope freq factors for llama3; may return nullptr for llama2 and other models
  14384. ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
  14385. // compute Q and K and RoPE them
  14386. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  14387. cb(Qcur, "Qcur", il);
  14388. if (model.layers[il].bq) {
  14389. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  14390. cb(Qcur, "Qcur", il);
  14391. }
  14392. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  14393. cb(Kcur, "Kcur", il);
  14394. if (model.layers[il].bk) {
  14395. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  14396. cb(Kcur, "Kcur", il);
  14397. }
  14398. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  14399. cb(Vcur, "Vcur", il);
  14400. if (model.layers[il].bv) {
  14401. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  14402. cb(Vcur, "Vcur", il);
  14403. }
  14404. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  14405. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  14406. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  14407. Qcur = ggml_rope_ext(
  14408. ctx0, Qcur, inp_pos, rope_factors,
  14409. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  14410. ext_factor, attn_factor, beta_fast, beta_slow
  14411. );
  14412. cb(Qcur, "Qcur", il);
  14413. cb(Kcur, "Kcur", il);
  14414. cb(Vcur, "Vcur", il);
  14415. Kcur = ggml_rope_ext(
  14416. ctx0, Kcur, inp_pos, rope_factors,
  14417. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  14418. ext_factor, attn_factor, beta_fast, beta_slow
  14419. );
  14420. Kcur = build_norm(Kcur,
  14421. model.layers[il].attn_k_norm, nullptr,
  14422. LLM_NORM_RMS, il);
  14423. cb(Kcur, "Kcur_norm", il);
  14424. Qcur = build_norm(Qcur,
  14425. model.layers[il].attn_q_norm, nullptr,
  14426. LLM_NORM_RMS, il);
  14427. cb(Qcur, "Qcur_norm", il);
  14428. cur = build_attn(inp_attn,
  14429. model.layers[il].wo, model.layers[il].bo,
  14430. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
  14431. cb(cur, "attn_out", il);
  14432. }
  14433. if (il == n_layer - 1 && inp_out_ids) {
  14434. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  14435. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  14436. }
  14437. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  14438. cb(ffn_inp, "ffn_inp", il);
  14439. cur = build_norm(ffn_inp,
  14440. model.layers[il].ffn_norm, NULL,
  14441. LLM_NORM_RMS, il);
  14442. cb(cur, "ffn_norm", il);
  14443. // feed-forward network (non-MoE)
  14444. ggml_tensor * cur_mlp = build_ffn(cur,
  14445. model.layers[il].ffn_up, NULL, NULL,
  14446. model.layers[il].ffn_gate, NULL, NULL,
  14447. model.layers[il].ffn_down, NULL, NULL,
  14448. NULL,
  14449. LLM_FFN_SILU, LLM_FFN_PAR, il);
  14450. cb(cur_mlp, "ffn_out", il);
  14451. cur = ggml_add(ctx0, cur_mlp, ffn_inp);
  14452. cur = build_cvec(cur, il);
  14453. cb(cur, "l_out", il);
  14454. // input for next layer
  14455. inpL = cur;
  14456. }
  14457. cur = inpL;
  14458. cur = build_norm(cur,
  14459. model.output_norm, NULL,
  14460. LLM_NORM_RMS, -1);
  14461. cb(cur, "result_norm", -1);
  14462. res->t_embd = cur;
  14463. // lm_head
  14464. cur = build_lora_mm(model.output, cur);
  14465. cb(cur, "result_output", -1);
  14466. res->t_logits = cur;
  14467. ggml_build_forward_expand(gf, cur);
  14468. }
  14469. };
  14470. struct llm_build_smollm3 : public llm_graph_context {
  14471. llm_build_smollm3(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  14472. const int64_t n_embd_head = hparams.n_embd_head_v;
  14473. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  14474. GGML_ASSERT(n_embd_head == hparams.n_rot);
  14475. ggml_tensor * cur;
  14476. ggml_tensor * inpL;
  14477. inpL = build_inp_embd(model.tok_embd);
  14478. // inp_pos - contains the positions
  14479. ggml_tensor * inp_pos = build_inp_pos();
  14480. auto * inp_attn = build_attn_inp_kv();
  14481. const float kq_scale = hparams.f_attention_scale == 0.0f ? 1.0f/sqrtf(float(n_embd_head)) : hparams.f_attention_scale;
  14482. ggml_tensor * inp_out_ids = build_inp_out_ids();
  14483. for (int il = 0; il < n_layer; ++il) {
  14484. ggml_tensor * inpSA = inpL;
  14485. const bool use_rope = (il + 1) % hparams.n_no_rope_layer_step != 0;
  14486. // norm
  14487. cur = build_norm(inpL,
  14488. model.layers[il].attn_norm, NULL,
  14489. LLM_NORM_RMS, il);
  14490. cb(cur, "attn_norm", il);
  14491. // self-attention
  14492. {
  14493. // compute Q and K and RoPE them
  14494. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  14495. cb(Qcur, "Qcur", il);
  14496. if (model.layers[il].bq) {
  14497. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  14498. cb(Qcur, "Qcur", il);
  14499. }
  14500. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  14501. cb(Kcur, "Kcur", il);
  14502. if (model.layers[il].bk) {
  14503. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  14504. cb(Kcur, "Kcur", il);
  14505. }
  14506. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  14507. cb(Vcur, "Vcur", il);
  14508. if (model.layers[il].bv) {
  14509. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  14510. cb(Vcur, "Vcur", il);
  14511. }
  14512. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  14513. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  14514. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  14515. if (use_rope) {
  14516. Qcur = ggml_rope_ext(
  14517. ctx0, Qcur, inp_pos, nullptr,
  14518. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  14519. ext_factor, attn_factor, beta_fast, beta_slow
  14520. );
  14521. Kcur = ggml_rope_ext(
  14522. ctx0, Kcur, inp_pos, nullptr,
  14523. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  14524. ext_factor, attn_factor, beta_fast, beta_slow
  14525. );
  14526. }
  14527. cb(Qcur, "Qcur", il);
  14528. cb(Kcur, "Kcur", il);
  14529. cb(Vcur, "Vcur", il);
  14530. cur = build_attn(inp_attn,
  14531. model.layers[il].wo, model.layers[il].bo,
  14532. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
  14533. cb(cur, "attn_out", il);
  14534. }
  14535. if (il == n_layer - 1 && inp_out_ids) {
  14536. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  14537. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  14538. }
  14539. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  14540. cb(ffn_inp, "ffn_inp", il);
  14541. // feed-forward network
  14542. {
  14543. cur = build_norm(ffn_inp,
  14544. model.layers[il].ffn_norm, NULL,
  14545. LLM_NORM_RMS, il);
  14546. cb(cur, "ffn_norm", il);
  14547. cur = build_ffn(cur,
  14548. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  14549. model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, NULL,
  14550. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  14551. NULL,
  14552. LLM_FFN_SILU, LLM_FFN_PAR, il);
  14553. cb(cur, "ffn_out", il);
  14554. }
  14555. cur = ggml_add(ctx0, cur, ffn_inp);
  14556. cb(cur, "ffn_out", il);
  14557. cur = build_cvec(cur, il);
  14558. cb(cur, "l_out", il);
  14559. // input for next layer
  14560. inpL = cur;
  14561. }
  14562. cur = inpL;
  14563. cur = build_norm(cur,
  14564. model.output_norm, NULL,
  14565. LLM_NORM_RMS, -1);
  14566. cb(cur, "result_norm", -1);
  14567. res->t_embd = cur;
  14568. // lm_head
  14569. cur = build_lora_mm(model.output, cur);
  14570. cb(cur, "result_output", -1);
  14571. res->t_logits = cur;
  14572. ggml_build_forward_expand(gf, cur);
  14573. }
  14574. };
  14575. struct llm_build_openai_moe_iswa : public llm_graph_context {
  14576. llm_build_openai_moe_iswa(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  14577. ggml_tensor * cur;
  14578. ggml_tensor * inpL;
  14579. inpL = build_inp_embd(model.tok_embd);
  14580. // inp_pos - contains the positions
  14581. ggml_tensor * inp_pos = build_inp_pos();
  14582. auto * inp_attn = build_attn_inp_kv_iswa();
  14583. for (int il = 0; il < n_layer; ++il) {
  14584. ggml_tensor * inpSA = inpL;
  14585. // norm
  14586. cur = build_norm(inpL,
  14587. model.layers[il].attn_norm, nullptr,
  14588. LLM_NORM_RMS, il);
  14589. cb(cur, "attn_norm", il);
  14590. // self-attention
  14591. {
  14592. // compute Q and K and RoPE them
  14593. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  14594. cb(Qcur, "Qcur", il);
  14595. if (model.layers[il].bq) {
  14596. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  14597. cb(Qcur, "Qcur", il);
  14598. }
  14599. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  14600. cb(Kcur, "Kcur", il);
  14601. if (model.layers[il].bk) {
  14602. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  14603. cb(Kcur, "Kcur", il);
  14604. }
  14605. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  14606. cb(Vcur, "Vcur", il);
  14607. if (model.layers[il].bv) {
  14608. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  14609. cb(Vcur, "Vcur", il);
  14610. }
  14611. Qcur = ggml_reshape_3d(ctx0, Qcur, n_rot, n_head, n_tokens);
  14612. Kcur = ggml_reshape_3d(ctx0, Kcur, n_rot, n_head_kv, n_tokens);
  14613. Vcur = ggml_reshape_3d(ctx0, Vcur, n_rot, n_head_kv, n_tokens);
  14614. Qcur = ggml_rope_ext(
  14615. ctx0, Qcur, inp_pos, nullptr,
  14616. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  14617. ext_factor, attn_factor, beta_fast, beta_slow
  14618. );
  14619. Kcur = ggml_rope_ext(
  14620. ctx0, Kcur, inp_pos, nullptr,
  14621. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  14622. ext_factor, attn_factor, beta_fast, beta_slow
  14623. );
  14624. cb(Qcur, "Qcur", il);
  14625. cb(Kcur, "Kcur", il);
  14626. cb(Vcur, "Vcur", il);
  14627. cur = build_attn(inp_attn,
  14628. model.layers[il].wo, model.layers[il].bo,
  14629. Qcur, Kcur, Vcur, nullptr, model.layers[il].attn_sinks, nullptr, 1.0f/sqrtf(float(n_rot)), il);
  14630. cb(cur, "attn_out", il);
  14631. }
  14632. if (il == n_layer - 1) {
  14633. // skip computing output for unused tokens
  14634. ggml_tensor * inp_out_ids = build_inp_out_ids();
  14635. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  14636. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  14637. }
  14638. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  14639. cb(ffn_inp, "ffn_inp", il);
  14640. cur = ffn_inp;
  14641. cur = build_norm(cur,
  14642. model.layers[il].attn_post_norm, nullptr,
  14643. LLM_NORM_RMS, il);
  14644. cb(cur, "attn_post_norm", il);
  14645. // MoE branch
  14646. cur = build_moe_ffn(cur,
  14647. model.layers[il].ffn_gate_inp, model.layers[il].ffn_gate_inp_b,
  14648. model.layers[il].ffn_up_exps, model.layers[il].ffn_up_exps_b,
  14649. model.layers[il].ffn_gate_exps, model.layers[il].ffn_gate_exps_b,
  14650. model.layers[il].ffn_down_exps, model.layers[il].ffn_down_exps_b,
  14651. nullptr,
  14652. n_expert, n_expert_used,
  14653. LLM_FFN_SWIGLU_OAI_MOE, false,
  14654. false, 0.0,
  14655. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX_WEIGHT,
  14656. il);
  14657. cb(cur, "ffn_moe_out", il);
  14658. cur = ggml_add(ctx0, cur, ffn_inp);
  14659. cur = build_cvec(cur, il);
  14660. cb(cur, "l_out", il);
  14661. // input for next layer
  14662. inpL = cur;
  14663. }
  14664. cur = inpL;
  14665. cur = build_norm(cur,
  14666. model.output_norm, NULL,
  14667. LLM_NORM_RMS, -1);
  14668. cb(cur, "result_norm", -1);
  14669. res->t_embd = cur;
  14670. // lm_head
  14671. cur = build_lora_mm(model.output, cur);
  14672. cb(cur, "result_output", -1);
  14673. res->t_logits = cur;
  14674. ggml_build_forward_expand(gf, cur);
  14675. }
  14676. };
  14677. struct llm_build_lfm2 : public llm_graph_context {
  14678. const llama_model & model;
  14679. llm_build_lfm2(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params), model(model) {
  14680. ggml_tensor * cur = build_inp_embd(model.tok_embd);
  14681. cb(cur, "model.embed_tokens", -1);
  14682. ggml_tensor * inp_pos = build_inp_pos();
  14683. auto * inp_hybrid = build_inp_mem_hybrid();
  14684. ggml_tensor * inp_out_ids = build_inp_out_ids();
  14685. for (int il = 0; il < n_layer; ++il) {
  14686. auto * prev_cur = cur;
  14687. cur = build_norm(cur, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
  14688. cb(cur, "model.layers.{}.operator_norm", il);
  14689. cur = hparams.is_recurrent(il) ?
  14690. build_shortconv_block(cur, inp_hybrid->get_recr(), il) :
  14691. build_attn_block(cur, inp_pos, inp_hybrid->get_attn(), il) ;
  14692. if (il == n_layer - 1 && inp_out_ids) {
  14693. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  14694. prev_cur = ggml_get_rows(ctx0, prev_cur, inp_out_ids);
  14695. }
  14696. cur = ggml_add(ctx0, prev_cur, cur);
  14697. cur = ggml_add(ctx0, cur, build_feed_forward(cur, il));
  14698. }
  14699. cur = build_norm(cur, model.tok_norm, NULL, LLM_NORM_RMS, -1);
  14700. cb(cur, "model.embedding_norm", -1);
  14701. res->t_embd = cur;
  14702. cur = build_lora_mm(model.output, cur);
  14703. cb(cur, "lm_head", -1);
  14704. res->t_logits = cur;
  14705. ggml_build_forward_expand(gf, cur);
  14706. }
  14707. ggml_tensor * build_feed_forward(ggml_tensor * cur,
  14708. int il) const {
  14709. cur = build_norm(cur, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);
  14710. cb(cur, "model.layers.{}.ffn_norm", il);
  14711. GGML_ASSERT(!model.layers[il].ffn_up_b);
  14712. GGML_ASSERT(!model.layers[il].ffn_gate_b);
  14713. GGML_ASSERT(!model.layers[il].ffn_down_b);
  14714. cur = build_ffn(cur,
  14715. model.layers[il].ffn_up, NULL, NULL,
  14716. model.layers[il].ffn_gate, NULL, NULL,
  14717. model.layers[il].ffn_down, NULL, NULL,
  14718. NULL,
  14719. LLM_FFN_SILU, LLM_FFN_PAR, il);
  14720. cb(cur, "model.layers.{}.feed_forward.w2", il);
  14721. return cur;
  14722. }
  14723. ggml_tensor * build_attn_block(ggml_tensor * cur,
  14724. ggml_tensor * inp_pos,
  14725. llm_graph_input_attn_kv * inp_attn,
  14726. int il) const {
  14727. GGML_ASSERT(hparams.n_embd_v_gqa(il) == hparams.n_embd_k_gqa(il));
  14728. auto const n_embd_head = hparams.n_embd_head_v;
  14729. auto const n_head_kv = hparams.n_head_kv(il);
  14730. auto * q = build_lora_mm(model.layers[il].wq, cur);
  14731. cb(q, "model.layers.{}.self_attn.q_proj", il);
  14732. auto * k = build_lora_mm(model.layers[il].wk, cur);
  14733. cb(k, "model.layers.{}.self_attn.k_proj", il);
  14734. auto * v = build_lora_mm(model.layers[il].wv, cur);
  14735. cb(v, "model.layers.{}.self_attn.v_proj", il);
  14736. q = ggml_reshape_3d(ctx0, q, n_embd_head, n_head, n_tokens);
  14737. k = ggml_reshape_3d(ctx0, k, n_embd_head, n_head_kv, n_tokens);
  14738. v = ggml_reshape_3d(ctx0, v, n_embd_head, n_head_kv, n_tokens);
  14739. // qk norm
  14740. q = build_norm(q, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);
  14741. cb(q, "model.layers.{}.self_attn.q_layernorm", il);
  14742. k = build_norm(k, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);
  14743. cb(k, "model.layers.{}.self_attn.k_layernorm", il);
  14744. // RoPE
  14745. q = ggml_rope_ext(
  14746. ctx0, q, inp_pos, nullptr,
  14747. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  14748. ext_factor, attn_factor, beta_fast, beta_slow
  14749. );
  14750. k = ggml_rope_ext(
  14751. ctx0, k, inp_pos, nullptr,
  14752. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  14753. ext_factor, attn_factor, beta_fast, beta_slow
  14754. );
  14755. cur = build_attn(inp_attn, model.layers[il].wo, NULL,
  14756. q, k, v, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  14757. cb(cur, "model.layers.{}.self_attn.out_proj", il);
  14758. return cur;
  14759. }
  14760. ggml_tensor * build_shortconv_block(ggml_tensor * cur,
  14761. llm_graph_input_rs * inp_recr,
  14762. int il) {
  14763. const auto * mctx_cur = static_cast<const llama_memory_hybrid_context *>(mctx)->get_recr();
  14764. const uint32_t kv_head = mctx_cur->get_head();
  14765. const int64_t n_seq_tokens = ubatch.n_seq_tokens;
  14766. const int64_t n_seqs = ubatch.n_seqs;
  14767. GGML_ASSERT(n_seqs != 0);
  14768. GGML_ASSERT(ubatch.equal_seqs());
  14769. GGML_ASSERT(ubatch.n_tokens == n_seq_tokens * n_seqs);
  14770. GGML_ASSERT(hparams.n_shortconv_l_cache > 1);
  14771. const uint32_t d_conv = hparams.n_shortconv_l_cache - 1;
  14772. // {n_embd, n_tokens} => {n_embd, n_seq_tokens, n_seqs}
  14773. cur = ggml_reshape_3d(ctx0, cur, cur->ne[0], n_seq_tokens, n_seqs);
  14774. auto * bcx = build_lora_mm(model.layers[il].shortconv.in_proj, cur);
  14775. cb(bcx, "model.layers.{}.conv.in_proj", il);
  14776. constexpr auto n_chunks = 3;
  14777. GGML_ASSERT(bcx->ne[0] % n_chunks == 0);
  14778. auto const chunk_size = bcx->ne[0] / n_chunks;
  14779. 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));
  14780. 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));
  14781. 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));
  14782. auto * bx = ggml_transpose(ctx0, ggml_mul(ctx0, b, x));
  14783. // read conv state
  14784. auto * conv_state = mctx_cur->get_r_l(il);
  14785. auto * conv_rs = build_rs(inp_recr, conv_state, hparams.n_embd_r(), n_seqs);
  14786. auto * conv = ggml_reshape_3d(ctx0, conv_rs, d_conv, hparams.n_embd, n_seqs);
  14787. bx = ggml_concat(ctx0, conv, bx, 0);
  14788. GGML_ASSERT(bx->ne[0] > conv->ne[0]);
  14789. // last d_conv columns is a new conv state
  14790. 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));
  14791. GGML_ASSERT(ggml_are_same_shape(conv, new_conv));
  14792. // write new conv conv state
  14793. ggml_build_forward_expand(
  14794. gf,
  14795. ggml_cpy(
  14796. ctx0,
  14797. new_conv,
  14798. ggml_view_1d(
  14799. ctx0,
  14800. conv_state,
  14801. ggml_nelements(new_conv),
  14802. kv_head*d_conv*n_embd*ggml_element_size(new_conv)
  14803. )
  14804. )
  14805. );
  14806. auto * conv_kernel = model.layers[il].shortconv.conv;
  14807. auto * conv_out = ggml_ssm_conv(ctx0, bx, conv_kernel);
  14808. cb(conv_out, "model.layers.{}.conv.conv", il);
  14809. auto * y = ggml_mul(ctx0, c, conv_out);
  14810. y = build_lora_mm(model.layers[il].shortconv.out_proj, y);
  14811. cb(y, "model.layers.{}.conv.out_proj", il);
  14812. // {n_embd, n_seq_tokens, n_seqs} => {n_embd, n_tokens}
  14813. y = ggml_reshape_2d(ctx0, y, y->ne[0], n_seq_tokens * n_seqs);
  14814. return y;
  14815. }
  14816. };
  14817. struct llm_build_seed_oss : public llm_graph_context {
  14818. llm_build_seed_oss(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  14819. const int64_t n_embd_head = hparams.n_embd_head_v;
  14820. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  14821. GGML_ASSERT(n_embd_head == hparams.n_rot);
  14822. ggml_tensor * cur;
  14823. ggml_tensor * inpL;
  14824. inpL = build_inp_embd(model.tok_embd);
  14825. // inp_pos - contains the positions
  14826. ggml_tensor * inp_pos = build_inp_pos();
  14827. auto * inp_attn = build_attn_inp_kv();
  14828. const float kq_scale = hparams.f_attention_scale == 0.0f ? 1.0f/sqrtf(float(n_embd_head)) : hparams.f_attention_scale;
  14829. ggml_tensor * inp_out_ids = build_inp_out_ids();
  14830. for (int il = 0; il < n_layer; ++il) {
  14831. ggml_tensor * inpSA = inpL;
  14832. // norm
  14833. cur = build_norm(inpL,
  14834. model.layers[il].attn_norm, NULL,
  14835. LLM_NORM_RMS, il);
  14836. cb(cur, "attn_norm", il);
  14837. // self-attention
  14838. {
  14839. // compute Q and K and RoPE them
  14840. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  14841. cb(Qcur, "Qcur", il);
  14842. if (model.layers[il].bq) {
  14843. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  14844. cb(Qcur, "Qcur", il);
  14845. }
  14846. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  14847. cb(Kcur, "Kcur", il);
  14848. if (model.layers[il].bk) {
  14849. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  14850. cb(Kcur, "Kcur", il);
  14851. }
  14852. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  14853. cb(Vcur, "Vcur", il);
  14854. if (model.layers[il].bv) {
  14855. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  14856. cb(Vcur, "Vcur", il);
  14857. }
  14858. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  14859. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  14860. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  14861. Qcur = ggml_rope_ext(
  14862. ctx0, Qcur, inp_pos, nullptr,
  14863. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  14864. ext_factor, attn_factor, beta_fast, beta_slow
  14865. );
  14866. Kcur = ggml_rope_ext(
  14867. ctx0, Kcur, inp_pos, nullptr,
  14868. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  14869. ext_factor, attn_factor, beta_fast, beta_slow
  14870. );
  14871. cb(Qcur, "Qcur", il);
  14872. cb(Kcur, "Kcur", il);
  14873. cb(Vcur, "Vcur", il);
  14874. cur = build_attn(inp_attn,
  14875. model.layers[il].wo, model.layers[il].bo,
  14876. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
  14877. cb(cur, "attn_out", il);
  14878. }
  14879. if (il == n_layer - 1 && inp_out_ids) {
  14880. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  14881. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  14882. }
  14883. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  14884. cb(ffn_inp, "ffn_inp", il);
  14885. // feed-forward network
  14886. cur = build_norm(ffn_inp,
  14887. model.layers[il].attn_post_norm, NULL,
  14888. LLM_NORM_RMS, il);
  14889. cb(cur, "attn_post_norm", il);
  14890. cur = build_ffn(cur,
  14891. model.layers[il].ffn_up, NULL, NULL,
  14892. model.layers[il].ffn_gate, NULL, NULL,
  14893. model.layers[il].ffn_down, NULL, NULL,
  14894. NULL,
  14895. LLM_FFN_SILU, LLM_FFN_PAR, il);
  14896. cb(cur, "ffn_out", il);
  14897. cur = ggml_add(ctx0, cur, ffn_inp);
  14898. cb(cur, "ffn_out", il);
  14899. cur = build_cvec(cur, il);
  14900. cb(cur, "l_out", il);
  14901. // input for next layer
  14902. inpL = cur;
  14903. }
  14904. cur = inpL;
  14905. cur = build_norm(cur,
  14906. model.output_norm, NULL,
  14907. LLM_NORM_RMS, -1);
  14908. cb(cur, "result_norm", -1);
  14909. res->t_embd = cur;
  14910. // lm_head
  14911. cur = build_lora_mm(model.output, cur);
  14912. cb(cur, "result_output", -1);
  14913. res->t_logits = cur;
  14914. ggml_build_forward_expand(gf, cur);
  14915. }
  14916. };
  14917. template <bool iswa>
  14918. struct llm_build_smallthinker : public llm_graph_context{
  14919. llm_build_smallthinker(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params){
  14920. const int64_t n_embd_head = hparams.n_embd_head_v;
  14921. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  14922. GGML_ASSERT(n_embd_head == hparams.n_rot);
  14923. ggml_tensor * cur;
  14924. ggml_tensor * inpL;
  14925. inpL = build_inp_embd(model.tok_embd);
  14926. // inp_pos - contains the positions
  14927. ggml_tensor * inp_pos = build_inp_pos();
  14928. using inp_attn_type = std::conditional_t<iswa, llm_graph_input_attn_kv_iswa, llm_graph_input_attn_kv>;
  14929. inp_attn_type * inp_attn = nullptr;
  14930. if constexpr (iswa) {
  14931. inp_attn = build_attn_inp_kv_iswa();
  14932. } else {
  14933. inp_attn = build_attn_inp_kv();
  14934. }
  14935. ggml_tensor * inp_out_ids = build_inp_out_ids();
  14936. for (int il = 0; il < n_layer; ++il) {
  14937. ggml_tensor * inpSA = inpL;
  14938. ggml_tensor * probs = nullptr;
  14939. probs = build_lora_mm(model.layers[il].ffn_gate_inp, inpL); // [n_expert, n_tokens]
  14940. cb(probs, "ffn_moe_logits", il);
  14941. // norm
  14942. cur = build_norm(inpL,model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
  14943. cb(cur, "attn_norm", il);
  14944. // self_attention
  14945. {
  14946. // compute Q and K and RoPE them
  14947. struct ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  14948. cb(Qcur, "Qcur", il);
  14949. struct ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  14950. cb(Kcur, "Kcur", il);
  14951. struct ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  14952. cb(Vcur, "Vcur", il);
  14953. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  14954. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  14955. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  14956. if (hparams.n_no_rope_layer_step == n_layer || il % hparams.n_no_rope_layer_step != 0) {
  14957. Qcur = ggml_rope_ext(ctx0, Qcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  14958. ext_factor, attn_factor, beta_fast, beta_slow);
  14959. Kcur = ggml_rope_ext(ctx0, Kcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  14960. ext_factor, attn_factor, beta_fast, beta_slow);
  14961. }
  14962. cb(Qcur, "Qcur", il);
  14963. cb(Kcur, "Kcur", il);
  14964. cur = build_attn(inp_attn,
  14965. model.layers[il].wo, model.layers[il].bo,
  14966. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f / sqrtf(float(n_embd_head)), il);
  14967. }
  14968. if (il == n_layer - 1 && inp_out_ids) {
  14969. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  14970. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  14971. probs = ggml_get_rows(ctx0, probs, inp_out_ids);
  14972. }
  14973. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  14974. cb(ffn_inp, "ffn_inp", il);
  14975. // MoE branch
  14976. cur = build_norm(ffn_inp, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);
  14977. cb(cur, "ffn_norm", il);
  14978. ggml_tensor * ffn_out =
  14979. build_moe_ffn(cur,
  14980. nullptr,
  14981. model.layers[il].ffn_up_exps,
  14982. model.layers[il].ffn_gate_exps,
  14983. model.layers[il].ffn_down_exps,
  14984. nullptr,
  14985. n_expert, n_expert_used,
  14986. LLM_FFN_RELU, true,
  14987. false, 0.0,
  14988. static_cast<llama_expert_gating_func_type>(hparams.expert_gating_func),
  14989. il, probs);
  14990. cb(ffn_out, "ffn_out", il);
  14991. cur = ffn_out;
  14992. cur = ggml_add(ctx0, cur, ffn_inp);
  14993. cur = build_cvec(cur, il);
  14994. cb(cur, "l_out", il);
  14995. // input for next layer
  14996. inpL = cur;
  14997. }
  14998. cur = inpL;
  14999. cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1);
  15000. cb(cur, "result_norm", -1);
  15001. // lm_head
  15002. cur = build_lora_mm(model.output, cur);
  15003. cb(cur, "result_output", -1);
  15004. res->t_logits = cur;
  15005. ggml_build_forward_expand(gf, cur);
  15006. }
  15007. };
  15008. struct llm_build_qwen3next : public llm_graph_context_mamba {
  15009. llm_build_qwen3next(const llama_model & model, const llm_graph_params & params) : llm_graph_context_mamba(params) {
  15010. const int64_t n_embd_head = hparams.n_embd_head_v;
  15011. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  15012. ggml_tensor * cur;
  15013. ggml_tensor * inpL;
  15014. inpL = build_inp_embd(model.tok_embd);
  15015. auto * inp = build_inp_mem_hybrid();
  15016. ggml_tensor * inp_pos = build_inp_pos();
  15017. ggml_tensor * inp_out_ids = build_inp_out_ids();
  15018. for (int il = 0; il < n_layer; ++il) {
  15019. struct ggml_tensor * inpSA = inpL;
  15020. // Pre-norm for attention/linear attention
  15021. cur = build_norm(inpL,
  15022. model.layers[il].attn_norm, NULL,
  15023. LLM_NORM_RMS, il);
  15024. cb(cur, "attn_norm", il);
  15025. // Determine layer type and build appropriate attention mechanism
  15026. if (hparams.is_recurrent(il)) {
  15027. // Linear attention layer (gated delta net)
  15028. cur = build_qwen3next_linear_attn_layer(inp->get_recr(), cur, model, ubatch, il);
  15029. } else {
  15030. // Full attention layer
  15031. cur = build_qwen3next_attention_layer(
  15032. cur, inp_pos, inp->get_attn(), model,
  15033. n_embd_head, il);
  15034. }
  15035. // Post-attention norm
  15036. cur = build_norm(cur,
  15037. model.layers[il].attn_post_norm, NULL,
  15038. LLM_NORM_RMS, il);
  15039. cb(cur, "attn_post_norm", il);
  15040. if (il == n_layer - 1 && inp_out_ids) {
  15041. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  15042. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  15043. }
  15044. // Residual connection
  15045. cur = ggml_add(ctx0, cur, inpSA);
  15046. cb(cur, "attn_residual", il);
  15047. // FFN layer (MoE or dense)
  15048. cur = build_layer_ffn(cur, model, il);
  15049. // Input for next layer
  15050. inpL = cur;
  15051. }
  15052. cur = inpL;
  15053. // Final norm
  15054. cur = build_norm(cur,
  15055. model.output_norm, NULL,
  15056. LLM_NORM_RMS, -1);
  15057. cb(cur, "result_norm", -1);
  15058. res->t_embd = cur;
  15059. // LM head
  15060. cur = build_lora_mm(model.output, cur);
  15061. cb(cur, "result_output", -1);
  15062. res->t_logits = cur;
  15063. ggml_build_forward_expand(gf, cur);
  15064. }
  15065. private:
  15066. ggml_tensor * build_qwen3next_attention_layer(
  15067. ggml_tensor * cur,
  15068. ggml_tensor * inp_pos,
  15069. llm_graph_input_attn_kv * inp_attn,
  15070. const llama_model & model,
  15071. const int64_t n_embd_head,
  15072. const int il) {
  15073. // QKV projection with gating
  15074. ggml_tensor * qkv_g = build_lora_mm(model.layers[il].wq, cur);
  15075. cb(qkv_g, "qkv_g", il);
  15076. // Split into Q and gate
  15077. const int64_t n_embd_q = hparams.n_head(il) * n_embd_head;
  15078. ggml_tensor * Qcur = ggml_view_3d(ctx0, qkv_g, n_embd_head, hparams.n_head(il), n_tokens,
  15079. n_embd_head * sizeof(float), qkv_g->nb[1], 0);
  15080. ggml_tensor * gate = ggml_view_3d(ctx0, qkv_g, n_embd_head, hparams.n_head(il), n_tokens,
  15081. n_embd_head * sizeof(float), qkv_g->nb[1], n_embd_q * ggml_element_size(qkv_g));
  15082. // K and V projections
  15083. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  15084. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  15085. cb(Kcur, "Kcur", il);
  15086. cb(Vcur, "Vcur", il);
  15087. Qcur = ggml_reshape_3d(ctx0, ggml_cont(ctx0, Qcur), n_embd_head, hparams.n_head(il), n_tokens);
  15088. Kcur = ggml_reshape_3d(ctx0, ggml_cont(ctx0, Kcur), n_embd_head, hparams.n_head_kv(il), n_tokens);
  15089. Vcur = ggml_reshape_3d(ctx0, ggml_cont(ctx0, Vcur), n_embd_head, hparams.n_head_kv(il), n_tokens);
  15090. // Apply Q/K normalization
  15091. Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);
  15092. Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);
  15093. // Apply RoPE
  15094. Qcur = ggml_rope_ext(
  15095. ctx0, Qcur, inp_pos, nullptr,
  15096. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  15097. ext_factor, attn_factor, beta_fast, beta_slow);
  15098. Kcur = ggml_rope_ext(
  15099. ctx0, Kcur, inp_pos, nullptr,
  15100. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  15101. ext_factor, attn_factor, beta_fast, beta_slow);
  15102. cb(Qcur, "Qcur", il);
  15103. cb(Kcur, "Kcur", il);
  15104. cb(Vcur, "Vcur", il);
  15105. // Attention computation
  15106. const float kq_scale = hparams.f_attention_scale == 0.0f ? 1.0f/sqrtf(float(n_embd_head)) : hparams.f_attention_scale;
  15107. cur = build_attn(inp_attn,
  15108. model.layers[il].wo, nullptr,
  15109. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
  15110. // Apply gating
  15111. gate = ggml_reshape_2d(ctx0, ggml_cont(ctx0, gate), n_embd_q, n_tokens);
  15112. cur = ggml_cont(ctx0, ggml_mul(ctx0, cur, ggml_sigmoid(ctx0, gate)));
  15113. cb(cur, "attn_gated", il);
  15114. return cur;
  15115. }
  15116. ggml_tensor * build_qwen3next_linear_attn_layer(llm_graph_input_rs * inp,
  15117. ggml_tensor * cur,
  15118. const llama_model & model,
  15119. const llama_ubatch & ubatch,
  15120. int il) {
  15121. // Gated Delta Net implementation using the new ggml_delta_net function
  15122. const auto * mctx_cur = inp->mctx;
  15123. const auto kv_head = mctx_cur->get_head();
  15124. const int64_t d_inner = hparams.ssm_d_inner;
  15125. const int64_t n_heads = hparams.ssm_dt_rank;
  15126. const int64_t head_dim = d_inner / n_heads;
  15127. const int64_t n_seqs = ubatch.n_seqs;
  15128. const int64_t head_k_dim = hparams.ssm_d_state;
  15129. const int64_t head_v_dim = hparams.ssm_d_state;
  15130. const int64_t num_k_heads = hparams.ssm_n_group;
  15131. const int64_t num_v_heads = hparams.ssm_dt_rank;
  15132. const int64_t n_seq_tokens = ubatch.n_seq_tokens;
  15133. const int64_t n_tokens = ubatch.n_tokens;
  15134. GGML_ASSERT(n_seqs != 0);
  15135. GGML_ASSERT(ubatch.equal_seqs());
  15136. GGML_ASSERT(ubatch.n_tokens == n_seq_tokens * n_seqs);
  15137. // Input projections
  15138. ggml_tensor * mixed_qkvz = build_lora_mm(model.layers[il].ssm_in, cur);
  15139. cb(mixed_qkvz, "linear_attn_mixed_qkvz", il);
  15140. ggml_tensor * mixed_ba = build_lora_mm(model.layers[il].ssm_beta_alpha, cur);
  15141. cb(mixed_ba, "linear_attn_mixed_ba", il);
  15142. // Reshape mixed_qkvz: [batch, seq_len, hidden_size] -> [batch, seq_len, num_k_heads, 2*head_k_dim + 2*head_v_dim*num_v_heads/num_k_heads]
  15143. int64_t qkvz_new_dim = 2 * head_k_dim + 2 * head_v_dim * num_v_heads / num_k_heads;
  15144. ggml_tensor * mixed_qkvz_reshaped =
  15145. ggml_reshape_4d(ctx0, mixed_qkvz, qkvz_new_dim, num_k_heads, n_tokens, n_seqs);
  15146. // Reshape mixed_ba: [batch, seq_len, hidden_size] -> [batch, seq_len, num_k_heads, 2*num_v_heads/num_k_heads]
  15147. int64_t ba_new_dim = 2 * num_v_heads / num_k_heads;
  15148. ggml_tensor * mixed_ba_reshaped = ggml_reshape_4d(ctx0, mixed_ba, ba_new_dim, num_k_heads, n_tokens, n_seqs);
  15149. // Split mixed_qkvz into query, key, value, z
  15150. int64_t split_sizes_qkvz[4] = {
  15151. head_k_dim, // query size
  15152. head_k_dim, // key size
  15153. head_v_dim * num_v_heads / num_k_heads, // value size
  15154. head_v_dim * num_v_heads / num_k_heads // z size
  15155. };
  15156. ggml_tensor * query = ggml_cont(ctx0, ggml_view_4d(ctx0, mixed_qkvz_reshaped, split_sizes_qkvz[0], num_k_heads, n_tokens,
  15157. n_seqs, split_sizes_qkvz[0] * sizeof(float), mixed_qkvz_reshaped->nb[1],
  15158. mixed_qkvz_reshaped->nb[2], 0));
  15159. ggml_tensor * key = ggml_cont(ctx0, ggml_view_4d(ctx0, mixed_qkvz_reshaped, split_sizes_qkvz[1], num_k_heads, n_tokens, n_seqs,
  15160. split_sizes_qkvz[1] * sizeof(float), mixed_qkvz_reshaped->nb[1],
  15161. mixed_qkvz_reshaped->nb[2], split_sizes_qkvz[0] * sizeof(float)));
  15162. ggml_tensor * value =
  15163. ggml_view_4d(ctx0, mixed_qkvz_reshaped, split_sizes_qkvz[2], num_k_heads, n_tokens, n_seqs,
  15164. split_sizes_qkvz[2] * sizeof(float), mixed_qkvz_reshaped->nb[1], mixed_qkvz_reshaped->nb[2],
  15165. (split_sizes_qkvz[0] + split_sizes_qkvz[1]) * sizeof(float));
  15166. ggml_tensor * z =
  15167. ggml_view_4d(ctx0, mixed_qkvz_reshaped, split_sizes_qkvz[3], num_k_heads, n_tokens, n_seqs,
  15168. split_sizes_qkvz[3] * sizeof(float), mixed_qkvz_reshaped->nb[1], mixed_qkvz_reshaped->nb[2],
  15169. (split_sizes_qkvz[0] + split_sizes_qkvz[1] + split_sizes_qkvz[2]) * sizeof(float));
  15170. // Reshape value and z to merge head dimensions: [batch, seq_len, num_k_heads, head_v_dim*num_v_heads/num_k_heads] -> [batch, seq_len, num_v_heads, head_v_dim]
  15171. ggml_tensor * value_reshaped = ggml_reshape_4d(ctx0, ggml_cont(ctx0, value), head_v_dim, num_v_heads, n_tokens, n_seqs);
  15172. ggml_tensor * z_reshaped = ggml_reshape_4d(ctx0, ggml_cont(ctx0, z), head_v_dim, num_v_heads, n_tokens, n_seqs);
  15173. GGML_ASSERT(ggml_nelements(query) + ggml_nelements(key) + ggml_nelements(value_reshaped) +
  15174. ggml_nelements(z_reshaped) ==
  15175. ggml_nelements(mixed_qkvz));
  15176. // Split mixed_ba into b and a (beta and alpha parameters)
  15177. int64_t split_sizes_ba[2] = {
  15178. num_v_heads / num_k_heads, // beta size
  15179. num_v_heads / num_k_heads // alpha size
  15180. };
  15181. ggml_tensor * b =
  15182. ggml_view_4d(ctx0, mixed_ba_reshaped, split_sizes_ba[0], num_k_heads, n_tokens, n_seqs,
  15183. split_sizes_ba[0] * sizeof(float), mixed_ba_reshaped->nb[1], mixed_ba_reshaped->nb[2], 0);
  15184. ggml_tensor * a = ggml_view_4d(ctx0, mixed_ba_reshaped, split_sizes_ba[1], num_k_heads, n_tokens, n_seqs,
  15185. split_sizes_ba[1] * sizeof(float), mixed_ba_reshaped->nb[1],
  15186. mixed_ba_reshaped->nb[2], split_sizes_ba[0] * sizeof(float));
  15187. // Reshape b and a to merge head dimensions: [batch, seq_len, num_k_heads, num_v_heads/num_k_heads] -> [batch, seq_len, num_v_heads]
  15188. ggml_tensor * beta = ggml_reshape_3d(ctx0, ggml_cont(ctx0, b), num_v_heads, n_tokens, n_seqs);
  15189. ggml_tensor * alpha = ggml_reshape_3d(ctx0, ggml_cont(ctx0, a), num_v_heads, n_tokens, n_seqs);
  15190. GGML_ASSERT(ggml_nelements(beta) + ggml_nelements(alpha) == ggml_nelements(mixed_ba));
  15191. // Softplus would be nice...
  15192. ggml_tensor * alpha_biased = ggml_add(ctx0, alpha, model.layers[il].ssm_dt); // a + dt_bias
  15193. ggml_tensor * alpha_exp = ggml_exp(ctx0, alpha_biased); // exp(a + dt_bias)
  15194. ggml_tensor * one_tensor = ggml_new_tensor_1d(ctx0, GGML_TYPE_F32, 1); // Create scalar tensor
  15195. ggml_exp(ctx0, one_tensor); // make it a 1
  15196. ggml_tensor * one_plus_exp = ggml_add1(ctx0, alpha_exp, one_tensor); // 1 + exp(a + dt_bias)
  15197. ggml_tensor * alpha_softplus = ggml_log(ctx0, one_plus_exp); // log(1 + exp(...))
  15198. ggml_tensor * A_log_exp = ggml_exp(ctx0, model.layers[il].ssm_a); // A_log.exp()
  15199. ggml_tensor * gate_scaled = ggml_mul(ctx0, alpha_softplus, A_log_exp); // A_log.exp() * softplus
  15200. ggml_tensor * gate = ggml_neg(ctx0, gate_scaled); // - (A_log.exp() * softplus)
  15201. // Get convolution weights and bias
  15202. ggml_tensor * conv_weight = model.layers[il].ssm_conv1d;
  15203. ggml_tensor * conv_bias = nullptr; // Add if your model has conv bias
  15204. // Get recurrent states (conv_states not needed as it's handled internally by ggml_delta_net)
  15205. ggml_tensor * ssm_states_all = mctx_cur->get_s_l(il);
  15206. // Beta tensor
  15207. beta = ggml_reshape_3d(ctx0, beta, n_heads, n_tokens, n_seqs);
  15208. // Get current state slice
  15209. ggml_tensor * state = ggml_view_4d(ctx0, ssm_states_all, head_dim, head_dim, n_heads, n_seqs,
  15210. ssm_states_all->nb[0], ssm_states_all->nb[1], ssm_states_all->nb[2],
  15211. kv_head * head_dim * head_dim * n_heads * ggml_element_size(ssm_states_all));
  15212. state = ggml_cont(ctx0, state);
  15213. ggml_tensor * target_gate = ggml_new_tensor_4d(ctx0, GGML_TYPE_F32, head_dim, n_heads, n_tokens, n_seqs);
  15214. ggml_tensor * gate_broadcast = ggml_reshape_4d(ctx0, gate, 1, n_heads, n_tokens, n_seqs);
  15215. gate = ggml_repeat(ctx0, gate_broadcast, target_gate);
  15216. // Call the new ggml_delta_net function with the corrected flow
  15217. ggml_tensor * output = ggml_delta_net(ctx0,
  15218. key, // k tensor
  15219. value_reshaped, // v tensor
  15220. query, // q tensor
  15221. gate, // g tensor
  15222. conv_weight, // conv_weight tensor
  15223. conv_bias, // conv_bias tensor (can be nullptr)
  15224. beta, // beta tensor
  15225. state, // state tensor
  15226. 64, // chunk_size (adjust as needed)
  15227. true, // use_qk_l2norm
  15228. 1.0f // scale (adjust based on your model)
  15229. );
  15230. cb(output, "delta_net_output", il);
  15231. // Extract the output part (first half of the concatenated result)
  15232. ggml_tensor * attn_out = ggml_view_4d(ctx0, output, head_dim, n_heads, n_tokens, n_seqs, output->nb[0],
  15233. output->nb[1], output->nb[2], 0);
  15234. // Extract the new state (second half of the concatenated result)
  15235. ggml_tensor * new_state =
  15236. ggml_view_4d(ctx0, output, head_dim, head_dim, n_heads, n_seqs, output->nb[0], output->nb[1], output->nb[2],
  15237. n_tokens * head_dim * n_heads * sizeof(float));
  15238. // Update the recurrent states
  15239. ggml_build_forward_expand(
  15240. gf, ggml_cpy(ctx0, new_state,
  15241. ggml_view_1d(
  15242. ctx0, ssm_states_all, head_dim * head_dim * n_heads * n_seqs,
  15243. kv_head * n_seqs * head_dim * head_dim * n_heads * ggml_element_size(ssm_states_all))));
  15244. // Reshape both attn_out and z to 2D tensors for normalization
  15245. // attn_out: [head_dim, n_heads, n_tokens, n_seqs] -> [n_heads * n_tokens * n_seqs, head_dim]
  15246. ggml_tensor * attn_out_2d = ggml_reshape_2d(ctx0, ggml_cont(ctx0, attn_out), head_dim, n_heads * n_tokens * n_seqs);
  15247. // z: [head_dim, n_heads, n_tokens, n_seqs] -> [n_heads * n_tokens * n_seqs, head_dim]
  15248. ggml_tensor * z_2d = ggml_reshape_2d(ctx0, z_reshaped, head_dim, n_heads * n_tokens * n_seqs);
  15249. // Apply gated normalization: self.norm(core_attn_out, z)
  15250. // This is Qwen3NextRMSNormGated which applies: RMSNorm(x) * silu(gate)
  15251. ggml_tensor * attn_out_norm = build_norm(attn_out_2d, model.layers[il].ssm_norm, NULL, LLM_NORM_RMS, il);
  15252. // Apply silu gate: attn_out_norm * silu(z_2d)
  15253. ggml_tensor * z_silu = ggml_silu(ctx0, z_2d);
  15254. ggml_tensor * gated_output = ggml_mul(ctx0, attn_out_norm, z_silu);
  15255. // Reshape back to original dimensions: [n_heads * n_tokens * n_seqs, head_dim] -> [head_dim, n_heads, n_tokens, n_seqs]
  15256. ggml_tensor * gated_output_4d = ggml_reshape_4d(ctx0, gated_output, head_dim, n_heads, n_tokens, n_seqs);
  15257. // Final reshape: [head_dim, n_heads, n_tokens, n_seqs] -> [n_tokens, n_seqs, n_heads * head_dim]
  15258. ggml_tensor * final_output = ggml_reshape_3d(ctx0, gated_output_4d, n_heads * head_dim, n_tokens, n_seqs);
  15259. // Output projection
  15260. cur = build_lora_mm(model.layers[il].ssm_out, final_output);
  15261. cb(cur, "linear_attn_out", il);
  15262. // Reshape back to original dimensions
  15263. cur = ggml_cont(ctx0, ggml_reshape_2d(ctx0, cur, n_embd, n_tokens));
  15264. return cur;
  15265. }
  15266. ggml_tensor * build_layer_ffn(ggml_tensor * cur, const llama_model & model, const int il) {
  15267. // Check if this is an MoE layer
  15268. if (model.layers[il].ffn_gate_inp != nullptr) {
  15269. // MoE branch
  15270. ggml_tensor * moe_out = build_moe_ffn(cur,
  15271. model.layers[il].ffn_gate_inp,
  15272. model.layers[il].ffn_up_exps,
  15273. model.layers[il].ffn_gate_exps,
  15274. model.layers[il].ffn_down_exps,
  15275. nullptr,
  15276. n_expert, n_expert_used,
  15277. LLM_FFN_SILU, true,
  15278. false, 0.0,
  15279. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  15280. il);
  15281. cb(moe_out, "ffn_moe_out", il);
  15282. // Add shared experts if present
  15283. if (model.layers[il].ffn_up_shexp != nullptr) {
  15284. ggml_tensor * ffn_shexp = build_ffn(cur,
  15285. model.layers[il].ffn_up_shexp, NULL, NULL,
  15286. model.layers[il].ffn_gate_shexp, NULL, NULL,
  15287. model.layers[il].ffn_down_shexp, NULL, NULL,
  15288. NULL,
  15289. LLM_FFN_SILU, LLM_FFN_PAR, il);
  15290. cb(ffn_shexp, "ffn_shexp", il);
  15291. cur = ggml_add(ctx0, moe_out, ffn_shexp);
  15292. cb(cur, "ffn_out", il);
  15293. } else {
  15294. cur = moe_out;
  15295. }
  15296. } else {
  15297. // Dense FFN branch
  15298. cur = build_ffn(cur,
  15299. model.layers[il].ffn_up, NULL, NULL,
  15300. model.layers[il].ffn_gate, NULL, NULL,
  15301. model.layers[il].ffn_down, NULL, NULL,
  15302. NULL,
  15303. LLM_FFN_SILU, LLM_FFN_PAR, il);
  15304. cb(cur, "ffn_out", il);
  15305. }
  15306. // Residual connection
  15307. cur = ggml_add(ctx0, cur, cur); // This should be the residual from before FFN
  15308. cb(cur, "ffn_residual", il);
  15309. return cur;
  15310. }
  15311. };
  15312. llama_memory_i * llama_model::create_memory(const llama_memory_params & params, llama_cparams & cparams) const {
  15313. llama_memory_i * res;
  15314. switch (arch) {
  15315. // Models that need specific instantiation should be handled in the
  15316. // switch statement
  15317. case LLM_ARCH_BERT:
  15318. case LLM_ARCH_JINA_BERT_V2:
  15319. case LLM_ARCH_JINA_BERT_V3:
  15320. case LLM_ARCH_NOMIC_BERT:
  15321. case LLM_ARCH_NOMIC_BERT_MOE:
  15322. case LLM_ARCH_NEO_BERT:
  15323. case LLM_ARCH_WAVTOKENIZER_DEC:
  15324. //case LLM_ARCH_GEMMA_EMBEDDING: // TODO: disabled until the cacheless SWA logic is fixed [TAG_NO_CACHE_ISWA]
  15325. case LLM_ARCH_DREAM:
  15326. case LLM_ARCH_LLADA:
  15327. case LLM_ARCH_LLADA_MOE:
  15328. {
  15329. res = nullptr;
  15330. } break;
  15331. // Models that need standard caching should rely on recurrent/hybrid
  15332. // checks
  15333. default:
  15334. {
  15335. if (llm_arch_is_recurrent(arch)) {
  15336. res = new llama_memory_recurrent(
  15337. *this,
  15338. GGML_TYPE_F32,
  15339. GGML_TYPE_F32,
  15340. cparams.offload_kqv,
  15341. std::max((uint32_t) 1, cparams.n_seq_max),
  15342. cparams.n_seq_max,
  15343. nullptr);
  15344. } else if (llm_arch_is_hybrid(arch)) {
  15345. // The main difference between hybrid architectures is the
  15346. // layer filters, so pick the right one here
  15347. llama_memory_hybrid::layer_filter_cb filter_attn = nullptr;
  15348. llama_memory_hybrid::layer_filter_cb filter_recr = nullptr;
  15349. if (arch == LLM_ARCH_FALCON_H1) {
  15350. filter_attn = [&](int32_t) { return true; };
  15351. filter_recr = [&](int32_t) { return true; };
  15352. } else if (arch == LLM_ARCH_NEMOTRON_H) {
  15353. filter_attn = [&](int32_t il) {
  15354. return !hparams.is_recurrent(il) && hparams.n_ff(il) == 0;
  15355. };
  15356. filter_recr = [&](int32_t il) {
  15357. return hparams.is_recurrent(il) && hparams.n_ff(il) == 0;
  15358. };
  15359. }
  15360. const auto padding = llama_kv_cache::get_padding(cparams);
  15361. cparams.n_ctx = GGML_PAD(cparams.n_ctx, padding);
  15362. res = new llama_memory_hybrid(
  15363. /* model */ *this,
  15364. /* attn_type_k */ params.type_k,
  15365. /* attn_type_v */ params.type_v,
  15366. /* attn_v_trans */ !cparams.flash_attn,
  15367. /* attn_kv_size */ cparams.n_ctx,
  15368. /* attn_n_pad */ padding,
  15369. /* attn_n_swa */ hparams.n_swa,
  15370. /* attn_swa_type */ hparams.swa_type,
  15371. /* recurrent_type_k */ GGML_TYPE_F32,
  15372. /* recurrent_type_v */ GGML_TYPE_F32,
  15373. /* recurrent_kv_size */ std::max((uint32_t) 1, cparams.n_seq_max),
  15374. /* n_seq_max */ cparams.n_seq_max,
  15375. /* offload */ cparams.offload_kqv,
  15376. /* unified */ cparams.kv_unified,
  15377. /* filter_attn */ std::move(filter_attn),
  15378. /* filter_recr */ std::move(filter_recr));
  15379. } else {
  15380. const auto padding = llama_kv_cache::get_padding(cparams);
  15381. uint32_t n_ctx_per_stream = cparams.n_ctx;
  15382. if (!cparams.kv_unified) {
  15383. n_ctx_per_stream = (cparams.n_ctx + cparams.n_seq_max - 1)/cparams.n_seq_max;
  15384. n_ctx_per_stream = GGML_PAD(n_ctx_per_stream, padding);
  15385. cparams.n_ctx = n_ctx_per_stream*cparams.n_seq_max;
  15386. } else {
  15387. n_ctx_per_stream = GGML_PAD(n_ctx_per_stream, padding);
  15388. cparams.n_ctx = n_ctx_per_stream;
  15389. }
  15390. LLAMA_LOG_DEBUG("%s: n_ctx = %u (padded)\n", __func__, cparams.n_ctx);
  15391. llama_memory_i::layer_reuse_cb reuse = nullptr;
  15392. if (arch == LLM_ARCH_GEMMA3N) {
  15393. reuse = [&](int32_t il) {
  15394. if (il >= (int32_t) hparams.n_layer_kv_from_start) {
  15395. return (int32_t) hparams.n_layer_kv_from_start - (hparams.is_swa(il) ? 2 : 1);
  15396. }
  15397. return -1;
  15398. };
  15399. }
  15400. if (hparams.swa_type != LLAMA_SWA_TYPE_NONE) {
  15401. GGML_ASSERT(hparams.is_swa_any());
  15402. res = new llama_kv_cache_iswa(
  15403. *this,
  15404. params.type_k,
  15405. params.type_v,
  15406. !cparams.flash_attn,
  15407. cparams.offload_kqv,
  15408. params.swa_full,
  15409. cparams.kv_unified,
  15410. n_ctx_per_stream,
  15411. cparams.n_seq_max,
  15412. cparams.n_ubatch,
  15413. padding,
  15414. nullptr,
  15415. reuse);
  15416. } else {
  15417. GGML_ASSERT(!hparams.is_swa_any());
  15418. res = new llama_kv_cache(
  15419. *this,
  15420. params.type_k,
  15421. params.type_v,
  15422. !cparams.flash_attn,
  15423. cparams.offload_kqv,
  15424. cparams.kv_unified,
  15425. n_ctx_per_stream,
  15426. cparams.n_seq_max,
  15427. padding,
  15428. hparams.n_swa,
  15429. hparams.swa_type,
  15430. nullptr,
  15431. nullptr);
  15432. }
  15433. }
  15434. }
  15435. }
  15436. return res;
  15437. }
  15438. ggml_cgraph * llama_model::build_graph(const llm_graph_params & params) const {
  15439. std::unique_ptr<llm_graph_context> llm;
  15440. switch (arch) {
  15441. case LLM_ARCH_LLAMA:
  15442. {
  15443. llm = std::make_unique<llm_build_llama>(*this, params);
  15444. } break;
  15445. case LLM_ARCH_LLAMA4:
  15446. {
  15447. if (hparams.swa_type == LLAMA_SWA_TYPE_NONE) {
  15448. llm = std::make_unique<llm_build_llama>(*this, params);
  15449. } else {
  15450. llm = std::make_unique<llm_build_llama_iswa>(*this, params);
  15451. }
  15452. } break;
  15453. case LLM_ARCH_DECI:
  15454. {
  15455. llm = std::make_unique<llm_build_deci>(*this, params);
  15456. } break;
  15457. case LLM_ARCH_BAICHUAN:
  15458. {
  15459. llm = std::make_unique<llm_build_baichuan>(*this, params);
  15460. } break;
  15461. case LLM_ARCH_FALCON:
  15462. {
  15463. llm = std::make_unique<llm_build_falcon>(*this, params);
  15464. } break;
  15465. case LLM_ARCH_GROK:
  15466. {
  15467. llm = std::make_unique<llm_build_grok>(*this, params);
  15468. } break;
  15469. case LLM_ARCH_STARCODER:
  15470. {
  15471. llm = std::make_unique<llm_build_starcoder>(*this, params);
  15472. } break;
  15473. case LLM_ARCH_REFACT:
  15474. {
  15475. llm = std::make_unique<llm_build_refact>(*this, params);
  15476. } break;
  15477. case LLM_ARCH_BERT:
  15478. case LLM_ARCH_JINA_BERT_V2:
  15479. case LLM_ARCH_JINA_BERT_V3:
  15480. case LLM_ARCH_NOMIC_BERT:
  15481. case LLM_ARCH_NOMIC_BERT_MOE:
  15482. {
  15483. llm = std::make_unique<llm_build_bert>(*this, params);
  15484. } break;
  15485. case LLM_ARCH_NEO_BERT:
  15486. {
  15487. llm = std::make_unique<llm_build_neo_bert>(*this, params);
  15488. } break;
  15489. case LLM_ARCH_BLOOM:
  15490. {
  15491. llm = std::make_unique<llm_build_bloom>(*this, params);
  15492. } break;
  15493. case LLM_ARCH_MPT:
  15494. {
  15495. llm = std::make_unique<llm_build_mpt>(*this, params);
  15496. } break;
  15497. case LLM_ARCH_STABLELM:
  15498. {
  15499. llm = std::make_unique<llm_build_stablelm>(*this, params);
  15500. } break;
  15501. case LLM_ARCH_QWEN:
  15502. {
  15503. llm = std::make_unique<llm_build_qwen>(*this, params);
  15504. } break;
  15505. case LLM_ARCH_QWEN2:
  15506. {
  15507. llm = std::make_unique<llm_build_qwen2>(*this, params);
  15508. } break;
  15509. case LLM_ARCH_DREAM:
  15510. {
  15511. llm = std::make_unique<llm_build_dream>(*this, params);
  15512. }
  15513. break;
  15514. case LLM_ARCH_LLADA:
  15515. {
  15516. llm = std::make_unique<llm_build_llada>(*this, params);
  15517. }
  15518. break;
  15519. case LLM_ARCH_LLADA_MOE:
  15520. {
  15521. llm = std::make_unique<llm_build_llada_moe>(*this, params);
  15522. }
  15523. break;
  15524. case LLM_ARCH_QWEN2VL:
  15525. {
  15526. llm = std::make_unique<llm_build_qwen2vl>(*this, params);
  15527. } break;
  15528. case LLM_ARCH_QWEN2MOE:
  15529. {
  15530. llm = std::make_unique<llm_build_qwen2moe>(*this, params);
  15531. } break;
  15532. case LLM_ARCH_QWEN3:
  15533. {
  15534. llm = std::make_unique<llm_build_qwen3>(*this, params);
  15535. } break;
  15536. case LLM_ARCH_QWEN3MOE:
  15537. {
  15538. llm = std::make_unique<llm_build_qwen3moe>(*this, params);
  15539. } break;
  15540. case LLM_ARCH_PHI2:
  15541. {
  15542. llm = std::make_unique<llm_build_phi2>(*this, params);
  15543. } break;
  15544. case LLM_ARCH_PHI3:
  15545. case LLM_ARCH_PHIMOE:
  15546. {
  15547. if (hparams.swa_type != LLAMA_SWA_TYPE_NONE) {
  15548. llm = std::make_unique<llm_build_phi3<true>> (*this, params);
  15549. } else {
  15550. llm = std::make_unique<llm_build_phi3<false>>(*this, params);
  15551. }
  15552. } break;
  15553. case LLM_ARCH_PLAMO:
  15554. {
  15555. llm = std::make_unique<llm_build_plamo>(*this, params);
  15556. } break;
  15557. case LLM_ARCH_PLAMO2:
  15558. {
  15559. llm = std::make_unique<llm_build_plamo2>(*this, params);
  15560. } break;
  15561. case LLM_ARCH_GPT2:
  15562. {
  15563. llm = std::make_unique<llm_build_gpt2>(*this, params);
  15564. } break;
  15565. case LLM_ARCH_CODESHELL:
  15566. {
  15567. llm = std::make_unique<llm_build_codeshell>(*this, params);
  15568. } break;
  15569. case LLM_ARCH_ORION:
  15570. {
  15571. llm = std::make_unique<llm_build_orion>(*this, params);
  15572. } break;
  15573. case LLM_ARCH_INTERNLM2:
  15574. {
  15575. llm = std::make_unique<llm_build_internlm2>(*this, params);
  15576. } break;
  15577. case LLM_ARCH_MINICPM3:
  15578. {
  15579. llm = std::make_unique<llm_build_minicpm3>(*this, params);
  15580. } break;
  15581. case LLM_ARCH_GEMMA:
  15582. {
  15583. llm = std::make_unique<llm_build_gemma>(*this, params);
  15584. } break;
  15585. case LLM_ARCH_GEMMA2:
  15586. {
  15587. llm = std::make_unique<llm_build_gemma2_iswa>(*this, params);
  15588. } break;
  15589. case LLM_ARCH_GEMMA3:
  15590. {
  15591. llm = std::make_unique<llm_build_gemma3_iswa>(*this, params);
  15592. } break;
  15593. case LLM_ARCH_GEMMA3N:
  15594. {
  15595. llm = std::make_unique<llm_build_gemma3n_iswa>(*this, params);
  15596. } break;
  15597. case LLM_ARCH_GEMMA_EMBEDDING:
  15598. {
  15599. llm = std::make_unique<llm_build_gemma_embedding_iswa>(*this, params);
  15600. } break;
  15601. case LLM_ARCH_STARCODER2:
  15602. {
  15603. llm = std::make_unique<llm_build_starcoder2>(*this, params);
  15604. } break;
  15605. case LLM_ARCH_MAMBA:
  15606. case LLM_ARCH_MAMBA2:
  15607. {
  15608. llm = std::make_unique<llm_build_mamba>(*this, params);
  15609. } break;
  15610. case LLM_ARCH_JAMBA:
  15611. {
  15612. llm = std::make_unique<llm_build_jamba>(*this, params);
  15613. } break;
  15614. case LLM_ARCH_XVERSE:
  15615. {
  15616. llm = std::make_unique<llm_build_xverse>(*this, params);
  15617. } break;
  15618. case LLM_ARCH_COMMAND_R:
  15619. {
  15620. llm = std::make_unique<llm_build_command_r>(*this, params);
  15621. } break;
  15622. case LLM_ARCH_COHERE2:
  15623. {
  15624. llm = std::make_unique<llm_build_cohere2_iswa>(*this, params);
  15625. } break;
  15626. case LLM_ARCH_DBRX:
  15627. {
  15628. llm = std::make_unique<llm_build_dbrx>(*this, params);
  15629. } break;
  15630. case LLM_ARCH_OLMO:
  15631. {
  15632. llm = std::make_unique<llm_build_olmo>(*this, params);
  15633. } break;
  15634. case LLM_ARCH_OLMO2:
  15635. {
  15636. if (hparams.swa_type == LLAMA_SWA_TYPE_STANDARD) {
  15637. llm = std::make_unique<llm_build_olmo2<true>>(*this, params);
  15638. } else {
  15639. llm = std::make_unique<llm_build_olmo2<false>>(*this, params);
  15640. }
  15641. } break;
  15642. case LLM_ARCH_OLMOE:
  15643. {
  15644. llm = std::make_unique<llm_build_olmoe>(*this, params);
  15645. } break;
  15646. case LLM_ARCH_OPENELM:
  15647. {
  15648. llm = std::make_unique<llm_build_openelm>(*this, params);
  15649. } break;
  15650. case LLM_ARCH_GPTNEOX:
  15651. {
  15652. llm = std::make_unique<llm_build_gptneox>(*this, params);
  15653. } break;
  15654. case LLM_ARCH_ARCTIC:
  15655. {
  15656. llm = std::make_unique<llm_build_arctic>(*this, params);
  15657. } break;
  15658. case LLM_ARCH_DEEPSEEK:
  15659. {
  15660. llm = std::make_unique<llm_build_deepseek>(*this, params);
  15661. } break;
  15662. case LLM_ARCH_DEEPSEEK2:
  15663. {
  15664. llm = std::make_unique<llm_build_deepseek2>(*this, params);
  15665. } break;
  15666. case LLM_ARCH_CHATGLM:
  15667. {
  15668. llm = std::make_unique<llm_build_chatglm>(*this, params);
  15669. } break;
  15670. case LLM_ARCH_GLM4:
  15671. {
  15672. llm = std::make_unique<llm_build_glm4>(*this, params);
  15673. } break;
  15674. case LLM_ARCH_GLM4_MOE:
  15675. {
  15676. llm = std::make_unique<llm_build_glm4_moe>(*this, params);
  15677. } break;
  15678. case LLM_ARCH_BITNET:
  15679. {
  15680. llm = std::make_unique<llm_build_bitnet>(*this, params);
  15681. } break;
  15682. case LLM_ARCH_T5:
  15683. {
  15684. switch (params.gtype) {
  15685. case LLM_GRAPH_TYPE_ENCODER:
  15686. llm = std::make_unique<llm_build_t5_enc>(*this, params);
  15687. break;
  15688. case LLM_GRAPH_TYPE_DEFAULT:
  15689. case LLM_GRAPH_TYPE_DECODER:
  15690. llm = std::make_unique<llm_build_t5_dec>(*this, params);
  15691. break;
  15692. default:
  15693. GGML_ABORT("invalid graph type");
  15694. };
  15695. } break;
  15696. case LLM_ARCH_T5ENCODER:
  15697. {
  15698. llm = std::make_unique<llm_build_t5_enc>(*this, params);
  15699. }
  15700. break;
  15701. case LLM_ARCH_JAIS:
  15702. {
  15703. llm = std::make_unique<llm_build_jais>(*this, params);
  15704. } break;
  15705. case LLM_ARCH_NEMOTRON:
  15706. {
  15707. llm = std::make_unique<llm_build_nemotron>(*this, params);
  15708. } break;
  15709. case LLM_ARCH_NEMOTRON_H:
  15710. {
  15711. llm = std::make_unique<llm_build_nemotron_h>(*this, params);
  15712. } break;
  15713. case LLM_ARCH_EXAONE:
  15714. {
  15715. llm = std::make_unique<llm_build_exaone>(*this, params);
  15716. } break;
  15717. case LLM_ARCH_EXAONE4:
  15718. {
  15719. if (hparams.swa_type == LLAMA_SWA_TYPE_STANDARD) {
  15720. llm = std::make_unique<llm_build_exaone4<true>>(*this, params);
  15721. } else {
  15722. llm = std::make_unique<llm_build_exaone4<false>>(*this, params);
  15723. }
  15724. } break;
  15725. case LLM_ARCH_RWKV6:
  15726. {
  15727. llm = std::make_unique<llm_build_rwkv6>(*this, params);
  15728. } break;
  15729. case LLM_ARCH_RWKV6QWEN2:
  15730. {
  15731. llm = std::make_unique<llm_build_rwkv6qwen2>(*this, params);
  15732. } break;
  15733. case LLM_ARCH_RWKV7:
  15734. {
  15735. llm = std::make_unique<llm_build_rwkv7>(*this, params);
  15736. } break;
  15737. case LLM_ARCH_ARWKV7:
  15738. {
  15739. llm = std::make_unique<llm_build_arwkv7>(*this, params);
  15740. } break;
  15741. case LLM_ARCH_GRANITE:
  15742. case LLM_ARCH_GRANITE_MOE:
  15743. case LLM_ARCH_MINICPM:
  15744. {
  15745. llm = std::make_unique<llm_build_granite>(*this, params);
  15746. } break;
  15747. case LLM_ARCH_GRANITE_HYBRID:
  15748. {
  15749. llm = std::make_unique<llm_build_granite_hybrid>(*this, params);
  15750. } break;
  15751. case LLM_ARCH_CHAMELEON:
  15752. {
  15753. llm = std::make_unique<llm_build_chameleon>(*this, params);
  15754. } break;
  15755. case LLM_ARCH_WAVTOKENIZER_DEC:
  15756. {
  15757. llm = std::make_unique<llm_build_wavtokenizer_dec>(*this, params);
  15758. } break;
  15759. case LLM_ARCH_PLM:
  15760. {
  15761. llm = std::make_unique<llm_build_plm>(*this, params);
  15762. } break;
  15763. case LLM_ARCH_BAILINGMOE:
  15764. {
  15765. llm = std::make_unique<llm_build_bailingmoe>(*this, params);
  15766. } break;
  15767. case LLM_ARCH_SEED_OSS:
  15768. {
  15769. llm = std::make_unique<llm_build_seed_oss>(*this, params);
  15770. } break;
  15771. case LLM_ARCH_DOTS1:
  15772. {
  15773. llm = std::make_unique<llm_build_dots1>(*this, params);
  15774. } break;
  15775. case LLM_ARCH_ARCEE:
  15776. {
  15777. llm = std::make_unique<llm_build_arcee>(*this, params);
  15778. } break;
  15779. case LLM_ARCH_ERNIE4_5:
  15780. {
  15781. llm = std::make_unique<llm_build_ernie4_5>(*this, params);
  15782. } break;
  15783. case LLM_ARCH_ERNIE4_5_MOE:
  15784. {
  15785. llm = std::make_unique<llm_build_ernie4_5_moe>(*this, params);
  15786. } break;
  15787. case LLM_ARCH_HUNYUAN_MOE:
  15788. {
  15789. llm = std::make_unique<llm_build_hunyuan_moe>(*this, params);
  15790. } break;
  15791. case LLM_ARCH_HUNYUAN_DENSE:
  15792. {
  15793. llm = std::make_unique<llm_build_hunyuan_dense>(*this, params);
  15794. } break;
  15795. case LLM_ARCH_SMOLLM3:
  15796. {
  15797. llm = std::make_unique<llm_build_smollm3>(*this, params);
  15798. } break;
  15799. case LLM_ARCH_OPENAI_MOE:
  15800. {
  15801. llm = std::make_unique<llm_build_openai_moe_iswa>(*this, params);
  15802. } break;
  15803. case LLM_ARCH_FALCON_H1:
  15804. {
  15805. llm = std::make_unique<llm_build_falcon_h1>(*this, params);
  15806. } break;
  15807. case LLM_ARCH_LFM2:
  15808. {
  15809. llm = std::make_unique<llm_build_lfm2>(*this, params);
  15810. } break;
  15811. case LLM_ARCH_SMALLTHINKER:
  15812. {
  15813. if (hparams.swa_type == LLAMA_SWA_TYPE_STANDARD) {
  15814. llm = std::make_unique<llm_build_smallthinker<true>> (*this, params);
  15815. } else {
  15816. llm = std::make_unique<llm_build_smallthinker<false>>(*this, params);
  15817. }
  15818. } break;
  15819. case LLM_ARCH_QWEN3NEXT:
  15820. {
  15821. llm = std::make_unique<llm_build_qwen3next>(*this, params);
  15822. } break;
  15823. default:
  15824. GGML_ABORT("fatal error");
  15825. }
  15826. // add on pooling layer
  15827. llm->build_pooling(cls, cls_b, cls_out, cls_out_b);
  15828. return llm->res->get_gf();
  15829. }
  15830. //
  15831. // interface implementation
  15832. //
  15833. llama_model_params llama_model_default_params() {
  15834. llama_model_params result = {
  15835. /*.devices =*/ nullptr,
  15836. /*.tensor_buft_overrides =*/ nullptr,
  15837. /*.n_gpu_layers =*/ 999,
  15838. /*.split_mode =*/ LLAMA_SPLIT_MODE_LAYER,
  15839. /*.main_gpu =*/ 0,
  15840. /*.tensor_split =*/ nullptr,
  15841. /*.progress_callback =*/ nullptr,
  15842. /*.progress_callback_user_data =*/ nullptr,
  15843. /*.kv_overrides =*/ nullptr,
  15844. /*.vocab_only =*/ false,
  15845. /*.use_mmap =*/ true,
  15846. /*.use_mlock =*/ false,
  15847. /*.check_tensors =*/ false,
  15848. /*.use_extra_bufts =*/ true,
  15849. };
  15850. return result;
  15851. }
  15852. const llama_vocab * llama_model_get_vocab(const llama_model * model) {
  15853. return &model->vocab;
  15854. }
  15855. void llama_free_model(llama_model * model) {
  15856. llama_model_free(model);
  15857. }
  15858. void llama_model_free(llama_model * model) {
  15859. delete model;
  15860. }
  15861. int32_t llama_model_n_ctx_train(const llama_model * model) {
  15862. return model->hparams.n_ctx_train;
  15863. }
  15864. int32_t llama_model_n_embd(const llama_model * model) {
  15865. return model->hparams.n_embd;
  15866. }
  15867. int32_t llama_model_n_layer(const llama_model * model) {
  15868. return model->hparams.n_layer;
  15869. }
  15870. int32_t llama_model_n_head(const llama_model * model) {
  15871. return model->hparams.n_head();
  15872. }
  15873. int32_t llama_model_n_head_kv(const llama_model * model) {
  15874. return model->hparams.n_head_kv();
  15875. }
  15876. int32_t llama_model_n_swa(const llama_model * model) {
  15877. return model->hparams.n_swa;
  15878. }
  15879. uint32_t llama_model_n_cls_out(const struct llama_model * model) {
  15880. return model->hparams.n_cls_out;
  15881. }
  15882. const char * llama_model_cls_label(const struct llama_model * model, uint32_t i) {
  15883. if (i < model->classifier_labels.size()) {
  15884. return model->classifier_labels[i].c_str();
  15885. }
  15886. return nullptr;
  15887. }
  15888. // deprecated
  15889. int32_t llama_n_ctx_train(const llama_model * model) {
  15890. return llama_model_n_ctx_train(model);
  15891. }
  15892. // deprecated
  15893. int32_t llama_n_embd(const llama_model * model) {
  15894. return llama_model_n_embd(model);
  15895. }
  15896. // deprecated
  15897. int32_t llama_n_layer(const llama_model * model) {
  15898. return llama_model_n_layer(model);
  15899. }
  15900. // deprecated
  15901. int32_t llama_n_head(const llama_model * model) {
  15902. return llama_model_n_head(model);
  15903. }
  15904. llama_rope_type llama_model_rope_type(const llama_model * model) {
  15905. switch (model->arch) {
  15906. // these models do not use RoPE
  15907. case LLM_ARCH_GPT2:
  15908. case LLM_ARCH_GPTJ:
  15909. case LLM_ARCH_MPT:
  15910. case LLM_ARCH_REFACT:
  15911. case LLM_ARCH_BLOOM:
  15912. case LLM_ARCH_MAMBA:
  15913. case LLM_ARCH_MAMBA2:
  15914. case LLM_ARCH_JAMBA:
  15915. case LLM_ARCH_JINA_BERT_V2:
  15916. case LLM_ARCH_T5:
  15917. case LLM_ARCH_T5ENCODER:
  15918. case LLM_ARCH_JAIS:
  15919. case LLM_ARCH_RWKV6:
  15920. case LLM_ARCH_RWKV6QWEN2:
  15921. case LLM_ARCH_RWKV7:
  15922. case LLM_ARCH_ARWKV7:
  15923. case LLM_ARCH_WAVTOKENIZER_DEC:
  15924. case LLM_ARCH_NEMOTRON_H:
  15925. return LLAMA_ROPE_TYPE_NONE;
  15926. // use what we call a normal RoPE, operating on pairs of consecutive head values
  15927. case LLM_ARCH_LLAMA:
  15928. case LLM_ARCH_LLADA:
  15929. case LLM_ARCH_LLAMA4:
  15930. case LLM_ARCH_DECI:
  15931. case LLM_ARCH_BAICHUAN:
  15932. case LLM_ARCH_STARCODER:
  15933. case LLM_ARCH_INTERNLM2:
  15934. case LLM_ARCH_MINICPM:
  15935. case LLM_ARCH_XVERSE:
  15936. case LLM_ARCH_COMMAND_R:
  15937. case LLM_ARCH_COHERE2:
  15938. case LLM_ARCH_OLMO:
  15939. case LLM_ARCH_ARCTIC:
  15940. case LLM_ARCH_DEEPSEEK:
  15941. case LLM_ARCH_DEEPSEEK2:
  15942. case LLM_ARCH_PLM:
  15943. case LLM_ARCH_CHATGLM:
  15944. case LLM_ARCH_GLM4:
  15945. case LLM_ARCH_GRANITE:
  15946. case LLM_ARCH_GRANITE_MOE:
  15947. case LLM_ARCH_GRANITE_HYBRID:
  15948. case LLM_ARCH_CHAMELEON:
  15949. case LLM_ARCH_BAILINGMOE:
  15950. case LLM_ARCH_NEO_BERT:
  15951. case LLM_ARCH_SMOLLM3:
  15952. case LLM_ARCH_ARCEE:
  15953. case LLM_ARCH_ERNIE4_5:
  15954. case LLM_ARCH_ERNIE4_5_MOE:
  15955. return LLAMA_ROPE_TYPE_NORM;
  15956. // the pairs of head values are offset by n_rot/2
  15957. case LLM_ARCH_FALCON:
  15958. case LLM_ARCH_FALCON_H1:
  15959. case LLM_ARCH_GROK:
  15960. case LLM_ARCH_DBRX:
  15961. case LLM_ARCH_BERT:
  15962. case LLM_ARCH_JINA_BERT_V3:
  15963. case LLM_ARCH_NOMIC_BERT:
  15964. case LLM_ARCH_NOMIC_BERT_MOE:
  15965. case LLM_ARCH_STABLELM:
  15966. case LLM_ARCH_BITNET:
  15967. case LLM_ARCH_QWEN:
  15968. case LLM_ARCH_QWEN2:
  15969. case LLM_ARCH_DREAM:
  15970. case LLM_ARCH_QWEN2MOE:
  15971. case LLM_ARCH_QWEN3:
  15972. case LLM_ARCH_QWEN3MOE:
  15973. case LLM_ARCH_QWEN3NEXT:
  15974. case LLM_ARCH_LLADA_MOE:
  15975. case LLM_ARCH_OLMO2:
  15976. case LLM_ARCH_OLMOE:
  15977. case LLM_ARCH_PHI2:
  15978. case LLM_ARCH_PHI3:
  15979. case LLM_ARCH_PHIMOE:
  15980. case LLM_ARCH_PLAMO:
  15981. case LLM_ARCH_PLAMO2:
  15982. case LLM_ARCH_GEMMA:
  15983. case LLM_ARCH_GEMMA2:
  15984. case LLM_ARCH_GEMMA3:
  15985. case LLM_ARCH_GEMMA3N:
  15986. case LLM_ARCH_GEMMA_EMBEDDING:
  15987. case LLM_ARCH_STARCODER2:
  15988. case LLM_ARCH_OPENELM:
  15989. case LLM_ARCH_GPTNEOX:
  15990. case LLM_ARCH_CODESHELL:
  15991. case LLM_ARCH_ORION:
  15992. case LLM_ARCH_NEMOTRON:
  15993. case LLM_ARCH_EXAONE:
  15994. case LLM_ARCH_EXAONE4:
  15995. case LLM_ARCH_MINICPM3:
  15996. case LLM_ARCH_DOTS1:
  15997. case LLM_ARCH_HUNYUAN_MOE:
  15998. case LLM_ARCH_OPENAI_MOE:
  15999. case LLM_ARCH_HUNYUAN_DENSE:
  16000. case LLM_ARCH_LFM2:
  16001. case LLM_ARCH_SMALLTHINKER:
  16002. case LLM_ARCH_GLM4_MOE:
  16003. case LLM_ARCH_SEED_OSS:
  16004. return LLAMA_ROPE_TYPE_NEOX;
  16005. case LLM_ARCH_QWEN2VL:
  16006. return LLAMA_ROPE_TYPE_MROPE;
  16007. // all model arches should be listed explicitly here
  16008. case LLM_ARCH_UNKNOWN:
  16009. GGML_ABORT("unknown architecture");
  16010. }
  16011. return LLAMA_ROPE_TYPE_NONE;
  16012. }
  16013. float llama_model_rope_freq_scale_train(const llama_model * model) {
  16014. return model->hparams.rope_freq_scale_train;
  16015. }
  16016. int32_t llama_model_meta_val_str(const llama_model * model, const char * key, char * buf, size_t buf_size) {
  16017. const auto & it = model->gguf_kv.find(key);
  16018. if (it == model->gguf_kv.end()) {
  16019. if (buf_size > 0) {
  16020. buf[0] = '\0';
  16021. }
  16022. return -1;
  16023. }
  16024. return snprintf(buf, buf_size, "%s", it->second.c_str());
  16025. }
  16026. int32_t llama_model_meta_count(const llama_model * model) {
  16027. return (int)model->gguf_kv.size();
  16028. }
  16029. int32_t llama_model_meta_key_by_index(const llama_model * model, int i, char * buf, size_t buf_size) {
  16030. if (i < 0 || i >= (int)model->gguf_kv.size()) {
  16031. if (buf_size > 0) {
  16032. buf[0] = '\0';
  16033. }
  16034. return -1;
  16035. }
  16036. auto it = model->gguf_kv.begin();
  16037. std::advance(it, i);
  16038. return snprintf(buf, buf_size, "%s", it->first.c_str());
  16039. }
  16040. int32_t llama_model_meta_val_str_by_index(const llama_model * model, int32_t i, char * buf, size_t buf_size) {
  16041. if (i < 0 || i >= (int)model->gguf_kv.size()) {
  16042. if (buf_size > 0) {
  16043. buf[0] = '\0';
  16044. }
  16045. return -1;
  16046. }
  16047. auto it = model->gguf_kv.begin();
  16048. std::advance(it, i);
  16049. return snprintf(buf, buf_size, "%s", it->second.c_str());
  16050. }
  16051. int32_t llama_model_desc(const llama_model * model, char * buf, size_t buf_size) {
  16052. return snprintf(buf, buf_size, "%s", model->desc().c_str());
  16053. }
  16054. uint64_t llama_model_size(const llama_model * model) {
  16055. return model->size();
  16056. }
  16057. const char * llama_model_chat_template(const llama_model * model, const char * name) {
  16058. const auto key = name ? LLM_KV(model->arch, name)(LLM_KV_TOKENIZER_CHAT_TEMPLATE)
  16059. : LLM_KV(model->arch)(LLM_KV_TOKENIZER_CHAT_TEMPLATE);
  16060. const auto & it = model->gguf_kv.find(key);
  16061. if (it == model->gguf_kv.end()) {
  16062. // one-off fix for very popular models (so we are not flooded with issues)
  16063. // do not extend this list unless absolutely necessary
  16064. // Mistral-Small-2503 does not have built-in chat template
  16065. llama_vocab_pre_type pre_type = model->vocab.get_pre_type();
  16066. if (!name && pre_type == LLAMA_VOCAB_PRE_TYPE_TEKKEN && model->layers.size() == 40) {
  16067. return "mistral-v7-tekken";
  16068. }
  16069. return nullptr;
  16070. }
  16071. return it->second.c_str();
  16072. }
  16073. uint64_t llama_model_n_params(const llama_model * model) {
  16074. return model->n_elements();
  16075. }
  16076. bool llama_model_has_encoder(const llama_model * model) {
  16077. switch (model->arch) {
  16078. case LLM_ARCH_T5: return true;
  16079. case LLM_ARCH_T5ENCODER: return true;
  16080. default: return false;
  16081. }
  16082. }
  16083. bool llama_model_has_decoder(const llama_model * model) {
  16084. switch (model->arch) {
  16085. case LLM_ARCH_T5ENCODER: return false;
  16086. default: return true;
  16087. }
  16088. }
  16089. llama_token llama_model_decoder_start_token(const llama_model * model) {
  16090. return model->hparams.dec_start_token_id;
  16091. }
  16092. bool llama_model_is_recurrent(const llama_model * model) {
  16093. return llm_arch_is_recurrent(model->arch);
  16094. }
  16095. bool llama_model_is_diffusion(const llama_model * model) {
  16096. return llm_arch_is_diffusion(model->arch);
  16097. }
  16098. const std::vector<std::pair<std::string, ggml_tensor *>> & llama_internal_get_tensor_map(const llama_model * model) {
  16099. return model->tensors_by_name;
  16100. }