llama-model.cpp 901 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651
  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 <cfloat>
  15. #include <cstring>
  16. #include <cmath>
  17. #include <functional>
  18. #include <map>
  19. #include <regex>
  20. #include <sstream>
  21. #include <stdexcept>
  22. const char * llm_type_name(llm_type type) {
  23. switch (type) {
  24. case LLM_TYPE_14M: return "14M";
  25. case LLM_TYPE_17M: return "17M";
  26. case LLM_TYPE_22M: return "22M";
  27. case LLM_TYPE_33M: return "33M";
  28. case LLM_TYPE_60M: return "60M";
  29. case LLM_TYPE_70M: return "70M";
  30. case LLM_TYPE_80M: return "80M";
  31. case LLM_TYPE_109M: return "109M";
  32. case LLM_TYPE_137M: return "137M";
  33. case LLM_TYPE_140M: return "140M";
  34. case LLM_TYPE_160M: return "160M";
  35. case LLM_TYPE_190M: return "190M";
  36. case LLM_TYPE_220M: return "220M";
  37. case LLM_TYPE_250M: return "250M";
  38. case LLM_TYPE_256M: return "256M";
  39. case LLM_TYPE_270M: return "270M";
  40. case LLM_TYPE_335M: return "335M";
  41. case LLM_TYPE_350M: return "350M";
  42. case LLM_TYPE_360M: return "360M";
  43. case LLM_TYPE_410M: return "410M";
  44. case LLM_TYPE_450M: return "450M";
  45. case LLM_TYPE_475M: return "475M";
  46. case LLM_TYPE_558M: return "558M";
  47. case LLM_TYPE_700M: return "700M";
  48. case LLM_TYPE_770M: return "770M";
  49. case LLM_TYPE_780M: return "780M";
  50. case LLM_TYPE_950M: return "950M";
  51. case LLM_TYPE_0_3B: return "0.3B";
  52. case LLM_TYPE_0_5B: return "0.5B";
  53. case LLM_TYPE_0_6B: return "0.6B";
  54. case LLM_TYPE_1B: return "1B";
  55. case LLM_TYPE_1_2B: return "1.2B";
  56. case LLM_TYPE_1_3B: return "1.3B";
  57. case LLM_TYPE_1_4B: return "1.4B";
  58. case LLM_TYPE_1_5B: return "1.5B";
  59. case LLM_TYPE_1_6B: return "1.6B";
  60. case LLM_TYPE_1_7B: return "1.7B";
  61. case LLM_TYPE_1_8B: return "1.8B";
  62. case LLM_TYPE_2B: return "2B";
  63. case LLM_TYPE_2_6B: return "2.6B";
  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_7B_A1B: return "7B.A1B";
  112. case LLM_TYPE_8B_A1B: return "8B.A1B";
  113. case LLM_TYPE_16B_A1B: return "16B.A1B";
  114. case LLM_TYPE_21B_A3B: return "21B.A3B";
  115. case LLM_TYPE_30B_A3B: return "30B.A3B";
  116. case LLM_TYPE_100B_A6B: return "100B.A6B";
  117. case LLM_TYPE_106B_A12B: return "106B.A12B";
  118. case LLM_TYPE_235B_A22B: return "235B.A22B";
  119. case LLM_TYPE_300B_A47B: return "300B.A47B";
  120. case LLM_TYPE_355B_A32B: return "355B.A32B";
  121. case LLM_TYPE_E2B: return "E2B";
  122. case LLM_TYPE_E4B: return "E4B";
  123. default: return "?B";
  124. }
  125. }
  126. static const char * llama_expert_gating_func_name(llama_expert_gating_func_type type) {
  127. switch (type) {
  128. case LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX: return "softmax";
  129. case LLAMA_EXPERT_GATING_FUNC_TYPE_SIGMOID: return "sigmoid";
  130. default: return "unknown";
  131. }
  132. }
  133. static const std::map<llama_rope_scaling_type, const char *> LLAMA_ROPE_SCALING_TYPES = {
  134. { LLAMA_ROPE_SCALING_TYPE_NONE, "none" },
  135. { LLAMA_ROPE_SCALING_TYPE_LINEAR, "linear" },
  136. { LLAMA_ROPE_SCALING_TYPE_YARN, "yarn" },
  137. { LLAMA_ROPE_SCALING_TYPE_LONGROPE, "longrope" },
  138. };
  139. std::string llama_rope_scaling_type_name(llama_rope_scaling_type rope_scaling_type) {
  140. return LLAMA_ROPE_SCALING_TYPES.at(rope_scaling_type);
  141. }
  142. static llama_rope_scaling_type llama_rope_scaling_type_from_string(const std::string & name) {
  143. for (const auto & kv : LLAMA_ROPE_SCALING_TYPES) {
  144. if (kv.second == name) {
  145. return (llama_rope_scaling_type) kv.first;
  146. }
  147. }
  148. return LLAMA_ROPE_SCALING_TYPE_UNSPECIFIED;
  149. }
  150. // checks if the weight tensor can be used with the specified buffer type and device
  151. 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) {
  152. GGML_ASSERT(w != nullptr);
  153. if (op == GGML_OP_NONE) {
  154. return true;
  155. }
  156. ggml_init_params params = {
  157. /*.mem_size =*/ ggml_tensor_overhead()*8,
  158. /*.mem_buffer =*/ NULL,
  159. /*.no_alloc =*/ true,
  160. };
  161. ggml_context_ptr ctx_ptr { ggml_init(params) };
  162. if (!ctx_ptr) {
  163. throw std::runtime_error(format("failed to create ggml context"));
  164. }
  165. ggml_context * ctx = ctx_ptr.get();
  166. ggml_tensor * op_tensor = nullptr;
  167. switch (op) {
  168. case GGML_OP_GET_ROWS:
  169. {
  170. ggml_tensor * b = ggml_new_tensor_1d(ctx, GGML_TYPE_I32, 512);
  171. op_tensor = ggml_get_rows(ctx, w, b);
  172. } break;
  173. case GGML_OP_MUL_MAT:
  174. {
  175. ggml_tensor * b = ggml_new_tensor_4d(ctx, GGML_TYPE_F32, w->ne[0], 512, w->ne[2], w->ne[3]);
  176. op_tensor = ggml_mul_mat(ctx, w, b);
  177. } break;
  178. case GGML_OP_MUL_MAT_ID:
  179. {
  180. int n_expert_used = hparams.n_expert_used;
  181. ggml_tensor * b = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, w->ne[0], n_expert_used, 512);
  182. ggml_tensor * ids = ggml_new_tensor_2d(ctx, GGML_TYPE_I32, n_expert_used, 512);
  183. op_tensor = ggml_mul_mat_id(ctx, w, b, ids);
  184. } break;
  185. case GGML_OP_ADD:
  186. {
  187. ggml_tensor * a = ggml_new_tensor_4d(ctx, GGML_TYPE_F32, w->ne[0], w->ne[1], w->ne[2], w->ne[3]);
  188. op_tensor = ggml_add(ctx, a, w);
  189. } break;
  190. case GGML_OP_ADD_ID:
  191. {
  192. int n_expert_used = hparams.n_expert_used;
  193. ggml_tensor * a = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, w->ne[0], n_expert_used, 512);
  194. ggml_tensor * c = ggml_new_tensor_2d(ctx, GGML_TYPE_I32, n_expert_used, 512);
  195. op_tensor = ggml_add_id(ctx, a, w, c);
  196. } break;
  197. case GGML_OP_MUL:
  198. {
  199. ggml_tensor * a = ggml_new_tensor_4d(ctx, GGML_TYPE_F32, w->ne[0], w->ne[1], w->ne[2], w->ne[3]);
  200. op_tensor = ggml_mul(ctx, a, w);
  201. } break;
  202. case GGML_OP_DIV:
  203. {
  204. ggml_tensor * a = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, w->ne[0]);
  205. op_tensor = ggml_div(ctx, a, w);
  206. } break;
  207. case GGML_OP_ROPE:
  208. {
  209. int n_embd_head = hparams.n_embd_head_v;
  210. int n_head = hparams.n_head();
  211. ggml_tensor * a = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, n_embd_head, n_head, 512);
  212. ggml_tensor * b = ggml_new_tensor_1d(ctx, GGML_TYPE_I32, 512);
  213. op_tensor = ggml_rope_ext(
  214. ctx, a, b, w,
  215. 0, 0, 0, 0, 0,
  216. 0, 0, 0, 0
  217. );
  218. } break;
  219. case GGML_OP_SSM_CONV:
  220. {
  221. const int64_t n_seq_tokens = 512;
  222. const int64_t n_seqs = 3;
  223. ggml_tensor * conv_x = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, w->ne[0] - 1 + n_seq_tokens, w->ne[1], n_seqs);
  224. op_tensor = ggml_ssm_conv(ctx, conv_x, w);
  225. } break;
  226. case GGML_OP_SSM_SCAN:
  227. {
  228. // w is ssm_a, which is used to distinguish Mamba-1 and Mamba-2
  229. const int64_t d_state = w->ne[0] == 1 ? hparams.ssm_d_state : w->ne[0];
  230. const int64_t n_head = w->ne[1];
  231. const int64_t head_dim = hparams.ssm_d_inner / n_head;
  232. const int64_t n_group = hparams.ssm_n_group ? hparams.ssm_n_group : 1;
  233. const int64_t n_seq_tokens = 512;
  234. const int64_t n_seqs = 3;
  235. ggml_tensor * s = ggml_new_tensor_4d(ctx, GGML_TYPE_F32, d_state, head_dim, n_head, n_seqs);
  236. ggml_tensor * x = ggml_new_tensor_4d(ctx, GGML_TYPE_F32, head_dim, n_head, n_seq_tokens, n_seqs);
  237. ggml_tensor * dt = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, n_head, n_seq_tokens, n_seqs);
  238. ggml_tensor * B = ggml_new_tensor_4d(ctx, GGML_TYPE_F32, d_state, n_group, n_seq_tokens, n_seqs);
  239. ggml_tensor * C = ggml_new_tensor_4d(ctx, GGML_TYPE_F32, d_state, n_group, n_seq_tokens, n_seqs);
  240. ggml_tensor * ids = ggml_new_tensor_1d(ctx, GGML_TYPE_I32, n_seqs);
  241. op_tensor = ggml_ssm_scan(ctx, s, x, dt, w, B, C, ids);
  242. } break;
  243. case GGML_OP_RWKV_WKV6:
  244. {
  245. // FIXME
  246. const int64_t S = 123;
  247. const int64_t H = 123;
  248. const int64_t n_tokens = 123;
  249. const int64_t n_seqs = 123;
  250. ggml_tensor * k = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, S, H, n_tokens);
  251. ggml_tensor * v = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, S, H, n_tokens);
  252. ggml_tensor * r = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, S, H, n_tokens);
  253. ggml_tensor * tf = w;
  254. ggml_tensor * td = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, S, H, n_tokens);
  255. ggml_tensor * state = ggml_new_tensor_4d(ctx, GGML_TYPE_F32, S, n_seqs, S, H);
  256. op_tensor = ggml_rwkv_wkv6(ctx, k, v, r, tf, td, state);
  257. } break;
  258. case GGML_OP_IM2COL:
  259. {
  260. const int n_embd = hparams.n_embd;
  261. ggml_tensor * b = ggml_new_tensor_4d(ctx, GGML_TYPE_F32, n_embd, w->ne[1], 1, 1);
  262. op_tensor = ggml_im2col(ctx, w, b, 1, 0, 0, 0, 1, 0, false, GGML_TYPE_F16);
  263. } break;
  264. case GGML_OP_SCALE:
  265. {
  266. op_tensor = ggml_scale(ctx, w, 1.0f);
  267. } break;
  268. default:
  269. GGML_ABORT("%s: missing test for op %s for tensor %s", __func__, ggml_op_name(op), w->name);
  270. }
  271. // create a temporary dummy buffer for the weight so that supports_op can check the buffer type
  272. GGML_ASSERT(w->buffer == nullptr);
  273. w->buffer = ggml_backend_buft_alloc_buffer(buft, 0);
  274. bool op_supported = ggml_backend_dev_supports_op(dev, op_tensor);
  275. ggml_backend_buffer_free(w->buffer);
  276. w->buffer = nullptr;
  277. return op_supported;
  278. }
  279. // lists of buffer types used for each layer
  280. using buft_list_t = std::vector<std::pair<ggml_backend_dev_t, ggml_backend_buffer_type_t>>;
  281. // find the first buffer type in the list that can use the tensor
  282. 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) {
  283. GGML_ASSERT(!buft_list.empty());
  284. for (const auto & cur : buft_list) {
  285. ggml_backend_dev_t cur_dev = cur.first;
  286. ggml_backend_buffer_type_t cur_buft = cur.second;
  287. if (weight_buft_supported(hparams, tensor, op, cur_buft, cur_dev)) {
  288. return cur_buft;
  289. }
  290. }
  291. return nullptr;
  292. }
  293. // CPU: ACCEL -> GPU host -> CPU extra -> CPU
  294. static buft_list_t make_cpu_buft_list(const std::vector<ggml_backend_dev_t> & devices, bool use_extra_bufts, bool no_host) {
  295. buft_list_t buft_list;
  296. // add ACCEL buffer types
  297. for (size_t i = 0; i < ggml_backend_dev_count(); ++i) {
  298. ggml_backend_dev_t dev = ggml_backend_dev_get(i);
  299. if (ggml_backend_dev_type(dev) == GGML_BACKEND_DEVICE_TYPE_ACCEL) {
  300. auto * buft = ggml_backend_dev_buffer_type(dev);
  301. // skip
  302. if (buft != ggml_backend_cpu_buffer_type()) {
  303. buft_list.emplace_back(dev, buft);
  304. }
  305. }
  306. }
  307. // add a host buffer type
  308. // storing the tensors in a host buffer is useful when the processing of large batches
  309. // is offloaded to a GPU device, since it reduces the time spent on data transfers
  310. // generally, this will be done using the first device in the list
  311. // a better approach would be to handle this on a weight-by-weight basis using the offload_op
  312. // function of the device to determine if it would benefit from being stored in a host buffer
  313. if (!no_host) {
  314. for (auto * dev : devices) {
  315. ggml_backend_buffer_type_t buft = ggml_backend_dev_host_buffer_type(dev);
  316. if (buft) {
  317. buft_list.emplace_back(dev, buft);
  318. break;
  319. }
  320. }
  321. }
  322. // add extra buffer types
  323. if (use_extra_bufts) {
  324. auto * cpu_dev = ggml_backend_dev_by_type(GGML_BACKEND_DEVICE_TYPE_CPU);
  325. if (cpu_dev == nullptr) {
  326. throw std::runtime_error(format("%s: no CPU backend found", __func__));
  327. }
  328. auto * cpu_reg = ggml_backend_dev_backend_reg(cpu_dev);
  329. auto ggml_backend_dev_get_extra_bufts_fn = (ggml_backend_dev_get_extra_bufts_t)
  330. ggml_backend_reg_get_proc_address(cpu_reg, "ggml_backend_dev_get_extra_bufts");
  331. if (ggml_backend_dev_get_extra_bufts_fn) {
  332. ggml_backend_buffer_type_t * extra_bufts = ggml_backend_dev_get_extra_bufts_fn(cpu_dev);
  333. while (extra_bufts && *extra_bufts) {
  334. buft_list.emplace_back(cpu_dev, *extra_bufts);
  335. ++extra_bufts;
  336. }
  337. }
  338. }
  339. // add the CPU buffer type
  340. for (size_t i = 0; i < ggml_backend_dev_count(); ++i) {
  341. ggml_backend_dev_t dev = ggml_backend_dev_get(i);
  342. if (ggml_backend_dev_type(dev) == GGML_BACKEND_DEVICE_TYPE_CPU) {
  343. buft_list.emplace_back(dev, ggml_backend_dev_buffer_type(dev));
  344. }
  345. }
  346. return buft_list;
  347. }
  348. // GPU: split if LLAMA_SPLIT_MODE_ROW -> GPU
  349. static buft_list_t make_gpu_buft_list(ggml_backend_dev_t dev, llama_split_mode split_mode, const float * tensor_split) {
  350. buft_list_t buft_list;
  351. // add the device split buffer type if requested and available
  352. if (split_mode == LLAMA_SPLIT_MODE_ROW) {
  353. ggml_backend_reg_t reg = ggml_backend_dev_backend_reg(dev);
  354. auto ggml_backend_split_buffer_type_fn = (ggml_backend_split_buffer_type_t)
  355. ggml_backend_reg_get_proc_address(reg, "ggml_backend_split_buffer_type");
  356. if (ggml_backend_split_buffer_type_fn) {
  357. size_t dev_index = [&]() {
  358. auto * reg = ggml_backend_dev_backend_reg(dev);
  359. for (size_t i = 0; i < ggml_backend_reg_dev_count(reg); ++i) {
  360. if (ggml_backend_reg_dev_get(reg, i) == dev) {
  361. return i;
  362. }
  363. }
  364. throw std::runtime_error(format("device %s not found in its backend reg", ggml_backend_dev_name(dev)));
  365. }();
  366. auto * buft = ggml_backend_split_buffer_type_fn(dev_index, tensor_split);
  367. if (buft != nullptr) {
  368. buft_list.emplace_back(dev, buft);
  369. }
  370. }
  371. }
  372. // add the device default buffer type
  373. buft_list.emplace_back(dev, ggml_backend_dev_buffer_type(dev));
  374. // add the device extra buffer type (if any)
  375. ggml_backend_reg_t reg = ggml_backend_dev_backend_reg(dev);
  376. auto ggml_backend_dev_get_extra_bufts_fn = (ggml_backend_dev_get_extra_bufts_t)
  377. ggml_backend_reg_get_proc_address(reg, "ggml_backend_dev_get_extra_bufts");
  378. if (ggml_backend_dev_get_extra_bufts_fn) {
  379. ggml_backend_buffer_type_t * extra_bufts = ggml_backend_dev_get_extra_bufts_fn(dev);
  380. while (extra_bufts && *extra_bufts) {
  381. buft_list.emplace_back(dev, *extra_bufts);
  382. ++extra_bufts;
  383. }
  384. }
  385. return buft_list;
  386. }
  387. struct llama_model::impl {
  388. impl() {}
  389. ~impl() {}
  390. uint64_t n_elements = 0;
  391. size_t n_bytes = 0;
  392. std::string desc_str;
  393. // model memory mapped files
  394. llama_mmaps mappings;
  395. // objects representing data potentially being locked in memory
  396. llama_mlocks mlock_bufs;
  397. llama_mlocks mlock_mmaps;
  398. // contexts where the model tensors metadata is stored as well ass the corresponding buffers:
  399. std::vector<std::pair<ggml_context_ptr, std::vector<ggml_backend_buffer_ptr>>> ctxs_bufs;
  400. buft_list_t cpu_buft_list;
  401. std::map<ggml_backend_dev_t, buft_list_t> gpu_buft_list;
  402. struct layer_dev {
  403. ggml_backend_dev_t dev;
  404. buft_list_t * buft_list;
  405. };
  406. layer_dev dev_input = {};
  407. layer_dev dev_output = {};
  408. std::vector<layer_dev> dev_layer;
  409. bool has_tensor_overrides;
  410. };
  411. llama_model::llama_model(const llama_model_params & params) : params(params), pimpl(std::make_unique<impl>()) {
  412. pimpl->has_tensor_overrides = params.tensor_buft_overrides && params.tensor_buft_overrides[0].pattern;
  413. }
  414. llama_model::~llama_model() {}
  415. void llama_model::load_stats(llama_model_loader & ml) {
  416. pimpl->n_elements = ml.n_elements;
  417. pimpl->n_bytes = ml.n_bytes;
  418. }
  419. void llama_model::load_arch(llama_model_loader & ml) {
  420. arch = ml.get_arch();
  421. if (arch == LLM_ARCH_UNKNOWN) {
  422. throw std::runtime_error("unknown model architecture: '" + ml.get_arch_name() + "'");
  423. }
  424. }
  425. void llama_model::load_hparams(llama_model_loader & ml) {
  426. const gguf_context * ctx = ml.meta.get();
  427. // get metadata as string
  428. for (int i = 0; i < gguf_get_n_kv(ctx); i++) {
  429. gguf_type type = gguf_get_kv_type(ctx, i);
  430. if (type == GGUF_TYPE_ARRAY) {
  431. continue;
  432. }
  433. const char * name = gguf_get_key(ctx, i);
  434. const std::string value = gguf_kv_to_str(ctx, i);
  435. gguf_kv.emplace(name, value);
  436. }
  437. // get general kv
  438. ml.get_key(LLM_KV_GENERAL_NAME, name, false);
  439. // everything past this point is not vocab-related
  440. // for CLIP models, we only need to load tensors, no hparams
  441. if (hparams.vocab_only || ml.get_arch() == LLM_ARCH_CLIP) {
  442. return;
  443. }
  444. ml.get_key(LLM_KV_CONTEXT_LENGTH, hparams.n_ctx_train);
  445. ml.get_key(LLM_KV_EMBEDDING_LENGTH, hparams.n_embd);
  446. ml.get_key(LLM_KV_BLOCK_COUNT, hparams.n_layer);
  447. ml.get_key(LLM_KV_EXPERT_COUNT, hparams.n_expert, false);
  448. ml.get_key(LLM_KV_EXPERT_USED_COUNT, hparams.n_expert_used, false);
  449. ml.get_key(LLM_KV_EXPERT_GROUP_COUNT, hparams.n_expert_groups, false);
  450. ml.get_key(LLM_KV_EXPERT_GROUP_USED_COUNT, hparams.n_group_used, false);
  451. if (arch == LLM_ARCH_WAVTOKENIZER_DEC) {
  452. ml.get_key(LLM_KV_FEATURES_LENGTH, hparams.n_embd_features);
  453. ml.get_key(LLM_KV_POSNET_EMBEDDING_LENGTH, hparams.posnet.n_embd);
  454. ml.get_key(LLM_KV_POSNET_BLOCK_COUNT, hparams.posnet.n_layer);
  455. ml.get_key(LLM_KV_CONVNEXT_EMBEDDING_LENGTH, hparams.convnext.n_embd);
  456. ml.get_key(LLM_KV_CONVNEXT_BLOCK_COUNT, hparams.convnext.n_layer);
  457. }
  458. GGML_ASSERT(hparams.n_expert <= LLAMA_MAX_EXPERTS);
  459. GGML_ASSERT(hparams.n_expert_used <= hparams.n_expert);
  460. if (hparams.n_expert > 0) {
  461. GGML_ASSERT(hparams.n_expert_used > 0);
  462. GGML_ASSERT(hparams.n_expert_groups < hparams.n_expert);
  463. if (hparams.n_expert_groups > 1) {
  464. GGML_ASSERT(hparams.n_expert % hparams.n_expert_groups == 0);
  465. GGML_ASSERT(hparams.n_group_used > 0);
  466. GGML_ASSERT(hparams.n_group_used < hparams.n_expert_groups);
  467. }
  468. } else {
  469. GGML_ASSERT(hparams.n_expert_used == 0);
  470. GGML_ASSERT(hparams.n_expert_groups == 0);
  471. }
  472. std::fill(hparams.n_head_arr.begin(), hparams.n_head_arr.end(), 0);
  473. std::fill(hparams.n_head_kv_arr.begin(), hparams.n_head_kv_arr.end(), 0);
  474. std::fill(hparams.n_ff_arr.begin(), hparams.n_ff_arr.end(), 0);
  475. std::fill(
  476. hparams.recurrent_layer_arr.begin(),
  477. hparams.recurrent_layer_arr.end(),
  478. llm_arch_is_recurrent(ml.get_arch()));
  479. std::fill(hparams.rope_sections.begin(), hparams.rope_sections.end(), 0);
  480. std::fill(hparams.swa_layers.begin(), hparams.swa_layers.end(), 0);
  481. std::fill(hparams.xielu_alpha_n.begin(), hparams.xielu_alpha_n.end(), 0.0f);
  482. std::fill(hparams.xielu_alpha_p.begin(), hparams.xielu_alpha_p.end(), 0.0f);
  483. std::fill(hparams.xielu_beta.begin(), hparams.xielu_beta.end(), 0.0f);
  484. std::fill(hparams.xielu_eps.begin(), hparams.xielu_eps.end(), 0.0f);
  485. ml.get_key_or_arr(LLM_KV_FEED_FORWARD_LENGTH, hparams.n_ff_arr, hparams.n_layer, false);
  486. ml.get_key_or_arr(LLM_KV_ATTENTION_HEAD_COUNT, hparams.n_head_arr, hparams.n_layer, false);
  487. // n_head_kv is optional, default to n_head
  488. hparams.n_head_kv_arr = hparams.n_head_arr;
  489. ml.get_key_or_arr(LLM_KV_ATTENTION_HEAD_COUNT_KV, hparams.n_head_kv_arr, hparams.n_layer, false);
  490. bool rope_finetuned = false;
  491. ml.get_key(LLM_KV_ROPE_SCALING_FINETUNED, rope_finetuned, false);
  492. hparams.rope_finetuned = rope_finetuned;
  493. hparams.n_ctx_orig_yarn = hparams.n_ctx_train;
  494. ml.get_key(LLM_KV_ROPE_SCALING_ORIG_CTX_LEN, hparams.n_ctx_orig_yarn, false);
  495. // rope_freq_base (optional)
  496. hparams.rope_freq_base_train = 10000.0f;
  497. ml.get_key(LLM_KV_ROPE_FREQ_BASE, hparams.rope_freq_base_train, false);
  498. std::string rope_scaling("linear");
  499. ml.get_key(LLM_KV_ROPE_SCALING_TYPE, rope_scaling, false);
  500. hparams.rope_scaling_type_train = llama_rope_scaling_type_from_string(rope_scaling);
  501. GGML_ASSERT(hparams.rope_scaling_type_train != LLAMA_ROPE_SCALING_TYPE_UNSPECIFIED);
  502. // rope_freq_scale (inverse of the kv) is optional
  503. float ropescale = 0.0f;
  504. if (!ml.get_key(LLM_KV_ROPE_SCALING_FACTOR, ropescale, false)) {
  505. // try the old key name
  506. ml.get_key(LLM_KV_ROPE_SCALE_LINEAR, ropescale, false);
  507. }
  508. hparams.rope_freq_scale_train = ropescale == 0.0f ? 1.0f : 1.0f/ropescale;
  509. // by default assume that the sliding-window layers use the same scaling type as the non-sliding-window layers
  510. hparams.rope_freq_base_train_swa = hparams.rope_freq_base_train;
  511. hparams.rope_freq_scale_train_swa = hparams.rope_freq_scale_train;
  512. ml.get_key(LLM_KV_ROPE_SCALING_ATTN_FACTOR, hparams.rope_attn_factor, false);
  513. // non-transformer models do not have attention heads
  514. if (hparams.n_head() > 0) {
  515. // gpt-neox n_rot = rotary_pct * (n_embd / n_head)
  516. // gpt-j n_rot = rotary_dim
  517. hparams.n_embd_head_k = hparams.n_embd / hparams.n_head();
  518. ml.get_key(LLM_KV_ATTENTION_KEY_LENGTH, hparams.n_embd_head_k, false);
  519. hparams.n_embd_head_v = hparams.n_embd / hparams.n_head();
  520. ml.get_key(LLM_KV_ATTENTION_VALUE_LENGTH, hparams.n_embd_head_v, false);
  521. // sanity check for n_rot (optional)
  522. hparams.n_rot = hparams.n_embd_head_k;
  523. ml.get_key(LLM_KV_ROPE_DIMENSION_COUNT, hparams.n_rot, false);
  524. if (arch == LLM_ARCH_LLAMA || arch == LLM_ARCH_DECI || arch == LLM_ARCH_FALCON) {
  525. if (hparams.n_rot != hparams.n_embd_head_k) {
  526. throw std::runtime_error(format("invalid n_rot: %u, expected %u", hparams.n_rot, hparams.n_embd_head_k));
  527. }
  528. }
  529. } else {
  530. hparams.n_rot = 0;
  531. hparams.n_embd_head_k = 0;
  532. hparams.n_embd_head_v = 0;
  533. }
  534. // for differentiating model types
  535. uint32_t n_vocab = 0;
  536. ml.get_key(LLM_KV_VOCAB_SIZE, n_vocab, false) || ml.get_arr_n(LLM_KV_TOKENIZER_LIST, n_vocab, false);
  537. // for classifier models
  538. ml.get_arr(LLM_KV_CLASSIFIER_OUTPUT_LABELS, classifier_labels, false);
  539. if (!classifier_labels.empty()) {
  540. hparams.n_cls_out = classifier_labels.size();
  541. }
  542. // arch-specific KVs
  543. switch (arch) {
  544. case LLM_ARCH_LLAMA:
  545. {
  546. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  547. if (hparams.n_expert == 8) {
  548. switch (hparams.n_layer) {
  549. case 32: type = LLM_TYPE_8x7B; break;
  550. case 56: type = LLM_TYPE_8x22B; break;
  551. default: type = LLM_TYPE_UNKNOWN;
  552. }
  553. } else {
  554. switch (hparams.n_layer) {
  555. case 16: type = LLM_TYPE_1B; break; // Llama 3.2 1B
  556. case 22: type = LLM_TYPE_1B; break;
  557. case 26: type = LLM_TYPE_3B; break;
  558. case 28: type = LLM_TYPE_3B; break; // Llama 3.2 3B
  559. case 30: type = LLM_TYPE_256M; break; // smoldocling 256M
  560. // granite uses a vocab with len 49152
  561. case 32: type = n_vocab == 49152 ? LLM_TYPE_3B : (n_vocab < 40000 ? LLM_TYPE_7B : LLM_TYPE_8B); break;
  562. case 36: type = LLM_TYPE_8B; break; // granite
  563. case 40: type = LLM_TYPE_13B; break;
  564. case 48: type = LLM_TYPE_34B; break;
  565. case 60: type = LLM_TYPE_30B; break;
  566. case 80: type = hparams.n_head() == hparams.n_head_kv() ? LLM_TYPE_65B : LLM_TYPE_70B; break;
  567. default: type = LLM_TYPE_UNKNOWN;
  568. }
  569. }
  570. } break;
  571. case LLM_ARCH_LLAMA4:
  572. {
  573. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  574. ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp);
  575. ml.get_key(LLM_KV_INTERLEAVE_MOE_LAYER_STEP, hparams.n_moe_layer_step);
  576. const bool found_swa = ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa, false);
  577. if (found_swa && hparams.n_swa == 0) {
  578. hparams.swa_type = LLAMA_SWA_TYPE_NONE;
  579. hparams.n_no_rope_layer_step = hparams.n_layer; // always use rope
  580. } else {
  581. hparams.swa_type = LLAMA_SWA_TYPE_CHUNKED;
  582. hparams.n_swa = 8192;
  583. hparams.set_swa_pattern(4); // pattern: 3 chunked - 1 full
  584. }
  585. switch (hparams.n_expert) {
  586. case 0: {
  587. // MobileLLM (no MoE)
  588. switch (hparams.n_embd) {
  589. case 2048: type = LLM_TYPE_140M; break;
  590. case 4096: type = LLM_TYPE_360M; break;
  591. case 6144: type = LLM_TYPE_950M; break;
  592. default: type = LLM_TYPE_UNKNOWN;
  593. }
  594. } break;
  595. case 16: type = LLM_TYPE_17B_16E; break;
  596. case 128: type = LLM_TYPE_17B_128E; break;
  597. default: type = LLM_TYPE_UNKNOWN;
  598. }
  599. hparams.use_kq_norm = type != LLM_TYPE_17B_128E;
  600. } break;
  601. case LLM_ARCH_ARCEE:
  602. {
  603. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  604. // Arcee uses the same structure as Llama
  605. switch (hparams.n_layer) {
  606. case 36: type = LLM_TYPE_4B; break;
  607. default: type = LLM_TYPE_UNKNOWN;
  608. }
  609. } break;
  610. case LLM_ARCH_DECI:
  611. {
  612. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  613. switch (hparams.n_layer) {
  614. case 32: type = LLM_TYPE_7B; break;
  615. case 80: type = LLM_TYPE_70B; break;
  616. case 162: type = LLM_TYPE_405B; break;
  617. default: type = LLM_TYPE_UNKNOWN;
  618. }
  619. } break;
  620. case LLM_ARCH_MINICPM:
  621. {
  622. // Backward-compatible defaults for older MiniCPM GGUFs
  623. hparams.f_embedding_scale = 12.0f;
  624. hparams.f_residual_scale = 1.4f / sqrtf(float(hparams.n_layer));
  625. hparams.f_logit_scale = hparams.n_embd ? (256.0f / float(hparams.n_embd)) : 1.0f;
  626. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  627. // Optional KV reads, override defaults if present in newer GGUF exports
  628. ml.get_key(LLM_KV_EMBEDDING_SCALE, hparams.f_embedding_scale, /*required=*/false);
  629. ml.get_key(LLM_KV_RESIDUAL_SCALE, hparams.f_residual_scale, /*required=*/false);
  630. ml.get_key(LLM_KV_LOGIT_SCALE, hparams.f_logit_scale, /*required=*/false);
  631. // MiniCPM uses rope by default, unlike Granite which uses it as a switch
  632. hparams.rope_finetuned = true;
  633. switch (hparams.n_layer) {
  634. case 52: type = LLM_TYPE_1B; break;
  635. case 40: type = LLM_TYPE_2B; break;
  636. default: type = LLM_TYPE_UNKNOWN;
  637. }
  638. } break;
  639. case LLM_ARCH_MINICPM3:
  640. {
  641. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  642. ml.get_key(LLM_KV_ATTENTION_Q_LORA_RANK, hparams.n_lora_q);
  643. ml.get_key(LLM_KV_ATTENTION_KV_LORA_RANK, hparams.n_lora_kv);
  644. switch (hparams.n_layer) {
  645. case 62: type = LLM_TYPE_4B; break;
  646. default: type = LLM_TYPE_UNKNOWN;
  647. }
  648. } break;
  649. case LLM_ARCH_GROK:
  650. {
  651. // defaults for old GGUFs
  652. hparams.yarn_beta_fast = 8.0f;
  653. hparams.f_logit_scale = 0.5773502691896257f;
  654. hparams.f_embedding_scale = 78.38367176906169f;
  655. hparams.f_attn_out_scale = 0.08838834764831845f;
  656. hparams.f_attn_logit_softcapping = 30.0f;
  657. hparams.f_router_logit_softcapping = 30.0f;
  658. // no final_logit_softcapping in grok-1
  659. hparams.f_final_logit_softcapping = 0.0f;
  660. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  661. ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp, false);
  662. ml.get_key(LLM_KV_LOGIT_SCALE, hparams.f_logit_scale, false);
  663. ml.get_key(LLM_KV_EMBEDDING_SCALE, hparams.f_embedding_scale, false);
  664. ml.get_key(LLM_KV_ATTENTION_OUTPUT_SCALE, hparams.f_attn_out_scale, false);
  665. ml.get_key(LLM_KV_ATTN_LOGIT_SOFTCAPPING, hparams.f_attn_logit_softcapping, false);
  666. ml.get_key(LLM_KV_ROUTER_LOGIT_SOFTCAPPING, hparams.f_router_logit_softcapping, false);
  667. ml.get_key(LLM_KV_FINAL_LOGIT_SOFTCAPPING, hparams.f_final_logit_softcapping, false);
  668. ml.get_key(LLM_KV_ATTENTION_TEMPERATURE_LENGTH, hparams.attn_temp_length, false);
  669. ml.get_key(LLM_KV_ROPE_SCALING_YARN_EXT_FACTOR, hparams.yarn_ext_factor, false);
  670. ml.get_key(LLM_KV_ROPE_SCALING_YARN_ATTN_FACTOR, hparams.yarn_attn_factor, false);
  671. ml.get_key(LLM_KV_ROPE_SCALING_YARN_BETA_FAST, hparams.yarn_beta_fast, false);
  672. ml.get_key(LLM_KV_ROPE_SCALING_YARN_BETA_SLOW, hparams.yarn_beta_slow, false);
  673. switch (hparams.n_layer) {
  674. case 64: type = LLM_TYPE_314B; break;
  675. default: type = LLM_TYPE_UNKNOWN;
  676. }
  677. } break;
  678. case LLM_ARCH_FALCON:
  679. {
  680. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  681. switch (hparams.n_layer) {
  682. case 32: type = LLM_TYPE_7B; break;
  683. case 60: type = LLM_TYPE_40B; break;
  684. default: type = LLM_TYPE_UNKNOWN;
  685. }
  686. } break;
  687. case LLM_ARCH_BAICHUAN:
  688. {
  689. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  690. switch (hparams.n_layer) {
  691. case 32: type = LLM_TYPE_7B; break;
  692. case 40: type = LLM_TYPE_13B; break;
  693. default: type = LLM_TYPE_UNKNOWN;
  694. }
  695. if (type == LLM_TYPE_13B) {
  696. // TODO: become GGUF KV parameter
  697. hparams.f_max_alibi_bias = 8.0f;
  698. }
  699. } break;
  700. case LLM_ARCH_STARCODER:
  701. {
  702. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  703. switch (hparams.n_layer) {
  704. case 24: type = LLM_TYPE_1B; break;
  705. case 36: type = LLM_TYPE_3B; break;
  706. case 42: type = LLM_TYPE_7B; break;
  707. case 40: type = LLM_TYPE_15B; break;
  708. default: type = LLM_TYPE_UNKNOWN;
  709. }
  710. } break;
  711. case LLM_ARCH_REFACT:
  712. {
  713. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  714. switch (hparams.n_layer) {
  715. case 32: type = LLM_TYPE_1B; break;
  716. default: type = LLM_TYPE_UNKNOWN;
  717. }
  718. // TODO: become GGUF KV parameter
  719. hparams.f_max_alibi_bias = 8.0f;
  720. } break;
  721. case LLM_ARCH_BERT:
  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 3:
  728. type = LLM_TYPE_17M; break; // bge-micro
  729. case 6:
  730. type = LLM_TYPE_22M; break; // MiniLM-L6
  731. case 12:
  732. switch (hparams.n_embd) {
  733. case 384: type = LLM_TYPE_33M; break; // MiniLM-L12, bge-small
  734. case 768: type = LLM_TYPE_109M; break; // bge-base
  735. default: type = LLM_TYPE_UNKNOWN;
  736. } break;
  737. case 24:
  738. type = LLM_TYPE_335M; break; // bge-large
  739. default: type = LLM_TYPE_UNKNOWN;
  740. }
  741. } break;
  742. case LLM_ARCH_JINA_BERT_V2:
  743. {
  744. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  745. ml.get_key(LLM_KV_ATTENTION_CAUSAL, hparams.causal_attn);
  746. ml.get_key(LLM_KV_POOLING_TYPE, hparams.pooling_type, false);
  747. hparams.f_max_alibi_bias = 8.0f;
  748. switch (hparams.n_layer) {
  749. case 4: type = LLM_TYPE_33M; break; // jina-embeddings-small
  750. case 12: type = LLM_TYPE_137M; break; // jina-embeddings-base
  751. default: type = LLM_TYPE_UNKNOWN;
  752. }
  753. } break;
  754. case LLM_ARCH_JINA_BERT_V3:
  755. {
  756. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  757. ml.get_key(LLM_KV_ATTENTION_CAUSAL, hparams.causal_attn);
  758. ml.get_key(LLM_KV_POOLING_TYPE, hparams.pooling_type, false);
  759. switch (hparams.n_layer) {
  760. case 24:
  761. type = LLM_TYPE_558M; break;
  762. default: type = LLM_TYPE_UNKNOWN;
  763. }
  764. } break;
  765. case LLM_ARCH_NOMIC_BERT:
  766. case LLM_ARCH_NOMIC_BERT_MOE:
  767. {
  768. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  769. ml.get_key(LLM_KV_ATTENTION_CAUSAL, hparams.causal_attn);
  770. ml.get_key(LLM_KV_POOLING_TYPE, hparams.pooling_type);
  771. ml.get_key(LLM_KV_MOE_EVERY_N_LAYERS, hparams.moe_every_n_layers, 0);
  772. if (hparams.n_layer == 12 && hparams.n_embd == 768) {
  773. if (arch == LLM_ARCH_NOMIC_BERT) {
  774. type = LLM_TYPE_137M;
  775. } else if (arch == LLM_ARCH_NOMIC_BERT_MOE && hparams.moe_every_n_layers == 2) {
  776. type = LLM_TYPE_475M;
  777. }
  778. }
  779. } break;
  780. case LLM_ARCH_NEO_BERT:
  781. {
  782. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  783. ml.get_key(LLM_KV_ATTENTION_CAUSAL, hparams.causal_attn);
  784. ml.get_key(LLM_KV_POOLING_TYPE, hparams.pooling_type);
  785. if (hparams.n_layer == 28) {
  786. type = LLM_TYPE_250M;
  787. }
  788. } break;
  789. case LLM_ARCH_BLOOM:
  790. {
  791. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  792. switch (hparams.n_layer) {
  793. case 24: type = LLM_TYPE_1B; break;
  794. case 30:
  795. switch (hparams.n_embd) {
  796. case 2560: type = LLM_TYPE_3B; break;
  797. case 4096: type = LLM_TYPE_7B; break;
  798. default: type = LLM_TYPE_UNKNOWN;
  799. } break;
  800. default: type = LLM_TYPE_UNKNOWN;
  801. }
  802. // TODO: become GGUF KV parameter
  803. hparams.f_max_alibi_bias = 8.0f;
  804. } break;
  805. case LLM_ARCH_MPT:
  806. {
  807. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  808. ml.get_key(LLM_KV_ATTENTION_CLAMP_KQV, hparams.f_clamp_kqv, false);
  809. ml.get_key(LLM_KV_ATTENTION_MAX_ALIBI_BIAS, hparams.f_max_alibi_bias);
  810. switch (hparams.n_layer) {
  811. case 32: type = LLM_TYPE_7B; break;
  812. case 48: type = LLM_TYPE_30B; break;
  813. default: type = LLM_TYPE_UNKNOWN;
  814. }
  815. } break;
  816. case LLM_ARCH_STABLELM:
  817. {
  818. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  819. switch (hparams.n_layer) {
  820. case 24: type = LLM_TYPE_1B; break;
  821. case 32: type = LLM_TYPE_3B; break;
  822. case 40: type = LLM_TYPE_12B; break;
  823. default: type = LLM_TYPE_UNKNOWN;
  824. }
  825. } break;
  826. case LLM_ARCH_QWEN:
  827. {
  828. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  829. switch (hparams.n_layer) {
  830. case 32: type = LLM_TYPE_7B; break;
  831. case 40: type = LLM_TYPE_13B; break;
  832. default: type = LLM_TYPE_UNKNOWN;
  833. }
  834. } break;
  835. case LLM_ARCH_QWEN2VL:
  836. {
  837. ml.get_key_or_arr(LLM_KV_ROPE_DIMENSION_SECTIONS, hparams.rope_sections, 4, true);
  838. }
  839. // fall through
  840. case LLM_ARCH_QWEN2:
  841. {
  842. ml.get_key(LLM_KV_POOLING_TYPE, hparams.pooling_type, false);
  843. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  844. switch (hparams.n_layer) {
  845. case 24: type = hparams.n_embd == 1024 ? LLM_TYPE_0_5B : LLM_TYPE_1B; break;
  846. case 28: type = hparams.n_embd == 1536 ? LLM_TYPE_1_5B : LLM_TYPE_7B; break;
  847. case 32: type = LLM_TYPE_7B; break;
  848. case 36: type = LLM_TYPE_3B; break;
  849. case 40: type = hparams.n_head() == 20 ? LLM_TYPE_4B : LLM_TYPE_13B; break;
  850. case 48: type = LLM_TYPE_14B; break;
  851. case 64: type = LLM_TYPE_32B; break;
  852. case 80: type = LLM_TYPE_70B; break;
  853. default: type = LLM_TYPE_UNKNOWN;
  854. }
  855. } break;
  856. case LLM_ARCH_DREAM:
  857. {
  858. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  859. // Dream models are primarily 7B with 28 layers
  860. switch (hparams.n_layer) {
  861. case 28:
  862. type = LLM_TYPE_7B;
  863. break;
  864. default:
  865. type = LLM_TYPE_UNKNOWN;
  866. }
  867. // Set non-causal attention for diffusion models
  868. hparams.causal_attn = false;
  869. }
  870. break;
  871. case LLM_ARCH_LLADA:
  872. {
  873. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  874. // LLaDA-8B has 32 layers, similar to LLaMA but for diffusion
  875. switch (hparams.n_layer) {
  876. case 32:
  877. type = LLM_TYPE_8B;
  878. break;
  879. default:
  880. type = LLM_TYPE_UNKNOWN;
  881. }
  882. // Set non-causal attention for diffusion models
  883. hparams.causal_attn = false;
  884. }
  885. break;
  886. case LLM_ARCH_LLADA_MOE:
  887. {
  888. ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp, false);
  889. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  890. // diffusion language model uses non-causal attention
  891. hparams.causal_attn = false;
  892. switch (hparams.n_layer) {
  893. case 16: type = LLM_TYPE_A1_7B; break;
  894. default: type = LLM_TYPE_UNKNOWN;
  895. }
  896. } break;
  897. case LLM_ARCH_QWEN2MOE:
  898. {
  899. ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp, false);
  900. ml.get_key(LLM_KV_EXPERT_SHARED_FEED_FORWARD_LENGTH, hparams.n_ff_shexp, false);
  901. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  902. switch (hparams.n_layer) {
  903. case 24: type = LLM_TYPE_A2_7B; break;
  904. case 28: type = LLM_TYPE_57B_A14B; break;
  905. default: type = LLM_TYPE_UNKNOWN;
  906. }
  907. } break;
  908. case LLM_ARCH_QWEN3:
  909. {
  910. ml.get_key(LLM_KV_POOLING_TYPE, hparams.pooling_type, false);
  911. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  912. switch (hparams.n_layer) {
  913. case 28: type = hparams.n_embd == 1024 ? LLM_TYPE_0_6B : LLM_TYPE_1_7B; break;
  914. case 36: type = hparams.n_embd == 2560 ? LLM_TYPE_4B : LLM_TYPE_8B; break;
  915. case 40: type = LLM_TYPE_14B; break;
  916. case 64: type = LLM_TYPE_32B; break;
  917. default: type = LLM_TYPE_UNKNOWN;
  918. }
  919. } break;
  920. case LLM_ARCH_QWEN3MOE:
  921. {
  922. ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp, false);
  923. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  924. switch (hparams.n_layer) {
  925. case 48: type = LLM_TYPE_30B_A3B; break;
  926. case 94: type = LLM_TYPE_235B_A22B; break;
  927. default: type = LLM_TYPE_UNKNOWN;
  928. }
  929. } break;
  930. case LLM_ARCH_PHI2:
  931. {
  932. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  933. switch (hparams.n_layer) {
  934. case 24: type = LLM_TYPE_1B; break;
  935. case 32: type = LLM_TYPE_3B; break;
  936. default: type = LLM_TYPE_UNKNOWN;
  937. }
  938. } break;
  939. case LLM_ARCH_PHI3:
  940. {
  941. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  942. switch (hparams.n_layer) {
  943. case 24: type = LLM_TYPE_1B; break;
  944. case 32: type = LLM_TYPE_3B; break;
  945. case 40: type = LLM_TYPE_14B; break;
  946. default: type = LLM_TYPE_UNKNOWN;
  947. }
  948. const bool found_swa = ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa, false);
  949. if (found_swa && hparams.n_swa > 0) {
  950. LLAMA_LOG_WARN("%s: Phi SWA is currently disabled - results might be suboptimal for some models (see %s)\n",
  951. __func__, "https://github.com/ggml-org/llama.cpp/pull/13676");
  952. // TODO: fix conversion scripts to correctly populate `n_swa` and `n_swa_pattern`
  953. hparams.swa_type = LLAMA_SWA_TYPE_NONE;
  954. hparams.n_swa = 0;
  955. hparams.set_swa_pattern(1);
  956. }
  957. } break;
  958. case LLM_ARCH_PHIMOE:
  959. {
  960. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  961. switch (hparams.n_layer) {
  962. case 32: type = LLM_TYPE_16x3_8B; break;
  963. default: type = LLM_TYPE_UNKNOWN;
  964. }
  965. } break;
  966. case LLM_ARCH_PLAMO:
  967. {
  968. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  969. switch (hparams.n_layer) {
  970. case 40: type = LLM_TYPE_13B; break;
  971. default: type = LLM_TYPE_UNKNOWN;
  972. }
  973. } break;
  974. case LLM_ARCH_PLAMO2:
  975. {
  976. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  977. // Load Mamba SSM parameters
  978. ml.get_key(LLM_KV_SSM_CONV_KERNEL, hparams.ssm_d_conv);
  979. ml.get_key(LLM_KV_SSM_INNER_SIZE, hparams.ssm_d_inner);
  980. ml.get_key(LLM_KV_SSM_STATE_SIZE, hparams.ssm_d_state);
  981. ml.get_key(LLM_KV_SSM_TIME_STEP_RANK, hparams.ssm_dt_rank);
  982. ml.get_key(LLM_KV_SSM_GROUP_COUNT, hparams.ssm_n_group);
  983. for (uint32_t i = 0; i < hparams.n_layer; ++i) {
  984. hparams.recurrent_layer_arr[i] = hparams.n_head_kv(i) == 0;
  985. }
  986. switch (hparams.n_layer) {
  987. case 16: type = LLM_TYPE_1B; break;
  988. case 32:
  989. if (hparams.n_embd == 2048) {
  990. type = LLM_TYPE_2B;
  991. } else if (hparams.n_embd == 4096) {
  992. type = LLM_TYPE_8B;
  993. }
  994. break;
  995. default: type = LLM_TYPE_UNKNOWN;
  996. }
  997. // Load attention parameters
  998. ml.get_key(LLM_KV_ATTENTION_KEY_LENGTH, hparams.n_embd_head_k, false);
  999. ml.get_key(LLM_KV_ATTENTION_VALUE_LENGTH, hparams.n_embd_head_v, false);
  1000. } break;
  1001. case LLM_ARCH_GPT2:
  1002. {
  1003. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  1004. switch (hparams.n_layer) {
  1005. case 12: type = LLM_TYPE_SMALL; break;
  1006. case 24: type = LLM_TYPE_MEDIUM; break;
  1007. case 36: type = LLM_TYPE_LARGE; break;
  1008. case 48: type = LLM_TYPE_XL; break;
  1009. default: type = LLM_TYPE_UNKNOWN;
  1010. }
  1011. } break;
  1012. case LLM_ARCH_CODESHELL:
  1013. {
  1014. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  1015. switch (hparams.n_layer) {
  1016. case 42: type = LLM_TYPE_7B; break;
  1017. default: type = LLM_TYPE_UNKNOWN;
  1018. }
  1019. } break;
  1020. case LLM_ARCH_ORION:
  1021. {
  1022. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  1023. switch (hparams.n_layer) {
  1024. case 40: type = LLM_TYPE_14B; break;
  1025. default: type = LLM_TYPE_UNKNOWN;
  1026. }
  1027. } break;
  1028. case LLM_ARCH_INTERNLM2:
  1029. {
  1030. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1031. switch (hparams.n_layer) {
  1032. case 32: type = LLM_TYPE_7B; break;
  1033. case 48: type = LLM_TYPE_20B; break;
  1034. default: type = LLM_TYPE_UNKNOWN;
  1035. }
  1036. } break;
  1037. case LLM_ARCH_GEMMA:
  1038. {
  1039. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1040. switch (hparams.n_layer) {
  1041. case 18: type = LLM_TYPE_2B; break;
  1042. case 28: type = LLM_TYPE_7B; break;
  1043. default: type = LLM_TYPE_UNKNOWN;
  1044. }
  1045. } break;
  1046. case LLM_ARCH_GEMMA2:
  1047. {
  1048. hparams.swa_type = LLAMA_SWA_TYPE_STANDARD;
  1049. hparams.n_swa = 4096; // default value of gemma 2
  1050. hparams.set_swa_pattern(2);
  1051. hparams.attn_soft_cap = true;
  1052. ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa, false);
  1053. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1054. ml.get_key(LLM_KV_ATTN_LOGIT_SOFTCAPPING, hparams.f_attn_logit_softcapping, false);
  1055. ml.get_key(LLM_KV_FINAL_LOGIT_SOFTCAPPING, hparams.f_final_logit_softcapping, false);
  1056. switch (hparams.n_layer) {
  1057. case 26: type = LLM_TYPE_2B; break;
  1058. case 42: type = LLM_TYPE_9B; break;
  1059. case 46: type = LLM_TYPE_27B; break;
  1060. default: type = LLM_TYPE_UNKNOWN;
  1061. }
  1062. // ref: https://github.com/google/gemma_pytorch/blob/014acb7ac4563a5f77c76d7ff98f31b568c16508/gemma/config.py#L173
  1063. hparams.f_attention_scale = type == LLM_TYPE_27B
  1064. ? 1.0f / std::sqrt(float(hparams.n_embd / hparams.n_head(0)))
  1065. : 1.0f / std::sqrt(float(hparams.n_embd_head_k));
  1066. } break;
  1067. case LLM_ARCH_GEMMA3:
  1068. {
  1069. hparams.swa_type = LLAMA_SWA_TYPE_STANDARD;
  1070. hparams.set_swa_pattern(6);
  1071. hparams.rope_freq_base_train_swa = 10000.0f;
  1072. hparams.rope_freq_scale_train_swa = 1.0f;
  1073. ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa);
  1074. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1075. switch (hparams.n_layer) {
  1076. case 18: type = LLM_TYPE_270M; break;
  1077. case 26: type = LLM_TYPE_1B; break;
  1078. case 34: type = LLM_TYPE_4B; break;
  1079. case 48: type = LLM_TYPE_12B; break;
  1080. case 62: type = LLM_TYPE_27B; break;
  1081. default: type = LLM_TYPE_UNKNOWN;
  1082. }
  1083. // ref: https://github.com/google/gemma_pytorch/blob/014acb7ac4563a5f77c76d7ff98f31b568c16508/gemma/config.py#L289
  1084. hparams.f_attention_scale = type == LLM_TYPE_27B
  1085. ? 1.0f / std::sqrt(float(hparams.n_embd / hparams.n_head(0)))
  1086. : 1.0f / std::sqrt(float(hparams.n_embd_head_k));
  1087. } break;
  1088. case LLM_ARCH_GEMMA3N:
  1089. {
  1090. hparams.swa_type = LLAMA_SWA_TYPE_STANDARD;
  1091. hparams.set_swa_pattern(5);
  1092. hparams.n_layer_kv_from_start = 20;
  1093. hparams.rope_freq_base_train_swa = 10000.0f;
  1094. hparams.rope_freq_scale_train_swa = 1.0f;
  1095. hparams.f_attention_scale = 1.0f;
  1096. ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa);
  1097. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1098. switch (hparams.n_layer) {
  1099. case 30: type = LLM_TYPE_E2B; break;
  1100. case 35: type = LLM_TYPE_E4B; break;
  1101. default: type = LLM_TYPE_UNKNOWN;
  1102. }
  1103. } break;
  1104. case LLM_ARCH_GEMMA_EMBEDDING:
  1105. {
  1106. hparams.swa_type = LLAMA_SWA_TYPE_SYMMETRIC;
  1107. hparams.set_swa_pattern(6);
  1108. hparams.causal_attn = false; // embeddings do not use causal attention
  1109. hparams.rope_freq_base_train_swa = 10000.0f;
  1110. hparams.rope_freq_scale_train_swa = 1.0f;
  1111. ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa);
  1112. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1113. ml.get_key(LLM_KV_POOLING_TYPE, hparams.pooling_type);
  1114. //applied only if model converted with --sentence-transformers-dense-modules
  1115. ml.get_key(LLM_KV_DENSE_2_FEAT_IN, hparams.dense_2_feat_in, false);
  1116. ml.get_key(LLM_KV_DENSE_2_FEAT_OUT, hparams.dense_2_feat_out, false);
  1117. ml.get_key(LLM_KV_DENSE_3_FEAT_IN, hparams.dense_3_feat_in, false);
  1118. ml.get_key(LLM_KV_DENSE_3_FEAT_OUT, hparams.dense_3_feat_out, false);
  1119. GGML_ASSERT((hparams.dense_2_feat_in == 0 || hparams.dense_2_feat_in == hparams.n_embd) && "dense_2_feat_in must be equal to n_embd");
  1120. GGML_ASSERT((hparams.dense_3_feat_out == 0 || hparams.dense_3_feat_out == hparams.n_embd) && "dense_3_feat_out must be equal to n_embd");
  1121. switch (hparams.n_layer) {
  1122. case 24: type = LLM_TYPE_0_3B; break;
  1123. default: type = LLM_TYPE_UNKNOWN;
  1124. }
  1125. hparams.f_attention_scale = 1.0f / std::sqrt(float(hparams.n_embd_head_k));
  1126. } break;
  1127. case LLM_ARCH_STARCODER2:
  1128. {
  1129. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  1130. switch (hparams.n_layer) {
  1131. case 30: type = LLM_TYPE_3B; break;
  1132. case 32: type = LLM_TYPE_7B; break;
  1133. case 40: type = LLM_TYPE_15B; break;
  1134. case 52: type = LLM_TYPE_20B; break; // granite
  1135. case 88: type = LLM_TYPE_34B; break; // granite
  1136. default: type = LLM_TYPE_UNKNOWN;
  1137. }
  1138. } break;
  1139. case LLM_ARCH_MAMBA:
  1140. {
  1141. ml.get_key(LLM_KV_SSM_CONV_KERNEL, hparams.ssm_d_conv);
  1142. ml.get_key(LLM_KV_SSM_INNER_SIZE, hparams.ssm_d_inner);
  1143. ml.get_key(LLM_KV_SSM_STATE_SIZE, hparams.ssm_d_state);
  1144. ml.get_key(LLM_KV_SSM_TIME_STEP_RANK, hparams.ssm_dt_rank);
  1145. ml.get_key(LLM_KV_SSM_DT_B_C_RMS, hparams.ssm_dt_b_c_rms, false);
  1146. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1147. switch (hparams.n_layer) {
  1148. case 24:
  1149. switch (hparams.n_embd) {
  1150. case 768: type = LLM_TYPE_SMALL; break;
  1151. default: type = LLM_TYPE_UNKNOWN;
  1152. } break;
  1153. case 48:
  1154. switch (hparams.n_embd) {
  1155. case 1024: type = LLM_TYPE_MEDIUM; break;
  1156. case 1536: type = LLM_TYPE_LARGE; break;
  1157. case 2048: type = LLM_TYPE_XL; break;
  1158. default: type = LLM_TYPE_UNKNOWN;
  1159. } break;
  1160. case 64:
  1161. switch (hparams.n_embd) {
  1162. case 2560: type = LLM_TYPE_3B; break;
  1163. default: type = LLM_TYPE_UNKNOWN;
  1164. } break;
  1165. default: type = LLM_TYPE_UNKNOWN;
  1166. }
  1167. } break;
  1168. case LLM_ARCH_MAMBA2:
  1169. {
  1170. ml.get_key(LLM_KV_SSM_CONV_KERNEL, hparams.ssm_d_conv);
  1171. ml.get_key(LLM_KV_SSM_INNER_SIZE, hparams.ssm_d_inner);
  1172. ml.get_key(LLM_KV_SSM_STATE_SIZE, hparams.ssm_d_state);
  1173. ml.get_key(LLM_KV_SSM_TIME_STEP_RANK, hparams.ssm_dt_rank);
  1174. ml.get_key(LLM_KV_SSM_GROUP_COUNT, hparams.ssm_n_group);
  1175. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1176. switch (hparams.n_layer) {
  1177. case 24:
  1178. switch (hparams.n_embd) {
  1179. case 768: type = LLM_TYPE_SMALL; break;
  1180. default: type = LLM_TYPE_UNKNOWN;
  1181. } break;
  1182. case 48:
  1183. switch (hparams.n_embd) {
  1184. case 1024: type = LLM_TYPE_MEDIUM; break;
  1185. case 1536: type = LLM_TYPE_LARGE; break;
  1186. case 2048: type = LLM_TYPE_XL; break;
  1187. default: type = LLM_TYPE_UNKNOWN;
  1188. } break;
  1189. case 64:
  1190. switch (hparams.n_embd) {
  1191. case 2560: type = LLM_TYPE_3B; break;
  1192. case 4096: type = LLM_TYPE_7B; break;
  1193. default: type = LLM_TYPE_UNKNOWN;
  1194. } break;
  1195. default: type = LLM_TYPE_UNKNOWN;
  1196. }
  1197. } break;
  1198. case LLM_ARCH_JAMBA:
  1199. {
  1200. ml.get_key(LLM_KV_SSM_CONV_KERNEL, hparams.ssm_d_conv);
  1201. ml.get_key(LLM_KV_SSM_INNER_SIZE, hparams.ssm_d_inner);
  1202. ml.get_key(LLM_KV_SSM_STATE_SIZE, hparams.ssm_d_state);
  1203. ml.get_key(LLM_KV_SSM_TIME_STEP_RANK, hparams.ssm_dt_rank);
  1204. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1205. for (uint32_t i = 0; i < hparams.n_layer; ++i) {
  1206. hparams.recurrent_layer_arr[i] = hparams.n_head_kv(i) == 0;
  1207. }
  1208. switch (hparams.n_layer) {
  1209. // TODO: Jamba layers are a bit heterogenous, so naming this is hard.
  1210. case 12: // 900M 8x???M
  1211. case 32: // 51B 16x?B
  1212. default: type = LLM_TYPE_UNKNOWN;
  1213. }
  1214. } break;
  1215. case LLM_ARCH_XVERSE:
  1216. {
  1217. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1218. switch (hparams.n_layer) {
  1219. case 32: type = LLM_TYPE_7B; break;
  1220. case 40: type = LLM_TYPE_13B; break;
  1221. case 80: type = LLM_TYPE_65B; break;
  1222. default: type = LLM_TYPE_UNKNOWN;
  1223. }
  1224. } break;
  1225. case LLM_ARCH_COMMAND_R:
  1226. {
  1227. ml.get_key(LLM_KV_LOGIT_SCALE, hparams.f_logit_scale);
  1228. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  1229. switch (hparams.n_layer) {
  1230. case 40: type = LLM_TYPE_35B; break;
  1231. default: type = LLM_TYPE_UNKNOWN;
  1232. }
  1233. } break;
  1234. case LLM_ARCH_COHERE2:
  1235. {
  1236. hparams.swa_type = LLAMA_SWA_TYPE_STANDARD;
  1237. hparams.set_swa_pattern(4);
  1238. ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa);
  1239. ml.get_key(LLM_KV_LOGIT_SCALE, hparams.f_logit_scale);
  1240. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  1241. switch (hparams.n_layer) {
  1242. case 32: type = LLM_TYPE_8B; break;
  1243. default: type = LLM_TYPE_UNKNOWN;
  1244. }
  1245. } break;
  1246. case LLM_ARCH_DBRX:
  1247. {
  1248. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  1249. ml.get_key(LLM_KV_ATTENTION_CLAMP_KQV, hparams.f_clamp_kqv);
  1250. switch (hparams.n_layer) {
  1251. case 40: type = LLM_TYPE_16x12B; break;
  1252. default: type = LLM_TYPE_UNKNOWN;
  1253. }
  1254. } break;
  1255. case LLM_ARCH_OLMO:
  1256. {
  1257. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  1258. ml.get_key(LLM_KV_ATTENTION_CLAMP_KQV, hparams.f_clamp_kqv, false);
  1259. switch (hparams.n_layer) {
  1260. case 22: type = LLM_TYPE_1B; break;
  1261. case 32: type = LLM_TYPE_7B; break;
  1262. case 80: type = LLM_TYPE_70B; break;
  1263. default: type = LLM_TYPE_UNKNOWN;
  1264. }
  1265. } break;
  1266. case LLM_ARCH_OLMO2:
  1267. {
  1268. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1269. const bool found_swa = ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa, false);
  1270. if (found_swa && hparams.n_swa > 0) {
  1271. hparams.swa_type = LLAMA_SWA_TYPE_STANDARD;
  1272. hparams.set_swa_pattern(4);
  1273. } else {
  1274. hparams.swa_type = LLAMA_SWA_TYPE_NONE;
  1275. }
  1276. switch (hparams.n_layer) {
  1277. case 16: type = LLM_TYPE_1B; break;
  1278. case 32: type = LLM_TYPE_7B; break;
  1279. case 40: type = LLM_TYPE_13B; break;
  1280. case 64: type = LLM_TYPE_32B; break;
  1281. default: type = LLM_TYPE_UNKNOWN;
  1282. }
  1283. } break;
  1284. case LLM_ARCH_SEED_OSS:
  1285. {
  1286. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1287. switch (hparams.n_layer) {
  1288. case 64: type = LLM_TYPE_36B; break;
  1289. default: type = LLM_TYPE_UNKNOWN;
  1290. }
  1291. } break;
  1292. case LLM_ARCH_OLMOE:
  1293. {
  1294. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1295. switch (hparams.n_layer) {
  1296. case 16: type = LLM_TYPE_A1_7B; break;
  1297. default: type = LLM_TYPE_UNKNOWN;
  1298. }
  1299. } break;
  1300. case LLM_ARCH_OPENELM:
  1301. {
  1302. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1303. switch (hparams.n_layer) {
  1304. case 16: type = LLM_TYPE_270M; break;
  1305. case 20: type = LLM_TYPE_450M; break;
  1306. case 28: type = LLM_TYPE_1B; break;
  1307. case 36: type = LLM_TYPE_3B; break;
  1308. default: type = LLM_TYPE_UNKNOWN;
  1309. }
  1310. } break;
  1311. case LLM_ARCH_GPTNEOX:
  1312. {
  1313. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  1314. ml.get_key(LLM_KV_USE_PARALLEL_RESIDUAL, hparams.use_par_res);
  1315. switch (hparams.n_layer) {
  1316. case 6:
  1317. switch (hparams.n_ff()) {
  1318. case 512: type = LLM_TYPE_14M; break;
  1319. case 2048: type = LLM_TYPE_70M; break;
  1320. default: type = LLM_TYPE_UNKNOWN;
  1321. } break;
  1322. case 12:
  1323. switch (hparams.n_ff()) {
  1324. case 3072: type = LLM_TYPE_160M; break;
  1325. default: type = LLM_TYPE_UNKNOWN;
  1326. } break;
  1327. case 16:
  1328. switch (hparams.n_ff()) {
  1329. case 8192: type = LLM_TYPE_1B; break;
  1330. default: type = LLM_TYPE_UNKNOWN;
  1331. } break;
  1332. case 24:
  1333. switch (hparams.n_ff()) {
  1334. case 4096: type = LLM_TYPE_410M; break;
  1335. case 8192: type = LLM_TYPE_1_4B; break;
  1336. default: type = LLM_TYPE_UNKNOWN;
  1337. } break;
  1338. case 32:
  1339. switch (hparams.n_ff()) {
  1340. case 10240: type = LLM_TYPE_2_8B; break;
  1341. case 16384: type = LLM_TYPE_6_9B; break;
  1342. default: type = LLM_TYPE_UNKNOWN;
  1343. } break;
  1344. case 36:
  1345. switch (hparams.n_ff()) {
  1346. case 20480: type = LLM_TYPE_12B; break;
  1347. default: type = LLM_TYPE_UNKNOWN;
  1348. } break;
  1349. case 44:
  1350. switch (hparams.n_ff()) {
  1351. case 24576: type = LLM_TYPE_20B; break;
  1352. default: type = LLM_TYPE_UNKNOWN;
  1353. } break;
  1354. default: type = LLM_TYPE_UNKNOWN;
  1355. }
  1356. } break;
  1357. case LLM_ARCH_ARCTIC:
  1358. {
  1359. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1360. if (hparams.n_expert == 128) {
  1361. switch (hparams.n_layer) {
  1362. case 35: type = LLM_TYPE_10B_128x3_66B; break;
  1363. default: type = LLM_TYPE_UNKNOWN;
  1364. }
  1365. } else {
  1366. type = LLM_TYPE_UNKNOWN;
  1367. }
  1368. } break;
  1369. case LLM_ARCH_DEEPSEEK:
  1370. {
  1371. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1372. ml.get_key(LLM_KV_LEADING_DENSE_BLOCK_COUNT, hparams.n_layer_dense_lead);
  1373. ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp);
  1374. ml.get_key(LLM_KV_EXPERT_SHARED_COUNT, hparams.n_expert_shared);
  1375. ml.get_key(LLM_KV_EXPERT_WEIGHTS_SCALE, hparams.expert_weights_scale);
  1376. switch (hparams.n_layer) {
  1377. case 28: type = LLM_TYPE_20B; break;
  1378. default: type = LLM_TYPE_UNKNOWN;
  1379. }
  1380. } break;
  1381. case LLM_ARCH_DEEPSEEK2:
  1382. {
  1383. bool is_lite = (hparams.n_layer == 27);
  1384. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1385. ml.get_key(LLM_KV_LEADING_DENSE_BLOCK_COUNT, hparams.n_layer_dense_lead);
  1386. if (!is_lite) {
  1387. ml.get_key(LLM_KV_ATTENTION_Q_LORA_RANK, hparams.n_lora_q);
  1388. }
  1389. ml.get_key(LLM_KV_ATTENTION_KV_LORA_RANK, hparams.n_lora_kv);
  1390. ml.get_key(LLM_KV_ATTENTION_KEY_LENGTH_MLA, hparams.n_embd_head_k_mla, false);
  1391. ml.get_key(LLM_KV_ATTENTION_VALUE_LENGTH_MLA, hparams.n_embd_head_v_mla, false);
  1392. ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp);
  1393. ml.get_key(LLM_KV_EXPERT_SHARED_COUNT, hparams.n_expert_shared);
  1394. ml.get_key(LLM_KV_EXPERT_WEIGHTS_SCALE, hparams.expert_weights_scale);
  1395. ml.get_key(LLM_KV_EXPERT_WEIGHTS_NORM, hparams.expert_weights_norm, false);
  1396. ml.get_key(LLM_KV_EXPERT_GATING_FUNC, hparams.expert_gating_func, false);
  1397. if (hparams.expert_gating_func == LLAMA_EXPERT_GATING_FUNC_TYPE_NONE) {
  1398. // for compatibility with existing DeepSeek V2 and V2.5 GGUFs
  1399. // that have no expert_gating_func model parameter set
  1400. hparams.expert_gating_func = LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX;
  1401. }
  1402. ml.get_key(LLM_KV_ROPE_SCALING_YARN_LOG_MUL, hparams.rope_yarn_log_mul, false);
  1403. switch (hparams.n_layer) {
  1404. case 27: type = LLM_TYPE_16B; break;
  1405. case 60: type = LLM_TYPE_236B; break;
  1406. case 61: type = LLM_TYPE_671B; break;
  1407. default: type = LLM_TYPE_UNKNOWN;
  1408. }
  1409. } break;
  1410. case LLM_ARCH_PLM:
  1411. {
  1412. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1413. ml.get_key(LLM_KV_ATTENTION_KV_LORA_RANK, hparams.n_lora_kv);
  1414. switch (hparams.n_layer) {
  1415. case 32: type = LLM_TYPE_1_8B; break;
  1416. default: type = LLM_TYPE_UNKNOWN;
  1417. }
  1418. } break;
  1419. case LLM_ARCH_CHATGLM:
  1420. {
  1421. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1422. switch (hparams.n_layer) {
  1423. case 28: {
  1424. if (hparams.n_head(0) == 16) {
  1425. type = LLM_TYPE_1_5B;
  1426. } else {
  1427. type = LLM_TYPE_6B;
  1428. }
  1429. } break;
  1430. case 40: {
  1431. if (hparams.n_head(0) == 24) {
  1432. type = LLM_TYPE_4B;
  1433. } else {
  1434. type = LLM_TYPE_9B;
  1435. }
  1436. } break;
  1437. default: type = LLM_TYPE_UNKNOWN;
  1438. }
  1439. } break;
  1440. case LLM_ARCH_GLM4:
  1441. {
  1442. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1443. switch (hparams.n_layer) {
  1444. case 40: type = LLM_TYPE_9B; break;
  1445. case 61: type = LLM_TYPE_32B; break;
  1446. default: type = LLM_TYPE_UNKNOWN;
  1447. }
  1448. } break;
  1449. case LLM_ARCH_GLM4_MOE:
  1450. {
  1451. ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp);
  1452. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1453. // MoE parameters
  1454. ml.get_key(LLM_KV_EXPERT_COUNT, hparams.n_expert);
  1455. ml.get_key(LLM_KV_EXPERT_USED_COUNT, hparams.n_expert_used);
  1456. ml.get_key(LLM_KV_EXPERT_SHARED_COUNT, hparams.n_expert_shared);
  1457. ml.get_key(LLM_KV_LEADING_DENSE_BLOCK_COUNT, hparams.n_layer_dense_lead, false);
  1458. ml.get_key(LLM_KV_EXPERT_WEIGHTS_SCALE, hparams.expert_weights_scale);
  1459. ml.get_key(LLM_KV_EXPERT_WEIGHTS_NORM, hparams.expert_weights_norm, false);
  1460. // Expert gating function (GLM-4.5 uses sigmoid)
  1461. ml.get_key(LLM_KV_EXPERT_GATING_FUNC, hparams.expert_gating_func, false);
  1462. if (hparams.expert_gating_func == LLAMA_EXPERT_GATING_FUNC_TYPE_NONE) {
  1463. hparams.expert_gating_func = LLAMA_EXPERT_GATING_FUNC_TYPE_SIGMOID;
  1464. }
  1465. // NextN/MTP parameters
  1466. ml.get_key(LLM_KV_NEXTN_PREDICT_LAYERS, hparams.nextn_predict_layers, false);
  1467. // TODO: when MTP is implemented, this should probably be updated if needed
  1468. hparams.n_layer_kv_from_start = hparams.n_layer - hparams.nextn_predict_layers;
  1469. switch (hparams.n_layer) {
  1470. case 47: type = LLM_TYPE_106B_A12B; break; // GLM-4.5-Air (46 layers + 1 NextN layer)
  1471. case 93: type = LLM_TYPE_355B_A32B; break; // GLM-4.5 (92 layers + 1 NextN layer)
  1472. default: type = LLM_TYPE_UNKNOWN;
  1473. }
  1474. } break;
  1475. case LLM_ARCH_BITNET:
  1476. {
  1477. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1478. switch (hparams.n_layer) {
  1479. case 26: type = LLM_TYPE_3B; break;
  1480. default: type = LLM_TYPE_UNKNOWN;
  1481. }
  1482. } break;
  1483. case LLM_ARCH_T5:
  1484. {
  1485. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1486. ml.get_key(LLM_KV_ATTENTION_RELATIVE_BUCKETS_COUNT, hparams.n_rel_attn_bkts);
  1487. uint32_t dec_start_token_id;
  1488. if (ml.get_key(LLM_KV_DECODER_START_TOKEN_ID, dec_start_token_id, false)) {
  1489. hparams.dec_start_token_id = dec_start_token_id;
  1490. }
  1491. hparams.dec_n_layer = hparams.n_layer;
  1492. ml.get_key(LLM_KV_DECODER_BLOCK_COUNT, hparams.dec_n_layer, false);
  1493. switch (hparams.n_layer) {
  1494. case 6: type = LLM_TYPE_60M; break; // t5-small
  1495. case 8: type = LLM_TYPE_80M; break; // flan-t5-small
  1496. case 12:
  1497. switch (hparams.n_ff()) {
  1498. case 3072: type = LLM_TYPE_220M; break; // t5-base
  1499. case 2048: type = LLM_TYPE_250M; break; // flan-t5-base
  1500. default: type = LLM_TYPE_UNKNOWN;
  1501. } break;
  1502. case 24:
  1503. switch (hparams.n_ff()) {
  1504. case 4096: type = LLM_TYPE_770M; break; // t5-large
  1505. case 2816: type = LLM_TYPE_780M; break; // flan-t5-large
  1506. case 16384: type = LLM_TYPE_3B; break; // t5-3b
  1507. case 5120: type = LLM_TYPE_3B; break; // flan-t5-xl
  1508. case 65536: type = LLM_TYPE_11B; break; // t5-11b
  1509. case 10240: type = LLM_TYPE_11B; break; // flan-t5-xxl
  1510. default: type = LLM_TYPE_UNKNOWN;
  1511. } break;
  1512. default: type = LLM_TYPE_UNKNOWN;
  1513. }
  1514. } break;
  1515. case LLM_ARCH_T5ENCODER:
  1516. {
  1517. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1518. ml.get_key(LLM_KV_ATTENTION_RELATIVE_BUCKETS_COUNT, hparams.n_rel_attn_bkts);
  1519. type = LLM_TYPE_UNKNOWN;
  1520. } break;
  1521. case LLM_ARCH_JAIS:
  1522. {
  1523. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  1524. ml.get_key(LLM_KV_ATTENTION_MAX_ALIBI_BIAS, hparams.f_max_alibi_bias);
  1525. switch (hparams.n_layer) {
  1526. case 24: type = LLM_TYPE_1_3B; break;
  1527. case 40: type = LLM_TYPE_13B; break;
  1528. /* TODO: add variants */
  1529. default: type = LLM_TYPE_UNKNOWN;
  1530. }
  1531. } break;
  1532. case LLM_ARCH_NEMOTRON:
  1533. {
  1534. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  1535. switch (hparams.n_layer) {
  1536. case 32: type = LLM_TYPE_4B; break;
  1537. default: type = LLM_TYPE_UNKNOWN;
  1538. }
  1539. } break;
  1540. case LLM_ARCH_NEMOTRON_H:
  1541. {
  1542. ml.get_key(LLM_KV_SSM_CONV_KERNEL, hparams.ssm_d_conv);
  1543. ml.get_key(LLM_KV_SSM_INNER_SIZE, hparams.ssm_d_inner);
  1544. ml.get_key(LLM_KV_SSM_STATE_SIZE, hparams.ssm_d_state);
  1545. ml.get_key(LLM_KV_SSM_TIME_STEP_RANK, hparams.ssm_dt_rank);
  1546. ml.get_key(LLM_KV_SSM_GROUP_COUNT, hparams.ssm_n_group);
  1547. // A layer is recurrent IFF the n_head_kv value is set to 0 and
  1548. // the n_ff value is set to 0
  1549. for (uint32_t i = 0; i < hparams.n_layer; ++i) {
  1550. hparams.recurrent_layer_arr[i] = (hparams.n_head_kv(i) == 0 && hparams.n_ff(i) == 0);
  1551. }
  1552. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1553. switch (hparams.n_layer) {
  1554. case 56: type = LLM_TYPE_9B; break;
  1555. default: type = LLM_TYPE_UNKNOWN;
  1556. }
  1557. } break;
  1558. case LLM_ARCH_EXAONE:
  1559. {
  1560. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1561. switch (hparams.n_layer) {
  1562. case 32: type = LLM_TYPE_8B; break;
  1563. default: type = LLM_TYPE_UNKNOWN;
  1564. }
  1565. } break;
  1566. case LLM_ARCH_EXAONE4:
  1567. {
  1568. if (hparams.n_layer == 64) { // 32B
  1569. hparams.swa_type = LLAMA_SWA_TYPE_STANDARD;
  1570. hparams.n_swa = 4096;
  1571. hparams.set_swa_pattern(4);
  1572. }
  1573. ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa, false);
  1574. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1575. switch (hparams.n_layer) {
  1576. case 30: type = LLM_TYPE_1_2B; break;
  1577. case 64: type = LLM_TYPE_32B; break;
  1578. default: type = LLM_TYPE_UNKNOWN;
  1579. }
  1580. } break;
  1581. case LLM_ARCH_RWKV6:
  1582. case LLM_ARCH_RWKV6QWEN2:
  1583. {
  1584. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps, false);
  1585. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps, false);
  1586. ml.get_key(LLM_KV_WKV_HEAD_SIZE, hparams.wkv_head_size);
  1587. ml.get_key(LLM_KV_TIME_MIX_EXTRA_DIM, hparams.time_mix_extra_dim);
  1588. ml.get_key(LLM_KV_TIME_DECAY_EXTRA_DIM, hparams.time_decay_extra_dim);
  1589. ml.get_key(LLM_KV_RESCALE_EVERY_N_LAYERS, hparams.rescale_every_n_layers, false);
  1590. ml.get_key(LLM_KV_TOKEN_SHIFT_COUNT, hparams.token_shift_count, false);
  1591. switch (hparams.n_layer) {
  1592. case 24: type = LLM_TYPE_1_6B; break;
  1593. case 32:
  1594. switch (hparams.n_embd) {
  1595. case 2560: type = LLM_TYPE_3B; break;
  1596. case 4096: type = LLM_TYPE_7B; break;
  1597. default: type = LLM_TYPE_UNKNOWN;
  1598. } break;
  1599. case 61: type = LLM_TYPE_14B; break;
  1600. case 64: type = LLM_TYPE_32B; break;
  1601. default: type = LLM_TYPE_UNKNOWN;
  1602. }
  1603. } break;
  1604. case LLM_ARCH_RWKV7:
  1605. case LLM_ARCH_ARWKV7:
  1606. {
  1607. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps, false);
  1608. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps, false);
  1609. ml.get_key(LLM_KV_WKV_HEAD_SIZE, hparams.wkv_head_size);
  1610. ml.get_key(LLM_KV_ATTENTION_DECAY_LORA_RANK, hparams.n_lora_decay);
  1611. ml.get_key(LLM_KV_ATTENTION_ICLR_LORA_RANK, hparams.n_lora_iclr);
  1612. ml.get_key(LLM_KV_ATTENTION_VALUE_RESIDUAL_MIX_LORA_RANK, hparams.n_lora_value_res_mix);
  1613. ml.get_key(LLM_KV_ATTENTION_GATE_LORA_RANK, hparams.n_lora_gate, false);
  1614. ml.get_key(LLM_KV_TOKEN_SHIFT_COUNT, hparams.token_shift_count, false);
  1615. switch (hparams.n_layer) {
  1616. case 12:
  1617. switch (hparams.n_embd) {
  1618. case 768: type = LLM_TYPE_190M; break;
  1619. default: type = LLM_TYPE_UNKNOWN;
  1620. } break;
  1621. case 24:
  1622. switch (hparams.n_embd) {
  1623. case 1024: type = LLM_TYPE_450M; break;
  1624. case 2048: type = LLM_TYPE_1_5B; break;
  1625. default: type = LLM_TYPE_UNKNOWN;
  1626. } break;
  1627. case 28:
  1628. switch (hparams.n_embd) {
  1629. case 1536: type = LLM_TYPE_1_5B; break;
  1630. case 3584: type = LLM_TYPE_7B; break;
  1631. default: type = LLM_TYPE_UNKNOWN;
  1632. } break;
  1633. case 32:
  1634. switch (hparams.n_embd) {
  1635. case 2560: type = LLM_TYPE_2_9B; break;
  1636. case 4096: type = LLM_TYPE_7B; break;
  1637. default: type = LLM_TYPE_UNKNOWN;
  1638. } break;
  1639. case 61:
  1640. switch (hparams.n_embd) {
  1641. case 4096: type = LLM_TYPE_14B; break;
  1642. default: type = LLM_TYPE_UNKNOWN;
  1643. } break;
  1644. default: type = LLM_TYPE_UNKNOWN;
  1645. }
  1646. } break;
  1647. case LLM_ARCH_GRANITE:
  1648. case LLM_ARCH_GRANITE_MOE:
  1649. {
  1650. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1651. ml.get_key(LLM_KV_LOGIT_SCALE, hparams.f_logit_scale);
  1652. ml.get_key(LLM_KV_RESIDUAL_SCALE, hparams.f_residual_scale);
  1653. ml.get_key(LLM_KV_EMBEDDING_SCALE, hparams.f_embedding_scale);
  1654. ml.get_key(LLM_KV_ATTENTION_SCALE, hparams.f_attention_scale);
  1655. // Granite uses rope_finetuned as a switch for rope, so default to true
  1656. bool rope_finetuned = true;
  1657. ml.get_key(LLM_KV_ROPE_SCALING_FINETUNED, rope_finetuned, false);
  1658. hparams.rope_finetuned = rope_finetuned;
  1659. switch (hparams.n_layer) {
  1660. case 32: type = LLM_TYPE_3B; break;
  1661. case 40: type = LLM_TYPE_3B; break;
  1662. // Add additional layer/vocab/etc checks here for other model sizes
  1663. default: type = LLM_TYPE_UNKNOWN;
  1664. }
  1665. // For Granite MoE Shared
  1666. ml.get_key(LLM_KV_EXPERT_SHARED_FEED_FORWARD_LENGTH, hparams.n_ff_shexp, /* required */ false);
  1667. } break;
  1668. case LLM_ARCH_GRANITE_HYBRID:
  1669. {
  1670. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1671. ml.get_key(LLM_KV_LOGIT_SCALE, hparams.f_logit_scale, /* required */ false);
  1672. ml.get_key(LLM_KV_RESIDUAL_SCALE, hparams.f_residual_scale, /* required */ false);
  1673. ml.get_key(LLM_KV_EMBEDDING_SCALE, hparams.f_embedding_scale, /* required */ false);
  1674. ml.get_key(LLM_KV_ATTENTION_SCALE, hparams.f_attention_scale, /* required */ false);
  1675. ml.get_key(LLM_KV_SSM_CONV_KERNEL, hparams.ssm_d_conv);
  1676. ml.get_key(LLM_KV_SSM_INNER_SIZE, hparams.ssm_d_inner);
  1677. ml.get_key(LLM_KV_SSM_STATE_SIZE, hparams.ssm_d_state);
  1678. ml.get_key(LLM_KV_SSM_TIME_STEP_RANK, hparams.ssm_dt_rank);
  1679. ml.get_key(LLM_KV_SSM_GROUP_COUNT, hparams.ssm_n_group);
  1680. // Granite uses rope_finetuned as a switch for rope, so default to true
  1681. bool rope_finetuned = true;
  1682. ml.get_key(LLM_KV_ROPE_SCALING_FINETUNED, rope_finetuned, false);
  1683. hparams.rope_finetuned = rope_finetuned;
  1684. // A layer is recurrent IFF the n_head_kv value is set to 0
  1685. for (uint32_t i = 0; i < hparams.n_layer; ++i) {
  1686. hparams.recurrent_layer_arr[i] = hparams.n_head_kv(i) == 0;
  1687. }
  1688. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1689. switch (hparams.n_embd) {
  1690. case 1536: type = LLM_TYPE_7B_A1B; break;
  1691. case 2048: case 2560: type = LLM_TYPE_3B; break;
  1692. case 4096: type = LLM_TYPE_32B; break;
  1693. default: type = LLM_TYPE_UNKNOWN;
  1694. }
  1695. // For Granite MoE Shared
  1696. ml.get_key(LLM_KV_EXPERT_SHARED_FEED_FORWARD_LENGTH, hparams.n_ff_shexp, /* required */ false);
  1697. } break;
  1698. case LLM_ARCH_CHAMELEON:
  1699. {
  1700. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1701. hparams.f_norm_eps = 1e-5; // eps for qk-norm, torch default
  1702. ml.get_key(LLM_KV_SWIN_NORM, hparams.swin_norm);
  1703. switch (hparams.n_layer) {
  1704. case 32: type = LLM_TYPE_7B; break;
  1705. case 48: type = LLM_TYPE_34B; break;
  1706. default: type = LLM_TYPE_UNKNOWN;
  1707. }
  1708. } break;
  1709. case LLM_ARCH_WAVTOKENIZER_DEC:
  1710. {
  1711. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  1712. ml.get_key(LLM_KV_ATTENTION_GROUPNORM_EPS, hparams.f_norm_group_eps);
  1713. ml.get_key(LLM_KV_ATTENTION_GROUPNORM_GROUPS, hparams.n_norm_groups);
  1714. ml.get_key(LLM_KV_ATTENTION_CAUSAL, hparams.causal_attn);
  1715. } break;
  1716. case LLM_ARCH_BAILINGMOE:
  1717. {
  1718. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1719. ml.get_key(LLM_KV_LEADING_DENSE_BLOCK_COUNT, hparams.n_layer_dense_lead);
  1720. ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp);
  1721. ml.get_key(LLM_KV_EXPERT_SHARED_COUNT, hparams.n_expert_shared);
  1722. ml.get_key(LLM_KV_EXPERT_WEIGHTS_SCALE, hparams.expert_weights_scale);
  1723. ml.get_key(LLM_KV_EXPERT_WEIGHTS_NORM, hparams.expert_weights_norm, false);
  1724. switch (hparams.n_layer) {
  1725. case 28: type = LLM_TYPE_16B; break;
  1726. case 88: type = LLM_TYPE_290B; break;
  1727. default: type = LLM_TYPE_UNKNOWN;
  1728. }
  1729. } break;
  1730. case LLM_ARCH_BAILINGMOE2:
  1731. {
  1732. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1733. ml.get_key(LLM_KV_LEADING_DENSE_BLOCK_COUNT, hparams.n_layer_dense_lead);
  1734. ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp);
  1735. ml.get_key(LLM_KV_EXPERT_SHARED_FEED_FORWARD_LENGTH, hparams.n_ff_shexp);
  1736. ml.get_key(LLM_KV_EXPERT_SHARED_COUNT, hparams.n_expert_shared);
  1737. ml.get_key(LLM_KV_EXPERT_WEIGHTS_SCALE, hparams.expert_weights_scale);
  1738. ml.get_key(LLM_KV_EXPERT_WEIGHTS_NORM, hparams.expert_weights_norm, false);
  1739. ml.get_key(LLM_KV_EXPERT_GATING_FUNC, hparams.expert_gating_func);
  1740. ml.get_key(LLM_KV_NEXTN_PREDICT_LAYERS, hparams.nextn_predict_layers, false);
  1741. // TODO: when MTP is implemented, this should probably be updated if needed
  1742. hparams.n_layer_kv_from_start = hparams.n_layer - hparams.nextn_predict_layers;
  1743. switch (hparams.n_layer) {
  1744. case 20: type = LLM_TYPE_16B_A1B; break;
  1745. case 21: type = LLM_TYPE_16B_A1B; break;
  1746. case 32: type = LLM_TYPE_100B_A6B; break;
  1747. case 33: type = LLM_TYPE_100B_A6B; break;
  1748. default: type = LLM_TYPE_UNKNOWN;
  1749. }
  1750. } break;
  1751. case LLM_ARCH_DOTS1:
  1752. {
  1753. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1754. ml.get_key(LLM_KV_LEADING_DENSE_BLOCK_COUNT, hparams.n_layer_dense_lead);
  1755. ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp);
  1756. ml.get_key(LLM_KV_EXPERT_SHARED_COUNT, hparams.n_expert_shared);
  1757. ml.get_key(LLM_KV_EXPERT_WEIGHTS_SCALE, hparams.expert_weights_scale);
  1758. ml.get_key(LLM_KV_EXPERT_WEIGHTS_NORM, hparams.expert_weights_norm, false);
  1759. ml.get_key(LLM_KV_EXPERT_GATING_FUNC, hparams.expert_gating_func, false);
  1760. switch (hparams.n_layer) {
  1761. case 62: type = LLM_TYPE_142B; break;
  1762. default: type = LLM_TYPE_UNKNOWN;
  1763. }
  1764. } break;
  1765. case LLM_ARCH_ERNIE4_5:
  1766. case LLM_ARCH_ERNIE4_5_MOE:
  1767. {
  1768. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1769. if (arch == LLM_ARCH_ERNIE4_5_MOE) {
  1770. ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp);
  1771. ml.get_key(LLM_KV_EXPERT_SHARED_FEED_FORWARD_LENGTH, hparams.n_ff_shexp, false);
  1772. ml.get_key(LLM_KV_INTERLEAVE_MOE_LAYER_STEP, hparams.n_moe_layer_step);
  1773. ml.get_key(LLM_KV_LEADING_DENSE_BLOCK_COUNT, hparams.n_layer_dense_lead);
  1774. }
  1775. switch (hparams.n_layer) {
  1776. case 18: type = LLM_TYPE_0_3B; break;
  1777. case 28: type = LLM_TYPE_21B_A3B; break;
  1778. case 54: type = LLM_TYPE_300B_A47B; break;
  1779. default: type = LLM_TYPE_UNKNOWN;
  1780. }
  1781. } break;
  1782. case LLM_ARCH_FALCON_H1:
  1783. {
  1784. // Common parameters
  1785. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1786. // SSM parameters
  1787. ml.get_key(LLM_KV_SSM_CONV_KERNEL, hparams.ssm_d_conv);
  1788. ml.get_key(LLM_KV_SSM_INNER_SIZE, hparams.ssm_d_inner);
  1789. ml.get_key(LLM_KV_SSM_STATE_SIZE, hparams.ssm_d_state);
  1790. ml.get_key(LLM_KV_SSM_TIME_STEP_RANK, hparams.ssm_dt_rank);
  1791. ml.get_key(LLM_KV_SSM_GROUP_COUNT, hparams.ssm_n_group);
  1792. std::fill(hparams.recurrent_layer_arr.begin(), hparams.recurrent_layer_arr.end(), true);
  1793. switch (hparams.n_layer) {
  1794. case 36:
  1795. type = LLM_TYPE_0_5B; break;
  1796. case 24:
  1797. type = LLM_TYPE_1_5B; break;
  1798. case 66:
  1799. type = LLM_TYPE_1B; break;
  1800. case 32:
  1801. type = LLM_TYPE_3B; break;
  1802. case 44:
  1803. type = LLM_TYPE_7B; break;
  1804. case 72:
  1805. type = LLM_TYPE_34B; break;
  1806. default:
  1807. type = LLM_TYPE_UNKNOWN;
  1808. }
  1809. } break;
  1810. case LLM_ARCH_HUNYUAN_MOE:
  1811. {
  1812. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1813. ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp);
  1814. ml.get_key(LLM_KV_EXPERT_SHARED_FEED_FORWARD_LENGTH, hparams.n_ff_shexp);
  1815. switch (hparams.n_layer) {
  1816. case 32: type = LLM_TYPE_A13B; break;
  1817. default: type = LLM_TYPE_UNKNOWN;
  1818. }
  1819. } break;
  1820. case LLM_ARCH_HUNYUAN_DENSE:
  1821. {
  1822. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1823. switch (hparams.n_embd) {
  1824. case 1024: type = LLM_TYPE_0_5B; break;
  1825. case 2048: type = LLM_TYPE_1_8B; break;
  1826. case 3072: type = LLM_TYPE_4B; break;
  1827. case 4096: type = LLM_TYPE_7B; break;
  1828. default: type = LLM_TYPE_UNKNOWN;
  1829. }
  1830. } break;
  1831. case LLM_ARCH_SMOLLM3:
  1832. {
  1833. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1834. hparams.n_no_rope_layer_step = 4;
  1835. switch (hparams.n_layer) {
  1836. case 36: type = LLM_TYPE_3B; break;
  1837. default: type = LLM_TYPE_UNKNOWN;
  1838. }
  1839. } break;
  1840. case LLM_ARCH_OPENAI_MOE:
  1841. {
  1842. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1843. ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp);
  1844. ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa);
  1845. hparams.swa_type = LLAMA_SWA_TYPE_STANDARD;
  1846. hparams.set_swa_pattern(2);
  1847. switch (hparams.n_layer) {
  1848. case 24: type = LLM_TYPE_20B; break;
  1849. case 36: type = LLM_TYPE_120B; break;
  1850. default: type = LLM_TYPE_UNKNOWN;
  1851. }
  1852. } break;
  1853. case LLM_ARCH_LFM2:
  1854. {
  1855. ml.get_key(LLM_KV_SHORTCONV_L_CACHE, hparams.n_shortconv_l_cache);
  1856. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1857. for (uint32_t il = 0; il < hparams.n_layer; ++il) {
  1858. hparams.recurrent_layer_arr[il] = hparams.n_head_kv(il) == 0;
  1859. }
  1860. hparams.n_layer_dense_lead = hparams.n_layer;
  1861. switch (hparams.n_ff()) {
  1862. case 4608: type = LLM_TYPE_350M; break;
  1863. case 6912: type = LLM_TYPE_700M; break;
  1864. case 8192: type = LLM_TYPE_1_2B; break;
  1865. case 10752: type = LLM_TYPE_2_6B; break;
  1866. default: type = LLM_TYPE_UNKNOWN;
  1867. }
  1868. } break;
  1869. case LLM_ARCH_LFM2MOE:
  1870. {
  1871. ml.get_key(LLM_KV_SHORTCONV_L_CACHE, hparams.n_shortconv_l_cache);
  1872. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1873. ml.get_key(LLM_KV_LEADING_DENSE_BLOCK_COUNT, hparams.n_layer_dense_lead);
  1874. ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp);
  1875. ml.get_key(LLM_KV_EXPERT_GATING_FUNC, hparams.expert_gating_func);
  1876. for (uint32_t il = 0; il < hparams.n_layer; ++il) {
  1877. hparams.recurrent_layer_arr[il] = hparams.n_head_kv(il) == 0;
  1878. }
  1879. type = LLM_TYPE_8B_A1B;
  1880. } break;
  1881. case LLM_ARCH_SMALLTHINKER:
  1882. {
  1883. const bool found_swa = ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa, false);
  1884. if (found_swa && hparams.n_swa > 0) {
  1885. hparams.swa_type = LLAMA_SWA_TYPE_STANDARD;
  1886. hparams.n_swa = 4096;
  1887. hparams.set_swa_pattern(4, true);
  1888. } else {
  1889. hparams.swa_type = LLAMA_SWA_TYPE_NONE;
  1890. hparams.n_no_rope_layer_step = hparams.n_layer;
  1891. }
  1892. ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp, false);
  1893. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1894. ml.get_key(LLM_KV_EXPERT_GATING_FUNC, hparams.expert_gating_func, false);
  1895. switch (hparams.n_layer) {
  1896. case 32: type = LLM_TYPE_4B; break;
  1897. case 52: type = LLM_TYPE_20B; break;
  1898. default: type = LLM_TYPE_UNKNOWN;
  1899. }
  1900. } break;
  1901. case LLM_ARCH_GROVEMOE:
  1902. {
  1903. ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp);
  1904. ml.get_key(LLM_KV_EXPERT_CHUNK_FEED_FORWARD_LENGTH, hparams.n_ff_chexp);
  1905. ml.get_key(LLM_KV_EXPERT_GROUP_SCALE, hparams.expert_group_scale);
  1906. ml.get_key(LLM_KV_EXPERTS_PER_GROUP, hparams.n_group_experts);
  1907. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1908. switch (hparams.n_layer) {
  1909. case 48: type = LLM_TYPE_30B_A3B; break;
  1910. default: type = LLM_TYPE_UNKNOWN;
  1911. }
  1912. } break;
  1913. case LLM_ARCH_APERTUS:
  1914. {
  1915. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1916. ml.get_key_or_arr(LLM_KV_XIELU_ALPHA_N, hparams.xielu_alpha_n, hparams.n_layer);
  1917. ml.get_key_or_arr(LLM_KV_XIELU_ALPHA_P, hparams.xielu_alpha_p, hparams.n_layer);
  1918. ml.get_key_or_arr(LLM_KV_XIELU_BETA, hparams.xielu_beta, hparams.n_layer);
  1919. ml.get_key_or_arr(LLM_KV_XIELU_EPS, hparams.xielu_eps, hparams.n_layer);
  1920. switch (hparams.n_layer) {
  1921. case 32: type = LLM_TYPE_8B; break;
  1922. default: type = LLM_TYPE_UNKNOWN;
  1923. }
  1924. } break;
  1925. case LLM_ARCH_COGVLM:
  1926. {
  1927. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1928. switch (hparams.n_layer) {
  1929. case 32: type = LLM_TYPE_13B; break;
  1930. default: type = LLM_TYPE_UNKNOWN;
  1931. }
  1932. } break;
  1933. default: throw std::runtime_error("unsupported model architecture");
  1934. }
  1935. pimpl->n_bytes = ml.n_bytes;
  1936. pimpl->desc_str = arch_name() + " " + type_name() + " " + ml.ftype_name();
  1937. if (hparams.f_max_alibi_bias > 0.0f) {
  1938. hparams.use_alibi = true;
  1939. }
  1940. hparams.rope_type = llama_model_rope_type(this);
  1941. }
  1942. void llama_model::load_vocab(llama_model_loader & ml) {
  1943. const auto kv = LLM_KV(arch);
  1944. vocab.load(ml, kv);
  1945. }
  1946. bool llama_model::load_tensors(llama_model_loader & ml) {
  1947. const auto & split_mode = params.split_mode;
  1948. const auto & n_gpu_layers = params.n_gpu_layers;
  1949. const auto & use_mlock = params.use_mlock;
  1950. const auto & tensor_split = params.tensor_split;
  1951. const int n_layer = hparams.n_layer;
  1952. const bool use_mmap_buffer = true;
  1953. LLAMA_LOG_INFO("%s: loading model tensors, this can take a while... (mmap = %s)\n", __func__, ml.use_mmap ? "true" : "false");
  1954. // build a list of buffer types for the CPU and GPU devices
  1955. pimpl->cpu_buft_list = make_cpu_buft_list(devices, params.use_extra_bufts, params.no_host);
  1956. for (auto * dev : devices) {
  1957. buft_list_t buft_list = make_gpu_buft_list(dev, split_mode, tensor_split);
  1958. // add CPU buffer types as a fallback
  1959. buft_list.insert(buft_list.end(), pimpl->cpu_buft_list.begin(), pimpl->cpu_buft_list.end());
  1960. pimpl->gpu_buft_list.emplace(dev, std::move(buft_list));
  1961. }
  1962. // calculate the split points
  1963. bool all_zero = tensor_split == nullptr || std::all_of(tensor_split, tensor_split + n_devices(), [](float x) { return x == 0.0f; });
  1964. std::vector<float> splits(n_devices());
  1965. if (all_zero) {
  1966. // default split, by free memory
  1967. for (size_t i = 0; i < n_devices(); ++i) {
  1968. ggml_backend_dev_t dev = devices[i];
  1969. size_t total;
  1970. size_t free;
  1971. ggml_backend_dev_memory(dev, &free, &total);
  1972. splits[i] = free;
  1973. }
  1974. } else {
  1975. std::copy(tensor_split, tensor_split + n_devices(), splits.begin());
  1976. }
  1977. // sum and normalize the splits to get the split points
  1978. float split_sum = 0.0f;
  1979. for (size_t i = 0; i < n_devices(); ++i) {
  1980. split_sum += splits[i];
  1981. splits[i] = split_sum;
  1982. }
  1983. for (size_t i = 0; i < n_devices(); ++i) {
  1984. splits[i] /= split_sum;
  1985. }
  1986. ggml_backend_dev_t cpu_dev = ggml_backend_dev_by_type(GGML_BACKEND_DEVICE_TYPE_CPU);
  1987. if (cpu_dev == nullptr) {
  1988. throw std::runtime_error(format("%s: no CPU backend found", __func__));
  1989. }
  1990. const int i_gpu_start = std::max((int) hparams.n_layer - n_gpu_layers, (int) 0);
  1991. const int act_gpu_layers = devices.empty() ? 0 : std::min(n_gpu_layers, (int)n_layer + 1);
  1992. auto get_layer_buft_list = [&](int il) -> llama_model::impl::layer_dev {
  1993. const bool is_swa = il < (int) hparams.n_layer && hparams.is_swa(il);
  1994. if (il < i_gpu_start || (il - i_gpu_start) >= act_gpu_layers) {
  1995. LLAMA_LOG_DEBUG("load_tensors: layer %3d assigned to device %s, is_swa = %d\n", il, ggml_backend_dev_name(cpu_dev), is_swa);
  1996. return {cpu_dev, &pimpl->cpu_buft_list};
  1997. }
  1998. const int layer_gpu = std::upper_bound(splits.begin(), splits.begin() + n_devices(), float(il - i_gpu_start)/act_gpu_layers) - splits.begin();
  1999. auto * dev = devices.at(layer_gpu);
  2000. LLAMA_LOG_DEBUG("load_tensors: layer %3d assigned to device %s, is_swa = %d\n", il, ggml_backend_dev_name(dev), is_swa);
  2001. return {dev, &pimpl->gpu_buft_list.at(dev)};
  2002. };
  2003. // assign the input layer
  2004. // there is very little benefit to offloading the input layer, so always keep it on the CPU
  2005. pimpl->dev_input = { cpu_dev, &pimpl->cpu_buft_list };
  2006. // assign the repeating layers to the devices according to the splits
  2007. pimpl->dev_layer.resize(n_layer);
  2008. for (int il = 0; il < n_layer; ++il) {
  2009. pimpl->dev_layer[il] = get_layer_buft_list(il);
  2010. }
  2011. // assign the output layer
  2012. pimpl->dev_output = get_layer_buft_list(n_layer);
  2013. // one ggml context per buffer type
  2014. int max_n_tensors = ml.n_tensors;
  2015. max_n_tensors += 1; // duplicated output tensor
  2016. max_n_tensors += n_layer*2; // duplicated rope freq tensors
  2017. const size_t ctx_size = ggml_tensor_overhead()*max_n_tensors;
  2018. // define a comparator for the buft -> ctx map to ensure that the order is well-defined:
  2019. struct ggml_backend_buft_comparator {
  2020. bool operator()(const ggml_backend_buffer_type_t & lhs, const ggml_backend_buffer_type_t & rhs) const {
  2021. return strcmp(ggml_backend_buft_name(lhs), ggml_backend_buft_name(rhs)) < 0;
  2022. }
  2023. };
  2024. std::map<ggml_backend_buffer_type_t, ggml_context_ptr, ggml_backend_buft_comparator> ctx_map;
  2025. auto ctx_for_buft = [&](ggml_backend_buffer_type_t buft) -> ggml_context * {
  2026. auto it = ctx_map.find(buft);
  2027. if (it == ctx_map.end()) {
  2028. ggml_init_params params = {
  2029. /*.mem_size =*/ ctx_size,
  2030. /*.mem_buffer =*/ NULL,
  2031. /*.no_alloc =*/ true,
  2032. };
  2033. ggml_context * ctx = ggml_init(params);
  2034. if (!ctx) {
  2035. throw std::runtime_error(format("failed to create ggml context"));
  2036. }
  2037. ctx_map.emplace(buft, ctx);
  2038. return ctx;
  2039. }
  2040. return it->second.get();
  2041. };
  2042. const auto TENSOR_DUPLICATED = llama_model_loader::TENSOR_DUPLICATED;
  2043. const auto TENSOR_NOT_REQUIRED = llama_model_loader::TENSOR_NOT_REQUIRED;
  2044. const auto TENSOR_SKIP = llama_model_loader::TENSOR_SKIP;
  2045. // create tensors for the weights
  2046. {
  2047. // note: cast to int64_t since we will use these for the tensor dimensions
  2048. const int64_t n_head = hparams.n_head();
  2049. const int64_t n_head_kv = hparams.n_head_kv();
  2050. const int64_t n_embd = hparams.n_embd;
  2051. const int64_t n_embd_k_gqa = hparams.n_embd_k_gqa();
  2052. const int64_t n_embd_v_gqa = hparams.n_embd_v_gqa();
  2053. const int64_t n_embd_head_k = hparams.n_embd_head_k;
  2054. const int64_t n_embd_head_v = hparams.n_embd_head_v;
  2055. const int64_t n_ff = hparams.n_ff();
  2056. const int64_t n_embd_gqa = n_embd_v_gqa;
  2057. const int64_t n_vocab = vocab.n_tokens();
  2058. const int64_t n_token_types = vocab.n_token_types();
  2059. const int64_t n_rot = hparams.n_rot;
  2060. const int64_t n_expert = hparams.n_expert;
  2061. const int64_t n_expert_used = hparams.n_expert_used;
  2062. const int64_t n_ctx_train = hparams.n_ctx_train;
  2063. if (n_expert > 0 && hparams.n_expert_used == 0) {
  2064. throw std::runtime_error("model has expert layers but no expert layers are used");
  2065. }
  2066. int n_moved_tensors = 0;
  2067. ggml_tensor * first_moved_tensor = nullptr;
  2068. ggml_backend_buffer_type_t first_moved_from_buft = nullptr;
  2069. ggml_backend_buffer_type_t first_moved_to_buft = nullptr;
  2070. auto create_tensor = [&](const LLM_TN_IMPL & tn, const std::initializer_list<int64_t> & ne, int flags) -> ggml_tensor * {
  2071. ggml_tensor * t_meta = ml.get_tensor_meta(tn.str().c_str());
  2072. if (!t_meta) {
  2073. if (flags & TENSOR_NOT_REQUIRED) {
  2074. return nullptr;
  2075. }
  2076. throw std::runtime_error(format("missing tensor '%s'", tn.str().c_str()));
  2077. }
  2078. // some models use the token embedding tensor as the output, but since these are used in different layers and with different ops
  2079. // the tensor is duplicated
  2080. // to handle this, we check if the tensor is duplicated, and if so, we assume that it is being loaded as the output tensor
  2081. llm_tensor tn_tensor = tn.tensor;
  2082. if (tn.tensor == LLM_TENSOR_TOKEN_EMBD && flags & TENSOR_DUPLICATED) {
  2083. tn_tensor = LLM_TENSOR_OUTPUT;
  2084. }
  2085. llm_tensor_info info;
  2086. try {
  2087. info = llm_tensor_info_for(tn_tensor);
  2088. } catch (const std::out_of_range & e) {
  2089. throw std::runtime_error(format("missing tensor info mapping for %s", tn.str().c_str()));
  2090. }
  2091. // skip unused tensors
  2092. if (info.op == GGML_OP_NONE || flags & TENSOR_SKIP) {
  2093. const size_t nbytes = ggml_nbytes(t_meta);
  2094. LLAMA_LOG_WARN("model has unused tensor %s (size = %zu bytes) -- ignoring\n", tn.str().c_str(), nbytes);
  2095. ml.size_data -= nbytes;
  2096. ml.n_created++;
  2097. return nullptr;
  2098. }
  2099. // tensors with "bias" suffix are always used with GGML_OP_ADD or GGML_OP_ADD_ID
  2100. ggml_op op;
  2101. bool bias = tn.suffix != nullptr && strcmp(tn.suffix, "bias") == 0;
  2102. if (bias) {
  2103. if (info.op == GGML_OP_MUL_MAT_ID) {
  2104. op = GGML_OP_ADD_ID;
  2105. } else {
  2106. op = GGML_OP_ADD;
  2107. }
  2108. } else {
  2109. op = info.op;
  2110. }
  2111. // sanity checks
  2112. if (info.layer == LLM_TENSOR_LAYER_INPUT || info.layer == LLM_TENSOR_LAYER_OUTPUT) {
  2113. if (tn.bid != -1) {
  2114. GGML_ABORT("input/output layer tensor %s used with a layer number", tn.str().c_str());
  2115. }
  2116. } else {
  2117. if (tn.bid == -1) {
  2118. GGML_ABORT("repeating layer tensor %s used without a layer number", tn.str().c_str());
  2119. }
  2120. }
  2121. // select the buffer type for this tensor
  2122. buft_list_t * buft_list;
  2123. switch (info.layer) {
  2124. case LLM_TENSOR_LAYER_INPUT:
  2125. buft_list = pimpl->dev_input.buft_list;
  2126. break;
  2127. case LLM_TENSOR_LAYER_OUTPUT:
  2128. buft_list = pimpl->dev_output.buft_list;
  2129. break;
  2130. case LLM_TENSOR_LAYER_REPEATING:
  2131. buft_list = pimpl->dev_layer.at(tn.bid).buft_list;
  2132. break;
  2133. default:
  2134. GGML_ABORT("invalid layer %d for tensor %s", info.layer, tn.str().c_str());
  2135. }
  2136. ggml_backend_buffer_type_t buft = nullptr;
  2137. // check overrides
  2138. if (ml.tensor_buft_overrides) {
  2139. std::string tensor_name = tn.str();
  2140. for (const auto * overrides = ml.tensor_buft_overrides; overrides->pattern != nullptr; ++overrides) {
  2141. std::regex pattern(overrides->pattern);
  2142. if (std::regex_search(tensor_name, pattern)) {
  2143. if (overrides->buft == ggml_backend_cpu_buffer_type()) {
  2144. // when overriding to a CPU buffer, consider the extra buffer types
  2145. buft = select_weight_buft(hparams, t_meta, op, pimpl->cpu_buft_list);
  2146. } else {
  2147. buft = overrides->buft;
  2148. }
  2149. LLAMA_LOG_DEBUG("tensor %s (%zu MiB %s) buffer type overridden to %s\n",
  2150. tensor_name.c_str(),
  2151. ggml_nbytes(t_meta) / 1024 / 1024, ggml_type_name(t_meta->type),
  2152. ggml_backend_buft_name(buft));
  2153. break;
  2154. }
  2155. }
  2156. }
  2157. if (!buft) {
  2158. buft = select_weight_buft(hparams, t_meta, op, *buft_list);
  2159. if (!buft) {
  2160. throw std::runtime_error(format("failed to find a compatible buffer type for tensor %s", tn.str().c_str()));
  2161. }
  2162. }
  2163. // avoid using a host buffer when using mmap
  2164. auto * buft_dev = ggml_backend_buft_get_device(buft);
  2165. if (ml.use_mmap && buft_dev && buft == ggml_backend_dev_host_buffer_type(buft_dev)) {
  2166. auto * cpu_dev = ggml_backend_dev_by_type(GGML_BACKEND_DEVICE_TYPE_CPU);
  2167. if (!cpu_dev) {
  2168. throw std::runtime_error("no CPU backend found");
  2169. }
  2170. buft = ggml_backend_dev_buffer_type(cpu_dev);
  2171. }
  2172. if (buft != buft_list->front().second) {
  2173. n_moved_tensors++;
  2174. if (!first_moved_tensor) {
  2175. first_moved_tensor = t_meta;
  2176. first_moved_from_buft = buft_list->front().second;
  2177. first_moved_to_buft = buft;
  2178. }
  2179. }
  2180. ggml_context * ctx = ctx_for_buft(buft);
  2181. // if duplicated, check if the original tensor was allocated in the same buffer type context and avoid creating a new one
  2182. if (flags & TENSOR_DUPLICATED) {
  2183. ggml_tensor * t = ggml_get_tensor(ctx, tn.str().c_str());
  2184. if (t) {
  2185. return t;
  2186. }
  2187. }
  2188. return ml.create_tensor(ctx, tn, ne, flags);
  2189. };
  2190. layers.resize(n_layer);
  2191. // TODO: move to a separate function
  2192. const auto tn = LLM_TN(arch);
  2193. switch (arch) {
  2194. case LLM_ARCH_LLAMA:
  2195. case LLM_ARCH_REFACT:
  2196. case LLM_ARCH_MINICPM:
  2197. case LLM_ARCH_GRANITE:
  2198. case LLM_ARCH_GRANITE_MOE:
  2199. {
  2200. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2201. // output
  2202. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2203. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  2204. // if output is NULL, init from the input tok embed
  2205. if (output == NULL) {
  2206. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  2207. }
  2208. for (int i = 0; i < n_layer; ++i) {
  2209. auto & layer = layers[i];
  2210. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2211. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  2212. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  2213. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  2214. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  2215. // optional bias tensors
  2216. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2217. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED);
  2218. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED);
  2219. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2220. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2221. if (hparams.rope_scaling_type_train == LLAMA_ROPE_SCALING_TYPE_LONGROPE) {
  2222. layer.rope_long = create_tensor(tn(LLM_TENSOR_ROPE_FACTORS_LONG, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  2223. layer.rope_short = create_tensor(tn(LLM_TENSOR_ROPE_FACTORS_SHORT, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  2224. }
  2225. else {
  2226. layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  2227. }
  2228. if (n_expert == 0) {
  2229. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  2230. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  2231. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2232. // optional MLP bias
  2233. layer.ffn_gate_b = create_tensor(tn(LLM_TENSOR_FFN_GATE, "bias", i), {n_ff}, TENSOR_NOT_REQUIRED);
  2234. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2235. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, TENSOR_NOT_REQUIRED);
  2236. } else {
  2237. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  2238. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff, n_expert}, TENSOR_NOT_REQUIRED);
  2239. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), { n_ff, n_embd, n_expert}, 0);
  2240. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0);
  2241. // For Granite MoE Shared
  2242. if (hparams.n_ff_shexp > 0) {
  2243. layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), {n_embd, hparams.n_ff_shexp}, 0);
  2244. layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), {n_embd, hparams.n_ff_shexp}, 0);
  2245. layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), {hparams.n_ff_shexp, n_embd}, 0);
  2246. }
  2247. }
  2248. }
  2249. } break;
  2250. case LLM_ARCH_LLADA:
  2251. {
  2252. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, 0);
  2253. // output
  2254. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), { n_embd }, 0);
  2255. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), { n_embd, n_vocab }, TENSOR_NOT_REQUIRED);
  2256. // if output is NULL, init from the input tok embed
  2257. if (output == NULL) {
  2258. output =
  2259. create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, TENSOR_DUPLICATED);
  2260. }
  2261. for (int i = 0; i < n_layer; ++i) {
  2262. auto & layer = layers[i];
  2263. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), { n_embd }, 0);
  2264. // Use separate Q, K, V projections without bias, matching LLaDALlamaBlock
  2265. layer.wq =
  2266. create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), { n_embd, n_embd_head_k * n_head }, 0);
  2267. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), { n_embd, n_embd_k_gqa }, 0);
  2268. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), { n_embd, n_embd_v_gqa }, 0);
  2269. // No bias for QKV projections as per config: include_bias=false, include_qkv_bias=false
  2270. layer.wo =
  2271. create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), { n_embd_head_k * n_head, n_embd }, 0);
  2272. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), { n_embd }, TENSOR_NOT_REQUIRED);
  2273. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), { n_embd }, 0);
  2274. layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), { n_rot / 2 },
  2275. TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  2276. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), { n_embd, n_ff }, 0);
  2277. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd }, 0);
  2278. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), { n_embd, n_ff }, 0);
  2279. // optional MLP bias
  2280. layer.ffn_gate_b =
  2281. create_tensor(tn(LLM_TENSOR_FFN_GATE, "bias", i), { n_ff }, TENSOR_NOT_REQUIRED);
  2282. layer.ffn_down_b =
  2283. create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), { n_embd }, TENSOR_NOT_REQUIRED);
  2284. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), { n_ff }, TENSOR_NOT_REQUIRED);
  2285. }
  2286. }
  2287. break;
  2288. case LLM_ARCH_LLADA_MOE:
  2289. {
  2290. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2291. // output
  2292. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2293. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  2294. GGML_ASSERT(n_expert > 0 && "n_expert must be > 0 for llada-moe");
  2295. GGML_ASSERT(n_expert_used > 0 && "n_expert_used must be > 0 for llada-moe");
  2296. for (int i = 0; i < n_layer; ++i) {
  2297. auto & layer = layers[i];
  2298. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2299. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  2300. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  2301. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  2302. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2303. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0);
  2304. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0);
  2305. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2306. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  2307. const int64_t n_ff_exp = hparams.n_ff_exp ? hparams.n_ff_exp : n_ff / n_expert_used;
  2308. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);
  2309. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp, n_embd, n_expert}, 0);
  2310. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);
  2311. }
  2312. } break;
  2313. case LLM_ARCH_LLAMA4:
  2314. {
  2315. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2316. // output
  2317. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2318. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  2319. // if output is NULL, init from the input tok embed
  2320. if (output == NULL) {
  2321. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  2322. }
  2323. for (int i = 0; i < n_layer; ++i) {
  2324. bool is_moe_layer = hparams.n_moe_layer_step > 0 && (i + 1) % hparams.n_moe_layer_step == 0;
  2325. auto & layer = layers[i];
  2326. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2327. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  2328. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  2329. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  2330. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  2331. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2332. layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  2333. if (is_moe_layer) {
  2334. int n_ff_exp = hparams.n_ff_exp;
  2335. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  2336. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff_exp, n_expert}, 0);
  2337. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), { n_ff_exp, n_embd, n_expert}, 0);
  2338. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff_exp, n_expert}, 0);
  2339. // Shared expert
  2340. const int64_t n_ff_shexp = n_ff_exp;
  2341. layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), { n_embd, n_ff_shexp}, 0);
  2342. layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), {n_ff_shexp, n_embd }, 0);
  2343. layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), { n_embd, n_ff_shexp}, 0);
  2344. } else {
  2345. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  2346. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  2347. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2348. }
  2349. }
  2350. } break;
  2351. case LLM_ARCH_DECI:
  2352. {
  2353. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2354. // output
  2355. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2356. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  2357. // if output is NULL, init from the input tok embed
  2358. if (output == NULL) {
  2359. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  2360. }
  2361. for (int i = 0; i < n_layer; ++i) {
  2362. auto & layer = layers[i];
  2363. const int64_t n_embd_k_gqa = hparams.n_embd_k_gqa(i);
  2364. const int64_t n_embd_v_gqa = hparams.n_embd_v_gqa(i);
  2365. const int64_t n_embd_gqa = hparams.n_embd_v_gqa(i);
  2366. const int64_t n_ff = hparams.n_ff(i);
  2367. const int64_t n_head = hparams.n_head(i);
  2368. const int64_t n_head_kv = hparams.n_head_kv(i);
  2369. if (n_head_kv == 0 && n_head > 0) {
  2370. // linear attention for DeciLMCausalModel
  2371. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2372. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2373. }
  2374. else if (n_head_kv > 0) {
  2375. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2376. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  2377. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  2378. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  2379. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  2380. }
  2381. // optional bias tensors
  2382. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2383. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED);
  2384. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED);
  2385. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2386. if (n_ff > 0) {
  2387. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2388. }
  2389. if (hparams.rope_scaling_type_train == LLAMA_ROPE_SCALING_TYPE_LONGROPE) {
  2390. layer.rope_long = create_tensor(tn(LLM_TENSOR_ROPE_FACTORS_LONG, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  2391. layer.rope_short = create_tensor(tn(LLM_TENSOR_ROPE_FACTORS_SHORT, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  2392. }
  2393. else {
  2394. layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  2395. }
  2396. if (n_ff > 0) {
  2397. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  2398. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  2399. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2400. }
  2401. // optional MLP bias
  2402. layer.ffn_gate_b = create_tensor(tn(LLM_TENSOR_FFN_GATE, "bias", i), {n_ff}, TENSOR_NOT_REQUIRED);
  2403. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2404. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, TENSOR_NOT_REQUIRED);
  2405. }
  2406. } break;
  2407. case LLM_ARCH_MINICPM3:
  2408. {
  2409. const int64_t n_embd_head_qk_rope = hparams.n_rot;
  2410. const int64_t n_embd_head_qk_nope = hparams.n_embd_head_k - hparams.n_rot;
  2411. const int64_t q_lora_rank = hparams.n_lora_q;
  2412. const int64_t kv_lora_rank = hparams.n_lora_kv;
  2413. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2414. // output
  2415. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2416. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  2417. // if output is NULL, init from the input tok embed
  2418. if (output == NULL) {
  2419. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  2420. }
  2421. for (int i = 0; i < n_layer; ++i) {
  2422. auto & layer = layers[i];
  2423. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2424. layer.attn_q_a_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_A_NORM, "weight", i), {q_lora_rank}, 0);
  2425. layer.attn_kv_a_norm = create_tensor(tn(LLM_TENSOR_ATTN_KV_A_NORM, "weight", i), {kv_lora_rank}, 0);
  2426. layer.wq_a = create_tensor(tn(LLM_TENSOR_ATTN_Q_A, "weight", i), {n_embd, q_lora_rank}, 0);
  2427. layer.wq_b = create_tensor(tn(LLM_TENSOR_ATTN_Q_B, "weight", i), {q_lora_rank, n_head * n_embd_head_k}, 0);
  2428. 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);
  2429. 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);
  2430. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), { n_head * ( n_embd_head_v), n_embd}, 0);
  2431. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2432. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  2433. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  2434. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2435. 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));
  2436. 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));
  2437. }
  2438. } break;
  2439. case LLM_ARCH_GROK:
  2440. {
  2441. if (n_expert == 0) {
  2442. throw std::runtime_error("Grok model cannot have zero experts");
  2443. }
  2444. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2445. // output
  2446. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2447. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  2448. // if output is NULL, init from the input tok embed
  2449. if (output == NULL) {
  2450. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  2451. }
  2452. 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
  2453. for (int i = 0; i < n_layer; ++i) {
  2454. auto & layer = layers[i];
  2455. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2456. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  2457. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  2458. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  2459. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2460. layer.attn_out_norm = create_tensor(tn(LLM_TENSOR_ATTN_OUT_NORM, "weight", i), {n_embd}, 0);
  2461. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2462. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, TENSOR_NOT_REQUIRED);
  2463. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, TENSOR_NOT_REQUIRED);
  2464. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, TENSOR_NOT_REQUIRED);
  2465. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  2466. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff_exp, n_expert}, TENSOR_NOT_REQUIRED);
  2467. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp, n_embd, n_expert}, 0);
  2468. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff_exp, n_expert}, 0);
  2469. layer.ffn_post_norm = create_tensor(tn(LLM_TENSOR_LAYER_OUT_NORM, "weight", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2470. if (!layer.ffn_post_norm) {
  2471. layer.ffn_post_norm = create_tensor(tn(LLM_TENSOR_FFN_POST_NORM, "weight", i), {n_embd}, 0);
  2472. }
  2473. }
  2474. } break;
  2475. case LLM_ARCH_DBRX:
  2476. {
  2477. if (n_expert == 0) {
  2478. throw std::runtime_error("DBRX model cannot have zero experts");
  2479. }
  2480. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2481. // output
  2482. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2483. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  2484. for (int i = 0; i < n_layer; ++i) {
  2485. auto & layer = layers[i];
  2486. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2487. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, 0);
  2488. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2489. layer.attn_out_norm = create_tensor(tn(LLM_TENSOR_ATTN_OUT_NORM, "weight", i), {n_embd}, 0);
  2490. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  2491. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0);
  2492. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff, n_embd, n_expert}, 0);
  2493. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0);
  2494. }
  2495. } break;
  2496. case LLM_ARCH_BAICHUAN:
  2497. {
  2498. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2499. {
  2500. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2501. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  2502. }
  2503. for (int i = 0; i < n_layer; ++i) {
  2504. auto & layer = layers[i];
  2505. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2506. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  2507. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  2508. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  2509. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2510. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2511. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  2512. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  2513. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2514. }
  2515. } break;
  2516. case LLM_ARCH_FALCON:
  2517. {
  2518. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2519. // output
  2520. {
  2521. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2522. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
  2523. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  2524. if (!output) {
  2525. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); // needs to be on GPU
  2526. }
  2527. }
  2528. for (int i = 0; i < n_layer; ++i) {
  2529. auto & layer = layers[i];
  2530. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2531. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0);
  2532. layer.attn_norm_2 = create_tensor(tn(LLM_TENSOR_ATTN_NORM_2, "weight", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2533. layer.attn_norm_2_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM_2, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2534. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, 0);
  2535. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2536. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  2537. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2538. }
  2539. } break;
  2540. case LLM_ARCH_STARCODER:
  2541. {
  2542. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2543. pos_embd = create_tensor(tn(LLM_TENSOR_POS_EMBD, "weight"), {n_embd, n_ctx_train}, 0);
  2544. // output
  2545. {
  2546. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2547. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
  2548. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  2549. if (!output) {
  2550. // needs to be on GPU
  2551. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  2552. }
  2553. }
  2554. for (int i = 0; i < n_layer; ++i) {
  2555. auto & layer = layers[i];
  2556. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2557. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0);
  2558. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, 0);
  2559. layer.bqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd + 2*n_embd_gqa}, 0);
  2560. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2561. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0);
  2562. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2563. layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd}, 0);
  2564. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  2565. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, 0);
  2566. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2567. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, 0);
  2568. }
  2569. } break;
  2570. case LLM_ARCH_BERT:
  2571. case LLM_ARCH_NOMIC_BERT:
  2572. case LLM_ARCH_NOMIC_BERT_MOE:
  2573. case LLM_ARCH_JINA_BERT_V3:
  2574. {
  2575. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2576. type_embd = create_tensor(tn(LLM_TENSOR_TOKEN_TYPES, "weight"), {n_embd, n_token_types}, TENSOR_NOT_REQUIRED);
  2577. if (arch == LLM_ARCH_BERT) {
  2578. pos_embd = create_tensor(tn(LLM_TENSOR_POS_EMBD, "weight"), {n_embd, n_ctx_train}, 0);
  2579. cls = create_tensor(tn(LLM_TENSOR_CLS, "weight"), {n_embd, n_embd}, TENSOR_NOT_REQUIRED);
  2580. cls_b = create_tensor(tn(LLM_TENSOR_CLS, "bias"), {n_embd}, TENSOR_NOT_REQUIRED);
  2581. cls_out = create_tensor(tn(LLM_TENSOR_CLS_OUT, "weight"), {n_embd, hparams.n_cls_out}, TENSOR_NOT_REQUIRED);
  2582. cls_out_b = create_tensor(tn(LLM_TENSOR_CLS_OUT, "bias"), {hparams.n_cls_out}, TENSOR_NOT_REQUIRED);
  2583. }
  2584. tok_norm = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "weight"), {n_embd}, 0);
  2585. tok_norm_b = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "bias"), {n_embd}, 0);
  2586. for (int i = 0; i < n_layer; ++i) {
  2587. auto & layer = layers[i];
  2588. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, TENSOR_NOT_REQUIRED);
  2589. layer.bqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd + 2*n_embd_gqa}, TENSOR_NOT_REQUIRED);
  2590. if (!layer.wqkv) {
  2591. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  2592. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, 0);
  2593. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  2594. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, 0);
  2595. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  2596. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, 0);
  2597. }
  2598. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2599. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2600. layer.attn_out_norm = create_tensor(tn(LLM_TENSOR_ATTN_OUT_NORM, "weight", i), {n_embd}, 0);
  2601. layer.attn_out_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_OUT_NORM, "bias", i), {n_embd}, 0);
  2602. if (hparams.moe_every_n_layers > 0 && i % hparams.moe_every_n_layers == 1) {
  2603. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff, n_expert}, 0);
  2604. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), { n_ff, n_embd, n_expert}, 0);
  2605. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  2606. } else {
  2607. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2608. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, TENSOR_NOT_REQUIRED);
  2609. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  2610. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2611. if (arch == LLM_ARCH_NOMIC_BERT) {
  2612. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  2613. }
  2614. }
  2615. layer.layer_out_norm = create_tensor(tn(LLM_TENSOR_LAYER_OUT_NORM, "weight", i), {n_embd}, 0);
  2616. layer.layer_out_norm_b = create_tensor(tn(LLM_TENSOR_LAYER_OUT_NORM, "bias", i), {n_embd}, 0);
  2617. }
  2618. } break;
  2619. case LLM_ARCH_NEO_BERT:
  2620. {
  2621. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2622. cls = create_tensor(tn(LLM_TENSOR_CLS, "weight"), {n_embd, n_embd}, TENSOR_NOT_REQUIRED);
  2623. cls_b = create_tensor(tn(LLM_TENSOR_CLS, "bias"), {n_embd}, TENSOR_NOT_REQUIRED);
  2624. cls_out = create_tensor(tn(LLM_TENSOR_CLS_OUT, "weight"), {n_embd, hparams.n_cls_out}, TENSOR_NOT_REQUIRED);
  2625. cls_out_b = create_tensor(tn(LLM_TENSOR_CLS_OUT, "bias"), {hparams.n_cls_out}, TENSOR_NOT_REQUIRED);
  2626. output_norm_enc = create_tensor(tn(LLM_TENSOR_ENC_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2627. for (int i = 0; i < n_layer; ++i) {
  2628. auto & layer = layers[i];
  2629. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2630. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, 0);
  2631. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2632. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2633. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff*2}, 0);
  2634. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  2635. }
  2636. } break;
  2637. case LLM_ARCH_JINA_BERT_V2:
  2638. {
  2639. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); // word_embeddings
  2640. type_embd = create_tensor(tn(LLM_TENSOR_TOKEN_TYPES, "weight"), {n_embd, n_token_types}, 0); // token_type_embeddings
  2641. tok_norm = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "weight"), {n_embd}, 0); // LayerNorm
  2642. tok_norm_b = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "bias"), {n_embd}, 0); //LayerNorm bias
  2643. cls = create_tensor(tn(LLM_TENSOR_CLS, "weight"), {n_embd, 1}, TENSOR_NOT_REQUIRED);
  2644. cls_b = create_tensor(tn(LLM_TENSOR_CLS, "bias"), {1}, TENSOR_NOT_REQUIRED);
  2645. for (int i = 0; i < n_layer; ++i) {
  2646. auto & layer = layers[i]; // JinaBertLayer
  2647. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  2648. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, 0);
  2649. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2650. layer.attn_q_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2651. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  2652. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, 0);
  2653. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2654. layer.attn_k_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2655. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  2656. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, 0);
  2657. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0); //output_dens
  2658. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0); //output_dens
  2659. layer.attn_out_norm = create_tensor(tn(LLM_TENSOR_ATTN_OUT_NORM, "weight", i), {n_embd}, 0); //output_norm
  2660. layer.attn_out_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_OUT_NORM, "bias", i), {n_embd}, 0);
  2661. layer.attn_norm_2 = create_tensor(tn(LLM_TENSOR_ATTN_NORM_2, "weight", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2662. layer.attn_norm_2_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM_2, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2663. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, TENSOR_NOT_REQUIRED);
  2664. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, layer.ffn_gate ? n_ff : n_ff * 2}, 0);
  2665. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  2666. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, 0);
  2667. layer.layer_out_norm = create_tensor(tn(LLM_TENSOR_LAYER_OUT_NORM, "weight", i), {n_embd}, 0);
  2668. layer.layer_out_norm_b = create_tensor(tn(LLM_TENSOR_LAYER_OUT_NORM, "bias", i), {n_embd}, 0);
  2669. }
  2670. } break;
  2671. case LLM_ARCH_BLOOM:
  2672. {
  2673. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2674. tok_norm = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "weight"), {n_embd}, 0);
  2675. tok_norm_b = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "bias"), {n_embd}, 0);
  2676. // output
  2677. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2678. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
  2679. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  2680. // if output is NULL, init from the input tok embed
  2681. if (output == NULL) {
  2682. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  2683. }
  2684. for (int i = 0; i < n_layer; ++i) {
  2685. auto & layer = layers[i];
  2686. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2687. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0);
  2688. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, 0);
  2689. layer.bqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd + 2*n_embd_gqa}, 0);
  2690. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2691. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0);
  2692. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2693. layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd}, 0);
  2694. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  2695. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, 0);
  2696. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2697. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, 0);
  2698. }
  2699. } break;
  2700. case LLM_ARCH_MPT:
  2701. {
  2702. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2703. pos_embd = create_tensor(tn(LLM_TENSOR_POS_EMBD, "weight"), {n_embd, n_ctx_train}, TENSOR_NOT_REQUIRED);
  2704. // output
  2705. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2706. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, TENSOR_NOT_REQUIRED);
  2707. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  2708. if (!output) {
  2709. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); // needs to be on GPU
  2710. }
  2711. for (int i = 0; i < n_layer; ++i) {
  2712. auto & layer = layers[i];
  2713. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2714. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2715. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, 0);
  2716. layer.bqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd + 2*n_embd_gqa}, TENSOR_NOT_REQUIRED);
  2717. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2718. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2719. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2720. layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2721. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  2722. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2723. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2724. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, TENSOR_NOT_REQUIRED);
  2725. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2726. layer.attn_q_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2727. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2728. layer.attn_k_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2729. // AWQ ScaleActivation layer
  2730. layer.ffn_act = create_tensor(tn(LLM_TENSOR_FFN_ACT, "scales", i), {n_ff}, TENSOR_NOT_REQUIRED);
  2731. }
  2732. } break;
  2733. case LLM_ARCH_STABLELM:
  2734. {
  2735. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2736. // output
  2737. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
  2738. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2739. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  2740. for (int i = 0; i < n_layer; ++i) {
  2741. auto & layer = layers[i];
  2742. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2743. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0);
  2744. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  2745. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  2746. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  2747. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2748. // optional bias tensors, present in Stable LM 2 1.6B
  2749. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2750. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED);
  2751. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED);
  2752. // optional q and k layernorms, present in StableLM 2 12B
  2753. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k, n_head}, TENSOR_NOT_REQUIRED);
  2754. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k, n_head_kv}, TENSOR_NOT_REQUIRED);
  2755. // optional FFN norm, not present in StableLM 2 12B which uses parallel residual
  2756. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2757. layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2758. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  2759. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  2760. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2761. }
  2762. } break;
  2763. case LLM_ARCH_QWEN:
  2764. {
  2765. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2766. // output
  2767. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2768. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  2769. for (int i = 0; i < n_layer; ++i) {
  2770. auto & layer = layers[i];
  2771. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2772. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd*3}, 0);
  2773. layer.bqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd*3}, 0);
  2774. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2775. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2776. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff/2}, 0);
  2777. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff/2, n_embd}, 0);
  2778. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff/2}, 0);
  2779. }
  2780. } break;
  2781. case LLM_ARCH_QWEN2:
  2782. case LLM_ARCH_QWEN2VL:
  2783. case LLM_ARCH_DREAM:
  2784. {
  2785. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2786. // output
  2787. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2788. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  2789. output_b = create_tensor(tn(LLM_TENSOR_OUTPUT, "bias"), {n_vocab}, TENSOR_NOT_REQUIRED);
  2790. // if output is NULL, init from the input tok embed
  2791. if (output == NULL) {
  2792. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  2793. }
  2794. for (int i = 0; i < n_layer; ++i) {
  2795. auto & layer = layers[i];
  2796. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2797. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  2798. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  2799. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  2800. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2801. // optional bias tensors
  2802. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, 0);
  2803. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, 0);
  2804. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, 0);
  2805. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2806. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  2807. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  2808. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2809. }
  2810. } break;
  2811. case LLM_ARCH_QWEN2MOE:
  2812. {
  2813. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2814. // output
  2815. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2816. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  2817. for (int i = 0; i < n_layer; ++i) {
  2818. auto & layer = layers[i];
  2819. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2820. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  2821. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  2822. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  2823. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2824. // optional bias tensors
  2825. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2826. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED);
  2827. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED);
  2828. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2829. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  2830. if (n_expert == 0) {
  2831. throw std::runtime_error("n_expert must be > 0 for QWEN2MOE");
  2832. }
  2833. if (n_expert_used == 0) {
  2834. throw std::runtime_error("n_expert_used must be > 0 for QWEN2MOE");
  2835. }
  2836. // MoE branch
  2837. const int64_t n_ff_exp = hparams.n_ff_exp ? hparams.n_ff_exp : n_ff / n_expert_used;
  2838. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);
  2839. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp, n_embd, n_expert}, 0);
  2840. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);
  2841. // Shared expert branch
  2842. const int64_t n_ff_shexp = hparams.n_ff_shexp ? hparams.n_ff_shexp : n_ff;
  2843. layer.ffn_gate_inp_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP_SHEXP, "weight", i), {n_embd}, 0);
  2844. layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), { n_embd, n_ff_shexp}, 0);
  2845. layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), {n_ff_shexp, n_embd}, 0);
  2846. layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), { n_embd, n_ff_shexp}, 0);
  2847. }
  2848. } break;
  2849. case LLM_ARCH_QWEN3:
  2850. {
  2851. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2852. // output
  2853. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2854. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  2855. // if output is NULL, init from the input tok embed
  2856. if (output == NULL) {
  2857. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  2858. }
  2859. // output rerank head
  2860. cls_out = create_tensor(tn(LLM_TENSOR_CLS_OUT, "weight"), {n_embd, hparams.n_cls_out}, TENSOR_NOT_REQUIRED);
  2861. for (int i = 0; i < n_layer; ++i) {
  2862. auto & layer = layers[i];
  2863. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2864. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  2865. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  2866. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  2867. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  2868. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0);
  2869. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0);
  2870. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2871. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  2872. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  2873. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2874. }
  2875. } break;
  2876. case LLM_ARCH_QWEN3MOE:
  2877. {
  2878. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2879. // output
  2880. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2881. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  2882. // if output is NULL, init from the input tok embed
  2883. if (output == NULL) {
  2884. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  2885. }
  2886. for (int i = 0; i < n_layer; ++i) {
  2887. auto & layer = layers[i];
  2888. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2889. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  2890. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  2891. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  2892. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  2893. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0);
  2894. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0);
  2895. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2896. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  2897. if (n_expert == 0) {
  2898. throw std::runtime_error("n_expert must be > 0 for QWEN3MOE");
  2899. }
  2900. if (n_expert_used == 0) {
  2901. throw std::runtime_error("n_expert_used must be > 0 for QWEN3MOE");
  2902. }
  2903. // MoE branch
  2904. const int64_t n_ff_exp = hparams.n_ff_exp ? hparams.n_ff_exp : n_ff / n_expert_used;
  2905. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);
  2906. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp, n_embd, n_expert}, 0);
  2907. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);
  2908. }
  2909. } break;
  2910. case LLM_ARCH_PHI2:
  2911. {
  2912. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2913. // output
  2914. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2915. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
  2916. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  2917. output_b = create_tensor(tn(LLM_TENSOR_OUTPUT, "bias"), {n_vocab}, 0);
  2918. for (int i = 0; i < n_layer; ++i) {
  2919. auto & layer = layers[i];
  2920. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2921. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0);
  2922. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, TENSOR_NOT_REQUIRED);
  2923. layer.bqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd + 2*n_embd_gqa}, TENSOR_NOT_REQUIRED);
  2924. if (layer.wqkv == nullptr) {
  2925. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  2926. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, 0);
  2927. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  2928. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, 0);
  2929. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  2930. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, 0);
  2931. }
  2932. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2933. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0);
  2934. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  2935. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, 0);
  2936. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2937. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, 0);
  2938. }
  2939. } break;
  2940. case LLM_ARCH_PHI3:
  2941. {
  2942. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, 0);
  2943. // output
  2944. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), { n_embd }, 0);
  2945. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  2946. // if output is NULL, init from the input tok embed
  2947. if (output == NULL) {
  2948. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  2949. }
  2950. for (int i = 0; i < n_layer; ++i) {
  2951. auto & layer = layers[i];
  2952. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), { n_embd }, 0);
  2953. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), { n_embd, n_embd + 2 * n_embd_gqa }, TENSOR_NOT_REQUIRED);
  2954. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), { n_embd, n_embd }, 0);
  2955. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), { n_embd }, 0);
  2956. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd }, 0);
  2957. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), { n_embd, 2 * n_ff }, 0);
  2958. layer.rope_long = create_tensor(tn(LLM_TENSOR_ROPE_FACTORS_LONG, "weight", i), { n_rot/2 }, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  2959. layer.rope_short = create_tensor(tn(LLM_TENSOR_ROPE_FACTORS_SHORT, "weight", i), { n_rot/2 }, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  2960. }
  2961. } break;
  2962. case LLM_ARCH_PHIMOE:
  2963. {
  2964. const int64_t n_embd_head = n_embd / n_head;
  2965. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, 0);
  2966. // output
  2967. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), { n_embd }, 0);
  2968. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
  2969. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), { n_embd, n_vocab }, 0);
  2970. output_b = create_tensor(tn(LLM_TENSOR_OUTPUT, "bias"), { n_vocab }, 0);
  2971. for (int i = 0; i < n_layer; ++i) {
  2972. auto & layer = layers[i];
  2973. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), { n_embd }, 0);
  2974. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), { n_embd }, 0);
  2975. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), { n_embd, n_embd + 2 * n_embd_gqa }, TENSOR_NOT_REQUIRED);
  2976. if (layer.wqkv == nullptr) {
  2977. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  2978. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, 0);
  2979. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  2980. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, 0);
  2981. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  2982. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, 0);
  2983. }
  2984. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), { n_embd, n_embd }, 0);
  2985. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), { n_embd }, 0);
  2986. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), { n_embd }, 0);
  2987. layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), { n_embd }, 0);
  2988. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  2989. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0);
  2990. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff, n_embd, n_expert}, 0);
  2991. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0);
  2992. 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));
  2993. 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));
  2994. }
  2995. } break;
  2996. case LLM_ARCH_PLAMO:
  2997. {
  2998. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2999. // output
  3000. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3001. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  3002. for (int i = 0; i < n_layer; ++i) {
  3003. auto & layer = layers[i];
  3004. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3005. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  3006. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  3007. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  3008. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  3009. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  3010. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3011. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3012. }
  3013. } break;
  3014. case LLM_ARCH_PLAMO2:
  3015. {
  3016. // mamba parameters
  3017. const uint32_t d_conv = hparams.ssm_d_conv;
  3018. const uint32_t d_state = hparams.ssm_d_state;
  3019. const uint32_t num_heads = hparams.ssm_dt_rank;
  3020. const uint32_t intermediate_size = hparams.ssm_d_inner;
  3021. const int64_t dt_dim = std::max(64, int(hparams.n_embd / 16));
  3022. // attention parameters
  3023. const uint32_t qk_dim = hparams.n_embd_head_k;
  3024. const uint32_t v_dim = hparams.n_embd_head_v;
  3025. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3026. // output
  3027. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3028. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  3029. // if output is NULL, init from the input tok embed
  3030. if (output == NULL) {
  3031. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3032. }
  3033. for (int i = 0; i < n_layer; ++i) {
  3034. auto & layer = layers[i];
  3035. bool is_mamba_layer = hparams.is_recurrent(i);
  3036. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3037. if (is_mamba_layer) {
  3038. layer.ssm_in = create_tensor(tn(LLM_TENSOR_SSM_IN, "weight", i), {n_embd, 2 * intermediate_size}, 0);
  3039. layer.ssm_conv1d = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "weight", i), {d_conv, intermediate_size}, 0);
  3040. layer.ssm_x = create_tensor(tn(LLM_TENSOR_SSM_X, "weight", i), {intermediate_size, dt_dim + 2*d_state}, 0);
  3041. layer.ssm_dt = create_tensor(tn(LLM_TENSOR_SSM_DT, "weight", i), {dt_dim, num_heads}, 0);
  3042. layer.ssm_dt_b = create_tensor(tn(LLM_TENSOR_SSM_DT, "bias", i), {num_heads}, 0);
  3043. layer.ssm_a = create_tensor(tn(LLM_TENSOR_SSM_A, i), {num_heads}, 0);
  3044. layer.ssm_d = create_tensor(tn(LLM_TENSOR_SSM_D, i), {num_heads}, 0);
  3045. layer.ssm_out = create_tensor(tn(LLM_TENSOR_SSM_OUT, "weight", i), {intermediate_size, n_embd}, 0);
  3046. layer.ssm_dt_norm = create_tensor(tn(LLM_TENSOR_SSM_DT_NORM, i), {dt_dim}, 0);
  3047. layer.ssm_b_norm = create_tensor(tn(LLM_TENSOR_SSM_B_NORM, i), {d_state}, 0);
  3048. layer.ssm_c_norm = create_tensor(tn(LLM_TENSOR_SSM_C_NORM, i), {d_state}, 0);
  3049. } else {
  3050. const int64_t num_attention_heads = hparams.n_head(i);
  3051. const int64_t q_num_heads = num_attention_heads;
  3052. const int64_t num_key_value_heads = hparams.n_head_kv(i);
  3053. const int64_t k_num_heads = num_key_value_heads;
  3054. const int64_t v_num_heads = num_key_value_heads;
  3055. const int64_t q_proj_dim = q_num_heads * qk_dim;
  3056. const int64_t k_proj_dim = k_num_heads * qk_dim;
  3057. const int64_t v_proj_dim = v_num_heads * v_dim;
  3058. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, q_proj_dim + k_proj_dim + v_proj_dim}, 0);
  3059. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {qk_dim, num_attention_heads}, 0);
  3060. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {qk_dim, k_num_heads}, 0);
  3061. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {q_num_heads * v_dim, n_embd}, 0);
  3062. }
  3063. // All layers have post-attention norm, FFN norm, and FFN tensors
  3064. layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, i), {n_embd}, 0);
  3065. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3066. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  3067. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff * 2}, 0);
  3068. layer.ffn_post_norm = create_tensor(tn(LLM_TENSOR_FFN_POST_NORM, i), {n_embd}, 0);
  3069. }
  3070. } break;
  3071. case LLM_ARCH_GPT2:
  3072. {
  3073. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3074. pos_embd = create_tensor(tn(LLM_TENSOR_POS_EMBD, "weight"), {n_embd, n_ctx_train}, 0);
  3075. // output
  3076. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3077. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
  3078. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  3079. // if output is NULL, init from the input tok embed
  3080. if (output == NULL) {
  3081. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3082. }
  3083. for (int i = 0; i < n_layer; ++i) {
  3084. auto & layer = layers[i];
  3085. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3086. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0);
  3087. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, 0);
  3088. layer.bqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd + 2*n_embd_gqa}, 0);
  3089. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  3090. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0);
  3091. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3092. layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd}, 0);
  3093. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  3094. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, 0);
  3095. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3096. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, 0);
  3097. }
  3098. } break;
  3099. case LLM_ARCH_CODESHELL:
  3100. {
  3101. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  3102. // if tok embd is NULL, init from output
  3103. if (tok_embd == NULL) {
  3104. tok_embd = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3105. }
  3106. // output
  3107. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3108. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
  3109. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  3110. for (int i = 0; i < n_layer; ++i) {
  3111. auto & layer = layers[i];
  3112. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3113. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", 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.bqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd + 2*n_embd_gqa}, 0);
  3116. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  3117. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0);
  3118. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3119. layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd}, 0);
  3120. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  3121. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, 0);
  3122. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3123. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, 0);
  3124. }
  3125. } break;
  3126. case LLM_ARCH_ORION:
  3127. {
  3128. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3129. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3130. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
  3131. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  3132. for (int i = 0; i < n_layer; ++i) {
  3133. auto & layer = layers[i];
  3134. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3135. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0);
  3136. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  3137. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  3138. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  3139. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  3140. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3141. layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd}, 0);
  3142. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  3143. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3144. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3145. }
  3146. } break;
  3147. case LLM_ARCH_INTERNLM2:
  3148. {
  3149. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3150. // output
  3151. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3152. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  3153. for (int i = 0; i < n_layer; ++i) {
  3154. auto & layer = layers[i];
  3155. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3156. // layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, 0);
  3157. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  3158. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  3159. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  3160. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  3161. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3162. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  3163. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3164. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3165. }
  3166. } break;
  3167. case LLM_ARCH_GEMMA:
  3168. {
  3169. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3170. // output
  3171. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3172. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); // same as tok_embd, duplicated to allow offloading
  3173. for (int i = 0; i < n_layer; ++i) {
  3174. auto & layer = layers[i];
  3175. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3176. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  3177. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  3178. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  3179. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  3180. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3181. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  3182. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3183. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3184. }
  3185. } break;
  3186. case LLM_ARCH_GEMMA2:
  3187. {
  3188. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3189. // output
  3190. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3191. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); // same as tok_embd, duplicated to allow offloading
  3192. for (int i = 0; i < n_layer; ++i) {
  3193. auto & layer = layers[i];
  3194. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3195. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  3196. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  3197. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  3198. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  3199. layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), {n_embd}, 0);
  3200. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3201. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  3202. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3203. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3204. layer.ffn_post_norm = create_tensor(tn(LLM_TENSOR_FFN_POST_NORM, "weight", i), {n_embd}, 0);
  3205. }
  3206. } break;
  3207. case LLM_ARCH_GEMMA3:
  3208. case LLM_ARCH_GEMMA_EMBEDDING:
  3209. {
  3210. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3211. // output
  3212. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3213. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  3214. // if output is NULL, init from the input tok embed
  3215. if (output == NULL) {
  3216. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3217. }
  3218. // Dense linear weights
  3219. dense_2_out_layers = create_tensor(tn(LLM_TENSOR_DENSE_2_OUT, "weight"), {n_embd, hparams.dense_2_feat_out}, TENSOR_NOT_REQUIRED);
  3220. dense_3_out_layers = create_tensor(tn(LLM_TENSOR_DENSE_3_OUT, "weight"), {hparams.dense_3_feat_in, n_embd}, TENSOR_NOT_REQUIRED);
  3221. for (int i = 0; i < n_layer; ++i) {
  3222. auto & layer = layers[i];
  3223. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3224. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  3225. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  3226. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  3227. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  3228. layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), {n_embd}, 0);
  3229. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0);
  3230. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0);
  3231. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3232. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  3233. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3234. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3235. layer.ffn_post_norm = create_tensor(tn(LLM_TENSOR_FFN_POST_NORM, "weight", i), {n_embd}, 0);
  3236. }
  3237. } break;
  3238. case LLM_ARCH_GEMMA3N:
  3239. {
  3240. const int64_t n_altup = hparams.n_altup;
  3241. const int64_t laurel_rank = hparams.laurel_rank;
  3242. const int64_t n_embd_altup = hparams.n_embd_altup;
  3243. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  3244. // if output is NULL, init from the input tok embed
  3245. if (output == NULL) {
  3246. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3247. }
  3248. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3249. tok_embd_per_layer = create_tensor(tn(LLM_TENSOR_PER_LAYER_TOKEN_EMBD, "weight"), {n_embd_altup * n_layer, n_vocab}, 0);
  3250. altup_proj = create_tensor(tn(LLM_TENSOR_ALTUP_PROJ, "weight"), {n_embd, n_embd, n_altup - 1}, 0);
  3251. altup_unembd_proj = create_tensor(tn(LLM_TENSOR_ALTUP_UNEMBD_PROJ, "weight"), {n_embd, n_embd, n_altup - 1}, 0);
  3252. per_layer_model_proj = create_tensor(tn(LLM_TENSOR_PER_LAYER_MODEL_PROJ, "weight"), {n_embd, n_embd_altup * n_layer}, 0);
  3253. per_layer_proj_norm = create_tensor(tn(LLM_TENSOR_PER_LAYER_PROJ_NORM, "weight"), {n_embd_altup}, 0);
  3254. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3255. for (int i = 0; i < n_layer; ++i) {
  3256. auto & layer = layers[i];
  3257. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3258. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  3259. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  3260. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  3261. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  3262. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0);
  3263. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0);
  3264. layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), {n_embd}, 0);
  3265. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3266. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  3267. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3268. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3269. layer.ffn_post_norm = create_tensor(tn(LLM_TENSOR_FFN_POST_NORM, "weight", i), {n_embd}, 0);
  3270. // altup & laurel
  3271. layer.per_layer_inp_gate = create_tensor(tn(LLM_TENSOR_PER_LAYER_INP_GATE, "weight", i), {n_embd, n_embd_altup}, 0);
  3272. layer.per_layer_proj = create_tensor(tn(LLM_TENSOR_PER_LAYER_PROJ, "weight", i), {n_embd_altup, n_embd}, 0);
  3273. layer.per_layer_post_norm = create_tensor(tn(LLM_TENSOR_PER_LAYER_POST_NORM, "weight", i), {n_embd}, 0);
  3274. layer.altup_correct_coef = create_tensor(tn(LLM_TENSOR_ALTUP_CORRECT_COEF, "weight", i), {n_altup, n_altup}, 0);
  3275. layer.altup_correct_scale = create_tensor(tn(LLM_TENSOR_ALTUP_CORRECT_SCALE, "weight", i), {n_embd}, 0);
  3276. layer.altup_predict_coef = create_tensor(tn(LLM_TENSOR_ALTUP_PREDICT_COEF, "weight", i), {n_altup, n_altup * n_altup}, 0);
  3277. layer.altup_router = create_tensor(tn(LLM_TENSOR_ALTUP_ROUTER, "weight", i), {n_embd, n_altup}, 0);
  3278. layer.altup_router_norm = create_tensor(tn(LLM_TENSOR_ALTUP_ROUTER_NORM, "weight", i), {n_embd}, 0);
  3279. layer.laurel_l = create_tensor(tn(LLM_TENSOR_LAUREL_L, "weight", i), {n_embd, laurel_rank}, 0);
  3280. layer.laurel_r = create_tensor(tn(LLM_TENSOR_LAUREL_R, "weight", i), {laurel_rank, n_embd}, 0);
  3281. layer.laurel_post_norm = create_tensor(tn(LLM_TENSOR_LAUREL_POST_NORM, "weight", i), {n_embd}, 0);
  3282. }
  3283. } break;
  3284. case LLM_ARCH_STARCODER2:
  3285. {
  3286. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3287. // output
  3288. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3289. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
  3290. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  3291. // if output is NULL, init from the input tok embed
  3292. if (output == NULL) {
  3293. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3294. }
  3295. for (int i = 0; i < n_layer; ++i) {
  3296. auto & layer = layers[i];
  3297. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3298. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0);
  3299. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  3300. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  3301. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  3302. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  3303. // optional bias tensors
  3304. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, 0);
  3305. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, 0);
  3306. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, 0);
  3307. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0);
  3308. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3309. layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd}, 0);
  3310. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3311. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3312. // optional bias tensors
  3313. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, 0);
  3314. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP , "bias", i), { n_ff}, 0);
  3315. }
  3316. } break;
  3317. case LLM_ARCH_MAMBA:
  3318. {
  3319. const int64_t d_conv = hparams.ssm_d_conv;
  3320. const int64_t d_inner = hparams.ssm_d_inner;
  3321. const int64_t d_state = hparams.ssm_d_state;
  3322. const int64_t dt_rank = hparams.ssm_dt_rank;
  3323. // only an expansion factor of 2 is supported for now
  3324. if (2 * n_embd != d_inner) {
  3325. throw std::runtime_error("only an expansion factor of 2 is supported for now");
  3326. }
  3327. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3328. // output
  3329. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3330. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  3331. // if output is NULL, init from the input tok embed, duplicated to allow offloading
  3332. if (output == NULL) {
  3333. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3334. }
  3335. for (int i = 0; i < n_layer; ++i) {
  3336. auto & layer = layers[i];
  3337. // norm
  3338. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3339. layer.ssm_in = create_tensor(tn(LLM_TENSOR_SSM_IN, "weight", i), {n_embd, 2*d_inner}, 0);
  3340. layer.ssm_conv1d = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "weight", i), {d_conv, d_inner}, 0);
  3341. layer.ssm_conv1d_b = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "bias", i), {d_inner}, 0);
  3342. layer.ssm_x = create_tensor(tn(LLM_TENSOR_SSM_X, "weight", i), {d_inner, dt_rank + 2*d_state}, 0);
  3343. layer.ssm_dt = create_tensor(tn(LLM_TENSOR_SSM_DT, "weight", i), {dt_rank, d_inner}, 0);
  3344. layer.ssm_dt_b = create_tensor(tn(LLM_TENSOR_SSM_DT, "bias", i), {d_inner}, 0);
  3345. // no "weight" suffix for these
  3346. layer.ssm_a = create_tensor(tn(LLM_TENSOR_SSM_A, i), {d_state, d_inner}, 0);
  3347. layer.ssm_d = create_tensor(tn(LLM_TENSOR_SSM_D, i), {d_inner}, 0);
  3348. // out_proj
  3349. layer.ssm_out = create_tensor(tn(LLM_TENSOR_SSM_OUT, "weight", i), {d_inner, n_embd}, 0);
  3350. }
  3351. } break;
  3352. case LLM_ARCH_MAMBA2:
  3353. {
  3354. const int64_t d_conv = hparams.ssm_d_conv;
  3355. const int64_t d_inner = hparams.ssm_d_inner;
  3356. const int64_t d_state = hparams.ssm_d_state;
  3357. const int64_t n_head = hparams.ssm_dt_rank;
  3358. const int64_t n_group = hparams.ssm_n_group;
  3359. const int64_t d_in_proj = 2*d_inner + 2*n_group*d_state + n_head;
  3360. // only an expansion factor of 2 is supported for now
  3361. GGML_ASSERT(2 * n_embd == d_inner);
  3362. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3363. // output
  3364. {
  3365. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3366. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  3367. // if output is NULL, init from the input tok embed, duplicated to allow offloading
  3368. if (output == NULL) {
  3369. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3370. }
  3371. }
  3372. for (int i = 0; i < n_layer; ++i) {
  3373. auto & layer = layers[i];
  3374. // norm
  3375. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3376. layer.ssm_in = create_tensor(tn(LLM_TENSOR_SSM_IN, "weight", i), {n_embd, d_in_proj}, 0);
  3377. layer.ssm_conv1d = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "weight", i), {d_conv, d_inner + 2*n_group*d_state}, 0);
  3378. layer.ssm_conv1d_b = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "bias", i), {d_inner + 2*n_group*d_state}, 0);
  3379. layer.ssm_dt_b = create_tensor(tn(LLM_TENSOR_SSM_DT, "bias", i), {n_head}, 0);
  3380. // no "weight" suffix for these
  3381. layer.ssm_a = create_tensor(tn(LLM_TENSOR_SSM_A, i), {1, n_head}, 0);
  3382. layer.ssm_d = create_tensor(tn(LLM_TENSOR_SSM_D, i), {1, n_head}, 0);
  3383. layer.ssm_norm = create_tensor(tn(LLM_TENSOR_SSM_NORM, "weight", i), {d_inner / n_group, n_group}, 0);
  3384. // out_proj
  3385. layer.ssm_out = create_tensor(tn(LLM_TENSOR_SSM_OUT, "weight", i), {d_inner, n_embd}, 0);
  3386. }
  3387. } break;
  3388. case LLM_ARCH_JAMBA:
  3389. {
  3390. const int64_t d_conv = hparams.ssm_d_conv;
  3391. const int64_t d_inner = hparams.ssm_d_inner;
  3392. const int64_t d_state = hparams.ssm_d_state;
  3393. const int64_t dt_rank = hparams.ssm_dt_rank;
  3394. // only an expansion factor of 2 is supported for now
  3395. GGML_ASSERT(2 * n_embd == d_inner);
  3396. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3397. // output
  3398. {
  3399. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3400. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  3401. // if output is NULL, init from the input tok embed, duplicated to allow offloading
  3402. if (output == NULL) {
  3403. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3404. }
  3405. }
  3406. for (int i = 0; i < n_layer; ++i) {
  3407. const int64_t n_head_kv = hparams.n_head_kv(i);
  3408. const int64_t n_embd_gqa = hparams.n_embd_v_gqa(i);
  3409. auto & layer = layers[i];
  3410. // norm
  3411. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3412. if (n_head_kv == 0) {
  3413. // Mamba layer
  3414. layer.ssm_in = create_tensor(tn(LLM_TENSOR_SSM_IN, "weight", i), {n_embd, 2*d_inner}, 0);
  3415. layer.ssm_conv1d = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "weight", i), {d_conv, d_inner}, 0);
  3416. layer.ssm_conv1d_b = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "bias", i), {d_inner}, 0);
  3417. layer.ssm_x = create_tensor(tn(LLM_TENSOR_SSM_X, "weight", i), {d_inner, dt_rank + 2*d_state}, 0);
  3418. layer.ssm_dt_norm = create_tensor(tn(LLM_TENSOR_SSM_DT_NORM, "weight", i), {dt_rank}, 0);
  3419. layer.ssm_dt = create_tensor(tn(LLM_TENSOR_SSM_DT, "weight", i), {dt_rank, d_inner}, 0);
  3420. layer.ssm_dt_b = create_tensor(tn(LLM_TENSOR_SSM_DT, "bias", i), {d_inner}, 0);
  3421. layer.ssm_b_norm = create_tensor(tn(LLM_TENSOR_SSM_B_NORM, "weight", i), {d_state}, 0);
  3422. layer.ssm_c_norm = create_tensor(tn(LLM_TENSOR_SSM_C_NORM, "weight", i), {d_state}, 0);
  3423. // no "weight" suffix for these
  3424. layer.ssm_a = create_tensor(tn(LLM_TENSOR_SSM_A, i), {d_state, d_inner}, 0);
  3425. layer.ssm_d = create_tensor(tn(LLM_TENSOR_SSM_D, i), {d_inner}, 0);
  3426. // out_proj
  3427. layer.ssm_out = create_tensor(tn(LLM_TENSOR_SSM_OUT, "weight", i), {d_inner, n_embd}, 0);
  3428. } else {
  3429. // Attention layers
  3430. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  3431. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  3432. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  3433. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  3434. }
  3435. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3436. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, TENSOR_NOT_REQUIRED);
  3437. if (layer.ffn_gate_inp) {
  3438. // MoE
  3439. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0);
  3440. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff, n_embd, n_expert}, 0);
  3441. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0);
  3442. } else {
  3443. // FFN (no MoE)
  3444. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  3445. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  3446. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3447. }
  3448. }
  3449. } break;
  3450. case LLM_ARCH_GRANITE_HYBRID:
  3451. {
  3452. // mamba2 Mixer SSM params
  3453. // NOTE: int64_t for tensor dimensions
  3454. const int64_t d_conv = hparams.ssm_d_conv;
  3455. const int64_t d_inner = hparams.ssm_d_inner;
  3456. const int64_t d_state = hparams.ssm_d_state;
  3457. const int64_t n_ssm_head = hparams.ssm_dt_rank;
  3458. const int64_t n_group = hparams.ssm_n_group;
  3459. const int64_t d_in_proj = 2*d_inner + 2*n_group*d_state + n_ssm_head;
  3460. // only an expansion factor of 2 is supported for now
  3461. GGML_ASSERT(2 * n_embd == d_inner);
  3462. // embeddings
  3463. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3464. // output
  3465. {
  3466. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3467. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  3468. // if output is NULL, init from the input tok embed, duplicated to allow offloading
  3469. if (output == NULL) {
  3470. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3471. }
  3472. }
  3473. for (int i = 0; i < n_layer; ++i) {
  3474. auto & layer = layers[i];
  3475. // norm
  3476. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3477. if (hparams.is_recurrent(i)) {
  3478. // ssm layers
  3479. layer.ssm_in = create_tensor(tn(LLM_TENSOR_SSM_IN, "weight", i), {n_embd, d_in_proj}, 0);
  3480. layer.ssm_conv1d = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "weight", i), {d_conv, d_inner + 2*n_group*d_state}, 0);
  3481. layer.ssm_conv1d_b = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "bias", i), {d_inner + 2*n_group*d_state}, TENSOR_NOT_REQUIRED);
  3482. layer.ssm_dt_b = create_tensor(tn(LLM_TENSOR_SSM_DT, "bias", i), {n_ssm_head}, 0);
  3483. // no "weight" suffix for these
  3484. layer.ssm_a = create_tensor(tn(LLM_TENSOR_SSM_A, i), {1, n_ssm_head}, 0);
  3485. layer.ssm_d = create_tensor(tn(LLM_TENSOR_SSM_D, i), {1, n_ssm_head}, 0);
  3486. layer.ssm_norm = create_tensor(tn(LLM_TENSOR_SSM_NORM, "weight", i), {d_inner / n_group, n_group}, 0);
  3487. // out_proj
  3488. layer.ssm_out = create_tensor(tn(LLM_TENSOR_SSM_OUT, "weight", i), {d_inner, n_embd}, 0);
  3489. } else {
  3490. // attention layers (with optional bias)
  3491. const int64_t n_head_i = hparams.n_head(i);
  3492. const int64_t n_embd_k_gqa_i = hparams.n_embd_k_gqa(i);
  3493. const int64_t n_embd_v_gqa_i = hparams.n_embd_v_gqa(i);
  3494. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head_i}, 0);
  3495. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa_i}, 0);
  3496. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa_i}, 0);
  3497. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head_i, n_embd}, 0);
  3498. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  3499. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_k_gqa_i}, TENSOR_NOT_REQUIRED);
  3500. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_v_gqa_i}, TENSOR_NOT_REQUIRED);
  3501. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  3502. }
  3503. // feed forward (w/ optional biases)
  3504. if (n_expert > 0) {
  3505. // MoE FFN
  3506. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3507. layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  3508. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  3509. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff, n_expert}, TENSOR_NOT_REQUIRED);
  3510. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), { n_ff, n_embd, n_expert}, 0);
  3511. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0);
  3512. // For Granite MoE Shared
  3513. if (hparams.n_ff_shexp > 0) {
  3514. layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), {n_embd, hparams.n_ff_shexp}, 0);
  3515. layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), {n_embd, hparams.n_ff_shexp}, 0);
  3516. layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), {hparams.n_ff_shexp, n_embd}, 0);
  3517. }
  3518. } else {
  3519. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3520. layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 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. layer.ffn_gate_b = create_tensor(tn(LLM_TENSOR_FFN_GATE, "bias", i), {n_ff}, TENSOR_NOT_REQUIRED);
  3525. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  3526. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, TENSOR_NOT_REQUIRED);
  3527. }
  3528. }
  3529. } break;
  3530. case LLM_ARCH_XVERSE:
  3531. {
  3532. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3533. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3534. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  3535. for (int i = 0; i < n_layer; ++i) {
  3536. auto & layer = layers[i];
  3537. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3538. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  3539. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  3540. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  3541. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  3542. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3543. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  3544. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3545. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3546. }
  3547. } break;
  3548. case LLM_ARCH_COMMAND_R:
  3549. {
  3550. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3551. // output
  3552. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3553. // init output from the input tok embed
  3554. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3555. for (int i = 0; i < n_layer; ++i) {
  3556. auto & layer = layers[i];
  3557. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3558. if (n_layer >= 64){
  3559. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k, n_head}, 0);
  3560. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k, n_head_kv}, 0);
  3561. }
  3562. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  3563. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  3564. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  3565. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  3566. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  3567. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3568. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3569. }
  3570. } break;
  3571. case LLM_ARCH_COHERE2:
  3572. {
  3573. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, 0);
  3574. // output
  3575. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), { n_embd }, 0);
  3576. // init output from the input tok embed
  3577. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab },
  3578. TENSOR_DUPLICATED);
  3579. for (int i = 0; i < n_layer; ++i) {
  3580. auto & layer = layers[i];
  3581. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), { n_embd }, 0);
  3582. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), { n_embd, n_embd }, 0);
  3583. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), { n_embd, n_embd_gqa }, 0);
  3584. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), { n_embd, n_embd_gqa }, 0);
  3585. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), { n_embd, n_embd }, 0);
  3586. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), { n_embd, n_ff }, 0);
  3587. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd }, 0);
  3588. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), { n_embd, n_ff }, 0);
  3589. }
  3590. }
  3591. break;
  3592. case LLM_ARCH_OLMO: // adapted from LLM_ARCH_LLAMA with norm params removed
  3593. {
  3594. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3595. // output
  3596. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  3597. // if output is NULL, init from the input tok embed
  3598. if (output == NULL) {
  3599. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3600. }
  3601. for (int i = 0; i < n_layer; ++i) {
  3602. auto & layer = layers[i];
  3603. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  3604. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  3605. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  3606. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  3607. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  3608. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3609. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3610. }
  3611. } break;
  3612. case LLM_ARCH_OLMO2:
  3613. {
  3614. const int64_t n_embd_head = n_embd / n_head;
  3615. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3616. // output
  3617. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3618. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  3619. for (int i = 0; i < n_layer; ++i) {
  3620. auto & layer = layers[i];
  3621. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  3622. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  3623. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  3624. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  3625. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd}, 0);
  3626. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_head_kv * n_embd_head}, 0);
  3627. layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), {n_embd}, 0);
  3628. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  3629. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3630. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3631. layer.ffn_post_norm = create_tensor(tn(LLM_TENSOR_FFN_POST_NORM, "weight", i), {n_embd}, 0);
  3632. }
  3633. } break;
  3634. case LLM_ARCH_SEED_OSS:
  3635. {
  3636. const uint32_t head_dim = hparams.n_embd_head_k;
  3637. const int64_t n_qo_dim = n_head * head_dim;
  3638. const int64_t n_kv_dim = n_head_kv * head_dim;
  3639. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3640. // output
  3641. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3642. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  3643. // if output is NULL, init from the input tok embed
  3644. if (output == NULL) {
  3645. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3646. }
  3647. for (int i = 0; i < n_layer; ++i) {
  3648. auto & layer = layers[i];
  3649. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_qo_dim}, 0);
  3650. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_kv_dim}, 0);
  3651. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_kv_dim}, 0);
  3652. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_qo_dim, n_embd}, 0);
  3653. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_qo_dim}, TENSOR_NOT_REQUIRED);
  3654. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_kv_dim}, TENSOR_NOT_REQUIRED);
  3655. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_kv_dim}, TENSOR_NOT_REQUIRED);
  3656. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3657. layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), {n_embd}, 0);
  3658. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  3659. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3660. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3661. }
  3662. } break;
  3663. case LLM_ARCH_OLMOE:
  3664. {
  3665. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3666. // output
  3667. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3668. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  3669. for (int i = 0; i < n_layer; ++i) {
  3670. auto & layer = layers[i];
  3671. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3672. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  3673. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  3674. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  3675. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  3676. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd}, 0);
  3677. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd}, 0);
  3678. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3679. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  3680. if (n_expert == 0) {
  3681. throw std::runtime_error("n_expert must be > 0");
  3682. }
  3683. if (n_expert_used == 0) {
  3684. throw std::runtime_error("n_expert_used must be > 0");
  3685. }
  3686. // MoE branch
  3687. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0);
  3688. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff, n_embd, n_expert}, 0);
  3689. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0);
  3690. }
  3691. } break;
  3692. case LLM_ARCH_OPENELM:
  3693. {
  3694. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3695. // output
  3696. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3697. // init output from the input tok embed
  3698. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3699. for (int i = 0; i < n_layer; ++i) {
  3700. const int64_t n_head = hparams.n_head(i);
  3701. const int64_t n_head_qkv = 2*hparams.n_head_kv(i) + n_head;
  3702. const int64_t n_ff = hparams.n_ff(i);
  3703. auto & layer = layers[i];
  3704. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3705. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_head_qkv*n_embd_head_k}, 0);
  3706. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0);
  3707. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0);
  3708. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_head*n_embd_head_k, n_embd}, 0);
  3709. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3710. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  3711. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  3712. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3713. }
  3714. } break;
  3715. case LLM_ARCH_GPTNEOX:
  3716. {
  3717. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3718. // output
  3719. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3720. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
  3721. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  3722. for (int i = 0; i < n_layer; ++i) {
  3723. auto & layer = layers[i];
  3724. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3725. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0);
  3726. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, 0);
  3727. layer.bqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd + 2*n_embd_gqa}, 0);
  3728. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  3729. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0);
  3730. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3731. layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd}, 0);
  3732. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  3733. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, 0);
  3734. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3735. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, 0);
  3736. }
  3737. } break;
  3738. case LLM_ARCH_ARCTIC:
  3739. {
  3740. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3741. // output
  3742. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3743. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  3744. // if output is NULL, init from the input tok embed
  3745. if (output == NULL) {
  3746. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3747. }
  3748. for (int i = 0; i < n_layer; ++i) {
  3749. auto & layer = layers[i];
  3750. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3751. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  3752. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  3753. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  3754. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  3755. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3756. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_embd}, 0);
  3757. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_embd, n_embd}, 0);
  3758. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_embd}, 0);
  3759. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  3760. layer.ffn_norm_exps = create_tensor(tn(LLM_TENSOR_FFN_NORM_EXPS, "weight", i), {n_embd}, 0);
  3761. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff, n_expert}, false);
  3762. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), { n_ff, n_embd, n_expert}, 0);
  3763. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0);
  3764. }
  3765. } break;
  3766. case LLM_ARCH_DEEPSEEK:
  3767. {
  3768. const int64_t n_ff_exp = hparams.n_ff_exp;
  3769. const int64_t n_expert_shared = hparams.n_expert_shared;
  3770. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3771. // output
  3772. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3773. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  3774. for (int i = 0; i < n_layer; ++i) {
  3775. auto & layer = layers[i];
  3776. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3777. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  3778. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  3779. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  3780. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  3781. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3782. if (i < (int) hparams.n_layer_dense_lead) {
  3783. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  3784. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3785. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3786. } else {
  3787. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  3788. if (n_expert == 0) {
  3789. throw std::runtime_error("n_expert must be > 0");
  3790. }
  3791. if (n_expert_used == 0) {
  3792. throw std::runtime_error("n_expert_used must be > 0");
  3793. }
  3794. // MoE branch
  3795. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);
  3796. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp, n_embd, n_expert}, 0);
  3797. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);
  3798. // Shared expert branch
  3799. layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), {n_embd, n_ff_exp * n_expert_shared}, 0);
  3800. layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), { n_ff_exp * n_expert_shared, n_embd}, 0);
  3801. layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), {n_embd, n_ff_exp * n_expert_shared}, 0);
  3802. }
  3803. }
  3804. } break;
  3805. case LLM_ARCH_DEEPSEEK2:
  3806. {
  3807. const bool is_lite = (hparams.n_layer == 27);
  3808. const bool is_mla = (hparams.n_embd_head_k_mla != 0 && hparams.n_embd_head_v_mla != 0);
  3809. // note: these are the actual head sizes you get when treating as MHA or after "decompression" using wv_b for MLA
  3810. const int64_t n_embd_head_k_mla = is_mla ? hparams.n_embd_head_k_mla : hparams.n_embd_head_k;
  3811. const int64_t n_embd_head_v_mla = is_mla ? hparams.n_embd_head_v_mla : hparams.n_embd_head_v;
  3812. const int64_t n_embd_head_qk_rope = hparams.n_rot;
  3813. const int64_t n_embd_head_qk_nope = n_embd_head_k_mla - n_embd_head_qk_rope;
  3814. const int64_t q_lora_rank = hparams.n_lora_q;
  3815. const int64_t kv_lora_rank = hparams.n_lora_kv;
  3816. const int64_t n_ff_exp = hparams.n_ff_exp;
  3817. const int64_t n_expert_shared = hparams.n_expert_shared;
  3818. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3819. // output
  3820. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3821. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  3822. for (int i = 0; i < n_layer; ++i) {
  3823. auto & layer = layers[i];
  3824. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3825. if (!is_lite) {
  3826. layer.attn_q_a_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_A_NORM, "weight", i), {q_lora_rank}, 0);
  3827. }
  3828. layer.attn_kv_a_norm = create_tensor(tn(LLM_TENSOR_ATTN_KV_A_NORM, "weight", i), {kv_lora_rank}, 0);
  3829. if (!is_lite) {
  3830. layer.wq_a = create_tensor(tn(LLM_TENSOR_ATTN_Q_A, "weight", i), {n_embd, q_lora_rank}, 0);
  3831. layer.wq_b = create_tensor(tn(LLM_TENSOR_ATTN_Q_B, "weight", i), {q_lora_rank, n_head * n_embd_head_k_mla}, 0);
  3832. } else {
  3833. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_head * n_embd_head_k_mla}, 0);
  3834. }
  3835. 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);
  3836. // note: only old legacy GGUF files will have the unsplit wkv_b tensor in
  3837. if (is_mla) {
  3838. layer.wk_b = create_tensor(tn(LLM_TENSOR_ATTN_K_B, "weight", i), {n_embd_head_qk_nope, kv_lora_rank, n_head}, 0);
  3839. layer.wv_b = create_tensor(tn(LLM_TENSOR_ATTN_V_B, "weight", i), {kv_lora_rank, n_embd_head_v_mla, n_head}, 0);
  3840. } else {
  3841. 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);
  3842. }
  3843. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_head * n_embd_head_v_mla, n_embd}, 0);
  3844. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3845. if (i < (int) hparams.n_layer_dense_lead) {
  3846. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  3847. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3848. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3849. } else {
  3850. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  3851. layer.ffn_exp_probs_b = create_tensor(tn(LLM_TENSOR_FFN_EXP_PROBS_B, "bias", i), {n_expert}, TENSOR_NOT_REQUIRED);
  3852. if (n_expert == 0) {
  3853. throw std::runtime_error("n_expert must be > 0");
  3854. }
  3855. if (n_expert_used == 0) {
  3856. throw std::runtime_error("n_expert_used must be > 0");
  3857. }
  3858. // MoE branch
  3859. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);
  3860. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp, n_embd, n_expert}, 0);
  3861. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);
  3862. // Shared expert branch
  3863. layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), {n_embd, n_ff_exp * n_expert_shared}, 0);
  3864. layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), { n_ff_exp * n_expert_shared, n_embd}, 0);
  3865. layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), {n_embd, n_ff_exp * n_expert_shared}, 0);
  3866. }
  3867. }
  3868. } break;
  3869. case LLM_ARCH_PLM:
  3870. {
  3871. const int64_t n_embd_head_qk_rope = hparams.n_rot;
  3872. const int64_t n_embd_head_qk_nope = hparams.n_embd_head_k - hparams.n_rot;
  3873. const int64_t kv_lora_rank = hparams.n_lora_kv;
  3874. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3875. // output
  3876. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3877. // output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  3878. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3879. for (int i = 0; i < n_layer; ++i) {
  3880. auto & layer = layers[i];
  3881. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3882. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  3883. 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);
  3884. layer.attn_kv_a_norm = create_tensor(tn(LLM_TENSOR_ATTN_KV_A_NORM, "weight", i), {kv_lora_rank}, 0);
  3885. 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);
  3886. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), { n_head * ( n_embd_head_v), n_embd}, 0);
  3887. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3888. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3889. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3890. }
  3891. } break;
  3892. case LLM_ARCH_BITNET:
  3893. {
  3894. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3895. // output
  3896. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3897. for (int i = 0; i < n_layer; ++i) {
  3898. auto & layer = layers[i];
  3899. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3900. layer.attn_sub_norm = create_tensor(tn(LLM_TENSOR_ATTN_SUB_NORM, "weight", i), {n_embd}, 0);
  3901. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  3902. layer.wq_scale = create_tensor(tn(LLM_TENSOR_ATTN_Q, "scale", i), {1}, TENSOR_NOT_REQUIRED);
  3903. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  3904. layer.wk_scale = create_tensor(tn(LLM_TENSOR_ATTN_K, "scale", i), {1}, TENSOR_NOT_REQUIRED);
  3905. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  3906. layer.wv_scale = create_tensor(tn(LLM_TENSOR_ATTN_V, "scale", i), {1}, TENSOR_NOT_REQUIRED);
  3907. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  3908. layer.wo_scale = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "scale", i), {1}, TENSOR_NOT_REQUIRED);
  3909. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3910. layer.ffn_sub_norm = create_tensor(tn(LLM_TENSOR_FFN_SUB_NORM, "weight", i), {n_ff}, 0);
  3911. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  3912. layer.ffn_gate_scale = create_tensor(tn(LLM_TENSOR_FFN_GATE, "scale", i), {1}, TENSOR_NOT_REQUIRED);
  3913. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  3914. layer.ffn_down_scale = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "scale", i), {1}, TENSOR_NOT_REQUIRED);
  3915. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3916. layer.ffn_up_scale = create_tensor(tn(LLM_TENSOR_FFN_UP, "scale", i), {1}, TENSOR_NOT_REQUIRED);
  3917. }
  3918. } break;
  3919. case LLM_ARCH_T5:
  3920. {
  3921. const auto n_rel_attn_bkts = hparams.n_rel_attn_bkts;
  3922. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3923. // output
  3924. output_norm_enc = create_tensor(tn(LLM_TENSOR_ENC_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3925. output_norm = create_tensor(tn(LLM_TENSOR_DEC_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3926. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  3927. // if output is NULL, init from the input tok embed
  3928. if (output == NULL) {
  3929. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3930. }
  3931. // n_layer: number of encoder_layers
  3932. // dec_n_layer: number of decoder_layers
  3933. const int dec_n_layer = hparams.dec_n_layer;
  3934. if (dec_n_layer > n_layer) {
  3935. layers.resize(dec_n_layer);
  3936. }
  3937. // load encoder layers
  3938. for (int i = 0; i < n_layer; ++i) {
  3939. auto & layer = layers[i];
  3940. layer.attn_norm_enc = create_tensor(tn(LLM_TENSOR_ENC_ATTN_NORM, "weight", i), {n_embd}, 0);
  3941. 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);
  3942. layer.wq_enc = create_tensor(tn(LLM_TENSOR_ENC_ATTN_Q, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  3943. layer.wk_enc = create_tensor(tn(LLM_TENSOR_ENC_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  3944. layer.wv_enc = create_tensor(tn(LLM_TENSOR_ENC_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  3945. layer.wo_enc = create_tensor(tn(LLM_TENSOR_ENC_ATTN_OUT, "weight", i), {n_embd_v_gqa, n_embd}, 0);
  3946. layer.ffn_norm_enc = create_tensor(tn(LLM_TENSOR_ENC_FFN_NORM, "weight", i), {n_embd}, 0);
  3947. layer.ffn_gate_enc = create_tensor(tn(LLM_TENSOR_ENC_FFN_GATE, "weight", i), {n_embd, n_ff}, TENSOR_NOT_REQUIRED);
  3948. layer.ffn_down_enc = create_tensor(tn(LLM_TENSOR_ENC_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3949. layer.ffn_up_enc = create_tensor(tn(LLM_TENSOR_ENC_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3950. }
  3951. // load decoder layers
  3952. for (int i = 0; i < dec_n_layer; ++i) {
  3953. auto & layer = layers[i];
  3954. layer.attn_norm = create_tensor(tn(LLM_TENSOR_DEC_ATTN_NORM, "weight", i), {n_embd}, 0);
  3955. layer.attn_rel_b = create_tensor(tn(LLM_TENSOR_DEC_ATTN_REL_B, "weight", i), {n_head, n_rel_attn_bkts}, TENSOR_NOT_REQUIRED);
  3956. layer.wq = create_tensor(tn(LLM_TENSOR_DEC_ATTN_Q, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  3957. layer.wk = create_tensor(tn(LLM_TENSOR_DEC_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  3958. layer.wv = create_tensor(tn(LLM_TENSOR_DEC_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  3959. layer.wo = create_tensor(tn(LLM_TENSOR_DEC_ATTN_OUT, "weight", i), {n_embd_v_gqa, n_embd}, 0);
  3960. layer.attn_norm_cross = create_tensor(tn(LLM_TENSOR_DEC_CROSS_ATTN_NORM, "weight", i), {n_embd}, 0);
  3961. // this tensor seems to be unused in HF transformers implementation
  3962. 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);
  3963. layer.wq_cross = create_tensor(tn(LLM_TENSOR_DEC_CROSS_ATTN_Q, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  3964. layer.wk_cross = create_tensor(tn(LLM_TENSOR_DEC_CROSS_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  3965. layer.wv_cross = create_tensor(tn(LLM_TENSOR_DEC_CROSS_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  3966. layer.wo_cross = create_tensor(tn(LLM_TENSOR_DEC_CROSS_ATTN_OUT, "weight", i), {n_embd_v_gqa, n_embd}, 0);
  3967. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_DEC_FFN_NORM, "weight", i), {n_embd}, 0);
  3968. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_DEC_FFN_GATE, "weight", i), {n_embd, n_ff}, TENSOR_NOT_REQUIRED);
  3969. layer.ffn_down = create_tensor(tn(LLM_TENSOR_DEC_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3970. layer.ffn_up = create_tensor(tn(LLM_TENSOR_DEC_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3971. }
  3972. } break;
  3973. case LLM_ARCH_T5ENCODER:
  3974. {
  3975. const auto n_rel_attn_bkts = hparams.n_rel_attn_bkts;
  3976. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3977. // output
  3978. output_norm_enc = create_tensor(tn(LLM_TENSOR_ENC_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3979. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  3980. // if output is NULL, init from the input tok embed
  3981. if (output == NULL) {
  3982. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3983. }
  3984. for (int i = 0; i < n_layer; ++i) {
  3985. auto & layer = layers[i];
  3986. layer.attn_norm_enc = create_tensor(tn(LLM_TENSOR_ENC_ATTN_NORM, "weight", i), {n_embd}, 0);
  3987. 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);
  3988. layer.wq_enc = create_tensor(tn(LLM_TENSOR_ENC_ATTN_Q, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  3989. layer.wk_enc = create_tensor(tn(LLM_TENSOR_ENC_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  3990. layer.wv_enc = create_tensor(tn(LLM_TENSOR_ENC_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  3991. layer.wo_enc = create_tensor(tn(LLM_TENSOR_ENC_ATTN_OUT, "weight", i), {n_embd_v_gqa, n_embd}, 0);
  3992. layer.ffn_norm_enc = create_tensor(tn(LLM_TENSOR_ENC_FFN_NORM, "weight", i), {n_embd}, 0);
  3993. layer.ffn_gate_enc = create_tensor(tn(LLM_TENSOR_ENC_FFN_GATE, "weight", i), {n_embd, n_ff}, TENSOR_NOT_REQUIRED);
  3994. layer.ffn_down_enc = create_tensor(tn(LLM_TENSOR_ENC_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3995. layer.ffn_up_enc = create_tensor(tn(LLM_TENSOR_ENC_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3996. }
  3997. } break;
  3998. case LLM_ARCH_JAIS:
  3999. {
  4000. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  4001. // output
  4002. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  4003. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
  4004. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  4005. for (int i = 0; i < n_layer; ++i) {
  4006. auto & layer = layers[i];
  4007. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  4008. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0);
  4009. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, 0);
  4010. layer.bqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd + 2*n_embd_gqa}, 0);
  4011. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  4012. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0);
  4013. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  4014. layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd}, 0);
  4015. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  4016. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, 0);
  4017. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  4018. layer.ffn_gate_b = create_tensor(tn(LLM_TENSOR_FFN_GATE, "bias", i), {n_ff}, 0);
  4019. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  4020. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, 0);
  4021. }
  4022. } break;
  4023. case LLM_ARCH_CHATGLM:
  4024. {
  4025. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  4026. // output
  4027. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  4028. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  4029. // if output is NULL, init from the input tok embed
  4030. if (output == NULL) {
  4031. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  4032. }
  4033. for (int i = 0; i < n_layer; ++i) {
  4034. auto & layer = layers[i];
  4035. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  4036. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, TENSOR_NOT_REQUIRED);
  4037. layer.bqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd + 2*n_embd_gqa}, TENSOR_NOT_REQUIRED);
  4038. if (layer.wqkv == nullptr) {
  4039. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  4040. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  4041. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  4042. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  4043. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED);
  4044. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED);
  4045. }
  4046. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  4047. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  4048. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff * 2}, 0);
  4049. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  4050. }
  4051. } break;
  4052. case LLM_ARCH_GLM4:
  4053. {
  4054. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  4055. // output
  4056. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  4057. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  4058. // if output is NULL, init from the input tok embed
  4059. if (output == NULL) {
  4060. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  4061. }
  4062. for (int i = 0; i < n_layer; ++i) {
  4063. auto & layer = layers[i];
  4064. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  4065. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, TENSOR_NOT_REQUIRED);
  4066. layer.bqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd + 2*n_embd_gqa}, TENSOR_NOT_REQUIRED);
  4067. if (layer.wqkv == nullptr) {
  4068. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  4069. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  4070. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  4071. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  4072. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED);
  4073. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED);
  4074. }
  4075. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  4076. layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), {n_embd}, 0);
  4077. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  4078. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  4079. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff * 2}, 0);
  4080. layer.ffn_post_norm = create_tensor(tn(LLM_TENSOR_FFN_POST_NORM, "weight", i), {n_embd}, 0);
  4081. }
  4082. } break;
  4083. case LLM_ARCH_GLM4_MOE:
  4084. {
  4085. const int64_t n_expert = hparams.n_expert;
  4086. const int64_t n_expert_used = hparams.n_expert_used;
  4087. const int64_t n_expert_shared = hparams.n_expert_shared;
  4088. GGML_ASSERT(hparams.n_expert > 0 && "n_expert must be > 0 for GLM4_MOE MoE layers");
  4089. GGML_ASSERT(hparams.n_expert_used > 0 && "n_expert_used must be > 0 for GLM4_MOE MoE layers");
  4090. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, 0);
  4091. // output
  4092. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), { n_embd }, 0);
  4093. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), { n_embd, n_vocab }, TENSOR_NOT_REQUIRED);
  4094. // if output is NULL, init from the input tok embed
  4095. if (output == NULL) {
  4096. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, TENSOR_DUPLICATED);
  4097. }
  4098. // Load ALL tensors including NextN layer to satisfy total tensor count
  4099. // but only PROCESS up to last layer (skipping final NextN layer) in forward pass
  4100. for (int i = 0; i < n_layer; ++i) {
  4101. int flags = 0;
  4102. if (hparams.nextn_predict_layers > 0 && static_cast<uint32_t>(i) >= n_layer - hparams.nextn_predict_layers) {
  4103. // skip all tensors in the NextN layers
  4104. flags |= TENSOR_SKIP;
  4105. }
  4106. auto & layer = layers[i];
  4107. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), { n_embd }, flags);
  4108. // GLM-style attention with bias terms
  4109. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), { n_embd, n_embd_head_k * n_head }, flags);
  4110. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), { n_embd, n_embd_k_gqa }, flags);
  4111. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), { n_embd, n_embd_v_gqa }, flags);
  4112. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), { n_embd_head_k * n_head }, flags);
  4113. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), { n_embd_k_gqa }, flags);
  4114. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), { n_embd_v_gqa }, flags);
  4115. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), { n_embd_head_k * n_head, n_embd }, flags);
  4116. // K/Q norm tensors (optional for GLM-4.5 355B variant)
  4117. layer.attn_q_norm = create_tensor(
  4118. tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), { n_embd_head_k }, TENSOR_NOT_REQUIRED | flags);
  4119. layer.attn_k_norm = create_tensor(
  4120. tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), { n_embd_head_k }, TENSOR_NOT_REQUIRED | flags);
  4121. layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), { n_embd }, flags);
  4122. // Check if this layer uses MoE or dense FFN based on n_layer_dense_lead
  4123. // GLM 4.5 uses hybrid architecture: layer 0 is dense, layers 1+ are MoE
  4124. const bool use_moe = (static_cast<uint32_t>(i) >= hparams.n_layer_dense_lead);
  4125. if (use_moe) {
  4126. // MoE layers
  4127. layer.ffn_gate_inp =
  4128. create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), { n_embd, n_expert }, flags);
  4129. layer.ffn_exp_probs_b = create_tensor(tn(LLM_TENSOR_FFN_EXP_PROBS_B, "bias", i), { n_expert }, flags);
  4130. // MoE branch
  4131. const int64_t n_ff_exp = hparams.n_ff_exp ? hparams.n_ff_exp : n_ff / n_expert_used;
  4132. layer.ffn_gate_exps = create_tensor(
  4133. tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert }, flags);
  4134. layer.ffn_down_exps = create_tensor(
  4135. tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), { n_ff_exp, n_embd, n_expert }, flags);
  4136. layer.ffn_up_exps = create_tensor(
  4137. tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert }, flags);
  4138. // Shared expert
  4139. if (n_expert_shared > 0) {
  4140. const int64_t n_ff_shexp = n_ff_exp * n_expert_shared;
  4141. layer.ffn_gate_shexp = create_tensor(
  4142. tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), { n_embd, n_ff_shexp }, flags);
  4143. layer.ffn_down_shexp = create_tensor(
  4144. tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), { n_ff_shexp, n_embd }, flags);
  4145. layer.ffn_up_shexp = create_tensor(
  4146. tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), { n_embd, n_ff_shexp }, flags);
  4147. }
  4148. } else {
  4149. // Dense layers (first k layers) - GLM uses separate gate/up projections
  4150. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), { n_embd, n_ff }, flags);
  4151. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd }, flags);
  4152. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), { n_embd, n_ff }, flags);
  4153. }
  4154. // NextN/MTP tensors (preserved but unused) - conditionally load for last nextn_predict_layers
  4155. if (hparams.nextn_predict_layers > 0 && static_cast<uint32_t>(i) >= n_layer - hparams.nextn_predict_layers) {
  4156. layer.nextn.eh_proj = create_tensor(tn(LLM_TENSOR_NEXTN_EH_PROJ, "weight", i), { 2 * n_embd, n_embd }, flags);
  4157. layer.nextn.enorm = create_tensor(tn(LLM_TENSOR_NEXTN_ENORM, "weight", i), { n_embd }, flags);
  4158. layer.nextn.hnorm = create_tensor(tn(LLM_TENSOR_NEXTN_HNORM, "weight", i), { n_embd }, flags);
  4159. // Optional tensors
  4160. layer.nextn.embed_tokens = create_tensor(tn(LLM_TENSOR_NEXTN_EMBED_TOKENS, "weight", i), { n_embd, n_vocab }, flags | TENSOR_NOT_REQUIRED);
  4161. layer.nextn.shared_head_head = create_tensor(tn(LLM_TENSOR_NEXTN_SHARED_HEAD_HEAD, "weight", i), { n_embd, n_vocab }, flags | TENSOR_NOT_REQUIRED);
  4162. layer.nextn.shared_head_norm = create_tensor(tn(LLM_TENSOR_NEXTN_SHARED_HEAD_NORM, "weight", i), { n_embd }, flags | TENSOR_NOT_REQUIRED);
  4163. }
  4164. }
  4165. }
  4166. break;
  4167. case LLM_ARCH_NEMOTRON:
  4168. {
  4169. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  4170. // output
  4171. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  4172. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
  4173. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  4174. for (int i = 0; i < n_layer; ++i) {
  4175. auto & layer = layers[i];
  4176. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  4177. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0);
  4178. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  4179. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  4180. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  4181. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  4182. // optional bias tensors
  4183. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  4184. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED);
  4185. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED);
  4186. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  4187. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  4188. layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd}, 0);
  4189. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  4190. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  4191. // optional MLP bias
  4192. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  4193. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, TENSOR_NOT_REQUIRED);
  4194. }
  4195. } break;
  4196. case LLM_ARCH_NEMOTRON_H:
  4197. {
  4198. // mamba2 Mixer SSM params
  4199. // NOTE: int64_t for tensor dimensions
  4200. const int64_t d_conv = hparams.ssm_d_conv;
  4201. const int64_t d_inner = hparams.ssm_d_inner;
  4202. const int64_t d_state = hparams.ssm_d_state;
  4203. const int64_t n_ssm_head = hparams.ssm_dt_rank;
  4204. const int64_t n_group = hparams.ssm_n_group;
  4205. const int64_t d_in_proj = 2*d_inner + 2*n_group*d_state + n_ssm_head;
  4206. // embeddings
  4207. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  4208. // output
  4209. {
  4210. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  4211. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  4212. // if output is NULL, init from the input tok embed, duplicated to allow offloading
  4213. if (output == NULL) {
  4214. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  4215. }
  4216. }
  4217. for (int i = 0; i < n_layer; ++i) {
  4218. auto & layer = layers[i];
  4219. // all blocks use the attn norm
  4220. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  4221. if (hparams.is_recurrent(i)) {
  4222. // ssm layers
  4223. layer.ssm_in = create_tensor(tn(LLM_TENSOR_SSM_IN, "weight", i), {n_embd, d_in_proj}, 0);
  4224. layer.ssm_conv1d = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "weight", i), {d_conv, d_inner + 2*n_group*d_state}, 0);
  4225. layer.ssm_conv1d_b = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "bias", i), {d_inner + 2*n_group*d_state}, TENSOR_NOT_REQUIRED);
  4226. layer.ssm_dt_b = create_tensor(tn(LLM_TENSOR_SSM_DT, "bias", i), {n_ssm_head}, 0);
  4227. // no "weight" suffix for these
  4228. layer.ssm_a = create_tensor(tn(LLM_TENSOR_SSM_A, i), {1, n_ssm_head}, 0);
  4229. layer.ssm_d = create_tensor(tn(LLM_TENSOR_SSM_D, i), {1, n_ssm_head}, 0);
  4230. layer.ssm_norm = create_tensor(tn(LLM_TENSOR_SSM_NORM, "weight", i), {d_inner / n_group, n_group}, 0);
  4231. // out_proj
  4232. layer.ssm_out = create_tensor(tn(LLM_TENSOR_SSM_OUT, "weight", i), {d_inner, n_embd}, 0);
  4233. } else if (hparams.n_ff(i) == 0) {
  4234. // attention layers (with optional bias)
  4235. const int64_t n_head_i = hparams.n_head(i);
  4236. const int64_t n_embd_k_gqa_i = hparams.n_embd_k_gqa(i);
  4237. const int64_t n_embd_v_gqa_i = hparams.n_embd_v_gqa(i);
  4238. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head_i}, 0);
  4239. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa_i}, 0);
  4240. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa_i}, 0);
  4241. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head_i, n_embd}, 0);
  4242. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  4243. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_k_gqa_i}, TENSOR_NOT_REQUIRED);
  4244. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_v_gqa_i}, TENSOR_NOT_REQUIRED);
  4245. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  4246. } else {
  4247. // mlp layers
  4248. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { hparams.n_ff(i), n_embd}, 0);
  4249. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, hparams.n_ff(i)}, 0);
  4250. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  4251. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {hparams.n_ff(i)}, TENSOR_NOT_REQUIRED);
  4252. }
  4253. }
  4254. } break;
  4255. case LLM_ARCH_EXAONE:
  4256. {
  4257. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  4258. // output
  4259. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  4260. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  4261. // if output is NULL, init from the input tok embed
  4262. if (output == NULL) {
  4263. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  4264. }
  4265. for (int i = 0; i < n_layer; ++i) {
  4266. auto & layer = layers[i];
  4267. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  4268. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  4269. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  4270. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  4271. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  4272. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  4273. layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  4274. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  4275. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  4276. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  4277. }
  4278. } break;
  4279. case LLM_ARCH_EXAONE4:
  4280. {
  4281. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  4282. // output
  4283. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  4284. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  4285. // if output is NULL, init from the input tok embed
  4286. if (output == NULL) {
  4287. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  4288. }
  4289. for (int i = 0; i < n_layer; ++i) {
  4290. auto & layer = layers[i];
  4291. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  4292. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  4293. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  4294. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  4295. layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  4296. layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), {n_embd}, 0);
  4297. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0);
  4298. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0);
  4299. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  4300. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  4301. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  4302. layer.ffn_post_norm = create_tensor(tn(LLM_TENSOR_FFN_POST_NORM, "weight", i), {n_embd}, 0);
  4303. }
  4304. } break;
  4305. case LLM_ARCH_RWKV6:
  4306. {
  4307. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  4308. // Block 0, LN0
  4309. tok_norm = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "weight"), {n_embd}, 0);
  4310. tok_norm_b = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "bias"), {n_embd}, 0);
  4311. // output
  4312. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  4313. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
  4314. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  4315. const int time_mix_extra_dim = hparams.time_mix_extra_dim;
  4316. const int time_decay_extra_dim = hparams.time_decay_extra_dim;
  4317. const int head_size = hparams.wkv_head_size;
  4318. const int attn_hidden_size = n_embd;
  4319. const int ffn_size = hparams.n_ff_arr[0];
  4320. for (int i = 0; i < n_layer; ++i) {
  4321. auto & layer = layers[i];
  4322. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  4323. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0);
  4324. layer.attn_norm_2 = create_tensor(tn(LLM_TENSOR_ATTN_NORM_2, "weight", i), {n_embd}, 0);
  4325. layer.attn_norm_2_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM_2, "bias", i), {n_embd}, 0);
  4326. layer.time_mix_w1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_W1, "weight", i), {n_embd, time_mix_extra_dim * 5}, 0);
  4327. layer.time_mix_w2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_W2, "weight", i), {time_mix_extra_dim, n_embd, 5}, 0);
  4328. layer.time_mix_lerp_x = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_X, "weight", i), {n_embd, 1, 1}, 0);
  4329. layer.time_mix_lerp_w = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_W, "weight", i), {n_embd, 1, 1}, TENSOR_NOT_REQUIRED);
  4330. layer.time_mix_lerp_k = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_K, "weight", i), {n_embd, 1, 1}, TENSOR_NOT_REQUIRED);
  4331. layer.time_mix_lerp_v = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_V, "weight", i), {n_embd, 1, 1}, TENSOR_NOT_REQUIRED);
  4332. layer.time_mix_lerp_r = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_R, "weight", i), {n_embd, 1, 1}, TENSOR_NOT_REQUIRED);
  4333. layer.time_mix_lerp_g = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_G, "weight", i), {n_embd, 1, 1}, TENSOR_NOT_REQUIRED);
  4334. layer.time_mix_lerp_fused = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_FUSED, "weight", i), {n_embd, 1, 1, 5}, TENSOR_NOT_REQUIRED);
  4335. GGML_ASSERT(!(layer.time_mix_lerp_fused == NULL && layer.time_mix_lerp_w == NULL));
  4336. layer.time_mix_first = create_tensor(tn(LLM_TENSOR_TIME_MIX_FIRST, "weight", i), {head_size, n_embd / head_size}, 0);
  4337. layer.time_mix_decay = create_tensor(tn(LLM_TENSOR_TIME_MIX_DECAY, "weight", i), {n_embd}, 0);
  4338. layer.time_mix_decay_w1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_DECAY_W1, "weight", i), {n_embd, time_decay_extra_dim}, 0);
  4339. layer.time_mix_decay_w2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_DECAY_W2, "weight", i), {time_decay_extra_dim, attn_hidden_size}, 0);
  4340. layer.time_mix_key = create_tensor(tn(LLM_TENSOR_TIME_MIX_KEY, "weight", i), {attn_hidden_size, n_embd}, 0);
  4341. layer.time_mix_value = create_tensor(tn(LLM_TENSOR_TIME_MIX_VALUE, "weight", i), {attn_hidden_size, n_embd}, 0);
  4342. layer.time_mix_receptance = create_tensor(tn(LLM_TENSOR_TIME_MIX_RECEPTANCE, "weight", i), {attn_hidden_size, n_embd}, 0);
  4343. layer.time_mix_gate = create_tensor(tn(LLM_TENSOR_TIME_MIX_GATE, "weight", i), {attn_hidden_size, n_embd}, 0);
  4344. layer.time_mix_ln = create_tensor(tn(LLM_TENSOR_TIME_MIX_LN, "weight", i), {n_embd}, 0);
  4345. layer.time_mix_ln_b = create_tensor(tn(LLM_TENSOR_TIME_MIX_LN, "bias", i), {n_embd}, 0);
  4346. layer.time_mix_output = create_tensor(tn(LLM_TENSOR_TIME_MIX_OUTPUT, "weight", i), {n_embd, attn_hidden_size}, 0);
  4347. layer.channel_mix_lerp_k = create_tensor(tn(LLM_TENSOR_CHANNEL_MIX_LERP_K, "weight", i), {n_embd, 1, 1}, 0);
  4348. layer.channel_mix_lerp_r = create_tensor(tn(LLM_TENSOR_CHANNEL_MIX_LERP_R, "weight", i), {n_embd, 1, 1}, 0);
  4349. layer.channel_mix_key = create_tensor(tn(LLM_TENSOR_CHANNEL_MIX_KEY, "weight", i), {n_embd, ffn_size}, 0);
  4350. layer.channel_mix_value = create_tensor(tn(LLM_TENSOR_CHANNEL_MIX_VALUE, "weight", i), {ffn_size, n_embd}, 0);
  4351. layer.channel_mix_receptance = create_tensor(tn(LLM_TENSOR_CHANNEL_MIX_RECEPTANCE, "weight", i), {n_embd, n_embd}, 0);
  4352. }
  4353. } break;
  4354. case LLM_ARCH_RWKV6QWEN2:
  4355. {
  4356. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  4357. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  4358. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, TENSOR_NOT_REQUIRED);
  4359. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  4360. const int time_mix_extra_dim = hparams.time_mix_extra_dim;
  4361. const int time_decay_extra_dim = hparams.time_decay_extra_dim;
  4362. const int head_size = hparams.wkv_head_size;
  4363. const int attn_hidden_size = n_embd;
  4364. const int n_head_kv = hparams.n_head_kv();
  4365. int attn_key_value_size;
  4366. if (n_head_kv == 0 || attn_hidden_size / head_size == n_head_kv) {
  4367. attn_key_value_size = attn_hidden_size;
  4368. } else {
  4369. attn_key_value_size = n_head_kv * head_size;
  4370. }
  4371. for (int i = 0; i < n_layer; ++i) {
  4372. auto & layer = layers[i];
  4373. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  4374. layer.time_mix_w1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_W1, "weight", i), {n_embd, time_mix_extra_dim * 5}, 0);
  4375. layer.time_mix_w2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_W2, "weight", i), {time_mix_extra_dim, n_embd, 5}, 0);
  4376. layer.time_mix_lerp_x = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_X, "weight", i), {n_embd, 1, 1}, 0);
  4377. layer.time_mix_lerp_fused = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_FUSED, "weight", i), {n_embd, 1, 1, 5}, 0);
  4378. layer.time_mix_first = create_tensor(tn(LLM_TENSOR_TIME_MIX_FIRST, "weight", i), {head_size, n_embd / head_size}, TENSOR_NOT_REQUIRED);
  4379. layer.time_mix_decay = create_tensor(tn(LLM_TENSOR_TIME_MIX_DECAY, "weight", i), {n_embd}, 0);
  4380. layer.time_mix_decay_w1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_DECAY_W1, "weight", i), {n_embd, time_decay_extra_dim}, 0);
  4381. layer.time_mix_decay_w2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_DECAY_W2, "weight", i), {time_decay_extra_dim, attn_hidden_size}, 0);
  4382. layer.time_mix_key = create_tensor(tn(LLM_TENSOR_TIME_MIX_KEY, "weight", i), {n_embd, attn_key_value_size}, 0);
  4383. layer.time_mix_value = create_tensor(tn(LLM_TENSOR_TIME_MIX_VALUE, "weight", i), {n_embd, attn_key_value_size}, 0);
  4384. layer.time_mix_receptance = create_tensor(tn(LLM_TENSOR_TIME_MIX_RECEPTANCE, "weight", i), {attn_hidden_size, n_embd}, 0);
  4385. layer.time_mix_gate = create_tensor(tn(LLM_TENSOR_TIME_MIX_GATE, "weight", i), {attn_hidden_size, n_embd}, 0);
  4386. // optional bias tensors
  4387. layer.time_mix_key_b = create_tensor(tn(LLM_TENSOR_TIME_MIX_KEY, "bias", i), {attn_key_value_size}, TENSOR_NOT_REQUIRED);
  4388. layer.time_mix_value_b = create_tensor(tn(LLM_TENSOR_TIME_MIX_VALUE, "bias", i), {attn_key_value_size}, TENSOR_NOT_REQUIRED);
  4389. layer.time_mix_receptance_b = create_tensor(tn(LLM_TENSOR_TIME_MIX_RECEPTANCE, "bias", i), {attn_hidden_size}, TENSOR_NOT_REQUIRED);
  4390. layer.time_mix_output = create_tensor(tn(LLM_TENSOR_TIME_MIX_OUTPUT, "weight", i), {n_embd, attn_hidden_size}, 0);
  4391. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  4392. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  4393. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  4394. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  4395. }
  4396. } break;
  4397. case LLM_ARCH_RWKV7:
  4398. {
  4399. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  4400. // Block 0, LN0
  4401. tok_norm = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "weight"), {n_embd}, 0);
  4402. tok_norm_b = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "bias"), {n_embd}, 0);
  4403. // output
  4404. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  4405. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
  4406. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  4407. const int n_lora_decay = hparams.n_lora_decay;
  4408. const int n_lora_iclr = hparams.n_lora_iclr;
  4409. const int n_lora_value_res_mix = hparams.n_lora_value_res_mix;
  4410. const int n_lora_gate = hparams.n_lora_gate;
  4411. const int attn_hidden_size = n_embd;
  4412. const int ffn_size = hparams.n_ff_arr[0];
  4413. for (int i = 0; i < n_layer; ++i) {
  4414. auto & layer = layers[i];
  4415. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  4416. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0);
  4417. layer.attn_norm_2 = create_tensor(tn(LLM_TENSOR_ATTN_NORM_2, "weight", i), {n_embd}, 0);
  4418. layer.attn_norm_2_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM_2, "bias", i), {n_embd}, 0);
  4419. layer.time_mix_w0 = create_tensor(tn(LLM_TENSOR_TIME_MIX_W0, "weight", i), {n_embd}, 0);
  4420. layer.time_mix_w1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_W1, "weight", i), {n_embd, n_lora_decay}, 0);
  4421. layer.time_mix_w2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_W2, "weight", i), {n_lora_decay, n_embd}, 0);
  4422. layer.time_mix_a0 = create_tensor(tn(LLM_TENSOR_TIME_MIX_A0, "weight", i), {n_embd}, 0);
  4423. layer.time_mix_a1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_A1, "weight", i), {n_embd, n_lora_iclr}, 0);
  4424. layer.time_mix_a2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_A2, "weight", i), {n_lora_iclr, n_embd}, 0);
  4425. if (i == 0) {
  4426. // actually not used
  4427. layer.time_mix_v0 = create_tensor(tn(LLM_TENSOR_TIME_MIX_V0, "weight", i), {n_embd}, 0);
  4428. layer.time_mix_v1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_V1, "weight", i), {n_embd, n_lora_iclr}, 0);
  4429. layer.time_mix_v2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_V2, "weight", i), {n_lora_iclr, n_embd}, 0);
  4430. } else {
  4431. layer.time_mix_v0 = create_tensor(tn(LLM_TENSOR_TIME_MIX_V0, "weight", i), {n_embd}, 0);
  4432. layer.time_mix_v1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_V1, "weight", i), {n_embd, n_lora_value_res_mix}, 0);
  4433. layer.time_mix_v2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_V2, "weight", i), {n_lora_value_res_mix, n_embd}, 0);
  4434. }
  4435. layer.time_mix_g1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_G1, "weight", i), {n_embd, n_lora_gate}, 0);
  4436. layer.time_mix_g2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_G2, "weight", i), {n_lora_gate, n_embd}, 0);
  4437. layer.time_mix_lerp_fused = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_FUSED, "weight", i), {n_embd, 1, 1, 6}, 0);
  4438. layer.time_mix_k_k = create_tensor(tn(LLM_TENSOR_TIME_MIX_K_K, "weight", i), {attn_hidden_size}, 0);
  4439. layer.time_mix_k_a = create_tensor(tn(LLM_TENSOR_TIME_MIX_K_A, "weight", i), {attn_hidden_size}, 0);
  4440. layer.time_mix_r_k = create_tensor(tn(LLM_TENSOR_TIME_MIX_R_K, "weight", i), {attn_hidden_size}, 0);
  4441. layer.time_mix_key = create_tensor(tn(LLM_TENSOR_TIME_MIX_KEY, "weight", i), {attn_hidden_size, n_embd}, 0);
  4442. layer.time_mix_value = create_tensor(tn(LLM_TENSOR_TIME_MIX_VALUE, "weight", i), {attn_hidden_size, n_embd}, 0);
  4443. layer.time_mix_receptance = create_tensor(tn(LLM_TENSOR_TIME_MIX_RECEPTANCE, "weight", i), {attn_hidden_size, n_embd}, 0);
  4444. layer.time_mix_ln = create_tensor(tn(LLM_TENSOR_TIME_MIX_LN, "weight", i), {n_embd}, 0);
  4445. layer.time_mix_ln_b = create_tensor(tn(LLM_TENSOR_TIME_MIX_LN, "bias", i), {n_embd}, 0);
  4446. layer.time_mix_output = create_tensor(tn(LLM_TENSOR_TIME_MIX_OUTPUT, "weight", i), {n_embd, attn_hidden_size}, 0);
  4447. layer.channel_mix_lerp_k = create_tensor(tn(LLM_TENSOR_CHANNEL_MIX_LERP_K, "weight", i), {n_embd, 1, 1}, 0);
  4448. layer.channel_mix_key = create_tensor(tn(LLM_TENSOR_CHANNEL_MIX_KEY, "weight", i), {n_embd, ffn_size}, 0);
  4449. layer.channel_mix_value = create_tensor(tn(LLM_TENSOR_CHANNEL_MIX_VALUE, "weight", i), {ffn_size, n_embd}, 0);
  4450. }
  4451. } break;
  4452. case LLM_ARCH_ARWKV7:
  4453. {
  4454. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  4455. // output
  4456. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  4457. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  4458. const int n_lora_decay = hparams.n_lora_decay;
  4459. const int n_lora_iclr = hparams.n_lora_iclr;
  4460. const int n_lora_value_res_mix = hparams.n_lora_value_res_mix;
  4461. const int n_lora_gate = hparams.n_lora_gate;
  4462. const int attn_hidden_size = n_embd;
  4463. for (int i = 0; i < n_layer; ++i) {
  4464. auto & layer = layers[i];
  4465. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  4466. layer.time_mix_w0 = create_tensor(tn(LLM_TENSOR_TIME_MIX_W0, "weight", i), {n_embd}, 0);
  4467. layer.time_mix_w1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_W1, "weight", i), {n_embd, n_lora_decay}, 0);
  4468. layer.time_mix_w2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_W2, "weight", i), {n_lora_decay, n_embd}, 0);
  4469. layer.time_mix_a0 = create_tensor(tn(LLM_TENSOR_TIME_MIX_A0, "weight", i), {n_embd}, 0);
  4470. layer.time_mix_a1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_A1, "weight", i), {n_embd, n_lora_iclr}, 0);
  4471. layer.time_mix_a2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_A2, "weight", i), {n_lora_iclr, n_embd}, 0);
  4472. if (i == 0) {
  4473. // actually not used
  4474. layer.time_mix_v0 = create_tensor(tn(LLM_TENSOR_TIME_MIX_V0, "weight", i), {n_embd}, 0);
  4475. layer.time_mix_v1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_V1, "weight", i), {n_embd, n_lora_iclr}, 0);
  4476. layer.time_mix_v2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_V2, "weight", i), {n_lora_iclr, n_embd}, 0);
  4477. } else {
  4478. layer.time_mix_v0 = create_tensor(tn(LLM_TENSOR_TIME_MIX_V0, "weight", i), {n_embd}, 0);
  4479. layer.time_mix_v1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_V1, "weight", i), {n_embd, n_lora_value_res_mix}, 0);
  4480. layer.time_mix_v2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_V2, "weight", i), {n_lora_value_res_mix, n_embd}, 0);
  4481. }
  4482. layer.time_mix_g1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_G1, "weight", i), {n_embd, n_lora_gate}, TENSOR_NOT_REQUIRED);
  4483. layer.time_mix_g2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_G2, "weight", i), {n_lora_gate, n_embd}, TENSOR_NOT_REQUIRED);
  4484. try {
  4485. layer.time_mix_lerp_fused = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_FUSED, "weight", i), {n_embd, 1, 1, 6}, 0);
  4486. } catch(std::runtime_error & e) {
  4487. // ARWKV models may not have gate tensors
  4488. layer.time_mix_lerp_fused = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_FUSED, "weight", i), {n_embd, 1, 1, 5}, 0);
  4489. }
  4490. layer.time_mix_k_k = create_tensor(tn(LLM_TENSOR_TIME_MIX_K_K, "weight", i), {attn_hidden_size}, 0);
  4491. layer.time_mix_k_a = create_tensor(tn(LLM_TENSOR_TIME_MIX_K_A, "weight", i), {attn_hidden_size}, 0);
  4492. layer.time_mix_r_k = create_tensor(tn(LLM_TENSOR_TIME_MIX_R_K, "weight", i), {attn_hidden_size}, 0);
  4493. layer.time_mix_key = create_tensor(tn(LLM_TENSOR_TIME_MIX_KEY, "weight", i), {attn_hidden_size, n_embd}, 0);
  4494. layer.time_mix_value = create_tensor(tn(LLM_TENSOR_TIME_MIX_VALUE, "weight", i), {attn_hidden_size, n_embd}, 0);
  4495. layer.time_mix_receptance = create_tensor(tn(LLM_TENSOR_TIME_MIX_RECEPTANCE, "weight", i), {attn_hidden_size, n_embd}, 0);
  4496. layer.time_mix_ln = create_tensor(tn(LLM_TENSOR_TIME_MIX_LN, "weight", i), {n_embd}, TENSOR_NOT_REQUIRED);
  4497. layer.time_mix_ln_b = create_tensor(tn(LLM_TENSOR_TIME_MIX_LN, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  4498. layer.time_mix_output = create_tensor(tn(LLM_TENSOR_TIME_MIX_OUTPUT, "weight", i), {n_embd, attn_hidden_size}, 0);
  4499. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  4500. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  4501. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  4502. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  4503. }
  4504. } break;
  4505. case LLM_ARCH_CHAMELEON:
  4506. {
  4507. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  4508. // output
  4509. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  4510. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  4511. // if output is NULL, init from the input tok embed
  4512. if (output == NULL) {
  4513. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  4514. }
  4515. for (int i = 0; i < n_layer; ++i) {
  4516. auto & layer = layers[i];
  4517. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  4518. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k, n_head}, 0);
  4519. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k, n_head_kv}, 0);
  4520. layer.attn_q_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "bias", i), {n_embd_head_k, n_head}, TENSOR_NOT_REQUIRED);
  4521. 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);
  4522. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  4523. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  4524. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  4525. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  4526. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  4527. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  4528. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  4529. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  4530. }
  4531. } break;
  4532. case LLM_ARCH_WAVTOKENIZER_DEC:
  4533. {
  4534. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {hparams.n_embd_features, n_vocab}, 0);
  4535. conv1d = create_tensor(tn(LLM_TENSOR_CONV1D, "weight"), {7, hparams.n_embd_features, hparams.posnet.n_embd}, 0);
  4536. conv1d_b = create_tensor(tn(LLM_TENSOR_CONV1D, "bias"), {1, hparams.posnet.n_embd}, 0);
  4537. // posnet
  4538. {
  4539. const int64_t n_embd = hparams.posnet.n_embd;
  4540. for (uint32_t i = 0; i < hparams.posnet.n_layer; ++i) {
  4541. auto & layer = layers[i].posnet;
  4542. // posnet:
  4543. //
  4544. // - resnet
  4545. // - resnet
  4546. // - attn
  4547. // - resnet
  4548. // - resnet
  4549. // - norm
  4550. //
  4551. switch (i) {
  4552. case 0:
  4553. case 1:
  4554. case 3:
  4555. case 4:
  4556. {
  4557. layer.norm1 = create_tensor(tn(LLM_TENSOR_POS_NET_NORM1, "weight", i), {1, n_embd}, 0);
  4558. layer.norm1_b = create_tensor(tn(LLM_TENSOR_POS_NET_NORM1, "bias", i), {1, n_embd}, 0);
  4559. layer.conv1 = create_tensor(tn(LLM_TENSOR_POS_NET_CONV1, "weight", i), {3, n_embd, n_embd}, 0);
  4560. layer.conv1_b = create_tensor(tn(LLM_TENSOR_POS_NET_CONV1, "bias", i), {1, n_embd}, 0);
  4561. layer.norm2 = create_tensor(tn(LLM_TENSOR_POS_NET_NORM2, "weight", i), {1, n_embd}, 0);
  4562. layer.norm2_b = create_tensor(tn(LLM_TENSOR_POS_NET_NORM2, "bias", i), {1, n_embd}, 0);
  4563. layer.conv2 = create_tensor(tn(LLM_TENSOR_POS_NET_CONV2, "weight", i), {3, n_embd, n_embd}, 0);
  4564. layer.conv2_b = create_tensor(tn(LLM_TENSOR_POS_NET_CONV2, "bias", i), {1, n_embd}, 0);
  4565. } break;
  4566. case 2:
  4567. {
  4568. layer.attn_norm = create_tensor(tn(LLM_TENSOR_POS_NET_ATTN_NORM, "weight", i), {1, n_embd}, 0);
  4569. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_POS_NET_ATTN_NORM, "bias", i), {1, n_embd}, 0);
  4570. layer.attn_q = create_tensor(tn(LLM_TENSOR_POS_NET_ATTN_Q, "weight", i), {1, n_embd, n_embd}, 0);
  4571. layer.attn_q_b = create_tensor(tn(LLM_TENSOR_POS_NET_ATTN_Q, "bias", i), {1, n_embd}, 0);
  4572. layer.attn_k = create_tensor(tn(LLM_TENSOR_POS_NET_ATTN_K, "weight", i), {1, n_embd, n_embd}, 0);
  4573. layer.attn_k_b = create_tensor(tn(LLM_TENSOR_POS_NET_ATTN_K, "bias", i), {1, n_embd}, 0);
  4574. layer.attn_v = create_tensor(tn(LLM_TENSOR_POS_NET_ATTN_V, "weight", i), {1, n_embd, n_embd}, 0);
  4575. layer.attn_v_b = create_tensor(tn(LLM_TENSOR_POS_NET_ATTN_V, "bias", i), {1, n_embd}, 0);
  4576. layer.attn_o = create_tensor(tn(LLM_TENSOR_POS_NET_ATTN_OUT, "weight", i), {1, n_embd, n_embd}, 0);
  4577. layer.attn_o_b = create_tensor(tn(LLM_TENSOR_POS_NET_ATTN_OUT, "bias", i), {1, n_embd}, 0);
  4578. } break;
  4579. case 5:
  4580. {
  4581. layer.norm = create_tensor(tn(LLM_TENSOR_POS_NET_ATTN_NORM, "weight", i), {1, n_embd}, 0);
  4582. layer.norm_b = create_tensor(tn(LLM_TENSOR_POS_NET_ATTN_NORM, "bias", i), {1, n_embd}, 0);
  4583. } break;
  4584. default: GGML_ABORT("unknown posnet layer");
  4585. };
  4586. }
  4587. }
  4588. GGML_ASSERT(hparams.posnet.n_embd == hparams.convnext.n_embd);
  4589. tok_norm = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "weight"), {hparams.posnet.n_embd}, 0);
  4590. tok_norm_b = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "bias"), {hparams.posnet.n_embd}, 0);
  4591. // convnext
  4592. {
  4593. const int64_t n_embd = hparams.convnext.n_embd;
  4594. for (uint32_t i = 0; i < hparams.convnext.n_layer; ++i) {
  4595. auto & layer = layers[i].convnext;
  4596. layer.dw = create_tensor(tn(LLM_TENSOR_CONVNEXT_DW, "weight", i), {7, 1, n_embd}, 0);
  4597. layer.dw_b = create_tensor(tn(LLM_TENSOR_CONVNEXT_DW, "bias", i), {1, n_embd}, 0);
  4598. layer.norm = create_tensor(tn(LLM_TENSOR_CONVNEXT_NORM, "weight", i), {n_embd}, 0);
  4599. layer.norm_b = create_tensor(tn(LLM_TENSOR_CONVNEXT_NORM, "bias", i), {n_embd}, 0);
  4600. layer.pw1 = create_tensor(tn(LLM_TENSOR_CONVNEXT_PW1, "weight", i), {n_embd, n_ff}, 0);
  4601. layer.pw1_b = create_tensor(tn(LLM_TENSOR_CONVNEXT_PW1, "bias", i), {n_ff}, 0);
  4602. layer.pw2 = create_tensor(tn(LLM_TENSOR_CONVNEXT_PW2, "weight", i), {n_ff, n_embd}, 0);
  4603. layer.pw2_b = create_tensor(tn(LLM_TENSOR_CONVNEXT_PW2, "bias", i), {n_embd}, 0);
  4604. layer.gamma = create_tensor(tn(LLM_TENSOR_CONVNEXT_GAMMA, "weight", i), {n_embd}, 0);
  4605. }
  4606. // output
  4607. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  4608. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
  4609. }
  4610. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {hparams.convnext.n_embd, n_embd}, 0);
  4611. output_b = create_tensor(tn(LLM_TENSOR_OUTPUT, "bias"), {n_embd}, 0);
  4612. } break;
  4613. case LLM_ARCH_BAILINGMOE:
  4614. {
  4615. const int64_t n_ff_exp = hparams.n_ff_exp;
  4616. const int64_t n_expert_shared = hparams.n_expert_shared;
  4617. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  4618. // output
  4619. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  4620. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  4621. for (int i = 0; i < n_layer; ++i) {
  4622. auto & layer = layers[i];
  4623. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  4624. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_head * n_rot}, 0);
  4625. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_head_kv * n_rot}, 0);
  4626. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_head_kv * n_rot}, 0);
  4627. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_head * n_rot, n_embd}, 0);
  4628. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  4629. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  4630. if (n_expert == 0) {
  4631. throw std::runtime_error("n_expert must be > 0");
  4632. }
  4633. if (n_expert_used == 0) {
  4634. throw std::runtime_error("n_expert_used must be > 0");
  4635. }
  4636. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);
  4637. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp, n_embd, n_expert}, 0);
  4638. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);
  4639. layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), {n_embd, n_ff_exp * n_expert_shared}, 0);
  4640. layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), { n_ff_exp * n_expert_shared, n_embd}, 0);
  4641. layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), {n_embd, n_ff_exp * n_expert_shared}, 0);
  4642. }
  4643. } break;
  4644. case LLM_ARCH_BAILINGMOE2:
  4645. {
  4646. const int64_t n_ff_exp = hparams.n_ff_exp;
  4647. const int64_t n_expert_shared = hparams.n_expert_shared;
  4648. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  4649. // output
  4650. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  4651. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  4652. GGML_ASSERT(n_expert > 0 && "n_expert must be > 0 for bailingmoe2");
  4653. GGML_ASSERT(n_expert_used > 0 && "n_expert_used must be > 0 for bailingmoe2");
  4654. for (int i = 0; i < n_layer; ++i) {
  4655. int flags = 0;
  4656. if (hparams.nextn_predict_layers > 0 && static_cast<uint32_t>(i) >= n_layer - hparams.nextn_predict_layers) {
  4657. // skip all tensors in the NextN layers
  4658. flags |= TENSOR_SKIP;
  4659. }
  4660. auto & layer = layers[i];
  4661. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, flags);
  4662. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, flags);
  4663. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, flags);
  4664. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, flags);
  4665. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, flags);
  4666. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, flags);
  4667. if (static_cast<uint32_t>(i) >= hparams.n_layer_dense_lead) { // MoE layers
  4668. const int64_t n_ff_shexp = (hparams.n_ff_shexp ? hparams.n_ff_shexp : n_ff_exp) * n_expert_shared;
  4669. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, flags);
  4670. layer.ffn_exp_probs_b = create_tensor(tn(LLM_TENSOR_FFN_EXP_PROBS_B, "bias", i), {n_expert}, TENSOR_NOT_REQUIRED | flags);
  4671. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, flags);
  4672. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp, n_embd, n_expert}, flags);
  4673. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, flags);
  4674. layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), {n_embd, n_ff_shexp}, flags);
  4675. layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), {n_ff_shexp, n_embd}, flags);
  4676. layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), {n_embd, n_ff_shexp}, flags);
  4677. } else { // Dense layers
  4678. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, flags);
  4679. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, flags);
  4680. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, flags);
  4681. }
  4682. // NextN/MTP tensors (preserved but unused) - conditionally load for last nextn_predict_layers
  4683. if (hparams.nextn_predict_layers > 0 && static_cast<uint32_t>(i) >= n_layer - hparams.nextn_predict_layers) {
  4684. layer.nextn.eh_proj = create_tensor(tn(LLM_TENSOR_NEXTN_EH_PROJ, "weight", i), { 2 * n_embd, n_embd }, flags);
  4685. layer.nextn.embed_tokens = create_tensor(tn(LLM_TENSOR_NEXTN_EMBED_TOKENS, "weight", i), { n_embd, n_vocab }, TENSOR_NOT_REQUIRED | flags);
  4686. layer.nextn.enorm = create_tensor(tn(LLM_TENSOR_NEXTN_ENORM, "weight", i), { n_embd }, flags);
  4687. layer.nextn.hnorm = create_tensor(tn(LLM_TENSOR_NEXTN_HNORM, "weight", i), { n_embd }, flags);
  4688. layer.nextn.shared_head_head = create_tensor(tn(LLM_TENSOR_NEXTN_SHARED_HEAD_HEAD, "weight", i), { n_embd, n_vocab }, TENSOR_NOT_REQUIRED | flags);
  4689. layer.nextn.shared_head_norm = create_tensor(tn(LLM_TENSOR_NEXTN_SHARED_HEAD_NORM, "weight", i), { n_embd }, TENSOR_NOT_REQUIRED | flags);
  4690. layer.layer_out_norm = create_tensor(tn(LLM_TENSOR_LAYER_OUT_NORM, "weight", i), {n_embd}, flags);
  4691. }
  4692. }
  4693. } break;
  4694. case LLM_ARCH_DOTS1:
  4695. {
  4696. const int64_t n_ff_exp = hparams.n_ff_exp;
  4697. const int64_t n_expert_shared = hparams.n_expert_shared;
  4698. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  4699. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  4700. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  4701. for (int i = 0; i < n_layer; ++i) {
  4702. auto & layer = layers[i];
  4703. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  4704. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  4705. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  4706. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  4707. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  4708. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0);
  4709. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0);
  4710. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  4711. if (i < (int) hparams.n_layer_dense_lead) {
  4712. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  4713. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  4714. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  4715. } else {
  4716. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  4717. layer.ffn_exp_probs_b = create_tensor(tn(LLM_TENSOR_FFN_EXP_PROBS_B, "bias", i), {n_expert}, TENSOR_NOT_REQUIRED);
  4718. if (n_expert == 0) {
  4719. throw std::runtime_error("n_expert must be > 0");
  4720. }
  4721. if (n_expert_used == 0) {
  4722. throw std::runtime_error("n_expert_used must be > 0");
  4723. }
  4724. // MoE branch
  4725. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);
  4726. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp, n_embd, n_expert}, 0);
  4727. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);
  4728. // Shared expert branch
  4729. layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), {n_embd, n_ff_exp * n_expert_shared}, 0);
  4730. layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), { n_ff_exp * n_expert_shared, n_embd}, 0);
  4731. layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), {n_embd, n_ff_exp * n_expert_shared}, 0);
  4732. }
  4733. }
  4734. } break;
  4735. case LLM_ARCH_ARCEE:
  4736. {
  4737. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  4738. // output
  4739. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  4740. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  4741. // if output is NULL, init from the input tok embed
  4742. if (output == NULL) {
  4743. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  4744. }
  4745. for (int i = 0; i < n_layer; ++i) {
  4746. auto & layer = layers[i];
  4747. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  4748. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  4749. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  4750. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  4751. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  4752. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  4753. layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  4754. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  4755. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  4756. }
  4757. } break;
  4758. case LLM_ARCH_ERNIE4_5:
  4759. case LLM_ARCH_ERNIE4_5_MOE:
  4760. {
  4761. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  4762. // output
  4763. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  4764. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  4765. // if output is NULL, init from the input tok embed
  4766. if (output == NULL) {
  4767. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  4768. }
  4769. for (int i = 0; i < n_layer; ++i) {
  4770. auto & layer = layers[i];
  4771. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  4772. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  4773. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  4774. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  4775. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  4776. // optional bias tensors
  4777. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  4778. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED);
  4779. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED);
  4780. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  4781. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  4782. if (arch == LLM_ARCH_ERNIE4_5_MOE && static_cast<uint32_t>(i) >= hparams.n_layer_dense_lead) { // MoE layers
  4783. int n_ff_exp = hparams.n_ff_exp;
  4784. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  4785. layer.ffn_exp_probs_b = create_tensor(tn(LLM_TENSOR_FFN_EXP_PROBS_B, "bias", i), {n_expert}, TENSOR_NOT_REQUIRED);
  4786. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff_exp, n_expert}, TENSOR_NOT_REQUIRED);
  4787. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), { n_ff_exp, n_embd, n_expert}, 0);
  4788. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff_exp, n_expert}, 0);
  4789. // Shared expert (if present)
  4790. if (hparams.n_ff_shexp > 0) {
  4791. layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), { n_embd, hparams.n_ff_shexp}, 0);
  4792. layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), {hparams.n_ff_shexp, n_embd }, 0);
  4793. layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), { n_embd, hparams.n_ff_shexp}, 0);
  4794. }
  4795. } else { // Dense layers
  4796. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  4797. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  4798. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  4799. }
  4800. }
  4801. } break;
  4802. case LLM_ARCH_FALCON_H1:
  4803. {
  4804. // Common
  4805. const int64_t hidden_size = hparams.n_embd; // hidden_size
  4806. // mamba2 Mixer SSM params
  4807. const int64_t ssm_conv_kernel_size = hparams.ssm_d_conv; // ssm_conv_kernel_size
  4808. const int64_t ssm_n_groups = hparams.ssm_n_group; // ssm_n_groups
  4809. const int64_t ssm_state_size = hparams.ssm_d_state; // ssm_state_size
  4810. const int64_t ssm_intermediate_size = hparams.ssm_d_inner; // TODO expand
  4811. const int64_t ssm_num_heads = hparams.ssm_dt_rank; // ssm_num_heads
  4812. const int64_t ssm_conv_dim = ssm_intermediate_size + 2 * ssm_n_groups * ssm_state_size;
  4813. const int64_t ssm_projection_size = ssm_intermediate_size + ssm_conv_dim + ssm_num_heads;
  4814. // attn params
  4815. const int64_t attn_num_attention_head = hparams.n_head(0); // rename to: attn_num_attention_head
  4816. const int64_t attn_num_key_value_head = hparams.n_head_kv(0);
  4817. // ffn params
  4818. const int64_t ffn_intermediate_size = hparams.n_ff(0);
  4819. // embeddings
  4820. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {hidden_size, n_vocab}, 0);
  4821. // output
  4822. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {hidden_size, n_vocab}, TENSOR_NOT_REQUIRED);
  4823. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {hidden_size}, 0);
  4824. // if output is NULL, init from the input tok embed
  4825. if (output == NULL) {
  4826. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {hidden_size, n_vocab}, TENSOR_DUPLICATED);
  4827. }
  4828. for (int i = 0; i < n_layer; ++i) {
  4829. auto & layer = layers[i];
  4830. /*SSM LAYERS*/
  4831. // ssm in
  4832. layer.ssm_in = create_tensor(tn(LLM_TENSOR_SSM_IN, "weight", i), {hidden_size, ssm_projection_size}, 0);
  4833. // ssm 1d conv
  4834. layer.ssm_conv1d = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "weight", i), {ssm_conv_kernel_size, ssm_conv_dim}, 0);
  4835. layer.ssm_conv1d_b = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "bias", i), {ssm_conv_dim}, TENSOR_NOT_REQUIRED);
  4836. // ssm_dt
  4837. layer.ssm_dt_b = create_tensor(tn(LLM_TENSOR_SSM_DT, "bias", i), {ssm_num_heads}, 0);
  4838. // no "weight" suffix for these
  4839. layer.ssm_a = create_tensor(tn(LLM_TENSOR_SSM_A, i), {1, ssm_num_heads}, 0);
  4840. layer.ssm_d = create_tensor(tn(LLM_TENSOR_SSM_D, i), {1, ssm_num_heads}, 0);
  4841. // ssm_norm
  4842. layer.ssm_norm = create_tensor(tn(LLM_TENSOR_SSM_NORM, "weight", i), {ssm_intermediate_size / ssm_n_groups, ssm_n_groups}, TENSOR_NOT_REQUIRED);
  4843. // out_proj
  4844. layer.ssm_out = create_tensor(tn(LLM_TENSOR_SSM_OUT, "weight", i), {ssm_intermediate_size, hidden_size}, 0);
  4845. /*ATTENTION LAYERS*/
  4846. // attention layers (with optional bias)
  4847. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {hidden_size, n_embd_head_k * attn_num_attention_head}, 0);
  4848. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {hidden_size, attn_num_key_value_head * n_embd_head_k}, 0);
  4849. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {hidden_size, attn_num_key_value_head * n_embd_head_v}, 0);
  4850. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * attn_num_attention_head, hidden_size}, 0);
  4851. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {hidden_size}, TENSOR_NOT_REQUIRED);
  4852. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {attn_num_key_value_head * n_embd_head_k}, TENSOR_NOT_REQUIRED);
  4853. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {attn_num_key_value_head * n_embd_head_v}, TENSOR_NOT_REQUIRED);
  4854. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {hidden_size}, TENSOR_NOT_REQUIRED);
  4855. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {hidden_size}, 0);
  4856. // feed forward (w/ optional biases)
  4857. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, i), {hidden_size}, 0);
  4858. layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  4859. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {hidden_size, ffn_intermediate_size}, 0);
  4860. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { ffn_intermediate_size, hidden_size}, 0);
  4861. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {hidden_size, ffn_intermediate_size}, 0);
  4862. layer.ffn_gate_b = create_tensor(tn(LLM_TENSOR_FFN_GATE, "bias", i), {ffn_intermediate_size}, TENSOR_NOT_REQUIRED);
  4863. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {hidden_size}, TENSOR_NOT_REQUIRED);
  4864. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {ffn_intermediate_size}, TENSOR_NOT_REQUIRED);
  4865. }
  4866. } break;
  4867. case LLM_ARCH_HUNYUAN_MOE:
  4868. {
  4869. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  4870. // output
  4871. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  4872. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  4873. // if output is NULL, init from the input tok embed
  4874. if (output == NULL) {
  4875. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  4876. }
  4877. for (int i = 0; i < n_layer; ++i) {
  4878. auto & layer = layers[i];
  4879. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  4880. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  4881. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  4882. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  4883. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  4884. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0);
  4885. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0);
  4886. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  4887. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  4888. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0);
  4889. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), { n_ff, n_embd, n_expert}, 0);
  4890. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0);
  4891. layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), {n_embd, hparams.n_ff_shexp}, 0);
  4892. layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), {n_embd, hparams.n_ff_shexp}, 0);
  4893. layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), {hparams.n_ff_shexp, n_embd}, 0);
  4894. }
  4895. } break;
  4896. case LLM_ARCH_HUNYUAN_DENSE:
  4897. {
  4898. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  4899. // output
  4900. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  4901. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  4902. // if output is NULL, init from the input tok embed
  4903. if (output == NULL) {
  4904. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  4905. }
  4906. for (int i = 0; i < n_layer; ++i) {
  4907. auto & layer = layers[i];
  4908. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  4909. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  4910. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  4911. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  4912. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  4913. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0);
  4914. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0);
  4915. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  4916. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  4917. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  4918. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  4919. }
  4920. } break;
  4921. case LLM_ARCH_SMOLLM3:
  4922. {
  4923. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  4924. // output
  4925. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  4926. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  4927. // if output is NULL, init from the input tok embed
  4928. if (output == NULL) {
  4929. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  4930. }
  4931. for (int i = 0; i < n_layer; ++i) {
  4932. auto & layer = layers[i];
  4933. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  4934. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  4935. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  4936. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  4937. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  4938. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  4939. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  4940. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  4941. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  4942. }
  4943. } break;
  4944. case LLM_ARCH_OPENAI_MOE:
  4945. {
  4946. const int64_t n_ff_exp = hparams.n_ff_exp;
  4947. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  4948. // output
  4949. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  4950. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  4951. for (int i = 0; i < n_layer; ++i) {
  4952. auto & layer = layers[i];
  4953. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  4954. layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), {n_embd}, 0);
  4955. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_head * n_rot}, 0);
  4956. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_head_kv * n_rot}, 0);
  4957. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_head_kv * n_rot}, 0);
  4958. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_head * n_rot, n_embd}, 0);
  4959. layer.attn_sinks = create_tensor(tn(LLM_TENSOR_ATTN_SINKS, "weight", i), {n_head}, 0);
  4960. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), { n_embd, n_expert}, 0);
  4961. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);
  4962. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp, n_embd, n_expert}, 0);
  4963. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);
  4964. // bias
  4965. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_head * n_rot}, 0);
  4966. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_head_kv * n_rot}, 0);
  4967. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_head_kv * n_rot}, 0);
  4968. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0);
  4969. layer.ffn_gate_inp_b = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "bias", i), {n_expert}, 0);
  4970. layer.ffn_gate_exps_b = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "bias", i), {n_ff_exp, n_expert}, 0);
  4971. layer.ffn_down_exps_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "bias", i), { n_embd, n_expert}, 0);
  4972. layer.ffn_up_exps_b = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "bias", i), {n_ff_exp, n_expert}, 0);
  4973. }
  4974. } break;
  4975. case LLM_ARCH_LFM2:
  4976. case LLM_ARCH_LFM2MOE:
  4977. {
  4978. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  4979. tok_norm = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "weight"), {n_embd}, 0);
  4980. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  4981. if (output == NULL) {
  4982. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  4983. }
  4984. for (int i = 0; i < n_layer; ++i) {
  4985. auto & layer = layers[i];
  4986. const bool is_moe_layer = i >= static_cast<int>(hparams.n_layer_dense_lead);
  4987. // ffn/moe is same for transformer and conv layers
  4988. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  4989. if (is_moe_layer) {
  4990. GGML_ASSERT(n_expert && n_expert_used);
  4991. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  4992. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, hparams.n_ff_exp, n_expert}, 0);
  4993. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {hparams.n_ff_exp, n_embd, n_expert}, 0);
  4994. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, hparams.n_ff_exp, n_expert}, 0);
  4995. layer.ffn_exp_probs_b = create_tensor(tn(LLM_TENSOR_FFN_EXP_PROBS_B, "bias", i), {n_expert}, 0);
  4996. } else { // dense
  4997. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  4998. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  4999. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  5000. }
  5001. // for operator_norm
  5002. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  5003. if (!hparams.is_recurrent(i)) {
  5004. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0);
  5005. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0);
  5006. GGML_ASSERT(n_embd_v_gqa == n_embd_k_gqa);
  5007. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  5008. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, hparams.n_embd_k_gqa(i)}, 0);
  5009. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, hparams.n_embd_v_gqa(i)}, 0);
  5010. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  5011. } else {
  5012. layer.shortconv.conv = create_tensor(tn(LLM_TENSOR_SHORTCONV_CONV, "weight", i), {hparams.n_shortconv_l_cache, n_embd}, 0);
  5013. layer.shortconv.in_proj = create_tensor(tn(LLM_TENSOR_SHORTCONV_INPROJ, "weight", i), {n_embd, 3 * n_embd}, 0);
  5014. layer.shortconv.out_proj = create_tensor(tn(LLM_TENSOR_SHORTCONV_OUTPROJ, "weight", i), {n_embd, n_embd}, 0);
  5015. }
  5016. }
  5017. } break;
  5018. case LLM_ARCH_SMALLTHINKER:
  5019. {
  5020. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, 0);
  5021. // output
  5022. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), { n_embd }, 0);
  5023. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  5024. // if output is NULL, init from the input tok embed
  5025. if (output == NULL) {
  5026. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  5027. }
  5028. for (int i = 0; i < n_layer; ++i) {
  5029. auto & layer = layers[i];
  5030. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), { n_embd }, 0);
  5031. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), { n_embd, n_embd_head_k * n_head }, 0);
  5032. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), { n_embd, n_embd_gqa }, 0);
  5033. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), { n_embd, n_embd_gqa }, 0);
  5034. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), { n_embd_head_k * n_head, n_embd }, 0);
  5035. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), { n_embd }, 0);
  5036. GGML_ASSERT(n_expert > 0 && "n_expert must be > 0 for SMALLTHINKER");
  5037. GGML_ASSERT(n_expert_used > 0 && "n_expert_used must be > 0 for SMALLTHINKER");
  5038. // MoE branch
  5039. const int64_t n_ff_exp = hparams.n_ff_exp;
  5040. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), { n_embd, n_expert }, 0);
  5041. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert }, 0);
  5042. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), { n_ff_exp, n_embd, n_expert }, 0);
  5043. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert }, 0);
  5044. }
  5045. } break;
  5046. case LLM_ARCH_GROVEMOE:
  5047. {
  5048. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  5049. // output
  5050. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  5051. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  5052. // if output is NULL, init from the input tok embed
  5053. if (output == NULL) {
  5054. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  5055. }
  5056. GGML_ASSERT(n_expert > 0 && "n_expert must be > 0 for GROVEMOE");
  5057. GGML_ASSERT(n_expert_used > 0 && "n_expert_used must be > 0 for GROVEMOE");
  5058. GGML_ASSERT(hparams.n_group_experts > 0 && "n_group_experts must be > 0 for GROVEMOE");
  5059. for (int i = 0; i < n_layer; ++i) {
  5060. auto & layer = layers[i];
  5061. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  5062. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  5063. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  5064. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  5065. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  5066. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0);
  5067. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0);
  5068. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  5069. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  5070. // MoE branch
  5071. const int64_t n_ff_exp = hparams.n_ff_exp ? hparams.n_ff_exp : n_ff / n_expert_used;
  5072. const int64_t n_ff_chexp = hparams.n_ff_chexp ? hparams.n_ff_chexp : n_embd_head_k;
  5073. const int64_t n_chunk_expert = n_expert / hparams.n_group_experts;
  5074. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);
  5075. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp, n_embd, n_expert}, 0);
  5076. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);
  5077. layer.ffn_gate_chexps = create_tensor(tn(LLM_TENSOR_FFN_GATE_CHEXPS, "weight", i), { n_embd, n_ff_chexp, n_chunk_expert}, 0);
  5078. layer.ffn_down_chexps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_CHEXPS, "weight", i), {n_ff_chexp, n_embd, n_chunk_expert}, 0);
  5079. layer.ffn_up_chexps = create_tensor(tn(LLM_TENSOR_FFN_UP_CHEXPS, "weight", i), { n_embd, n_ff_chexp, n_chunk_expert}, 0);
  5080. }
  5081. } break;
  5082. case LLM_ARCH_APERTUS:
  5083. {
  5084. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, 0);
  5085. // output
  5086. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), { n_embd }, 0);
  5087. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), { n_embd, n_vocab }, 0);
  5088. for (int i = 0; i < n_layer; ++i) {
  5089. auto & layer = layers[i];
  5090. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), { n_embd }, 0);
  5091. if (hparams.rope_scaling_type_train == LLAMA_ROPE_SCALING_TYPE_LONGROPE) {
  5092. layer.rope_long = create_tensor(tn(LLM_TENSOR_ROPE_FACTORS_LONG, "weight", i), { n_rot/2 }, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  5093. layer.rope_short = create_tensor(tn(LLM_TENSOR_ROPE_FACTORS_SHORT, "weight", i), { n_rot/2 }, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  5094. } else {
  5095. layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), { n_rot/2 }, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  5096. }
  5097. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), { n_embd, n_embd_head_k * n_head }, 0);
  5098. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), { n_embd, n_embd_gqa }, 0);
  5099. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), { n_embd, n_embd_gqa }, 0);
  5100. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), { n_embd_head_k * n_head, n_embd }, 0);
  5101. // optional bias tensors
  5102. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), { n_embd }, TENSOR_NOT_REQUIRED);
  5103. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), { n_embd_gqa }, TENSOR_NOT_REQUIRED);
  5104. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), { n_embd_gqa }, TENSOR_NOT_REQUIRED);
  5105. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), { n_embd }, TENSOR_NOT_REQUIRED);
  5106. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), { n_embd }, 0);
  5107. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd }, 0);
  5108. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), { n_embd, n_ff }, 0);
  5109. // Q and K layernorms for Apertus
  5110. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), { n_embd_head_k }, 0);
  5111. layer.attn_q_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "bias", i), { n_embd_head_k }, TENSOR_NOT_REQUIRED);
  5112. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), { n_embd_head_k }, 0);
  5113. layer.attn_k_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "bias", i), { n_embd_head_k }, TENSOR_NOT_REQUIRED);
  5114. }
  5115. } break;
  5116. case LLM_ARCH_COGVLM:
  5117. {
  5118. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  5119. // output
  5120. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  5121. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  5122. // if output is NULL, init from the input tok embed
  5123. if (output == NULL) {
  5124. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  5125. }
  5126. for (int i = 0; i < n_layer; ++i) {
  5127. auto & layer = layers[i];
  5128. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  5129. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd_head_k * n_head * 3}, 0);
  5130. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  5131. layer.visexp_attn_wqkv = create_tensor(tn(LLM_TENSOR_VISEXP_ATTN_QKV, "weight", i), {n_embd, n_embd_head_k * n_head * 3}, 0);
  5132. layer.visexp_attn_wo = create_tensor(tn(LLM_TENSOR_VISEXP_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  5133. layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  5134. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  5135. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  5136. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  5137. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  5138. layer.visexp_ffn_gate = create_tensor(tn(LLM_TENSOR_VISEXP_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  5139. layer.visexp_ffn_down = create_tensor(tn(LLM_TENSOR_VISEXP_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  5140. layer.visexp_ffn_up = create_tensor(tn(LLM_TENSOR_VISEXP_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  5141. }
  5142. } break;
  5143. default:
  5144. throw std::runtime_error("unknown architecture");
  5145. }
  5146. if (n_moved_tensors > 0) {
  5147. LLAMA_LOG_DEBUG("%s: tensor '%s' (%s) (and %d others) cannot be used with preferred buffer type %s, using %s instead\n",
  5148. __func__, first_moved_tensor->name, ggml_type_name(first_moved_tensor->type), n_moved_tensors - 1,
  5149. ggml_backend_buft_name(first_moved_from_buft), ggml_backend_buft_name(first_moved_to_buft));
  5150. }
  5151. }
  5152. ml.done_getting_tensors();
  5153. ml.init_mappings(true, use_mlock ? &pimpl->mlock_mmaps : nullptr);
  5154. pimpl->mappings.reserve(ml.mappings.size());
  5155. // create the backend buffers
  5156. std::vector<std::pair<ggml_context *, llama_buf_map>> ctx_buf_maps;
  5157. ctx_buf_maps.reserve(ctx_map.size());
  5158. // Ensure we have enough capacity for the maximum backend buffer we will potentially create
  5159. const size_t n_max_backend_buffer = ctx_map.size() * ml.files.size();
  5160. pimpl->ctxs_bufs.reserve(n_max_backend_buffer);
  5161. for (auto & [buft, ctx_ptr] : ctx_map) {
  5162. ggml_context * ctx = ctx_ptr.get();
  5163. // skip contexts without tensors
  5164. if (ggml_get_first_tensor(ctx) == nullptr) {
  5165. continue;
  5166. }
  5167. llama_buf_map buf_map;
  5168. buf_map.reserve(n_max_backend_buffer);
  5169. // check if it is possible to use buffer_from_host_ptr with this buffer type
  5170. ggml_backend_dev_t dev = ggml_backend_buft_get_device(buft);
  5171. if (!dev) {
  5172. // FIXME: workaround for CPU backend buft having a NULL device
  5173. dev = ggml_backend_dev_by_type(GGML_BACKEND_DEVICE_TYPE_CPU);
  5174. if (!dev) {
  5175. throw std::runtime_error(format("%s: no CPU backend found", __func__));
  5176. }
  5177. }
  5178. ggml_backend_dev_props props;
  5179. ggml_backend_dev_get_props(dev, &props);
  5180. bool buffer_from_host_ptr_supported = props.caps.buffer_from_host_ptr;
  5181. bool is_default_buft = buft == ggml_backend_dev_buffer_type(dev);
  5182. std::vector<ggml_backend_buffer_ptr> bufs;
  5183. if (ml.use_mmap && use_mmap_buffer && buffer_from_host_ptr_supported && is_default_buft) {
  5184. for (uint32_t idx = 0; idx < ml.files.size(); idx++) {
  5185. // only the mmap region containing the tensors in the model is mapped to the backend buffer
  5186. // 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
  5187. // this allows using partial offloading when the model size exceeds the metal buffer size, but not the RAM size
  5188. void * addr = nullptr;
  5189. size_t first, last; // NOLINT
  5190. ml.get_mapping_range(&first, &last, &addr, idx, ctx);
  5191. if (first >= last) {
  5192. continue;
  5193. }
  5194. const size_t max_size = ggml_get_max_tensor_size(ctx);
  5195. ggml_backend_buffer_t buf = ggml_backend_dev_buffer_from_host_ptr(dev, (char *) addr + first, last - first, max_size);
  5196. if (buf == nullptr) {
  5197. throw std::runtime_error(format("unable to allocate %s buffer", ggml_backend_buft_name(buft)));
  5198. }
  5199. bufs.emplace_back(buf);
  5200. buf_map.emplace(idx, buf);
  5201. }
  5202. }
  5203. else {
  5204. ggml_backend_buffer_t buf = ggml_backend_alloc_ctx_tensors_from_buft(ctx, buft);
  5205. if (buf == nullptr) {
  5206. throw std::runtime_error(format("unable to allocate %s buffer", ggml_backend_buft_name(buft)));
  5207. }
  5208. if (use_mlock && ggml_backend_buffer_is_host(buf)) {
  5209. pimpl->mlock_bufs.emplace_back(new llama_mlock);
  5210. auto & mlock_buf = pimpl->mlock_bufs.back();
  5211. mlock_buf->init (ggml_backend_buffer_get_base(buf));
  5212. mlock_buf->grow_to(ggml_backend_buffer_get_size(buf));
  5213. }
  5214. bufs.emplace_back(buf);
  5215. for (uint32_t idx = 0; idx < ml.files.size(); idx++) {
  5216. buf_map.emplace(idx, buf);
  5217. }
  5218. }
  5219. pimpl->ctxs_bufs.emplace_back(std::move(ctx_ptr), std::move(bufs));
  5220. for (auto & buf : buf_map) {
  5221. // indicate that this buffer contains weights
  5222. // 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
  5223. ggml_backend_buffer_set_usage(buf.second, GGML_BACKEND_BUFFER_USAGE_WEIGHTS);
  5224. }
  5225. ctx_buf_maps.emplace_back(ctx, buf_map);
  5226. }
  5227. if (llama_supports_gpu_offload()) {
  5228. const int n_gpu = std::min(n_gpu_layers, int(hparams.n_layer));
  5229. LLAMA_LOG_INFO("%s: offloading %d repeating layers to GPU\n", __func__, n_gpu);
  5230. if (n_gpu_layers > (int) hparams.n_layer) {
  5231. LLAMA_LOG_INFO("%s: offloading output layer to GPU\n", __func__);
  5232. }
  5233. const int max_backend_supported_layers = hparams.n_layer + 1;
  5234. const int max_offloadable_layers = hparams.n_layer + 1;
  5235. LLAMA_LOG_INFO("%s: offloaded %d/%d layers to GPU\n", __func__, std::min(n_gpu_layers, max_offloadable_layers), max_backend_supported_layers);
  5236. }
  5237. // print memory requirements per buffer type
  5238. for (auto & [_, bufs] : pimpl->ctxs_bufs) {
  5239. for (auto & buf: bufs) {
  5240. LLAMA_LOG_INFO("%s: %12s model buffer size = %8.2f MiB\n",
  5241. __func__, ggml_backend_buffer_name(buf.get()), ggml_backend_buffer_get_size(buf.get()) / 1024.0 / 1024.0);
  5242. }
  5243. }
  5244. // populate tensors_by_name
  5245. for (auto & [ctx, _] : pimpl->ctxs_bufs) {
  5246. for (auto * cur = ggml_get_first_tensor(ctx.get()); cur != NULL; cur = ggml_get_next_tensor(ctx.get(), cur)) {
  5247. tensors_by_name.emplace_back(ggml_get_name(cur), cur);
  5248. }
  5249. }
  5250. // load tensor data
  5251. for (auto & [ctx, buf_map] : ctx_buf_maps) {
  5252. if (!ml.load_all_data(ctx, buf_map, use_mlock ? &pimpl->mlock_mmaps : NULL, params.progress_callback, params.progress_callback_user_data)) {
  5253. return false;
  5254. }
  5255. }
  5256. if (use_mmap_buffer) {
  5257. for (auto & mapping : ml.mappings) {
  5258. pimpl->mappings.emplace_back(std::move(mapping));
  5259. }
  5260. }
  5261. return true;
  5262. }
  5263. std::string llama_model::arch_name() const {
  5264. return llm_arch_name(arch);
  5265. }
  5266. std::string llama_model::type_name() const {
  5267. return llm_type_name(type);
  5268. }
  5269. std::string llama_model::desc() const {
  5270. return pimpl->desc_str;
  5271. }
  5272. size_t llama_model::size() const {
  5273. return pimpl->n_bytes;
  5274. }
  5275. size_t llama_model::n_tensors() const {
  5276. return tensors_by_name.size();
  5277. }
  5278. size_t llama_model::n_devices() const {
  5279. return devices.size();
  5280. }
  5281. std::map<ggml_backend_buffer_type_t, size_t> llama_model::memory_breakdown() const {
  5282. std::map<ggml_backend_buffer_type_t, size_t> ret;
  5283. for (const auto & [_, bufs] : pimpl->ctxs_bufs) {
  5284. for (const auto & buf : bufs) {
  5285. ret[ggml_backend_buffer_get_type(buf.get())] += ggml_backend_buffer_get_size(buf.get());
  5286. }
  5287. }
  5288. return ret;
  5289. }
  5290. uint64_t llama_model::n_elements() const {
  5291. return pimpl->n_elements;
  5292. }
  5293. void llama_model::print_info() const {
  5294. const std::string rope_scaling_type = llama_rope_scaling_type_name(hparams.rope_scaling_type_train);
  5295. auto print_f = [](const std::function<uint32_t(uint32_t)> & f, uint32_t n) {
  5296. bool is_var = false;
  5297. std::vector<uint32_t> v;
  5298. for (uint32_t i = 0; i < n; ++i) {
  5299. v.push_back(f(i));
  5300. if (v[i] != v[0]) {
  5301. is_var = true;
  5302. }
  5303. }
  5304. std::stringstream ss;
  5305. if (is_var) {
  5306. ss << "[";
  5307. for (uint32_t i = 0; i < n; ++i) {
  5308. ss << v[i];
  5309. if (i < n - 1) {
  5310. ss << ", ";
  5311. }
  5312. }
  5313. ss << "]";
  5314. } else {
  5315. ss << v[0];
  5316. }
  5317. return ss.str();
  5318. };
  5319. // hparams
  5320. LLAMA_LOG_INFO("%s: arch = %s\n", __func__, arch_name().c_str());
  5321. LLAMA_LOG_INFO("%s: vocab_only = %d\n", __func__, hparams.vocab_only);
  5322. if (!hparams.vocab_only) {
  5323. LLAMA_LOG_INFO("%s: n_ctx_train = %u\n", __func__, hparams.n_ctx_train);
  5324. LLAMA_LOG_INFO("%s: n_embd = %u\n", __func__, hparams.n_embd);
  5325. LLAMA_LOG_INFO("%s: n_layer = %u\n", __func__, hparams.n_layer);
  5326. LLAMA_LOG_INFO("%s: n_head = %s\n", __func__, print_f([&](uint32_t il) { return hparams.n_head(il); }, hparams.n_layer).c_str());
  5327. 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());
  5328. LLAMA_LOG_INFO("%s: n_rot = %u\n", __func__, hparams.n_rot);
  5329. LLAMA_LOG_INFO("%s: n_swa = %u\n", __func__, hparams.n_swa);
  5330. LLAMA_LOG_INFO("%s: is_swa_any = %u\n", __func__, hparams.is_swa_any());
  5331. LLAMA_LOG_INFO("%s: n_embd_head_k = %u\n", __func__, hparams.n_embd_head_k);
  5332. LLAMA_LOG_INFO("%s: n_embd_head_v = %u\n", __func__, hparams.n_embd_head_v);
  5333. LLAMA_LOG_INFO("%s: n_gqa = %s\n", __func__, print_f([&](uint32_t il) { return hparams.n_gqa(il); }, hparams.n_layer).c_str());
  5334. 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());
  5335. 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());
  5336. LLAMA_LOG_INFO("%s: f_norm_eps = %.1e\n", __func__, hparams.f_norm_eps);
  5337. LLAMA_LOG_INFO("%s: f_norm_rms_eps = %.1e\n", __func__, hparams.f_norm_rms_eps);
  5338. LLAMA_LOG_INFO("%s: f_clamp_kqv = %.1e\n", __func__, hparams.f_clamp_kqv);
  5339. LLAMA_LOG_INFO("%s: f_max_alibi_bias = %.1e\n", __func__, hparams.f_max_alibi_bias);
  5340. LLAMA_LOG_INFO("%s: f_logit_scale = %.1e\n", __func__, hparams.f_logit_scale);
  5341. LLAMA_LOG_INFO("%s: f_attn_scale = %.1e\n", __func__, hparams.f_attention_scale);
  5342. LLAMA_LOG_INFO("%s: n_ff = %s\n", __func__, print_f([&](uint32_t il) { return hparams.n_ff(il); }, hparams.n_layer).c_str());
  5343. LLAMA_LOG_INFO("%s: n_expert = %u\n", __func__, hparams.n_expert);
  5344. LLAMA_LOG_INFO("%s: n_expert_used = %u\n", __func__, hparams.n_expert_used);
  5345. LLAMA_LOG_INFO("%s: n_expert_groups = %d\n", __func__, hparams.n_expert_groups);
  5346. LLAMA_LOG_INFO("%s: n_group_used = %d\n", __func__, hparams.n_group_used);
  5347. LLAMA_LOG_INFO("%s: causal attn = %d\n", __func__, hparams.causal_attn);
  5348. LLAMA_LOG_INFO("%s: pooling type = %d\n", __func__, hparams.pooling_type);
  5349. LLAMA_LOG_INFO("%s: rope type = %d\n", __func__, hparams.rope_type);
  5350. LLAMA_LOG_INFO("%s: rope scaling = %s\n", __func__, rope_scaling_type.c_str());
  5351. LLAMA_LOG_INFO("%s: freq_base_train = %.1f\n", __func__, hparams.rope_freq_base_train);
  5352. LLAMA_LOG_INFO("%s: freq_scale_train = %g\n", __func__, hparams.rope_freq_scale_train);
  5353. LLAMA_LOG_INFO("%s: n_ctx_orig_yarn = %u\n", __func__, hparams.n_ctx_orig_yarn);
  5354. LLAMA_LOG_INFO("%s: rope_finetuned = %s\n", __func__, hparams.rope_finetuned ? "yes" : "unknown");
  5355. if (!classifier_labels.empty()) {
  5356. LLAMA_LOG_INFO("%s: n_cls_out = %u\n", __func__, hparams.n_cls_out);
  5357. size_t i = 0;
  5358. for (auto label : classifier_labels) {
  5359. LLAMA_LOG_INFO("%s: cls_label[%2zu] = %s\n", __func__, i++, label.c_str());
  5360. }
  5361. }
  5362. }
  5363. if (arch == LLM_ARCH_MAMBA ||
  5364. arch == LLM_ARCH_MAMBA2 ||
  5365. arch == LLM_ARCH_JAMBA ||
  5366. arch == LLM_ARCH_FALCON_H1 ||
  5367. arch == LLM_ARCH_PLAMO2 ||
  5368. arch == LLM_ARCH_GRANITE_HYBRID ||
  5369. arch == LLM_ARCH_NEMOTRON_H) {
  5370. LLAMA_LOG_INFO("%s: ssm_d_conv = %u\n", __func__, hparams.ssm_d_conv);
  5371. LLAMA_LOG_INFO("%s: ssm_d_inner = %u\n", __func__, hparams.ssm_d_inner);
  5372. LLAMA_LOG_INFO("%s: ssm_d_state = %u\n", __func__, hparams.ssm_d_state);
  5373. LLAMA_LOG_INFO("%s: ssm_dt_rank = %u\n", __func__, hparams.ssm_dt_rank);
  5374. LLAMA_LOG_INFO("%s: ssm_n_group = %u\n", __func__, hparams.ssm_n_group);
  5375. LLAMA_LOG_INFO("%s: ssm_dt_b_c_rms = %d\n", __func__, hparams.ssm_dt_b_c_rms);
  5376. }
  5377. LLAMA_LOG_INFO("%s: model type = %s\n", __func__, type_name().c_str());
  5378. if (pimpl->n_elements >= 1e12) {
  5379. LLAMA_LOG_INFO("%s: model params = %.2f T\n", __func__, pimpl->n_elements*1e-12);
  5380. } else if (pimpl->n_elements >= 1e9) {
  5381. LLAMA_LOG_INFO("%s: model params = %.2f B\n", __func__, pimpl->n_elements*1e-9);
  5382. } else if (pimpl->n_elements >= 1e6) {
  5383. LLAMA_LOG_INFO("%s: model params = %.2f M\n", __func__, pimpl->n_elements*1e-6);
  5384. } else {
  5385. LLAMA_LOG_INFO("%s: model params = %.2f K\n", __func__, pimpl->n_elements*1e-3);
  5386. }
  5387. // general kv
  5388. LLAMA_LOG_INFO("%s: general.name = %s\n", __func__, name.c_str());
  5389. if (arch == LLM_ARCH_DEEPSEEK) {
  5390. LLAMA_LOG_INFO("%s: n_layer_dense_lead = %d\n", __func__, hparams.n_layer_dense_lead);
  5391. LLAMA_LOG_INFO("%s: n_ff_exp = %d\n", __func__, hparams.n_ff_exp);
  5392. LLAMA_LOG_INFO("%s: n_expert_shared = %d\n", __func__, hparams.n_expert_shared);
  5393. LLAMA_LOG_INFO("%s: expert_weights_scale = %.1f\n", __func__, hparams.expert_weights_scale);
  5394. }
  5395. if (arch == LLM_ARCH_DEEPSEEK2) {
  5396. LLAMA_LOG_INFO("%s: n_layer_dense_lead = %d\n", __func__, hparams.n_layer_dense_lead);
  5397. LLAMA_LOG_INFO("%s: n_lora_q = %d\n", __func__, hparams.n_lora_q);
  5398. LLAMA_LOG_INFO("%s: n_lora_kv = %d\n", __func__, hparams.n_lora_kv);
  5399. LLAMA_LOG_INFO("%s: n_embd_head_k_mla = %d\n", __func__, hparams.n_embd_head_k_mla);
  5400. LLAMA_LOG_INFO("%s: n_embd_head_v_mla = %d\n", __func__, hparams.n_embd_head_v_mla);
  5401. LLAMA_LOG_INFO("%s: n_ff_exp = %d\n", __func__, hparams.n_ff_exp);
  5402. LLAMA_LOG_INFO("%s: n_expert_shared = %d\n", __func__, hparams.n_expert_shared);
  5403. LLAMA_LOG_INFO("%s: expert_weights_scale = %.1f\n", __func__, hparams.expert_weights_scale);
  5404. LLAMA_LOG_INFO("%s: expert_weights_norm = %d\n", __func__, hparams.expert_weights_norm);
  5405. LLAMA_LOG_INFO("%s: expert_gating_func = %s\n", __func__, llama_expert_gating_func_name((llama_expert_gating_func_type) hparams.expert_gating_func));
  5406. LLAMA_LOG_INFO("%s: rope_yarn_log_mul = %.4f\n", __func__, hparams.rope_yarn_log_mul);
  5407. }
  5408. if (arch == LLM_ARCH_QWEN2MOE) {
  5409. LLAMA_LOG_INFO("%s: n_ff_exp = %d\n", __func__, hparams.n_ff_exp);
  5410. LLAMA_LOG_INFO("%s: n_ff_shexp = %d\n", __func__, hparams.n_ff_shexp);
  5411. }
  5412. if (arch == LLM_ARCH_QWEN3MOE || arch == LLM_ARCH_OPENAI_MOE) {
  5413. LLAMA_LOG_INFO("%s: n_ff_exp = %d\n", __func__, hparams.n_ff_exp);
  5414. }
  5415. if (arch == LLM_ARCH_MINICPM ||
  5416. arch == LLM_ARCH_GRANITE ||
  5417. arch == LLM_ARCH_GRANITE_MOE ||
  5418. arch == LLM_ARCH_GRANITE_HYBRID) {
  5419. LLAMA_LOG_INFO("%s: f_embedding_scale = %f\n", __func__, hparams.f_embedding_scale);
  5420. LLAMA_LOG_INFO("%s: f_residual_scale = %f\n", __func__, hparams.f_residual_scale);
  5421. LLAMA_LOG_INFO("%s: f_attention_scale = %f\n", __func__, hparams.f_attention_scale);
  5422. LLAMA_LOG_INFO("%s: n_ff_shexp = %d\n", __func__, hparams.n_ff_shexp);
  5423. }
  5424. if (arch == LLM_ARCH_BAILINGMOE) {
  5425. LLAMA_LOG_INFO("%s: n_layer_dense_lead = %d\n", __func__, hparams.n_layer_dense_lead);
  5426. LLAMA_LOG_INFO("%s: n_ff_exp = %d\n", __func__, hparams.n_ff_exp);
  5427. LLAMA_LOG_INFO("%s: n_expert_shared = %d\n", __func__, hparams.n_expert_shared);
  5428. LLAMA_LOG_INFO("%s: expert_weights_scale = %.1f\n", __func__, hparams.expert_weights_scale);
  5429. LLAMA_LOG_INFO("%s: expert_weights_norm = %d\n", __func__, hparams.expert_weights_norm);
  5430. }
  5431. if (arch == LLM_ARCH_BAILINGMOE2) {
  5432. LLAMA_LOG_INFO("%s: n_layer_dense_lead = %d\n", __func__, hparams.n_layer_dense_lead);
  5433. LLAMA_LOG_INFO("%s: n_ff_exp = %d\n", __func__, hparams.n_ff_exp);
  5434. LLAMA_LOG_INFO("%s: n_ff_shexp = %d\n", __func__, hparams.n_ff_shexp);
  5435. LLAMA_LOG_INFO("%s: n_expert_shared = %d\n", __func__, hparams.n_expert_shared);
  5436. LLAMA_LOG_INFO("%s: expert_weights_scale = %.1f\n", __func__, hparams.expert_weights_scale);
  5437. LLAMA_LOG_INFO("%s: expert_weights_norm = %d\n", __func__, hparams.expert_weights_norm);
  5438. LLAMA_LOG_INFO("%s: expert_gating_func = %s\n", __func__, llama_expert_gating_func_name((llama_expert_gating_func_type) hparams.expert_gating_func));
  5439. LLAMA_LOG_INFO("%s: nextn_predict_layers = %d\n", __func__, hparams.nextn_predict_layers);
  5440. }
  5441. if (arch == LLM_ARCH_SMALLTHINKER || arch == LLM_ARCH_LFM2MOE) {
  5442. LLAMA_LOG_INFO("%s: n_ff_exp = %d\n", __func__, hparams.n_ff_exp);
  5443. LLAMA_LOG_INFO("%s: expert_gating_func = %s\n", __func__, llama_expert_gating_func_name((llama_expert_gating_func_type) hparams.expert_gating_func));
  5444. }
  5445. if (arch == LLM_ARCH_GROVEMOE) {
  5446. LLAMA_LOG_INFO("%s: n_ff_exp = %d\n", __func__, hparams.n_ff_exp);
  5447. LLAMA_LOG_INFO("%s: n_ff_chexp = %d\n", __func__, hparams.n_ff_chexp);
  5448. LLAMA_LOG_INFO("%s: n_group_experts = %d\n", __func__, hparams.n_group_experts);
  5449. LLAMA_LOG_INFO("%s: expert_group_scale = %.2f\n", __func__, hparams.expert_group_scale);
  5450. }
  5451. vocab.print_info();
  5452. }
  5453. ggml_backend_dev_t llama_model::dev_layer(int il) const {
  5454. return pimpl->dev_layer.at(il).dev;
  5455. }
  5456. ggml_backend_dev_t llama_model::dev_output() const {
  5457. return pimpl->dev_output.dev;
  5458. }
  5459. template<typename F>
  5460. static bool buft_supported(ggml_backend_buffer_type_t buft, ggml_backend_dev_t dev, F & fn) {
  5461. ggml_init_params params = {
  5462. /*.mem_size =*/ ggml_tensor_overhead()*8,
  5463. /*.mem_buffer =*/ NULL,
  5464. /*.no_alloc =*/ true,
  5465. };
  5466. ggml_context_ptr ctx { ggml_init(params) };
  5467. if (!ctx) {
  5468. throw std::runtime_error(format("failed to create ggml context"));
  5469. }
  5470. ggml_backend_buffer_ptr buf { ggml_backend_buft_alloc_buffer(buft, 0) };
  5471. ggml_tensor * op_tensor = fn(ctx.get());
  5472. for (int i = 0; i < GGML_MAX_SRC; i++) {
  5473. if (op_tensor->src[i] != nullptr) {
  5474. assert(op_tensor->src[i]->buffer == nullptr);
  5475. op_tensor->src[i]->buffer = buf.get();
  5476. }
  5477. }
  5478. bool op_supported = ggml_backend_dev_supports_op(dev, op_tensor);
  5479. return op_supported;
  5480. }
  5481. template<typename F>
  5482. static ggml_backend_buffer_type_t select_buft(const buft_list_t & buft_list, const F & fn) {
  5483. for (const auto & cur : buft_list) {
  5484. ggml_backend_dev_t cur_dev = cur.first;
  5485. ggml_backend_buffer_type_t cur_buft = cur.second;
  5486. if (buft_supported(cur_buft, cur_dev, fn)) {
  5487. return cur_buft;
  5488. }
  5489. }
  5490. throw std::runtime_error(format("no suitable buffer type found"));
  5491. }
  5492. ggml_backend_buffer_type_t llama_model::select_buft(int il) const {
  5493. return ::select_buft(
  5494. *pimpl->dev_layer.at(il).buft_list,
  5495. [&](ggml_context * ctx) {
  5496. ggml_tensor * cur = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, hparams.n_embd);
  5497. ggml_tensor * layer_dir = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, hparams.n_embd);
  5498. return ggml_add(ctx, cur, layer_dir);
  5499. });
  5500. }
  5501. bool llama_model::has_tensor_overrides() const {
  5502. return pimpl->has_tensor_overrides;
  5503. }
  5504. const ggml_tensor * llama_model::get_tensor(const char * name) const {
  5505. auto it = std::find_if(tensors_by_name.begin(), tensors_by_name.end(),
  5506. [name](const std::pair<std::string, ggml_tensor *> & it) {
  5507. return it.first == name;
  5508. });
  5509. if (it == tensors_by_name.end()) {
  5510. return nullptr;
  5511. }
  5512. return it->second;
  5513. }
  5514. float llama_model::get_rope_freq_base (const llama_cparams & cparams, int il) const {
  5515. return hparams.is_swa(il) ? hparams.rope_freq_base_train_swa : cparams.rope_freq_base;
  5516. }
  5517. float llama_model::get_rope_freq_scale(const llama_cparams & cparams, int il) const {
  5518. return hparams.is_swa(il) ? hparams.rope_freq_scale_train_swa : cparams.rope_freq_scale;
  5519. }
  5520. ggml_tensor * llama_model::get_rope_factors(const llama_cparams & cparams, int il) const {
  5521. const uint32_t n_ctx_per_seq = cparams.n_ctx / cparams.n_seq_max;
  5522. // choose long/short freq factors based on the context size
  5523. if (layers[il].rope_freqs != nullptr) {
  5524. return layers[il].rope_freqs;
  5525. }
  5526. if (n_ctx_per_seq > hparams.n_ctx_orig_yarn) {
  5527. return layers[il].rope_long;
  5528. }
  5529. return layers[il].rope_short;
  5530. }
  5531. struct llm_build_llama : public llm_graph_context {
  5532. llm_build_llama(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  5533. const int64_t n_embd_head = hparams.n_embd_head_v;
  5534. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  5535. GGML_ASSERT(n_embd_head == hparams.n_rot);
  5536. ggml_tensor * cur;
  5537. ggml_tensor * inpL;
  5538. inpL = build_inp_embd(model.tok_embd);
  5539. // inp_pos - contains the positions
  5540. ggml_tensor * inp_pos = build_inp_pos();
  5541. auto * inp_attn = build_attn_inp_kv();
  5542. const float kq_scale = hparams.f_attention_scale == 0.0f ? 1.0f/sqrtf(float(n_embd_head)) : hparams.f_attention_scale;
  5543. ggml_tensor * inp_out_ids = build_inp_out_ids();
  5544. for (int il = 0; il < n_layer; ++il) {
  5545. ggml_tensor * inpSA = inpL;
  5546. // norm
  5547. cur = build_norm(inpL,
  5548. model.layers[il].attn_norm, NULL,
  5549. LLM_NORM_RMS, il);
  5550. cb(cur, "attn_norm", il);
  5551. // self-attention
  5552. {
  5553. // rope freq factors for llama3; may return nullptr for llama2 and other models
  5554. ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
  5555. // compute Q and K and RoPE them
  5556. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  5557. cb(Qcur, "Qcur", il);
  5558. if (model.layers[il].bq) {
  5559. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  5560. cb(Qcur, "Qcur", il);
  5561. }
  5562. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  5563. cb(Kcur, "Kcur", il);
  5564. if (model.layers[il].bk) {
  5565. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  5566. cb(Kcur, "Kcur", il);
  5567. }
  5568. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  5569. cb(Vcur, "Vcur", il);
  5570. if (model.layers[il].bv) {
  5571. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  5572. cb(Vcur, "Vcur", il);
  5573. }
  5574. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  5575. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  5576. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  5577. Qcur = ggml_rope_ext(
  5578. ctx0, Qcur, inp_pos, rope_factors,
  5579. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  5580. ext_factor, attn_factor, beta_fast, beta_slow
  5581. );
  5582. Kcur = ggml_rope_ext(
  5583. ctx0, Kcur, inp_pos, rope_factors,
  5584. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  5585. ext_factor, attn_factor, beta_fast, beta_slow
  5586. );
  5587. cb(Qcur, "Qcur", il);
  5588. cb(Kcur, "Kcur", il);
  5589. cb(Vcur, "Vcur", il);
  5590. if (hparams.use_kq_norm) {
  5591. // Llama4TextL2Norm
  5592. Qcur = ggml_rms_norm(ctx0, Qcur, hparams.f_norm_rms_eps);
  5593. Kcur = ggml_rms_norm(ctx0, Kcur, hparams.f_norm_rms_eps);
  5594. cb(Qcur, "Qcur_normed", il);
  5595. cb(Kcur, "Kcur_normed", il);
  5596. }
  5597. cur = build_attn(inp_attn,
  5598. model.layers[il].wo, model.layers[il].bo,
  5599. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
  5600. cb(cur, "attn_out", il);
  5601. }
  5602. if (il == n_layer - 1 && inp_out_ids) {
  5603. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  5604. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  5605. }
  5606. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  5607. cb(ffn_inp, "ffn_inp", il);
  5608. // feed-forward network (non-MoE)
  5609. if (model.layers[il].ffn_gate_inp == nullptr) {
  5610. cur = build_norm(ffn_inp,
  5611. model.layers[il].ffn_norm, NULL,
  5612. LLM_NORM_RMS, il);
  5613. cb(cur, "ffn_norm", il);
  5614. cur = build_ffn(cur,
  5615. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  5616. model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, NULL,
  5617. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  5618. NULL,
  5619. LLM_FFN_SILU, LLM_FFN_PAR, il);
  5620. cb(cur, "ffn_out", il);
  5621. } else {
  5622. // MoE branch
  5623. cur = build_norm(ffn_inp,
  5624. model.layers[il].ffn_norm, NULL,
  5625. LLM_NORM_RMS, il);
  5626. cb(cur, "ffn_norm", il);
  5627. cur = build_moe_ffn(cur,
  5628. model.layers[il].ffn_gate_inp,
  5629. model.layers[il].ffn_up_exps,
  5630. model.layers[il].ffn_gate_exps,
  5631. model.layers[il].ffn_down_exps,
  5632. nullptr,
  5633. n_expert, n_expert_used,
  5634. LLM_FFN_SILU, true,
  5635. false, 0.0,
  5636. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  5637. il);
  5638. cb(cur, "ffn_moe_out", il);
  5639. }
  5640. cur = ggml_add(ctx0, cur, ffn_inp);
  5641. cb(cur, "ffn_out", il);
  5642. cur = build_cvec(cur, il);
  5643. cb(cur, "l_out", il);
  5644. // input for next layer
  5645. inpL = cur;
  5646. }
  5647. cur = inpL;
  5648. cur = build_norm(cur,
  5649. model.output_norm, NULL,
  5650. LLM_NORM_RMS, -1);
  5651. cb(cur, "result_norm", -1);
  5652. res->t_embd = cur;
  5653. // lm_head
  5654. cur = build_lora_mm(model.output, cur);
  5655. cb(cur, "result_output", -1);
  5656. res->t_logits = cur;
  5657. ggml_build_forward_expand(gf, cur);
  5658. }
  5659. };
  5660. struct llm_build_llama_iswa : public llm_graph_context {
  5661. llm_build_llama_iswa(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  5662. const int64_t n_embd_head = hparams.n_embd_head_v;
  5663. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  5664. GGML_ASSERT(n_embd_head == hparams.n_rot);
  5665. ggml_tensor * cur;
  5666. ggml_tensor * inpL;
  5667. inpL = build_inp_embd(model.tok_embd);
  5668. // inp_pos - contains the positions
  5669. ggml_tensor * inp_pos = build_inp_pos();
  5670. // temperature tuning
  5671. ggml_tensor * inp_attn_scale = nullptr;
  5672. inp_attn_scale = build_inp_attn_scale();
  5673. auto * inp_attn = build_attn_inp_kv_iswa();
  5674. const float kq_scale = hparams.f_attention_scale == 0.0f ? 1.0f/sqrtf(float(n_embd_head)) : hparams.f_attention_scale;
  5675. ggml_tensor * inp_out_ids = build_inp_out_ids();
  5676. for (int il = 0; il < n_layer; ++il) {
  5677. ggml_tensor * inpSA = inpL;
  5678. const bool use_rope = hparams.n_no_rope_layer_step > 0 &&
  5679. (il + 1) % hparams.n_no_rope_layer_step != 0;
  5680. // norm
  5681. cur = build_norm(inpL,
  5682. model.layers[il].attn_norm, NULL,
  5683. LLM_NORM_RMS, il);
  5684. cb(cur, "attn_norm", il);
  5685. // self-attention
  5686. {
  5687. // rope freq factors for llama3; may return nullptr for llama2 and other models
  5688. ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
  5689. // compute Q and K and RoPE them
  5690. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  5691. cb(Qcur, "Qcur", il);
  5692. if (model.layers[il].bq) {
  5693. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  5694. cb(Qcur, "Qcur", il);
  5695. }
  5696. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  5697. cb(Kcur, "Kcur", il);
  5698. if (model.layers[il].bk) {
  5699. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  5700. cb(Kcur, "Kcur", il);
  5701. }
  5702. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  5703. cb(Vcur, "Vcur", il);
  5704. if (model.layers[il].bv) {
  5705. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  5706. cb(Vcur, "Vcur", il);
  5707. }
  5708. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  5709. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  5710. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  5711. if (use_rope) {
  5712. Qcur = ggml_rope_ext(
  5713. ctx0, Qcur, inp_pos, rope_factors,
  5714. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  5715. ext_factor, attn_factor, beta_fast, beta_slow
  5716. );
  5717. Kcur = ggml_rope_ext(
  5718. ctx0, Kcur, inp_pos, rope_factors,
  5719. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  5720. ext_factor, attn_factor, beta_fast, beta_slow
  5721. );
  5722. } else if (inp_attn_scale) {
  5723. Qcur = ggml_mul(ctx0, Qcur, inp_attn_scale);
  5724. }
  5725. cb(Qcur, "Qcur", il);
  5726. cb(Kcur, "Kcur", il);
  5727. cb(Vcur, "Vcur", il);
  5728. if (use_rope && hparams.use_kq_norm) {
  5729. // Llama4TextL2Norm
  5730. Qcur = ggml_rms_norm(ctx0, Qcur, hparams.f_norm_rms_eps);
  5731. Kcur = ggml_rms_norm(ctx0, Kcur, hparams.f_norm_rms_eps);
  5732. cb(Qcur, "Qcur_normed", il);
  5733. cb(Kcur, "Kcur_normed", il);
  5734. }
  5735. cur = build_attn(inp_attn,
  5736. model.layers[il].wo, model.layers[il].bo,
  5737. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
  5738. cb(cur, "attn_out", il);
  5739. }
  5740. if (il == n_layer - 1 && inp_out_ids) {
  5741. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  5742. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  5743. }
  5744. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  5745. cb(ffn_inp, "ffn_inp", il);
  5746. // feed-forward network (non-MoE)
  5747. if (model.layers[il].ffn_gate_inp == nullptr) {
  5748. cur = build_norm(ffn_inp,
  5749. model.layers[il].ffn_norm, NULL,
  5750. LLM_NORM_RMS, il);
  5751. cb(cur, "ffn_norm", il);
  5752. cur = build_ffn(cur,
  5753. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  5754. model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, NULL,
  5755. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  5756. NULL,
  5757. LLM_FFN_SILU, LLM_FFN_PAR, il);
  5758. cb(cur, "ffn_out", il);
  5759. } else {
  5760. ggml_tensor * ffn_inp_normed = build_norm(ffn_inp,
  5761. model.layers[il].ffn_norm, NULL,
  5762. LLM_NORM_RMS, il);
  5763. cb(cur, "ffn_norm", il);
  5764. ggml_tensor * moe_out = build_moe_ffn(ffn_inp_normed,
  5765. model.layers[il].ffn_gate_inp,
  5766. model.layers[il].ffn_up_exps,
  5767. model.layers[il].ffn_gate_exps,
  5768. model.layers[il].ffn_down_exps,
  5769. nullptr,
  5770. n_expert, n_expert_used,
  5771. LLM_FFN_SILU, false,
  5772. false, 0.0,
  5773. LLAMA_EXPERT_GATING_FUNC_TYPE_SIGMOID,
  5774. il);
  5775. // Shared experts
  5776. ggml_tensor * shexp_out = build_ffn(ffn_inp_normed,
  5777. model.layers[il].ffn_up_shexp, NULL, NULL,
  5778. model.layers[il].ffn_gate_shexp, NULL, NULL,
  5779. model.layers[il].ffn_down_shexp, NULL, NULL,
  5780. NULL,
  5781. LLM_FFN_SILU, LLM_FFN_PAR, il);
  5782. cb(shexp_out, "ffn_moe_shexp", il);
  5783. cur = ggml_add(ctx0, moe_out, shexp_out);
  5784. cb(cur, "ffn_moe_out_merged", il);
  5785. }
  5786. cur = ggml_add(ctx0, cur, ffn_inp);
  5787. cb(cur, "ffn_out", il);
  5788. cur = build_cvec(cur, il);
  5789. cb(cur, "l_out", il);
  5790. // input for next layer
  5791. inpL = cur;
  5792. }
  5793. cur = inpL;
  5794. cur = build_norm(cur,
  5795. model.output_norm, NULL,
  5796. LLM_NORM_RMS, -1);
  5797. cb(cur, "result_norm", -1);
  5798. res->t_embd = cur;
  5799. // lm_head
  5800. cur = build_lora_mm(model.output, cur);
  5801. cb(cur, "result_output", -1);
  5802. res->t_logits = cur;
  5803. ggml_build_forward_expand(gf, cur);
  5804. }
  5805. };
  5806. struct llm_build_deci : public llm_graph_context {
  5807. llm_build_deci(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  5808. const int64_t n_embd_head = hparams.n_embd_head_v;
  5809. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  5810. GGML_ASSERT(n_embd_head == hparams.n_rot);
  5811. ggml_tensor * cur;
  5812. ggml_tensor * inpL;
  5813. inpL = build_inp_embd(model.tok_embd);
  5814. // inp_pos - contains the positions
  5815. ggml_tensor * inp_pos = build_inp_pos();
  5816. auto * inp_attn = build_attn_inp_kv();
  5817. const float kq_scale = hparams.f_attention_scale == 0.0f ? 1.0f/sqrtf(float(n_embd_head)) : hparams.f_attention_scale;
  5818. ggml_tensor * inp_out_ids = build_inp_out_ids();
  5819. for (int il = 0; il < n_layer; ++il) {
  5820. ggml_tensor * inpSA = inpL;
  5821. const int64_t n_head_kv = hparams.n_head_kv(il);
  5822. const int64_t n_head = hparams.n_head(il);
  5823. const int64_t n_ff = hparams.n_ff(il);
  5824. if (n_head == 0) {
  5825. // attention-free layer of Llama-3_1-Nemotron-51B
  5826. cur = inpL;
  5827. } else {
  5828. // norm
  5829. cur = build_norm(inpL,
  5830. model.layers[il].attn_norm, NULL,
  5831. LLM_NORM_RMS, il);
  5832. cb(cur, "attn_norm", il);
  5833. }
  5834. if (n_head > 0 && n_head_kv == 0) {
  5835. // "linear attention" of Llama-3_1-Nemotron-51B
  5836. cur = build_lora_mm(model.layers[il].wo, cur);
  5837. cb(cur, "wo", il);
  5838. } else if (n_head > 0) {
  5839. // self-attention
  5840. // rope freq factors for llama3; may return nullptr for llama2 and other models
  5841. ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
  5842. // compute Q and K and RoPE them
  5843. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  5844. cb(Qcur, "Qcur", il);
  5845. if (model.layers[il].bq) {
  5846. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  5847. cb(Qcur, "Qcur", il);
  5848. }
  5849. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  5850. cb(Kcur, "Kcur", il);
  5851. if (model.layers[il].bk) {
  5852. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  5853. cb(Kcur, "Kcur", il);
  5854. }
  5855. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  5856. cb(Vcur, "Vcur", il);
  5857. if (model.layers[il].bv) {
  5858. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  5859. cb(Vcur, "Vcur", il);
  5860. }
  5861. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  5862. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  5863. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  5864. Qcur = ggml_rope_ext(
  5865. ctx0, Qcur, inp_pos, rope_factors,
  5866. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  5867. ext_factor, attn_factor, beta_fast, beta_slow
  5868. );
  5869. Kcur = ggml_rope_ext(
  5870. ctx0, Kcur, inp_pos, rope_factors,
  5871. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  5872. ext_factor, attn_factor, beta_fast, beta_slow
  5873. );
  5874. cb(Qcur, "Qcur", il);
  5875. cb(Kcur, "Kcur", il);
  5876. cb(Vcur, "Vcur", il);
  5877. cur = build_attn(inp_attn,
  5878. model.layers[il].wo, model.layers[il].bo,
  5879. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
  5880. }
  5881. if (il == n_layer - 1 && inp_out_ids) {
  5882. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  5883. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  5884. }
  5885. // FFN-free layer of Llama-3_1-Nemotron-Ultra-253B
  5886. if (n_ff == 0) {
  5887. continue;
  5888. }
  5889. // modified to support attention-free layer of Llama-3_1-Nemotron-51B
  5890. ggml_tensor * ffn_inp = cur;
  5891. if (n_head > 0) {
  5892. ffn_inp = ggml_add(ctx0, cur, inpSA);
  5893. cb(ffn_inp, "ffn_inp", il);
  5894. }
  5895. // feed-forward network
  5896. if (model.layers[il].ffn_gate_inp == nullptr) {
  5897. cur = build_norm(ffn_inp,
  5898. model.layers[il].ffn_norm, NULL,
  5899. LLM_NORM_RMS, il);
  5900. cb(cur, "ffn_norm", il);
  5901. cur = build_ffn(cur,
  5902. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  5903. model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, NULL,
  5904. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  5905. NULL,
  5906. LLM_FFN_SILU, LLM_FFN_PAR, il);
  5907. cb(cur, "ffn_out", il);
  5908. }
  5909. cur = ggml_add(ctx0, cur, ffn_inp);
  5910. cb(cur, "ffn_out", il);
  5911. cur = build_cvec(cur, il);
  5912. cb(cur, "l_out", il);
  5913. // input for next layer
  5914. inpL = cur;
  5915. }
  5916. cur = inpL;
  5917. cur = build_norm(cur,
  5918. model.output_norm, NULL,
  5919. LLM_NORM_RMS, -1);
  5920. cb(cur, "result_norm", -1);
  5921. res->t_embd = cur;
  5922. // lm_head
  5923. cur = build_lora_mm(model.output, cur);
  5924. cb(cur, "result_output", -1);
  5925. res->t_logits = cur;
  5926. ggml_build_forward_expand(gf, cur);
  5927. }
  5928. };
  5929. struct llm_build_baichuan : public llm_graph_context {
  5930. llm_build_baichuan(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  5931. const int64_t n_embd_head = hparams.n_embd_head_v;
  5932. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  5933. GGML_ASSERT(n_embd_head == hparams.n_rot);
  5934. ggml_tensor * cur;
  5935. ggml_tensor * inpL;
  5936. inpL = build_inp_embd(model.tok_embd);
  5937. // inp_pos - contains the positions
  5938. ggml_tensor * inp_pos = model.type == LLM_TYPE_7B ? build_inp_pos() : nullptr;
  5939. auto * inp_attn = build_attn_inp_kv();
  5940. ggml_tensor * inp_out_ids = build_inp_out_ids();
  5941. for (int il = 0; il < n_layer; ++il) {
  5942. ggml_tensor * inpSA = inpL;
  5943. cur = build_norm(inpL,
  5944. model.layers[il].attn_norm, NULL,
  5945. LLM_NORM_RMS, il);
  5946. cb(cur, "attn_norm", il);
  5947. // self-attention
  5948. {
  5949. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  5950. cb(Qcur, "Qcur", il);
  5951. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  5952. cb(Kcur, "Kcur", il);
  5953. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  5954. cb(Vcur, "Vcur", il);
  5955. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  5956. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  5957. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  5958. switch (model.type) {
  5959. case LLM_TYPE_7B:
  5960. Qcur = ggml_rope_ext(
  5961. ctx0, Qcur, inp_pos, nullptr,
  5962. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  5963. ext_factor, attn_factor, beta_fast, beta_slow
  5964. );
  5965. Kcur = ggml_rope_ext(
  5966. ctx0, Kcur, inp_pos, nullptr,
  5967. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  5968. ext_factor, attn_factor, beta_fast, beta_slow
  5969. );
  5970. break;
  5971. case LLM_TYPE_13B:
  5972. break;
  5973. default:
  5974. GGML_ABORT("fatal error");
  5975. }
  5976. cb(Qcur, "Qcur", il);
  5977. cb(Kcur, "Kcur", il);
  5978. cb(Vcur, "Vcur", il);
  5979. cur = build_attn(inp_attn,
  5980. model.layers[il].wo, NULL,
  5981. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  5982. }
  5983. if (il == n_layer - 1 && inp_out_ids) {
  5984. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  5985. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  5986. }
  5987. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  5988. cb(ffn_inp, "ffn_inp", il);
  5989. // feed-forward network
  5990. {
  5991. cur = build_norm(ffn_inp,
  5992. model.layers[il].ffn_norm, NULL,
  5993. LLM_NORM_RMS, il);
  5994. cb(cur, "ffn_norm", il);
  5995. cur = build_ffn(cur,
  5996. model.layers[il].ffn_up, NULL, NULL,
  5997. model.layers[il].ffn_gate, NULL, NULL,
  5998. model.layers[il].ffn_down, NULL, NULL,
  5999. NULL,
  6000. LLM_FFN_SILU, LLM_FFN_PAR, il);
  6001. cb(cur, "ffn_out", il);
  6002. }
  6003. cur = ggml_add(ctx0, cur, ffn_inp);
  6004. cur = build_cvec(cur, il);
  6005. cb(cur, "l_out", il);
  6006. // input for next layer
  6007. inpL = cur;
  6008. }
  6009. cur = inpL;
  6010. cur = build_norm(cur,
  6011. model.output_norm, NULL,
  6012. LLM_NORM_RMS, -1);
  6013. cb(cur, "result_norm", -1);
  6014. res->t_embd = cur;
  6015. // lm_head
  6016. cur = build_lora_mm(model.output, cur);
  6017. cb(cur, "result_output", -1);
  6018. res->t_logits = cur;
  6019. ggml_build_forward_expand(gf, cur);
  6020. }
  6021. };
  6022. struct llm_build_xverse : public llm_graph_context {
  6023. llm_build_xverse(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  6024. const int64_t n_embd_head = hparams.n_embd_head_v;
  6025. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  6026. GGML_ASSERT(n_embd_head == hparams.n_rot);
  6027. ggml_tensor * cur;
  6028. ggml_tensor * inpL;
  6029. inpL = build_inp_embd(model.tok_embd);
  6030. // inp_pos - contains the positions
  6031. ggml_tensor * inp_pos = build_inp_pos();
  6032. auto * inp_attn = build_attn_inp_kv();
  6033. ggml_tensor * inp_out_ids = build_inp_out_ids();
  6034. for (int il = 0; il < n_layer; ++il) {
  6035. ggml_tensor * inpSA = inpL;
  6036. cur = build_norm(inpL,
  6037. model.layers[il].attn_norm, NULL,
  6038. LLM_NORM_RMS, il);
  6039. cb(cur, "attn_norm", il);
  6040. // self-attention
  6041. {
  6042. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  6043. cb(Qcur, "Qcur", il);
  6044. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  6045. cb(Kcur, "Kcur", il);
  6046. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  6047. cb(Vcur, "Vcur", il);
  6048. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  6049. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  6050. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  6051. Qcur = ggml_rope_ext(
  6052. ctx0, Qcur, inp_pos, nullptr,
  6053. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  6054. ext_factor, attn_factor, beta_fast, beta_slow
  6055. );
  6056. Kcur = ggml_rope_ext(
  6057. ctx0, Kcur, inp_pos, nullptr,
  6058. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  6059. ext_factor, attn_factor, beta_fast, beta_slow
  6060. );
  6061. cb(Qcur, "Qcur", il);
  6062. cb(Kcur, "Kcur", il);
  6063. cb(Vcur, "Vcur", il);
  6064. cur = build_attn(inp_attn,
  6065. model.layers[il].wo, NULL,
  6066. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  6067. }
  6068. if (il == n_layer - 1 && inp_out_ids) {
  6069. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  6070. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  6071. }
  6072. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  6073. cb(ffn_inp, "ffn_inp", il);
  6074. // feed-forward network
  6075. {
  6076. cur = build_norm(ffn_inp,
  6077. model.layers[il].ffn_norm, NULL,
  6078. LLM_NORM_RMS, il);
  6079. cb(cur, "ffn_norm", il);
  6080. cur = build_ffn(cur,
  6081. model.layers[il].ffn_up, NULL, NULL,
  6082. model.layers[il].ffn_gate, NULL, NULL,
  6083. model.layers[il].ffn_down, NULL, NULL,
  6084. NULL,
  6085. LLM_FFN_SILU, LLM_FFN_PAR, il);
  6086. cb(cur, "ffn_out", il);
  6087. }
  6088. cur = ggml_add(ctx0, cur, ffn_inp);
  6089. cur = build_cvec(cur, il);
  6090. cb(cur, "l_out", il);
  6091. // input for next layer
  6092. inpL = cur;
  6093. }
  6094. cur = inpL;
  6095. cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1);
  6096. cb(cur, "result_norm", -1);
  6097. res->t_embd = cur;
  6098. // lm_head
  6099. cur = build_lora_mm(model.output, cur);
  6100. cb(cur, "result_output", -1);
  6101. res->t_logits = cur;
  6102. ggml_build_forward_expand(gf, cur);
  6103. }
  6104. };
  6105. struct llm_build_falcon : public llm_graph_context {
  6106. llm_build_falcon(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  6107. const int64_t n_embd_head = hparams.n_embd_head_v;
  6108. const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  6109. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  6110. GGML_ASSERT(n_embd_head == hparams.n_rot);
  6111. ggml_tensor * cur;
  6112. ggml_tensor * inpL;
  6113. inpL = build_inp_embd(model.tok_embd);
  6114. // inp_pos - contains the positions
  6115. ggml_tensor * inp_pos = build_inp_pos();
  6116. auto * inp_attn = build_attn_inp_kv();
  6117. ggml_tensor * inp_out_ids = build_inp_out_ids();
  6118. for (int il = 0; il < n_layer; ++il) {
  6119. ggml_tensor * attn_norm;
  6120. attn_norm = build_norm(inpL,
  6121. model.layers[il].attn_norm,
  6122. model.layers[il].attn_norm_b,
  6123. LLM_NORM, il);
  6124. cb(attn_norm, "attn_norm", il);
  6125. // self-attention
  6126. {
  6127. if (model.layers[il].attn_norm_2) {
  6128. // Falcon-40B
  6129. cur = build_norm(inpL,
  6130. model.layers[il].attn_norm_2,
  6131. model.layers[il].attn_norm_2_b,
  6132. LLM_NORM, il);
  6133. cb(cur, "attn_norm_2", il);
  6134. } else {
  6135. cur = attn_norm;
  6136. }
  6137. cur = build_lora_mm(model.layers[il].wqkv, cur);
  6138. cb(cur, "wqkv", il);
  6139. 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));
  6140. 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));
  6141. 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));
  6142. // using mode = 2 for neox mode
  6143. Qcur = ggml_rope_ext(
  6144. ctx0, Qcur, inp_pos, nullptr,
  6145. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  6146. ext_factor, attn_factor, beta_fast, beta_slow
  6147. );
  6148. Kcur = ggml_rope_ext(
  6149. ctx0, Kcur, inp_pos, nullptr,
  6150. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  6151. ext_factor, attn_factor, beta_fast, beta_slow
  6152. );
  6153. cb(Qcur, "Qcur", il);
  6154. cb(Kcur, "Kcur", il);
  6155. cb(Vcur, "Vcur", il);
  6156. cur = build_attn(inp_attn,
  6157. model.layers[il].wo, NULL,
  6158. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  6159. }
  6160. if (il == n_layer - 1 && inp_out_ids) {
  6161. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  6162. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  6163. attn_norm = ggml_get_rows(ctx0, attn_norm, inp_out_ids);
  6164. }
  6165. ggml_tensor * ffn_inp = cur;
  6166. // feed forward
  6167. {
  6168. cur = build_ffn(attn_norm, // !! use the attn norm, not the result
  6169. model.layers[il].ffn_up, NULL, NULL,
  6170. NULL, NULL, NULL,
  6171. model.layers[il].ffn_down, NULL, NULL,
  6172. NULL,
  6173. LLM_FFN_GELU, LLM_FFN_SEQ, il);
  6174. cb(cur, "ffn_out", il);
  6175. }
  6176. cur = ggml_add(ctx0, cur, ffn_inp);
  6177. cur = ggml_add(ctx0, cur, inpL);
  6178. cur = build_cvec(cur, il);
  6179. cb(cur, "l_out", il);
  6180. // input for next layer
  6181. inpL = cur;
  6182. }
  6183. cur = inpL;
  6184. // norm
  6185. cur = build_norm(cur,
  6186. model.output_norm,
  6187. model.output_norm_b,
  6188. LLM_NORM, -1);
  6189. cb(cur, "result_norm", -1);
  6190. res->t_embd = cur;
  6191. cur = build_lora_mm(model.output, cur);
  6192. cb(cur, "result_output", -1);
  6193. res->t_logits = cur;
  6194. ggml_build_forward_expand(gf, cur);
  6195. }
  6196. };
  6197. struct llm_build_grok : public llm_graph_context {
  6198. llm_build_grok(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  6199. const int64_t n_embd_head = hparams.n_embd_head_v;
  6200. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  6201. GGML_ASSERT(n_embd_head == hparams.n_rot);
  6202. ggml_tensor * cur;
  6203. ggml_tensor * inpL;
  6204. inpL = build_inp_embd(model.tok_embd);
  6205. // inp_pos - contains the positions
  6206. ggml_tensor * inp_pos = build_inp_pos();
  6207. auto * inp_attn = build_attn_inp_kv();
  6208. ggml_tensor * inp_out_ids = build_inp_out_ids();
  6209. for (int il = 0; il < n_layer; ++il) {
  6210. ggml_tensor * inpSA = inpL;
  6211. // norm
  6212. cur = build_norm(inpL,
  6213. model.layers[il].attn_norm, NULL,
  6214. LLM_NORM_RMS, il);
  6215. cb(cur, "attn_norm", il);
  6216. // self-attention
  6217. {
  6218. // compute Q and K and RoPE them
  6219. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  6220. cb(Qcur, "Qcur", il);
  6221. if (model.layers[il].bq) {
  6222. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  6223. cb(Qcur, "Qcur", il);
  6224. }
  6225. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  6226. cb(Kcur, "Kcur", il);
  6227. if (model.layers[il].bk) {
  6228. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  6229. cb(Kcur, "Kcur", il);
  6230. }
  6231. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  6232. cb(Vcur, "Vcur", il);
  6233. if (model.layers[il].bv) {
  6234. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  6235. cb(Vcur, "Vcur", il);
  6236. }
  6237. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  6238. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  6239. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  6240. Qcur = ggml_rope_ext(
  6241. ctx0, Qcur, inp_pos, nullptr,
  6242. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  6243. ext_factor, attn_factor, beta_fast, beta_slow
  6244. );
  6245. Kcur = ggml_rope_ext(
  6246. ctx0, Kcur, inp_pos, nullptr,
  6247. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  6248. ext_factor, attn_factor, beta_fast, beta_slow
  6249. );
  6250. cb(Qcur, "Qcur", il);
  6251. cb(Kcur, "Kcur", il);
  6252. cb(Vcur, "Vcur", il);
  6253. cur = build_attn(inp_attn,
  6254. model.layers[il].wo, model.layers[il].bo,
  6255. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f, il);
  6256. }
  6257. if (il == n_layer - 1 && inp_out_ids) {
  6258. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  6259. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  6260. }
  6261. cur = build_norm(cur,
  6262. model.layers[il].attn_out_norm, NULL,
  6263. LLM_NORM_RMS, il);
  6264. cb(cur, "attn_out_norm", il);
  6265. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  6266. cb(ffn_inp, "ffn_inp", il);
  6267. // feed-forward network
  6268. cur = build_norm(ffn_inp,
  6269. model.layers[il].ffn_norm, NULL,
  6270. LLM_NORM_RMS, il);
  6271. cb(cur, "ffn_norm", il);
  6272. // MoE branch
  6273. ggml_tensor * moe_out = build_moe_ffn(cur,
  6274. model.layers[il].ffn_gate_inp,
  6275. model.layers[il].ffn_up_exps,
  6276. model.layers[il].ffn_gate_exps,
  6277. model.layers[il].ffn_down_exps,
  6278. nullptr,
  6279. n_expert, n_expert_used,
  6280. LLM_FFN_GELU, true,
  6281. false, 0.0,
  6282. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  6283. il);
  6284. cb(moe_out, "ffn_moe_out", il);
  6285. if (model.layers[il].ffn_up) {
  6286. ggml_tensor * ffn_out = build_ffn(cur,
  6287. model.layers[il].ffn_up, NULL, NULL,
  6288. model.layers[il].ffn_gate, NULL, NULL,
  6289. model.layers[il].ffn_down, NULL, NULL,
  6290. NULL,
  6291. LLM_FFN_GELU, LLM_FFN_PAR, il);
  6292. cb(ffn_out, "ffn_out", il);
  6293. cur = ggml_scale(ctx0, ggml_add(ctx0, ffn_out, moe_out), std::sqrt(2) / 2);
  6294. cb(cur, "ffn_out", il);
  6295. } else {
  6296. cur = moe_out;
  6297. }
  6298. cur = build_norm(cur,
  6299. model.layers[il].ffn_post_norm, NULL,
  6300. LLM_NORM_RMS, il);
  6301. cb(cur, "ffn_post_norm", il);
  6302. cur = ggml_add(ctx0, cur, ffn_inp);
  6303. cb(cur, "ffn_out", il);
  6304. cur = build_cvec(cur, il);
  6305. cb(cur, "l_out", il);
  6306. // input for next layer
  6307. inpL = cur;
  6308. }
  6309. cur = inpL;
  6310. cur = build_norm(cur,
  6311. model.output_norm, NULL,
  6312. LLM_NORM_RMS, -1);
  6313. cb(cur, "result_norm", -1);
  6314. res->t_embd = cur;
  6315. // lm_head
  6316. cur = build_lora_mm(model.output, cur);
  6317. cur = ggml_scale(ctx0, cur, hparams.f_logit_scale);
  6318. // final logit soft-capping
  6319. if (hparams.f_final_logit_softcapping) {
  6320. cur = ggml_scale(ctx0, cur, 1.0f / hparams.f_final_logit_softcapping);
  6321. cur = ggml_tanh(ctx0, cur);
  6322. cur = ggml_scale(ctx0, cur, hparams.f_final_logit_softcapping);
  6323. }
  6324. cb(cur, "result_output", -1);
  6325. res->t_logits = cur;
  6326. ggml_build_forward_expand(gf, cur);
  6327. }
  6328. };
  6329. struct llm_build_dbrx : public llm_graph_context {
  6330. llm_build_dbrx(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  6331. const int64_t n_embd_head = hparams.n_embd_head_v;
  6332. const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  6333. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  6334. GGML_ASSERT(n_embd_head == hparams.n_rot);
  6335. ggml_tensor * cur;
  6336. ggml_tensor * inpL;
  6337. inpL = build_inp_embd(model.tok_embd);
  6338. // inp_pos - contains the positions
  6339. ggml_tensor * inp_pos = build_inp_pos();
  6340. auto * inp_attn = build_attn_inp_kv();
  6341. ggml_tensor * inp_out_ids = build_inp_out_ids();
  6342. for (int il = 0; il < n_layer; ++il) {
  6343. ggml_tensor * inpSA = inpL;
  6344. // norm
  6345. cur = build_norm(inpL,
  6346. model.layers[il].attn_norm, NULL,
  6347. LLM_NORM, il);
  6348. cb(cur, "attn_norm", il);
  6349. // self-attention
  6350. {
  6351. ggml_tensor * Qcur = nullptr;
  6352. ggml_tensor * Kcur = nullptr;
  6353. ggml_tensor * Vcur = nullptr;
  6354. cur = build_lora_mm(model.layers[il].wqkv, cur);
  6355. cb(cur, "wqkv", il);
  6356. cur = ggml_clamp(ctx0, cur, -hparams.f_clamp_kqv, hparams.f_clamp_kqv);
  6357. cb(cur, "wqkv_clamped", il);
  6358. 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));
  6359. 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));
  6360. 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));
  6361. Qcur = ggml_rope_ext(
  6362. ctx0, Qcur, inp_pos, nullptr,
  6363. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  6364. ext_factor, attn_factor, beta_fast, beta_slow
  6365. );
  6366. Kcur = ggml_rope_ext(
  6367. ctx0, Kcur, inp_pos, nullptr,
  6368. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  6369. ext_factor, attn_factor, beta_fast, beta_slow
  6370. );
  6371. cb(Qcur, "Qcur", il);
  6372. cb(Kcur, "Kcur", il);
  6373. cb(Vcur, "Vcur", il);
  6374. cur = build_attn(inp_attn,
  6375. model.layers[il].wo, NULL,
  6376. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  6377. }
  6378. if (il == n_layer - 1 && inp_out_ids) {
  6379. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  6380. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  6381. }
  6382. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  6383. cb(ffn_inp, "ffn_inp", il);
  6384. // feed-forward network
  6385. // MoE branch
  6386. cur = build_norm(ffn_inp,
  6387. model.layers[il].attn_out_norm, NULL,
  6388. LLM_NORM, il);
  6389. cb(cur, "attn_out_norm", il);
  6390. cur = build_moe_ffn(cur,
  6391. model.layers[il].ffn_gate_inp,
  6392. model.layers[il].ffn_up_exps,
  6393. model.layers[il].ffn_gate_exps,
  6394. model.layers[il].ffn_down_exps,
  6395. nullptr,
  6396. n_expert, n_expert_used,
  6397. LLM_FFN_SILU, true,
  6398. false, 0.0,
  6399. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  6400. il);
  6401. cb(cur, "ffn_moe_out", il);
  6402. cur = ggml_add(ctx0, cur, ffn_inp);
  6403. cb(cur, "ffn_out", il);
  6404. cur = build_cvec(cur, il);
  6405. cb(cur, "l_out", il);
  6406. // input for next layer
  6407. inpL = cur;
  6408. }
  6409. cur = inpL;
  6410. cur = build_norm(cur,
  6411. model.output_norm, NULL,
  6412. LLM_NORM, -1);
  6413. cb(cur, "result_norm", -1);
  6414. res->t_embd = cur;
  6415. // lm_head
  6416. cur = build_lora_mm(model.output, cur);
  6417. cb(cur, "result_output", -1);
  6418. res->t_logits = cur;
  6419. ggml_build_forward_expand(gf, cur);
  6420. }
  6421. };
  6422. struct llm_build_starcoder : public llm_graph_context {
  6423. llm_build_starcoder(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  6424. const int64_t n_embd_head = hparams.n_embd_head_v;
  6425. const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  6426. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  6427. ggml_tensor * cur;
  6428. ggml_tensor * inpL;
  6429. inpL = build_inp_embd(model.tok_embd);
  6430. // inp_pos - contains the positions
  6431. ggml_tensor * inp_pos = build_inp_pos();
  6432. auto * inp_attn = build_attn_inp_kv();
  6433. ggml_tensor * pos = ggml_get_rows(ctx0, model.pos_embd, inp_pos);
  6434. cb(pos, "pos_embd", -1);
  6435. inpL = ggml_add(ctx0, inpL, pos);
  6436. cb(inpL, "inpL", -1);
  6437. ggml_tensor * inp_out_ids = build_inp_out_ids();
  6438. for (int il = 0; il < n_layer; ++il) {
  6439. cur = build_norm(inpL,
  6440. model.layers[il].attn_norm,
  6441. model.layers[il].attn_norm_b,
  6442. LLM_NORM, il);
  6443. cb(cur, "attn_norm", il);
  6444. // self-attention
  6445. {
  6446. cur = build_lora_mm(model.layers[il].wqkv, cur);
  6447. cb(cur, "wqkv", il);
  6448. cur = ggml_add(ctx0, cur, model.layers[il].bqkv);
  6449. cb(cur, "bqkv", il);
  6450. 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));
  6451. 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));
  6452. 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));
  6453. cb(Qcur, "Qcur", il);
  6454. cb(Kcur, "Kcur", il);
  6455. cb(Vcur, "Vcur", il);
  6456. cur = build_attn(inp_attn,
  6457. model.layers[il].wo, model.layers[il].bo,
  6458. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  6459. }
  6460. if (il == n_layer - 1 && inp_out_ids) {
  6461. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  6462. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  6463. }
  6464. // add the input
  6465. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
  6466. cb(ffn_inp, "ffn_inp", il);
  6467. // FF
  6468. {
  6469. cur = build_norm(ffn_inp,
  6470. model.layers[il].ffn_norm,
  6471. model.layers[il].ffn_norm_b,
  6472. LLM_NORM, il);
  6473. cb(cur, "ffn_norm", il);
  6474. cur = build_ffn(cur,
  6475. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  6476. NULL, NULL, NULL,
  6477. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  6478. NULL,
  6479. LLM_FFN_GELU, LLM_FFN_SEQ, il);
  6480. cb(cur, "ffn_out", il);
  6481. }
  6482. cur = ggml_add(ctx0, cur, ffn_inp);
  6483. cur = build_cvec(cur, il);
  6484. cb(cur, "l_out", il);
  6485. // input for next layer
  6486. inpL = cur;
  6487. }
  6488. cur = build_norm(inpL,
  6489. model.output_norm,
  6490. model.output_norm_b,
  6491. LLM_NORM, -1);
  6492. cb(cur, "result_norm", -1);
  6493. res->t_embd = cur;
  6494. cur = build_lora_mm(model.output, cur);
  6495. cb(cur, "result_output", -1);
  6496. res->t_logits = cur;
  6497. ggml_build_forward_expand(gf, cur);
  6498. }
  6499. };
  6500. struct llm_build_refact : public llm_graph_context {
  6501. llm_build_refact(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  6502. const int64_t n_embd_head = hparams.n_embd_head_v;
  6503. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  6504. ggml_tensor * cur;
  6505. ggml_tensor * inpL;
  6506. inpL = build_inp_embd(model.tok_embd);
  6507. auto * inp_attn = build_attn_inp_kv();
  6508. ggml_tensor * inp_out_ids = build_inp_out_ids();
  6509. for (int il = 0; il < n_layer; ++il) {
  6510. ggml_tensor * inpSA = inpL;
  6511. cur = build_norm(inpL,
  6512. model.layers[il].attn_norm, NULL,
  6513. LLM_NORM_RMS, il);
  6514. cb(cur, "attn_norm", il);
  6515. // self-attention
  6516. {
  6517. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  6518. cb(Qcur, "Qcur", il);
  6519. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  6520. cb(Kcur, "Kcur", il);
  6521. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  6522. cb(Vcur, "Vcur", il);
  6523. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  6524. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  6525. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  6526. cb(Qcur, "Qcur", il);
  6527. cb(Kcur, "Kcur", il);
  6528. cb(Vcur, "Vcur", il);
  6529. cur = build_attn(inp_attn,
  6530. model.layers[il].wo, NULL,
  6531. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  6532. }
  6533. if (il == n_layer - 1 && inp_out_ids) {
  6534. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  6535. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  6536. }
  6537. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  6538. cb(ffn_inp, "ffn_inp", il);
  6539. // feed-forward network
  6540. {
  6541. cur = build_norm(ffn_inp,
  6542. model.layers[il].ffn_norm, NULL,
  6543. LLM_NORM_RMS, il);
  6544. cb(cur, "ffn_norm", il);
  6545. cur = build_ffn(cur,
  6546. model.layers[il].ffn_up, NULL, NULL,
  6547. model.layers[il].ffn_gate, NULL, NULL,
  6548. model.layers[il].ffn_down, NULL, NULL,
  6549. NULL,
  6550. LLM_FFN_SILU, LLM_FFN_PAR, il);
  6551. cb(cur, "ffn_out", il);
  6552. }
  6553. cur = ggml_add(ctx0, cur, ffn_inp);
  6554. cur = build_cvec(cur, il);
  6555. cb(cur, "l_out", il);
  6556. // input for next layer
  6557. inpL = cur;
  6558. }
  6559. cur = inpL;
  6560. cur = build_norm(cur,
  6561. model.output_norm, NULL,
  6562. LLM_NORM_RMS, -1);
  6563. cb(cur, "result_norm", -1);
  6564. res->t_embd = cur;
  6565. // lm_head
  6566. cur = build_lora_mm(model.output, cur);
  6567. cb(cur, "result_output", -1);
  6568. res->t_logits = cur;
  6569. ggml_build_forward_expand(gf, cur);
  6570. }
  6571. };
  6572. struct llm_build_bert : public llm_graph_context {
  6573. llm_build_bert(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  6574. const int64_t n_embd_head = hparams.n_embd_head_v;
  6575. const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  6576. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  6577. ggml_tensor * cur;
  6578. ggml_tensor * inpL;
  6579. ggml_tensor * inp_pos = nullptr;
  6580. if (model.arch != LLM_ARCH_JINA_BERT_V2) {
  6581. inp_pos = build_inp_pos();
  6582. }
  6583. // construct input embeddings (token, type, position)
  6584. inpL = build_inp_embd(model.tok_embd);
  6585. // token types are hardcoded to zero ("Sentence A")
  6586. if (model.type_embd) {
  6587. ggml_tensor * type_row0 = ggml_view_1d(ctx0, model.type_embd, n_embd, 0);
  6588. inpL = ggml_add(ctx0, inpL, type_row0);
  6589. }
  6590. if (model.arch == LLM_ARCH_BERT) {
  6591. inpL = ggml_add(ctx0, ggml_get_rows(ctx0, model.pos_embd, inp_pos), inpL);
  6592. }
  6593. cb(inpL, "inp_embd", -1);
  6594. // embed layer norm
  6595. inpL = build_norm(inpL, model.tok_norm, model.tok_norm_b, LLM_NORM, -1);
  6596. cb(inpL, "inp_norm", -1);
  6597. auto * inp_attn = build_attn_inp_no_cache();
  6598. ggml_tensor * inp_out_ids = build_inp_out_ids();
  6599. for (int il = 0; il < n_layer; ++il) {
  6600. ggml_tensor * cur = inpL;
  6601. {
  6602. ggml_tensor * Qcur;
  6603. ggml_tensor * Kcur;
  6604. ggml_tensor * Vcur;
  6605. // self-attention
  6606. if (model.layers[il].wqkv) {
  6607. cur = build_lora_mm(model.layers[il].wqkv, cur);
  6608. cb(cur, "wqkv", il);
  6609. if (model.layers[il].bqkv) {
  6610. cur = ggml_add(ctx0, cur, model.layers[il].bqkv);
  6611. cb(cur, "bqkv", il);
  6612. }
  6613. 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));
  6614. 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));
  6615. 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));
  6616. } else {
  6617. Qcur = ggml_add(ctx0, build_lora_mm(model.layers[il].wq, cur), model.layers[il].bq);
  6618. Kcur = ggml_add(ctx0, build_lora_mm(model.layers[il].wk, cur), model.layers[il].bk);
  6619. Vcur = ggml_add(ctx0, build_lora_mm(model.layers[il].wv, cur), model.layers[il].bv);
  6620. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  6621. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  6622. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  6623. }
  6624. if (model.layers[il].attn_q_norm) {
  6625. Qcur = ggml_reshape_2d(ctx0, Qcur, n_embd_head*n_head, n_tokens);
  6626. Qcur = build_norm(Qcur,
  6627. model.layers[il].attn_q_norm,
  6628. model.layers[il].attn_q_norm_b,
  6629. LLM_NORM, il);
  6630. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  6631. }
  6632. if (model.layers[il].attn_k_norm) {
  6633. Kcur = ggml_reshape_2d(ctx0, Kcur, n_embd_head*n_head_kv, n_tokens);
  6634. Kcur = build_norm(Kcur,
  6635. model.layers[il].attn_k_norm,
  6636. model.layers[il].attn_k_norm_b,
  6637. LLM_NORM, il);
  6638. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  6639. }
  6640. // RoPE
  6641. if (model.arch == LLM_ARCH_NOMIC_BERT || model.arch == LLM_ARCH_NOMIC_BERT_MOE || model.arch == LLM_ARCH_JINA_BERT_V3) {
  6642. Qcur = ggml_rope_ext(
  6643. ctx0, Qcur, inp_pos, nullptr,
  6644. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  6645. ext_factor, attn_factor, beta_fast, beta_slow
  6646. );
  6647. Kcur = ggml_rope_ext(
  6648. ctx0, Kcur, inp_pos, nullptr,
  6649. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  6650. ext_factor, attn_factor, beta_fast, beta_slow
  6651. );
  6652. }
  6653. cb(Qcur, "Qcur", il);
  6654. cb(Kcur, "Kcur", il);
  6655. cb(Vcur, "Vcur", il);
  6656. cur = build_attn(inp_attn,
  6657. model.layers[il].wo, model.layers[il].bo,
  6658. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  6659. cb(cur, "kqv_out", il);
  6660. }
  6661. if (il == n_layer - 1 && inp_out_ids) {
  6662. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  6663. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  6664. }
  6665. // re-add the layer input
  6666. cur = ggml_add(ctx0, cur, inpL);
  6667. // attention layer norm
  6668. cur = build_norm(cur, model.layers[il].attn_out_norm, model.layers[il].attn_out_norm_b, LLM_NORM, il);
  6669. if (model.layers[il].attn_norm_2 != nullptr) {
  6670. cur = ggml_add(ctx0, cur, inpL); // re-add the layer input
  6671. cur = build_norm(cur, model.layers[il].attn_norm_2, model.layers[il].attn_norm_2_b, LLM_NORM, il);
  6672. }
  6673. ggml_tensor * ffn_inp = cur;
  6674. cb(ffn_inp, "ffn_inp", il);
  6675. // feed-forward network
  6676. if (hparams.moe_every_n_layers > 0 && il % hparams.moe_every_n_layers == 1) {
  6677. // MoE branch
  6678. cur = build_moe_ffn(cur,
  6679. model.layers[il].ffn_gate_inp,
  6680. model.layers[il].ffn_up_exps,
  6681. nullptr,
  6682. model.layers[il].ffn_down_exps,
  6683. nullptr,
  6684. hparams.n_expert,
  6685. hparams.n_expert_used,
  6686. LLM_FFN_GELU,
  6687. false, false,
  6688. 0.0f,
  6689. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX, il);
  6690. cb(cur, "ffn_moe_out", il);
  6691. } else if (model.arch == LLM_ARCH_BERT || model.arch == LLM_ARCH_NOMIC_BERT_MOE || model.arch == LLM_ARCH_JINA_BERT_V3) {
  6692. cur = build_ffn(cur,
  6693. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  6694. NULL, NULL, NULL,
  6695. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  6696. NULL,
  6697. LLM_FFN_GELU, LLM_FFN_SEQ, il);
  6698. cb(cur, "ffn_out", il);
  6699. } else if (model.arch == LLM_ARCH_JINA_BERT_V2) {
  6700. cur = build_ffn(cur,
  6701. model.layers[il].ffn_up, NULL, NULL,
  6702. model.layers[il].ffn_gate, NULL, NULL,
  6703. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  6704. NULL,
  6705. model.layers[il].ffn_gate ? LLM_FFN_GELU : LLM_FFN_GEGLU, LLM_FFN_PAR, il);
  6706. cb(cur, "ffn_out", il);
  6707. } else {
  6708. cur = build_ffn(cur,
  6709. model.layers[il].ffn_up, NULL, NULL,
  6710. model.layers[il].ffn_gate, NULL, NULL,
  6711. model.layers[il].ffn_down, NULL, NULL,
  6712. NULL,
  6713. LLM_FFN_SILU, LLM_FFN_PAR, il);
  6714. cb(cur, "ffn_out", il);
  6715. }
  6716. // attentions bypass the intermediate layer
  6717. cur = ggml_add(ctx0, cur, ffn_inp);
  6718. // output layer norm
  6719. cur = build_norm(cur, model.layers[il].layer_out_norm, model.layers[il].layer_out_norm_b, LLM_NORM, il);
  6720. // input for next layer
  6721. inpL = cur;
  6722. }
  6723. cur = inpL;
  6724. cb(cur, "result_embd", -1);
  6725. res->t_embd = cur;
  6726. ggml_build_forward_expand(gf, cur);
  6727. }
  6728. };
  6729. struct llm_build_neo_bert : public llm_graph_context {
  6730. llm_build_neo_bert(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  6731. const int64_t n_embd_head = hparams.n_embd_head_v;
  6732. const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  6733. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  6734. ggml_tensor * cur;
  6735. ggml_tensor * inpL;
  6736. ggml_tensor * inp_pos = build_inp_pos();
  6737. // construct input embeddings (token, type, position)
  6738. inpL = build_inp_embd(model.tok_embd);
  6739. cb(inpL, "inp_embd", -1);
  6740. auto * inp_attn = build_attn_inp_no_cache();
  6741. ggml_tensor * inp_out_ids = build_inp_out_ids();
  6742. for (int il = 0; il < n_layer; ++il) {
  6743. ggml_tensor * cur = inpL;
  6744. // pre-norm
  6745. cur = build_norm(inpL,
  6746. model.layers[il].attn_norm, NULL,
  6747. LLM_NORM_RMS, il);
  6748. {
  6749. ggml_tensor * Qcur;
  6750. ggml_tensor * Kcur;
  6751. ggml_tensor * Vcur;
  6752. // self-attention
  6753. cur = build_lora_mm(model.layers[il].wqkv, cur);
  6754. cb(cur, "wqkv", il);
  6755. 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));
  6756. 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));
  6757. 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));
  6758. // RoPE
  6759. Qcur = ggml_rope_ext(
  6760. ctx0, Qcur, inp_pos, nullptr,
  6761. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  6762. ext_factor, attn_factor, beta_fast, beta_slow
  6763. );
  6764. Kcur = ggml_rope_ext(
  6765. ctx0, Kcur, inp_pos, nullptr,
  6766. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  6767. ext_factor, attn_factor, beta_fast, beta_slow
  6768. );
  6769. cb(Qcur, "Qcur", il);
  6770. cb(Kcur, "Kcur", il);
  6771. cb(Vcur, "Vcur", il);
  6772. cur = build_attn(inp_attn,
  6773. model.layers[il].wo, nullptr,
  6774. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  6775. cb(cur, "kqv_out", il);
  6776. }
  6777. if (il == n_layer - 1 && inp_out_ids) {
  6778. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  6779. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  6780. }
  6781. // re-add the layer input
  6782. cur = ggml_add(ctx0, cur, inpL);
  6783. ggml_tensor * ffn_inp = cur;
  6784. cb(ffn_inp, "ffn_inp", il);
  6785. // pre-norm
  6786. cur = build_norm(ffn_inp,
  6787. model.layers[il].ffn_norm, NULL,
  6788. LLM_NORM_RMS, il);
  6789. cb(cur, "ffn_norm", il);
  6790. // feed-forward network
  6791. cur = build_ffn(cur,
  6792. model.layers[il].ffn_up,
  6793. NULL, NULL, NULL, NULL, NULL,
  6794. model.layers[il].ffn_down,
  6795. NULL, NULL, NULL,
  6796. LLM_FFN_SWIGLU, LLM_FFN_SEQ, il);
  6797. // attentions bypass the intermediate layer
  6798. cur = ggml_add(ctx0, cur, ffn_inp);
  6799. // input for next layer
  6800. inpL = cur;
  6801. }
  6802. cur = inpL;
  6803. cur = build_norm(cur,
  6804. model.output_norm_enc, NULL,
  6805. LLM_NORM_RMS, -1);
  6806. cb(cur, "result_embd", -1);
  6807. res->t_embd = cur;
  6808. ggml_build_forward_expand(gf, cur);
  6809. }
  6810. };
  6811. struct llm_build_bloom : public llm_graph_context {
  6812. llm_build_bloom(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  6813. const int64_t n_embd_head = hparams.n_embd_head_v;
  6814. const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  6815. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  6816. ggml_tensor * cur;
  6817. ggml_tensor * inpL;
  6818. inpL = build_inp_embd(model.tok_embd);
  6819. auto * inp_attn = build_attn_inp_kv();
  6820. inpL = build_norm(inpL,
  6821. model.tok_norm,
  6822. model.tok_norm_b,
  6823. LLM_NORM, -1);
  6824. cb(inpL, "inp_norm", -1);
  6825. ggml_tensor * inp_out_ids = build_inp_out_ids();
  6826. for (int il = 0; il < n_layer; ++il) {
  6827. cur = build_norm(inpL,
  6828. model.layers[il].attn_norm,
  6829. model.layers[il].attn_norm_b,
  6830. LLM_NORM, il);
  6831. cb(cur, "attn_norm", il);
  6832. // self-attention
  6833. {
  6834. cur = build_lora_mm(model.layers[il].wqkv, cur);
  6835. cb(cur, "wqkv", il);
  6836. cur = ggml_add(ctx0, cur, model.layers[il].bqkv);
  6837. cb(cur, "bqkv", il);
  6838. 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));
  6839. 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));
  6840. 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));
  6841. cb(Qcur, "Qcur", il);
  6842. cb(Kcur, "Kcur", il);
  6843. cb(Vcur, "Vcur", il);
  6844. cur = build_attn(inp_attn,
  6845. model.layers[il].wo, model.layers[il].bo,
  6846. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  6847. }
  6848. if (il == n_layer - 1 && inp_out_ids) {
  6849. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  6850. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  6851. }
  6852. // Add the input
  6853. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
  6854. cb(ffn_inp, "ffn_inp", il);
  6855. // FF
  6856. {
  6857. cur = build_norm(ffn_inp,
  6858. model.layers[il].ffn_norm,
  6859. model.layers[il].ffn_norm_b,
  6860. LLM_NORM, il);
  6861. cb(cur, "ffn_norm", il);
  6862. cur = build_ffn(cur,
  6863. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  6864. NULL, NULL, NULL,
  6865. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  6866. NULL,
  6867. LLM_FFN_GELU, LLM_FFN_SEQ, il);
  6868. cb(cur, "ffn_out", il);
  6869. }
  6870. cur = ggml_add(ctx0, cur, ffn_inp);
  6871. cur = build_cvec(cur, il);
  6872. cb(cur, "l_out", il);
  6873. // input for next layer
  6874. inpL = cur;
  6875. }
  6876. cur = build_norm(inpL,
  6877. model.output_norm,
  6878. model.output_norm_b,
  6879. LLM_NORM, -1);
  6880. cb(cur, "result_norm", -1);
  6881. res->t_embd = cur;
  6882. cur = build_lora_mm(model.output, cur);
  6883. cb(cur, "result_output", -1);
  6884. res->t_logits = cur;
  6885. ggml_build_forward_expand(gf, cur);
  6886. }
  6887. };
  6888. struct llm_build_mpt : public llm_graph_context {
  6889. llm_build_mpt(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  6890. const int64_t n_embd_head = hparams.n_embd_head_v;
  6891. const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  6892. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  6893. ggml_tensor * cur;
  6894. ggml_tensor * pos;
  6895. ggml_tensor * inpL;
  6896. inpL = build_inp_embd(model.tok_embd);
  6897. auto * inp_attn = build_attn_inp_kv();
  6898. if (model.pos_embd) {
  6899. // inp_pos - contains the positions
  6900. ggml_tensor * inp_pos = build_inp_pos();
  6901. pos = ggml_get_rows(ctx0, model.pos_embd, inp_pos);
  6902. cb(pos, "pos_embd", -1);
  6903. inpL = ggml_add(ctx0, inpL, pos);
  6904. cb(inpL, "inpL", -1);
  6905. }
  6906. ggml_tensor * inp_out_ids = build_inp_out_ids();
  6907. for (int il = 0; il < n_layer; ++il) {
  6908. ggml_tensor * attn_norm;
  6909. attn_norm = build_norm(inpL,
  6910. model.layers[il].attn_norm,
  6911. model.layers[il].attn_norm_b,
  6912. LLM_NORM, il);
  6913. cb(attn_norm, "attn_norm", il);
  6914. // self-attention
  6915. {
  6916. cur = attn_norm;
  6917. cur = build_lora_mm(model.layers[il].wqkv, cur);
  6918. cb(cur, "wqkv", il);
  6919. if (model.layers[il].bqkv){
  6920. cur = ggml_add(ctx0, cur, model.layers[il].bqkv);
  6921. cb(cur, "bqkv", il);
  6922. }
  6923. if (hparams.f_clamp_kqv > 0.0f) {
  6924. cur = ggml_clamp(ctx0, cur, -hparams.f_clamp_kqv, hparams.f_clamp_kqv);
  6925. cb(cur, "wqkv_clamped", il);
  6926. }
  6927. 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));
  6928. 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));
  6929. 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));
  6930. // Q/K Layernorm
  6931. if (model.layers[il].attn_q_norm) {
  6932. Qcur = ggml_reshape_2d(ctx0, Qcur, n_embd_head*n_head, n_tokens);
  6933. Kcur = ggml_reshape_2d(ctx0, Kcur, n_embd_head*n_head_kv, n_tokens);
  6934. Qcur = build_norm(Qcur,
  6935. model.layers[il].attn_q_norm,
  6936. model.layers[il].attn_q_norm_b,
  6937. LLM_NORM, il);
  6938. Kcur = build_norm(Kcur,
  6939. model.layers[il].attn_k_norm,
  6940. model.layers[il].attn_k_norm_b,
  6941. LLM_NORM, il);
  6942. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  6943. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  6944. }
  6945. cb(Qcur, "Qcur", il);
  6946. cb(Kcur, "Kcur", il);
  6947. cb(Vcur, "Vcur", il);
  6948. cur = build_attn(inp_attn,
  6949. model.layers[il].wo, model.layers[il].bo,
  6950. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  6951. }
  6952. if (il == n_layer - 1 && inp_out_ids) {
  6953. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  6954. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  6955. }
  6956. // Add the input
  6957. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
  6958. cb(ffn_inp, "ffn_inp", il);
  6959. // feed forward
  6960. {
  6961. cur = build_norm(ffn_inp,
  6962. model.layers[il].ffn_norm,
  6963. model.layers[il].ffn_norm_b,
  6964. LLM_NORM, il);
  6965. cb(cur, "ffn_norm", il);
  6966. cur = build_ffn(cur,
  6967. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  6968. NULL, NULL, NULL,
  6969. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  6970. model.layers[il].ffn_act,
  6971. LLM_FFN_GELU, LLM_FFN_SEQ, il);
  6972. cb(cur, "ffn_out", il);
  6973. }
  6974. cur = ggml_add(ctx0, cur, ffn_inp);
  6975. cur = build_cvec(cur, il);
  6976. cb(cur, "l_out", il);
  6977. // input for next layer
  6978. inpL = cur;
  6979. }
  6980. cur = inpL;
  6981. cur = build_norm(cur,
  6982. model.output_norm,
  6983. model.output_norm_b,
  6984. LLM_NORM, -1);
  6985. cb(cur, "result_norm", -1);
  6986. res->t_embd = cur;
  6987. cur = build_lora_mm(model.output, cur);
  6988. cb(cur, "result_output", -1);
  6989. res->t_logits = cur;
  6990. ggml_build_forward_expand(gf, cur);
  6991. }
  6992. };
  6993. struct llm_build_stablelm : public llm_graph_context {
  6994. llm_build_stablelm(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  6995. const int64_t n_embd_head = hparams.n_embd_head_v;
  6996. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  6997. ggml_tensor * cur;
  6998. ggml_tensor * inpL;
  6999. inpL = build_inp_embd(model.tok_embd);
  7000. // inp_pos - contains the positions
  7001. ggml_tensor * inp_pos = build_inp_pos();
  7002. auto * inp_attn = build_attn_inp_kv();
  7003. ggml_tensor * inp_out_ids = build_inp_out_ids();
  7004. for (int il = 0; il < n_layer; ++il) {
  7005. // norm
  7006. cur = build_norm(inpL,
  7007. model.layers[il].attn_norm,
  7008. model.layers[il].attn_norm_b,
  7009. LLM_NORM, il);
  7010. cb(cur, "attn_norm", il);
  7011. ggml_tensor * inpSA = cur;
  7012. // self-attention
  7013. {
  7014. // compute Q and K and RoPE them
  7015. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  7016. cb(Qcur, "Qcur", il);
  7017. if (model.layers[il].bq) {
  7018. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  7019. cb(Qcur, "Qcur", il);
  7020. }
  7021. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  7022. cb(Kcur, "Kcur", il);
  7023. if (model.layers[il].bk) {
  7024. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  7025. cb(Kcur, "Kcur", il);
  7026. }
  7027. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  7028. cb(Vcur, "Vcur", il);
  7029. if (model.layers[il].bv) {
  7030. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  7031. cb(Vcur, "Vcur", il);
  7032. }
  7033. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  7034. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  7035. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  7036. if (model.layers[il].attn_q_norm) {
  7037. Qcur = build_norm(Qcur,
  7038. model.layers[il].attn_q_norm,
  7039. NULL,
  7040. LLM_NORM, il);
  7041. cb(Qcur, "Qcur", il);
  7042. }
  7043. if (model.layers[il].attn_k_norm) {
  7044. Kcur = build_norm(Kcur,
  7045. model.layers[il].attn_k_norm,
  7046. NULL,
  7047. LLM_NORM, il);
  7048. cb(Kcur, "Kcur", il);
  7049. }
  7050. Qcur = ggml_rope_ext(
  7051. ctx0, Qcur, inp_pos, nullptr,
  7052. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  7053. ext_factor, attn_factor, beta_fast, beta_slow
  7054. );
  7055. Kcur = ggml_rope_ext(
  7056. ctx0, Kcur, inp_pos, nullptr,
  7057. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  7058. ext_factor, attn_factor, beta_fast, beta_slow
  7059. );
  7060. cb(Qcur, "Qcur", il);
  7061. cb(Kcur, "Kcur", il);
  7062. cb(Vcur, "Vcur", il);
  7063. cur = build_attn(inp_attn,
  7064. model.layers[il].wo, NULL,
  7065. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  7066. }
  7067. if (il == n_layer - 1 && inp_out_ids) {
  7068. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  7069. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  7070. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  7071. }
  7072. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
  7073. cb(ffn_inp, "ffn_inp", il);
  7074. // feed-forward network
  7075. {
  7076. if (model.layers[il].ffn_norm) {
  7077. cur = build_norm(ffn_inp,
  7078. model.layers[il].ffn_norm,
  7079. model.layers[il].ffn_norm_b,
  7080. LLM_NORM, il);
  7081. cb(cur, "ffn_norm", il);
  7082. } else {
  7083. // parallel residual
  7084. cur = inpSA;
  7085. }
  7086. cur = build_ffn(cur,
  7087. model.layers[il].ffn_up, NULL, NULL,
  7088. model.layers[il].ffn_gate, NULL, NULL,
  7089. model.layers[il].ffn_down, NULL, NULL,
  7090. NULL,
  7091. LLM_FFN_SILU, LLM_FFN_PAR, il);
  7092. cb(cur, "ffn_out", il);
  7093. }
  7094. cur = ggml_add(ctx0, cur, ffn_inp);
  7095. cur = build_cvec(cur, il);
  7096. cb(cur, "l_out", il);
  7097. // input for next layer
  7098. inpL = cur;
  7099. }
  7100. cur = inpL;
  7101. cur = build_norm(cur,
  7102. model.output_norm,
  7103. model.output_norm_b,
  7104. LLM_NORM, -1);
  7105. cb(cur, "result_norm", -1);
  7106. res->t_embd = cur;
  7107. // lm_head
  7108. cur = build_lora_mm(model.output, cur);
  7109. cb(cur, "result_output", -1);
  7110. res->t_logits = cur;
  7111. ggml_build_forward_expand(gf, cur);
  7112. }
  7113. };
  7114. struct llm_build_qwen : public llm_graph_context {
  7115. llm_build_qwen(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  7116. const int64_t n_embd_head = hparams.n_embd_head_v;
  7117. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  7118. ggml_tensor * cur;
  7119. ggml_tensor * inpL;
  7120. inpL = build_inp_embd(model.tok_embd);
  7121. // inp_pos - contains the positions
  7122. ggml_tensor * inp_pos = build_inp_pos();
  7123. auto * inp_attn = build_attn_inp_kv();
  7124. ggml_tensor * inp_out_ids = build_inp_out_ids();
  7125. for (int il = 0; il < n_layer; ++il) {
  7126. ggml_tensor * inpSA = inpL;
  7127. cur = build_norm(inpL,
  7128. model.layers[il].attn_norm, NULL,
  7129. LLM_NORM_RMS, il);
  7130. cb(cur, "attn_norm", il);
  7131. // self-attention
  7132. {
  7133. cur = build_lora_mm(model.layers[il].wqkv, cur);
  7134. cb(cur, "wqkv", il);
  7135. cur = ggml_add(ctx0, cur, model.layers[il].bqkv);
  7136. cb(cur, "bqkv", il);
  7137. 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));
  7138. 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));
  7139. 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));
  7140. // using mode = 2 for neox mode
  7141. Qcur = ggml_rope_ext(
  7142. ctx0, Qcur, inp_pos, nullptr,
  7143. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  7144. ext_factor, attn_factor, beta_fast, beta_slow
  7145. );
  7146. Kcur = ggml_rope_ext(
  7147. ctx0, Kcur, inp_pos, nullptr,
  7148. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  7149. ext_factor, attn_factor, beta_fast, beta_slow
  7150. );
  7151. cb(Qcur, "Qcur", il);
  7152. cb(Kcur, "Kcur", il);
  7153. cb(Vcur, "Vcur", il);
  7154. cur = build_attn(inp_attn,
  7155. model.layers[il].wo, NULL,
  7156. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  7157. }
  7158. if (il == n_layer - 1 && inp_out_ids) {
  7159. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  7160. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  7161. }
  7162. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  7163. cb(ffn_inp, "ffn_inp", il);
  7164. // feed-forward forward
  7165. {
  7166. cur = build_norm(ffn_inp,
  7167. model.layers[il].ffn_norm, NULL,
  7168. LLM_NORM_RMS, il);
  7169. cb(cur, "ffn_norm", il);
  7170. cur = build_ffn(cur,
  7171. model.layers[il].ffn_up, NULL, NULL,
  7172. model.layers[il].ffn_gate, NULL, NULL,
  7173. model.layers[il].ffn_down, NULL, NULL,
  7174. NULL,
  7175. LLM_FFN_SILU, LLM_FFN_PAR, il);
  7176. cb(cur, "ffn_out", il);
  7177. }
  7178. cur = ggml_add(ctx0, cur, ffn_inp);
  7179. cur = build_cvec(cur, il);
  7180. cb(cur, "l_out", il);
  7181. // input for next layer
  7182. inpL = cur;
  7183. }
  7184. cur = inpL;
  7185. cur = build_norm(cur,
  7186. model.output_norm, NULL,
  7187. LLM_NORM_RMS, -1);
  7188. cb(cur, "result_norm", -1);
  7189. res->t_embd = cur;
  7190. // lm_head
  7191. cur = build_lora_mm(model.output, cur);
  7192. cb(cur, "result_output", -1);
  7193. res->t_logits = cur;
  7194. ggml_build_forward_expand(gf, cur);
  7195. }
  7196. };
  7197. struct llm_build_qwen2 : public llm_graph_context {
  7198. llm_build_qwen2(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  7199. const int64_t n_embd_head = hparams.n_embd_head_v;
  7200. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  7201. GGML_ASSERT(n_embd_head == hparams.n_rot);
  7202. ggml_tensor * cur;
  7203. ggml_tensor * inpL;
  7204. inpL = build_inp_embd(model.tok_embd);
  7205. // inp_pos - contains the positions
  7206. ggml_tensor * inp_pos = build_inp_pos();
  7207. auto * inp_attn = build_attn_inp_kv();
  7208. ggml_tensor * inp_out_ids = build_inp_out_ids();
  7209. for (int il = 0; il < n_layer; ++il) {
  7210. ggml_tensor * inpSA = inpL;
  7211. // norm
  7212. cur = build_norm(inpL,
  7213. model.layers[il].attn_norm, NULL,
  7214. LLM_NORM_RMS, il);
  7215. cb(cur, "attn_norm", il);
  7216. // self-attention
  7217. {
  7218. // compute Q and K and RoPE them
  7219. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  7220. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  7221. cb(Qcur, "Qcur", il);
  7222. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  7223. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  7224. cb(Kcur, "Kcur", il);
  7225. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  7226. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  7227. cb(Vcur, "Vcur", il);
  7228. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  7229. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  7230. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  7231. Qcur = ggml_rope_ext(
  7232. ctx0, Qcur, inp_pos, nullptr,
  7233. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  7234. ext_factor, attn_factor, beta_fast, beta_slow
  7235. );
  7236. Kcur = ggml_rope_ext(
  7237. ctx0, Kcur, inp_pos, nullptr,
  7238. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  7239. ext_factor, attn_factor, beta_fast, beta_slow
  7240. );
  7241. cb(Qcur, "Qcur", il);
  7242. cb(Kcur, "Kcur", il);
  7243. cb(Vcur, "Vcur", il);
  7244. cur = build_attn(inp_attn,
  7245. model.layers[il].wo, model.layers[il].bo,
  7246. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  7247. }
  7248. if (il == n_layer - 1 && inp_out_ids) {
  7249. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  7250. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  7251. }
  7252. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  7253. cb(ffn_inp, "ffn_inp", il);
  7254. // feed-forward network
  7255. cur = build_norm(ffn_inp,
  7256. model.layers[il].ffn_norm, NULL,
  7257. LLM_NORM_RMS, il);
  7258. cb(cur, "ffn_norm", il);
  7259. cur = build_ffn(cur,
  7260. model.layers[il].ffn_up, NULL, NULL,
  7261. model.layers[il].ffn_gate, NULL, NULL,
  7262. model.layers[il].ffn_down, NULL, NULL,
  7263. NULL,
  7264. LLM_FFN_SILU, LLM_FFN_PAR, il);
  7265. cb(cur, "ffn_out", il);
  7266. cur = ggml_add(ctx0, cur, ffn_inp);
  7267. cur = build_cvec(cur, il);
  7268. cb(cur, "l_out", il);
  7269. // input for next layer
  7270. inpL = cur;
  7271. }
  7272. cur = inpL;
  7273. cur = build_norm(cur,
  7274. model.output_norm, NULL,
  7275. LLM_NORM_RMS, -1);
  7276. cb(cur, "result_norm", -1);
  7277. res->t_embd = cur;
  7278. // lm_head
  7279. cur = build_lora_mm(model.output, cur);
  7280. if (model.output_b != nullptr) {
  7281. cur = ggml_add(ctx0, cur, model.output_b);
  7282. }
  7283. cb(cur, "result_output", -1);
  7284. res->t_logits = cur;
  7285. ggml_build_forward_expand(gf, cur);
  7286. }
  7287. };
  7288. struct llm_build_dream : public llm_graph_context {
  7289. llm_build_dream(const llama_model & model, const llm_graph_params & params) :
  7290. llm_graph_context(params) {
  7291. //copied from qwen2
  7292. const int64_t n_embd_head = hparams.n_embd_head_v;
  7293. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  7294. GGML_ASSERT(n_embd_head == hparams.n_rot);
  7295. ggml_tensor * cur;
  7296. ggml_tensor * inpL;
  7297. inpL = build_inp_embd(model.tok_embd);
  7298. // inp_pos - contains the positions
  7299. ggml_tensor * inp_pos = build_inp_pos();
  7300. auto * inp_attn = build_attn_inp_no_cache();
  7301. ggml_tensor * inp_out_ids = build_inp_out_ids();
  7302. for (int il = 0; il < n_layer; ++il) {
  7303. ggml_tensor * inpSA = inpL;
  7304. // norm
  7305. cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
  7306. cb(cur, "attn_norm", il);
  7307. // self-attention
  7308. {
  7309. // compute Q and K and RoPE them
  7310. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  7311. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  7312. cb(Qcur, "Qcur", il);
  7313. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  7314. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  7315. cb(Kcur, "Kcur", il);
  7316. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  7317. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  7318. cb(Vcur, "Vcur", il);
  7319. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  7320. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  7321. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  7322. Qcur = ggml_rope_ext(ctx0, Qcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  7323. ext_factor, attn_factor, beta_fast, beta_slow);
  7324. Kcur = ggml_rope_ext(ctx0, Kcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  7325. ext_factor, attn_factor, beta_fast, beta_slow);
  7326. cb(Qcur, "Qcur", il);
  7327. cb(Kcur, "Kcur", il);
  7328. cb(Vcur, "Vcur", il);
  7329. cur = build_attn(inp_attn,
  7330. model.layers[il].wo, model.layers[il].bo,
  7331. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f / sqrtf(float(n_embd_head)), il);
  7332. }
  7333. if (il == n_layer - 1 && inp_out_ids) {
  7334. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  7335. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  7336. }
  7337. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  7338. cb(ffn_inp, "ffn_inp", il);
  7339. // feed-forward network
  7340. cur = build_norm(ffn_inp, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);
  7341. cb(cur, "ffn_norm", il);
  7342. cur = build_ffn(cur, model.layers[il].ffn_up, NULL, NULL, model.layers[il].ffn_gate, NULL, NULL,
  7343. model.layers[il].ffn_down, NULL, NULL, NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
  7344. cb(cur, "ffn_out", il);
  7345. cur = ggml_add(ctx0, cur, ffn_inp);
  7346. cur = build_cvec(cur, il);
  7347. cb(cur, "l_out", il);
  7348. // input for next layer
  7349. inpL = cur;
  7350. }
  7351. cur = inpL;
  7352. cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1);
  7353. cb(cur, "result_norm", -1);
  7354. res->t_embd = cur;
  7355. // lm_head
  7356. cur = build_lora_mm(model.output, cur);
  7357. cb(cur, "result_output", -1);
  7358. res->t_logits = cur;
  7359. ggml_build_forward_expand(gf, cur);
  7360. }
  7361. };
  7362. struct llm_build_llada : public llm_graph_context {
  7363. llm_build_llada(const llama_model & model, const llm_graph_params & params) :
  7364. llm_graph_context(params) {
  7365. // LLaDA is similar to LLaMA but uses non-causal attention for diffusion
  7366. const int64_t n_embd_head = hparams.n_embd_head_v;
  7367. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  7368. GGML_ASSERT(n_embd_head == hparams.n_rot);
  7369. ggml_tensor * cur;
  7370. ggml_tensor * inpL;
  7371. inpL = build_inp_embd(model.tok_embd);
  7372. // inp_pos - contains the positions
  7373. ggml_tensor * inp_pos = build_inp_pos();
  7374. // Non-causal attention for diffusion
  7375. auto * inp_attn = build_attn_inp_no_cache();
  7376. ggml_tensor * inp_out_ids = build_inp_out_ids();
  7377. for (int il = 0; il < n_layer; ++il) {
  7378. ggml_tensor * inpSA = inpL;
  7379. // norm
  7380. cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
  7381. cb(cur, "attn_norm", il);
  7382. // self-attention
  7383. {
  7384. // compute separate Q, K, V projections without bias, matching LLaDALlamaBlock
  7385. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  7386. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  7387. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  7388. cb(Qcur, "Qcur", il);
  7389. cb(Kcur, "Kcur", il);
  7390. cb(Vcur, "Vcur", il);
  7391. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  7392. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  7393. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  7394. Qcur = ggml_rope_ext(ctx0, Qcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  7395. ext_factor, attn_factor, beta_fast, beta_slow);
  7396. Kcur = ggml_rope_ext(ctx0, Kcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  7397. ext_factor, attn_factor, beta_fast, beta_slow);
  7398. cb(Qcur, "Qcur", il);
  7399. cb(Kcur, "Kcur", il);
  7400. cb(Vcur, "Vcur", il);
  7401. cur = build_attn(inp_attn,
  7402. model.layers[il].wo, NULL,
  7403. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f / sqrtf(float(n_embd_head)), il);
  7404. }
  7405. if (il == n_layer - 1 && inp_out_ids) {
  7406. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  7407. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  7408. }
  7409. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  7410. cb(ffn_inp, "ffn_inp", il);
  7411. // feed-forward network
  7412. cur = build_norm(ffn_inp, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);
  7413. cb(cur, "ffn_norm", il);
  7414. cur = build_ffn(cur, model.layers[il].ffn_up, NULL, NULL, model.layers[il].ffn_gate, NULL, NULL,
  7415. model.layers[il].ffn_down, NULL, NULL, NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
  7416. cb(cur, "ffn_out", il);
  7417. cur = ggml_add(ctx0, cur, ffn_inp);
  7418. cur = build_cvec(cur, il);
  7419. cb(cur, "l_out", il);
  7420. // input for next layer
  7421. inpL = cur;
  7422. }
  7423. cur = inpL;
  7424. cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1);
  7425. cb(cur, "result_norm", -1);
  7426. res->t_embd = cur;
  7427. // lm_head
  7428. cur = build_lora_mm(model.output, cur);
  7429. cb(cur, "result_output", -1);
  7430. res->t_logits = cur;
  7431. ggml_build_forward_expand(gf, cur);
  7432. }
  7433. };
  7434. struct llm_build_qwen2vl : public llm_graph_context {
  7435. llm_build_qwen2vl(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  7436. const int64_t n_embd_head = hparams.n_embd_head_v;
  7437. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  7438. GGML_ASSERT(n_embd_head == hparams.n_rot);
  7439. ggml_tensor * cur;
  7440. ggml_tensor * inpL;
  7441. inpL = build_inp_embd(model.tok_embd);
  7442. // inp_pos - contains the positions
  7443. ggml_tensor * inp_pos = build_inp_pos();
  7444. auto * inp_attn = build_attn_inp_kv();
  7445. int sections[4];
  7446. std::copy(std::begin(hparams.rope_sections), std::begin(hparams.rope_sections) + 4, sections);
  7447. ggml_tensor * inp_out_ids = build_inp_out_ids();
  7448. for (int il = 0; il < n_layer; ++il) {
  7449. ggml_tensor * inpSA = inpL;
  7450. // norm
  7451. cur = build_norm(inpL,
  7452. model.layers[il].attn_norm, NULL,
  7453. LLM_NORM_RMS, il);
  7454. cb(cur, "attn_norm", il);
  7455. // self-attention
  7456. {
  7457. // compute Q and K and RoPE them
  7458. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  7459. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  7460. cb(Qcur, "Qcur", il);
  7461. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  7462. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  7463. cb(Kcur, "Kcur", il);
  7464. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  7465. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  7466. cb(Vcur, "Vcur", il);
  7467. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  7468. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  7469. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  7470. Qcur = ggml_rope_multi(
  7471. ctx0, Qcur, inp_pos, nullptr,
  7472. n_rot, sections, rope_type, n_ctx_orig, freq_base, freq_scale,
  7473. ext_factor, attn_factor, beta_fast, beta_slow
  7474. );
  7475. Kcur = ggml_rope_multi(
  7476. ctx0, Kcur, inp_pos, nullptr,
  7477. n_rot, sections, rope_type, n_ctx_orig, freq_base, freq_scale,
  7478. ext_factor, attn_factor, beta_fast, beta_slow
  7479. );
  7480. cb(Qcur, "Qcur", il);
  7481. cb(Kcur, "Kcur", il);
  7482. cb(Vcur, "Vcur", il);
  7483. cur = build_attn(inp_attn,
  7484. model.layers[il].wo, model.layers[il].bo,
  7485. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  7486. }
  7487. if (il == n_layer - 1 && inp_out_ids) {
  7488. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  7489. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  7490. }
  7491. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  7492. cb(ffn_inp, "ffn_inp", il);
  7493. // feed-forward network
  7494. cur = build_norm(ffn_inp,
  7495. model.layers[il].ffn_norm, NULL,
  7496. LLM_NORM_RMS, il);
  7497. cb(cur, "ffn_norm", il);
  7498. cur = build_ffn(cur,
  7499. model.layers[il].ffn_up, NULL, NULL,
  7500. model.layers[il].ffn_gate, NULL, NULL,
  7501. model.layers[il].ffn_down, NULL, NULL,
  7502. NULL,
  7503. LLM_FFN_SILU, LLM_FFN_PAR, il);
  7504. cb(cur, "ffn_out", il);
  7505. cur = ggml_add(ctx0, cur, ffn_inp);
  7506. cur = build_cvec(cur, il);
  7507. cb(cur, "l_out", il);
  7508. // input for next layer
  7509. inpL = cur;
  7510. }
  7511. cur = inpL;
  7512. cur = build_norm(cur,
  7513. model.output_norm, NULL,
  7514. LLM_NORM_RMS, -1);
  7515. cb(cur, "result_norm", -1);
  7516. res->t_embd = cur;
  7517. // lm_head
  7518. cur = build_lora_mm(model.output, cur);
  7519. cb(cur, "result_output", -1);
  7520. res->t_logits = cur;
  7521. ggml_build_forward_expand(gf, cur);
  7522. }
  7523. };
  7524. struct llm_build_qwen2moe : public llm_graph_context {
  7525. llm_build_qwen2moe(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  7526. const int64_t n_embd_head = hparams.n_embd_head_v;
  7527. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  7528. GGML_ASSERT(n_embd_head == hparams.n_rot);
  7529. ggml_tensor * cur;
  7530. ggml_tensor * inpL;
  7531. inpL = build_inp_embd(model.tok_embd);
  7532. // inp_pos - contains the positions
  7533. ggml_tensor * inp_pos = build_inp_pos();
  7534. auto * inp_attn = build_attn_inp_kv();
  7535. ggml_tensor * inp_out_ids = build_inp_out_ids();
  7536. for (int il = 0; il < n_layer; ++il) {
  7537. ggml_tensor * inpSA = inpL;
  7538. // norm
  7539. cur = build_norm(inpL,
  7540. model.layers[il].attn_norm, NULL,
  7541. LLM_NORM_RMS, il);
  7542. cb(cur, "attn_norm", il);
  7543. // self_attention
  7544. {
  7545. // compute Q and K and RoPE them
  7546. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  7547. cb(Qcur, "Qcur", il);
  7548. if (model.layers[il].bq) {
  7549. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  7550. cb(Qcur, "Qcur", il);
  7551. }
  7552. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  7553. cb(Kcur, "Kcur", il);
  7554. if (model.layers[il].bk) {
  7555. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  7556. cb(Kcur, "Kcur", il);
  7557. }
  7558. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  7559. cb(Vcur, "Vcur", il);
  7560. if (model.layers[il].bv) {
  7561. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  7562. cb(Vcur, "Vcur", il);
  7563. }
  7564. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  7565. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  7566. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  7567. Qcur = ggml_rope_ext(
  7568. ctx0, Qcur, inp_pos, nullptr,
  7569. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  7570. ext_factor, attn_factor, beta_fast, beta_slow
  7571. );
  7572. Kcur = ggml_rope_ext(
  7573. ctx0, Kcur, inp_pos, nullptr,
  7574. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  7575. ext_factor, attn_factor, beta_fast, beta_slow
  7576. );
  7577. cb(Qcur, "Qcur", il);
  7578. cb(Kcur, "Kcur", il);
  7579. cb(Vcur, "Vcur", il);
  7580. cur = build_attn(inp_attn,
  7581. model.layers[il].wo, model.layers[il].bo,
  7582. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  7583. }
  7584. if (il == n_layer - 1 && inp_out_ids) {
  7585. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  7586. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  7587. }
  7588. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  7589. cb(ffn_inp, "ffn_inp", il);
  7590. // MoE branch
  7591. cur = build_norm(ffn_inp,
  7592. model.layers[il].ffn_norm, NULL,
  7593. LLM_NORM_RMS, il);
  7594. cb(cur, "ffn_norm", il);
  7595. ggml_tensor * moe_out =
  7596. build_moe_ffn(cur,
  7597. model.layers[il].ffn_gate_inp,
  7598. model.layers[il].ffn_up_exps,
  7599. model.layers[il].ffn_gate_exps,
  7600. model.layers[il].ffn_down_exps,
  7601. nullptr,
  7602. n_expert, n_expert_used,
  7603. LLM_FFN_SILU, false,
  7604. false, 0.0,
  7605. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  7606. il);
  7607. cb(moe_out, "ffn_moe_out", il);
  7608. // FFN shared expert
  7609. {
  7610. ggml_tensor * cur_gate_inp = build_lora_mm(model.layers[il].ffn_gate_inp_shexp, cur);
  7611. cb(cur_gate_inp, "ffn_shexp_gate_inp", il);
  7612. // sigmoid
  7613. ggml_tensor * cur_gate = ggml_div(ctx0, ggml_silu(ctx0, cur_gate_inp), cur_gate_inp);
  7614. cb(cur_gate, "ffn_shexp_gate", il);
  7615. ggml_tensor * cur_ffn = build_ffn(cur,
  7616. model.layers[il].ffn_up_shexp, NULL, NULL,
  7617. model.layers[il].ffn_gate_shexp, NULL, NULL,
  7618. model.layers[il].ffn_down_shexp, NULL, NULL,
  7619. NULL,
  7620. LLM_FFN_SILU, LLM_FFN_PAR, il);
  7621. cb(cur_ffn, "ffn_shexp", il);
  7622. ggml_tensor * ffn_shexp_out = ggml_mul(ctx0, cur_ffn, cur_gate);
  7623. cb(ffn_shexp_out, "ffn_shexp_out", il);
  7624. moe_out = ggml_add(ctx0, moe_out, ffn_shexp_out);
  7625. cb(moe_out, "ffn_out", il);
  7626. cur = moe_out;
  7627. }
  7628. cur = ggml_add(ctx0, cur, ffn_inp);
  7629. cur = build_cvec(cur, il);
  7630. cb(cur, "l_out", il);
  7631. // input for next layer
  7632. inpL = cur;
  7633. }
  7634. cur = inpL;
  7635. cur = build_norm(cur,
  7636. model.output_norm, NULL,
  7637. LLM_NORM_RMS, -1);
  7638. cb(cur, "result_norm", -1);
  7639. res->t_embd = cur;
  7640. // lm_head
  7641. cur = build_lora_mm(model.output, cur);
  7642. cb(cur, "result_output", -1);
  7643. res->t_logits = cur;
  7644. ggml_build_forward_expand(gf, cur);
  7645. }
  7646. };
  7647. struct llm_build_qwen3 : public llm_graph_context {
  7648. llm_build_qwen3(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  7649. const int64_t n_embd_head = hparams.n_embd_head_v;
  7650. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  7651. GGML_ASSERT(n_embd_head == hparams.n_rot);
  7652. ggml_tensor * cur;
  7653. ggml_tensor * inpL;
  7654. inpL = build_inp_embd(model.tok_embd);
  7655. // inp_pos - contains the positions
  7656. ggml_tensor * inp_pos = build_inp_pos();
  7657. auto * inp_attn = build_attn_inp_kv();
  7658. ggml_tensor * inp_out_ids = build_inp_out_ids();
  7659. for (int il = 0; il < n_layer; ++il) {
  7660. ggml_tensor * inpSA = inpL;
  7661. // norm
  7662. cur = build_norm(inpL,
  7663. model.layers[il].attn_norm, NULL,
  7664. LLM_NORM_RMS, il);
  7665. cb(cur, "attn_norm", il);
  7666. // self-attention
  7667. {
  7668. // compute Q and K and RoPE them
  7669. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  7670. cb(Qcur, "Qcur", il);
  7671. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  7672. cb(Kcur, "Kcur", il);
  7673. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  7674. cb(Vcur, "Vcur", il);
  7675. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  7676. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  7677. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  7678. Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);
  7679. cb(Qcur, "Qcur_normed", il);
  7680. Qcur = ggml_rope_ext(
  7681. ctx0, Qcur, inp_pos, nullptr,
  7682. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  7683. ext_factor, attn_factor, beta_fast, beta_slow
  7684. );
  7685. Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);
  7686. cb(Kcur, "Kcur_normed", il);
  7687. Kcur = ggml_rope_ext(
  7688. ctx0, Kcur, inp_pos, nullptr,
  7689. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  7690. ext_factor, attn_factor, beta_fast, beta_slow
  7691. );
  7692. cb(Qcur, "Qcur", il);
  7693. cb(Kcur, "Kcur", il);
  7694. cb(Vcur, "Vcur", il);
  7695. cur = build_attn(inp_attn,
  7696. model.layers[il].wo, model.layers[il].bo,
  7697. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  7698. }
  7699. if (il == n_layer - 1 && inp_out_ids) {
  7700. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  7701. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  7702. }
  7703. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  7704. cb(ffn_inp, "ffn_inp", il);
  7705. // feed-forward network
  7706. cur = build_norm(ffn_inp,
  7707. model.layers[il].ffn_norm, NULL,
  7708. LLM_NORM_RMS, il);
  7709. cb(cur, "ffn_norm", il);
  7710. cur = build_ffn(cur,
  7711. model.layers[il].ffn_up, NULL, NULL,
  7712. model.layers[il].ffn_gate, NULL, NULL,
  7713. model.layers[il].ffn_down, NULL, NULL,
  7714. NULL,
  7715. LLM_FFN_SILU, LLM_FFN_PAR, il);
  7716. cb(cur, "ffn_out", il);
  7717. cur = ggml_add(ctx0, cur, ffn_inp);
  7718. cur = build_cvec(cur, il);
  7719. cb(cur, "l_out", il);
  7720. // input for next layer
  7721. inpL = cur;
  7722. }
  7723. cur = inpL;
  7724. cur = build_norm(cur,
  7725. model.output_norm, NULL,
  7726. LLM_NORM_RMS, -1);
  7727. cb(cur, "result_norm", -1);
  7728. res->t_embd = cur;
  7729. // lm_head
  7730. cur = build_lora_mm(model.output, cur);
  7731. cb(cur, "result_output", -1);
  7732. res->t_logits = cur;
  7733. ggml_build_forward_expand(gf, cur);
  7734. }
  7735. };
  7736. struct llm_build_qwen3moe : public llm_graph_context {
  7737. llm_build_qwen3moe(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  7738. const int64_t n_embd_head = hparams.n_embd_head_v;
  7739. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  7740. GGML_ASSERT(n_embd_head == hparams.n_rot);
  7741. ggml_tensor * cur;
  7742. ggml_tensor * inpL;
  7743. inpL = build_inp_embd(model.tok_embd);
  7744. // inp_pos - contains the positions
  7745. ggml_tensor * inp_pos = build_inp_pos();
  7746. auto * inp_attn = build_attn_inp_kv();
  7747. ggml_tensor * inp_out_ids = build_inp_out_ids();
  7748. for (int il = 0; il < n_layer; ++il) {
  7749. ggml_tensor * inpSA = inpL;
  7750. // norm
  7751. cur = build_norm(inpL,
  7752. model.layers[il].attn_norm, NULL,
  7753. LLM_NORM_RMS, il);
  7754. cb(cur, "attn_norm", il);
  7755. // self_attention
  7756. {
  7757. // compute Q and K and RoPE them
  7758. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  7759. cb(Qcur, "Qcur", il);
  7760. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  7761. cb(Kcur, "Kcur", il);
  7762. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  7763. cb(Vcur, "Vcur", il);
  7764. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  7765. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  7766. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  7767. Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);
  7768. cb(Qcur, "Qcur_normed", il);
  7769. Qcur = ggml_rope_ext(
  7770. ctx0, Qcur, inp_pos, nullptr,
  7771. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  7772. ext_factor, attn_factor, beta_fast, beta_slow
  7773. );
  7774. Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);
  7775. cb(Kcur, "Kcur_normed", il);
  7776. Kcur = ggml_rope_ext(
  7777. ctx0, Kcur, inp_pos, nullptr,
  7778. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  7779. ext_factor, attn_factor, beta_fast, beta_slow
  7780. );
  7781. cb(Qcur, "Qcur", il);
  7782. cb(Kcur, "Kcur", il);
  7783. cb(Vcur, "Vcur", il);
  7784. cur = build_attn(inp_attn,
  7785. model.layers[il].wo, model.layers[il].bo,
  7786. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  7787. }
  7788. if (il == n_layer - 1 && inp_out_ids) {
  7789. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  7790. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  7791. }
  7792. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  7793. cb(ffn_inp, "ffn_inp", il);
  7794. // MoE branch
  7795. cur = build_norm(ffn_inp,
  7796. model.layers[il].ffn_norm, NULL,
  7797. LLM_NORM_RMS, il);
  7798. cb(cur, "ffn_norm", il);
  7799. ggml_tensor * moe_out =
  7800. build_moe_ffn(cur,
  7801. model.layers[il].ffn_gate_inp,
  7802. model.layers[il].ffn_up_exps,
  7803. model.layers[il].ffn_gate_exps,
  7804. model.layers[il].ffn_down_exps,
  7805. nullptr,
  7806. n_expert, n_expert_used,
  7807. LLM_FFN_SILU, true,
  7808. false, 0.0,
  7809. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  7810. il);
  7811. cb(moe_out, "ffn_moe_out", il);
  7812. cur = moe_out;
  7813. cur = ggml_add(ctx0, cur, ffn_inp);
  7814. cur = build_cvec(cur, il);
  7815. cb(cur, "l_out", il);
  7816. // input for next layer
  7817. inpL = cur;
  7818. }
  7819. cur = inpL;
  7820. cur = build_norm(cur,
  7821. model.output_norm, NULL,
  7822. LLM_NORM_RMS, -1);
  7823. cb(cur, "result_norm", -1);
  7824. res->t_embd = cur;
  7825. // lm_head
  7826. cur = build_lora_mm(model.output, cur);
  7827. cb(cur, "result_output", -1);
  7828. res->t_logits = cur;
  7829. ggml_build_forward_expand(gf, cur);
  7830. }
  7831. };
  7832. struct llm_build_phi2 : public llm_graph_context {
  7833. llm_build_phi2(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  7834. const int64_t n_embd_head = hparams.n_embd_head_v;
  7835. const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  7836. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  7837. ggml_tensor * cur;
  7838. ggml_tensor * attn_norm_output;
  7839. ggml_tensor * ffn_output;
  7840. ggml_tensor * inpL;
  7841. inpL = build_inp_embd(model.tok_embd);
  7842. // inp_pos - contains the positions
  7843. ggml_tensor * inp_pos = build_inp_pos();
  7844. auto * inp_attn = build_attn_inp_kv();
  7845. ggml_tensor * inp_out_ids = build_inp_out_ids();
  7846. for (int il = 0; il < n_layer; ++il) {
  7847. attn_norm_output = build_norm(inpL,
  7848. model.layers[il].attn_norm,
  7849. model.layers[il].attn_norm_b,
  7850. LLM_NORM, il);
  7851. cb(attn_norm_output, "attn_norm", il);
  7852. // self-attention
  7853. {
  7854. ggml_tensor * Qcur = nullptr;
  7855. ggml_tensor * Kcur = nullptr;
  7856. ggml_tensor * Vcur = nullptr;
  7857. if (model.layers[il].wqkv) {
  7858. cur = build_lora_mm(model.layers[il].wqkv, attn_norm_output);
  7859. cb(cur, "wqkv", il);
  7860. cur = ggml_add(ctx0, cur, model.layers[il].bqkv);
  7861. cb(cur, "bqkv", il);
  7862. 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));
  7863. 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));
  7864. 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));
  7865. } else {
  7866. Qcur = ggml_add(ctx0, build_lora_mm(model.layers[il].wq, attn_norm_output), model.layers[il].bq);
  7867. Kcur = ggml_add(ctx0, build_lora_mm(model.layers[il].wk, attn_norm_output), model.layers[il].bk);
  7868. Vcur = ggml_add(ctx0, build_lora_mm(model.layers[il].wv, attn_norm_output), model.layers[il].bv);
  7869. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  7870. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  7871. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  7872. }
  7873. Qcur = ggml_rope_ext(
  7874. ctx0, Qcur, inp_pos, nullptr,
  7875. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  7876. ext_factor, attn_factor, beta_fast, beta_slow
  7877. );
  7878. Kcur = ggml_rope_ext(
  7879. ctx0, Kcur, inp_pos, nullptr,
  7880. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  7881. ext_factor, attn_factor, beta_fast, beta_slow
  7882. );
  7883. cb(Qcur, "Qcur", il);
  7884. cb(Kcur, "Kcur", il);
  7885. cb(Vcur, "Vcur", il);
  7886. // with phi2, we scale the Q to avoid precision issues
  7887. // ref: https://github.com/ml-explore/mlx-examples/blob/08e862336ade809bc37d1035f94b359e7d1a5152/phi2/phi2.py#L64-L66
  7888. Qcur = ggml_scale(ctx0, Qcur, 1.0f/sqrtf(float(n_embd_head)));
  7889. cur = build_attn(inp_attn,
  7890. model.layers[il].wo, model.layers[il].bo,
  7891. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f, il);
  7892. }
  7893. if (il == n_layer - 1 && inp_out_ids) {
  7894. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  7895. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  7896. attn_norm_output = ggml_get_rows(ctx0, attn_norm_output, inp_out_ids);
  7897. }
  7898. // FF
  7899. {
  7900. ffn_output = build_ffn(attn_norm_output,
  7901. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  7902. NULL, NULL, NULL,
  7903. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  7904. NULL,
  7905. LLM_FFN_GELU, LLM_FFN_SEQ, il);
  7906. cb(ffn_output, "ffn_out", il);
  7907. }
  7908. cur = ggml_add(ctx0, cur, ffn_output);
  7909. cur = ggml_add(ctx0, cur, inpL);
  7910. cur = build_cvec(cur, il);
  7911. cb(cur, "l_out", il);
  7912. // input for next layer
  7913. inpL = cur;
  7914. }
  7915. cur = build_norm(inpL,
  7916. model.output_norm,
  7917. model.output_norm_b,
  7918. LLM_NORM, -1);
  7919. cb(cur, "result_norm", -1);
  7920. res->t_embd = cur;
  7921. cur = build_lora_mm(model.output, cur);
  7922. cb(cur, "result_output_no_bias", -1);
  7923. cur = ggml_add(ctx0, cur, model.output_b);
  7924. cb(cur, "result_output", -1);
  7925. res->t_logits = cur;
  7926. ggml_build_forward_expand(gf, cur);
  7927. }
  7928. };
  7929. template<bool iswa>
  7930. struct llm_build_phi3 : public llm_graph_context {
  7931. llm_build_phi3(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  7932. const int64_t n_embd_head = hparams.n_embd_head_v;
  7933. const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  7934. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  7935. ggml_tensor * cur;
  7936. ggml_tensor * inpL;
  7937. inpL = build_inp_embd(model.tok_embd);
  7938. // inp_pos - contains the positions
  7939. ggml_tensor * inp_pos = build_inp_pos();
  7940. using inp_attn_type = std::conditional_t<iswa, llm_graph_input_attn_kv_iswa, llm_graph_input_attn_kv>;
  7941. inp_attn_type * inp_attn = nullptr;
  7942. if constexpr (iswa) {
  7943. inp_attn = build_attn_inp_kv_iswa();
  7944. } else {
  7945. inp_attn = build_attn_inp_kv();
  7946. }
  7947. ggml_tensor * inp_out_ids = build_inp_out_ids();
  7948. for (int il = 0; il < n_layer; ++il) {
  7949. auto * residual = inpL;
  7950. // self-attention
  7951. {
  7952. // rope freq factors for 128k context
  7953. ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
  7954. ggml_tensor* attn_norm_output = build_norm(inpL,
  7955. model.layers[il].attn_norm,
  7956. model.layers[il].attn_norm_b,
  7957. LLM_NORM_RMS, il);
  7958. cb(attn_norm_output, "attn_norm", il);
  7959. ggml_tensor * Qcur = nullptr;
  7960. ggml_tensor * Kcur = nullptr;
  7961. ggml_tensor * Vcur = nullptr;
  7962. if (model.layers[il].wqkv) {
  7963. cur = build_lora_mm(model.layers[il].wqkv, attn_norm_output);
  7964. cb(cur, "wqkv", il);
  7965. 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));
  7966. 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));
  7967. 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));
  7968. } else {
  7969. Qcur = ggml_add(ctx0, build_lora_mm(model.layers[il].wq, attn_norm_output), model.layers[il].bq);
  7970. Kcur = ggml_add(ctx0, build_lora_mm(model.layers[il].wk, attn_norm_output), model.layers[il].bk);
  7971. Vcur = ggml_add(ctx0, build_lora_mm(model.layers[il].wv, attn_norm_output), model.layers[il].bv);
  7972. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  7973. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  7974. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  7975. }
  7976. Qcur = ggml_rope_ext(
  7977. ctx0, Qcur, inp_pos, rope_factors,
  7978. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  7979. ext_factor, attn_factor, beta_fast, beta_slow
  7980. );
  7981. Kcur = ggml_rope_ext(
  7982. ctx0, Kcur, inp_pos, rope_factors,
  7983. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  7984. ext_factor, attn_factor, beta_fast, beta_slow
  7985. );
  7986. cb(Qcur, "Qcur", il);
  7987. cb(Kcur, "Kcur", il);
  7988. cb(Vcur, "Vcur", il);
  7989. Qcur = ggml_scale(ctx0, Qcur, 1.0f / sqrtf(float(n_embd_head)));
  7990. cb(Qcur, "Qcur", il);
  7991. cur = build_attn(inp_attn,
  7992. model.layers[il].wo, model.layers[il].bo,
  7993. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f, il);
  7994. }
  7995. if (il == n_layer - 1 && inp_out_ids) {
  7996. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  7997. residual = ggml_get_rows(ctx0, residual, inp_out_ids);
  7998. }
  7999. cur = ggml_add(ctx0, cur, residual);
  8000. residual = cur;
  8001. cur = build_norm(cur,
  8002. model.layers[il].ffn_norm, model.layers[il].ffn_norm_b,
  8003. LLM_NORM_RMS, il);
  8004. cb(cur, "ffn_norm", il);
  8005. // feed-forward network
  8006. if (model.layers[il].ffn_gate_inp == nullptr) {
  8007. cur = build_ffn(cur,
  8008. model.layers[il].ffn_up, NULL, NULL,
  8009. NULL, NULL, NULL,
  8010. model.layers[il].ffn_down, NULL, NULL,
  8011. NULL,
  8012. LLM_FFN_SWIGLU, LLM_FFN_SEQ, il);
  8013. cb(cur, "ffn_out", il);
  8014. } else {
  8015. // MoE branch
  8016. cur = build_moe_ffn(cur,
  8017. model.layers[il].ffn_gate_inp,
  8018. model.layers[il].ffn_up_exps,
  8019. model.layers[il].ffn_gate_exps,
  8020. model.layers[il].ffn_down_exps,
  8021. nullptr,
  8022. n_expert, n_expert_used,
  8023. LLM_FFN_SILU, true,
  8024. false, 0.0,
  8025. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  8026. il);
  8027. cb(cur, "ffn_moe_out", il);
  8028. }
  8029. cur = ggml_add(ctx0, residual, cur);
  8030. cur = build_cvec(cur, il);
  8031. cb(cur, "l_out", il);
  8032. // input for next layer
  8033. inpL = cur;
  8034. }
  8035. cur = build_norm(inpL,
  8036. model.output_norm,
  8037. model.output_norm_b,
  8038. LLM_NORM_RMS, -1);
  8039. cb(cur, "result_norm", -1);
  8040. res->t_embd = cur;
  8041. cur = build_lora_mm(model.output, cur);
  8042. if (model.output_b != nullptr) {
  8043. cb(cur, "result_output_no_bias", -1);
  8044. cur = ggml_add(ctx0, cur, model.output_b);
  8045. }
  8046. cb(cur, "result_output", -1);
  8047. res->t_logits = cur;
  8048. ggml_build_forward_expand(gf, cur);
  8049. }
  8050. };
  8051. struct llm_build_plamo : public llm_graph_context {
  8052. llm_build_plamo(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  8053. const int64_t n_embd_head = hparams.n_embd_head_v;
  8054. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  8055. GGML_ASSERT(n_embd_head == hparams.n_rot);
  8056. ggml_tensor * cur;
  8057. ggml_tensor * inpL;
  8058. inpL = build_inp_embd(model.tok_embd);
  8059. // inp_pos - contains the positions
  8060. ggml_tensor * inp_pos = build_inp_pos();
  8061. auto * inp_attn = build_attn_inp_kv();
  8062. ggml_tensor * inp_out_ids = build_inp_out_ids();
  8063. for (int il = 0; il < n_layer; ++il) {
  8064. // norm
  8065. cur = build_norm(inpL,
  8066. model.layers[il].attn_norm, NULL,
  8067. LLM_NORM_RMS, il);
  8068. cb(cur, "attn_norm", il);
  8069. ggml_tensor * sa_inp = cur;
  8070. // self-attention
  8071. {
  8072. // compute Q and K and RoPE them
  8073. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  8074. cb(Qcur, "Qcur", il);
  8075. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  8076. cb(Kcur, "Kcur", il);
  8077. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  8078. cb(Vcur, "Vcur", il);
  8079. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  8080. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  8081. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  8082. Qcur = ggml_rope_ext(
  8083. ctx0, Qcur, inp_pos, nullptr,
  8084. n_embd_head, rope_type, n_ctx_orig, freq_base, freq_scale,
  8085. ext_factor, attn_factor, beta_fast, beta_slow
  8086. );
  8087. Kcur = ggml_rope_ext(
  8088. ctx0, Kcur, inp_pos, nullptr,
  8089. n_embd_head, rope_type, n_ctx_orig, freq_base, freq_scale,
  8090. ext_factor, attn_factor, beta_fast, beta_slow
  8091. );
  8092. cb(Qcur, "Qcur", il);
  8093. cb(Kcur, "Kcur", il);
  8094. cb(Vcur, "Vcur", il);
  8095. cur = build_attn(inp_attn,
  8096. model.layers[il].wo, NULL,
  8097. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  8098. }
  8099. if (il == n_layer - 1 && inp_out_ids) {
  8100. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  8101. sa_inp = ggml_get_rows(ctx0, sa_inp, inp_out_ids);
  8102. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  8103. }
  8104. ggml_tensor * sa_out = cur;
  8105. cur = sa_inp;
  8106. // feed-forward network
  8107. {
  8108. cur = build_ffn(cur,
  8109. model.layers[il].ffn_up, NULL, NULL,
  8110. model.layers[il].ffn_gate, NULL, NULL,
  8111. model.layers[il].ffn_down, NULL, NULL,
  8112. NULL,
  8113. LLM_FFN_SILU, LLM_FFN_PAR, il);
  8114. cb(cur, "ffn_out", il);
  8115. }
  8116. cur = ggml_add(ctx0, cur, sa_out);
  8117. cur = ggml_add(ctx0, cur, inpL);
  8118. cur = build_cvec(cur, il);
  8119. cb(cur, "l_out", il);
  8120. // input for next layer
  8121. inpL = cur;
  8122. }
  8123. cur = inpL;
  8124. cur = build_norm(cur,
  8125. model.output_norm, NULL,
  8126. LLM_NORM_RMS, -1);
  8127. cb(cur, "result_norm", -1);
  8128. res->t_embd = cur;
  8129. // lm_head
  8130. cur = build_lora_mm(model.output, cur);
  8131. cb(cur, "result_output", -1);
  8132. res->t_logits = cur;
  8133. ggml_build_forward_expand(gf, cur);
  8134. }
  8135. };
  8136. struct llm_build_gpt2 : public llm_graph_context {
  8137. llm_build_gpt2(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  8138. const int64_t n_embd_head = hparams.n_embd_head_v;
  8139. const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  8140. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  8141. ggml_tensor * cur;
  8142. ggml_tensor * pos;
  8143. ggml_tensor * inpL;
  8144. inpL = build_inp_embd(model.tok_embd);
  8145. // inp_pos - contains the positions
  8146. ggml_tensor * inp_pos = build_inp_pos();
  8147. auto * inp_attn = build_attn_inp_kv();
  8148. pos = ggml_get_rows(ctx0, model.pos_embd, inp_pos);
  8149. cb(pos, "pos_embd", -1);
  8150. inpL = ggml_add(ctx0, inpL, pos);
  8151. cb(inpL, "inpL", -1);
  8152. ggml_tensor * inp_out_ids = build_inp_out_ids();
  8153. for (int il = 0; il < n_layer; ++il) {
  8154. cur = build_norm(inpL,
  8155. model.layers[il].attn_norm,
  8156. model.layers[il].attn_norm_b,
  8157. LLM_NORM, il);
  8158. cb(cur, "attn_norm", il);
  8159. // self-attention
  8160. {
  8161. cur = build_lora_mm(model.layers[il].wqkv, cur);
  8162. cb(cur, "wqkv", il);
  8163. cur = ggml_add(ctx0, cur, model.layers[il].bqkv);
  8164. cb(cur, "bqkv", il);
  8165. 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));
  8166. 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));
  8167. 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));
  8168. cb(Qcur, "Qcur", il);
  8169. cb(Kcur, "Kcur", il);
  8170. cb(Vcur, "Vcur", il);
  8171. cur = build_attn(inp_attn,
  8172. model.layers[il].wo, model.layers[il].bo,
  8173. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  8174. }
  8175. if (il == n_layer - 1 && inp_out_ids) {
  8176. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  8177. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  8178. }
  8179. // add the input
  8180. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
  8181. cb(ffn_inp, "ffn_inp", il);
  8182. // FF
  8183. {
  8184. cur = build_norm(ffn_inp,
  8185. model.layers[il].ffn_norm,
  8186. model.layers[il].ffn_norm_b,
  8187. LLM_NORM, il);
  8188. cb(cur, "ffn_norm", il);
  8189. cur = build_ffn(cur,
  8190. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  8191. NULL, NULL, NULL,
  8192. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  8193. NULL,
  8194. LLM_FFN_GELU, LLM_FFN_SEQ, il);
  8195. cb(cur, "ffn_out", il);
  8196. }
  8197. cur = ggml_add(ctx0, cur, ffn_inp);
  8198. cur = build_cvec(cur, il);
  8199. cb(cur, "l_out", il);
  8200. // input for next layer
  8201. inpL = cur;
  8202. }
  8203. cur = build_norm(inpL,
  8204. model.output_norm,
  8205. model.output_norm_b,
  8206. LLM_NORM, -1);
  8207. cb(cur, "result_norm", -1);
  8208. res->t_embd = cur;
  8209. cur = build_lora_mm(model.output, cur);
  8210. cb(cur, "result_output", -1);
  8211. res->t_logits = cur;
  8212. ggml_build_forward_expand(gf, cur);
  8213. }
  8214. };
  8215. struct llm_build_codeshell : public llm_graph_context {
  8216. llm_build_codeshell(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  8217. const int64_t n_embd_head = hparams.n_embd_head_v;
  8218. const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  8219. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  8220. GGML_ASSERT(n_embd_head == hparams.n_rot);
  8221. ggml_tensor * cur;
  8222. ggml_tensor * inpL;
  8223. inpL = build_inp_embd(model.tok_embd);
  8224. // inp_pos - contains the positions
  8225. ggml_tensor * inp_pos = build_inp_pos();
  8226. auto * inp_attn = build_attn_inp_kv();
  8227. ggml_tensor * inp_out_ids = build_inp_out_ids();
  8228. for (int il = 0; il < n_layer; ++il) {
  8229. cur = build_norm(inpL,
  8230. model.layers[il].attn_norm,
  8231. model.layers[il].attn_norm_b,
  8232. LLM_NORM, il);
  8233. cb(cur, "attn_norm", il);
  8234. // self-attention
  8235. {
  8236. cur = build_lora_mm(model.layers[il].wqkv, cur);
  8237. cb(cur, "wqkv", il);
  8238. cur = ggml_add(ctx0, cur, model.layers[il].bqkv);
  8239. cb(cur, "bqkv", il);
  8240. 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));
  8241. 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));
  8242. 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));
  8243. Qcur = ggml_rope_ext(
  8244. ctx0, Qcur, inp_pos, nullptr,
  8245. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  8246. ext_factor, attn_factor, beta_fast, beta_slow
  8247. );
  8248. Kcur = ggml_rope_ext(
  8249. ctx0, Kcur, inp_pos, nullptr,
  8250. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  8251. ext_factor, attn_factor, beta_fast, beta_slow
  8252. );
  8253. cb(Qcur, "Qcur", il);
  8254. cb(Kcur, "Kcur", il);
  8255. cb(Vcur, "Vcur", il);
  8256. cur = build_attn(inp_attn,
  8257. model.layers[il].wo, model.layers[il].bo,
  8258. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  8259. }
  8260. if (il == n_layer - 1 && inp_out_ids) {
  8261. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  8262. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  8263. }
  8264. // add the input
  8265. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
  8266. cb(ffn_inp, "ffn_inp", il);
  8267. // FF
  8268. {
  8269. cur = build_norm(ffn_inp,
  8270. model.layers[il].ffn_norm,
  8271. model.layers[il].ffn_norm_b,
  8272. LLM_NORM, il);
  8273. cb(cur, "ffn_norm", il);
  8274. cur = build_ffn(cur,
  8275. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  8276. NULL, NULL, NULL,
  8277. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  8278. NULL,
  8279. LLM_FFN_GELU, LLM_FFN_SEQ, il);
  8280. cb(cur, "ffn_out", il);
  8281. }
  8282. cur = ggml_add(ctx0, cur, ffn_inp);
  8283. cur = build_cvec(cur, il);
  8284. cb(cur, "l_out", il);
  8285. // input for next layer
  8286. inpL = cur;
  8287. }
  8288. cur = build_norm(inpL,
  8289. model.output_norm,
  8290. model.output_norm_b,
  8291. LLM_NORM, -1);
  8292. cb(cur, "result_norm", -1);
  8293. res->t_embd = cur;
  8294. cur = build_lora_mm(model.output, cur);
  8295. cb(cur, "result_output", -1);
  8296. res->t_logits = cur;
  8297. ggml_build_forward_expand(gf, cur);
  8298. }
  8299. };
  8300. struct llm_build_orion : public llm_graph_context {
  8301. llm_build_orion(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  8302. const int64_t n_embd_head = hparams.n_embd_head_v;
  8303. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  8304. GGML_ASSERT(n_embd_head == hparams.n_rot);
  8305. ggml_tensor * cur;
  8306. ggml_tensor * inpL;
  8307. inpL = build_inp_embd(model.tok_embd);
  8308. // inp_pos - contains the positions
  8309. ggml_tensor * inp_pos = build_inp_pos();
  8310. auto * inp_attn = build_attn_inp_kv();
  8311. ggml_tensor * inp_out_ids = build_inp_out_ids();
  8312. for (int il = 0; il < n_layer; ++il) {
  8313. ggml_tensor * inpSA = inpL;
  8314. // norm
  8315. cur = build_norm(inpL,
  8316. model.layers[il].attn_norm, model.layers[il].attn_norm_b,
  8317. LLM_NORM, il);
  8318. cb(cur, "attn_norm", il);
  8319. // self-attention
  8320. {
  8321. // compute Q and K and RoPE them
  8322. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  8323. cb(Qcur, "Qcur", il);
  8324. // if (model.layers[il].bq) {
  8325. // Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  8326. // cb(Qcur, "Qcur", il);
  8327. // }
  8328. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  8329. cb(Kcur, "Kcur", il);
  8330. // if (model.layers[il].bk) {
  8331. // Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  8332. // cb(Kcur, "Kcur", il);
  8333. // }
  8334. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  8335. cb(Vcur, "Vcur", il);
  8336. // if (model.layers[il].bv) {
  8337. // Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  8338. // cb(Vcur, "Vcur", il);
  8339. // }
  8340. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  8341. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  8342. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  8343. Qcur = ggml_rope_ext(
  8344. ctx0, Qcur, inp_pos, nullptr,
  8345. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  8346. ext_factor, attn_factor, beta_fast, beta_slow
  8347. );
  8348. Kcur = ggml_rope_ext(
  8349. ctx0, Kcur, inp_pos, nullptr,
  8350. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  8351. ext_factor, attn_factor, beta_fast, beta_slow
  8352. );
  8353. cb(Qcur, "Qcur", il);
  8354. cb(Kcur, "Kcur", il);
  8355. cb(Vcur, "Vcur", il);
  8356. cur = build_attn(inp_attn,
  8357. model.layers[il].wo, NULL,
  8358. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  8359. }
  8360. if (il == n_layer - 1 && inp_out_ids) {
  8361. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  8362. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  8363. }
  8364. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  8365. cb(ffn_inp, "ffn_inp", il);
  8366. // feed-forward network
  8367. cur = build_norm(ffn_inp,
  8368. model.layers[il].ffn_norm, model.layers[il].ffn_norm_b,
  8369. LLM_NORM, il);
  8370. cb(cur, "ffn_norm", il);
  8371. cur = build_ffn(cur,
  8372. model.layers[il].ffn_up, NULL, NULL,
  8373. model.layers[il].ffn_gate, NULL, NULL,
  8374. model.layers[il].ffn_down, NULL, NULL,
  8375. NULL,
  8376. LLM_FFN_SILU, LLM_FFN_PAR, il);
  8377. cb(cur, "ffn_out", il);
  8378. cur = ggml_add(ctx0, cur, ffn_inp);
  8379. cur = build_cvec(cur, il);
  8380. cb(cur, "l_out", il);
  8381. // input for next layer
  8382. inpL = cur;
  8383. }
  8384. cur = inpL;
  8385. cur = build_norm(cur,
  8386. model.output_norm, model.output_norm_b,
  8387. LLM_NORM, -1);
  8388. cb(cur, "result_norm", -1);
  8389. res->t_embd = cur;
  8390. // lm_head
  8391. cur = build_lora_mm(model.output, cur);
  8392. cb(cur, "result_output", -1);
  8393. res->t_logits = cur;
  8394. ggml_build_forward_expand(gf, cur);
  8395. }
  8396. };
  8397. struct llm_build_internlm2 : public llm_graph_context {
  8398. llm_build_internlm2(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  8399. const int64_t n_embd_head = hparams.n_embd_head_v;
  8400. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  8401. GGML_ASSERT(n_embd_head == hparams.n_rot);
  8402. ggml_tensor * cur;
  8403. ggml_tensor * inpL;
  8404. inpL = build_inp_embd(model.tok_embd);
  8405. // inp_pos - contains the positions
  8406. ggml_tensor * inp_pos = build_inp_pos();
  8407. auto * inp_attn = build_attn_inp_kv();
  8408. ggml_tensor * inp_out_ids = build_inp_out_ids();
  8409. for (int il = 0; il < n_layer; ++il) {
  8410. ggml_tensor * inpSA = inpL;
  8411. // norm
  8412. cur = build_norm(inpL,
  8413. model.layers[il].attn_norm, NULL,
  8414. LLM_NORM_RMS, il);
  8415. cb(cur, "attn_norm", il);
  8416. // self-attention
  8417. {
  8418. // compute Q and K and RoPE them
  8419. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  8420. cb(Qcur, "Qcur", il);
  8421. if (model.layers[il].bq) {
  8422. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  8423. cb(Qcur, "Qcur", il);
  8424. }
  8425. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  8426. cb(Kcur, "Kcur", il);
  8427. if (model.layers[il].bk) {
  8428. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  8429. cb(Kcur, "Kcur", il);
  8430. }
  8431. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  8432. cb(Vcur, "Vcur", il);
  8433. if (model.layers[il].bv) {
  8434. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  8435. cb(Vcur, "Vcur", il);
  8436. }
  8437. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  8438. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  8439. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  8440. Qcur = ggml_rope_ext(
  8441. ctx0, Qcur, inp_pos, nullptr,
  8442. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  8443. ext_factor, attn_factor, beta_fast, beta_slow
  8444. );
  8445. Kcur = ggml_rope_ext(
  8446. ctx0, Kcur, inp_pos, nullptr,
  8447. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  8448. ext_factor, attn_factor, beta_fast, beta_slow
  8449. );
  8450. cb(Qcur, "Qcur", il);
  8451. cb(Kcur, "Kcur", il);
  8452. cb(Vcur, "Vcur", il);
  8453. cur = build_attn(inp_attn,
  8454. model.layers[il].wo, model.layers[il].bo,
  8455. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  8456. }
  8457. if (il == n_layer - 1 && inp_out_ids) {
  8458. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  8459. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  8460. }
  8461. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  8462. cb(ffn_inp, "ffn_inp", il);
  8463. // feed-forward network
  8464. cur = build_norm(ffn_inp,
  8465. model.layers[il].ffn_norm, NULL,
  8466. LLM_NORM_RMS, il);
  8467. cb(cur, "ffn_norm", il);
  8468. cur = build_ffn(cur,
  8469. model.layers[il].ffn_up, NULL, NULL,
  8470. model.layers[il].ffn_gate, NULL, NULL,
  8471. model.layers[il].ffn_down, NULL, NULL,
  8472. NULL,
  8473. LLM_FFN_SILU, LLM_FFN_PAR, il);
  8474. cb(cur, "ffn_out", il);
  8475. cur = ggml_add(ctx0, cur, ffn_inp);
  8476. cur = build_cvec(cur, il);
  8477. cb(cur, "l_out", il);
  8478. // input for next layer
  8479. inpL = cur;
  8480. }
  8481. cur = inpL;
  8482. cur = build_norm(cur,
  8483. model.output_norm, NULL,
  8484. LLM_NORM_RMS, -1);
  8485. cb(cur, "result_norm", -1);
  8486. res->t_embd = cur;
  8487. // lm_head
  8488. cur = build_lora_mm(model.output, cur);
  8489. cb(cur, "result_output", -1);
  8490. res->t_logits = cur;
  8491. ggml_build_forward_expand(gf, cur);
  8492. }
  8493. };
  8494. struct llm_build_minicpm3 : public llm_graph_context {
  8495. llm_build_minicpm3(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  8496. //TODO: if the model varies, these parameters need to be read from the model
  8497. const int64_t n_embd_base = 256;
  8498. const float scale_embd = 12.0f;
  8499. const float scale_depth = 1.4f;
  8500. const float kq_scale = 1.0f / sqrtf(float(hparams.n_embd_head_k));
  8501. const uint32_t n_embd_head_qk_rope = hparams.n_rot;
  8502. const uint32_t n_embd_head_qk_nope = hparams.n_embd_head_k - hparams.n_rot;
  8503. const uint32_t kv_lora_rank = hparams.n_lora_kv;
  8504. ggml_tensor * cur;
  8505. ggml_tensor * inpL;
  8506. inpL = build_inp_embd(model.tok_embd);
  8507. // scale the input embeddings
  8508. inpL = ggml_scale(ctx0, inpL, scale_embd);
  8509. cb(inpL, "inp_scaled", -1);
  8510. // inp_pos - contains the positions
  8511. ggml_tensor * inp_pos = build_inp_pos();
  8512. auto * inp_attn = build_attn_inp_kv();
  8513. ggml_tensor * inp_out_ids = build_inp_out_ids();
  8514. for (int il = 0; il < n_layer; ++il) {
  8515. ggml_tensor * inpSA = inpL;
  8516. ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
  8517. // norm
  8518. cur = build_norm(inpL,
  8519. model.layers[il].attn_norm, NULL,
  8520. LLM_NORM_RMS, il);
  8521. cb(cur, "attn_norm", il);
  8522. // self_attention
  8523. {
  8524. ggml_tensor * q = NULL;
  8525. // {n_embd, q_lora_rank} * {n_embd, n_tokens} -> {q_lora_rank, n_tokens}
  8526. q = ggml_mul_mat(ctx0, model.layers[il].wq_a, cur);
  8527. cb(q, "q", il);
  8528. q = build_norm(q,
  8529. model.layers[il].attn_q_a_norm, NULL,
  8530. LLM_NORM_RMS, il);
  8531. cb(q, "q", il);
  8532. // {q_lora_rank, n_head * hparams.n_embd_head_k} * {q_lora_rank, n_tokens} -> {n_head * hparams.n_embd_head_k, n_tokens}
  8533. q = ggml_mul_mat(ctx0, model.layers[il].wq_b, q);
  8534. cb(q, "q", il);
  8535. // split into {n_head * n_embd_head_qk_nope, n_tokens}
  8536. ggml_tensor * q_nope = ggml_view_3d(ctx0, q, n_embd_head_qk_nope, n_head, n_tokens,
  8537. ggml_row_size(q->type, hparams.n_embd_head_k),
  8538. ggml_row_size(q->type, hparams.n_embd_head_k * n_head),
  8539. 0);
  8540. cb(q_nope, "q_nope", il);
  8541. // and {n_head * n_embd_head_qk_rope, n_tokens}
  8542. ggml_tensor * q_pe = ggml_view_3d(ctx0, q, n_embd_head_qk_rope, n_head, n_tokens,
  8543. ggml_row_size(q->type, hparams.n_embd_head_k),
  8544. ggml_row_size(q->type, hparams.n_embd_head_k * n_head),
  8545. ggml_row_size(q->type, n_embd_head_qk_nope));
  8546. cb(q_pe, "q_pe", il);
  8547. // {n_embd, kv_lora_rank + n_embd_head_qk_rope} * {n_embd, n_tokens} -> {kv_lora_rank + n_embd_head_qk_rope, n_tokens}
  8548. ggml_tensor * kv_pe_compresseed = ggml_mul_mat(ctx0, model.layers[il].wkv_a_mqa, cur);
  8549. cb(kv_pe_compresseed, "kv_pe_compresseed", il);
  8550. // split into {kv_lora_rank, n_tokens}
  8551. ggml_tensor * kv_compressed = ggml_view_2d(ctx0, kv_pe_compresseed, kv_lora_rank, n_tokens,
  8552. kv_pe_compresseed->nb[1],
  8553. 0);
  8554. cb(kv_compressed, "kv_compressed", il);
  8555. // and {n_embd_head_qk_rope, n_tokens}
  8556. ggml_tensor * k_pe = ggml_view_3d(ctx0, kv_pe_compresseed, n_embd_head_qk_rope, 1, n_tokens,
  8557. kv_pe_compresseed->nb[1],
  8558. kv_pe_compresseed->nb[1],
  8559. ggml_row_size(kv_pe_compresseed->type, kv_lora_rank));
  8560. cb(k_pe, "k_pe", il);
  8561. kv_compressed = build_norm(kv_compressed,
  8562. model.layers[il].attn_kv_a_norm, NULL,
  8563. LLM_NORM_RMS, il);
  8564. cb(kv_compressed, "kv_compressed", il);
  8565. // {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}
  8566. ggml_tensor * kv = ggml_mul_mat(ctx0, model.layers[il].wkv_b, kv_compressed);
  8567. cb(kv, "kv", il);
  8568. // split into {n_head * n_embd_head_qk_nope, n_tokens}
  8569. ggml_tensor * k_nope = ggml_view_3d(ctx0, kv, n_embd_head_qk_nope, n_head, n_tokens,
  8570. ggml_row_size(kv->type, n_embd_head_qk_nope + hparams.n_embd_head_v),
  8571. ggml_row_size(kv->type, n_head * (n_embd_head_qk_nope + hparams.n_embd_head_v)),
  8572. 0);
  8573. cb(k_nope, "k_nope", il);
  8574. // and {n_head * n_embd_head_v, n_tokens}
  8575. ggml_tensor * v_states = ggml_view_3d(ctx0, kv, hparams.n_embd_head_v, n_head, n_tokens,
  8576. ggml_row_size(kv->type, (n_embd_head_qk_nope + hparams.n_embd_head_v)),
  8577. ggml_row_size(kv->type, (n_embd_head_qk_nope + hparams.n_embd_head_v)*n_head),
  8578. ggml_row_size(kv->type, (n_embd_head_qk_nope)));
  8579. cb(v_states, "v_states", il);
  8580. v_states = ggml_cont(ctx0, v_states);
  8581. cb(v_states, "v_states", il);
  8582. q_pe = ggml_rope_ext(
  8583. ctx0, q_pe, inp_pos, rope_factors,
  8584. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  8585. ext_factor, attn_factor, beta_fast, beta_slow
  8586. );
  8587. cb(q_pe, "q_pe", il);
  8588. // shared RoPE key
  8589. k_pe = ggml_rope_ext(
  8590. ctx0, k_pe, inp_pos, rope_factors,
  8591. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  8592. ext_factor, attn_factor, beta_fast, beta_slow
  8593. );
  8594. cb(k_pe, "k_pe", il);
  8595. ggml_tensor * q_states = ggml_concat(ctx0, q_nope, q_pe, 0);
  8596. cb(q_states, "q_states", il);
  8597. ggml_tensor * k_states = ggml_concat(ctx0, k_nope, ggml_repeat(ctx0, k_pe, q_pe), 0);
  8598. cb(k_states, "k_states", il);
  8599. cur = build_attn(inp_attn,
  8600. model.layers[il].wo, NULL,
  8601. q_states, k_states, v_states, nullptr, nullptr, nullptr, kq_scale, il);
  8602. }
  8603. if (il == n_layer - 1 && inp_out_ids) {
  8604. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  8605. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  8606. }
  8607. // scale_res - scale the hidden states for residual connection
  8608. const float scale_res = scale_depth/sqrtf(float(n_layer)); // TODO: is this correct?
  8609. cur = ggml_scale(ctx0, cur, scale_res);
  8610. cb(cur, "hidden_scaled", il);
  8611. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  8612. cb(ffn_inp, "ffn_inp", il);
  8613. // feed-forward network
  8614. {
  8615. cur = build_norm(ffn_inp,
  8616. model.layers[il].ffn_norm, NULL,
  8617. LLM_NORM_RMS, il);
  8618. cb(cur, "ffn_norm", il);
  8619. cur = build_ffn(cur,
  8620. model.layers[il].ffn_up, NULL, NULL,
  8621. model.layers[il].ffn_gate, NULL, NULL,
  8622. model.layers[il].ffn_down, NULL, NULL,
  8623. NULL,
  8624. LLM_FFN_SILU, LLM_FFN_PAR, il);
  8625. cb(cur, "ffn_out", il);
  8626. }
  8627. // scale the hidden states for residual connection
  8628. cur = ggml_scale(ctx0, cur, scale_res);
  8629. cb(cur, "hidden_scaled_ffn", il);
  8630. cur = ggml_add(ctx0, cur, ffn_inp);
  8631. cur = build_cvec(cur, il);
  8632. cb(cur, "l_out", il);
  8633. // input for next layer
  8634. inpL = cur;
  8635. }
  8636. cur = inpL;
  8637. cur = build_norm(cur,
  8638. model.output_norm, NULL,
  8639. LLM_NORM_RMS, -1);
  8640. cb(cur, "result_norm", -1);
  8641. res->t_embd = cur;
  8642. // lm_head scaling
  8643. const float scale_lmhead = float(n_embd_base)/float(n_embd);
  8644. cur = ggml_scale(ctx0, cur, scale_lmhead);
  8645. cb(cur, "lmhead_scaling", -1);
  8646. // lm_head
  8647. cur = build_lora_mm(model.output, cur);
  8648. cb(cur, "result_output", -1);
  8649. res->t_logits = cur;
  8650. ggml_build_forward_expand(gf, cur);
  8651. }
  8652. };
  8653. struct llm_build_gemma : public llm_graph_context {
  8654. llm_build_gemma(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  8655. const int64_t n_embd_head = hparams.n_embd_head_v;
  8656. ggml_tensor * cur;
  8657. ggml_tensor * inpL;
  8658. inpL = build_inp_embd(model.tok_embd);
  8659. inpL = ggml_scale(ctx0, inpL, sqrtf(n_embd));
  8660. cb(inpL, "inp_scaled", -1);
  8661. // inp_pos - contains the positions
  8662. ggml_tensor * inp_pos = build_inp_pos();
  8663. auto * inp_attn = build_attn_inp_kv();
  8664. ggml_tensor * inp_out_ids = build_inp_out_ids();
  8665. for (int il = 0; il < n_layer; ++il) {
  8666. // norm
  8667. cur = build_norm(inpL,
  8668. model.layers[il].attn_norm, NULL,
  8669. LLM_NORM_RMS, il);
  8670. cb(cur, "attn_norm", il);
  8671. // self-attention
  8672. {
  8673. // compute Q and K and RoPE them
  8674. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  8675. cb(Qcur, "Qcur", il);
  8676. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  8677. cb(Kcur, "Kcur", il);
  8678. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  8679. cb(Vcur, "Vcur", il);
  8680. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  8681. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  8682. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  8683. Qcur = ggml_rope_ext(
  8684. ctx0, Qcur, inp_pos, nullptr,
  8685. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  8686. ext_factor, attn_factor, beta_fast, beta_slow);
  8687. Kcur = ggml_rope_ext(
  8688. ctx0, Kcur, inp_pos, nullptr,
  8689. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  8690. ext_factor, attn_factor, beta_fast, beta_slow);
  8691. cb(Qcur, "Qcur", il);
  8692. cb(Kcur, "Kcur", il);
  8693. cb(Vcur, "Vcur", il);
  8694. Qcur = ggml_scale(ctx0, Qcur, 1.0f / sqrtf(float(n_embd_head)));
  8695. cb(Qcur, "Qcur_scaled", il);
  8696. cur = build_attn(inp_attn,
  8697. model.layers[il].wo, NULL,
  8698. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f, il);
  8699. }
  8700. if (il == n_layer - 1 && inp_out_ids) {
  8701. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  8702. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  8703. }
  8704. ggml_tensor * sa_out = ggml_add(ctx0, cur, inpL);
  8705. cb(sa_out, "sa_out", il);
  8706. cur = build_norm(sa_out,
  8707. model.layers[il].ffn_norm, NULL,
  8708. LLM_NORM_RMS, il);
  8709. cb(cur, "ffn_norm", il);
  8710. // feed-forward network
  8711. {
  8712. cur = build_ffn(cur,
  8713. model.layers[il].ffn_up, NULL, NULL,
  8714. model.layers[il].ffn_gate, NULL, NULL,
  8715. model.layers[il].ffn_down, NULL, NULL,
  8716. NULL,
  8717. LLM_FFN_GELU, LLM_FFN_PAR, il);
  8718. cb(cur, "ffn_out", il);
  8719. }
  8720. cur = ggml_add(ctx0, cur, sa_out);
  8721. cur = build_cvec(cur, il);
  8722. cb(cur, "l_out", il);
  8723. // input for next layer
  8724. inpL = cur;
  8725. }
  8726. cur = inpL;
  8727. cur = build_norm(cur,
  8728. model.output_norm, NULL,
  8729. LLM_NORM_RMS, -1);
  8730. cb(cur, "result_norm", -1);
  8731. res->t_embd = cur;
  8732. // lm_head
  8733. cur = build_lora_mm(model.output, cur);
  8734. cb(cur, "result_output", -1);
  8735. res->t_logits = cur;
  8736. ggml_build_forward_expand(gf, cur);
  8737. }
  8738. };
  8739. struct llm_build_gemma2_iswa : public llm_graph_context {
  8740. llm_build_gemma2_iswa(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  8741. const int64_t n_embd_head = hparams.n_embd_head_k;
  8742. ggml_tensor * cur;
  8743. ggml_tensor * inpL;
  8744. inpL = build_inp_embd(model.tok_embd);
  8745. inpL = ggml_scale(ctx0, inpL, sqrtf(n_embd));
  8746. cb(inpL, "inp_scaled", -1);
  8747. // inp_pos - contains the positions
  8748. ggml_tensor * inp_pos = build_inp_pos();
  8749. auto * inp_attn = build_attn_inp_kv_iswa();
  8750. ggml_tensor * inp_out_ids = build_inp_out_ids();
  8751. for (int il = 0; il < n_layer; ++il) {
  8752. // norm
  8753. cur = build_norm(inpL,
  8754. model.layers[il].attn_norm, NULL,
  8755. LLM_NORM_RMS, il);
  8756. cb(cur, "attn_norm", il);
  8757. // self-attention
  8758. {
  8759. // compute Q and K and RoPE them
  8760. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  8761. cb(Qcur, "Qcur", il);
  8762. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  8763. cb(Kcur, "Kcur", il);
  8764. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  8765. cb(Vcur, "Vcur", il);
  8766. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  8767. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  8768. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  8769. Qcur = ggml_rope_ext(
  8770. ctx0, Qcur, inp_pos, nullptr,
  8771. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  8772. ext_factor, attn_factor, beta_fast, beta_slow);
  8773. Kcur = ggml_rope_ext(
  8774. ctx0, Kcur, inp_pos, nullptr,
  8775. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  8776. ext_factor, attn_factor, beta_fast, beta_slow);
  8777. cb(Qcur, "Qcur", il);
  8778. cb(Kcur, "Kcur", il);
  8779. cb(Vcur, "Vcur", il);
  8780. Qcur = ggml_scale(ctx0, Qcur, hparams.f_attention_scale);
  8781. cur = build_attn(inp_attn,
  8782. model.layers[il].wo, NULL,
  8783. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f, il);
  8784. }
  8785. if (il == n_layer - 1 && inp_out_ids) {
  8786. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  8787. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  8788. }
  8789. cur = build_norm(cur,
  8790. model.layers[il].attn_post_norm, NULL,
  8791. LLM_NORM_RMS, il);
  8792. cb(cur, "attn_post_norm", il);
  8793. ggml_tensor * sa_out = ggml_add(ctx0, cur, inpL);
  8794. cb(sa_out, "sa_out", il);
  8795. cur = build_norm(sa_out,
  8796. model.layers[il].ffn_norm, NULL,
  8797. LLM_NORM_RMS, il);
  8798. cb(cur, "ffn_norm", il);
  8799. // feed-forward network
  8800. {
  8801. cur = build_ffn(cur,
  8802. model.layers[il].ffn_up, NULL, NULL,
  8803. model.layers[il].ffn_gate, NULL, NULL,
  8804. model.layers[il].ffn_down, NULL, NULL,
  8805. NULL,
  8806. LLM_FFN_GELU, LLM_FFN_PAR, il);
  8807. cb(cur, "ffn_out", il);
  8808. }
  8809. cur = build_norm(cur,
  8810. model.layers[il].ffn_post_norm, NULL,
  8811. LLM_NORM_RMS, -1);
  8812. cb(cur, "ffn_post_norm", -1);
  8813. cur = ggml_add(ctx0, cur, sa_out);
  8814. cur = build_cvec(cur, il);
  8815. cb(cur, "l_out", il);
  8816. // input for next layer
  8817. inpL = cur;
  8818. }
  8819. cur = inpL;
  8820. cur = build_norm(cur,
  8821. model.output_norm, NULL,
  8822. LLM_NORM_RMS, -1);
  8823. cb(cur, "result_norm", -1);
  8824. res->t_embd = cur;
  8825. // lm_head
  8826. cur = build_lora_mm(model.output, cur);
  8827. // final logit soft-capping
  8828. cur = ggml_scale(ctx0, cur, 1.0f / hparams.f_final_logit_softcapping);
  8829. cur = ggml_tanh(ctx0, cur);
  8830. cur = ggml_scale(ctx0, cur, hparams.f_final_logit_softcapping);
  8831. cb(cur, "result_output", -1);
  8832. res->t_logits = cur;
  8833. ggml_build_forward_expand(gf, cur);
  8834. }
  8835. };
  8836. struct llm_build_gemma3_iswa : public llm_graph_context {
  8837. llm_build_gemma3_iswa(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  8838. const int64_t n_embd_head = hparams.n_embd_head_k;
  8839. ggml_tensor * cur;
  8840. ggml_tensor * inpL;
  8841. inpL = build_inp_embd(model.tok_embd);
  8842. // important: do not normalize weights for raw embeddings input (i.e. encoded image emdeddings)
  8843. if (ubatch.token) {
  8844. inpL = ggml_scale(ctx0, inpL, sqrtf(n_embd));
  8845. cb(inpL, "inp_scaled", -1);
  8846. }
  8847. // inp_pos - contains the positions
  8848. ggml_tensor * inp_pos = build_inp_pos();
  8849. // TODO: is causal == true correct? might need some changes
  8850. auto * inp_attn = build_attn_inp_kv_iswa();
  8851. ggml_tensor * inp_out_ids = build_inp_out_ids();
  8852. for (int il = 0; il < n_layer; ++il) {
  8853. const float freq_base_l = model.get_rope_freq_base (cparams, il);
  8854. const float freq_scale_l = model.get_rope_freq_scale(cparams, il);
  8855. // norm
  8856. cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
  8857. cb(cur, "attn_norm", il);
  8858. // self-attention
  8859. {
  8860. // compute Q and K and RoPE them
  8861. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  8862. cb(Qcur, "Qcur", il);
  8863. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  8864. cb(Kcur, "Kcur", il);
  8865. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  8866. cb(Vcur, "Vcur", il);
  8867. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  8868. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  8869. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  8870. Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);
  8871. cb(Qcur, "Qcur_normed", il);
  8872. Qcur = ggml_rope_ext(
  8873. ctx0, Qcur, inp_pos, nullptr,
  8874. n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l,
  8875. ext_factor, attn_factor, beta_fast, beta_slow);
  8876. Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);
  8877. cb(Kcur, "Kcur_normed", il);
  8878. Kcur = ggml_rope_ext(
  8879. ctx0, Kcur, inp_pos, nullptr,
  8880. n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l,
  8881. ext_factor, attn_factor, beta_fast, beta_slow);
  8882. cb(Qcur, "Qcur", il);
  8883. cb(Kcur, "Kcur", il);
  8884. cb(Vcur, "Vcur", il);
  8885. // ref: https://github.com/google/gemma_pytorch/blob/014acb7ac4563a5f77c76d7ff98f31b568c16508/gemma/model.py#L315
  8886. Qcur = ggml_scale(ctx0, Qcur, hparams.f_attention_scale);
  8887. cur = build_attn(inp_attn,
  8888. model.layers[il].wo, NULL,
  8889. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f, il);
  8890. }
  8891. if (il == n_layer - 1 && inp_out_ids) {
  8892. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  8893. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  8894. }
  8895. cur = build_norm(cur,
  8896. model.layers[il].attn_post_norm, NULL,
  8897. LLM_NORM_RMS, il);
  8898. cb(cur, "attn_post_norm", il);
  8899. ggml_tensor * sa_out = ggml_add(ctx0, cur, inpL);
  8900. cb(sa_out, "sa_out", il);
  8901. cur = build_norm(sa_out,
  8902. model.layers[il].ffn_norm, NULL,
  8903. LLM_NORM_RMS, il);
  8904. cb(cur, "ffn_norm", il);
  8905. // feed-forward network
  8906. {
  8907. cur = build_ffn(cur,
  8908. model.layers[il].ffn_up, NULL, NULL,
  8909. model.layers[il].ffn_gate, NULL, NULL,
  8910. model.layers[il].ffn_down, NULL, NULL,
  8911. NULL,
  8912. LLM_FFN_GELU, LLM_FFN_PAR, il);
  8913. cb(cur, "ffn_out", il);
  8914. }
  8915. cur = build_norm(cur,
  8916. model.layers[il].ffn_post_norm, NULL,
  8917. LLM_NORM_RMS, -1);
  8918. cb(cur, "ffn_post_norm", -1);
  8919. cur = ggml_add(ctx0, cur, sa_out);
  8920. cur = build_cvec(cur, il);
  8921. cb(cur, "l_out", il);
  8922. // input for next layer
  8923. inpL = cur;
  8924. }
  8925. cur = inpL;
  8926. cur = build_norm(cur,
  8927. model.output_norm, NULL,
  8928. LLM_NORM_RMS, -1);
  8929. cb(cur, "result_norm", -1);
  8930. res->t_embd = cur;
  8931. // lm_head
  8932. cur = build_lora_mm(model.output, cur);
  8933. cb(cur, "result_output", -1);
  8934. res->t_logits = cur;
  8935. ggml_build_forward_expand(gf, cur);
  8936. }
  8937. };
  8938. struct llm_build_gemma3n_iswa : public llm_graph_context {
  8939. const llama_model & model;
  8940. const int64_t n_embd_head;
  8941. const int64_t n_embd_altup;
  8942. const int64_t n_altup;
  8943. const int i_altup_act;
  8944. const int n_layer_sparsity = 10; // number of layers using activation sparsity
  8945. const float f_sparsity_std_mul = 1.6448533535003662f; // std_multiplier = normal_dist.icdf(0.95)
  8946. llm_build_gemma3n_iswa(const llama_model & model, const llm_graph_params & params)
  8947. : llm_graph_context(params),
  8948. model(model),
  8949. n_embd_head(model.hparams.n_embd_head_k),
  8950. n_embd_altup(model.hparams.n_embd_altup),
  8951. n_altup(model.hparams.n_altup),
  8952. i_altup_act(model.hparams.i_altup_act) {
  8953. ggml_tensor * cur;
  8954. ggml_tensor * inpL;
  8955. inpL = build_inp_embd(model.tok_embd);
  8956. // important: do not normalize weights for raw embeddings input (i.e. encoded image emdeddings)
  8957. if (ubatch.token) {
  8958. inpL = ggml_scale(ctx0, inpL, sqrtf(n_embd));
  8959. cb(inpL, "inp_scaled", -1);
  8960. }
  8961. // inp_pos - contains the positions
  8962. ggml_tensor * inp_pos = build_inp_pos();
  8963. // TODO: is causal == true correct? might need some changes
  8964. auto * inp_attn = build_attn_inp_kv_iswa();
  8965. // inp_per_layer shape: [n_embd_altup, n_tokens, n_layer]
  8966. ggml_tensor * inp_per_layer = project_per_layer_inputs(inpL, get_per_layer_inputs());
  8967. // inpL now has only 1 altup, project it to the rest of the altups
  8968. // these "added" altups will be concat to the last dim of inpL
  8969. {
  8970. ggml_tensor * target_magnitude = calc_magnitude(inpL);
  8971. ggml_tensor * inp_repeated = ggml_repeat_4d(ctx0, inpL, n_embd, n_tokens, n_altup - 1, 1);
  8972. ggml_tensor * altup_added = ggml_mul_mat(ctx0, model.altup_proj, inp_repeated); // shape: [n_embd, n_tokens, n_altup - 1]
  8973. ggml_tensor * new_magnitude = calc_magnitude(altup_added);
  8974. altup_added = ggml_div(ctx0,
  8975. ggml_mul(ctx0, altup_added, target_magnitude),
  8976. new_magnitude);
  8977. inpL = ggml_concat(ctx0, inpL, altup_added, 2); // shape: [n_embd, n_tokens, n_altup]
  8978. cb(inpL, "inp_stacked", -1);
  8979. }
  8980. // inpL now has shape: [n_embd, n_tokens, n_altup]
  8981. // inp_per_layer now has shape: [n_embd_altup, n_tokens, n_layer]
  8982. for (int il = 0; il < n_layer; ++il) {
  8983. // this block is made to be closely resemble Gemma3p5DecoderLayer on python code
  8984. const float freq_base_l = model.get_rope_freq_base (cparams, il);
  8985. const float freq_scale_l = model.get_rope_freq_scale(cparams, il);
  8986. ggml_tensor * cur = inpL; // [n_embd, n_tokens, n_altup]
  8987. ggml_tensor * predictions = altup_predict(cur, il); // [n_embd, n_tokens, n_altup]
  8988. // predicted value will go through self-attention and laurel
  8989. ggml_tensor * active_prediction = view_2d_slice(predictions, i_altup_act); // [n_embd, n_tokens]
  8990. cur = active_prediction;
  8991. cb(cur, "active_prediction", il);
  8992. // norm
  8993. cur = build_norm(cur, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
  8994. cb(cur, "attn_norm", il);
  8995. // laurel
  8996. ggml_tensor * laurel_out = laurel(cur, il); // [n_embd, n_tokens]
  8997. // self-attention
  8998. if (hparams.has_kv(il)) {
  8999. // compute Q and K and RoPE them
  9000. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  9001. cb(Qcur, "Qcur", il);
  9002. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  9003. cb(Kcur, "Kcur", il);
  9004. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  9005. cb(Vcur, "Vcur", il);
  9006. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  9007. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  9008. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  9009. Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);
  9010. Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);
  9011. Vcur = ggml_rms_norm(ctx0, Vcur, hparams.f_norm_rms_eps);
  9012. cb(Qcur, "Qcur_normed", il);
  9013. cb(Kcur, "Kcur_normed", il);
  9014. cb(Vcur, "Vcur_normed", il);
  9015. Qcur = ggml_rope_ext(
  9016. ctx0, Qcur, inp_pos, nullptr,
  9017. n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l,
  9018. ext_factor, attn_factor, beta_fast, beta_slow);
  9019. Kcur = ggml_rope_ext(
  9020. ctx0, Kcur, inp_pos, nullptr,
  9021. n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l,
  9022. ext_factor, attn_factor, beta_fast, beta_slow);
  9023. cb(Qcur, "Qcur_pos", il);
  9024. cb(Kcur, "Kcur_pos", il);
  9025. cur = build_attn(inp_attn,
  9026. model.layers[il].wo, NULL,
  9027. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, hparams.f_attention_scale, il);
  9028. } else {
  9029. // reuse KV cache of earlier layers
  9030. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  9031. cb(Qcur, "Qcur", il);
  9032. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  9033. Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);
  9034. cb(Qcur, "Qcur_normed", il);
  9035. Qcur = ggml_rope_ext(
  9036. ctx0, Qcur, inp_pos, nullptr,
  9037. n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l,
  9038. ext_factor, attn_factor, beta_fast, beta_slow);
  9039. cb(Qcur, "Qcur_pos", il);
  9040. cur = build_attn(inp_attn,
  9041. model.layers[il].wo, NULL,
  9042. Qcur, nullptr, nullptr, nullptr, nullptr, nullptr, hparams.f_attention_scale, il);
  9043. }
  9044. cur = build_norm(cur,
  9045. model.layers[il].attn_post_norm, NULL,
  9046. LLM_NORM_RMS, il);
  9047. cb(cur, "attn_post_norm", il);
  9048. cur = ggml_add(ctx0, cur, active_prediction); // [n_embd, n_tokens]
  9049. cb(cur, "attn_gated", il);
  9050. ggml_tensor * attn_laurel = ggml_scale(ctx0,
  9051. ggml_add(ctx0, cur, laurel_out),
  9052. 1.0f / sqrtf(2.0f)); // [n_embd, n_tokens]
  9053. cb(attn_laurel, "attn_laurel", il);
  9054. cur = build_norm(attn_laurel,
  9055. model.layers[il].ffn_norm, NULL,
  9056. LLM_NORM_RMS, il);
  9057. cb(cur, "ffn_norm", il);
  9058. // feed-forward network
  9059. {
  9060. ggml_tensor * up_proj = build_lora_mm(model.layers[il].ffn_up, cur);
  9061. ggml_tensor * gate_proj = build_lora_mm(model.layers[il].ffn_gate, cur);
  9062. if (il < n_layer_sparsity) {
  9063. // apply activation sparsity
  9064. gate_proj = gaussian_topk(gate_proj);
  9065. }
  9066. gate_proj = ggml_gelu(ctx0, gate_proj);
  9067. cur = ggml_mul(ctx0, up_proj, gate_proj);
  9068. cur = build_lora_mm(model.layers[il].ffn_down, cur);
  9069. cb(cur, "ffn_out", il);
  9070. }
  9071. cur = build_norm(cur,
  9072. model.layers[il].ffn_post_norm, NULL,
  9073. LLM_NORM_RMS, -1);
  9074. cb(cur, "ffn_post_norm", il);
  9075. ggml_tensor * attn_ffw_laurel_gated = ggml_add(ctx0, cur, attn_laurel); // [n_embd, n_tokens]
  9076. cb(attn_ffw_laurel_gated, "attn_ffw_laurel_gated", il);
  9077. ggml_tensor * corrected = altup_correct(predictions, attn_ffw_laurel_gated, il); // [n_embd, n_tokens, n_altup]
  9078. ggml_tensor * first_prediction; // [n_embd, n_tokens]
  9079. {
  9080. first_prediction = view_2d_slice(corrected, i_altup_act); // [n_embd, n_tokens]
  9081. first_prediction = ggml_mul(ctx0, first_prediction, model.layers[il].altup_correct_scale);
  9082. first_prediction = build_lora_mm(model.layers[il].per_layer_inp_gate, first_prediction);
  9083. first_prediction = ggml_gelu(ctx0, first_prediction); // [n_embd_altup, n_tokens]
  9084. cb(first_prediction, "first_prediction_gated", il);
  9085. ggml_tensor * inp_this_layer = view_2d_slice(inp_per_layer, il); // [n_embd_altup, n_tokens]
  9086. first_prediction = ggml_mul(ctx0, first_prediction, inp_this_layer); // [n_embd_altup, n_tokens]
  9087. cb(first_prediction, "first_prediction_scaled", il);
  9088. first_prediction = build_lora_mm(model.layers[il].per_layer_proj, first_prediction); // [n_embd, n_tokens]
  9089. first_prediction = build_norm(first_prediction,
  9090. model.layers[il].per_layer_post_norm, NULL,
  9091. LLM_NORM_RMS, il);
  9092. cb(first_prediction, "first_prediction_out", il);
  9093. }
  9094. // equivalent to python code: corrected_predictions[1:] += first_prediction
  9095. {
  9096. ggml_tensor * slice_first = view_2d_slice(corrected, 0);
  9097. ggml_tensor * slice_rest = ggml_view_3d(ctx0, corrected, n_embd, n_tokens, n_altup - 1,
  9098. ggml_row_size(corrected->type, n_embd),
  9099. ggml_row_size(corrected->type, n_embd*n_tokens),
  9100. n_embd*n_tokens*ggml_element_size(corrected));
  9101. ggml_tensor * tmp = ggml_add(ctx0, slice_rest, first_prediction); // [n_embd, n_tokens, n_altup - 1]
  9102. corrected = ggml_concat(ctx0, slice_first, tmp, 2); // [n_embd, n_tokens, n_altup]
  9103. }
  9104. cur = corrected; // [n_embd, n_tokens, n_altup]
  9105. cur = build_cvec(cur, il);
  9106. cb(cur, "l_out", il);
  9107. // input for next layer
  9108. inpL = cur;
  9109. }
  9110. cur = inpL; // [n_embd, n_tokens, n_altup]
  9111. // cur now has multiple altup(s), we want to merge them back to 1 altup
  9112. {
  9113. ggml_tensor * target_magnitude = calc_magnitude(view_2d_slice(cur, i_altup_act)); // [n_embd, n_tokens]
  9114. // do a view to skip the first slice (active altup)
  9115. ggml_tensor * alt_slice = ggml_view_3d(ctx0, cur, n_embd, n_tokens, n_altup - 1,
  9116. ggml_row_size(cur->type, n_embd),
  9117. ggml_row_size(cur->type, n_embd*n_tokens),
  9118. n_embd*n_tokens*ggml_element_size(cur));
  9119. ggml_tensor * altup_unembd = ggml_mul_mat(ctx0, model.altup_unembd_proj, alt_slice); // shape: [n_embd, n_tokens, n_altup - 1]
  9120. ggml_tensor * new_magnitude = calc_magnitude(altup_unembd);
  9121. altup_unembd = ggml_div(ctx0,
  9122. ggml_mul(ctx0, altup_unembd, target_magnitude),
  9123. new_magnitude);
  9124. cb(altup_unembd, "altup_unembd", -1);
  9125. // equivalent to torch.mean(hidden_states, dim=0)
  9126. cur = view_2d_slice(cur, 0); // [n_embd, n_tokens]
  9127. for (int i = 0; i < n_altup - 1; ++i) {
  9128. cur = ggml_add(ctx0, cur, view_2d_slice(altup_unembd, i));
  9129. }
  9130. cur = ggml_scale(ctx0, cur, 1.0f / float(n_altup)); // [n_embd, n_tokens]
  9131. cb(cur, "unembd_merged", -1);
  9132. }
  9133. // cur now has shape: [n_embd, n_tokens]
  9134. // TODO: move this to right after the last KV layer
  9135. {
  9136. // skip computing output for unused tokens
  9137. ggml_tensor * inp_out_ids = build_inp_out_ids();
  9138. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  9139. }
  9140. cur = build_norm(cur,
  9141. model.output_norm, NULL,
  9142. LLM_NORM_RMS, -1);
  9143. cb(cur, "result_norm", -1);
  9144. res->t_embd = cur;
  9145. cur = build_lora_mm(model.output, cur);
  9146. {
  9147. // final logit soft-capping
  9148. cur = ggml_scale(ctx0, cur, 1.0f / hparams.f_final_logit_softcapping);
  9149. cur = ggml_tanh(ctx0, cur);
  9150. cur = ggml_scale(ctx0, cur, hparams.f_final_logit_softcapping);
  9151. }
  9152. cb(cur, "result_output", -1);
  9153. res->t_logits = cur;
  9154. ggml_build_forward_expand(gf, cur);
  9155. }
  9156. ggml_tensor * calc_magnitude(ggml_tensor * x) {
  9157. return ggml_sqrt(ctx0, ggml_sum_rows(ctx0, ggml_sqr(ctx0, x)));
  9158. }
  9159. // get 2D slice view from a 3D tensor, the idx corresponds to the 3rd dim
  9160. ggml_tensor * view_2d_slice(ggml_tensor * x, int idx) {
  9161. GGML_ASSERT(idx < (int)x->ne[2]);
  9162. return ggml_view_2d(ctx0, x, x->ne[0], x->ne[1],
  9163. ggml_row_size(x->type, x->ne[0]),
  9164. idx * x->ne[0] * x->ne[1] * ggml_element_size(x));
  9165. }
  9166. // equivalent to get_per_layer_inputs() in python code
  9167. // output shape: [n_embd_altup, n_layer, n_tokens]
  9168. ggml_tensor * get_per_layer_inputs() {
  9169. auto inp = std::make_unique<llm_graph_input_embd>();
  9170. ggml_tensor * inp_per_layer;
  9171. if (ubatch.token) {
  9172. inp->tokens = ggml_new_tensor_1d(ctx0, GGML_TYPE_I32, ubatch.n_tokens);
  9173. ggml_set_input(inp->tokens);
  9174. res->t_tokens = inp->tokens;
  9175. inp_per_layer = ggml_get_rows(ctx0, model.tok_embd_per_layer, inp->tokens);
  9176. inp_per_layer = ggml_reshape_3d(ctx0, inp_per_layer, n_embd_altup, n_layer, n_tokens);
  9177. inp_per_layer = ggml_scale(ctx0, inp_per_layer, sqrtf((float)n_embd_altup));
  9178. cb(inp_per_layer, "inp_per_layer_selected", -1);
  9179. } else {
  9180. GGML_ABORT("TODO: support embd input");
  9181. }
  9182. res->add_input(std::move(inp));
  9183. return inp_per_layer;
  9184. }
  9185. // equivalent to project_per_layer_inputs() in python code
  9186. // this calculates the per-layer inputs, so the final tensor shape will have n_layer as the last dim
  9187. // output shape: [n_embd_altup, n_tokens, n_layer]
  9188. ggml_tensor * project_per_layer_inputs(ggml_tensor * inputs_embeds, ggml_tensor * inp_per_layer) {
  9189. const float per_layer_projection_scale = 1.0f / sqrtf((float)n_embd);
  9190. const float per_layer_input_scale = 1.0f / sqrtf(2.0f);
  9191. ggml_tensor * per_layer_proj = ggml_mul_mat(ctx0, model.per_layer_model_proj, inputs_embeds);
  9192. per_layer_proj = ggml_scale(ctx0, per_layer_proj, per_layer_projection_scale);
  9193. per_layer_proj = ggml_reshape_3d(ctx0, per_layer_proj, n_embd_altup, n_layer, n_tokens);
  9194. per_layer_proj = build_norm(per_layer_proj,
  9195. model.per_layer_proj_norm, NULL,
  9196. LLM_NORM_RMS, -1); // [n_embd_altup, n_layer, n_tokens]
  9197. cb(per_layer_proj, "per_layer_proj", -1);
  9198. inp_per_layer = ggml_add(ctx0, inp_per_layer, per_layer_proj);
  9199. inp_per_layer = ggml_scale(ctx0, inp_per_layer, per_layer_input_scale);
  9200. cb(inp_per_layer, "inp_per_layer", -1);
  9201. // permute to shape: [n_embd_altup, n_tokens, n_layer]
  9202. inp_per_layer = ggml_cont(ctx0, ggml_permute(ctx0, inp_per_layer, 0, 2, 1, 3));
  9203. return inp_per_layer;
  9204. }
  9205. // input cur shape: [n_altup, n_tokens]
  9206. // output shape: [n_altup, n_tokens]
  9207. ggml_tensor * laurel(ggml_tensor * cur, int il) {
  9208. ggml_tensor * tmp = cur;
  9209. tmp = build_lora_mm(model.layers[il].laurel_l, tmp);
  9210. tmp = build_lora_mm(model.layers[il].laurel_r, tmp);
  9211. tmp = build_norm(tmp, model.layers[il].laurel_post_norm, NULL, LLM_NORM_RMS, il);
  9212. tmp = ggml_add(ctx0, tmp, cur);
  9213. cb(tmp, "laurel_out", il);
  9214. return tmp;
  9215. }
  9216. // input x shape: [n_embd, n_tokens]
  9217. // output shape: [n_embd, n_tokens]
  9218. ggml_tensor * gaussian_topk(ggml_tensor * x) {
  9219. ggml_tensor * mean = ggml_mean(ctx0, x);
  9220. ggml_tensor * std = ggml_sqrt(ctx0, ggml_scale(ctx0,
  9221. ggml_sum_rows(ctx0, ggml_sqr(ctx0, ggml_sub(ctx0, x, mean))),
  9222. 1.0f / (float)(x->ne[0] - 1)
  9223. ));
  9224. ggml_tensor * cutoff_x = ggml_add(ctx0, mean, ggml_scale(ctx0, std, f_sparsity_std_mul));
  9225. return ggml_relu(ctx0, ggml_sub(ctx0, x, cutoff_x));
  9226. }
  9227. //
  9228. // altup functions
  9229. //
  9230. // equivalent to compute_router_modalities() in python code
  9231. // input x shape: [n_embd, n_tokens]
  9232. // output shape: [n_altup, n_tokens]
  9233. ggml_tensor * altup_compute_router_modalities(ggml_tensor * x, int il) {
  9234. ggml_tensor * router_inputs = build_norm(x,
  9235. model.layers[il].altup_router_norm, NULL,
  9236. LLM_NORM_RMS, il);
  9237. // router_input_scale
  9238. router_inputs = ggml_scale(ctx0, router_inputs, 1.0f / (float)n_embd);
  9239. ggml_tensor * output = ggml_mul_mat(ctx0, model.layers[il].altup_router, router_inputs);
  9240. return ggml_tanh(ctx0, output); // [n_altup, n_tokens]
  9241. }
  9242. // input cur shape: [n_embd, n_tokens, n_altup]
  9243. // output shape: [n_embd, n_tokens, n_altup]
  9244. ggml_tensor * altup_predict(ggml_tensor * cur, int il) {
  9245. ggml_tensor * activated = view_2d_slice(cur, i_altup_act); // [n_embd, n_tokens]
  9246. ggml_tensor * modalities = altup_compute_router_modalities(activated, il); // [n_altup, n_tokens]
  9247. cb(modalities, "modalities", il);
  9248. ggml_tensor * all_coefs = build_lora_mm(model.layers[il].altup_predict_coef, modalities);
  9249. cb(all_coefs, "all_coefs", il);
  9250. // first dim now having n_altup^2 elements, we reshape it to 2D (so we end up with 3D tensor)
  9251. all_coefs = ggml_reshape_3d(ctx0, all_coefs, n_altup, n_altup, n_tokens);
  9252. // permute to [n_altup, n_embd, n_tokens]
  9253. ggml_tensor * cur_permuted = ggml_cont(ctx0, ggml_permute(ctx0, cur, 1, 2, 0, 3));
  9254. ggml_tensor * predictions = ggml_mul_mat(ctx0, cur_permuted, all_coefs); // [n_altup, n_embd, n_tokens]
  9255. // final shape must be the same as cur: [n_embd, n_tokens, n_altup]
  9256. predictions = ggml_cont(ctx0, ggml_permute(ctx0, predictions, 0, 2, 1, 3));
  9257. predictions = ggml_add(ctx0, predictions, cur);
  9258. cb(predictions, "predictions", il);
  9259. return predictions;
  9260. }
  9261. // input predictions shape: [n_embd, n_tokens, n_altup]
  9262. // input activated shape: [n_embd, n_tokens]
  9263. // output shape: [n_embd, n_tokens, n_altup]
  9264. ggml_tensor * altup_correct(ggml_tensor * predictions, ggml_tensor * activated, int il) {
  9265. ggml_tensor * modalities = altup_compute_router_modalities(activated, il); // [n_altup, n_tokens]
  9266. cb(modalities, "modalities", il);
  9267. ggml_tensor * active_prediction = view_2d_slice(predictions, i_altup_act);
  9268. ggml_tensor * innovation = ggml_sub(ctx0, activated, active_prediction); // [n_embd, n_tokens]
  9269. cb(innovation, "innovation", il);
  9270. ggml_tensor * all_coefs = build_lora_mm(model.layers[il].altup_correct_coef, modalities); // [n_altup, n_tokens]
  9271. all_coefs = ggml_scale_bias(ctx0, all_coefs, 1.0f, 1.0f); // + 1.0
  9272. cb(all_coefs, "all_coefs", il);
  9273. all_coefs = ggml_transpose(ctx0, all_coefs); // [n_tokens, n_altup]
  9274. all_coefs = ggml_cont_3d(ctx0, all_coefs, 1, n_tokens, n_altup); // [1, n_tokens, n_altup]
  9275. innovation = ggml_repeat_4d(ctx0, innovation, n_embd, n_tokens, n_altup, 1);
  9276. ggml_tensor * corrected = ggml_mul(ctx0, innovation, all_coefs); // [n_embd, n_tokens, n_altup]
  9277. corrected = ggml_add(ctx0, corrected, predictions); // [n_embd, n_tokens, n_altup]
  9278. cb(corrected, "corrected", il);
  9279. return corrected;
  9280. }
  9281. };
  9282. struct llm_build_gemma_embedding : public llm_graph_context {
  9283. llm_build_gemma_embedding(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  9284. const int64_t n_embd_head = hparams.n_embd_head_k;
  9285. ggml_tensor * cur;
  9286. ggml_tensor * inpL;
  9287. inpL = build_inp_embd(model.tok_embd);
  9288. // important: do not normalize weights for raw embeddings input (i.e. encoded image emdeddings)
  9289. if (ubatch.token) {
  9290. inpL = ggml_scale(ctx0, inpL, sqrtf(n_embd));
  9291. cb(inpL, "inp_scaled", -1);
  9292. }
  9293. // inp_pos - contains the positions
  9294. ggml_tensor * inp_pos = build_inp_pos();
  9295. auto * inp_attn = build_attn_inp_no_cache();
  9296. ggml_tensor * inp_out_ids = build_inp_out_ids();
  9297. for (int il = 0; il < n_layer; ++il) {
  9298. const float freq_base_l = model.get_rope_freq_base (cparams, il);
  9299. const float freq_scale_l = model.get_rope_freq_scale(cparams, il);
  9300. // norm
  9301. cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
  9302. cb(cur, "attn_norm", il);
  9303. // self-attention
  9304. {
  9305. // compute Q and K and RoPE them
  9306. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  9307. cb(Qcur, "Qcur", il);
  9308. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  9309. cb(Kcur, "Kcur", il);
  9310. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  9311. cb(Vcur, "Vcur", il);
  9312. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  9313. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  9314. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  9315. Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);
  9316. cb(Qcur, "Qcur_normed", il);
  9317. Qcur = ggml_rope_ext(
  9318. ctx0, Qcur, inp_pos, nullptr,
  9319. n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l,
  9320. ext_factor, attn_factor, beta_fast, beta_slow);
  9321. Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);
  9322. cb(Kcur, "Kcur_normed", il);
  9323. Kcur = ggml_rope_ext(
  9324. ctx0, Kcur, inp_pos, nullptr,
  9325. n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l,
  9326. ext_factor, attn_factor, beta_fast, beta_slow);
  9327. cb(Qcur, "Qcur", il);
  9328. cb(Kcur, "Kcur", il);
  9329. cb(Vcur, "Vcur", il);
  9330. // ref: https://github.com/google/gemma_pytorch/blob/014acb7ac4563a5f77c76d7ff98f31b568c16508/gemma/model.py#L315
  9331. Qcur = ggml_scale(ctx0, Qcur, hparams.f_attention_scale);
  9332. cur = build_attn(inp_attn,
  9333. model.layers[il].wo, NULL,
  9334. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f, il);
  9335. }
  9336. if (il == n_layer - 1 && inp_out_ids) {
  9337. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  9338. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  9339. }
  9340. cur = build_norm(cur,
  9341. model.layers[il].attn_post_norm, NULL,
  9342. LLM_NORM_RMS, il);
  9343. cb(cur, "attn_post_norm", il);
  9344. ggml_tensor * sa_out = ggml_add(ctx0, cur, inpL);
  9345. cb(sa_out, "sa_out", il);
  9346. cur = build_norm(sa_out,
  9347. model.layers[il].ffn_norm, NULL,
  9348. LLM_NORM_RMS, il);
  9349. cb(cur, "ffn_norm", il);
  9350. // feed-forward network
  9351. {
  9352. cur = build_ffn(cur,
  9353. model.layers[il].ffn_up, NULL, NULL,
  9354. model.layers[il].ffn_gate, NULL, NULL,
  9355. model.layers[il].ffn_down, NULL, NULL,
  9356. NULL,
  9357. LLM_FFN_GELU, LLM_FFN_PAR, il);
  9358. cb(cur, "ffn_out", il);
  9359. }
  9360. cur = build_norm(cur,
  9361. model.layers[il].ffn_post_norm, NULL,
  9362. LLM_NORM_RMS, -1);
  9363. cb(cur, "ffn_post_norm", -1);
  9364. cur = ggml_add(ctx0, cur, sa_out);
  9365. cur = build_cvec(cur, il);
  9366. cb(cur, "l_out", il);
  9367. // input for next layer
  9368. inpL = cur;
  9369. }
  9370. cur = inpL;
  9371. cur = build_norm(cur,
  9372. model.output_norm, NULL,
  9373. LLM_NORM_RMS, -1);
  9374. cb(cur, "result_norm", -1);
  9375. res->t_embd = cur;
  9376. ggml_build_forward_expand(gf, cur);
  9377. }
  9378. };
  9379. // TODO: move up next to build_starcoder
  9380. struct llm_build_starcoder2 : public llm_graph_context {
  9381. llm_build_starcoder2(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  9382. const int64_t n_embd_head = hparams.n_embd_head_v;
  9383. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  9384. GGML_ASSERT(n_embd_head == hparams.n_rot);
  9385. ggml_tensor * cur;
  9386. ggml_tensor * inpL;
  9387. inpL = build_inp_embd(model.tok_embd);
  9388. // inp_pos - contains the positions
  9389. ggml_tensor * inp_pos = build_inp_pos();
  9390. auto * inp_attn = build_attn_inp_kv();
  9391. ggml_tensor * inp_out_ids = build_inp_out_ids();
  9392. for (int il = 0; il < n_layer; ++il) {
  9393. ggml_tensor * inpSA = inpL;
  9394. // norm
  9395. cur = build_norm(inpL,
  9396. model.layers[il].attn_norm, model.layers[il].attn_norm_b,
  9397. LLM_NORM, il);
  9398. cb(cur, "attn_norm", il);
  9399. // self-attention
  9400. {
  9401. // compute Q and K and RoPE them
  9402. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  9403. cb(Qcur, "Qcur", il);
  9404. if (model.layers[il].bq) {
  9405. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  9406. cb(Qcur, "Qcur", il);
  9407. }
  9408. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  9409. cb(Kcur, "Kcur", il);
  9410. if (model.layers[il].bk) {
  9411. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  9412. cb(Kcur, "Kcur", il);
  9413. }
  9414. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  9415. cb(Vcur, "Vcur", il);
  9416. if (model.layers[il].bv) {
  9417. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  9418. cb(Vcur, "Vcur", il);
  9419. }
  9420. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  9421. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  9422. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  9423. Qcur = ggml_rope_ext(
  9424. ctx0, Qcur, inp_pos, nullptr,
  9425. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  9426. ext_factor, attn_factor, beta_fast, beta_slow
  9427. );
  9428. Kcur = ggml_rope_ext(
  9429. ctx0, Kcur, inp_pos, nullptr,
  9430. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  9431. ext_factor, attn_factor, beta_fast, beta_slow
  9432. );
  9433. cb(Qcur, "Qcur", il);
  9434. cb(Kcur, "Kcur", il);
  9435. cb(Vcur, "Vcur", il);
  9436. cur = build_attn(inp_attn,
  9437. model.layers[il].wo, model.layers[il].bo,
  9438. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  9439. }
  9440. if (il == n_layer - 1 && inp_out_ids) {
  9441. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  9442. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  9443. }
  9444. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  9445. cb(ffn_inp, "ffn_inp", il);
  9446. // feed-forward network
  9447. cur = build_norm(ffn_inp,
  9448. model.layers[il].ffn_norm, model.layers[il].ffn_norm_b,
  9449. LLM_NORM, il);
  9450. cb(cur, "ffn_norm", il);
  9451. cur = build_ffn(cur,
  9452. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  9453. NULL, NULL, NULL,
  9454. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  9455. NULL,
  9456. LLM_FFN_GELU, LLM_FFN_SEQ, il);
  9457. cb(cur, "ffn_out", il);
  9458. cur = ggml_add(ctx0, cur, ffn_inp);
  9459. cur = build_cvec(cur, il);
  9460. cb(cur, "l_out", il);
  9461. // input for next layer
  9462. inpL = cur;
  9463. }
  9464. cur = inpL;
  9465. cur = build_norm(cur,
  9466. model.output_norm, model.output_norm_b,
  9467. LLM_NORM, -1);
  9468. cb(cur, "result_norm", -1);
  9469. res->t_embd = cur;
  9470. // lm_head
  9471. cur = build_lora_mm(model.output, cur);
  9472. cb(cur, "result_output", -1);
  9473. res->t_logits = cur;
  9474. ggml_build_forward_expand(gf, cur);
  9475. }
  9476. };
  9477. struct llm_graph_context_mamba : public llm_graph_context {
  9478. llm_graph_context_mamba(const llm_graph_params & params) : llm_graph_context(params) {}
  9479. ggml_tensor * build_mamba_layer(
  9480. llm_graph_input_rs * inp,
  9481. ggml_tensor * cur,
  9482. const llama_model & model,
  9483. const llama_ubatch & ubatch,
  9484. int il) {
  9485. const auto * mctx_cur = inp->mctx;
  9486. const auto kv_head = mctx_cur->get_head();
  9487. const auto & layer = model.layers[il];
  9488. const int64_t d_conv = hparams.ssm_d_conv;
  9489. const int64_t d_inner = hparams.ssm_d_inner;
  9490. const int64_t d_state = hparams.ssm_d_state;
  9491. const int64_t dt_rank = hparams.ssm_dt_rank;
  9492. const int64_t n_head = d_inner;
  9493. const int64_t head_dim = 1;
  9494. const int64_t n_seqs = ubatch.n_seqs;
  9495. // Some variants of Mamba arch (e.g. FalconMamba do apply layer norm on B and Dt layers)
  9496. const bool ssm_dt_b_c_rms = hparams.ssm_dt_b_c_rms;
  9497. const int64_t n_seq_tokens = ubatch.n_seq_tokens;
  9498. GGML_ASSERT(n_seqs != 0);
  9499. GGML_ASSERT(ubatch.equal_seqs());
  9500. GGML_ASSERT(ubatch.n_tokens == n_seq_tokens * n_seqs);
  9501. ggml_tensor * conv_states_all = mctx_cur->get_r_l(il);
  9502. ggml_tensor * ssm_states_all = mctx_cur->get_s_l(il);
  9503. ggml_tensor * conv = build_rs(inp, conv_states_all, hparams.n_embd_r(), n_seqs);
  9504. conv = ggml_reshape_3d(ctx0, conv, d_conv - 1, d_inner, n_seqs);
  9505. // {n_embd, n_tokens} => {n_embd, n_seq_tokens, n_seqs}
  9506. cur = ggml_reshape_3d(ctx0, cur, cur->ne[0], n_seq_tokens, n_seqs);
  9507. // {n_embd, 2*d_inner} @ {n_embd, n_seq_tokens, n_seqs} => {2*d_inner, n_seq_tokens, n_seqs}
  9508. ggml_tensor * xz = build_lora_mm(layer.ssm_in, cur);
  9509. // split the above in two
  9510. // => {d_inner, n_seq_tokens, n_seqs}
  9511. ggml_tensor * x = ggml_view_3d(ctx0, xz, d_inner, xz->ne[1], xz->ne[2], xz->nb[1], xz->nb[2], 0);
  9512. 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));
  9513. // conv
  9514. {
  9515. // => {d_conv - 1 + n_seq_tokens, d_inner, n_seqs}
  9516. ggml_tensor * conv_x = ggml_concat(ctx0, conv, ggml_transpose(ctx0, x), 0);
  9517. // copy last (d_conv - 1) columns back into the state cache
  9518. 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]));
  9519. ggml_build_forward_expand(gf,
  9520. ggml_cpy(ctx0, last_conv,
  9521. ggml_view_1d(ctx0, conv_states_all,
  9522. (d_conv - 1)*(d_inner)*(n_seqs),
  9523. kv_head*(d_conv - 1)*(d_inner)*ggml_element_size(conv_states_all))));
  9524. // 1D convolution
  9525. // The equivalent is to make a self-overlapping view of conv_x
  9526. // over d_conv columns at each stride in the 3rd dimension,
  9527. // then element-wise multiply that with the conv1d weight,
  9528. // then sum the elements of each row,
  9529. // (the last two steps are a dot product over rows (also doable with mul_mat))
  9530. // then permute away the ne[0] dimension,
  9531. // and then you're left with the resulting x tensor.
  9532. // For simultaneous sequences, all sequences need to have the same length.
  9533. x = ggml_ssm_conv(ctx0, conv_x, layer.ssm_conv1d);
  9534. // bias
  9535. x = ggml_add(ctx0, x, layer.ssm_conv1d_b);
  9536. x = ggml_silu(ctx0, x);
  9537. }
  9538. // ssm
  9539. {
  9540. // {d_inner, dt_rank + 2*d_state} @ {d_inner, n_seq_tokens, n_seqs} => {dt_rank + 2*d_state, n_seq_tokens, n_seqs}
  9541. ggml_tensor * x_db = build_lora_mm(layer.ssm_x, x);
  9542. // split
  9543. 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);
  9544. 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);
  9545. 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));
  9546. // Some Mamba variants (e.g. FalconMamba, Jamba) apply RMS norm in B, C & Dt layers
  9547. if (ssm_dt_b_c_rms || (layer.ssm_dt_norm && layer.ssm_b_norm && layer.ssm_c_norm)) {
  9548. dt = build_norm(dt, layer.ssm_dt_norm, NULL, LLM_NORM_RMS, il);
  9549. B = build_norm(B, layer.ssm_b_norm, NULL, LLM_NORM_RMS, il);
  9550. C = build_norm(C, layer.ssm_c_norm, NULL, LLM_NORM_RMS, il);
  9551. }
  9552. // {dt_rank, d_inner} @ {dt_rank, n_seq_tokens, n_seqs} => {d_inner, n_seq_tokens, n_seqs}
  9553. dt = build_lora_mm(layer.ssm_dt, dt);
  9554. dt = ggml_add(ctx0, dt, layer.ssm_dt_b);
  9555. cur = x;
  9556. x = ggml_reshape_4d(ctx0, x, head_dim, n_head, n_seq_tokens, n_seqs);
  9557. ggml_tensor * A = layer.ssm_a;
  9558. // use the states and the indices provided by build_recurrent_state
  9559. // (this is necessary in order to properly use the states before they are overwritten,
  9560. // while avoiding to make unnecessary copies of the states)
  9561. auto get_ssm_rows = [&](ggml_context * ctx, ggml_tensor * states, ggml_tensor * ids) {
  9562. ggml_tensor * ssm = ggml_reshape_4d(ctx, states, d_state, head_dim, n_head, mctx_cur->get_size());
  9563. // Custom operator to optimize the parallel associative scan
  9564. // as described in the Annex D of the Mamba paper.
  9565. // => {d_inner, n_seq_tokens, n_seqs} and {d_state, d_inner, n_seqs}
  9566. return ggml_ssm_scan(ctx, ssm, x, dt, A, B, C, ids);
  9567. };
  9568. ggml_tensor * y_ssm = build_rs(inp, ssm_states_all, hparams.n_embd_s(), ubatch.n_seqs, get_ssm_rows);
  9569. // store last states
  9570. ggml_build_forward_expand(gf,
  9571. ggml_cpy(ctx0,
  9572. ggml_view_1d(ctx0, y_ssm, d_state*d_inner*n_seqs, x->nb[3]*x->ne[3]),
  9573. 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))));
  9574. ggml_tensor * y = ggml_view_3d(ctx0, y_ssm, d_inner, n_seq_tokens, n_seqs, x->nb[2], x->nb[3], 0);
  9575. // TODO: skip computing output earlier for unused tokens
  9576. y = ggml_add(ctx0, y, ggml_mul(ctx0, cur, layer.ssm_d));
  9577. y = ggml_swiglu_split(ctx0, ggml_cont(ctx0, z), y);
  9578. // {d_inner, n_embd} @ {d_inner, n_seq_tokens, n_seqs} => {n_embd, n_seq_tokens, n_seqs}
  9579. cur = build_lora_mm(layer.ssm_out, y);
  9580. }
  9581. // {n_embd, n_seq_tokens, n_seqs} => {n_embd, n_tokens}
  9582. cur = ggml_reshape_2d(ctx0, cur, cur->ne[0], n_seq_tokens * n_seqs);
  9583. return cur;
  9584. }
  9585. ggml_tensor * build_mamba2_layer(
  9586. llm_graph_input_rs * inp,
  9587. ggml_tensor * cur,
  9588. const llama_model & model,
  9589. const llama_ubatch & ubatch,
  9590. int il) const {
  9591. const auto * mctx_cur = inp->mctx;
  9592. const auto kv_head = mctx_cur->get_head();
  9593. const int64_t d_conv = hparams.ssm_d_conv;
  9594. const int64_t d_inner = hparams.ssm_d_inner;
  9595. const int64_t d_state = hparams.ssm_d_state;
  9596. const int64_t n_head = hparams.ssm_dt_rank;
  9597. const int64_t head_dim = d_inner / n_head;
  9598. const int64_t n_group = hparams.ssm_n_group;
  9599. const int64_t n_seqs = ubatch.n_seqs;
  9600. const int64_t n_seq_tokens = ubatch.n_seq_tokens;
  9601. GGML_ASSERT(n_seqs != 0);
  9602. GGML_ASSERT(ubatch.equal_seqs());
  9603. GGML_ASSERT(ubatch.n_tokens == n_seq_tokens * n_seqs);
  9604. ggml_tensor * conv_states_all = mctx_cur->get_r_l(il);
  9605. ggml_tensor * ssm_states_all = mctx_cur->get_s_l(il);
  9606. ggml_tensor * conv = build_rs(inp, conv_states_all, hparams.n_embd_r(), n_seqs);
  9607. conv = ggml_reshape_3d(ctx0, conv, d_conv - 1, d_inner + 2*n_group*d_state, n_seqs);
  9608. // {n_embd, n_tokens} => {n_embd, n_seq_tokens, n_seqs}
  9609. cur = ggml_reshape_3d(ctx0, cur, cur->ne[0], n_seq_tokens, n_seqs);
  9610. // d_in_proj = 2 * self.d_inner + 2 * self.ngroups * self.d_state + self.nheads
  9611. // {n_embd, d_in_proj} @ {n_embd, n_seq_tokens, n_seqs} => {d_in_proj, n_seq_tokens, n_seqs}
  9612. ggml_tensor * zxBCdt = build_lora_mm(model.layers[il].ssm_in, cur);
  9613. // split the above in three
  9614. 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);
  9615. 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));
  9616. 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));
  9617. // conv
  9618. {
  9619. // => {d_conv - 1 + n_seq_tokens, d_inner + 2*n_group*d_state, n_seqs}
  9620. ggml_tensor * conv_x = ggml_concat(ctx0, conv, ggml_transpose(ctx0, xBC), 0);
  9621. // copy last (d_conv - 1) columns back into the state cache
  9622. 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]));
  9623. ggml_build_forward_expand(gf,
  9624. ggml_cpy(ctx0, last_conv,
  9625. ggml_view_1d(ctx0, conv_states_all,
  9626. (d_conv - 1)*(d_inner + 2*n_group*d_state)*(n_seqs),
  9627. kv_head*(d_conv - 1)*(d_inner + 2*n_group*d_state)*ggml_element_size(conv_states_all))));
  9628. // 1D convolution
  9629. // The equivalent is to make a self-overlapping view of conv_x
  9630. // over d_conv columns at each stride in the 3rd dimension,
  9631. // then element-wise multiply that with the conv1d weight,
  9632. // then sum the elements of each row,
  9633. // (the last two steps are a dot product over rows (also doable with mul_mat))
  9634. // then permute away the ne[0] dimension,
  9635. // and then you're left with the resulting x tensor.
  9636. // For simultaneous sequences, all sequences need to have the same length.
  9637. xBC = ggml_ssm_conv(ctx0, conv_x, model.layers[il].ssm_conv1d);
  9638. // bias
  9639. xBC = ggml_add(ctx0, xBC, model.layers[il].ssm_conv1d_b);
  9640. xBC = ggml_silu(ctx0, xBC);
  9641. }
  9642. // ssm
  9643. {
  9644. // These correspond to V K Q in SSM/attention duality
  9645. 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);
  9646. 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));
  9647. 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));
  9648. // {n_head, n_seq_tokens, n_seqs}
  9649. dt = ggml_add(ctx0, ggml_cont(ctx0, dt), model.layers[il].ssm_dt_b);
  9650. ggml_tensor * A = model.layers[il].ssm_a;
  9651. // use the states and the indices provided by build_recurrent_state
  9652. // (this is necessary in order to properly use the states before they are overwritten,
  9653. // while avoiding to make unnecessary copies of the states)
  9654. auto get_ssm_rows = [&](ggml_context * ctx, ggml_tensor * states, ggml_tensor * ids) {
  9655. ggml_tensor * ssm = ggml_reshape_4d(ctx, states, d_state, head_dim, n_head, mctx_cur->get_size());
  9656. // TODO: use semistructured matrices to implement state-space duality
  9657. // => {d_inner, n_seq_tokens, n_seqs} and {d_state, d_inner, n_seqs}
  9658. return ggml_ssm_scan(ctx, ssm, x, dt, A, B, C, ids);
  9659. };
  9660. ggml_tensor * y_ssm = build_rs(inp, ssm_states_all, hparams.n_embd_s(), ubatch.n_seqs, get_ssm_rows);
  9661. // store last states
  9662. ggml_build_forward_expand(gf,
  9663. ggml_cpy(ctx0,
  9664. ggml_view_1d(ctx0, y_ssm, d_state*d_inner*n_seqs, ggml_nelements(x)*x->nb[0]),
  9665. 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))));
  9666. 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);
  9667. // TODO: skip computing output earlier for unused tokens
  9668. y = ggml_add(ctx0, y, ggml_mul(ctx0, x, model.layers[il].ssm_d));
  9669. cb(y, "mamba2_y_add_d", il);
  9670. y = ggml_swiglu_split(ctx0, ggml_cont(ctx0, z), y);
  9671. // grouped RMS norm
  9672. if (model.layers[il].ssm_norm) {
  9673. y = ggml_reshape_4d(ctx0, y, d_inner / n_group, n_group, n_seq_tokens, n_seqs);
  9674. y = build_norm(y, model.layers[il].ssm_norm, NULL, LLM_NORM_RMS, il);
  9675. }
  9676. y = ggml_reshape_3d(ctx0, y, d_inner, n_seq_tokens, n_seqs);
  9677. // {d_inner, n_embd} @ {d_inner, n_seq_tokens, n_seqs} => {n_embd, n_seq_tokens, n_seqs}
  9678. cur = build_lora_mm(model.layers[il].ssm_out, y);
  9679. }
  9680. // {n_embd, n_seq_tokens, n_seqs} => {n_embd, n_tokens}
  9681. cur = ggml_reshape_2d(ctx0, cur, cur->ne[0], n_seq_tokens * n_seqs);
  9682. cb(cur, "mamba_out", il);
  9683. return cur;
  9684. }
  9685. };
  9686. struct llm_build_mamba : public llm_graph_context_mamba {
  9687. llm_build_mamba(const llama_model & model, const llm_graph_params & params) : llm_graph_context_mamba(params) {
  9688. ggml_tensor * cur;
  9689. ggml_tensor * inpL;
  9690. // {n_embd, n_tokens}
  9691. inpL = build_inp_embd(model.tok_embd);
  9692. auto * rs_inp = build_rs_inp();
  9693. ggml_tensor * inp_out_ids = build_inp_out_ids();
  9694. for (int il = 0; il < n_layer; ++il) {
  9695. // norm
  9696. cur = build_norm(inpL,
  9697. model.layers[il].attn_norm, NULL,
  9698. LLM_NORM_RMS, il);
  9699. cb(cur, "attn_norm", il);
  9700. if (model.arch == LLM_ARCH_MAMBA2) {
  9701. cur = build_mamba2_layer(rs_inp, cur, model, ubatch, il);
  9702. } else {
  9703. cur = build_mamba_layer(rs_inp, cur, model, ubatch, il);
  9704. }
  9705. if (il == n_layer - 1 && inp_out_ids) {
  9706. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  9707. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  9708. }
  9709. // residual
  9710. cur = ggml_add(ctx0, cur, inpL);
  9711. cur = build_cvec(cur, il);
  9712. cb(cur, "l_out", il);
  9713. // input for next layer
  9714. inpL = cur;
  9715. }
  9716. // final rmsnorm
  9717. cur = build_norm(inpL, model.output_norm, NULL, LLM_NORM_RMS, -1);
  9718. cb(cur, "result_norm", -1);
  9719. res->t_embd = cur;
  9720. // lm_head
  9721. cur = build_lora_mm(model.output, cur);
  9722. cb(cur, "result_output", -1);
  9723. res->t_logits = cur;
  9724. ggml_build_forward_expand(gf, cur);
  9725. }
  9726. };
  9727. struct llm_build_jamba : public llm_graph_context_mamba {
  9728. llm_build_jamba(const llama_model & model, const llm_graph_params & params) : llm_graph_context_mamba(params) {
  9729. const int64_t n_embd_head = hparams.n_embd_head_v;
  9730. ggml_tensor * cur;
  9731. ggml_tensor * inpL;
  9732. // {n_embd, n_tokens}
  9733. inpL = build_inp_embd(model.tok_embd);
  9734. auto * inp_hybrid = build_inp_mem_hybrid();
  9735. ggml_tensor * inp_out_ids = build_inp_out_ids();
  9736. for (int il = 0; il < n_layer; ++il) {
  9737. const int64_t n_head_kv = hparams.n_head_kv(il);
  9738. cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
  9739. cb(cur, "attn_norm", il);
  9740. if (n_head_kv == 0) {
  9741. cur = build_mamba_layer(inp_hybrid->get_recr(), cur, model, ubatch, il);
  9742. } else {
  9743. // Attention
  9744. struct ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  9745. struct ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  9746. struct ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  9747. cb(Qcur, "Qcur", il);
  9748. cb(Kcur, "Kcur", il);
  9749. cb(Vcur, "Vcur", il);
  9750. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  9751. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  9752. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  9753. cb(Qcur, "Qcur", il);
  9754. cb(Kcur, "Kcur", il);
  9755. cb(Vcur, "Vcur", il);
  9756. // No RoPE :)
  9757. cur = build_attn(inp_hybrid->get_attn(),
  9758. model.layers[il].wo, NULL,
  9759. Qcur, Kcur, Vcur, NULL, NULL, NULL, 1.0f/sqrtf(float(n_embd_head)), il);
  9760. }
  9761. if (il == n_layer - 1 && inp_out_ids) {
  9762. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  9763. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  9764. }
  9765. // residual
  9766. struct ggml_tensor * ffn_inp = ggml_add(ctx0, inpL, cur);
  9767. cb(cur, "ffn_inp", il);
  9768. cur = build_norm(ffn_inp, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);
  9769. cb(cur, "ffn_norm", il);
  9770. // feed-forward network
  9771. if (model.layers[il].ffn_gate_inp == nullptr) {
  9772. // FFN
  9773. cur = build_ffn(cur,
  9774. model.layers[il].ffn_up, NULL, NULL,
  9775. model.layers[il].ffn_gate, NULL, NULL,
  9776. model.layers[il].ffn_down, NULL, NULL,
  9777. NULL,
  9778. LLM_FFN_SILU, LLM_FFN_PAR, il);
  9779. cb(cur, "ffn_out", il);
  9780. } else {
  9781. // MoE branch
  9782. cur = build_moe_ffn(cur,
  9783. model.layers[il].ffn_gate_inp,
  9784. model.layers[il].ffn_up_exps,
  9785. model.layers[il].ffn_gate_exps,
  9786. model.layers[il].ffn_down_exps,
  9787. nullptr,
  9788. n_expert, n_expert_used,
  9789. LLM_FFN_SILU, false,
  9790. false, 0.0,
  9791. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  9792. il);
  9793. cb(cur, "ffn_moe_out", il);
  9794. }
  9795. // residual
  9796. cur = ggml_add(ctx0, ffn_inp, cur);
  9797. cur = build_cvec(cur, il);
  9798. cb(cur, "l_out", il);
  9799. // input for next layer
  9800. inpL = cur;
  9801. }
  9802. // final rmsnorm
  9803. cur = build_norm(inpL, model.output_norm, NULL, LLM_NORM_RMS, -1);
  9804. cb(cur, "result_norm", -1);
  9805. res->t_embd = cur;
  9806. // lm_head
  9807. cur = build_lora_mm(model.output, cur);
  9808. cb(cur, "result_output", -1);
  9809. res->t_logits = cur;
  9810. ggml_build_forward_expand(gf, cur);
  9811. }
  9812. };
  9813. struct llm_build_command_r : public llm_graph_context {
  9814. llm_build_command_r(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  9815. const int64_t n_embd_head = hparams.n_embd_head_v;
  9816. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  9817. const float f_logit_scale = hparams.f_logit_scale;
  9818. ggml_tensor * cur;
  9819. ggml_tensor * inpL;
  9820. inpL = build_inp_embd(model.tok_embd);
  9821. // inp_pos - contains the positions
  9822. ggml_tensor * inp_pos = build_inp_pos();
  9823. auto * inp_attn = build_attn_inp_kv();
  9824. ggml_tensor * inp_out_ids = build_inp_out_ids();
  9825. for (int il = 0; il < n_layer; ++il) {
  9826. // norm
  9827. cur = build_norm(inpL,
  9828. model.layers[il].attn_norm, NULL,
  9829. LLM_NORM, il);
  9830. cb(cur, "attn_norm", il);
  9831. ggml_tensor * ffn_inp = cur;
  9832. // self-attention
  9833. {
  9834. // compute Q and K and RoPE them
  9835. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  9836. cb(Qcur, "Qcur", il);
  9837. if (model.layers[il].bq) {
  9838. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  9839. cb(Qcur, "Qcur", il);
  9840. }
  9841. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  9842. cb(Kcur, "Kcur", il);
  9843. if (model.layers[il].bk) {
  9844. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  9845. cb(Kcur, "Kcur", il);
  9846. }
  9847. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  9848. cb(Vcur, "Vcur", il);
  9849. if (model.layers[il].bv) {
  9850. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  9851. cb(Vcur, "Vcur", il);
  9852. }
  9853. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  9854. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  9855. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  9856. if (model.layers[il].attn_q_norm) {
  9857. Qcur = build_norm(Qcur,
  9858. model.layers[il].attn_q_norm,
  9859. NULL,
  9860. LLM_NORM, il);
  9861. cb(Qcur, "Qcur", il);
  9862. }
  9863. Qcur = ggml_rope_ext(
  9864. ctx0, Qcur, inp_pos, nullptr,
  9865. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  9866. ext_factor, attn_factor, beta_fast, beta_slow
  9867. );
  9868. if (model.layers[il].attn_k_norm) {
  9869. Kcur = build_norm(Kcur,
  9870. model.layers[il].attn_k_norm,
  9871. NULL,
  9872. LLM_NORM, il);
  9873. cb(Kcur, "Kcur", il);
  9874. }
  9875. Kcur = ggml_rope_ext(
  9876. ctx0, Kcur, inp_pos, nullptr,
  9877. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  9878. ext_factor, attn_factor, beta_fast, beta_slow
  9879. );
  9880. cb(Qcur, "Qcur", il);
  9881. cb(Kcur, "Kcur", il);
  9882. cb(Vcur, "Vcur", il);
  9883. cur = build_attn(inp_attn,
  9884. model.layers[il].wo, model.layers[il].bo,
  9885. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  9886. }
  9887. if (il == n_layer - 1 && inp_out_ids) {
  9888. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  9889. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  9890. ffn_inp = ggml_get_rows(ctx0, ffn_inp, inp_out_ids);
  9891. }
  9892. ggml_tensor * attn_out = cur;
  9893. // feed-forward network
  9894. {
  9895. cur = build_ffn(ffn_inp,
  9896. model.layers[il].ffn_up, NULL, NULL,
  9897. model.layers[il].ffn_gate, NULL, NULL,
  9898. model.layers[il].ffn_down, NULL, NULL,
  9899. NULL,
  9900. LLM_FFN_SILU, LLM_FFN_PAR, il);
  9901. cb(cur, "ffn_out", il);
  9902. }
  9903. // add together residual + FFN + self-attention
  9904. cur = ggml_add(ctx0, cur, inpL);
  9905. cur = ggml_add(ctx0, cur, attn_out);
  9906. cur = build_cvec(cur, il);
  9907. cb(cur, "l_out", il);
  9908. // input for next layer
  9909. inpL = cur;
  9910. }
  9911. cur = inpL;
  9912. cur = build_norm(cur,
  9913. model.output_norm, NULL,
  9914. LLM_NORM, -1);
  9915. cb(cur, "result_norm", -1);
  9916. res->t_embd = cur;
  9917. // lm_head
  9918. cur = build_lora_mm(model.output, cur);
  9919. if (f_logit_scale) {
  9920. cur = ggml_scale(ctx0, cur, f_logit_scale);
  9921. }
  9922. cb(cur, "result_output", -1);
  9923. res->t_logits = cur;
  9924. ggml_build_forward_expand(gf, cur);
  9925. }
  9926. };
  9927. struct llm_build_cohere2_iswa : public llm_graph_context {
  9928. llm_build_cohere2_iswa(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  9929. const int64_t n_embd_head = hparams.n_embd_head_v;
  9930. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  9931. const float f_logit_scale = hparams.f_logit_scale;
  9932. ggml_tensor * cur;
  9933. ggml_tensor * inpL;
  9934. inpL = build_inp_embd(model.tok_embd);
  9935. // inp_pos - contains the positions
  9936. ggml_tensor * inp_pos = build_inp_pos();
  9937. auto * inp_attn = build_attn_inp_kv_iswa();
  9938. ggml_tensor * inp_out_ids = build_inp_out_ids();
  9939. for (int il = 0; il < n_layer; ++il) {
  9940. const bool is_swa = hparams.is_swa(il);
  9941. // norm
  9942. cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM, il);
  9943. cb(cur, "attn_norm", il);
  9944. ggml_tensor * ffn_inp = cur;
  9945. // self-attention
  9946. {
  9947. // rope freq factors for 128k context
  9948. ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
  9949. // compute Q and K and RoPE them
  9950. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  9951. cb(Qcur, "Qcur", il);
  9952. if (model.layers[il].bq) {
  9953. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  9954. cb(Qcur, "Qcur", il);
  9955. }
  9956. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  9957. cb(Kcur, "Kcur", il);
  9958. if (model.layers[il].bk) {
  9959. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  9960. cb(Kcur, "Kcur", il);
  9961. }
  9962. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  9963. cb(Vcur, "Vcur", il);
  9964. if (model.layers[il].bv) {
  9965. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  9966. cb(Vcur, "Vcur", il);
  9967. }
  9968. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  9969. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  9970. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  9971. if (is_swa) {
  9972. Qcur = ggml_rope_ext(
  9973. ctx0, Qcur, inp_pos, rope_factors,
  9974. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  9975. ext_factor, attn_factor, beta_fast, beta_slow
  9976. );
  9977. Kcur = ggml_rope_ext(
  9978. ctx0, Kcur, inp_pos, rope_factors,
  9979. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  9980. ext_factor, attn_factor, beta_fast, beta_slow
  9981. );
  9982. }
  9983. cb(Qcur, "Qcur", il);
  9984. cb(Kcur, "Kcur", il);
  9985. cb(Vcur, "Vcur", il);
  9986. cur = build_attn(inp_attn,
  9987. model.layers[il].wo, model.layers[il].bo,
  9988. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  9989. }
  9990. if (il == n_layer - 1 && inp_out_ids) {
  9991. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  9992. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  9993. ffn_inp = ggml_get_rows(ctx0, ffn_inp, inp_out_ids);
  9994. }
  9995. ggml_tensor * attn_out = cur;
  9996. // feed-forward network
  9997. {
  9998. cur = build_ffn(ffn_inp, model.layers[il].ffn_up, NULL, NULL, model.layers[il].ffn_gate,
  9999. NULL, NULL, model.layers[il].ffn_down, NULL, NULL, NULL, LLM_FFN_SILU, LLM_FFN_PAR,
  10000. il);
  10001. cb(cur, "ffn_out", il);
  10002. }
  10003. // add together residual + FFN + self-attention
  10004. cur = ggml_add(ctx0, cur, inpL);
  10005. cur = ggml_add(ctx0, cur, attn_out);
  10006. cur = build_cvec(cur, il);
  10007. cb(cur, "l_out", il);
  10008. // input for next layer
  10009. inpL = cur;
  10010. }
  10011. cur = inpL;
  10012. cur = build_norm(cur, model.output_norm, NULL, LLM_NORM, -1);
  10013. cb(cur, "result_norm", -1);
  10014. res->t_embd = cur;
  10015. // lm_head
  10016. cur = build_lora_mm(model.output, cur);
  10017. if (f_logit_scale) {
  10018. cur = ggml_scale(ctx0, cur, f_logit_scale);
  10019. }
  10020. cb(cur, "result_output", -1);
  10021. res->t_logits = cur;
  10022. ggml_build_forward_expand(gf, cur);
  10023. }
  10024. };
  10025. // ref: https://allenai.org/olmo
  10026. // based on the original build_llama() function, changes:
  10027. // * non-parametric layer norm
  10028. // * clamp qkv
  10029. // * removed bias
  10030. // * removed MoE
  10031. struct llm_build_olmo : public llm_graph_context {
  10032. llm_build_olmo(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  10033. const int64_t n_embd_head = hparams.n_embd_head_v;
  10034. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  10035. GGML_ASSERT(n_embd_head == hparams.n_rot);
  10036. ggml_tensor * cur;
  10037. ggml_tensor * inpL;
  10038. inpL = build_inp_embd(model.tok_embd);
  10039. // inp_pos - contains the positions
  10040. ggml_tensor * inp_pos = build_inp_pos();
  10041. auto * inp_attn = build_attn_inp_kv();
  10042. ggml_tensor * inp_out_ids = build_inp_out_ids();
  10043. for (int il = 0; il < n_layer; ++il) {
  10044. ggml_tensor * inpSA = inpL;
  10045. // norm
  10046. cur = build_norm(inpL,
  10047. NULL, NULL,
  10048. LLM_NORM, il);
  10049. cb(cur, "attn_norm", il);
  10050. // self-attention
  10051. {
  10052. // compute Q and K and RoPE them
  10053. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  10054. cb(Qcur, "Qcur", il);
  10055. if (hparams.f_clamp_kqv > 0.0f) {
  10056. Qcur = ggml_clamp(ctx0, Qcur, -hparams.f_clamp_kqv, hparams.f_clamp_kqv);
  10057. cb(Qcur, "Qcur", il);
  10058. }
  10059. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  10060. cb(Kcur, "Kcur", il);
  10061. if (hparams.f_clamp_kqv > 0.0f) {
  10062. Kcur = ggml_clamp(ctx0, Kcur, -hparams.f_clamp_kqv, hparams.f_clamp_kqv);
  10063. cb(Kcur, "Kcur", il);
  10064. }
  10065. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  10066. cb(Vcur, "Vcur", il);
  10067. if (hparams.f_clamp_kqv > 0.0f) {
  10068. Vcur = ggml_clamp(ctx0, Vcur, -hparams.f_clamp_kqv, hparams.f_clamp_kqv);
  10069. cb(Vcur, "Vcur", il);
  10070. }
  10071. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  10072. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  10073. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  10074. Qcur = ggml_rope_ext(
  10075. ctx0, Qcur, inp_pos, nullptr,
  10076. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  10077. ext_factor, attn_factor, beta_fast, beta_slow
  10078. );
  10079. Kcur = ggml_rope_ext(
  10080. ctx0, Kcur, inp_pos, nullptr,
  10081. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  10082. ext_factor, attn_factor, beta_fast, beta_slow
  10083. );
  10084. cb(Qcur, "Qcur", il);
  10085. cb(Kcur, "Kcur", il);
  10086. cb(Vcur, "Vcur", il);
  10087. cur = build_attn(inp_attn,
  10088. model.layers[il].wo, nullptr,
  10089. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  10090. }
  10091. if (il == n_layer - 1 && inp_out_ids) {
  10092. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  10093. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  10094. }
  10095. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  10096. cb(ffn_inp, "ffn_inp", il);
  10097. // feed-forward network
  10098. cur = build_norm(ffn_inp,
  10099. NULL, NULL,
  10100. LLM_NORM, il);
  10101. cb(cur, "ffn_norm", il);
  10102. cur = build_ffn(cur,
  10103. model.layers[il].ffn_up, NULL, NULL,
  10104. model.layers[il].ffn_gate, NULL, NULL,
  10105. model.layers[il].ffn_down, NULL, NULL,
  10106. NULL,
  10107. LLM_FFN_SILU, LLM_FFN_PAR, il);
  10108. cb(cur, "ffn_out", il);
  10109. cur = ggml_add(ctx0, cur, ffn_inp);
  10110. cb(cur, "ffn_out", il);
  10111. cur = build_cvec(cur, il);
  10112. cb(cur, "l_out", il);
  10113. // input for next layer
  10114. inpL = cur;
  10115. }
  10116. cur = inpL;
  10117. cur = build_norm(cur,
  10118. NULL, NULL,
  10119. LLM_NORM, -1);
  10120. cb(cur, "result_norm", -1);
  10121. res->t_embd = cur;
  10122. // lm_head
  10123. cur = build_lora_mm(model.output, cur);
  10124. cb(cur, "result_output", -1);
  10125. res->t_logits = cur;
  10126. ggml_build_forward_expand(gf, cur);
  10127. }
  10128. };
  10129. template <bool iswa>
  10130. struct llm_build_olmo2 : public llm_graph_context {
  10131. llm_build_olmo2(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  10132. const int64_t n_embd_head = hparams.n_embd_head_v;
  10133. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  10134. GGML_ASSERT(n_embd_head == hparams.n_rot);
  10135. ggml_tensor * cur;
  10136. ggml_tensor * inpL;
  10137. inpL = build_inp_embd(model.tok_embd);
  10138. // inp_pos - contains the positions
  10139. ggml_tensor * inp_pos = build_inp_pos();
  10140. using inp_attn_type = std::conditional_t<iswa, llm_graph_input_attn_kv_iswa, llm_graph_input_attn_kv>;
  10141. inp_attn_type * inp_attn = nullptr;
  10142. if constexpr (iswa) {
  10143. inp_attn = build_attn_inp_kv_iswa();
  10144. } else {
  10145. inp_attn = build_attn_inp_kv();
  10146. }
  10147. ggml_tensor * inp_out_ids = build_inp_out_ids();
  10148. for (int il = 0; il < n_layer; ++il) {
  10149. ggml_tensor * inpSA = inpL;
  10150. cur = inpL;
  10151. // self_attention
  10152. {
  10153. // compute Q and K and RoPE them
  10154. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  10155. cb(Qcur, "Qcur", il);
  10156. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  10157. cb(Kcur, "Kcur", il);
  10158. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  10159. cb(Vcur, "Vcur", il);
  10160. Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL,
  10161. LLM_NORM_RMS, il);
  10162. cb(Qcur, "Qcur_normed", il);
  10163. Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL,
  10164. LLM_NORM_RMS, il);
  10165. cb(Kcur, "Kcur_normed", il);
  10166. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  10167. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  10168. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  10169. const bool is_swa = hparams.is_swa(il);
  10170. if (is_swa) {
  10171. // For sliding window layers, Olmo3 use regular rope with no yarn rope scaling.
  10172. // This is achieved here by setting freq_scale and attn_factor to 1.
  10173. // We also set ext_factor to 0 to avoid a few unnecessary computations.
  10174. Qcur = ggml_rope_ext(
  10175. ctx0, Qcur, inp_pos, nullptr,
  10176. n_rot, rope_type, n_ctx_orig, freq_base, 1.0,
  10177. 0.0, 1.0, beta_fast, beta_slow
  10178. );
  10179. Kcur = ggml_rope_ext(
  10180. ctx0, Kcur, inp_pos, nullptr,
  10181. n_rot, rope_type, n_ctx_orig, freq_base, 1.0,
  10182. 0.0, 1.0, beta_fast, beta_slow
  10183. );
  10184. } else {
  10185. Qcur = ggml_rope_ext(
  10186. ctx0, Qcur, inp_pos, nullptr,
  10187. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  10188. ext_factor, attn_factor, beta_fast, beta_slow
  10189. );
  10190. Kcur = ggml_rope_ext(
  10191. ctx0, Kcur, inp_pos, nullptr,
  10192. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  10193. ext_factor, attn_factor, beta_fast, beta_slow
  10194. );
  10195. }
  10196. cb(Qcur, "Qcur", il);
  10197. cb(Kcur, "Kcur", il);
  10198. cb(Vcur, "Vcur", il);
  10199. cur = build_attn(inp_attn,
  10200. model.layers[il].wo, NULL,
  10201. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  10202. }
  10203. if (il == n_layer - 1 && inp_out_ids) {
  10204. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  10205. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  10206. }
  10207. cur = build_norm(cur,
  10208. model.layers[il].attn_post_norm, NULL,
  10209. LLM_NORM_RMS, il);
  10210. cb(cur, "attn_post_norm", il);
  10211. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  10212. cb(ffn_inp, "ffn_inp", il);
  10213. // feed-forward network
  10214. cur = build_ffn(ffn_inp,
  10215. model.layers[il].ffn_up, NULL, NULL,
  10216. model.layers[il].ffn_gate, NULL, NULL,
  10217. model.layers[il].ffn_down, NULL, NULL,
  10218. NULL,
  10219. LLM_FFN_SILU, LLM_FFN_PAR, il);
  10220. cb(cur, "ffn_out", il);
  10221. cur = build_norm(cur,
  10222. model.layers[il].ffn_post_norm, NULL,
  10223. LLM_NORM_RMS, -1);
  10224. cb(cur, "ffn_post_norm", -1);
  10225. cur = ggml_add(ctx0, cur, ffn_inp);
  10226. cb(cur, "ffn_out", il);
  10227. cur = build_cvec(cur, il);
  10228. cb(cur, "l_out", il);
  10229. // input for next layer
  10230. inpL = cur;
  10231. }
  10232. cur = inpL;
  10233. cur = build_norm(cur,
  10234. model.output_norm, NULL,
  10235. LLM_NORM_RMS, -1);
  10236. cb(cur, "result_norm", -1);
  10237. res->t_embd = cur;
  10238. // lm_head
  10239. cur = build_lora_mm(model.output, cur);
  10240. cb(cur, "result_output", -1);
  10241. res->t_logits = cur;
  10242. ggml_build_forward_expand(gf, cur);
  10243. }
  10244. };
  10245. // based on the build_qwen2moe() function, changes:
  10246. // * removed shared experts
  10247. // * removed bias
  10248. // * added q, k norm
  10249. struct llm_build_olmoe : public llm_graph_context {
  10250. llm_build_olmoe(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  10251. const int64_t n_embd_head = hparams.n_embd_head_v;
  10252. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  10253. GGML_ASSERT(n_embd_head == hparams.n_rot);
  10254. ggml_tensor * cur;
  10255. ggml_tensor * inpL;
  10256. inpL = build_inp_embd(model.tok_embd);
  10257. // inp_pos - contains the positions
  10258. ggml_tensor * inp_pos = build_inp_pos();
  10259. auto * inp_attn = build_attn_inp_kv();
  10260. ggml_tensor * inp_out_ids = build_inp_out_ids();
  10261. for (int il = 0; il < n_layer; ++il) {
  10262. ggml_tensor * inpSA = inpL;
  10263. // norm
  10264. cur = build_norm(inpL,
  10265. model.layers[il].attn_norm, NULL,
  10266. LLM_NORM_RMS, il);
  10267. cb(cur, "attn_norm", il);
  10268. // self_attention
  10269. {
  10270. // compute Q and K and RoPE them
  10271. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  10272. cb(Qcur, "Qcur", il);
  10273. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  10274. cb(Kcur, "Kcur", il);
  10275. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  10276. cb(Vcur, "Vcur", il);
  10277. Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL,
  10278. LLM_NORM_RMS, il);
  10279. cb(Qcur, "Qcur_normed", il);
  10280. Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL,
  10281. LLM_NORM_RMS, il);
  10282. cb(Kcur, "Kcur_normed", il);
  10283. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  10284. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  10285. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  10286. Qcur = ggml_rope_ext(
  10287. ctx0, Qcur, inp_pos, nullptr,
  10288. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  10289. ext_factor, attn_factor, beta_fast, beta_slow
  10290. );
  10291. Kcur = ggml_rope_ext(
  10292. ctx0, Kcur, inp_pos, nullptr,
  10293. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  10294. ext_factor, attn_factor, beta_fast, beta_slow
  10295. );
  10296. cb(Qcur, "Qcur", il);
  10297. cb(Kcur, "Kcur", il);
  10298. cb(Vcur, "Vcur", il);
  10299. cur = build_attn(inp_attn,
  10300. model.layers[il].wo, NULL,
  10301. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  10302. }
  10303. if (il == n_layer - 1 && inp_out_ids) {
  10304. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  10305. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  10306. }
  10307. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  10308. cb(ffn_inp, "ffn_inp", il);
  10309. // MoE branch
  10310. cur = build_norm(ffn_inp,
  10311. model.layers[il].ffn_norm, NULL,
  10312. LLM_NORM_RMS, il);
  10313. cb(cur, "ffn_norm", il);
  10314. cur = build_moe_ffn(cur,
  10315. model.layers[il].ffn_gate_inp,
  10316. model.layers[il].ffn_up_exps,
  10317. model.layers[il].ffn_gate_exps,
  10318. model.layers[il].ffn_down_exps,
  10319. nullptr,
  10320. n_expert, n_expert_used,
  10321. LLM_FFN_SILU, false,
  10322. false, 0.0,
  10323. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  10324. il);
  10325. cb(cur, "ffn_moe_out", il);
  10326. cur = ggml_add(ctx0, cur, ffn_inp);
  10327. cur = build_cvec(cur, il);
  10328. cb(cur, "l_out", il);
  10329. // input for next layer
  10330. inpL = cur;
  10331. }
  10332. cur = inpL;
  10333. cur = build_norm(cur,
  10334. model.output_norm, NULL,
  10335. LLM_NORM_RMS, -1);
  10336. cb(cur, "result_norm", -1);
  10337. res->t_embd = cur;
  10338. // lm_head
  10339. cur = build_lora_mm(model.output, cur);
  10340. cb(cur, "result_output", -1);
  10341. res->t_logits = cur;
  10342. ggml_build_forward_expand(gf, cur);
  10343. }
  10344. };
  10345. struct llm_build_llada_moe : public llm_graph_context {
  10346. llm_build_llada_moe(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  10347. const int64_t n_embd_head = hparams.n_embd_head_v;
  10348. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  10349. GGML_ASSERT(n_embd_head == hparams.n_rot);
  10350. ggml_tensor * cur;
  10351. ggml_tensor * inpL;
  10352. inpL = build_inp_embd(model.tok_embd);
  10353. // inp_pos - contains the positions
  10354. ggml_tensor * inp_pos = build_inp_pos();
  10355. auto * inp_attn = build_attn_inp_no_cache();
  10356. ggml_tensor * inp_out_ids = build_inp_out_ids();
  10357. for (int il = 0; il < n_layer; ++il) {
  10358. ggml_tensor * inpSA = inpL;
  10359. // norm
  10360. cur = build_norm(inpL,
  10361. model.layers[il].attn_norm, NULL,
  10362. LLM_NORM_RMS, il);
  10363. cb(cur, "attn_norm", il);
  10364. // self_attention
  10365. {
  10366. // compute Q and K and RoPE them
  10367. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  10368. cb(Qcur, "Qcur", il);
  10369. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  10370. cb(Kcur, "Kcur", il);
  10371. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  10372. cb(Vcur, "Vcur", il);
  10373. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  10374. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  10375. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  10376. Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);
  10377. cb(Qcur, "Qcur_normed", il);
  10378. Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);
  10379. cb(Kcur, "Kcur_normed", il);
  10380. Qcur = ggml_rope_ext(
  10381. ctx0, Qcur, inp_pos, nullptr,
  10382. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  10383. ext_factor, attn_factor, beta_fast, beta_slow
  10384. );
  10385. Kcur = ggml_rope_ext(
  10386. ctx0, Kcur, inp_pos, nullptr,
  10387. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  10388. ext_factor, attn_factor, beta_fast, beta_slow
  10389. );
  10390. cb(Qcur, "Qcur", il);
  10391. cb(Kcur, "Kcur", il);
  10392. cb(Vcur, "Vcur", il);
  10393. cur = build_attn(inp_attn,
  10394. model.layers[il].wo, NULL,
  10395. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  10396. }
  10397. if (il == n_layer - 1 && inp_out_ids) {
  10398. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  10399. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  10400. }
  10401. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  10402. cb(ffn_inp, "ffn_inp", il);
  10403. // MoE branch
  10404. cur = build_norm(ffn_inp,
  10405. model.layers[il].ffn_norm, NULL,
  10406. LLM_NORM_RMS, il);
  10407. cb(cur, "ffn_norm", il);
  10408. cur = build_moe_ffn(cur,
  10409. model.layers[il].ffn_gate_inp,
  10410. model.layers[il].ffn_up_exps,
  10411. model.layers[il].ffn_gate_exps,
  10412. model.layers[il].ffn_down_exps,
  10413. nullptr,
  10414. n_expert, n_expert_used,
  10415. LLM_FFN_SILU, false,
  10416. false, 0.0,
  10417. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  10418. il);
  10419. cb(cur, "ffn_moe_out", il);
  10420. cur = ggml_add(ctx0, cur, ffn_inp);
  10421. cur = build_cvec(cur, il);
  10422. cb(cur, "l_out", il);
  10423. // input for next layer
  10424. inpL = cur;
  10425. }
  10426. cur = inpL;
  10427. cur = build_norm(cur,
  10428. model.output_norm, NULL,
  10429. LLM_NORM_RMS, -1);
  10430. cb(cur, "result_norm", -1);
  10431. res->t_embd = cur;
  10432. // lm_head
  10433. cur = build_lora_mm(model.output, cur);
  10434. cb(cur, "result_output", -1);
  10435. res->t_logits = cur;
  10436. ggml_build_forward_expand(gf, cur);
  10437. }
  10438. };
  10439. struct llm_build_openelm : public llm_graph_context {
  10440. llm_build_openelm(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  10441. const int64_t n_embd_head = hparams.n_embd_head_v;
  10442. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  10443. ggml_tensor * cur;
  10444. ggml_tensor * inpL;
  10445. inpL = build_inp_embd(model.tok_embd);
  10446. // inp_pos - contains the positions
  10447. ggml_tensor * inp_pos = build_inp_pos();
  10448. auto * inp_attn = build_attn_inp_kv();
  10449. ggml_tensor * inp_out_ids = build_inp_out_ids();
  10450. for (int il = 0; il < n_layer; ++il) {
  10451. const int64_t n_head = hparams.n_head(il);
  10452. const int64_t n_head_kv = hparams.n_head_kv(il);
  10453. const int64_t n_head_qkv = 2*n_head_kv + n_head;
  10454. cur = inpL;
  10455. ggml_tensor * residual = cur;
  10456. // norm
  10457. cur = build_norm(inpL,
  10458. model.layers[il].attn_norm, NULL,
  10459. LLM_NORM_RMS, il);
  10460. cb(cur, "attn_norm", il);
  10461. // self-attention
  10462. {
  10463. cur = build_lora_mm(model.layers[il].wqkv, cur);
  10464. cb(cur, "wqkv", il);
  10465. cur = ggml_reshape_3d(ctx0, cur, n_embd_head_k, n_head_qkv, n_tokens);
  10466. ggml_tensor * Qcur = ggml_view_3d(ctx0, cur, n_embd_head, n_head, n_tokens, cur->nb[1], cur->nb[2], 0);
  10467. cb(Qcur, "Qcur", il);
  10468. 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);
  10469. cb(Kcur, "Kcur", il);
  10470. 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)));
  10471. cb(Vcur, "Vcur", il);
  10472. Qcur = build_norm(Qcur,
  10473. model.layers[il].attn_q_norm, NULL,
  10474. LLM_NORM_RMS, il);
  10475. cb(Qcur, "Qcur", il);
  10476. Kcur = build_norm(Kcur,
  10477. model.layers[il].attn_k_norm, NULL,
  10478. LLM_NORM_RMS, il);
  10479. cb(Kcur, "Kcur", il);
  10480. Qcur = ggml_rope_ext(
  10481. ctx0, Qcur, inp_pos, NULL,
  10482. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  10483. ext_factor, attn_factor, beta_fast, beta_slow
  10484. );
  10485. Kcur = ggml_rope_ext(
  10486. ctx0, Kcur, inp_pos, NULL,
  10487. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  10488. ext_factor, attn_factor, beta_fast, beta_slow
  10489. );
  10490. cb(Qcur, "Qcur", il);
  10491. cb(Kcur, "Kcur", il);
  10492. cb(Qcur, "Vcur", il);
  10493. cur = build_attn(inp_attn,
  10494. model.layers[il].wo, NULL,
  10495. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  10496. }
  10497. if (il == n_layer - 1 && inp_out_ids) {
  10498. residual = ggml_get_rows(ctx0, residual, inp_out_ids);
  10499. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  10500. }
  10501. ggml_tensor * ffn_inp = ggml_add(ctx0, residual, cur);
  10502. cb(ffn_inp, "ffn_inp", il);
  10503. // feed-forward network
  10504. {
  10505. cur = build_norm(ffn_inp,
  10506. model.layers[il].ffn_norm, NULL,
  10507. LLM_NORM_RMS, il);
  10508. cb(cur, "ffn_norm", il);
  10509. cur = build_ffn(cur,
  10510. model.layers[il].ffn_up, NULL, NULL,
  10511. model.layers[il].ffn_gate, NULL, NULL,
  10512. model.layers[il].ffn_down, NULL, NULL,
  10513. NULL,
  10514. LLM_FFN_SILU, LLM_FFN_PAR, il);
  10515. cb(cur, "ffn_out", il);
  10516. }
  10517. cur = ggml_add(ctx0, cur, ffn_inp);
  10518. cur = build_cvec(cur, il);
  10519. cb(cur, "l_out", il);
  10520. inpL = cur;
  10521. }
  10522. cur = inpL;
  10523. // norm
  10524. cur = build_norm(cur,
  10525. model.output_norm, NULL,
  10526. LLM_NORM_RMS, -1);
  10527. cb(cur, "result_norm", -1);
  10528. res->t_embd = cur;
  10529. cur = build_lora_mm(model.output, cur);
  10530. cb(cur, "result_output", -1);
  10531. res->t_logits = cur;
  10532. ggml_build_forward_expand(gf, cur);
  10533. }
  10534. };
  10535. struct llm_build_gptneox : public llm_graph_context {
  10536. llm_build_gptneox(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  10537. const int64_t n_embd_head = hparams.n_embd_head_v;
  10538. const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  10539. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  10540. ggml_tensor * cur;
  10541. ggml_tensor * inpL;
  10542. inpL = build_inp_embd(model.tok_embd);
  10543. // inp_pos - contains the positions
  10544. ggml_tensor * inp_pos = build_inp_pos();
  10545. auto * inp_attn = build_attn_inp_kv();
  10546. ggml_tensor * inp_out_ids = build_inp_out_ids();
  10547. for (int il = 0; il < n_layer; ++il) {
  10548. cur = build_norm(inpL,
  10549. model.layers[il].attn_norm,
  10550. model.layers[il].attn_norm_b,
  10551. LLM_NORM, il);
  10552. cb(cur, "attn_norm", il);
  10553. // self-attention
  10554. {
  10555. cur = build_lora_mm(model.layers[il].wqkv, cur);
  10556. cb(cur, "wqkv", il);
  10557. cur = ggml_add(ctx0, cur, model.layers[il].bqkv);
  10558. cb(cur, "bqkv", il);
  10559. 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));
  10560. 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));
  10561. 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));
  10562. Qcur = ggml_rope_ext(
  10563. ctx0, Qcur, inp_pos, nullptr,
  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, nullptr,
  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, 1.0f/sqrtf(float(n_embd_head)), il);
  10578. }
  10579. if (il == n_layer - 1 && inp_out_ids) {
  10580. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  10581. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  10582. }
  10583. // ffn
  10584. if (hparams.use_par_res) {
  10585. // attention and ffn are computed in parallel
  10586. // x = x + attn(ln1(x)) + ffn(ln2(x))
  10587. ggml_tensor * attn_out = cur;
  10588. cur = build_norm(inpL,
  10589. model.layers[il].ffn_norm,
  10590. model.layers[il].ffn_norm_b,
  10591. LLM_NORM, il);
  10592. cb(cur, "ffn_norm", il);
  10593. cur = build_ffn(cur,
  10594. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  10595. NULL, NULL, NULL,
  10596. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  10597. NULL,
  10598. LLM_FFN_GELU, LLM_FFN_SEQ, il);
  10599. cb(cur, "ffn_out", il);
  10600. cur = ggml_add(ctx0, cur, inpL);
  10601. cb(cur, "ffn_out", il);
  10602. cur = ggml_add(ctx0, cur, attn_out);
  10603. cur = build_cvec(cur, il);
  10604. cb(cur, "l_out", il);
  10605. // input for next layer
  10606. inpL = cur;
  10607. } else {
  10608. // attention and ffn are computed sequentially
  10609. // x = x + attn(ln1(x))
  10610. // x = x + ffn(ln2(x))
  10611. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
  10612. cb(ffn_inp, "ffn_inp", il);
  10613. cur = build_norm(ffn_inp,
  10614. model.layers[il].ffn_norm,
  10615. model.layers[il].ffn_norm_b,
  10616. LLM_NORM, il);
  10617. cb(cur, "ffn_norm", il);
  10618. cur = build_ffn(cur,
  10619. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  10620. NULL, NULL, NULL,
  10621. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  10622. NULL,
  10623. LLM_FFN_GELU, LLM_FFN_SEQ, il);
  10624. cb(cur, "ffn_out", il);
  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. }
  10632. cur = build_norm(inpL,
  10633. model.output_norm,
  10634. model.output_norm_b,
  10635. LLM_NORM, -1);
  10636. cb(cur, "result_norm", -1);
  10637. res->t_embd = cur;
  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_arctic : public llm_graph_context {
  10645. llm_build_arctic(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  10646. const int64_t n_embd_head = hparams.n_embd_head_v;
  10647. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  10648. GGML_ASSERT(n_embd_head == hparams.n_rot);
  10649. ggml_tensor * cur;
  10650. ggml_tensor * inpL;
  10651. inpL = build_inp_embd(model.tok_embd);
  10652. // inp_pos - contains the positions
  10653. ggml_tensor * inp_pos = build_inp_pos();
  10654. auto * inp_attn = build_attn_inp_kv();
  10655. ggml_tensor * inp_out_ids = build_inp_out_ids();
  10656. for (int il = 0; il < n_layer; ++il) {
  10657. ggml_tensor * inpSA = inpL;
  10658. // norm
  10659. cur = build_norm(inpL,
  10660. model.layers[il].attn_norm, NULL,
  10661. LLM_NORM_RMS, il);
  10662. cb(cur, "attn_norm", il);
  10663. // self-attention
  10664. {
  10665. // compute Q and K and RoPE them
  10666. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  10667. cb(Qcur, "Qcur", il);
  10668. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  10669. cb(Kcur, "Kcur", il);
  10670. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  10671. cb(Vcur, "Vcur", il);
  10672. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  10673. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  10674. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  10675. Qcur = ggml_rope_ext(
  10676. ctx0, Qcur, inp_pos, nullptr,
  10677. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  10678. ext_factor, attn_factor, beta_fast, beta_slow
  10679. );
  10680. Kcur = ggml_rope_ext(
  10681. ctx0, Kcur, inp_pos, nullptr,
  10682. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  10683. ext_factor, attn_factor, beta_fast, beta_slow
  10684. );
  10685. cb(Qcur, "Qcur", il);
  10686. cb(Kcur, "Kcur", il);
  10687. cb(Vcur, "Vcur", il);
  10688. cur = build_attn(inp_attn,
  10689. model.layers[il].wo, NULL,
  10690. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  10691. }
  10692. if (il == n_layer - 1 && inp_out_ids) {
  10693. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  10694. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  10695. }
  10696. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  10697. cb(ffn_inp, "ffn_inp", il);
  10698. // feed-forward network
  10699. cur = build_norm(ffn_inp,
  10700. model.layers[il].ffn_norm, NULL,
  10701. LLM_NORM_RMS, il);
  10702. cb(cur, "ffn_norm", il);
  10703. cur = build_ffn(cur,
  10704. model.layers[il].ffn_up, NULL, NULL,
  10705. model.layers[il].ffn_gate, NULL, NULL,
  10706. model.layers[il].ffn_down, NULL, NULL,
  10707. NULL,
  10708. LLM_FFN_SILU, LLM_FFN_PAR, il);
  10709. cb(cur, "ffn_out", il);
  10710. ggml_tensor * ffn_out = ggml_add(ctx0, cur, ffn_inp);
  10711. cb(ffn_out, "ffn_out", il);
  10712. // MoE
  10713. cur = build_norm(inpSA,
  10714. model.layers[il].ffn_norm_exps, NULL,
  10715. LLM_NORM_RMS, il);
  10716. cb(cur, "ffn_norm_exps", il);
  10717. cur = build_moe_ffn(cur,
  10718. model.layers[il].ffn_gate_inp,
  10719. model.layers[il].ffn_up_exps,
  10720. model.layers[il].ffn_gate_exps,
  10721. model.layers[il].ffn_down_exps,
  10722. nullptr,
  10723. n_expert, n_expert_used,
  10724. LLM_FFN_SILU, true,
  10725. false, 0.0,
  10726. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  10727. il);
  10728. cb(cur, "ffn_moe_out", il);
  10729. cur = ggml_add(ctx0, cur, ffn_out);
  10730. cb(cur, "ffn_out", il);
  10731. cur = build_cvec(cur, il);
  10732. cb(cur, "l_out", il);
  10733. // input for next layer
  10734. inpL = cur;
  10735. }
  10736. cur = inpL;
  10737. cur = build_norm(cur,
  10738. model.output_norm, NULL,
  10739. LLM_NORM_RMS, -1);
  10740. cb(cur, "result_norm", -1);
  10741. res->t_embd = cur;
  10742. // lm_head
  10743. cur = build_lora_mm(model.output, cur);
  10744. cb(cur, "result_output", -1);
  10745. res->t_logits = cur;
  10746. ggml_build_forward_expand(gf, cur);
  10747. }
  10748. };
  10749. struct llm_build_deepseek : public llm_graph_context {
  10750. llm_build_deepseek(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  10751. const int64_t n_embd_head = hparams.n_embd_head_v;
  10752. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  10753. GGML_ASSERT(n_embd_head == hparams.n_rot);
  10754. ggml_tensor * cur;
  10755. ggml_tensor * inpL;
  10756. inpL = build_inp_embd(model.tok_embd);
  10757. // inp_pos - contains the positions
  10758. ggml_tensor * inp_pos = build_inp_pos();
  10759. auto * inp_attn = build_attn_inp_kv();
  10760. const float kq_scale = hparams.f_attention_scale == 0.0f ? 1.0f/sqrtf(float(n_embd_head)) : hparams.f_attention_scale;
  10761. ggml_tensor * inp_out_ids = build_inp_out_ids();
  10762. for (int il = 0; il < n_layer; ++il) {
  10763. ggml_tensor * inpSA = inpL;
  10764. // norm
  10765. cur = build_norm(inpL,
  10766. model.layers[il].attn_norm, NULL,
  10767. LLM_NORM_RMS, il);
  10768. cb(cur, "attn_norm", il);
  10769. // self-attention
  10770. {
  10771. // rope freq factors for llama3; may return nullptr for llama2 and other models
  10772. ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
  10773. // compute Q and K and RoPE them
  10774. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  10775. cb(Qcur, "Qcur", il);
  10776. if (model.layers[il].bq) {
  10777. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  10778. cb(Qcur, "Qcur", il);
  10779. }
  10780. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  10781. cb(Kcur, "Kcur", il);
  10782. if (model.layers[il].bk) {
  10783. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  10784. cb(Kcur, "Kcur", il);
  10785. }
  10786. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  10787. cb(Vcur, "Vcur", il);
  10788. if (model.layers[il].bv) {
  10789. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  10790. cb(Vcur, "Vcur", il);
  10791. }
  10792. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  10793. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  10794. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  10795. Qcur = ggml_rope_ext(
  10796. ctx0, Qcur, inp_pos, rope_factors,
  10797. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  10798. ext_factor, attn_factor, beta_fast, beta_slow
  10799. );
  10800. Kcur = ggml_rope_ext(
  10801. ctx0, Kcur, inp_pos, rope_factors,
  10802. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  10803. ext_factor, attn_factor, beta_fast, beta_slow
  10804. );
  10805. cb(Qcur, "Qcur", il);
  10806. cb(Kcur, "Kcur", il);
  10807. cb(Vcur, "Vcur", il);
  10808. cur = build_attn(inp_attn,
  10809. model.layers[il].wo, model.layers[il].bo,
  10810. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
  10811. }
  10812. if (il == n_layer - 1 && inp_out_ids) {
  10813. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  10814. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  10815. }
  10816. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  10817. cb(ffn_inp, "ffn_inp", il);
  10818. cur = build_norm(ffn_inp,
  10819. model.layers[il].ffn_norm, NULL,
  10820. LLM_NORM_RMS, il);
  10821. cb(cur, "ffn_norm", il);
  10822. if ((uint32_t) il < hparams.n_layer_dense_lead) {
  10823. cur = build_ffn(cur,
  10824. model.layers[il].ffn_up, NULL, NULL,
  10825. model.layers[il].ffn_gate, NULL, NULL,
  10826. model.layers[il].ffn_down, NULL, NULL,
  10827. NULL,
  10828. LLM_FFN_SILU, LLM_FFN_PAR, il);
  10829. cb(cur, "ffn_out", il);
  10830. } else {
  10831. // MoE branch
  10832. ggml_tensor * moe_out =
  10833. build_moe_ffn(cur,
  10834. model.layers[il].ffn_gate_inp,
  10835. model.layers[il].ffn_up_exps,
  10836. model.layers[il].ffn_gate_exps,
  10837. model.layers[il].ffn_down_exps,
  10838. nullptr,
  10839. n_expert, n_expert_used,
  10840. LLM_FFN_SILU, false,
  10841. false, hparams.expert_weights_scale,
  10842. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  10843. il);
  10844. cb(moe_out, "ffn_moe_out", il);
  10845. // FFN shared expert
  10846. {
  10847. ggml_tensor * ffn_shexp = build_ffn(cur,
  10848. model.layers[il].ffn_up_shexp, NULL, NULL,
  10849. model.layers[il].ffn_gate_shexp, NULL, NULL,
  10850. model.layers[il].ffn_down_shexp, NULL, NULL,
  10851. NULL,
  10852. LLM_FFN_SILU, LLM_FFN_PAR, il);
  10853. cb(ffn_shexp, "ffn_shexp", il);
  10854. cur = ggml_add(ctx0, moe_out, ffn_shexp);
  10855. cb(cur, "ffn_out", il);
  10856. }
  10857. }
  10858. cur = ggml_add(ctx0, cur, ffn_inp);
  10859. cur = build_cvec(cur, il);
  10860. cb(cur, "l_out", il);
  10861. // input for next layer
  10862. inpL = cur;
  10863. }
  10864. cur = inpL;
  10865. cur = build_norm(cur,
  10866. model.output_norm, NULL,
  10867. LLM_NORM_RMS, -1);
  10868. cb(cur, "result_norm", -1);
  10869. res->t_embd = cur;
  10870. // lm_head
  10871. cur = build_lora_mm(model.output, cur);
  10872. cb(cur, "result_output", -1);
  10873. res->t_logits = cur;
  10874. ggml_build_forward_expand(gf, cur);
  10875. }
  10876. };
  10877. struct llm_build_deepseek2 : public llm_graph_context {
  10878. llm_build_deepseek2(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  10879. bool is_lite = (hparams.n_layer == 27);
  10880. const bool is_mla = (hparams.n_embd_head_k_mla != 0 && hparams.n_embd_head_v_mla != 0);
  10881. // note: these are the actual head sizes you get when treating as MHA or after "decompression" using wv_b for MLA
  10882. const int64_t n_embd_head_k = is_mla ? hparams.n_embd_head_k_mla : hparams.n_embd_head_k;
  10883. const int64_t n_embd_head_v = is_mla ? hparams.n_embd_head_v_mla : hparams.n_embd_head_v;
  10884. const int64_t n_embd_head_qk_rope = hparams.n_rot;
  10885. const int64_t n_embd_head_qk_nope = n_embd_head_k - n_embd_head_qk_rope;
  10886. const uint32_t kv_lora_rank = hparams.n_lora_kv;
  10887. // We have to pre-scale kq_scale and attn_factor to make the YaRN RoPE work correctly.
  10888. // See https://github.com/ggerganov/llama.cpp/discussions/7416 for detailed explanation.
  10889. const float mscale = attn_factor * (1.0f + hparams.rope_yarn_log_mul * logf(1.0f / freq_scale));
  10890. const float kq_scale = 1.0f*mscale*mscale/sqrtf(float(n_embd_head_k));
  10891. const float attn_factor = 1.0f / (1.0f + 0.1f * logf(1.0f / freq_scale));
  10892. ggml_tensor * cur;
  10893. ggml_tensor * inpL;
  10894. // {n_embd, n_tokens}
  10895. inpL = build_inp_embd(model.tok_embd);
  10896. // inp_pos - contains the positions
  10897. ggml_tensor * inp_pos = build_inp_pos();
  10898. auto * inp_attn = build_attn_inp_kv();
  10899. ggml_tensor * inp_out_ids = build_inp_out_ids();
  10900. for (int il = 0; il < n_layer; ++il) {
  10901. ggml_tensor * inpSA = inpL;
  10902. // norm
  10903. cur = build_norm(inpL,
  10904. model.layers[il].attn_norm, NULL,
  10905. LLM_NORM_RMS, il);
  10906. cb(cur, "attn_norm", il);
  10907. // self_attention
  10908. {
  10909. ggml_tensor * q = NULL;
  10910. if (!is_lite) {
  10911. q = ggml_mul_mat(ctx0, model.layers[il].wq_a, cur);
  10912. cb(q, "q", il);
  10913. q = build_norm(q,
  10914. model.layers[il].attn_q_a_norm, nullptr,
  10915. LLM_NORM_RMS, il);
  10916. cb(q, "q", il);
  10917. q = ggml_mul_mat(ctx0, model.layers[il].wq_b, q);
  10918. cb(q, "q", il);
  10919. } else {
  10920. q = ggml_mul_mat(ctx0, model.layers[il].wq, cur);
  10921. cb(q, "q", il);
  10922. }
  10923. // split into {n_embd_head_qk_nope, n_head, n_tokens}
  10924. ggml_tensor * q_nope = ggml_view_3d(ctx0, q,
  10925. n_embd_head_qk_nope, n_head, n_tokens,
  10926. ggml_row_size(q->type, n_embd_head_k),
  10927. ggml_row_size(q->type, n_embd_head_k) * n_head,
  10928. 0);
  10929. cb(q_nope, "q_nope", il);
  10930. // and {n_embd_head_qk_rope, n_head, n_tokens}
  10931. ggml_tensor * q_pe = ggml_view_3d(ctx0, q,
  10932. n_embd_head_qk_rope, n_head, n_tokens,
  10933. ggml_row_size(q->type, n_embd_head_k),
  10934. ggml_row_size(q->type, n_embd_head_k) * n_head,
  10935. ggml_row_size(q->type, n_embd_head_qk_nope));
  10936. cb(q_pe, "q_pe", il);
  10937. ggml_tensor * kv_cmpr_pe = ggml_mul_mat(ctx0, model.layers[il].wkv_a_mqa, cur);
  10938. cb(kv_cmpr_pe, "kv_cmpr_pe", il);
  10939. // split into {kv_lora_rank, n_tokens}
  10940. ggml_tensor * kv_cmpr = ggml_view_2d(ctx0, kv_cmpr_pe,
  10941. kv_lora_rank, n_tokens,
  10942. ggml_row_size(kv_cmpr_pe->type, kv_lora_rank + n_embd_head_qk_rope),
  10943. 0);
  10944. cb(kv_cmpr, "kv_cmpr", il);
  10945. // and {n_embd_head_qk_rope, 1, n_tokens}
  10946. ggml_tensor * k_pe = ggml_view_3d(ctx0, kv_cmpr_pe,
  10947. n_embd_head_qk_rope, 1, n_tokens,
  10948. ggml_row_size(kv_cmpr_pe->type, kv_lora_rank + n_embd_head_qk_rope),
  10949. ggml_row_size(kv_cmpr_pe->type, kv_lora_rank + n_embd_head_qk_rope),
  10950. ggml_row_size(kv_cmpr_pe->type, kv_lora_rank));
  10951. cb(k_pe, "k_pe", il);
  10952. q_pe = ggml_rope_ext(ctx0, q_pe, inp_pos, nullptr,
  10953. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  10954. ext_factor, attn_factor, beta_fast, beta_slow
  10955. );
  10956. cb(q_pe, "q_pe", il);
  10957. k_pe = ggml_rope_ext(ctx0, k_pe, inp_pos, nullptr,
  10958. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  10959. ext_factor, attn_factor, beta_fast, beta_slow
  10960. );
  10961. cb(k_pe, "k_pe", il);
  10962. kv_cmpr = build_norm(kv_cmpr,
  10963. model.layers[il].attn_kv_a_norm, nullptr,
  10964. LLM_NORM_RMS, il);
  10965. cb(kv_cmpr, "kv_cmpr", il);
  10966. if (is_mla) {
  10967. // {n_embd_head_qk_nope, n_tokens, n_head}
  10968. q_nope = ggml_permute(ctx0, q_nope, 0, 2, 1, 3);
  10969. cb(q_nope, "q_nope_perm", il);
  10970. // {n_embd_head_qk_nope, kv_lora_rank, n_head} x {n_embd_head_qk_nope, n_tokens, n_head}
  10971. ggml_tensor * q_nope_absorbed = ggml_mul_mat(ctx0, model.layers[il].wk_b, q_nope);
  10972. cb(q_nope_absorbed, "q_nope_absorbed", il);
  10973. // {kv_lora_rank, n_head, n_tokens}
  10974. q_nope_absorbed = ggml_permute(ctx0, q_nope_absorbed, 0, 2, 1, 3);
  10975. cb(q_nope_absorbed, "q_nope_absorbed_perm", il);
  10976. // {n_embd_head_qk_rope + kv_lora_rank, n_head, n_tokens}
  10977. // note: rope must go first for in-place context shifting in build_rope_shift()
  10978. ggml_tensor * Qcur = ggml_concat(ctx0, q_pe, q_nope_absorbed, 0);
  10979. cb(Qcur, "Qcur", il);
  10980. kv_cmpr = ggml_reshape_3d(ctx0, kv_cmpr, kv_lora_rank, 1, n_tokens);
  10981. cb(kv_cmpr, "kv_cmpr_reshape", il);
  10982. // {n_embd_head_qk_rope + kv_lora_rank, 1, n_tokens}
  10983. ggml_tensor * Kcur = ggml_concat(ctx0, k_pe, kv_cmpr, 0);
  10984. cb(Kcur, "Kcur", il);
  10985. // {kv_lora_rank, 1, n_tokens}
  10986. ggml_tensor * Vcur = kv_cmpr;
  10987. cb(Vcur, "Vcur", il);
  10988. // note: MLA with the absorption optimzation converts into MQA (ie: GQA with 1 group)
  10989. cur = build_attn(inp_attn,
  10990. model.layers[il].wo, NULL,
  10991. Qcur, Kcur, Vcur, nullptr, nullptr, model.layers[il].wv_b, kq_scale, il);
  10992. } else {
  10993. ggml_tensor * kv = ggml_mul_mat(ctx0, model.layers[il].wkv_b, kv_cmpr);
  10994. cb(kv, "kv", il);
  10995. // split into {n_embd_head_qk_nope, n_head, n_tokens}
  10996. ggml_tensor * k_nope = ggml_view_3d(ctx0, kv,
  10997. n_embd_head_qk_nope, n_head, n_tokens,
  10998. ggml_row_size(kv->type, n_embd_head_qk_nope + n_embd_head_v),
  10999. ggml_row_size(kv->type, n_embd_head_qk_nope + n_embd_head_v) * n_head,
  11000. 0);
  11001. cb(k_nope, "k_nope_view", il);
  11002. // and {n_embd_head_v, n_head, n_tokens}
  11003. ggml_tensor * Vcur = ggml_view_3d(ctx0, kv,
  11004. n_embd_head_v, n_head, n_tokens,
  11005. ggml_row_size(kv->type, n_embd_head_qk_nope + n_embd_head_v),
  11006. ggml_row_size(kv->type, n_embd_head_qk_nope + n_embd_head_v) * n_head,
  11007. ggml_row_size(kv->type, n_embd_head_qk_nope));
  11008. cb(Vcur, "Vcur_view", il);
  11009. Vcur = ggml_cont(ctx0, Vcur);
  11010. cb(Vcur, "Vcur_cont", il);
  11011. // note: rope must go first for in-place context shifting in build_rope_shift()
  11012. ggml_tensor * Qcur = ggml_concat(ctx0, q_pe, q_nope, 0);
  11013. cb(Qcur, "Qcur", il);
  11014. ggml_tensor * Kcur = ggml_concat(ctx0, ggml_repeat(ctx0, k_pe, q_pe), k_nope, 0);
  11015. cb(Kcur, "Kcur", il);
  11016. // note: MLA without the absorption optimization converts into MHA (ie: GQA with full n_head groups)
  11017. cur = build_attn(inp_attn,
  11018. model.layers[il].wo, NULL,
  11019. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
  11020. }
  11021. }
  11022. if (il == n_layer - 1 && inp_out_ids) {
  11023. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  11024. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  11025. }
  11026. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  11027. cb(ffn_inp, "ffn_inp", il);
  11028. cur = build_norm(ffn_inp,
  11029. model.layers[il].ffn_norm, NULL,
  11030. LLM_NORM_RMS, il);
  11031. cb(cur, "ffn_norm", il);
  11032. if ((uint32_t) il < hparams.n_layer_dense_lead) {
  11033. cur = build_ffn(cur,
  11034. model.layers[il].ffn_up, NULL, NULL,
  11035. model.layers[il].ffn_gate, NULL, NULL,
  11036. model.layers[il].ffn_down, NULL, NULL,
  11037. NULL,
  11038. LLM_FFN_SILU, LLM_FFN_PAR, il);
  11039. cb(cur, "ffn_out", il);
  11040. } else {
  11041. // MoE branch
  11042. ggml_tensor * moe_out =
  11043. build_moe_ffn(cur,
  11044. model.layers[il].ffn_gate_inp,
  11045. model.layers[il].ffn_up_exps,
  11046. model.layers[il].ffn_gate_exps,
  11047. model.layers[il].ffn_down_exps,
  11048. model.layers[il].ffn_exp_probs_b,
  11049. n_expert, n_expert_used,
  11050. LLM_FFN_SILU, hparams.expert_weights_norm,
  11051. true, hparams.expert_weights_scale,
  11052. (llama_expert_gating_func_type) hparams.expert_gating_func,
  11053. il);
  11054. cb(moe_out, "ffn_moe_out", il);
  11055. // FFN shared expert
  11056. {
  11057. ggml_tensor * ffn_shexp = build_ffn(cur,
  11058. model.layers[il].ffn_up_shexp, NULL, NULL,
  11059. model.layers[il].ffn_gate_shexp, NULL, NULL,
  11060. model.layers[il].ffn_down_shexp, NULL, NULL,
  11061. NULL,
  11062. LLM_FFN_SILU, LLM_FFN_PAR, il);
  11063. cb(ffn_shexp, "ffn_shexp", il);
  11064. cur = ggml_add(ctx0, moe_out, ffn_shexp);
  11065. cb(cur, "ffn_out", il);
  11066. }
  11067. }
  11068. cur = ggml_add(ctx0, cur, ffn_inp);
  11069. cur = build_cvec(cur, il);
  11070. cb(cur, "l_out", il);
  11071. // input for next layer
  11072. inpL = cur;
  11073. }
  11074. cur = inpL;
  11075. cur = build_norm(cur,
  11076. model.output_norm, NULL,
  11077. LLM_NORM_RMS, -1);
  11078. cb(cur, "result_norm", -1);
  11079. res->t_embd = cur;
  11080. // lm_head
  11081. cur = ggml_mul_mat(ctx0, model.output, cur);
  11082. cb(cur, "result_output", -1);
  11083. res->t_logits = cur;
  11084. ggml_build_forward_expand(gf, cur);
  11085. }
  11086. };
  11087. struct llm_build_bitnet : public llm_graph_context {
  11088. llm_build_bitnet(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  11089. const int64_t n_embd_head = hparams.n_embd_head_v;
  11090. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  11091. ggml_tensor * cur;
  11092. ggml_tensor * inpL;
  11093. inpL = build_inp_embd(model.tok_embd);
  11094. // inp_pos - contains the positions
  11095. ggml_tensor * inp_pos = build_inp_pos();
  11096. auto * inp_attn = build_attn_inp_kv();
  11097. ggml_tensor * inp_out_ids = build_inp_out_ids();
  11098. for (int il = 0; il < n_layer; ++il) {
  11099. ggml_tensor * inpSA = inpL;
  11100. cur = build_norm(inpL,
  11101. model.layers[il].attn_norm, NULL,
  11102. LLM_NORM_RMS, il);
  11103. cb(cur, "attn_norm", il);
  11104. // self-attention
  11105. {
  11106. // compute Q and K and RoPE them
  11107. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  11108. if (model.layers[il].wq_scale) {
  11109. Qcur = ggml_mul(ctx0, Qcur, model.layers[il].wq_scale);
  11110. }
  11111. cb(Qcur, "Qcur", il);
  11112. if (model.layers[il].bq) {
  11113. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  11114. cb(Qcur, "Qcur", il);
  11115. }
  11116. // B1.K
  11117. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  11118. if (model.layers[il].wk_scale) {
  11119. Kcur = ggml_mul(ctx0, Kcur, model.layers[il].wk_scale);
  11120. }
  11121. cb(Kcur, "Kcur", il);
  11122. if (model.layers[il].bk) {
  11123. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  11124. cb(Kcur, "Kcur", il);
  11125. }
  11126. // B1.V
  11127. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  11128. if (model.layers[il].wv_scale) {
  11129. Vcur = ggml_mul(ctx0, Vcur, model.layers[il].wv_scale);
  11130. }
  11131. cb(Vcur, "Vcur", il);
  11132. if (model.layers[il].bv) {
  11133. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  11134. cb(Vcur, "Vcur", il);
  11135. }
  11136. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  11137. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  11138. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  11139. Qcur = ggml_rope_ext(
  11140. ctx0, Qcur, inp_pos, nullptr,
  11141. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  11142. ext_factor, attn_factor, beta_fast, beta_slow
  11143. );
  11144. Kcur = ggml_rope_ext(
  11145. ctx0, Kcur, inp_pos, nullptr,
  11146. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  11147. ext_factor, attn_factor, beta_fast, beta_slow
  11148. );
  11149. cb(Qcur, "Qcur", il);
  11150. cb(Kcur, "Kcur", il);
  11151. cb(Vcur, "Vcur", il);
  11152. cur = build_attn(inp_attn,
  11153. NULL, NULL,
  11154. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  11155. cur = build_norm(cur,
  11156. model.layers[il].attn_sub_norm, NULL,
  11157. LLM_NORM_RMS, il);
  11158. cb(cur, "attn_sub_norm", il);
  11159. cur = build_lora_mm(model.layers[il].wo, cur);
  11160. if (model.layers[il].wo_scale) {
  11161. cur = ggml_mul(ctx0, cur, model.layers[il].wo_scale);
  11162. }
  11163. if (model.layers[il].bo) {
  11164. cur = ggml_add(ctx0, cur, model.layers[il].bo);
  11165. }
  11166. cb(cur, "attn_o_out", il);
  11167. }
  11168. if (il == n_layer - 1 && inp_out_ids) {
  11169. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  11170. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  11171. }
  11172. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  11173. cb(ffn_inp, "ffn_inp", il);
  11174. // feed-forward forward
  11175. cur = build_norm(ffn_inp,
  11176. model.layers[il].ffn_norm, NULL,
  11177. LLM_NORM_RMS, il);
  11178. cb(cur, "ffn_norm", il);
  11179. cur = build_ffn(cur,
  11180. model.layers[il].ffn_up, NULL, model.layers[il].ffn_up_scale,
  11181. model.layers[il].ffn_gate, NULL, model.layers[il].ffn_gate_scale,
  11182. NULL, NULL, NULL,
  11183. NULL,
  11184. LLM_FFN_SILU, LLM_FFN_PAR, il);
  11185. cb(cur, "ffn_sub_out", il);
  11186. cur = build_norm(cur,
  11187. model.layers[il].ffn_sub_norm, NULL,
  11188. LLM_NORM_RMS, il);
  11189. cb(cur, "ffn_sub_norm", il);
  11190. cur = build_lora_mm(model.layers[il].ffn_down, cur);
  11191. if (model.layers[il].ffn_down_scale) {
  11192. cur = ggml_mul(ctx0, cur, model.layers[il].ffn_down_scale);
  11193. }
  11194. cb(cur, "ffn_down", il);
  11195. cur = ggml_add(ctx0, cur, ffn_inp);
  11196. cb(cur, "l_out", il);
  11197. // input for next layer
  11198. inpL = cur;
  11199. }
  11200. cur = inpL;
  11201. cur = build_norm(cur,
  11202. model.output_norm, NULL,
  11203. LLM_NORM_RMS, -1);
  11204. cb(cur, "result_norm", -1);
  11205. res->t_embd = cur;
  11206. // lm_head
  11207. // FIXME: do not use model.tok_embd directly, duplicate as model.output
  11208. cur = build_lora_mm(model.tok_embd, cur);
  11209. cb(cur, "result_output", -1);
  11210. res->t_logits = cur;
  11211. ggml_build_forward_expand(gf, cur);
  11212. }
  11213. };
  11214. struct llm_build_t5_enc : public llm_graph_context {
  11215. llm_build_t5_enc(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  11216. const int64_t n_embd_head = hparams.n_embd_head_v;
  11217. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  11218. ggml_tensor * cur;
  11219. ggml_tensor * inpL;
  11220. inpL = build_inp_embd(model.tok_embd);
  11221. ggml_tensor * pos_bucket_enc = build_inp_pos_bucket_enc();
  11222. auto * inp_attn = build_attn_inp_no_cache();
  11223. ggml_tensor * inp_out_ids = build_inp_out_ids();
  11224. for (int il = 0; il < n_layer; ++il) {
  11225. ggml_tensor * inpSA = inpL;
  11226. // norm
  11227. cur = build_norm(inpL,
  11228. model.layers[il].attn_norm_enc, NULL,
  11229. LLM_NORM_RMS, il);
  11230. cb(cur, "attn_norm", il);
  11231. // self-attention
  11232. {
  11233. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq_enc, cur);
  11234. cb(Qcur, "Qcur", il);
  11235. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk_enc, cur);
  11236. cb(Kcur, "Kcur", il);
  11237. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv_enc, cur);
  11238. cb(Vcur, "Vcur", il);
  11239. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  11240. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  11241. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  11242. 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;
  11243. ggml_tensor * kq_b = build_pos_bias(pos_bucket_enc, attn_rel_b);
  11244. cur = build_attn(inp_attn,
  11245. model.layers[il].wo_enc, nullptr,
  11246. Qcur, Kcur, Vcur, kq_b, nullptr, nullptr, 1.0f, il);
  11247. cb(cur, "kqv_out", il);
  11248. }
  11249. if (il == n_layer - 1 && inp_out_ids) {
  11250. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  11251. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  11252. }
  11253. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  11254. cb(ffn_inp, "ffn_inp", il);
  11255. // feed-forward network
  11256. {
  11257. cur = build_norm(ffn_inp,
  11258. model.layers[il].ffn_norm_enc, NULL,
  11259. LLM_NORM_RMS, il);
  11260. cb(cur, "ffn_norm", il);
  11261. // T5 uses relu, flan-T5 uses gelu-gated
  11262. cur = build_ffn(cur,
  11263. model.layers[il].ffn_up_enc, NULL, NULL,
  11264. model.layers[il].ffn_gate_enc, NULL, NULL,
  11265. model.layers[il].ffn_down_enc, NULL, NULL,
  11266. NULL,
  11267. model.layers[il].ffn_gate_enc ? LLM_FFN_GELU : LLM_FFN_RELU,
  11268. model.layers[il].ffn_gate_enc ? LLM_FFN_PAR : LLM_FFN_SEQ,
  11269. il);
  11270. cb(cur, "ffn_out", il);
  11271. }
  11272. cur = ggml_add(ctx0, cur, ffn_inp);
  11273. cb(cur, "ffn_out", il);
  11274. cur = build_cvec(cur, il);
  11275. cb(cur, "l_out", il);
  11276. // input for next layer
  11277. inpL = cur;
  11278. }
  11279. cur = inpL;
  11280. cb(cur, "result_embd", -1);
  11281. cur = build_norm(cur,
  11282. model.output_norm_enc, NULL,
  11283. LLM_NORM_RMS, -1);
  11284. cb(cur, "result_norm", -1);
  11285. res->t_embd = cur;
  11286. ggml_build_forward_expand(gf, cur);
  11287. }
  11288. };
  11289. struct llm_build_t5_dec : public llm_graph_context {
  11290. llm_build_t5_dec(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  11291. const int64_t n_embd_head = hparams.n_embd_head_v;
  11292. //const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  11293. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  11294. ggml_tensor * cur;
  11295. ggml_tensor * inpL;
  11296. inpL = build_inp_embd(model.tok_embd);
  11297. ggml_tensor * embd_enc = build_inp_cross_embd();
  11298. ggml_tensor * pos_bucket_dec = build_inp_pos_bucket_dec();
  11299. const int64_t n_outputs_enc = embd_enc->ne[1];
  11300. auto * inp_attn_self = build_attn_inp_kv();
  11301. auto * inp_attn_cross = build_attn_inp_cross();
  11302. ggml_tensor * inp_out_ids = build_inp_out_ids();
  11303. const int64_t dec_n_layer = hparams.dec_n_layer;
  11304. for (int il = 0; il < dec_n_layer; ++il) {
  11305. ggml_tensor * inpSA = inpL;
  11306. // norm
  11307. cur = build_norm(inpL,
  11308. model.layers[il].attn_norm, NULL,
  11309. LLM_NORM_RMS, il);
  11310. cb(cur, "attn_norm", il);
  11311. // self-attention
  11312. {
  11313. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  11314. cb(Qcur, "Qcur", il);
  11315. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  11316. cb(Kcur, "Kcur", il);
  11317. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  11318. cb(Vcur, "Vcur", il);
  11319. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  11320. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  11321. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  11322. ggml_tensor * attn_rel_b = model.layers[il].attn_rel_b ? model.layers[il].attn_rel_b : model.layers[0].attn_rel_b;
  11323. ggml_tensor * kq_b = build_pos_bias(pos_bucket_dec, attn_rel_b);
  11324. cur = build_attn(inp_attn_self,
  11325. model.layers[il].wo, model.layers[il].bo,
  11326. Qcur, Kcur, Vcur, kq_b, nullptr, nullptr, 1.0f, il);
  11327. cb(cur, "kqv_out", il);
  11328. }
  11329. cur = ggml_add(ctx0, cur, inpSA);
  11330. cb(cur, "cross_inp", il);
  11331. ggml_tensor * inpCA = cur;
  11332. // norm
  11333. cur = build_norm(cur,
  11334. model.layers[il].attn_norm_cross, NULL,
  11335. LLM_NORM_RMS, il);
  11336. cb(cur, "attn_norm_cross", il);
  11337. // cross-attention
  11338. {
  11339. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq_cross, cur);
  11340. cb(Qcur, "Qcur", il);
  11341. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk_cross, embd_enc);
  11342. cb(Kcur, "Kcur", il);
  11343. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv_cross, embd_enc);
  11344. cb(Vcur, "Vcur", il);
  11345. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  11346. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_outputs_enc);
  11347. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_outputs_enc);
  11348. cur = build_attn(inp_attn_cross,
  11349. model.layers[il].wo_cross, nullptr,
  11350. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f, il);
  11351. cb(cur, "kqv_out", il);
  11352. //ggml_tensor * q = ggml_permute(ctx0, Qcur, 0, 2, 1, 3);
  11353. //ggml_tensor * k = ggml_cont(ctx0, ggml_permute(ctx0, Kcur, 0, 2, 1, 3));
  11354. //ggml_tensor * kq = ggml_mul_mat(ctx0, k, q);
  11355. //cb(kq, "kq", il);
  11356. //kq = ggml_soft_max_ext(ctx0, kq, KQ_mask_cross, 1.0f, hparams.f_max_alibi_bias);
  11357. //cb(kq, "kq_soft_max_ext", il);
  11358. //ggml_tensor * v = ggml_cont(ctx0, ggml_transpose(ctx0, ggml_reshape_2d(ctx0, Vcur, n_embd_gqa, n_outputs_enc)));
  11359. //cb(v, "v", il);
  11360. //ggml_tensor * kqv = ggml_mul_mat(ctx0, ggml_reshape_3d(ctx0, v, n_outputs_enc, n_embd_head, n_head_kv), kq);
  11361. //cb(kqv, "kqv", il);
  11362. //ggml_tensor * kqv_merged = ggml_permute(ctx0, kqv, 0, 2, 1, 3);
  11363. //cb(kqv_merged, "kqv_merged", il);
  11364. //cur = ggml_cont_2d(ctx0, kqv_merged, n_embd_gqa, n_tokens);
  11365. //cb(cur, "kqv_merged_cont", il);
  11366. //ggml_build_forward_expand(gf, cur);
  11367. //cur = build_lora_mm(model.layers[il].wo_cross, cur);
  11368. //cb(cur, "kqv_out", il);
  11369. }
  11370. if (il == dec_n_layer - 1 && inp_out_ids) {
  11371. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  11372. inpCA = ggml_get_rows(ctx0, inpCA, inp_out_ids);
  11373. }
  11374. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpCA);
  11375. cb(ffn_inp, "ffn_inp", il);
  11376. // feed-forward network
  11377. {
  11378. cur = build_norm(ffn_inp,
  11379. model.layers[il].ffn_norm, NULL,
  11380. LLM_NORM_RMS, il);
  11381. cb(cur, "ffn_norm", il);
  11382. // T5 uses relu, flan-T5 uses gelu-gated
  11383. cur = build_ffn(cur,
  11384. model.layers[il].ffn_up, NULL, NULL,
  11385. model.layers[il].ffn_gate, NULL, NULL,
  11386. model.layers[il].ffn_down, NULL, NULL,
  11387. NULL,
  11388. model.layers[il].ffn_gate ? LLM_FFN_GELU : LLM_FFN_RELU,
  11389. model.layers[il].ffn_gate ? LLM_FFN_PAR : LLM_FFN_SEQ,
  11390. il);
  11391. cb(cur, "ffn_out", il);
  11392. }
  11393. cur = ggml_add(ctx0, cur, ffn_inp);
  11394. cb(cur, "ffn_out", il);
  11395. cur = build_cvec(cur, il);
  11396. cb(cur, "l_out", il);
  11397. // input for next layer
  11398. inpL = cur;
  11399. }
  11400. cur = inpL;
  11401. cb(cur, "result_embd", -1);
  11402. cur = build_norm(cur,
  11403. model.output_norm, NULL,
  11404. LLM_NORM_RMS, -1);
  11405. cb(cur, "result_norm", -1);
  11406. res->t_embd = cur;
  11407. // lm_head
  11408. cur = build_lora_mm(model.output, cur);
  11409. cb(cur, "result_output", -1);
  11410. res->t_logits = cur;
  11411. ggml_build_forward_expand(gf, cur);
  11412. }
  11413. };
  11414. struct llm_build_jais : public llm_graph_context {
  11415. llm_build_jais(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  11416. const int64_t n_embd_head = hparams.n_embd_head_v;
  11417. const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  11418. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  11419. ggml_tensor * cur;
  11420. ggml_tensor * inpL;
  11421. inpL = build_inp_embd(model.tok_embd);
  11422. auto * inp_attn = build_attn_inp_kv();
  11423. ggml_tensor * inp_out_ids = build_inp_out_ids();
  11424. for (int il = 0; il < n_layer; ++il) {
  11425. cur = build_norm(inpL,
  11426. model.layers[il].attn_norm,
  11427. model.layers[il].attn_norm_b,
  11428. LLM_NORM, il);
  11429. cb(cur, "attn_norm", il);
  11430. // self-attention
  11431. {
  11432. cur = build_lora_mm(model.layers[il].wqkv, cur);
  11433. cb(cur, "wqkv", il);
  11434. cur = ggml_add(ctx0, cur, model.layers[il].bqkv);
  11435. cb(cur, "bqkv", il);
  11436. 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));
  11437. 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));
  11438. 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));
  11439. cb(Qcur, "Qcur", il);
  11440. cb(Kcur, "Kcur", il);
  11441. cb(Vcur, "Vcur", il);
  11442. cur = build_attn(inp_attn,
  11443. model.layers[il].wo, model.layers[il].bo,
  11444. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/float(n_embd_head), il);
  11445. }
  11446. if (il == n_layer - 1 && inp_out_ids) {
  11447. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  11448. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  11449. }
  11450. // add the input
  11451. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
  11452. cb(ffn_inp, "ffn_inp", il);
  11453. // FF
  11454. {
  11455. cur = build_norm(ffn_inp,
  11456. model.layers[il].ffn_norm,
  11457. model.layers[il].ffn_norm_b,
  11458. LLM_NORM, il);
  11459. cb(cur, "ffn_norm", il);
  11460. cur = build_ffn(cur,
  11461. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  11462. model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, NULL,
  11463. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  11464. NULL,
  11465. LLM_FFN_SILU, LLM_FFN_PAR, il);
  11466. cb(cur, "ffn_out", il);
  11467. }
  11468. inpL = ggml_add(ctx0, cur, ffn_inp);
  11469. cb(inpL, "l_out", il);
  11470. }
  11471. cur = build_norm(inpL,
  11472. model.output_norm,
  11473. model.output_norm_b,
  11474. LLM_NORM, -1);
  11475. cb(cur, "result_norm", -1);
  11476. res->t_embd = cur;
  11477. cur = build_lora_mm(model.output, cur);
  11478. cb(cur, "result_output", -1);
  11479. res->t_logits = cur;
  11480. ggml_build_forward_expand(gf, cur);
  11481. }
  11482. };
  11483. struct llm_build_chatglm : public llm_graph_context {
  11484. llm_build_chatglm(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  11485. const int64_t n_embd_head = hparams.n_embd_head_v;
  11486. const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  11487. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  11488. ggml_tensor * cur;
  11489. ggml_tensor * inpL;
  11490. inpL = build_inp_embd(model.tok_embd);
  11491. // inp_pos - contains the positions
  11492. ggml_tensor * inp_pos = build_inp_pos();
  11493. auto * inp_attn = build_attn_inp_kv();
  11494. ggml_tensor * inp_out_ids = build_inp_out_ids();
  11495. for (int il = 0; il < n_layer; ++il) {
  11496. ggml_tensor * inpSA = inpL;
  11497. cur = build_norm(inpL,
  11498. model.layers[il].attn_norm,
  11499. NULL,
  11500. LLM_NORM_RMS, il);
  11501. cb(cur, "attn_norm", il);
  11502. // self-attention
  11503. {
  11504. ggml_tensor * Qcur = nullptr;
  11505. ggml_tensor * Kcur = nullptr;
  11506. ggml_tensor * Vcur = nullptr;
  11507. if (model.layers[il].wqkv == nullptr) {
  11508. Qcur = build_lora_mm(model.layers[il].wq, cur);
  11509. if (model.layers[il].bq) {
  11510. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  11511. }
  11512. Kcur = build_lora_mm(model.layers[il].wk, cur);
  11513. if (model.layers[il].bk) {
  11514. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  11515. }
  11516. Vcur = build_lora_mm(model.layers[il].wv, cur);
  11517. if (model.layers[il].bv) {
  11518. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  11519. }
  11520. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  11521. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  11522. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  11523. } else {
  11524. cur = build_lora_mm(model.layers[il].wqkv, cur);
  11525. cb(cur, "wqkv", il);
  11526. if (model.layers[il].bqkv) {
  11527. cur = ggml_add(ctx0, cur, model.layers[il].bqkv);
  11528. cb(cur, "bqkv", il);
  11529. }
  11530. 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));
  11531. 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));
  11532. 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));
  11533. }
  11534. //printf("freq_base: %f freq_scale: %f ext_factor: %f attn_factor: %f\n", freq_base, freq_scale, ext_factor, attn_factor);
  11535. Qcur = ggml_rope_ext(
  11536. ctx0, Qcur, inp_pos, nullptr,
  11537. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  11538. ext_factor, attn_factor, beta_fast, beta_slow
  11539. );
  11540. Kcur = ggml_rope_ext(
  11541. ctx0, Kcur, inp_pos, nullptr,
  11542. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  11543. ext_factor, attn_factor, beta_fast, beta_slow
  11544. );
  11545. cb(Qcur, "Qcur", il);
  11546. cb(Kcur, "Kcur", il);
  11547. cb(Vcur, "Vcur", il);
  11548. cur = build_attn(inp_attn,
  11549. model.layers[il].wo, NULL,
  11550. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  11551. }
  11552. if (il == n_layer - 1 && inp_out_ids) {
  11553. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  11554. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  11555. }
  11556. // Add the input
  11557. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  11558. cb(ffn_inp, "ffn_inp", il);
  11559. // FF
  11560. {
  11561. cur = build_norm(ffn_inp,
  11562. model.layers[il].ffn_norm,
  11563. NULL,
  11564. LLM_NORM_RMS, il);
  11565. cb(cur, "ffn_norm", il);
  11566. cur = build_ffn(cur,
  11567. model.layers[il].ffn_up, NULL, NULL,
  11568. NULL, NULL, NULL,
  11569. model.layers[il].ffn_down, NULL, NULL,
  11570. NULL,
  11571. LLM_FFN_SWIGLU, LLM_FFN_SEQ, il);
  11572. cb(cur, "ffn_out", il);
  11573. }
  11574. inpL = ggml_add(ctx0, cur, ffn_inp);
  11575. cb(inpL, "l_out", il);
  11576. }
  11577. cur = build_norm(inpL,
  11578. model.output_norm,
  11579. NULL,
  11580. LLM_NORM_RMS, -1);
  11581. cb(cur, "result_norm", -1);
  11582. res->t_embd = cur;
  11583. cur = build_lora_mm(model.output, cur);
  11584. cb(cur, "result_output", -1);
  11585. res->t_logits = cur;
  11586. ggml_build_forward_expand(gf, cur);
  11587. }
  11588. };
  11589. struct llm_build_glm4 : public llm_graph_context {
  11590. llm_build_glm4(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  11591. const int64_t n_embd_head = hparams.n_embd_head_v;
  11592. const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  11593. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  11594. ggml_tensor * cur;
  11595. ggml_tensor * inpL;
  11596. inpL = build_inp_embd(model.tok_embd);
  11597. // inp_pos - contains the positions
  11598. ggml_tensor * inp_pos = build_inp_pos();
  11599. auto * inp_attn = build_attn_inp_kv();
  11600. ggml_tensor * inp_out_ids = build_inp_out_ids();
  11601. for (int il = 0; il < n_layer; ++il) {
  11602. ggml_tensor * inpSA = inpL;
  11603. // Pre-attention norm
  11604. cur = build_norm(inpL,
  11605. model.layers[il].attn_norm,
  11606. NULL,
  11607. LLM_NORM_RMS, il);
  11608. cb(cur, "attn_norm", il);
  11609. // self-attention
  11610. {
  11611. ggml_tensor * Qcur = nullptr;
  11612. ggml_tensor * Kcur = nullptr;
  11613. ggml_tensor * Vcur = nullptr;
  11614. if (model.layers[il].wqkv == nullptr) {
  11615. Qcur = build_lora_mm(model.layers[il].wq, cur);
  11616. if (model.layers[il].bq) {
  11617. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  11618. }
  11619. Kcur = build_lora_mm(model.layers[il].wk, cur);
  11620. if (model.layers[il].bk) {
  11621. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  11622. }
  11623. Vcur = build_lora_mm(model.layers[il].wv, cur);
  11624. if (model.layers[il].bv) {
  11625. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  11626. }
  11627. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  11628. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  11629. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  11630. } else {
  11631. cur = build_lora_mm(model.layers[il].wqkv, cur);
  11632. cb(cur, "wqkv", il);
  11633. if (model.layers[il].bqkv) {
  11634. cur = ggml_add(ctx0, cur, model.layers[il].bqkv);
  11635. cb(cur, "bqkv", il);
  11636. }
  11637. 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));
  11638. 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));
  11639. 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));
  11640. }
  11641. Qcur = ggml_rope_ext(
  11642. ctx0, Qcur, inp_pos, nullptr,
  11643. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  11644. ext_factor, attn_factor, beta_fast, beta_slow
  11645. );
  11646. Kcur = ggml_rope_ext(
  11647. ctx0, Kcur, inp_pos, nullptr,
  11648. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  11649. ext_factor, attn_factor, beta_fast, beta_slow
  11650. );
  11651. cb(Qcur, "Qcur", il);
  11652. cb(Kcur, "Kcur", il);
  11653. cb(Vcur, "Vcur", il);
  11654. cur = build_attn(inp_attn,
  11655. model.layers[il].wo, NULL,
  11656. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  11657. }
  11658. if (il == n_layer - 1 && inp_out_ids) {
  11659. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  11660. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  11661. }
  11662. // Post-attention norm (new!)
  11663. cur = build_norm(cur,
  11664. model.layers[il].attn_post_norm,
  11665. NULL,
  11666. LLM_NORM_RMS, il);
  11667. cb(cur, "post_attn_norm", il);
  11668. // Add the input (residual connection after post-attention norm)
  11669. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  11670. cb(ffn_inp, "ffn_inp", il);
  11671. // FF
  11672. {
  11673. // Pre-MLP norm
  11674. cur = build_norm(ffn_inp,
  11675. model.layers[il].ffn_norm,
  11676. NULL,
  11677. LLM_NORM_RMS, il);
  11678. cb(cur, "ffn_norm", il);
  11679. // MLP
  11680. cur = build_ffn(cur,
  11681. model.layers[il].ffn_up, NULL, NULL,
  11682. NULL, NULL, NULL,
  11683. model.layers[il].ffn_down, NULL, NULL,
  11684. NULL,
  11685. LLM_FFN_SWIGLU, LLM_FFN_SEQ, il);
  11686. cb(cur, "ffn_out", il);
  11687. // Post-MLP norm
  11688. cur = build_norm(cur,
  11689. model.layers[il].ffn_post_norm,
  11690. NULL,
  11691. LLM_NORM_RMS, il);
  11692. cb(cur, "post_mlp_norm", il);
  11693. }
  11694. // Add residual connection after post-MLP norm
  11695. inpL = ggml_add(ctx0, cur, ffn_inp);
  11696. cb(inpL, "l_out", il);
  11697. }
  11698. // Final norm
  11699. cur = build_norm(inpL,
  11700. model.output_norm,
  11701. NULL,
  11702. LLM_NORM_RMS, -1);
  11703. cb(cur, "result_norm", -1);
  11704. res->t_embd = cur;
  11705. // Output projection
  11706. cur = build_lora_mm(model.output, cur);
  11707. cb(cur, "result_output", -1);
  11708. res->t_logits = cur;
  11709. ggml_build_forward_expand(gf, cur);
  11710. }
  11711. };
  11712. struct llm_build_glm4_moe : public llm_graph_context {
  11713. llm_build_glm4_moe(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  11714. const int64_t n_embd_head = hparams.n_embd_head_v;
  11715. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  11716. ggml_tensor * cur;
  11717. ggml_tensor * inpL;
  11718. inpL = build_inp_embd(model.tok_embd);
  11719. // inp_pos - contains the positions
  11720. ggml_tensor * inp_pos = build_inp_pos();
  11721. auto * inp_attn = build_attn_inp_kv();
  11722. ggml_tensor * inp_out_ids = build_inp_out_ids();
  11723. // Only process up to last layer (skip final NextN layer)
  11724. // Final layer tensors are loaded but not processed in forward pass
  11725. const int n_transformer_layers = n_layer - hparams.nextn_predict_layers;
  11726. for (int il = 0; il < n_transformer_layers; ++il) {
  11727. ggml_tensor * inpSA = inpL;
  11728. // Pre-attention norm
  11729. cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
  11730. cb(cur, "attn_norm", il);
  11731. // self-attention
  11732. {
  11733. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  11734. if (model.layers[il].bq) {
  11735. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  11736. }
  11737. cb(Qcur, "Qcur", il);
  11738. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  11739. if (model.layers[il].bk) {
  11740. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  11741. }
  11742. cb(Kcur, "Kcur", il);
  11743. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  11744. if (model.layers[il].bv) {
  11745. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  11746. }
  11747. cb(Vcur, "Vcur", il);
  11748. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  11749. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  11750. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  11751. // Apply Q/K norm if available (GLM-4.5 355B variant)
  11752. if (model.layers[il].attn_q_norm) {
  11753. Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);
  11754. cb(Qcur, "Qcur_normed", il);
  11755. }
  11756. if (model.layers[il].attn_k_norm) {
  11757. Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);
  11758. cb(Kcur, "Kcur_normed", il);
  11759. }
  11760. Qcur = ggml_rope_ext(
  11761. ctx0, Qcur, inp_pos, nullptr,
  11762. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  11763. ext_factor, attn_factor, beta_fast, beta_slow
  11764. );
  11765. Kcur = ggml_rope_ext(
  11766. ctx0, Kcur, inp_pos, nullptr,
  11767. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  11768. ext_factor, attn_factor, beta_fast, beta_slow
  11769. );
  11770. cb(Qcur, "Qcur", il);
  11771. cb(Kcur, "Kcur", il);
  11772. cb(Vcur, "Vcur", il);
  11773. cur = build_attn(inp_attn,
  11774. model.layers[il].wo, NULL,
  11775. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  11776. }
  11777. if (il == n_transformer_layers - 1 && inp_out_ids) {
  11778. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  11779. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  11780. }
  11781. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  11782. cb(ffn_inp, "ffn_inp", il);
  11783. // Post-attention norm
  11784. cur = build_norm(ffn_inp, model.layers[il].attn_post_norm, NULL, LLM_NORM_RMS, il);
  11785. cb(cur, "post_attn_norm", il);
  11786. // Check if this is a dense layer (n_layer_dense_lead=1, so layer 0 is dense)
  11787. if (static_cast<uint32_t>(il) < hparams.n_layer_dense_lead) {
  11788. // Dense FFN layer
  11789. cur = build_ffn(cur,
  11790. model.layers[il].ffn_up, NULL, NULL,
  11791. model.layers[il].ffn_gate, NULL, NULL,
  11792. model.layers[il].ffn_down, NULL, NULL,
  11793. NULL,
  11794. LLM_FFN_SILU, LLM_FFN_PAR, il);
  11795. cb(cur, "ffn_out", il);
  11796. } else {
  11797. // Process routed experts using existing MoE infrastructure
  11798. ggml_tensor * routed_out = build_moe_ffn(cur,
  11799. model.layers[il].ffn_gate_inp,
  11800. model.layers[il].ffn_up_exps,
  11801. model.layers[il].ffn_gate_exps,
  11802. model.layers[il].ffn_down_exps,
  11803. model.layers[il].ffn_exp_probs_b,
  11804. n_expert, n_expert_used,
  11805. LLM_FFN_SILU, hparams.expert_weights_norm,
  11806. true, hparams.expert_weights_scale,
  11807. (llama_expert_gating_func_type) hparams.expert_gating_func,
  11808. il);
  11809. cb(routed_out, "ffn_moe_out", il);
  11810. // Process shared expert on original input
  11811. ggml_tensor * shared_out = build_ffn(cur,
  11812. model.layers[il].ffn_up_shexp, NULL, NULL,
  11813. model.layers[il].ffn_gate_shexp, NULL, NULL,
  11814. model.layers[il].ffn_down_shexp, NULL, NULL,
  11815. NULL,
  11816. LLM_FFN_SILU, LLM_FFN_PAR, il);
  11817. cb(shared_out, "ffn_shexp_out", il);
  11818. // Final output: routed_output + shared_output
  11819. cur = ggml_add(ctx0, routed_out, shared_out);
  11820. cb(cur, "ffn_out", il);
  11821. }
  11822. cur = ggml_add(ctx0, cur, ffn_inp);
  11823. cur = build_cvec(cur, il);
  11824. cb(cur, "l_out", il);
  11825. // input for next layer
  11826. inpL = cur;
  11827. }
  11828. cur = inpL;
  11829. cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1);
  11830. cb(cur, "result_norm", -1);
  11831. res->t_embd = cur;
  11832. // lm_head
  11833. cur = build_lora_mm(model.output, cur);
  11834. cb(cur, "result_output", -1);
  11835. res->t_logits = cur;
  11836. ggml_build_forward_expand(gf, cur);
  11837. }
  11838. };
  11839. struct llm_build_nemotron : public llm_graph_context {
  11840. llm_build_nemotron(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  11841. const int64_t n_embd_head = hparams.n_embd_head_v;
  11842. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  11843. //GGML_ASSERT(n_embd_head == hparams.n_rot);
  11844. ggml_tensor * cur;
  11845. ggml_tensor * inpL;
  11846. inpL = build_inp_embd(model.tok_embd);
  11847. // inp_pos - contains the positions
  11848. ggml_tensor * inp_pos = build_inp_pos();
  11849. auto * inp_attn = build_attn_inp_kv();
  11850. ggml_tensor * inp_out_ids = build_inp_out_ids();
  11851. for (int il = 0; il < n_layer; ++il) {
  11852. ggml_tensor * inpSA = inpL;
  11853. // norm
  11854. cur = build_norm(inpL,
  11855. model.layers[il].attn_norm,
  11856. model.layers[il].attn_norm_b,
  11857. LLM_NORM, il);
  11858. cb(cur, "attn_norm", il);
  11859. // self-attention
  11860. {
  11861. // compute Q and K and RoPE them
  11862. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  11863. cb(Qcur, "Qcur", il);
  11864. if (model.layers[il].bq) {
  11865. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  11866. cb(Qcur, "Qcur", il);
  11867. }
  11868. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  11869. cb(Kcur, "Kcur", il);
  11870. if (model.layers[il].bk) {
  11871. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  11872. cb(Kcur, "Kcur", il);
  11873. }
  11874. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  11875. cb(Vcur, "Vcur", il);
  11876. if (model.layers[il].bv) {
  11877. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  11878. cb(Vcur, "Vcur", il);
  11879. }
  11880. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  11881. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  11882. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  11883. Qcur = ggml_rope_ext(
  11884. ctx0, Qcur, inp_pos, nullptr,
  11885. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  11886. ext_factor, attn_factor, beta_fast, beta_slow
  11887. );
  11888. Kcur = ggml_rope_ext(
  11889. ctx0, Kcur, inp_pos, nullptr,
  11890. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  11891. ext_factor, attn_factor, beta_fast, beta_slow
  11892. );
  11893. cb(Qcur, "Qcur", il);
  11894. cb(Kcur, "Kcur", il);
  11895. cb(Vcur, "Vcur", il);
  11896. cur = build_attn(inp_attn,
  11897. model.layers[il].wo, model.layers[il].bo,
  11898. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  11899. }
  11900. if (il == n_layer - 1 && inp_out_ids) {
  11901. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  11902. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  11903. }
  11904. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  11905. cb(ffn_inp, "ffn_inp", il);
  11906. // feed-forward network
  11907. cur = build_norm(ffn_inp,
  11908. model.layers[il].ffn_norm,
  11909. model.layers[il].ffn_norm_b,
  11910. LLM_NORM, il);
  11911. cb(cur, "ffn_norm", il);
  11912. cur = build_ffn(cur,
  11913. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  11914. NULL, NULL, NULL,
  11915. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  11916. NULL,
  11917. LLM_FFN_RELU_SQR, LLM_FFN_SEQ, il);
  11918. cur = ggml_add(ctx0, cur, ffn_inp);
  11919. cb(cur, "ffn_out", il);
  11920. cur = build_cvec(cur, il);
  11921. cb(cur, "l_out", il);
  11922. // input for next layer
  11923. inpL = cur;
  11924. }
  11925. cur = inpL;
  11926. cur = build_norm(cur,
  11927. model.output_norm, model.output_norm_b,
  11928. LLM_NORM, -1);
  11929. cb(cur, "result_norm", -1);
  11930. res->t_embd = cur;
  11931. // lm_head
  11932. cur = build_lora_mm(model.output, cur);
  11933. cb(cur, "result_output", -1);
  11934. res->t_logits = cur;
  11935. ggml_build_forward_expand(gf, cur);
  11936. }
  11937. };
  11938. struct llm_build_nemotron_h : public llm_graph_context_mamba {
  11939. llm_build_nemotron_h(
  11940. const llama_model & model,
  11941. const llm_graph_params & params) :
  11942. llm_graph_context_mamba(params) {
  11943. const int64_t n_embd_head = hparams.n_embd_head_v;
  11944. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  11945. ggml_tensor * cur;
  11946. ggml_tensor * inpL;
  11947. inpL = build_inp_embd(model.tok_embd);
  11948. ggml_build_forward_expand(gf, inpL);
  11949. auto * inp = build_inp_mem_hybrid();
  11950. ggml_tensor * inp_out_ids = build_inp_out_ids();
  11951. for (int il = 0; il < n_layer; ++il) {
  11952. struct ggml_tensor * inpSA = inpL;
  11953. // norm
  11954. cur = build_norm(inpL,
  11955. model.layers[il].attn_norm, NULL,
  11956. LLM_NORM_RMS, il);
  11957. cb(cur, "attn_norm", il);
  11958. if (hparams.is_recurrent(il)) {
  11959. // ssm layer //
  11960. cur = build_mamba2_layer(inp->get_recr(), cur, model, ubatch, il);
  11961. } else if (hparams.n_ff(il) == 0) {
  11962. // attention layer //
  11963. cur = build_attention_layer(cur, inp->get_attn(), model, n_embd_head, il);
  11964. } else {
  11965. cur = build_ffn_layer(cur, model, il);
  11966. }
  11967. if (il == n_layer - 1 && inp_out_ids) {
  11968. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  11969. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  11970. }
  11971. // add residual
  11972. cur = ggml_add(ctx0, cur, inpSA);
  11973. cb(cur, "nemotron_h_block_out", il);
  11974. // input for next layer
  11975. inpL = cur;
  11976. }
  11977. cur = inpL;
  11978. cur = build_norm(cur,
  11979. model.output_norm, NULL,
  11980. LLM_NORM_RMS, -1);
  11981. cb(cur, "result_norm", -1);
  11982. res->t_embd = cur;
  11983. // lm_head
  11984. cur = build_lora_mm(model.output, cur);
  11985. cb(cur, "result_output", -1);
  11986. res->t_logits = cur;
  11987. ggml_build_forward_expand(gf, cur);
  11988. }
  11989. ggml_tensor * build_attention_layer(
  11990. ggml_tensor * cur,
  11991. llm_graph_input_attn_kv * inp_attn,
  11992. const llama_model & model,
  11993. const int64_t n_embd_head,
  11994. const int il) {
  11995. // compute Q and K and (optionally) RoPE them
  11996. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  11997. cb(Qcur, "Qcur", il);
  11998. if (model.layers[il].bq) {
  11999. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  12000. cb(Qcur, "Qcur", il);
  12001. }
  12002. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  12003. cb(Kcur, "Kcur", il);
  12004. if (model.layers[il].bk) {
  12005. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  12006. cb(Kcur, "Kcur", il);
  12007. }
  12008. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  12009. cb(Vcur, "Vcur", il);
  12010. if (model.layers[il].bv) {
  12011. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  12012. cb(Vcur, "Vcur", il);
  12013. }
  12014. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, hparams.n_head(il), n_tokens);
  12015. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, hparams.n_head_kv(il), n_tokens);
  12016. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, hparams.n_head_kv(il), n_tokens);
  12017. cb(Qcur, "Qcur", il);
  12018. cb(Kcur, "Kcur", il);
  12019. cb(Vcur, "Vcur", il);
  12020. const float kq_scale = hparams.f_attention_scale == 0.0f ? 1.0f/sqrtf(float(n_embd_head)) : hparams.f_attention_scale;
  12021. cur = build_attn(inp_attn,
  12022. model.layers[il].wo, model.layers[il].bo,
  12023. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
  12024. cb(cur, "attn_out", il);
  12025. return cur;
  12026. }
  12027. ggml_tensor * build_ffn_layer(
  12028. ggml_tensor * cur,
  12029. const llama_model & model,
  12030. const int il) {
  12031. cur = build_ffn(cur,
  12032. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  12033. NULL, NULL, NULL,
  12034. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  12035. NULL,
  12036. LLM_FFN_RELU_SQR, LLM_FFN_PAR, il);
  12037. cb(cur, "ffn_out", il);
  12038. cur = build_cvec(cur, il);
  12039. cb(cur, "l_out", il);
  12040. return cur;
  12041. }
  12042. };
  12043. struct llm_build_exaone : public llm_graph_context {
  12044. llm_build_exaone(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  12045. const int64_t n_embd_head = hparams.n_embd_head_v;
  12046. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  12047. GGML_ASSERT(n_embd_head == hparams.n_rot);
  12048. ggml_tensor * cur;
  12049. ggml_tensor * inpL;
  12050. inpL = build_inp_embd(model.tok_embd);
  12051. // inp_pos - contains the positions
  12052. ggml_tensor * inp_pos = build_inp_pos();
  12053. auto * inp_attn = build_attn_inp_kv();
  12054. ggml_tensor * inp_out_ids = build_inp_out_ids();
  12055. for (int il = 0; il < n_layer; ++il) {
  12056. ggml_tensor * inpSA = inpL;
  12057. // norm
  12058. cur = build_norm(inpL,
  12059. model.layers[il].attn_norm, NULL,
  12060. LLM_NORM_RMS, il);
  12061. cb(cur, "attn_norm", il);
  12062. // self-attention
  12063. {
  12064. // rope freq factors for llama3; may return nullptr for llama2 and other models
  12065. ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
  12066. // compute Q and K and RoPE them
  12067. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  12068. cb(Qcur, "Qcur", il);
  12069. if (model.layers[il].bq) {
  12070. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  12071. cb(Qcur, "Qcur", il);
  12072. }
  12073. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  12074. cb(Kcur, "Kcur", il);
  12075. if (model.layers[il].bk) {
  12076. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  12077. cb(Kcur, "Kcur", il);
  12078. }
  12079. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  12080. cb(Vcur, "Vcur", il);
  12081. if (model.layers[il].bv) {
  12082. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  12083. cb(Vcur, "Vcur", il);
  12084. }
  12085. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  12086. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  12087. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  12088. Qcur = ggml_rope_ext(
  12089. ctx0, Qcur, inp_pos, rope_factors,
  12090. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  12091. ext_factor, attn_factor, beta_fast, beta_slow
  12092. );
  12093. Kcur = ggml_rope_ext(
  12094. ctx0, Kcur, inp_pos, rope_factors,
  12095. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  12096. ext_factor, attn_factor, beta_fast, beta_slow
  12097. );
  12098. cb(Qcur, "Qcur", il);
  12099. cb(Kcur, "Kcur", il);
  12100. cb(Vcur, "Vcur", il);
  12101. cur = build_attn(inp_attn,
  12102. model.layers[il].wo, model.layers[il].bo,
  12103. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  12104. }
  12105. if (il == n_layer - 1 && inp_out_ids) {
  12106. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  12107. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  12108. }
  12109. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  12110. cb(ffn_inp, "ffn_inp", il);
  12111. // feed-forward network
  12112. cur = build_norm(ffn_inp,
  12113. model.layers[il].ffn_norm, NULL,
  12114. LLM_NORM_RMS, il);
  12115. cb(cur, "ffn_norm", il);
  12116. cur = build_ffn(cur,
  12117. model.layers[il].ffn_up, NULL, NULL,
  12118. model.layers[il].ffn_gate, NULL, NULL,
  12119. model.layers[il].ffn_down, NULL, NULL,
  12120. NULL,
  12121. LLM_FFN_SILU, LLM_FFN_PAR, il);
  12122. cb(cur, "ffn_out", il);
  12123. cur = ggml_add(ctx0, cur, ffn_inp);
  12124. cb(cur, "ffn_out", il);
  12125. cur = build_cvec(cur, il);
  12126. cb(cur, "l_out", il);
  12127. // input for next layer
  12128. inpL = cur;
  12129. }
  12130. cur = inpL;
  12131. cur = build_norm(cur,
  12132. model.output_norm, NULL,
  12133. LLM_NORM_RMS, -1);
  12134. cb(cur, "result_norm", -1);
  12135. res->t_embd = cur;
  12136. // lm_head
  12137. cur = build_lora_mm(model.output, cur);
  12138. cb(cur, "result_output", -1);
  12139. res->t_logits = cur;
  12140. ggml_build_forward_expand(gf, cur);
  12141. }
  12142. };
  12143. template <bool iswa>
  12144. struct llm_build_exaone4 : public llm_graph_context {
  12145. llm_build_exaone4(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  12146. const int64_t n_embd_head = hparams.n_embd_head_k;
  12147. GGML_ASSERT(n_embd_head == hparams.n_embd_head_v);
  12148. GGML_ASSERT(n_embd_head == hparams.n_rot);
  12149. ggml_tensor * cur;
  12150. ggml_tensor * inpL;
  12151. inpL = build_inp_embd(model.tok_embd);
  12152. // inp_pos - contains the positions
  12153. ggml_tensor * inp_pos = build_inp_pos();
  12154. using inp_attn_type = std::conditional_t<iswa, llm_graph_input_attn_kv_iswa, llm_graph_input_attn_kv>;
  12155. inp_attn_type * inp_attn = nullptr;
  12156. if constexpr (iswa) {
  12157. inp_attn = build_attn_inp_kv_iswa();
  12158. } else {
  12159. inp_attn = build_attn_inp_kv();
  12160. }
  12161. ggml_tensor * inp_out_ids = build_inp_out_ids();
  12162. for (int il = 0; il < n_layer; ++il) {
  12163. ggml_tensor * inpSA = inpL;
  12164. // use RoPE for SWA layers or non-SWA models
  12165. const bool use_rope = hparams.is_swa(il) || hparams.swa_type == LLAMA_SWA_TYPE_NONE;
  12166. cur = inpL;
  12167. // self-attention
  12168. {
  12169. ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
  12170. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  12171. cb(Qcur, "Qcur", il);
  12172. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  12173. cb(Kcur, "Kcur", il);
  12174. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  12175. cb(Vcur, "Vcur", il);
  12176. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  12177. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  12178. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  12179. Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);
  12180. Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);
  12181. cb(Qcur, "Qcur_normed", il);
  12182. cb(Kcur, "Kcur_normed", il);
  12183. if (use_rope) {
  12184. Qcur = ggml_rope_ext(
  12185. ctx0, Qcur, inp_pos, rope_factors,
  12186. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  12187. ext_factor, attn_factor, beta_fast, beta_slow
  12188. );
  12189. Kcur = ggml_rope_ext(
  12190. ctx0, Kcur, inp_pos, rope_factors,
  12191. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  12192. ext_factor, attn_factor, beta_fast, beta_slow
  12193. );
  12194. }
  12195. cb(Qcur, "Qcur", il);
  12196. cb(Kcur, "Kcur", il);
  12197. cb(Vcur, "Vcur", il);
  12198. cur = build_attn(inp_attn,
  12199. model.layers[il].wo, NULL,
  12200. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  12201. cb(cur, "attn_out", il);
  12202. }
  12203. if (il == n_layer - 1 && inp_out_ids) {
  12204. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  12205. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  12206. }
  12207. cur = build_norm(cur,
  12208. model.layers[il].attn_post_norm, NULL,
  12209. LLM_NORM_RMS, il);
  12210. cb(cur, "attn_post_norm", il);
  12211. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  12212. cb(ffn_inp, "ffn_inp", il);
  12213. // feed-forward network
  12214. cur = build_ffn(ffn_inp,
  12215. model.layers[il].ffn_up, NULL, NULL,
  12216. model.layers[il].ffn_gate, NULL, NULL,
  12217. model.layers[il].ffn_down, NULL, NULL,
  12218. NULL,
  12219. LLM_FFN_SILU, LLM_FFN_PAR, il);
  12220. cb(cur, "ffn_out", il);
  12221. cur = build_norm(cur,
  12222. model.layers[il].ffn_post_norm, NULL,
  12223. LLM_NORM_RMS, -1);
  12224. cb(cur, "ffn_post_norm", -1);
  12225. cur = ggml_add(ctx0, cur, ffn_inp);
  12226. cur = build_cvec(cur, il);
  12227. cb(cur, "l_out", il);
  12228. // input for next layer
  12229. inpL = cur;
  12230. }
  12231. cur = inpL;
  12232. cur = build_norm(cur,
  12233. model.output_norm, NULL,
  12234. LLM_NORM_RMS, -1);
  12235. cb(cur, "result_norm", -1);
  12236. res->t_embd = cur;
  12237. // lm_head
  12238. cur = build_lora_mm(model.output, cur);
  12239. cb(cur, "result_output", -1);
  12240. res->t_logits = cur;
  12241. ggml_build_forward_expand(gf, cur);
  12242. }
  12243. };
  12244. struct llm_build_rwkv6_base : public llm_graph_context {
  12245. const llama_model & model;
  12246. llm_build_rwkv6_base(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params), model(model) {
  12247. }
  12248. ggml_tensor * build_rwkv6_channel_mix(
  12249. const llama_layer * layer,
  12250. ggml_tensor * cur,
  12251. ggml_tensor * x_prev,
  12252. llm_arch arch) const {
  12253. ggml_tensor * sx = ggml_sub(ctx0, x_prev, cur);
  12254. switch (arch) {
  12255. case LLM_ARCH_RWKV6:
  12256. {
  12257. ggml_tensor * xk = ggml_add(ctx0, ggml_mul(ctx0, sx, layer->channel_mix_lerp_k), cur);
  12258. ggml_tensor * xr = ggml_add(ctx0, ggml_mul(ctx0, sx, layer->channel_mix_lerp_r), cur);
  12259. ggml_tensor * r = ggml_sigmoid(ctx0, build_lora_mm(layer->channel_mix_receptance, xr));
  12260. ggml_tensor * k = ggml_sqr(
  12261. ctx0,
  12262. ggml_relu(
  12263. ctx0,
  12264. build_lora_mm(layer->channel_mix_key, xk)
  12265. )
  12266. );
  12267. cur = ggml_mul(ctx0, r, build_lora_mm(layer->channel_mix_value, k));
  12268. } break;
  12269. default:
  12270. GGML_ABORT("fatal error");
  12271. }
  12272. return cur;
  12273. }
  12274. ggml_tensor * build_rwkv6_time_mix(
  12275. llm_graph_input_rs * inp,
  12276. ggml_tensor * cur,
  12277. ggml_tensor * x_prev,
  12278. const llama_ubatch & ubatch,
  12279. int il) const {
  12280. const auto * mctx_cur = static_cast<const llama_memory_recurrent_context *>(mctx);
  12281. const auto n_tokens = ubatch.n_tokens;
  12282. const auto n_seqs = ubatch.n_seqs;
  12283. const auto n_seq_tokens = ubatch.n_seq_tokens;
  12284. const auto n_embd = hparams.n_embd;
  12285. const auto head_size = hparams.wkv_head_size;
  12286. const auto n_head = n_embd / head_size;
  12287. const auto n_head_kv = hparams.n_head_kv(il);
  12288. const auto kv_head = mctx_cur->get_head();
  12289. const auto & layer = model.layers[il];
  12290. bool is_qrwkv = layer.time_mix_first == nullptr;
  12291. ggml_tensor * sx = ggml_sub(ctx0, x_prev, cur);
  12292. sx = ggml_reshape_2d(ctx0, sx, n_embd, n_tokens);
  12293. cur = ggml_reshape_2d(ctx0, cur, n_embd, n_tokens);
  12294. ggml_tensor * xxx = ggml_add(ctx0, ggml_mul(ctx0, sx, layer.time_mix_lerp_x), cur);
  12295. xxx = ggml_reshape_4d(
  12296. ctx0,
  12297. ggml_tanh(
  12298. ctx0,
  12299. ggml_mul_mat(ctx0, layer.time_mix_w1, xxx)
  12300. ),
  12301. layer.time_mix_w1->ne[1] / 5, 1, 5, n_tokens
  12302. );
  12303. xxx = ggml_cont(ctx0, ggml_permute(ctx0, xxx, 0, 1, 3, 2));
  12304. xxx = ggml_mul_mat(
  12305. ctx0,
  12306. ggml_reshape_4d(
  12307. ctx0,
  12308. layer.time_mix_w2,
  12309. layer.time_mix_w2->ne[0], layer.time_mix_w2->ne[1], 1, 5
  12310. ),
  12311. xxx
  12312. );
  12313. ggml_tensor *xw, *xk, *xv, *xr, *xg;
  12314. if (layer.time_mix_lerp_fused) {
  12315. // fusing these weights makes some performance improvement
  12316. sx = ggml_reshape_3d(ctx0, sx, n_embd, 1, n_tokens);
  12317. cur = ggml_reshape_3d(ctx0, cur, n_embd, 1, n_tokens);
  12318. xxx = ggml_add(ctx0, ggml_mul(ctx0, ggml_add(ctx0, xxx, layer.time_mix_lerp_fused), sx), cur);
  12319. xw = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], 0);
  12320. xk = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], n_embd * n_tokens * sizeof(float));
  12321. xv = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], n_embd * n_tokens * 2 * sizeof(float));
  12322. xr = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], n_embd * n_tokens * 3 * sizeof(float));
  12323. xg = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], n_embd * n_tokens * 4 * sizeof(float));
  12324. } else {
  12325. // for backward compatibility
  12326. xw = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], 0);
  12327. xk = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], n_embd * n_tokens * sizeof(float));
  12328. xv = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], n_embd * n_tokens * 2 * sizeof(float));
  12329. xr = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], n_embd * n_tokens * 3 * sizeof(float));
  12330. xg = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], n_embd * n_tokens * 4 * sizeof(float));
  12331. xw = ggml_add(ctx0, ggml_mul(ctx0, ggml_add(ctx0, xw, layer.time_mix_lerp_w), sx), cur);
  12332. xk = ggml_add(ctx0, ggml_mul(ctx0, ggml_add(ctx0, xk, layer.time_mix_lerp_k), sx), cur);
  12333. xv = ggml_add(ctx0, ggml_mul(ctx0, ggml_add(ctx0, xv, layer.time_mix_lerp_v), sx), cur);
  12334. xr = ggml_add(ctx0, ggml_mul(ctx0, ggml_add(ctx0, xr, layer.time_mix_lerp_r), sx), cur);
  12335. xg = ggml_add(ctx0, ggml_mul(ctx0, ggml_add(ctx0, xg, layer.time_mix_lerp_g), sx), cur);
  12336. }
  12337. ggml_tensor * r = build_lora_mm(layer.time_mix_receptance, xr);
  12338. ggml_tensor * k = build_lora_mm(layer.time_mix_key, xk);
  12339. ggml_tensor * v = build_lora_mm(layer.time_mix_value, xv);
  12340. if (layer.time_mix_receptance_b) {
  12341. r = ggml_add(ctx0, r, layer.time_mix_receptance_b);
  12342. }
  12343. if (layer.time_mix_key_b) {
  12344. k = ggml_add(ctx0, k, layer.time_mix_key_b);
  12345. }
  12346. if (layer.time_mix_value_b) {
  12347. v = ggml_add(ctx0, v, layer.time_mix_value_b);
  12348. }
  12349. ggml_tensor * g = build_lora_mm(layer.time_mix_gate, xg);
  12350. if (is_qrwkv) {
  12351. g = ggml_sigmoid(ctx0, g);
  12352. } else {
  12353. g = ggml_silu(ctx0, g);
  12354. }
  12355. if (n_head_kv != 0 && n_head_kv != n_head) {
  12356. GGML_ASSERT(n_head % n_head_kv == 0);
  12357. k = ggml_reshape_4d(ctx0, k, head_size, 1, n_head_kv, n_tokens);
  12358. v = ggml_reshape_4d(ctx0, v, head_size, 1, n_head_kv, n_tokens);
  12359. ggml_tensor * tmp = ggml_new_tensor_4d(ctx0, GGML_TYPE_F32, head_size, n_head / n_head_kv, n_head_kv, n_tokens);
  12360. k = ggml_repeat(ctx0, k, tmp);
  12361. v = ggml_repeat(ctx0, v, tmp);
  12362. }
  12363. k = ggml_reshape_3d(ctx0, k, head_size, n_head, n_tokens);
  12364. v = ggml_reshape_3d(ctx0, v, head_size, n_head, n_tokens);
  12365. r = ggml_reshape_3d(ctx0, r, head_size, n_head, n_tokens);
  12366. ggml_tensor * w = ggml_mul_mat(
  12367. ctx0,
  12368. layer.time_mix_decay_w2,
  12369. ggml_tanh(
  12370. ctx0,
  12371. ggml_mul_mat(ctx0, layer.time_mix_decay_w1, xw)
  12372. )
  12373. );
  12374. w = ggml_add(ctx0, w, layer.time_mix_decay);
  12375. w = ggml_exp(ctx0, ggml_neg(ctx0, ggml_exp(ctx0, w)));
  12376. w = ggml_reshape_3d(ctx0, w, head_size, n_head, n_tokens);
  12377. if (is_qrwkv) {
  12378. // k = k * (1 - w)
  12379. k = ggml_sub(ctx0, k, ggml_mul(ctx0, k, w));
  12380. }
  12381. ggml_tensor * wkv_state = build_rs(
  12382. inp, mctx_cur->get_s_l(il),
  12383. hparams.n_embd_s(), n_seqs);
  12384. ggml_tensor * wkv_output;
  12385. if (is_qrwkv) {
  12386. wkv_output = ggml_gated_linear_attn(ctx0, k, v, r, w, wkv_state, pow(head_size, -0.5f));
  12387. } else {
  12388. wkv_output = ggml_rwkv_wkv6(ctx0, k, v, r, layer.time_mix_first, w, wkv_state);
  12389. }
  12390. cur = ggml_view_1d(ctx0, wkv_output, n_embd * n_tokens, 0);
  12391. wkv_state = ggml_view_1d(ctx0, wkv_output, n_embd * head_size * n_seqs, n_embd * n_tokens * sizeof(float));
  12392. ggml_build_forward_expand(
  12393. gf,
  12394. ggml_cpy(
  12395. ctx0,
  12396. wkv_state,
  12397. ggml_view_1d(
  12398. ctx0,
  12399. mctx_cur->get_s_l(il),
  12400. hparams.n_embd_s() * n_seqs,
  12401. hparams.n_embd_s() * kv_head * ggml_element_size(mctx_cur->get_s_l(il))
  12402. )
  12403. )
  12404. );
  12405. if (!is_qrwkv) {
  12406. // group norm with head_count groups
  12407. cur = ggml_reshape_3d(ctx0, cur, n_embd / n_head, n_head, n_tokens);
  12408. cur = ggml_norm(ctx0, cur, 64e-5f);
  12409. // Convert back to regular vectors.
  12410. cur = ggml_reshape_2d(ctx0, cur, n_embd, n_tokens);
  12411. cur = ggml_add(ctx0, ggml_mul(ctx0, cur, layer.time_mix_ln), layer.time_mix_ln_b);
  12412. } else {
  12413. cur = ggml_reshape_2d(ctx0, cur, n_embd, n_tokens);
  12414. }
  12415. cur = ggml_mul(ctx0, cur, g);
  12416. cur = build_lora_mm(layer.time_mix_output, cur);
  12417. return ggml_reshape_3d(ctx0, cur, n_embd, n_seq_tokens, n_seqs);
  12418. }
  12419. };
  12420. struct llm_build_rwkv6 : public llm_build_rwkv6_base {
  12421. llm_build_rwkv6(const llama_model & model, const llm_graph_params & params) : llm_build_rwkv6_base(model, params) {
  12422. GGML_ASSERT(hparams.token_shift_count == 2);
  12423. ggml_tensor * cur;
  12424. ggml_tensor * inpL;
  12425. inpL = build_inp_embd(model.tok_embd);
  12426. inpL = build_norm(inpL, model.tok_norm, model.tok_norm_b, LLM_NORM, -1);
  12427. auto * rs_inp = build_rs_inp();
  12428. const auto n_embd = hparams.n_embd;
  12429. const auto n_seq_tokens = ubatch.n_seq_tokens;
  12430. const auto n_seqs = ubatch.n_seqs;
  12431. ggml_tensor * inp_out_ids = build_inp_out_ids();
  12432. for (int il = 0; il < n_layer; ++il) {
  12433. const llama_layer * layer = &model.layers[il];
  12434. inpL = ggml_reshape_3d(ctx0, inpL, n_embd, n_seq_tokens, n_seqs);
  12435. ggml_tensor * token_shift = build_rwkv_token_shift_load(rs_inp, ubatch, il);
  12436. ggml_tensor * att_shift = ggml_view_3d(ctx0, token_shift, n_embd, 1, n_seqs, token_shift->nb[1], token_shift->nb[2], 0);
  12437. 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));
  12438. ggml_tensor * att_norm = build_norm(inpL, layer->attn_norm, layer->attn_norm_b, LLM_NORM, il);
  12439. cb(att_norm, "attn_norm", il);
  12440. ggml_tensor * x_prev = ggml_concat(
  12441. ctx0,
  12442. att_shift,
  12443. ggml_view_3d(ctx0, att_norm, n_embd, n_seq_tokens - 1, n_seqs, att_norm->nb[1], att_norm->nb[2], 0),
  12444. 1
  12445. );
  12446. cur = build_rwkv6_time_mix(rs_inp, att_norm, x_prev, ubatch, il);
  12447. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
  12448. cb(ffn_inp, "ffn_inp", il);
  12449. ggml_tensor * ffn_norm = build_norm(ffn_inp, layer->attn_norm_2, layer->attn_norm_2_b, LLM_NORM, il);
  12450. cb(ffn_norm, "ffn_norm", il);
  12451. x_prev = ggml_concat(
  12452. ctx0,
  12453. ffn_shift,
  12454. ggml_view_3d(ctx0, ffn_norm, n_embd, n_seq_tokens - 1, n_seqs, ffn_norm->nb[1], ffn_norm->nb[2], 0),
  12455. 1
  12456. );
  12457. token_shift = ggml_concat(ctx0,
  12458. 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)),
  12459. 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)),
  12460. 1
  12461. );
  12462. ggml_build_forward_expand(gf, build_rwkv_token_shift_store(token_shift, ubatch, il));
  12463. ffn_inp = ggml_reshape_2d(ctx0, ffn_inp, n_embd, n_tokens);
  12464. ffn_norm = ggml_reshape_2d(ctx0, ffn_norm, n_embd, n_tokens);
  12465. x_prev = ggml_reshape_2d(ctx0, x_prev, n_embd, n_tokens);
  12466. cur = ggml_reshape_2d(ctx0, cur, n_embd, n_tokens);
  12467. if (il == n_layer - 1 && inp_out_ids) {
  12468. ffn_inp = ggml_get_rows(ctx0, ffn_inp, inp_out_ids);
  12469. ffn_norm = ggml_get_rows(ctx0, ffn_norm, inp_out_ids);
  12470. x_prev = ggml_get_rows(ctx0, x_prev, inp_out_ids);
  12471. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  12472. }
  12473. cur = build_rwkv6_channel_mix(layer, ffn_norm, x_prev, LLM_ARCH_RWKV6);
  12474. cur = ggml_add(ctx0, cur, ffn_inp);
  12475. if (hparams.rescale_every_n_layers != 0 && (il + 1) % hparams.rescale_every_n_layers == 0) {
  12476. cur = ggml_scale(ctx0, cur, 0.5F);
  12477. }
  12478. cur = build_cvec(cur, il);
  12479. cb(cur, "l_out", il);
  12480. // input for next layer
  12481. inpL = cur;
  12482. }
  12483. cur = inpL;
  12484. cur = build_norm(cur, model.output_norm, model.output_norm_b, LLM_NORM, -1);
  12485. cb(cur, "result_norm", -1);
  12486. res->t_embd = cur;
  12487. cur = build_lora_mm(model.output, cur);
  12488. cb(cur, "result_output", -1);
  12489. res->t_logits = cur;
  12490. ggml_build_forward_expand(gf, cur);
  12491. }
  12492. };
  12493. // ref: https://huggingface.co/recursal/QRWKV6-32B-Instruct-Preview-v0.1/blob/main/modeling_rwkv6qwen2.py
  12494. struct llm_build_rwkv6qwen2 : public llm_build_rwkv6_base {
  12495. llm_build_rwkv6qwen2(const llama_model & model, const llm_graph_params & params) : llm_build_rwkv6_base(model, params) {
  12496. GGML_ASSERT(n_embd == hparams.n_embd_r());
  12497. ggml_tensor * cur;
  12498. ggml_tensor * inpL;
  12499. inpL = build_inp_embd(model.tok_embd);
  12500. auto * rs_inp = build_rs_inp();
  12501. const auto n_embd = hparams.n_embd;
  12502. const auto n_seq_tokens = ubatch.n_seq_tokens;
  12503. const auto n_seqs = ubatch.n_seqs;
  12504. ggml_tensor * inp_out_ids = build_inp_out_ids();
  12505. for (int il = 0; il < n_layer; ++il) {
  12506. const llama_layer * layer = &model.layers[il];
  12507. inpL = ggml_reshape_3d(ctx0, inpL, n_embd, n_seq_tokens, n_seqs);
  12508. ggml_tensor * token_shift = build_rwkv_token_shift_load(rs_inp, ubatch, il);
  12509. ggml_tensor * att_norm = build_norm(inpL, layer->attn_norm, layer->attn_norm_b, LLM_NORM_RMS, il);
  12510. cb(att_norm, "attn_norm", il);
  12511. ggml_tensor * x_prev = ggml_concat(
  12512. ctx0,
  12513. token_shift,
  12514. ggml_view_3d(ctx0, att_norm, n_embd, n_seq_tokens - 1, n_seqs, att_norm->nb[1], att_norm->nb[2], 0),
  12515. 1
  12516. );
  12517. cur = build_rwkv6_time_mix(rs_inp, att_norm, x_prev, ubatch, il);
  12518. 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));
  12519. ggml_build_forward_expand(gf, build_rwkv_token_shift_store(token_shift, ubatch, il));
  12520. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
  12521. cb(ffn_inp, "ffn_inp", il);
  12522. cur = ggml_reshape_2d(ctx0, cur, n_embd, n_tokens);
  12523. ffn_inp = ggml_reshape_2d(ctx0, ffn_inp, n_embd, n_tokens);
  12524. if (il == n_layer - 1 && inp_out_ids) {
  12525. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  12526. ffn_inp = ggml_get_rows(ctx0, ffn_inp, inp_out_ids);
  12527. }
  12528. // feed-forward network
  12529. cur = build_norm(ffn_inp,
  12530. model.layers[il].ffn_norm, NULL,
  12531. LLM_NORM_RMS, il);
  12532. cb(cur, "ffn_norm", il);
  12533. cur = build_ffn(cur,
  12534. model.layers[il].ffn_up, NULL, NULL,
  12535. model.layers[il].ffn_gate, NULL, NULL,
  12536. model.layers[il].ffn_down, NULL, NULL,
  12537. NULL,
  12538. LLM_FFN_SILU, LLM_FFN_PAR, il);
  12539. cb(cur, "ffn_out", il);
  12540. cur = ggml_add(ctx0, cur, ffn_inp);
  12541. cur = build_cvec(cur, il);
  12542. cb(cur, "l_out", il);
  12543. // input for next layer
  12544. inpL = cur;
  12545. }
  12546. cur = inpL;
  12547. cur = build_norm(cur, model.output_norm, model.output_norm_b, LLM_NORM_RMS, -1);
  12548. cb(cur, "result_norm", -1);
  12549. res->t_embd = cur;
  12550. cur = build_lora_mm(model.output, cur);
  12551. cb(cur, "result_output", -1);
  12552. res->t_logits = cur;
  12553. ggml_build_forward_expand(gf, cur);
  12554. }
  12555. };
  12556. struct llm_build_rwkv7_base : public llm_graph_context {
  12557. const llama_model & model;
  12558. llm_build_rwkv7_base(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params), model(model) {
  12559. }
  12560. ggml_tensor * build_rwkv7_channel_mix(
  12561. const llama_layer * layer,
  12562. ggml_tensor * cur,
  12563. ggml_tensor * x_prev,
  12564. llm_arch arch) const {
  12565. ggml_tensor * sx = ggml_sub(ctx0, x_prev, cur);
  12566. switch (arch) {
  12567. case LLM_ARCH_RWKV7:
  12568. {
  12569. ggml_tensor * xk = ggml_add(ctx0, ggml_mul(ctx0, sx, layer->channel_mix_lerp_k), cur);
  12570. ggml_tensor * k = ggml_sqr(
  12571. ctx0,
  12572. ggml_relu(
  12573. ctx0,
  12574. build_lora_mm(layer->channel_mix_key, xk)
  12575. )
  12576. );
  12577. cur = build_lora_mm(layer->channel_mix_value, k);
  12578. } break;
  12579. default:
  12580. GGML_ABORT("fatal error");
  12581. }
  12582. return cur;
  12583. }
  12584. ggml_tensor * build_rwkv7_time_mix(
  12585. llm_graph_input_rs * inp,
  12586. ggml_tensor * cur,
  12587. ggml_tensor * x_prev,
  12588. ggml_tensor *& first_layer_value,
  12589. const llama_ubatch & ubatch,
  12590. int il) const {
  12591. const auto * mctx_cur = static_cast<const llama_memory_recurrent_context *>(mctx);
  12592. const auto n_tokens = ubatch.n_tokens;
  12593. const auto n_seqs = ubatch.n_seqs;
  12594. const auto n_embd = hparams.n_embd;
  12595. const auto head_size = hparams.wkv_head_size;
  12596. const auto head_count = n_embd / head_size;
  12597. const auto n_seq_tokens = ubatch.n_seq_tokens;
  12598. const auto kv_head = mctx_cur->get_head();
  12599. const auto & layer = model.layers[il];
  12600. bool has_gating = layer.time_mix_g1 && layer.time_mix_g2;
  12601. ggml_tensor * sx = ggml_sub(ctx0, x_prev, cur);
  12602. ggml_tensor * dummy = ggml_new_tensor_4d(ctx0, GGML_TYPE_F32, n_embd, n_seq_tokens, n_seqs, has_gating ? 6 : 5);
  12603. sx = ggml_repeat(ctx0, sx, dummy);
  12604. ggml_tensor * xxx = ggml_add(ctx0, ggml_mul(ctx0, sx, layer.time_mix_lerp_fused), cur);
  12605. ggml_tensor * xr = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], 0);
  12606. ggml_tensor * xw = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], n_embd * n_tokens * sizeof(float));
  12607. ggml_tensor * xk = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], n_embd * n_tokens * 2 * sizeof(float));
  12608. ggml_tensor * xv = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], n_embd * n_tokens * 3 * sizeof(float));
  12609. ggml_tensor * xa = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], n_embd * n_tokens * 4 * sizeof(float));
  12610. 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;
  12611. ggml_tensor * r = build_lora_mm(layer.time_mix_receptance, xr);
  12612. ggml_tensor * w = ggml_add(
  12613. ctx0,
  12614. ggml_mul_mat(ctx0, layer.time_mix_w2, ggml_tanh(ctx0, ggml_mul_mat(ctx0, layer.time_mix_w1, xw))),
  12615. layer.time_mix_w0
  12616. );
  12617. w = ggml_exp(ctx0, ggml_scale(ctx0, ggml_sigmoid(ctx0, w), -0.606531));
  12618. ggml_tensor * k = build_lora_mm(layer.time_mix_key, xk);
  12619. ggml_tensor * v = build_lora_mm(layer.time_mix_value, xv);
  12620. if (first_layer_value == nullptr) {
  12621. first_layer_value = v;
  12622. } else {
  12623. // Add the first layer value as a residual connection.
  12624. v = ggml_add(ctx0, v,
  12625. ggml_mul(ctx0,
  12626. ggml_sub(ctx0, first_layer_value, v),
  12627. ggml_sigmoid(ctx0, ggml_add(ctx0,
  12628. ggml_mul_mat(ctx0, layer.time_mix_v2, ggml_mul_mat(ctx0, layer.time_mix_v1, xv)),
  12629. layer.time_mix_v0
  12630. )
  12631. )
  12632. )
  12633. );
  12634. }
  12635. ggml_tensor * g = nullptr;
  12636. if (layer.time_mix_g1 && layer.time_mix_g2) {
  12637. g = ggml_mul_mat(ctx0, layer.time_mix_g2, ggml_sigmoid(ctx0, ggml_mul_mat(ctx0, layer.time_mix_g1, xg)));
  12638. }
  12639. ggml_tensor * a = ggml_sigmoid(ctx0,
  12640. ggml_add(
  12641. ctx0,
  12642. ggml_mul_mat(ctx0, layer.time_mix_a2, ggml_mul_mat(ctx0, layer.time_mix_a1, xa)),
  12643. layer.time_mix_a0
  12644. )
  12645. );
  12646. ggml_tensor * kk = ggml_reshape_3d(ctx0, ggml_mul(ctx0, k, layer.time_mix_k_k), head_size, head_count, n_tokens);
  12647. kk = ggml_l2_norm(ctx0, kk, 1e-12);
  12648. ggml_tensor * ka = ggml_mul(ctx0, k, layer.time_mix_k_a);
  12649. k = ggml_add(ctx0, k, ggml_sub(ctx0, ggml_mul(ctx0, a, ka), ka));
  12650. r = ggml_reshape_3d(ctx0, r, head_size, head_count, n_tokens);
  12651. w = ggml_reshape_3d(ctx0, w, head_size, head_count, n_tokens);
  12652. k = ggml_reshape_3d(ctx0, k, head_size, head_count, n_tokens);
  12653. v = ggml_reshape_3d(ctx0, v, head_size, head_count, n_tokens);
  12654. a = ggml_reshape_3d(ctx0, a, head_size, head_count, n_tokens);
  12655. ggml_tensor * wkv_state = build_rs(
  12656. inp, mctx_cur->get_s_l(il),
  12657. hparams.n_embd_s(), n_seqs);
  12658. ggml_tensor * wkv_output = ggml_rwkv_wkv7(ctx0, r, w, k, v, ggml_neg(ctx0, kk), ggml_mul(ctx0, kk, a), wkv_state);
  12659. cur = ggml_view_1d(ctx0, wkv_output, n_embd * n_tokens, 0);
  12660. wkv_state = ggml_view_1d(ctx0, wkv_output, n_embd * head_size * n_seqs, n_embd * n_tokens * sizeof(float));
  12661. ggml_build_forward_expand(
  12662. gf,
  12663. ggml_cpy(
  12664. ctx0,
  12665. wkv_state,
  12666. ggml_view_1d(
  12667. ctx0,
  12668. mctx_cur->get_s_l(il),
  12669. hparams.n_embd_s() * n_seqs,
  12670. hparams.n_embd_s() * kv_head * ggml_element_size(mctx_cur->get_s_l(il))
  12671. )
  12672. )
  12673. );
  12674. if (layer.time_mix_ln && layer.time_mix_ln_b) {
  12675. // group norm with head_count groups
  12676. cur = ggml_reshape_3d(ctx0, cur, n_embd / head_count, head_count, n_tokens);
  12677. cur = ggml_norm(ctx0, cur, 64e-5f);
  12678. // Convert back to regular vectors.
  12679. cur = ggml_reshape_2d(ctx0, cur, n_embd, n_tokens);
  12680. cur = ggml_add(ctx0, ggml_mul(ctx0, cur, layer.time_mix_ln), layer.time_mix_ln_b);
  12681. } else {
  12682. cur = ggml_reshape_2d(ctx0, cur, n_embd, n_tokens);
  12683. }
  12684. ggml_tensor * rk = ggml_sum_rows(ctx0,
  12685. ggml_mul(ctx0, ggml_mul(ctx0, k, r), ggml_reshape_2d(ctx0, layer.time_mix_r_k, head_size, head_count)));
  12686. cur = ggml_add(ctx0, cur, ggml_reshape_2d(ctx0, ggml_mul(ctx0, v, rk), n_embd, n_tokens));
  12687. if (has_gating) {
  12688. cur = ggml_mul(ctx0, cur, g);
  12689. }
  12690. cur = build_lora_mm(layer.time_mix_output, cur);
  12691. return ggml_reshape_3d(ctx0, cur, n_embd, n_seq_tokens, n_seqs);
  12692. }
  12693. };
  12694. struct llm_build_rwkv7 : public llm_build_rwkv7_base {
  12695. llm_build_rwkv7(const llama_model & model, const llm_graph_params & params) : llm_build_rwkv7_base(model, params) {
  12696. GGML_ASSERT(hparams.token_shift_count == 2);
  12697. ggml_tensor * cur;
  12698. ggml_tensor * inpL;
  12699. ggml_tensor * v_first = nullptr;
  12700. inpL = build_inp_embd(model.tok_embd);
  12701. inpL = build_norm(inpL, model.tok_norm, model.tok_norm_b, LLM_NORM, -1);
  12702. auto * rs_inp = build_rs_inp();
  12703. const auto n_embd = hparams.n_embd;
  12704. const auto n_seq_tokens = ubatch.n_seq_tokens;
  12705. const auto n_seqs = ubatch.n_seqs;
  12706. ggml_tensor * inp_out_ids = build_inp_out_ids();
  12707. for (int il = 0; il < n_layer; ++il) {
  12708. const llama_layer * layer = &model.layers[il];
  12709. inpL = ggml_reshape_3d(ctx0, inpL, n_embd, n_seq_tokens, n_seqs);
  12710. ggml_tensor * token_shift = build_rwkv_token_shift_load(rs_inp, ubatch, il);
  12711. ggml_tensor * att_shift = ggml_view_3d(ctx0, token_shift, n_embd, 1, n_seqs, token_shift->nb[1], token_shift->nb[2], 0);
  12712. 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));
  12713. ggml_tensor * att_norm = build_norm(inpL, layer->attn_norm, layer->attn_norm_b, LLM_NORM, il);
  12714. cb(att_norm, "attn_norm", il);
  12715. ggml_tensor * x_prev = ggml_concat(
  12716. ctx0,
  12717. att_shift,
  12718. ggml_view_3d(ctx0, att_norm, n_embd, n_seq_tokens - 1, n_seqs, att_norm->nb[1], att_norm->nb[2], 0),
  12719. 1
  12720. );
  12721. cur = build_rwkv7_time_mix(rs_inp, att_norm, x_prev, v_first, ubatch, il);
  12722. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
  12723. cb(ffn_inp, "ffn_inp", il);
  12724. ggml_tensor * ffn_norm = build_norm(ffn_inp, layer->attn_norm_2, layer->attn_norm_2_b, LLM_NORM, il);
  12725. cb(ffn_norm, "ffn_norm", il);
  12726. x_prev = ggml_concat(
  12727. ctx0,
  12728. ffn_shift,
  12729. ggml_view_3d(ctx0, ffn_norm, n_embd, n_seq_tokens - 1, n_seqs, ffn_norm->nb[1], ffn_norm->nb[2], 0),
  12730. 1
  12731. );
  12732. token_shift = ggml_concat(ctx0,
  12733. 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)),
  12734. 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)),
  12735. 1
  12736. );
  12737. ggml_build_forward_expand(gf, build_rwkv_token_shift_store(token_shift, ubatch, il));
  12738. ffn_inp = ggml_reshape_2d(ctx0, ffn_inp, n_embd, n_tokens);
  12739. ffn_norm = ggml_reshape_2d(ctx0, ffn_norm, n_embd, n_tokens);
  12740. x_prev = ggml_reshape_2d(ctx0, x_prev, n_embd, n_tokens);
  12741. if (il == n_layer - 1 && inp_out_ids) {
  12742. ffn_inp = ggml_get_rows(ctx0, ffn_inp, inp_out_ids);
  12743. ffn_norm = ggml_get_rows(ctx0, ffn_norm, inp_out_ids);
  12744. x_prev = ggml_get_rows(ctx0, x_prev, inp_out_ids);
  12745. }
  12746. cur = build_rwkv7_channel_mix(layer, ffn_norm, x_prev, LLM_ARCH_RWKV7);
  12747. cur = ggml_add(ctx0, cur, ffn_inp);
  12748. cur = build_cvec(cur, il);
  12749. cb(cur, "l_out", il);
  12750. // input for next layer
  12751. inpL = cur;
  12752. }
  12753. cur = inpL;
  12754. cur = build_norm(cur, model.output_norm, model.output_norm_b, LLM_NORM, -1);
  12755. cb(cur, "result_norm", -1);
  12756. res->t_embd = cur;
  12757. cur = build_lora_mm(model.output, cur);
  12758. cb(cur, "result_output", -1);
  12759. res->t_logits = cur;
  12760. ggml_build_forward_expand(gf, cur);
  12761. }
  12762. };
  12763. struct llm_build_arwkv7 : public llm_build_rwkv7_base {
  12764. llm_build_arwkv7(const llama_model & model, const llm_graph_params & params) : llm_build_rwkv7_base(model, params) {
  12765. GGML_ASSERT(n_embd == hparams.n_embd_r());
  12766. ggml_tensor * cur;
  12767. ggml_tensor * inpL;
  12768. ggml_tensor * v_first = nullptr;
  12769. inpL = build_inp_embd(model.tok_embd);
  12770. auto * rs_inp = build_rs_inp();
  12771. const auto n_embd = hparams.n_embd;
  12772. const auto n_seq_tokens = ubatch.n_seq_tokens;
  12773. const auto n_seqs = ubatch.n_seqs;
  12774. ggml_tensor * inp_out_ids = build_inp_out_ids();
  12775. for (int il = 0; il < n_layer; ++il) {
  12776. const llama_layer * layer = &model.layers[il];
  12777. inpL = ggml_reshape_3d(ctx0, inpL, n_embd, n_seq_tokens, n_seqs);
  12778. ggml_tensor * token_shift = build_rwkv_token_shift_load(rs_inp, ubatch, il);
  12779. ggml_tensor * att_norm = build_norm(inpL, layer->attn_norm, layer->attn_norm_b, LLM_NORM_RMS, il);
  12780. cb(att_norm, "attn_norm", il);
  12781. ggml_tensor * x_prev = ggml_concat(
  12782. ctx0,
  12783. token_shift,
  12784. ggml_view_3d(ctx0, att_norm, n_embd, n_seq_tokens - 1, n_seqs, att_norm->nb[1], att_norm->nb[2], 0),
  12785. 1
  12786. );
  12787. cur = build_rwkv7_time_mix(rs_inp, att_norm, x_prev, v_first, ubatch, il);
  12788. 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));
  12789. ggml_build_forward_expand(gf, build_rwkv_token_shift_store(token_shift, ubatch, il));
  12790. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
  12791. cb(ffn_inp, "ffn_inp", il);
  12792. cur = ggml_reshape_2d(ctx0, cur, n_embd, n_tokens);
  12793. ffn_inp = ggml_reshape_2d(ctx0, ffn_inp, n_embd, n_tokens);
  12794. if (il == n_layer - 1 && inp_out_ids) {
  12795. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  12796. ffn_inp = ggml_get_rows(ctx0, ffn_inp, inp_out_ids);
  12797. }
  12798. // feed-forward network
  12799. cur = build_norm(ffn_inp,
  12800. model.layers[il].ffn_norm, NULL,
  12801. LLM_NORM_RMS, il);
  12802. cb(cur, "ffn_norm", il);
  12803. cur = build_ffn(cur,
  12804. model.layers[il].ffn_up, NULL, NULL,
  12805. model.layers[il].ffn_gate, NULL, NULL,
  12806. model.layers[il].ffn_down, NULL, NULL,
  12807. NULL,
  12808. LLM_FFN_SILU, LLM_FFN_PAR, il);
  12809. cb(cur, "ffn_out", il);
  12810. cur = ggml_add(ctx0, cur, ffn_inp);
  12811. cur = build_cvec(cur, il);
  12812. cb(cur, "l_out", il);
  12813. // input for next layer
  12814. inpL = cur;
  12815. }
  12816. cur = inpL;
  12817. cur = build_norm(cur, model.output_norm, model.output_norm_b, LLM_NORM_RMS, -1);
  12818. cb(cur, "result_norm", -1);
  12819. res->t_embd = cur;
  12820. cur = build_lora_mm(model.output, cur);
  12821. cb(cur, "result_output", -1);
  12822. res->t_logits = cur;
  12823. ggml_build_forward_expand(gf, cur);
  12824. }
  12825. };
  12826. struct llm_build_granite : public llm_graph_context {
  12827. llm_build_granite(
  12828. const llama_model & model,
  12829. const llm_graph_params & params)
  12830. : llm_graph_context(params) {
  12831. const int64_t n_embd_head = hparams.n_embd_head_v;
  12832. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  12833. GGML_ASSERT(n_embd_head == hparams.n_rot);
  12834. ggml_tensor * cur;
  12835. ggml_tensor * inpL;
  12836. inpL = build_inp_embd(model.tok_embd);
  12837. // inp_pos - built only if rope enabled
  12838. ggml_tensor * inp_pos = nullptr;
  12839. if (hparams.rope_finetuned) {
  12840. inp_pos = build_inp_pos();
  12841. }
  12842. auto * inp_attn = build_attn_inp_kv();
  12843. ggml_tensor * inp_out_ids = build_inp_out_ids();
  12844. for (int il = 0; il < n_layer; ++il) {
  12845. ggml_tensor * inpSA = inpL;
  12846. // norm
  12847. cur = build_norm(inpL,
  12848. model.layers[il].attn_norm, NULL,
  12849. LLM_NORM_RMS, il);
  12850. cb(cur, "attn_norm", il);
  12851. // self-attention
  12852. cur = build_attention_layer(
  12853. cur, inp_pos, inp_attn,
  12854. model, n_embd_head, il);
  12855. if (il == n_layer - 1 && inp_out_ids) {
  12856. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  12857. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  12858. }
  12859. // ffn
  12860. cur = build_layer_ffn(cur, inpSA, model, il);
  12861. // input for next layer
  12862. inpL = cur;
  12863. }
  12864. cur = inpL;
  12865. cur = build_norm(cur,
  12866. model.output_norm, NULL,
  12867. LLM_NORM_RMS, -1);
  12868. cb(cur, "result_norm", -1);
  12869. res->t_embd = cur;
  12870. // lm_head
  12871. cur = build_lora_mm(model.output, cur);
  12872. // For Granite architectures - scale logits
  12873. cur = ggml_scale(ctx0, cur, 1.0f / hparams.f_logit_scale);
  12874. cb(cur, "result_output", -1);
  12875. res->t_logits = cur;
  12876. ggml_build_forward_expand(gf, cur);
  12877. }
  12878. ggml_tensor * build_attention_layer(
  12879. ggml_tensor * cur,
  12880. ggml_tensor * inp_pos,
  12881. llm_graph_input_attn_kv * inp_attn,
  12882. const llama_model & model,
  12883. const int64_t n_embd_head,
  12884. const int il) {
  12885. // compute Q and K and (optionally) RoPE them
  12886. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  12887. cb(Qcur, "Qcur", il);
  12888. if (model.layers[il].bq) {
  12889. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  12890. cb(Qcur, "Qcur", il);
  12891. }
  12892. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  12893. cb(Kcur, "Kcur", il);
  12894. if (model.layers[il].bk) {
  12895. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  12896. cb(Kcur, "Kcur", il);
  12897. }
  12898. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  12899. cb(Vcur, "Vcur", il);
  12900. if (model.layers[il].bv) {
  12901. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  12902. cb(Vcur, "Vcur", il);
  12903. }
  12904. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, hparams.n_head(il), n_tokens);
  12905. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, hparams.n_head_kv(il), n_tokens);
  12906. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, hparams.n_head_kv(il), n_tokens);
  12907. const bool use_rope = hparams.rope_finetuned;
  12908. if (use_rope) {
  12909. ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
  12910. Qcur = ggml_rope_ext(
  12911. ctx0, Qcur, inp_pos, rope_factors,
  12912. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  12913. ext_factor, attn_factor, beta_fast, beta_slow
  12914. );
  12915. Kcur = ggml_rope_ext(
  12916. ctx0, Kcur, inp_pos, rope_factors,
  12917. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  12918. ext_factor, attn_factor, beta_fast, beta_slow
  12919. );
  12920. }
  12921. cb(Qcur, "Qcur", il);
  12922. cb(Kcur, "Kcur", il);
  12923. cb(Vcur, "Vcur", il);
  12924. const float kq_scale = hparams.f_attention_scale == 0.0f ? 1.0f/sqrtf(float(n_embd_head)) : hparams.f_attention_scale;
  12925. cur = build_attn(inp_attn,
  12926. model.layers[il].wo, model.layers[il].bo,
  12927. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
  12928. cb(cur, "attn_out", il);
  12929. return cur;
  12930. }
  12931. ggml_tensor * build_layer_ffn(
  12932. ggml_tensor * cur,
  12933. ggml_tensor * inpSA,
  12934. const llama_model & model,
  12935. const int il) {
  12936. // For Granite architectures - scale residual
  12937. if (hparams.f_residual_scale) {
  12938. cur = ggml_scale(ctx0, cur, hparams.f_residual_scale);
  12939. }
  12940. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  12941. cb(ffn_inp, "ffn_inp", il);
  12942. // feed-forward network (non-MoE)
  12943. if (model.layers[il].ffn_gate_inp == nullptr) {
  12944. cur = build_norm(ffn_inp,
  12945. model.layers[il].ffn_norm, NULL,
  12946. LLM_NORM_RMS, il);
  12947. cb(cur, "ffn_norm", il);
  12948. cur = build_ffn(cur,
  12949. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  12950. model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, NULL,
  12951. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  12952. NULL,
  12953. LLM_FFN_SILU, LLM_FFN_PAR, il);
  12954. cb(cur, "ffn_out", il);
  12955. } else {
  12956. // MoE branch
  12957. cur = build_norm(ffn_inp,
  12958. model.layers[il].ffn_norm, NULL,
  12959. LLM_NORM_RMS, il);
  12960. cb(cur, "ffn_norm", il);
  12961. ggml_tensor * moe_out = build_moe_ffn(cur,
  12962. model.layers[il].ffn_gate_inp,
  12963. model.layers[il].ffn_up_exps,
  12964. model.layers[il].ffn_gate_exps,
  12965. model.layers[il].ffn_down_exps,
  12966. nullptr,
  12967. n_expert, n_expert_used,
  12968. LLM_FFN_SILU, true,
  12969. false, 0.0,
  12970. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  12971. il);
  12972. cb(moe_out, "ffn_moe_out", il);
  12973. // For Granite MoE Shared
  12974. if (hparams.n_ff_shexp > 0) {
  12975. ggml_tensor * ffn_shexp = build_ffn(cur,
  12976. model.layers[il].ffn_up_shexp, NULL, NULL,
  12977. model.layers[il].ffn_gate_shexp, NULL, NULL,
  12978. model.layers[il].ffn_down_shexp, NULL, NULL,
  12979. NULL,
  12980. LLM_FFN_SILU, LLM_FFN_PAR, il);
  12981. cb(ffn_shexp, "ffn_shexp", il);
  12982. cur = ggml_add(ctx0, moe_out, ffn_shexp);
  12983. cb(cur, "ffn_out", il);
  12984. } else {
  12985. cur = moe_out;
  12986. }
  12987. }
  12988. // For Granite architectures - scale residual
  12989. if (hparams.f_residual_scale) {
  12990. cur = ggml_scale(ctx0, cur, hparams.f_residual_scale);
  12991. }
  12992. cur = ggml_add(ctx0, cur, ffn_inp);
  12993. cb(cur, "ffn_out", il);
  12994. cur = build_cvec(cur, il);
  12995. cb(cur, "l_out", il);
  12996. return cur;
  12997. }
  12998. };
  12999. struct llm_build_granite_hybrid : public llm_graph_context_mamba {
  13000. llm_build_granite_hybrid(
  13001. const llama_model & model,
  13002. const llm_graph_params & params) :
  13003. llm_graph_context_mamba(params) {
  13004. const int64_t n_embd_head = hparams.n_embd_head_v;
  13005. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  13006. ggml_tensor * cur;
  13007. ggml_tensor * inpL;
  13008. inpL = build_inp_embd(model.tok_embd);
  13009. auto * inp = build_inp_mem_hybrid();
  13010. ggml_tensor * inp_out_ids = build_inp_out_ids();
  13011. // Positional embeddings populated if rope enabled
  13012. ggml_tensor * inp_pos = nullptr;
  13013. if (hparams.rope_finetuned) {
  13014. inp_pos = build_inp_pos();
  13015. }
  13016. for (int il = 0; il < n_layer; ++il) {
  13017. struct ggml_tensor * inpSA = inpL;
  13018. // norm
  13019. cur = build_norm(inpL,
  13020. model.layers[il].attn_norm, NULL,
  13021. LLM_NORM_RMS, il);
  13022. cb(cur, "attn_norm", il);
  13023. if (hparams.is_recurrent(il)) {
  13024. // ssm layer //
  13025. cur = build_mamba2_layer(inp->get_recr(), cur, model, ubatch, il);
  13026. } else {
  13027. // attention layer //
  13028. cur = build_attention_layer(
  13029. cur, inp_pos, inp->get_attn(), model,
  13030. n_embd_head, il);
  13031. }
  13032. if (il == n_layer - 1 && inp_out_ids) {
  13033. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  13034. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  13035. }
  13036. // ffn
  13037. cur = build_layer_ffn(cur, inpSA, model, il);
  13038. // input for next layer
  13039. inpL = cur;
  13040. }
  13041. cur = inpL;
  13042. cur = build_norm(cur,
  13043. model.output_norm, NULL,
  13044. LLM_NORM_RMS, -1);
  13045. cb(cur, "result_norm", -1);
  13046. res->t_embd = cur;
  13047. // lm_head
  13048. cur = build_lora_mm(model.output, cur);
  13049. // For Granite architectures - scale logits
  13050. if (hparams.f_logit_scale) {
  13051. cur = ggml_scale(ctx0, cur, 1.0f / hparams.f_logit_scale);
  13052. }
  13053. cb(cur, "result_output", -1);
  13054. res->t_logits = cur;
  13055. ggml_build_forward_expand(gf, cur);
  13056. }
  13057. ggml_tensor * build_attention_layer(
  13058. ggml_tensor * cur,
  13059. ggml_tensor * inp_pos,
  13060. llm_graph_input_attn_kv * inp_attn,
  13061. const llama_model & model,
  13062. const int64_t n_embd_head,
  13063. const int il) {
  13064. // compute Q and K and (optionally) RoPE them
  13065. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  13066. cb(Qcur, "Qcur", il);
  13067. if (model.layers[il].bq) {
  13068. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  13069. cb(Qcur, "Qcur", il);
  13070. }
  13071. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  13072. cb(Kcur, "Kcur", il);
  13073. if (model.layers[il].bk) {
  13074. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  13075. cb(Kcur, "Kcur", il);
  13076. }
  13077. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  13078. cb(Vcur, "Vcur", il);
  13079. if (model.layers[il].bv) {
  13080. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  13081. cb(Vcur, "Vcur", il);
  13082. }
  13083. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, hparams.n_head(il), n_tokens);
  13084. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, hparams.n_head_kv(il), n_tokens);
  13085. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, hparams.n_head_kv(il), n_tokens);
  13086. const bool use_rope = hparams.rope_finetuned;
  13087. if (use_rope) {
  13088. ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
  13089. Qcur = ggml_rope_ext(
  13090. ctx0, Qcur, inp_pos, rope_factors,
  13091. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  13092. ext_factor, attn_factor, beta_fast, beta_slow
  13093. );
  13094. Kcur = ggml_rope_ext(
  13095. ctx0, Kcur, inp_pos, rope_factors,
  13096. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  13097. ext_factor, attn_factor, beta_fast, beta_slow
  13098. );
  13099. }
  13100. cb(Qcur, "Qcur", il);
  13101. cb(Kcur, "Kcur", il);
  13102. cb(Vcur, "Vcur", il);
  13103. const float kq_scale = hparams.f_attention_scale == 0.0f ? 1.0f/sqrtf(float(n_embd_head)) : hparams.f_attention_scale;
  13104. cur = build_attn(inp_attn,
  13105. model.layers[il].wo, model.layers[il].bo,
  13106. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
  13107. cb(cur, "attn_out", il);
  13108. return cur;
  13109. }
  13110. ggml_tensor * build_layer_ffn(
  13111. ggml_tensor * cur,
  13112. ggml_tensor * inpSA,
  13113. const llama_model & model,
  13114. const int il) {
  13115. // For Granite architectures - scale residual
  13116. if (hparams.f_residual_scale) {
  13117. cur = ggml_scale(ctx0, cur, hparams.f_residual_scale);
  13118. }
  13119. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  13120. cb(ffn_inp, "ffn_inp", il);
  13121. // feed-forward network (non-MoE)
  13122. if (model.layers[il].ffn_gate_inp == nullptr) {
  13123. cur = build_norm(ffn_inp,
  13124. model.layers[il].ffn_norm, NULL,
  13125. LLM_NORM_RMS, il);
  13126. cb(cur, "ffn_norm", il);
  13127. cur = build_ffn(cur,
  13128. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  13129. model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, NULL,
  13130. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  13131. NULL,
  13132. LLM_FFN_SILU, LLM_FFN_PAR, il);
  13133. cb(cur, "ffn_out", il);
  13134. } else {
  13135. // MoE branch
  13136. cur = build_norm(ffn_inp,
  13137. model.layers[il].ffn_norm, NULL,
  13138. LLM_NORM_RMS, il);
  13139. cb(cur, "ffn_norm", il);
  13140. ggml_tensor * moe_out = build_moe_ffn(cur,
  13141. model.layers[il].ffn_gate_inp,
  13142. model.layers[il].ffn_up_exps,
  13143. model.layers[il].ffn_gate_exps,
  13144. model.layers[il].ffn_down_exps,
  13145. nullptr,
  13146. n_expert, n_expert_used,
  13147. LLM_FFN_SILU, true,
  13148. false, 0.0,
  13149. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  13150. il);
  13151. cb(moe_out, "ffn_moe_out", il);
  13152. // For Granite MoE Shared
  13153. if (hparams.n_ff_shexp > 0) {
  13154. ggml_tensor * ffn_shexp = build_ffn(cur,
  13155. model.layers[il].ffn_up_shexp, NULL, NULL,
  13156. model.layers[il].ffn_gate_shexp, NULL, NULL,
  13157. model.layers[il].ffn_down_shexp, NULL, NULL,
  13158. NULL,
  13159. LLM_FFN_SILU, LLM_FFN_PAR, il);
  13160. cb(ffn_shexp, "ffn_shexp", il);
  13161. cur = ggml_add(ctx0, moe_out, ffn_shexp);
  13162. cb(cur, "ffn_out", il);
  13163. } else {
  13164. cur = moe_out;
  13165. }
  13166. }
  13167. // For Granite architectures - scale residual
  13168. if (hparams.f_residual_scale) {
  13169. cur = ggml_scale(ctx0, cur, hparams.f_residual_scale);
  13170. }
  13171. cur = ggml_add(ctx0, cur, ffn_inp);
  13172. cb(cur, "ffn_out", il);
  13173. cur = build_cvec(cur, il);
  13174. cb(cur, "l_out", il);
  13175. return cur;
  13176. }
  13177. };
  13178. // ref: https://github.com/facebookresearch/chameleon
  13179. // based on the original build_llama() function, changes:
  13180. // * qk-norm
  13181. // * swin-norm
  13182. // * removed bias
  13183. // * removed MoE
  13184. struct llm_build_chameleon : public llm_graph_context {
  13185. llm_build_chameleon(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  13186. const int64_t n_embd_head = hparams.n_embd_head_v;
  13187. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  13188. GGML_ASSERT(n_embd_head == hparams.n_rot);
  13189. ggml_tensor * cur;
  13190. ggml_tensor * inpL;
  13191. inpL = build_inp_embd(model.tok_embd);
  13192. // inp_pos - contains the positions
  13193. ggml_tensor * inp_pos = build_inp_pos();
  13194. auto * inp_attn = build_attn_inp_kv();
  13195. ggml_tensor * inp_out_ids = build_inp_out_ids();
  13196. for (int il = 0; il < n_layer; ++il) {
  13197. ggml_tensor * inpSA = inpL;
  13198. // norm
  13199. if (hparams.swin_norm) {
  13200. cur = inpL;
  13201. } else {
  13202. cur = build_norm(inpL,
  13203. model.layers[il].attn_norm, NULL,
  13204. LLM_NORM_RMS, il);
  13205. cb(cur, "attn_norm", il);
  13206. }
  13207. // self-attention
  13208. {
  13209. // compute Q and K and RoPE them
  13210. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  13211. cb(Qcur, "Qcur", il);
  13212. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  13213. cb(Kcur, "Kcur", il);
  13214. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  13215. cb(Vcur, "Vcur", il);
  13216. if (model.layers[il].attn_q_norm) {
  13217. Qcur = ggml_view_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens,
  13218. ggml_element_size(Qcur) * n_embd_head,
  13219. ggml_element_size(Qcur) * n_embd_head * n_head,
  13220. 0);
  13221. cb(Qcur, "Qcur", il);
  13222. Qcur = build_norm(Qcur,
  13223. model.layers[il].attn_q_norm,
  13224. model.layers[il].attn_q_norm_b,
  13225. LLM_NORM, il);
  13226. cb(Qcur, "Qcur", il);
  13227. }
  13228. if (model.layers[il].attn_k_norm) {
  13229. Kcur = ggml_view_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens,
  13230. ggml_element_size(Kcur) * n_embd_head,
  13231. ggml_element_size(Kcur) * n_embd_head * n_head_kv,
  13232. 0);
  13233. cb(Kcur, "Kcur", il);
  13234. Kcur = build_norm(Kcur,
  13235. model.layers[il].attn_k_norm,
  13236. model.layers[il].attn_k_norm_b,
  13237. LLM_NORM, il);
  13238. cb(Kcur, "Kcur", il);
  13239. }
  13240. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  13241. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  13242. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  13243. Qcur = ggml_rope_ext(
  13244. ctx0, Qcur, inp_pos, nullptr,
  13245. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  13246. ext_factor, attn_factor, beta_fast, beta_slow
  13247. );
  13248. Kcur = ggml_rope_ext(
  13249. ctx0, Kcur, inp_pos, nullptr,
  13250. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  13251. ext_factor, attn_factor, beta_fast, beta_slow
  13252. );
  13253. cb(Qcur, "Qcur", il);
  13254. cb(Kcur, "Kcur", il);
  13255. cb(Vcur, "Vcur", il);
  13256. cur = build_attn(inp_attn,
  13257. model.layers[il].wo, nullptr,
  13258. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  13259. }
  13260. if (il == n_layer - 1 && inp_out_ids) {
  13261. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  13262. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  13263. }
  13264. if (hparams.swin_norm) {
  13265. cur = build_norm(cur,
  13266. model.layers[il].attn_norm, NULL,
  13267. LLM_NORM_RMS, il);
  13268. }
  13269. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  13270. cb(ffn_inp, "ffn_inp", il);
  13271. // feed-forward network
  13272. if (!hparams.swin_norm) {
  13273. cur = build_norm(ffn_inp,
  13274. model.layers[il].ffn_norm, NULL,
  13275. LLM_NORM_RMS, il);
  13276. cb(cur, "ffn_norm", il);
  13277. }
  13278. cur = build_ffn(cur,
  13279. model.layers[il].ffn_up, NULL, NULL,
  13280. model.layers[il].ffn_gate, NULL, NULL,
  13281. model.layers[il].ffn_down, NULL, NULL,
  13282. NULL,
  13283. LLM_FFN_SILU, LLM_FFN_PAR, il);
  13284. cb(cur, "ffn_out", il);
  13285. if (hparams.swin_norm) {
  13286. cur = build_norm(cur,
  13287. model.layers[il].ffn_norm, NULL,
  13288. LLM_NORM_RMS, il);
  13289. cb(cur, "ffn_norm", il);
  13290. }
  13291. cur = ggml_add(ctx0, cur, ffn_inp);
  13292. cb(cur, "ffn_out", il);
  13293. cur = build_cvec(cur, il);
  13294. cb(cur, "l_out", il);
  13295. // input for next layer
  13296. inpL = cur;
  13297. }
  13298. cur = inpL;
  13299. cur = build_norm(cur,
  13300. model.output_norm, NULL,
  13301. LLM_NORM_RMS, -1);
  13302. cb(cur, "result_norm", -1);
  13303. res->t_embd = cur;
  13304. // lm_head
  13305. cur = build_lora_mm(model.output, cur);
  13306. cb(cur, "result_output_with_img_logits", -1);
  13307. // TODO: this suppresses the output of image tokens, which is required to enable text-only outputs.
  13308. // Needs to be removed once image outputs are supported.
  13309. int img_token_end_idx = 8196;
  13310. int img_token_start_idx = 4;
  13311. int num_img_tokens = img_token_end_idx - img_token_start_idx;
  13312. // creates 1d tensor of size num_img_tokens and values -FLT_MAX,
  13313. // which ensures that text token values are always at least larger than image token values
  13314. ggml_tensor * img_logits = ggml_new_tensor_1d(ctx0, GGML_TYPE_F32, num_img_tokens);
  13315. img_logits = ggml_clamp(ctx0, img_logits, -FLT_MAX, -FLT_MAX);
  13316. cb(img_logits, "img_logits", -1);
  13317. cur = ggml_set_1d(ctx0, cur, img_logits, ggml_element_size(cur) * img_token_start_idx);
  13318. cb(cur, "result_output", -1);
  13319. res->t_logits = cur;
  13320. ggml_build_forward_expand(gf, cur);
  13321. }
  13322. };
  13323. struct llm_build_wavtokenizer_dec : public llm_graph_context {
  13324. llm_build_wavtokenizer_dec(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  13325. ggml_tensor * cur;
  13326. ggml_tensor * inpL;
  13327. inpL = build_inp_embd(model.tok_embd);
  13328. cur = ggml_cont(ctx0, ggml_transpose(ctx0, inpL));
  13329. cur = ggml_conv_1d_ph(ctx0, model.conv1d, cur, 1, 1);
  13330. cur = ggml_add(ctx0, cur, model.conv1d_b);
  13331. // posnet
  13332. for (uint32_t il = 0; il < hparams.posnet.n_layer; ++il) {
  13333. const auto & layer = model.layers[il].posnet;
  13334. inpL = cur;
  13335. switch (il) {
  13336. case 0:
  13337. case 1:
  13338. case 3:
  13339. case 4:
  13340. {
  13341. cur = build_norm(cur,
  13342. layer.norm1,
  13343. layer.norm1_b,
  13344. LLM_NORM_GROUP, 0);
  13345. cur = ggml_mul(ctx0, ggml_sigmoid(ctx0, cur), cur);
  13346. cur = ggml_conv_1d_ph(ctx0, layer.conv1, cur, 1, 1);
  13347. cur = ggml_add(ctx0, cur, layer.conv1_b);
  13348. cur = build_norm(cur,
  13349. layer.norm2,
  13350. layer.norm2_b,
  13351. LLM_NORM_GROUP, 0);
  13352. cur = ggml_mul(ctx0, ggml_sigmoid(ctx0, cur), cur);
  13353. cur = ggml_conv_1d_ph(ctx0, layer.conv2, cur, 1, 1);
  13354. cur = ggml_add(ctx0, cur, layer.conv2_b);
  13355. cur = ggml_add(ctx0, cur, inpL);
  13356. } break;
  13357. case 2:
  13358. {
  13359. cur = build_norm(cur,
  13360. layer.attn_norm,
  13361. layer.attn_norm_b,
  13362. LLM_NORM_GROUP, 0);
  13363. ggml_tensor * q;
  13364. ggml_tensor * k;
  13365. ggml_tensor * v;
  13366. q = ggml_conv_1d_ph(ctx0, layer.attn_q, cur, 1, 1);
  13367. k = ggml_conv_1d_ph(ctx0, layer.attn_k, cur, 1, 1);
  13368. v = ggml_conv_1d_ph(ctx0, layer.attn_v, cur, 1, 1);
  13369. q = ggml_add(ctx0, q, layer.attn_q_b);
  13370. k = ggml_add(ctx0, k, layer.attn_k_b);
  13371. v = ggml_add(ctx0, v, layer.attn_v_b);
  13372. q = ggml_cont(ctx0, ggml_transpose(ctx0, q));
  13373. k = ggml_cont(ctx0, ggml_transpose(ctx0, k));
  13374. ggml_tensor * kq = ggml_mul_mat(ctx0, k, q);
  13375. kq = ggml_soft_max_ext(ctx0, kq, nullptr, 1.0f/sqrtf(float(hparams.posnet.n_embd)), 0.0f);
  13376. cur = ggml_mul_mat(ctx0, kq, v);
  13377. cur = ggml_conv_1d_ph(ctx0, layer.attn_o, cur, 1, 1);
  13378. cur = ggml_add(ctx0, cur, layer.attn_o_b);
  13379. cur = ggml_add(ctx0, cur, inpL);
  13380. } break;
  13381. case 5:
  13382. {
  13383. cur = build_norm(cur,
  13384. layer.norm,
  13385. layer.norm_b,
  13386. LLM_NORM_GROUP, 0);
  13387. } break;
  13388. default: GGML_ABORT("unknown posnet layer");
  13389. };
  13390. }
  13391. cur = ggml_cont(ctx0, ggml_transpose(ctx0, cur));
  13392. cur = build_norm(cur,
  13393. model.tok_norm,
  13394. model.tok_norm_b,
  13395. LLM_NORM, -1);
  13396. cur = ggml_cont(ctx0, ggml_transpose(ctx0, cur));
  13397. inpL = cur;
  13398. // convnext
  13399. for (uint32_t il = 0; il < hparams.convnext.n_layer; ++il) {
  13400. const auto & layer = model.layers[il].convnext;
  13401. cur = inpL;
  13402. cur = ggml_conv_1d_dw_ph(ctx0, layer.dw, cur, 1, 1);
  13403. cur = ggml_add(ctx0, cur, layer.dw_b);
  13404. cur = ggml_cont(ctx0, ggml_transpose(ctx0, cur));
  13405. cur = build_norm(cur,
  13406. layer.norm,
  13407. layer.norm_b,
  13408. LLM_NORM, -1);
  13409. cur = build_ffn(cur,
  13410. layer.pw1, layer.pw1_b, NULL,
  13411. NULL, NULL, NULL,
  13412. layer.pw2, layer.pw2_b, NULL,
  13413. NULL,
  13414. LLM_FFN_GELU, LLM_FFN_SEQ, il);
  13415. cur = ggml_mul(ctx0, cur, layer.gamma);
  13416. cur = ggml_cont(ctx0, ggml_transpose(ctx0, cur));
  13417. inpL = ggml_add(ctx0, cur, inpL);
  13418. }
  13419. cur = inpL;
  13420. cur = ggml_cont(ctx0, ggml_transpose(ctx0, cur));
  13421. cur = build_norm(cur,
  13422. model.output_norm,
  13423. model.output_norm_b,
  13424. LLM_NORM, -1);
  13425. // lm_head
  13426. cur = build_lora_mm(model.output, cur);
  13427. cur = ggml_add(ctx0, cur, model.output_b);
  13428. cb(cur, "result_embd", -1);
  13429. res->t_embd = cur;
  13430. ggml_build_forward_expand(gf, cur);
  13431. }
  13432. };
  13433. struct llm_build_plm : public llm_graph_context {
  13434. llm_build_plm(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  13435. const float kq_scale = 1.0f/sqrtf(float(hparams.n_embd_head_k));
  13436. const uint32_t n_embd_head_qk_rope = hparams.n_rot;
  13437. const uint32_t n_embd_head_qk_nope = hparams.n_embd_head_k - hparams.n_rot;
  13438. const uint32_t kv_lora_rank = hparams.n_lora_kv;
  13439. ggml_tensor * cur;
  13440. ggml_tensor * inpL;
  13441. // {n_embd, n_tokens}
  13442. inpL = build_inp_embd(model.tok_embd);
  13443. // inp_pos - contains the positions
  13444. ggml_tensor * inp_pos = build_inp_pos();
  13445. auto * inp_attn = build_attn_inp_kv();
  13446. ggml_tensor * inp_out_ids = build_inp_out_ids();
  13447. for (int il = 0; il < n_layer; ++il) {
  13448. ggml_tensor * inpSA = inpL;
  13449. // norm
  13450. cur = build_norm(inpL,
  13451. model.layers[il].attn_norm, NULL,
  13452. LLM_NORM_RMS, il);
  13453. cb(cur, "attn_norm", il);
  13454. // self_attention
  13455. {
  13456. ggml_tensor * q = NULL;
  13457. q = ggml_mul_mat(ctx0, model.layers[il].wq, cur);
  13458. cb(q, "q", il);
  13459. // split into {n_head * n_embd_head_qk_nope, n_tokens}
  13460. ggml_tensor * q_nope = ggml_view_3d(ctx0, q, n_embd_head_qk_nope, n_head, n_tokens,
  13461. ggml_row_size(q->type, hparams.n_embd_head_k),
  13462. ggml_row_size(q->type, hparams.n_embd_head_k * n_head),
  13463. 0);
  13464. cb(q_nope, "q_nope", il);
  13465. // and {n_head * n_embd_head_qk_rope, n_tokens}
  13466. ggml_tensor * q_pe = ggml_view_3d(ctx0, q, n_embd_head_qk_rope, n_head, n_tokens,
  13467. ggml_row_size(q->type, hparams.n_embd_head_k),
  13468. ggml_row_size(q->type, hparams.n_embd_head_k * n_head),
  13469. ggml_row_size(q->type, n_embd_head_qk_nope));
  13470. cb(q_pe, "q_pe", il);
  13471. // {n_embd, kv_lora_rank + n_embd_head_qk_rope} * {n_embd, n_tokens} -> {kv_lora_rank + n_embd_head_qk_rope, n_tokens}
  13472. ggml_tensor * kv_pe_compresseed = ggml_mul_mat(ctx0, model.layers[il].wkv_a_mqa, cur);
  13473. cb(kv_pe_compresseed, "kv_pe_compresseed", il);
  13474. // split into {kv_lora_rank, n_tokens}
  13475. ggml_tensor * kv_compressed = ggml_view_2d(ctx0, kv_pe_compresseed, kv_lora_rank, n_tokens,
  13476. kv_pe_compresseed->nb[1],
  13477. 0);
  13478. cb(kv_compressed, "kv_compressed", il);
  13479. // and {n_embd_head_qk_rope, n_tokens}
  13480. ggml_tensor * k_pe = ggml_view_3d(ctx0, kv_pe_compresseed, n_embd_head_qk_rope, 1, n_tokens,
  13481. kv_pe_compresseed->nb[1],
  13482. kv_pe_compresseed->nb[1],
  13483. ggml_row_size(kv_pe_compresseed->type, kv_lora_rank));
  13484. cb(k_pe, "k_pe", il);
  13485. kv_compressed = build_norm(kv_compressed,
  13486. model.layers[il].attn_kv_a_norm, NULL,
  13487. LLM_NORM_RMS, il);
  13488. cb(kv_compressed, "kv_compressed", il);
  13489. // {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}
  13490. ggml_tensor * kv = ggml_mul_mat(ctx0, model.layers[il].wkv_b, kv_compressed);
  13491. cb(kv, "kv", il);
  13492. // split into {n_head * n_embd_head_qk_nope, n_tokens}
  13493. ggml_tensor * k_nope = ggml_view_3d(ctx0, kv, n_embd_head_qk_nope, n_head, n_tokens,
  13494. ggml_row_size(kv->type, n_embd_head_qk_nope + hparams.n_embd_head_v),
  13495. ggml_row_size(kv->type, n_head * (n_embd_head_qk_nope + hparams.n_embd_head_v)),
  13496. 0);
  13497. cb(k_nope, "k_nope", il);
  13498. // and {n_head * n_embd_head_v, n_tokens}
  13499. ggml_tensor * v_states = ggml_view_3d(ctx0, kv, hparams.n_embd_head_v, n_head, n_tokens,
  13500. ggml_row_size(kv->type, (n_embd_head_qk_nope + hparams.n_embd_head_v)),
  13501. ggml_row_size(kv->type, (n_embd_head_qk_nope + hparams.n_embd_head_v)*n_head),
  13502. ggml_row_size(kv->type, (n_embd_head_qk_nope)));
  13503. cb(v_states, "v_states", il);
  13504. v_states = ggml_cont(ctx0, v_states);
  13505. cb(v_states, "v_states", il);
  13506. v_states = ggml_view_2d(ctx0, v_states, hparams.n_embd_head_v * n_head, n_tokens,
  13507. ggml_row_size(kv->type, hparams.n_embd_head_v * n_head),
  13508. 0);
  13509. cb(v_states, "v_states", il);
  13510. q_pe = ggml_rope_ext(
  13511. ctx0, q_pe, inp_pos, nullptr,
  13512. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  13513. ext_factor, attn_factor, beta_fast, beta_slow
  13514. );
  13515. cb(q_pe, "q_pe", il);
  13516. // shared RoPE key
  13517. k_pe = ggml_rope_ext(
  13518. ctx0, k_pe, inp_pos, nullptr,
  13519. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  13520. ext_factor, attn_factor, beta_fast, beta_slow
  13521. );
  13522. cb(k_pe, "k_pe", il);
  13523. ggml_tensor * q_states = ggml_concat(ctx0, q_nope, q_pe, 0);
  13524. cb(q_states, "q_states", il);
  13525. ggml_tensor * k_states = ggml_concat(ctx0, k_nope, ggml_repeat(ctx0, k_pe, q_pe), 0);
  13526. cb(k_states, "k_states", il);
  13527. cur = build_attn(inp_attn,
  13528. model.layers[il].wo, NULL,
  13529. q_states, k_states, v_states, nullptr, nullptr, nullptr, kq_scale, il);
  13530. }
  13531. if (il == n_layer - 1 && inp_out_ids) {
  13532. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  13533. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  13534. }
  13535. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  13536. cb(ffn_inp, "ffn_inp", il);
  13537. cur = build_norm(ffn_inp,
  13538. model.layers[il].ffn_norm, NULL,
  13539. LLM_NORM_RMS, il);
  13540. cb(cur, "ffn_norm", il);
  13541. cur = build_ffn(cur,
  13542. model.layers[il].ffn_up, NULL, NULL,
  13543. NULL, NULL, NULL,
  13544. model.layers[il].ffn_down, NULL, NULL,
  13545. NULL,
  13546. LLM_FFN_RELU_SQR, LLM_FFN_SEQ, il);
  13547. cb(cur, "ffn_out", il);
  13548. cur = ggml_add(ctx0, cur, ffn_inp);
  13549. cur = build_cvec(cur, il);
  13550. cb(cur, "l_out", il);
  13551. // input for next layer
  13552. inpL = cur;
  13553. }
  13554. cur = inpL;
  13555. cur = build_norm(cur,
  13556. model.output_norm, NULL,
  13557. LLM_NORM_RMS, -1);
  13558. cb(cur, "result_norm", -1);
  13559. res->t_embd = cur;
  13560. cur = build_lora_mm(model.output, cur);
  13561. cb(cur, "result_output", -1);
  13562. res->t_logits = cur;
  13563. ggml_build_forward_expand(gf, cur);
  13564. }
  13565. };
  13566. struct llm_build_bailingmoe : public llm_graph_context {
  13567. llm_build_bailingmoe(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  13568. ggml_tensor * cur;
  13569. ggml_tensor * inpL;
  13570. inpL = build_inp_embd(model.tok_embd);
  13571. // inp_pos - contains the positions
  13572. ggml_tensor * inp_pos = build_inp_pos();
  13573. auto * inp_attn = build_attn_inp_kv();
  13574. ggml_tensor * inp_out_ids = build_inp_out_ids();
  13575. for (int il = 0; il < n_layer; ++il) {
  13576. ggml_tensor * inpSA = inpL;
  13577. // norm
  13578. cur = build_norm(inpL,
  13579. model.layers[il].attn_norm, NULL,
  13580. LLM_NORM_RMS, il);
  13581. cb(cur, "attn_norm", il);
  13582. // self-attention
  13583. {
  13584. // rope freq factors for llama3; may return nullptr for llama2 and other models
  13585. ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
  13586. // compute Q and K and RoPE them
  13587. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  13588. cb(Qcur, "Qcur", il);
  13589. if (model.layers[il].bq) {
  13590. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  13591. cb(Qcur, "Qcur", il);
  13592. }
  13593. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  13594. cb(Kcur, "Kcur", il);
  13595. if (model.layers[il].bk) {
  13596. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  13597. cb(Kcur, "Kcur", il);
  13598. }
  13599. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  13600. cb(Vcur, "Vcur", il);
  13601. if (model.layers[il].bv) {
  13602. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  13603. cb(Vcur, "Vcur", il);
  13604. }
  13605. Qcur = ggml_reshape_3d(ctx0, Qcur, n_rot, n_head, n_tokens);
  13606. Kcur = ggml_reshape_3d(ctx0, Kcur, n_rot, n_head_kv, n_tokens);
  13607. Vcur = ggml_reshape_3d(ctx0, Vcur, n_rot, n_head_kv, n_tokens);
  13608. Qcur = ggml_rope_ext(
  13609. ctx0, Qcur, inp_pos, rope_factors,
  13610. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  13611. ext_factor, attn_factor, beta_fast, beta_slow
  13612. );
  13613. Kcur = ggml_rope_ext(
  13614. ctx0, Kcur, inp_pos, rope_factors,
  13615. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  13616. ext_factor, attn_factor, beta_fast, beta_slow
  13617. );
  13618. cb(Qcur, "Qcur", il);
  13619. cb(Kcur, "Kcur", il);
  13620. cb(Vcur, "Vcur", il);
  13621. cur = build_attn(inp_attn,
  13622. model.layers[il].wo, model.layers[il].bo,
  13623. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_rot)), il);
  13624. }
  13625. if (il == n_layer - 1 && inp_out_ids) {
  13626. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  13627. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  13628. }
  13629. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  13630. cb(ffn_inp, "ffn_inp", il);
  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. ggml_tensor * moe_out =
  13636. build_moe_ffn(cur,
  13637. model.layers[il].ffn_gate_inp,
  13638. model.layers[il].ffn_up_exps,
  13639. model.layers[il].ffn_gate_exps,
  13640. model.layers[il].ffn_down_exps,
  13641. nullptr,
  13642. n_expert, n_expert_used,
  13643. LLM_FFN_SILU, hparams.expert_weights_norm,
  13644. false, hparams.expert_weights_scale,
  13645. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  13646. il);
  13647. cb(moe_out, "ffn_moe_out", il);
  13648. // FFN shared expert
  13649. {
  13650. ggml_tensor * ffn_shexp = build_ffn(cur,
  13651. model.layers[il].ffn_up_shexp, NULL, NULL,
  13652. model.layers[il].ffn_gate_shexp, NULL, NULL,
  13653. model.layers[il].ffn_down_shexp, NULL, NULL,
  13654. NULL,
  13655. LLM_FFN_SILU, LLM_FFN_PAR, il);
  13656. cb(ffn_shexp, "ffn_shexp", il);
  13657. cur = ggml_add(ctx0, moe_out, ffn_shexp);
  13658. cb(cur, "ffn_out", il);
  13659. }
  13660. cur = ggml_add(ctx0, cur, ffn_inp);
  13661. cur = build_cvec(cur, il);
  13662. cb(cur, "l_out", il);
  13663. // input for next layer
  13664. inpL = cur;
  13665. }
  13666. cur = inpL;
  13667. cur = build_norm(cur,
  13668. model.output_norm, NULL,
  13669. LLM_NORM_RMS, -1);
  13670. cb(cur, "result_norm", -1);
  13671. res->t_embd = cur;
  13672. // lm_head
  13673. cur = build_lora_mm(model.output, cur);
  13674. cb(cur, "result_output", -1);
  13675. res->t_logits = cur;
  13676. ggml_build_forward_expand(gf, cur);
  13677. }
  13678. };
  13679. struct llm_build_bailingmoe2 : public llm_graph_context {
  13680. llm_build_bailingmoe2(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  13681. const int64_t n_embd_head = hparams.n_embd_head_v;
  13682. const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  13683. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  13684. ggml_tensor * cur;
  13685. ggml_tensor * inpL;
  13686. inpL = build_inp_embd(model.tok_embd);
  13687. // inp_pos - contains the positions
  13688. ggml_tensor * inp_pos = build_inp_pos();
  13689. auto * inp_attn = build_attn_inp_kv();
  13690. ggml_tensor * inp_out_ids = build_inp_out_ids();
  13691. const int n_transformer_layers = n_layer - hparams.nextn_predict_layers;
  13692. for (int il = 0; il < n_transformer_layers; ++il) {
  13693. ggml_tensor * inpSA = inpL;
  13694. // norm
  13695. cur = build_norm(inpL,
  13696. model.layers[il].attn_norm, NULL,
  13697. LLM_NORM_RMS, il);
  13698. cb(cur, "attn_norm", il);
  13699. // self_attention
  13700. {
  13701. cur = build_lora_mm(model.layers[il].wqkv, cur);
  13702. cb(cur, "wqkv", il);
  13703. 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));
  13704. 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));
  13705. 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));
  13706. Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);
  13707. cb(Qcur, "Qcur_normed", il);
  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 = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);
  13714. cb(Kcur, "Kcur_normed", il);
  13715. Kcur = ggml_rope_ext(
  13716. ctx0, Kcur, inp_pos, nullptr,
  13717. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  13718. ext_factor, attn_factor, beta_fast, beta_slow
  13719. );
  13720. cb(Qcur, "Qcur", il);
  13721. cb(Kcur, "Kcur", il);
  13722. cb(Vcur, "Vcur", il);
  13723. cur = build_attn(inp_attn,
  13724. model.layers[il].wo, model.layers[il].bo,
  13725. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  13726. }
  13727. if (il == n_transformer_layers - 1 && inp_out_ids) {
  13728. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  13729. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  13730. }
  13731. ggml_tensor * sa_out = ggml_add(ctx0, cur, inpSA);
  13732. cb(sa_out, "sa_out", il);
  13733. // MoE branch
  13734. cur = build_norm(sa_out,
  13735. model.layers[il].ffn_norm, NULL,
  13736. LLM_NORM_RMS, il);
  13737. cb(cur, "ffn_norm", il);
  13738. if (static_cast<uint32_t>(il) < hparams.n_layer_dense_lead) {
  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. ggml_tensor * moe_out =
  13748. build_moe_ffn(cur,
  13749. model.layers[il].ffn_gate_inp,
  13750. model.layers[il].ffn_up_exps,
  13751. model.layers[il].ffn_gate_exps,
  13752. model.layers[il].ffn_down_exps,
  13753. model.layers[il].ffn_exp_probs_b,
  13754. n_expert, n_expert_used,
  13755. LLM_FFN_SILU, hparams.expert_weights_norm,
  13756. true, hparams.expert_weights_scale,
  13757. (llama_expert_gating_func_type) hparams.expert_gating_func,
  13758. il);
  13759. cb(moe_out, "ffn_moe_out", il);
  13760. {
  13761. ggml_tensor * ffn_shexp = build_ffn(cur,
  13762. model.layers[il].ffn_up_shexp, NULL, NULL,
  13763. model.layers[il].ffn_gate_shexp, NULL, NULL,
  13764. model.layers[il].ffn_down_shexp, NULL, NULL,
  13765. NULL,
  13766. LLM_FFN_SILU, LLM_FFN_PAR, il);
  13767. cb(ffn_shexp, "ffn_shexp", il);
  13768. cur = ggml_add(ctx0, moe_out, ffn_shexp);
  13769. cb(cur, "ffn_out", il);
  13770. }
  13771. }
  13772. cur = ggml_add(ctx0, cur, sa_out);
  13773. cur = build_cvec(cur, il);
  13774. cb(cur, "l_out", il);
  13775. // input for next layer
  13776. inpL = cur;
  13777. }
  13778. cur = inpL;
  13779. cur = build_norm(cur,
  13780. model.output_norm, NULL,
  13781. LLM_NORM_RMS, -1);
  13782. cb(cur, "result_norm", -1);
  13783. res->t_embd = cur;
  13784. // lm_head
  13785. cur = build_lora_mm(model.output, cur);
  13786. cb(cur, "result_output", -1);
  13787. res->t_logits = cur;
  13788. ggml_build_forward_expand(gf, cur);
  13789. }
  13790. };
  13791. struct llm_build_dots1 : public llm_graph_context {
  13792. llm_build_dots1(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  13793. const int64_t n_embd_head = hparams.n_embd_head_v;
  13794. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  13795. GGML_ASSERT(n_embd_head == hparams.n_rot);
  13796. ggml_tensor * cur;
  13797. ggml_tensor * inpL;
  13798. inpL = build_inp_embd(model.tok_embd);
  13799. // inp_pos - contains the positions
  13800. ggml_tensor * inp_pos = build_inp_pos();
  13801. auto * inp_attn = build_attn_inp_kv();
  13802. ggml_tensor * inp_out_ids = build_inp_out_ids();
  13803. for (int il = 0; il < n_layer; ++il) {
  13804. ggml_tensor * inpSA = inpL;
  13805. // norm
  13806. cur = build_norm(inpL,
  13807. model.layers[il].attn_norm, NULL,
  13808. LLM_NORM_RMS, il);
  13809. cb(cur, "attn_norm", il);
  13810. // self_attention
  13811. {
  13812. // compute Q and K and RoPE them
  13813. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  13814. cb(Qcur, "Qcur", il);
  13815. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  13816. cb(Kcur, "Kcur", il);
  13817. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  13818. cb(Vcur, "Vcur", il);
  13819. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  13820. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  13821. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  13822. Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);
  13823. cb(Qcur, "Qcur_normed", il);
  13824. Qcur = ggml_rope_ext(
  13825. ctx0, Qcur, inp_pos, nullptr,
  13826. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  13827. ext_factor, attn_factor, beta_fast, beta_slow
  13828. );
  13829. Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);
  13830. cb(Kcur, "Kcur_normed", il);
  13831. Kcur = ggml_rope_ext(
  13832. ctx0, Kcur, inp_pos, nullptr,
  13833. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  13834. ext_factor, attn_factor, beta_fast, beta_slow
  13835. );
  13836. cb(Qcur, "Qcur", il);
  13837. cb(Kcur, "Kcur", il);
  13838. cb(Vcur, "Vcur", il);
  13839. cur = build_attn(inp_attn,
  13840. model.layers[il].wo, model.layers[il].bo,
  13841. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  13842. }
  13843. if (il == n_layer - 1 && inp_out_ids) {
  13844. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  13845. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  13846. }
  13847. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  13848. cb(ffn_inp, "ffn_inp", il);
  13849. // MoE branch
  13850. cur = build_norm(ffn_inp,
  13851. model.layers[il].ffn_norm, NULL,
  13852. LLM_NORM_RMS, il);
  13853. cb(cur, "ffn_norm", il);
  13854. if ((uint32_t) il < hparams.n_layer_dense_lead) {
  13855. cur = build_ffn(cur,
  13856. model.layers[il].ffn_up, NULL, NULL,
  13857. model.layers[il].ffn_gate, NULL, NULL,
  13858. model.layers[il].ffn_down, NULL, NULL,
  13859. NULL,
  13860. LLM_FFN_SILU, LLM_FFN_PAR, il);
  13861. cb(cur, "ffn_out", il);
  13862. } else {
  13863. ggml_tensor * moe_out =
  13864. build_moe_ffn(cur,
  13865. model.layers[il].ffn_gate_inp,
  13866. model.layers[il].ffn_up_exps,
  13867. model.layers[il].ffn_gate_exps,
  13868. model.layers[il].ffn_down_exps,
  13869. model.layers[il].ffn_exp_probs_b,
  13870. n_expert, n_expert_used,
  13871. LLM_FFN_SILU, hparams.expert_weights_norm,
  13872. true, hparams.expert_weights_scale,
  13873. (llama_expert_gating_func_type) hparams.expert_gating_func,
  13874. il);
  13875. cb(moe_out, "ffn_moe_out", il);
  13876. {
  13877. ggml_tensor * ffn_shexp = build_ffn(cur,
  13878. model.layers[il].ffn_up_shexp, NULL, NULL,
  13879. model.layers[il].ffn_gate_shexp, NULL, NULL,
  13880. model.layers[il].ffn_down_shexp, NULL, NULL,
  13881. NULL,
  13882. LLM_FFN_SILU, LLM_FFN_PAR, il);
  13883. cb(ffn_shexp, "ffn_shexp", il);
  13884. cur = ggml_add(ctx0, moe_out, ffn_shexp);
  13885. cb(cur, "ffn_out", il);
  13886. }
  13887. }
  13888. cur = ggml_add(ctx0, cur, ffn_inp);
  13889. cur = build_cvec(cur, il);
  13890. cb(cur, "l_out", il);
  13891. // input for next layer
  13892. inpL = cur;
  13893. }
  13894. cur = inpL;
  13895. cur = build_norm(cur,
  13896. model.output_norm, NULL,
  13897. LLM_NORM_RMS, -1);
  13898. cb(cur, "result_norm", -1);
  13899. res->t_embd = cur;
  13900. // lm_head
  13901. cur = build_lora_mm(model.output, cur);
  13902. cb(cur, "result_output", -1);
  13903. res->t_logits = cur;
  13904. ggml_build_forward_expand(gf, cur);
  13905. }
  13906. };
  13907. struct llm_build_ernie4_5 : public llm_graph_context {
  13908. llm_build_ernie4_5(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  13909. const int64_t n_embd_head = hparams.n_embd_head_v;
  13910. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  13911. GGML_ASSERT(n_embd_head == hparams.n_rot);
  13912. ggml_tensor * cur;
  13913. ggml_tensor * inpL;
  13914. inpL = build_inp_embd(model.tok_embd);
  13915. // inp_pos - contains the positions
  13916. ggml_tensor * inp_pos = build_inp_pos();
  13917. auto * inp_attn = build_attn_inp_kv();
  13918. for (int il = 0; il < n_layer; ++il) {
  13919. ggml_tensor * inpSA = inpL;
  13920. // norm
  13921. {
  13922. cur = build_norm(inpL,
  13923. model.layers[il].attn_norm, NULL,
  13924. LLM_NORM_RMS, il);
  13925. cb(cur, "attn_norm", il);
  13926. }
  13927. // self-attention
  13928. {
  13929. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  13930. cb(Qcur, "Qcur", il);
  13931. if (model.layers[il].bq) {
  13932. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  13933. cb(Qcur, "Qcur", il);
  13934. }
  13935. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  13936. cb(Kcur, "Kcur", il);
  13937. if (model.layers[il].bk) {
  13938. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  13939. cb(Kcur, "Kcur", il);
  13940. }
  13941. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  13942. cb(Vcur, "Vcur", il);
  13943. if (model.layers[il].bv) {
  13944. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  13945. cb(Vcur, "Vcur", il);
  13946. }
  13947. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  13948. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  13949. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  13950. Qcur = ggml_rope_ext(
  13951. ctx0, Qcur, inp_pos, nullptr,
  13952. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  13953. ext_factor, attn_factor, beta_fast, beta_slow
  13954. );
  13955. Kcur = ggml_rope_ext(
  13956. ctx0, Kcur, inp_pos, nullptr,
  13957. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  13958. ext_factor, attn_factor, beta_fast, beta_slow
  13959. );
  13960. cb(Qcur, "Qcur", il);
  13961. cb(Kcur, "Kcur", il);
  13962. cb(Vcur, "Vcur", il);
  13963. cur = build_attn(inp_attn,
  13964. model.layers[il].wo, NULL,
  13965. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  13966. }
  13967. if (il == n_layer - 1) {
  13968. // skip computing output for unused tokens
  13969. ggml_tensor * inp_out_ids = build_inp_out_ids();
  13970. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  13971. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  13972. }
  13973. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  13974. cb(ffn_inp, "ffn_inp", il);
  13975. // feed-forward network
  13976. {
  13977. cur = build_norm(ffn_inp,
  13978. model.layers[il].ffn_norm, NULL,
  13979. LLM_NORM_RMS, il);
  13980. cb(cur, "ffn_norm", il);
  13981. cur = build_ffn(cur,
  13982. model.layers[il].ffn_up, NULL, NULL,
  13983. model.layers[il].ffn_gate, NULL, NULL,
  13984. model.layers[il].ffn_down, NULL, NULL,
  13985. NULL,
  13986. LLM_FFN_SILU, LLM_FFN_PAR, il);
  13987. cb(cur, "ffn_out", il);
  13988. }
  13989. cur = ggml_add(ctx0, cur, ffn_inp);
  13990. cur = build_cvec(cur, il);
  13991. cb(cur, "l_out", il);
  13992. // input for next layer
  13993. inpL = cur;
  13994. }
  13995. cur = inpL;
  13996. cur = build_norm(cur,
  13997. model.output_norm, NULL,
  13998. LLM_NORM_RMS, -1);
  13999. cb(cur, "result_norm", -1);
  14000. res->t_embd = cur;
  14001. // lm_head
  14002. cur = build_lora_mm(model.output, cur);
  14003. cb(cur, "result_output", -1);
  14004. res->t_logits = cur;
  14005. ggml_build_forward_expand(gf, cur);
  14006. }
  14007. };
  14008. struct llm_build_ernie4_5_moe : public llm_graph_context {
  14009. llm_build_ernie4_5_moe(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  14010. const int64_t n_embd_head = hparams.n_embd_head_v;
  14011. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  14012. GGML_ASSERT(n_embd_head == hparams.n_rot);
  14013. ggml_tensor * cur;
  14014. ggml_tensor * inpL;
  14015. inpL = build_inp_embd(model.tok_embd);
  14016. // inp_pos - contains the positions
  14017. ggml_tensor * inp_pos = build_inp_pos();
  14018. auto * inp_attn = build_attn_inp_kv();
  14019. ggml_tensor * inp_out_ids = build_inp_out_ids();
  14020. GGML_ASSERT(hparams.n_moe_layer_step > 0 && "Ernie 4.5 MoE requires n_moe_layer_step > 0");
  14021. for (int il = 0; il < n_layer; ++il) {
  14022. ggml_tensor * inpSA = inpL;
  14023. // norm
  14024. {
  14025. cur = build_norm(inpL,
  14026. model.layers[il].attn_norm, NULL,
  14027. LLM_NORM_RMS, il);
  14028. cb(cur, "attn_norm", il);
  14029. }
  14030. // self-attention
  14031. {
  14032. // compute Q and K and RoPE them
  14033. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  14034. cb(Qcur, "Qcur", il);
  14035. if (model.layers[il].bq) {
  14036. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  14037. cb(Qcur, "Qcur", il);
  14038. }
  14039. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  14040. cb(Kcur, "Kcur", il);
  14041. if (model.layers[il].bk) {
  14042. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  14043. cb(Kcur, "Kcur", il);
  14044. }
  14045. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  14046. cb(Vcur, "Vcur", il);
  14047. if (model.layers[il].bv) {
  14048. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  14049. cb(Vcur, "Vcur", il);
  14050. }
  14051. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  14052. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  14053. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  14054. Qcur = ggml_rope_ext(
  14055. ctx0, Qcur, inp_pos, nullptr,
  14056. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  14057. ext_factor, attn_factor, beta_fast, beta_slow
  14058. );
  14059. Kcur = ggml_rope_ext(
  14060. ctx0, Kcur, inp_pos, nullptr,
  14061. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  14062. ext_factor, attn_factor, beta_fast, beta_slow
  14063. );
  14064. cb(Qcur, "Qcur", il);
  14065. cb(Kcur, "Kcur", il);
  14066. cb(Vcur, "Vcur", il);
  14067. cur = build_attn(inp_attn,
  14068. model.layers[il].wo, NULL,
  14069. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  14070. cb(cur, "attn_out", il);
  14071. }
  14072. if (il == n_layer - 1 && inp_out_ids) {
  14073. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  14074. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  14075. }
  14076. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  14077. cb(ffn_inp, "ffn_inp", il);
  14078. // feed-forward network
  14079. bool is_moe_layer = static_cast<uint32_t>(il) >= hparams.n_layer_dense_lead && (il + 1) % hparams.n_moe_layer_step == 0;
  14080. if (!is_moe_layer) {
  14081. cur = build_norm(ffn_inp,
  14082. model.layers[il].ffn_norm, NULL,
  14083. LLM_NORM_RMS, il);
  14084. cb(cur, "ffn_norm", il);
  14085. cur = build_ffn(cur,
  14086. model.layers[il].ffn_up, NULL, NULL,
  14087. model.layers[il].ffn_gate, NULL, NULL,
  14088. model.layers[il].ffn_down, NULL, NULL,
  14089. NULL,
  14090. LLM_FFN_SILU, LLM_FFN_PAR, il);
  14091. cb(cur, "ffn_out", il);
  14092. } else {
  14093. // MoE branch
  14094. cur = build_norm(ffn_inp,
  14095. model.layers[il].ffn_norm, NULL,
  14096. LLM_NORM_RMS, il);
  14097. cb(cur, "ffn_norm", il);
  14098. ggml_tensor * moe_out = build_moe_ffn(cur,
  14099. model.layers[il].ffn_gate_inp,
  14100. model.layers[il].ffn_up_exps,
  14101. model.layers[il].ffn_gate_exps,
  14102. model.layers[il].ffn_down_exps,
  14103. model.layers[il].ffn_exp_probs_b,
  14104. n_expert, n_expert_used,
  14105. LLM_FFN_SILU, true,
  14106. false, 0.0,
  14107. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  14108. il);
  14109. cb(moe_out, "ffn_moe_out", il);
  14110. // Shared expert (if present)
  14111. if (hparams.n_ff_shexp > 0) {
  14112. ggml_tensor * ffn_shexp = build_ffn(cur,
  14113. model.layers[il].ffn_up_shexp, NULL, NULL,
  14114. model.layers[il].ffn_gate_shexp, NULL, NULL,
  14115. model.layers[il].ffn_down_shexp, NULL, NULL,
  14116. NULL,
  14117. LLM_FFN_SILU, LLM_FFN_PAR, il);
  14118. cb(ffn_shexp, "ffn_shexp", il);
  14119. cur = ggml_add(ctx0, moe_out, ffn_shexp);
  14120. } else {
  14121. cur = moe_out;
  14122. }
  14123. cb(cur, "ffn_out", il);
  14124. }
  14125. cur = ggml_add(ctx0, cur, ffn_inp);
  14126. cb(cur, "ffn_out", il);
  14127. cur = build_cvec(cur, il);
  14128. cb(cur, "l_out", il);
  14129. // input for next layer
  14130. inpL = cur;
  14131. }
  14132. cur = inpL;
  14133. cur = build_norm(cur,
  14134. model.output_norm, NULL,
  14135. LLM_NORM_RMS, -1);
  14136. cb(cur, "result_norm", -1);
  14137. res->t_embd = cur;
  14138. // lm_head
  14139. cur = build_lora_mm(model.output, cur);
  14140. cb(cur, "result_output", -1);
  14141. res->t_logits = cur;
  14142. ggml_build_forward_expand(gf, cur);
  14143. }
  14144. };
  14145. struct llm_build_falcon_h1 : public llm_graph_context_mamba {
  14146. llm_build_falcon_h1(const llama_model & model, const llm_graph_params & params) : llm_graph_context_mamba(params) {
  14147. const int64_t n_embd_head = hparams.n_embd_head_v;
  14148. ggml_tensor * cur;
  14149. ggml_tensor * inpL;
  14150. inpL = build_inp_embd(model.tok_embd);
  14151. // inp_pos - contains the positions
  14152. ggml_tensor * inp_pos = build_inp_pos();
  14153. // Build the inputs in the recurrent & kv cache
  14154. auto * inp = build_inp_mem_hybrid();
  14155. const float kq_scale = hparams.f_attention_scale == 0.0f ? 1.0f/sqrtf(float(n_embd_head)) : hparams.f_attention_scale;
  14156. ggml_tensor * inp_out_ids = build_inp_out_ids();
  14157. for (int il = 0; il < n_layer; ++il) {
  14158. ggml_tensor * inpSA = inpL;
  14159. cur = build_norm(inpL,
  14160. model.layers[il].attn_norm, NULL,
  14161. LLM_NORM_RMS, il);
  14162. cb(cur, "attn_norm", il);
  14163. // self-attention
  14164. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  14165. cb(Qcur, "Qcur", il);
  14166. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  14167. cb(Kcur, "Kcur", il);
  14168. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  14169. cb(Vcur, "Vcur", il);
  14170. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  14171. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  14172. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  14173. Qcur = ggml_rope_ext(
  14174. ctx0, Qcur, inp_pos, nullptr,
  14175. n_rot, hparams.rope_type, n_ctx_orig, freq_base, freq_scale,
  14176. ext_factor, attn_factor, beta_fast, beta_slow);
  14177. Kcur = ggml_rope_ext(
  14178. ctx0, Kcur, inp_pos, nullptr,
  14179. n_rot, hparams.rope_type, n_ctx_orig, freq_base, freq_scale,
  14180. ext_factor, attn_factor, beta_fast, beta_slow
  14181. );
  14182. cb(Qcur, "Qcur-post-rope", il);
  14183. cb(Kcur, "Kcur-post-rope", il);
  14184. cb(Vcur, "Vcur-post-rope", il);
  14185. ggml_tensor * attn_out = build_attn(inp->get_attn(),
  14186. model.layers[il].wo, NULL,
  14187. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
  14188. cb(attn_out, "attn_out", il);
  14189. cur = build_norm(inpL,
  14190. model.layers[il].attn_norm, NULL,
  14191. LLM_NORM_RMS, il);
  14192. // Mamba2 layer
  14193. cb(cur, "ssm_in", il);
  14194. ggml_tensor * ssm_out = build_mamba2_layer(inp->get_recr(), cur, model, ubatch, il);
  14195. cb(ssm_out, "ssm_out", il);
  14196. // // Aggregation
  14197. cur = ggml_add(ctx0, attn_out, ssm_out);
  14198. inpSA = ggml_add(ctx0, cur, inpSA);
  14199. cb(cur, "layer_out", il);
  14200. if (il == n_layer - 1 && inp_out_ids) {
  14201. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  14202. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  14203. }
  14204. ggml_tensor * ffn_inp = inpSA;
  14205. cb(ffn_inp, "ffn_inp", il);
  14206. // feed-forward network
  14207. cur = build_norm(ffn_inp,
  14208. model.layers[il].ffn_norm, NULL,
  14209. LLM_NORM_RMS, il);
  14210. cb(cur, "ffn_norm", il);
  14211. cur = build_ffn(cur,
  14212. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  14213. model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, NULL,
  14214. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  14215. NULL,
  14216. LLM_FFN_SILU, LLM_FFN_PAR, il);
  14217. cb(cur, "ffn_out", il);
  14218. cur = ggml_add(ctx0, cur, inpSA);
  14219. cur = build_cvec(cur, il);
  14220. cb(cur, "l_out", il);
  14221. // input for next layer
  14222. inpL = cur;
  14223. }
  14224. cur = inpL;
  14225. cur = build_norm(cur,
  14226. model.output_norm, NULL,
  14227. LLM_NORM_RMS, -1);
  14228. cb(cur, "result_norm", -1);
  14229. res->t_embd = cur;
  14230. // lm_head
  14231. cur = build_lora_mm(model.output, cur);
  14232. cb(cur, "result_output", -1);
  14233. res->t_logits = cur;
  14234. ggml_build_forward_expand(gf, cur);
  14235. }
  14236. };
  14237. struct llm_build_plamo2 : public llm_graph_context_mamba {
  14238. llm_build_plamo2(const llama_model & model, const llm_graph_params & params) : llm_graph_context_mamba(params) {
  14239. ggml_tensor * cur;
  14240. ggml_tensor * inpL;
  14241. // {n_embd, n_tokens}
  14242. inpL = build_inp_embd(model.tok_embd);
  14243. cb(inpL, "embedding_output", -1);
  14244. ggml_tensor * inp_pos = build_inp_pos();
  14245. auto * inp_hybrid = build_inp_mem_hybrid();
  14246. ggml_tensor * inp_out_ids = build_inp_out_ids();
  14247. for (int il = 0; il < n_layer; ++il) {
  14248. ggml_tensor * residual = inpL;
  14249. // ggml_graph_add_node(gf, model.layers[il].attn_norm);
  14250. // cb(model.layers[il].attn_norm, "attn_norm", il);
  14251. // pre_mixer_norm
  14252. cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
  14253. // check if this layer is Mamba or Attention
  14254. bool is_mamba_layer = hparams.is_recurrent(il);
  14255. if (is_mamba_layer) {
  14256. // PLaMo-2 Mamba layer
  14257. cur = build_plamo2_mamba_layer(inp_hybrid->get_recr(), cur, model, ubatch, il);
  14258. } else {
  14259. // PLaMo-2 Attention layer
  14260. cur = build_plamo2_attn_layer(inp_hybrid->get_attn(), inp_pos, cur, model, il);
  14261. }
  14262. // post_mixer_norm
  14263. cur = build_norm(cur, model.layers[il].attn_post_norm, NULL, LLM_NORM_RMS, il);
  14264. cb(cur, "attn_post_norm", il);
  14265. // residual connection
  14266. cur = ggml_add(ctx0, cur, residual);
  14267. cb(cur, "attn_residual", il);
  14268. residual = cur;
  14269. // pre-ffn norm
  14270. cur = build_norm(cur, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);
  14271. cb(cur, "ffn_pre_norm", il);
  14272. // feed-forward network
  14273. cur = build_ffn(cur,
  14274. model.layers[il].ffn_up, NULL, NULL,
  14275. NULL, NULL, NULL,
  14276. model.layers[il].ffn_down, NULL, NULL,
  14277. NULL,
  14278. LLM_FFN_SWIGLU, LLM_FFN_SEQ, il);
  14279. cb(cur, "ffn_out", il);
  14280. // post ffn norm
  14281. cur = build_norm(cur, model.layers[il].ffn_post_norm, NULL, LLM_NORM_RMS, il);
  14282. cb(cur, "ffn_post_norm", il);
  14283. if (il == n_layer - 1 && inp_out_ids) {
  14284. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  14285. residual = ggml_get_rows(ctx0, residual, inp_out_ids);
  14286. }
  14287. // residual connection
  14288. cur = ggml_add(ctx0, cur, residual);
  14289. cb(cur, "ffn_residual", il);
  14290. inpL = cur;
  14291. }
  14292. cur = inpL;
  14293. // final norm
  14294. cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1);
  14295. cb(cur, "result_norm", -1);
  14296. res->t_embd = cur;
  14297. // lm_head
  14298. cur = build_lora_mm(model.output, cur);
  14299. cb(cur, "result_output", -1);
  14300. // Explicitly mark as output tensor to ensure proper backend assignment
  14301. ggml_set_output(cur);
  14302. res->t_logits = cur;
  14303. ggml_build_forward_expand(gf, cur);
  14304. }
  14305. private:
  14306. ggml_tensor * build_plamo2_attn_layer(
  14307. llm_graph_input_attn_kv * inp,
  14308. ggml_tensor * inp_pos,
  14309. ggml_tensor * cur,
  14310. const llama_model & model,
  14311. int il) {
  14312. // self-attention
  14313. {
  14314. // PLaMo-2 uses combined QKV tensor
  14315. ggml_tensor * qkv = build_lora_mm(model.layers[il].wqkv, cur);
  14316. cb(qkv, "wqkv", il);
  14317. // split QKV tensor into Q, K, V
  14318. const int64_t n_embd_head_q = hparams.n_embd_head_k;
  14319. const int64_t n_embd_head_k = hparams.n_embd_head_k;
  14320. const int64_t n_embd_head_v = hparams.n_embd_head_v;
  14321. int32_t n_head = hparams.n_head(il);
  14322. int32_t n_head_kv = hparams.n_head_kv(il);
  14323. const int64_t q_offset = 0;
  14324. const int64_t k_offset = n_embd_head_q * n_head;
  14325. const int64_t v_offset = k_offset + n_embd_head_k * n_head_kv;
  14326. 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));
  14327. 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));
  14328. 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));
  14329. cb(Qcur, "Qcur", il);
  14330. cb(Kcur, "Kcur", il);
  14331. cb(Vcur, "Vcur", il);
  14332. Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);
  14333. cb(Qcur, "Qcur_normed", il);
  14334. Qcur = ggml_rope_ext(
  14335. ctx0, Qcur, inp_pos, nullptr,
  14336. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  14337. ext_factor, attn_factor, beta_fast, beta_slow
  14338. );
  14339. Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);
  14340. cb(Kcur, "Kcur_normed", il);
  14341. Kcur = ggml_rope_ext(
  14342. ctx0, Kcur, inp_pos, nullptr,
  14343. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  14344. ext_factor, attn_factor, beta_fast, beta_slow
  14345. );
  14346. cur = build_attn(inp,
  14347. model.layers[il].wo, NULL,
  14348. Qcur, Kcur, Vcur, NULL, NULL, NULL, 1.0f/sqrtf(float(n_embd_head_v)), il);
  14349. }
  14350. cb(cur, "attn_out", il);
  14351. return cur;
  14352. }
  14353. ggml_tensor * build_plamo2_mamba_layer(
  14354. llm_graph_input_rs * inp,
  14355. ggml_tensor * cur,
  14356. const llama_model & model,
  14357. const llama_ubatch & ubatch,
  14358. int il) {
  14359. const auto * mctx_cur = inp->mctx;
  14360. const auto kv_head = mctx_cur->get_head();
  14361. const int64_t d_conv = hparams.ssm_d_conv;
  14362. const int64_t d_inner = hparams.ssm_d_inner;
  14363. const int64_t d_state = hparams.ssm_d_state;
  14364. const int64_t n_heads = hparams.ssm_dt_rank;
  14365. const int64_t head_dim = d_inner / n_heads;
  14366. const int64_t n_group = hparams.ssm_n_group;
  14367. const int64_t n_seqs = ubatch.n_seqs;
  14368. const int64_t n_seq_tokens = ubatch.n_seq_tokens;
  14369. GGML_ASSERT(n_seqs != 0);
  14370. GGML_ASSERT(ubatch.equal_seqs());
  14371. GGML_ASSERT(ubatch.n_tokens == n_seq_tokens * n_seqs);
  14372. ggml_tensor * conv_states_all = mctx_cur->get_r_l(il);
  14373. ggml_tensor * ssm_states_all = mctx_cur->get_s_l(il);
  14374. ggml_tensor * conv = build_rs(inp, conv_states_all, hparams.n_embd_r(), n_seqs);
  14375. conv = ggml_reshape_3d(ctx0, conv, d_conv - 1, d_inner + 2*n_group*d_state, n_seqs);
  14376. // {n_embd, n_tokens} => {n_embd, n_seq_tokens, n_seqs}
  14377. cur = ggml_reshape_3d(ctx0, cur, cur->ne[0], n_seq_tokens, n_seqs);
  14378. // in_proj: {n_embd, 2*d_inner} @ {n_embd, n_seq_tokens, n_seqs} => {2*d_inner, n_seq_tokens, n_seqs}
  14379. ggml_tensor * zx = build_lora_mm(model.layers[il].ssm_in, cur);
  14380. cb(zx, "mamba_in_proj", il);
  14381. // {8192, 5, 1, 1} -> {8192, 1, 5, 1}
  14382. zx = ggml_permute(ctx0, zx, 0, 2, 1, 3);
  14383. zx = ggml_cont_4d(ctx0, zx, head_dim * 2, n_heads, n_seq_tokens, n_seqs);
  14384. cb(zx, "mamba_in_proj_out", il);
  14385. // split into z and x
  14386. // => {head_dim * n_heads, n_seq_tokens, n_seqs}
  14387. 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));
  14388. x = ggml_cont_3d(ctx0, x, head_dim * n_heads, n_seq_tokens, n_seqs);
  14389. // x = ggml_permute(ctx0, x, 0, 2, 1, 3);
  14390. cb(x, "mamba_x_split", il);
  14391. 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);
  14392. cb(z, "mamba_z_split", il);
  14393. // conv1d
  14394. {
  14395. // => {d_conv - 1 + n_seq_tokens, d_inner, n_seqs}
  14396. ggml_tensor * conv_x = ggml_concat(ctx0, conv, ggml_transpose(ctx0, x), 0);
  14397. cb(conv_x, "mamba_conv1d_input", il);
  14398. // copy last (d_conv - 1) columns back into the state cache
  14399. ggml_tensor * last_conv = ggml_view_3d(ctx0, conv_x, d_conv - 1, d_inner, n_seqs,
  14400. conv_x->nb[1], conv_x->nb[2], n_seq_tokens*(conv_x->nb[0]));
  14401. ggml_build_forward_expand(gf,
  14402. ggml_cpy(ctx0, last_conv,
  14403. ggml_view_1d(ctx0, conv_states_all,
  14404. (d_conv - 1)*(d_inner + 2*n_group*d_state)*(n_seqs),
  14405. kv_head*(d_conv - 1)*(d_inner + 2*n_group*d_state)*ggml_element_size(conv_states_all))));
  14406. cb(conv_states_all, "mamba_conv1d_state", il);
  14407. // 1D convolution
  14408. x = ggml_ssm_conv(ctx0, conv_x, model.layers[il].ssm_conv1d);
  14409. cb(x, "mamba_conv1d", il);
  14410. x = ggml_silu(ctx0, x);
  14411. cb(x, "mamba_conv1d_silu", il);
  14412. }
  14413. // SSM
  14414. {
  14415. // 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}
  14416. ggml_tensor * x_bcdt = build_lora_mm(model.layers[il].ssm_x, x);
  14417. cb(x_bcdt, "mamba_bcdt_proj", il);
  14418. // split into dt, B, C
  14419. const int64_t dt_dim = std::max(64, int(hparams.n_embd / 16));
  14420. 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);
  14421. 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);
  14422. 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));
  14423. cb(B, "mamba_B_raw", il);
  14424. cb(C, "mamba_C_raw", il);
  14425. cb(dt, "mamba_dt_raw", il);
  14426. // Apply RMS norm to dt, B, C (PLaMo-2 specific)
  14427. B = build_norm(B, model.layers[il].ssm_b_norm, NULL, LLM_NORM_RMS, il);
  14428. C = build_norm(C, model.layers[il].ssm_c_norm, NULL, LLM_NORM_RMS, il);
  14429. dt = build_norm(dt, model.layers[il].ssm_dt_norm, NULL, LLM_NORM_RMS, il);
  14430. cb(B, "mamba_B_normed", il);
  14431. cb(C, "mamba_C_normed", il);
  14432. cb(dt, "mamba_dt_normed", il);
  14433. // dt_proj: {dt_rank, d_inner} @ {dt_rank, n_seq_tokens, n_seqs} => {d_inner, n_seq_tokens, n_seqs}
  14434. dt = build_lora_mm(model.layers[il].ssm_dt, dt);
  14435. dt = ggml_add(ctx0, dt, model.layers[il].ssm_dt_b);
  14436. cb(dt, "mamba_dt_proj", il);
  14437. ggml_tensor * A = ggml_reshape_2d(ctx0, model.layers[il].ssm_a, 1, n_heads);
  14438. cb(A, "mamba_A", il);
  14439. 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);
  14440. 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);
  14441. 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);
  14442. // use the states and the indices provided by build_recurrent_state
  14443. // (this is necessary in order to properly use the states before they are overwritten,
  14444. // while avoiding to make unnecessary copies of the states)
  14445. auto get_ssm_rows = [&](ggml_context * ctx, ggml_tensor * states, ggml_tensor * ids) {
  14446. ggml_tensor * ssm = ggml_reshape_4d(ctx, states, d_state, head_dim, n_heads, mctx_cur->get_size());
  14447. // Custom operator to optimize the parallel associative scan
  14448. // as described in the Annex D of the Mamba paper.
  14449. // => {d_inner, n_seq_tokens, n_seqs} and {d_state, d_inner, n_seqs}
  14450. return ggml_ssm_scan(ctx, ssm, x, dt, A, B, C, ids);
  14451. };
  14452. ggml_tensor * y_ssm = build_rs(inp, ssm_states_all, hparams.n_embd_s(), ubatch.n_seqs, get_ssm_rows);
  14453. cb(y_ssm, "mamba_ssm_scan", il);
  14454. // store last states
  14455. ggml_build_forward_expand(gf,
  14456. ggml_cpy(ctx0,
  14457. 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)),
  14458. 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))));
  14459. cb(ssm_states_all, "mamba_ssm_states", il);
  14460. 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);
  14461. cb(y, "mamba_y_view", il);
  14462. // Add D parameter and apply gating with z
  14463. // {d_inner, n_seq_tokens, n_seqs} * {d_inner} => {d_inner, n_seq_tokens, n_seqs}
  14464. ggml_tensor * D = ggml_reshape_2d(ctx0, model.layers[il].ssm_d, 1, n_heads);
  14465. y = ggml_add(ctx0, y, ggml_mul(ctx0, x, D));
  14466. cb(y, "mamba_y_add_d", il);
  14467. y = ggml_swiglu_split(ctx0, ggml_cont(ctx0, z), y);
  14468. cb(y, "mamba_y_swiglu_z", il);
  14469. // out_proj: {d_inner, n_embd} @ {d_inner, n_seq_tokens, n_seqs} => {n_embd, n_seq_tokens, n_seqs}
  14470. y = ggml_view_3d(ctx0, y, head_dim * n_heads, n_seq_tokens, n_seqs, y->nb[2], y->nb[3], 0);
  14471. cur = build_lora_mm(model.layers[il].ssm_out, y);
  14472. cb(cur, "mamba_out_proj", il);
  14473. }
  14474. // {n_embd, n_seq_tokens, n_seqs} => {n_embd, n_tokens}
  14475. cur = ggml_reshape_2d(ctx0, cur, cur->ne[0], n_seq_tokens * n_seqs);
  14476. cb(cur, "mamba_out", il);
  14477. return cur;
  14478. }
  14479. };
  14480. struct llm_build_arcee : public llm_graph_context {
  14481. llm_build_arcee(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  14482. const int64_t n_embd_head = hparams.n_embd_head_v;
  14483. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  14484. GGML_ASSERT(n_embd_head == hparams.n_rot);
  14485. ggml_tensor * cur;
  14486. ggml_tensor * inpL;
  14487. inpL = build_inp_embd(model.tok_embd);
  14488. // inp_pos - contains the positions
  14489. ggml_tensor * inp_pos = build_inp_pos();
  14490. auto * inp_attn = build_attn_inp_kv();
  14491. const float kq_scale = hparams.f_attention_scale == 0.0f ? 1.0f/sqrtf(float(n_embd_head)) : hparams.f_attention_scale;
  14492. ggml_tensor * inp_out_ids = build_inp_out_ids();
  14493. for (int il = 0; il < n_layer; ++il) {
  14494. ggml_tensor * inpSA = inpL;
  14495. // norm
  14496. cur = build_norm(inpL,
  14497. model.layers[il].attn_norm, NULL,
  14498. LLM_NORM_RMS, il);
  14499. cb(cur, "attn_norm", il);
  14500. // self-attention
  14501. {
  14502. // rope freq factors for llama3; may return nullptr for llama2 and other models
  14503. ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
  14504. // compute Q and K and RoPE them
  14505. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  14506. cb(Qcur, "Qcur", il);
  14507. if (model.layers[il].bq) {
  14508. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  14509. cb(Qcur, "Qcur", il);
  14510. }
  14511. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  14512. cb(Kcur, "Kcur", il);
  14513. if (model.layers[il].bk) {
  14514. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  14515. cb(Kcur, "Kcur", il);
  14516. }
  14517. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  14518. cb(Vcur, "Vcur", il);
  14519. if (model.layers[il].bv) {
  14520. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  14521. cb(Vcur, "Vcur", il);
  14522. }
  14523. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  14524. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  14525. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  14526. Qcur = ggml_rope_ext(
  14527. ctx0, Qcur, inp_pos, rope_factors,
  14528. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  14529. ext_factor, attn_factor, beta_fast, beta_slow
  14530. );
  14531. Kcur = ggml_rope_ext(
  14532. ctx0, Kcur, inp_pos, rope_factors,
  14533. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  14534. ext_factor, attn_factor, beta_fast, beta_slow
  14535. );
  14536. cb(Qcur, "Qcur", il);
  14537. cb(Kcur, "Kcur", il);
  14538. cb(Vcur, "Vcur", il);
  14539. cur = build_attn(inp_attn,
  14540. model.layers[il].wo, model.layers[il].bo,
  14541. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
  14542. cb(cur, "attn_out", il);
  14543. }
  14544. if (il == n_layer - 1 && inp_out_ids) {
  14545. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  14546. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  14547. }
  14548. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  14549. cb(ffn_inp, "ffn_inp", il);
  14550. // feed-forward network
  14551. // ARCEE uses relu^2 instead of silu
  14552. cur = build_norm(ffn_inp,
  14553. model.layers[il].ffn_norm, NULL,
  14554. LLM_NORM_RMS, il);
  14555. cb(cur, "ffn_norm", il);
  14556. cur = build_ffn(cur,
  14557. model.layers[il].ffn_up, NULL, NULL,
  14558. NULL, NULL, NULL,
  14559. model.layers[il].ffn_down, NULL, NULL,
  14560. NULL,
  14561. LLM_FFN_RELU_SQR, LLM_FFN_SEQ, il);
  14562. cb(cur, "ffn_out", il);
  14563. cur = ggml_add(ctx0, cur, ffn_inp);
  14564. cb(cur, "ffn_out", il);
  14565. cur = build_cvec(cur, il);
  14566. cb(cur, "l_out", il);
  14567. // input for next layer
  14568. inpL = cur;
  14569. }
  14570. cur = inpL;
  14571. cur = build_norm(cur,
  14572. model.output_norm, NULL,
  14573. LLM_NORM_RMS, -1);
  14574. cb(cur, "result_norm", -1);
  14575. res->t_embd = cur;
  14576. // lm_head
  14577. cur = build_lora_mm(model.output, cur);
  14578. cb(cur, "result_output", -1);
  14579. res->t_logits = cur;
  14580. ggml_build_forward_expand(gf, cur);
  14581. }
  14582. };
  14583. struct llm_build_hunyuan_moe : public llm_graph_context {
  14584. llm_build_hunyuan_moe(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  14585. const int64_t n_embd_head = hparams.n_embd_head_v;
  14586. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  14587. GGML_ASSERT(n_embd_head == hparams.n_rot);
  14588. ggml_tensor * cur;
  14589. ggml_tensor * inpL;
  14590. inpL = build_inp_embd(model.tok_embd);
  14591. // inp_pos - contains the positions
  14592. ggml_tensor * inp_pos = build_inp_pos();
  14593. auto * inp_attn = build_attn_inp_kv();
  14594. const float kq_scale = 1.0f / sqrtf(float(n_embd_head));
  14595. ggml_tensor * inp_out_ids = build_inp_out_ids();
  14596. for (int il = 0; il < n_layer; ++il) {
  14597. ggml_tensor * inpSA = inpL;
  14598. // norm
  14599. cur = build_norm(inpL,
  14600. model.layers[il].attn_norm, NULL,
  14601. LLM_NORM_RMS, il);
  14602. cb(cur, "attn_norm", il);
  14603. // self-attention
  14604. {
  14605. // rope freq factors for llama3; may return nullptr for llama2 and other models
  14606. ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
  14607. // compute Q and K and RoPE them
  14608. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  14609. cb(Qcur, "Qcur", il);
  14610. if (model.layers[il].bq) {
  14611. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  14612. cb(Qcur, "Qcur", il);
  14613. }
  14614. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  14615. cb(Kcur, "Kcur", il);
  14616. if (model.layers[il].bk) {
  14617. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  14618. cb(Kcur, "Kcur", il);
  14619. }
  14620. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  14621. cb(Vcur, "Vcur", il);
  14622. if (model.layers[il].bv) {
  14623. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  14624. cb(Vcur, "Vcur", il);
  14625. }
  14626. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  14627. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  14628. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  14629. Qcur = ggml_rope_ext(
  14630. ctx0, Qcur, inp_pos, rope_factors,
  14631. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  14632. ext_factor, attn_factor, beta_fast, beta_slow
  14633. );
  14634. cb(Qcur, "Qcur", il);
  14635. cb(Kcur, "Kcur", il);
  14636. cb(Vcur, "Vcur", il);
  14637. Kcur = ggml_rope_ext(
  14638. ctx0, Kcur, inp_pos, rope_factors,
  14639. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  14640. ext_factor, attn_factor, beta_fast, beta_slow
  14641. );
  14642. Kcur = build_norm(Kcur,
  14643. model.layers[il].attn_k_norm, nullptr,
  14644. LLM_NORM_RMS, il);
  14645. cb(Kcur, "Kcur_norm", il);
  14646. Qcur = build_norm(Qcur,
  14647. model.layers[il].attn_q_norm, nullptr,
  14648. LLM_NORM_RMS, il);
  14649. cb(Qcur, "Qcur_norm", il);
  14650. cur = build_attn(inp_attn,
  14651. model.layers[il].wo, model.layers[il].bo,
  14652. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
  14653. cb(cur, "attn_out", il);
  14654. }
  14655. if (il == n_layer - 1 && inp_out_ids) {
  14656. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  14657. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  14658. }
  14659. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  14660. cb(ffn_inp, "ffn_inp", il);
  14661. cur = build_norm(ffn_inp,
  14662. model.layers[il].ffn_norm, NULL,
  14663. LLM_NORM_RMS, il);
  14664. cb(cur, "ffn_norm", il);
  14665. // feed-forward network (non-MoE)
  14666. ggml_tensor * cur_mlp = build_ffn(cur,
  14667. model.layers[il].ffn_up_shexp, NULL, NULL,
  14668. model.layers[il].ffn_gate_shexp, NULL, NULL,
  14669. model.layers[il].ffn_down_shexp, NULL, NULL,
  14670. NULL,
  14671. LLM_FFN_SILU, LLM_FFN_PAR, il);
  14672. cb(cur_mlp, "ffn_mlp", il);
  14673. // MoE branch
  14674. ggml_tensor * cur_moe = build_moe_ffn(cur,
  14675. model.layers[il].ffn_gate_inp,
  14676. model.layers[il].ffn_up_exps,
  14677. model.layers[il].ffn_gate_exps,
  14678. model.layers[il].ffn_down_exps,
  14679. nullptr,
  14680. n_expert, n_expert_used,
  14681. LLM_FFN_SILU,
  14682. true, // norm_topk_prob
  14683. false,
  14684. 0.0,
  14685. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  14686. il);
  14687. cb(cur_moe, "ffn_moe_out", il);
  14688. ggml_tensor * ffn_out = ggml_add(ctx0, cur_moe, cur_mlp);
  14689. cb(ffn_out, "ffn_out", il);
  14690. cur = ggml_add(ctx0, ffn_out, ffn_inp);
  14691. cur = build_cvec(cur, il);
  14692. cb(cur, "l_out", il);
  14693. // input for next layer
  14694. inpL = cur;
  14695. }
  14696. cur = inpL;
  14697. cur = build_norm(cur,
  14698. model.output_norm, NULL,
  14699. LLM_NORM_RMS, -1);
  14700. cb(cur, "result_norm", -1);
  14701. res->t_embd = cur;
  14702. // lm_head
  14703. cur = build_lora_mm(model.output, cur);
  14704. cb(cur, "result_output", -1);
  14705. res->t_logits = cur;
  14706. ggml_build_forward_expand(gf, cur);
  14707. }
  14708. };
  14709. struct llm_build_hunyuan_dense : public llm_graph_context {
  14710. llm_build_hunyuan_dense(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  14711. const int64_t n_embd_head = hparams.n_embd_head_v;
  14712. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  14713. GGML_ASSERT(n_embd_head == hparams.n_rot);
  14714. ggml_tensor * cur;
  14715. ggml_tensor * inpL;
  14716. inpL = build_inp_embd(model.tok_embd);
  14717. // inp_pos - contains the positions
  14718. ggml_tensor * inp_pos = build_inp_pos();
  14719. auto * inp_attn = build_attn_inp_kv();
  14720. const float kq_scale = 1.0f / sqrtf(float(n_embd_head));
  14721. ggml_tensor * inp_out_ids = build_inp_out_ids();
  14722. for (int il = 0; il < n_layer; ++il) {
  14723. ggml_tensor * inpSA = inpL;
  14724. // norm
  14725. cur = build_norm(inpL,
  14726. model.layers[il].attn_norm, NULL,
  14727. LLM_NORM_RMS, il);
  14728. cb(cur, "attn_norm", il);
  14729. // self-attention
  14730. {
  14731. // rope freq factors for llama3; may return nullptr for llama2 and other models
  14732. ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
  14733. // compute Q and K and RoPE them
  14734. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  14735. cb(Qcur, "Qcur", il);
  14736. if (model.layers[il].bq) {
  14737. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  14738. cb(Qcur, "Qcur", il);
  14739. }
  14740. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  14741. cb(Kcur, "Kcur", il);
  14742. if (model.layers[il].bk) {
  14743. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  14744. cb(Kcur, "Kcur", il);
  14745. }
  14746. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  14747. cb(Vcur, "Vcur", il);
  14748. if (model.layers[il].bv) {
  14749. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  14750. cb(Vcur, "Vcur", il);
  14751. }
  14752. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  14753. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  14754. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  14755. Qcur = ggml_rope_ext(
  14756. ctx0, Qcur, inp_pos, rope_factors,
  14757. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  14758. ext_factor, attn_factor, beta_fast, beta_slow
  14759. );
  14760. cb(Qcur, "Qcur", il);
  14761. cb(Kcur, "Kcur", il);
  14762. cb(Vcur, "Vcur", il);
  14763. Kcur = ggml_rope_ext(
  14764. ctx0, Kcur, inp_pos, rope_factors,
  14765. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  14766. ext_factor, attn_factor, beta_fast, beta_slow
  14767. );
  14768. Kcur = build_norm(Kcur,
  14769. model.layers[il].attn_k_norm, nullptr,
  14770. LLM_NORM_RMS, il);
  14771. cb(Kcur, "Kcur_norm", il);
  14772. Qcur = build_norm(Qcur,
  14773. model.layers[il].attn_q_norm, nullptr,
  14774. LLM_NORM_RMS, il);
  14775. cb(Qcur, "Qcur_norm", il);
  14776. cur = build_attn(inp_attn,
  14777. model.layers[il].wo, model.layers[il].bo,
  14778. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
  14779. cb(cur, "attn_out", il);
  14780. }
  14781. if (il == n_layer - 1 && inp_out_ids) {
  14782. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  14783. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  14784. }
  14785. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  14786. cb(ffn_inp, "ffn_inp", il);
  14787. cur = build_norm(ffn_inp,
  14788. model.layers[il].ffn_norm, NULL,
  14789. LLM_NORM_RMS, il);
  14790. cb(cur, "ffn_norm", il);
  14791. // feed-forward network (non-MoE)
  14792. ggml_tensor * cur_mlp = build_ffn(cur,
  14793. model.layers[il].ffn_up, NULL, NULL,
  14794. model.layers[il].ffn_gate, NULL, NULL,
  14795. model.layers[il].ffn_down, NULL, NULL,
  14796. NULL,
  14797. LLM_FFN_SILU, LLM_FFN_PAR, il);
  14798. cb(cur_mlp, "ffn_out", il);
  14799. cur = ggml_add(ctx0, cur_mlp, ffn_inp);
  14800. cur = build_cvec(cur, il);
  14801. cb(cur, "l_out", il);
  14802. // input for next layer
  14803. inpL = cur;
  14804. }
  14805. cur = inpL;
  14806. cur = build_norm(cur,
  14807. model.output_norm, NULL,
  14808. LLM_NORM_RMS, -1);
  14809. cb(cur, "result_norm", -1);
  14810. res->t_embd = cur;
  14811. // lm_head
  14812. cur = build_lora_mm(model.output, cur);
  14813. cb(cur, "result_output", -1);
  14814. res->t_logits = cur;
  14815. ggml_build_forward_expand(gf, cur);
  14816. }
  14817. };
  14818. struct llm_build_smollm3 : public llm_graph_context {
  14819. llm_build_smollm3(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  14820. const int64_t n_embd_head = hparams.n_embd_head_v;
  14821. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  14822. GGML_ASSERT(n_embd_head == hparams.n_rot);
  14823. ggml_tensor * cur;
  14824. ggml_tensor * inpL;
  14825. inpL = build_inp_embd(model.tok_embd);
  14826. // inp_pos - contains the positions
  14827. ggml_tensor * inp_pos = build_inp_pos();
  14828. auto * inp_attn = build_attn_inp_kv();
  14829. const float kq_scale = hparams.f_attention_scale == 0.0f ? 1.0f/sqrtf(float(n_embd_head)) : hparams.f_attention_scale;
  14830. ggml_tensor * inp_out_ids = build_inp_out_ids();
  14831. for (int il = 0; il < n_layer; ++il) {
  14832. ggml_tensor * inpSA = inpL;
  14833. const bool use_rope = (il + 1) % hparams.n_no_rope_layer_step != 0;
  14834. // norm
  14835. cur = build_norm(inpL,
  14836. model.layers[il].attn_norm, NULL,
  14837. LLM_NORM_RMS, il);
  14838. cb(cur, "attn_norm", il);
  14839. // self-attention
  14840. {
  14841. // compute Q and K and RoPE them
  14842. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  14843. cb(Qcur, "Qcur", il);
  14844. if (model.layers[il].bq) {
  14845. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  14846. cb(Qcur, "Qcur", il);
  14847. }
  14848. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  14849. cb(Kcur, "Kcur", il);
  14850. if (model.layers[il].bk) {
  14851. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  14852. cb(Kcur, "Kcur", il);
  14853. }
  14854. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  14855. cb(Vcur, "Vcur", il);
  14856. if (model.layers[il].bv) {
  14857. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  14858. cb(Vcur, "Vcur", il);
  14859. }
  14860. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  14861. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  14862. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  14863. if (use_rope) {
  14864. Qcur = ggml_rope_ext(
  14865. ctx0, Qcur, inp_pos, nullptr,
  14866. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  14867. ext_factor, attn_factor, beta_fast, beta_slow
  14868. );
  14869. Kcur = ggml_rope_ext(
  14870. ctx0, Kcur, inp_pos, nullptr,
  14871. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  14872. ext_factor, attn_factor, beta_fast, beta_slow
  14873. );
  14874. }
  14875. cb(Qcur, "Qcur", il);
  14876. cb(Kcur, "Kcur", il);
  14877. cb(Vcur, "Vcur", il);
  14878. cur = build_attn(inp_attn,
  14879. model.layers[il].wo, model.layers[il].bo,
  14880. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
  14881. cb(cur, "attn_out", il);
  14882. }
  14883. if (il == n_layer - 1 && inp_out_ids) {
  14884. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  14885. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  14886. }
  14887. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  14888. cb(ffn_inp, "ffn_inp", il);
  14889. // feed-forward network
  14890. {
  14891. cur = build_norm(ffn_inp,
  14892. model.layers[il].ffn_norm, NULL,
  14893. LLM_NORM_RMS, il);
  14894. cb(cur, "ffn_norm", il);
  14895. cur = build_ffn(cur,
  14896. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  14897. model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, NULL,
  14898. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  14899. NULL,
  14900. LLM_FFN_SILU, LLM_FFN_PAR, il);
  14901. cb(cur, "ffn_out", il);
  14902. }
  14903. cur = ggml_add(ctx0, cur, ffn_inp);
  14904. cb(cur, "ffn_out", il);
  14905. cur = build_cvec(cur, il);
  14906. cb(cur, "l_out", il);
  14907. // input for next layer
  14908. inpL = cur;
  14909. }
  14910. cur = inpL;
  14911. cur = build_norm(cur,
  14912. model.output_norm, NULL,
  14913. LLM_NORM_RMS, -1);
  14914. cb(cur, "result_norm", -1);
  14915. res->t_embd = cur;
  14916. // lm_head
  14917. cur = build_lora_mm(model.output, cur);
  14918. cb(cur, "result_output", -1);
  14919. res->t_logits = cur;
  14920. ggml_build_forward_expand(gf, cur);
  14921. }
  14922. };
  14923. struct llm_build_openai_moe_iswa : public llm_graph_context {
  14924. llm_build_openai_moe_iswa(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  14925. ggml_tensor * cur;
  14926. ggml_tensor * inpL;
  14927. inpL = build_inp_embd(model.tok_embd);
  14928. // inp_pos - contains the positions
  14929. ggml_tensor * inp_pos = build_inp_pos();
  14930. auto * inp_attn = build_attn_inp_kv_iswa();
  14931. for (int il = 0; il < n_layer; ++il) {
  14932. ggml_tensor * inpSA = inpL;
  14933. // norm
  14934. cur = build_norm(inpL,
  14935. model.layers[il].attn_norm, nullptr,
  14936. LLM_NORM_RMS, il);
  14937. cb(cur, "attn_norm", il);
  14938. // self-attention
  14939. {
  14940. // compute Q and K and RoPE them
  14941. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  14942. cb(Qcur, "Qcur", il);
  14943. if (model.layers[il].bq) {
  14944. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  14945. cb(Qcur, "Qcur", il);
  14946. }
  14947. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  14948. cb(Kcur, "Kcur", il);
  14949. if (model.layers[il].bk) {
  14950. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  14951. cb(Kcur, "Kcur", il);
  14952. }
  14953. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  14954. cb(Vcur, "Vcur", il);
  14955. if (model.layers[il].bv) {
  14956. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  14957. cb(Vcur, "Vcur", il);
  14958. }
  14959. Qcur = ggml_reshape_3d(ctx0, Qcur, n_rot, n_head, n_tokens);
  14960. Kcur = ggml_reshape_3d(ctx0, Kcur, n_rot, n_head_kv, n_tokens);
  14961. Vcur = ggml_reshape_3d(ctx0, Vcur, n_rot, n_head_kv, n_tokens);
  14962. Qcur = ggml_rope_ext(
  14963. ctx0, Qcur, inp_pos, nullptr,
  14964. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  14965. ext_factor, attn_factor, beta_fast, beta_slow
  14966. );
  14967. Kcur = ggml_rope_ext(
  14968. ctx0, Kcur, inp_pos, nullptr,
  14969. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  14970. ext_factor, attn_factor, beta_fast, beta_slow
  14971. );
  14972. cb(Qcur, "Qcur", il);
  14973. cb(Kcur, "Kcur", il);
  14974. cb(Vcur, "Vcur", il);
  14975. cur = build_attn(inp_attn,
  14976. model.layers[il].wo, model.layers[il].bo,
  14977. Qcur, Kcur, Vcur, nullptr, model.layers[il].attn_sinks, nullptr, 1.0f/sqrtf(float(n_rot)), il);
  14978. cb(cur, "attn_out", il);
  14979. }
  14980. if (il == n_layer - 1) {
  14981. // skip computing output for unused tokens
  14982. ggml_tensor * inp_out_ids = build_inp_out_ids();
  14983. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  14984. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  14985. }
  14986. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  14987. cb(ffn_inp, "ffn_inp", il);
  14988. cur = ffn_inp;
  14989. cur = build_norm(cur,
  14990. model.layers[il].attn_post_norm, nullptr,
  14991. LLM_NORM_RMS, il);
  14992. cb(cur, "attn_post_norm", il);
  14993. // MoE branch
  14994. cur = build_moe_ffn(cur,
  14995. model.layers[il].ffn_gate_inp, model.layers[il].ffn_gate_inp_b,
  14996. model.layers[il].ffn_up_exps, model.layers[il].ffn_up_exps_b,
  14997. model.layers[il].ffn_gate_exps, model.layers[il].ffn_gate_exps_b,
  14998. model.layers[il].ffn_down_exps, model.layers[il].ffn_down_exps_b,
  14999. nullptr,
  15000. n_expert, n_expert_used,
  15001. LLM_FFN_SWIGLU_OAI_MOE, false,
  15002. false, 0.0,
  15003. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX_WEIGHT,
  15004. il);
  15005. cb(cur, "ffn_moe_out", il);
  15006. cur = ggml_add(ctx0, cur, ffn_inp);
  15007. cur = build_cvec(cur, il);
  15008. cb(cur, "l_out", il);
  15009. // input for next layer
  15010. inpL = cur;
  15011. }
  15012. cur = inpL;
  15013. cur = build_norm(cur,
  15014. model.output_norm, NULL,
  15015. LLM_NORM_RMS, -1);
  15016. cb(cur, "result_norm", -1);
  15017. res->t_embd = cur;
  15018. // lm_head
  15019. cur = build_lora_mm(model.output, cur);
  15020. cb(cur, "result_output", -1);
  15021. res->t_logits = cur;
  15022. ggml_build_forward_expand(gf, cur);
  15023. }
  15024. };
  15025. struct llm_build_lfm2 : public llm_graph_context {
  15026. const llama_model & model;
  15027. llm_build_lfm2(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params), model(model) {
  15028. ggml_tensor * cur = build_inp_embd(model.tok_embd);
  15029. cb(cur, "model.embed_tokens", -1);
  15030. ggml_tensor * inp_pos = build_inp_pos();
  15031. auto * inp_hybrid = build_inp_mem_hybrid();
  15032. ggml_tensor * inp_out_ids = build_inp_out_ids();
  15033. for (int il = 0; il < n_layer; ++il) {
  15034. const bool is_moe_layer = il >= static_cast<int>(hparams.n_layer_dense_lead);
  15035. auto * prev_cur = cur;
  15036. cur = build_norm(cur, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
  15037. cb(cur, "model.layers.{}.operator_norm", il);
  15038. cur = hparams.is_recurrent(il) ?
  15039. build_shortconv_block(cur, inp_hybrid->get_recr(), il) :
  15040. build_attn_block(cur, inp_pos, inp_hybrid->get_attn(), il) ;
  15041. if (il == n_layer - 1 && inp_out_ids) {
  15042. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  15043. prev_cur = ggml_get_rows(ctx0, prev_cur, inp_out_ids);
  15044. }
  15045. cur = ggml_add(ctx0, prev_cur, cur);
  15046. auto * ffn_norm_out = build_norm(cur, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);
  15047. cb(ffn_norm_out, "model.layers.{}.ffn_norm", il);
  15048. ggml_tensor * ffn_out = is_moe_layer ?
  15049. build_moe_feed_forward(ffn_norm_out, il) :
  15050. build_dense_feed_forward(ffn_norm_out, il);
  15051. cb(ffn_norm_out, "model.layers.{}.ffn_out", il);
  15052. cur = ggml_add(ctx0, cur, ffn_out);
  15053. }
  15054. cur = build_norm(cur, model.tok_norm, NULL, LLM_NORM_RMS, -1);
  15055. cb(cur, "model.embedding_norm", -1);
  15056. res->t_embd = cur;
  15057. cur = build_lora_mm(model.output, cur);
  15058. cb(cur, "lm_head", -1);
  15059. res->t_logits = cur;
  15060. ggml_build_forward_expand(gf, cur);
  15061. }
  15062. ggml_tensor * build_moe_feed_forward(ggml_tensor * cur,
  15063. int il) const {
  15064. return build_moe_ffn(cur,
  15065. model.layers[il].ffn_gate_inp,
  15066. model.layers[il].ffn_up_exps,
  15067. model.layers[il].ffn_gate_exps,
  15068. model.layers[il].ffn_down_exps,
  15069. model.layers[il].ffn_exp_probs_b,
  15070. n_expert, n_expert_used,
  15071. LLM_FFN_SILU, true,
  15072. false, 0.0,
  15073. static_cast<llama_expert_gating_func_type>(hparams.expert_gating_func),
  15074. il);
  15075. }
  15076. ggml_tensor * build_dense_feed_forward(ggml_tensor * cur,
  15077. int il) const {
  15078. GGML_ASSERT(!model.layers[il].ffn_up_b);
  15079. GGML_ASSERT(!model.layers[il].ffn_gate_b);
  15080. GGML_ASSERT(!model.layers[il].ffn_down_b);
  15081. return build_ffn(cur,
  15082. model.layers[il].ffn_up, NULL, NULL,
  15083. model.layers[il].ffn_gate, NULL, NULL,
  15084. model.layers[il].ffn_down, NULL, NULL,
  15085. NULL,
  15086. LLM_FFN_SILU, LLM_FFN_PAR, il);
  15087. }
  15088. ggml_tensor * build_attn_block(ggml_tensor * cur,
  15089. ggml_tensor * inp_pos,
  15090. llm_graph_input_attn_kv * inp_attn,
  15091. int il) const {
  15092. GGML_ASSERT(hparams.n_embd_v_gqa(il) == hparams.n_embd_k_gqa(il));
  15093. auto const n_embd_head = hparams.n_embd_head_v;
  15094. auto const n_head_kv = hparams.n_head_kv(il);
  15095. auto * q = build_lora_mm(model.layers[il].wq, cur);
  15096. cb(q, "model.layers.{}.self_attn.q_proj", il);
  15097. auto * k = build_lora_mm(model.layers[il].wk, cur);
  15098. cb(k, "model.layers.{}.self_attn.k_proj", il);
  15099. auto * v = build_lora_mm(model.layers[il].wv, cur);
  15100. cb(v, "model.layers.{}.self_attn.v_proj", il);
  15101. q = ggml_reshape_3d(ctx0, q, n_embd_head, n_head, n_tokens);
  15102. k = ggml_reshape_3d(ctx0, k, n_embd_head, n_head_kv, n_tokens);
  15103. v = ggml_reshape_3d(ctx0, v, n_embd_head, n_head_kv, n_tokens);
  15104. // qk norm
  15105. q = build_norm(q, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);
  15106. cb(q, "model.layers.{}.self_attn.q_layernorm", il);
  15107. k = build_norm(k, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);
  15108. cb(k, "model.layers.{}.self_attn.k_layernorm", il);
  15109. // RoPE
  15110. q = ggml_rope_ext(
  15111. ctx0, q, inp_pos, nullptr,
  15112. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  15113. ext_factor, attn_factor, beta_fast, beta_slow
  15114. );
  15115. k = ggml_rope_ext(
  15116. ctx0, k, inp_pos, nullptr,
  15117. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  15118. ext_factor, attn_factor, beta_fast, beta_slow
  15119. );
  15120. cur = build_attn(inp_attn, model.layers[il].wo, NULL,
  15121. q, k, v, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  15122. cb(cur, "model.layers.{}.self_attn.out_proj", il);
  15123. return cur;
  15124. }
  15125. ggml_tensor * build_shortconv_block(ggml_tensor * cur,
  15126. llm_graph_input_rs * inp_recr,
  15127. int il) {
  15128. const auto * mctx_cur = static_cast<const llama_memory_hybrid_context *>(mctx)->get_recr();
  15129. const uint32_t kv_head = mctx_cur->get_head();
  15130. const int64_t n_seq_tokens = ubatch.n_seq_tokens;
  15131. const int64_t n_seqs = ubatch.n_seqs;
  15132. GGML_ASSERT(n_seqs != 0);
  15133. GGML_ASSERT(ubatch.equal_seqs());
  15134. GGML_ASSERT(ubatch.n_tokens == n_seq_tokens * n_seqs);
  15135. GGML_ASSERT(hparams.n_shortconv_l_cache > 1);
  15136. const uint32_t d_conv = hparams.n_shortconv_l_cache - 1;
  15137. // {n_embd, n_tokens} => {n_embd, n_seq_tokens, n_seqs}
  15138. cur = ggml_reshape_3d(ctx0, cur, cur->ne[0], n_seq_tokens, n_seqs);
  15139. auto * bcx = build_lora_mm(model.layers[il].shortconv.in_proj, cur);
  15140. cb(bcx, "model.layers.{}.conv.in_proj", il);
  15141. constexpr auto n_chunks = 3;
  15142. GGML_ASSERT(bcx->ne[0] % n_chunks == 0);
  15143. auto const chunk_size = bcx->ne[0] / n_chunks;
  15144. 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));
  15145. 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));
  15146. 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));
  15147. auto * bx = ggml_transpose(ctx0, ggml_mul(ctx0, b, x));
  15148. // read conv state
  15149. auto * conv_state = mctx_cur->get_r_l(il);
  15150. auto * conv_rs = build_rs(inp_recr, conv_state, hparams.n_embd_r(), n_seqs);
  15151. auto * conv = ggml_reshape_3d(ctx0, conv_rs, d_conv, hparams.n_embd, n_seqs);
  15152. bx = ggml_concat(ctx0, conv, bx, 0);
  15153. GGML_ASSERT(bx->ne[0] > conv->ne[0]);
  15154. // last d_conv columns is a new conv state
  15155. 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));
  15156. GGML_ASSERT(ggml_are_same_shape(conv, new_conv));
  15157. // write new conv conv state
  15158. ggml_build_forward_expand(
  15159. gf,
  15160. ggml_cpy(
  15161. ctx0,
  15162. new_conv,
  15163. ggml_view_1d(
  15164. ctx0,
  15165. conv_state,
  15166. ggml_nelements(new_conv),
  15167. kv_head*d_conv*n_embd*ggml_element_size(new_conv)
  15168. )
  15169. )
  15170. );
  15171. auto * conv_kernel = model.layers[il].shortconv.conv;
  15172. auto * conv_out = ggml_ssm_conv(ctx0, bx, conv_kernel);
  15173. cb(conv_out, "model.layers.{}.conv.conv", il);
  15174. auto * y = ggml_mul(ctx0, c, conv_out);
  15175. y = build_lora_mm(model.layers[il].shortconv.out_proj, y);
  15176. cb(y, "model.layers.{}.conv.out_proj", il);
  15177. // {n_embd, n_seq_tokens, n_seqs} => {n_embd, n_tokens}
  15178. y = ggml_reshape_2d(ctx0, y, y->ne[0], n_seq_tokens * n_seqs);
  15179. return y;
  15180. }
  15181. };
  15182. struct llm_build_seed_oss : public llm_graph_context {
  15183. llm_build_seed_oss(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  15184. const int64_t n_embd_head = hparams.n_embd_head_v;
  15185. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  15186. GGML_ASSERT(n_embd_head == hparams.n_rot);
  15187. ggml_tensor * cur;
  15188. ggml_tensor * inpL;
  15189. inpL = build_inp_embd(model.tok_embd);
  15190. // inp_pos - contains the positions
  15191. ggml_tensor * inp_pos = build_inp_pos();
  15192. auto * inp_attn = build_attn_inp_kv();
  15193. const float kq_scale = hparams.f_attention_scale == 0.0f ? 1.0f/sqrtf(float(n_embd_head)) : hparams.f_attention_scale;
  15194. ggml_tensor * inp_out_ids = build_inp_out_ids();
  15195. for (int il = 0; il < n_layer; ++il) {
  15196. ggml_tensor * inpSA = inpL;
  15197. // norm
  15198. cur = build_norm(inpL,
  15199. model.layers[il].attn_norm, NULL,
  15200. LLM_NORM_RMS, il);
  15201. cb(cur, "attn_norm", il);
  15202. // self-attention
  15203. {
  15204. // compute Q and K and RoPE them
  15205. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  15206. cb(Qcur, "Qcur", il);
  15207. if (model.layers[il].bq) {
  15208. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  15209. cb(Qcur, "Qcur", il);
  15210. }
  15211. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  15212. cb(Kcur, "Kcur", il);
  15213. if (model.layers[il].bk) {
  15214. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  15215. cb(Kcur, "Kcur", il);
  15216. }
  15217. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  15218. cb(Vcur, "Vcur", il);
  15219. if (model.layers[il].bv) {
  15220. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  15221. cb(Vcur, "Vcur", il);
  15222. }
  15223. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  15224. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  15225. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  15226. Qcur = ggml_rope_ext(
  15227. ctx0, Qcur, inp_pos, nullptr,
  15228. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  15229. ext_factor, attn_factor, beta_fast, beta_slow
  15230. );
  15231. Kcur = ggml_rope_ext(
  15232. ctx0, Kcur, inp_pos, nullptr,
  15233. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  15234. ext_factor, attn_factor, beta_fast, beta_slow
  15235. );
  15236. cb(Qcur, "Qcur", il);
  15237. cb(Kcur, "Kcur", il);
  15238. cb(Vcur, "Vcur", il);
  15239. cur = build_attn(inp_attn,
  15240. model.layers[il].wo, model.layers[il].bo,
  15241. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
  15242. cb(cur, "attn_out", il);
  15243. }
  15244. if (il == n_layer - 1 && inp_out_ids) {
  15245. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  15246. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  15247. }
  15248. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  15249. cb(ffn_inp, "ffn_inp", il);
  15250. // feed-forward network
  15251. cur = build_norm(ffn_inp,
  15252. model.layers[il].attn_post_norm, NULL,
  15253. LLM_NORM_RMS, il);
  15254. cb(cur, "attn_post_norm", il);
  15255. cur = build_ffn(cur,
  15256. model.layers[il].ffn_up, NULL, NULL,
  15257. model.layers[il].ffn_gate, NULL, NULL,
  15258. model.layers[il].ffn_down, NULL, NULL,
  15259. NULL,
  15260. LLM_FFN_SILU, LLM_FFN_PAR, il);
  15261. cb(cur, "ffn_out", il);
  15262. cur = ggml_add(ctx0, cur, ffn_inp);
  15263. cb(cur, "ffn_out", il);
  15264. cur = build_cvec(cur, il);
  15265. cb(cur, "l_out", il);
  15266. // input for next layer
  15267. inpL = cur;
  15268. }
  15269. cur = inpL;
  15270. cur = build_norm(cur,
  15271. model.output_norm, NULL,
  15272. LLM_NORM_RMS, -1);
  15273. cb(cur, "result_norm", -1);
  15274. res->t_embd = cur;
  15275. // lm_head
  15276. cur = build_lora_mm(model.output, cur);
  15277. cb(cur, "result_output", -1);
  15278. res->t_logits = cur;
  15279. ggml_build_forward_expand(gf, cur);
  15280. }
  15281. };
  15282. template <bool iswa>
  15283. struct llm_build_smallthinker : public llm_graph_context{
  15284. llm_build_smallthinker(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params){
  15285. const int64_t n_embd_head = hparams.n_embd_head_v;
  15286. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  15287. GGML_ASSERT(n_embd_head == hparams.n_rot);
  15288. ggml_tensor * cur;
  15289. ggml_tensor * inpL;
  15290. inpL = build_inp_embd(model.tok_embd);
  15291. // inp_pos - contains the positions
  15292. ggml_tensor * inp_pos = build_inp_pos();
  15293. using inp_attn_type = std::conditional_t<iswa, llm_graph_input_attn_kv_iswa, llm_graph_input_attn_kv>;
  15294. inp_attn_type * inp_attn = nullptr;
  15295. if constexpr (iswa) {
  15296. inp_attn = build_attn_inp_kv_iswa();
  15297. } else {
  15298. inp_attn = build_attn_inp_kv();
  15299. }
  15300. ggml_tensor * inp_out_ids = build_inp_out_ids();
  15301. for (int il = 0; il < n_layer; ++il) {
  15302. ggml_tensor * inpSA = inpL;
  15303. ggml_tensor * probs = nullptr;
  15304. probs = build_lora_mm(model.layers[il].ffn_gate_inp, inpL); // [n_expert, n_tokens]
  15305. cb(probs, "ffn_moe_logits", il);
  15306. // norm
  15307. cur = build_norm(inpL,model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
  15308. cb(cur, "attn_norm", il);
  15309. // self_attention
  15310. {
  15311. // compute Q and K and RoPE them
  15312. struct ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  15313. cb(Qcur, "Qcur", il);
  15314. struct ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  15315. cb(Kcur, "Kcur", il);
  15316. struct ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  15317. cb(Vcur, "Vcur", il);
  15318. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  15319. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  15320. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  15321. if (hparams.n_no_rope_layer_step == n_layer || il % hparams.n_no_rope_layer_step != 0) {
  15322. Qcur = ggml_rope_ext(ctx0, Qcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  15323. ext_factor, attn_factor, beta_fast, beta_slow);
  15324. Kcur = ggml_rope_ext(ctx0, Kcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  15325. ext_factor, attn_factor, beta_fast, beta_slow);
  15326. }
  15327. cb(Qcur, "Qcur", il);
  15328. cb(Kcur, "Kcur", il);
  15329. cur = build_attn(inp_attn,
  15330. model.layers[il].wo, model.layers[il].bo,
  15331. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f / sqrtf(float(n_embd_head)), il);
  15332. }
  15333. if (il == n_layer - 1 && inp_out_ids) {
  15334. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  15335. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  15336. probs = ggml_get_rows(ctx0, probs, inp_out_ids);
  15337. }
  15338. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  15339. cb(ffn_inp, "ffn_inp", il);
  15340. // MoE branch
  15341. cur = build_norm(ffn_inp, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);
  15342. cb(cur, "ffn_norm", il);
  15343. ggml_tensor * ffn_out =
  15344. build_moe_ffn(cur,
  15345. nullptr,
  15346. model.layers[il].ffn_up_exps,
  15347. model.layers[il].ffn_gate_exps,
  15348. model.layers[il].ffn_down_exps,
  15349. nullptr,
  15350. n_expert, n_expert_used,
  15351. LLM_FFN_RELU, true,
  15352. false, 0.0,
  15353. static_cast<llama_expert_gating_func_type>(hparams.expert_gating_func),
  15354. il, probs);
  15355. cb(ffn_out, "ffn_out", il);
  15356. cur = ffn_out;
  15357. cur = ggml_add(ctx0, cur, ffn_inp);
  15358. cur = build_cvec(cur, il);
  15359. cb(cur, "l_out", il);
  15360. // input for next layer
  15361. inpL = cur;
  15362. }
  15363. cur = inpL;
  15364. cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1);
  15365. cb(cur, "result_norm", -1);
  15366. res->t_embd = cur;
  15367. // lm_head
  15368. cur = build_lora_mm(model.output, cur);
  15369. cb(cur, "result_output", -1);
  15370. res->t_logits = cur;
  15371. ggml_build_forward_expand(gf, cur);
  15372. }
  15373. };
  15374. struct llm_build_grovemoe : public llm_graph_context {
  15375. llm_build_grovemoe(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  15376. const int64_t n_embd_head = hparams.n_embd_head_v;
  15377. const int64_t n_chunk_expert = n_expert / hparams.n_group_experts;
  15378. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  15379. GGML_ASSERT(n_embd_head == hparams.n_rot);
  15380. ggml_tensor * cur;
  15381. ggml_tensor * inpL;
  15382. inpL = build_inp_embd(model.tok_embd);
  15383. // inp_pos - contains the positions
  15384. ggml_tensor * inp_pos = build_inp_pos();
  15385. auto * inp_attn = build_attn_inp_kv();
  15386. ggml_tensor * inp_out_ids = build_inp_out_ids();
  15387. for (int il = 0; il < n_layer; ++il) {
  15388. ggml_tensor * inpSA = inpL;
  15389. // norm
  15390. cur = build_norm(inpL,
  15391. model.layers[il].attn_norm, NULL,
  15392. LLM_NORM_RMS, il);
  15393. cb(cur, "attn_norm", il);
  15394. // self_attention
  15395. {
  15396. // compute Q and K and RoPE them
  15397. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  15398. cb(Qcur, "Qcur", il);
  15399. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  15400. cb(Kcur, "Kcur", il);
  15401. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  15402. cb(Vcur, "Vcur", il);
  15403. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  15404. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  15405. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  15406. Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);
  15407. cb(Qcur, "Qcur_normed", il);
  15408. Qcur = ggml_rope_ext(
  15409. ctx0, Qcur, inp_pos, nullptr,
  15410. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  15411. ext_factor, attn_factor, beta_fast, beta_slow
  15412. );
  15413. Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);
  15414. cb(Kcur, "Kcur_normed", il);
  15415. Kcur = ggml_rope_ext(
  15416. ctx0, Kcur, inp_pos, nullptr,
  15417. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  15418. ext_factor, attn_factor, beta_fast, beta_slow
  15419. );
  15420. cb(Qcur, "Qcur", il);
  15421. cb(Kcur, "Kcur", il);
  15422. cb(Vcur, "Vcur", il);
  15423. cur = build_attn(inp_attn,
  15424. model.layers[il].wo, model.layers[il].bo,
  15425. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  15426. }
  15427. if (il == n_layer - 1 && inp_out_ids) {
  15428. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  15429. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  15430. }
  15431. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  15432. cb(ffn_inp, "ffn_inp", il);
  15433. // MoE branch
  15434. cur = build_norm(ffn_inp,
  15435. model.layers[il].ffn_norm, NULL,
  15436. LLM_NORM_RMS, il);
  15437. cb(cur, "ffn_norm", il);
  15438. ggml_tensor * probs = build_lora_mm(model.layers[il].ffn_gate_inp, cur); // [n_expert, n_tokens]
  15439. cb(probs, "ffn_moe_logits", il);
  15440. ggml_tensor * moe_out =
  15441. build_moe_ffn(cur,
  15442. nullptr,
  15443. model.layers[il].ffn_up_exps,
  15444. model.layers[il].ffn_gate_exps,
  15445. model.layers[il].ffn_down_exps,
  15446. nullptr,
  15447. n_expert, n_expert_used,
  15448. LLM_FFN_SILU, true,
  15449. false, 0.0,
  15450. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  15451. il, probs);
  15452. cb(moe_out, "ffn_moe_out", il);
  15453. cur = moe_out;
  15454. // TODO: Only do the expert selection and weights once
  15455. moe_out =
  15456. build_moe_ffn(cur,
  15457. nullptr,
  15458. model.layers[il].ffn_up_chexps,
  15459. model.layers[il].ffn_gate_chexps,
  15460. model.layers[il].ffn_down_chexps,
  15461. nullptr,
  15462. n_chunk_expert, n_expert_used > n_chunk_expert ? n_chunk_expert : n_expert_used,
  15463. LLM_FFN_SILU, true,
  15464. false, 0.0,
  15465. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  15466. il, probs);
  15467. cb(moe_out, "ffn_adj_moe_out", il);
  15468. cur = ggml_add(ctx0, cur, ggml_scale(ctx0, moe_out, hparams.expert_group_scale));
  15469. cb(cur, "ffn_final_moe_out", il);
  15470. cur = ggml_add(ctx0, cur, ffn_inp);
  15471. cur = build_cvec(cur, il);
  15472. cb(cur, "l_out", il);
  15473. // input for next layer
  15474. inpL = cur;
  15475. }
  15476. cur = inpL;
  15477. cur = build_norm(cur,
  15478. model.output_norm, NULL,
  15479. LLM_NORM_RMS, -1);
  15480. cb(cur, "result_norm", -1);
  15481. res->t_embd = cur;
  15482. // lm_head
  15483. cur = build_lora_mm(model.output, cur);
  15484. cb(cur, "result_output", -1);
  15485. res->t_logits = cur;
  15486. ggml_build_forward_expand(gf, cur);
  15487. }
  15488. };
  15489. struct llm_build_apertus : public llm_graph_context {
  15490. llm_build_apertus(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  15491. const int64_t n_embd_head = hparams.n_embd_head_v;
  15492. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  15493. GGML_ASSERT(n_embd_head == hparams.n_rot);
  15494. ggml_tensor * cur;
  15495. ggml_tensor * inpL;
  15496. inpL = build_inp_embd(model.tok_embd);
  15497. ggml_tensor * inp_pos = build_inp_pos();
  15498. auto * inp_attn = build_attn_inp_kv();
  15499. const float kq_scale = hparams.f_attention_scale == 0.0f ? 1.0f / sqrtf(float(n_embd_head)) : hparams.f_attention_scale;
  15500. ggml_tensor * inp_out_ids = build_inp_out_ids();
  15501. for (int il = 0; il < n_layer; ++il) {
  15502. ggml_tensor * inpSA = inpL;
  15503. cur = build_norm(inpL,
  15504. model.layers[il].attn_norm, nullptr,
  15505. LLM_NORM_RMS, il);
  15506. cb(cur, "attn_norm", il);
  15507. // self-attention
  15508. {
  15509. ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
  15510. // compute Q and K and RoPE them
  15511. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  15512. cb(Qcur, "Qcur", il);
  15513. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  15514. cb(Kcur, "Kcur", il);
  15515. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  15516. cb(Vcur, "Vcur", il);
  15517. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  15518. Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);
  15519. cb(Qcur, "Qcur_normed", il);
  15520. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  15521. Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);
  15522. cb(Kcur, "Kcur_normed", il);
  15523. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  15524. Qcur = ggml_rope_ext(
  15525. ctx0, Qcur, inp_pos, rope_factors,
  15526. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  15527. ext_factor, attn_factor, beta_fast, beta_slow
  15528. );
  15529. Kcur = ggml_rope_ext(
  15530. ctx0, Kcur, inp_pos, rope_factors,
  15531. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  15532. ext_factor, attn_factor, beta_fast, beta_slow
  15533. );
  15534. cb(Qcur, "Qcur_pos", il);
  15535. cb(Kcur, "Kcur_pos", il);
  15536. cb(Vcur, "Vcur_pos", il);
  15537. cur = build_attn(inp_attn,
  15538. model.layers[il].wo, model.layers[il].bo,
  15539. Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
  15540. cb(cur, "attn_out", il);
  15541. }
  15542. if (il == n_layer - 1 && inp_out_ids) {
  15543. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  15544. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  15545. }
  15546. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  15547. cb(ffn_inp, "ffn_inp", il);
  15548. // feed-forward network with xIELU activation
  15549. {
  15550. cur = build_norm(ffn_inp,
  15551. model.layers[il].ffn_norm, nullptr,
  15552. LLM_NORM_RMS, il);
  15553. cb(cur, "ffn_norm", il);
  15554. // Up projection
  15555. ggml_tensor * up = build_lora_mm(model.layers[il].ffn_up, cur);
  15556. cb(up, "ffn_up", il);
  15557. float alpha_n_val = hparams.xielu_alpha_n[il];
  15558. float alpha_p_val = hparams.xielu_alpha_p[il];
  15559. float beta_val = hparams.xielu_beta[il];
  15560. float eps_val = hparams.xielu_eps[il];
  15561. // Apply xIELU activation
  15562. ggml_tensor * activated = ggml_xielu(ctx0, up, alpha_n_val, alpha_p_val, beta_val, eps_val);
  15563. cb(activated, "ffn_xielu", il);
  15564. // Down projection
  15565. cur = build_lora_mm(model.layers[il].ffn_down, activated);
  15566. cb(cur, "ffn_down", il);
  15567. }
  15568. cur = ggml_add(ctx0, cur, ffn_inp);
  15569. cb(cur, "ffn_out", il);
  15570. cur = build_cvec(cur, il);
  15571. cb(cur, "l_out", il);
  15572. // input for next layer
  15573. inpL = cur;
  15574. }
  15575. cur = inpL;
  15576. cur = build_norm(cur,
  15577. model.output_norm, nullptr,
  15578. LLM_NORM_RMS, -1);
  15579. cb(cur, "result_norm", -1);
  15580. res->t_embd = cur;
  15581. // lm_head
  15582. cur = build_lora_mm(model.output, cur);
  15583. cb(cur, "result_output", -1);
  15584. res->t_logits = cur;
  15585. ggml_build_forward_expand(gf, cur);
  15586. }
  15587. };
  15588. struct llm_build_cogvlm : public llm_graph_context {
  15589. llm_build_cogvlm(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  15590. const int64_t n_embd_head = hparams.n_embd_head_v;
  15591. float kq_scale = 1.0f / sqrtf(float(n_embd_head));
  15592. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  15593. GGML_ASSERT(n_embd_head == hparams.n_rot);
  15594. ggml_tensor * inpL, * cur;
  15595. inpL = build_inp_embd(model.tok_embd);
  15596. ggml_tensor * inp_pos = build_inp_pos();
  15597. auto * inp_attn = build_attn_inp_kv();
  15598. // check ubatch to see if we have input tokens (text)
  15599. // or an input embedding vector (image)
  15600. bool is_text;
  15601. if (ubatch.token) {
  15602. is_text = true;
  15603. } else {
  15604. is_text = false;
  15605. }
  15606. for (int il = 0; il < n_layer; ++il) {
  15607. // get either the text or image weight tensors
  15608. ggml_tensor * wqkv, * wo;
  15609. ggml_tensor * ffn_gate, * ffn_down, * ffn_up;
  15610. if (is_text) {
  15611. wqkv = model.layers[il].wqkv;
  15612. wo = model.layers[il].wo;
  15613. ffn_gate = model.layers[il].ffn_gate;
  15614. ffn_down = model.layers[il].ffn_down;
  15615. ffn_up = model.layers[il].ffn_up;
  15616. } else {
  15617. wqkv = model.layers[il].visexp_attn_wqkv;
  15618. wo = model.layers[il].visexp_attn_wo;
  15619. ffn_gate = model.layers[il].visexp_ffn_gate;
  15620. ffn_down = model.layers[il].visexp_ffn_down;
  15621. ffn_up = model.layers[il].visexp_ffn_up;
  15622. }
  15623. ggml_tensor * inpSA = inpL;
  15624. cur = build_norm(inpSA, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
  15625. // build self attention
  15626. {
  15627. ggml_tensor * qkv = build_lora_mm(wqkv, cur);
  15628. // split qkv into Q, K, V along the first dimension
  15629. ggml_tensor * Qcur = ggml_view_3d(ctx0, qkv, n_embd_head, n_head, n_tokens, n_embd_head * sizeof(float),
  15630. qkv->nb[1], 0);
  15631. ggml_tensor * Kcur = ggml_view_3d(ctx0, qkv, n_embd_head, n_head_kv, n_tokens, n_embd_head * sizeof(float),
  15632. qkv->nb[1], n_embd * ggml_element_size(qkv));
  15633. ggml_tensor * Vcur = ggml_view_3d(ctx0, qkv, n_embd_head, n_head_kv, n_tokens, n_embd_head * sizeof(float),
  15634. qkv->nb[1], 2 * n_embd * ggml_element_size(qkv));
  15635. Qcur = ggml_rope(ctx0, Qcur, inp_pos, n_embd_head, rope_type);
  15636. Kcur = ggml_rope(ctx0, Kcur, inp_pos, n_embd_head, rope_type);
  15637. cur = build_attn(inp_attn, wo, nullptr, Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
  15638. cb(cur, "attn_out", il);
  15639. }
  15640. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  15641. cb(ffn_inp, "ffn_inp", il);
  15642. cur = build_norm(ffn_inp, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);
  15643. cb(cur, "ffn_norm", il);
  15644. cur = build_ffn(cur,
  15645. ffn_up, NULL, NULL,
  15646. ffn_gate, NULL, NULL,
  15647. ffn_down, NULL, NULL,
  15648. NULL,
  15649. LLM_FFN_SILU, LLM_FFN_PAR, il);
  15650. cur = ggml_add(ctx0, cur, ffn_inp);
  15651. cb(cur, "ffn_out", il);
  15652. inpL = cur;
  15653. }
  15654. cur = inpL;
  15655. cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1);
  15656. cb(cur, "result_norm", -1);
  15657. res->t_embd = cur;
  15658. cur = build_lora_mm(model.output, cur);
  15659. cb(cur, "result_output", -1);
  15660. res->t_logits = cur;
  15661. ggml_build_forward_expand(gf, cur);
  15662. }
  15663. };
  15664. llama_memory_i * llama_model::create_memory(const llama_memory_params & params, const llama_cparams & cparams) const {
  15665. llama_memory_i * res;
  15666. switch (arch) {
  15667. // Models that need specific instantiation should be handled in the
  15668. // switch statement
  15669. case LLM_ARCH_BERT:
  15670. case LLM_ARCH_JINA_BERT_V2:
  15671. case LLM_ARCH_JINA_BERT_V3:
  15672. case LLM_ARCH_NOMIC_BERT:
  15673. case LLM_ARCH_NOMIC_BERT_MOE:
  15674. case LLM_ARCH_NEO_BERT:
  15675. case LLM_ARCH_WAVTOKENIZER_DEC:
  15676. case LLM_ARCH_GEMMA_EMBEDDING:
  15677. case LLM_ARCH_DREAM:
  15678. case LLM_ARCH_LLADA:
  15679. case LLM_ARCH_LLADA_MOE:
  15680. {
  15681. res = nullptr;
  15682. } break;
  15683. // Models that need standard caching should rely on recurrent/hybrid
  15684. // checks
  15685. default:
  15686. {
  15687. if (llm_arch_is_recurrent(arch)) {
  15688. res = new llama_memory_recurrent(
  15689. *this,
  15690. GGML_TYPE_F32,
  15691. GGML_TYPE_F32,
  15692. cparams.offload_kqv,
  15693. std::max((uint32_t) 1, cparams.n_seq_max),
  15694. cparams.n_seq_max,
  15695. nullptr);
  15696. } else if (llm_arch_is_hybrid(arch)) {
  15697. // The main difference between hybrid architectures is the
  15698. // layer filters, so pick the right one here
  15699. llama_memory_hybrid::layer_filter_cb filter_attn = nullptr;
  15700. llama_memory_hybrid::layer_filter_cb filter_recr = nullptr;
  15701. if (arch == LLM_ARCH_FALCON_H1) {
  15702. filter_attn = [&](int32_t) { return true; };
  15703. filter_recr = [&](int32_t) { return true; };
  15704. } else if (arch == LLM_ARCH_NEMOTRON_H) {
  15705. filter_attn = [&](int32_t il) {
  15706. return !hparams.is_recurrent(il) && hparams.n_ff(il) == 0;
  15707. };
  15708. filter_recr = [&](int32_t il) {
  15709. return hparams.is_recurrent(il) && hparams.n_ff(il) == 0;
  15710. };
  15711. }
  15712. res = new llama_memory_hybrid(
  15713. /* model */ *this,
  15714. /* attn_type_k */ params.type_k,
  15715. /* attn_type_v */ params.type_v,
  15716. /* attn_v_trans */ !cparams.flash_attn,
  15717. /* attn_kv_size */ cparams.n_ctx,
  15718. /* attn_n_pad */ 1,
  15719. /* attn_n_swa */ hparams.n_swa,
  15720. /* attn_swa_type */ hparams.swa_type,
  15721. /* recurrent_type_k */ GGML_TYPE_F32,
  15722. /* recurrent_type_v */ GGML_TYPE_F32,
  15723. /* recurrent_kv_size */ std::max((uint32_t) 1, cparams.n_seq_max),
  15724. /* n_seq_max */ cparams.n_seq_max,
  15725. /* offload */ cparams.offload_kqv,
  15726. /* unified */ cparams.kv_unified,
  15727. /* filter_attn */ std::move(filter_attn),
  15728. /* filter_recr */ std::move(filter_recr));
  15729. } else {
  15730. uint32_t n_ctx_per_stream = cparams.n_ctx;
  15731. if (!cparams.kv_unified) {
  15732. n_ctx_per_stream = (cparams.n_ctx + cparams.n_seq_max - 1)/cparams.n_seq_max;
  15733. }
  15734. llama_memory_i::layer_reuse_cb reuse = nullptr;
  15735. if (arch == LLM_ARCH_GEMMA3N) {
  15736. reuse = [&](int32_t il) {
  15737. if (il >= (int32_t) hparams.n_layer_kv_from_start) {
  15738. return (int32_t) hparams.n_layer_kv_from_start - (hparams.is_swa(il) ? 2 : 1);
  15739. }
  15740. return -1;
  15741. };
  15742. }
  15743. if (hparams.swa_type != LLAMA_SWA_TYPE_NONE) {
  15744. GGML_ASSERT(hparams.is_swa_any());
  15745. res = new llama_kv_cache_iswa(
  15746. *this,
  15747. params.type_k,
  15748. params.type_v,
  15749. !cparams.flash_attn,
  15750. cparams.offload_kqv,
  15751. params.swa_full,
  15752. cparams.kv_unified,
  15753. n_ctx_per_stream,
  15754. cparams.n_seq_max,
  15755. cparams.n_ubatch,
  15756. 1,
  15757. nullptr,
  15758. reuse);
  15759. } else {
  15760. GGML_ASSERT(!hparams.is_swa_any());
  15761. res = new llama_kv_cache(
  15762. *this,
  15763. params.type_k,
  15764. params.type_v,
  15765. !cparams.flash_attn,
  15766. cparams.offload_kqv,
  15767. cparams.kv_unified,
  15768. n_ctx_per_stream,
  15769. cparams.n_seq_max,
  15770. 1,
  15771. hparams.n_swa,
  15772. hparams.swa_type,
  15773. nullptr,
  15774. nullptr);
  15775. }
  15776. }
  15777. }
  15778. }
  15779. return res;
  15780. }
  15781. ggml_cgraph * llama_model::build_graph(const llm_graph_params & params) const {
  15782. std::unique_ptr<llm_graph_context> llm;
  15783. switch (arch) {
  15784. case LLM_ARCH_LLAMA:
  15785. {
  15786. llm = std::make_unique<llm_build_llama>(*this, params);
  15787. } break;
  15788. case LLM_ARCH_LLAMA4:
  15789. {
  15790. if (hparams.swa_type == LLAMA_SWA_TYPE_NONE) {
  15791. llm = std::make_unique<llm_build_llama>(*this, params);
  15792. } else {
  15793. llm = std::make_unique<llm_build_llama_iswa>(*this, params);
  15794. }
  15795. } break;
  15796. case LLM_ARCH_DECI:
  15797. {
  15798. llm = std::make_unique<llm_build_deci>(*this, params);
  15799. } break;
  15800. case LLM_ARCH_BAICHUAN:
  15801. {
  15802. llm = std::make_unique<llm_build_baichuan>(*this, params);
  15803. } break;
  15804. case LLM_ARCH_FALCON:
  15805. {
  15806. llm = std::make_unique<llm_build_falcon>(*this, params);
  15807. } break;
  15808. case LLM_ARCH_GROK:
  15809. {
  15810. llm = std::make_unique<llm_build_grok>(*this, params);
  15811. } break;
  15812. case LLM_ARCH_STARCODER:
  15813. {
  15814. llm = std::make_unique<llm_build_starcoder>(*this, params);
  15815. } break;
  15816. case LLM_ARCH_REFACT:
  15817. {
  15818. llm = std::make_unique<llm_build_refact>(*this, params);
  15819. } break;
  15820. case LLM_ARCH_BERT:
  15821. case LLM_ARCH_JINA_BERT_V2:
  15822. case LLM_ARCH_JINA_BERT_V3:
  15823. case LLM_ARCH_NOMIC_BERT:
  15824. case LLM_ARCH_NOMIC_BERT_MOE:
  15825. {
  15826. llm = std::make_unique<llm_build_bert>(*this, params);
  15827. } break;
  15828. case LLM_ARCH_NEO_BERT:
  15829. {
  15830. llm = std::make_unique<llm_build_neo_bert>(*this, params);
  15831. } break;
  15832. case LLM_ARCH_BLOOM:
  15833. {
  15834. llm = std::make_unique<llm_build_bloom>(*this, params);
  15835. } break;
  15836. case LLM_ARCH_MPT:
  15837. {
  15838. llm = std::make_unique<llm_build_mpt>(*this, params);
  15839. } break;
  15840. case LLM_ARCH_STABLELM:
  15841. {
  15842. llm = std::make_unique<llm_build_stablelm>(*this, params);
  15843. } break;
  15844. case LLM_ARCH_QWEN:
  15845. {
  15846. llm = std::make_unique<llm_build_qwen>(*this, params);
  15847. } break;
  15848. case LLM_ARCH_QWEN2:
  15849. {
  15850. llm = std::make_unique<llm_build_qwen2>(*this, params);
  15851. } break;
  15852. case LLM_ARCH_DREAM:
  15853. {
  15854. llm = std::make_unique<llm_build_dream>(*this, params);
  15855. }
  15856. break;
  15857. case LLM_ARCH_LLADA:
  15858. {
  15859. llm = std::make_unique<llm_build_llada>(*this, params);
  15860. }
  15861. break;
  15862. case LLM_ARCH_LLADA_MOE:
  15863. {
  15864. llm = std::make_unique<llm_build_llada_moe>(*this, params);
  15865. }
  15866. break;
  15867. case LLM_ARCH_QWEN2VL:
  15868. {
  15869. llm = std::make_unique<llm_build_qwen2vl>(*this, params);
  15870. } break;
  15871. case LLM_ARCH_QWEN2MOE:
  15872. {
  15873. llm = std::make_unique<llm_build_qwen2moe>(*this, params);
  15874. } break;
  15875. case LLM_ARCH_QWEN3:
  15876. {
  15877. llm = std::make_unique<llm_build_qwen3>(*this, params);
  15878. } break;
  15879. case LLM_ARCH_QWEN3MOE:
  15880. {
  15881. llm = std::make_unique<llm_build_qwen3moe>(*this, params);
  15882. } break;
  15883. case LLM_ARCH_PHI2:
  15884. {
  15885. llm = std::make_unique<llm_build_phi2>(*this, params);
  15886. } break;
  15887. case LLM_ARCH_PHI3:
  15888. case LLM_ARCH_PHIMOE:
  15889. {
  15890. if (hparams.swa_type != LLAMA_SWA_TYPE_NONE) {
  15891. llm = std::make_unique<llm_build_phi3<true>> (*this, params);
  15892. } else {
  15893. llm = std::make_unique<llm_build_phi3<false>>(*this, params);
  15894. }
  15895. } break;
  15896. case LLM_ARCH_PLAMO:
  15897. {
  15898. llm = std::make_unique<llm_build_plamo>(*this, params);
  15899. } break;
  15900. case LLM_ARCH_PLAMO2:
  15901. {
  15902. llm = std::make_unique<llm_build_plamo2>(*this, params);
  15903. } break;
  15904. case LLM_ARCH_GPT2:
  15905. {
  15906. llm = std::make_unique<llm_build_gpt2>(*this, params);
  15907. } break;
  15908. case LLM_ARCH_CODESHELL:
  15909. {
  15910. llm = std::make_unique<llm_build_codeshell>(*this, params);
  15911. } break;
  15912. case LLM_ARCH_ORION:
  15913. {
  15914. llm = std::make_unique<llm_build_orion>(*this, params);
  15915. } break;
  15916. case LLM_ARCH_INTERNLM2:
  15917. {
  15918. llm = std::make_unique<llm_build_internlm2>(*this, params);
  15919. } break;
  15920. case LLM_ARCH_MINICPM3:
  15921. {
  15922. llm = std::make_unique<llm_build_minicpm3>(*this, params);
  15923. } break;
  15924. case LLM_ARCH_GEMMA:
  15925. {
  15926. llm = std::make_unique<llm_build_gemma>(*this, params);
  15927. } break;
  15928. case LLM_ARCH_GEMMA2:
  15929. {
  15930. llm = std::make_unique<llm_build_gemma2_iswa>(*this, params);
  15931. } break;
  15932. case LLM_ARCH_GEMMA3:
  15933. {
  15934. llm = std::make_unique<llm_build_gemma3_iswa>(*this, params);
  15935. } break;
  15936. case LLM_ARCH_GEMMA3N:
  15937. {
  15938. llm = std::make_unique<llm_build_gemma3n_iswa>(*this, params);
  15939. } break;
  15940. case LLM_ARCH_GEMMA_EMBEDDING:
  15941. {
  15942. llm = std::make_unique<llm_build_gemma_embedding>(*this, params);
  15943. } break;
  15944. case LLM_ARCH_STARCODER2:
  15945. {
  15946. llm = std::make_unique<llm_build_starcoder2>(*this, params);
  15947. } break;
  15948. case LLM_ARCH_MAMBA:
  15949. case LLM_ARCH_MAMBA2:
  15950. {
  15951. llm = std::make_unique<llm_build_mamba>(*this, params);
  15952. } break;
  15953. case LLM_ARCH_JAMBA:
  15954. {
  15955. llm = std::make_unique<llm_build_jamba>(*this, params);
  15956. } break;
  15957. case LLM_ARCH_XVERSE:
  15958. {
  15959. llm = std::make_unique<llm_build_xverse>(*this, params);
  15960. } break;
  15961. case LLM_ARCH_COMMAND_R:
  15962. {
  15963. llm = std::make_unique<llm_build_command_r>(*this, params);
  15964. } break;
  15965. case LLM_ARCH_COHERE2:
  15966. {
  15967. llm = std::make_unique<llm_build_cohere2_iswa>(*this, params);
  15968. } break;
  15969. case LLM_ARCH_DBRX:
  15970. {
  15971. llm = std::make_unique<llm_build_dbrx>(*this, params);
  15972. } break;
  15973. case LLM_ARCH_OLMO:
  15974. {
  15975. llm = std::make_unique<llm_build_olmo>(*this, params);
  15976. } break;
  15977. case LLM_ARCH_OLMO2:
  15978. {
  15979. if (hparams.swa_type == LLAMA_SWA_TYPE_STANDARD) {
  15980. llm = std::make_unique<llm_build_olmo2<true>>(*this, params);
  15981. } else {
  15982. llm = std::make_unique<llm_build_olmo2<false>>(*this, params);
  15983. }
  15984. } break;
  15985. case LLM_ARCH_OLMOE:
  15986. {
  15987. llm = std::make_unique<llm_build_olmoe>(*this, params);
  15988. } break;
  15989. case LLM_ARCH_OPENELM:
  15990. {
  15991. llm = std::make_unique<llm_build_openelm>(*this, params);
  15992. } break;
  15993. case LLM_ARCH_GPTNEOX:
  15994. {
  15995. llm = std::make_unique<llm_build_gptneox>(*this, params);
  15996. } break;
  15997. case LLM_ARCH_ARCTIC:
  15998. {
  15999. llm = std::make_unique<llm_build_arctic>(*this, params);
  16000. } break;
  16001. case LLM_ARCH_DEEPSEEK:
  16002. {
  16003. llm = std::make_unique<llm_build_deepseek>(*this, params);
  16004. } break;
  16005. case LLM_ARCH_DEEPSEEK2:
  16006. {
  16007. llm = std::make_unique<llm_build_deepseek2>(*this, params);
  16008. } break;
  16009. case LLM_ARCH_CHATGLM:
  16010. {
  16011. llm = std::make_unique<llm_build_chatglm>(*this, params);
  16012. } break;
  16013. case LLM_ARCH_GLM4:
  16014. {
  16015. llm = std::make_unique<llm_build_glm4>(*this, params);
  16016. } break;
  16017. case LLM_ARCH_GLM4_MOE:
  16018. {
  16019. llm = std::make_unique<llm_build_glm4_moe>(*this, params);
  16020. } break;
  16021. case LLM_ARCH_BITNET:
  16022. {
  16023. llm = std::make_unique<llm_build_bitnet>(*this, params);
  16024. } break;
  16025. case LLM_ARCH_T5:
  16026. {
  16027. switch (params.gtype) {
  16028. case LLM_GRAPH_TYPE_ENCODER:
  16029. llm = std::make_unique<llm_build_t5_enc>(*this, params);
  16030. break;
  16031. case LLM_GRAPH_TYPE_DEFAULT:
  16032. case LLM_GRAPH_TYPE_DECODER:
  16033. llm = std::make_unique<llm_build_t5_dec>(*this, params);
  16034. break;
  16035. default:
  16036. GGML_ABORT("invalid graph type");
  16037. };
  16038. } break;
  16039. case LLM_ARCH_T5ENCODER:
  16040. {
  16041. llm = std::make_unique<llm_build_t5_enc>(*this, params);
  16042. }
  16043. break;
  16044. case LLM_ARCH_JAIS:
  16045. {
  16046. llm = std::make_unique<llm_build_jais>(*this, params);
  16047. } break;
  16048. case LLM_ARCH_NEMOTRON:
  16049. {
  16050. llm = std::make_unique<llm_build_nemotron>(*this, params);
  16051. } break;
  16052. case LLM_ARCH_NEMOTRON_H:
  16053. {
  16054. llm = std::make_unique<llm_build_nemotron_h>(*this, params);
  16055. } break;
  16056. case LLM_ARCH_EXAONE:
  16057. {
  16058. llm = std::make_unique<llm_build_exaone>(*this, params);
  16059. } break;
  16060. case LLM_ARCH_EXAONE4:
  16061. {
  16062. if (hparams.swa_type == LLAMA_SWA_TYPE_STANDARD) {
  16063. llm = std::make_unique<llm_build_exaone4<true>>(*this, params);
  16064. } else {
  16065. llm = std::make_unique<llm_build_exaone4<false>>(*this, params);
  16066. }
  16067. } break;
  16068. case LLM_ARCH_RWKV6:
  16069. {
  16070. llm = std::make_unique<llm_build_rwkv6>(*this, params);
  16071. } break;
  16072. case LLM_ARCH_RWKV6QWEN2:
  16073. {
  16074. llm = std::make_unique<llm_build_rwkv6qwen2>(*this, params);
  16075. } break;
  16076. case LLM_ARCH_RWKV7:
  16077. {
  16078. llm = std::make_unique<llm_build_rwkv7>(*this, params);
  16079. } break;
  16080. case LLM_ARCH_ARWKV7:
  16081. {
  16082. llm = std::make_unique<llm_build_arwkv7>(*this, params);
  16083. } break;
  16084. case LLM_ARCH_GRANITE:
  16085. case LLM_ARCH_GRANITE_MOE:
  16086. case LLM_ARCH_MINICPM:
  16087. {
  16088. llm = std::make_unique<llm_build_granite>(*this, params);
  16089. } break;
  16090. case LLM_ARCH_GRANITE_HYBRID:
  16091. {
  16092. llm = std::make_unique<llm_build_granite_hybrid>(*this, params);
  16093. } break;
  16094. case LLM_ARCH_CHAMELEON:
  16095. {
  16096. llm = std::make_unique<llm_build_chameleon>(*this, params);
  16097. } break;
  16098. case LLM_ARCH_WAVTOKENIZER_DEC:
  16099. {
  16100. llm = std::make_unique<llm_build_wavtokenizer_dec>(*this, params);
  16101. } break;
  16102. case LLM_ARCH_PLM:
  16103. {
  16104. llm = std::make_unique<llm_build_plm>(*this, params);
  16105. } break;
  16106. case LLM_ARCH_BAILINGMOE:
  16107. {
  16108. llm = std::make_unique<llm_build_bailingmoe>(*this, params);
  16109. } break;
  16110. case LLM_ARCH_BAILINGMOE2:
  16111. {
  16112. llm = std::make_unique<llm_build_bailingmoe2>(*this, params);
  16113. } break;
  16114. case LLM_ARCH_SEED_OSS:
  16115. {
  16116. llm = std::make_unique<llm_build_seed_oss>(*this, params);
  16117. } break;
  16118. case LLM_ARCH_DOTS1:
  16119. {
  16120. llm = std::make_unique<llm_build_dots1>(*this, params);
  16121. } break;
  16122. case LLM_ARCH_ARCEE:
  16123. {
  16124. llm = std::make_unique<llm_build_arcee>(*this, params);
  16125. } break;
  16126. case LLM_ARCH_ERNIE4_5:
  16127. {
  16128. llm = std::make_unique<llm_build_ernie4_5>(*this, params);
  16129. } break;
  16130. case LLM_ARCH_ERNIE4_5_MOE:
  16131. {
  16132. llm = std::make_unique<llm_build_ernie4_5_moe>(*this, params);
  16133. } break;
  16134. case LLM_ARCH_HUNYUAN_MOE:
  16135. {
  16136. llm = std::make_unique<llm_build_hunyuan_moe>(*this, params);
  16137. } break;
  16138. case LLM_ARCH_HUNYUAN_DENSE:
  16139. {
  16140. llm = std::make_unique<llm_build_hunyuan_dense>(*this, params);
  16141. } break;
  16142. case LLM_ARCH_SMOLLM3:
  16143. {
  16144. llm = std::make_unique<llm_build_smollm3>(*this, params);
  16145. } break;
  16146. case LLM_ARCH_OPENAI_MOE:
  16147. {
  16148. llm = std::make_unique<llm_build_openai_moe_iswa>(*this, params);
  16149. } break;
  16150. case LLM_ARCH_FALCON_H1:
  16151. {
  16152. llm = std::make_unique<llm_build_falcon_h1>(*this, params);
  16153. } break;
  16154. case LLM_ARCH_LFM2:
  16155. case LLM_ARCH_LFM2MOE:
  16156. {
  16157. llm = std::make_unique<llm_build_lfm2>(*this, params);
  16158. } break;
  16159. case LLM_ARCH_SMALLTHINKER:
  16160. {
  16161. if (hparams.swa_type == LLAMA_SWA_TYPE_STANDARD) {
  16162. llm = std::make_unique<llm_build_smallthinker<true>> (*this, params);
  16163. } else {
  16164. llm = std::make_unique<llm_build_smallthinker<false>>(*this, params);
  16165. }
  16166. } break;
  16167. case LLM_ARCH_GROVEMOE:
  16168. {
  16169. llm = std::make_unique<llm_build_grovemoe>(*this, params);
  16170. } break;
  16171. case LLM_ARCH_APERTUS:
  16172. {
  16173. llm = std::make_unique<llm_build_apertus>(*this, params);
  16174. } break;
  16175. case LLM_ARCH_COGVLM:
  16176. {
  16177. llm = std::make_unique<llm_build_cogvlm>(*this, params);
  16178. } break;
  16179. default:
  16180. GGML_ABORT("fatal error");
  16181. }
  16182. // add on pooling layer
  16183. llm->build_pooling(cls, cls_b, cls_out, cls_out_b);
  16184. // if the gguf model was converted with --sentence-transformers-dense-modules
  16185. // there will be two additional dense projection layers
  16186. // dense linear projections are applied after pooling
  16187. // TODO: move reranking logic here and generalize
  16188. llm->build_dense_out(dense_2_out_layers, dense_3_out_layers);
  16189. return llm->res->get_gf();
  16190. }
  16191. //
  16192. // interface implementation
  16193. //
  16194. llama_model_params llama_model_default_params() {
  16195. llama_model_params result = {
  16196. /*.devices =*/ nullptr,
  16197. /*.tensor_buft_overrides =*/ nullptr,
  16198. /*.n_gpu_layers =*/ 999,
  16199. /*.split_mode =*/ LLAMA_SPLIT_MODE_LAYER,
  16200. /*.main_gpu =*/ 0,
  16201. /*.tensor_split =*/ nullptr,
  16202. /*.progress_callback =*/ nullptr,
  16203. /*.progress_callback_user_data =*/ nullptr,
  16204. /*.kv_overrides =*/ nullptr,
  16205. /*.vocab_only =*/ false,
  16206. /*.use_mmap =*/ true,
  16207. /*.use_mlock =*/ false,
  16208. /*.check_tensors =*/ false,
  16209. /*.use_extra_bufts =*/ true,
  16210. /*.no_host =*/ false,
  16211. };
  16212. return result;
  16213. }
  16214. const llama_vocab * llama_model_get_vocab(const llama_model * model) {
  16215. return &model->vocab;
  16216. }
  16217. void llama_free_model(llama_model * model) {
  16218. llama_model_free(model);
  16219. }
  16220. void llama_model_free(llama_model * model) {
  16221. delete model;
  16222. }
  16223. int32_t llama_model_n_ctx_train(const llama_model * model) {
  16224. return model->hparams.n_ctx_train;
  16225. }
  16226. int32_t llama_model_n_embd(const llama_model * model) {
  16227. return model->hparams.n_embd;
  16228. }
  16229. int32_t llama_model_n_layer(const llama_model * model) {
  16230. return model->hparams.n_layer;
  16231. }
  16232. int32_t llama_model_n_head(const llama_model * model) {
  16233. return model->hparams.n_head();
  16234. }
  16235. int32_t llama_model_n_head_kv(const llama_model * model) {
  16236. return model->hparams.n_head_kv();
  16237. }
  16238. int32_t llama_model_n_swa(const llama_model * model) {
  16239. return model->hparams.n_swa;
  16240. }
  16241. uint32_t llama_model_n_cls_out(const struct llama_model * model) {
  16242. return model->hparams.n_cls_out;
  16243. }
  16244. const char * llama_model_cls_label(const struct llama_model * model, uint32_t i) {
  16245. if (i < model->classifier_labels.size()) {
  16246. return model->classifier_labels[i].c_str();
  16247. }
  16248. return nullptr;
  16249. }
  16250. // deprecated
  16251. int32_t llama_n_ctx_train(const llama_model * model) {
  16252. return llama_model_n_ctx_train(model);
  16253. }
  16254. // deprecated
  16255. int32_t llama_n_embd(const llama_model * model) {
  16256. return llama_model_n_embd(model);
  16257. }
  16258. // deprecated
  16259. int32_t llama_n_layer(const llama_model * model) {
  16260. return llama_model_n_layer(model);
  16261. }
  16262. // deprecated
  16263. int32_t llama_n_head(const llama_model * model) {
  16264. return llama_model_n_head(model);
  16265. }
  16266. llama_rope_type llama_model_rope_type(const llama_model * model) {
  16267. switch (model->arch) {
  16268. // these models do not use RoPE
  16269. case LLM_ARCH_CLIP:
  16270. case LLM_ARCH_GPT2:
  16271. case LLM_ARCH_GPTJ:
  16272. case LLM_ARCH_MPT:
  16273. case LLM_ARCH_REFACT:
  16274. case LLM_ARCH_BLOOM:
  16275. case LLM_ARCH_MAMBA:
  16276. case LLM_ARCH_MAMBA2:
  16277. case LLM_ARCH_JAMBA:
  16278. case LLM_ARCH_JINA_BERT_V2:
  16279. case LLM_ARCH_T5:
  16280. case LLM_ARCH_T5ENCODER:
  16281. case LLM_ARCH_JAIS:
  16282. case LLM_ARCH_RWKV6:
  16283. case LLM_ARCH_RWKV6QWEN2:
  16284. case LLM_ARCH_RWKV7:
  16285. case LLM_ARCH_ARWKV7:
  16286. case LLM_ARCH_WAVTOKENIZER_DEC:
  16287. case LLM_ARCH_NEMOTRON_H:
  16288. return LLAMA_ROPE_TYPE_NONE;
  16289. // use what we call a normal RoPE, operating on pairs of consecutive head values
  16290. case LLM_ARCH_LLAMA:
  16291. case LLM_ARCH_LLADA:
  16292. case LLM_ARCH_LLAMA4:
  16293. case LLM_ARCH_DECI:
  16294. case LLM_ARCH_BAICHUAN:
  16295. case LLM_ARCH_STARCODER:
  16296. case LLM_ARCH_INTERNLM2:
  16297. case LLM_ARCH_MINICPM:
  16298. case LLM_ARCH_XVERSE:
  16299. case LLM_ARCH_COMMAND_R:
  16300. case LLM_ARCH_COHERE2:
  16301. case LLM_ARCH_OLMO:
  16302. case LLM_ARCH_ARCTIC:
  16303. case LLM_ARCH_DEEPSEEK:
  16304. case LLM_ARCH_DEEPSEEK2:
  16305. case LLM_ARCH_PLM:
  16306. case LLM_ARCH_CHATGLM:
  16307. case LLM_ARCH_GLM4:
  16308. case LLM_ARCH_GRANITE:
  16309. case LLM_ARCH_GRANITE_MOE:
  16310. case LLM_ARCH_GRANITE_HYBRID:
  16311. case LLM_ARCH_CHAMELEON:
  16312. case LLM_ARCH_BAILINGMOE:
  16313. case LLM_ARCH_NEO_BERT:
  16314. case LLM_ARCH_SMOLLM3:
  16315. case LLM_ARCH_ARCEE:
  16316. case LLM_ARCH_ERNIE4_5:
  16317. case LLM_ARCH_ERNIE4_5_MOE:
  16318. return LLAMA_ROPE_TYPE_NORM;
  16319. // the pairs of head values are offset by n_rot/2
  16320. case LLM_ARCH_FALCON:
  16321. case LLM_ARCH_FALCON_H1:
  16322. case LLM_ARCH_GROK:
  16323. case LLM_ARCH_DBRX:
  16324. case LLM_ARCH_BERT:
  16325. case LLM_ARCH_JINA_BERT_V3:
  16326. case LLM_ARCH_NOMIC_BERT:
  16327. case LLM_ARCH_NOMIC_BERT_MOE:
  16328. case LLM_ARCH_STABLELM:
  16329. case LLM_ARCH_BITNET:
  16330. case LLM_ARCH_QWEN:
  16331. case LLM_ARCH_QWEN2:
  16332. case LLM_ARCH_DREAM:
  16333. case LLM_ARCH_QWEN2MOE:
  16334. case LLM_ARCH_QWEN3:
  16335. case LLM_ARCH_QWEN3MOE:
  16336. case LLM_ARCH_LLADA_MOE:
  16337. case LLM_ARCH_OLMO2:
  16338. case LLM_ARCH_OLMOE:
  16339. case LLM_ARCH_PHI2:
  16340. case LLM_ARCH_PHI3:
  16341. case LLM_ARCH_PHIMOE:
  16342. case LLM_ARCH_PLAMO:
  16343. case LLM_ARCH_PLAMO2:
  16344. case LLM_ARCH_GEMMA:
  16345. case LLM_ARCH_GEMMA2:
  16346. case LLM_ARCH_GEMMA3:
  16347. case LLM_ARCH_GEMMA3N:
  16348. case LLM_ARCH_GEMMA_EMBEDDING:
  16349. case LLM_ARCH_STARCODER2:
  16350. case LLM_ARCH_OPENELM:
  16351. case LLM_ARCH_GPTNEOX:
  16352. case LLM_ARCH_CODESHELL:
  16353. case LLM_ARCH_ORION:
  16354. case LLM_ARCH_NEMOTRON:
  16355. case LLM_ARCH_EXAONE:
  16356. case LLM_ARCH_EXAONE4:
  16357. case LLM_ARCH_MINICPM3:
  16358. case LLM_ARCH_BAILINGMOE2:
  16359. case LLM_ARCH_DOTS1:
  16360. case LLM_ARCH_HUNYUAN_MOE:
  16361. case LLM_ARCH_OPENAI_MOE:
  16362. case LLM_ARCH_HUNYUAN_DENSE:
  16363. case LLM_ARCH_LFM2:
  16364. case LLM_ARCH_LFM2MOE:
  16365. case LLM_ARCH_SMALLTHINKER:
  16366. case LLM_ARCH_GLM4_MOE:
  16367. case LLM_ARCH_SEED_OSS:
  16368. case LLM_ARCH_GROVEMOE:
  16369. case LLM_ARCH_APERTUS:
  16370. case LLM_ARCH_COGVLM:
  16371. return LLAMA_ROPE_TYPE_NEOX;
  16372. case LLM_ARCH_QWEN2VL:
  16373. return LLAMA_ROPE_TYPE_MROPE;
  16374. // all model arches should be listed explicitly here
  16375. case LLM_ARCH_UNKNOWN:
  16376. GGML_ABORT("unknown architecture");
  16377. }
  16378. return LLAMA_ROPE_TYPE_NONE;
  16379. }
  16380. float llama_model_rope_freq_scale_train(const llama_model * model) {
  16381. return model->hparams.rope_freq_scale_train;
  16382. }
  16383. int32_t llama_model_meta_val_str(const llama_model * model, const char * key, char * buf, size_t buf_size) {
  16384. const auto & it = model->gguf_kv.find(key);
  16385. if (it == model->gguf_kv.end()) {
  16386. if (buf_size > 0) {
  16387. buf[0] = '\0';
  16388. }
  16389. return -1;
  16390. }
  16391. return snprintf(buf, buf_size, "%s", it->second.c_str());
  16392. }
  16393. int32_t llama_model_meta_count(const llama_model * model) {
  16394. return (int)model->gguf_kv.size();
  16395. }
  16396. int32_t llama_model_meta_key_by_index(const llama_model * model, int i, char * buf, size_t buf_size) {
  16397. if (i < 0 || i >= (int)model->gguf_kv.size()) {
  16398. if (buf_size > 0) {
  16399. buf[0] = '\0';
  16400. }
  16401. return -1;
  16402. }
  16403. auto it = model->gguf_kv.begin();
  16404. std::advance(it, i);
  16405. return snprintf(buf, buf_size, "%s", it->first.c_str());
  16406. }
  16407. int32_t llama_model_meta_val_str_by_index(const llama_model * model, int32_t i, char * buf, size_t buf_size) {
  16408. if (i < 0 || i >= (int)model->gguf_kv.size()) {
  16409. if (buf_size > 0) {
  16410. buf[0] = '\0';
  16411. }
  16412. return -1;
  16413. }
  16414. auto it = model->gguf_kv.begin();
  16415. std::advance(it, i);
  16416. return snprintf(buf, buf_size, "%s", it->second.c_str());
  16417. }
  16418. int32_t llama_model_desc(const llama_model * model, char * buf, size_t buf_size) {
  16419. return snprintf(buf, buf_size, "%s", model->desc().c_str());
  16420. }
  16421. uint64_t llama_model_size(const llama_model * model) {
  16422. return model->size();
  16423. }
  16424. const char * llama_model_chat_template(const llama_model * model, const char * name) {
  16425. const auto key = name ? LLM_KV(model->arch, name)(LLM_KV_TOKENIZER_CHAT_TEMPLATE)
  16426. : LLM_KV(model->arch)(LLM_KV_TOKENIZER_CHAT_TEMPLATE);
  16427. const auto & it = model->gguf_kv.find(key);
  16428. if (it == model->gguf_kv.end()) {
  16429. // one-off fix for very popular models (so we are not flooded with issues)
  16430. // do not extend this list unless absolutely necessary
  16431. // Mistral-Small-2503 does not have built-in chat template
  16432. llama_vocab_pre_type pre_type = model->vocab.get_pre_type();
  16433. if (!name && pre_type == LLAMA_VOCAB_PRE_TYPE_TEKKEN && model->layers.size() == 40) {
  16434. return "mistral-v7-tekken";
  16435. }
  16436. return nullptr;
  16437. }
  16438. return it->second.c_str();
  16439. }
  16440. uint64_t llama_model_n_params(const llama_model * model) {
  16441. return model->n_elements();
  16442. }
  16443. bool llama_model_has_encoder(const llama_model * model) {
  16444. switch (model->arch) {
  16445. case LLM_ARCH_T5: return true;
  16446. case LLM_ARCH_T5ENCODER: return true;
  16447. default: return false;
  16448. }
  16449. }
  16450. bool llama_model_has_decoder(const llama_model * model) {
  16451. switch (model->arch) {
  16452. case LLM_ARCH_T5ENCODER: return false;
  16453. default: return true;
  16454. }
  16455. }
  16456. llama_token llama_model_decoder_start_token(const llama_model * model) {
  16457. return model->hparams.dec_start_token_id;
  16458. }
  16459. bool llama_model_is_recurrent(const llama_model * model) {
  16460. return llm_arch_is_recurrent(model->arch);
  16461. }
  16462. bool llama_model_is_hybrid(const llama_model * model) {
  16463. return llm_arch_is_hybrid(model->arch);
  16464. }
  16465. bool llama_model_is_diffusion(const llama_model * model) {
  16466. return llm_arch_is_diffusion(model->arch);
  16467. }
  16468. const std::vector<std::pair<std::string, ggml_tensor *>> & llama_internal_get_tensor_map(const llama_model * model) {
  16469. return model->tensors_by_name;
  16470. }