llama-model.cpp 817 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833
  1. #include "llama-model.h"
  2. #include "llama-impl.h"
  3. #include "llama-mmap.h"
  4. #include "llama-batch.h"
  5. #include "llama-cparams.h"
  6. #include "llama-model-loader.h"
  7. #include "llama-kv-cache-unified.h"
  8. #include "llama-kv-cache-unified-iswa.h"
  9. #include "llama-memory-hybrid.h"
  10. #include "llama-memory-recurrent.h"
  11. #include "ggml-cpp.h"
  12. #include <algorithm>
  13. #include <cassert>
  14. #include <cmath>
  15. #include <cfloat>
  16. #include <cstring>
  17. #include <cmath>
  18. #include <functional>
  19. #include <map>
  20. #include <regex>
  21. #include <sstream>
  22. #include <stdexcept>
  23. const char * llm_type_name(llm_type type) {
  24. switch (type) {
  25. case LLM_TYPE_14M: return "14M";
  26. case LLM_TYPE_17M: return "17M";
  27. case LLM_TYPE_22M: return "22M";
  28. case LLM_TYPE_33M: return "33M";
  29. case LLM_TYPE_60M: return "60M";
  30. case LLM_TYPE_70M: return "70M";
  31. case LLM_TYPE_80M: return "80M";
  32. case LLM_TYPE_109M: return "109M";
  33. case LLM_TYPE_137M: return "137M";
  34. case LLM_TYPE_160M: return "160M";
  35. case LLM_TYPE_190M: return "190M";
  36. case LLM_TYPE_220M: return "220M";
  37. case LLM_TYPE_250M: return "250M";
  38. case LLM_TYPE_256M: return "256M";
  39. case LLM_TYPE_270M: return "270M";
  40. case LLM_TYPE_335M: return "335M";
  41. case LLM_TYPE_350M: return "350M";
  42. case LLM_TYPE_410M: return "410M";
  43. case LLM_TYPE_450M: return "450M";
  44. case LLM_TYPE_475M: return "475M";
  45. case LLM_TYPE_700M: return "700M";
  46. case LLM_TYPE_770M: return "770M";
  47. case LLM_TYPE_780M: return "780M";
  48. case LLM_TYPE_0_3B: return "0.3B";
  49. case LLM_TYPE_0_5B: return "0.5B";
  50. case LLM_TYPE_0_6B: return "0.6B";
  51. case LLM_TYPE_1B: return "1B";
  52. case LLM_TYPE_1_2B: return "1.2B";
  53. case LLM_TYPE_1_3B: return "1.3B";
  54. case LLM_TYPE_1_4B: return "1.4B";
  55. case LLM_TYPE_1_5B: return "1.5B";
  56. case LLM_TYPE_1_6B: return "1.6B";
  57. case LLM_TYPE_1_7B: return "1.7B";
  58. case LLM_TYPE_1_8B: return "1.8B";
  59. case LLM_TYPE_2B: return "2B";
  60. case LLM_TYPE_2_8B: return "2.8B";
  61. case LLM_TYPE_2_9B: return "2.9B";
  62. case LLM_TYPE_3B: return "3B";
  63. case LLM_TYPE_4B: return "4B";
  64. case LLM_TYPE_6B: return "6B";
  65. case LLM_TYPE_6_9B: return "6.9B";
  66. case LLM_TYPE_7B: return "7B";
  67. case LLM_TYPE_8B: return "8B";
  68. case LLM_TYPE_9B: return "9B";
  69. case LLM_TYPE_11B: return "11B";
  70. case LLM_TYPE_12B: return "12B";
  71. case LLM_TYPE_13B: return "13B";
  72. case LLM_TYPE_14B: return "14B";
  73. case LLM_TYPE_15B: return "15B";
  74. case LLM_TYPE_16B: return "16B";
  75. case LLM_TYPE_20B: return "20B";
  76. case LLM_TYPE_27B: return "27B";
  77. case LLM_TYPE_30B: return "30B";
  78. case LLM_TYPE_32B: return "32B";
  79. case LLM_TYPE_34B: return "34B";
  80. case LLM_TYPE_35B: return "35B";
  81. case LLM_TYPE_40B: return "40B";
  82. case LLM_TYPE_65B: return "65B";
  83. case LLM_TYPE_70B: return "70B";
  84. case LLM_TYPE_120B: return "120B";
  85. case LLM_TYPE_142B: return "142B";
  86. case LLM_TYPE_236B: return "236B";
  87. case LLM_TYPE_290B: return "290B";
  88. case LLM_TYPE_314B: return "314B";
  89. case LLM_TYPE_405B: return "405B";
  90. case LLM_TYPE_671B: return "671B";
  91. case LLM_TYPE_SMALL: return "0.1B";
  92. case LLM_TYPE_MEDIUM: return "0.4B";
  93. case LLM_TYPE_LARGE: return "0.8B";
  94. case LLM_TYPE_XL: return "1.5B";
  95. case LLM_TYPE_A1_7B: return "A1.7B";
  96. case LLM_TYPE_A2_7B: return "A2.7B";
  97. case LLM_TYPE_8x7B: return "8x7B";
  98. case LLM_TYPE_8x22B: return "8x22B";
  99. case LLM_TYPE_16x12B: return "16x12B";
  100. case LLM_TYPE_16x3_8B: return "16x3.8B";
  101. case LLM_TYPE_10B_128x3_66B: return "10B+128x3.66B";
  102. case LLM_TYPE_57B_A14B: return "57B.A14B";
  103. case LLM_TYPE_17B_16E: return "17Bx16E (Scout)";
  104. case LLM_TYPE_17B_128E: return "17Bx128E (Maverick)";
  105. case LLM_TYPE_A13B: return "A13B";
  106. case LLM_TYPE_21B_A3B: return "21B.A3B";
  107. case LLM_TYPE_30B_A3B: return "30B.A3B";
  108. case LLM_TYPE_106B_A12B: return "106B.A12B";
  109. case LLM_TYPE_235B_A22B: return "235B.A22B";
  110. case LLM_TYPE_300B_A47B: return "300B.A47B";
  111. case LLM_TYPE_355B_A32B: return "355B.A32B";
  112. case LLM_TYPE_E2B: return "E2B";
  113. case LLM_TYPE_E4B: return "E4B";
  114. default: return "?B";
  115. }
  116. }
  117. static const char * llama_expert_gating_func_name(llama_expert_gating_func_type type) {
  118. switch (type) {
  119. case LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX: return "softmax";
  120. case LLAMA_EXPERT_GATING_FUNC_TYPE_SIGMOID: return "sigmoid";
  121. default: return "unknown";
  122. }
  123. }
  124. static const std::map<llama_rope_scaling_type, const char *> LLAMA_ROPE_SCALING_TYPES = {
  125. { LLAMA_ROPE_SCALING_TYPE_NONE, "none" },
  126. { LLAMA_ROPE_SCALING_TYPE_LINEAR, "linear" },
  127. { LLAMA_ROPE_SCALING_TYPE_YARN, "yarn" },
  128. { LLAMA_ROPE_SCALING_TYPE_LONGROPE, "longrope" },
  129. };
  130. std::string llama_rope_scaling_type_name(llama_rope_scaling_type rope_scaling_type) {
  131. return LLAMA_ROPE_SCALING_TYPES.at(rope_scaling_type);
  132. }
  133. static llama_rope_scaling_type llama_rope_scaling_type_from_string(const std::string & name) {
  134. for (const auto & kv : LLAMA_ROPE_SCALING_TYPES) {
  135. if (kv.second == name) {
  136. return (llama_rope_scaling_type) kv.first;
  137. }
  138. }
  139. return LLAMA_ROPE_SCALING_TYPE_UNSPECIFIED;
  140. }
  141. // checks if the weight tensor can be used with the specified buffer type and device
  142. 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) {
  143. GGML_ASSERT(w != nullptr);
  144. if (op == GGML_OP_NONE) {
  145. return true;
  146. }
  147. ggml_init_params params = {
  148. /*.mem_size =*/ ggml_tensor_overhead()*8,
  149. /*.mem_buffer =*/ NULL,
  150. /*.no_alloc =*/ true,
  151. };
  152. ggml_context_ptr ctx_ptr { ggml_init(params) };
  153. if (!ctx_ptr) {
  154. throw std::runtime_error(format("failed to create ggml context"));
  155. }
  156. ggml_context * ctx = ctx_ptr.get();
  157. ggml_tensor * op_tensor = nullptr;
  158. switch (op) {
  159. case GGML_OP_GET_ROWS:
  160. {
  161. ggml_tensor * b = ggml_new_tensor_1d(ctx, GGML_TYPE_I32, 512);
  162. op_tensor = ggml_get_rows(ctx, w, b);
  163. } break;
  164. case GGML_OP_MUL_MAT:
  165. {
  166. ggml_tensor * b = ggml_new_tensor_4d(ctx, GGML_TYPE_F32, w->ne[0], 512, w->ne[2], w->ne[3]);
  167. op_tensor = ggml_mul_mat(ctx, w, b);
  168. } break;
  169. case GGML_OP_MUL_MAT_ID:
  170. {
  171. int n_expert_used = hparams.n_expert_used;
  172. ggml_tensor * b = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, w->ne[0], n_expert_used, 512);
  173. ggml_tensor * ids = ggml_new_tensor_2d(ctx, GGML_TYPE_I32, n_expert_used, 512);
  174. op_tensor = ggml_mul_mat_id(ctx, w, b, ids);
  175. } break;
  176. case GGML_OP_ADD:
  177. {
  178. ggml_tensor * a = ggml_new_tensor_4d(ctx, GGML_TYPE_F32, w->ne[0], w->ne[1], w->ne[2], w->ne[3]);
  179. op_tensor = ggml_add(ctx, a, w);
  180. } break;
  181. case GGML_OP_ADD_ID:
  182. {
  183. int n_expert_used = hparams.n_expert_used;
  184. ggml_tensor * a = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, w->ne[0], n_expert_used, 512);
  185. ggml_tensor * c = ggml_new_tensor_2d(ctx, GGML_TYPE_I32, n_expert_used, 512);
  186. op_tensor = ggml_add_id(ctx, a, w, c);
  187. } break;
  188. case GGML_OP_MUL:
  189. {
  190. ggml_tensor * a = ggml_new_tensor_4d(ctx, GGML_TYPE_F32, w->ne[0], w->ne[1], w->ne[2], w->ne[3]);
  191. op_tensor = ggml_mul(ctx, a, w);
  192. } break;
  193. case GGML_OP_DIV:
  194. {
  195. ggml_tensor * a = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, w->ne[0]);
  196. op_tensor = ggml_div(ctx, a, w);
  197. } break;
  198. case GGML_OP_ROPE:
  199. {
  200. int n_embd_head = hparams.n_embd_head_v;
  201. int n_head = hparams.n_head();
  202. ggml_tensor * a = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, n_embd_head, n_head, 512);
  203. ggml_tensor * b = ggml_new_tensor_1d(ctx, GGML_TYPE_I32, 512);
  204. op_tensor = ggml_rope_ext(
  205. ctx, a, b, w,
  206. 0, 0, 0, 0, 0,
  207. 0, 0, 0, 0
  208. );
  209. } break;
  210. case GGML_OP_SSM_CONV:
  211. {
  212. const int64_t n_seq_tokens = 512;
  213. const int64_t n_seqs = 3;
  214. ggml_tensor * conv_x = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, w->ne[0] - 1 + n_seq_tokens, w->ne[1], n_seqs);
  215. op_tensor = ggml_ssm_conv(ctx, conv_x, w);
  216. } break;
  217. case GGML_OP_SSM_SCAN:
  218. {
  219. // w is ssm_a, which is used to distinguish Mamba-1 and Mamba-2
  220. const int64_t d_state = w->ne[0] == 1 ? hparams.ssm_d_state : w->ne[0];
  221. const int64_t n_head = w->ne[1];
  222. const int64_t head_dim = hparams.ssm_d_inner / n_head;
  223. const int64_t n_group = hparams.ssm_n_group ? hparams.ssm_n_group : 1;
  224. const int64_t n_seq_tokens = 512;
  225. const int64_t n_seqs = 3;
  226. ggml_tensor * s = ggml_new_tensor_4d(ctx, GGML_TYPE_F32, d_state, head_dim, n_head, n_seqs);
  227. ggml_tensor * x = ggml_new_tensor_4d(ctx, GGML_TYPE_F32, head_dim, n_head, n_seq_tokens, n_seqs);
  228. ggml_tensor * dt = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, n_head, n_seq_tokens, n_seqs);
  229. ggml_tensor * B = ggml_new_tensor_4d(ctx, GGML_TYPE_F32, d_state, n_group, n_seq_tokens, n_seqs);
  230. ggml_tensor * C = ggml_new_tensor_4d(ctx, GGML_TYPE_F32, d_state, n_group, n_seq_tokens, n_seqs);
  231. ggml_tensor * ids = ggml_new_tensor_1d(ctx, GGML_TYPE_I32, n_seqs);
  232. op_tensor = ggml_ssm_scan(ctx, s, x, dt, w, B, C, ids);
  233. } break;
  234. case GGML_OP_RWKV_WKV6:
  235. {
  236. // FIXME
  237. const int64_t S = 123;
  238. const int64_t H = 123;
  239. const int64_t n_tokens = 123;
  240. const int64_t n_seqs = 123;
  241. ggml_tensor * k = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, S, H, n_tokens);
  242. ggml_tensor * v = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, S, H, n_tokens);
  243. ggml_tensor * r = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, S, H, n_tokens);
  244. ggml_tensor * tf = w;
  245. ggml_tensor * td = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, S, H, n_tokens);
  246. ggml_tensor * state = ggml_new_tensor_4d(ctx, GGML_TYPE_F32, S, n_seqs, S, H);
  247. op_tensor = ggml_rwkv_wkv6(ctx, k, v, r, tf, td, state);
  248. } break;
  249. case GGML_OP_IM2COL:
  250. {
  251. const int n_embd = hparams.n_embd;
  252. ggml_tensor * b = ggml_new_tensor_4d(ctx, GGML_TYPE_F32, n_embd, w->ne[1], 1, 1);
  253. op_tensor = ggml_im2col(ctx, w, b, 1, 0, 0, 0, 1, 0, false, GGML_TYPE_F16);
  254. } break;
  255. case GGML_OP_SCALE:
  256. {
  257. op_tensor = ggml_scale(ctx, w, 1.0f);
  258. } break;
  259. default:
  260. GGML_ABORT("%s: missing test for op %s for tensor %s", __func__, ggml_op_name(op), w->name);
  261. }
  262. // create a temporary dummy buffer for the weight so that supports_op can check the buffer type
  263. GGML_ASSERT(w->buffer == nullptr);
  264. w->buffer = ggml_backend_buft_alloc_buffer(buft, 0);
  265. bool op_supported = ggml_backend_dev_supports_op(dev, op_tensor);
  266. ggml_backend_buffer_free(w->buffer);
  267. w->buffer = nullptr;
  268. return op_supported;
  269. }
  270. // lists of buffer types used for each layer
  271. using buft_list_t = std::vector<std::pair<ggml_backend_dev_t, ggml_backend_buffer_type_t>>;
  272. // find the first buffer type in the list that can use the tensor
  273. 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) {
  274. GGML_ASSERT(!buft_list.empty());
  275. for (const auto & cur : buft_list) {
  276. ggml_backend_dev_t cur_dev = cur.first;
  277. ggml_backend_buffer_type_t cur_buft = cur.second;
  278. if (weight_buft_supported(hparams, tensor, op, cur_buft, cur_dev)) {
  279. return cur_buft;
  280. }
  281. }
  282. return nullptr;
  283. }
  284. // CPU: ACCEL -> GPU host -> CPU extra -> CPU
  285. static buft_list_t make_cpu_buft_list(const std::vector<ggml_backend_dev_t> & devices, bool use_extra_bufts) {
  286. buft_list_t buft_list;
  287. // add ACCEL buffer types
  288. for (size_t i = 0; i < ggml_backend_dev_count(); ++i) {
  289. ggml_backend_dev_t dev = ggml_backend_dev_get(i);
  290. if (ggml_backend_dev_type(dev) == GGML_BACKEND_DEVICE_TYPE_ACCEL) {
  291. auto * buft = ggml_backend_dev_buffer_type(dev);
  292. // skip
  293. if (buft != ggml_backend_cpu_buffer_type()) {
  294. buft_list.emplace_back(dev, buft);
  295. }
  296. }
  297. }
  298. // add a host buffer type
  299. // storing the tensors in a host buffer is useful when the processing of large batches
  300. // is offloaded to a GPU device, since it reduces the time spent on data transfers
  301. // generally, this will be done using the first device in the list
  302. // a better approach would be to handle this on a weight-by-weight basis using the offload_op
  303. // function of the device to determine if it would benefit from being stored in a host buffer
  304. for (auto * dev : devices) {
  305. ggml_backend_buffer_type_t buft = ggml_backend_dev_host_buffer_type(dev);
  306. if (buft) {
  307. buft_list.emplace_back(dev, buft);
  308. break;
  309. }
  310. }
  311. // add extra buffer types
  312. if (use_extra_bufts) {
  313. auto * cpu_dev = ggml_backend_dev_by_type(GGML_BACKEND_DEVICE_TYPE_CPU);
  314. if (cpu_dev == nullptr) {
  315. throw std::runtime_error(format("%s: no CPU backend found", __func__));
  316. }
  317. auto * cpu_reg = ggml_backend_dev_backend_reg(cpu_dev);
  318. auto ggml_backend_dev_get_extra_bufts_fn = (ggml_backend_dev_get_extra_bufts_t)
  319. ggml_backend_reg_get_proc_address(cpu_reg, "ggml_backend_dev_get_extra_bufts");
  320. if (ggml_backend_dev_get_extra_bufts_fn) {
  321. ggml_backend_buffer_type_t * extra_bufts = ggml_backend_dev_get_extra_bufts_fn(cpu_dev);
  322. while (extra_bufts && *extra_bufts) {
  323. buft_list.emplace_back(cpu_dev, *extra_bufts);
  324. ++extra_bufts;
  325. }
  326. }
  327. }
  328. // add the CPU buffer type
  329. for (size_t i = 0; i < ggml_backend_dev_count(); ++i) {
  330. ggml_backend_dev_t dev = ggml_backend_dev_get(i);
  331. if (ggml_backend_dev_type(dev) == GGML_BACKEND_DEVICE_TYPE_CPU) {
  332. buft_list.emplace_back(dev, ggml_backend_dev_buffer_type(dev));
  333. }
  334. }
  335. return buft_list;
  336. }
  337. // GPU: split if LLAMA_SPLIT_MODE_ROW -> GPU
  338. static buft_list_t make_gpu_buft_list(ggml_backend_dev_t dev, llama_split_mode split_mode, const float * tensor_split) {
  339. buft_list_t buft_list;
  340. // add the device split buffer type if requested and available
  341. if (split_mode == LLAMA_SPLIT_MODE_ROW) {
  342. ggml_backend_reg_t reg = ggml_backend_dev_backend_reg(dev);
  343. auto ggml_backend_split_buffer_type_fn = (ggml_backend_split_buffer_type_t)
  344. ggml_backend_reg_get_proc_address(reg, "ggml_backend_split_buffer_type");
  345. if (ggml_backend_split_buffer_type_fn) {
  346. size_t dev_index = [&]() {
  347. auto * reg = ggml_backend_dev_backend_reg(dev);
  348. for (size_t i = 0; i < ggml_backend_reg_dev_count(reg); ++i) {
  349. if (ggml_backend_reg_dev_get(reg, i) == dev) {
  350. return i;
  351. }
  352. }
  353. throw std::runtime_error(format("device %s not found in its backend reg", ggml_backend_dev_name(dev)));
  354. }();
  355. auto * buft = ggml_backend_split_buffer_type_fn(dev_index, tensor_split);
  356. if (buft != nullptr) {
  357. buft_list.emplace_back(dev, buft);
  358. }
  359. }
  360. }
  361. // add the device default buffer type
  362. buft_list.emplace_back(dev, ggml_backend_dev_buffer_type(dev));
  363. return buft_list;
  364. }
  365. struct llama_model::impl {
  366. impl() {}
  367. ~impl() {}
  368. uint64_t n_elements = 0;
  369. size_t n_bytes = 0;
  370. std::string desc_str;
  371. // model memory mapped files
  372. llama_mmaps mappings;
  373. // objects representing data potentially being locked in memory
  374. llama_mlocks mlock_bufs;
  375. llama_mlocks mlock_mmaps;
  376. // contexts where the model tensors metadata is stored
  377. std::vector<ggml_context_ptr> ctxs;
  378. // the model memory buffers for the tensor data
  379. std::vector<ggml_backend_buffer_ptr> bufs;
  380. buft_list_t cpu_buft_list;
  381. std::map<ggml_backend_dev_t, buft_list_t> gpu_buft_list;
  382. struct layer_dev {
  383. ggml_backend_dev_t dev;
  384. buft_list_t * buft_list;
  385. };
  386. layer_dev dev_input = {};
  387. layer_dev dev_output = {};
  388. std::vector<layer_dev> dev_layer;
  389. bool has_tensor_overrides;
  390. };
  391. llama_model::llama_model(const llama_model_params & params) : params(params), pimpl(std::make_unique<impl>()) {
  392. pimpl->has_tensor_overrides = params.tensor_buft_overrides && params.tensor_buft_overrides[0].pattern;
  393. }
  394. llama_model::~llama_model() {}
  395. void llama_model::load_stats(llama_model_loader & ml) {
  396. pimpl->n_elements = ml.n_elements;
  397. pimpl->n_bytes = ml.n_bytes;
  398. }
  399. void llama_model::load_arch(llama_model_loader & ml) {
  400. arch = ml.get_arch();
  401. if (arch == LLM_ARCH_UNKNOWN) {
  402. throw std::runtime_error("unknown model architecture: '" + ml.get_arch_name() + "'");
  403. }
  404. }
  405. void llama_model::load_hparams(llama_model_loader & ml) {
  406. const gguf_context * ctx = ml.meta.get();
  407. // get metadata as string
  408. for (int i = 0; i < gguf_get_n_kv(ctx); i++) {
  409. gguf_type type = gguf_get_kv_type(ctx, i);
  410. if (type == GGUF_TYPE_ARRAY) {
  411. continue;
  412. }
  413. const char * name = gguf_get_key(ctx, i);
  414. const std::string value = gguf_kv_to_str(ctx, i);
  415. gguf_kv.emplace(name, value);
  416. }
  417. // get general kv
  418. ml.get_key(LLM_KV_GENERAL_NAME, name, false);
  419. // everything past this point is not vocab-related
  420. if (hparams.vocab_only) {
  421. return;
  422. }
  423. ml.get_key(LLM_KV_CONTEXT_LENGTH, hparams.n_ctx_train);
  424. ml.get_key(LLM_KV_EMBEDDING_LENGTH, hparams.n_embd);
  425. ml.get_key(LLM_KV_BLOCK_COUNT, hparams.n_layer);
  426. ml.get_key(LLM_KV_EXPERT_COUNT, hparams.n_expert, false);
  427. ml.get_key(LLM_KV_EXPERT_USED_COUNT, hparams.n_expert_used, false);
  428. if (arch == LLM_ARCH_WAVTOKENIZER_DEC) {
  429. ml.get_key(LLM_KV_FEATURES_LENGTH, hparams.n_embd_features);
  430. ml.get_key(LLM_KV_POSNET_EMBEDDING_LENGTH, hparams.posnet.n_embd);
  431. ml.get_key(LLM_KV_POSNET_BLOCK_COUNT, hparams.posnet.n_layer);
  432. ml.get_key(LLM_KV_CONVNEXT_EMBEDDING_LENGTH, hparams.convnext.n_embd);
  433. ml.get_key(LLM_KV_CONVNEXT_BLOCK_COUNT, hparams.convnext.n_layer);
  434. }
  435. GGML_ASSERT(hparams.n_expert <= LLAMA_MAX_EXPERTS);
  436. GGML_ASSERT(hparams.n_expert_used <= hparams.n_expert);
  437. if (hparams.n_expert > 0) {
  438. GGML_ASSERT(hparams.n_expert_used > 0);
  439. } else {
  440. GGML_ASSERT(hparams.n_expert_used == 0);
  441. }
  442. std::fill(hparams.n_head_arr.begin(), hparams.n_head_arr.end(), 0);
  443. std::fill(hparams.n_head_kv_arr.begin(), hparams.n_head_kv_arr.end(), 0);
  444. std::fill(hparams.n_ff_arr.begin(), hparams.n_ff_arr.end(), 0);
  445. std::fill(
  446. hparams.recurrent_layer_arr.begin(),
  447. hparams.recurrent_layer_arr.end(),
  448. llm_arch_is_recurrent(ml.get_arch()));
  449. std::fill(hparams.rope_sections.begin(), hparams.rope_sections.end(), 0);
  450. std::fill(hparams.swa_layers.begin(), hparams.swa_layers.end(), 0);
  451. ml.get_key_or_arr(LLM_KV_FEED_FORWARD_LENGTH, hparams.n_ff_arr, hparams.n_layer, false);
  452. ml.get_key_or_arr(LLM_KV_ATTENTION_HEAD_COUNT, hparams.n_head_arr, hparams.n_layer, false);
  453. // n_head_kv is optional, default to n_head
  454. hparams.n_head_kv_arr = hparams.n_head_arr;
  455. ml.get_key_or_arr(LLM_KV_ATTENTION_HEAD_COUNT_KV, hparams.n_head_kv_arr, hparams.n_layer, false);
  456. bool rope_finetuned = false;
  457. ml.get_key(LLM_KV_ROPE_SCALING_FINETUNED, rope_finetuned, false);
  458. hparams.rope_finetuned = rope_finetuned;
  459. hparams.n_ctx_orig_yarn = hparams.n_ctx_train;
  460. ml.get_key(LLM_KV_ROPE_SCALING_ORIG_CTX_LEN, hparams.n_ctx_orig_yarn, false);
  461. // rope_freq_base (optional)
  462. hparams.rope_freq_base_train = 10000.0f;
  463. ml.get_key(LLM_KV_ROPE_FREQ_BASE, hparams.rope_freq_base_train, false);
  464. std::string rope_scaling("linear");
  465. ml.get_key(LLM_KV_ROPE_SCALING_TYPE, rope_scaling, false);
  466. hparams.rope_scaling_type_train = llama_rope_scaling_type_from_string(rope_scaling);
  467. GGML_ASSERT(hparams.rope_scaling_type_train != LLAMA_ROPE_SCALING_TYPE_UNSPECIFIED);
  468. // rope_freq_scale (inverse of the kv) is optional
  469. float ropescale = 0.0f;
  470. if (!ml.get_key(LLM_KV_ROPE_SCALING_FACTOR, ropescale, false)) {
  471. // try the old key name
  472. ml.get_key(LLM_KV_ROPE_SCALE_LINEAR, ropescale, false);
  473. }
  474. hparams.rope_freq_scale_train = ropescale == 0.0f ? 1.0f : 1.0f/ropescale;
  475. // by default assume that the sliding-window layers use the same scaling type as the non-sliding-window layers
  476. hparams.rope_freq_base_train_swa = hparams.rope_freq_base_train;
  477. hparams.rope_freq_scale_train_swa = hparams.rope_freq_scale_train;
  478. ml.get_key(LLM_KV_ROPE_SCALING_ATTN_FACTOR, hparams.rope_attn_factor, false);
  479. // non-transformer models do not have attention heads
  480. if (hparams.n_head() > 0) {
  481. // gpt-neox n_rot = rotary_pct * (n_embd / n_head)
  482. // gpt-j n_rot = rotary_dim
  483. hparams.n_embd_head_k = hparams.n_embd / hparams.n_head();
  484. ml.get_key(LLM_KV_ATTENTION_KEY_LENGTH, hparams.n_embd_head_k, false);
  485. hparams.n_embd_head_v = hparams.n_embd / hparams.n_head();
  486. ml.get_key(LLM_KV_ATTENTION_VALUE_LENGTH, hparams.n_embd_head_v, false);
  487. // sanity check for n_rot (optional)
  488. hparams.n_rot = hparams.n_embd_head_k;
  489. ml.get_key(LLM_KV_ROPE_DIMENSION_COUNT, hparams.n_rot, false);
  490. if (arch == LLM_ARCH_LLAMA || arch == LLM_ARCH_DECI || arch == LLM_ARCH_FALCON) {
  491. if (hparams.n_rot != hparams.n_embd_head_k) {
  492. throw std::runtime_error(format("invalid n_rot: %u, expected %u", hparams.n_rot, hparams.n_embd_head_k));
  493. }
  494. }
  495. } else {
  496. hparams.n_rot = 0;
  497. hparams.n_embd_head_k = 0;
  498. hparams.n_embd_head_v = 0;
  499. }
  500. // for differentiating model types
  501. uint32_t n_vocab = 0;
  502. ml.get_key(LLM_KV_VOCAB_SIZE, n_vocab, false) || ml.get_arr_n(LLM_KV_TOKENIZER_LIST, n_vocab, false);
  503. // for classifier models
  504. ml.get_arr(LLM_KV_CLASSIFIER_OUTPUT_LABELS, classifier_labels, false);
  505. if (!classifier_labels.empty()) {
  506. hparams.n_cls_out = classifier_labels.size();
  507. }
  508. // arch-specific KVs
  509. switch (arch) {
  510. case LLM_ARCH_LLAMA:
  511. {
  512. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  513. if (hparams.n_expert == 8) {
  514. switch (hparams.n_layer) {
  515. case 32: type = LLM_TYPE_8x7B; break;
  516. case 56: type = LLM_TYPE_8x22B; break;
  517. default: type = LLM_TYPE_UNKNOWN;
  518. }
  519. } else {
  520. switch (hparams.n_layer) {
  521. case 16: type = LLM_TYPE_1B; break; // Llama 3.2 1B
  522. case 22: type = LLM_TYPE_1B; break;
  523. case 26: type = LLM_TYPE_3B; break;
  524. case 28: type = LLM_TYPE_3B; break; // Llama 3.2 3B
  525. case 30: type = LLM_TYPE_256M; break; // smoldocling 256M
  526. // granite uses a vocab with len 49152
  527. case 32: type = n_vocab == 49152 ? LLM_TYPE_3B : (n_vocab < 40000 ? LLM_TYPE_7B : LLM_TYPE_8B); break;
  528. case 36: type = LLM_TYPE_8B; break; // granite
  529. case 40: type = LLM_TYPE_13B; break;
  530. case 48: type = LLM_TYPE_34B; break;
  531. case 60: type = LLM_TYPE_30B; break;
  532. case 80: type = hparams.n_head() == hparams.n_head_kv() ? LLM_TYPE_65B : LLM_TYPE_70B; break;
  533. default: type = LLM_TYPE_UNKNOWN;
  534. }
  535. }
  536. } break;
  537. case LLM_ARCH_LLAMA4:
  538. {
  539. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  540. ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp);
  541. ml.get_key(LLM_KV_INTERLEAVE_MOE_LAYER_STEP, hparams.n_moe_layer_step);
  542. hparams.swa_type = LLAMA_SWA_TYPE_CHUNKED;
  543. hparams.n_swa = 8192; // should this be a gguf kv? currently it's the same for Scout and Maverick
  544. hparams.set_swa_pattern(4); // pattern: 3 chunked - 1 full
  545. switch (hparams.n_expert) {
  546. case 16: type = LLM_TYPE_17B_16E; break;
  547. case 128: type = LLM_TYPE_17B_128E; break;
  548. default: type = LLM_TYPE_UNKNOWN;
  549. }
  550. if (type == LLM_TYPE_17B_128E) {
  551. hparams.use_kq_norm = false;
  552. }
  553. } break;
  554. case LLM_ARCH_ARCEE:
  555. {
  556. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  557. // Arcee uses the same structure as Llama
  558. switch (hparams.n_layer) {
  559. case 36: type = LLM_TYPE_4B; break;
  560. default: type = LLM_TYPE_UNKNOWN;
  561. }
  562. } break;
  563. case LLM_ARCH_DECI:
  564. {
  565. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  566. switch (hparams.n_layer) {
  567. case 32: type = LLM_TYPE_7B; break;
  568. case 80: type = LLM_TYPE_70B; break;
  569. case 162: type = LLM_TYPE_405B; break;
  570. default: type = LLM_TYPE_UNKNOWN;
  571. }
  572. } break;
  573. case LLM_ARCH_MINICPM:
  574. {
  575. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  576. ml.get_key(LLM_KV_EMBEDDING_SCALE, hparams.f_embedding_scale);
  577. ml.get_key(LLM_KV_RESIDUAL_SCALE, hparams.f_residual_scale);
  578. ml.get_key(LLM_KV_LOGIT_SCALE, hparams.f_logit_scale);
  579. // MiniCPM uses rope by default, unlike Granite which uses it as a switch
  580. hparams.rope_finetuned = true;
  581. switch (hparams.n_layer) {
  582. case 52: type = LLM_TYPE_1B; break;
  583. case 40: type = LLM_TYPE_2B; break;
  584. default: type = LLM_TYPE_UNKNOWN;
  585. }
  586. } break;
  587. case LLM_ARCH_MINICPM3:
  588. {
  589. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  590. ml.get_key(LLM_KV_ATTENTION_Q_LORA_RANK, hparams.n_lora_q);
  591. ml.get_key(LLM_KV_ATTENTION_KV_LORA_RANK, hparams.n_lora_kv);
  592. switch (hparams.n_layer) {
  593. case 62: type = LLM_TYPE_4B; break;
  594. default: type = LLM_TYPE_UNKNOWN;
  595. }
  596. } break;
  597. case LLM_ARCH_GROK:
  598. {
  599. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  600. switch (hparams.n_layer) {
  601. case 64: type = LLM_TYPE_314B; break;
  602. default: type = LLM_TYPE_UNKNOWN;
  603. }
  604. } break;
  605. case LLM_ARCH_FALCON:
  606. {
  607. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  608. switch (hparams.n_layer) {
  609. case 32: type = LLM_TYPE_7B; break;
  610. case 60: type = LLM_TYPE_40B; break;
  611. default: type = LLM_TYPE_UNKNOWN;
  612. }
  613. } break;
  614. case LLM_ARCH_BAICHUAN:
  615. {
  616. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  617. switch (hparams.n_layer) {
  618. case 32: type = LLM_TYPE_7B; break;
  619. case 40: type = LLM_TYPE_13B; break;
  620. default: type = LLM_TYPE_UNKNOWN;
  621. }
  622. if (type == LLM_TYPE_13B) {
  623. // TODO: become GGUF KV parameter
  624. hparams.f_max_alibi_bias = 8.0f;
  625. }
  626. } break;
  627. case LLM_ARCH_STARCODER:
  628. {
  629. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  630. switch (hparams.n_layer) {
  631. case 24: type = LLM_TYPE_1B; break;
  632. case 36: type = LLM_TYPE_3B; break;
  633. case 42: type = LLM_TYPE_7B; break;
  634. case 40: type = LLM_TYPE_15B; break;
  635. default: type = LLM_TYPE_UNKNOWN;
  636. }
  637. } break;
  638. case LLM_ARCH_REFACT:
  639. {
  640. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  641. switch (hparams.n_layer) {
  642. case 32: type = LLM_TYPE_1B; break;
  643. default: type = LLM_TYPE_UNKNOWN;
  644. }
  645. // TODO: become GGUF KV parameter
  646. hparams.f_max_alibi_bias = 8.0f;
  647. } break;
  648. case LLM_ARCH_BERT:
  649. {
  650. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  651. ml.get_key(LLM_KV_ATTENTION_CAUSAL, hparams.causal_attn);
  652. ml.get_key(LLM_KV_POOLING_TYPE, hparams.pooling_type, false);
  653. switch (hparams.n_layer) {
  654. case 3:
  655. type = LLM_TYPE_17M; break; // bge-micro
  656. case 6:
  657. type = LLM_TYPE_22M; break; // MiniLM-L6
  658. case 12:
  659. switch (hparams.n_embd) {
  660. case 384: type = LLM_TYPE_33M; break; // MiniLM-L12, bge-small
  661. case 768: type = LLM_TYPE_109M; break; // bge-base
  662. default: type = LLM_TYPE_UNKNOWN;
  663. } break;
  664. case 24:
  665. type = LLM_TYPE_335M; break; // bge-large
  666. default: type = LLM_TYPE_UNKNOWN;
  667. }
  668. } break;
  669. case LLM_ARCH_JINA_BERT_V2:
  670. {
  671. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  672. ml.get_key(LLM_KV_ATTENTION_CAUSAL, hparams.causal_attn);
  673. ml.get_key(LLM_KV_POOLING_TYPE, hparams.pooling_type, false);
  674. hparams.f_max_alibi_bias = 8.0f;
  675. switch (hparams.n_layer) {
  676. case 4: type = LLM_TYPE_33M; break; // jina-embeddings-small
  677. case 12: type = LLM_TYPE_137M; break; // jina-embeddings-base
  678. default: type = LLM_TYPE_UNKNOWN;
  679. }
  680. } break;
  681. case LLM_ARCH_NOMIC_BERT:
  682. case LLM_ARCH_NOMIC_BERT_MOE:
  683. {
  684. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  685. ml.get_key(LLM_KV_ATTENTION_CAUSAL, hparams.causal_attn);
  686. ml.get_key(LLM_KV_POOLING_TYPE, hparams.pooling_type);
  687. ml.get_key(LLM_KV_MOE_EVERY_N_LAYERS, hparams.moe_every_n_layers, 0);
  688. if (hparams.n_layer == 12 && hparams.n_embd == 768) {
  689. if (arch == LLM_ARCH_NOMIC_BERT) {
  690. type = LLM_TYPE_137M;
  691. } else if (arch == LLM_ARCH_NOMIC_BERT_MOE && hparams.moe_every_n_layers == 2) {
  692. type = LLM_TYPE_475M;
  693. }
  694. }
  695. } break;
  696. case LLM_ARCH_NEO_BERT:
  697. {
  698. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  699. ml.get_key(LLM_KV_ATTENTION_CAUSAL, hparams.causal_attn);
  700. ml.get_key(LLM_KV_POOLING_TYPE, hparams.pooling_type);
  701. if (hparams.n_layer == 28) {
  702. type = LLM_TYPE_250M;
  703. }
  704. } break;
  705. case LLM_ARCH_BLOOM:
  706. {
  707. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  708. switch (hparams.n_layer) {
  709. case 24: type = LLM_TYPE_1B; break;
  710. case 30:
  711. switch (hparams.n_embd) {
  712. case 2560: type = LLM_TYPE_3B; break;
  713. case 4096: type = LLM_TYPE_7B; break;
  714. default: type = LLM_TYPE_UNKNOWN;
  715. } 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_MPT:
  722. {
  723. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  724. ml.get_key(LLM_KV_ATTENTION_CLAMP_KQV, hparams.f_clamp_kqv, false);
  725. ml.get_key(LLM_KV_ATTENTION_MAX_ALIBI_BIAS, hparams.f_max_alibi_bias);
  726. switch (hparams.n_layer) {
  727. case 32: type = LLM_TYPE_7B; break;
  728. case 48: type = LLM_TYPE_30B; break;
  729. default: type = LLM_TYPE_UNKNOWN;
  730. }
  731. } break;
  732. case LLM_ARCH_STABLELM:
  733. {
  734. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  735. switch (hparams.n_layer) {
  736. case 24: type = LLM_TYPE_1B; break;
  737. case 32: type = LLM_TYPE_3B; break;
  738. case 40: type = LLM_TYPE_12B; break;
  739. default: type = LLM_TYPE_UNKNOWN;
  740. }
  741. } break;
  742. case LLM_ARCH_QWEN:
  743. {
  744. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  745. switch (hparams.n_layer) {
  746. case 32: type = LLM_TYPE_7B; break;
  747. case 40: type = LLM_TYPE_13B; break;
  748. default: type = LLM_TYPE_UNKNOWN;
  749. }
  750. } break;
  751. case LLM_ARCH_QWEN2VL:
  752. {
  753. ml.get_key_or_arr(LLM_KV_ROPE_DIMENSION_SECTIONS, hparams.rope_sections, 4, true);
  754. }
  755. // fall through
  756. case LLM_ARCH_QWEN2:
  757. {
  758. ml.get_key(LLM_KV_POOLING_TYPE, hparams.pooling_type, false);
  759. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  760. switch (hparams.n_layer) {
  761. case 24: type = hparams.n_embd == 1024 ? LLM_TYPE_0_5B : LLM_TYPE_1B; break;
  762. case 28: type = hparams.n_embd == 1536 ? LLM_TYPE_1_5B : LLM_TYPE_7B; break;
  763. case 32: type = LLM_TYPE_7B; break;
  764. case 36: type = LLM_TYPE_3B; break;
  765. case 40: type = hparams.n_head() == 20 ? LLM_TYPE_4B : LLM_TYPE_13B; break;
  766. case 48: type = LLM_TYPE_14B; break;
  767. case 64: type = LLM_TYPE_32B; break;
  768. case 80: type = LLM_TYPE_70B; break;
  769. default: type = LLM_TYPE_UNKNOWN;
  770. }
  771. } break;
  772. case LLM_ARCH_DREAM:
  773. {
  774. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  775. // Dream models are primarily 7B with 28 layers
  776. switch (hparams.n_layer) {
  777. case 28:
  778. type = LLM_TYPE_7B;
  779. break;
  780. default:
  781. type = LLM_TYPE_UNKNOWN;
  782. }
  783. // Set non-causal attention for diffusion models
  784. hparams.causal_attn = false;
  785. }
  786. break;
  787. case LLM_ARCH_LLADA:
  788. {
  789. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  790. // LLaDA-8B has 32 layers, similar to LLaMA but for diffusion
  791. switch (hparams.n_layer) {
  792. case 32:
  793. type = LLM_TYPE_8B;
  794. break;
  795. default:
  796. type = LLM_TYPE_UNKNOWN;
  797. }
  798. // Set non-causal attention for diffusion models
  799. hparams.causal_attn = false;
  800. }
  801. break;
  802. case LLM_ARCH_QWEN2MOE:
  803. {
  804. ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp, false);
  805. ml.get_key(LLM_KV_EXPERT_SHARED_FEED_FORWARD_LENGTH, hparams.n_ff_shexp, false);
  806. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  807. switch (hparams.n_layer) {
  808. case 24: type = LLM_TYPE_A2_7B; break;
  809. case 28: type = LLM_TYPE_57B_A14B; break;
  810. default: type = LLM_TYPE_UNKNOWN;
  811. }
  812. } break;
  813. case LLM_ARCH_QWEN3:
  814. {
  815. ml.get_key(LLM_KV_POOLING_TYPE, hparams.pooling_type, false);
  816. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  817. switch (hparams.n_layer) {
  818. case 28: type = hparams.n_embd == 1024 ? LLM_TYPE_0_6B : LLM_TYPE_1_7B; break;
  819. case 36: type = hparams.n_embd == 2560 ? LLM_TYPE_4B : LLM_TYPE_8B; break;
  820. case 40: type = LLM_TYPE_14B; break;
  821. case 64: type = LLM_TYPE_32B; break;
  822. default: type = LLM_TYPE_UNKNOWN;
  823. }
  824. } break;
  825. case LLM_ARCH_QWEN3MOE:
  826. {
  827. ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp, false);
  828. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  829. switch (hparams.n_layer) {
  830. case 48: type = LLM_TYPE_30B_A3B; break;
  831. case 94: type = LLM_TYPE_235B_A22B; break;
  832. default: type = LLM_TYPE_UNKNOWN;
  833. }
  834. } break;
  835. case LLM_ARCH_PHI2:
  836. {
  837. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  838. switch (hparams.n_layer) {
  839. case 24: type = LLM_TYPE_1B; break;
  840. case 32: type = LLM_TYPE_3B; break;
  841. default: type = LLM_TYPE_UNKNOWN;
  842. }
  843. } break;
  844. case LLM_ARCH_PHI3:
  845. {
  846. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  847. switch (hparams.n_layer) {
  848. case 24: type = LLM_TYPE_1B; break;
  849. case 32: type = LLM_TYPE_3B; break;
  850. case 40: type = LLM_TYPE_14B; break;
  851. default: type = LLM_TYPE_UNKNOWN;
  852. }
  853. const bool found_swa = ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa, false);
  854. if (found_swa && hparams.n_swa > 0) {
  855. LLAMA_LOG_WARN("%s: Phi SWA is currently disabled - results might be suboptimal for some models (see %s)\n",
  856. __func__, "https://github.com/ggml-org/llama.cpp/pull/13676");
  857. // TODO: fix conversion scripts to correctly populate `n_swa` and `n_swa_pattern`
  858. hparams.swa_type = LLAMA_SWA_TYPE_NONE;
  859. hparams.n_swa = 0;
  860. hparams.set_swa_pattern(1);
  861. }
  862. } break;
  863. case LLM_ARCH_PHIMOE:
  864. {
  865. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  866. switch (hparams.n_layer) {
  867. case 32: type = LLM_TYPE_16x3_8B; break;
  868. default: type = LLM_TYPE_UNKNOWN;
  869. }
  870. } break;
  871. case LLM_ARCH_PLAMO:
  872. {
  873. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  874. switch (hparams.n_layer) {
  875. case 40: type = LLM_TYPE_13B; break;
  876. default: type = LLM_TYPE_UNKNOWN;
  877. }
  878. } break;
  879. case LLM_ARCH_PLAMO2:
  880. {
  881. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  882. // Load Mamba SSM parameters
  883. ml.get_key(LLM_KV_SSM_CONV_KERNEL, hparams.ssm_d_conv);
  884. ml.get_key(LLM_KV_SSM_INNER_SIZE, hparams.ssm_d_inner);
  885. ml.get_key(LLM_KV_SSM_STATE_SIZE, hparams.ssm_d_state);
  886. ml.get_key(LLM_KV_SSM_TIME_STEP_RANK, hparams.ssm_dt_rank);
  887. ml.get_key(LLM_KV_SSM_GROUP_COUNT, hparams.ssm_n_group);
  888. for (uint32_t i = 0; i < hparams.n_layer; ++i) {
  889. hparams.recurrent_layer_arr[i] = hparams.n_head_kv(i) == 0;
  890. }
  891. switch (hparams.n_layer) {
  892. case 16: type = LLM_TYPE_1B; break;
  893. case 32:
  894. if (hparams.n_embd == 2048) {
  895. type = LLM_TYPE_2B;
  896. } else if (hparams.n_embd == 4096) {
  897. type = LLM_TYPE_8B;
  898. }
  899. break;
  900. default: type = LLM_TYPE_UNKNOWN;
  901. }
  902. } break;
  903. case LLM_ARCH_GPT2:
  904. {
  905. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  906. switch (hparams.n_layer) {
  907. case 12: type = LLM_TYPE_SMALL; break;
  908. case 24: type = LLM_TYPE_MEDIUM; break;
  909. case 36: type = LLM_TYPE_LARGE; break;
  910. case 48: type = LLM_TYPE_XL; break;
  911. default: type = LLM_TYPE_UNKNOWN;
  912. }
  913. } break;
  914. case LLM_ARCH_CODESHELL:
  915. {
  916. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  917. switch (hparams.n_layer) {
  918. case 42: type = LLM_TYPE_7B; break;
  919. default: type = LLM_TYPE_UNKNOWN;
  920. }
  921. } break;
  922. case LLM_ARCH_ORION:
  923. {
  924. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  925. switch (hparams.n_layer) {
  926. case 40: type = LLM_TYPE_14B; break;
  927. default: type = LLM_TYPE_UNKNOWN;
  928. }
  929. } break;
  930. case LLM_ARCH_INTERNLM2:
  931. {
  932. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  933. switch (hparams.n_layer) {
  934. case 32: type = LLM_TYPE_7B; break;
  935. case 48: type = LLM_TYPE_20B; break;
  936. default: type = LLM_TYPE_UNKNOWN;
  937. }
  938. } break;
  939. case LLM_ARCH_GEMMA:
  940. {
  941. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  942. switch (hparams.n_layer) {
  943. case 18: type = LLM_TYPE_2B; break;
  944. case 28: type = LLM_TYPE_7B; break;
  945. default: type = LLM_TYPE_UNKNOWN;
  946. }
  947. } break;
  948. case LLM_ARCH_GEMMA2:
  949. {
  950. hparams.swa_type = LLAMA_SWA_TYPE_STANDARD;
  951. hparams.n_swa = 4096; // default value of gemma 2
  952. hparams.set_swa_pattern(2);
  953. hparams.attn_soft_cap = true;
  954. ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa, false);
  955. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  956. ml.get_key(LLM_KV_ATTN_LOGIT_SOFTCAPPING, hparams.f_attn_logit_softcapping, false);
  957. ml.get_key(LLM_KV_FINAL_LOGIT_SOFTCAPPING, hparams.f_final_logit_softcapping, false);
  958. switch (hparams.n_layer) {
  959. case 26: type = LLM_TYPE_2B; break;
  960. case 42: type = LLM_TYPE_9B; break;
  961. case 46: type = LLM_TYPE_27B; break;
  962. default: type = LLM_TYPE_UNKNOWN;
  963. }
  964. // ref: https://github.com/google/gemma_pytorch/blob/014acb7ac4563a5f77c76d7ff98f31b568c16508/gemma/config.py#L173
  965. hparams.f_attention_scale = type == LLM_TYPE_27B
  966. ? 1.0f / std::sqrt(float(hparams.n_embd / hparams.n_head(0)))
  967. : 1.0f / std::sqrt(float(hparams.n_embd_head_k));
  968. } break;
  969. case LLM_ARCH_GEMMA3:
  970. {
  971. hparams.swa_type = LLAMA_SWA_TYPE_STANDARD;
  972. hparams.set_swa_pattern(6);
  973. hparams.rope_freq_base_train_swa = 10000.0f;
  974. hparams.rope_freq_scale_train_swa = 1.0f;
  975. ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa);
  976. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  977. switch (hparams.n_layer) {
  978. case 18: type = LLM_TYPE_537M; break;
  979. case 26: type = LLM_TYPE_1B; break;
  980. case 34: type = LLM_TYPE_4B; break;
  981. case 48: type = LLM_TYPE_12B; break;
  982. case 62: type = LLM_TYPE_27B; break;
  983. default: type = LLM_TYPE_UNKNOWN;
  984. }
  985. // ref: https://github.com/google/gemma_pytorch/blob/014acb7ac4563a5f77c76d7ff98f31b568c16508/gemma/config.py#L289
  986. hparams.f_attention_scale = type == LLM_TYPE_27B
  987. ? 1.0f / std::sqrt(float(hparams.n_embd / hparams.n_head(0)))
  988. : 1.0f / std::sqrt(float(hparams.n_embd_head_k));
  989. } break;
  990. case LLM_ARCH_GEMMA3N:
  991. {
  992. hparams.swa_type = LLAMA_SWA_TYPE_STANDARD;
  993. hparams.set_swa_pattern(5);
  994. hparams.rope_freq_base_train_swa = 10000.0f;
  995. hparams.rope_freq_scale_train_swa = 1.0f;
  996. hparams.f_attention_scale = 1.0f;
  997. ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa);
  998. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  999. switch (hparams.n_layer) {
  1000. case 30: type = LLM_TYPE_E2B; break;
  1001. case 35: type = LLM_TYPE_E4B; break;
  1002. default: type = LLM_TYPE_UNKNOWN;
  1003. }
  1004. } break;
  1005. case LLM_ARCH_STARCODER2:
  1006. {
  1007. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  1008. switch (hparams.n_layer) {
  1009. case 30: type = LLM_TYPE_3B; break;
  1010. case 32: type = LLM_TYPE_7B; break;
  1011. case 40: type = LLM_TYPE_15B; break;
  1012. case 52: type = LLM_TYPE_20B; break; // granite
  1013. case 88: type = LLM_TYPE_34B; break; // granite
  1014. default: type = LLM_TYPE_UNKNOWN;
  1015. }
  1016. } break;
  1017. case LLM_ARCH_MAMBA:
  1018. {
  1019. ml.get_key(LLM_KV_SSM_CONV_KERNEL, hparams.ssm_d_conv);
  1020. ml.get_key(LLM_KV_SSM_INNER_SIZE, hparams.ssm_d_inner);
  1021. ml.get_key(LLM_KV_SSM_STATE_SIZE, hparams.ssm_d_state);
  1022. ml.get_key(LLM_KV_SSM_TIME_STEP_RANK, hparams.ssm_dt_rank);
  1023. ml.get_key(LLM_KV_SSM_DT_B_C_RMS, hparams.ssm_dt_b_c_rms, false);
  1024. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1025. switch (hparams.n_layer) {
  1026. case 24:
  1027. switch (hparams.n_embd) {
  1028. case 768: type = LLM_TYPE_SMALL; break;
  1029. default: type = LLM_TYPE_UNKNOWN;
  1030. } break;
  1031. case 48:
  1032. switch (hparams.n_embd) {
  1033. case 1024: type = LLM_TYPE_MEDIUM; break;
  1034. case 1536: type = LLM_TYPE_LARGE; break;
  1035. case 2048: type = LLM_TYPE_XL; break;
  1036. default: type = LLM_TYPE_UNKNOWN;
  1037. } break;
  1038. case 64:
  1039. switch (hparams.n_embd) {
  1040. case 2560: type = LLM_TYPE_3B; break;
  1041. default: type = LLM_TYPE_UNKNOWN;
  1042. } break;
  1043. default: type = LLM_TYPE_UNKNOWN;
  1044. }
  1045. } break;
  1046. case LLM_ARCH_MAMBA2:
  1047. {
  1048. ml.get_key(LLM_KV_SSM_CONV_KERNEL, hparams.ssm_d_conv);
  1049. ml.get_key(LLM_KV_SSM_INNER_SIZE, hparams.ssm_d_inner);
  1050. ml.get_key(LLM_KV_SSM_STATE_SIZE, hparams.ssm_d_state);
  1051. ml.get_key(LLM_KV_SSM_TIME_STEP_RANK, hparams.ssm_dt_rank);
  1052. ml.get_key(LLM_KV_SSM_GROUP_COUNT, hparams.ssm_n_group);
  1053. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1054. switch (hparams.n_layer) {
  1055. case 24:
  1056. switch (hparams.n_embd) {
  1057. case 768: type = LLM_TYPE_SMALL; break;
  1058. default: type = LLM_TYPE_UNKNOWN;
  1059. } break;
  1060. case 48:
  1061. switch (hparams.n_embd) {
  1062. case 1024: type = LLM_TYPE_MEDIUM; break;
  1063. case 1536: type = LLM_TYPE_LARGE; break;
  1064. case 2048: type = LLM_TYPE_XL; break;
  1065. default: type = LLM_TYPE_UNKNOWN;
  1066. } break;
  1067. case 64:
  1068. switch (hparams.n_embd) {
  1069. case 2560: type = LLM_TYPE_3B; break;
  1070. case 4096: type = LLM_TYPE_7B; break;
  1071. default: type = LLM_TYPE_UNKNOWN;
  1072. } break;
  1073. default: type = LLM_TYPE_UNKNOWN;
  1074. }
  1075. } break;
  1076. case LLM_ARCH_JAMBA:
  1077. {
  1078. ml.get_key(LLM_KV_SSM_CONV_KERNEL, hparams.ssm_d_conv);
  1079. ml.get_key(LLM_KV_SSM_INNER_SIZE, hparams.ssm_d_inner);
  1080. ml.get_key(LLM_KV_SSM_STATE_SIZE, hparams.ssm_d_state);
  1081. ml.get_key(LLM_KV_SSM_TIME_STEP_RANK, hparams.ssm_dt_rank);
  1082. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1083. for (uint32_t i = 0; i < hparams.n_layer; ++i) {
  1084. hparams.recurrent_layer_arr[i] = hparams.n_head_kv(i) == 0;
  1085. }
  1086. switch (hparams.n_layer) {
  1087. // TODO: Jamba layers are a bit heterogenous, so naming this is hard.
  1088. case 12: // 900M 8x???M
  1089. case 32: // 51B 16x?B
  1090. default: type = LLM_TYPE_UNKNOWN;
  1091. }
  1092. } break;
  1093. case LLM_ARCH_XVERSE:
  1094. {
  1095. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1096. switch (hparams.n_layer) {
  1097. case 32: type = LLM_TYPE_7B; break;
  1098. case 40: type = LLM_TYPE_13B; break;
  1099. case 80: type = LLM_TYPE_65B; break;
  1100. default: type = LLM_TYPE_UNKNOWN;
  1101. }
  1102. } break;
  1103. case LLM_ARCH_COMMAND_R:
  1104. {
  1105. ml.get_key(LLM_KV_LOGIT_SCALE, hparams.f_logit_scale);
  1106. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  1107. switch (hparams.n_layer) {
  1108. case 40: type = LLM_TYPE_35B; break;
  1109. default: type = LLM_TYPE_UNKNOWN;
  1110. }
  1111. } break;
  1112. case LLM_ARCH_COHERE2:
  1113. {
  1114. hparams.swa_type = LLAMA_SWA_TYPE_STANDARD;
  1115. hparams.set_swa_pattern(4);
  1116. ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa);
  1117. ml.get_key(LLM_KV_LOGIT_SCALE, hparams.f_logit_scale);
  1118. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  1119. switch (hparams.n_layer) {
  1120. case 32: type = LLM_TYPE_8B; break;
  1121. default: type = LLM_TYPE_UNKNOWN;
  1122. }
  1123. } break;
  1124. case LLM_ARCH_DBRX:
  1125. {
  1126. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  1127. ml.get_key(LLM_KV_ATTENTION_CLAMP_KQV, hparams.f_clamp_kqv);
  1128. switch (hparams.n_layer) {
  1129. case 40: type = LLM_TYPE_16x12B; break;
  1130. default: type = LLM_TYPE_UNKNOWN;
  1131. }
  1132. } break;
  1133. case LLM_ARCH_OLMO:
  1134. {
  1135. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  1136. ml.get_key(LLM_KV_ATTENTION_CLAMP_KQV, hparams.f_clamp_kqv, false);
  1137. switch (hparams.n_layer) {
  1138. case 22: type = LLM_TYPE_1B; break;
  1139. case 32: type = LLM_TYPE_7B; break;
  1140. case 80: type = LLM_TYPE_70B; break;
  1141. default: type = LLM_TYPE_UNKNOWN;
  1142. }
  1143. } break;
  1144. case LLM_ARCH_OLMO2:
  1145. {
  1146. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1147. switch (hparams.n_layer) {
  1148. case 16: type = LLM_TYPE_1B; break;
  1149. case 32: type = LLM_TYPE_7B; break;
  1150. case 40: type = LLM_TYPE_13B; break;
  1151. case 64: type = LLM_TYPE_32B; break;
  1152. default: type = LLM_TYPE_UNKNOWN;
  1153. }
  1154. } break;
  1155. case LLM_ARCH_OLMOE:
  1156. {
  1157. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1158. switch (hparams.n_layer) {
  1159. case 16: type = LLM_TYPE_A1_7B; break;
  1160. default: type = LLM_TYPE_UNKNOWN;
  1161. }
  1162. } break;
  1163. case LLM_ARCH_OPENELM:
  1164. {
  1165. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1166. switch (hparams.n_layer) {
  1167. case 16: type = LLM_TYPE_270M; break;
  1168. case 20: type = LLM_TYPE_450M; break;
  1169. case 28: type = LLM_TYPE_1B; break;
  1170. case 36: type = LLM_TYPE_3B; break;
  1171. default: type = LLM_TYPE_UNKNOWN;
  1172. }
  1173. } break;
  1174. case LLM_ARCH_GPTNEOX:
  1175. {
  1176. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  1177. ml.get_key(LLM_KV_USE_PARALLEL_RESIDUAL, hparams.use_par_res);
  1178. switch (hparams.n_layer) {
  1179. case 6:
  1180. switch (hparams.n_ff()) {
  1181. case 512: type = LLM_TYPE_14M; break;
  1182. case 2048: type = LLM_TYPE_70M; break;
  1183. default: type = LLM_TYPE_UNKNOWN;
  1184. } break;
  1185. case 12:
  1186. switch (hparams.n_ff()) {
  1187. case 3072: type = LLM_TYPE_160M; break;
  1188. default: type = LLM_TYPE_UNKNOWN;
  1189. } break;
  1190. case 16:
  1191. switch (hparams.n_ff()) {
  1192. case 8192: type = LLM_TYPE_1B; break;
  1193. default: type = LLM_TYPE_UNKNOWN;
  1194. } break;
  1195. case 24:
  1196. switch (hparams.n_ff()) {
  1197. case 4096: type = LLM_TYPE_410M; break;
  1198. case 8192: type = LLM_TYPE_1_4B; break;
  1199. default: type = LLM_TYPE_UNKNOWN;
  1200. } break;
  1201. case 32:
  1202. switch (hparams.n_ff()) {
  1203. case 10240: type = LLM_TYPE_2_8B; break;
  1204. case 16384: type = LLM_TYPE_6_9B; break;
  1205. default: type = LLM_TYPE_UNKNOWN;
  1206. } break;
  1207. case 36:
  1208. switch (hparams.n_ff()) {
  1209. case 20480: type = LLM_TYPE_12B; break;
  1210. default: type = LLM_TYPE_UNKNOWN;
  1211. } break;
  1212. case 44:
  1213. switch (hparams.n_ff()) {
  1214. case 24576: type = LLM_TYPE_20B; break;
  1215. default: type = LLM_TYPE_UNKNOWN;
  1216. } break;
  1217. default: type = LLM_TYPE_UNKNOWN;
  1218. }
  1219. } break;
  1220. case LLM_ARCH_ARCTIC:
  1221. {
  1222. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1223. if (hparams.n_expert == 128) {
  1224. switch (hparams.n_layer) {
  1225. case 35: type = LLM_TYPE_10B_128x3_66B; break;
  1226. default: type = LLM_TYPE_UNKNOWN;
  1227. }
  1228. } else {
  1229. type = LLM_TYPE_UNKNOWN;
  1230. }
  1231. } break;
  1232. case LLM_ARCH_DEEPSEEK:
  1233. {
  1234. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1235. ml.get_key(LLM_KV_LEADING_DENSE_BLOCK_COUNT, hparams.n_layer_dense_lead);
  1236. ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp);
  1237. ml.get_key(LLM_KV_EXPERT_SHARED_COUNT, hparams.n_expert_shared);
  1238. ml.get_key(LLM_KV_EXPERT_WEIGHTS_SCALE, hparams.expert_weights_scale);
  1239. switch (hparams.n_layer) {
  1240. case 28: type = LLM_TYPE_20B; break;
  1241. default: type = LLM_TYPE_UNKNOWN;
  1242. }
  1243. } break;
  1244. case LLM_ARCH_DEEPSEEK2:
  1245. {
  1246. bool is_lite = (hparams.n_layer == 27);
  1247. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1248. ml.get_key(LLM_KV_LEADING_DENSE_BLOCK_COUNT, hparams.n_layer_dense_lead);
  1249. if (!is_lite) {
  1250. ml.get_key(LLM_KV_ATTENTION_Q_LORA_RANK, hparams.n_lora_q);
  1251. }
  1252. ml.get_key(LLM_KV_ATTENTION_KV_LORA_RANK, hparams.n_lora_kv);
  1253. ml.get_key(LLM_KV_ATTENTION_KEY_LENGTH_MLA, hparams.n_embd_head_k_mla, false);
  1254. ml.get_key(LLM_KV_ATTENTION_VALUE_LENGTH_MLA, hparams.n_embd_head_v_mla, false);
  1255. ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp);
  1256. ml.get_key(LLM_KV_EXPERT_SHARED_COUNT, hparams.n_expert_shared);
  1257. ml.get_key(LLM_KV_EXPERT_WEIGHTS_SCALE, hparams.expert_weights_scale);
  1258. ml.get_key(LLM_KV_EXPERT_WEIGHTS_NORM, hparams.expert_weights_norm, false);
  1259. ml.get_key(LLM_KV_EXPERT_GATING_FUNC, hparams.expert_gating_func, false);
  1260. if (hparams.expert_gating_func == LLAMA_EXPERT_GATING_FUNC_TYPE_NONE) {
  1261. // for compatibility with existing DeepSeek V2 and V2.5 GGUFs
  1262. // that have no expert_gating_func model parameter set
  1263. hparams.expert_gating_func = LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX;
  1264. }
  1265. ml.get_key(LLM_KV_ROPE_SCALING_YARN_LOG_MUL, hparams.rope_yarn_log_mul, false);
  1266. switch (hparams.n_layer) {
  1267. case 27: type = LLM_TYPE_16B; break;
  1268. case 60: type = LLM_TYPE_236B; break;
  1269. case 61: type = LLM_TYPE_671B; break;
  1270. default: type = LLM_TYPE_UNKNOWN;
  1271. }
  1272. } break;
  1273. case LLM_ARCH_PLM:
  1274. {
  1275. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1276. ml.get_key(LLM_KV_ATTENTION_KV_LORA_RANK, hparams.n_lora_kv);
  1277. switch (hparams.n_layer) {
  1278. case 32: type = LLM_TYPE_1_8B; break;
  1279. default: type = LLM_TYPE_UNKNOWN;
  1280. }
  1281. } break;
  1282. case LLM_ARCH_CHATGLM:
  1283. {
  1284. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1285. switch (hparams.n_layer) {
  1286. case 28: {
  1287. if (hparams.n_head(0) == 16) {
  1288. type = LLM_TYPE_1_5B;
  1289. } else {
  1290. type = LLM_TYPE_6B;
  1291. }
  1292. } break;
  1293. case 40: {
  1294. if (hparams.n_head(0) == 24) {
  1295. type = LLM_TYPE_4B;
  1296. } else {
  1297. type = LLM_TYPE_9B;
  1298. }
  1299. } break;
  1300. default: type = LLM_TYPE_UNKNOWN;
  1301. }
  1302. } break;
  1303. case LLM_ARCH_GLM4:
  1304. {
  1305. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1306. switch (hparams.n_layer) {
  1307. case 40: type = LLM_TYPE_9B; break;
  1308. case 61: type = LLM_TYPE_32B; break;
  1309. default: type = LLM_TYPE_UNKNOWN;
  1310. }
  1311. } break;
  1312. case LLM_ARCH_GLM4_MOE:
  1313. {
  1314. ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp);
  1315. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1316. // MoE parameters
  1317. ml.get_key(LLM_KV_EXPERT_COUNT, hparams.n_expert);
  1318. ml.get_key(LLM_KV_EXPERT_USED_COUNT, hparams.n_expert_used);
  1319. ml.get_key(LLM_KV_EXPERT_SHARED_COUNT, hparams.n_expert_shared);
  1320. ml.get_key(LLM_KV_LEADING_DENSE_BLOCK_COUNT, hparams.n_layer_dense_lead, false);
  1321. ml.get_key(LLM_KV_EXPERT_WEIGHTS_SCALE, hparams.expert_weights_scale);
  1322. ml.get_key(LLM_KV_EXPERT_WEIGHTS_NORM, hparams.expert_weights_norm, false);
  1323. // Expert gating function (GLM-4.5 uses sigmoid)
  1324. ml.get_key(LLM_KV_EXPERT_GATING_FUNC, hparams.expert_gating_func, false);
  1325. if (hparams.expert_gating_func == LLAMA_EXPERT_GATING_FUNC_TYPE_NONE) {
  1326. hparams.expert_gating_func = LLAMA_EXPERT_GATING_FUNC_TYPE_SIGMOID;
  1327. }
  1328. // NextN/MTP parameters
  1329. ml.get_key(LLM_KV_NEXTN_PREDICT_LAYERS, hparams.nextn_predict_layers, false);
  1330. switch (hparams.n_layer) {
  1331. case 47: type = LLM_TYPE_106B_A12B; break; // GLM-4.5-Air (46 layers + 1 NextN layer)
  1332. case 93: type = LLM_TYPE_355B_A32B; break; // GLM-4.5 (92 layers + 1 NextN layer)
  1333. default: type = LLM_TYPE_UNKNOWN;
  1334. }
  1335. } break;
  1336. case LLM_ARCH_BITNET:
  1337. {
  1338. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1339. switch (hparams.n_layer) {
  1340. case 26: type = LLM_TYPE_3B; break;
  1341. default: type = LLM_TYPE_UNKNOWN;
  1342. }
  1343. } break;
  1344. case LLM_ARCH_T5:
  1345. {
  1346. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1347. ml.get_key(LLM_KV_ATTENTION_RELATIVE_BUCKETS_COUNT, hparams.n_rel_attn_bkts);
  1348. uint32_t dec_start_token_id;
  1349. if (ml.get_key(LLM_KV_DECODER_START_TOKEN_ID, dec_start_token_id, false)) {
  1350. hparams.dec_start_token_id = dec_start_token_id;
  1351. }
  1352. switch (hparams.n_layer) {
  1353. case 6: type = LLM_TYPE_60M; break; // t5-small
  1354. case 8: type = LLM_TYPE_80M; break; // flan-t5-small
  1355. case 12:
  1356. switch (hparams.n_ff()) {
  1357. case 3072: type = LLM_TYPE_220M; break; // t5-base
  1358. case 2048: type = LLM_TYPE_250M; break; // flan-t5-base
  1359. default: type = LLM_TYPE_UNKNOWN;
  1360. } break;
  1361. case 24:
  1362. switch (hparams.n_ff()) {
  1363. case 4096: type = LLM_TYPE_770M; break; // t5-large
  1364. case 2816: type = LLM_TYPE_780M; break; // flan-t5-large
  1365. case 16384: type = LLM_TYPE_3B; break; // t5-3b
  1366. case 5120: type = LLM_TYPE_3B; break; // flan-t5-xl
  1367. case 65536: type = LLM_TYPE_11B; break; // t5-11b
  1368. case 10240: type = LLM_TYPE_11B; break; // flan-t5-xxl
  1369. default: type = LLM_TYPE_UNKNOWN;
  1370. } break;
  1371. default: type = LLM_TYPE_UNKNOWN;
  1372. }
  1373. } break;
  1374. case LLM_ARCH_T5ENCODER:
  1375. {
  1376. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1377. ml.get_key(LLM_KV_ATTENTION_RELATIVE_BUCKETS_COUNT, hparams.n_rel_attn_bkts);
  1378. type = LLM_TYPE_UNKNOWN;
  1379. } break;
  1380. case LLM_ARCH_JAIS:
  1381. {
  1382. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  1383. ml.get_key(LLM_KV_ATTENTION_MAX_ALIBI_BIAS, hparams.f_max_alibi_bias);
  1384. switch (hparams.n_layer) {
  1385. case 24: type = LLM_TYPE_1_3B; break;
  1386. case 40: type = LLM_TYPE_13B; break;
  1387. /* TODO: add variants */
  1388. default: type = LLM_TYPE_UNKNOWN;
  1389. }
  1390. } break;
  1391. case LLM_ARCH_NEMOTRON:
  1392. {
  1393. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  1394. switch (hparams.n_layer) {
  1395. case 32: type = LLM_TYPE_4B; break;
  1396. default: type = LLM_TYPE_UNKNOWN;
  1397. }
  1398. } break;
  1399. case LLM_ARCH_EXAONE:
  1400. {
  1401. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1402. switch (hparams.n_layer) {
  1403. case 32: type = LLM_TYPE_8B; break;
  1404. default: type = LLM_TYPE_UNKNOWN;
  1405. }
  1406. } break;
  1407. case LLM_ARCH_EXAONE4:
  1408. {
  1409. if (hparams.n_layer == 64) { // 32B
  1410. hparams.swa_type = LLAMA_SWA_TYPE_STANDARD;
  1411. hparams.n_swa = 4096;
  1412. hparams.set_swa_pattern(4);
  1413. }
  1414. ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa, false);
  1415. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1416. switch (hparams.n_layer) {
  1417. case 30: type = LLM_TYPE_1_2B; break;
  1418. case 64: type = LLM_TYPE_32B; break;
  1419. default: type = LLM_TYPE_UNKNOWN;
  1420. }
  1421. } break;
  1422. case LLM_ARCH_RWKV6:
  1423. case LLM_ARCH_RWKV6QWEN2:
  1424. {
  1425. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps, false);
  1426. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps, false);
  1427. ml.get_key(LLM_KV_WKV_HEAD_SIZE, hparams.wkv_head_size);
  1428. ml.get_key(LLM_KV_TIME_MIX_EXTRA_DIM, hparams.time_mix_extra_dim);
  1429. ml.get_key(LLM_KV_TIME_DECAY_EXTRA_DIM, hparams.time_decay_extra_dim);
  1430. ml.get_key(LLM_KV_RESCALE_EVERY_N_LAYERS, hparams.rescale_every_n_layers, false);
  1431. ml.get_key(LLM_KV_TOKEN_SHIFT_COUNT, hparams.token_shift_count, false);
  1432. switch (hparams.n_layer) {
  1433. case 24: type = LLM_TYPE_1_6B; break;
  1434. case 32:
  1435. switch (hparams.n_embd) {
  1436. case 2560: type = LLM_TYPE_3B; break;
  1437. case 4096: type = LLM_TYPE_7B; break;
  1438. default: type = LLM_TYPE_UNKNOWN;
  1439. } break;
  1440. case 61: type = LLM_TYPE_14B; break;
  1441. case 64: type = LLM_TYPE_32B; break;
  1442. default: type = LLM_TYPE_UNKNOWN;
  1443. }
  1444. } break;
  1445. case LLM_ARCH_RWKV7:
  1446. case LLM_ARCH_ARWKV7:
  1447. {
  1448. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps, false);
  1449. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps, false);
  1450. ml.get_key(LLM_KV_WKV_HEAD_SIZE, hparams.wkv_head_size);
  1451. ml.get_key(LLM_KV_ATTENTION_DECAY_LORA_RANK, hparams.n_lora_decay);
  1452. ml.get_key(LLM_KV_ATTENTION_ICLR_LORA_RANK, hparams.n_lora_iclr);
  1453. ml.get_key(LLM_KV_ATTENTION_VALUE_RESIDUAL_MIX_LORA_RANK, hparams.n_lora_value_res_mix);
  1454. ml.get_key(LLM_KV_ATTENTION_GATE_LORA_RANK, hparams.n_lora_gate, false);
  1455. ml.get_key(LLM_KV_TOKEN_SHIFT_COUNT, hparams.token_shift_count, false);
  1456. switch (hparams.n_layer) {
  1457. case 12:
  1458. switch (hparams.n_embd) {
  1459. case 768: type = LLM_TYPE_190M; break;
  1460. default: type = LLM_TYPE_UNKNOWN;
  1461. } break;
  1462. case 24:
  1463. switch (hparams.n_embd) {
  1464. case 1024: type = LLM_TYPE_450M; break;
  1465. case 2048: type = LLM_TYPE_1_5B; break;
  1466. default: type = LLM_TYPE_UNKNOWN;
  1467. } break;
  1468. case 28:
  1469. switch (hparams.n_embd) {
  1470. case 1536: type = LLM_TYPE_1_5B; break;
  1471. case 3584: type = LLM_TYPE_7B; break;
  1472. default: type = LLM_TYPE_UNKNOWN;
  1473. } break;
  1474. case 32:
  1475. switch (hparams.n_embd) {
  1476. case 2560: type = LLM_TYPE_2_9B; break;
  1477. case 4096: type = LLM_TYPE_7B; break;
  1478. default: type = LLM_TYPE_UNKNOWN;
  1479. } break;
  1480. case 61:
  1481. switch (hparams.n_embd) {
  1482. case 4096: type = LLM_TYPE_14B; break;
  1483. default: type = LLM_TYPE_UNKNOWN;
  1484. } break;
  1485. default: type = LLM_TYPE_UNKNOWN;
  1486. }
  1487. } break;
  1488. case LLM_ARCH_GRANITE:
  1489. case LLM_ARCH_GRANITE_MOE:
  1490. {
  1491. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1492. ml.get_key(LLM_KV_LOGIT_SCALE, hparams.f_logit_scale);
  1493. ml.get_key(LLM_KV_RESIDUAL_SCALE, hparams.f_residual_scale);
  1494. ml.get_key(LLM_KV_EMBEDDING_SCALE, hparams.f_embedding_scale);
  1495. ml.get_key(LLM_KV_ATTENTION_SCALE, hparams.f_attention_scale);
  1496. // Granite uses rope_finetuned as a switch for rope, so default to true
  1497. bool rope_finetuned = true;
  1498. ml.get_key(LLM_KV_ROPE_SCALING_FINETUNED, rope_finetuned, false);
  1499. hparams.rope_finetuned = rope_finetuned;
  1500. switch (hparams.n_layer) {
  1501. case 32: type = LLM_TYPE_3B; break;
  1502. case 40: type = LLM_TYPE_3B; break;
  1503. // Add additional layer/vocab/etc checks here for other model sizes
  1504. default: type = LLM_TYPE_UNKNOWN;
  1505. }
  1506. // For Granite MoE Shared
  1507. ml.get_key(LLM_KV_EXPERT_SHARED_FEED_FORWARD_LENGTH, hparams.n_ff_shexp, /* required */ false);
  1508. } break;
  1509. case LLM_ARCH_GRANITE_HYBRID:
  1510. {
  1511. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1512. ml.get_key(LLM_KV_LOGIT_SCALE, hparams.f_logit_scale, /* required */ false);
  1513. ml.get_key(LLM_KV_RESIDUAL_SCALE, hparams.f_residual_scale, /* required */ false);
  1514. ml.get_key(LLM_KV_EMBEDDING_SCALE, hparams.f_embedding_scale, /* required */ false);
  1515. ml.get_key(LLM_KV_ATTENTION_SCALE, hparams.f_attention_scale, /* required */ false);
  1516. ml.get_key(LLM_KV_SSM_CONV_KERNEL, hparams.ssm_d_conv);
  1517. ml.get_key(LLM_KV_SSM_INNER_SIZE, hparams.ssm_d_inner);
  1518. ml.get_key(LLM_KV_SSM_STATE_SIZE, hparams.ssm_d_state);
  1519. ml.get_key(LLM_KV_SSM_TIME_STEP_RANK, hparams.ssm_dt_rank);
  1520. ml.get_key(LLM_KV_SSM_GROUP_COUNT, hparams.ssm_n_group);
  1521. // Granite uses rope_finetuned as a switch for rope, so default to true
  1522. bool rope_finetuned = true;
  1523. ml.get_key(LLM_KV_ROPE_SCALING_FINETUNED, rope_finetuned, false);
  1524. hparams.rope_finetuned = rope_finetuned;
  1525. // A layer is recurrent IFF the n_head_kv value is set to 0
  1526. for (uint32_t i = 0; i < hparams.n_layer; ++i) {
  1527. hparams.recurrent_layer_arr[i] = hparams.n_head_kv(i) == 0;
  1528. }
  1529. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1530. switch (hparams.n_layer) {
  1531. // TODO: Add llm type label (not sure this is useful)
  1532. default: type = LLM_TYPE_UNKNOWN;
  1533. }
  1534. // For Granite MoE Shared
  1535. ml.get_key(LLM_KV_EXPERT_SHARED_FEED_FORWARD_LENGTH, hparams.n_ff_shexp, /* required */ false);
  1536. } break;
  1537. case LLM_ARCH_CHAMELEON:
  1538. {
  1539. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1540. hparams.f_norm_eps = 1e-5; // eps for qk-norm, torch default
  1541. ml.get_key(LLM_KV_SWIN_NORM, hparams.swin_norm);
  1542. switch (hparams.n_layer) {
  1543. case 32: type = LLM_TYPE_7B; break;
  1544. case 48: type = LLM_TYPE_34B; break;
  1545. default: type = LLM_TYPE_UNKNOWN;
  1546. }
  1547. } break;
  1548. case LLM_ARCH_WAVTOKENIZER_DEC:
  1549. {
  1550. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  1551. ml.get_key(LLM_KV_ATTENTION_GROUPNORM_EPS, hparams.f_norm_group_eps);
  1552. ml.get_key(LLM_KV_ATTENTION_GROUPNORM_GROUPS, hparams.n_norm_groups);
  1553. ml.get_key(LLM_KV_ATTENTION_CAUSAL, hparams.causal_attn);
  1554. } break;
  1555. case LLM_ARCH_BAILINGMOE:
  1556. {
  1557. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1558. ml.get_key(LLM_KV_LEADING_DENSE_BLOCK_COUNT, hparams.n_layer_dense_lead);
  1559. ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp);
  1560. ml.get_key(LLM_KV_EXPERT_SHARED_COUNT, hparams.n_expert_shared);
  1561. ml.get_key(LLM_KV_EXPERT_WEIGHTS_SCALE, hparams.expert_weights_scale);
  1562. ml.get_key(LLM_KV_EXPERT_WEIGHTS_NORM, hparams.expert_weights_norm, false);
  1563. switch (hparams.n_layer) {
  1564. case 28: type = LLM_TYPE_16B; break;
  1565. case 88: type = LLM_TYPE_290B; break;
  1566. default: type = LLM_TYPE_UNKNOWN;
  1567. }
  1568. } break;
  1569. case LLM_ARCH_DOTS1:
  1570. {
  1571. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1572. ml.get_key(LLM_KV_LEADING_DENSE_BLOCK_COUNT, hparams.n_layer_dense_lead);
  1573. ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp);
  1574. ml.get_key(LLM_KV_EXPERT_SHARED_COUNT, hparams.n_expert_shared);
  1575. ml.get_key(LLM_KV_EXPERT_WEIGHTS_SCALE, hparams.expert_weights_scale);
  1576. ml.get_key(LLM_KV_EXPERT_WEIGHTS_NORM, hparams.expert_weights_norm, false);
  1577. ml.get_key(LLM_KV_EXPERT_GATING_FUNC, hparams.expert_gating_func, false);
  1578. switch (hparams.n_layer) {
  1579. case 62: type = LLM_TYPE_142B; break;
  1580. default: type = LLM_TYPE_UNKNOWN;
  1581. }
  1582. } break;
  1583. case LLM_ARCH_ERNIE4_5:
  1584. case LLM_ARCH_ERNIE4_5_MOE:
  1585. {
  1586. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1587. if (arch == LLM_ARCH_ERNIE4_5_MOE) {
  1588. ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp);
  1589. ml.get_key(LLM_KV_EXPERT_SHARED_FEED_FORWARD_LENGTH, hparams.n_ff_shexp, false);
  1590. ml.get_key(LLM_KV_INTERLEAVE_MOE_LAYER_STEP, hparams.n_moe_layer_step);
  1591. ml.get_key(LLM_KV_LEADING_DENSE_BLOCK_COUNT, hparams.n_layer_dense_lead);
  1592. }
  1593. switch (hparams.n_layer) {
  1594. case 18: type = LLM_TYPE_0_3B; break;
  1595. case 28: type = LLM_TYPE_21B_A3B; break;
  1596. case 54: type = LLM_TYPE_300B_A47B; break;
  1597. default: type = LLM_TYPE_UNKNOWN;
  1598. }
  1599. } break;
  1600. case LLM_ARCH_FALCON_H1:
  1601. {
  1602. // Common parameters
  1603. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1604. // SSM parameters
  1605. ml.get_key(LLM_KV_SSM_CONV_KERNEL, hparams.ssm_d_conv);
  1606. ml.get_key(LLM_KV_SSM_INNER_SIZE, hparams.ssm_d_inner);
  1607. ml.get_key(LLM_KV_SSM_STATE_SIZE, hparams.ssm_d_state);
  1608. ml.get_key(LLM_KV_SSM_TIME_STEP_RANK, hparams.ssm_dt_rank);
  1609. ml.get_key(LLM_KV_SSM_GROUP_COUNT, hparams.ssm_n_group);
  1610. std::fill(hparams.recurrent_layer_arr.begin(), hparams.recurrent_layer_arr.end(), true);
  1611. switch (hparams.n_layer) {
  1612. case 36:
  1613. type = LLM_TYPE_0_5B; break;
  1614. case 24:
  1615. type = LLM_TYPE_1_5B; break;
  1616. case 66:
  1617. type = LLM_TYPE_1B; break;
  1618. case 32:
  1619. type = LLM_TYPE_3B; break;
  1620. case 44:
  1621. type = LLM_TYPE_7B; break;
  1622. case 72:
  1623. type = LLM_TYPE_34B; break;
  1624. default:
  1625. type = LLM_TYPE_UNKNOWN;
  1626. }
  1627. } break;
  1628. case LLM_ARCH_HUNYUAN_MOE:
  1629. {
  1630. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1631. ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp);
  1632. ml.get_key(LLM_KV_EXPERT_SHARED_FEED_FORWARD_LENGTH, hparams.n_ff_shexp);
  1633. switch (hparams.n_layer) {
  1634. case 32: type = LLM_TYPE_A13B; break;
  1635. default: type = LLM_TYPE_UNKNOWN;
  1636. }
  1637. } break;
  1638. case LLM_ARCH_HUNYUAN_DENSE:
  1639. {
  1640. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1641. switch (hparams.n_embd) {
  1642. case 1024: type = LLM_TYPE_0_5B; break;
  1643. case 2048: type = LLM_TYPE_1_8B; break;
  1644. case 3072: type = LLM_TYPE_4B; break;
  1645. case 4096: type = LLM_TYPE_7B; break;
  1646. default: type = LLM_TYPE_UNKNOWN;
  1647. }
  1648. } break;
  1649. case LLM_ARCH_SMOLLM3:
  1650. {
  1651. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1652. hparams.n_no_rope_layer_step = 4;
  1653. switch (hparams.n_layer) {
  1654. case 36: type = LLM_TYPE_3B; break;
  1655. default: type = LLM_TYPE_UNKNOWN;
  1656. }
  1657. } break;
  1658. case LLM_ARCH_OPENAI_MOE:
  1659. {
  1660. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1661. ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp);
  1662. ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa);
  1663. hparams.swa_type = LLAMA_SWA_TYPE_STANDARD;
  1664. hparams.set_swa_pattern(2);
  1665. switch (hparams.n_layer) {
  1666. case 24: type = LLM_TYPE_20B; break;
  1667. case 36: type = LLM_TYPE_120B; break;
  1668. default: type = LLM_TYPE_UNKNOWN;
  1669. }
  1670. } break;
  1671. case LLM_ARCH_LFM2:
  1672. {
  1673. ml.get_key(LLM_KV_SHORTCONV_L_CACHE, hparams.n_shortconv_l_cache);
  1674. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1675. for (uint32_t il = 0; il < hparams.n_layer; ++il) {
  1676. hparams.recurrent_layer_arr[il] = hparams.n_head_kv(il) == 0;
  1677. }
  1678. switch (hparams.n_embd) {
  1679. case 1024: type = LLM_TYPE_350M; break;
  1680. case 1536: type = LLM_TYPE_700M; break;
  1681. case 2048: type = LLM_TYPE_1_2B; break;
  1682. default: type = LLM_TYPE_UNKNOWN;
  1683. }
  1684. } break;
  1685. case LLM_ARCH_SMALLTHINKER:
  1686. {
  1687. const bool found_swa = ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa, false);
  1688. if (found_swa && hparams.n_swa > 0) {
  1689. hparams.swa_type = LLAMA_SWA_TYPE_STANDARD;
  1690. hparams.n_swa = 4096;
  1691. hparams.set_swa_pattern(4, true);
  1692. } else {
  1693. hparams.swa_type = LLAMA_SWA_TYPE_NONE;
  1694. hparams.n_no_rope_layer_step = hparams.n_layer;
  1695. }
  1696. ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp, false);
  1697. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1698. ml.get_key(LLM_KV_EXPERT_GATING_FUNC, hparams.expert_gating_func, false);
  1699. switch (hparams.n_layer) {
  1700. case 32: type = LLM_TYPE_4B; break;
  1701. case 52: type = LLM_TYPE_20B; break;
  1702. default: type = LLM_TYPE_UNKNOWN;
  1703. }
  1704. } break;
  1705. default: throw std::runtime_error("unsupported model architecture");
  1706. }
  1707. pimpl->n_bytes = ml.n_bytes;
  1708. pimpl->desc_str = arch_name() + " " + type_name() + " " + ml.ftype_name();
  1709. if (hparams.f_max_alibi_bias > 0.0f) {
  1710. hparams.use_alibi = true;
  1711. }
  1712. hparams.rope_type = llama_model_rope_type(this);
  1713. }
  1714. void llama_model::load_vocab(llama_model_loader & ml) {
  1715. const auto kv = LLM_KV(arch);
  1716. vocab.load(ml, kv);
  1717. }
  1718. bool llama_model::load_tensors(llama_model_loader & ml) {
  1719. const auto & split_mode = params.split_mode;
  1720. const auto & n_gpu_layers = params.n_gpu_layers;
  1721. const auto & use_mlock = params.use_mlock;
  1722. const auto & tensor_split = params.tensor_split;
  1723. const int n_layer = hparams.n_layer;
  1724. const bool use_mmap_buffer = true;
  1725. LLAMA_LOG_INFO("%s: loading model tensors, this can take a while... (mmap = %s)\n", __func__, ml.use_mmap ? "true" : "false");
  1726. // build a list of buffer types for the CPU and GPU devices
  1727. pimpl->cpu_buft_list = make_cpu_buft_list(devices, params.use_extra_bufts);
  1728. for (auto * dev : devices) {
  1729. buft_list_t buft_list = make_gpu_buft_list(dev, split_mode, tensor_split);
  1730. // add CPU buffer types as a fallback
  1731. buft_list.insert(buft_list.end(), pimpl->cpu_buft_list.begin(), pimpl->cpu_buft_list.end());
  1732. pimpl->gpu_buft_list.emplace(dev, std::move(buft_list));
  1733. }
  1734. // calculate the split points
  1735. bool all_zero = tensor_split == nullptr || std::all_of(tensor_split, tensor_split + n_devices(), [](float x) { return x == 0.0f; });
  1736. std::vector<float> splits(n_devices());
  1737. if (all_zero) {
  1738. // default split, by free memory
  1739. for (size_t i = 0; i < n_devices(); ++i) {
  1740. ggml_backend_dev_t dev = devices[i];
  1741. size_t total;
  1742. size_t free;
  1743. ggml_backend_dev_memory(dev, &free, &total);
  1744. splits[i] = free;
  1745. }
  1746. } else {
  1747. std::copy(tensor_split, tensor_split + n_devices(), splits.begin());
  1748. }
  1749. // sum and normalize the splits to get the split points
  1750. float split_sum = 0.0f;
  1751. for (size_t i = 0; i < n_devices(); ++i) {
  1752. split_sum += splits[i];
  1753. splits[i] = split_sum;
  1754. }
  1755. for (size_t i = 0; i < n_devices(); ++i) {
  1756. splits[i] /= split_sum;
  1757. }
  1758. ggml_backend_dev_t cpu_dev = ggml_backend_dev_by_type(GGML_BACKEND_DEVICE_TYPE_CPU);
  1759. if (cpu_dev == nullptr) {
  1760. throw std::runtime_error(format("%s: no CPU backend found", __func__));
  1761. }
  1762. const int i_gpu_start = std::max((int) hparams.n_layer - n_gpu_layers, (int) 0);
  1763. const int act_gpu_layers = devices.empty() ? 0 : std::min(n_gpu_layers, (int)n_layer + 1);
  1764. auto get_layer_buft_list = [&](int il) -> llama_model::impl::layer_dev {
  1765. const bool is_swa = il < (int) hparams.n_layer && hparams.is_swa(il);
  1766. if (il < i_gpu_start || (il - i_gpu_start) >= act_gpu_layers) {
  1767. LLAMA_LOG_DEBUG("load_tensors: layer %3d assigned to device %s, is_swa = %d\n", il, ggml_backend_dev_name(cpu_dev), is_swa);
  1768. return {cpu_dev, &pimpl->cpu_buft_list};
  1769. }
  1770. const int layer_gpu = std::upper_bound(splits.begin(), splits.begin() + n_devices(), float(il - i_gpu_start)/act_gpu_layers) - splits.begin();
  1771. auto * dev = devices.at(layer_gpu);
  1772. LLAMA_LOG_DEBUG("load_tensors: layer %3d assigned to device %s, is_swa = %d\n", il, ggml_backend_dev_name(dev), is_swa);
  1773. return {dev, &pimpl->gpu_buft_list.at(dev)};
  1774. };
  1775. // assign the input layer
  1776. // there is very little benefit to offloading the input layer, so always keep it on the CPU
  1777. pimpl->dev_input = { cpu_dev, &pimpl->cpu_buft_list };
  1778. // assign the repeating layers to the devices according to the splits
  1779. pimpl->dev_layer.resize(n_layer);
  1780. for (int il = 0; il < n_layer; ++il) {
  1781. pimpl->dev_layer[il] = get_layer_buft_list(il);
  1782. }
  1783. // assign the output layer
  1784. pimpl->dev_output = get_layer_buft_list(n_layer);
  1785. // one ggml context per buffer type
  1786. int max_n_tensors = ml.n_tensors;
  1787. max_n_tensors += 1; // duplicated output tensor
  1788. max_n_tensors += n_layer*2; // duplicated rope freq tensors
  1789. const size_t ctx_size = ggml_tensor_overhead()*max_n_tensors;
  1790. std::map<ggml_backend_buffer_type_t, ggml_context *> ctx_map;
  1791. auto ctx_for_buft = [&](ggml_backend_buffer_type_t buft) -> ggml_context * {
  1792. auto it = ctx_map.find(buft);
  1793. if (it == ctx_map.end()) {
  1794. ggml_init_params params = {
  1795. /*.mem_size =*/ ctx_size,
  1796. /*.mem_buffer =*/ NULL,
  1797. /*.no_alloc =*/ true,
  1798. };
  1799. ggml_context * ctx = ggml_init(params);
  1800. if (!ctx) {
  1801. throw std::runtime_error(format("failed to create ggml context"));
  1802. }
  1803. ctx_map[buft] = ctx;
  1804. pimpl->ctxs.emplace_back(ctx);
  1805. return ctx;
  1806. }
  1807. return it->second;
  1808. };
  1809. const auto TENSOR_DUPLICATED = llama_model_loader::TENSOR_DUPLICATED;
  1810. const auto TENSOR_NOT_REQUIRED = llama_model_loader::TENSOR_NOT_REQUIRED;
  1811. const auto TENSOR_SKIP = llama_model_loader::TENSOR_SKIP;
  1812. // create tensors for the weights
  1813. {
  1814. // note: cast to int64_t since we will use these for the tensor dimensions
  1815. const int64_t n_head = hparams.n_head();
  1816. const int64_t n_head_kv = hparams.n_head_kv();
  1817. const int64_t n_embd = hparams.n_embd;
  1818. const int64_t n_embd_k_gqa = hparams.n_embd_k_gqa();
  1819. const int64_t n_embd_v_gqa = hparams.n_embd_v_gqa();
  1820. const int64_t n_embd_head_k = hparams.n_embd_head_k;
  1821. const int64_t n_embd_head_v = hparams.n_embd_head_v;
  1822. const int64_t n_ff = hparams.n_ff();
  1823. const int64_t n_embd_gqa = n_embd_v_gqa;
  1824. const int64_t n_vocab = vocab.n_tokens();
  1825. const int64_t n_token_types = vocab.n_token_types();
  1826. const int64_t n_rot = hparams.n_rot;
  1827. const int64_t n_expert = hparams.n_expert;
  1828. const int64_t n_expert_used = hparams.n_expert_used;
  1829. const int64_t n_ctx_train = hparams.n_ctx_train;
  1830. if (n_expert > 0 && hparams.n_expert_used == 0) {
  1831. throw std::runtime_error("model has expert layers but no expert layers are used");
  1832. }
  1833. int n_moved_tensors = 0;
  1834. ggml_tensor * first_moved_tensor = nullptr;
  1835. ggml_backend_buffer_type_t first_moved_from_buft = nullptr;
  1836. ggml_backend_buffer_type_t first_moved_to_buft = nullptr;
  1837. auto create_tensor = [&](const LLM_TN_IMPL & tn, const std::initializer_list<int64_t> & ne, int flags) -> ggml_tensor * {
  1838. ggml_tensor * t_meta = ml.get_tensor_meta(tn.str().c_str());
  1839. if (!t_meta) {
  1840. if (flags & TENSOR_NOT_REQUIRED) {
  1841. return nullptr;
  1842. }
  1843. throw std::runtime_error(format("missing tensor '%s'", tn.str().c_str()));
  1844. }
  1845. // some models use the token embedding tensor as the output, but since these are used in different layers and with different ops
  1846. // the tensor is duplicated
  1847. // to handle this, we check if the tensor is duplicated, and if so, we assume that it is being loaded as the output tensor
  1848. llm_tensor tn_tensor = tn.tensor;
  1849. if (tn.tensor == LLM_TENSOR_TOKEN_EMBD && flags & TENSOR_DUPLICATED) {
  1850. tn_tensor = LLM_TENSOR_OUTPUT;
  1851. }
  1852. llm_tensor_info info;
  1853. try {
  1854. info = llm_tensor_info_for(tn_tensor);
  1855. } catch (const std::out_of_range & e) {
  1856. throw std::runtime_error(format("missing tensor info mapping for %s", tn.str().c_str()));
  1857. }
  1858. // skip unused tensors
  1859. if (info.op == GGML_OP_NONE || flags & TENSOR_SKIP) {
  1860. const size_t nbytes = ggml_nbytes(t_meta);
  1861. LLAMA_LOG_WARN("model has unused tensor %s (size = %zu bytes) -- ignoring\n", tn.str().c_str(), nbytes);
  1862. ml.size_data -= nbytes;
  1863. ml.n_created++;
  1864. return nullptr;
  1865. }
  1866. // tensors with "bias" suffix are always used with GGML_OP_ADD or GGML_OP_ADD_ID
  1867. ggml_op op;
  1868. bool bias = tn.suffix != nullptr && strcmp(tn.suffix, "bias") == 0;
  1869. if (bias) {
  1870. if (info.op == GGML_OP_MUL_MAT_ID) {
  1871. op = GGML_OP_ADD_ID;
  1872. } else {
  1873. op = GGML_OP_ADD;
  1874. }
  1875. } else {
  1876. op = info.op;
  1877. }
  1878. // sanity checks
  1879. if (info.layer == LLM_TENSOR_LAYER_INPUT || info.layer == LLM_TENSOR_LAYER_OUTPUT) {
  1880. if (tn.bid != -1) {
  1881. GGML_ABORT("input/output layer tensor %s used with a layer number", tn.str().c_str());
  1882. }
  1883. } else {
  1884. if (tn.bid == -1) {
  1885. GGML_ABORT("repeating layer tensor %s used without a layer number", tn.str().c_str());
  1886. }
  1887. }
  1888. // select the buffer type for this tensor
  1889. buft_list_t * buft_list;
  1890. switch (info.layer) {
  1891. case LLM_TENSOR_LAYER_INPUT:
  1892. buft_list = pimpl->dev_input.buft_list;
  1893. break;
  1894. case LLM_TENSOR_LAYER_OUTPUT:
  1895. buft_list = pimpl->dev_output.buft_list;
  1896. break;
  1897. case LLM_TENSOR_LAYER_REPEATING:
  1898. buft_list = pimpl->dev_layer.at(tn.bid).buft_list;
  1899. break;
  1900. default:
  1901. GGML_ABORT("invalid layer %d for tensor %s", info.layer, tn.str().c_str());
  1902. }
  1903. ggml_backend_buffer_type_t buft = nullptr;
  1904. // check overrides
  1905. if (ml.tensor_buft_overrides) {
  1906. std::string tensor_name = tn.str();
  1907. for (const auto * overrides = ml.tensor_buft_overrides; overrides->pattern != nullptr; ++overrides) {
  1908. std::regex pattern(overrides->pattern);
  1909. if (std::regex_search(tensor_name, pattern)) {
  1910. if (overrides->buft == ggml_backend_cpu_buffer_type()) {
  1911. // when overriding to a CPU buffer, consider the extra buffer types
  1912. buft = select_weight_buft(hparams, t_meta, op, pimpl->cpu_buft_list);
  1913. } else {
  1914. buft = overrides->buft;
  1915. }
  1916. LLAMA_LOG_DEBUG("tensor %s (%zu MiB %s) buffer type overridden to %s\n",
  1917. tensor_name.c_str(),
  1918. ggml_nbytes(t_meta) / 1024 / 1024, ggml_type_name(t_meta->type),
  1919. ggml_backend_buft_name(buft));
  1920. break;
  1921. }
  1922. }
  1923. }
  1924. if (!buft) {
  1925. buft = select_weight_buft(hparams, t_meta, op, *buft_list);
  1926. if (!buft) {
  1927. throw std::runtime_error(format("failed to find a compatible buffer type for tensor %s", tn.str().c_str()));
  1928. }
  1929. }
  1930. // avoid using a host buffer when using mmap
  1931. auto * buft_dev = ggml_backend_buft_get_device(buft);
  1932. if (ml.use_mmap && buft_dev && buft == ggml_backend_dev_host_buffer_type(buft_dev)) {
  1933. auto * cpu_dev = ggml_backend_dev_by_type(GGML_BACKEND_DEVICE_TYPE_CPU);
  1934. if (!cpu_dev) {
  1935. throw std::runtime_error("no CPU backend found");
  1936. }
  1937. buft = ggml_backend_dev_buffer_type(cpu_dev);
  1938. }
  1939. if (buft != buft_list->front().second) {
  1940. n_moved_tensors++;
  1941. if (!first_moved_tensor) {
  1942. first_moved_tensor = t_meta;
  1943. first_moved_from_buft = buft_list->front().second;
  1944. first_moved_to_buft = buft;
  1945. }
  1946. }
  1947. ggml_context * ctx = ctx_for_buft(buft);
  1948. // if duplicated, check if the original tensor was allocated in the same buffer type context and avoid creating a new one
  1949. if (flags & TENSOR_DUPLICATED) {
  1950. ggml_tensor * t = ggml_get_tensor(ctx, tn.str().c_str());
  1951. if (t) {
  1952. return t;
  1953. }
  1954. }
  1955. return ml.create_tensor(ctx, tn, ne, flags);
  1956. };
  1957. layers.resize(n_layer);
  1958. // TODO: move to a separate function
  1959. const auto tn = LLM_TN(arch);
  1960. switch (arch) {
  1961. case LLM_ARCH_LLAMA:
  1962. case LLM_ARCH_REFACT:
  1963. case LLM_ARCH_MINICPM:
  1964. case LLM_ARCH_GRANITE:
  1965. case LLM_ARCH_GRANITE_MOE:
  1966. {
  1967. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  1968. // output
  1969. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  1970. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  1971. // if output is NULL, init from the input tok embed
  1972. if (output == NULL) {
  1973. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  1974. }
  1975. for (int i = 0; i < n_layer; ++i) {
  1976. auto & layer = layers[i];
  1977. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  1978. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  1979. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  1980. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  1981. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  1982. // optional bias tensors
  1983. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  1984. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED);
  1985. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED);
  1986. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  1987. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  1988. if (hparams.rope_scaling_type_train == LLAMA_ROPE_SCALING_TYPE_LONGROPE) {
  1989. layer.rope_long = create_tensor(tn(LLM_TENSOR_ROPE_FACTORS_LONG, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  1990. layer.rope_short = create_tensor(tn(LLM_TENSOR_ROPE_FACTORS_SHORT, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  1991. }
  1992. else {
  1993. layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  1994. }
  1995. if (n_expert == 0) {
  1996. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  1997. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  1998. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  1999. // optional MLP bias
  2000. layer.ffn_gate_b = create_tensor(tn(LLM_TENSOR_FFN_GATE, "bias", i), {n_ff}, TENSOR_NOT_REQUIRED);
  2001. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2002. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, TENSOR_NOT_REQUIRED);
  2003. } else {
  2004. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  2005. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff, n_expert}, TENSOR_NOT_REQUIRED);
  2006. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), { n_ff, n_embd, n_expert}, 0);
  2007. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0);
  2008. // For Granite MoE Shared
  2009. if (hparams.n_ff_shexp > 0) {
  2010. layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), {n_embd, hparams.n_ff_shexp}, 0);
  2011. layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), {n_embd, hparams.n_ff_shexp}, 0);
  2012. layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), {hparams.n_ff_shexp, n_embd}, 0);
  2013. }
  2014. }
  2015. }
  2016. } break;
  2017. case LLM_ARCH_LLADA:
  2018. {
  2019. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, 0);
  2020. // output
  2021. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), { n_embd }, 0);
  2022. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), { n_embd, n_vocab }, TENSOR_NOT_REQUIRED);
  2023. // if output is NULL, init from the input tok embed
  2024. if (output == NULL) {
  2025. output =
  2026. create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, TENSOR_DUPLICATED);
  2027. }
  2028. for (int i = 0; i < n_layer; ++i) {
  2029. auto & layer = layers[i];
  2030. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), { n_embd }, 0);
  2031. // Use separate Q, K, V projections without bias, matching LLaDALlamaBlock
  2032. layer.wq =
  2033. create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), { n_embd, n_embd_head_k * n_head }, 0);
  2034. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), { n_embd, n_embd_k_gqa }, 0);
  2035. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), { n_embd, n_embd_v_gqa }, 0);
  2036. // No bias for QKV projections as per config: include_bias=false, include_qkv_bias=false
  2037. layer.wo =
  2038. create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), { n_embd_head_k * n_head, n_embd }, 0);
  2039. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), { n_embd }, TENSOR_NOT_REQUIRED);
  2040. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), { n_embd }, 0);
  2041. layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), { n_rot / 2 },
  2042. TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  2043. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), { n_embd, n_ff }, 0);
  2044. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd }, 0);
  2045. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), { n_embd, n_ff }, 0);
  2046. // optional MLP bias
  2047. layer.ffn_gate_b =
  2048. create_tensor(tn(LLM_TENSOR_FFN_GATE, "bias", i), { n_ff }, TENSOR_NOT_REQUIRED);
  2049. layer.ffn_down_b =
  2050. create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), { n_embd }, TENSOR_NOT_REQUIRED);
  2051. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), { n_ff }, TENSOR_NOT_REQUIRED);
  2052. }
  2053. }
  2054. break;
  2055. case LLM_ARCH_LLAMA4:
  2056. {
  2057. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2058. // output
  2059. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2060. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  2061. // if output is NULL, init from the input tok embed
  2062. if (output == NULL) {
  2063. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  2064. }
  2065. GGML_ASSERT(hparams.n_moe_layer_step > 0 && "Llama 4 requires n_moe_layer_step > 0");
  2066. for (int i = 0; i < n_layer; ++i) {
  2067. bool is_moe_layer = (i + 1) % hparams.n_moe_layer_step == 0;
  2068. auto & layer = layers[i];
  2069. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2070. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  2071. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  2072. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  2073. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  2074. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2075. layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  2076. if (is_moe_layer) {
  2077. int n_ff_exp = hparams.n_ff_exp;
  2078. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  2079. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff_exp, n_expert}, 0);
  2080. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), { n_ff_exp, n_embd, n_expert}, 0);
  2081. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff_exp, n_expert}, 0);
  2082. // Shared expert
  2083. const int64_t n_ff_shexp = n_ff_exp;
  2084. layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), { n_embd, n_ff_shexp}, 0);
  2085. layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), {n_ff_shexp, n_embd }, 0);
  2086. layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), { n_embd, n_ff_shexp}, 0);
  2087. } else {
  2088. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  2089. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  2090. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2091. }
  2092. }
  2093. } break;
  2094. case LLM_ARCH_DECI:
  2095. {
  2096. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2097. // output
  2098. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2099. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  2100. // if output is NULL, init from the input tok embed
  2101. if (output == NULL) {
  2102. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  2103. }
  2104. for (int i = 0; i < n_layer; ++i) {
  2105. auto & layer = layers[i];
  2106. const int64_t n_embd_k_gqa = hparams.n_embd_k_gqa(i);
  2107. const int64_t n_embd_v_gqa = hparams.n_embd_v_gqa(i);
  2108. const int64_t n_embd_gqa = hparams.n_embd_v_gqa(i);
  2109. const int64_t n_ff = hparams.n_ff(i);
  2110. const int64_t n_head = hparams.n_head(i);
  2111. const int64_t n_head_kv = hparams.n_head_kv(i);
  2112. if (n_head_kv == 0 && n_head > 0) {
  2113. // linear attention for DeciLMCausalModel
  2114. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2115. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2116. }
  2117. else if (n_head_kv > 0) {
  2118. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2119. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  2120. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  2121. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  2122. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  2123. }
  2124. // optional bias tensors
  2125. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2126. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED);
  2127. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED);
  2128. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2129. if (n_ff > 0) {
  2130. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2131. }
  2132. if (hparams.rope_scaling_type_train == LLAMA_ROPE_SCALING_TYPE_LONGROPE) {
  2133. layer.rope_long = create_tensor(tn(LLM_TENSOR_ROPE_FACTORS_LONG, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  2134. layer.rope_short = create_tensor(tn(LLM_TENSOR_ROPE_FACTORS_SHORT, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  2135. }
  2136. else {
  2137. layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  2138. }
  2139. if (n_ff > 0) {
  2140. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  2141. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  2142. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2143. }
  2144. // optional MLP bias
  2145. layer.ffn_gate_b = create_tensor(tn(LLM_TENSOR_FFN_GATE, "bias", i), {n_ff}, TENSOR_NOT_REQUIRED);
  2146. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2147. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, TENSOR_NOT_REQUIRED);
  2148. }
  2149. } break;
  2150. case LLM_ARCH_MINICPM3:
  2151. {
  2152. const int64_t n_embd_head_qk_rope = hparams.n_rot;
  2153. const int64_t n_embd_head_qk_nope = hparams.n_embd_head_k - hparams.n_rot;
  2154. const int64_t q_lora_rank = hparams.n_lora_q;
  2155. const int64_t kv_lora_rank = hparams.n_lora_kv;
  2156. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2157. // output
  2158. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2159. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  2160. // if output is NULL, init from the input tok embed
  2161. if (output == NULL) {
  2162. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  2163. }
  2164. for (int i = 0; i < n_layer; ++i) {
  2165. auto & layer = layers[i];
  2166. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2167. layer.attn_q_a_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_A_NORM, "weight", i), {q_lora_rank}, 0);
  2168. layer.attn_kv_a_norm = create_tensor(tn(LLM_TENSOR_ATTN_KV_A_NORM, "weight", i), {kv_lora_rank}, 0);
  2169. layer.wq_a = create_tensor(tn(LLM_TENSOR_ATTN_Q_A, "weight", i), {n_embd, q_lora_rank}, 0);
  2170. layer.wq_b = create_tensor(tn(LLM_TENSOR_ATTN_Q_B, "weight", i), {q_lora_rank, n_head * n_embd_head_k}, 0);
  2171. 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);
  2172. 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);
  2173. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), { n_head * ( n_embd_head_v), n_embd}, 0);
  2174. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2175. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  2176. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  2177. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2178. 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));
  2179. 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));
  2180. }
  2181. } break;
  2182. case LLM_ARCH_GROK:
  2183. {
  2184. if (n_expert == 0) {
  2185. throw std::runtime_error("Grok model cannot have zero experts");
  2186. }
  2187. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2188. // output
  2189. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2190. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  2191. // if output is NULL, init from the input tok embed
  2192. if (output == NULL) {
  2193. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  2194. }
  2195. for (int i = 0; i < n_layer; ++i) {
  2196. auto & layer = layers[i];
  2197. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2198. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  2199. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  2200. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  2201. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2202. layer.attn_out_norm = create_tensor(tn(LLM_TENSOR_ATTN_OUT_NORM, "weight", i), {n_embd}, 0);
  2203. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2204. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  2205. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff, n_expert}, TENSOR_NOT_REQUIRED);
  2206. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), { n_ff, n_embd, n_expert}, 0);
  2207. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0);
  2208. layer.layer_out_norm = create_tensor(tn(LLM_TENSOR_LAYER_OUT_NORM, "weight", i), {n_embd}, 0);
  2209. }
  2210. } break;
  2211. case LLM_ARCH_DBRX:
  2212. {
  2213. if (n_expert == 0) {
  2214. throw std::runtime_error("DBRX model cannot have zero experts");
  2215. }
  2216. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2217. // output
  2218. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2219. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  2220. for (int i = 0; i < n_layer; ++i) {
  2221. auto & layer = layers[i];
  2222. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2223. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, 0);
  2224. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2225. layer.attn_out_norm = create_tensor(tn(LLM_TENSOR_ATTN_OUT_NORM, "weight", i), {n_embd}, 0);
  2226. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  2227. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0);
  2228. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff, n_embd, n_expert}, 0);
  2229. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0);
  2230. }
  2231. } break;
  2232. case LLM_ARCH_BAICHUAN:
  2233. {
  2234. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2235. {
  2236. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2237. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  2238. }
  2239. for (int i = 0; i < n_layer; ++i) {
  2240. auto & layer = layers[i];
  2241. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2242. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  2243. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  2244. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  2245. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2246. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2247. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  2248. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  2249. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2250. }
  2251. } break;
  2252. case LLM_ARCH_FALCON:
  2253. {
  2254. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2255. // output
  2256. {
  2257. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2258. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
  2259. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  2260. if (!output) {
  2261. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); // needs to be on GPU
  2262. }
  2263. }
  2264. for (int i = 0; i < n_layer; ++i) {
  2265. auto & layer = layers[i];
  2266. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2267. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0);
  2268. layer.attn_norm_2 = create_tensor(tn(LLM_TENSOR_ATTN_NORM_2, "weight", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2269. layer.attn_norm_2_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM_2, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2270. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, 0);
  2271. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2272. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  2273. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2274. }
  2275. } break;
  2276. case LLM_ARCH_STARCODER:
  2277. {
  2278. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2279. pos_embd = create_tensor(tn(LLM_TENSOR_POS_EMBD, "weight"), {n_embd, n_ctx_train}, 0);
  2280. // output
  2281. {
  2282. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2283. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
  2284. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  2285. if (!output) {
  2286. // needs to be on GPU
  2287. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  2288. }
  2289. }
  2290. for (int i = 0; i < n_layer; ++i) {
  2291. auto & layer = layers[i];
  2292. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2293. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0);
  2294. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, 0);
  2295. layer.bqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd + 2*n_embd_gqa}, 0);
  2296. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2297. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0);
  2298. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2299. layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd}, 0);
  2300. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  2301. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, 0);
  2302. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2303. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, 0);
  2304. }
  2305. } break;
  2306. case LLM_ARCH_BERT:
  2307. case LLM_ARCH_NOMIC_BERT:
  2308. case LLM_ARCH_NOMIC_BERT_MOE:
  2309. {
  2310. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2311. type_embd = create_tensor(tn(LLM_TENSOR_TOKEN_TYPES, "weight"), {n_embd, n_token_types}, TENSOR_NOT_REQUIRED);
  2312. if (arch == LLM_ARCH_BERT) {
  2313. pos_embd = create_tensor(tn(LLM_TENSOR_POS_EMBD, "weight"), {n_embd, n_ctx_train}, 0);
  2314. cls = create_tensor(tn(LLM_TENSOR_CLS, "weight"), {n_embd, n_embd}, TENSOR_NOT_REQUIRED);
  2315. cls_b = create_tensor(tn(LLM_TENSOR_CLS, "bias"), {n_embd}, TENSOR_NOT_REQUIRED);
  2316. cls_out = create_tensor(tn(LLM_TENSOR_CLS_OUT, "weight"), {n_embd, hparams.n_cls_out}, TENSOR_NOT_REQUIRED);
  2317. cls_out_b = create_tensor(tn(LLM_TENSOR_CLS_OUT, "bias"), {hparams.n_cls_out}, TENSOR_NOT_REQUIRED);
  2318. }
  2319. tok_norm = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "weight"), {n_embd}, 0);
  2320. tok_norm_b = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "bias"), {n_embd}, 0);
  2321. for (int i = 0; i < n_layer; ++i) {
  2322. auto & layer = layers[i];
  2323. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, TENSOR_NOT_REQUIRED);
  2324. layer.bqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd + 2*n_embd_gqa}, TENSOR_NOT_REQUIRED);
  2325. if (!layer.wqkv) {
  2326. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  2327. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, 0);
  2328. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  2329. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, 0);
  2330. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  2331. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, 0);
  2332. }
  2333. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2334. layer.attn_out_norm = create_tensor(tn(LLM_TENSOR_ATTN_OUT_NORM, "weight", i), {n_embd}, 0);
  2335. layer.attn_out_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_OUT_NORM, "bias", i), {n_embd}, 0);
  2336. if (hparams.moe_every_n_layers > 0 && i % hparams.moe_every_n_layers == 1) {
  2337. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0);
  2338. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff, n_expert}, 0);
  2339. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), { n_ff, n_embd, n_expert}, 0);
  2340. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  2341. } else {
  2342. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2343. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  2344. if (arch == LLM_ARCH_BERT || arch == LLM_ARCH_NOMIC_BERT_MOE) {
  2345. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0);
  2346. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, 0);
  2347. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, 0);
  2348. } else {
  2349. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  2350. }
  2351. }
  2352. layer.layer_out_norm = create_tensor(tn(LLM_TENSOR_LAYER_OUT_NORM, "weight", i), {n_embd}, 0);
  2353. layer.layer_out_norm_b = create_tensor(tn(LLM_TENSOR_LAYER_OUT_NORM, "bias", i), {n_embd}, 0);
  2354. }
  2355. } break;
  2356. case LLM_ARCH_NEO_BERT:
  2357. {
  2358. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2359. cls = create_tensor(tn(LLM_TENSOR_CLS, "weight"), {n_embd, n_embd}, TENSOR_NOT_REQUIRED);
  2360. cls_b = create_tensor(tn(LLM_TENSOR_CLS, "bias"), {n_embd}, TENSOR_NOT_REQUIRED);
  2361. cls_out = create_tensor(tn(LLM_TENSOR_CLS_OUT, "weight"), {n_embd, hparams.n_cls_out}, TENSOR_NOT_REQUIRED);
  2362. cls_out_b = create_tensor(tn(LLM_TENSOR_CLS_OUT, "bias"), {hparams.n_cls_out}, TENSOR_NOT_REQUIRED);
  2363. output_norm_enc = create_tensor(tn(LLM_TENSOR_ENC_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2364. for (int i = 0; i < n_layer; ++i) {
  2365. auto & layer = layers[i];
  2366. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2367. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, 0);
  2368. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2369. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2370. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff*2}, 0);
  2371. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  2372. }
  2373. } break;
  2374. case LLM_ARCH_JINA_BERT_V2:
  2375. {
  2376. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); // word_embeddings
  2377. type_embd = create_tensor(tn(LLM_TENSOR_TOKEN_TYPES, "weight"), {n_embd, n_token_types}, 0); // token_type_embeddings
  2378. tok_norm = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "weight"), {n_embd}, 0); // LayerNorm
  2379. tok_norm_b = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "bias"), {n_embd}, 0); //LayerNorm bias
  2380. cls = create_tensor(tn(LLM_TENSOR_CLS, "weight"), {n_embd, 1}, TENSOR_NOT_REQUIRED);
  2381. cls_b = create_tensor(tn(LLM_TENSOR_CLS, "bias"), {1}, TENSOR_NOT_REQUIRED);
  2382. for (int i = 0; i < n_layer; ++i) {
  2383. auto & layer = layers[i]; // JinaBertLayer
  2384. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  2385. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, 0);
  2386. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2387. layer.attn_q_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2388. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  2389. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, 0);
  2390. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2391. layer.attn_k_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2392. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  2393. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, 0);
  2394. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0); //output_dens
  2395. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0); //output_dens
  2396. layer.attn_out_norm = create_tensor(tn(LLM_TENSOR_ATTN_OUT_NORM, "weight", i), {n_embd}, 0); //output_norm
  2397. layer.attn_out_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_OUT_NORM, "bias", i), {n_embd}, 0);
  2398. layer.attn_norm_2 = create_tensor(tn(LLM_TENSOR_ATTN_NORM_2, "weight", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2399. layer.attn_norm_2_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM_2, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2400. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, TENSOR_NOT_REQUIRED);
  2401. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, layer.ffn_gate ? n_ff : n_ff * 2}, 0);
  2402. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  2403. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, 0);
  2404. layer.layer_out_norm = create_tensor(tn(LLM_TENSOR_LAYER_OUT_NORM, "weight", i), {n_embd}, 0);
  2405. layer.layer_out_norm_b = create_tensor(tn(LLM_TENSOR_LAYER_OUT_NORM, "bias", i), {n_embd}, 0);
  2406. }
  2407. } break;
  2408. case LLM_ARCH_BLOOM:
  2409. {
  2410. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2411. tok_norm = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "weight"), {n_embd}, 0);
  2412. tok_norm_b = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "bias"), {n_embd}, 0);
  2413. // output
  2414. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2415. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {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_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0);
  2425. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, 0);
  2426. layer.bqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd + 2*n_embd_gqa}, 0);
  2427. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2428. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0);
  2429. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2430. layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd}, 0);
  2431. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  2432. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, 0);
  2433. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2434. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, 0);
  2435. }
  2436. } break;
  2437. case LLM_ARCH_MPT:
  2438. {
  2439. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2440. pos_embd = create_tensor(tn(LLM_TENSOR_POS_EMBD, "weight"), {n_embd, n_ctx_train}, TENSOR_NOT_REQUIRED);
  2441. // output
  2442. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2443. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, TENSOR_NOT_REQUIRED);
  2444. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  2445. if (!output) {
  2446. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); // needs to be on GPU
  2447. }
  2448. for (int i = 0; i < n_layer; ++i) {
  2449. auto & layer = layers[i];
  2450. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2451. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2452. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, 0);
  2453. layer.bqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd + 2*n_embd_gqa}, TENSOR_NOT_REQUIRED);
  2454. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2455. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2456. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2457. layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2458. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  2459. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2460. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2461. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, TENSOR_NOT_REQUIRED);
  2462. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2463. layer.attn_q_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2464. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2465. layer.attn_k_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2466. // AWQ ScaleActivation layer
  2467. layer.ffn_act = create_tensor(tn(LLM_TENSOR_FFN_ACT, "scales", i), {n_ff}, TENSOR_NOT_REQUIRED);
  2468. }
  2469. } break;
  2470. case LLM_ARCH_STABLELM:
  2471. {
  2472. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2473. // output
  2474. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
  2475. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2476. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  2477. for (int i = 0; i < n_layer; ++i) {
  2478. auto & layer = layers[i];
  2479. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2480. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0);
  2481. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  2482. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  2483. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  2484. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2485. // optional bias tensors, present in Stable LM 2 1.6B
  2486. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2487. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED);
  2488. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED);
  2489. // optional q and k layernorms, present in StableLM 2 12B
  2490. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k, n_head}, TENSOR_NOT_REQUIRED);
  2491. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k, n_head_kv}, TENSOR_NOT_REQUIRED);
  2492. // optional FFN norm, not present in StableLM 2 12B which uses parallel residual
  2493. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2494. layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2495. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  2496. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  2497. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2498. }
  2499. } break;
  2500. case LLM_ARCH_QWEN:
  2501. {
  2502. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2503. // output
  2504. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2505. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  2506. for (int i = 0; i < n_layer; ++i) {
  2507. auto & layer = layers[i];
  2508. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2509. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd*3}, 0);
  2510. layer.bqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd*3}, 0);
  2511. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2512. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2513. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff/2}, 0);
  2514. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff/2, n_embd}, 0);
  2515. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff/2}, 0);
  2516. }
  2517. } break;
  2518. case LLM_ARCH_QWEN2:
  2519. case LLM_ARCH_QWEN2VL:
  2520. case LLM_ARCH_DREAM:
  2521. {
  2522. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2523. // output
  2524. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2525. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  2526. output_b = create_tensor(tn(LLM_TENSOR_OUTPUT, "bias"), {n_vocab}, TENSOR_NOT_REQUIRED);
  2527. // if output is NULL, init from the input tok embed
  2528. if (output == NULL) {
  2529. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  2530. }
  2531. for (int i = 0; i < n_layer; ++i) {
  2532. auto & layer = layers[i];
  2533. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2534. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  2535. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  2536. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  2537. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2538. // optional bias tensors
  2539. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, 0);
  2540. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, 0);
  2541. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, 0);
  2542. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2543. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  2544. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  2545. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2546. }
  2547. } break;
  2548. case LLM_ARCH_QWEN2MOE:
  2549. {
  2550. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2551. // output
  2552. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2553. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  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.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  2558. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  2559. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  2560. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2561. // optional bias tensors
  2562. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2563. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED);
  2564. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED);
  2565. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2566. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  2567. if (n_expert == 0) {
  2568. throw std::runtime_error("n_expert must be > 0 for QWEN2MOE");
  2569. }
  2570. if (n_expert_used == 0) {
  2571. throw std::runtime_error("n_expert_used must be > 0 for QWEN2MOE");
  2572. }
  2573. // MoE branch
  2574. const int64_t n_ff_exp = hparams.n_ff_exp ? hparams.n_ff_exp : n_ff / n_expert_used;
  2575. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);
  2576. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp, n_embd, n_expert}, 0);
  2577. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);
  2578. // Shared expert branch
  2579. const int64_t n_ff_shexp = hparams.n_ff_shexp ? hparams.n_ff_shexp : n_ff;
  2580. layer.ffn_gate_inp_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP_SHEXP, "weight", i), {n_embd}, 0);
  2581. layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), { n_embd, n_ff_shexp}, 0);
  2582. layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), {n_ff_shexp, n_embd}, 0);
  2583. layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), { n_embd, n_ff_shexp}, 0);
  2584. }
  2585. } break;
  2586. case LLM_ARCH_QWEN3:
  2587. {
  2588. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2589. // output
  2590. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2591. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  2592. // if output is NULL, init from the input tok embed
  2593. if (output == NULL) {
  2594. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  2595. }
  2596. for (int i = 0; i < n_layer; ++i) {
  2597. auto & layer = layers[i];
  2598. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2599. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  2600. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  2601. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  2602. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  2603. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0);
  2604. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0);
  2605. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2606. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  2607. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  2608. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2609. }
  2610. } break;
  2611. case LLM_ARCH_QWEN3MOE:
  2612. {
  2613. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2614. // output
  2615. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2616. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  2617. // if output is NULL, init from the input tok embed
  2618. if (output == NULL) {
  2619. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  2620. }
  2621. for (int i = 0; i < n_layer; ++i) {
  2622. auto & layer = layers[i];
  2623. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2624. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  2625. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  2626. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  2627. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  2628. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0);
  2629. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0);
  2630. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2631. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  2632. if (n_expert == 0) {
  2633. throw std::runtime_error("n_expert must be > 0 for QWEN3MOE");
  2634. }
  2635. if (n_expert_used == 0) {
  2636. throw std::runtime_error("n_expert_used must be > 0 for QWEN3MOE");
  2637. }
  2638. // MoE branch
  2639. const int64_t n_ff_exp = hparams.n_ff_exp ? hparams.n_ff_exp : n_ff / n_expert_used;
  2640. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);
  2641. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp, n_embd, n_expert}, 0);
  2642. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);
  2643. }
  2644. } break;
  2645. case LLM_ARCH_PHI2:
  2646. {
  2647. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2648. // output
  2649. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2650. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
  2651. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  2652. output_b = create_tensor(tn(LLM_TENSOR_OUTPUT, "bias"), {n_vocab}, 0);
  2653. for (int i = 0; i < n_layer; ++i) {
  2654. auto & layer = layers[i];
  2655. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2656. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0);
  2657. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, TENSOR_NOT_REQUIRED);
  2658. layer.bqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd + 2*n_embd_gqa}, TENSOR_NOT_REQUIRED);
  2659. if (layer.wqkv == nullptr) {
  2660. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  2661. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, 0);
  2662. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  2663. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, 0);
  2664. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  2665. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, 0);
  2666. }
  2667. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2668. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0);
  2669. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  2670. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, 0);
  2671. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2672. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, 0);
  2673. }
  2674. } break;
  2675. case LLM_ARCH_PHI3:
  2676. {
  2677. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, 0);
  2678. // output
  2679. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), { n_embd }, 0);
  2680. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  2681. // if output is NULL, init from the input tok embed
  2682. if (output == NULL) {
  2683. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  2684. }
  2685. for (int i = 0; i < n_layer; ++i) {
  2686. auto & layer = layers[i];
  2687. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), { n_embd }, 0);
  2688. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), { n_embd, n_embd + 2 * n_embd_gqa }, TENSOR_NOT_REQUIRED);
  2689. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), { n_embd, n_embd }, 0);
  2690. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), { n_embd }, 0);
  2691. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd }, 0);
  2692. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), { n_embd, 2 * n_ff }, 0);
  2693. layer.rope_long = create_tensor(tn(LLM_TENSOR_ROPE_FACTORS_LONG, "weight", i), { n_rot/2 }, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  2694. layer.rope_short = create_tensor(tn(LLM_TENSOR_ROPE_FACTORS_SHORT, "weight", i), { n_rot/2 }, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  2695. }
  2696. } break;
  2697. case LLM_ARCH_PHIMOE:
  2698. {
  2699. const int64_t n_embd_head = n_embd / n_head;
  2700. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, 0);
  2701. // output
  2702. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), { n_embd }, 0);
  2703. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
  2704. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), { n_embd, n_vocab }, 0);
  2705. output_b = create_tensor(tn(LLM_TENSOR_OUTPUT, "bias"), { n_vocab }, 0);
  2706. for (int i = 0; i < n_layer; ++i) {
  2707. auto & layer = layers[i];
  2708. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), { n_embd }, 0);
  2709. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), { n_embd }, 0);
  2710. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), { n_embd, n_embd + 2 * n_embd_gqa }, TENSOR_NOT_REQUIRED);
  2711. if (layer.wqkv == nullptr) {
  2712. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  2713. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, 0);
  2714. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  2715. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, 0);
  2716. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  2717. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, 0);
  2718. }
  2719. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), { n_embd, n_embd }, 0);
  2720. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), { n_embd }, 0);
  2721. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), { n_embd }, 0);
  2722. layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), { n_embd }, 0);
  2723. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  2724. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0);
  2725. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff, n_embd, n_expert}, 0);
  2726. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0);
  2727. 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));
  2728. 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));
  2729. }
  2730. } break;
  2731. case LLM_ARCH_PLAMO:
  2732. {
  2733. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2734. // output
  2735. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2736. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  2737. for (int i = 0; i < n_layer; ++i) {
  2738. auto & layer = layers[i];
  2739. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2740. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  2741. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  2742. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  2743. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2744. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  2745. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  2746. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2747. }
  2748. } break;
  2749. case LLM_ARCH_PLAMO2:
  2750. {
  2751. const uint32_t d_conv = hparams.ssm_d_conv;
  2752. const uint32_t d_state = hparams.ssm_d_state;
  2753. const uint32_t num_heads = hparams.ssm_dt_rank;
  2754. const uint32_t intermediate_size = hparams.ssm_d_inner;
  2755. const uint32_t head_dim = intermediate_size / num_heads;
  2756. const uint32_t qk_dim = head_dim;
  2757. const uint32_t v_dim = head_dim;
  2758. const int64_t num_attention_heads = hparams.n_head();
  2759. const int64_t q_num_heads = num_attention_heads;
  2760. const int64_t dt_dim = std::max(64, int(hparams.n_embd / 16));
  2761. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2762. // output
  2763. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2764. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  2765. // if output is NULL, init from the input tok embed
  2766. if (output == NULL) {
  2767. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  2768. }
  2769. for (int i = 0; i < n_layer; ++i) {
  2770. auto & layer = layers[i];
  2771. bool is_mamba_layer = hparams.is_recurrent(i);
  2772. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2773. if (is_mamba_layer) {
  2774. layer.ssm_in = create_tensor(tn(LLM_TENSOR_SSM_IN, "weight", i), {n_embd, 2 * intermediate_size}, 0);
  2775. layer.ssm_conv1d = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "weight", i), {d_conv, intermediate_size}, 0);
  2776. layer.ssm_x = create_tensor(tn(LLM_TENSOR_SSM_X, "weight", i), {intermediate_size, dt_dim + 2*d_state}, 0);
  2777. layer.ssm_dt = create_tensor(tn(LLM_TENSOR_SSM_DT, "weight", i), {dt_dim, num_heads}, 0);
  2778. layer.ssm_dt_b = create_tensor(tn(LLM_TENSOR_SSM_DT, "bias", i), {num_heads}, 0);
  2779. layer.ssm_a = create_tensor(tn(LLM_TENSOR_SSM_A, i), {num_heads}, 0);
  2780. layer.ssm_d = create_tensor(tn(LLM_TENSOR_SSM_D, i), {num_heads}, 0);
  2781. layer.ssm_out = create_tensor(tn(LLM_TENSOR_SSM_OUT, "weight", i), {intermediate_size, n_embd}, 0);
  2782. layer.ssm_dt_norm = create_tensor(tn(LLM_TENSOR_SSM_DT_NORM, i), {dt_dim}, 0);
  2783. layer.ssm_b_norm = create_tensor(tn(LLM_TENSOR_SSM_B_NORM, i), {d_state}, 0);
  2784. layer.ssm_c_norm = create_tensor(tn(LLM_TENSOR_SSM_C_NORM, i), {d_state}, 0);
  2785. } else {
  2786. const int64_t num_key_value_heads = hparams.n_head_kv(i);
  2787. const int64_t k_num_heads = num_key_value_heads;
  2788. const int64_t v_num_heads = num_key_value_heads;
  2789. const int64_t q_proj_dim = q_num_heads * qk_dim;
  2790. const int64_t k_proj_dim = k_num_heads * qk_dim;
  2791. const int64_t v_proj_dim = v_num_heads * v_dim;
  2792. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, q_proj_dim + k_proj_dim + v_proj_dim}, 0);
  2793. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {head_dim, num_attention_heads}, 0);
  2794. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {head_dim, k_num_heads}, 0);
  2795. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {q_num_heads * v_dim, n_embd}, 0);
  2796. }
  2797. // All layers have post-attention norm, FFN norm, and FFN tensors
  2798. layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, i), {n_embd}, 0);
  2799. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2800. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  2801. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff * 2}, 0);
  2802. layer.ffn_post_norm = create_tensor(tn(LLM_TENSOR_FFN_POST_NORM, i), {n_embd}, 0);
  2803. }
  2804. } break;
  2805. case LLM_ARCH_GPT2:
  2806. {
  2807. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2808. pos_embd = create_tensor(tn(LLM_TENSOR_POS_EMBD, "weight"), {n_embd, n_ctx_train}, 0);
  2809. // output
  2810. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2811. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
  2812. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  2813. // if output is NULL, init from the input tok embed
  2814. if (output == NULL) {
  2815. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  2816. }
  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.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0);
  2821. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, 0);
  2822. layer.bqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd + 2*n_embd_gqa}, 0);
  2823. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2824. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0);
  2825. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2826. layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd}, 0);
  2827. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  2828. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, 0);
  2829. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2830. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, 0);
  2831. }
  2832. } break;
  2833. case LLM_ARCH_CODESHELL:
  2834. {
  2835. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  2836. // if tok embd is NULL, init from output
  2837. if (tok_embd == NULL) {
  2838. tok_embd = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  2839. }
  2840. // output
  2841. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2842. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
  2843. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  2844. for (int i = 0; i < n_layer; ++i) {
  2845. auto & layer = layers[i];
  2846. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2847. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0);
  2848. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, 0);
  2849. layer.bqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd + 2*n_embd_gqa}, 0);
  2850. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2851. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0);
  2852. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2853. layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd}, 0);
  2854. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  2855. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, 0);
  2856. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2857. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, 0);
  2858. }
  2859. } break;
  2860. case LLM_ARCH_ORION:
  2861. {
  2862. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2863. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2864. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
  2865. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  2866. for (int i = 0; i < n_layer; ++i) {
  2867. auto & layer = layers[i];
  2868. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2869. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0);
  2870. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  2871. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  2872. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  2873. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2874. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2875. layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd}, 0);
  2876. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  2877. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  2878. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2879. }
  2880. } break;
  2881. case LLM_ARCH_INTERNLM2:
  2882. {
  2883. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2884. // output
  2885. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2886. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  2887. for (int i = 0; i < n_layer; ++i) {
  2888. auto & layer = layers[i];
  2889. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2890. // layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, 0);
  2891. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  2892. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  2893. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  2894. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2895. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2896. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  2897. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  2898. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2899. }
  2900. } break;
  2901. case LLM_ARCH_GEMMA:
  2902. {
  2903. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2904. // output
  2905. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2906. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); // same as tok_embd, duplicated to allow offloading
  2907. for (int i = 0; i < n_layer; ++i) {
  2908. auto & layer = layers[i];
  2909. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2910. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  2911. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  2912. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  2913. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  2914. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2915. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  2916. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2917. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  2918. }
  2919. } break;
  2920. case LLM_ARCH_GEMMA2:
  2921. {
  2922. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2923. // output
  2924. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2925. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); // same as tok_embd, duplicated to allow offloading
  2926. for (int i = 0; i < n_layer; ++i) {
  2927. auto & layer = layers[i];
  2928. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2929. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  2930. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  2931. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  2932. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  2933. layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), {n_embd}, 0);
  2934. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2935. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  2936. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2937. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  2938. layer.ffn_post_norm = create_tensor(tn(LLM_TENSOR_FFN_POST_NORM, "weight", i), {n_embd}, 0);
  2939. }
  2940. } break;
  2941. case LLM_ARCH_GEMMA3:
  2942. {
  2943. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2944. // output
  2945. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2946. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  2947. // if output is NULL, init from the input tok embed
  2948. if (output == NULL) {
  2949. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  2950. }
  2951. for (int i = 0; i < n_layer; ++i) {
  2952. auto & layer = layers[i];
  2953. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2954. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  2955. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  2956. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  2957. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  2958. layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), {n_embd}, 0);
  2959. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0);
  2960. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0);
  2961. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2962. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  2963. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2964. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  2965. layer.ffn_post_norm = create_tensor(tn(LLM_TENSOR_FFN_POST_NORM, "weight", i), {n_embd}, 0);
  2966. }
  2967. } break;
  2968. case LLM_ARCH_GEMMA3N:
  2969. {
  2970. const int64_t n_altup = hparams.n_altup;
  2971. const int64_t laurel_rank = hparams.laurel_rank;
  2972. const int64_t n_embd_altup = hparams.n_embd_altup;
  2973. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  2974. // if output is NULL, init from the input tok embed
  2975. if (output == NULL) {
  2976. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  2977. }
  2978. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2979. tok_embd_per_layer = create_tensor(tn(LLM_TENSOR_PER_LAYER_TOKEN_EMBD, "weight"), {n_embd_altup * n_layer, n_vocab}, 0);
  2980. altup_proj = create_tensor(tn(LLM_TENSOR_ALTUP_PROJ, "weight"), {n_embd, n_embd, n_altup - 1}, 0);
  2981. altup_unembd_proj = create_tensor(tn(LLM_TENSOR_ALTUP_UNEMBD_PROJ, "weight"), {n_embd, n_embd, n_altup - 1}, 0);
  2982. per_layer_model_proj = create_tensor(tn(LLM_TENSOR_PER_LAYER_MODEL_PROJ, "weight"), {n_embd, n_embd_altup * n_layer}, 0);
  2983. per_layer_proj_norm = create_tensor(tn(LLM_TENSOR_PER_LAYER_PROJ_NORM, "weight"), {n_embd_altup}, 0);
  2984. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2985. for (int i = 0; i < n_layer; ++i) {
  2986. auto & layer = layers[i];
  2987. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2988. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  2989. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  2990. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  2991. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  2992. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0);
  2993. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0);
  2994. layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), {n_embd}, 0);
  2995. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2996. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  2997. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2998. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  2999. layer.ffn_post_norm = create_tensor(tn(LLM_TENSOR_FFN_POST_NORM, "weight", i), {n_embd}, 0);
  3000. // altup & laurel
  3001. layer.per_layer_inp_gate = create_tensor(tn(LLM_TENSOR_PER_LAYER_INP_GATE, "weight", i), {n_embd, n_embd_altup}, 0);
  3002. layer.per_layer_proj = create_tensor(tn(LLM_TENSOR_PER_LAYER_PROJ, "weight", i), {n_embd_altup, n_embd}, 0);
  3003. layer.per_layer_post_norm = create_tensor(tn(LLM_TENSOR_PER_LAYER_POST_NORM, "weight", i), {n_embd}, 0);
  3004. layer.altup_correct_coef = create_tensor(tn(LLM_TENSOR_ALTUP_CORRECT_COEF, "weight", i), {n_altup, n_altup}, 0);
  3005. layer.altup_correct_scale = create_tensor(tn(LLM_TENSOR_ALTUP_CORRECT_SCALE, "weight", i), {n_embd}, 0);
  3006. layer.altup_predict_coef = create_tensor(tn(LLM_TENSOR_ALTUP_PREDICT_COEF, "weight", i), {n_altup, n_altup * n_altup}, 0);
  3007. layer.altup_router = create_tensor(tn(LLM_TENSOR_ALTUP_ROUTER, "weight", i), {n_embd, n_altup}, 0);
  3008. layer.altup_router_norm = create_tensor(tn(LLM_TENSOR_ALTUP_ROUTER_NORM, "weight", i), {n_embd}, 0);
  3009. layer.laurel_l = create_tensor(tn(LLM_TENSOR_LAUREL_L, "weight", i), {n_embd, laurel_rank}, 0);
  3010. layer.laurel_r = create_tensor(tn(LLM_TENSOR_LAUREL_R, "weight", i), {laurel_rank, n_embd}, 0);
  3011. layer.laurel_post_norm = create_tensor(tn(LLM_TENSOR_LAUREL_POST_NORM, "weight", i), {n_embd}, 0);
  3012. }
  3013. } break;
  3014. case LLM_ARCH_STARCODER2:
  3015. {
  3016. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3017. // output
  3018. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3019. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
  3020. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  3021. // if output is NULL, init from the input tok embed
  3022. if (output == NULL) {
  3023. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3024. }
  3025. for (int i = 0; i < n_layer; ++i) {
  3026. auto & layer = layers[i];
  3027. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3028. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0);
  3029. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  3030. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  3031. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  3032. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  3033. // optional bias tensors
  3034. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, 0);
  3035. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, 0);
  3036. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, 0);
  3037. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0);
  3038. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3039. layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd}, 0);
  3040. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3041. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3042. // optional bias tensors
  3043. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, 0);
  3044. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP , "bias", i), { n_ff}, 0);
  3045. }
  3046. } break;
  3047. case LLM_ARCH_MAMBA:
  3048. {
  3049. const int64_t d_conv = hparams.ssm_d_conv;
  3050. const int64_t d_inner = hparams.ssm_d_inner;
  3051. const int64_t d_state = hparams.ssm_d_state;
  3052. const int64_t dt_rank = hparams.ssm_dt_rank;
  3053. // only an expansion factor of 2 is supported for now
  3054. if (2 * n_embd != d_inner) {
  3055. throw std::runtime_error("only an expansion factor of 2 is supported for now");
  3056. }
  3057. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3058. // output
  3059. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3060. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  3061. // if output is NULL, init from the input tok embed, duplicated to allow offloading
  3062. if (output == NULL) {
  3063. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3064. }
  3065. for (int i = 0; i < n_layer; ++i) {
  3066. auto & layer = layers[i];
  3067. // norm
  3068. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3069. layer.ssm_in = create_tensor(tn(LLM_TENSOR_SSM_IN, "weight", i), {n_embd, 2*d_inner}, 0);
  3070. layer.ssm_conv1d = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "weight", i), {d_conv, d_inner}, 0);
  3071. layer.ssm_conv1d_b = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "bias", i), {d_inner}, 0);
  3072. layer.ssm_x = create_tensor(tn(LLM_TENSOR_SSM_X, "weight", i), {d_inner, dt_rank + 2*d_state}, 0);
  3073. layer.ssm_dt = create_tensor(tn(LLM_TENSOR_SSM_DT, "weight", i), {dt_rank, d_inner}, 0);
  3074. layer.ssm_dt_b = create_tensor(tn(LLM_TENSOR_SSM_DT, "bias", i), {d_inner}, 0);
  3075. // no "weight" suffix for these
  3076. layer.ssm_a = create_tensor(tn(LLM_TENSOR_SSM_A, i), {d_state, d_inner}, 0);
  3077. layer.ssm_d = create_tensor(tn(LLM_TENSOR_SSM_D, i), {d_inner}, 0);
  3078. // out_proj
  3079. layer.ssm_out = create_tensor(tn(LLM_TENSOR_SSM_OUT, "weight", i), {d_inner, n_embd}, 0);
  3080. }
  3081. } break;
  3082. case LLM_ARCH_MAMBA2:
  3083. {
  3084. const int64_t d_conv = hparams.ssm_d_conv;
  3085. const int64_t d_inner = hparams.ssm_d_inner;
  3086. const int64_t d_state = hparams.ssm_d_state;
  3087. const int64_t n_head = hparams.ssm_dt_rank;
  3088. const int64_t n_group = hparams.ssm_n_group;
  3089. const int64_t d_in_proj = 2*d_inner + 2*n_group*d_state + n_head;
  3090. // only an expansion factor of 2 is supported for now
  3091. GGML_ASSERT(2 * n_embd == d_inner);
  3092. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3093. // output
  3094. {
  3095. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3096. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  3097. // if output is NULL, init from the input tok embed, duplicated to allow offloading
  3098. if (output == NULL) {
  3099. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3100. }
  3101. }
  3102. for (int i = 0; i < n_layer; ++i) {
  3103. auto & layer = layers[i];
  3104. // norm
  3105. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3106. layer.ssm_in = create_tensor(tn(LLM_TENSOR_SSM_IN, "weight", i), {n_embd, d_in_proj}, 0);
  3107. layer.ssm_conv1d = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "weight", i), {d_conv, d_inner + 2*n_group*d_state}, 0);
  3108. layer.ssm_conv1d_b = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "bias", i), {d_inner + 2*n_group*d_state}, 0);
  3109. layer.ssm_dt_b = create_tensor(tn(LLM_TENSOR_SSM_DT, "bias", i), {n_head}, 0);
  3110. // no "weight" suffix for these
  3111. layer.ssm_a = create_tensor(tn(LLM_TENSOR_SSM_A, i), {1, n_head}, 0);
  3112. layer.ssm_d = create_tensor(tn(LLM_TENSOR_SSM_D, i), {1, n_head}, 0);
  3113. layer.ssm_norm = create_tensor(tn(LLM_TENSOR_SSM_NORM, "weight", i), {d_inner / n_group, n_group}, 0);
  3114. // out_proj
  3115. layer.ssm_out = create_tensor(tn(LLM_TENSOR_SSM_OUT, "weight", i), {d_inner, n_embd}, 0);
  3116. }
  3117. } break;
  3118. case LLM_ARCH_JAMBA:
  3119. {
  3120. const int64_t d_conv = hparams.ssm_d_conv;
  3121. const int64_t d_inner = hparams.ssm_d_inner;
  3122. const int64_t d_state = hparams.ssm_d_state;
  3123. const int64_t dt_rank = hparams.ssm_dt_rank;
  3124. // only an expansion factor of 2 is supported for now
  3125. GGML_ASSERT(2 * n_embd == d_inner);
  3126. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3127. // output
  3128. {
  3129. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3130. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  3131. // if output is NULL, init from the input tok embed, duplicated to allow offloading
  3132. if (output == NULL) {
  3133. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3134. }
  3135. }
  3136. for (int i = 0; i < n_layer; ++i) {
  3137. const int64_t n_head_kv = hparams.n_head_kv(i);
  3138. const int64_t n_embd_gqa = hparams.n_embd_v_gqa(i);
  3139. auto & layer = layers[i];
  3140. // norm
  3141. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3142. if (n_head_kv == 0) {
  3143. // Mamba layer
  3144. layer.ssm_in = create_tensor(tn(LLM_TENSOR_SSM_IN, "weight", i), {n_embd, 2*d_inner}, 0);
  3145. layer.ssm_conv1d = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "weight", i), {d_conv, d_inner}, 0);
  3146. layer.ssm_conv1d_b = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "bias", i), {d_inner}, 0);
  3147. layer.ssm_x = create_tensor(tn(LLM_TENSOR_SSM_X, "weight", i), {d_inner, dt_rank + 2*d_state}, 0);
  3148. layer.ssm_dt_norm = create_tensor(tn(LLM_TENSOR_SSM_DT_NORM, "weight", i), {dt_rank}, 0);
  3149. layer.ssm_dt = create_tensor(tn(LLM_TENSOR_SSM_DT, "weight", i), {dt_rank, d_inner}, 0);
  3150. layer.ssm_dt_b = create_tensor(tn(LLM_TENSOR_SSM_DT, "bias", i), {d_inner}, 0);
  3151. layer.ssm_b_norm = create_tensor(tn(LLM_TENSOR_SSM_B_NORM, "weight", i), {d_state}, 0);
  3152. layer.ssm_c_norm = create_tensor(tn(LLM_TENSOR_SSM_C_NORM, "weight", i), {d_state}, 0);
  3153. // no "weight" suffix for these
  3154. layer.ssm_a = create_tensor(tn(LLM_TENSOR_SSM_A, i), {d_state, d_inner}, 0);
  3155. layer.ssm_d = create_tensor(tn(LLM_TENSOR_SSM_D, i), {d_inner}, 0);
  3156. // out_proj
  3157. layer.ssm_out = create_tensor(tn(LLM_TENSOR_SSM_OUT, "weight", i), {d_inner, n_embd}, 0);
  3158. } else {
  3159. // Attention layers
  3160. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  3161. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  3162. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  3163. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  3164. }
  3165. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3166. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, TENSOR_NOT_REQUIRED);
  3167. if (layer.ffn_gate_inp) {
  3168. // MoE
  3169. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0);
  3170. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff, n_embd, n_expert}, 0);
  3171. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0);
  3172. } else {
  3173. // FFN (no MoE)
  3174. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  3175. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  3176. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3177. }
  3178. }
  3179. } break;
  3180. case LLM_ARCH_GRANITE_HYBRID:
  3181. {
  3182. // mamba2 Mixer SSM params
  3183. // NOTE: int64_t for tensor dimensions
  3184. const int64_t d_conv = hparams.ssm_d_conv;
  3185. const int64_t d_inner = hparams.ssm_d_inner;
  3186. const int64_t d_state = hparams.ssm_d_state;
  3187. const int64_t n_ssm_head = hparams.ssm_dt_rank;
  3188. const int64_t n_group = hparams.ssm_n_group;
  3189. const int64_t d_in_proj = 2*d_inner + 2*n_group*d_state + n_ssm_head;
  3190. // only an expansion factor of 2 is supported for now
  3191. GGML_ASSERT(2 * n_embd == d_inner);
  3192. // embeddings
  3193. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3194. // output
  3195. {
  3196. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3197. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  3198. // if output is NULL, init from the input tok embed, duplicated to allow offloading
  3199. if (output == NULL) {
  3200. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3201. }
  3202. }
  3203. for (int i = 0; i < n_layer; ++i) {
  3204. auto & layer = layers[i];
  3205. // norm
  3206. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3207. if (hparams.is_recurrent(i)) {
  3208. // ssm layers
  3209. layer.ssm_in = create_tensor(tn(LLM_TENSOR_SSM_IN, "weight", i), {n_embd, d_in_proj}, 0);
  3210. layer.ssm_conv1d = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "weight", i), {d_conv, d_inner + 2*n_group*d_state}, 0);
  3211. layer.ssm_conv1d_b = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "bias", i), {d_inner + 2*n_group*d_state}, TENSOR_NOT_REQUIRED);
  3212. layer.ssm_dt_b = create_tensor(tn(LLM_TENSOR_SSM_DT, "bias", i), {n_ssm_head}, 0);
  3213. // no "weight" suffix for these
  3214. layer.ssm_a = create_tensor(tn(LLM_TENSOR_SSM_A, i), {1, n_ssm_head}, 0);
  3215. layer.ssm_d = create_tensor(tn(LLM_TENSOR_SSM_D, i), {1, n_ssm_head}, 0);
  3216. layer.ssm_norm = create_tensor(tn(LLM_TENSOR_SSM_NORM, "weight", i), {d_inner / n_group, n_group}, 0);
  3217. // out_proj
  3218. layer.ssm_out = create_tensor(tn(LLM_TENSOR_SSM_OUT, "weight", i), {d_inner, n_embd}, 0);
  3219. } else {
  3220. // attention layers (with optional bias)
  3221. const int64_t n_head_i = hparams.n_head(i);
  3222. const int64_t n_embd_k_gqa_i = hparams.n_embd_k_gqa(i);
  3223. const int64_t n_embd_v_gqa_i = hparams.n_embd_v_gqa(i);
  3224. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head_i}, 0);
  3225. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa_i}, 0);
  3226. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa_i}, 0);
  3227. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head_i, n_embd}, 0);
  3228. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  3229. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_k_gqa_i}, TENSOR_NOT_REQUIRED);
  3230. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_v_gqa_i}, TENSOR_NOT_REQUIRED);
  3231. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  3232. }
  3233. // feed forward (w/ optional biases)
  3234. if (n_expert > 0) {
  3235. // MoE FFN
  3236. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3237. layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  3238. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  3239. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff, n_expert}, TENSOR_NOT_REQUIRED);
  3240. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), { n_ff, n_embd, n_expert}, 0);
  3241. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0);
  3242. // For Granite MoE Shared
  3243. if (hparams.n_ff_shexp > 0) {
  3244. layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), {n_embd, hparams.n_ff_shexp}, 0);
  3245. layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), {n_embd, hparams.n_ff_shexp}, 0);
  3246. layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), {hparams.n_ff_shexp, n_embd}, 0);
  3247. }
  3248. } else {
  3249. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3250. layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  3251. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  3252. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3253. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3254. layer.ffn_gate_b = create_tensor(tn(LLM_TENSOR_FFN_GATE, "bias", i), {n_ff}, TENSOR_NOT_REQUIRED);
  3255. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  3256. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, TENSOR_NOT_REQUIRED);
  3257. }
  3258. }
  3259. } break;
  3260. case LLM_ARCH_XVERSE:
  3261. {
  3262. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3263. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3264. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  3265. for (int i = 0; i < n_layer; ++i) {
  3266. auto & layer = layers[i];
  3267. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3268. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  3269. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  3270. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  3271. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  3272. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3273. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  3274. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3275. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3276. }
  3277. } break;
  3278. case LLM_ARCH_COMMAND_R:
  3279. {
  3280. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3281. // output
  3282. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3283. // init output from the input tok embed
  3284. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3285. for (int i = 0; i < n_layer; ++i) {
  3286. auto & layer = layers[i];
  3287. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3288. if (n_layer >= 64){
  3289. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k, n_head}, 0);
  3290. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k, n_head_kv}, 0);
  3291. }
  3292. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  3293. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  3294. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  3295. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  3296. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  3297. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3298. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3299. }
  3300. } break;
  3301. case LLM_ARCH_COHERE2:
  3302. {
  3303. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, 0);
  3304. // output
  3305. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), { n_embd }, 0);
  3306. // init output from the input tok embed
  3307. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab },
  3308. TENSOR_DUPLICATED);
  3309. for (int i = 0; i < n_layer; ++i) {
  3310. auto & layer = layers[i];
  3311. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), { n_embd }, 0);
  3312. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), { n_embd, n_embd }, 0);
  3313. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), { n_embd, n_embd_gqa }, 0);
  3314. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), { n_embd, n_embd_gqa }, 0);
  3315. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), { n_embd, n_embd }, 0);
  3316. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), { n_embd, n_ff }, 0);
  3317. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd }, 0);
  3318. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), { n_embd, n_ff }, 0);
  3319. }
  3320. }
  3321. break;
  3322. case LLM_ARCH_OLMO: // adapted from LLM_ARCH_LLAMA with norm params removed
  3323. {
  3324. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3325. // output
  3326. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  3327. // if output is NULL, init from the input tok embed
  3328. if (output == NULL) {
  3329. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3330. }
  3331. for (int i = 0; i < n_layer; ++i) {
  3332. auto & layer = layers[i];
  3333. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  3334. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  3335. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  3336. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  3337. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  3338. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3339. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3340. }
  3341. } break;
  3342. case LLM_ARCH_OLMO2:
  3343. {
  3344. const int64_t n_embd_head = n_embd / n_head;
  3345. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3346. // output
  3347. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3348. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  3349. for (int i = 0; i < n_layer; ++i) {
  3350. auto & layer = layers[i];
  3351. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  3352. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  3353. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  3354. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  3355. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd}, 0);
  3356. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_head_kv * n_embd_head}, 0);
  3357. layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), {n_embd}, 0);
  3358. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  3359. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3360. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3361. layer.ffn_post_norm = create_tensor(tn(LLM_TENSOR_FFN_POST_NORM, "weight", i), {n_embd}, 0);
  3362. }
  3363. } break;
  3364. case LLM_ARCH_OLMOE:
  3365. {
  3366. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3367. // output
  3368. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3369. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  3370. for (int i = 0; i < n_layer; ++i) {
  3371. auto & layer = layers[i];
  3372. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3373. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  3374. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  3375. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  3376. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  3377. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd}, 0);
  3378. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd}, 0);
  3379. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3380. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  3381. if (n_expert == 0) {
  3382. throw std::runtime_error("n_expert must be > 0");
  3383. }
  3384. if (n_expert_used == 0) {
  3385. throw std::runtime_error("n_expert_used must be > 0");
  3386. }
  3387. // MoE branch
  3388. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0);
  3389. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff, n_embd, n_expert}, 0);
  3390. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0);
  3391. }
  3392. } break;
  3393. case LLM_ARCH_OPENELM:
  3394. {
  3395. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3396. // output
  3397. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3398. // init output from the input tok embed
  3399. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3400. for (int i = 0; i < n_layer; ++i) {
  3401. const int64_t n_head = hparams.n_head(i);
  3402. const int64_t n_head_qkv = 2*hparams.n_head_kv(i) + n_head;
  3403. const int64_t n_ff = hparams.n_ff(i);
  3404. auto & layer = layers[i];
  3405. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3406. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_head_qkv*n_embd_head_k}, 0);
  3407. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0);
  3408. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0);
  3409. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_head*n_embd_head_k, n_embd}, 0);
  3410. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3411. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  3412. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  3413. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3414. }
  3415. } break;
  3416. case LLM_ARCH_GPTNEOX:
  3417. {
  3418. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3419. // output
  3420. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3421. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
  3422. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  3423. for (int i = 0; i < n_layer; ++i) {
  3424. auto & layer = layers[i];
  3425. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3426. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0);
  3427. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, 0);
  3428. layer.bqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd + 2*n_embd_gqa}, 0);
  3429. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  3430. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0);
  3431. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3432. layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd}, 0);
  3433. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  3434. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, 0);
  3435. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3436. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, 0);
  3437. }
  3438. } break;
  3439. case LLM_ARCH_ARCTIC:
  3440. {
  3441. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3442. // output
  3443. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3444. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  3445. // if output is NULL, init from the input tok embed
  3446. if (output == NULL) {
  3447. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3448. }
  3449. for (int i = 0; i < n_layer; ++i) {
  3450. auto & layer = layers[i];
  3451. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3452. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  3453. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  3454. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  3455. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  3456. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3457. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_embd}, 0);
  3458. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_embd, n_embd}, 0);
  3459. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_embd}, 0);
  3460. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  3461. layer.ffn_norm_exps = create_tensor(tn(LLM_TENSOR_FFN_NORM_EXPS, "weight", i), {n_embd}, 0);
  3462. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff, n_expert}, false);
  3463. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), { n_ff, n_embd, n_expert}, 0);
  3464. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0);
  3465. }
  3466. } break;
  3467. case LLM_ARCH_DEEPSEEK:
  3468. {
  3469. const int64_t n_ff_exp = hparams.n_ff_exp;
  3470. const int64_t n_expert_shared = hparams.n_expert_shared;
  3471. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3472. // output
  3473. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3474. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  3475. for (int i = 0; i < n_layer; ++i) {
  3476. auto & layer = layers[i];
  3477. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3478. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  3479. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  3480. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  3481. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  3482. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3483. if (i < (int) hparams.n_layer_dense_lead) {
  3484. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  3485. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3486. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3487. } else {
  3488. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  3489. if (n_expert == 0) {
  3490. throw std::runtime_error("n_expert must be > 0");
  3491. }
  3492. if (n_expert_used == 0) {
  3493. throw std::runtime_error("n_expert_used must be > 0");
  3494. }
  3495. // MoE branch
  3496. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);
  3497. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp, n_embd, n_expert}, 0);
  3498. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);
  3499. // Shared expert branch
  3500. layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), {n_embd, n_ff_exp * n_expert_shared}, 0);
  3501. layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), { n_ff_exp * n_expert_shared, n_embd}, 0);
  3502. layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), {n_embd, n_ff_exp * n_expert_shared}, 0);
  3503. }
  3504. }
  3505. } break;
  3506. case LLM_ARCH_DEEPSEEK2:
  3507. {
  3508. const bool is_lite = (hparams.n_layer == 27);
  3509. const bool is_mla = (hparams.n_embd_head_k_mla != 0 && hparams.n_embd_head_v_mla != 0);
  3510. // note: these are the actual head sizes you get when treating as MHA or after "decompression" using wv_b for MLA
  3511. const int64_t n_embd_head_k_mla = is_mla ? hparams.n_embd_head_k_mla : hparams.n_embd_head_k;
  3512. const int64_t n_embd_head_v_mla = is_mla ? hparams.n_embd_head_v_mla : hparams.n_embd_head_v;
  3513. const int64_t n_embd_head_qk_rope = hparams.n_rot;
  3514. const int64_t n_embd_head_qk_nope = n_embd_head_k_mla - n_embd_head_qk_rope;
  3515. const int64_t q_lora_rank = hparams.n_lora_q;
  3516. const int64_t kv_lora_rank = hparams.n_lora_kv;
  3517. const int64_t n_ff_exp = hparams.n_ff_exp;
  3518. const int64_t n_expert_shared = hparams.n_expert_shared;
  3519. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3520. // output
  3521. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3522. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  3523. for (int i = 0; i < n_layer; ++i) {
  3524. auto & layer = layers[i];
  3525. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3526. if (!is_lite) {
  3527. layer.attn_q_a_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_A_NORM, "weight", i), {q_lora_rank}, 0);
  3528. }
  3529. layer.attn_kv_a_norm = create_tensor(tn(LLM_TENSOR_ATTN_KV_A_NORM, "weight", i), {kv_lora_rank}, 0);
  3530. if (!is_lite) {
  3531. layer.wq_a = create_tensor(tn(LLM_TENSOR_ATTN_Q_A, "weight", i), {n_embd, q_lora_rank}, 0);
  3532. layer.wq_b = create_tensor(tn(LLM_TENSOR_ATTN_Q_B, "weight", i), {q_lora_rank, n_head * n_embd_head_k_mla}, 0);
  3533. } else {
  3534. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_head * n_embd_head_k_mla}, 0);
  3535. }
  3536. 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);
  3537. // note: only old legacy GGUF files will have the unsplit wkv_b tensor in
  3538. if (is_mla) {
  3539. layer.wk_b = create_tensor(tn(LLM_TENSOR_ATTN_K_B, "weight", i), {n_embd_head_qk_nope, kv_lora_rank, n_head}, 0);
  3540. layer.wv_b = create_tensor(tn(LLM_TENSOR_ATTN_V_B, "weight", i), {kv_lora_rank, n_embd_head_v_mla, n_head}, 0);
  3541. } else {
  3542. 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);
  3543. }
  3544. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_head * n_embd_head_v_mla, n_embd}, 0);
  3545. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3546. if (i < (int) hparams.n_layer_dense_lead) {
  3547. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  3548. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3549. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3550. } else {
  3551. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  3552. layer.ffn_exp_probs_b = create_tensor(tn(LLM_TENSOR_FFN_EXP_PROBS_B, "bias", i), {n_expert}, TENSOR_NOT_REQUIRED);
  3553. if (n_expert == 0) {
  3554. throw std::runtime_error("n_expert must be > 0");
  3555. }
  3556. if (n_expert_used == 0) {
  3557. throw std::runtime_error("n_expert_used must be > 0");
  3558. }
  3559. // MoE branch
  3560. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);
  3561. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp, n_embd, n_expert}, 0);
  3562. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);
  3563. // Shared expert branch
  3564. layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), {n_embd, n_ff_exp * n_expert_shared}, 0);
  3565. layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), { n_ff_exp * n_expert_shared, n_embd}, 0);
  3566. layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), {n_embd, n_ff_exp * n_expert_shared}, 0);
  3567. }
  3568. }
  3569. } break;
  3570. case LLM_ARCH_PLM:
  3571. {
  3572. const int64_t n_embd_head_qk_rope = hparams.n_rot;
  3573. const int64_t n_embd_head_qk_nope = hparams.n_embd_head_k - hparams.n_rot;
  3574. const int64_t kv_lora_rank = hparams.n_lora_kv;
  3575. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3576. // output
  3577. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3578. // output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  3579. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3580. for (int i = 0; i < n_layer; ++i) {
  3581. auto & layer = layers[i];
  3582. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3583. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  3584. 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);
  3585. layer.attn_kv_a_norm = create_tensor(tn(LLM_TENSOR_ATTN_KV_A_NORM, "weight", i), {kv_lora_rank}, 0);
  3586. 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);
  3587. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), { n_head * ( n_embd_head_v), n_embd}, 0);
  3588. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3589. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3590. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3591. }
  3592. } break;
  3593. case LLM_ARCH_BITNET:
  3594. {
  3595. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3596. // output
  3597. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3598. for (int i = 0; i < n_layer; ++i) {
  3599. auto & layer = layers[i];
  3600. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3601. layer.attn_sub_norm = create_tensor(tn(LLM_TENSOR_ATTN_SUB_NORM, "weight", i), {n_embd}, 0);
  3602. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  3603. layer.wq_scale = create_tensor(tn(LLM_TENSOR_ATTN_Q, "scale", i), {1}, TENSOR_NOT_REQUIRED);
  3604. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  3605. layer.wk_scale = create_tensor(tn(LLM_TENSOR_ATTN_K, "scale", i), {1}, TENSOR_NOT_REQUIRED);
  3606. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  3607. layer.wv_scale = create_tensor(tn(LLM_TENSOR_ATTN_V, "scale", i), {1}, TENSOR_NOT_REQUIRED);
  3608. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  3609. layer.wo_scale = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "scale", i), {1}, TENSOR_NOT_REQUIRED);
  3610. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3611. layer.ffn_sub_norm = create_tensor(tn(LLM_TENSOR_FFN_SUB_NORM, "weight", i), {n_ff}, 0);
  3612. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  3613. layer.ffn_gate_scale = create_tensor(tn(LLM_TENSOR_FFN_GATE, "scale", i), {1}, TENSOR_NOT_REQUIRED);
  3614. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  3615. layer.ffn_down_scale = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "scale", i), {1}, TENSOR_NOT_REQUIRED);
  3616. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3617. layer.ffn_up_scale = create_tensor(tn(LLM_TENSOR_FFN_UP, "scale", i), {1}, TENSOR_NOT_REQUIRED);
  3618. }
  3619. } break;
  3620. case LLM_ARCH_T5:
  3621. {
  3622. const auto n_rel_attn_bkts = hparams.n_rel_attn_bkts;
  3623. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3624. // output
  3625. output_norm_enc = create_tensor(tn(LLM_TENSOR_ENC_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3626. output_norm = create_tensor(tn(LLM_TENSOR_DEC_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3627. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  3628. // if output is NULL, init from the input tok embed
  3629. if (output == NULL) {
  3630. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3631. }
  3632. for (int i = 0; i < n_layer; ++i) {
  3633. auto & layer = layers[i];
  3634. layer.attn_norm_enc = create_tensor(tn(LLM_TENSOR_ENC_ATTN_NORM, "weight", i), {n_embd}, 0);
  3635. 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);
  3636. layer.wq_enc = create_tensor(tn(LLM_TENSOR_ENC_ATTN_Q, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  3637. layer.wk_enc = create_tensor(tn(LLM_TENSOR_ENC_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  3638. layer.wv_enc = create_tensor(tn(LLM_TENSOR_ENC_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  3639. layer.wo_enc = create_tensor(tn(LLM_TENSOR_ENC_ATTN_OUT, "weight", i), {n_embd_v_gqa, n_embd}, 0);
  3640. layer.ffn_norm_enc = create_tensor(tn(LLM_TENSOR_ENC_FFN_NORM, "weight", i), {n_embd}, 0);
  3641. layer.ffn_gate_enc = create_tensor(tn(LLM_TENSOR_ENC_FFN_GATE, "weight", i), {n_embd, n_ff}, TENSOR_NOT_REQUIRED);
  3642. layer.ffn_down_enc = create_tensor(tn(LLM_TENSOR_ENC_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3643. layer.ffn_up_enc = create_tensor(tn(LLM_TENSOR_ENC_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3644. layer.attn_norm = create_tensor(tn(LLM_TENSOR_DEC_ATTN_NORM, "weight", i), {n_embd}, 0);
  3645. layer.attn_rel_b = create_tensor(tn(LLM_TENSOR_DEC_ATTN_REL_B, "weight", i), {n_head, n_rel_attn_bkts}, TENSOR_NOT_REQUIRED);
  3646. layer.wq = create_tensor(tn(LLM_TENSOR_DEC_ATTN_Q, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  3647. layer.wk = create_tensor(tn(LLM_TENSOR_DEC_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  3648. layer.wv = create_tensor(tn(LLM_TENSOR_DEC_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  3649. layer.wo = create_tensor(tn(LLM_TENSOR_DEC_ATTN_OUT, "weight", i), {n_embd_v_gqa, n_embd}, 0);
  3650. layer.attn_norm_cross = create_tensor(tn(LLM_TENSOR_DEC_CROSS_ATTN_NORM, "weight", i), {n_embd}, 0);
  3651. // this tensor seems to be unused in HF transformers implementation
  3652. 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);
  3653. layer.wq_cross = create_tensor(tn(LLM_TENSOR_DEC_CROSS_ATTN_Q, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  3654. layer.wk_cross = create_tensor(tn(LLM_TENSOR_DEC_CROSS_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  3655. layer.wv_cross = create_tensor(tn(LLM_TENSOR_DEC_CROSS_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  3656. layer.wo_cross = create_tensor(tn(LLM_TENSOR_DEC_CROSS_ATTN_OUT, "weight", i), {n_embd_v_gqa, n_embd}, 0);
  3657. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_DEC_FFN_NORM, "weight", i), {n_embd}, 0);
  3658. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_DEC_FFN_GATE, "weight", i), {n_embd, n_ff}, TENSOR_NOT_REQUIRED);
  3659. layer.ffn_down = create_tensor(tn(LLM_TENSOR_DEC_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3660. layer.ffn_up = create_tensor(tn(LLM_TENSOR_DEC_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3661. }
  3662. } break;
  3663. case LLM_ARCH_T5ENCODER:
  3664. {
  3665. const auto n_rel_attn_bkts = hparams.n_rel_attn_bkts;
  3666. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3667. // output
  3668. output_norm_enc = create_tensor(tn(LLM_TENSOR_ENC_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3669. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  3670. // if output is NULL, init from the input tok embed
  3671. if (output == NULL) {
  3672. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3673. }
  3674. for (int i = 0; i < n_layer; ++i) {
  3675. auto & layer = layers[i];
  3676. layer.attn_norm_enc = create_tensor(tn(LLM_TENSOR_ENC_ATTN_NORM, "weight", i), {n_embd}, 0);
  3677. 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);
  3678. layer.wq_enc = create_tensor(tn(LLM_TENSOR_ENC_ATTN_Q, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  3679. layer.wk_enc = create_tensor(tn(LLM_TENSOR_ENC_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  3680. layer.wv_enc = create_tensor(tn(LLM_TENSOR_ENC_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  3681. layer.wo_enc = create_tensor(tn(LLM_TENSOR_ENC_ATTN_OUT, "weight", i), {n_embd_v_gqa, n_embd}, 0);
  3682. layer.ffn_norm_enc = create_tensor(tn(LLM_TENSOR_ENC_FFN_NORM, "weight", i), {n_embd}, 0);
  3683. layer.ffn_gate_enc = create_tensor(tn(LLM_TENSOR_ENC_FFN_GATE, "weight", i), {n_embd, n_ff}, TENSOR_NOT_REQUIRED);
  3684. layer.ffn_down_enc = create_tensor(tn(LLM_TENSOR_ENC_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3685. layer.ffn_up_enc = create_tensor(tn(LLM_TENSOR_ENC_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3686. }
  3687. } break;
  3688. case LLM_ARCH_JAIS:
  3689. {
  3690. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3691. // output
  3692. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3693. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
  3694. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  3695. for (int i = 0; i < n_layer; ++i) {
  3696. auto & layer = layers[i];
  3697. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3698. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0);
  3699. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, 0);
  3700. layer.bqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd + 2*n_embd_gqa}, 0);
  3701. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  3702. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0);
  3703. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3704. layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd}, 0);
  3705. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  3706. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, 0);
  3707. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  3708. layer.ffn_gate_b = create_tensor(tn(LLM_TENSOR_FFN_GATE, "bias", i), {n_ff}, 0);
  3709. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3710. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, 0);
  3711. }
  3712. } break;
  3713. case LLM_ARCH_CHATGLM:
  3714. {
  3715. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3716. // output
  3717. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3718. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  3719. // if output is NULL, init from the input tok embed
  3720. if (output == NULL) {
  3721. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3722. }
  3723. for (int i = 0; i < n_layer; ++i) {
  3724. auto & layer = layers[i];
  3725. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3726. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, TENSOR_NOT_REQUIRED);
  3727. layer.bqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd + 2*n_embd_gqa}, TENSOR_NOT_REQUIRED);
  3728. if (layer.wqkv == nullptr) {
  3729. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  3730. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  3731. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  3732. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  3733. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED);
  3734. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED);
  3735. }
  3736. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  3737. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3738. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff * 2}, 0);
  3739. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  3740. }
  3741. } break;
  3742. case LLM_ARCH_GLM4:
  3743. {
  3744. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3745. // output
  3746. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3747. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  3748. // if output is NULL, init from the input tok embed
  3749. if (output == NULL) {
  3750. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3751. }
  3752. for (int i = 0; i < n_layer; ++i) {
  3753. auto & layer = layers[i];
  3754. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3755. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, TENSOR_NOT_REQUIRED);
  3756. layer.bqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd + 2*n_embd_gqa}, TENSOR_NOT_REQUIRED);
  3757. if (layer.wqkv == nullptr) {
  3758. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  3759. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  3760. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  3761. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  3762. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED);
  3763. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED);
  3764. }
  3765. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  3766. layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), {n_embd}, 0);
  3767. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3768. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3769. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff * 2}, 0);
  3770. layer.ffn_post_norm = create_tensor(tn(LLM_TENSOR_FFN_POST_NORM, "weight", i), {n_embd}, 0);
  3771. }
  3772. } break;
  3773. case LLM_ARCH_GLM4_MOE:
  3774. {
  3775. const int64_t n_expert = hparams.n_expert;
  3776. const int64_t n_expert_used = hparams.n_expert_used;
  3777. const int64_t n_expert_shared = hparams.n_expert_shared;
  3778. GGML_ASSERT(hparams.n_expert > 0 && "n_expert must be > 0 for GLM4_MOE MoE layers");
  3779. GGML_ASSERT(hparams.n_expert_used > 0 && "n_expert_used must be > 0 for GLM4_MOE MoE layers");
  3780. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, 0);
  3781. // output
  3782. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), { n_embd }, 0);
  3783. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), { n_embd, n_vocab }, TENSOR_NOT_REQUIRED);
  3784. // if output is NULL, init from the input tok embed
  3785. if (output == NULL) {
  3786. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, TENSOR_DUPLICATED);
  3787. }
  3788. // Load ALL tensors including NextN layer to satisfy total tensor count
  3789. // but only PROCESS up to last layer (skipping final NextN layer) in forward pass
  3790. for (int i = 0; i < n_layer; ++i) {
  3791. int flags = 0;
  3792. if (hparams.nextn_predict_layers > 0 && static_cast<uint32_t>(i) >= n_layer - hparams.nextn_predict_layers) {
  3793. // skip all tensors in the NextN layers
  3794. flags |= TENSOR_SKIP;
  3795. }
  3796. auto & layer = layers[i];
  3797. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), { n_embd }, flags);
  3798. // GLM-style attention with bias terms
  3799. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), { n_embd, n_embd_head_k * n_head }, flags);
  3800. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), { n_embd, n_embd_k_gqa }, flags);
  3801. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), { n_embd, n_embd_v_gqa }, flags);
  3802. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), { n_embd_head_k * n_head }, flags);
  3803. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), { n_embd_k_gqa }, flags);
  3804. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), { n_embd_v_gqa }, flags);
  3805. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), { n_embd_head_k * n_head, n_embd }, flags);
  3806. // K/Q norm tensors (optional for GLM-4.5 355B variant)
  3807. layer.attn_q_norm = create_tensor(
  3808. tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), { n_embd_head_k }, TENSOR_NOT_REQUIRED | flags);
  3809. layer.attn_k_norm = create_tensor(
  3810. tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), { n_embd_head_k }, TENSOR_NOT_REQUIRED | flags);
  3811. layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), { n_embd }, flags);
  3812. // Check if this layer uses MoE or dense FFN based on n_layer_dense_lead
  3813. // GLM 4.5 uses hybrid architecture: layer 0 is dense, layers 1+ are MoE
  3814. const bool use_moe = (static_cast<uint32_t>(i) >= hparams.n_layer_dense_lead);
  3815. if (use_moe) {
  3816. // MoE layers
  3817. layer.ffn_gate_inp =
  3818. create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), { n_embd, n_expert }, flags);
  3819. layer.ffn_exp_probs_b = create_tensor(tn(LLM_TENSOR_FFN_EXP_PROBS_B, "bias", i), { n_expert }, flags);
  3820. // MoE branch
  3821. const int64_t n_ff_exp = hparams.n_ff_exp ? hparams.n_ff_exp : n_ff / n_expert_used;
  3822. layer.ffn_gate_exps = create_tensor(
  3823. tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert }, flags);
  3824. layer.ffn_down_exps = create_tensor(
  3825. tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), { n_ff_exp, n_embd, n_expert }, flags);
  3826. layer.ffn_up_exps = create_tensor(
  3827. tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert }, flags);
  3828. // Shared expert
  3829. if (n_expert_shared > 0) {
  3830. const int64_t n_ff_shexp = n_ff_exp * n_expert_shared;
  3831. layer.ffn_gate_shexp = create_tensor(
  3832. tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), { n_embd, n_ff_shexp }, flags);
  3833. layer.ffn_down_shexp = create_tensor(
  3834. tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), { n_ff_shexp, n_embd }, flags);
  3835. layer.ffn_up_shexp = create_tensor(
  3836. tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), { n_embd, n_ff_shexp }, flags);
  3837. }
  3838. } else {
  3839. // Dense layers (first k layers) - GLM uses separate gate/up projections
  3840. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), { n_embd, n_ff }, flags);
  3841. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd }, flags);
  3842. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), { n_embd, n_ff }, flags);
  3843. }
  3844. // NextN/MTP tensors (preserved but unused) - conditionally load for last nextn_predict_layers
  3845. if (hparams.nextn_predict_layers > 0 && static_cast<uint32_t>(i) >= n_layer - hparams.nextn_predict_layers) {
  3846. layer.nextn.eh_proj = create_tensor(tn(LLM_TENSOR_NEXTN_EH_PROJ, "weight", i), { 2 * n_embd, n_embd }, flags);
  3847. layer.nextn.embed_tokens = create_tensor(tn(LLM_TENSOR_NEXTN_EMBED_TOKENS, "weight", i), { n_embd, n_vocab }, flags);
  3848. layer.nextn.enorm = create_tensor(tn(LLM_TENSOR_NEXTN_ENORM, "weight", i), { n_embd }, flags);
  3849. layer.nextn.hnorm = create_tensor(tn(LLM_TENSOR_NEXTN_HNORM, "weight", i), { n_embd }, flags);
  3850. layer.nextn.shared_head_head = create_tensor(tn(LLM_TENSOR_NEXTN_SHARED_HEAD_HEAD, "weight", i), { n_embd, n_vocab }, flags);
  3851. layer.nextn.shared_head_norm = create_tensor(tn(LLM_TENSOR_NEXTN_SHARED_HEAD_NORM, "weight", i), { n_embd }, flags);
  3852. }
  3853. }
  3854. }
  3855. break;
  3856. case LLM_ARCH_NEMOTRON:
  3857. {
  3858. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3859. // output
  3860. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3861. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
  3862. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  3863. for (int i = 0; i < n_layer; ++i) {
  3864. auto & layer = layers[i];
  3865. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3866. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0);
  3867. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  3868. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  3869. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  3870. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  3871. // optional bias tensors
  3872. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  3873. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED);
  3874. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED);
  3875. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  3876. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3877. layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd}, 0);
  3878. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3879. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3880. // optional MLP bias
  3881. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  3882. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, TENSOR_NOT_REQUIRED);
  3883. }
  3884. } break;
  3885. case LLM_ARCH_EXAONE:
  3886. {
  3887. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3888. // output
  3889. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3890. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  3891. // if output is NULL, init from the input tok embed
  3892. if (output == NULL) {
  3893. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3894. }
  3895. for (int i = 0; i < n_layer; ++i) {
  3896. auto & layer = layers[i];
  3897. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3898. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  3899. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  3900. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  3901. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  3902. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3903. layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  3904. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  3905. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3906. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3907. }
  3908. } break;
  3909. case LLM_ARCH_EXAONE4:
  3910. {
  3911. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3912. // output
  3913. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3914. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  3915. // if output is NULL, init from the input tok embed
  3916. if (output == NULL) {
  3917. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3918. }
  3919. for (int i = 0; i < n_layer; ++i) {
  3920. auto & layer = layers[i];
  3921. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  3922. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  3923. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  3924. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  3925. layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  3926. layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), {n_embd}, 0);
  3927. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0);
  3928. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0);
  3929. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  3930. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3931. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3932. layer.ffn_post_norm = create_tensor(tn(LLM_TENSOR_FFN_POST_NORM, "weight", i), {n_embd}, 0);
  3933. }
  3934. } break;
  3935. case LLM_ARCH_RWKV6:
  3936. {
  3937. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3938. // Block 0, LN0
  3939. tok_norm = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "weight"), {n_embd}, 0);
  3940. tok_norm_b = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "bias"), {n_embd}, 0);
  3941. // output
  3942. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3943. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
  3944. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  3945. const int time_mix_extra_dim = hparams.time_mix_extra_dim;
  3946. const int time_decay_extra_dim = hparams.time_decay_extra_dim;
  3947. const int head_size = hparams.wkv_head_size;
  3948. const int attn_hidden_size = n_embd;
  3949. const int ffn_size = hparams.n_ff_arr[0];
  3950. for (int i = 0; i < n_layer; ++i) {
  3951. auto & layer = layers[i];
  3952. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3953. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0);
  3954. layer.attn_norm_2 = create_tensor(tn(LLM_TENSOR_ATTN_NORM_2, "weight", i), {n_embd}, 0);
  3955. layer.attn_norm_2_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM_2, "bias", i), {n_embd}, 0);
  3956. layer.time_mix_w1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_W1, "weight", i), {n_embd, time_mix_extra_dim * 5}, 0);
  3957. layer.time_mix_w2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_W2, "weight", i), {time_mix_extra_dim, n_embd, 5}, 0);
  3958. layer.time_mix_lerp_x = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_X, "weight", i), {n_embd, 1, 1}, 0);
  3959. layer.time_mix_lerp_w = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_W, "weight", i), {n_embd, 1, 1}, TENSOR_NOT_REQUIRED);
  3960. layer.time_mix_lerp_k = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_K, "weight", i), {n_embd, 1, 1}, TENSOR_NOT_REQUIRED);
  3961. layer.time_mix_lerp_v = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_V, "weight", i), {n_embd, 1, 1}, TENSOR_NOT_REQUIRED);
  3962. layer.time_mix_lerp_r = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_R, "weight", i), {n_embd, 1, 1}, TENSOR_NOT_REQUIRED);
  3963. layer.time_mix_lerp_g = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_G, "weight", i), {n_embd, 1, 1}, TENSOR_NOT_REQUIRED);
  3964. layer.time_mix_lerp_fused = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_FUSED, "weight", i), {n_embd, 1, 1, 5}, TENSOR_NOT_REQUIRED);
  3965. GGML_ASSERT(!(layer.time_mix_lerp_fused == NULL && layer.time_mix_lerp_w == NULL));
  3966. layer.time_mix_first = create_tensor(tn(LLM_TENSOR_TIME_MIX_FIRST, "weight", i), {head_size, n_embd / head_size}, 0);
  3967. layer.time_mix_decay = create_tensor(tn(LLM_TENSOR_TIME_MIX_DECAY, "weight", i), {n_embd}, 0);
  3968. layer.time_mix_decay_w1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_DECAY_W1, "weight", i), {n_embd, time_decay_extra_dim}, 0);
  3969. layer.time_mix_decay_w2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_DECAY_W2, "weight", i), {time_decay_extra_dim, attn_hidden_size}, 0);
  3970. layer.time_mix_key = create_tensor(tn(LLM_TENSOR_TIME_MIX_KEY, "weight", i), {attn_hidden_size, n_embd}, 0);
  3971. layer.time_mix_value = create_tensor(tn(LLM_TENSOR_TIME_MIX_VALUE, "weight", i), {attn_hidden_size, n_embd}, 0);
  3972. layer.time_mix_receptance = create_tensor(tn(LLM_TENSOR_TIME_MIX_RECEPTANCE, "weight", i), {attn_hidden_size, n_embd}, 0);
  3973. layer.time_mix_gate = create_tensor(tn(LLM_TENSOR_TIME_MIX_GATE, "weight", i), {attn_hidden_size, n_embd}, 0);
  3974. layer.time_mix_ln = create_tensor(tn(LLM_TENSOR_TIME_MIX_LN, "weight", i), {n_embd}, 0);
  3975. layer.time_mix_ln_b = create_tensor(tn(LLM_TENSOR_TIME_MIX_LN, "bias", i), {n_embd}, 0);
  3976. layer.time_mix_output = create_tensor(tn(LLM_TENSOR_TIME_MIX_OUTPUT, "weight", i), {n_embd, attn_hidden_size}, 0);
  3977. layer.channel_mix_lerp_k = create_tensor(tn(LLM_TENSOR_CHANNEL_MIX_LERP_K, "weight", i), {n_embd, 1, 1}, 0);
  3978. layer.channel_mix_lerp_r = create_tensor(tn(LLM_TENSOR_CHANNEL_MIX_LERP_R, "weight", i), {n_embd, 1, 1}, 0);
  3979. layer.channel_mix_key = create_tensor(tn(LLM_TENSOR_CHANNEL_MIX_KEY, "weight", i), {n_embd, ffn_size}, 0);
  3980. layer.channel_mix_value = create_tensor(tn(LLM_TENSOR_CHANNEL_MIX_VALUE, "weight", i), {ffn_size, n_embd}, 0);
  3981. layer.channel_mix_receptance = create_tensor(tn(LLM_TENSOR_CHANNEL_MIX_RECEPTANCE, "weight", i), {n_embd, n_embd}, 0);
  3982. }
  3983. } break;
  3984. case LLM_ARCH_RWKV6QWEN2:
  3985. {
  3986. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3987. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3988. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, TENSOR_NOT_REQUIRED);
  3989. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  3990. const int time_mix_extra_dim = hparams.time_mix_extra_dim;
  3991. const int time_decay_extra_dim = hparams.time_decay_extra_dim;
  3992. const int head_size = hparams.wkv_head_size;
  3993. const int attn_hidden_size = n_embd;
  3994. const int n_head_kv = hparams.n_head_kv();
  3995. int attn_key_value_size;
  3996. if (n_head_kv == 0 || attn_hidden_size / head_size == n_head_kv) {
  3997. attn_key_value_size = attn_hidden_size;
  3998. } else {
  3999. attn_key_value_size = n_head_kv * head_size;
  4000. }
  4001. for (int i = 0; i < n_layer; ++i) {
  4002. auto & layer = layers[i];
  4003. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  4004. layer.time_mix_w1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_W1, "weight", i), {n_embd, time_mix_extra_dim * 5}, 0);
  4005. layer.time_mix_w2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_W2, "weight", i), {time_mix_extra_dim, n_embd, 5}, 0);
  4006. layer.time_mix_lerp_x = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_X, "weight", i), {n_embd, 1, 1}, 0);
  4007. layer.time_mix_lerp_fused = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_FUSED, "weight", i), {n_embd, 1, 1, 5}, 0);
  4008. layer.time_mix_first = create_tensor(tn(LLM_TENSOR_TIME_MIX_FIRST, "weight", i), {head_size, n_embd / head_size}, TENSOR_NOT_REQUIRED);
  4009. layer.time_mix_decay = create_tensor(tn(LLM_TENSOR_TIME_MIX_DECAY, "weight", i), {n_embd}, 0);
  4010. layer.time_mix_decay_w1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_DECAY_W1, "weight", i), {n_embd, time_decay_extra_dim}, 0);
  4011. layer.time_mix_decay_w2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_DECAY_W2, "weight", i), {time_decay_extra_dim, attn_hidden_size}, 0);
  4012. layer.time_mix_key = create_tensor(tn(LLM_TENSOR_TIME_MIX_KEY, "weight", i), {n_embd, attn_key_value_size}, 0);
  4013. layer.time_mix_value = create_tensor(tn(LLM_TENSOR_TIME_MIX_VALUE, "weight", i), {n_embd, attn_key_value_size}, 0);
  4014. layer.time_mix_receptance = create_tensor(tn(LLM_TENSOR_TIME_MIX_RECEPTANCE, "weight", i), {attn_hidden_size, n_embd}, 0);
  4015. layer.time_mix_gate = create_tensor(tn(LLM_TENSOR_TIME_MIX_GATE, "weight", i), {attn_hidden_size, n_embd}, 0);
  4016. // optional bias tensors
  4017. layer.time_mix_key_b = create_tensor(tn(LLM_TENSOR_TIME_MIX_KEY, "bias", i), {attn_key_value_size}, TENSOR_NOT_REQUIRED);
  4018. layer.time_mix_value_b = create_tensor(tn(LLM_TENSOR_TIME_MIX_VALUE, "bias", i), {attn_key_value_size}, TENSOR_NOT_REQUIRED);
  4019. layer.time_mix_receptance_b = create_tensor(tn(LLM_TENSOR_TIME_MIX_RECEPTANCE, "bias", i), {attn_hidden_size}, TENSOR_NOT_REQUIRED);
  4020. layer.time_mix_output = create_tensor(tn(LLM_TENSOR_TIME_MIX_OUTPUT, "weight", i), {n_embd, attn_hidden_size}, 0);
  4021. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  4022. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  4023. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  4024. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  4025. }
  4026. } break;
  4027. case LLM_ARCH_RWKV7:
  4028. {
  4029. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  4030. // Block 0, LN0
  4031. tok_norm = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "weight"), {n_embd}, 0);
  4032. tok_norm_b = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "bias"), {n_embd}, 0);
  4033. // output
  4034. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  4035. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
  4036. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  4037. const int n_lora_decay = hparams.n_lora_decay;
  4038. const int n_lora_iclr = hparams.n_lora_iclr;
  4039. const int n_lora_value_res_mix = hparams.n_lora_value_res_mix;
  4040. const int n_lora_gate = hparams.n_lora_gate;
  4041. const int attn_hidden_size = n_embd;
  4042. const int ffn_size = hparams.n_ff_arr[0];
  4043. for (int i = 0; i < n_layer; ++i) {
  4044. auto & layer = layers[i];
  4045. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  4046. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0);
  4047. layer.attn_norm_2 = create_tensor(tn(LLM_TENSOR_ATTN_NORM_2, "weight", i), {n_embd}, 0);
  4048. layer.attn_norm_2_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM_2, "bias", i), {n_embd}, 0);
  4049. layer.time_mix_w0 = create_tensor(tn(LLM_TENSOR_TIME_MIX_W0, "weight", i), {n_embd}, 0);
  4050. layer.time_mix_w1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_W1, "weight", i), {n_embd, n_lora_decay}, 0);
  4051. layer.time_mix_w2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_W2, "weight", i), {n_lora_decay, n_embd}, 0);
  4052. layer.time_mix_a0 = create_tensor(tn(LLM_TENSOR_TIME_MIX_A0, "weight", i), {n_embd}, 0);
  4053. layer.time_mix_a1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_A1, "weight", i), {n_embd, n_lora_iclr}, 0);
  4054. layer.time_mix_a2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_A2, "weight", i), {n_lora_iclr, n_embd}, 0);
  4055. if (i == 0) {
  4056. // actually not used
  4057. layer.time_mix_v0 = create_tensor(tn(LLM_TENSOR_TIME_MIX_V0, "weight", i), {n_embd}, 0);
  4058. layer.time_mix_v1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_V1, "weight", i), {n_embd, n_lora_iclr}, 0);
  4059. layer.time_mix_v2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_V2, "weight", i), {n_lora_iclr, n_embd}, 0);
  4060. } else {
  4061. layer.time_mix_v0 = create_tensor(tn(LLM_TENSOR_TIME_MIX_V0, "weight", i), {n_embd}, 0);
  4062. layer.time_mix_v1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_V1, "weight", i), {n_embd, n_lora_value_res_mix}, 0);
  4063. layer.time_mix_v2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_V2, "weight", i), {n_lora_value_res_mix, n_embd}, 0);
  4064. }
  4065. layer.time_mix_g1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_G1, "weight", i), {n_embd, n_lora_gate}, 0);
  4066. layer.time_mix_g2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_G2, "weight", i), {n_lora_gate, n_embd}, 0);
  4067. layer.time_mix_lerp_fused = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_FUSED, "weight", i), {n_embd, 1, 1, 6}, 0);
  4068. layer.time_mix_k_k = create_tensor(tn(LLM_TENSOR_TIME_MIX_K_K, "weight", i), {attn_hidden_size}, 0);
  4069. layer.time_mix_k_a = create_tensor(tn(LLM_TENSOR_TIME_MIX_K_A, "weight", i), {attn_hidden_size}, 0);
  4070. layer.time_mix_r_k = create_tensor(tn(LLM_TENSOR_TIME_MIX_R_K, "weight", i), {attn_hidden_size}, 0);
  4071. layer.time_mix_key = create_tensor(tn(LLM_TENSOR_TIME_MIX_KEY, "weight", i), {attn_hidden_size, n_embd}, 0);
  4072. layer.time_mix_value = create_tensor(tn(LLM_TENSOR_TIME_MIX_VALUE, "weight", i), {attn_hidden_size, n_embd}, 0);
  4073. layer.time_mix_receptance = create_tensor(tn(LLM_TENSOR_TIME_MIX_RECEPTANCE, "weight", i), {attn_hidden_size, n_embd}, 0);
  4074. layer.time_mix_ln = create_tensor(tn(LLM_TENSOR_TIME_MIX_LN, "weight", i), {n_embd}, 0);
  4075. layer.time_mix_ln_b = create_tensor(tn(LLM_TENSOR_TIME_MIX_LN, "bias", i), {n_embd}, 0);
  4076. layer.time_mix_output = create_tensor(tn(LLM_TENSOR_TIME_MIX_OUTPUT, "weight", i), {n_embd, attn_hidden_size}, 0);
  4077. layer.channel_mix_lerp_k = create_tensor(tn(LLM_TENSOR_CHANNEL_MIX_LERP_K, "weight", i), {n_embd, 1, 1}, 0);
  4078. layer.channel_mix_key = create_tensor(tn(LLM_TENSOR_CHANNEL_MIX_KEY, "weight", i), {n_embd, ffn_size}, 0);
  4079. layer.channel_mix_value = create_tensor(tn(LLM_TENSOR_CHANNEL_MIX_VALUE, "weight", i), {ffn_size, n_embd}, 0);
  4080. }
  4081. } break;
  4082. case LLM_ARCH_ARWKV7:
  4083. {
  4084. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  4085. // output
  4086. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  4087. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  4088. const int n_lora_decay = hparams.n_lora_decay;
  4089. const int n_lora_iclr = hparams.n_lora_iclr;
  4090. const int n_lora_value_res_mix = hparams.n_lora_value_res_mix;
  4091. const int n_lora_gate = hparams.n_lora_gate;
  4092. const int attn_hidden_size = n_embd;
  4093. for (int i = 0; i < n_layer; ++i) {
  4094. auto & layer = layers[i];
  4095. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  4096. layer.time_mix_w0 = create_tensor(tn(LLM_TENSOR_TIME_MIX_W0, "weight", i), {n_embd}, 0);
  4097. layer.time_mix_w1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_W1, "weight", i), {n_embd, n_lora_decay}, 0);
  4098. layer.time_mix_w2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_W2, "weight", i), {n_lora_decay, n_embd}, 0);
  4099. layer.time_mix_a0 = create_tensor(tn(LLM_TENSOR_TIME_MIX_A0, "weight", i), {n_embd}, 0);
  4100. layer.time_mix_a1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_A1, "weight", i), {n_embd, n_lora_iclr}, 0);
  4101. layer.time_mix_a2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_A2, "weight", i), {n_lora_iclr, n_embd}, 0);
  4102. if (i == 0) {
  4103. // actually not used
  4104. layer.time_mix_v0 = create_tensor(tn(LLM_TENSOR_TIME_MIX_V0, "weight", i), {n_embd}, 0);
  4105. layer.time_mix_v1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_V1, "weight", i), {n_embd, n_lora_iclr}, 0);
  4106. layer.time_mix_v2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_V2, "weight", i), {n_lora_iclr, n_embd}, 0);
  4107. } else {
  4108. layer.time_mix_v0 = create_tensor(tn(LLM_TENSOR_TIME_MIX_V0, "weight", i), {n_embd}, 0);
  4109. layer.time_mix_v1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_V1, "weight", i), {n_embd, n_lora_value_res_mix}, 0);
  4110. layer.time_mix_v2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_V2, "weight", i), {n_lora_value_res_mix, n_embd}, 0);
  4111. }
  4112. layer.time_mix_g1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_G1, "weight", i), {n_embd, n_lora_gate}, TENSOR_NOT_REQUIRED);
  4113. layer.time_mix_g2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_G2, "weight", i), {n_lora_gate, n_embd}, TENSOR_NOT_REQUIRED);
  4114. try {
  4115. layer.time_mix_lerp_fused = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_FUSED, "weight", i), {n_embd, 1, 1, 6}, 0);
  4116. } catch(std::runtime_error & e) {
  4117. // ARWKV models may not have gate tensors
  4118. layer.time_mix_lerp_fused = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_FUSED, "weight", i), {n_embd, 1, 1, 5}, 0);
  4119. }
  4120. layer.time_mix_k_k = create_tensor(tn(LLM_TENSOR_TIME_MIX_K_K, "weight", i), {attn_hidden_size}, 0);
  4121. layer.time_mix_k_a = create_tensor(tn(LLM_TENSOR_TIME_MIX_K_A, "weight", i), {attn_hidden_size}, 0);
  4122. layer.time_mix_r_k = create_tensor(tn(LLM_TENSOR_TIME_MIX_R_K, "weight", i), {attn_hidden_size}, 0);
  4123. layer.time_mix_key = create_tensor(tn(LLM_TENSOR_TIME_MIX_KEY, "weight", i), {attn_hidden_size, n_embd}, 0);
  4124. layer.time_mix_value = create_tensor(tn(LLM_TENSOR_TIME_MIX_VALUE, "weight", i), {attn_hidden_size, n_embd}, 0);
  4125. layer.time_mix_receptance = create_tensor(tn(LLM_TENSOR_TIME_MIX_RECEPTANCE, "weight", i), {attn_hidden_size, n_embd}, 0);
  4126. layer.time_mix_ln = create_tensor(tn(LLM_TENSOR_TIME_MIX_LN, "weight", i), {n_embd}, TENSOR_NOT_REQUIRED);
  4127. layer.time_mix_ln_b = create_tensor(tn(LLM_TENSOR_TIME_MIX_LN, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  4128. layer.time_mix_output = create_tensor(tn(LLM_TENSOR_TIME_MIX_OUTPUT, "weight", i), {n_embd, attn_hidden_size}, 0);
  4129. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  4130. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  4131. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  4132. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  4133. }
  4134. } break;
  4135. case LLM_ARCH_CHAMELEON:
  4136. {
  4137. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  4138. // output
  4139. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  4140. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  4141. // if output is NULL, init from the input tok embed
  4142. if (output == NULL) {
  4143. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  4144. }
  4145. for (int i = 0; i < n_layer; ++i) {
  4146. auto & layer = layers[i];
  4147. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  4148. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k, n_head}, 0);
  4149. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k, n_head_kv}, 0);
  4150. layer.attn_q_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "bias", i), {n_embd_head_k, n_head}, TENSOR_NOT_REQUIRED);
  4151. 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);
  4152. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  4153. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  4154. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  4155. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  4156. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  4157. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  4158. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  4159. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  4160. }
  4161. } break;
  4162. case LLM_ARCH_WAVTOKENIZER_DEC:
  4163. {
  4164. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {hparams.n_embd_features, n_vocab}, 0);
  4165. conv1d = create_tensor(tn(LLM_TENSOR_CONV1D, "weight"), {7, hparams.n_embd_features, hparams.posnet.n_embd}, 0);
  4166. conv1d_b = create_tensor(tn(LLM_TENSOR_CONV1D, "bias"), {1, hparams.posnet.n_embd}, 0);
  4167. // posnet
  4168. {
  4169. const int64_t n_embd = hparams.posnet.n_embd;
  4170. for (uint32_t i = 0; i < hparams.posnet.n_layer; ++i) {
  4171. auto & layer = layers[i].posnet;
  4172. // posnet:
  4173. //
  4174. // - resnet
  4175. // - resnet
  4176. // - attn
  4177. // - resnet
  4178. // - resnet
  4179. // - norm
  4180. //
  4181. switch (i) {
  4182. case 0:
  4183. case 1:
  4184. case 3:
  4185. case 4:
  4186. {
  4187. layer.norm1 = create_tensor(tn(LLM_TENSOR_POS_NET_NORM1, "weight", i), {1, n_embd}, 0);
  4188. layer.norm1_b = create_tensor(tn(LLM_TENSOR_POS_NET_NORM1, "bias", i), {1, n_embd}, 0);
  4189. layer.conv1 = create_tensor(tn(LLM_TENSOR_POS_NET_CONV1, "weight", i), {3, n_embd, n_embd}, 0);
  4190. layer.conv1_b = create_tensor(tn(LLM_TENSOR_POS_NET_CONV1, "bias", i), {1, n_embd}, 0);
  4191. layer.norm2 = create_tensor(tn(LLM_TENSOR_POS_NET_NORM2, "weight", i), {1, n_embd}, 0);
  4192. layer.norm2_b = create_tensor(tn(LLM_TENSOR_POS_NET_NORM2, "bias", i), {1, n_embd}, 0);
  4193. layer.conv2 = create_tensor(tn(LLM_TENSOR_POS_NET_CONV2, "weight", i), {3, n_embd, n_embd}, 0);
  4194. layer.conv2_b = create_tensor(tn(LLM_TENSOR_POS_NET_CONV2, "bias", i), {1, n_embd}, 0);
  4195. } break;
  4196. case 2:
  4197. {
  4198. layer.attn_norm = create_tensor(tn(LLM_TENSOR_POS_NET_ATTN_NORM, "weight", i), {1, n_embd}, 0);
  4199. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_POS_NET_ATTN_NORM, "bias", i), {1, n_embd}, 0);
  4200. layer.attn_q = create_tensor(tn(LLM_TENSOR_POS_NET_ATTN_Q, "weight", i), {1, n_embd, n_embd}, 0);
  4201. layer.attn_q_b = create_tensor(tn(LLM_TENSOR_POS_NET_ATTN_Q, "bias", i), {1, n_embd}, 0);
  4202. layer.attn_k = create_tensor(tn(LLM_TENSOR_POS_NET_ATTN_K, "weight", i), {1, n_embd, n_embd}, 0);
  4203. layer.attn_k_b = create_tensor(tn(LLM_TENSOR_POS_NET_ATTN_K, "bias", i), {1, n_embd}, 0);
  4204. layer.attn_v = create_tensor(tn(LLM_TENSOR_POS_NET_ATTN_V, "weight", i), {1, n_embd, n_embd}, 0);
  4205. layer.attn_v_b = create_tensor(tn(LLM_TENSOR_POS_NET_ATTN_V, "bias", i), {1, n_embd}, 0);
  4206. layer.attn_o = create_tensor(tn(LLM_TENSOR_POS_NET_ATTN_OUT, "weight", i), {1, n_embd, n_embd}, 0);
  4207. layer.attn_o_b = create_tensor(tn(LLM_TENSOR_POS_NET_ATTN_OUT, "bias", i), {1, n_embd}, 0);
  4208. } break;
  4209. case 5:
  4210. {
  4211. layer.norm = create_tensor(tn(LLM_TENSOR_POS_NET_ATTN_NORM, "weight", i), {1, n_embd}, 0);
  4212. layer.norm_b = create_tensor(tn(LLM_TENSOR_POS_NET_ATTN_NORM, "bias", i), {1, n_embd}, 0);
  4213. } break;
  4214. default: GGML_ABORT("unknown posnet layer");
  4215. };
  4216. }
  4217. }
  4218. GGML_ASSERT(hparams.posnet.n_embd == hparams.convnext.n_embd);
  4219. tok_norm = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "weight"), {hparams.posnet.n_embd}, 0);
  4220. tok_norm_b = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "bias"), {hparams.posnet.n_embd}, 0);
  4221. // convnext
  4222. {
  4223. const int64_t n_embd = hparams.convnext.n_embd;
  4224. for (uint32_t i = 0; i < hparams.convnext.n_layer; ++i) {
  4225. auto & layer = layers[i].convnext;
  4226. layer.dw = create_tensor(tn(LLM_TENSOR_CONVNEXT_DW, "weight", i), {7, 1, n_embd}, 0);
  4227. layer.dw_b = create_tensor(tn(LLM_TENSOR_CONVNEXT_DW, "bias", i), {1, n_embd}, 0);
  4228. layer.norm = create_tensor(tn(LLM_TENSOR_CONVNEXT_NORM, "weight", i), {n_embd}, 0);
  4229. layer.norm_b = create_tensor(tn(LLM_TENSOR_CONVNEXT_NORM, "bias", i), {n_embd}, 0);
  4230. layer.pw1 = create_tensor(tn(LLM_TENSOR_CONVNEXT_PW1, "weight", i), {n_embd, n_ff}, 0);
  4231. layer.pw1_b = create_tensor(tn(LLM_TENSOR_CONVNEXT_PW1, "bias", i), {n_ff}, 0);
  4232. layer.pw2 = create_tensor(tn(LLM_TENSOR_CONVNEXT_PW2, "weight", i), {n_ff, n_embd}, 0);
  4233. layer.pw2_b = create_tensor(tn(LLM_TENSOR_CONVNEXT_PW2, "bias", i), {n_embd}, 0);
  4234. layer.gamma = create_tensor(tn(LLM_TENSOR_CONVNEXT_GAMMA, "weight", i), {n_embd}, 0);
  4235. }
  4236. // output
  4237. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  4238. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
  4239. }
  4240. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {hparams.convnext.n_embd, n_embd}, 0);
  4241. output_b = create_tensor(tn(LLM_TENSOR_OUTPUT, "bias"), {n_embd}, 0);
  4242. } break;
  4243. case LLM_ARCH_BAILINGMOE:
  4244. {
  4245. const int64_t n_ff_exp = hparams.n_ff_exp;
  4246. const int64_t n_expert_shared = hparams.n_expert_shared;
  4247. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  4248. // output
  4249. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  4250. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  4251. for (int i = 0; i < n_layer; ++i) {
  4252. auto & layer = layers[i];
  4253. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  4254. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_head * n_rot}, 0);
  4255. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_head_kv * n_rot}, 0);
  4256. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_head_kv * n_rot}, 0);
  4257. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_head * n_rot, n_embd}, 0);
  4258. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  4259. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  4260. if (n_expert == 0) {
  4261. throw std::runtime_error("n_expert must be > 0");
  4262. }
  4263. if (n_expert_used == 0) {
  4264. throw std::runtime_error("n_expert_used must be > 0");
  4265. }
  4266. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);
  4267. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp, n_embd, n_expert}, 0);
  4268. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);
  4269. layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), {n_embd, n_ff_exp * n_expert_shared}, 0);
  4270. layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), { n_ff_exp * n_expert_shared, n_embd}, 0);
  4271. layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), {n_embd, n_ff_exp * n_expert_shared}, 0);
  4272. }
  4273. } break;
  4274. case LLM_ARCH_DOTS1:
  4275. {
  4276. const int64_t n_ff_exp = hparams.n_ff_exp;
  4277. const int64_t n_expert_shared = hparams.n_expert_shared;
  4278. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  4279. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  4280. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  4281. for (int i = 0; i < n_layer; ++i) {
  4282. auto & layer = layers[i];
  4283. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  4284. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  4285. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  4286. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  4287. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  4288. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0);
  4289. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0);
  4290. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  4291. if (i < (int) hparams.n_layer_dense_lead) {
  4292. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  4293. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  4294. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  4295. } else {
  4296. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  4297. layer.ffn_exp_probs_b = create_tensor(tn(LLM_TENSOR_FFN_EXP_PROBS_B, "bias", i), {n_expert}, TENSOR_NOT_REQUIRED);
  4298. if (n_expert == 0) {
  4299. throw std::runtime_error("n_expert must be > 0");
  4300. }
  4301. if (n_expert_used == 0) {
  4302. throw std::runtime_error("n_expert_used must be > 0");
  4303. }
  4304. // MoE branch
  4305. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);
  4306. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp, n_embd, n_expert}, 0);
  4307. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);
  4308. // Shared expert branch
  4309. layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), {n_embd, n_ff_exp * n_expert_shared}, 0);
  4310. layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), { n_ff_exp * n_expert_shared, n_embd}, 0);
  4311. layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), {n_embd, n_ff_exp * n_expert_shared}, 0);
  4312. }
  4313. }
  4314. } break;
  4315. case LLM_ARCH_ARCEE:
  4316. {
  4317. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  4318. // output
  4319. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  4320. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  4321. // if output is NULL, init from the input tok embed
  4322. if (output == NULL) {
  4323. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  4324. }
  4325. for (int i = 0; i < n_layer; ++i) {
  4326. auto & layer = layers[i];
  4327. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  4328. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  4329. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  4330. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  4331. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  4332. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  4333. layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  4334. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  4335. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  4336. }
  4337. } break;
  4338. case LLM_ARCH_ERNIE4_5:
  4339. case LLM_ARCH_ERNIE4_5_MOE:
  4340. {
  4341. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  4342. // output
  4343. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  4344. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  4345. // if output is NULL, init from the input tok embed
  4346. if (output == NULL) {
  4347. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  4348. }
  4349. for (int i = 0; i < n_layer; ++i) {
  4350. auto & layer = layers[i];
  4351. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  4352. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  4353. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  4354. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  4355. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  4356. // optional bias tensors
  4357. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  4358. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED);
  4359. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED);
  4360. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  4361. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  4362. if (arch == LLM_ARCH_ERNIE4_5_MOE && static_cast<uint32_t>(i) >= hparams.n_layer_dense_lead) { // MoE layers
  4363. int n_ff_exp = hparams.n_ff_exp;
  4364. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  4365. layer.ffn_exp_probs_b = create_tensor(tn(LLM_TENSOR_FFN_EXP_PROBS_B, "bias", i), {n_expert}, TENSOR_NOT_REQUIRED);
  4366. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff_exp, n_expert}, TENSOR_NOT_REQUIRED);
  4367. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), { n_ff_exp, n_embd, n_expert}, 0);
  4368. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff_exp, n_expert}, 0);
  4369. // Shared expert (if present)
  4370. if (hparams.n_ff_shexp > 0) {
  4371. layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), { n_embd, hparams.n_ff_shexp}, 0);
  4372. layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), {hparams.n_ff_shexp, n_embd }, 0);
  4373. layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), { n_embd, hparams.n_ff_shexp}, 0);
  4374. }
  4375. } else { // Dense layers
  4376. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  4377. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  4378. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  4379. }
  4380. }
  4381. } break;
  4382. case LLM_ARCH_FALCON_H1:
  4383. {
  4384. // Common
  4385. const int64_t hidden_size = hparams.n_embd; // hidden_size
  4386. // mamba2 Mixer SSM params
  4387. const int64_t ssm_conv_kernel_size = hparams.ssm_d_conv; // ssm_conv_kernel_size
  4388. const int64_t ssm_n_groups = hparams.ssm_n_group; // ssm_n_groups
  4389. const int64_t ssm_state_size = hparams.ssm_d_state; // ssm_state_size
  4390. const int64_t ssm_intermediate_size = hparams.ssm_d_inner; // TODO expand
  4391. const int64_t ssm_num_heads = hparams.ssm_dt_rank; // ssm_num_heads
  4392. const int64_t ssm_conv_dim = ssm_intermediate_size + 2 * ssm_n_groups * ssm_state_size;
  4393. const int64_t ssm_projection_size = ssm_intermediate_size + ssm_conv_dim + ssm_num_heads;
  4394. // attn params
  4395. const int64_t attn_num_attention_head = hparams.n_head(0); // rename to: attn_num_attention_head
  4396. const int64_t attn_num_key_value_head = hparams.n_head_kv(0);
  4397. // ffn params
  4398. const int64_t ffn_intermediate_size = hparams.n_ff(0);
  4399. // embeddings
  4400. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {hidden_size, n_vocab}, 0);
  4401. // output
  4402. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {hidden_size, n_vocab}, TENSOR_NOT_REQUIRED);
  4403. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {hidden_size}, 0);
  4404. // if output is NULL, init from the input tok embed
  4405. if (output == NULL) {
  4406. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {hidden_size, n_vocab}, TENSOR_DUPLICATED);
  4407. }
  4408. for (int i = 0; i < n_layer; ++i) {
  4409. auto & layer = layers[i];
  4410. /*SSM LAYERS*/
  4411. // ssm in
  4412. layer.ssm_in = create_tensor(tn(LLM_TENSOR_SSM_IN, "weight", i), {hidden_size, ssm_projection_size}, 0);
  4413. // ssm 1d conv
  4414. layer.ssm_conv1d = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "weight", i), {ssm_conv_kernel_size, ssm_conv_dim}, 0);
  4415. layer.ssm_conv1d_b = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "bias", i), {ssm_conv_dim}, TENSOR_NOT_REQUIRED);
  4416. // ssm_dt
  4417. layer.ssm_dt_b = create_tensor(tn(LLM_TENSOR_SSM_DT, "bias", i), {ssm_num_heads}, 0);
  4418. // no "weight" suffix for these
  4419. layer.ssm_a = create_tensor(tn(LLM_TENSOR_SSM_A, i), {1, ssm_num_heads}, 0);
  4420. layer.ssm_d = create_tensor(tn(LLM_TENSOR_SSM_D, i), {1, ssm_num_heads}, 0);
  4421. // ssm_norm
  4422. layer.ssm_norm = create_tensor(tn(LLM_TENSOR_SSM_NORM, "weight", i), {ssm_intermediate_size / ssm_n_groups, ssm_n_groups}, TENSOR_NOT_REQUIRED);
  4423. // out_proj
  4424. layer.ssm_out = create_tensor(tn(LLM_TENSOR_SSM_OUT, "weight", i), {ssm_intermediate_size, hidden_size}, 0);
  4425. /*ATTENTION LAYERS*/
  4426. // attention layers (with optional bias)
  4427. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {hidden_size, n_embd_head_k * attn_num_attention_head}, 0);
  4428. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {hidden_size, attn_num_key_value_head * n_embd_head_k}, 0);
  4429. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {hidden_size, attn_num_key_value_head * n_embd_head_v}, 0);
  4430. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * attn_num_attention_head, hidden_size}, 0);
  4431. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {hidden_size}, TENSOR_NOT_REQUIRED);
  4432. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {attn_num_key_value_head * n_embd_head_k}, TENSOR_NOT_REQUIRED);
  4433. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {attn_num_key_value_head * n_embd_head_v}, TENSOR_NOT_REQUIRED);
  4434. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {hidden_size}, TENSOR_NOT_REQUIRED);
  4435. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {hidden_size}, 0);
  4436. // feed forward (w/ optional biases)
  4437. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, i), {hidden_size}, 0);
  4438. layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  4439. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {hidden_size, ffn_intermediate_size}, 0);
  4440. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { ffn_intermediate_size, hidden_size}, 0);
  4441. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {hidden_size, ffn_intermediate_size}, 0);
  4442. layer.ffn_gate_b = create_tensor(tn(LLM_TENSOR_FFN_GATE, "bias", i), {ffn_intermediate_size}, TENSOR_NOT_REQUIRED);
  4443. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {hidden_size}, TENSOR_NOT_REQUIRED);
  4444. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {ffn_intermediate_size}, TENSOR_NOT_REQUIRED);
  4445. }
  4446. } break;
  4447. case LLM_ARCH_HUNYUAN_MOE:
  4448. {
  4449. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  4450. // output
  4451. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  4452. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  4453. // if output is NULL, init from the input tok embed
  4454. if (output == NULL) {
  4455. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  4456. }
  4457. for (int i = 0; i < n_layer; ++i) {
  4458. auto & layer = layers[i];
  4459. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  4460. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  4461. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  4462. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  4463. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  4464. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0);
  4465. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0);
  4466. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  4467. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  4468. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0);
  4469. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), { n_ff, n_embd, n_expert}, 0);
  4470. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0);
  4471. layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), {n_embd, hparams.n_ff_shexp}, 0);
  4472. layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), {n_embd, hparams.n_ff_shexp}, 0);
  4473. layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), {hparams.n_ff_shexp, n_embd}, 0);
  4474. }
  4475. } break;
  4476. case LLM_ARCH_HUNYUAN_DENSE:
  4477. {
  4478. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  4479. // output
  4480. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  4481. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  4482. // if output is NULL, init from the input tok embed
  4483. if (output == NULL) {
  4484. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  4485. }
  4486. for (int i = 0; i < n_layer; ++i) {
  4487. auto & layer = layers[i];
  4488. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  4489. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  4490. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  4491. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  4492. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  4493. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0);
  4494. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0);
  4495. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  4496. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  4497. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  4498. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  4499. }
  4500. } break;
  4501. case LLM_ARCH_SMOLLM3:
  4502. {
  4503. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  4504. // output
  4505. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  4506. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  4507. // if output is NULL, init from the input tok embed
  4508. if (output == NULL) {
  4509. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  4510. }
  4511. for (int i = 0; i < n_layer; ++i) {
  4512. auto & layer = layers[i];
  4513. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  4514. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  4515. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  4516. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  4517. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  4518. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  4519. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  4520. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  4521. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  4522. }
  4523. } break;
  4524. case LLM_ARCH_OPENAI_MOE:
  4525. {
  4526. const int64_t n_ff_exp = hparams.n_ff_exp;
  4527. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  4528. // output
  4529. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  4530. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  4531. for (int i = 0; i < n_layer; ++i) {
  4532. auto & layer = layers[i];
  4533. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  4534. layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), {n_embd}, 0);
  4535. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_head * n_rot}, 0);
  4536. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_head_kv * n_rot}, 0);
  4537. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_head_kv * n_rot}, 0);
  4538. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_head * n_rot, n_embd}, 0);
  4539. layer.attn_sinks = create_tensor(tn(LLM_TENSOR_ATTN_SINKS, "weight", i), {n_head}, 0);
  4540. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), { n_embd, n_expert}, 0);
  4541. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);
  4542. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp, n_embd, n_expert}, 0);
  4543. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);
  4544. // bias
  4545. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_head * n_rot}, 0);
  4546. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_head_kv * n_rot}, 0);
  4547. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_head_kv * n_rot}, 0);
  4548. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0);
  4549. layer.ffn_gate_inp_b = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "bias", i), {n_expert}, 0);
  4550. layer.ffn_gate_exps_b = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "bias", i), {n_ff_exp, n_expert}, 0);
  4551. layer.ffn_down_exps_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "bias", i), { n_embd, n_expert}, 0);
  4552. layer.ffn_up_exps_b = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "bias", i), {n_ff_exp, n_expert}, 0);
  4553. }
  4554. } break;
  4555. case LLM_ARCH_LFM2:
  4556. {
  4557. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  4558. tok_norm = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "weight"), {n_embd}, 0);
  4559. for (int i = 0; i < n_layer; ++i) {
  4560. auto & layer = layers[i];
  4561. // ffn is same for transformer and conv layers
  4562. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  4563. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  4564. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  4565. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  4566. // for operator_norm
  4567. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  4568. if (!hparams.is_recurrent(i)) {
  4569. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0);
  4570. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0);
  4571. GGML_ASSERT(n_embd_v_gqa == n_embd_k_gqa);
  4572. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  4573. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, hparams.n_embd_k_gqa(i)}, 0);
  4574. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, hparams.n_embd_v_gqa(i)}, 0);
  4575. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  4576. } else {
  4577. layer.shortconv.conv = create_tensor(tn(LLM_TENSOR_SHORTCONV_CONV, "weight", i), {hparams.n_shortconv_l_cache, n_embd}, 0);
  4578. layer.shortconv.in_proj = create_tensor(tn(LLM_TENSOR_SHORTCONV_INPROJ, "weight", i), {n_embd, 3 * n_embd}, 0);
  4579. layer.shortconv.out_proj = create_tensor(tn(LLM_TENSOR_SHORTCONV_OUTPROJ, "weight", i), {n_embd, n_embd}, 0);
  4580. }
  4581. }
  4582. } break;
  4583. case LLM_ARCH_SMALLTHINKER:
  4584. {
  4585. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, 0);
  4586. // output
  4587. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), { n_embd }, 0);
  4588. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  4589. // if output is NULL, init from the input tok embed
  4590. if (output == NULL) {
  4591. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  4592. }
  4593. for (int i = 0; i < n_layer; ++i) {
  4594. auto & layer = layers[i];
  4595. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), { n_embd }, 0);
  4596. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), { n_embd, n_embd_head_k * n_head }, 0);
  4597. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), { n_embd, n_embd_gqa }, 0);
  4598. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), { n_embd, n_embd_gqa }, 0);
  4599. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), { n_embd_head_k * n_head, n_embd }, 0);
  4600. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), { n_embd }, 0);
  4601. GGML_ASSERT(n_expert > 0 && "n_expert must be > 0 for SMALLTHINKER");
  4602. GGML_ASSERT(n_expert_used > 0 && "n_expert_used must be > 0 for SMALLTHINKER");
  4603. // MoE branch
  4604. const int64_t n_ff_exp = hparams.n_ff_exp;
  4605. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), { n_embd, n_expert }, 0);
  4606. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert }, 0);
  4607. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), { n_ff_exp, n_embd, n_expert }, 0);
  4608. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert }, 0);
  4609. }
  4610. } break;
  4611. default:
  4612. throw std::runtime_error("unknown architecture");
  4613. }
  4614. if (n_moved_tensors > 0) {
  4615. LLAMA_LOG_DEBUG("%s: tensor '%s' (%s) (and %d others) cannot be used with preferred buffer type %s, using %s instead\n",
  4616. __func__, first_moved_tensor->name, ggml_type_name(first_moved_tensor->type), n_moved_tensors - 1,
  4617. ggml_backend_buft_name(first_moved_from_buft), ggml_backend_buft_name(first_moved_to_buft));
  4618. }
  4619. }
  4620. ml.done_getting_tensors();
  4621. ml.init_mappings(true, use_mlock ? &pimpl->mlock_mmaps : nullptr);
  4622. pimpl->mappings.reserve(ml.mappings.size());
  4623. // create the backend buffers
  4624. std::vector<std::pair<ggml_context *, llama_buf_map>> ctx_bufs;
  4625. ctx_bufs.reserve(ctx_map.size());
  4626. // Ensure we have enough capacity for the maximum backend buffer we will potentially create
  4627. const size_t n_max_backend_buffer = ctx_map.size() * ml.files.size();
  4628. pimpl->bufs.reserve(n_max_backend_buffer);
  4629. for (auto & it : ctx_map) {
  4630. ggml_backend_buffer_type_t buft = it.first;
  4631. ggml_context * ctx = it.second;
  4632. // skip contexts without tensors
  4633. if (ggml_get_first_tensor(ctx) == nullptr) {
  4634. continue;
  4635. }
  4636. llama_buf_map buf_map;
  4637. buf_map.reserve(n_max_backend_buffer);
  4638. // check if it is possible to use buffer_from_host_ptr with this buffer type
  4639. ggml_backend_dev_t dev = ggml_backend_buft_get_device(buft);
  4640. if (!dev) {
  4641. // FIXME: workaround for CPU backend buft having a NULL device
  4642. dev = ggml_backend_dev_by_type(GGML_BACKEND_DEVICE_TYPE_CPU);
  4643. if (!dev) {
  4644. throw std::runtime_error(format("%s: no CPU backend found", __func__));
  4645. }
  4646. }
  4647. ggml_backend_dev_props props;
  4648. ggml_backend_dev_get_props(dev, &props);
  4649. bool buffer_from_host_ptr_supported = props.caps.buffer_from_host_ptr;
  4650. bool is_default_buft = buft == ggml_backend_dev_buffer_type(dev);
  4651. if (ml.use_mmap && use_mmap_buffer && buffer_from_host_ptr_supported && is_default_buft) {
  4652. for (uint32_t idx = 0; idx < ml.files.size(); idx++) {
  4653. // only the mmap region containing the tensors in the model is mapped to the backend buffer
  4654. // 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
  4655. // this allows using partial offloading when the model size exceeds the metal buffer size, but not the RAM size
  4656. void * addr = nullptr;
  4657. size_t first, last; // NOLINT
  4658. ml.get_mapping_range(&first, &last, &addr, idx, ctx);
  4659. if (first >= last) {
  4660. continue;
  4661. }
  4662. const size_t max_size = ggml_get_max_tensor_size(ctx);
  4663. ggml_backend_buffer_t buf = ggml_backend_dev_buffer_from_host_ptr(dev, (char *) addr + first, last - first, max_size);
  4664. if (buf == nullptr) {
  4665. throw std::runtime_error(format("unable to allocate %s buffer", ggml_backend_buft_name(buft)));
  4666. }
  4667. pimpl->bufs.emplace_back(buf);
  4668. buf_map.emplace(idx, buf);
  4669. }
  4670. }
  4671. else {
  4672. ggml_backend_buffer_t buf = ggml_backend_alloc_ctx_tensors_from_buft(ctx, buft);
  4673. if (buf == nullptr) {
  4674. throw std::runtime_error(format("unable to allocate %s buffer", ggml_backend_buft_name(buft)));
  4675. }
  4676. pimpl->bufs.emplace_back(buf);
  4677. if (use_mlock && ggml_backend_buffer_is_host(buf)) {
  4678. pimpl->mlock_bufs.emplace_back(new llama_mlock);
  4679. auto & mlock_buf = pimpl->mlock_bufs.back();
  4680. mlock_buf->init (ggml_backend_buffer_get_base(buf));
  4681. mlock_buf->grow_to(ggml_backend_buffer_get_size(buf));
  4682. }
  4683. for (uint32_t idx = 0; idx < ml.files.size(); idx++) {
  4684. buf_map.emplace(idx, buf);
  4685. }
  4686. }
  4687. if (pimpl->bufs.empty()) {
  4688. throw std::runtime_error("failed to allocate buffer");
  4689. }
  4690. for (auto & buf : buf_map) {
  4691. // indicate that this buffer contains weights
  4692. // 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
  4693. ggml_backend_buffer_set_usage(buf.second, GGML_BACKEND_BUFFER_USAGE_WEIGHTS);
  4694. }
  4695. ctx_bufs.emplace_back(ctx, buf_map);
  4696. }
  4697. if (llama_supports_gpu_offload()) {
  4698. const int n_gpu = std::min(n_gpu_layers, int(hparams.n_layer));
  4699. LLAMA_LOG_INFO("%s: offloading %d repeating layers to GPU\n", __func__, n_gpu);
  4700. if (n_gpu_layers > (int) hparams.n_layer) {
  4701. LLAMA_LOG_INFO("%s: offloading output layer to GPU\n", __func__);
  4702. }
  4703. const int max_backend_supported_layers = hparams.n_layer + 1;
  4704. const int max_offloadable_layers = hparams.n_layer + 1;
  4705. LLAMA_LOG_INFO("%s: offloaded %d/%d layers to GPU\n", __func__, std::min(n_gpu_layers, max_offloadable_layers), max_backend_supported_layers);
  4706. }
  4707. // print memory requirements per buffer type
  4708. for (auto & buf : pimpl->bufs) {
  4709. LLAMA_LOG_INFO("%s: %12s model buffer size = %8.2f MiB\n", __func__, ggml_backend_buffer_name(buf.get()), ggml_backend_buffer_get_size(buf.get()) / 1024.0 / 1024.0);
  4710. }
  4711. // populate tensors_by_name
  4712. for (auto & ctx : pimpl->ctxs) {
  4713. for (auto * cur = ggml_get_first_tensor(ctx.get()); cur != NULL; cur = ggml_get_next_tensor(ctx.get(), cur)) {
  4714. tensors_by_name.emplace_back(ggml_get_name(cur), cur);
  4715. }
  4716. }
  4717. // load tensor data
  4718. for (auto & it : ctx_bufs) {
  4719. ggml_context * ctx = it.first;
  4720. auto & bufs = it.second;
  4721. if (!ml.load_all_data(ctx, bufs, use_mlock ? &pimpl->mlock_mmaps : NULL, params.progress_callback, params.progress_callback_user_data)) {
  4722. return false;
  4723. }
  4724. }
  4725. if (use_mmap_buffer) {
  4726. for (auto & mapping : ml.mappings) {
  4727. pimpl->mappings.emplace_back(std::move(mapping));
  4728. }
  4729. }
  4730. return true;
  4731. }
  4732. std::string llama_model::arch_name() const {
  4733. return llm_arch_name(arch);
  4734. }
  4735. std::string llama_model::type_name() const {
  4736. return llm_type_name(type);
  4737. }
  4738. std::string llama_model::desc() const {
  4739. return pimpl->desc_str;
  4740. }
  4741. size_t llama_model::size() const {
  4742. return pimpl->n_bytes;
  4743. }
  4744. size_t llama_model::n_tensors() const {
  4745. return tensors_by_name.size();
  4746. }
  4747. size_t llama_model::n_devices() const {
  4748. return devices.size();
  4749. }
  4750. uint64_t llama_model::n_elements() const {
  4751. return pimpl->n_elements;
  4752. }
  4753. void llama_model::print_info() const {
  4754. const std::string rope_scaling_type = llama_rope_scaling_type_name(hparams.rope_scaling_type_train);
  4755. auto print_f = [](const std::function<uint32_t(uint32_t)> & f, uint32_t n) {
  4756. bool is_var = false;
  4757. std::vector<uint32_t> v;
  4758. for (uint32_t i = 0; i < n; ++i) {
  4759. v.push_back(f(i));
  4760. if (v[i] != v[0]) {
  4761. is_var = true;
  4762. }
  4763. }
  4764. std::stringstream ss;
  4765. if (is_var) {
  4766. ss << "[";
  4767. for (uint32_t i = 0; i < n; ++i) {
  4768. ss << v[i];
  4769. if (i < n - 1) {
  4770. ss << ", ";
  4771. }
  4772. }
  4773. ss << "]";
  4774. } else {
  4775. ss << v[0];
  4776. }
  4777. return ss.str();
  4778. };
  4779. // hparams
  4780. LLAMA_LOG_INFO("%s: arch = %s\n", __func__, arch_name().c_str());
  4781. LLAMA_LOG_INFO("%s: vocab_only = %d\n", __func__, hparams.vocab_only);
  4782. if (!hparams.vocab_only) {
  4783. LLAMA_LOG_INFO("%s: n_ctx_train = %u\n", __func__, hparams.n_ctx_train);
  4784. LLAMA_LOG_INFO("%s: n_embd = %u\n", __func__, hparams.n_embd);
  4785. LLAMA_LOG_INFO("%s: n_layer = %u\n", __func__, hparams.n_layer);
  4786. LLAMA_LOG_INFO("%s: n_head = %s\n", __func__, print_f([&](uint32_t il) { return hparams.n_head(il); }, hparams.n_layer).c_str());
  4787. 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());
  4788. LLAMA_LOG_INFO("%s: n_rot = %u\n", __func__, hparams.n_rot);
  4789. LLAMA_LOG_INFO("%s: n_swa = %u\n", __func__, hparams.n_swa);
  4790. LLAMA_LOG_INFO("%s: is_swa_any = %u\n", __func__, hparams.is_swa_any());
  4791. LLAMA_LOG_INFO("%s: n_embd_head_k = %u\n", __func__, hparams.n_embd_head_k);
  4792. LLAMA_LOG_INFO("%s: n_embd_head_v = %u\n", __func__, hparams.n_embd_head_v);
  4793. LLAMA_LOG_INFO("%s: n_gqa = %s\n", __func__, print_f([&](uint32_t il) { return hparams.n_gqa(il); }, hparams.n_layer).c_str());
  4794. 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());
  4795. 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());
  4796. LLAMA_LOG_INFO("%s: f_norm_eps = %.1e\n", __func__, hparams.f_norm_eps);
  4797. LLAMA_LOG_INFO("%s: f_norm_rms_eps = %.1e\n", __func__, hparams.f_norm_rms_eps);
  4798. LLAMA_LOG_INFO("%s: f_clamp_kqv = %.1e\n", __func__, hparams.f_clamp_kqv);
  4799. LLAMA_LOG_INFO("%s: f_max_alibi_bias = %.1e\n", __func__, hparams.f_max_alibi_bias);
  4800. LLAMA_LOG_INFO("%s: f_logit_scale = %.1e\n", __func__, hparams.f_logit_scale);
  4801. LLAMA_LOG_INFO("%s: f_attn_scale = %.1e\n", __func__, hparams.f_attention_scale);
  4802. LLAMA_LOG_INFO("%s: n_ff = %s\n", __func__, print_f([&](uint32_t il) { return hparams.n_ff(il); }, hparams.n_layer).c_str());
  4803. LLAMA_LOG_INFO("%s: n_expert = %u\n", __func__, hparams.n_expert);
  4804. LLAMA_LOG_INFO("%s: n_expert_used = %u\n", __func__, hparams.n_expert_used);
  4805. LLAMA_LOG_INFO("%s: causal attn = %d\n", __func__, hparams.causal_attn);
  4806. LLAMA_LOG_INFO("%s: pooling type = %d\n", __func__, hparams.pooling_type);
  4807. LLAMA_LOG_INFO("%s: rope type = %d\n", __func__, hparams.rope_type);
  4808. LLAMA_LOG_INFO("%s: rope scaling = %s\n", __func__, rope_scaling_type.c_str());
  4809. LLAMA_LOG_INFO("%s: freq_base_train = %.1f\n", __func__, hparams.rope_freq_base_train);
  4810. LLAMA_LOG_INFO("%s: freq_scale_train = %g\n", __func__, hparams.rope_freq_scale_train);
  4811. LLAMA_LOG_INFO("%s: n_ctx_orig_yarn = %u\n", __func__, hparams.n_ctx_orig_yarn);
  4812. LLAMA_LOG_INFO("%s: rope_finetuned = %s\n", __func__, hparams.rope_finetuned ? "yes" : "unknown");
  4813. if (!classifier_labels.empty()) {
  4814. LLAMA_LOG_INFO("%s: n_cls_out = %u\n", __func__, hparams.n_cls_out);
  4815. size_t i = 0;
  4816. for (auto label : classifier_labels) {
  4817. LLAMA_LOG_INFO("%s: cls_label[%2zu] = %s\n", __func__, i++, label.c_str());
  4818. }
  4819. }
  4820. }
  4821. if (arch == LLM_ARCH_MAMBA ||
  4822. arch == LLM_ARCH_MAMBA2 ||
  4823. arch == LLM_ARCH_JAMBA ||
  4824. arch == LLM_ARCH_FALCON_H1 ||
  4825. arch == LLM_ARCH_PLAMO2 ||
  4826. arch == LLM_ARCH_GRANITE_HYBRID) {
  4827. LLAMA_LOG_INFO("%s: ssm_d_conv = %u\n", __func__, hparams.ssm_d_conv);
  4828. LLAMA_LOG_INFO("%s: ssm_d_inner = %u\n", __func__, hparams.ssm_d_inner);
  4829. LLAMA_LOG_INFO("%s: ssm_d_state = %u\n", __func__, hparams.ssm_d_state);
  4830. LLAMA_LOG_INFO("%s: ssm_dt_rank = %u\n", __func__, hparams.ssm_dt_rank);
  4831. LLAMA_LOG_INFO("%s: ssm_n_group = %u\n", __func__, hparams.ssm_n_group);
  4832. LLAMA_LOG_INFO("%s: ssm_dt_b_c_rms = %d\n", __func__, hparams.ssm_dt_b_c_rms);
  4833. }
  4834. LLAMA_LOG_INFO("%s: model type = %s\n", __func__, type_name().c_str());
  4835. if (pimpl->n_elements >= 1e12) {
  4836. LLAMA_LOG_INFO("%s: model params = %.2f T\n", __func__, pimpl->n_elements*1e-12);
  4837. } else if (pimpl->n_elements >= 1e9) {
  4838. LLAMA_LOG_INFO("%s: model params = %.2f B\n", __func__, pimpl->n_elements*1e-9);
  4839. } else if (pimpl->n_elements >= 1e6) {
  4840. LLAMA_LOG_INFO("%s: model params = %.2f M\n", __func__, pimpl->n_elements*1e-6);
  4841. } else {
  4842. LLAMA_LOG_INFO("%s: model params = %.2f K\n", __func__, pimpl->n_elements*1e-3);
  4843. }
  4844. // general kv
  4845. LLAMA_LOG_INFO("%s: general.name = %s\n", __func__, name.c_str());
  4846. if (arch == LLM_ARCH_DEEPSEEK) {
  4847. LLAMA_LOG_INFO("%s: n_layer_dense_lead = %d\n", __func__, hparams.n_layer_dense_lead);
  4848. LLAMA_LOG_INFO("%s: n_ff_exp = %d\n", __func__, hparams.n_ff_exp);
  4849. LLAMA_LOG_INFO("%s: n_expert_shared = %d\n", __func__, hparams.n_expert_shared);
  4850. LLAMA_LOG_INFO("%s: expert_weights_scale = %.1f\n", __func__, hparams.expert_weights_scale);
  4851. }
  4852. if (arch == LLM_ARCH_DEEPSEEK2) {
  4853. LLAMA_LOG_INFO("%s: n_layer_dense_lead = %d\n", __func__, hparams.n_layer_dense_lead);
  4854. LLAMA_LOG_INFO("%s: n_lora_q = %d\n", __func__, hparams.n_lora_q);
  4855. LLAMA_LOG_INFO("%s: n_lora_kv = %d\n", __func__, hparams.n_lora_kv);
  4856. LLAMA_LOG_INFO("%s: n_embd_head_k_mla = %d\n", __func__, hparams.n_embd_head_k_mla);
  4857. LLAMA_LOG_INFO("%s: n_embd_head_v_mla = %d\n", __func__, hparams.n_embd_head_v_mla);
  4858. LLAMA_LOG_INFO("%s: n_ff_exp = %d\n", __func__, hparams.n_ff_exp);
  4859. LLAMA_LOG_INFO("%s: n_expert_shared = %d\n", __func__, hparams.n_expert_shared);
  4860. LLAMA_LOG_INFO("%s: expert_weights_scale = %.1f\n", __func__, hparams.expert_weights_scale);
  4861. LLAMA_LOG_INFO("%s: expert_weights_norm = %d\n", __func__, hparams.expert_weights_norm);
  4862. LLAMA_LOG_INFO("%s: expert_gating_func = %s\n", __func__, llama_expert_gating_func_name((llama_expert_gating_func_type) hparams.expert_gating_func));
  4863. LLAMA_LOG_INFO("%s: rope_yarn_log_mul = %.4f\n", __func__, hparams.rope_yarn_log_mul);
  4864. }
  4865. if (arch == LLM_ARCH_QWEN2MOE) {
  4866. LLAMA_LOG_INFO("%s: n_ff_exp = %d\n", __func__, hparams.n_ff_exp);
  4867. LLAMA_LOG_INFO("%s: n_ff_shexp = %d\n", __func__, hparams.n_ff_shexp);
  4868. }
  4869. if (arch == LLM_ARCH_QWEN3MOE || arch == LLM_ARCH_OPENAI_MOE) {
  4870. LLAMA_LOG_INFO("%s: n_ff_exp = %d\n", __func__, hparams.n_ff_exp);
  4871. }
  4872. if (arch == LLM_ARCH_MINICPM ||
  4873. arch == LLM_ARCH_GRANITE ||
  4874. arch == LLM_ARCH_GRANITE_MOE ||
  4875. arch == LLM_ARCH_GRANITE_HYBRID) {
  4876. LLAMA_LOG_INFO("%s: f_embedding_scale = %f\n", __func__, hparams.f_embedding_scale);
  4877. LLAMA_LOG_INFO("%s: f_residual_scale = %f\n", __func__, hparams.f_residual_scale);
  4878. LLAMA_LOG_INFO("%s: f_attention_scale = %f\n", __func__, hparams.f_attention_scale);
  4879. LLAMA_LOG_INFO("%s: n_ff_shexp = %d\n", __func__, hparams.n_ff_shexp);
  4880. }
  4881. if (arch == LLM_ARCH_BAILINGMOE) {
  4882. LLAMA_LOG_INFO("%s: n_layer_dense_lead = %d\n", __func__, hparams.n_layer_dense_lead);
  4883. LLAMA_LOG_INFO("%s: n_ff_exp = %d\n", __func__, hparams.n_ff_exp);
  4884. LLAMA_LOG_INFO("%s: n_expert_shared = %d\n", __func__, hparams.n_expert_shared);
  4885. LLAMA_LOG_INFO("%s: expert_weights_scale = %.1f\n", __func__, hparams.expert_weights_scale);
  4886. LLAMA_LOG_INFO("%s: expert_weights_norm = %d\n", __func__, hparams.expert_weights_norm);
  4887. }
  4888. if (arch == LLM_ARCH_SMALLTHINKER) {
  4889. LLAMA_LOG_INFO("%s: n_ff_exp = %d\n", __func__, hparams.n_ff_exp);
  4890. LLAMA_LOG_INFO("%s: expert_gating_func = %s\n", __func__, llama_expert_gating_func_name((llama_expert_gating_func_type) hparams.expert_gating_func));
  4891. }
  4892. vocab.print_info();
  4893. }
  4894. ggml_backend_dev_t llama_model::dev_layer(int il) const {
  4895. return pimpl->dev_layer.at(il).dev;
  4896. }
  4897. ggml_backend_dev_t llama_model::dev_output() const {
  4898. return pimpl->dev_output.dev;
  4899. }
  4900. template<typename F>
  4901. static bool buft_supported(ggml_backend_buffer_type_t buft, ggml_backend_dev_t dev, F & fn) {
  4902. ggml_init_params params = {
  4903. /*.mem_size =*/ ggml_tensor_overhead()*8,
  4904. /*.mem_buffer =*/ NULL,
  4905. /*.no_alloc =*/ true,
  4906. };
  4907. ggml_context_ptr ctx { ggml_init(params) };
  4908. if (!ctx) {
  4909. throw std::runtime_error(format("failed to create ggml context"));
  4910. }
  4911. ggml_backend_buffer_ptr buf { ggml_backend_buft_alloc_buffer(buft, 0) };
  4912. ggml_tensor * op_tensor = fn(ctx.get());
  4913. for (int i = 0; i < GGML_MAX_SRC; i++) {
  4914. if (op_tensor->src[i] != nullptr) {
  4915. assert(op_tensor->src[i]->buffer == nullptr);
  4916. op_tensor->src[i]->buffer = buf.get();
  4917. }
  4918. }
  4919. bool op_supported = ggml_backend_dev_supports_op(dev, op_tensor);
  4920. return op_supported;
  4921. }
  4922. template<typename F>
  4923. static ggml_backend_buffer_type_t select_buft(const buft_list_t & buft_list, const F & fn) {
  4924. for (const auto & cur : buft_list) {
  4925. ggml_backend_dev_t cur_dev = cur.first;
  4926. ggml_backend_buffer_type_t cur_buft = cur.second;
  4927. if (buft_supported(cur_buft, cur_dev, fn)) {
  4928. return cur_buft;
  4929. }
  4930. }
  4931. throw std::runtime_error(format("no suitable buffer type found"));
  4932. }
  4933. ggml_backend_buffer_type_t llama_model::select_buft(int il) const {
  4934. return ::select_buft(
  4935. *pimpl->dev_layer.at(il).buft_list,
  4936. [&](ggml_context * ctx) {
  4937. ggml_tensor * cur = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, hparams.n_embd);
  4938. ggml_tensor * layer_dir = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, hparams.n_embd);
  4939. return ggml_add(ctx, cur, layer_dir);
  4940. });
  4941. }
  4942. bool llama_model::has_tensor_overrides() const {
  4943. return pimpl->has_tensor_overrides;
  4944. }
  4945. const ggml_tensor * llama_model::get_tensor(const char * name) const {
  4946. auto it = std::find_if(tensors_by_name.begin(), tensors_by_name.end(),
  4947. [name](const std::pair<std::string, ggml_tensor *> & it) {
  4948. return it.first == name;
  4949. });
  4950. if (it == tensors_by_name.end()) {
  4951. return nullptr;
  4952. }
  4953. return it->second;
  4954. }
  4955. float llama_model::get_rope_freq_base (const llama_cparams & cparams, int il) const {
  4956. return hparams.is_swa(il) ? hparams.rope_freq_base_train_swa : cparams.rope_freq_base;
  4957. }
  4958. float llama_model::get_rope_freq_scale(const llama_cparams & cparams, int il) const {
  4959. return hparams.is_swa(il) ? hparams.rope_freq_scale_train_swa : cparams.rope_freq_scale;
  4960. }
  4961. ggml_tensor * llama_model::get_rope_factors(const llama_cparams & cparams, int il) const {
  4962. const uint32_t n_ctx_per_seq = cparams.n_ctx / cparams.n_seq_max;
  4963. // choose long/short freq factors based on the context size
  4964. if (layers[il].rope_freqs != nullptr) {
  4965. return layers[il].rope_freqs;
  4966. }
  4967. if (n_ctx_per_seq > hparams.n_ctx_orig_yarn) {
  4968. return layers[il].rope_long;
  4969. }
  4970. return layers[il].rope_short;
  4971. }
  4972. struct llm_build_llama : public llm_graph_context {
  4973. llm_build_llama(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  4974. const int64_t n_embd_head = hparams.n_embd_head_v;
  4975. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  4976. GGML_ASSERT(n_embd_head == hparams.n_rot);
  4977. ggml_tensor * cur;
  4978. ggml_tensor * inpL;
  4979. inpL = build_inp_embd(model.tok_embd);
  4980. // inp_pos - contains the positions
  4981. ggml_tensor * inp_pos = build_inp_pos();
  4982. auto * inp_attn = build_attn_inp_kv_unified();
  4983. const float kq_scale = hparams.f_attention_scale == 0.0f ? 1.0f/sqrtf(float(n_embd_head)) : hparams.f_attention_scale;
  4984. ggml_tensor * inp_out_ids = build_inp_out_ids();
  4985. for (int il = 0; il < n_layer; ++il) {
  4986. ggml_tensor * inpSA = inpL;
  4987. // norm
  4988. cur = build_norm(inpL,
  4989. model.layers[il].attn_norm, NULL,
  4990. LLM_NORM_RMS, il);
  4991. cb(cur, "attn_norm", il);
  4992. // self-attention
  4993. {
  4994. // rope freq factors for llama3; may return nullptr for llama2 and other models
  4995. ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
  4996. // compute Q and K and RoPE them
  4997. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  4998. cb(Qcur, "Qcur", il);
  4999. if (model.layers[il].bq) {
  5000. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  5001. cb(Qcur, "Qcur", il);
  5002. }
  5003. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  5004. cb(Kcur, "Kcur", il);
  5005. if (model.layers[il].bk) {
  5006. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  5007. cb(Kcur, "Kcur", il);
  5008. }
  5009. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  5010. cb(Vcur, "Vcur", il);
  5011. if (model.layers[il].bv) {
  5012. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  5013. cb(Vcur, "Vcur", il);
  5014. }
  5015. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  5016. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  5017. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  5018. Qcur = ggml_rope_ext(
  5019. ctx0, Qcur, inp_pos, rope_factors,
  5020. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  5021. ext_factor, attn_factor, beta_fast, beta_slow
  5022. );
  5023. Kcur = ggml_rope_ext(
  5024. ctx0, Kcur, inp_pos, rope_factors,
  5025. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  5026. ext_factor, attn_factor, beta_fast, beta_slow
  5027. );
  5028. cb(Qcur, "Qcur", il);
  5029. cb(Kcur, "Kcur", il);
  5030. cb(Vcur, "Vcur", il);
  5031. cur = build_attn(inp_attn,
  5032. model.layers[il].wo, model.layers[il].bo,
  5033. Qcur, Kcur, Vcur, nullptr, nullptr, kq_scale, il);
  5034. cb(cur, "attn_out", il);
  5035. }
  5036. if (il == n_layer - 1 && inp_out_ids) {
  5037. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  5038. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  5039. }
  5040. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  5041. cb(ffn_inp, "ffn_inp", il);
  5042. // feed-forward network (non-MoE)
  5043. if (model.layers[il].ffn_gate_inp == nullptr) {
  5044. cur = build_norm(ffn_inp,
  5045. model.layers[il].ffn_norm, NULL,
  5046. LLM_NORM_RMS, il);
  5047. cb(cur, "ffn_norm", il);
  5048. cur = build_ffn(cur,
  5049. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  5050. model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, NULL,
  5051. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  5052. NULL,
  5053. LLM_FFN_SILU, LLM_FFN_PAR, il);
  5054. cb(cur, "ffn_out", il);
  5055. } else {
  5056. // MoE branch
  5057. cur = build_norm(ffn_inp,
  5058. model.layers[il].ffn_norm, NULL,
  5059. LLM_NORM_RMS, il);
  5060. cb(cur, "ffn_norm", il);
  5061. cur = build_moe_ffn(cur,
  5062. model.layers[il].ffn_gate_inp,
  5063. model.layers[il].ffn_up_exps,
  5064. model.layers[il].ffn_gate_exps,
  5065. model.layers[il].ffn_down_exps,
  5066. nullptr,
  5067. n_expert, n_expert_used,
  5068. LLM_FFN_SILU, true,
  5069. false, 0.0,
  5070. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  5071. il);
  5072. cb(cur, "ffn_moe_out", il);
  5073. }
  5074. cur = ggml_add(ctx0, cur, ffn_inp);
  5075. cb(cur, "ffn_out", il);
  5076. cur = build_cvec(cur, il);
  5077. cb(cur, "l_out", il);
  5078. // input for next layer
  5079. inpL = cur;
  5080. }
  5081. cur = inpL;
  5082. cur = build_norm(cur,
  5083. model.output_norm, NULL,
  5084. LLM_NORM_RMS, -1);
  5085. cb(cur, "result_norm", -1);
  5086. res->t_embd = cur;
  5087. // lm_head
  5088. cur = build_lora_mm(model.output, cur);
  5089. cb(cur, "result_output", -1);
  5090. res->t_logits = cur;
  5091. ggml_build_forward_expand(gf, cur);
  5092. }
  5093. };
  5094. struct llm_build_llama_iswa : public llm_graph_context {
  5095. llm_build_llama_iswa(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  5096. const int64_t n_embd_head = hparams.n_embd_head_v;
  5097. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  5098. GGML_ASSERT(n_embd_head == hparams.n_rot);
  5099. ggml_tensor * cur;
  5100. ggml_tensor * inpL;
  5101. inpL = build_inp_embd(model.tok_embd);
  5102. // inp_pos - contains the positions
  5103. ggml_tensor * inp_pos = build_inp_pos();
  5104. // temperature tuning
  5105. ggml_tensor * inp_attn_scale = nullptr;
  5106. inp_attn_scale = build_inp_attn_scale();
  5107. auto * inp_attn = build_attn_inp_kv_unified_iswa();
  5108. const float kq_scale = hparams.f_attention_scale == 0.0f ? 1.0f/sqrtf(float(n_embd_head)) : hparams.f_attention_scale;
  5109. ggml_tensor * inp_out_ids = build_inp_out_ids();
  5110. for (int il = 0; il < n_layer; ++il) {
  5111. ggml_tensor * inpSA = inpL;
  5112. const bool use_rope = (il + 1) % hparams.n_no_rope_layer_step != 0;
  5113. // norm
  5114. cur = build_norm(inpL,
  5115. model.layers[il].attn_norm, NULL,
  5116. LLM_NORM_RMS, il);
  5117. cb(cur, "attn_norm", il);
  5118. // self-attention
  5119. {
  5120. // rope freq factors for llama3; may return nullptr for llama2 and other models
  5121. ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
  5122. // compute Q and K and RoPE them
  5123. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  5124. cb(Qcur, "Qcur", il);
  5125. if (model.layers[il].bq) {
  5126. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  5127. cb(Qcur, "Qcur", il);
  5128. }
  5129. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  5130. cb(Kcur, "Kcur", il);
  5131. if (model.layers[il].bk) {
  5132. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  5133. cb(Kcur, "Kcur", il);
  5134. }
  5135. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  5136. cb(Vcur, "Vcur", il);
  5137. if (model.layers[il].bv) {
  5138. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  5139. cb(Vcur, "Vcur", il);
  5140. }
  5141. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  5142. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  5143. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  5144. if (use_rope) {
  5145. Qcur = ggml_rope_ext(
  5146. ctx0, Qcur, inp_pos, rope_factors,
  5147. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  5148. ext_factor, attn_factor, beta_fast, beta_slow
  5149. );
  5150. Kcur = ggml_rope_ext(
  5151. ctx0, Kcur, inp_pos, rope_factors,
  5152. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  5153. ext_factor, attn_factor, beta_fast, beta_slow
  5154. );
  5155. } else if (inp_attn_scale) {
  5156. Qcur = ggml_mul(ctx0, Qcur, inp_attn_scale);
  5157. }
  5158. cb(Qcur, "Qcur", il);
  5159. cb(Kcur, "Kcur", il);
  5160. cb(Vcur, "Vcur", il);
  5161. if (use_rope && hparams.use_kq_norm) {
  5162. // Llama4TextL2Norm
  5163. Qcur = ggml_rms_norm(ctx0, Qcur, hparams.f_norm_rms_eps);
  5164. Kcur = ggml_rms_norm(ctx0, Kcur, hparams.f_norm_rms_eps);
  5165. cb(Qcur, "Qcur_normed", il);
  5166. cb(Kcur, "Kcur_normed", il);
  5167. }
  5168. cur = build_attn(inp_attn,
  5169. model.layers[il].wo, model.layers[il].bo,
  5170. Qcur, Kcur, Vcur, nullptr, nullptr, kq_scale, il);
  5171. cb(cur, "attn_out", il);
  5172. }
  5173. if (il == n_layer - 1 && inp_out_ids) {
  5174. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  5175. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  5176. }
  5177. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  5178. cb(ffn_inp, "ffn_inp", il);
  5179. // feed-forward network (non-MoE)
  5180. if (model.layers[il].ffn_gate_inp == nullptr) {
  5181. cur = build_norm(ffn_inp,
  5182. model.layers[il].ffn_norm, NULL,
  5183. LLM_NORM_RMS, il);
  5184. cb(cur, "ffn_norm", il);
  5185. cur = build_ffn(cur,
  5186. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  5187. model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, NULL,
  5188. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  5189. NULL,
  5190. LLM_FFN_SILU, LLM_FFN_PAR, il);
  5191. cb(cur, "ffn_out", il);
  5192. } else {
  5193. ggml_tensor * ffn_inp_normed = build_norm(ffn_inp,
  5194. model.layers[il].ffn_norm, NULL,
  5195. LLM_NORM_RMS, il);
  5196. cb(cur, "ffn_norm", il);
  5197. ggml_tensor * moe_out = build_moe_ffn(ffn_inp_normed,
  5198. model.layers[il].ffn_gate_inp,
  5199. model.layers[il].ffn_up_exps,
  5200. model.layers[il].ffn_gate_exps,
  5201. model.layers[il].ffn_down_exps,
  5202. nullptr,
  5203. n_expert, n_expert_used,
  5204. LLM_FFN_SILU, false,
  5205. false, 0.0,
  5206. LLAMA_EXPERT_GATING_FUNC_TYPE_SIGMOID,
  5207. il);
  5208. // Shared experts
  5209. ggml_tensor * shexp_out = build_ffn(ffn_inp_normed,
  5210. model.layers[il].ffn_up_shexp, NULL, NULL,
  5211. model.layers[il].ffn_gate_shexp, NULL, NULL,
  5212. model.layers[il].ffn_down_shexp, NULL, NULL,
  5213. NULL,
  5214. LLM_FFN_SILU, LLM_FFN_PAR, il);
  5215. cb(shexp_out, "ffn_moe_shexp", il);
  5216. cur = ggml_add(ctx0, moe_out, shexp_out);
  5217. cb(cur, "ffn_moe_out_merged", il);
  5218. }
  5219. cur = ggml_add(ctx0, cur, ffn_inp);
  5220. cb(cur, "ffn_out", il);
  5221. cur = build_cvec(cur, il);
  5222. cb(cur, "l_out", il);
  5223. // input for next layer
  5224. inpL = cur;
  5225. }
  5226. cur = inpL;
  5227. cur = build_norm(cur,
  5228. model.output_norm, NULL,
  5229. LLM_NORM_RMS, -1);
  5230. cb(cur, "result_norm", -1);
  5231. res->t_embd = cur;
  5232. // lm_head
  5233. cur = build_lora_mm(model.output, cur);
  5234. cb(cur, "result_output", -1);
  5235. res->t_logits = cur;
  5236. ggml_build_forward_expand(gf, cur);
  5237. }
  5238. };
  5239. struct llm_build_deci : public llm_graph_context {
  5240. llm_build_deci(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  5241. const int64_t n_embd_head = hparams.n_embd_head_v;
  5242. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  5243. GGML_ASSERT(n_embd_head == hparams.n_rot);
  5244. ggml_tensor * cur;
  5245. ggml_tensor * inpL;
  5246. inpL = build_inp_embd(model.tok_embd);
  5247. // inp_pos - contains the positions
  5248. ggml_tensor * inp_pos = build_inp_pos();
  5249. auto * inp_attn = build_attn_inp_kv_unified();
  5250. const float kq_scale = hparams.f_attention_scale == 0.0f ? 1.0f/sqrtf(float(n_embd_head)) : hparams.f_attention_scale;
  5251. ggml_tensor * inp_out_ids = build_inp_out_ids();
  5252. for (int il = 0; il < n_layer; ++il) {
  5253. ggml_tensor * inpSA = inpL;
  5254. const int64_t n_head_kv = hparams.n_head_kv(il);
  5255. const int64_t n_head = hparams.n_head(il);
  5256. const int64_t n_ff = hparams.n_ff(il);
  5257. if (n_head == 0) {
  5258. // attention-free layer of Llama-3_1-Nemotron-51B
  5259. cur = inpL;
  5260. } else {
  5261. // norm
  5262. cur = build_norm(inpL,
  5263. model.layers[il].attn_norm, NULL,
  5264. LLM_NORM_RMS, il);
  5265. cb(cur, "attn_norm", il);
  5266. }
  5267. if (n_head > 0 && n_head_kv == 0) {
  5268. // "linear attention" of Llama-3_1-Nemotron-51B
  5269. cur = build_lora_mm(model.layers[il].wo, cur);
  5270. cb(cur, "wo", il);
  5271. } else if (n_head > 0) {
  5272. // self-attention
  5273. // rope freq factors for llama3; may return nullptr for llama2 and other models
  5274. ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
  5275. // compute Q and K and RoPE them
  5276. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  5277. cb(Qcur, "Qcur", il);
  5278. if (model.layers[il].bq) {
  5279. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  5280. cb(Qcur, "Qcur", il);
  5281. }
  5282. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  5283. cb(Kcur, "Kcur", il);
  5284. if (model.layers[il].bk) {
  5285. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  5286. cb(Kcur, "Kcur", il);
  5287. }
  5288. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  5289. cb(Vcur, "Vcur", il);
  5290. if (model.layers[il].bv) {
  5291. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  5292. cb(Vcur, "Vcur", il);
  5293. }
  5294. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  5295. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  5296. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  5297. Qcur = ggml_rope_ext(
  5298. ctx0, Qcur, inp_pos, rope_factors,
  5299. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  5300. ext_factor, attn_factor, beta_fast, beta_slow
  5301. );
  5302. Kcur = ggml_rope_ext(
  5303. ctx0, Kcur, inp_pos, rope_factors,
  5304. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  5305. ext_factor, attn_factor, beta_fast, beta_slow
  5306. );
  5307. cb(Qcur, "Qcur", il);
  5308. cb(Kcur, "Kcur", il);
  5309. cb(Vcur, "Vcur", il);
  5310. cur = build_attn(inp_attn,
  5311. model.layers[il].wo, model.layers[il].bo,
  5312. Qcur, Kcur, Vcur, nullptr, nullptr, kq_scale, il);
  5313. }
  5314. if (il == n_layer - 1 && inp_out_ids) {
  5315. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  5316. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  5317. }
  5318. // FFN-free layer of Llama-3_1-Nemotron-Ultra-253B
  5319. if (n_ff == 0) {
  5320. continue;
  5321. }
  5322. // modified to support attention-free layer of Llama-3_1-Nemotron-51B
  5323. ggml_tensor * ffn_inp = cur;
  5324. if (n_head > 0) {
  5325. ffn_inp = ggml_add(ctx0, cur, inpSA);
  5326. cb(ffn_inp, "ffn_inp", il);
  5327. }
  5328. // feed-forward network
  5329. if (model.layers[il].ffn_gate_inp == nullptr) {
  5330. cur = build_norm(ffn_inp,
  5331. model.layers[il].ffn_norm, NULL,
  5332. LLM_NORM_RMS, il);
  5333. cb(cur, "ffn_norm", il);
  5334. cur = build_ffn(cur,
  5335. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  5336. model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, NULL,
  5337. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  5338. NULL,
  5339. LLM_FFN_SILU, LLM_FFN_PAR, il);
  5340. cb(cur, "ffn_out", il);
  5341. }
  5342. cur = ggml_add(ctx0, cur, ffn_inp);
  5343. cb(cur, "ffn_out", il);
  5344. cur = build_cvec(cur, il);
  5345. cb(cur, "l_out", il);
  5346. // input for next layer
  5347. inpL = cur;
  5348. }
  5349. cur = inpL;
  5350. cur = build_norm(cur,
  5351. model.output_norm, NULL,
  5352. LLM_NORM_RMS, -1);
  5353. cb(cur, "result_norm", -1);
  5354. res->t_embd = cur;
  5355. // lm_head
  5356. cur = build_lora_mm(model.output, cur);
  5357. cb(cur, "result_output", -1);
  5358. res->t_logits = cur;
  5359. ggml_build_forward_expand(gf, cur);
  5360. }
  5361. };
  5362. struct llm_build_baichuan : public llm_graph_context {
  5363. llm_build_baichuan(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  5364. const int64_t n_embd_head = hparams.n_embd_head_v;
  5365. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  5366. GGML_ASSERT(n_embd_head == hparams.n_rot);
  5367. ggml_tensor * cur;
  5368. ggml_tensor * inpL;
  5369. inpL = build_inp_embd(model.tok_embd);
  5370. // inp_pos - contains the positions
  5371. ggml_tensor * inp_pos = model.type == LLM_TYPE_7B ? build_inp_pos() : nullptr;
  5372. auto * inp_attn = build_attn_inp_kv_unified();
  5373. ggml_tensor * inp_out_ids = build_inp_out_ids();
  5374. for (int il = 0; il < n_layer; ++il) {
  5375. ggml_tensor * inpSA = inpL;
  5376. cur = build_norm(inpL,
  5377. model.layers[il].attn_norm, NULL,
  5378. LLM_NORM_RMS, il);
  5379. cb(cur, "attn_norm", il);
  5380. // self-attention
  5381. {
  5382. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  5383. cb(Qcur, "Qcur", il);
  5384. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  5385. cb(Kcur, "Kcur", il);
  5386. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  5387. cb(Vcur, "Vcur", il);
  5388. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  5389. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  5390. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  5391. switch (model.type) {
  5392. case LLM_TYPE_7B:
  5393. Qcur = ggml_rope_ext(
  5394. ctx0, Qcur, inp_pos, nullptr,
  5395. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  5396. ext_factor, attn_factor, beta_fast, beta_slow
  5397. );
  5398. Kcur = ggml_rope_ext(
  5399. ctx0, Kcur, inp_pos, nullptr,
  5400. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  5401. ext_factor, attn_factor, beta_fast, beta_slow
  5402. );
  5403. break;
  5404. case LLM_TYPE_13B:
  5405. break;
  5406. default:
  5407. GGML_ABORT("fatal error");
  5408. }
  5409. cb(Qcur, "Qcur", il);
  5410. cb(Kcur, "Kcur", il);
  5411. cb(Vcur, "Vcur", il);
  5412. cur = build_attn(inp_attn,
  5413. model.layers[il].wo, NULL,
  5414. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  5415. }
  5416. if (il == n_layer - 1 && inp_out_ids) {
  5417. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  5418. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  5419. }
  5420. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  5421. cb(ffn_inp, "ffn_inp", il);
  5422. // feed-forward network
  5423. {
  5424. cur = build_norm(ffn_inp,
  5425. model.layers[il].ffn_norm, NULL,
  5426. LLM_NORM_RMS, il);
  5427. cb(cur, "ffn_norm", il);
  5428. cur = build_ffn(cur,
  5429. model.layers[il].ffn_up, NULL, NULL,
  5430. model.layers[il].ffn_gate, NULL, NULL,
  5431. model.layers[il].ffn_down, NULL, NULL,
  5432. NULL,
  5433. LLM_FFN_SILU, LLM_FFN_PAR, il);
  5434. cb(cur, "ffn_out", il);
  5435. }
  5436. cur = ggml_add(ctx0, cur, ffn_inp);
  5437. cur = build_cvec(cur, il);
  5438. cb(cur, "l_out", il);
  5439. // input for next layer
  5440. inpL = cur;
  5441. }
  5442. cur = inpL;
  5443. cur = build_norm(cur,
  5444. model.output_norm, NULL,
  5445. LLM_NORM_RMS, -1);
  5446. cb(cur, "result_norm", -1);
  5447. res->t_embd = cur;
  5448. // lm_head
  5449. cur = build_lora_mm(model.output, cur);
  5450. cb(cur, "result_output", -1);
  5451. res->t_logits = cur;
  5452. ggml_build_forward_expand(gf, cur);
  5453. }
  5454. };
  5455. struct llm_build_xverse : public llm_graph_context {
  5456. llm_build_xverse(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  5457. const int64_t n_embd_head = hparams.n_embd_head_v;
  5458. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  5459. GGML_ASSERT(n_embd_head == hparams.n_rot);
  5460. ggml_tensor * cur;
  5461. ggml_tensor * inpL;
  5462. inpL = build_inp_embd(model.tok_embd);
  5463. // inp_pos - contains the positions
  5464. ggml_tensor * inp_pos = build_inp_pos();
  5465. auto * inp_attn = build_attn_inp_kv_unified();
  5466. ggml_tensor * inp_out_ids = build_inp_out_ids();
  5467. for (int il = 0; il < n_layer; ++il) {
  5468. ggml_tensor * inpSA = inpL;
  5469. cur = build_norm(inpL,
  5470. model.layers[il].attn_norm, NULL,
  5471. LLM_NORM_RMS, il);
  5472. cb(cur, "attn_norm", il);
  5473. // self-attention
  5474. {
  5475. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  5476. cb(Qcur, "Qcur", il);
  5477. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  5478. cb(Kcur, "Kcur", il);
  5479. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  5480. cb(Vcur, "Vcur", il);
  5481. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  5482. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  5483. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  5484. Qcur = ggml_rope_ext(
  5485. ctx0, Qcur, inp_pos, nullptr,
  5486. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  5487. ext_factor, attn_factor, beta_fast, beta_slow
  5488. );
  5489. Kcur = ggml_rope_ext(
  5490. ctx0, Kcur, inp_pos, nullptr,
  5491. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  5492. ext_factor, attn_factor, beta_fast, beta_slow
  5493. );
  5494. cb(Qcur, "Qcur", il);
  5495. cb(Kcur, "Kcur", il);
  5496. cb(Vcur, "Vcur", il);
  5497. cur = build_attn(inp_attn,
  5498. model.layers[il].wo, NULL,
  5499. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  5500. }
  5501. if (il == n_layer - 1 && inp_out_ids) {
  5502. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  5503. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  5504. }
  5505. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  5506. cb(ffn_inp, "ffn_inp", il);
  5507. // feed-forward network
  5508. {
  5509. cur = build_norm(ffn_inp,
  5510. model.layers[il].ffn_norm, NULL,
  5511. LLM_NORM_RMS, il);
  5512. cb(cur, "ffn_norm", il);
  5513. cur = build_ffn(cur,
  5514. model.layers[il].ffn_up, NULL, NULL,
  5515. model.layers[il].ffn_gate, NULL, NULL,
  5516. model.layers[il].ffn_down, NULL, NULL,
  5517. NULL,
  5518. LLM_FFN_SILU, LLM_FFN_PAR, il);
  5519. cb(cur, "ffn_out", il);
  5520. }
  5521. cur = ggml_add(ctx0, cur, ffn_inp);
  5522. cur = build_cvec(cur, il);
  5523. cb(cur, "l_out", il);
  5524. // input for next layer
  5525. inpL = cur;
  5526. }
  5527. cur = inpL;
  5528. cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1);
  5529. cb(cur, "result_norm", -1);
  5530. res->t_embd = cur;
  5531. // lm_head
  5532. cur = build_lora_mm(model.output, cur);
  5533. cb(cur, "result_output", -1);
  5534. res->t_logits = cur;
  5535. ggml_build_forward_expand(gf, cur);
  5536. }
  5537. };
  5538. struct llm_build_falcon : public llm_graph_context {
  5539. llm_build_falcon(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  5540. const int64_t n_embd_head = hparams.n_embd_head_v;
  5541. const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  5542. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  5543. GGML_ASSERT(n_embd_head == hparams.n_rot);
  5544. ggml_tensor * cur;
  5545. ggml_tensor * inpL;
  5546. inpL = build_inp_embd(model.tok_embd);
  5547. // inp_pos - contains the positions
  5548. ggml_tensor * inp_pos = build_inp_pos();
  5549. auto * inp_attn = build_attn_inp_kv_unified();
  5550. ggml_tensor * inp_out_ids = build_inp_out_ids();
  5551. for (int il = 0; il < n_layer; ++il) {
  5552. ggml_tensor * attn_norm;
  5553. attn_norm = build_norm(inpL,
  5554. model.layers[il].attn_norm,
  5555. model.layers[il].attn_norm_b,
  5556. LLM_NORM, il);
  5557. cb(attn_norm, "attn_norm", il);
  5558. // self-attention
  5559. {
  5560. if (model.layers[il].attn_norm_2) {
  5561. // Falcon-40B
  5562. cur = build_norm(inpL,
  5563. model.layers[il].attn_norm_2,
  5564. model.layers[il].attn_norm_2_b,
  5565. LLM_NORM, il);
  5566. cb(cur, "attn_norm_2", il);
  5567. } else {
  5568. cur = attn_norm;
  5569. }
  5570. cur = build_lora_mm(model.layers[il].wqkv, cur);
  5571. cb(cur, "wqkv", il);
  5572. 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));
  5573. 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));
  5574. ggml_tensor * Vcur = ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd + n_embd_gqa));
  5575. Vcur = ggml_cont_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  5576. // using mode = 2 for neox mode
  5577. Qcur = ggml_rope_ext(
  5578. ctx0, Qcur, inp_pos, nullptr,
  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, nullptr,
  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. cur = build_attn(inp_attn,
  5591. model.layers[il].wo, NULL,
  5592. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  5593. }
  5594. if (il == n_layer - 1 && inp_out_ids) {
  5595. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  5596. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  5597. attn_norm = ggml_get_rows(ctx0, attn_norm, inp_out_ids);
  5598. }
  5599. ggml_tensor * ffn_inp = cur;
  5600. // feed forward
  5601. {
  5602. cur = build_ffn(attn_norm, // !! use the attn norm, not the result
  5603. model.layers[il].ffn_up, NULL, NULL,
  5604. NULL, NULL, NULL,
  5605. model.layers[il].ffn_down, NULL, NULL,
  5606. NULL,
  5607. LLM_FFN_GELU, LLM_FFN_SEQ, il);
  5608. cb(cur, "ffn_out", il);
  5609. }
  5610. cur = ggml_add(ctx0, cur, ffn_inp);
  5611. cur = ggml_add(ctx0, cur, inpL);
  5612. cur = build_cvec(cur, il);
  5613. cb(cur, "l_out", il);
  5614. // input for next layer
  5615. inpL = cur;
  5616. }
  5617. cur = inpL;
  5618. // norm
  5619. cur = build_norm(cur,
  5620. model.output_norm,
  5621. model.output_norm_b,
  5622. LLM_NORM, -1);
  5623. cb(cur, "result_norm", -1);
  5624. res->t_embd = cur;
  5625. cur = build_lora_mm(model.output, cur);
  5626. cb(cur, "result_output", -1);
  5627. res->t_logits = cur;
  5628. ggml_build_forward_expand(gf, cur);
  5629. }
  5630. };
  5631. struct llm_build_grok : public llm_graph_context {
  5632. llm_build_grok(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  5633. const int64_t n_embd_head = hparams.n_embd_head_v;
  5634. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  5635. GGML_ASSERT(n_embd_head == hparams.n_rot);
  5636. ggml_tensor * cur;
  5637. ggml_tensor * inpL;
  5638. inpL = build_inp_embd(model.tok_embd);
  5639. // multiply by embedding_multiplier_scale of 78.38367176906169
  5640. inpL = ggml_scale(ctx0, inpL, 78.38367176906169f);
  5641. // inp_pos - contains the positions
  5642. ggml_tensor * inp_pos = build_inp_pos();
  5643. auto * inp_attn = build_attn_inp_kv_unified();
  5644. ggml_tensor * inp_out_ids = build_inp_out_ids();
  5645. for (int il = 0; il < n_layer; ++il) {
  5646. ggml_tensor * inpSA = inpL;
  5647. // norm
  5648. cur = build_norm(inpL,
  5649. model.layers[il].attn_norm, NULL,
  5650. LLM_NORM_RMS, il);
  5651. cb(cur, "attn_norm", il);
  5652. // self-attention
  5653. {
  5654. // compute Q and K and RoPE them
  5655. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  5656. cb(Qcur, "Qcur", il);
  5657. if (model.layers[il].bq) {
  5658. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  5659. cb(Qcur, "Qcur", il);
  5660. }
  5661. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  5662. cb(Kcur, "Kcur", il);
  5663. if (model.layers[il].bk) {
  5664. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  5665. cb(Kcur, "Kcur", il);
  5666. }
  5667. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  5668. cb(Vcur, "Vcur", il);
  5669. if (model.layers[il].bv) {
  5670. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  5671. cb(Vcur, "Vcur", il);
  5672. }
  5673. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  5674. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  5675. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  5676. Qcur = ggml_rope_ext(
  5677. ctx0, Qcur, inp_pos, nullptr,
  5678. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  5679. ext_factor, attn_factor, beta_fast, beta_slow
  5680. );
  5681. Kcur = ggml_rope_ext(
  5682. ctx0, Kcur, inp_pos, nullptr,
  5683. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  5684. ext_factor, attn_factor, beta_fast, beta_slow
  5685. );
  5686. cb(Qcur, "Qcur", il);
  5687. cb(Kcur, "Kcur", il);
  5688. cb(Vcur, "Vcur", il);
  5689. cur = build_attn(inp_attn,
  5690. model.layers[il].wo, model.layers[il].bo,
  5691. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f, il);
  5692. }
  5693. if (il == n_layer - 1 && inp_out_ids) {
  5694. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  5695. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  5696. }
  5697. // Grok
  5698. // if attn_out_norm is present then apply it before adding the input
  5699. if (model.layers[il].attn_out_norm) {
  5700. cur = build_norm(cur,
  5701. model.layers[il].attn_out_norm, NULL,
  5702. LLM_NORM_RMS, il);
  5703. cb(cur, "attn_out_norm", il);
  5704. }
  5705. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  5706. cb(ffn_inp, "ffn_inp", il);
  5707. // feed-forward network
  5708. // MoE branch
  5709. cur = build_norm(ffn_inp,
  5710. model.layers[il].ffn_norm, NULL,
  5711. LLM_NORM_RMS, il);
  5712. cb(cur, "ffn_norm", il);
  5713. cur = build_moe_ffn(cur,
  5714. model.layers[il].ffn_gate_inp,
  5715. model.layers[il].ffn_up_exps,
  5716. model.layers[il].ffn_gate_exps,
  5717. model.layers[il].ffn_down_exps,
  5718. nullptr,
  5719. n_expert, n_expert_used,
  5720. LLM_FFN_GELU, true,
  5721. false, 0.0,
  5722. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  5723. il);
  5724. cb(cur, "ffn_moe_out", il);
  5725. // Grok
  5726. // if layer_out_norm is present then apply it before adding the input
  5727. // Idea: maybe ffn_out_norm is a better name
  5728. if (model.layers[il].layer_out_norm) {
  5729. cur = build_norm(cur,
  5730. model.layers[il].layer_out_norm, NULL,
  5731. LLM_NORM_RMS, il);
  5732. cb(cur, "layer_out_norm", il);
  5733. }
  5734. cur = ggml_add(ctx0, cur, ffn_inp);
  5735. cb(cur, "ffn_out", il);
  5736. cur = build_cvec(cur, il);
  5737. cb(cur, "l_out", il);
  5738. // input for next layer
  5739. inpL = cur;
  5740. }
  5741. cur = inpL;
  5742. cur = build_norm(cur,
  5743. model.output_norm, NULL,
  5744. LLM_NORM_RMS, -1);
  5745. cb(cur, "result_norm", -1);
  5746. res->t_embd = cur;
  5747. // lm_head
  5748. cur = build_lora_mm(model.output, cur);
  5749. // Grok
  5750. // multiply logits by output_multiplier_scale of 0.5773502691896257
  5751. cur = ggml_scale(ctx0, cur, 0.5773502691896257f);
  5752. cb(cur, "result_output", -1);
  5753. res->t_logits = cur;
  5754. ggml_build_forward_expand(gf, cur);
  5755. }
  5756. };
  5757. struct llm_build_dbrx : public llm_graph_context {
  5758. llm_build_dbrx(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  5759. const int64_t n_embd_head = hparams.n_embd_head_v;
  5760. const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  5761. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  5762. GGML_ASSERT(n_embd_head == hparams.n_rot);
  5763. ggml_tensor * cur;
  5764. ggml_tensor * inpL;
  5765. inpL = build_inp_embd(model.tok_embd);
  5766. // inp_pos - contains the positions
  5767. ggml_tensor * inp_pos = build_inp_pos();
  5768. auto * inp_attn = build_attn_inp_kv_unified();
  5769. ggml_tensor * inp_out_ids = build_inp_out_ids();
  5770. for (int il = 0; il < n_layer; ++il) {
  5771. ggml_tensor * inpSA = inpL;
  5772. // norm
  5773. cur = build_norm(inpL,
  5774. model.layers[il].attn_norm, NULL,
  5775. LLM_NORM, il);
  5776. cb(cur, "attn_norm", il);
  5777. // self-attention
  5778. {
  5779. ggml_tensor * Qcur = nullptr;
  5780. ggml_tensor * Kcur = nullptr;
  5781. ggml_tensor * Vcur = nullptr;
  5782. cur = build_lora_mm(model.layers[il].wqkv, cur);
  5783. cb(cur, "wqkv", il);
  5784. cur = ggml_clamp(ctx0, cur, -hparams.f_clamp_kqv, hparams.f_clamp_kqv);
  5785. cb(cur, "wqkv_clamped", il);
  5786. 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));
  5787. 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));
  5788. Vcur = ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd + n_embd_gqa));
  5789. Vcur = ggml_cont_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  5790. Qcur = ggml_rope_ext(
  5791. ctx0, Qcur, inp_pos, nullptr,
  5792. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  5793. ext_factor, attn_factor, beta_fast, beta_slow
  5794. );
  5795. Kcur = ggml_rope_ext(
  5796. ctx0, Kcur, inp_pos, nullptr,
  5797. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  5798. ext_factor, attn_factor, beta_fast, beta_slow
  5799. );
  5800. cb(Qcur, "Qcur", il);
  5801. cb(Kcur, "Kcur", il);
  5802. cb(Vcur, "Vcur", il);
  5803. cur = build_attn(inp_attn,
  5804. model.layers[il].wo, NULL,
  5805. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  5806. }
  5807. if (il == n_layer - 1 && inp_out_ids) {
  5808. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  5809. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  5810. }
  5811. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  5812. cb(ffn_inp, "ffn_inp", il);
  5813. // feed-forward network
  5814. // MoE branch
  5815. cur = build_norm(ffn_inp,
  5816. model.layers[il].attn_out_norm, NULL,
  5817. LLM_NORM, il);
  5818. cb(cur, "attn_out_norm", il);
  5819. cur = build_moe_ffn(cur,
  5820. model.layers[il].ffn_gate_inp,
  5821. model.layers[il].ffn_up_exps,
  5822. model.layers[il].ffn_gate_exps,
  5823. model.layers[il].ffn_down_exps,
  5824. nullptr,
  5825. n_expert, n_expert_used,
  5826. LLM_FFN_SILU, true,
  5827. false, 0.0,
  5828. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  5829. il);
  5830. cb(cur, "ffn_moe_out", il);
  5831. cur = ggml_add(ctx0, cur, ffn_inp);
  5832. cb(cur, "ffn_out", il);
  5833. cur = build_cvec(cur, il);
  5834. cb(cur, "l_out", il);
  5835. // input for next layer
  5836. inpL = cur;
  5837. }
  5838. cur = inpL;
  5839. cur = build_norm(cur,
  5840. model.output_norm, NULL,
  5841. LLM_NORM, -1);
  5842. cb(cur, "result_norm", -1);
  5843. res->t_embd = cur;
  5844. // lm_head
  5845. cur = build_lora_mm(model.output, cur);
  5846. cb(cur, "result_output", -1);
  5847. res->t_logits = cur;
  5848. ggml_build_forward_expand(gf, cur);
  5849. }
  5850. };
  5851. struct llm_build_starcoder : public llm_graph_context {
  5852. llm_build_starcoder(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  5853. const int64_t n_embd_head = hparams.n_embd_head_v;
  5854. const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  5855. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  5856. ggml_tensor * cur;
  5857. ggml_tensor * inpL;
  5858. inpL = build_inp_embd(model.tok_embd);
  5859. // inp_pos - contains the positions
  5860. ggml_tensor * inp_pos = build_inp_pos();
  5861. auto * inp_attn = build_attn_inp_kv_unified();
  5862. ggml_tensor * pos = ggml_get_rows(ctx0, model.pos_embd, inp_pos);
  5863. cb(pos, "pos_embd", -1);
  5864. inpL = ggml_add(ctx0, inpL, pos);
  5865. cb(inpL, "inpL", -1);
  5866. ggml_tensor * inp_out_ids = build_inp_out_ids();
  5867. for (int il = 0; il < n_layer; ++il) {
  5868. cur = build_norm(inpL,
  5869. model.layers[il].attn_norm,
  5870. model.layers[il].attn_norm_b,
  5871. LLM_NORM, il);
  5872. cb(cur, "attn_norm", il);
  5873. // self-attention
  5874. {
  5875. cur = build_lora_mm(model.layers[il].wqkv, cur);
  5876. cb(cur, "wqkv", il);
  5877. cur = ggml_add(ctx0, cur, model.layers[il].bqkv);
  5878. cb(cur, "bqkv", il);
  5879. ggml_tensor * Qcur = ggml_view_2d(ctx0, cur, n_embd, n_tokens, cur->nb[1], 0*sizeof(float)*(n_embd));
  5880. ggml_tensor * Kcur = ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd));
  5881. ggml_tensor * Vcur = ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd + n_embd_gqa));
  5882. Qcur = ggml_cont_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  5883. Kcur = ggml_cont_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  5884. Vcur = ggml_cont_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  5885. cb(Qcur, "Qcur", il);
  5886. cb(Kcur, "Kcur", il);
  5887. cb(Vcur, "Vcur", il);
  5888. cur = build_attn(inp_attn,
  5889. model.layers[il].wo, model.layers[il].bo,
  5890. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  5891. }
  5892. if (il == n_layer - 1 && inp_out_ids) {
  5893. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  5894. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  5895. }
  5896. // add the input
  5897. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
  5898. cb(ffn_inp, "ffn_inp", il);
  5899. // FF
  5900. {
  5901. cur = build_norm(ffn_inp,
  5902. model.layers[il].ffn_norm,
  5903. model.layers[il].ffn_norm_b,
  5904. LLM_NORM, il);
  5905. cb(cur, "ffn_norm", il);
  5906. cur = build_ffn(cur,
  5907. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  5908. NULL, NULL, NULL,
  5909. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  5910. NULL,
  5911. LLM_FFN_GELU, LLM_FFN_SEQ, il);
  5912. cb(cur, "ffn_out", il);
  5913. }
  5914. cur = ggml_add(ctx0, cur, ffn_inp);
  5915. cur = build_cvec(cur, il);
  5916. cb(cur, "l_out", il);
  5917. // input for next layer
  5918. inpL = cur;
  5919. }
  5920. cur = build_norm(inpL,
  5921. model.output_norm,
  5922. model.output_norm_b,
  5923. LLM_NORM, -1);
  5924. cb(cur, "result_norm", -1);
  5925. res->t_embd = cur;
  5926. cur = build_lora_mm(model.output, cur);
  5927. cb(cur, "result_output", -1);
  5928. res->t_logits = cur;
  5929. ggml_build_forward_expand(gf, cur);
  5930. }
  5931. };
  5932. struct llm_build_refact : public llm_graph_context {
  5933. llm_build_refact(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  5934. const int64_t n_embd_head = hparams.n_embd_head_v;
  5935. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  5936. ggml_tensor * cur;
  5937. ggml_tensor * inpL;
  5938. inpL = build_inp_embd(model.tok_embd);
  5939. auto * inp_attn = build_attn_inp_kv_unified();
  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. cb(Qcur, "Qcur", il);
  5959. cb(Kcur, "Kcur", il);
  5960. cb(Vcur, "Vcur", il);
  5961. cur = build_attn(inp_attn,
  5962. model.layers[il].wo, NULL,
  5963. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  5964. }
  5965. if (il == n_layer - 1 && inp_out_ids) {
  5966. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  5967. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  5968. }
  5969. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  5970. cb(ffn_inp, "ffn_inp", il);
  5971. // feed-forward network
  5972. {
  5973. cur = build_norm(ffn_inp,
  5974. model.layers[il].ffn_norm, NULL,
  5975. LLM_NORM_RMS, il);
  5976. cb(cur, "ffn_norm", il);
  5977. cur = build_ffn(cur,
  5978. model.layers[il].ffn_up, NULL, NULL,
  5979. model.layers[il].ffn_gate, NULL, NULL,
  5980. model.layers[il].ffn_down, NULL, NULL,
  5981. NULL,
  5982. LLM_FFN_SILU, LLM_FFN_PAR, il);
  5983. cb(cur, "ffn_out", il);
  5984. }
  5985. cur = ggml_add(ctx0, cur, ffn_inp);
  5986. cur = build_cvec(cur, il);
  5987. cb(cur, "l_out", il);
  5988. // input for next layer
  5989. inpL = cur;
  5990. }
  5991. cur = inpL;
  5992. cur = build_norm(cur,
  5993. model.output_norm, NULL,
  5994. LLM_NORM_RMS, -1);
  5995. cb(cur, "result_norm", -1);
  5996. res->t_embd = cur;
  5997. // lm_head
  5998. cur = build_lora_mm(model.output, cur);
  5999. cb(cur, "result_output", -1);
  6000. res->t_logits = cur;
  6001. ggml_build_forward_expand(gf, cur);
  6002. }
  6003. };
  6004. struct llm_build_bert : public llm_graph_context {
  6005. llm_build_bert(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  6006. const int64_t n_embd_head = hparams.n_embd_head_v;
  6007. const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  6008. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  6009. ggml_tensor * cur;
  6010. ggml_tensor * inpL;
  6011. ggml_tensor * inp_pos = nullptr;
  6012. if (model.arch != LLM_ARCH_JINA_BERT_V2) {
  6013. inp_pos = build_inp_pos();
  6014. }
  6015. // construct input embeddings (token, type, position)
  6016. inpL = build_inp_embd(model.tok_embd);
  6017. // token types are hardcoded to zero ("Sentence A")
  6018. if (model.type_embd) {
  6019. ggml_tensor * type_row0 = ggml_view_1d(ctx0, model.type_embd, n_embd, 0);
  6020. inpL = ggml_add(ctx0, inpL, type_row0);
  6021. }
  6022. if (model.arch == LLM_ARCH_BERT) {
  6023. inpL = ggml_add(ctx0, ggml_get_rows(ctx0, model.pos_embd, inp_pos), inpL);
  6024. }
  6025. cb(inpL, "inp_embd", -1);
  6026. // embed layer norm
  6027. inpL = build_norm(inpL, model.tok_norm, model.tok_norm_b, LLM_NORM, -1);
  6028. cb(inpL, "inp_norm", -1);
  6029. auto * inp_attn = build_attn_inp_no_cache();
  6030. ggml_tensor * inp_out_ids = build_inp_out_ids();
  6031. for (int il = 0; il < n_layer; ++il) {
  6032. ggml_tensor * cur = inpL;
  6033. {
  6034. ggml_tensor * Qcur;
  6035. ggml_tensor * Kcur;
  6036. ggml_tensor * Vcur;
  6037. // self-attention
  6038. if (model.layers[il].wqkv) {
  6039. cur = build_lora_mm(model.layers[il].wqkv, cur);
  6040. cb(cur, "wqkv", il);
  6041. if (model.layers[il].bqkv) {
  6042. cur = ggml_add(ctx0, cur, model.layers[il].bqkv);
  6043. cb(cur, "bqkv", il);
  6044. }
  6045. Qcur = ggml_view_2d(ctx0, cur, n_embd, n_tokens, cur->nb[1], 0*sizeof(float)*(n_embd));
  6046. Kcur = ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd));
  6047. Vcur = ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd + n_embd_gqa));
  6048. Vcur = ggml_cont_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  6049. } else {
  6050. Qcur = ggml_add(ctx0, build_lora_mm(model.layers[il].wq, cur), model.layers[il].bq);
  6051. Kcur = ggml_add(ctx0, build_lora_mm(model.layers[il].wk, cur), model.layers[il].bk);
  6052. Vcur = ggml_add(ctx0, build_lora_mm(model.layers[il].wv, cur), model.layers[il].bv);
  6053. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  6054. }
  6055. if (model.layers[il].attn_q_norm) {
  6056. Qcur = build_norm(Qcur,
  6057. model.layers[il].attn_q_norm,
  6058. model.layers[il].attn_q_norm_b,
  6059. LLM_NORM, il);
  6060. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  6061. } else {
  6062. Qcur = ggml_cont_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  6063. }
  6064. if (model.layers[il].attn_k_norm) {
  6065. Kcur = build_norm(Kcur,
  6066. model.layers[il].attn_k_norm,
  6067. model.layers[il].attn_k_norm_b,
  6068. LLM_NORM, il);
  6069. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  6070. } else {
  6071. Kcur = ggml_cont_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  6072. }
  6073. // RoPE
  6074. if (model.arch == LLM_ARCH_NOMIC_BERT || model.arch == LLM_ARCH_NOMIC_BERT_MOE) {
  6075. Qcur = ggml_rope_ext(
  6076. ctx0, Qcur, inp_pos, nullptr,
  6077. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  6078. ext_factor, attn_factor, beta_fast, beta_slow
  6079. );
  6080. Kcur = ggml_rope_ext(
  6081. ctx0, Kcur, inp_pos, nullptr,
  6082. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  6083. ext_factor, attn_factor, beta_fast, beta_slow
  6084. );
  6085. }
  6086. cb(Qcur, "Qcur", il);
  6087. cb(Kcur, "Kcur", il);
  6088. cb(Vcur, "Vcur", il);
  6089. cur = build_attn(inp_attn,
  6090. model.layers[il].wo, model.layers[il].bo,
  6091. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  6092. cb(cur, "kqv_out", il);
  6093. }
  6094. if (il == n_layer - 1 && inp_out_ids) {
  6095. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  6096. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  6097. }
  6098. // re-add the layer input
  6099. cur = ggml_add(ctx0, cur, inpL);
  6100. // attention layer norm
  6101. cur = build_norm(cur, model.layers[il].attn_out_norm, model.layers[il].attn_out_norm_b, LLM_NORM, il);
  6102. if (model.layers[il].attn_norm_2 != nullptr) {
  6103. cur = ggml_add(ctx0, cur, inpL); // re-add the layer input
  6104. cur = build_norm(cur, model.layers[il].attn_norm_2, model.layers[il].attn_norm_2_b, LLM_NORM, il);
  6105. }
  6106. ggml_tensor * ffn_inp = cur;
  6107. cb(ffn_inp, "ffn_inp", il);
  6108. // feed-forward network
  6109. if (hparams.moe_every_n_layers > 0 && il % hparams.moe_every_n_layers == 1) {
  6110. // MoE branch
  6111. cur = build_moe_ffn(cur,
  6112. model.layers[il].ffn_gate_inp,
  6113. model.layers[il].ffn_up_exps,
  6114. nullptr,
  6115. model.layers[il].ffn_down_exps,
  6116. nullptr,
  6117. hparams.n_expert,
  6118. hparams.n_expert_used,
  6119. LLM_FFN_GELU,
  6120. false, false,
  6121. 0.0f,
  6122. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX, il);
  6123. cb(cur, "ffn_moe_out", il);
  6124. } else if (model.arch == LLM_ARCH_BERT || model.arch == LLM_ARCH_NOMIC_BERT_MOE) {
  6125. cur = build_ffn(cur,
  6126. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  6127. NULL, NULL, NULL,
  6128. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  6129. NULL,
  6130. LLM_FFN_GELU, LLM_FFN_SEQ, il);
  6131. cb(cur, "ffn_out", il);
  6132. } else if (model.arch == LLM_ARCH_JINA_BERT_V2) {
  6133. cur = build_ffn(cur,
  6134. model.layers[il].ffn_up, NULL, NULL,
  6135. model.layers[il].ffn_gate, NULL, NULL,
  6136. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  6137. NULL,
  6138. model.layers[il].ffn_gate ? LLM_FFN_GELU : LLM_FFN_GEGLU, LLM_FFN_PAR, il);
  6139. cb(cur, "ffn_out", il);
  6140. } else {
  6141. cur = build_ffn(cur,
  6142. model.layers[il].ffn_up, NULL, NULL,
  6143. model.layers[il].ffn_gate, NULL, NULL,
  6144. model.layers[il].ffn_down, NULL, NULL,
  6145. NULL,
  6146. LLM_FFN_SILU, LLM_FFN_PAR, il);
  6147. cb(cur, "ffn_out", il);
  6148. }
  6149. // attentions bypass the intermediate layer
  6150. cur = ggml_add(ctx0, cur, ffn_inp);
  6151. // output layer norm
  6152. cur = build_norm(cur, model.layers[il].layer_out_norm, model.layers[il].layer_out_norm_b, LLM_NORM, il);
  6153. // input for next layer
  6154. inpL = cur;
  6155. }
  6156. cur = inpL;
  6157. cb(cur, "result_embd", -1);
  6158. res->t_embd = cur;
  6159. ggml_build_forward_expand(gf, cur);
  6160. }
  6161. };
  6162. struct llm_build_neo_bert : public llm_graph_context {
  6163. llm_build_neo_bert(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  6164. const int64_t n_embd_head = hparams.n_embd_head_v;
  6165. const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  6166. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  6167. ggml_tensor * cur;
  6168. ggml_tensor * inpL;
  6169. ggml_tensor * inp_pos = build_inp_pos();
  6170. // construct input embeddings (token, type, position)
  6171. inpL = build_inp_embd(model.tok_embd);
  6172. cb(inpL, "inp_embd", -1);
  6173. auto * inp_attn = build_attn_inp_no_cache();
  6174. ggml_tensor * inp_out_ids = build_inp_out_ids();
  6175. for (int il = 0; il < n_layer; ++il) {
  6176. ggml_tensor * cur = inpL;
  6177. // pre-norm
  6178. cur = build_norm(inpL,
  6179. model.layers[il].attn_norm, NULL,
  6180. LLM_NORM_RMS, il);
  6181. {
  6182. ggml_tensor * Qcur;
  6183. ggml_tensor * Kcur;
  6184. ggml_tensor * Vcur;
  6185. // self-attention
  6186. cur = build_lora_mm(model.layers[il].wqkv, cur);
  6187. cb(cur, "wqkv", il);
  6188. 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));
  6189. 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));
  6190. Vcur = ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd + n_embd_gqa));
  6191. Vcur = ggml_cont_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  6192. // RoPE
  6193. Qcur = ggml_rope_ext(
  6194. ctx0, Qcur, inp_pos, nullptr,
  6195. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  6196. ext_factor, attn_factor, beta_fast, beta_slow
  6197. );
  6198. Kcur = ggml_rope_ext(
  6199. ctx0, Kcur, inp_pos, nullptr,
  6200. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  6201. ext_factor, attn_factor, beta_fast, beta_slow
  6202. );
  6203. cb(Qcur, "Qcur", il);
  6204. cb(Kcur, "Kcur", il);
  6205. cb(Vcur, "Vcur", il);
  6206. cur = build_attn(inp_attn,
  6207. model.layers[il].wo, nullptr,
  6208. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  6209. cb(cur, "kqv_out", il);
  6210. }
  6211. if (il == n_layer - 1 && inp_out_ids) {
  6212. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  6213. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  6214. }
  6215. // re-add the layer input
  6216. cur = ggml_add(ctx0, cur, inpL);
  6217. ggml_tensor * ffn_inp = cur;
  6218. cb(ffn_inp, "ffn_inp", il);
  6219. // pre-norm
  6220. cur = build_norm(ffn_inp,
  6221. model.layers[il].ffn_norm, NULL,
  6222. LLM_NORM_RMS, il);
  6223. cb(cur, "ffn_norm", il);
  6224. // feed-forward network
  6225. cur = build_ffn(cur,
  6226. model.layers[il].ffn_up,
  6227. NULL, NULL, NULL, NULL, NULL,
  6228. model.layers[il].ffn_down,
  6229. NULL, NULL, NULL,
  6230. LLM_FFN_SWIGLU, LLM_FFN_SEQ, il);
  6231. // attentions bypass the intermediate layer
  6232. cur = ggml_add(ctx0, cur, ffn_inp);
  6233. // input for next layer
  6234. inpL = cur;
  6235. }
  6236. cur = inpL;
  6237. cur = build_norm(cur,
  6238. model.output_norm_enc, NULL,
  6239. LLM_NORM_RMS, -1);
  6240. cb(cur, "result_embd", -1);
  6241. res->t_embd = cur;
  6242. ggml_build_forward_expand(gf, cur);
  6243. }
  6244. };
  6245. struct llm_build_bloom : public llm_graph_context {
  6246. llm_build_bloom(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  6247. const int64_t n_embd_head = hparams.n_embd_head_v;
  6248. const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  6249. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  6250. ggml_tensor * cur;
  6251. ggml_tensor * inpL;
  6252. inpL = build_inp_embd(model.tok_embd);
  6253. auto * inp_attn = build_attn_inp_kv_unified();
  6254. inpL = build_norm(inpL,
  6255. model.tok_norm,
  6256. model.tok_norm_b,
  6257. LLM_NORM, -1);
  6258. cb(inpL, "inp_norm", -1);
  6259. ggml_tensor * inp_out_ids = build_inp_out_ids();
  6260. for (int il = 0; il < n_layer; ++il) {
  6261. cur = build_norm(inpL,
  6262. model.layers[il].attn_norm,
  6263. model.layers[il].attn_norm_b,
  6264. LLM_NORM, il);
  6265. cb(cur, "attn_norm", il);
  6266. // self-attention
  6267. {
  6268. cur = build_lora_mm(model.layers[il].wqkv, cur);
  6269. cb(cur, "wqkv", il);
  6270. cur = ggml_add(ctx0, cur, model.layers[il].bqkv);
  6271. cb(cur, "bqkv", il);
  6272. ggml_tensor * Qcur = ggml_view_2d(ctx0, cur, n_embd, n_tokens, cur->nb[1], 0*sizeof(float)*(n_embd));
  6273. ggml_tensor * Kcur = ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd));
  6274. ggml_tensor * Vcur = ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd + n_embd_gqa));
  6275. Qcur = ggml_cont_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  6276. Kcur = ggml_cont_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  6277. Vcur = ggml_cont_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  6278. cb(Qcur, "Qcur", il);
  6279. cb(Kcur, "Kcur", il);
  6280. cb(Vcur, "Vcur", il);
  6281. cur = build_attn(inp_attn,
  6282. model.layers[il].wo, model.layers[il].bo,
  6283. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  6284. }
  6285. if (il == n_layer - 1 && inp_out_ids) {
  6286. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  6287. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  6288. }
  6289. // Add the input
  6290. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
  6291. cb(ffn_inp, "ffn_inp", il);
  6292. // FF
  6293. {
  6294. cur = build_norm(ffn_inp,
  6295. model.layers[il].ffn_norm,
  6296. model.layers[il].ffn_norm_b,
  6297. LLM_NORM, il);
  6298. cb(cur, "ffn_norm", il);
  6299. cur = build_ffn(cur,
  6300. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  6301. NULL, NULL, NULL,
  6302. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  6303. NULL,
  6304. LLM_FFN_GELU, LLM_FFN_SEQ, il);
  6305. cb(cur, "ffn_out", il);
  6306. }
  6307. cur = ggml_add(ctx0, cur, ffn_inp);
  6308. cur = build_cvec(cur, il);
  6309. cb(cur, "l_out", il);
  6310. // input for next layer
  6311. inpL = cur;
  6312. }
  6313. cur = build_norm(inpL,
  6314. model.output_norm,
  6315. model.output_norm_b,
  6316. LLM_NORM, -1);
  6317. cb(cur, "result_norm", -1);
  6318. res->t_embd = cur;
  6319. cur = build_lora_mm(model.output, cur);
  6320. cb(cur, "result_output", -1);
  6321. res->t_logits = cur;
  6322. ggml_build_forward_expand(gf, cur);
  6323. }
  6324. };
  6325. struct llm_build_mpt : public llm_graph_context {
  6326. llm_build_mpt(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  6327. const int64_t n_embd_head = hparams.n_embd_head_v;
  6328. const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  6329. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  6330. ggml_tensor * cur;
  6331. ggml_tensor * pos;
  6332. ggml_tensor * inpL;
  6333. inpL = build_inp_embd(model.tok_embd);
  6334. auto * inp_attn = build_attn_inp_kv_unified();
  6335. if (model.pos_embd) {
  6336. // inp_pos - contains the positions
  6337. ggml_tensor * inp_pos = build_inp_pos();
  6338. pos = ggml_get_rows(ctx0, model.pos_embd, inp_pos);
  6339. cb(pos, "pos_embd", -1);
  6340. inpL = ggml_add(ctx0, inpL, pos);
  6341. cb(inpL, "inpL", -1);
  6342. }
  6343. ggml_tensor * inp_out_ids = build_inp_out_ids();
  6344. for (int il = 0; il < n_layer; ++il) {
  6345. ggml_tensor * attn_norm;
  6346. attn_norm = build_norm(inpL,
  6347. model.layers[il].attn_norm,
  6348. model.layers[il].attn_norm_b,
  6349. LLM_NORM, il);
  6350. cb(attn_norm, "attn_norm", il);
  6351. // self-attention
  6352. {
  6353. cur = attn_norm;
  6354. cur = build_lora_mm(model.layers[il].wqkv, cur);
  6355. cb(cur, "wqkv", il);
  6356. if (model.layers[il].bqkv){
  6357. cur = ggml_add(ctx0, cur, model.layers[il].bqkv);
  6358. cb(cur, "bqkv", il);
  6359. }
  6360. if (hparams.f_clamp_kqv > 0.0f) {
  6361. cur = ggml_clamp(ctx0, cur, -hparams.f_clamp_kqv, hparams.f_clamp_kqv);
  6362. cb(cur, "wqkv_clamped", il);
  6363. }
  6364. ggml_tensor * Qcur = ggml_view_2d(ctx0, cur, n_embd, n_tokens, cur->nb[1], 0*sizeof(float)*(n_embd));
  6365. ggml_tensor * Kcur = ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd));
  6366. ggml_tensor * Vcur = ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd + n_embd_gqa));
  6367. cb(Qcur, "Qcur", il);
  6368. cb(Kcur, "Kcur", il);
  6369. cb(Vcur, "Vcur", il);
  6370. // Q/K Layernorm
  6371. if (model.layers[il].attn_q_norm) {
  6372. Qcur = build_norm(Qcur,
  6373. model.layers[il].attn_q_norm,
  6374. model.layers[il].attn_q_norm_b,
  6375. LLM_NORM, il);
  6376. cb(Qcur, "Qcur", il);
  6377. Kcur = build_norm(Kcur,
  6378. model.layers[il].attn_k_norm,
  6379. model.layers[il].attn_k_norm_b,
  6380. LLM_NORM, il);
  6381. cb(Kcur, "Kcur", il);
  6382. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  6383. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  6384. } else {
  6385. Qcur = ggml_cont_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  6386. cb(Qcur, "Qcur", il);
  6387. Kcur = ggml_cont_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  6388. cb(Kcur, "Kcur", il);
  6389. }
  6390. Vcur = ggml_cont_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  6391. cb(Qcur, "Qcur", il);
  6392. cb(Kcur, "Kcur", il);
  6393. cb(Vcur, "Vcur", il);
  6394. cur = build_attn(inp_attn,
  6395. model.layers[il].wo, model.layers[il].bo,
  6396. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  6397. }
  6398. if (il == n_layer - 1 && inp_out_ids) {
  6399. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  6400. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  6401. }
  6402. // Add the input
  6403. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
  6404. cb(ffn_inp, "ffn_inp", il);
  6405. // feed forward
  6406. {
  6407. cur = build_norm(ffn_inp,
  6408. model.layers[il].ffn_norm,
  6409. model.layers[il].ffn_norm_b,
  6410. LLM_NORM, il);
  6411. cb(cur, "ffn_norm", il);
  6412. cur = build_ffn(cur,
  6413. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  6414. NULL, NULL, NULL,
  6415. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  6416. model.layers[il].ffn_act,
  6417. LLM_FFN_GELU, LLM_FFN_SEQ, il);
  6418. cb(cur, "ffn_out", il);
  6419. }
  6420. cur = ggml_add(ctx0, cur, ffn_inp);
  6421. cur = build_cvec(cur, il);
  6422. cb(cur, "l_out", il);
  6423. // input for next layer
  6424. inpL = cur;
  6425. }
  6426. cur = inpL;
  6427. cur = build_norm(cur,
  6428. model.output_norm,
  6429. model.output_norm_b,
  6430. LLM_NORM, -1);
  6431. cb(cur, "result_norm", -1);
  6432. res->t_embd = cur;
  6433. cur = build_lora_mm(model.output, cur);
  6434. cb(cur, "result_output", -1);
  6435. res->t_logits = cur;
  6436. ggml_build_forward_expand(gf, cur);
  6437. }
  6438. };
  6439. struct llm_build_stablelm : public llm_graph_context {
  6440. llm_build_stablelm(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  6441. const int64_t n_embd_head = hparams.n_embd_head_v;
  6442. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  6443. ggml_tensor * cur;
  6444. ggml_tensor * inpL;
  6445. inpL = build_inp_embd(model.tok_embd);
  6446. // inp_pos - contains the positions
  6447. ggml_tensor * inp_pos = build_inp_pos();
  6448. auto * inp_attn = build_attn_inp_kv_unified();
  6449. ggml_tensor * inp_out_ids = build_inp_out_ids();
  6450. for (int il = 0; il < n_layer; ++il) {
  6451. // norm
  6452. cur = build_norm(inpL,
  6453. model.layers[il].attn_norm,
  6454. model.layers[il].attn_norm_b,
  6455. LLM_NORM, il);
  6456. cb(cur, "attn_norm", il);
  6457. ggml_tensor * inpSA = cur;
  6458. // self-attention
  6459. {
  6460. // compute Q and K and RoPE them
  6461. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  6462. cb(Qcur, "Qcur", il);
  6463. if (model.layers[il].bq) {
  6464. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  6465. cb(Qcur, "Qcur", il);
  6466. }
  6467. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  6468. cb(Kcur, "Kcur", il);
  6469. if (model.layers[il].bk) {
  6470. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  6471. cb(Kcur, "Kcur", il);
  6472. }
  6473. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  6474. cb(Vcur, "Vcur", il);
  6475. if (model.layers[il].bv) {
  6476. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  6477. cb(Vcur, "Vcur", il);
  6478. }
  6479. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  6480. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  6481. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  6482. if (model.layers[il].attn_q_norm) {
  6483. Qcur = build_norm(Qcur,
  6484. model.layers[il].attn_q_norm,
  6485. NULL,
  6486. LLM_NORM, il);
  6487. cb(Qcur, "Qcur", il);
  6488. }
  6489. if (model.layers[il].attn_k_norm) {
  6490. Kcur = build_norm(Kcur,
  6491. model.layers[il].attn_k_norm,
  6492. NULL,
  6493. LLM_NORM, il);
  6494. cb(Kcur, "Kcur", il);
  6495. }
  6496. Qcur = ggml_rope_ext(
  6497. ctx0, Qcur, inp_pos, nullptr,
  6498. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  6499. ext_factor, attn_factor, beta_fast, beta_slow
  6500. );
  6501. Kcur = ggml_rope_ext(
  6502. ctx0, Kcur, inp_pos, nullptr,
  6503. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  6504. ext_factor, attn_factor, beta_fast, beta_slow
  6505. );
  6506. cb(Qcur, "Qcur", il);
  6507. cb(Kcur, "Kcur", il);
  6508. cb(Vcur, "Vcur", il);
  6509. cur = build_attn(inp_attn,
  6510. model.layers[il].wo, NULL,
  6511. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  6512. }
  6513. if (il == n_layer - 1 && inp_out_ids) {
  6514. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  6515. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  6516. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  6517. }
  6518. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
  6519. cb(ffn_inp, "ffn_inp", il);
  6520. // feed-forward network
  6521. {
  6522. if (model.layers[il].ffn_norm) {
  6523. cur = build_norm(ffn_inp,
  6524. model.layers[il].ffn_norm,
  6525. model.layers[il].ffn_norm_b,
  6526. LLM_NORM, il);
  6527. cb(cur, "ffn_norm", il);
  6528. } else {
  6529. // parallel residual
  6530. cur = inpSA;
  6531. }
  6532. cur = build_ffn(cur,
  6533. model.layers[il].ffn_up, NULL, NULL,
  6534. model.layers[il].ffn_gate, NULL, NULL,
  6535. model.layers[il].ffn_down, NULL, NULL,
  6536. NULL,
  6537. LLM_FFN_SILU, LLM_FFN_PAR, il);
  6538. cb(cur, "ffn_out", il);
  6539. }
  6540. cur = ggml_add(ctx0, cur, ffn_inp);
  6541. cur = build_cvec(cur, il);
  6542. cb(cur, "l_out", il);
  6543. // input for next layer
  6544. inpL = cur;
  6545. }
  6546. cur = inpL;
  6547. cur = build_norm(cur,
  6548. model.output_norm,
  6549. model.output_norm_b,
  6550. LLM_NORM, -1);
  6551. cb(cur, "result_norm", -1);
  6552. res->t_embd = cur;
  6553. // lm_head
  6554. cur = build_lora_mm(model.output, cur);
  6555. cb(cur, "result_output", -1);
  6556. res->t_logits = cur;
  6557. ggml_build_forward_expand(gf, cur);
  6558. }
  6559. };
  6560. struct llm_build_qwen : public llm_graph_context {
  6561. llm_build_qwen(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  6562. const int64_t n_embd_head = hparams.n_embd_head_v;
  6563. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  6564. ggml_tensor * cur;
  6565. ggml_tensor * inpL;
  6566. inpL = build_inp_embd(model.tok_embd);
  6567. // inp_pos - contains the positions
  6568. ggml_tensor * inp_pos = build_inp_pos();
  6569. auto * inp_attn = build_attn_inp_kv_unified();
  6570. ggml_tensor * inp_out_ids = build_inp_out_ids();
  6571. for (int il = 0; il < n_layer; ++il) {
  6572. ggml_tensor * inpSA = inpL;
  6573. cur = build_norm(inpL,
  6574. model.layers[il].attn_norm, NULL,
  6575. LLM_NORM_RMS, il);
  6576. cb(cur, "attn_norm", il);
  6577. // self-attention
  6578. {
  6579. cur = build_lora_mm(model.layers[il].wqkv, cur);
  6580. cb(cur, "wqkv", il);
  6581. cur = ggml_add(ctx0, cur, model.layers[il].bqkv);
  6582. cb(cur, "bqkv", il);
  6583. 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));
  6584. 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));
  6585. ggml_tensor * Vcur = ggml_view_2d(ctx0, cur, n_embd, n_tokens, cur->nb[1], 2*sizeof(float)*(n_embd));
  6586. Vcur = ggml_cont_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  6587. // using mode = 2 for neox mode
  6588. Qcur = ggml_rope_ext(
  6589. ctx0, Qcur, inp_pos, nullptr,
  6590. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  6591. ext_factor, attn_factor, beta_fast, beta_slow
  6592. );
  6593. Kcur = ggml_rope_ext(
  6594. ctx0, Kcur, inp_pos, nullptr,
  6595. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  6596. ext_factor, attn_factor, beta_fast, beta_slow
  6597. );
  6598. cb(Qcur, "Qcur", il);
  6599. cb(Kcur, "Kcur", il);
  6600. cb(Vcur, "Vcur", il);
  6601. cur = build_attn(inp_attn,
  6602. model.layers[il].wo, NULL,
  6603. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  6604. }
  6605. if (il == n_layer - 1 && inp_out_ids) {
  6606. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  6607. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  6608. }
  6609. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  6610. cb(ffn_inp, "ffn_inp", il);
  6611. // feed-forward forward
  6612. {
  6613. cur = build_norm(ffn_inp,
  6614. model.layers[il].ffn_norm, NULL,
  6615. LLM_NORM_RMS, il);
  6616. cb(cur, "ffn_norm", il);
  6617. cur = build_ffn(cur,
  6618. model.layers[il].ffn_up, NULL, NULL,
  6619. model.layers[il].ffn_gate, NULL, NULL,
  6620. model.layers[il].ffn_down, NULL, NULL,
  6621. NULL,
  6622. LLM_FFN_SILU, LLM_FFN_PAR, il);
  6623. cb(cur, "ffn_out", il);
  6624. }
  6625. cur = ggml_add(ctx0, cur, ffn_inp);
  6626. cur = build_cvec(cur, il);
  6627. cb(cur, "l_out", il);
  6628. // input for next layer
  6629. inpL = cur;
  6630. }
  6631. cur = inpL;
  6632. cur = build_norm(cur,
  6633. model.output_norm, NULL,
  6634. LLM_NORM_RMS, -1);
  6635. cb(cur, "result_norm", -1);
  6636. res->t_embd = cur;
  6637. // lm_head
  6638. cur = build_lora_mm(model.output, cur);
  6639. cb(cur, "result_output", -1);
  6640. res->t_logits = cur;
  6641. ggml_build_forward_expand(gf, cur);
  6642. }
  6643. };
  6644. struct llm_build_qwen2 : public llm_graph_context {
  6645. llm_build_qwen2(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  6646. const int64_t n_embd_head = hparams.n_embd_head_v;
  6647. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  6648. GGML_ASSERT(n_embd_head == hparams.n_rot);
  6649. ggml_tensor * cur;
  6650. ggml_tensor * inpL;
  6651. inpL = build_inp_embd(model.tok_embd);
  6652. // inp_pos - contains the positions
  6653. ggml_tensor * inp_pos = build_inp_pos();
  6654. auto * inp_attn = build_attn_inp_kv_unified();
  6655. ggml_tensor * inp_out_ids = build_inp_out_ids();
  6656. for (int il = 0; il < n_layer; ++il) {
  6657. ggml_tensor * inpSA = inpL;
  6658. // norm
  6659. cur = build_norm(inpL,
  6660. model.layers[il].attn_norm, NULL,
  6661. LLM_NORM_RMS, il);
  6662. cb(cur, "attn_norm", il);
  6663. // self-attention
  6664. {
  6665. // compute Q and K and RoPE them
  6666. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  6667. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  6668. cb(Qcur, "Qcur", il);
  6669. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  6670. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  6671. cb(Kcur, "Kcur", il);
  6672. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  6673. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  6674. cb(Vcur, "Vcur", il);
  6675. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  6676. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  6677. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  6678. Qcur = ggml_rope_ext(
  6679. ctx0, Qcur, inp_pos, nullptr,
  6680. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  6681. ext_factor, attn_factor, beta_fast, beta_slow
  6682. );
  6683. Kcur = ggml_rope_ext(
  6684. ctx0, Kcur, inp_pos, nullptr,
  6685. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  6686. ext_factor, attn_factor, beta_fast, beta_slow
  6687. );
  6688. cb(Qcur, "Qcur", il);
  6689. cb(Kcur, "Kcur", il);
  6690. cb(Vcur, "Vcur", il);
  6691. cur = build_attn(inp_attn,
  6692. model.layers[il].wo, model.layers[il].bo,
  6693. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  6694. }
  6695. if (il == n_layer - 1 && inp_out_ids) {
  6696. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  6697. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  6698. }
  6699. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  6700. cb(ffn_inp, "ffn_inp", il);
  6701. // feed-forward network
  6702. cur = build_norm(ffn_inp,
  6703. model.layers[il].ffn_norm, NULL,
  6704. LLM_NORM_RMS, il);
  6705. cb(cur, "ffn_norm", il);
  6706. cur = build_ffn(cur,
  6707. model.layers[il].ffn_up, NULL, NULL,
  6708. model.layers[il].ffn_gate, NULL, NULL,
  6709. model.layers[il].ffn_down, NULL, NULL,
  6710. NULL,
  6711. LLM_FFN_SILU, LLM_FFN_PAR, il);
  6712. cb(cur, "ffn_out", il);
  6713. cur = ggml_add(ctx0, cur, ffn_inp);
  6714. cur = build_cvec(cur, il);
  6715. cb(cur, "l_out", il);
  6716. // input for next layer
  6717. inpL = cur;
  6718. }
  6719. cur = inpL;
  6720. cur = build_norm(cur,
  6721. model.output_norm, NULL,
  6722. LLM_NORM_RMS, -1);
  6723. cb(cur, "result_norm", -1);
  6724. res->t_embd = cur;
  6725. // lm_head
  6726. cur = build_lora_mm(model.output, cur);
  6727. if (model.output_b != nullptr) {
  6728. cur = ggml_add(ctx0, cur, model.output_b);
  6729. }
  6730. cb(cur, "result_output", -1);
  6731. res->t_logits = cur;
  6732. ggml_build_forward_expand(gf, cur);
  6733. }
  6734. };
  6735. struct llm_build_dream : public llm_graph_context {
  6736. llm_build_dream(const llama_model & model, const llm_graph_params & params) :
  6737. llm_graph_context(params) {
  6738. //copied from qwen2
  6739. const int64_t n_embd_head = hparams.n_embd_head_v;
  6740. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  6741. GGML_ASSERT(n_embd_head == hparams.n_rot);
  6742. ggml_tensor * cur;
  6743. ggml_tensor * inpL;
  6744. inpL = build_inp_embd(model.tok_embd);
  6745. // inp_pos - contains the positions
  6746. ggml_tensor * inp_pos = build_inp_pos();
  6747. auto * inp_attn = build_attn_inp_no_cache();
  6748. ggml_tensor * inp_out_ids = build_inp_out_ids();
  6749. for (int il = 0; il < n_layer; ++il) {
  6750. ggml_tensor * inpSA = inpL;
  6751. // norm
  6752. cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
  6753. cb(cur, "attn_norm", il);
  6754. // self-attention
  6755. {
  6756. // compute Q and K and RoPE them
  6757. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  6758. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  6759. cb(Qcur, "Qcur", il);
  6760. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  6761. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  6762. cb(Kcur, "Kcur", il);
  6763. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  6764. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  6765. cb(Vcur, "Vcur", il);
  6766. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  6767. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  6768. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  6769. Qcur = ggml_rope_ext(ctx0, Qcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  6770. ext_factor, attn_factor, beta_fast, beta_slow);
  6771. Kcur = ggml_rope_ext(ctx0, Kcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  6772. ext_factor, attn_factor, beta_fast, beta_slow);
  6773. cb(Qcur, "Qcur", il);
  6774. cb(Kcur, "Kcur", il);
  6775. cb(Vcur, "Vcur", il);
  6776. cur = build_attn(inp_attn, model.layers[il].wo, model.layers[il].bo, Qcur, Kcur, Vcur, nullptr,
  6777. nullptr, 1.0f / sqrtf(float(n_embd_head)), il);
  6778. }
  6779. if (il == n_layer - 1 && inp_out_ids) {
  6780. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  6781. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  6782. }
  6783. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  6784. cb(ffn_inp, "ffn_inp", il);
  6785. // feed-forward network
  6786. cur = build_norm(ffn_inp, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);
  6787. cb(cur, "ffn_norm", il);
  6788. cur = build_ffn(cur, model.layers[il].ffn_up, NULL, NULL, model.layers[il].ffn_gate, NULL, NULL,
  6789. model.layers[il].ffn_down, NULL, NULL, NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
  6790. cb(cur, "ffn_out", il);
  6791. cur = ggml_add(ctx0, cur, ffn_inp);
  6792. cur = build_cvec(cur, il);
  6793. cb(cur, "l_out", il);
  6794. // input for next layer
  6795. inpL = cur;
  6796. }
  6797. cur = inpL;
  6798. cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1);
  6799. cb(cur, "result_norm", -1);
  6800. res->t_embd = cur;
  6801. // lm_head
  6802. cur = build_lora_mm(model.output, cur);
  6803. cb(cur, "result_output", -1);
  6804. res->t_logits = cur;
  6805. ggml_build_forward_expand(gf, cur);
  6806. }
  6807. };
  6808. struct llm_build_llada : public llm_graph_context {
  6809. llm_build_llada(const llama_model & model, const llm_graph_params & params) :
  6810. llm_graph_context(params) {
  6811. // LLaDA is similar to LLaMA but uses non-causal attention for diffusion
  6812. const int64_t n_embd_head = hparams.n_embd_head_v;
  6813. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  6814. GGML_ASSERT(n_embd_head == hparams.n_rot);
  6815. ggml_tensor * cur;
  6816. ggml_tensor * inpL;
  6817. inpL = build_inp_embd(model.tok_embd);
  6818. // inp_pos - contains the positions
  6819. ggml_tensor * inp_pos = build_inp_pos();
  6820. // Non-causal attention for diffusion
  6821. auto * inp_attn = build_attn_inp_no_cache();
  6822. ggml_tensor * inp_out_ids = build_inp_out_ids();
  6823. for (int il = 0; il < n_layer; ++il) {
  6824. ggml_tensor * inpSA = inpL;
  6825. // norm
  6826. cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
  6827. cb(cur, "attn_norm", il);
  6828. // self-attention
  6829. {
  6830. // compute separate Q, K, V projections without bias, matching LLaDALlamaBlock
  6831. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  6832. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  6833. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  6834. cb(Qcur, "Qcur", il);
  6835. cb(Kcur, "Kcur", il);
  6836. cb(Vcur, "Vcur", il);
  6837. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  6838. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  6839. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  6840. Qcur = ggml_rope_ext(ctx0, Qcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  6841. ext_factor, attn_factor, beta_fast, beta_slow);
  6842. Kcur = ggml_rope_ext(ctx0, Kcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  6843. ext_factor, attn_factor, beta_fast, beta_slow);
  6844. cb(Qcur, "Qcur", il);
  6845. cb(Kcur, "Kcur", il);
  6846. cb(Vcur, "Vcur", il);
  6847. cur = build_attn(inp_attn, model.layers[il].wo, NULL, Qcur, Kcur, Vcur, nullptr, nullptr,
  6848. 1.0f / sqrtf(float(n_embd_head)), il);
  6849. }
  6850. if (il == n_layer - 1 && inp_out_ids) {
  6851. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  6852. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  6853. }
  6854. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  6855. cb(ffn_inp, "ffn_inp", il);
  6856. // feed-forward network
  6857. cur = build_norm(ffn_inp, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);
  6858. cb(cur, "ffn_norm", il);
  6859. cur = build_ffn(cur, model.layers[il].ffn_up, NULL, NULL, model.layers[il].ffn_gate, NULL, NULL,
  6860. model.layers[il].ffn_down, NULL, NULL, NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
  6861. cb(cur, "ffn_out", il);
  6862. cur = ggml_add(ctx0, cur, ffn_inp);
  6863. cur = build_cvec(cur, il);
  6864. cb(cur, "l_out", il);
  6865. // input for next layer
  6866. inpL = cur;
  6867. }
  6868. cur = inpL;
  6869. cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1);
  6870. cb(cur, "result_norm", -1);
  6871. res->t_embd = cur;
  6872. // lm_head
  6873. cur = build_lora_mm(model.output, cur);
  6874. cb(cur, "result_output", -1);
  6875. res->t_logits = cur;
  6876. ggml_build_forward_expand(gf, cur);
  6877. }
  6878. };
  6879. struct llm_build_qwen2vl : public llm_graph_context {
  6880. llm_build_qwen2vl(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  6881. const int64_t n_embd_head = hparams.n_embd_head_v;
  6882. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  6883. GGML_ASSERT(n_embd_head == hparams.n_rot);
  6884. ggml_tensor * cur;
  6885. ggml_tensor * inpL;
  6886. inpL = build_inp_embd(model.tok_embd);
  6887. // inp_pos - contains the positions
  6888. ggml_tensor * inp_pos = build_inp_pos();
  6889. auto * inp_attn = build_attn_inp_kv_unified();
  6890. int sections[4];
  6891. std::copy(std::begin(hparams.rope_sections), std::begin(hparams.rope_sections) + 4, sections);
  6892. ggml_tensor * inp_out_ids = build_inp_out_ids();
  6893. for (int il = 0; il < n_layer; ++il) {
  6894. ggml_tensor * inpSA = inpL;
  6895. // norm
  6896. cur = build_norm(inpL,
  6897. model.layers[il].attn_norm, NULL,
  6898. LLM_NORM_RMS, il);
  6899. cb(cur, "attn_norm", il);
  6900. // self-attention
  6901. {
  6902. // compute Q and K and RoPE them
  6903. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  6904. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  6905. cb(Qcur, "Qcur", il);
  6906. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  6907. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  6908. cb(Kcur, "Kcur", il);
  6909. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  6910. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  6911. cb(Vcur, "Vcur", il);
  6912. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  6913. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  6914. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  6915. Qcur = ggml_rope_multi(
  6916. ctx0, Qcur, inp_pos, nullptr,
  6917. n_rot, sections, rope_type, n_ctx_orig, freq_base, freq_scale,
  6918. ext_factor, attn_factor, beta_fast, beta_slow
  6919. );
  6920. Kcur = ggml_rope_multi(
  6921. ctx0, Kcur, inp_pos, nullptr,
  6922. n_rot, sections, rope_type, n_ctx_orig, freq_base, freq_scale,
  6923. ext_factor, attn_factor, beta_fast, beta_slow
  6924. );
  6925. cb(Qcur, "Qcur", il);
  6926. cb(Kcur, "Kcur", il);
  6927. cb(Vcur, "Vcur", il);
  6928. cur = build_attn(inp_attn,
  6929. model.layers[il].wo, model.layers[il].bo,
  6930. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  6931. }
  6932. if (il == n_layer - 1 && inp_out_ids) {
  6933. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  6934. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  6935. }
  6936. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  6937. cb(ffn_inp, "ffn_inp", il);
  6938. // feed-forward network
  6939. cur = build_norm(ffn_inp,
  6940. model.layers[il].ffn_norm, NULL,
  6941. LLM_NORM_RMS, il);
  6942. cb(cur, "ffn_norm", il);
  6943. cur = build_ffn(cur,
  6944. model.layers[il].ffn_up, NULL, NULL,
  6945. model.layers[il].ffn_gate, NULL, NULL,
  6946. model.layers[il].ffn_down, NULL, NULL,
  6947. NULL,
  6948. LLM_FFN_SILU, LLM_FFN_PAR, il);
  6949. cb(cur, "ffn_out", il);
  6950. cur = ggml_add(ctx0, cur, ffn_inp);
  6951. cur = build_cvec(cur, il);
  6952. cb(cur, "l_out", il);
  6953. // input for next layer
  6954. inpL = cur;
  6955. }
  6956. cur = inpL;
  6957. cur = build_norm(cur,
  6958. model.output_norm, NULL,
  6959. LLM_NORM_RMS, -1);
  6960. cb(cur, "result_norm", -1);
  6961. res->t_embd = cur;
  6962. // lm_head
  6963. cur = build_lora_mm(model.output, cur);
  6964. cb(cur, "result_output", -1);
  6965. res->t_logits = cur;
  6966. ggml_build_forward_expand(gf, cur);
  6967. }
  6968. };
  6969. struct llm_build_qwen2moe : public llm_graph_context {
  6970. llm_build_qwen2moe(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  6971. const int64_t n_embd_head = hparams.n_embd_head_v;
  6972. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  6973. GGML_ASSERT(n_embd_head == hparams.n_rot);
  6974. ggml_tensor * cur;
  6975. ggml_tensor * inpL;
  6976. inpL = build_inp_embd(model.tok_embd);
  6977. // inp_pos - contains the positions
  6978. ggml_tensor * inp_pos = build_inp_pos();
  6979. auto * inp_attn = build_attn_inp_kv_unified();
  6980. ggml_tensor * inp_out_ids = build_inp_out_ids();
  6981. for (int il = 0; il < n_layer; ++il) {
  6982. ggml_tensor * inpSA = inpL;
  6983. // norm
  6984. cur = build_norm(inpL,
  6985. model.layers[il].attn_norm, NULL,
  6986. LLM_NORM_RMS, il);
  6987. cb(cur, "attn_norm", il);
  6988. // self_attention
  6989. {
  6990. // compute Q and K and RoPE them
  6991. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  6992. cb(Qcur, "Qcur", il);
  6993. if (model.layers[il].bq) {
  6994. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  6995. cb(Qcur, "Qcur", il);
  6996. }
  6997. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  6998. cb(Kcur, "Kcur", il);
  6999. if (model.layers[il].bk) {
  7000. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  7001. cb(Kcur, "Kcur", il);
  7002. }
  7003. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  7004. cb(Vcur, "Vcur", il);
  7005. if (model.layers[il].bv) {
  7006. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  7007. cb(Vcur, "Vcur", il);
  7008. }
  7009. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  7010. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  7011. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  7012. Qcur = ggml_rope_ext(
  7013. ctx0, Qcur, inp_pos, nullptr,
  7014. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  7015. ext_factor, attn_factor, beta_fast, beta_slow
  7016. );
  7017. Kcur = ggml_rope_ext(
  7018. ctx0, Kcur, inp_pos, nullptr,
  7019. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  7020. ext_factor, attn_factor, beta_fast, beta_slow
  7021. );
  7022. cb(Qcur, "Qcur", il);
  7023. cb(Kcur, "Kcur", il);
  7024. cb(Vcur, "Vcur", il);
  7025. cur = build_attn(inp_attn,
  7026. model.layers[il].wo, model.layers[il].bo,
  7027. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  7028. }
  7029. if (il == n_layer - 1 && inp_out_ids) {
  7030. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  7031. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  7032. }
  7033. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  7034. cb(ffn_inp, "ffn_inp", il);
  7035. // MoE branch
  7036. cur = build_norm(ffn_inp,
  7037. model.layers[il].ffn_norm, NULL,
  7038. LLM_NORM_RMS, il);
  7039. cb(cur, "ffn_norm", il);
  7040. ggml_tensor * moe_out =
  7041. build_moe_ffn(cur,
  7042. model.layers[il].ffn_gate_inp,
  7043. model.layers[il].ffn_up_exps,
  7044. model.layers[il].ffn_gate_exps,
  7045. model.layers[il].ffn_down_exps,
  7046. nullptr,
  7047. n_expert, n_expert_used,
  7048. LLM_FFN_SILU, false,
  7049. false, 0.0,
  7050. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  7051. il);
  7052. cb(moe_out, "ffn_moe_out", il);
  7053. // FFN shared expert
  7054. {
  7055. ggml_tensor * cur_gate_inp = build_lora_mm(model.layers[il].ffn_gate_inp_shexp, cur);
  7056. cb(cur_gate_inp, "ffn_shexp_gate_inp", il);
  7057. // sigmoid
  7058. ggml_tensor * cur_gate = ggml_div(ctx0, ggml_silu(ctx0, cur_gate_inp), cur_gate_inp);
  7059. cb(cur_gate, "ffn_shexp_gate", il);
  7060. ggml_tensor * cur_ffn = build_ffn(cur,
  7061. model.layers[il].ffn_up_shexp, NULL, NULL,
  7062. model.layers[il].ffn_gate_shexp, NULL, NULL,
  7063. model.layers[il].ffn_down_shexp, NULL, NULL,
  7064. NULL,
  7065. LLM_FFN_SILU, LLM_FFN_PAR, il);
  7066. cb(cur_ffn, "ffn_shexp", il);
  7067. ggml_tensor * ffn_shexp_out = ggml_mul(ctx0, cur_ffn, cur_gate);
  7068. cb(ffn_shexp_out, "ffn_shexp_out", il);
  7069. moe_out = ggml_add(ctx0, moe_out, ffn_shexp_out);
  7070. cb(moe_out, "ffn_out", il);
  7071. cur = moe_out;
  7072. }
  7073. cur = ggml_add(ctx0, cur, ffn_inp);
  7074. cur = build_cvec(cur, il);
  7075. cb(cur, "l_out", il);
  7076. // input for next layer
  7077. inpL = cur;
  7078. }
  7079. cur = inpL;
  7080. cur = build_norm(cur,
  7081. model.output_norm, NULL,
  7082. LLM_NORM_RMS, -1);
  7083. cb(cur, "result_norm", -1);
  7084. res->t_embd = cur;
  7085. // lm_head
  7086. cur = build_lora_mm(model.output, cur);
  7087. cb(cur, "result_output", -1);
  7088. res->t_logits = cur;
  7089. ggml_build_forward_expand(gf, cur);
  7090. }
  7091. };
  7092. struct llm_build_qwen3 : public llm_graph_context {
  7093. llm_build_qwen3(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  7094. const int64_t n_embd_head = hparams.n_embd_head_v;
  7095. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  7096. GGML_ASSERT(n_embd_head == hparams.n_rot);
  7097. ggml_tensor * cur;
  7098. ggml_tensor * inpL;
  7099. inpL = build_inp_embd(model.tok_embd);
  7100. // inp_pos - contains the positions
  7101. ggml_tensor * inp_pos = build_inp_pos();
  7102. auto * inp_attn = build_attn_inp_kv_unified();
  7103. ggml_tensor * inp_out_ids = build_inp_out_ids();
  7104. for (int il = 0; il < n_layer; ++il) {
  7105. ggml_tensor * inpSA = inpL;
  7106. // norm
  7107. cur = build_norm(inpL,
  7108. model.layers[il].attn_norm, NULL,
  7109. LLM_NORM_RMS, il);
  7110. cb(cur, "attn_norm", il);
  7111. // self-attention
  7112. {
  7113. // compute Q and K and RoPE them
  7114. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  7115. cb(Qcur, "Qcur", il);
  7116. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  7117. cb(Kcur, "Kcur", il);
  7118. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  7119. cb(Vcur, "Vcur", il);
  7120. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  7121. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  7122. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  7123. Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);
  7124. cb(Qcur, "Qcur_normed", il);
  7125. Qcur = ggml_rope_ext(
  7126. ctx0, Qcur, inp_pos, nullptr,
  7127. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  7128. ext_factor, attn_factor, beta_fast, beta_slow
  7129. );
  7130. Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);
  7131. cb(Kcur, "Kcur_normed", il);
  7132. Kcur = ggml_rope_ext(
  7133. ctx0, Kcur, inp_pos, nullptr,
  7134. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  7135. ext_factor, attn_factor, beta_fast, beta_slow
  7136. );
  7137. cb(Qcur, "Qcur", il);
  7138. cb(Kcur, "Kcur", il);
  7139. cb(Vcur, "Vcur", il);
  7140. cur = build_attn(inp_attn,
  7141. model.layers[il].wo, model.layers[il].bo,
  7142. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  7143. }
  7144. if (il == n_layer - 1 && inp_out_ids) {
  7145. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  7146. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  7147. }
  7148. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  7149. cb(ffn_inp, "ffn_inp", il);
  7150. // feed-forward network
  7151. cur = build_norm(ffn_inp,
  7152. model.layers[il].ffn_norm, NULL,
  7153. LLM_NORM_RMS, il);
  7154. cb(cur, "ffn_norm", il);
  7155. cur = build_ffn(cur,
  7156. model.layers[il].ffn_up, NULL, NULL,
  7157. model.layers[il].ffn_gate, NULL, NULL,
  7158. model.layers[il].ffn_down, NULL, NULL,
  7159. NULL,
  7160. LLM_FFN_SILU, LLM_FFN_PAR, il);
  7161. cb(cur, "ffn_out", il);
  7162. cur = ggml_add(ctx0, cur, ffn_inp);
  7163. cur = build_cvec(cur, il);
  7164. cb(cur, "l_out", il);
  7165. // input for next layer
  7166. inpL = cur;
  7167. }
  7168. cur = inpL;
  7169. cur = build_norm(cur,
  7170. model.output_norm, NULL,
  7171. LLM_NORM_RMS, -1);
  7172. cb(cur, "result_norm", -1);
  7173. res->t_embd = cur;
  7174. // lm_head
  7175. cur = build_lora_mm(model.output, cur);
  7176. cb(cur, "result_output", -1);
  7177. res->t_logits = cur;
  7178. ggml_build_forward_expand(gf, cur);
  7179. }
  7180. };
  7181. struct llm_build_qwen3moe : public llm_graph_context {
  7182. llm_build_qwen3moe(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  7183. const int64_t n_embd_head = hparams.n_embd_head_v;
  7184. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  7185. GGML_ASSERT(n_embd_head == hparams.n_rot);
  7186. ggml_tensor * cur;
  7187. ggml_tensor * inpL;
  7188. inpL = build_inp_embd(model.tok_embd);
  7189. // inp_pos - contains the positions
  7190. ggml_tensor * inp_pos = build_inp_pos();
  7191. auto * inp_attn = build_attn_inp_kv_unified();
  7192. ggml_tensor * inp_out_ids = build_inp_out_ids();
  7193. for (int il = 0; il < n_layer; ++il) {
  7194. ggml_tensor * inpSA = inpL;
  7195. // norm
  7196. cur = build_norm(inpL,
  7197. model.layers[il].attn_norm, NULL,
  7198. LLM_NORM_RMS, il);
  7199. cb(cur, "attn_norm", il);
  7200. // self_attention
  7201. {
  7202. // compute Q and K and RoPE them
  7203. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  7204. cb(Qcur, "Qcur", il);
  7205. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  7206. cb(Kcur, "Kcur", il);
  7207. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  7208. cb(Vcur, "Vcur", il);
  7209. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  7210. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  7211. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  7212. Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);
  7213. cb(Qcur, "Qcur_normed", il);
  7214. Qcur = ggml_rope_ext(
  7215. ctx0, Qcur, inp_pos, nullptr,
  7216. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  7217. ext_factor, attn_factor, beta_fast, beta_slow
  7218. );
  7219. Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);
  7220. cb(Kcur, "Kcur_normed", il);
  7221. Kcur = ggml_rope_ext(
  7222. ctx0, Kcur, inp_pos, nullptr,
  7223. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  7224. ext_factor, attn_factor, beta_fast, beta_slow
  7225. );
  7226. cb(Qcur, "Qcur", il);
  7227. cb(Kcur, "Kcur", il);
  7228. cb(Vcur, "Vcur", il);
  7229. cur = build_attn(inp_attn,
  7230. model.layers[il].wo, model.layers[il].bo,
  7231. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  7232. }
  7233. if (il == n_layer - 1 && inp_out_ids) {
  7234. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  7235. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  7236. }
  7237. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  7238. cb(ffn_inp, "ffn_inp", il);
  7239. // MoE branch
  7240. cur = build_norm(ffn_inp,
  7241. model.layers[il].ffn_norm, NULL,
  7242. LLM_NORM_RMS, il);
  7243. cb(cur, "ffn_norm", il);
  7244. ggml_tensor * moe_out =
  7245. build_moe_ffn(cur,
  7246. model.layers[il].ffn_gate_inp,
  7247. model.layers[il].ffn_up_exps,
  7248. model.layers[il].ffn_gate_exps,
  7249. model.layers[il].ffn_down_exps,
  7250. nullptr,
  7251. n_expert, n_expert_used,
  7252. LLM_FFN_SILU, true,
  7253. false, 0.0,
  7254. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  7255. il);
  7256. cb(moe_out, "ffn_moe_out", il);
  7257. cur = moe_out;
  7258. cur = ggml_add(ctx0, cur, ffn_inp);
  7259. cur = build_cvec(cur, il);
  7260. cb(cur, "l_out", il);
  7261. // input for next layer
  7262. inpL = cur;
  7263. }
  7264. cur = inpL;
  7265. cur = build_norm(cur,
  7266. model.output_norm, NULL,
  7267. LLM_NORM_RMS, -1);
  7268. cb(cur, "result_norm", -1);
  7269. res->t_embd = cur;
  7270. // lm_head
  7271. cur = build_lora_mm(model.output, cur);
  7272. cb(cur, "result_output", -1);
  7273. res->t_logits = cur;
  7274. ggml_build_forward_expand(gf, cur);
  7275. }
  7276. };
  7277. struct llm_build_phi2 : public llm_graph_context {
  7278. llm_build_phi2(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  7279. const int64_t n_embd_head = hparams.n_embd_head_v;
  7280. const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  7281. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  7282. ggml_tensor * cur;
  7283. ggml_tensor * attn_norm_output;
  7284. ggml_tensor * ffn_output;
  7285. ggml_tensor * inpL;
  7286. inpL = build_inp_embd(model.tok_embd);
  7287. // inp_pos - contains the positions
  7288. ggml_tensor * inp_pos = build_inp_pos();
  7289. auto * inp_attn = build_attn_inp_kv_unified();
  7290. ggml_tensor * inp_out_ids = build_inp_out_ids();
  7291. for (int il = 0; il < n_layer; ++il) {
  7292. attn_norm_output = build_norm(inpL,
  7293. model.layers[il].attn_norm,
  7294. model.layers[il].attn_norm_b,
  7295. LLM_NORM, il);
  7296. cb(attn_norm_output, "attn_norm", il);
  7297. // self-attention
  7298. {
  7299. ggml_tensor * Qcur = nullptr;
  7300. ggml_tensor * Kcur = nullptr;
  7301. ggml_tensor * Vcur = nullptr;
  7302. if (model.layers[il].wqkv) {
  7303. cur = build_lora_mm(model.layers[il].wqkv, attn_norm_output);
  7304. cb(cur, "wqkv", il);
  7305. cur = ggml_add(ctx0, cur, model.layers[il].bqkv);
  7306. cb(cur, "bqkv", il);
  7307. 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));
  7308. 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));
  7309. Vcur = ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd + n_embd_gqa));
  7310. Vcur = ggml_cont_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  7311. } else {
  7312. Qcur = ggml_add(ctx0, build_lora_mm(model.layers[il].wq, attn_norm_output), model.layers[il].bq);
  7313. Kcur = ggml_add(ctx0, build_lora_mm(model.layers[il].wk, attn_norm_output), model.layers[il].bk);
  7314. Vcur = ggml_add(ctx0, build_lora_mm(model.layers[il].wv, attn_norm_output), model.layers[il].bv);
  7315. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  7316. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  7317. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  7318. }
  7319. cb(Qcur, "Qcur", il);
  7320. cb(Kcur, "Kcur", il);
  7321. cb(Vcur, "Vcur", il);
  7322. Qcur = ggml_rope_ext(
  7323. ctx0, Qcur, inp_pos, nullptr,
  7324. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  7325. ext_factor, attn_factor, beta_fast, beta_slow
  7326. );
  7327. Kcur = ggml_rope_ext(
  7328. ctx0, Kcur, inp_pos, nullptr,
  7329. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  7330. ext_factor, attn_factor, beta_fast, beta_slow
  7331. );
  7332. cb(Qcur, "Qcur", il);
  7333. cb(Kcur, "Kcur", il);
  7334. cb(Vcur, "Vcur", il);
  7335. // with phi2, we scale the Q to avoid precision issues
  7336. // ref: https://github.com/ml-explore/mlx-examples/blob/08e862336ade809bc37d1035f94b359e7d1a5152/phi2/phi2.py#L64-L66
  7337. Qcur = ggml_scale(ctx0, Qcur, 1.0f/sqrtf(float(n_embd_head)));
  7338. cur = build_attn(inp_attn,
  7339. model.layers[il].wo, model.layers[il].bo,
  7340. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f, il);
  7341. }
  7342. if (il == n_layer - 1 && inp_out_ids) {
  7343. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  7344. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  7345. attn_norm_output = ggml_get_rows(ctx0, attn_norm_output, inp_out_ids);
  7346. }
  7347. // FF
  7348. {
  7349. ffn_output = build_ffn(attn_norm_output,
  7350. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  7351. NULL, NULL, NULL,
  7352. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  7353. NULL,
  7354. LLM_FFN_GELU, LLM_FFN_SEQ, il);
  7355. cb(ffn_output, "ffn_out", il);
  7356. }
  7357. cur = ggml_add(ctx0, cur, ffn_output);
  7358. cur = ggml_add(ctx0, cur, inpL);
  7359. cur = build_cvec(cur, il);
  7360. cb(cur, "l_out", il);
  7361. // input for next layer
  7362. inpL = cur;
  7363. }
  7364. cur = build_norm(inpL,
  7365. model.output_norm,
  7366. model.output_norm_b,
  7367. LLM_NORM, -1);
  7368. cb(cur, "result_norm", -1);
  7369. res->t_embd = cur;
  7370. cur = build_lora_mm(model.output, cur);
  7371. cb(cur, "result_output_no_bias", -1);
  7372. cur = ggml_add(ctx0, cur, model.output_b);
  7373. cb(cur, "result_output", -1);
  7374. res->t_logits = cur;
  7375. ggml_build_forward_expand(gf, cur);
  7376. }
  7377. };
  7378. template<bool iswa>
  7379. struct llm_build_phi3 : public llm_graph_context {
  7380. llm_build_phi3(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  7381. const int64_t n_embd_head = hparams.n_embd_head_v;
  7382. const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  7383. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  7384. ggml_tensor * cur;
  7385. ggml_tensor * inpL;
  7386. inpL = build_inp_embd(model.tok_embd);
  7387. // inp_pos - contains the positions
  7388. ggml_tensor * inp_pos = build_inp_pos();
  7389. using inp_attn_type = std::conditional_t<iswa, llm_graph_input_attn_kv_unified_iswa, llm_graph_input_attn_kv_unified>;
  7390. inp_attn_type * inp_attn = nullptr;
  7391. if constexpr (iswa) {
  7392. inp_attn = build_attn_inp_kv_unified_iswa();
  7393. } else {
  7394. inp_attn = build_attn_inp_kv_unified();
  7395. }
  7396. ggml_tensor * inp_out_ids = build_inp_out_ids();
  7397. for (int il = 0; il < n_layer; ++il) {
  7398. auto * residual = inpL;
  7399. // self-attention
  7400. {
  7401. // rope freq factors for 128k context
  7402. ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
  7403. ggml_tensor* attn_norm_output = build_norm(inpL,
  7404. model.layers[il].attn_norm,
  7405. model.layers[il].attn_norm_b,
  7406. LLM_NORM_RMS, il);
  7407. cb(attn_norm_output, "attn_norm", il);
  7408. ggml_tensor * Qcur = nullptr;
  7409. ggml_tensor * Kcur = nullptr;
  7410. ggml_tensor * Vcur = nullptr;
  7411. if (model.layers[il].wqkv) {
  7412. cur = build_lora_mm(model.layers[il].wqkv, attn_norm_output);
  7413. cb(cur, "wqkv", il);
  7414. 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));
  7415. 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));
  7416. Vcur = ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1 * sizeof(float) * (n_embd + n_embd_gqa));
  7417. Vcur = ggml_cont_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  7418. } else {
  7419. Qcur = ggml_add(ctx0, build_lora_mm(model.layers[il].wq, attn_norm_output), model.layers[il].bq);
  7420. Kcur = ggml_add(ctx0, build_lora_mm(model.layers[il].wk, attn_norm_output), model.layers[il].bk);
  7421. Vcur = ggml_add(ctx0, build_lora_mm(model.layers[il].wv, attn_norm_output), model.layers[il].bv);
  7422. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  7423. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  7424. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  7425. }
  7426. cb(Qcur, "Qcur", il);
  7427. cb(Kcur, "Kcur", il);
  7428. cb(Vcur, "Vcur", il);
  7429. Qcur = ggml_rope_ext(
  7430. ctx0, Qcur, inp_pos, rope_factors,
  7431. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  7432. ext_factor, attn_factor, beta_fast, beta_slow
  7433. );
  7434. Kcur = ggml_rope_ext(
  7435. ctx0, Kcur, inp_pos, rope_factors,
  7436. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  7437. ext_factor, attn_factor, beta_fast, beta_slow
  7438. );
  7439. cb(Qcur, "Qcur", il);
  7440. cb(Kcur, "Kcur", il);
  7441. cb(Vcur, "Vcur", il);
  7442. Qcur = ggml_scale(ctx0, Qcur, 1.0f / sqrtf(float(n_embd_head)));
  7443. cb(Qcur, "Qcur", il);
  7444. cur = build_attn(inp_attn,
  7445. model.layers[il].wo, model.layers[il].bo,
  7446. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f, il);
  7447. }
  7448. if (il == n_layer - 1 && inp_out_ids) {
  7449. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  7450. residual = ggml_get_rows(ctx0, residual, inp_out_ids);
  7451. }
  7452. cur = ggml_add(ctx0, cur, residual);
  7453. residual = cur;
  7454. cur = build_norm(cur,
  7455. model.layers[il].ffn_norm, model.layers[il].ffn_norm_b,
  7456. LLM_NORM_RMS, il);
  7457. cb(cur, "ffn_norm", il);
  7458. // feed-forward network
  7459. if (model.layers[il].ffn_gate_inp == nullptr) {
  7460. cur = build_ffn(cur,
  7461. model.layers[il].ffn_up, NULL, NULL,
  7462. NULL, NULL, NULL,
  7463. model.layers[il].ffn_down, NULL, NULL,
  7464. NULL,
  7465. LLM_FFN_SWIGLU, LLM_FFN_SEQ, il);
  7466. cb(cur, "ffn_out", il);
  7467. } else {
  7468. // MoE branch
  7469. cur = build_moe_ffn(cur,
  7470. model.layers[il].ffn_gate_inp,
  7471. model.layers[il].ffn_up_exps,
  7472. model.layers[il].ffn_gate_exps,
  7473. model.layers[il].ffn_down_exps,
  7474. nullptr,
  7475. n_expert, n_expert_used,
  7476. LLM_FFN_SILU, true,
  7477. false, 0.0,
  7478. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  7479. il);
  7480. cb(cur, "ffn_moe_out", il);
  7481. }
  7482. cur = ggml_add(ctx0, residual, cur);
  7483. cur = build_cvec(cur, il);
  7484. cb(cur, "l_out", il);
  7485. // input for next layer
  7486. inpL = cur;
  7487. }
  7488. cur = build_norm(inpL,
  7489. model.output_norm,
  7490. model.output_norm_b,
  7491. LLM_NORM_RMS, -1);
  7492. cb(cur, "result_norm", -1);
  7493. res->t_embd = cur;
  7494. cur = build_lora_mm(model.output, cur);
  7495. if (model.output_b != nullptr) {
  7496. cb(cur, "result_output_no_bias", -1);
  7497. cur = ggml_add(ctx0, cur, model.output_b);
  7498. }
  7499. cb(cur, "result_output", -1);
  7500. res->t_logits = cur;
  7501. ggml_build_forward_expand(gf, cur);
  7502. }
  7503. };
  7504. struct llm_build_plamo : public llm_graph_context {
  7505. llm_build_plamo(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  7506. const int64_t n_embd_head = hparams.n_embd_head_v;
  7507. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  7508. GGML_ASSERT(n_embd_head == hparams.n_rot);
  7509. ggml_tensor * cur;
  7510. ggml_tensor * inpL;
  7511. inpL = build_inp_embd(model.tok_embd);
  7512. // inp_pos - contains the positions
  7513. ggml_tensor * inp_pos = build_inp_pos();
  7514. auto * inp_attn = build_attn_inp_kv_unified();
  7515. ggml_tensor * inp_out_ids = build_inp_out_ids();
  7516. for (int il = 0; il < n_layer; ++il) {
  7517. // norm
  7518. cur = build_norm(inpL,
  7519. model.layers[il].attn_norm, NULL,
  7520. LLM_NORM_RMS, il);
  7521. cb(cur, "attn_norm", il);
  7522. ggml_tensor * sa_inp = cur;
  7523. // self-attention
  7524. {
  7525. // compute Q and K and RoPE them
  7526. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  7527. cb(Qcur, "Qcur", il);
  7528. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  7529. cb(Kcur, "Kcur", il);
  7530. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  7531. cb(Vcur, "Vcur", il);
  7532. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  7533. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  7534. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  7535. Qcur = ggml_rope_ext(
  7536. ctx0, Qcur, inp_pos, nullptr,
  7537. n_embd_head, rope_type, n_ctx_orig, freq_base, freq_scale,
  7538. ext_factor, attn_factor, beta_fast, beta_slow
  7539. );
  7540. Kcur = ggml_rope_ext(
  7541. ctx0, Kcur, inp_pos, nullptr,
  7542. n_embd_head, rope_type, n_ctx_orig, freq_base, freq_scale,
  7543. ext_factor, attn_factor, beta_fast, beta_slow
  7544. );
  7545. cb(Qcur, "Qcur", il);
  7546. cb(Kcur, "Kcur", il);
  7547. cb(Vcur, "Vcur", il);
  7548. cur = build_attn(inp_attn,
  7549. model.layers[il].wo, NULL,
  7550. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  7551. }
  7552. if (il == n_layer - 1 && inp_out_ids) {
  7553. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  7554. sa_inp = ggml_get_rows(ctx0, sa_inp, inp_out_ids);
  7555. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  7556. }
  7557. ggml_tensor * sa_out = cur;
  7558. cur = sa_inp;
  7559. // feed-forward network
  7560. {
  7561. cur = build_ffn(cur,
  7562. model.layers[il].ffn_up, NULL, NULL,
  7563. model.layers[il].ffn_gate, NULL, NULL,
  7564. model.layers[il].ffn_down, NULL, NULL,
  7565. NULL,
  7566. LLM_FFN_SILU, LLM_FFN_PAR, il);
  7567. cb(cur, "ffn_out", il);
  7568. }
  7569. cur = ggml_add(ctx0, cur, sa_out);
  7570. cur = ggml_add(ctx0, cur, inpL);
  7571. cur = build_cvec(cur, il);
  7572. cb(cur, "l_out", il);
  7573. // input for next layer
  7574. inpL = cur;
  7575. }
  7576. cur = inpL;
  7577. cur = build_norm(cur,
  7578. model.output_norm, NULL,
  7579. LLM_NORM_RMS, -1);
  7580. cb(cur, "result_norm", -1);
  7581. res->t_embd = cur;
  7582. // lm_head
  7583. cur = build_lora_mm(model.output, cur);
  7584. cb(cur, "result_output", -1);
  7585. res->t_logits = cur;
  7586. ggml_build_forward_expand(gf, cur);
  7587. }
  7588. };
  7589. struct llm_build_gpt2 : public llm_graph_context {
  7590. llm_build_gpt2(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  7591. const int64_t n_embd_head = hparams.n_embd_head_v;
  7592. const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  7593. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  7594. ggml_tensor * cur;
  7595. ggml_tensor * pos;
  7596. ggml_tensor * inpL;
  7597. inpL = build_inp_embd(model.tok_embd);
  7598. // inp_pos - contains the positions
  7599. ggml_tensor * inp_pos = build_inp_pos();
  7600. auto * inp_attn = build_attn_inp_kv_unified();
  7601. pos = ggml_get_rows(ctx0, model.pos_embd, inp_pos);
  7602. cb(pos, "pos_embd", -1);
  7603. inpL = ggml_add(ctx0, inpL, pos);
  7604. cb(inpL, "inpL", -1);
  7605. ggml_tensor * inp_out_ids = build_inp_out_ids();
  7606. for (int il = 0; il < n_layer; ++il) {
  7607. cur = build_norm(inpL,
  7608. model.layers[il].attn_norm,
  7609. model.layers[il].attn_norm_b,
  7610. LLM_NORM, il);
  7611. cb(cur, "attn_norm", il);
  7612. // self-attention
  7613. {
  7614. cur = build_lora_mm(model.layers[il].wqkv, cur);
  7615. cb(cur, "wqkv", il);
  7616. cur = ggml_add(ctx0, cur, model.layers[il].bqkv);
  7617. cb(cur, "bqkv", il);
  7618. ggml_tensor * Qcur = ggml_view_2d(ctx0, cur, n_embd, n_tokens, cur->nb[1], 0*sizeof(float)*(n_embd));
  7619. ggml_tensor * Kcur = ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd));
  7620. ggml_tensor * Vcur = ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd + n_embd_gqa));
  7621. cb(Qcur, "Qcur", il);
  7622. cb(Kcur, "Kcur", il);
  7623. cb(Vcur, "Vcur", il);
  7624. Qcur = ggml_cont_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  7625. Kcur = ggml_cont_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  7626. Vcur = ggml_cont_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  7627. cur = build_attn(inp_attn,
  7628. model.layers[il].wo, model.layers[il].bo,
  7629. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  7630. }
  7631. if (il == n_layer - 1 && inp_out_ids) {
  7632. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  7633. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  7634. }
  7635. // add the input
  7636. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
  7637. cb(ffn_inp, "ffn_inp", il);
  7638. // FF
  7639. {
  7640. cur = build_norm(ffn_inp,
  7641. model.layers[il].ffn_norm,
  7642. model.layers[il].ffn_norm_b,
  7643. LLM_NORM, il);
  7644. cb(cur, "ffn_norm", il);
  7645. cur = build_ffn(cur,
  7646. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  7647. NULL, NULL, NULL,
  7648. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  7649. NULL,
  7650. LLM_FFN_GELU, LLM_FFN_SEQ, il);
  7651. cb(cur, "ffn_out", il);
  7652. }
  7653. cur = ggml_add(ctx0, cur, ffn_inp);
  7654. cur = build_cvec(cur, il);
  7655. cb(cur, "l_out", il);
  7656. // input for next layer
  7657. inpL = cur;
  7658. }
  7659. cur = build_norm(inpL,
  7660. model.output_norm,
  7661. model.output_norm_b,
  7662. LLM_NORM, -1);
  7663. cb(cur, "result_norm", -1);
  7664. res->t_embd = cur;
  7665. cur = build_lora_mm(model.output, cur);
  7666. cb(cur, "result_output", -1);
  7667. res->t_logits = cur;
  7668. ggml_build_forward_expand(gf, cur);
  7669. }
  7670. };
  7671. struct llm_build_codeshell : public llm_graph_context {
  7672. llm_build_codeshell(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  7673. const int64_t n_embd_head = hparams.n_embd_head_v;
  7674. const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  7675. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  7676. GGML_ASSERT(n_embd_head == hparams.n_rot);
  7677. ggml_tensor * cur;
  7678. ggml_tensor * inpL;
  7679. inpL = build_inp_embd(model.tok_embd);
  7680. // inp_pos - contains the positions
  7681. ggml_tensor * inp_pos = build_inp_pos();
  7682. auto * inp_attn = build_attn_inp_kv_unified();
  7683. ggml_tensor * inp_out_ids = build_inp_out_ids();
  7684. for (int il = 0; il < n_layer; ++il) {
  7685. cur = build_norm(inpL,
  7686. model.layers[il].attn_norm,
  7687. model.layers[il].attn_norm_b,
  7688. LLM_NORM, il);
  7689. cb(cur, "attn_norm", il);
  7690. // self-attention
  7691. {
  7692. cur = build_lora_mm(model.layers[il].wqkv, cur);
  7693. cb(cur, "wqkv", il);
  7694. cur = ggml_add(ctx0, cur, model.layers[il].bqkv);
  7695. cb(cur, "bqkv", il);
  7696. 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));
  7697. 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));
  7698. ggml_tensor * Vcur = ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd + n_embd_gqa));
  7699. Vcur = ggml_cont_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  7700. Qcur = ggml_rope_ext(
  7701. ctx0, Qcur, inp_pos, nullptr,
  7702. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  7703. ext_factor, attn_factor, beta_fast, beta_slow
  7704. );
  7705. Kcur = ggml_rope_ext(
  7706. ctx0, Kcur, inp_pos, nullptr,
  7707. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  7708. ext_factor, attn_factor, beta_fast, beta_slow
  7709. );
  7710. cb(Qcur, "Qcur", il);
  7711. cb(Kcur, "Kcur", il);
  7712. cb(Vcur, "Vcur", il);
  7713. cur = build_attn(inp_attn,
  7714. model.layers[il].wo, model.layers[il].bo,
  7715. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  7716. }
  7717. if (il == n_layer - 1 && inp_out_ids) {
  7718. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  7719. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  7720. }
  7721. // add the input
  7722. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
  7723. cb(ffn_inp, "ffn_inp", il);
  7724. // FF
  7725. {
  7726. cur = build_norm(ffn_inp,
  7727. model.layers[il].ffn_norm,
  7728. model.layers[il].ffn_norm_b,
  7729. LLM_NORM, il);
  7730. cb(cur, "ffn_norm", il);
  7731. cur = build_ffn(cur,
  7732. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  7733. NULL, NULL, NULL,
  7734. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  7735. NULL,
  7736. LLM_FFN_GELU, LLM_FFN_SEQ, il);
  7737. cb(cur, "ffn_out", il);
  7738. }
  7739. cur = ggml_add(ctx0, cur, ffn_inp);
  7740. cur = build_cvec(cur, il);
  7741. cb(cur, "l_out", il);
  7742. // input for next layer
  7743. inpL = cur;
  7744. }
  7745. cur = build_norm(inpL,
  7746. model.output_norm,
  7747. model.output_norm_b,
  7748. LLM_NORM, -1);
  7749. cb(cur, "result_norm", -1);
  7750. res->t_embd = cur;
  7751. cur = build_lora_mm(model.output, cur);
  7752. cb(cur, "result_output", -1);
  7753. res->t_logits = cur;
  7754. ggml_build_forward_expand(gf, cur);
  7755. }
  7756. };
  7757. struct llm_build_orion : public llm_graph_context {
  7758. llm_build_orion(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  7759. const int64_t n_embd_head = hparams.n_embd_head_v;
  7760. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  7761. GGML_ASSERT(n_embd_head == hparams.n_rot);
  7762. ggml_tensor * cur;
  7763. ggml_tensor * inpL;
  7764. inpL = build_inp_embd(model.tok_embd);
  7765. // inp_pos - contains the positions
  7766. ggml_tensor * inp_pos = build_inp_pos();
  7767. auto * inp_attn = build_attn_inp_kv_unified();
  7768. ggml_tensor * inp_out_ids = build_inp_out_ids();
  7769. for (int il = 0; il < n_layer; ++il) {
  7770. ggml_tensor * inpSA = inpL;
  7771. // norm
  7772. cur = build_norm(inpL,
  7773. model.layers[il].attn_norm, model.layers[il].attn_norm_b,
  7774. LLM_NORM, il);
  7775. cb(cur, "attn_norm", il);
  7776. // self-attention
  7777. {
  7778. // compute Q and K and RoPE them
  7779. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  7780. cb(Qcur, "Qcur", il);
  7781. // if (model.layers[il].bq) {
  7782. // Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  7783. // cb(Qcur, "Qcur", il);
  7784. // }
  7785. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  7786. cb(Kcur, "Kcur", il);
  7787. // if (model.layers[il].bk) {
  7788. // Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  7789. // cb(Kcur, "Kcur", il);
  7790. // }
  7791. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  7792. cb(Vcur, "Vcur", il);
  7793. // if (model.layers[il].bv) {
  7794. // Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  7795. // cb(Vcur, "Vcur", il);
  7796. // }
  7797. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  7798. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  7799. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  7800. Qcur = ggml_rope_ext(
  7801. ctx0, Qcur, inp_pos, nullptr,
  7802. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  7803. ext_factor, attn_factor, beta_fast, beta_slow
  7804. );
  7805. Kcur = ggml_rope_ext(
  7806. ctx0, Kcur, inp_pos, nullptr,
  7807. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  7808. ext_factor, attn_factor, beta_fast, beta_slow
  7809. );
  7810. cb(Qcur, "Qcur", il);
  7811. cb(Kcur, "Kcur", il);
  7812. cb(Vcur, "Vcur", il);
  7813. cur = build_attn(inp_attn,
  7814. model.layers[il].wo, NULL,
  7815. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  7816. }
  7817. if (il == n_layer - 1 && inp_out_ids) {
  7818. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  7819. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  7820. }
  7821. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  7822. cb(ffn_inp, "ffn_inp", il);
  7823. // feed-forward network
  7824. cur = build_norm(ffn_inp,
  7825. model.layers[il].ffn_norm, model.layers[il].ffn_norm_b,
  7826. LLM_NORM, il);
  7827. cb(cur, "ffn_norm", il);
  7828. cur = build_ffn(cur,
  7829. model.layers[il].ffn_up, NULL, NULL,
  7830. model.layers[il].ffn_gate, NULL, NULL,
  7831. model.layers[il].ffn_down, NULL, NULL,
  7832. NULL,
  7833. LLM_FFN_SILU, LLM_FFN_PAR, il);
  7834. cb(cur, "ffn_out", il);
  7835. cur = ggml_add(ctx0, cur, ffn_inp);
  7836. cur = build_cvec(cur, il);
  7837. cb(cur, "l_out", il);
  7838. // input for next layer
  7839. inpL = cur;
  7840. }
  7841. cur = inpL;
  7842. cur = build_norm(cur,
  7843. model.output_norm, model.output_norm_b,
  7844. LLM_NORM, -1);
  7845. cb(cur, "result_norm", -1);
  7846. res->t_embd = cur;
  7847. // lm_head
  7848. cur = build_lora_mm(model.output, cur);
  7849. cb(cur, "result_output", -1);
  7850. res->t_logits = cur;
  7851. ggml_build_forward_expand(gf, cur);
  7852. }
  7853. };
  7854. struct llm_build_internlm2 : public llm_graph_context {
  7855. llm_build_internlm2(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  7856. const int64_t n_embd_head = hparams.n_embd_head_v;
  7857. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  7858. GGML_ASSERT(n_embd_head == hparams.n_rot);
  7859. ggml_tensor * cur;
  7860. ggml_tensor * inpL;
  7861. inpL = build_inp_embd(model.tok_embd);
  7862. // inp_pos - contains the positions
  7863. ggml_tensor * inp_pos = build_inp_pos();
  7864. auto * inp_attn = build_attn_inp_kv_unified();
  7865. ggml_tensor * inp_out_ids = build_inp_out_ids();
  7866. for (int il = 0; il < n_layer; ++il) {
  7867. ggml_tensor * inpSA = inpL;
  7868. // norm
  7869. cur = build_norm(inpL,
  7870. model.layers[il].attn_norm, NULL,
  7871. LLM_NORM_RMS, il);
  7872. cb(cur, "attn_norm", il);
  7873. // self-attention
  7874. {
  7875. // compute Q and K and RoPE them
  7876. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  7877. cb(Qcur, "Qcur", il);
  7878. if (model.layers[il].bq) {
  7879. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  7880. cb(Qcur, "Qcur", il);
  7881. }
  7882. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  7883. cb(Kcur, "Kcur", il);
  7884. if (model.layers[il].bk) {
  7885. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  7886. cb(Kcur, "Kcur", il);
  7887. }
  7888. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  7889. cb(Vcur, "Vcur", il);
  7890. if (model.layers[il].bv) {
  7891. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  7892. cb(Vcur, "Vcur", il);
  7893. }
  7894. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  7895. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  7896. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  7897. Qcur = ggml_rope_ext(
  7898. ctx0, Qcur, inp_pos, nullptr,
  7899. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  7900. ext_factor, attn_factor, beta_fast, beta_slow
  7901. );
  7902. Kcur = ggml_rope_ext(
  7903. ctx0, Kcur, inp_pos, nullptr,
  7904. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  7905. ext_factor, attn_factor, beta_fast, beta_slow
  7906. );
  7907. cb(Qcur, "Qcur", il);
  7908. cb(Kcur, "Kcur", il);
  7909. cb(Vcur, "Vcur", il);
  7910. cur = build_attn(inp_attn,
  7911. model.layers[il].wo, model.layers[il].bo,
  7912. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  7913. }
  7914. if (il == n_layer - 1 && inp_out_ids) {
  7915. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  7916. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  7917. }
  7918. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  7919. cb(ffn_inp, "ffn_inp", il);
  7920. // feed-forward network
  7921. cur = build_norm(ffn_inp,
  7922. model.layers[il].ffn_norm, NULL,
  7923. LLM_NORM_RMS, il);
  7924. cb(cur, "ffn_norm", il);
  7925. cur = build_ffn(cur,
  7926. model.layers[il].ffn_up, NULL, NULL,
  7927. model.layers[il].ffn_gate, NULL, NULL,
  7928. model.layers[il].ffn_down, NULL, NULL,
  7929. NULL,
  7930. LLM_FFN_SILU, LLM_FFN_PAR, il);
  7931. cb(cur, "ffn_out", il);
  7932. cur = ggml_add(ctx0, cur, ffn_inp);
  7933. cur = build_cvec(cur, il);
  7934. cb(cur, "l_out", il);
  7935. // input for next layer
  7936. inpL = cur;
  7937. }
  7938. cur = inpL;
  7939. cur = build_norm(cur,
  7940. model.output_norm, NULL,
  7941. LLM_NORM_RMS, -1);
  7942. cb(cur, "result_norm", -1);
  7943. res->t_embd = cur;
  7944. // lm_head
  7945. cur = build_lora_mm(model.output, cur);
  7946. cb(cur, "result_output", -1);
  7947. res->t_logits = cur;
  7948. ggml_build_forward_expand(gf, cur);
  7949. }
  7950. };
  7951. struct llm_build_minicpm3 : public llm_graph_context {
  7952. llm_build_minicpm3(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  7953. //TODO: if the model varies, these parameters need to be read from the model
  7954. const int64_t n_embd_base = 256;
  7955. const float scale_embd = 12.0f;
  7956. const float scale_depth = 1.4f;
  7957. const float kq_scale = 1.0f / sqrtf(float(hparams.n_embd_head_k));
  7958. const uint32_t n_embd_head_qk_rope = hparams.n_rot;
  7959. const uint32_t n_embd_head_qk_nope = hparams.n_embd_head_k - hparams.n_rot;
  7960. const uint32_t kv_lora_rank = hparams.n_lora_kv;
  7961. ggml_tensor * cur;
  7962. ggml_tensor * inpL;
  7963. inpL = build_inp_embd(model.tok_embd);
  7964. // scale the input embeddings
  7965. inpL = ggml_scale(ctx0, inpL, scale_embd);
  7966. cb(inpL, "inp_scaled", -1);
  7967. // inp_pos - contains the positions
  7968. ggml_tensor * inp_pos = build_inp_pos();
  7969. auto * inp_attn = build_attn_inp_kv_unified();
  7970. ggml_tensor * inp_out_ids = build_inp_out_ids();
  7971. for (int il = 0; il < n_layer; ++il) {
  7972. ggml_tensor * inpSA = inpL;
  7973. ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
  7974. // norm
  7975. cur = build_norm(inpL,
  7976. model.layers[il].attn_norm, NULL,
  7977. LLM_NORM_RMS, il);
  7978. cb(cur, "attn_norm", il);
  7979. // self_attention
  7980. {
  7981. ggml_tensor * q = NULL;
  7982. // {n_embd, q_lora_rank} * {n_embd, n_tokens} -> {q_lora_rank, n_tokens}
  7983. q = ggml_mul_mat(ctx0, model.layers[il].wq_a, cur);
  7984. cb(q, "q", il);
  7985. q = build_norm(q,
  7986. model.layers[il].attn_q_a_norm, NULL,
  7987. LLM_NORM_RMS, il);
  7988. cb(q, "q", il);
  7989. // {q_lora_rank, n_head * hparams.n_embd_head_k} * {q_lora_rank, n_tokens} -> {n_head * hparams.n_embd_head_k, n_tokens}
  7990. q = ggml_mul_mat(ctx0, model.layers[il].wq_b, q);
  7991. cb(q, "q", il);
  7992. // split into {n_head * n_embd_head_qk_nope, n_tokens}
  7993. ggml_tensor * q_nope = ggml_view_3d(ctx0, q, n_embd_head_qk_nope, n_head, n_tokens,
  7994. ggml_row_size(q->type, hparams.n_embd_head_k),
  7995. ggml_row_size(q->type, hparams.n_embd_head_k * n_head),
  7996. 0);
  7997. cb(q_nope, "q_nope", il);
  7998. // and {n_head * n_embd_head_qk_rope, n_tokens}
  7999. ggml_tensor * q_pe = ggml_view_3d(ctx0, q, n_embd_head_qk_rope, n_head, n_tokens,
  8000. ggml_row_size(q->type, hparams.n_embd_head_k),
  8001. ggml_row_size(q->type, hparams.n_embd_head_k * n_head),
  8002. ggml_row_size(q->type, n_embd_head_qk_nope));
  8003. cb(q_pe, "q_pe", il);
  8004. // {n_embd, kv_lora_rank + n_embd_head_qk_rope} * {n_embd, n_tokens} -> {kv_lora_rank + n_embd_head_qk_rope, n_tokens}
  8005. ggml_tensor * kv_pe_compresseed = ggml_mul_mat(ctx0, model.layers[il].wkv_a_mqa, cur);
  8006. cb(kv_pe_compresseed, "kv_pe_compresseed", il);
  8007. // split into {kv_lora_rank, n_tokens}
  8008. ggml_tensor * kv_compressed = ggml_view_2d(ctx0, kv_pe_compresseed, kv_lora_rank, n_tokens,
  8009. kv_pe_compresseed->nb[1],
  8010. 0);
  8011. cb(kv_compressed, "kv_compressed", il);
  8012. // and {n_embd_head_qk_rope, n_tokens}
  8013. ggml_tensor * k_pe = ggml_view_3d(ctx0, kv_pe_compresseed, n_embd_head_qk_rope, 1, n_tokens,
  8014. kv_pe_compresseed->nb[1],
  8015. kv_pe_compresseed->nb[1],
  8016. ggml_row_size(kv_pe_compresseed->type, kv_lora_rank));
  8017. cb(k_pe, "k_pe", il);
  8018. kv_compressed = build_norm(kv_compressed,
  8019. model.layers[il].attn_kv_a_norm, NULL,
  8020. LLM_NORM_RMS, il);
  8021. cb(kv_compressed, "kv_compressed", il);
  8022. // {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}
  8023. ggml_tensor * kv = ggml_mul_mat(ctx0, model.layers[il].wkv_b, kv_compressed);
  8024. cb(kv, "kv", il);
  8025. // split into {n_head * n_embd_head_qk_nope, n_tokens}
  8026. ggml_tensor * k_nope = ggml_view_3d(ctx0, kv, n_embd_head_qk_nope, n_head, n_tokens,
  8027. ggml_row_size(kv->type, n_embd_head_qk_nope + hparams.n_embd_head_v),
  8028. ggml_row_size(kv->type, n_head * (n_embd_head_qk_nope + hparams.n_embd_head_v)),
  8029. 0);
  8030. cb(k_nope, "k_nope", il);
  8031. // and {n_head * n_embd_head_v, n_tokens}
  8032. ggml_tensor * v_states = ggml_view_3d(ctx0, kv, hparams.n_embd_head_v, n_head, n_tokens,
  8033. ggml_row_size(kv->type, (n_embd_head_qk_nope + hparams.n_embd_head_v)),
  8034. ggml_row_size(kv->type, (n_embd_head_qk_nope + hparams.n_embd_head_v)*n_head),
  8035. ggml_row_size(kv->type, (n_embd_head_qk_nope)));
  8036. cb(v_states, "v_states", il);
  8037. v_states = ggml_cont(ctx0, v_states);
  8038. cb(v_states, "v_states", il);
  8039. q_pe = ggml_rope_ext(
  8040. ctx0, q_pe, inp_pos, rope_factors,
  8041. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  8042. ext_factor, attn_factor, beta_fast, beta_slow
  8043. );
  8044. cb(q_pe, "q_pe", il);
  8045. // shared RoPE key
  8046. k_pe = ggml_rope_ext(
  8047. ctx0, k_pe, inp_pos, rope_factors,
  8048. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  8049. ext_factor, attn_factor, beta_fast, beta_slow
  8050. );
  8051. cb(k_pe, "k_pe", il);
  8052. ggml_tensor * q_states = ggml_concat(ctx0, q_nope, q_pe, 0);
  8053. cb(q_states, "q_states", il);
  8054. ggml_tensor * k_states = ggml_concat(ctx0, k_nope, ggml_repeat(ctx0, k_pe, q_pe), 0);
  8055. cb(k_states, "k_states", il);
  8056. cur = build_attn(inp_attn,
  8057. model.layers[il].wo, NULL,
  8058. q_states, k_states, v_states, nullptr, nullptr, kq_scale, il);
  8059. }
  8060. if (il == n_layer - 1 && inp_out_ids) {
  8061. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  8062. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  8063. }
  8064. // scale_res - scale the hidden states for residual connection
  8065. const float scale_res = scale_depth/sqrtf(float(n_layer)); // TODO: is this correct?
  8066. cur = ggml_scale(ctx0, cur, scale_res);
  8067. cb(cur, "hidden_scaled", il);
  8068. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  8069. cb(ffn_inp, "ffn_inp", il);
  8070. // feed-forward network
  8071. {
  8072. cur = build_norm(ffn_inp,
  8073. model.layers[il].ffn_norm, NULL,
  8074. LLM_NORM_RMS, il);
  8075. cb(cur, "ffn_norm", il);
  8076. cur = build_ffn(cur,
  8077. model.layers[il].ffn_up, NULL, NULL,
  8078. model.layers[il].ffn_gate, NULL, NULL,
  8079. model.layers[il].ffn_down, NULL, NULL,
  8080. NULL,
  8081. LLM_FFN_SILU, LLM_FFN_PAR, il);
  8082. cb(cur, "ffn_out", il);
  8083. }
  8084. // scale the hidden states for residual connection
  8085. cur = ggml_scale(ctx0, cur, scale_res);
  8086. cb(cur, "hidden_scaled_ffn", il);
  8087. cur = ggml_add(ctx0, cur, ffn_inp);
  8088. cur = build_cvec(cur, il);
  8089. cb(cur, "l_out", il);
  8090. // input for next layer
  8091. inpL = cur;
  8092. }
  8093. cur = inpL;
  8094. cur = build_norm(cur,
  8095. model.output_norm, NULL,
  8096. LLM_NORM_RMS, -1);
  8097. cb(cur, "result_norm", -1);
  8098. res->t_embd = cur;
  8099. // lm_head scaling
  8100. const float scale_lmhead = float(n_embd_base)/float(n_embd);
  8101. cur = ggml_scale(ctx0, cur, scale_lmhead);
  8102. cb(cur, "lmhead_scaling", -1);
  8103. // lm_head
  8104. cur = build_lora_mm(model.output, cur);
  8105. cb(cur, "result_output", -1);
  8106. res->t_logits = cur;
  8107. ggml_build_forward_expand(gf, cur);
  8108. }
  8109. };
  8110. struct llm_build_gemma : public llm_graph_context {
  8111. llm_build_gemma(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  8112. const int64_t n_embd_head = hparams.n_embd_head_v;
  8113. ggml_tensor * cur;
  8114. ggml_tensor * inpL;
  8115. inpL = build_inp_embd(model.tok_embd);
  8116. inpL = ggml_scale(ctx0, inpL, sqrtf(n_embd));
  8117. cb(inpL, "inp_scaled", -1);
  8118. // inp_pos - contains the positions
  8119. ggml_tensor * inp_pos = build_inp_pos();
  8120. auto * inp_attn = build_attn_inp_kv_unified();
  8121. ggml_tensor * inp_out_ids = build_inp_out_ids();
  8122. for (int il = 0; il < n_layer; ++il) {
  8123. // norm
  8124. cur = build_norm(inpL,
  8125. model.layers[il].attn_norm, NULL,
  8126. LLM_NORM_RMS, il);
  8127. cb(cur, "attn_norm", il);
  8128. // self-attention
  8129. {
  8130. // compute Q and K and RoPE them
  8131. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  8132. cb(Qcur, "Qcur", il);
  8133. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  8134. cb(Kcur, "Kcur", il);
  8135. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  8136. cb(Vcur, "Vcur", il);
  8137. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  8138. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  8139. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  8140. Qcur = ggml_rope_ext(
  8141. ctx0, Qcur, inp_pos, nullptr,
  8142. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  8143. ext_factor, attn_factor, beta_fast, beta_slow);
  8144. Kcur = ggml_rope_ext(
  8145. ctx0, Kcur, inp_pos, nullptr,
  8146. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  8147. ext_factor, attn_factor, beta_fast, beta_slow);
  8148. cb(Qcur, "Qcur", il);
  8149. cb(Kcur, "Kcur", il);
  8150. cb(Vcur, "Vcur", il);
  8151. Qcur = ggml_scale(ctx0, Qcur, 1.0f / sqrtf(float(n_embd_head)));
  8152. cb(Qcur, "Qcur_scaled", il);
  8153. cur = build_attn(inp_attn,
  8154. model.layers[il].wo, NULL,
  8155. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f, il);
  8156. }
  8157. if (il == n_layer - 1 && inp_out_ids) {
  8158. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  8159. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  8160. }
  8161. ggml_tensor * sa_out = ggml_add(ctx0, cur, inpL);
  8162. cb(sa_out, "sa_out", il);
  8163. cur = build_norm(sa_out,
  8164. model.layers[il].ffn_norm, NULL,
  8165. LLM_NORM_RMS, il);
  8166. cb(cur, "ffn_norm", il);
  8167. // feed-forward network
  8168. {
  8169. cur = build_ffn(cur,
  8170. model.layers[il].ffn_up, NULL, NULL,
  8171. model.layers[il].ffn_gate, NULL, NULL,
  8172. model.layers[il].ffn_down, NULL, NULL,
  8173. NULL,
  8174. LLM_FFN_GELU, LLM_FFN_PAR, il);
  8175. cb(cur, "ffn_out", il);
  8176. }
  8177. cur = ggml_add(ctx0, cur, sa_out);
  8178. cur = build_cvec(cur, il);
  8179. cb(cur, "l_out", il);
  8180. // input for next layer
  8181. inpL = cur;
  8182. }
  8183. cur = inpL;
  8184. cur = build_norm(cur,
  8185. model.output_norm, NULL,
  8186. LLM_NORM_RMS, -1);
  8187. cb(cur, "result_norm", -1);
  8188. res->t_embd = cur;
  8189. // lm_head
  8190. cur = build_lora_mm(model.output, cur);
  8191. cb(cur, "result_output", -1);
  8192. res->t_logits = cur;
  8193. ggml_build_forward_expand(gf, cur);
  8194. }
  8195. };
  8196. struct llm_build_gemma2_iswa : public llm_graph_context {
  8197. llm_build_gemma2_iswa(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  8198. const int64_t n_embd_head = hparams.n_embd_head_k;
  8199. ggml_tensor * cur;
  8200. ggml_tensor * inpL;
  8201. inpL = build_inp_embd(model.tok_embd);
  8202. inpL = ggml_scale(ctx0, inpL, sqrtf(n_embd));
  8203. cb(inpL, "inp_scaled", -1);
  8204. // inp_pos - contains the positions
  8205. ggml_tensor * inp_pos = build_inp_pos();
  8206. auto * inp_attn = build_attn_inp_kv_unified_iswa();
  8207. ggml_tensor * inp_out_ids = build_inp_out_ids();
  8208. for (int il = 0; il < n_layer; ++il) {
  8209. // norm
  8210. cur = build_norm(inpL,
  8211. model.layers[il].attn_norm, NULL,
  8212. LLM_NORM_RMS, il);
  8213. cb(cur, "attn_norm", il);
  8214. // self-attention
  8215. {
  8216. // compute Q and K and RoPE them
  8217. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  8218. cb(Qcur, "Qcur", il);
  8219. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  8220. cb(Kcur, "Kcur", il);
  8221. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  8222. cb(Vcur, "Vcur", il);
  8223. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  8224. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  8225. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  8226. Qcur = ggml_rope_ext(
  8227. ctx0, Qcur, inp_pos, nullptr,
  8228. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  8229. ext_factor, attn_factor, beta_fast, beta_slow);
  8230. Kcur = ggml_rope_ext(
  8231. ctx0, Kcur, inp_pos, nullptr,
  8232. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  8233. ext_factor, attn_factor, beta_fast, beta_slow);
  8234. cb(Qcur, "Qcur", il);
  8235. cb(Kcur, "Kcur", il);
  8236. cb(Vcur, "Vcur", il);
  8237. Qcur = ggml_scale(ctx0, Qcur, hparams.f_attention_scale);
  8238. cur = build_attn(inp_attn,
  8239. model.layers[il].wo, NULL,
  8240. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f, il);
  8241. }
  8242. if (il == n_layer - 1 && inp_out_ids) {
  8243. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  8244. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  8245. }
  8246. cur = build_norm(cur,
  8247. model.layers[il].attn_post_norm, NULL,
  8248. LLM_NORM_RMS, il);
  8249. cb(cur, "attn_post_norm", il);
  8250. ggml_tensor * sa_out = ggml_add(ctx0, cur, inpL);
  8251. cb(sa_out, "sa_out", il);
  8252. cur = build_norm(sa_out,
  8253. model.layers[il].ffn_norm, NULL,
  8254. LLM_NORM_RMS, il);
  8255. cb(cur, "ffn_norm", il);
  8256. // feed-forward network
  8257. {
  8258. cur = build_ffn(cur,
  8259. model.layers[il].ffn_up, NULL, NULL,
  8260. model.layers[il].ffn_gate, NULL, NULL,
  8261. model.layers[il].ffn_down, NULL, NULL,
  8262. NULL,
  8263. LLM_FFN_GELU, LLM_FFN_PAR, il);
  8264. cb(cur, "ffn_out", il);
  8265. }
  8266. cur = build_norm(cur,
  8267. model.layers[il].ffn_post_norm, NULL,
  8268. LLM_NORM_RMS, -1);
  8269. cb(cur, "ffn_post_norm", -1);
  8270. cur = ggml_add(ctx0, cur, sa_out);
  8271. cur = build_cvec(cur, il);
  8272. cb(cur, "l_out", il);
  8273. // input for next layer
  8274. inpL = cur;
  8275. }
  8276. cur = inpL;
  8277. cur = build_norm(cur,
  8278. model.output_norm, NULL,
  8279. LLM_NORM_RMS, -1);
  8280. cb(cur, "result_norm", -1);
  8281. res->t_embd = cur;
  8282. // lm_head
  8283. cur = build_lora_mm(model.output, cur);
  8284. // final logit soft-capping
  8285. cur = ggml_scale(ctx0, cur, 1.0f / hparams.f_final_logit_softcapping);
  8286. cur = ggml_tanh(ctx0, cur);
  8287. cur = ggml_scale(ctx0, cur, hparams.f_final_logit_softcapping);
  8288. cb(cur, "result_output", -1);
  8289. res->t_logits = cur;
  8290. ggml_build_forward_expand(gf, cur);
  8291. }
  8292. };
  8293. struct llm_build_gemma3_iswa : public llm_graph_context {
  8294. llm_build_gemma3_iswa(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  8295. const int64_t n_embd_head = hparams.n_embd_head_k;
  8296. ggml_tensor * cur;
  8297. ggml_tensor * inpL;
  8298. inpL = build_inp_embd(model.tok_embd);
  8299. // important: do not normalize weights for raw embeddings input (i.e. encoded image emdeddings)
  8300. if (ubatch.token) {
  8301. inpL = ggml_scale(ctx0, inpL, sqrtf(n_embd));
  8302. cb(inpL, "inp_scaled", -1);
  8303. }
  8304. // inp_pos - contains the positions
  8305. ggml_tensor * inp_pos = build_inp_pos();
  8306. // TODO: is causal == true correct? might need some changes
  8307. auto * inp_attn = build_attn_inp_kv_unified_iswa();
  8308. ggml_tensor * inp_out_ids = build_inp_out_ids();
  8309. for (int il = 0; il < n_layer; ++il) {
  8310. const float freq_base_l = model.get_rope_freq_base (cparams, il);
  8311. const float freq_scale_l = model.get_rope_freq_scale(cparams, il);
  8312. // norm
  8313. cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
  8314. cb(cur, "attn_norm", il);
  8315. // self-attention
  8316. {
  8317. // compute Q and K and RoPE them
  8318. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  8319. cb(Qcur, "Qcur", il);
  8320. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  8321. cb(Kcur, "Kcur", il);
  8322. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  8323. cb(Vcur, "Vcur", il);
  8324. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  8325. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  8326. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  8327. Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);
  8328. cb(Qcur, "Qcur_normed", il);
  8329. Qcur = ggml_rope_ext(
  8330. ctx0, Qcur, inp_pos, nullptr,
  8331. n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l,
  8332. ext_factor, attn_factor, beta_fast, beta_slow);
  8333. Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);
  8334. cb(Kcur, "Kcur_normed", il);
  8335. Kcur = ggml_rope_ext(
  8336. ctx0, Kcur, inp_pos, nullptr,
  8337. n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l,
  8338. ext_factor, attn_factor, beta_fast, beta_slow);
  8339. cb(Qcur, "Qcur", il);
  8340. cb(Kcur, "Kcur", il);
  8341. cb(Vcur, "Vcur", il);
  8342. // ref: https://github.com/google/gemma_pytorch/blob/014acb7ac4563a5f77c76d7ff98f31b568c16508/gemma/model.py#L315
  8343. Qcur = ggml_scale(ctx0, Qcur, hparams.f_attention_scale);
  8344. cur = build_attn(inp_attn,
  8345. model.layers[il].wo, NULL,
  8346. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f, il);
  8347. }
  8348. if (il == n_layer - 1 && inp_out_ids) {
  8349. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  8350. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  8351. }
  8352. cur = build_norm(cur,
  8353. model.layers[il].attn_post_norm, NULL,
  8354. LLM_NORM_RMS, il);
  8355. cb(cur, "attn_post_norm", il);
  8356. ggml_tensor * sa_out = ggml_add(ctx0, cur, inpL);
  8357. cb(sa_out, "sa_out", il);
  8358. cur = build_norm(sa_out,
  8359. model.layers[il].ffn_norm, NULL,
  8360. LLM_NORM_RMS, il);
  8361. cb(cur, "ffn_norm", il);
  8362. // feed-forward network
  8363. {
  8364. cur = build_ffn(cur,
  8365. model.layers[il].ffn_up, NULL, NULL,
  8366. model.layers[il].ffn_gate, NULL, NULL,
  8367. model.layers[il].ffn_down, NULL, NULL,
  8368. NULL,
  8369. LLM_FFN_GELU, LLM_FFN_PAR, il);
  8370. cb(cur, "ffn_out", il);
  8371. }
  8372. cur = build_norm(cur,
  8373. model.layers[il].ffn_post_norm, NULL,
  8374. LLM_NORM_RMS, -1);
  8375. cb(cur, "ffn_post_norm", -1);
  8376. cur = ggml_add(ctx0, cur, sa_out);
  8377. cur = build_cvec(cur, il);
  8378. cb(cur, "l_out", il);
  8379. // input for next layer
  8380. inpL = cur;
  8381. }
  8382. cur = inpL;
  8383. cur = build_norm(cur,
  8384. model.output_norm, NULL,
  8385. LLM_NORM_RMS, -1);
  8386. cb(cur, "result_norm", -1);
  8387. res->t_embd = cur;
  8388. // lm_head
  8389. cur = build_lora_mm(model.output, cur);
  8390. cb(cur, "result_output", -1);
  8391. res->t_logits = cur;
  8392. ggml_build_forward_expand(gf, cur);
  8393. }
  8394. };
  8395. struct llm_build_gemma3n_iswa : public llm_graph_context {
  8396. const llama_model & model;
  8397. const int64_t n_embd_head;
  8398. const int64_t n_embd_altup;
  8399. const int64_t n_altup;
  8400. const int i_altup_act;
  8401. const int n_layer_kv = 20; // number of layers having KV [KV_REUSE]
  8402. const int n_layer_sparsity = 10; // number of layers using activation sparsity
  8403. const float f_sparsity_std_mul = 1.6448533535003662f; // std_multiplier = normal_dist.icdf(0.95)
  8404. llm_build_gemma3n_iswa(const llama_model & model, const llm_graph_params & params)
  8405. : llm_graph_context(params),
  8406. model(model),
  8407. n_embd_head(model.hparams.n_embd_head_k),
  8408. n_embd_altup(model.hparams.n_embd_altup),
  8409. n_altup(model.hparams.n_altup),
  8410. i_altup_act(model.hparams.i_altup_act) {
  8411. ggml_tensor * cur;
  8412. ggml_tensor * inpL;
  8413. inpL = build_inp_embd(model.tok_embd);
  8414. // important: do not normalize weights for raw embeddings input (i.e. encoded image emdeddings)
  8415. if (ubatch.token) {
  8416. inpL = ggml_scale(ctx0, inpL, sqrtf(n_embd));
  8417. cb(inpL, "inp_scaled", -1);
  8418. }
  8419. // inp_pos - contains the positions
  8420. ggml_tensor * inp_pos = build_inp_pos();
  8421. // TODO: is causal == true correct? might need some changes
  8422. auto * inp_attn = build_attn_inp_kv_unified_iswa();
  8423. // inp_per_layer shape: [n_embd_altup, n_tokens, n_layer]
  8424. ggml_tensor * inp_per_layer = project_per_layer_inputs(inpL, get_per_layer_inputs());
  8425. // inpL now has only 1 altup, project it to the rest of the altups
  8426. // these "added" altups will be concat to the last dim of inpL
  8427. {
  8428. ggml_tensor * target_magnitude = calc_magnitude(inpL);
  8429. ggml_tensor * inp_repeated = ggml_repeat_4d(ctx0, inpL, n_embd, n_tokens, n_altup - 1, 1);
  8430. ggml_tensor * altup_added = ggml_mul_mat(ctx0, model.altup_proj, inp_repeated); // shape: [n_embd, n_tokens, n_altup - 1]
  8431. ggml_tensor * new_magnitude = calc_magnitude(altup_added);
  8432. altup_added = ggml_div(ctx0,
  8433. ggml_mul(ctx0, altup_added, target_magnitude),
  8434. new_magnitude);
  8435. inpL = ggml_concat(ctx0, inpL, altup_added, 2); // shape: [n_embd, n_tokens, n_altup]
  8436. cb(inpL, "inp_stacked", -1);
  8437. }
  8438. // inpL now has shape: [n_embd, n_tokens, n_altup]
  8439. // inp_per_layer now has shape: [n_embd_altup, n_tokens, n_layer]
  8440. for (int il = 0; il < n_layer; ++il) {
  8441. // this block is made to be closely resemble Gemma3p5DecoderLayer on python code
  8442. const bool has_kv = (il < n_layer_kv);
  8443. const float freq_base_l = model.get_rope_freq_base (cparams, il);
  8444. const float freq_scale_l = model.get_rope_freq_scale(cparams, il);
  8445. ggml_tensor * cur = inpL; // [n_embd, n_tokens, n_altup]
  8446. ggml_tensor * predictions = altup_predict(cur, il); // [n_embd, n_tokens, n_altup]
  8447. // predicted value will go through self-attention and laurel
  8448. ggml_tensor * active_prediction = view_2d_slice(predictions, i_altup_act); // [n_embd, n_tokens]
  8449. cur = active_prediction;
  8450. cb(cur, "active_prediction", il);
  8451. // norm
  8452. cur = build_norm(cur, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
  8453. cb(cur, "attn_norm", il);
  8454. // laurel
  8455. ggml_tensor * laurel_out = laurel(cur, il); // [n_embd, n_tokens]
  8456. // self-attention
  8457. if (has_kv) {
  8458. // compute Q and K and RoPE them
  8459. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  8460. cb(Qcur, "Qcur", il);
  8461. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  8462. cb(Kcur, "Kcur", il);
  8463. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  8464. cb(Vcur, "Vcur", il);
  8465. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  8466. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  8467. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  8468. Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);
  8469. Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);
  8470. Vcur = ggml_rms_norm(ctx0, Vcur, hparams.f_norm_rms_eps);
  8471. cb(Qcur, "Qcur_normed", il);
  8472. cb(Kcur, "Kcur_normed", il);
  8473. cb(Vcur, "Vcur_normed", il);
  8474. Qcur = ggml_rope_ext(
  8475. ctx0, Qcur, inp_pos, nullptr,
  8476. n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l,
  8477. ext_factor, attn_factor, beta_fast, beta_slow);
  8478. Kcur = ggml_rope_ext(
  8479. ctx0, Kcur, inp_pos, nullptr,
  8480. n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l,
  8481. ext_factor, attn_factor, beta_fast, beta_slow);
  8482. cb(Qcur, "Qcur_pos", il);
  8483. cb(Kcur, "Kcur_pos", il);
  8484. cur = build_attn(inp_attn,
  8485. model.layers[il].wo, NULL,
  8486. Qcur, Kcur, Vcur, nullptr, nullptr, hparams.f_attention_scale, il);
  8487. } else {
  8488. // no KV layers
  8489. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  8490. cb(Qcur, "Qcur", il);
  8491. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  8492. Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);
  8493. cb(Qcur, "Qcur_normed", il);
  8494. Qcur = ggml_rope_ext(
  8495. ctx0, Qcur, inp_pos, nullptr,
  8496. n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l,
  8497. ext_factor, attn_factor, beta_fast, beta_slow);
  8498. cb(Qcur, "Qcur_pos", il);
  8499. cur = build_attn(inp_attn,
  8500. model.layers[il].wo, NULL,
  8501. Qcur, nullptr, nullptr, nullptr, nullptr, hparams.f_attention_scale, il);
  8502. }
  8503. cur = build_norm(cur,
  8504. model.layers[il].attn_post_norm, NULL,
  8505. LLM_NORM_RMS, il);
  8506. cb(cur, "attn_post_norm", il);
  8507. cur = ggml_add(ctx0, cur, active_prediction); // [n_embd, n_tokens]
  8508. cb(cur, "attn_gated", il);
  8509. ggml_tensor * attn_laurel = ggml_scale(ctx0,
  8510. ggml_add(ctx0, cur, laurel_out),
  8511. 1.0f / sqrtf(2.0f)); // [n_embd, n_tokens]
  8512. cb(attn_laurel, "attn_laurel", il);
  8513. cur = build_norm(attn_laurel,
  8514. model.layers[il].ffn_norm, NULL,
  8515. LLM_NORM_RMS, il);
  8516. cb(cur, "ffn_norm", il);
  8517. // feed-forward network
  8518. {
  8519. ggml_tensor * up_proj = build_lora_mm(model.layers[il].ffn_up, cur);
  8520. ggml_tensor * gate_proj = build_lora_mm(model.layers[il].ffn_gate, cur);
  8521. if (il < n_layer_sparsity) {
  8522. // apply activation sparsity
  8523. gate_proj = gaussian_topk(gate_proj);
  8524. }
  8525. gate_proj = ggml_gelu(ctx0, gate_proj);
  8526. cur = ggml_mul(ctx0, up_proj, gate_proj);
  8527. cur = build_lora_mm(model.layers[il].ffn_down, cur);
  8528. cb(cur, "ffn_out", il);
  8529. }
  8530. cur = build_norm(cur,
  8531. model.layers[il].ffn_post_norm, NULL,
  8532. LLM_NORM_RMS, -1);
  8533. cb(cur, "ffn_post_norm", il);
  8534. ggml_tensor * attn_ffw_laurel_gated = ggml_add(ctx0, cur, attn_laurel); // [n_embd, n_tokens]
  8535. cb(attn_ffw_laurel_gated, "attn_ffw_laurel_gated", il);
  8536. ggml_tensor * corrected = altup_correct(predictions, attn_ffw_laurel_gated, il); // [n_embd, n_tokens, n_altup]
  8537. ggml_tensor * first_prediction; // [n_embd, n_tokens]
  8538. {
  8539. first_prediction = view_2d_slice(corrected, i_altup_act); // [n_embd, n_tokens]
  8540. first_prediction = ggml_mul(ctx0, first_prediction, model.layers[il].altup_correct_scale);
  8541. first_prediction = build_lora_mm(model.layers[il].per_layer_inp_gate, first_prediction);
  8542. first_prediction = ggml_gelu(ctx0, first_prediction); // [n_embd_altup, n_tokens]
  8543. cb(first_prediction, "first_prediction_gated", il);
  8544. ggml_tensor * inp_this_layer = view_2d_slice(inp_per_layer, il); // [n_embd_altup, n_tokens]
  8545. first_prediction = ggml_mul(ctx0, first_prediction, inp_this_layer); // [n_embd_altup, n_tokens]
  8546. cb(first_prediction, "first_prediction_scaled", il);
  8547. first_prediction = build_lora_mm(model.layers[il].per_layer_proj, first_prediction); // [n_embd, n_tokens]
  8548. first_prediction = build_norm(first_prediction,
  8549. model.layers[il].per_layer_post_norm, NULL,
  8550. LLM_NORM_RMS, il);
  8551. cb(first_prediction, "first_prediction_out", il);
  8552. }
  8553. // equivalent to python code: corrected_predictions[1:] += first_prediction
  8554. {
  8555. ggml_tensor * slice_first = view_2d_slice(corrected, 0);
  8556. ggml_tensor * slice_rest = ggml_view_3d(ctx0, corrected, n_embd, n_tokens, n_altup - 1,
  8557. ggml_row_size(corrected->type, n_embd),
  8558. ggml_row_size(corrected->type, n_embd*n_tokens),
  8559. n_embd*n_tokens*ggml_element_size(corrected));
  8560. ggml_tensor * tmp = ggml_add(ctx0, slice_rest, first_prediction); // [n_embd, n_tokens, n_altup - 1]
  8561. corrected = ggml_concat(ctx0, slice_first, tmp, 2); // [n_embd, n_tokens, n_altup]
  8562. }
  8563. cur = corrected; // [n_embd, n_tokens, n_altup]
  8564. cur = build_cvec(cur, il);
  8565. cb(cur, "l_out", il);
  8566. // input for next layer
  8567. inpL = cur;
  8568. }
  8569. cur = inpL; // [n_embd, n_tokens, n_altup]
  8570. // cur now has multiple altup(s), we want to merge them back to 1 altup
  8571. {
  8572. ggml_tensor * target_magnitude = calc_magnitude(view_2d_slice(cur, i_altup_act)); // [n_embd, n_tokens]
  8573. // do a view to skip the first slice (active altup)
  8574. ggml_tensor * alt_slice = ggml_view_3d(ctx0, cur, n_embd, n_tokens, n_altup - 1,
  8575. ggml_row_size(cur->type, n_embd),
  8576. ggml_row_size(cur->type, n_embd*n_tokens),
  8577. n_embd*n_tokens*ggml_element_size(cur));
  8578. ggml_tensor * altup_unembd = ggml_mul_mat(ctx0, model.altup_unembd_proj, alt_slice); // shape: [n_embd, n_tokens, n_altup - 1]
  8579. ggml_tensor * new_magnitude = calc_magnitude(altup_unembd);
  8580. altup_unembd = ggml_div(ctx0,
  8581. ggml_mul(ctx0, altup_unembd, target_magnitude),
  8582. new_magnitude);
  8583. cb(altup_unembd, "altup_unembd", -1);
  8584. // equivalent to torch.mean(hidden_states, dim=0)
  8585. cur = view_2d_slice(cur, 0); // [n_embd, n_tokens]
  8586. for (int i = 0; i < n_altup - 1; ++i) {
  8587. cur = ggml_add(ctx0, cur, view_2d_slice(altup_unembd, i));
  8588. }
  8589. cur = ggml_scale(ctx0, cur, 1.0f / float(n_altup)); // [n_embd, n_tokens]
  8590. cb(cur, "unembd_merged", -1);
  8591. }
  8592. // cur now has shape: [n_embd, n_tokens]
  8593. // TODO: move this to right after the last KV layer
  8594. {
  8595. // skip computing output for unused tokens
  8596. ggml_tensor * inp_out_ids = build_inp_out_ids();
  8597. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  8598. }
  8599. cur = build_norm(cur,
  8600. model.output_norm, NULL,
  8601. LLM_NORM_RMS, -1);
  8602. cb(cur, "result_norm", -1);
  8603. res->t_embd = cur;
  8604. cur = build_lora_mm(model.output, cur);
  8605. {
  8606. // final logit soft-capping
  8607. cur = ggml_scale(ctx0, cur, 1.0f / hparams.f_final_logit_softcapping);
  8608. cur = ggml_tanh(ctx0, cur);
  8609. cur = ggml_scale(ctx0, cur, hparams.f_final_logit_softcapping);
  8610. }
  8611. cb(cur, "result_output", -1);
  8612. res->t_logits = cur;
  8613. ggml_build_forward_expand(gf, cur);
  8614. }
  8615. ggml_tensor * calc_magnitude(ggml_tensor * x) {
  8616. return ggml_sqrt(ctx0, ggml_sum_rows(ctx0, ggml_sqr(ctx0, x)));
  8617. }
  8618. // get 2D slice view from a 3D tensor, the idx corresponds to the 3rd dim
  8619. ggml_tensor * view_2d_slice(ggml_tensor * x, int idx) {
  8620. GGML_ASSERT(idx < (int)x->ne[2]);
  8621. return ggml_view_2d(ctx0, x, x->ne[0], x->ne[1],
  8622. ggml_row_size(x->type, x->ne[0]),
  8623. idx * x->ne[0] * x->ne[1] * ggml_element_size(x));
  8624. }
  8625. // equivalent to get_per_layer_inputs() in python code
  8626. // output shape: [n_embd_altup, n_layer, n_tokens]
  8627. ggml_tensor * get_per_layer_inputs() {
  8628. auto inp = std::make_unique<llm_graph_input_embd>();
  8629. ggml_tensor * inp_per_layer;
  8630. if (ubatch.token) {
  8631. inp->tokens = ggml_new_tensor_1d(ctx0, GGML_TYPE_I32, ubatch.n_tokens);
  8632. ggml_set_input(inp->tokens);
  8633. res->t_tokens = inp->tokens;
  8634. inp_per_layer = ggml_get_rows(ctx0, model.tok_embd_per_layer, inp->tokens);
  8635. inp_per_layer = ggml_reshape_3d(ctx0, inp_per_layer, n_embd_altup, n_layer, n_tokens);
  8636. inp_per_layer = ggml_scale(ctx0, inp_per_layer, sqrtf((float)n_embd_altup));
  8637. cb(inp_per_layer, "inp_per_layer_selected", -1);
  8638. } else {
  8639. GGML_ABORT("TODO: support embd input");
  8640. }
  8641. res->add_input(std::move(inp));
  8642. return inp_per_layer;
  8643. }
  8644. // equivalent to project_per_layer_inputs() in python code
  8645. // this calculates the per-layer inputs, so the final tensor shape will have n_layer as the last dim
  8646. // output shape: [n_embd_altup, n_tokens, n_layer]
  8647. ggml_tensor * project_per_layer_inputs(ggml_tensor * inputs_embeds, ggml_tensor * inp_per_layer) {
  8648. const float per_layer_projection_scale = 1.0f / sqrtf((float)n_embd);
  8649. const float per_layer_input_scale = 1.0f / sqrtf(2.0f);
  8650. ggml_tensor * per_layer_proj = ggml_mul_mat(ctx0, model.per_layer_model_proj, inputs_embeds);
  8651. per_layer_proj = ggml_scale(ctx0, per_layer_proj, per_layer_projection_scale);
  8652. per_layer_proj = ggml_reshape_3d(ctx0, per_layer_proj, n_embd_altup, n_layer, n_tokens);
  8653. per_layer_proj = build_norm(per_layer_proj,
  8654. model.per_layer_proj_norm, NULL,
  8655. LLM_NORM_RMS, -1); // [n_embd_altup, n_layer, n_tokens]
  8656. cb(per_layer_proj, "per_layer_proj", -1);
  8657. inp_per_layer = ggml_add(ctx0, inp_per_layer, per_layer_proj);
  8658. inp_per_layer = ggml_scale(ctx0, inp_per_layer, per_layer_input_scale);
  8659. cb(inp_per_layer, "inp_per_layer", -1);
  8660. // permute to shape: [n_embd_altup, n_tokens, n_layer]
  8661. inp_per_layer = ggml_cont(ctx0, ggml_permute(ctx0, inp_per_layer, 0, 2, 1, 3));
  8662. return inp_per_layer;
  8663. }
  8664. // input cur shape: [n_altup, n_tokens]
  8665. // output shape: [n_altup, n_tokens]
  8666. ggml_tensor * laurel(ggml_tensor * cur, int il) {
  8667. ggml_tensor * tmp = cur;
  8668. tmp = build_lora_mm(model.layers[il].laurel_l, tmp);
  8669. tmp = build_lora_mm(model.layers[il].laurel_r, tmp);
  8670. tmp = build_norm(tmp, model.layers[il].laurel_post_norm, NULL, LLM_NORM_RMS, il);
  8671. tmp = ggml_add(ctx0, tmp, cur);
  8672. cb(tmp, "laurel_out", il);
  8673. return tmp;
  8674. }
  8675. // input x shape: [n_embd, n_tokens]
  8676. // output shape: [n_embd, n_tokens]
  8677. ggml_tensor * gaussian_topk(ggml_tensor * x) {
  8678. ggml_tensor * mean = ggml_mean(ctx0, x);
  8679. ggml_tensor * std = ggml_sqrt(ctx0, ggml_scale(ctx0,
  8680. ggml_sum_rows(ctx0, ggml_sqr(ctx0, ggml_sub(ctx0, x, mean))),
  8681. 1.0f / (float)(x->ne[0] - 1)
  8682. ));
  8683. ggml_tensor * cutoff_x = ggml_add(ctx0, mean, ggml_scale(ctx0, std, f_sparsity_std_mul));
  8684. return ggml_relu(ctx0, ggml_sub(ctx0, x, cutoff_x));
  8685. }
  8686. //
  8687. // altup functions
  8688. //
  8689. // equivalent to compute_router_modalities() in python code
  8690. // input x shape: [n_embd, n_tokens]
  8691. // output shape: [n_altup, n_tokens]
  8692. ggml_tensor * altup_compute_router_modalities(ggml_tensor * x, int il) {
  8693. ggml_tensor * router_inputs = build_norm(x,
  8694. model.layers[il].altup_router_norm, NULL,
  8695. LLM_NORM_RMS, il);
  8696. // router_input_scale
  8697. router_inputs = ggml_scale(ctx0, router_inputs, 1.0f / (float)n_embd);
  8698. ggml_tensor * output = ggml_mul_mat(ctx0, model.layers[il].altup_router, router_inputs);
  8699. return ggml_tanh(ctx0, output); // [n_altup, n_tokens]
  8700. }
  8701. // input cur shape: [n_embd, n_tokens, n_altup]
  8702. // output shape: [n_embd, n_tokens, n_altup]
  8703. ggml_tensor * altup_predict(ggml_tensor * cur, int il) {
  8704. ggml_tensor * activated = view_2d_slice(cur, i_altup_act); // [n_embd, n_tokens]
  8705. ggml_tensor * modalities = altup_compute_router_modalities(activated, il); // [n_altup, n_tokens]
  8706. cb(modalities, "modalities", il);
  8707. ggml_tensor * all_coefs = build_lora_mm(model.layers[il].altup_predict_coef, modalities);
  8708. cb(all_coefs, "all_coefs", il);
  8709. // first dim now having n_altup^2 elements, we reshape it to 2D (so we end up with 3D tensor)
  8710. all_coefs = ggml_reshape_3d(ctx0, all_coefs, n_altup, n_altup, n_tokens);
  8711. // permute to [n_altup, n_embd, n_tokens]
  8712. ggml_tensor * cur_permuted = ggml_cont(ctx0, ggml_permute(ctx0, cur, 1, 2, 0, 3));
  8713. ggml_tensor * predictions = ggml_mul_mat(ctx0, cur_permuted, all_coefs); // [n_altup, n_embd, n_tokens]
  8714. // final shape must be the same as cur: [n_embd, n_tokens, n_altup]
  8715. predictions = ggml_cont(ctx0, ggml_permute(ctx0, predictions, 0, 2, 1, 3));
  8716. predictions = ggml_add(ctx0, predictions, cur);
  8717. cb(predictions, "predictions", il);
  8718. return predictions;
  8719. }
  8720. // input predictions shape: [n_embd, n_tokens, n_altup]
  8721. // input activated shape: [n_embd, n_tokens]
  8722. // output shape: [n_embd, n_tokens, n_altup]
  8723. ggml_tensor * altup_correct(ggml_tensor * predictions, ggml_tensor * activated, int il) {
  8724. ggml_tensor * modalities = altup_compute_router_modalities(activated, il); // [n_altup, n_tokens]
  8725. cb(modalities, "modalities", il);
  8726. ggml_tensor * active_prediction = view_2d_slice(predictions, i_altup_act);
  8727. ggml_tensor * innovation = ggml_sub(ctx0, activated, active_prediction); // [n_embd, n_tokens]
  8728. cb(innovation, "innovation", il);
  8729. ggml_tensor * all_coefs = build_lora_mm(model.layers[il].altup_correct_coef, modalities); // [n_altup, n_tokens]
  8730. all_coefs = ggml_scale_bias(ctx0, all_coefs, 1.0f, 1.0f); // + 1.0
  8731. cb(all_coefs, "all_coefs", il);
  8732. all_coefs = ggml_transpose(ctx0, all_coefs); // [n_tokens, n_altup]
  8733. all_coefs = ggml_cont_3d(ctx0, all_coefs, 1, n_tokens, n_altup); // [1, n_tokens, n_altup]
  8734. innovation = ggml_repeat_4d(ctx0, innovation, n_embd, n_tokens, n_altup, 1);
  8735. ggml_tensor * corrected = ggml_mul(ctx0, innovation, all_coefs); // [n_embd, n_tokens, n_altup]
  8736. corrected = ggml_add(ctx0, corrected, predictions); // [n_embd, n_tokens, n_altup]
  8737. cb(corrected, "corrected", il);
  8738. return corrected;
  8739. }
  8740. };
  8741. // TODO: move up next to build_starcoder
  8742. struct llm_build_starcoder2 : public llm_graph_context {
  8743. llm_build_starcoder2(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  8744. const int64_t n_embd_head = hparams.n_embd_head_v;
  8745. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  8746. GGML_ASSERT(n_embd_head == hparams.n_rot);
  8747. ggml_tensor * cur;
  8748. ggml_tensor * inpL;
  8749. inpL = build_inp_embd(model.tok_embd);
  8750. // inp_pos - contains the positions
  8751. ggml_tensor * inp_pos = build_inp_pos();
  8752. auto * inp_attn = build_attn_inp_kv_unified();
  8753. ggml_tensor * inp_out_ids = build_inp_out_ids();
  8754. for (int il = 0; il < n_layer; ++il) {
  8755. ggml_tensor * inpSA = inpL;
  8756. // norm
  8757. cur = build_norm(inpL,
  8758. model.layers[il].attn_norm, model.layers[il].attn_norm_b,
  8759. LLM_NORM, il);
  8760. cb(cur, "attn_norm", il);
  8761. // self-attention
  8762. {
  8763. // compute Q and K and RoPE them
  8764. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  8765. cb(Qcur, "Qcur", il);
  8766. if (model.layers[il].bq) {
  8767. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  8768. cb(Qcur, "Qcur", il);
  8769. }
  8770. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  8771. cb(Kcur, "Kcur", il);
  8772. if (model.layers[il].bk) {
  8773. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  8774. cb(Kcur, "Kcur", il);
  8775. }
  8776. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  8777. cb(Vcur, "Vcur", il);
  8778. if (model.layers[il].bv) {
  8779. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  8780. cb(Vcur, "Vcur", il);
  8781. }
  8782. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  8783. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  8784. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  8785. Qcur = ggml_rope_ext(
  8786. ctx0, Qcur, inp_pos, nullptr,
  8787. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  8788. ext_factor, attn_factor, beta_fast, beta_slow
  8789. );
  8790. Kcur = ggml_rope_ext(
  8791. ctx0, Kcur, inp_pos, nullptr,
  8792. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  8793. ext_factor, attn_factor, beta_fast, beta_slow
  8794. );
  8795. cb(Qcur, "Qcur", il);
  8796. cb(Kcur, "Kcur", il);
  8797. cb(Vcur, "Vcur", il);
  8798. cur = build_attn(inp_attn,
  8799. model.layers[il].wo, model.layers[il].bo,
  8800. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  8801. }
  8802. if (il == n_layer - 1 && inp_out_ids) {
  8803. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  8804. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  8805. }
  8806. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  8807. cb(ffn_inp, "ffn_inp", il);
  8808. // feed-forward network
  8809. cur = build_norm(ffn_inp,
  8810. model.layers[il].ffn_norm, model.layers[il].ffn_norm_b,
  8811. LLM_NORM, il);
  8812. cb(cur, "ffn_norm", il);
  8813. cur = build_ffn(cur,
  8814. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  8815. NULL, NULL, NULL,
  8816. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  8817. NULL,
  8818. LLM_FFN_GELU, LLM_FFN_SEQ, il);
  8819. cb(cur, "ffn_out", il);
  8820. cur = ggml_add(ctx0, cur, ffn_inp);
  8821. cur = build_cvec(cur, il);
  8822. cb(cur, "l_out", il);
  8823. // input for next layer
  8824. inpL = cur;
  8825. }
  8826. cur = inpL;
  8827. cur = build_norm(cur,
  8828. model.output_norm, model.output_norm_b,
  8829. LLM_NORM, -1);
  8830. cb(cur, "result_norm", -1);
  8831. res->t_embd = cur;
  8832. // lm_head
  8833. cur = build_lora_mm(model.output, cur);
  8834. cb(cur, "result_output", -1);
  8835. res->t_logits = cur;
  8836. ggml_build_forward_expand(gf, cur);
  8837. }
  8838. };
  8839. struct llm_graph_context_mamba : public llm_graph_context {
  8840. llm_graph_context_mamba(const llm_graph_params & params) : llm_graph_context(params) {}
  8841. ggml_tensor * build_mamba_layer(
  8842. llm_graph_input_rs * inp,
  8843. ggml_tensor * cur,
  8844. const llama_model & model,
  8845. const llama_ubatch & ubatch,
  8846. int il) {
  8847. const auto * mctx_cur = inp->mctx;
  8848. const auto kv_head = mctx_cur->get_head();
  8849. const auto & layer = model.layers[il];
  8850. const int64_t d_conv = hparams.ssm_d_conv;
  8851. const int64_t d_inner = hparams.ssm_d_inner;
  8852. const int64_t d_state = hparams.ssm_d_state;
  8853. const int64_t dt_rank = hparams.ssm_dt_rank;
  8854. const int64_t n_head = d_inner;
  8855. const int64_t head_dim = 1;
  8856. const int64_t n_seqs = ubatch.n_seqs;
  8857. // Some variants of Mamba arch (e.g. FalconMamba do apply layer norm on B and Dt layers)
  8858. const bool ssm_dt_b_c_rms = hparams.ssm_dt_b_c_rms;
  8859. const int64_t n_seq_tokens = ubatch.n_seq_tokens;
  8860. GGML_ASSERT(n_seqs != 0);
  8861. GGML_ASSERT(ubatch.equal_seqs());
  8862. GGML_ASSERT(ubatch.n_tokens == n_seq_tokens * n_seqs);
  8863. ggml_tensor * conv_states_all = mctx_cur->get_r_l(il);
  8864. ggml_tensor * ssm_states_all = mctx_cur->get_s_l(il);
  8865. ggml_tensor * conv = build_rs(inp, conv_states_all, hparams.n_embd_r(), n_seqs);
  8866. conv = ggml_reshape_3d(ctx0, conv, d_conv - 1, d_inner, n_seqs);
  8867. // {n_embd, n_tokens} => {n_embd, n_seq_tokens, n_seqs}
  8868. cur = ggml_reshape_3d(ctx0, cur, cur->ne[0], n_seq_tokens, n_seqs);
  8869. // {n_embd, 2*d_inner} @ {n_embd, n_seq_tokens, n_seqs} => {2*d_inner, n_seq_tokens, n_seqs}
  8870. ggml_tensor * xz = build_lora_mm(layer.ssm_in, cur);
  8871. // split the above in two
  8872. // => {d_inner, n_seq_tokens, n_seqs}
  8873. ggml_tensor * x = ggml_view_3d(ctx0, xz, d_inner, xz->ne[1], xz->ne[2], xz->nb[1], xz->nb[2], 0);
  8874. 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));
  8875. // conv
  8876. {
  8877. // => {d_conv - 1 + n_seq_tokens, d_inner, n_seqs}
  8878. ggml_tensor * conv_x = ggml_concat(ctx0, conv, ggml_transpose(ctx0, x), 0);
  8879. // copy last (d_conv - 1) columns back into the state cache
  8880. 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]));
  8881. ggml_build_forward_expand(gf,
  8882. ggml_cpy(ctx0, last_conv,
  8883. ggml_view_1d(ctx0, conv_states_all,
  8884. (d_conv - 1)*(d_inner)*(n_seqs),
  8885. kv_head*(d_conv - 1)*(d_inner)*ggml_element_size(conv_states_all))));
  8886. // 1D convolution
  8887. // The equivalent is to make a self-overlapping view of conv_x
  8888. // over d_conv columns at each stride in the 3rd dimension,
  8889. // then element-wise multiply that with the conv1d weight,
  8890. // then sum the elements of each row,
  8891. // (the last two steps are a dot product over rows (also doable with mul_mat))
  8892. // then permute away the ne[0] dimension,
  8893. // and then you're left with the resulting x tensor.
  8894. // For simultaneous sequences, all sequences need to have the same length.
  8895. x = ggml_ssm_conv(ctx0, conv_x, layer.ssm_conv1d);
  8896. // bias
  8897. x = ggml_add(ctx0, x, layer.ssm_conv1d_b);
  8898. x = ggml_silu(ctx0, x);
  8899. }
  8900. // ssm
  8901. {
  8902. // {d_inner, dt_rank + 2*d_state} @ {d_inner, n_seq_tokens, n_seqs} => {dt_rank + 2*d_state, n_seq_tokens, n_seqs}
  8903. ggml_tensor * x_db = build_lora_mm(layer.ssm_x, x);
  8904. // split
  8905. 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);
  8906. 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);
  8907. 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));
  8908. // Some Mamba variants (e.g. FalconMamba, Jamba) apply RMS norm in B, C & Dt layers
  8909. if (ssm_dt_b_c_rms || (layer.ssm_dt_norm && layer.ssm_b_norm && layer.ssm_c_norm)) {
  8910. dt = build_norm(dt, layer.ssm_dt_norm, NULL, LLM_NORM_RMS, il);
  8911. B = build_norm(B, layer.ssm_b_norm, NULL, LLM_NORM_RMS, il);
  8912. C = build_norm(C, layer.ssm_c_norm, NULL, LLM_NORM_RMS, il);
  8913. }
  8914. // {dt_rank, d_inner} @ {dt_rank, n_seq_tokens, n_seqs} => {d_inner, n_seq_tokens, n_seqs}
  8915. dt = build_lora_mm(layer.ssm_dt, dt);
  8916. dt = ggml_add(ctx0, dt, layer.ssm_dt_b);
  8917. cur = x;
  8918. x = ggml_reshape_4d(ctx0, x, head_dim, n_head, n_seq_tokens, n_seqs);
  8919. ggml_tensor * A = layer.ssm_a;
  8920. // use the states and the indices provided by build_recurrent_state
  8921. // (this is necessary in order to properly use the states before they are overwritten,
  8922. // while avoiding to make unnecessary copies of the states)
  8923. auto get_ssm_rows = [&](ggml_context * ctx, ggml_tensor * states, ggml_tensor * ids) {
  8924. ggml_tensor * ssm = ggml_reshape_4d(ctx, states, d_state, head_dim, n_head, mctx_cur->get_size());
  8925. // Custom operator to optimize the parallel associative scan
  8926. // as described in the Annex D of the Mamba paper.
  8927. // => {d_inner, n_seq_tokens, n_seqs} and {d_state, d_inner, n_seqs}
  8928. return ggml_ssm_scan(ctx, ssm, x, dt, A, B, C, ids);
  8929. };
  8930. ggml_tensor * y_ssm = build_rs(inp, ssm_states_all, hparams.n_embd_s(), ubatch.n_seqs, get_ssm_rows);
  8931. // store last states
  8932. ggml_build_forward_expand(gf,
  8933. ggml_cpy(ctx0,
  8934. ggml_view_1d(ctx0, y_ssm, d_state*d_inner*n_seqs, x->nb[3]*x->ne[3]),
  8935. 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))));
  8936. ggml_tensor * y = ggml_view_3d(ctx0, y_ssm, d_inner, n_seq_tokens, n_seqs, x->nb[2], x->nb[3], 0);
  8937. // TODO: skip computing output earlier for unused tokens
  8938. y = ggml_add(ctx0, y, ggml_mul(ctx0, cur, layer.ssm_d));
  8939. y = ggml_swiglu_split(ctx0, ggml_cont(ctx0, z), y);
  8940. // {d_inner, n_embd} @ {d_inner, n_seq_tokens, n_seqs} => {n_embd, n_seq_tokens, n_seqs}
  8941. cur = build_lora_mm(layer.ssm_out, y);
  8942. }
  8943. // {n_embd, n_seq_tokens, n_seqs} => {n_embd, n_tokens}
  8944. cur = ggml_reshape_2d(ctx0, cur, cur->ne[0], n_seq_tokens * n_seqs);
  8945. return cur;
  8946. }
  8947. ggml_tensor * build_mamba2_layer(
  8948. llm_graph_input_rs * inp,
  8949. ggml_tensor * cur,
  8950. const llama_model & model,
  8951. const llama_ubatch & ubatch,
  8952. int il) const {
  8953. const auto * mctx_cur = inp->mctx;
  8954. const auto kv_head = mctx_cur->get_head();
  8955. const int64_t d_conv = hparams.ssm_d_conv;
  8956. const int64_t d_inner = hparams.ssm_d_inner;
  8957. const int64_t d_state = hparams.ssm_d_state;
  8958. const int64_t n_head = hparams.ssm_dt_rank;
  8959. const int64_t head_dim = d_inner / n_head;
  8960. const int64_t n_group = hparams.ssm_n_group;
  8961. const int64_t n_seqs = ubatch.n_seqs;
  8962. const int64_t n_seq_tokens = ubatch.n_seq_tokens;
  8963. GGML_ASSERT(n_seqs != 0);
  8964. GGML_ASSERT(ubatch.equal_seqs());
  8965. GGML_ASSERT(ubatch.n_tokens == n_seq_tokens * n_seqs);
  8966. ggml_tensor * conv_states_all = mctx_cur->get_r_l(il);
  8967. ggml_tensor * ssm_states_all = mctx_cur->get_s_l(il);
  8968. ggml_tensor * conv = build_rs(inp, conv_states_all, hparams.n_embd_r(), n_seqs);
  8969. conv = ggml_reshape_3d(ctx0, conv, d_conv - 1, d_inner + 2*n_group*d_state, n_seqs);
  8970. // {n_embd, n_tokens} => {n_embd, n_seq_tokens, n_seqs}
  8971. cur = ggml_reshape_3d(ctx0, cur, cur->ne[0], n_seq_tokens, n_seqs);
  8972. // d_in_proj = 2 * self.d_inner + 2 * self.ngroups * self.d_state + self.nheads
  8973. // {n_embd, d_in_proj} @ {n_embd, n_seq_tokens, n_seqs} => {d_in_proj, n_seq_tokens, n_seqs}
  8974. ggml_tensor * zxBCdt = build_lora_mm(model.layers[il].ssm_in, cur);
  8975. // split the above in three
  8976. 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);
  8977. 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));
  8978. 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));
  8979. // conv
  8980. {
  8981. // => {d_conv - 1 + n_seq_tokens, d_inner + 2*n_group*d_state, n_seqs}
  8982. ggml_tensor * conv_x = ggml_concat(ctx0, conv, ggml_transpose(ctx0, xBC), 0);
  8983. // copy last (d_conv - 1) columns back into the state cache
  8984. 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]));
  8985. ggml_build_forward_expand(gf,
  8986. ggml_cpy(ctx0, last_conv,
  8987. ggml_view_1d(ctx0, conv_states_all,
  8988. (d_conv - 1)*(d_inner + 2*n_group*d_state)*(n_seqs),
  8989. kv_head*(d_conv - 1)*(d_inner + 2*n_group*d_state)*ggml_element_size(conv_states_all))));
  8990. // 1D convolution
  8991. // The equivalent is to make a self-overlapping view of conv_x
  8992. // over d_conv columns at each stride in the 3rd dimension,
  8993. // then element-wise multiply that with the conv1d weight,
  8994. // then sum the elements of each row,
  8995. // (the last two steps are a dot product over rows (also doable with mul_mat))
  8996. // then permute away the ne[0] dimension,
  8997. // and then you're left with the resulting x tensor.
  8998. // For simultaneous sequences, all sequences need to have the same length.
  8999. xBC = ggml_ssm_conv(ctx0, conv_x, model.layers[il].ssm_conv1d);
  9000. // bias
  9001. xBC = ggml_add(ctx0, xBC, model.layers[il].ssm_conv1d_b);
  9002. xBC = ggml_silu(ctx0, xBC);
  9003. }
  9004. // ssm
  9005. {
  9006. // These correspond to V K Q in SSM/attention duality
  9007. 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);
  9008. 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));
  9009. 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));
  9010. // {n_head, n_seq_tokens, n_seqs}
  9011. dt = ggml_add(ctx0, ggml_cont(ctx0, dt), model.layers[il].ssm_dt_b);
  9012. ggml_tensor * A = model.layers[il].ssm_a;
  9013. // use the states and the indices provided by build_recurrent_state
  9014. // (this is necessary in order to properly use the states before they are overwritten,
  9015. // while avoiding to make unnecessary copies of the states)
  9016. auto get_ssm_rows = [&](ggml_context * ctx, ggml_tensor * states, ggml_tensor * ids) {
  9017. ggml_tensor * ssm = ggml_reshape_4d(ctx, states, d_state, head_dim, n_head, mctx_cur->get_size());
  9018. // TODO: use semistructured matrices to implement state-space duality
  9019. // => {d_inner, n_seq_tokens, n_seqs} and {d_state, d_inner, n_seqs}
  9020. return ggml_ssm_scan(ctx, ssm, x, dt, A, B, C, ids);
  9021. };
  9022. ggml_tensor * y_ssm = build_rs(inp, ssm_states_all, hparams.n_embd_s(), ubatch.n_seqs, get_ssm_rows);
  9023. // store last states
  9024. ggml_build_forward_expand(gf,
  9025. ggml_cpy(ctx0,
  9026. ggml_view_1d(ctx0, y_ssm, d_state*d_inner*n_seqs, ggml_nelements(x)*x->nb[0]),
  9027. 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))));
  9028. 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);
  9029. // TODO: skip computing output earlier for unused tokens
  9030. y = ggml_add(ctx0, y, ggml_mul(ctx0, x, model.layers[il].ssm_d));
  9031. y = ggml_swiglu_split(ctx0, ggml_cont(ctx0, z), y);
  9032. // grouped RMS norm
  9033. if (model.layers[il].ssm_norm) {
  9034. y = ggml_reshape_4d(ctx0, y, d_inner / n_group, n_group, n_seq_tokens, n_seqs);
  9035. y = build_norm(y, model.layers[il].ssm_norm, NULL, LLM_NORM_RMS, il);
  9036. }
  9037. y = ggml_reshape_3d(ctx0, y, d_inner, n_seq_tokens, n_seqs);
  9038. // {d_inner, n_embd} @ {d_inner, n_seq_tokens, n_seqs} => {n_embd, n_seq_tokens, n_seqs}
  9039. cur = build_lora_mm(model.layers[il].ssm_out, y);
  9040. }
  9041. // {n_embd, n_seq_tokens, n_seqs} => {n_embd, n_tokens}
  9042. cur = ggml_reshape_2d(ctx0, cur, cur->ne[0], n_seq_tokens * n_seqs);
  9043. cb(cur, "mamba_out", il);
  9044. return cur;
  9045. }
  9046. };
  9047. struct llm_build_mamba : public llm_graph_context_mamba {
  9048. llm_build_mamba(const llama_model & model, const llm_graph_params & params) : llm_graph_context_mamba(params) {
  9049. ggml_tensor * cur;
  9050. ggml_tensor * inpL;
  9051. // {n_embd, n_tokens}
  9052. inpL = build_inp_embd(model.tok_embd);
  9053. auto * rs_inp = build_rs_inp();
  9054. ggml_tensor * inp_out_ids = build_inp_out_ids();
  9055. for (int il = 0; il < n_layer; ++il) {
  9056. // norm
  9057. cur = build_norm(inpL,
  9058. model.layers[il].attn_norm, NULL,
  9059. LLM_NORM_RMS, il);
  9060. cb(cur, "attn_norm", il);
  9061. if (model.arch == LLM_ARCH_MAMBA2) {
  9062. cur = build_mamba2_layer(rs_inp, cur, model, ubatch, il);
  9063. } else {
  9064. cur = build_mamba_layer(rs_inp, cur, model, ubatch, il);
  9065. }
  9066. if (il == n_layer - 1 && inp_out_ids) {
  9067. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  9068. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  9069. }
  9070. // residual
  9071. cur = ggml_add(ctx0, cur, inpL);
  9072. cur = build_cvec(cur, il);
  9073. cb(cur, "l_out", il);
  9074. // input for next layer
  9075. inpL = cur;
  9076. }
  9077. // final rmsnorm
  9078. cur = build_norm(inpL, model.output_norm, NULL, LLM_NORM_RMS, -1);
  9079. cb(cur, "result_norm", -1);
  9080. res->t_embd = cur;
  9081. // lm_head
  9082. cur = build_lora_mm(model.output, cur);
  9083. cb(cur, "result_output", -1);
  9084. res->t_logits = cur;
  9085. ggml_build_forward_expand(gf, cur);
  9086. }
  9087. };
  9088. struct llm_build_jamba : public llm_graph_context_mamba {
  9089. llm_build_jamba(const llama_model & model, const llm_graph_params & params) : llm_graph_context_mamba(params) {
  9090. const int64_t n_embd_head = hparams.n_embd_head_v;
  9091. ggml_tensor * cur;
  9092. ggml_tensor * inpL;
  9093. // {n_embd, n_tokens}
  9094. inpL = build_inp_embd(model.tok_embd);
  9095. auto * inp_hybrid = build_inp_mem_hybrid();
  9096. ggml_tensor * inp_out_ids = build_inp_out_ids();
  9097. for (int il = 0; il < n_layer; ++il) {
  9098. const int64_t n_head_kv = hparams.n_head_kv(il);
  9099. cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
  9100. cb(cur, "attn_norm", il);
  9101. if (n_head_kv == 0) {
  9102. cur = build_mamba_layer(inp_hybrid->get_recr(), cur, model, ubatch, il);
  9103. } else {
  9104. // Attention
  9105. struct ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  9106. struct ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  9107. struct ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  9108. cb(Qcur, "Qcur", il);
  9109. cb(Kcur, "Kcur", il);
  9110. cb(Vcur, "Vcur", il);
  9111. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  9112. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  9113. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  9114. cb(Qcur, "Qcur", il);
  9115. cb(Kcur, "Kcur", il);
  9116. cb(Vcur, "Vcur", il);
  9117. // No RoPE :)
  9118. cur = build_attn(inp_hybrid->get_attn(), model.layers[il].wo, NULL, Qcur, Kcur, Vcur, NULL, NULL, 1.0f/sqrtf(float(n_embd_head)), il);
  9119. }
  9120. if (il == n_layer - 1 && inp_out_ids) {
  9121. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  9122. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  9123. }
  9124. // residual
  9125. struct ggml_tensor * ffn_inp = ggml_add(ctx0, inpL, cur);
  9126. cb(cur, "ffn_inp", il);
  9127. cur = build_norm(ffn_inp, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);
  9128. cb(cur, "ffn_norm", il);
  9129. // feed-forward network
  9130. if (model.layers[il].ffn_gate_inp == nullptr) {
  9131. // FFN
  9132. cur = build_ffn(cur,
  9133. model.layers[il].ffn_up, NULL, NULL,
  9134. model.layers[il].ffn_gate, NULL, NULL,
  9135. model.layers[il].ffn_down, NULL, NULL,
  9136. NULL,
  9137. LLM_FFN_SILU, LLM_FFN_PAR, il);
  9138. cb(cur, "ffn_out", il);
  9139. } else {
  9140. // MoE branch
  9141. cur = build_moe_ffn(cur,
  9142. model.layers[il].ffn_gate_inp,
  9143. model.layers[il].ffn_up_exps,
  9144. model.layers[il].ffn_gate_exps,
  9145. model.layers[il].ffn_down_exps,
  9146. nullptr,
  9147. n_expert, n_expert_used,
  9148. LLM_FFN_SILU, false,
  9149. false, 0.0,
  9150. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  9151. il);
  9152. cb(cur, "ffn_moe_out", il);
  9153. }
  9154. // residual
  9155. cur = ggml_add(ctx0, ffn_inp, cur);
  9156. cur = build_cvec(cur, il);
  9157. cb(cur, "l_out", il);
  9158. // input for next layer
  9159. inpL = cur;
  9160. }
  9161. // final rmsnorm
  9162. cur = build_norm(inpL, model.output_norm, NULL, LLM_NORM_RMS, -1);
  9163. cb(cur, "result_norm", -1);
  9164. res->t_embd = cur;
  9165. // lm_head
  9166. cur = build_lora_mm(model.output, cur);
  9167. cb(cur, "result_output", -1);
  9168. res->t_logits = cur;
  9169. ggml_build_forward_expand(gf, cur);
  9170. }
  9171. };
  9172. struct llm_build_command_r : public llm_graph_context {
  9173. llm_build_command_r(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  9174. const int64_t n_embd_head = hparams.n_embd_head_v;
  9175. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  9176. const float f_logit_scale = hparams.f_logit_scale;
  9177. ggml_tensor * cur;
  9178. ggml_tensor * inpL;
  9179. inpL = build_inp_embd(model.tok_embd);
  9180. // inp_pos - contains the positions
  9181. ggml_tensor * inp_pos = build_inp_pos();
  9182. auto * inp_attn = build_attn_inp_kv_unified();
  9183. ggml_tensor * inp_out_ids = build_inp_out_ids();
  9184. for (int il = 0; il < n_layer; ++il) {
  9185. // norm
  9186. cur = build_norm(inpL,
  9187. model.layers[il].attn_norm, NULL,
  9188. LLM_NORM, il);
  9189. cb(cur, "attn_norm", il);
  9190. ggml_tensor * ffn_inp = cur;
  9191. // self-attention
  9192. {
  9193. // compute Q and K and RoPE them
  9194. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  9195. cb(Qcur, "Qcur", il);
  9196. if (model.layers[il].bq) {
  9197. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  9198. cb(Qcur, "Qcur", il);
  9199. }
  9200. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  9201. cb(Kcur, "Kcur", il);
  9202. if (model.layers[il].bk) {
  9203. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  9204. cb(Kcur, "Kcur", il);
  9205. }
  9206. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  9207. cb(Vcur, "Vcur", il);
  9208. if (model.layers[il].bv) {
  9209. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  9210. cb(Vcur, "Vcur", il);
  9211. }
  9212. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  9213. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  9214. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  9215. if (model.layers[il].attn_q_norm) {
  9216. Qcur = build_norm(Qcur,
  9217. model.layers[il].attn_q_norm,
  9218. NULL,
  9219. LLM_NORM, il);
  9220. cb(Qcur, "Qcur", il);
  9221. }
  9222. Qcur = ggml_rope_ext(
  9223. ctx0, Qcur, inp_pos, nullptr,
  9224. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  9225. ext_factor, attn_factor, beta_fast, beta_slow
  9226. );
  9227. if (model.layers[il].attn_k_norm) {
  9228. Kcur = build_norm(Kcur,
  9229. model.layers[il].attn_k_norm,
  9230. NULL,
  9231. LLM_NORM, il);
  9232. cb(Kcur, "Kcur", il);
  9233. }
  9234. Kcur = ggml_rope_ext(
  9235. ctx0, Kcur, inp_pos, nullptr,
  9236. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  9237. ext_factor, attn_factor, beta_fast, beta_slow
  9238. );
  9239. cb(Qcur, "Qcur", il);
  9240. cb(Kcur, "Kcur", il);
  9241. cb(Vcur, "Vcur", il);
  9242. cur = build_attn(inp_attn,
  9243. model.layers[il].wo, model.layers[il].bo,
  9244. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  9245. }
  9246. if (il == n_layer - 1 && inp_out_ids) {
  9247. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  9248. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  9249. ffn_inp = ggml_get_rows(ctx0, ffn_inp, inp_out_ids);
  9250. }
  9251. ggml_tensor * attn_out = cur;
  9252. // feed-forward network
  9253. {
  9254. cur = build_ffn(ffn_inp,
  9255. model.layers[il].ffn_up, NULL, NULL,
  9256. model.layers[il].ffn_gate, NULL, NULL,
  9257. model.layers[il].ffn_down, NULL, NULL,
  9258. NULL,
  9259. LLM_FFN_SILU, LLM_FFN_PAR, il);
  9260. cb(cur, "ffn_out", il);
  9261. }
  9262. // add together residual + FFN + self-attention
  9263. cur = ggml_add(ctx0, cur, inpL);
  9264. cur = ggml_add(ctx0, cur, attn_out);
  9265. cur = build_cvec(cur, il);
  9266. cb(cur, "l_out", il);
  9267. // input for next layer
  9268. inpL = cur;
  9269. }
  9270. cur = inpL;
  9271. cur = build_norm(cur,
  9272. model.output_norm, NULL,
  9273. LLM_NORM, -1);
  9274. cb(cur, "result_norm", -1);
  9275. res->t_embd = cur;
  9276. // lm_head
  9277. cur = build_lora_mm(model.output, cur);
  9278. if (f_logit_scale) {
  9279. cur = ggml_scale(ctx0, cur, f_logit_scale);
  9280. }
  9281. cb(cur, "result_output", -1);
  9282. res->t_logits = cur;
  9283. ggml_build_forward_expand(gf, cur);
  9284. }
  9285. };
  9286. struct llm_build_cohere2_iswa : public llm_graph_context {
  9287. llm_build_cohere2_iswa(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  9288. const int64_t n_embd_head = hparams.n_embd_head_v;
  9289. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  9290. const float f_logit_scale = hparams.f_logit_scale;
  9291. ggml_tensor * cur;
  9292. ggml_tensor * inpL;
  9293. inpL = build_inp_embd(model.tok_embd);
  9294. // inp_pos - contains the positions
  9295. ggml_tensor * inp_pos = build_inp_pos();
  9296. auto * inp_attn = build_attn_inp_kv_unified_iswa();
  9297. ggml_tensor * inp_out_ids = build_inp_out_ids();
  9298. for (int il = 0; il < n_layer; ++il) {
  9299. const bool is_swa = hparams.is_swa(il);
  9300. // norm
  9301. cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM, il);
  9302. cb(cur, "attn_norm", il);
  9303. ggml_tensor * ffn_inp = cur;
  9304. // self-attention
  9305. {
  9306. // rope freq factors for 128k context
  9307. ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
  9308. // compute Q and K and RoPE them
  9309. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  9310. cb(Qcur, "Qcur", il);
  9311. if (model.layers[il].bq) {
  9312. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  9313. cb(Qcur, "Qcur", il);
  9314. }
  9315. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  9316. cb(Kcur, "Kcur", il);
  9317. if (model.layers[il].bk) {
  9318. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  9319. cb(Kcur, "Kcur", il);
  9320. }
  9321. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  9322. cb(Vcur, "Vcur", il);
  9323. if (model.layers[il].bv) {
  9324. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  9325. cb(Vcur, "Vcur", il);
  9326. }
  9327. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  9328. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  9329. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  9330. if (is_swa) {
  9331. Qcur = ggml_rope_ext(
  9332. ctx0, Qcur, inp_pos, rope_factors,
  9333. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  9334. ext_factor, attn_factor, beta_fast, beta_slow
  9335. );
  9336. Kcur = ggml_rope_ext(
  9337. ctx0, Kcur, inp_pos, rope_factors,
  9338. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  9339. ext_factor, attn_factor, beta_fast, beta_slow
  9340. );
  9341. }
  9342. cb(Qcur, "Qcur", il);
  9343. cb(Kcur, "Kcur", il);
  9344. cb(Vcur, "Vcur", il);
  9345. cur = build_attn(inp_attn,
  9346. model.layers[il].wo, model.layers[il].bo,
  9347. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  9348. }
  9349. if (il == n_layer - 1 && inp_out_ids) {
  9350. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  9351. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  9352. ffn_inp = ggml_get_rows(ctx0, ffn_inp, inp_out_ids);
  9353. }
  9354. ggml_tensor * attn_out = cur;
  9355. // feed-forward network
  9356. {
  9357. cur = build_ffn(ffn_inp, model.layers[il].ffn_up, NULL, NULL, model.layers[il].ffn_gate,
  9358. NULL, NULL, model.layers[il].ffn_down, NULL, NULL, NULL, LLM_FFN_SILU, LLM_FFN_PAR,
  9359. il);
  9360. cb(cur, "ffn_out", il);
  9361. }
  9362. // add together residual + FFN + self-attention
  9363. cur = ggml_add(ctx0, cur, inpL);
  9364. cur = ggml_add(ctx0, cur, attn_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, model.output_norm, NULL, LLM_NORM, -1);
  9372. cb(cur, "result_norm", -1);
  9373. res->t_embd = cur;
  9374. // lm_head
  9375. cur = build_lora_mm(model.output, cur);
  9376. if (f_logit_scale) {
  9377. cur = ggml_scale(ctx0, cur, f_logit_scale);
  9378. }
  9379. cb(cur, "result_output", -1);
  9380. res->t_logits = cur;
  9381. ggml_build_forward_expand(gf, cur);
  9382. }
  9383. };
  9384. // ref: https://allenai.org/olmo
  9385. // based on the original build_llama() function, changes:
  9386. // * non-parametric layer norm
  9387. // * clamp qkv
  9388. // * removed bias
  9389. // * removed MoE
  9390. struct llm_build_olmo : public llm_graph_context {
  9391. llm_build_olmo(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  9392. const int64_t n_embd_head = hparams.n_embd_head_v;
  9393. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  9394. GGML_ASSERT(n_embd_head == hparams.n_rot);
  9395. ggml_tensor * cur;
  9396. ggml_tensor * inpL;
  9397. inpL = build_inp_embd(model.tok_embd);
  9398. // inp_pos - contains the positions
  9399. ggml_tensor * inp_pos = build_inp_pos();
  9400. auto * inp_attn = build_attn_inp_kv_unified();
  9401. ggml_tensor * inp_out_ids = build_inp_out_ids();
  9402. for (int il = 0; il < n_layer; ++il) {
  9403. ggml_tensor * inpSA = inpL;
  9404. // norm
  9405. cur = build_norm(inpL,
  9406. NULL, NULL,
  9407. LLM_NORM, il);
  9408. cb(cur, "attn_norm", il);
  9409. // self-attention
  9410. {
  9411. // compute Q and K and RoPE them
  9412. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  9413. cb(Qcur, "Qcur", il);
  9414. if (hparams.f_clamp_kqv > 0.0f) {
  9415. Qcur = ggml_clamp(ctx0, Qcur, -hparams.f_clamp_kqv, hparams.f_clamp_kqv);
  9416. cb(Qcur, "Qcur", il);
  9417. }
  9418. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  9419. cb(Kcur, "Kcur", il);
  9420. if (hparams.f_clamp_kqv > 0.0f) {
  9421. Kcur = ggml_clamp(ctx0, Kcur, -hparams.f_clamp_kqv, hparams.f_clamp_kqv);
  9422. cb(Kcur, "Kcur", il);
  9423. }
  9424. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  9425. cb(Vcur, "Vcur", il);
  9426. if (hparams.f_clamp_kqv > 0.0f) {
  9427. Vcur = ggml_clamp(ctx0, Vcur, -hparams.f_clamp_kqv, hparams.f_clamp_kqv);
  9428. cb(Vcur, "Vcur", il);
  9429. }
  9430. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  9431. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  9432. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  9433. Qcur = ggml_rope_ext(
  9434. ctx0, Qcur, inp_pos, nullptr,
  9435. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  9436. ext_factor, attn_factor, beta_fast, beta_slow
  9437. );
  9438. Kcur = ggml_rope_ext(
  9439. ctx0, Kcur, inp_pos, nullptr,
  9440. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  9441. ext_factor, attn_factor, beta_fast, beta_slow
  9442. );
  9443. cb(Qcur, "Qcur", il);
  9444. cb(Kcur, "Kcur", il);
  9445. cb(Vcur, "Vcur", il);
  9446. cur = build_attn(inp_attn,
  9447. model.layers[il].wo, nullptr,
  9448. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  9449. }
  9450. if (il == n_layer - 1 && inp_out_ids) {
  9451. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  9452. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  9453. }
  9454. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  9455. cb(ffn_inp, "ffn_inp", il);
  9456. // feed-forward network
  9457. cur = build_norm(ffn_inp,
  9458. NULL, NULL,
  9459. LLM_NORM, il);
  9460. cb(cur, "ffn_norm", il);
  9461. cur = build_ffn(cur,
  9462. model.layers[il].ffn_up, NULL, NULL,
  9463. model.layers[il].ffn_gate, NULL, NULL,
  9464. model.layers[il].ffn_down, NULL, NULL,
  9465. NULL,
  9466. LLM_FFN_SILU, LLM_FFN_PAR, il);
  9467. cb(cur, "ffn_out", il);
  9468. cur = ggml_add(ctx0, cur, ffn_inp);
  9469. cb(cur, "ffn_out", il);
  9470. cur = build_cvec(cur, il);
  9471. cb(cur, "l_out", il);
  9472. // input for next layer
  9473. inpL = cur;
  9474. }
  9475. cur = inpL;
  9476. cur = build_norm(cur,
  9477. NULL, NULL,
  9478. LLM_NORM, -1);
  9479. cb(cur, "result_norm", -1);
  9480. res->t_embd = cur;
  9481. // lm_head
  9482. cur = build_lora_mm(model.output, cur);
  9483. cb(cur, "result_output", -1);
  9484. res->t_logits = cur;
  9485. ggml_build_forward_expand(gf, cur);
  9486. }
  9487. };
  9488. struct llm_build_olmo2 : public llm_graph_context {
  9489. llm_build_olmo2(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  9490. const int64_t n_embd_head = hparams.n_embd_head_v;
  9491. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  9492. GGML_ASSERT(n_embd_head == hparams.n_rot);
  9493. ggml_tensor * cur;
  9494. ggml_tensor * inpL;
  9495. inpL = build_inp_embd(model.tok_embd);
  9496. // inp_pos - contains the positions
  9497. ggml_tensor * inp_pos = build_inp_pos();
  9498. auto * inp_attn = build_attn_inp_kv_unified();
  9499. ggml_tensor * inp_out_ids = build_inp_out_ids();
  9500. for (int il = 0; il < n_layer; ++il) {
  9501. ggml_tensor * inpSA = inpL;
  9502. cur = inpL;
  9503. // self_attention
  9504. {
  9505. // compute Q and K and RoPE them
  9506. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  9507. cb(Qcur, "Qcur", il);
  9508. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  9509. cb(Kcur, "Kcur", il);
  9510. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  9511. cb(Vcur, "Vcur", il);
  9512. Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL,
  9513. LLM_NORM_RMS, il);
  9514. cb(Qcur, "Qcur_normed", il);
  9515. Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL,
  9516. LLM_NORM_RMS, il);
  9517. cb(Kcur, "Kcur_normed", il);
  9518. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  9519. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  9520. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  9521. Qcur = ggml_rope_ext(
  9522. ctx0, Qcur, inp_pos, nullptr,
  9523. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  9524. ext_factor, attn_factor, beta_fast, beta_slow
  9525. );
  9526. Kcur = ggml_rope_ext(
  9527. ctx0, Kcur, inp_pos, nullptr,
  9528. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  9529. ext_factor, attn_factor, beta_fast, beta_slow
  9530. );
  9531. cb(Qcur, "Qcur", il);
  9532. cb(Kcur, "Kcur", il);
  9533. cb(Vcur, "Vcur", il);
  9534. cur = build_attn(inp_attn,
  9535. model.layers[il].wo, NULL,
  9536. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  9537. }
  9538. if (il == n_layer - 1 && inp_out_ids) {
  9539. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  9540. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  9541. }
  9542. cur = build_norm(cur,
  9543. model.layers[il].attn_post_norm, NULL,
  9544. LLM_NORM_RMS, il);
  9545. cb(cur, "attn_post_norm", il);
  9546. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  9547. cb(ffn_inp, "ffn_inp", il);
  9548. // feed-forward network
  9549. cur = build_ffn(ffn_inp,
  9550. model.layers[il].ffn_up, NULL, NULL,
  9551. model.layers[il].ffn_gate, NULL, NULL,
  9552. model.layers[il].ffn_down, NULL, NULL,
  9553. NULL,
  9554. LLM_FFN_SILU, LLM_FFN_PAR, il);
  9555. cb(cur, "ffn_out", il);
  9556. cur = build_norm(cur,
  9557. model.layers[il].ffn_post_norm, NULL,
  9558. LLM_NORM_RMS, -1);
  9559. cb(cur, "ffn_post_norm", -1);
  9560. cur = ggml_add(ctx0, cur, ffn_inp);
  9561. cb(cur, "ffn_out", il);
  9562. cur = build_cvec(cur, il);
  9563. cb(cur, "l_out", il);
  9564. // input for next layer
  9565. inpL = cur;
  9566. }
  9567. cur = inpL;
  9568. cur = build_norm(cur,
  9569. model.output_norm, NULL,
  9570. LLM_NORM_RMS, -1);
  9571. cb(cur, "result_norm", -1);
  9572. res->t_embd = cur;
  9573. // lm_head
  9574. cur = build_lora_mm(model.output, cur);
  9575. cb(cur, "result_output", -1);
  9576. res->t_logits = cur;
  9577. ggml_build_forward_expand(gf, cur);
  9578. }
  9579. };
  9580. // based on the build_qwen2moe() function, changes:
  9581. // * removed shared experts
  9582. // * removed bias
  9583. // * added q, k norm
  9584. struct llm_build_olmoe : public llm_graph_context {
  9585. llm_build_olmoe(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  9586. const int64_t n_embd_head = hparams.n_embd_head_v;
  9587. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  9588. GGML_ASSERT(n_embd_head == hparams.n_rot);
  9589. ggml_tensor * cur;
  9590. ggml_tensor * inpL;
  9591. inpL = build_inp_embd(model.tok_embd);
  9592. // inp_pos - contains the positions
  9593. ggml_tensor * inp_pos = build_inp_pos();
  9594. auto * inp_attn = build_attn_inp_kv_unified();
  9595. ggml_tensor * inp_out_ids = build_inp_out_ids();
  9596. for (int il = 0; il < n_layer; ++il) {
  9597. ggml_tensor * inpSA = inpL;
  9598. // norm
  9599. cur = build_norm(inpL,
  9600. model.layers[il].attn_norm, NULL,
  9601. LLM_NORM_RMS, il);
  9602. cb(cur, "attn_norm", il);
  9603. // self_attention
  9604. {
  9605. // compute Q and K and RoPE them
  9606. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  9607. cb(Qcur, "Qcur", il);
  9608. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  9609. cb(Kcur, "Kcur", il);
  9610. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  9611. cb(Vcur, "Vcur", il);
  9612. Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL,
  9613. LLM_NORM_RMS, il);
  9614. cb(Qcur, "Qcur_normed", il);
  9615. Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL,
  9616. LLM_NORM_RMS, il);
  9617. cb(Kcur, "Kcur_normed", il);
  9618. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  9619. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  9620. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  9621. Qcur = ggml_rope_ext(
  9622. ctx0, Qcur, inp_pos, nullptr,
  9623. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  9624. ext_factor, attn_factor, beta_fast, beta_slow
  9625. );
  9626. Kcur = ggml_rope_ext(
  9627. ctx0, Kcur, inp_pos, nullptr,
  9628. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  9629. ext_factor, attn_factor, beta_fast, beta_slow
  9630. );
  9631. cb(Qcur, "Qcur", il);
  9632. cb(Kcur, "Kcur", il);
  9633. cb(Vcur, "Vcur", il);
  9634. cur = build_attn(inp_attn,
  9635. model.layers[il].wo, NULL,
  9636. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  9637. }
  9638. if (il == n_layer - 1 && inp_out_ids) {
  9639. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  9640. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  9641. }
  9642. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  9643. cb(ffn_inp, "ffn_inp", il);
  9644. // MoE branch
  9645. cur = build_norm(ffn_inp,
  9646. model.layers[il].ffn_norm, NULL,
  9647. LLM_NORM_RMS, il);
  9648. cb(cur, "ffn_norm", il);
  9649. cur = build_moe_ffn(cur,
  9650. model.layers[il].ffn_gate_inp,
  9651. model.layers[il].ffn_up_exps,
  9652. model.layers[il].ffn_gate_exps,
  9653. model.layers[il].ffn_down_exps,
  9654. nullptr,
  9655. n_expert, n_expert_used,
  9656. LLM_FFN_SILU, false,
  9657. false, 0.0,
  9658. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  9659. il);
  9660. cb(cur, "ffn_moe_out", il);
  9661. cur = ggml_add(ctx0, cur, ffn_inp);
  9662. cur = build_cvec(cur, il);
  9663. cb(cur, "l_out", il);
  9664. // input for next layer
  9665. inpL = cur;
  9666. }
  9667. cur = inpL;
  9668. cur = build_norm(cur,
  9669. model.output_norm, NULL,
  9670. LLM_NORM_RMS, -1);
  9671. cb(cur, "result_norm", -1);
  9672. res->t_embd = cur;
  9673. // lm_head
  9674. cur = build_lora_mm(model.output, cur);
  9675. cb(cur, "result_output", -1);
  9676. res->t_logits = cur;
  9677. ggml_build_forward_expand(gf, cur);
  9678. }
  9679. };
  9680. struct llm_build_openelm : public llm_graph_context {
  9681. llm_build_openelm(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  9682. const int64_t n_embd_head = hparams.n_embd_head_v;
  9683. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  9684. ggml_tensor * cur;
  9685. ggml_tensor * inpL;
  9686. inpL = build_inp_embd(model.tok_embd);
  9687. // inp_pos - contains the positions
  9688. ggml_tensor * inp_pos = build_inp_pos();
  9689. auto * inp_attn = build_attn_inp_kv_unified();
  9690. ggml_tensor * inp_out_ids = build_inp_out_ids();
  9691. for (int il = 0; il < n_layer; ++il) {
  9692. const int64_t n_head = hparams.n_head(il);
  9693. const int64_t n_head_kv = hparams.n_head_kv(il);
  9694. const int64_t n_head_qkv = 2*n_head_kv + n_head;
  9695. cur = inpL;
  9696. ggml_tensor * residual = cur;
  9697. // norm
  9698. cur = build_norm(inpL,
  9699. model.layers[il].attn_norm, NULL,
  9700. LLM_NORM_RMS, il);
  9701. cb(cur, "attn_norm", il);
  9702. // self-attention
  9703. {
  9704. cur = build_lora_mm(model.layers[il].wqkv, cur);
  9705. cb(cur, "wqkv", il);
  9706. cur = ggml_reshape_3d(ctx0, cur, n_embd_head_k, n_head_qkv, n_tokens);
  9707. ggml_tensor * Qcur = ggml_view_3d(ctx0, cur, n_embd_head, n_head, n_tokens, cur->nb[1], cur->nb[2], 0);
  9708. cb(Qcur, "Qcur", il);
  9709. 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);
  9710. cb(Kcur, "Kcur", il);
  9711. 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)));
  9712. cb(Vcur, "Vcur", il);
  9713. Qcur = build_norm(Qcur,
  9714. model.layers[il].attn_q_norm, NULL,
  9715. LLM_NORM_RMS, il);
  9716. cb(Qcur, "Qcur", il);
  9717. Kcur = build_norm(Kcur,
  9718. model.layers[il].attn_k_norm, NULL,
  9719. LLM_NORM_RMS, il);
  9720. cb(Kcur, "Kcur", il);
  9721. Qcur = ggml_rope_ext(
  9722. ctx0, Qcur, inp_pos, NULL,
  9723. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  9724. ext_factor, attn_factor, beta_fast, beta_slow
  9725. );
  9726. Kcur = ggml_rope_ext(
  9727. ctx0, Kcur, inp_pos, NULL,
  9728. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  9729. ext_factor, attn_factor, beta_fast, beta_slow
  9730. );
  9731. cb(Qcur, "Qcur", il);
  9732. cb(Kcur, "Kcur", il);
  9733. cb(Qcur, "Vcur", il);
  9734. cur = build_attn(inp_attn,
  9735. model.layers[il].wo, NULL,
  9736. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  9737. }
  9738. if (il == n_layer - 1 && inp_out_ids) {
  9739. residual = ggml_get_rows(ctx0, residual, inp_out_ids);
  9740. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  9741. }
  9742. ggml_tensor * ffn_inp = ggml_add(ctx0, residual, cur);
  9743. cb(ffn_inp, "ffn_inp", il);
  9744. // feed-forward network
  9745. {
  9746. cur = build_norm(ffn_inp,
  9747. model.layers[il].ffn_norm, NULL,
  9748. LLM_NORM_RMS, il);
  9749. cb(cur, "ffn_norm", il);
  9750. cur = build_ffn(cur,
  9751. model.layers[il].ffn_up, NULL, NULL,
  9752. model.layers[il].ffn_gate, NULL, NULL,
  9753. model.layers[il].ffn_down, NULL, NULL,
  9754. NULL,
  9755. LLM_FFN_SILU, LLM_FFN_PAR, il);
  9756. cb(cur, "ffn_out", il);
  9757. }
  9758. cur = ggml_add(ctx0, cur, ffn_inp);
  9759. cur = build_cvec(cur, il);
  9760. cb(cur, "l_out", il);
  9761. inpL = cur;
  9762. }
  9763. cur = inpL;
  9764. // norm
  9765. cur = build_norm(cur,
  9766. model.output_norm, NULL,
  9767. LLM_NORM_RMS, -1);
  9768. cb(cur, "result_norm", -1);
  9769. res->t_embd = cur;
  9770. cur = build_lora_mm(model.output, cur);
  9771. cb(cur, "result_output", -1);
  9772. res->t_logits = cur;
  9773. ggml_build_forward_expand(gf, cur);
  9774. }
  9775. };
  9776. struct llm_build_gptneox : public llm_graph_context {
  9777. llm_build_gptneox(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  9778. const int64_t n_embd_head = hparams.n_embd_head_v;
  9779. const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  9780. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  9781. ggml_tensor * cur;
  9782. ggml_tensor * inpL;
  9783. inpL = build_inp_embd(model.tok_embd);
  9784. // inp_pos - contains the positions
  9785. ggml_tensor * inp_pos = build_inp_pos();
  9786. auto * inp_attn = build_attn_inp_kv_unified();
  9787. ggml_tensor * inp_out_ids = build_inp_out_ids();
  9788. for (int il = 0; il < n_layer; ++il) {
  9789. cur = build_norm(inpL,
  9790. model.layers[il].attn_norm,
  9791. model.layers[il].attn_norm_b,
  9792. LLM_NORM, il);
  9793. cb(cur, "attn_norm", il);
  9794. // self-attention
  9795. {
  9796. cur = build_lora_mm(model.layers[il].wqkv, cur);
  9797. cb(cur, "wqkv", il);
  9798. cur = ggml_add(ctx0, cur, model.layers[il].bqkv);
  9799. cb(cur, "bqkv", il);
  9800. 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));
  9801. 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));
  9802. ggml_tensor * Vcur = ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd + n_embd_gqa));
  9803. Vcur = ggml_cont_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  9804. Qcur = ggml_rope_ext(
  9805. ctx0, Qcur, inp_pos, nullptr,
  9806. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  9807. ext_factor, attn_factor, beta_fast, beta_slow
  9808. );
  9809. Kcur = ggml_rope_ext(
  9810. ctx0, Kcur, inp_pos, nullptr,
  9811. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  9812. ext_factor, attn_factor, beta_fast, beta_slow
  9813. );
  9814. cb(Qcur, "Qcur", il);
  9815. cb(Kcur, "Kcur", il);
  9816. cb(Vcur, "Vcur", il);
  9817. cur = build_attn(inp_attn,
  9818. model.layers[il].wo, model.layers[il].bo,
  9819. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  9820. }
  9821. if (il == n_layer - 1 && inp_out_ids) {
  9822. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  9823. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  9824. }
  9825. // ffn
  9826. if (hparams.use_par_res) {
  9827. // attention and ffn are computed in parallel
  9828. // x = x + attn(ln1(x)) + ffn(ln2(x))
  9829. ggml_tensor * attn_out = cur;
  9830. cur = build_norm(inpL,
  9831. model.layers[il].ffn_norm,
  9832. model.layers[il].ffn_norm_b,
  9833. LLM_NORM, il);
  9834. cb(cur, "ffn_norm", il);
  9835. cur = build_ffn(cur,
  9836. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  9837. NULL, NULL, NULL,
  9838. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  9839. NULL,
  9840. LLM_FFN_GELU, LLM_FFN_SEQ, il);
  9841. cb(cur, "ffn_out", il);
  9842. cur = ggml_add(ctx0, cur, inpL);
  9843. cb(cur, "ffn_out", il);
  9844. cur = ggml_add(ctx0, cur, attn_out);
  9845. cur = build_cvec(cur, il);
  9846. cb(cur, "l_out", il);
  9847. // input for next layer
  9848. inpL = cur;
  9849. } else {
  9850. // attention and ffn are computed sequentially
  9851. // x = x + attn(ln1(x))
  9852. // x = x + ffn(ln2(x))
  9853. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
  9854. cb(ffn_inp, "ffn_inp", il);
  9855. cur = build_norm(ffn_inp,
  9856. model.layers[il].ffn_norm,
  9857. model.layers[il].ffn_norm_b,
  9858. LLM_NORM, il);
  9859. cb(cur, "ffn_norm", il);
  9860. cur = build_ffn(cur,
  9861. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  9862. NULL, NULL, NULL,
  9863. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  9864. NULL,
  9865. LLM_FFN_GELU, LLM_FFN_SEQ, il);
  9866. cb(cur, "ffn_out", il);
  9867. cur = ggml_add(ctx0, cur, ffn_inp);
  9868. cur = build_cvec(cur, il);
  9869. cb(cur, "l_out", il);
  9870. // input for next layer
  9871. inpL = cur;
  9872. }
  9873. }
  9874. cur = build_norm(inpL,
  9875. model.output_norm,
  9876. model.output_norm_b,
  9877. LLM_NORM, -1);
  9878. cb(cur, "result_norm", -1);
  9879. res->t_embd = cur;
  9880. cur = build_lora_mm(model.output, cur);
  9881. cb(cur, "result_output", -1);
  9882. res->t_logits = cur;
  9883. ggml_build_forward_expand(gf, cur);
  9884. }
  9885. };
  9886. struct llm_build_arctic : public llm_graph_context {
  9887. llm_build_arctic(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  9888. const int64_t n_embd_head = hparams.n_embd_head_v;
  9889. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  9890. GGML_ASSERT(n_embd_head == hparams.n_rot);
  9891. ggml_tensor * cur;
  9892. ggml_tensor * inpL;
  9893. inpL = build_inp_embd(model.tok_embd);
  9894. // inp_pos - contains the positions
  9895. ggml_tensor * inp_pos = build_inp_pos();
  9896. auto * inp_attn = build_attn_inp_kv_unified();
  9897. ggml_tensor * inp_out_ids = build_inp_out_ids();
  9898. for (int il = 0; il < n_layer; ++il) {
  9899. ggml_tensor * inpSA = inpL;
  9900. // norm
  9901. cur = build_norm(inpL,
  9902. model.layers[il].attn_norm, NULL,
  9903. LLM_NORM_RMS, il);
  9904. cb(cur, "attn_norm", il);
  9905. // self-attention
  9906. {
  9907. // compute Q and K and RoPE them
  9908. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  9909. cb(Qcur, "Qcur", il);
  9910. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  9911. cb(Kcur, "Kcur", il);
  9912. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  9913. cb(Vcur, "Vcur", il);
  9914. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  9915. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  9916. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  9917. Qcur = ggml_rope_ext(
  9918. ctx0, Qcur, inp_pos, nullptr,
  9919. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  9920. ext_factor, attn_factor, beta_fast, beta_slow
  9921. );
  9922. Kcur = ggml_rope_ext(
  9923. ctx0, Kcur, inp_pos, nullptr,
  9924. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  9925. ext_factor, attn_factor, beta_fast, beta_slow
  9926. );
  9927. cb(Qcur, "Qcur", il);
  9928. cb(Kcur, "Kcur", il);
  9929. cb(Vcur, "Vcur", il);
  9930. cur = build_attn(inp_attn,
  9931. model.layers[il].wo, NULL,
  9932. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  9933. }
  9934. if (il == n_layer - 1 && inp_out_ids) {
  9935. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  9936. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  9937. }
  9938. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  9939. cb(ffn_inp, "ffn_inp", il);
  9940. // feed-forward network
  9941. cur = build_norm(ffn_inp,
  9942. model.layers[il].ffn_norm, NULL,
  9943. LLM_NORM_RMS, il);
  9944. cb(cur, "ffn_norm", il);
  9945. cur = build_ffn(cur,
  9946. model.layers[il].ffn_up, NULL, NULL,
  9947. model.layers[il].ffn_gate, NULL, NULL,
  9948. model.layers[il].ffn_down, NULL, NULL,
  9949. NULL,
  9950. LLM_FFN_SILU, LLM_FFN_PAR, il);
  9951. cb(cur, "ffn_out", il);
  9952. ggml_tensor * ffn_out = ggml_add(ctx0, cur, ffn_inp);
  9953. cb(ffn_out, "ffn_out", il);
  9954. // MoE
  9955. cur = build_norm(inpSA,
  9956. model.layers[il].ffn_norm_exps, NULL,
  9957. LLM_NORM_RMS, il);
  9958. cb(cur, "ffn_norm_exps", il);
  9959. cur = build_moe_ffn(cur,
  9960. model.layers[il].ffn_gate_inp,
  9961. model.layers[il].ffn_up_exps,
  9962. model.layers[il].ffn_gate_exps,
  9963. model.layers[il].ffn_down_exps,
  9964. nullptr,
  9965. n_expert, n_expert_used,
  9966. LLM_FFN_SILU, true,
  9967. false, 0.0,
  9968. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  9969. il);
  9970. cb(cur, "ffn_moe_out", il);
  9971. cur = ggml_add(ctx0, cur, ffn_out);
  9972. cb(cur, "ffn_out", il);
  9973. cur = build_cvec(cur, il);
  9974. cb(cur, "l_out", il);
  9975. // input for next layer
  9976. inpL = cur;
  9977. }
  9978. cur = inpL;
  9979. cur = build_norm(cur,
  9980. model.output_norm, NULL,
  9981. LLM_NORM_RMS, -1);
  9982. cb(cur, "result_norm", -1);
  9983. res->t_embd = cur;
  9984. // lm_head
  9985. cur = build_lora_mm(model.output, cur);
  9986. cb(cur, "result_output", -1);
  9987. res->t_logits = cur;
  9988. ggml_build_forward_expand(gf, cur);
  9989. }
  9990. };
  9991. struct llm_build_deepseek : public llm_graph_context {
  9992. llm_build_deepseek(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  9993. const int64_t n_embd_head = hparams.n_embd_head_v;
  9994. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  9995. GGML_ASSERT(n_embd_head == hparams.n_rot);
  9996. ggml_tensor * cur;
  9997. ggml_tensor * inpL;
  9998. inpL = build_inp_embd(model.tok_embd);
  9999. // inp_pos - contains the positions
  10000. ggml_tensor * inp_pos = build_inp_pos();
  10001. auto * inp_attn = build_attn_inp_kv_unified();
  10002. const float kq_scale = hparams.f_attention_scale == 0.0f ? 1.0f/sqrtf(float(n_embd_head)) : hparams.f_attention_scale;
  10003. ggml_tensor * inp_out_ids = build_inp_out_ids();
  10004. for (int il = 0; il < n_layer; ++il) {
  10005. ggml_tensor * inpSA = inpL;
  10006. // norm
  10007. cur = build_norm(inpL,
  10008. model.layers[il].attn_norm, NULL,
  10009. LLM_NORM_RMS, il);
  10010. cb(cur, "attn_norm", il);
  10011. // self-attention
  10012. {
  10013. // rope freq factors for llama3; may return nullptr for llama2 and other models
  10014. ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
  10015. // compute Q and K and RoPE them
  10016. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  10017. cb(Qcur, "Qcur", il);
  10018. if (model.layers[il].bq) {
  10019. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  10020. cb(Qcur, "Qcur", il);
  10021. }
  10022. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  10023. cb(Kcur, "Kcur", il);
  10024. if (model.layers[il].bk) {
  10025. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  10026. cb(Kcur, "Kcur", il);
  10027. }
  10028. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  10029. cb(Vcur, "Vcur", il);
  10030. if (model.layers[il].bv) {
  10031. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  10032. cb(Vcur, "Vcur", il);
  10033. }
  10034. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  10035. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  10036. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  10037. Qcur = ggml_rope_ext(
  10038. ctx0, Qcur, inp_pos, rope_factors,
  10039. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  10040. ext_factor, attn_factor, beta_fast, beta_slow
  10041. );
  10042. Kcur = ggml_rope_ext(
  10043. ctx0, Kcur, inp_pos, rope_factors,
  10044. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  10045. ext_factor, attn_factor, beta_fast, beta_slow
  10046. );
  10047. cb(Qcur, "Qcur", il);
  10048. cb(Kcur, "Kcur", il);
  10049. cb(Vcur, "Vcur", il);
  10050. cur = build_attn(inp_attn,
  10051. model.layers[il].wo, model.layers[il].bo,
  10052. Qcur, Kcur, Vcur, nullptr, nullptr, kq_scale, il);
  10053. }
  10054. if (il == n_layer - 1 && inp_out_ids) {
  10055. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  10056. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  10057. }
  10058. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  10059. cb(ffn_inp, "ffn_inp", il);
  10060. cur = build_norm(ffn_inp,
  10061. model.layers[il].ffn_norm, NULL,
  10062. LLM_NORM_RMS, il);
  10063. cb(cur, "ffn_norm", il);
  10064. if ((uint32_t) il < hparams.n_layer_dense_lead) {
  10065. cur = build_ffn(cur,
  10066. model.layers[il].ffn_up, NULL, NULL,
  10067. model.layers[il].ffn_gate, NULL, NULL,
  10068. model.layers[il].ffn_down, NULL, NULL,
  10069. NULL,
  10070. LLM_FFN_SILU, LLM_FFN_PAR, il);
  10071. cb(cur, "ffn_out", il);
  10072. } else {
  10073. // MoE branch
  10074. ggml_tensor * moe_out =
  10075. build_moe_ffn(cur,
  10076. model.layers[il].ffn_gate_inp,
  10077. model.layers[il].ffn_up_exps,
  10078. model.layers[il].ffn_gate_exps,
  10079. model.layers[il].ffn_down_exps,
  10080. nullptr,
  10081. n_expert, n_expert_used,
  10082. LLM_FFN_SILU, false,
  10083. false, hparams.expert_weights_scale,
  10084. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  10085. il);
  10086. cb(moe_out, "ffn_moe_out", il);
  10087. // FFN shared expert
  10088. {
  10089. ggml_tensor * ffn_shexp = build_ffn(cur,
  10090. model.layers[il].ffn_up_shexp, NULL, NULL,
  10091. model.layers[il].ffn_gate_shexp, NULL, NULL,
  10092. model.layers[il].ffn_down_shexp, NULL, NULL,
  10093. NULL,
  10094. LLM_FFN_SILU, LLM_FFN_PAR, il);
  10095. cb(ffn_shexp, "ffn_shexp", il);
  10096. cur = ggml_add(ctx0, moe_out, ffn_shexp);
  10097. cb(cur, "ffn_out", il);
  10098. }
  10099. }
  10100. cur = ggml_add(ctx0, cur, ffn_inp);
  10101. cur = build_cvec(cur, il);
  10102. cb(cur, "l_out", il);
  10103. // input for next layer
  10104. inpL = cur;
  10105. }
  10106. cur = inpL;
  10107. cur = build_norm(cur,
  10108. model.output_norm, NULL,
  10109. LLM_NORM_RMS, -1);
  10110. cb(cur, "result_norm", -1);
  10111. res->t_embd = cur;
  10112. // lm_head
  10113. cur = build_lora_mm(model.output, cur);
  10114. cb(cur, "result_output", -1);
  10115. res->t_logits = cur;
  10116. ggml_build_forward_expand(gf, cur);
  10117. }
  10118. };
  10119. struct llm_build_deepseek2 : public llm_graph_context {
  10120. llm_build_deepseek2(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  10121. bool is_lite = (hparams.n_layer == 27);
  10122. const bool is_mla = (hparams.n_embd_head_k_mla != 0 && hparams.n_embd_head_v_mla != 0);
  10123. // note: these are the actual head sizes you get when treating as MHA or after "decompression" using wv_b for MLA
  10124. const int64_t n_embd_head_k = is_mla ? hparams.n_embd_head_k_mla : hparams.n_embd_head_k;
  10125. const int64_t n_embd_head_v = is_mla ? hparams.n_embd_head_v_mla : hparams.n_embd_head_v;
  10126. const int64_t n_embd_head_qk_rope = hparams.n_rot;
  10127. const int64_t n_embd_head_qk_nope = n_embd_head_k - n_embd_head_qk_rope;
  10128. const uint32_t kv_lora_rank = hparams.n_lora_kv;
  10129. // We have to pre-scale kq_scale and attn_factor to make the YaRN RoPE work correctly.
  10130. // See https://github.com/ggerganov/llama.cpp/discussions/7416 for detailed explanation.
  10131. const float mscale = attn_factor * (1.0f + hparams.rope_yarn_log_mul * logf(1.0f / freq_scale));
  10132. const float kq_scale = 1.0f*mscale*mscale/sqrtf(float(n_embd_head_k));
  10133. const float attn_factor = 1.0f / (1.0f + 0.1f * logf(1.0f / freq_scale));
  10134. ggml_tensor * cur;
  10135. ggml_tensor * inpL;
  10136. // {n_embd, n_tokens}
  10137. inpL = build_inp_embd(model.tok_embd);
  10138. // inp_pos - contains the positions
  10139. ggml_tensor * inp_pos = build_inp_pos();
  10140. auto * inp_attn = build_attn_inp_kv_unified();
  10141. ggml_tensor * inp_out_ids = build_inp_out_ids();
  10142. for (int il = 0; il < n_layer; ++il) {
  10143. ggml_tensor * inpSA = inpL;
  10144. // norm
  10145. cur = build_norm(inpL,
  10146. model.layers[il].attn_norm, NULL,
  10147. LLM_NORM_RMS, il);
  10148. cb(cur, "attn_norm", il);
  10149. // self_attention
  10150. {
  10151. ggml_tensor * q = NULL;
  10152. if (!is_lite) {
  10153. q = ggml_mul_mat(ctx0, model.layers[il].wq_a, cur);
  10154. cb(q, "q", il);
  10155. q = build_norm(q,
  10156. model.layers[il].attn_q_a_norm, nullptr,
  10157. LLM_NORM_RMS, il);
  10158. cb(q, "q", il);
  10159. q = ggml_mul_mat(ctx0, model.layers[il].wq_b, q);
  10160. cb(q, "q", il);
  10161. } else {
  10162. q = ggml_mul_mat(ctx0, model.layers[il].wq, cur);
  10163. cb(q, "q", il);
  10164. }
  10165. // split into {n_embd_head_qk_nope, n_head, n_tokens}
  10166. ggml_tensor * q_nope = ggml_view_3d(ctx0, q,
  10167. n_embd_head_qk_nope, n_head, n_tokens,
  10168. ggml_row_size(q->type, n_embd_head_k),
  10169. ggml_row_size(q->type, n_embd_head_k) * n_head,
  10170. 0);
  10171. cb(q_nope, "q_nope", il);
  10172. // and {n_embd_head_qk_rope, n_head, n_tokens}
  10173. ggml_tensor * q_pe = ggml_view_3d(ctx0, q,
  10174. n_embd_head_qk_rope, n_head, n_tokens,
  10175. ggml_row_size(q->type, n_embd_head_k),
  10176. ggml_row_size(q->type, n_embd_head_k) * n_head,
  10177. ggml_row_size(q->type, n_embd_head_qk_nope));
  10178. cb(q_pe, "q_pe", il);
  10179. ggml_tensor * kv_cmpr_pe = ggml_mul_mat(ctx0, model.layers[il].wkv_a_mqa, cur);
  10180. cb(kv_cmpr_pe, "kv_cmpr_pe", il);
  10181. // split into {kv_lora_rank, n_tokens}
  10182. ggml_tensor * kv_cmpr = ggml_view_2d(ctx0, kv_cmpr_pe,
  10183. kv_lora_rank, n_tokens,
  10184. ggml_row_size(kv_cmpr_pe->type, kv_lora_rank + n_embd_head_qk_rope),
  10185. 0);
  10186. cb(kv_cmpr, "kv_cmpr", il);
  10187. // and {n_embd_head_qk_rope, 1, n_tokens}
  10188. ggml_tensor * k_pe = ggml_view_3d(ctx0, kv_cmpr_pe,
  10189. n_embd_head_qk_rope, 1, n_tokens,
  10190. ggml_row_size(kv_cmpr_pe->type, kv_lora_rank + n_embd_head_qk_rope),
  10191. ggml_row_size(kv_cmpr_pe->type, kv_lora_rank + n_embd_head_qk_rope),
  10192. ggml_row_size(kv_cmpr_pe->type, kv_lora_rank));
  10193. cb(k_pe, "k_pe", il);
  10194. q_pe = ggml_rope_ext(ctx0, q_pe, inp_pos, nullptr,
  10195. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  10196. ext_factor, attn_factor, beta_fast, beta_slow
  10197. );
  10198. cb(q_pe, "q_pe", il);
  10199. k_pe = ggml_rope_ext(ctx0, k_pe, inp_pos, nullptr,
  10200. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  10201. ext_factor, attn_factor, beta_fast, beta_slow
  10202. );
  10203. cb(k_pe, "k_pe", il);
  10204. kv_cmpr = build_norm(kv_cmpr,
  10205. model.layers[il].attn_kv_a_norm, nullptr,
  10206. LLM_NORM_RMS, il);
  10207. cb(kv_cmpr, "kv_cmpr", il);
  10208. if (is_mla) {
  10209. // {n_embd_head_qk_nope, n_tokens, n_head}
  10210. q_nope = ggml_permute(ctx0, q_nope, 0, 2, 1, 3);
  10211. cb(q_nope, "q_nope_perm", il);
  10212. // {n_embd_head_qk_nope, kv_lora_rank, n_head} x {n_embd_head_qk_nope, n_tokens, n_head}
  10213. ggml_tensor * q_nope_absorbed = ggml_mul_mat(ctx0, model.layers[il].wk_b, q_nope);
  10214. cb(q_nope_absorbed, "q_nope_absorbed", il);
  10215. // {kv_lora_rank, n_head, n_tokens}
  10216. q_nope_absorbed = ggml_permute(ctx0, q_nope_absorbed, 0, 2, 1, 3);
  10217. cb(q_nope_absorbed, "q_nope_absorbed_perm", il);
  10218. // {n_embd_head_qk_rope + kv_lora_rank, n_head, n_tokens}
  10219. // note: rope must go first for in-place context shifting in build_rope_shift()
  10220. ggml_tensor * Qcur = ggml_concat(ctx0, q_pe, q_nope_absorbed, 0);
  10221. cb(Qcur, "Qcur", il);
  10222. kv_cmpr = ggml_reshape_3d(ctx0, kv_cmpr, kv_lora_rank, 1, n_tokens);
  10223. cb(kv_cmpr, "kv_cmpr_reshape", il);
  10224. // {n_embd_head_qk_rope + kv_lora_rank, 1, n_tokens}
  10225. ggml_tensor * Kcur = ggml_concat(ctx0, k_pe, kv_cmpr, 0);
  10226. cb(Kcur, "Kcur", il);
  10227. // {kv_lora_rank, 1, n_tokens}
  10228. ggml_tensor * Vcur = kv_cmpr;
  10229. cb(Vcur, "Vcur", il);
  10230. // note: MLA with the absorption optimzation converts into MQA (ie: GQA with 1 group)
  10231. cur = build_attn(inp_attn,
  10232. model.layers[il].wo, NULL,
  10233. Qcur, Kcur, Vcur, nullptr, model.layers[il].wv_b, kq_scale, il);
  10234. } else {
  10235. ggml_tensor * kv = ggml_mul_mat(ctx0, model.layers[il].wkv_b, kv_cmpr);
  10236. cb(kv, "kv", il);
  10237. // split into {n_embd_head_qk_nope, n_head, n_tokens}
  10238. ggml_tensor * k_nope = ggml_view_3d(ctx0, kv,
  10239. n_embd_head_qk_nope, n_head, n_tokens,
  10240. ggml_row_size(kv->type, n_embd_head_qk_nope + n_embd_head_v),
  10241. ggml_row_size(kv->type, n_embd_head_qk_nope + n_embd_head_v) * n_head,
  10242. 0);
  10243. cb(k_nope, "k_nope_view", il);
  10244. // and {n_embd_head_v, n_head, n_tokens}
  10245. ggml_tensor * Vcur = ggml_view_3d(ctx0, kv,
  10246. n_embd_head_v, n_head, n_tokens,
  10247. ggml_row_size(kv->type, n_embd_head_qk_nope + n_embd_head_v),
  10248. ggml_row_size(kv->type, n_embd_head_qk_nope + n_embd_head_v) * n_head,
  10249. ggml_row_size(kv->type, n_embd_head_qk_nope));
  10250. cb(Vcur, "Vcur_view", il);
  10251. Vcur = ggml_cont(ctx0, Vcur);
  10252. cb(Vcur, "Vcur_cont", il);
  10253. // note: rope must go first for in-place context shifting in build_rope_shift()
  10254. ggml_tensor * Qcur = ggml_concat(ctx0, q_pe, q_nope, 0);
  10255. cb(Qcur, "Qcur", il);
  10256. ggml_tensor * Kcur = ggml_concat(ctx0, ggml_repeat(ctx0, k_pe, q_pe), k_nope, 0);
  10257. cb(Kcur, "Kcur", il);
  10258. // note: MLA without the absorption optimization converts into MHA (ie: GQA with full n_head groups)
  10259. cur = build_attn(inp_attn,
  10260. model.layers[il].wo, NULL,
  10261. Qcur, Kcur, Vcur, nullptr, nullptr, kq_scale, il);
  10262. }
  10263. }
  10264. if (il == n_layer - 1 && inp_out_ids) {
  10265. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  10266. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  10267. }
  10268. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  10269. cb(ffn_inp, "ffn_inp", il);
  10270. cur = build_norm(ffn_inp,
  10271. model.layers[il].ffn_norm, NULL,
  10272. LLM_NORM_RMS, il);
  10273. cb(cur, "ffn_norm", il);
  10274. if ((uint32_t) il < hparams.n_layer_dense_lead) {
  10275. cur = build_ffn(cur,
  10276. model.layers[il].ffn_up, NULL, NULL,
  10277. model.layers[il].ffn_gate, NULL, NULL,
  10278. model.layers[il].ffn_down, NULL, NULL,
  10279. NULL,
  10280. LLM_FFN_SILU, LLM_FFN_PAR, il);
  10281. cb(cur, "ffn_out", il);
  10282. } else {
  10283. // MoE branch
  10284. ggml_tensor * moe_out =
  10285. build_moe_ffn(cur,
  10286. model.layers[il].ffn_gate_inp,
  10287. model.layers[il].ffn_up_exps,
  10288. model.layers[il].ffn_gate_exps,
  10289. model.layers[il].ffn_down_exps,
  10290. model.layers[il].ffn_exp_probs_b,
  10291. n_expert, n_expert_used,
  10292. LLM_FFN_SILU, hparams.expert_weights_norm,
  10293. true, hparams.expert_weights_scale,
  10294. (llama_expert_gating_func_type) hparams.expert_gating_func,
  10295. il);
  10296. cb(moe_out, "ffn_moe_out", il);
  10297. // FFN shared expert
  10298. {
  10299. ggml_tensor * ffn_shexp = build_ffn(cur,
  10300. model.layers[il].ffn_up_shexp, NULL, NULL,
  10301. model.layers[il].ffn_gate_shexp, NULL, NULL,
  10302. model.layers[il].ffn_down_shexp, NULL, NULL,
  10303. NULL,
  10304. LLM_FFN_SILU, LLM_FFN_PAR, il);
  10305. cb(ffn_shexp, "ffn_shexp", il);
  10306. cur = ggml_add(ctx0, moe_out, ffn_shexp);
  10307. cb(cur, "ffn_out", il);
  10308. }
  10309. }
  10310. cur = ggml_add(ctx0, cur, ffn_inp);
  10311. cur = build_cvec(cur, il);
  10312. cb(cur, "l_out", il);
  10313. // input for next layer
  10314. inpL = cur;
  10315. }
  10316. cur = inpL;
  10317. cur = build_norm(cur,
  10318. model.output_norm, NULL,
  10319. LLM_NORM_RMS, -1);
  10320. cb(cur, "result_norm", -1);
  10321. res->t_embd = cur;
  10322. // lm_head
  10323. cur = ggml_mul_mat(ctx0, model.output, cur);
  10324. cb(cur, "result_output", -1);
  10325. res->t_logits = cur;
  10326. ggml_build_forward_expand(gf, cur);
  10327. }
  10328. };
  10329. struct llm_build_bitnet : public llm_graph_context {
  10330. llm_build_bitnet(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  10331. const int64_t n_embd_head = hparams.n_embd_head_v;
  10332. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  10333. ggml_tensor * cur;
  10334. ggml_tensor * inpL;
  10335. inpL = build_inp_embd(model.tok_embd);
  10336. // inp_pos - contains the positions
  10337. ggml_tensor * inp_pos = build_inp_pos();
  10338. auto * inp_attn = build_attn_inp_kv_unified();
  10339. ggml_tensor * inp_out_ids = build_inp_out_ids();
  10340. for (int il = 0; il < n_layer; ++il) {
  10341. ggml_tensor * inpSA = inpL;
  10342. cur = build_norm(inpL,
  10343. model.layers[il].attn_norm, NULL,
  10344. LLM_NORM_RMS, il);
  10345. cb(cur, "attn_norm", il);
  10346. // self-attention
  10347. {
  10348. // compute Q and K and RoPE them
  10349. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  10350. if (model.layers[il].wq_scale) {
  10351. Qcur = ggml_mul(ctx0, Qcur, model.layers[il].wq_scale);
  10352. }
  10353. cb(Qcur, "Qcur", il);
  10354. if (model.layers[il].bq) {
  10355. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  10356. cb(Qcur, "Qcur", il);
  10357. }
  10358. // B1.K
  10359. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  10360. if (model.layers[il].wk_scale) {
  10361. Kcur = ggml_mul(ctx0, Kcur, model.layers[il].wk_scale);
  10362. }
  10363. cb(Kcur, "Kcur", il);
  10364. if (model.layers[il].bk) {
  10365. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  10366. cb(Kcur, "Kcur", il);
  10367. }
  10368. // B1.V
  10369. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  10370. if (model.layers[il].wv_scale) {
  10371. Vcur = ggml_mul(ctx0, Vcur, model.layers[il].wv_scale);
  10372. }
  10373. cb(Vcur, "Vcur", il);
  10374. if (model.layers[il].bv) {
  10375. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  10376. cb(Vcur, "Vcur", il);
  10377. }
  10378. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  10379. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  10380. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  10381. Qcur = ggml_rope_ext(
  10382. ctx0, Qcur, inp_pos, nullptr,
  10383. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  10384. ext_factor, attn_factor, beta_fast, beta_slow
  10385. );
  10386. Kcur = ggml_rope_ext(
  10387. ctx0, Kcur, inp_pos, nullptr,
  10388. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  10389. ext_factor, attn_factor, beta_fast, beta_slow
  10390. );
  10391. cb(Qcur, "Qcur", il);
  10392. cb(Kcur, "Kcur", il);
  10393. cb(Vcur, "Vcur", il);
  10394. cur = build_attn(inp_attn,
  10395. NULL, NULL,
  10396. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  10397. cur = build_norm(cur,
  10398. model.layers[il].attn_sub_norm, NULL,
  10399. LLM_NORM_RMS, il);
  10400. cb(cur, "attn_sub_norm", il);
  10401. cur = build_lora_mm(model.layers[il].wo, cur);
  10402. if (model.layers[il].wo_scale) {
  10403. cur = ggml_mul(ctx0, cur, model.layers[il].wo_scale);
  10404. }
  10405. if (model.layers[il].bo) {
  10406. cur = ggml_add(ctx0, cur, model.layers[il].bo);
  10407. }
  10408. cb(cur, "attn_o_out", il);
  10409. }
  10410. if (il == n_layer - 1 && inp_out_ids) {
  10411. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  10412. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  10413. }
  10414. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  10415. cb(ffn_inp, "ffn_inp", il);
  10416. // feed-forward forward
  10417. cur = build_norm(ffn_inp,
  10418. model.layers[il].ffn_norm, NULL,
  10419. LLM_NORM_RMS, il);
  10420. cb(cur, "ffn_norm", il);
  10421. cur = build_ffn(cur,
  10422. model.layers[il].ffn_up, NULL, model.layers[il].ffn_up_scale,
  10423. model.layers[il].ffn_gate, NULL, model.layers[il].ffn_gate_scale,
  10424. NULL, NULL, NULL,
  10425. NULL,
  10426. LLM_FFN_SILU, LLM_FFN_PAR, il);
  10427. cb(cur, "ffn_sub_out", il);
  10428. cur = build_norm(cur,
  10429. model.layers[il].ffn_sub_norm, NULL,
  10430. LLM_NORM_RMS, il);
  10431. cb(cur, "ffn_sub_norm", il);
  10432. cur = build_lora_mm(model.layers[il].ffn_down, cur);
  10433. if (model.layers[il].ffn_down_scale) {
  10434. cur = ggml_mul(ctx0, cur, model.layers[il].ffn_down_scale);
  10435. }
  10436. cb(cur, "ffn_down", il);
  10437. cur = ggml_add(ctx0, cur, ffn_inp);
  10438. cb(cur, "l_out", il);
  10439. // input for next layer
  10440. inpL = cur;
  10441. }
  10442. cur = inpL;
  10443. cur = build_norm(cur,
  10444. model.output_norm, NULL,
  10445. LLM_NORM_RMS, -1);
  10446. cb(cur, "result_norm", -1);
  10447. res->t_embd = cur;
  10448. // lm_head
  10449. // FIXME: do not use model.tok_embd directly, duplicate as model.output
  10450. cur = build_lora_mm(model.tok_embd, cur);
  10451. cb(cur, "result_output", -1);
  10452. res->t_logits = cur;
  10453. ggml_build_forward_expand(gf, cur);
  10454. }
  10455. };
  10456. struct llm_build_t5_enc : public llm_graph_context {
  10457. llm_build_t5_enc(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  10458. const int64_t n_embd_head = hparams.n_embd_head_v;
  10459. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  10460. ggml_tensor * cur;
  10461. ggml_tensor * inpL;
  10462. inpL = build_inp_embd(model.tok_embd);
  10463. ggml_tensor * pos_bucket_enc = build_inp_pos_bucket_enc();
  10464. auto * inp_attn = build_attn_inp_no_cache();
  10465. ggml_tensor * inp_out_ids = build_inp_out_ids();
  10466. for (int il = 0; il < n_layer; ++il) {
  10467. ggml_tensor * inpSA = inpL;
  10468. // norm
  10469. cur = build_norm(inpL,
  10470. model.layers[il].attn_norm_enc, NULL,
  10471. LLM_NORM_RMS, il);
  10472. cb(cur, "attn_norm", il);
  10473. // self-attention
  10474. {
  10475. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq_enc, cur);
  10476. cb(Qcur, "Qcur", il);
  10477. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk_enc, cur);
  10478. cb(Kcur, "Kcur", il);
  10479. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv_enc, cur);
  10480. cb(Vcur, "Vcur", il);
  10481. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  10482. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  10483. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  10484. 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;
  10485. ggml_tensor * kq_b = build_pos_bias(pos_bucket_enc, attn_rel_b);
  10486. cur = build_attn(inp_attn,
  10487. model.layers[il].wo_enc, nullptr,
  10488. Qcur, Kcur, Vcur, kq_b, nullptr, 1.0f, il);
  10489. cb(cur, "kqv_out", il);
  10490. }
  10491. if (il == n_layer - 1 && inp_out_ids) {
  10492. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  10493. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  10494. }
  10495. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  10496. cb(ffn_inp, "ffn_inp", il);
  10497. // feed-forward network
  10498. {
  10499. cur = build_norm(ffn_inp,
  10500. model.layers[il].ffn_norm_enc, NULL,
  10501. LLM_NORM_RMS, il);
  10502. cb(cur, "ffn_norm", il);
  10503. // T5 uses relu, flan-T5 uses gelu-gated
  10504. cur = build_ffn(cur,
  10505. model.layers[il].ffn_up_enc, NULL, NULL,
  10506. model.layers[il].ffn_gate_enc, NULL, NULL,
  10507. model.layers[il].ffn_down_enc, NULL, NULL,
  10508. NULL,
  10509. model.layers[il].ffn_gate_enc ? LLM_FFN_GELU : LLM_FFN_RELU,
  10510. model.layers[il].ffn_gate_enc ? LLM_FFN_PAR : LLM_FFN_SEQ,
  10511. il);
  10512. cb(cur, "ffn_out", il);
  10513. }
  10514. cur = ggml_add(ctx0, cur, ffn_inp);
  10515. cb(cur, "ffn_out", il);
  10516. cur = build_cvec(cur, il);
  10517. cb(cur, "l_out", il);
  10518. // input for next layer
  10519. inpL = cur;
  10520. }
  10521. cur = inpL;
  10522. cb(cur, "result_embd", -1);
  10523. cur = build_norm(cur,
  10524. model.output_norm_enc, NULL,
  10525. LLM_NORM_RMS, -1);
  10526. cb(cur, "result_norm", -1);
  10527. res->t_embd = cur;
  10528. ggml_build_forward_expand(gf, cur);
  10529. }
  10530. };
  10531. struct llm_build_t5_dec : public llm_graph_context {
  10532. llm_build_t5_dec(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  10533. const int64_t n_embd_head = hparams.n_embd_head_v;
  10534. //const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  10535. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  10536. ggml_tensor * cur;
  10537. ggml_tensor * inpL;
  10538. inpL = build_inp_embd(model.tok_embd);
  10539. ggml_tensor * embd_enc = build_inp_cross_embd();
  10540. ggml_tensor * pos_bucket_dec = build_inp_pos_bucket_dec();
  10541. const int64_t n_outputs_enc = embd_enc->ne[1];
  10542. auto * inp_attn_self = build_attn_inp_kv_unified();
  10543. auto * inp_attn_cross = build_attn_inp_cross();
  10544. ggml_tensor * inp_out_ids = build_inp_out_ids();
  10545. for (int il = 0; il < n_layer; ++il) {
  10546. ggml_tensor * inpSA = inpL;
  10547. // norm
  10548. cur = build_norm(inpL,
  10549. model.layers[il].attn_norm, NULL,
  10550. LLM_NORM_RMS, il);
  10551. cb(cur, "attn_norm", il);
  10552. // self-attention
  10553. {
  10554. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  10555. cb(Qcur, "Qcur", il);
  10556. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  10557. cb(Kcur, "Kcur", il);
  10558. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  10559. cb(Vcur, "Vcur", il);
  10560. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  10561. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  10562. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  10563. ggml_tensor * attn_rel_b = model.layers[il].attn_rel_b ? model.layers[il].attn_rel_b : model.layers[0].attn_rel_b;
  10564. ggml_tensor * kq_b = build_pos_bias(pos_bucket_dec, attn_rel_b);
  10565. cur = build_attn(inp_attn_self,
  10566. model.layers[il].wo, model.layers[il].bo,
  10567. Qcur, Kcur, Vcur, kq_b, nullptr, 1.0f, il);
  10568. cb(cur, "kqv_out", il);
  10569. }
  10570. cur = ggml_add(ctx0, cur, inpSA);
  10571. cb(cur, "cross_inp", il);
  10572. ggml_tensor * inpCA = cur;
  10573. // norm
  10574. cur = build_norm(cur,
  10575. model.layers[il].attn_norm_cross, NULL,
  10576. LLM_NORM_RMS, il);
  10577. cb(cur, "attn_norm_cross", il);
  10578. // cross-attention
  10579. {
  10580. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq_cross, cur);
  10581. cb(Qcur, "Qcur", il);
  10582. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk_cross, embd_enc);
  10583. cb(Kcur, "Kcur", il);
  10584. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv_cross, embd_enc);
  10585. cb(Vcur, "Vcur", il);
  10586. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  10587. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_outputs_enc);
  10588. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_outputs_enc);
  10589. cur = build_attn(inp_attn_cross,
  10590. model.layers[il].wo_cross, nullptr,
  10591. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f, il);
  10592. cb(cur, "kqv_out", il);
  10593. //ggml_tensor * q = ggml_permute(ctx0, Qcur, 0, 2, 1, 3);
  10594. //ggml_tensor * k = ggml_cont(ctx0, ggml_permute(ctx0, Kcur, 0, 2, 1, 3));
  10595. //ggml_tensor * kq = ggml_mul_mat(ctx0, k, q);
  10596. //cb(kq, "kq", il);
  10597. //kq = ggml_soft_max_ext(ctx0, kq, KQ_mask_cross, 1.0f, hparams.f_max_alibi_bias);
  10598. //cb(kq, "kq_soft_max_ext", il);
  10599. //ggml_tensor * v = ggml_cont(ctx0, ggml_transpose(ctx0, ggml_reshape_2d(ctx0, Vcur, n_embd_gqa, n_outputs_enc)));
  10600. //cb(v, "v", il);
  10601. //ggml_tensor * kqv = ggml_mul_mat(ctx0, ggml_reshape_3d(ctx0, v, n_outputs_enc, n_embd_head, n_head_kv), kq);
  10602. //cb(kqv, "kqv", il);
  10603. //ggml_tensor * kqv_merged = ggml_permute(ctx0, kqv, 0, 2, 1, 3);
  10604. //cb(kqv_merged, "kqv_merged", il);
  10605. //cur = ggml_cont_2d(ctx0, kqv_merged, n_embd_gqa, n_tokens);
  10606. //cb(cur, "kqv_merged_cont", il);
  10607. //ggml_build_forward_expand(gf, cur);
  10608. //cur = build_lora_mm(model.layers[il].wo_cross, cur);
  10609. //cb(cur, "kqv_out", il);
  10610. }
  10611. if (il == n_layer - 1 && inp_out_ids) {
  10612. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  10613. inpCA = ggml_get_rows(ctx0, inpCA, inp_out_ids);
  10614. }
  10615. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpCA);
  10616. cb(ffn_inp, "ffn_inp", il);
  10617. // feed-forward network
  10618. {
  10619. cur = build_norm(ffn_inp,
  10620. model.layers[il].ffn_norm, NULL,
  10621. LLM_NORM_RMS, il);
  10622. cb(cur, "ffn_norm", il);
  10623. // T5 uses relu, flan-T5 uses gelu-gated
  10624. cur = build_ffn(cur,
  10625. model.layers[il].ffn_up, NULL, NULL,
  10626. model.layers[il].ffn_gate, NULL, NULL,
  10627. model.layers[il].ffn_down, NULL, NULL,
  10628. NULL,
  10629. model.layers[il].ffn_gate_enc ? LLM_FFN_GELU : LLM_FFN_RELU,
  10630. model.layers[il].ffn_gate_enc ? LLM_FFN_PAR : LLM_FFN_SEQ,
  10631. il);
  10632. cb(cur, "ffn_out", il);
  10633. }
  10634. cur = ggml_add(ctx0, cur, ffn_inp);
  10635. cb(cur, "ffn_out", il);
  10636. cur = build_cvec(cur, il);
  10637. cb(cur, "l_out", il);
  10638. // input for next layer
  10639. inpL = cur;
  10640. }
  10641. cur = inpL;
  10642. cb(cur, "result_embd", -1);
  10643. cur = build_norm(cur,
  10644. model.output_norm, NULL,
  10645. LLM_NORM_RMS, -1);
  10646. cb(cur, "result_norm", -1);
  10647. res->t_embd = cur;
  10648. // lm_head
  10649. cur = build_lora_mm(model.output, cur);
  10650. cb(cur, "result_output", -1);
  10651. res->t_logits = cur;
  10652. ggml_build_forward_expand(gf, cur);
  10653. }
  10654. };
  10655. struct llm_build_jais : public llm_graph_context {
  10656. llm_build_jais(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  10657. const int64_t n_embd_head = hparams.n_embd_head_v;
  10658. const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  10659. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  10660. ggml_tensor * cur;
  10661. ggml_tensor * inpL;
  10662. inpL = build_inp_embd(model.tok_embd);
  10663. auto * inp_attn = build_attn_inp_kv_unified();
  10664. ggml_tensor * inp_out_ids = build_inp_out_ids();
  10665. for (int il = 0; il < n_layer; ++il) {
  10666. cur = build_norm(inpL,
  10667. model.layers[il].attn_norm,
  10668. model.layers[il].attn_norm_b,
  10669. LLM_NORM, il);
  10670. cb(cur, "attn_norm", il);
  10671. // self-attention
  10672. {
  10673. cur = build_lora_mm(model.layers[il].wqkv, cur);
  10674. cb(cur, "wqkv", il);
  10675. cur = ggml_add(ctx0, cur, model.layers[il].bqkv);
  10676. cb(cur, "bqkv", il);
  10677. ggml_tensor * Qcur = ggml_view_2d(ctx0, cur, n_embd, n_tokens, cur->nb[1], 0*cur->nb[0]*(n_embd));
  10678. ggml_tensor * Kcur = ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*cur->nb[0]*(n_embd));
  10679. ggml_tensor * Vcur = ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*cur->nb[0]*(n_embd + n_embd_gqa));
  10680. cb(Qcur, "Qcur", il);
  10681. cb(Kcur, "Kcur", il);
  10682. cb(Vcur, "Vcur", il);
  10683. Qcur = ggml_cont_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  10684. Kcur = ggml_cont_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  10685. Vcur = ggml_cont_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  10686. cur = build_attn(inp_attn,
  10687. model.layers[il].wo, model.layers[il].bo,
  10688. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/float(n_embd_head), il);
  10689. }
  10690. if (il == n_layer - 1 && inp_out_ids) {
  10691. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  10692. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  10693. }
  10694. // add the input
  10695. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
  10696. cb(ffn_inp, "ffn_inp", il);
  10697. // FF
  10698. {
  10699. cur = build_norm(ffn_inp,
  10700. model.layers[il].ffn_norm,
  10701. model.layers[il].ffn_norm_b,
  10702. LLM_NORM, il);
  10703. cb(cur, "ffn_norm", il);
  10704. cur = build_ffn(cur,
  10705. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  10706. model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, NULL,
  10707. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  10708. NULL,
  10709. LLM_FFN_SILU, LLM_FFN_PAR, il);
  10710. cb(cur, "ffn_out", il);
  10711. }
  10712. inpL = ggml_add(ctx0, cur, ffn_inp);
  10713. cb(inpL, "l_out", il);
  10714. }
  10715. cur = build_norm(inpL,
  10716. model.output_norm,
  10717. model.output_norm_b,
  10718. LLM_NORM, -1);
  10719. cb(cur, "result_norm", -1);
  10720. res->t_embd = cur;
  10721. cur = build_lora_mm(model.output, cur);
  10722. cb(cur, "result_output", -1);
  10723. res->t_logits = cur;
  10724. ggml_build_forward_expand(gf, cur);
  10725. }
  10726. };
  10727. struct llm_build_chatglm : public llm_graph_context {
  10728. llm_build_chatglm(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  10729. const int64_t n_embd_head = hparams.n_embd_head_v;
  10730. const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  10731. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  10732. ggml_tensor * cur;
  10733. ggml_tensor * inpL;
  10734. inpL = build_inp_embd(model.tok_embd);
  10735. // inp_pos - contains the positions
  10736. ggml_tensor * inp_pos = build_inp_pos();
  10737. auto * inp_attn = build_attn_inp_kv_unified();
  10738. ggml_tensor * inp_out_ids = build_inp_out_ids();
  10739. for (int il = 0; il < n_layer; ++il) {
  10740. ggml_tensor * inpSA = inpL;
  10741. cur = build_norm(inpL,
  10742. model.layers[il].attn_norm,
  10743. NULL,
  10744. LLM_NORM_RMS, il);
  10745. cb(cur, "attn_norm", il);
  10746. // self-attention
  10747. {
  10748. ggml_tensor * Qcur = nullptr;
  10749. ggml_tensor * Kcur = nullptr;
  10750. ggml_tensor * Vcur = nullptr;
  10751. if (model.layers[il].wqkv == nullptr) {
  10752. Qcur = build_lora_mm(model.layers[il].wq, cur);
  10753. if (model.layers[il].bq) {
  10754. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  10755. }
  10756. Kcur = build_lora_mm(model.layers[il].wk, cur);
  10757. if (model.layers[il].bk) {
  10758. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  10759. }
  10760. Vcur = build_lora_mm(model.layers[il].wv, cur);
  10761. if (model.layers[il].bv) {
  10762. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  10763. }
  10764. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  10765. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  10766. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  10767. } else {
  10768. cur = build_lora_mm(model.layers[il].wqkv, cur);
  10769. cb(cur, "wqkv", il);
  10770. if (model.layers[il].bqkv) {
  10771. cur = ggml_add(ctx0, cur, model.layers[il].bqkv);
  10772. cb(cur, "bqkv", il);
  10773. }
  10774. 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));
  10775. 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));
  10776. Vcur = ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd + n_embd_gqa));
  10777. Vcur = ggml_cont_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  10778. }
  10779. //printf("freq_base: %f freq_scale: %f ext_factor: %f attn_factor: %f\n", freq_base, freq_scale, ext_factor, attn_factor);
  10780. Qcur = ggml_rope_ext(
  10781. ctx0, Qcur, inp_pos, nullptr,
  10782. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  10783. ext_factor, attn_factor, beta_fast, beta_slow
  10784. );
  10785. Kcur = ggml_rope_ext(
  10786. ctx0, Kcur, inp_pos, nullptr,
  10787. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  10788. ext_factor, attn_factor, beta_fast, beta_slow
  10789. );
  10790. cb(Qcur, "Qcur", il);
  10791. cb(Kcur, "Kcur", il);
  10792. cb(Vcur, "Vcur", il);
  10793. cur = build_attn(inp_attn,
  10794. model.layers[il].wo, NULL,
  10795. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  10796. }
  10797. if (il == n_layer - 1 && inp_out_ids) {
  10798. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  10799. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  10800. }
  10801. // Add the input
  10802. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  10803. cb(ffn_inp, "ffn_inp", il);
  10804. // FF
  10805. {
  10806. cur = build_norm(ffn_inp,
  10807. model.layers[il].ffn_norm,
  10808. NULL,
  10809. LLM_NORM_RMS, il);
  10810. cb(cur, "ffn_norm", il);
  10811. cur = build_ffn(cur,
  10812. model.layers[il].ffn_up, NULL, NULL,
  10813. NULL, NULL, NULL,
  10814. model.layers[il].ffn_down, NULL, NULL,
  10815. NULL,
  10816. LLM_FFN_SWIGLU, LLM_FFN_SEQ, il);
  10817. cb(cur, "ffn_out", il);
  10818. }
  10819. inpL = ggml_add(ctx0, cur, ffn_inp);
  10820. cb(inpL, "l_out", il);
  10821. }
  10822. cur = build_norm(inpL,
  10823. model.output_norm,
  10824. NULL,
  10825. LLM_NORM_RMS, -1);
  10826. cb(cur, "result_norm", -1);
  10827. res->t_embd = cur;
  10828. cur = build_lora_mm(model.output, cur);
  10829. cb(cur, "result_output", -1);
  10830. res->t_logits = cur;
  10831. ggml_build_forward_expand(gf, cur);
  10832. }
  10833. };
  10834. struct llm_build_glm4 : public llm_graph_context {
  10835. llm_build_glm4(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  10836. const int64_t n_embd_head = hparams.n_embd_head_v;
  10837. const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  10838. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  10839. ggml_tensor * cur;
  10840. ggml_tensor * inpL;
  10841. inpL = build_inp_embd(model.tok_embd);
  10842. // inp_pos - contains the positions
  10843. ggml_tensor * inp_pos = build_inp_pos();
  10844. auto * inp_attn = build_attn_inp_kv_unified();
  10845. ggml_tensor * inp_out_ids = build_inp_out_ids();
  10846. for (int il = 0; il < n_layer; ++il) {
  10847. ggml_tensor * inpSA = inpL;
  10848. // Pre-attention norm
  10849. cur = build_norm(inpL,
  10850. model.layers[il].attn_norm,
  10851. NULL,
  10852. LLM_NORM_RMS, il);
  10853. cb(cur, "attn_norm", il);
  10854. // self-attention
  10855. {
  10856. ggml_tensor * Qcur = nullptr;
  10857. ggml_tensor * Kcur = nullptr;
  10858. ggml_tensor * Vcur = nullptr;
  10859. if (model.layers[il].wqkv == nullptr) {
  10860. Qcur = build_lora_mm(model.layers[il].wq, cur);
  10861. if (model.layers[il].bq) {
  10862. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  10863. }
  10864. Kcur = build_lora_mm(model.layers[il].wk, cur);
  10865. if (model.layers[il].bk) {
  10866. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  10867. }
  10868. Vcur = build_lora_mm(model.layers[il].wv, cur);
  10869. if (model.layers[il].bv) {
  10870. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  10871. }
  10872. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  10873. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  10874. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  10875. } else {
  10876. cur = build_lora_mm(model.layers[il].wqkv, cur);
  10877. cb(cur, "wqkv", il);
  10878. if (model.layers[il].bqkv) {
  10879. cur = ggml_add(ctx0, cur, model.layers[il].bqkv);
  10880. cb(cur, "bqkv", il);
  10881. }
  10882. 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));
  10883. 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));
  10884. Vcur = ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd + n_embd_gqa));
  10885. Vcur = ggml_cont_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  10886. }
  10887. Qcur = ggml_rope_ext(
  10888. ctx0, Qcur, inp_pos, nullptr,
  10889. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  10890. ext_factor, attn_factor, beta_fast, beta_slow
  10891. );
  10892. Kcur = ggml_rope_ext(
  10893. ctx0, Kcur, inp_pos, nullptr,
  10894. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  10895. ext_factor, attn_factor, beta_fast, beta_slow
  10896. );
  10897. cb(Qcur, "Qcur", il);
  10898. cb(Kcur, "Kcur", il);
  10899. cb(Vcur, "Vcur", il);
  10900. cur = build_attn(inp_attn,
  10901. model.layers[il].wo, NULL,
  10902. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  10903. }
  10904. if (il == n_layer - 1 && inp_out_ids) {
  10905. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  10906. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  10907. }
  10908. // Post-attention norm (new!)
  10909. cur = build_norm(cur,
  10910. model.layers[il].attn_post_norm,
  10911. NULL,
  10912. LLM_NORM_RMS, il);
  10913. cb(cur, "post_attn_norm", il);
  10914. // Add the input (residual connection after post-attention norm)
  10915. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  10916. cb(ffn_inp, "ffn_inp", il);
  10917. // FF
  10918. {
  10919. // Pre-MLP norm
  10920. cur = build_norm(ffn_inp,
  10921. model.layers[il].ffn_norm,
  10922. NULL,
  10923. LLM_NORM_RMS, il);
  10924. cb(cur, "ffn_norm", il);
  10925. // MLP
  10926. cur = build_ffn(cur,
  10927. model.layers[il].ffn_up, NULL, NULL,
  10928. NULL, NULL, NULL,
  10929. model.layers[il].ffn_down, NULL, NULL,
  10930. NULL,
  10931. LLM_FFN_SWIGLU, LLM_FFN_SEQ, il);
  10932. cb(cur, "ffn_out", il);
  10933. // Post-MLP norm
  10934. cur = build_norm(cur,
  10935. model.layers[il].ffn_post_norm,
  10936. NULL,
  10937. LLM_NORM_RMS, il);
  10938. cb(cur, "post_mlp_norm", il);
  10939. }
  10940. // Add residual connection after post-MLP norm
  10941. inpL = ggml_add(ctx0, cur, ffn_inp);
  10942. cb(inpL, "l_out", il);
  10943. }
  10944. // Final norm
  10945. cur = build_norm(inpL,
  10946. model.output_norm,
  10947. NULL,
  10948. LLM_NORM_RMS, -1);
  10949. cb(cur, "result_norm", -1);
  10950. res->t_embd = cur;
  10951. // Output projection
  10952. cur = build_lora_mm(model.output, cur);
  10953. cb(cur, "result_output", -1);
  10954. res->t_logits = cur;
  10955. ggml_build_forward_expand(gf, cur);
  10956. }
  10957. };
  10958. struct llm_build_glm4_moe : public llm_graph_context {
  10959. llm_build_glm4_moe(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  10960. const int64_t n_embd_head = hparams.n_embd_head_v;
  10961. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  10962. ggml_tensor * cur;
  10963. ggml_tensor * inpL;
  10964. inpL = build_inp_embd(model.tok_embd);
  10965. // inp_pos - contains the positions
  10966. ggml_tensor * inp_pos = build_inp_pos();
  10967. auto * inp_attn = build_attn_inp_kv_unified();
  10968. ggml_tensor * inp_out_ids = build_inp_out_ids();
  10969. // Only process up to last layer (skip final NextN layer)
  10970. // Final layer tensors are loaded but not processed in forward pass
  10971. const int n_transformer_layers = n_layer - hparams.nextn_predict_layers;
  10972. for (int il = 0; il < n_transformer_layers; ++il) {
  10973. ggml_tensor * inpSA = inpL;
  10974. // Pre-attention norm
  10975. cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
  10976. cb(cur, "attn_norm", il);
  10977. // self-attention
  10978. {
  10979. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  10980. if (model.layers[il].bq) {
  10981. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  10982. }
  10983. cb(Qcur, "Qcur", il);
  10984. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  10985. if (model.layers[il].bk) {
  10986. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  10987. }
  10988. cb(Kcur, "Kcur", il);
  10989. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  10990. if (model.layers[il].bv) {
  10991. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  10992. }
  10993. cb(Vcur, "Vcur", il);
  10994. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  10995. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  10996. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  10997. // Apply Q/K norm if available (GLM-4.5 355B variant)
  10998. if (model.layers[il].attn_q_norm) {
  10999. Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);
  11000. cb(Qcur, "Qcur_normed", il);
  11001. }
  11002. if (model.layers[il].attn_k_norm) {
  11003. Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);
  11004. cb(Kcur, "Kcur_normed", il);
  11005. }
  11006. Qcur = ggml_rope_ext(
  11007. ctx0, Qcur, inp_pos, nullptr,
  11008. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  11009. ext_factor, attn_factor, beta_fast, beta_slow
  11010. );
  11011. Kcur = ggml_rope_ext(
  11012. ctx0, Kcur, inp_pos, nullptr,
  11013. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  11014. ext_factor, attn_factor, beta_fast, beta_slow
  11015. );
  11016. cb(Qcur, "Qcur", il);
  11017. cb(Kcur, "Kcur", il);
  11018. cb(Vcur, "Vcur", il);
  11019. cur = build_attn(inp_attn,
  11020. model.layers[il].wo, NULL,
  11021. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  11022. }
  11023. if (il == n_transformer_layers - 1 && inp_out_ids) {
  11024. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  11025. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  11026. }
  11027. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  11028. cb(ffn_inp, "ffn_inp", il);
  11029. // Post-attention norm
  11030. cur = build_norm(ffn_inp, model.layers[il].attn_post_norm, NULL, LLM_NORM_RMS, il);
  11031. cb(cur, "post_attn_norm", il);
  11032. // Check if this is a dense layer (n_layer_dense_lead=1, so layer 0 is dense)
  11033. if (static_cast<uint32_t>(il) < hparams.n_layer_dense_lead) {
  11034. // Dense FFN layer
  11035. cur = build_ffn(cur,
  11036. model.layers[il].ffn_up, NULL, NULL,
  11037. model.layers[il].ffn_gate, NULL, NULL,
  11038. model.layers[il].ffn_down, NULL, NULL,
  11039. NULL,
  11040. LLM_FFN_SILU, LLM_FFN_PAR, il);
  11041. cb(cur, "ffn_out", il);
  11042. } else {
  11043. // Process routed experts using existing MoE infrastructure
  11044. ggml_tensor * routed_out = build_moe_ffn(cur,
  11045. model.layers[il].ffn_gate_inp,
  11046. model.layers[il].ffn_up_exps,
  11047. model.layers[il].ffn_gate_exps,
  11048. model.layers[il].ffn_down_exps,
  11049. model.layers[il].ffn_exp_probs_b,
  11050. n_expert, n_expert_used,
  11051. LLM_FFN_SILU, hparams.expert_weights_norm,
  11052. true, hparams.expert_weights_scale,
  11053. (llama_expert_gating_func_type) hparams.expert_gating_func,
  11054. il);
  11055. cb(routed_out, "ffn_moe_out", il);
  11056. // Process shared expert on original input
  11057. ggml_tensor * shared_out = 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(shared_out, "ffn_shexp_out", il);
  11064. // Final output: routed_output + shared_output
  11065. cur = ggml_add(ctx0, routed_out, shared_out);
  11066. cb(cur, "ffn_out", il);
  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, model.output_norm, NULL, LLM_NORM_RMS, -1);
  11076. cb(cur, "result_norm", -1);
  11077. res->t_embd = cur;
  11078. // lm_head
  11079. cur = build_lora_mm(model.output, cur);
  11080. cb(cur, "result_output", -1);
  11081. res->t_logits = cur;
  11082. ggml_build_forward_expand(gf, cur);
  11083. }
  11084. };
  11085. struct llm_build_nemotron : public llm_graph_context {
  11086. llm_build_nemotron(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  11087. const int64_t n_embd_head = hparams.n_embd_head_v;
  11088. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  11089. //GGML_ASSERT(n_embd_head == hparams.n_rot);
  11090. ggml_tensor * cur;
  11091. ggml_tensor * inpL;
  11092. inpL = build_inp_embd(model.tok_embd);
  11093. // inp_pos - contains the positions
  11094. ggml_tensor * inp_pos = build_inp_pos();
  11095. auto * inp_attn = build_attn_inp_kv_unified();
  11096. ggml_tensor * inp_out_ids = build_inp_out_ids();
  11097. for (int il = 0; il < n_layer; ++il) {
  11098. ggml_tensor * inpSA = inpL;
  11099. // norm
  11100. cur = build_norm(inpL,
  11101. model.layers[il].attn_norm,
  11102. model.layers[il].attn_norm_b,
  11103. LLM_NORM, il);
  11104. cb(cur, "attn_norm", il);
  11105. // self-attention
  11106. {
  11107. // compute Q and K and RoPE them
  11108. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  11109. cb(Qcur, "Qcur", il);
  11110. if (model.layers[il].bq) {
  11111. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  11112. cb(Qcur, "Qcur", il);
  11113. }
  11114. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  11115. cb(Kcur, "Kcur", il);
  11116. if (model.layers[il].bk) {
  11117. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  11118. cb(Kcur, "Kcur", il);
  11119. }
  11120. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  11121. cb(Vcur, "Vcur", il);
  11122. if (model.layers[il].bv) {
  11123. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  11124. cb(Vcur, "Vcur", il);
  11125. }
  11126. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  11127. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  11128. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  11129. Qcur = ggml_rope_ext(
  11130. ctx0, Qcur, inp_pos, nullptr,
  11131. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  11132. ext_factor, attn_factor, beta_fast, beta_slow
  11133. );
  11134. Kcur = ggml_rope_ext(
  11135. ctx0, Kcur, inp_pos, nullptr,
  11136. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  11137. ext_factor, attn_factor, beta_fast, beta_slow
  11138. );
  11139. cb(Qcur, "Qcur", il);
  11140. cb(Kcur, "Kcur", il);
  11141. cb(Vcur, "Vcur", il);
  11142. cur = build_attn(inp_attn,
  11143. model.layers[il].wo, model.layers[il].bo,
  11144. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  11145. }
  11146. if (il == n_layer - 1 && inp_out_ids) {
  11147. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  11148. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  11149. }
  11150. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  11151. cb(ffn_inp, "ffn_inp", il);
  11152. // feed-forward network
  11153. cur = build_norm(ffn_inp,
  11154. model.layers[il].ffn_norm,
  11155. model.layers[il].ffn_norm_b,
  11156. LLM_NORM, il);
  11157. cb(cur, "ffn_norm", il);
  11158. cur = build_ffn(cur,
  11159. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  11160. NULL, NULL, NULL,
  11161. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  11162. NULL,
  11163. LLM_FFN_RELU_SQR, LLM_FFN_SEQ, il);
  11164. cur = ggml_add(ctx0, cur, ffn_inp);
  11165. cb(cur, "ffn_out", il);
  11166. cur = build_cvec(cur, il);
  11167. cb(cur, "l_out", il);
  11168. // input for next layer
  11169. inpL = cur;
  11170. }
  11171. cur = inpL;
  11172. cur = build_norm(cur,
  11173. model.output_norm, model.output_norm_b,
  11174. LLM_NORM, -1);
  11175. cb(cur, "result_norm", -1);
  11176. res->t_embd = cur;
  11177. // lm_head
  11178. cur = build_lora_mm(model.output, cur);
  11179. cb(cur, "result_output", -1);
  11180. res->t_logits = cur;
  11181. ggml_build_forward_expand(gf, cur);
  11182. }
  11183. };
  11184. struct llm_build_exaone : public llm_graph_context {
  11185. llm_build_exaone(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  11186. const int64_t n_embd_head = hparams.n_embd_head_v;
  11187. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  11188. GGML_ASSERT(n_embd_head == hparams.n_rot);
  11189. ggml_tensor * cur;
  11190. ggml_tensor * inpL;
  11191. inpL = build_inp_embd(model.tok_embd);
  11192. // inp_pos - contains the positions
  11193. ggml_tensor * inp_pos = build_inp_pos();
  11194. auto * inp_attn = build_attn_inp_kv_unified();
  11195. ggml_tensor * inp_out_ids = build_inp_out_ids();
  11196. for (int il = 0; il < n_layer; ++il) {
  11197. ggml_tensor * inpSA = inpL;
  11198. // norm
  11199. cur = build_norm(inpL,
  11200. model.layers[il].attn_norm, NULL,
  11201. LLM_NORM_RMS, il);
  11202. cb(cur, "attn_norm", il);
  11203. // self-attention
  11204. {
  11205. // rope freq factors for llama3; may return nullptr for llama2 and other models
  11206. ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
  11207. // compute Q and K and RoPE them
  11208. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  11209. cb(Qcur, "Qcur", il);
  11210. if (model.layers[il].bq) {
  11211. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  11212. cb(Qcur, "Qcur", il);
  11213. }
  11214. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  11215. cb(Kcur, "Kcur", il);
  11216. if (model.layers[il].bk) {
  11217. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  11218. cb(Kcur, "Kcur", il);
  11219. }
  11220. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  11221. cb(Vcur, "Vcur", il);
  11222. if (model.layers[il].bv) {
  11223. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  11224. cb(Vcur, "Vcur", il);
  11225. }
  11226. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  11227. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  11228. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  11229. Qcur = ggml_rope_ext(
  11230. ctx0, Qcur, inp_pos, rope_factors,
  11231. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  11232. ext_factor, attn_factor, beta_fast, beta_slow
  11233. );
  11234. Kcur = ggml_rope_ext(
  11235. ctx0, Kcur, inp_pos, rope_factors,
  11236. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  11237. ext_factor, attn_factor, beta_fast, beta_slow
  11238. );
  11239. cb(Qcur, "Qcur", il);
  11240. cb(Kcur, "Kcur", il);
  11241. cb(Vcur, "Vcur", il);
  11242. cur = build_attn(inp_attn,
  11243. model.layers[il].wo, model.layers[il].bo,
  11244. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  11245. }
  11246. if (il == n_layer - 1 && inp_out_ids) {
  11247. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  11248. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  11249. }
  11250. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  11251. cb(ffn_inp, "ffn_inp", il);
  11252. // feed-forward network
  11253. cur = build_norm(ffn_inp,
  11254. model.layers[il].ffn_norm, NULL,
  11255. LLM_NORM_RMS, il);
  11256. cb(cur, "ffn_norm", il);
  11257. cur = build_ffn(cur,
  11258. model.layers[il].ffn_up, NULL, NULL,
  11259. model.layers[il].ffn_gate, NULL, NULL,
  11260. model.layers[il].ffn_down, NULL, NULL,
  11261. NULL,
  11262. LLM_FFN_SILU, LLM_FFN_PAR, il);
  11263. cb(cur, "ffn_out", il);
  11264. cur = ggml_add(ctx0, cur, ffn_inp);
  11265. cb(cur, "ffn_out", il);
  11266. cur = build_cvec(cur, il);
  11267. cb(cur, "l_out", il);
  11268. // input for next layer
  11269. inpL = cur;
  11270. }
  11271. cur = inpL;
  11272. cur = build_norm(cur,
  11273. model.output_norm, NULL,
  11274. LLM_NORM_RMS, -1);
  11275. cb(cur, "result_norm", -1);
  11276. res->t_embd = cur;
  11277. // lm_head
  11278. cur = build_lora_mm(model.output, cur);
  11279. cb(cur, "result_output", -1);
  11280. res->t_logits = cur;
  11281. ggml_build_forward_expand(gf, cur);
  11282. }
  11283. };
  11284. template <bool iswa>
  11285. struct llm_build_exaone4 : public llm_graph_context {
  11286. llm_build_exaone4(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  11287. const int64_t n_embd_head = hparams.n_embd_head_k;
  11288. GGML_ASSERT(n_embd_head == hparams.n_embd_head_v);
  11289. GGML_ASSERT(n_embd_head == hparams.n_rot);
  11290. ggml_tensor * cur;
  11291. ggml_tensor * inpL;
  11292. inpL = build_inp_embd(model.tok_embd);
  11293. // inp_pos - contains the positions
  11294. ggml_tensor * inp_pos = build_inp_pos();
  11295. using inp_attn_type = std::conditional_t<iswa, llm_graph_input_attn_kv_unified_iswa, llm_graph_input_attn_kv_unified>;
  11296. inp_attn_type * inp_attn = nullptr;
  11297. if constexpr (iswa) {
  11298. inp_attn = build_attn_inp_kv_unified_iswa();
  11299. } else {
  11300. inp_attn = build_attn_inp_kv_unified();
  11301. }
  11302. ggml_tensor * inp_out_ids = build_inp_out_ids();
  11303. for (int il = 0; il < n_layer; ++il) {
  11304. ggml_tensor * inpSA = inpL;
  11305. // use RoPE for SWA layers or non-SWA models
  11306. const bool use_rope = hparams.is_swa(il) || hparams.swa_type == LLAMA_SWA_TYPE_NONE;
  11307. cur = inpL;
  11308. // self-attention
  11309. {
  11310. ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
  11311. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  11312. cb(Qcur, "Qcur", il);
  11313. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  11314. cb(Kcur, "Kcur", il);
  11315. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  11316. cb(Vcur, "Vcur", il);
  11317. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  11318. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  11319. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  11320. Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);
  11321. Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);
  11322. cb(Qcur, "Qcur_normed", il);
  11323. cb(Kcur, "Kcur_normed", il);
  11324. if (use_rope) {
  11325. Qcur = ggml_rope_ext(
  11326. ctx0, Qcur, inp_pos, rope_factors,
  11327. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  11328. ext_factor, attn_factor, beta_fast, beta_slow
  11329. );
  11330. Kcur = ggml_rope_ext(
  11331. ctx0, Kcur, inp_pos, rope_factors,
  11332. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  11333. ext_factor, attn_factor, beta_fast, beta_slow
  11334. );
  11335. }
  11336. cb(Qcur, "Qcur", il);
  11337. cb(Kcur, "Kcur", il);
  11338. cb(Vcur, "Vcur", il);
  11339. cur = build_attn(inp_attn,
  11340. model.layers[il].wo, NULL,
  11341. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  11342. cb(cur, "attn_out", il);
  11343. }
  11344. if (il == n_layer - 1 && inp_out_ids) {
  11345. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  11346. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  11347. }
  11348. cur = build_norm(cur,
  11349. model.layers[il].attn_post_norm, NULL,
  11350. LLM_NORM_RMS, il);
  11351. cb(cur, "attn_post_norm", il);
  11352. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  11353. cb(ffn_inp, "ffn_inp", il);
  11354. // feed-forward network
  11355. cur = build_ffn(ffn_inp,
  11356. model.layers[il].ffn_up, NULL, NULL,
  11357. model.layers[il].ffn_gate, NULL, NULL,
  11358. model.layers[il].ffn_down, NULL, NULL,
  11359. NULL,
  11360. LLM_FFN_SILU, LLM_FFN_PAR, il);
  11361. cb(cur, "ffn_out", il);
  11362. cur = build_norm(cur,
  11363. model.layers[il].ffn_post_norm, NULL,
  11364. LLM_NORM_RMS, -1);
  11365. cb(cur, "ffn_post_norm", -1);
  11366. cur = ggml_add(ctx0, cur, ffn_inp);
  11367. cur = build_cvec(cur, il);
  11368. cb(cur, "l_out", il);
  11369. // input for next layer
  11370. inpL = cur;
  11371. }
  11372. cur = inpL;
  11373. cur = build_norm(cur,
  11374. model.output_norm, NULL,
  11375. LLM_NORM_RMS, -1);
  11376. cb(cur, "result_norm", -1);
  11377. res->t_embd = cur;
  11378. // lm_head
  11379. cur = build_lora_mm(model.output, cur);
  11380. cb(cur, "result_output", -1);
  11381. res->t_logits = cur;
  11382. ggml_build_forward_expand(gf, cur);
  11383. }
  11384. };
  11385. struct llm_build_rwkv6_base : public llm_graph_context {
  11386. const llama_model & model;
  11387. llm_build_rwkv6_base(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params), model(model) {
  11388. }
  11389. ggml_tensor * build_rwkv6_channel_mix(
  11390. const llama_layer * layer,
  11391. ggml_tensor * cur,
  11392. ggml_tensor * x_prev,
  11393. llm_arch arch) const {
  11394. ggml_tensor * sx = ggml_sub(ctx0, x_prev, cur);
  11395. switch (arch) {
  11396. case LLM_ARCH_RWKV6:
  11397. {
  11398. ggml_tensor * xk = ggml_add(ctx0, ggml_mul(ctx0, sx, layer->channel_mix_lerp_k), cur);
  11399. ggml_tensor * xr = ggml_add(ctx0, ggml_mul(ctx0, sx, layer->channel_mix_lerp_r), cur);
  11400. ggml_tensor * r = ggml_sigmoid(ctx0, build_lora_mm(layer->channel_mix_receptance, xr));
  11401. ggml_tensor * k = ggml_sqr(
  11402. ctx0,
  11403. ggml_relu(
  11404. ctx0,
  11405. build_lora_mm(layer->channel_mix_key, xk)
  11406. )
  11407. );
  11408. cur = ggml_mul(ctx0, r, build_lora_mm(layer->channel_mix_value, k));
  11409. } break;
  11410. default:
  11411. GGML_ABORT("fatal error");
  11412. }
  11413. return cur;
  11414. }
  11415. ggml_tensor * build_rwkv6_time_mix(
  11416. llm_graph_input_rs * inp,
  11417. ggml_tensor * cur,
  11418. ggml_tensor * x_prev,
  11419. const llama_ubatch & ubatch,
  11420. int il) const {
  11421. const auto * mctx_cur = static_cast<const llama_memory_recurrent_context *>(mctx);
  11422. const auto n_tokens = ubatch.n_tokens;
  11423. const auto n_seqs = ubatch.n_seqs;
  11424. const auto n_seq_tokens = ubatch.n_seq_tokens;
  11425. const auto n_embd = hparams.n_embd;
  11426. const auto head_size = hparams.wkv_head_size;
  11427. const auto n_head = n_embd / head_size;
  11428. const auto n_head_kv = hparams.n_head_kv(il);
  11429. const auto kv_head = mctx_cur->get_head();
  11430. const auto & layer = model.layers[il];
  11431. bool is_qrwkv = layer.time_mix_first == nullptr;
  11432. ggml_tensor * sx = ggml_sub(ctx0, x_prev, cur);
  11433. sx = ggml_reshape_2d(ctx0, sx, n_embd, n_tokens);
  11434. cur = ggml_reshape_2d(ctx0, cur, n_embd, n_tokens);
  11435. ggml_tensor * xxx = ggml_add(ctx0, ggml_mul(ctx0, sx, layer.time_mix_lerp_x), cur);
  11436. xxx = ggml_reshape_4d(
  11437. ctx0,
  11438. ggml_tanh(
  11439. ctx0,
  11440. ggml_mul_mat(ctx0, layer.time_mix_w1, xxx)
  11441. ),
  11442. layer.time_mix_w1->ne[1] / 5, 1, 5, n_tokens
  11443. );
  11444. xxx = ggml_cont(ctx0, ggml_permute(ctx0, xxx, 0, 1, 3, 2));
  11445. xxx = ggml_mul_mat(
  11446. ctx0,
  11447. ggml_reshape_4d(
  11448. ctx0,
  11449. layer.time_mix_w2,
  11450. layer.time_mix_w2->ne[0], layer.time_mix_w2->ne[1], 1, 5
  11451. ),
  11452. xxx
  11453. );
  11454. ggml_tensor *xw, *xk, *xv, *xr, *xg;
  11455. if (layer.time_mix_lerp_fused) {
  11456. // fusing these weights makes some performance improvement
  11457. sx = ggml_reshape_3d(ctx0, sx, n_embd, 1, n_tokens);
  11458. cur = ggml_reshape_3d(ctx0, cur, n_embd, 1, n_tokens);
  11459. xxx = ggml_add(ctx0, ggml_mul(ctx0, ggml_add(ctx0, xxx, layer.time_mix_lerp_fused), sx), cur);
  11460. xw = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], 0);
  11461. xk = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], n_embd * n_tokens * sizeof(float));
  11462. xv = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], n_embd * n_tokens * 2 * sizeof(float));
  11463. xr = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], n_embd * n_tokens * 3 * sizeof(float));
  11464. xg = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], n_embd * n_tokens * 4 * sizeof(float));
  11465. } else {
  11466. // for backward compatibility
  11467. xw = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], 0);
  11468. xk = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], n_embd * n_tokens * sizeof(float));
  11469. xv = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], n_embd * n_tokens * 2 * sizeof(float));
  11470. xr = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], n_embd * n_tokens * 3 * sizeof(float));
  11471. xg = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], n_embd * n_tokens * 4 * sizeof(float));
  11472. xw = ggml_add(ctx0, ggml_mul(ctx0, ggml_add(ctx0, xw, layer.time_mix_lerp_w), sx), cur);
  11473. xk = ggml_add(ctx0, ggml_mul(ctx0, ggml_add(ctx0, xk, layer.time_mix_lerp_k), sx), cur);
  11474. xv = ggml_add(ctx0, ggml_mul(ctx0, ggml_add(ctx0, xv, layer.time_mix_lerp_v), sx), cur);
  11475. xr = ggml_add(ctx0, ggml_mul(ctx0, ggml_add(ctx0, xr, layer.time_mix_lerp_r), sx), cur);
  11476. xg = ggml_add(ctx0, ggml_mul(ctx0, ggml_add(ctx0, xg, layer.time_mix_lerp_g), sx), cur);
  11477. }
  11478. ggml_tensor * r = build_lora_mm(layer.time_mix_receptance, xr);
  11479. ggml_tensor * k = build_lora_mm(layer.time_mix_key, xk);
  11480. ggml_tensor * v = build_lora_mm(layer.time_mix_value, xv);
  11481. if (layer.time_mix_receptance_b) {
  11482. r = ggml_add(ctx0, r, layer.time_mix_receptance_b);
  11483. }
  11484. if (layer.time_mix_key_b) {
  11485. k = ggml_add(ctx0, k, layer.time_mix_key_b);
  11486. }
  11487. if (layer.time_mix_value_b) {
  11488. v = ggml_add(ctx0, v, layer.time_mix_value_b);
  11489. }
  11490. ggml_tensor * g = build_lora_mm(layer.time_mix_gate, xg);
  11491. if (is_qrwkv) {
  11492. g = ggml_sigmoid(ctx0, g);
  11493. } else {
  11494. g = ggml_silu(ctx0, g);
  11495. }
  11496. if (n_head_kv != 0 && n_head_kv != n_head) {
  11497. GGML_ASSERT(n_head % n_head_kv == 0);
  11498. k = ggml_reshape_4d(ctx0, k, head_size, 1, n_head_kv, n_tokens);
  11499. v = ggml_reshape_4d(ctx0, v, head_size, 1, n_head_kv, n_tokens);
  11500. ggml_tensor * tmp = ggml_new_tensor_4d(ctx0, GGML_TYPE_F32, head_size, n_head / n_head_kv, n_head_kv, n_tokens);
  11501. k = ggml_repeat(ctx0, k, tmp);
  11502. v = ggml_repeat(ctx0, v, tmp);
  11503. }
  11504. k = ggml_reshape_3d(ctx0, k, head_size, n_head, n_tokens);
  11505. v = ggml_reshape_3d(ctx0, v, head_size, n_head, n_tokens);
  11506. r = ggml_reshape_3d(ctx0, r, head_size, n_head, n_tokens);
  11507. ggml_tensor * w = ggml_mul_mat(
  11508. ctx0,
  11509. layer.time_mix_decay_w2,
  11510. ggml_tanh(
  11511. ctx0,
  11512. ggml_mul_mat(ctx0, layer.time_mix_decay_w1, xw)
  11513. )
  11514. );
  11515. w = ggml_add(ctx0, w, layer.time_mix_decay);
  11516. w = ggml_exp(ctx0, ggml_neg(ctx0, ggml_exp(ctx0, w)));
  11517. w = ggml_reshape_3d(ctx0, w, head_size, n_head, n_tokens);
  11518. if (is_qrwkv) {
  11519. // k = k * (1 - w)
  11520. k = ggml_sub(ctx0, k, ggml_mul(ctx0, k, w));
  11521. }
  11522. ggml_tensor * wkv_state = build_rs(
  11523. inp, mctx_cur->get_s_l(il),
  11524. hparams.n_embd_s(), n_seqs);
  11525. ggml_tensor * wkv_output;
  11526. if (is_qrwkv) {
  11527. wkv_output = ggml_gated_linear_attn(ctx0, k, v, r, w, wkv_state, pow(head_size, -0.5f));
  11528. } else {
  11529. wkv_output = ggml_rwkv_wkv6(ctx0, k, v, r, layer.time_mix_first, w, wkv_state);
  11530. }
  11531. cur = ggml_view_1d(ctx0, wkv_output, n_embd * n_tokens, 0);
  11532. wkv_state = ggml_view_1d(ctx0, wkv_output, n_embd * head_size * n_seqs, n_embd * n_tokens * sizeof(float));
  11533. ggml_build_forward_expand(
  11534. gf,
  11535. ggml_cpy(
  11536. ctx0,
  11537. wkv_state,
  11538. ggml_view_1d(
  11539. ctx0,
  11540. mctx_cur->get_s_l(il),
  11541. hparams.n_embd_s() * n_seqs,
  11542. hparams.n_embd_s() * kv_head * ggml_element_size(mctx_cur->get_s_l(il))
  11543. )
  11544. )
  11545. );
  11546. if (!is_qrwkv) {
  11547. // group norm with head_count groups
  11548. cur = ggml_reshape_3d(ctx0, cur, n_embd / n_head, n_head, n_tokens);
  11549. cur = ggml_norm(ctx0, cur, 64e-5f);
  11550. // Convert back to regular vectors.
  11551. cur = ggml_reshape_2d(ctx0, cur, n_embd, n_tokens);
  11552. cur = ggml_add(ctx0, ggml_mul(ctx0, cur, layer.time_mix_ln), layer.time_mix_ln_b);
  11553. } else {
  11554. cur = ggml_reshape_2d(ctx0, cur, n_embd, n_tokens);
  11555. }
  11556. cur = ggml_mul(ctx0, cur, g);
  11557. cur = build_lora_mm(layer.time_mix_output, cur);
  11558. return ggml_reshape_3d(ctx0, cur, n_embd, n_seq_tokens, n_seqs);
  11559. }
  11560. };
  11561. struct llm_build_rwkv6 : public llm_build_rwkv6_base {
  11562. llm_build_rwkv6(const llama_model & model, const llm_graph_params & params) : llm_build_rwkv6_base(model, params) {
  11563. GGML_ASSERT(hparams.token_shift_count == 2);
  11564. ggml_tensor * cur;
  11565. ggml_tensor * inpL;
  11566. inpL = build_inp_embd(model.tok_embd);
  11567. inpL = build_norm(inpL, model.tok_norm, model.tok_norm_b, LLM_NORM, -1);
  11568. auto * rs_inp = build_rs_inp();
  11569. const auto n_embd = hparams.n_embd;
  11570. const auto n_seq_tokens = ubatch.n_seq_tokens;
  11571. const auto n_seqs = ubatch.n_seqs;
  11572. ggml_tensor * inp_out_ids = build_inp_out_ids();
  11573. for (int il = 0; il < n_layer; ++il) {
  11574. const llama_layer * layer = &model.layers[il];
  11575. inpL = ggml_reshape_3d(ctx0, inpL, n_embd, n_seq_tokens, n_seqs);
  11576. ggml_tensor * token_shift = build_rwkv_token_shift_load(rs_inp, ubatch, il);
  11577. ggml_tensor * att_shift = ggml_view_3d(ctx0, token_shift, n_embd, 1, n_seqs, token_shift->nb[1], token_shift->nb[2], 0);
  11578. 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));
  11579. ggml_tensor * att_norm = build_norm(inpL, layer->attn_norm, layer->attn_norm_b, LLM_NORM, il);
  11580. cb(att_norm, "attn_norm", il);
  11581. ggml_tensor * x_prev = ggml_concat(
  11582. ctx0,
  11583. att_shift,
  11584. ggml_view_3d(ctx0, att_norm, n_embd, n_seq_tokens - 1, n_seqs, att_norm->nb[1], att_norm->nb[2], 0),
  11585. 1
  11586. );
  11587. cur = build_rwkv6_time_mix(rs_inp, att_norm, x_prev, ubatch, il);
  11588. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
  11589. cb(ffn_inp, "ffn_inp", il);
  11590. ggml_tensor * ffn_norm = build_norm(ffn_inp, layer->attn_norm_2, layer->attn_norm_2_b, LLM_NORM, il);
  11591. cb(ffn_norm, "ffn_norm", il);
  11592. x_prev = ggml_concat(
  11593. ctx0,
  11594. ffn_shift,
  11595. ggml_view_3d(ctx0, ffn_norm, n_embd, n_seq_tokens - 1, n_seqs, ffn_norm->nb[1], ffn_norm->nb[2], 0),
  11596. 1
  11597. );
  11598. token_shift = ggml_concat(ctx0,
  11599. 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)),
  11600. 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)),
  11601. 1
  11602. );
  11603. ggml_build_forward_expand(gf, build_rwkv_token_shift_store(token_shift, ubatch, il));
  11604. ffn_inp = ggml_reshape_2d(ctx0, ffn_inp, n_embd, n_tokens);
  11605. ffn_norm = ggml_reshape_2d(ctx0, ffn_norm, n_embd, n_tokens);
  11606. x_prev = ggml_reshape_2d(ctx0, x_prev, n_embd, n_tokens);
  11607. cur = ggml_reshape_2d(ctx0, cur, n_embd, n_tokens);
  11608. if (il == n_layer - 1 && inp_out_ids) {
  11609. ffn_inp = ggml_get_rows(ctx0, ffn_inp, inp_out_ids);
  11610. ffn_norm = ggml_get_rows(ctx0, ffn_norm, inp_out_ids);
  11611. x_prev = ggml_get_rows(ctx0, x_prev, inp_out_ids);
  11612. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  11613. }
  11614. cur = build_rwkv6_channel_mix(layer, ffn_norm, x_prev, LLM_ARCH_RWKV6);
  11615. cur = ggml_add(ctx0, cur, ffn_inp);
  11616. if (hparams.rescale_every_n_layers != 0 && (il + 1) % hparams.rescale_every_n_layers == 0) {
  11617. cur = ggml_scale(ctx0, cur, 0.5F);
  11618. }
  11619. cur = build_cvec(cur, il);
  11620. cb(cur, "l_out", il);
  11621. // input for next layer
  11622. inpL = cur;
  11623. }
  11624. cur = inpL;
  11625. cur = build_norm(cur, model.output_norm, model.output_norm_b, LLM_NORM, -1);
  11626. cb(cur, "result_norm", -1);
  11627. res->t_embd = cur;
  11628. cur = build_lora_mm(model.output, cur);
  11629. cb(cur, "result_output", -1);
  11630. res->t_logits = cur;
  11631. ggml_build_forward_expand(gf, cur);
  11632. }
  11633. };
  11634. // ref: https://huggingface.co/recursal/QRWKV6-32B-Instruct-Preview-v0.1/blob/main/modeling_rwkv6qwen2.py
  11635. struct llm_build_rwkv6qwen2 : public llm_build_rwkv6_base {
  11636. llm_build_rwkv6qwen2(const llama_model & model, const llm_graph_params & params) : llm_build_rwkv6_base(model, params) {
  11637. GGML_ASSERT(n_embd == hparams.n_embd_r());
  11638. ggml_tensor * cur;
  11639. ggml_tensor * inpL;
  11640. inpL = build_inp_embd(model.tok_embd);
  11641. auto * rs_inp = build_rs_inp();
  11642. const auto n_embd = hparams.n_embd;
  11643. const auto n_seq_tokens = ubatch.n_seq_tokens;
  11644. const auto n_seqs = ubatch.n_seqs;
  11645. ggml_tensor * inp_out_ids = build_inp_out_ids();
  11646. for (int il = 0; il < n_layer; ++il) {
  11647. const llama_layer * layer = &model.layers[il];
  11648. inpL = ggml_reshape_3d(ctx0, inpL, n_embd, n_seq_tokens, n_seqs);
  11649. ggml_tensor * token_shift = build_rwkv_token_shift_load(rs_inp, ubatch, il);
  11650. ggml_tensor * att_norm = build_norm(inpL, layer->attn_norm, layer->attn_norm_b, LLM_NORM_RMS, il);
  11651. cb(att_norm, "attn_norm", il);
  11652. ggml_tensor * x_prev = ggml_concat(
  11653. ctx0,
  11654. token_shift,
  11655. ggml_view_3d(ctx0, att_norm, n_embd, n_seq_tokens - 1, n_seqs, att_norm->nb[1], att_norm->nb[2], 0),
  11656. 1
  11657. );
  11658. cur = build_rwkv6_time_mix(rs_inp, att_norm, x_prev, ubatch, il);
  11659. 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));
  11660. ggml_build_forward_expand(gf, build_rwkv_token_shift_store(token_shift, ubatch, il));
  11661. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
  11662. cb(ffn_inp, "ffn_inp", il);
  11663. cur = ggml_reshape_2d(ctx0, cur, n_embd, n_tokens);
  11664. ffn_inp = ggml_reshape_2d(ctx0, ffn_inp, n_embd, n_tokens);
  11665. if (il == n_layer - 1 && inp_out_ids) {
  11666. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  11667. ffn_inp = ggml_get_rows(ctx0, ffn_inp, inp_out_ids);
  11668. }
  11669. // feed-forward network
  11670. cur = build_norm(ffn_inp,
  11671. model.layers[il].ffn_norm, NULL,
  11672. LLM_NORM_RMS, il);
  11673. cb(cur, "ffn_norm", il);
  11674. cur = build_ffn(cur,
  11675. model.layers[il].ffn_up, NULL, NULL,
  11676. model.layers[il].ffn_gate, NULL, NULL,
  11677. model.layers[il].ffn_down, NULL, NULL,
  11678. NULL,
  11679. LLM_FFN_SILU, LLM_FFN_PAR, il);
  11680. cb(cur, "ffn_out", il);
  11681. cur = ggml_add(ctx0, cur, ffn_inp);
  11682. cur = build_cvec(cur, il);
  11683. cb(cur, "l_out", il);
  11684. // input for next layer
  11685. inpL = cur;
  11686. }
  11687. cur = inpL;
  11688. cur = build_norm(cur, model.output_norm, model.output_norm_b, LLM_NORM_RMS, -1);
  11689. cb(cur, "result_norm", -1);
  11690. res->t_embd = cur;
  11691. cur = build_lora_mm(model.output, cur);
  11692. cb(cur, "result_output", -1);
  11693. res->t_logits = cur;
  11694. ggml_build_forward_expand(gf, cur);
  11695. }
  11696. };
  11697. struct llm_build_rwkv7_base : public llm_graph_context {
  11698. const llama_model & model;
  11699. llm_build_rwkv7_base(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params), model(model) {
  11700. }
  11701. ggml_tensor * build_rwkv7_channel_mix(
  11702. const llama_layer * layer,
  11703. ggml_tensor * cur,
  11704. ggml_tensor * x_prev,
  11705. llm_arch arch) const {
  11706. ggml_tensor * sx = ggml_sub(ctx0, x_prev, cur);
  11707. switch (arch) {
  11708. case LLM_ARCH_RWKV7:
  11709. {
  11710. ggml_tensor * xk = ggml_add(ctx0, ggml_mul(ctx0, sx, layer->channel_mix_lerp_k), cur);
  11711. ggml_tensor * k = ggml_sqr(
  11712. ctx0,
  11713. ggml_relu(
  11714. ctx0,
  11715. build_lora_mm(layer->channel_mix_key, xk)
  11716. )
  11717. );
  11718. cur = build_lora_mm(layer->channel_mix_value, k);
  11719. } break;
  11720. default:
  11721. GGML_ABORT("fatal error");
  11722. }
  11723. return cur;
  11724. }
  11725. ggml_tensor * build_rwkv7_time_mix(
  11726. llm_graph_input_rs * inp,
  11727. ggml_tensor * cur,
  11728. ggml_tensor * x_prev,
  11729. ggml_tensor *& first_layer_value,
  11730. const llama_ubatch & ubatch,
  11731. int il) const {
  11732. const auto * mctx_cur = static_cast<const llama_memory_recurrent_context *>(mctx);
  11733. const auto n_tokens = ubatch.n_tokens;
  11734. const auto n_seqs = ubatch.n_seqs;
  11735. const auto n_embd = hparams.n_embd;
  11736. const auto head_size = hparams.wkv_head_size;
  11737. const auto head_count = n_embd / head_size;
  11738. const auto n_seq_tokens = ubatch.n_seq_tokens;
  11739. const auto kv_head = mctx_cur->get_head();
  11740. const auto & layer = model.layers[il];
  11741. bool has_gating = layer.time_mix_g1 && layer.time_mix_g2;
  11742. ggml_tensor * sx = ggml_sub(ctx0, x_prev, cur);
  11743. ggml_tensor * dummy = ggml_new_tensor_4d(ctx0, GGML_TYPE_F32, n_embd, n_seq_tokens, n_seqs, has_gating ? 6 : 5);
  11744. sx = ggml_repeat(ctx0, sx, dummy);
  11745. ggml_tensor * xxx = ggml_add(ctx0, ggml_mul(ctx0, sx, layer.time_mix_lerp_fused), cur);
  11746. ggml_tensor * xr = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], 0);
  11747. ggml_tensor * xw = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], n_embd * n_tokens * sizeof(float));
  11748. ggml_tensor * xk = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], n_embd * n_tokens * 2 * sizeof(float));
  11749. ggml_tensor * xv = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], n_embd * n_tokens * 3 * sizeof(float));
  11750. ggml_tensor * xa = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], n_embd * n_tokens * 4 * sizeof(float));
  11751. 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;
  11752. ggml_tensor * r = build_lora_mm(layer.time_mix_receptance, xr);
  11753. ggml_tensor * w = ggml_add(
  11754. ctx0,
  11755. ggml_mul_mat(ctx0, layer.time_mix_w2, ggml_tanh(ctx0, ggml_mul_mat(ctx0, layer.time_mix_w1, xw))),
  11756. layer.time_mix_w0
  11757. );
  11758. w = ggml_exp(ctx0, ggml_scale(ctx0, ggml_sigmoid(ctx0, w), -0.606531));
  11759. ggml_tensor * k = build_lora_mm(layer.time_mix_key, xk);
  11760. ggml_tensor * v = build_lora_mm(layer.time_mix_value, xv);
  11761. if (first_layer_value == nullptr) {
  11762. first_layer_value = v;
  11763. } else {
  11764. // Add the first layer value as a residual connection.
  11765. v = ggml_add(ctx0, v,
  11766. ggml_mul(ctx0,
  11767. ggml_sub(ctx0, first_layer_value, v),
  11768. ggml_sigmoid(ctx0, ggml_add(ctx0,
  11769. ggml_mul_mat(ctx0, layer.time_mix_v2, ggml_mul_mat(ctx0, layer.time_mix_v1, xv)),
  11770. layer.time_mix_v0
  11771. )
  11772. )
  11773. )
  11774. );
  11775. }
  11776. ggml_tensor * g = nullptr;
  11777. if (layer.time_mix_g1 && layer.time_mix_g2) {
  11778. g = ggml_mul_mat(ctx0, layer.time_mix_g2, ggml_sigmoid(ctx0, ggml_mul_mat(ctx0, layer.time_mix_g1, xg)));
  11779. }
  11780. ggml_tensor * a = ggml_sigmoid(ctx0,
  11781. ggml_add(
  11782. ctx0,
  11783. ggml_mul_mat(ctx0, layer.time_mix_a2, ggml_mul_mat(ctx0, layer.time_mix_a1, xa)),
  11784. layer.time_mix_a0
  11785. )
  11786. );
  11787. ggml_tensor * kk = ggml_reshape_3d(ctx0, ggml_mul(ctx0, k, layer.time_mix_k_k), head_size, head_count, n_tokens);
  11788. kk = ggml_l2_norm(ctx0, kk, 1e-12);
  11789. ggml_tensor * ka = ggml_mul(ctx0, k, layer.time_mix_k_a);
  11790. k = ggml_add(ctx0, k, ggml_sub(ctx0, ggml_mul(ctx0, a, ka), ka));
  11791. r = ggml_reshape_3d(ctx0, r, head_size, head_count, n_tokens);
  11792. w = ggml_reshape_3d(ctx0, w, head_size, head_count, n_tokens);
  11793. k = ggml_reshape_3d(ctx0, k, head_size, head_count, n_tokens);
  11794. v = ggml_reshape_3d(ctx0, v, head_size, head_count, n_tokens);
  11795. a = ggml_reshape_3d(ctx0, a, head_size, head_count, n_tokens);
  11796. ggml_tensor * wkv_state = build_rs(
  11797. inp, mctx_cur->get_s_l(il),
  11798. hparams.n_embd_s(), n_seqs);
  11799. ggml_tensor * wkv_output = ggml_rwkv_wkv7(ctx0, r, w, k, v, ggml_neg(ctx0, kk), ggml_mul(ctx0, kk, a), wkv_state);
  11800. cur = ggml_view_1d(ctx0, wkv_output, n_embd * n_tokens, 0);
  11801. wkv_state = ggml_view_1d(ctx0, wkv_output, n_embd * head_size * n_seqs, n_embd * n_tokens * sizeof(float));
  11802. ggml_build_forward_expand(
  11803. gf,
  11804. ggml_cpy(
  11805. ctx0,
  11806. wkv_state,
  11807. ggml_view_1d(
  11808. ctx0,
  11809. mctx_cur->get_s_l(il),
  11810. hparams.n_embd_s() * n_seqs,
  11811. hparams.n_embd_s() * kv_head * ggml_element_size(mctx_cur->get_s_l(il))
  11812. )
  11813. )
  11814. );
  11815. if (layer.time_mix_ln && layer.time_mix_ln_b) {
  11816. // group norm with head_count groups
  11817. cur = ggml_reshape_3d(ctx0, cur, n_embd / head_count, head_count, n_tokens);
  11818. cur = ggml_norm(ctx0, cur, 64e-5f);
  11819. // Convert back to regular vectors.
  11820. cur = ggml_reshape_2d(ctx0, cur, n_embd, n_tokens);
  11821. cur = ggml_add(ctx0, ggml_mul(ctx0, cur, layer.time_mix_ln), layer.time_mix_ln_b);
  11822. } else {
  11823. cur = ggml_reshape_2d(ctx0, cur, n_embd, n_tokens);
  11824. }
  11825. ggml_tensor * rk = ggml_sum_rows(ctx0,
  11826. ggml_mul(ctx0, ggml_mul(ctx0, k, r), ggml_reshape_2d(ctx0, layer.time_mix_r_k, head_size, head_count)));
  11827. cur = ggml_add(ctx0, cur, ggml_reshape_2d(ctx0, ggml_mul(ctx0, v, rk), n_embd, n_tokens));
  11828. if (has_gating) {
  11829. cur = ggml_mul(ctx0, cur, g);
  11830. }
  11831. cur = build_lora_mm(layer.time_mix_output, cur);
  11832. return ggml_reshape_3d(ctx0, cur, n_embd, n_seq_tokens, n_seqs);
  11833. }
  11834. };
  11835. struct llm_build_rwkv7 : public llm_build_rwkv7_base {
  11836. llm_build_rwkv7(const llama_model & model, const llm_graph_params & params) : llm_build_rwkv7_base(model, params) {
  11837. GGML_ASSERT(hparams.token_shift_count == 2);
  11838. ggml_tensor * cur;
  11839. ggml_tensor * inpL;
  11840. ggml_tensor * v_first = nullptr;
  11841. inpL = build_inp_embd(model.tok_embd);
  11842. inpL = build_norm(inpL, model.tok_norm, model.tok_norm_b, LLM_NORM, -1);
  11843. auto * rs_inp = build_rs_inp();
  11844. const auto n_embd = hparams.n_embd;
  11845. const auto n_seq_tokens = ubatch.n_seq_tokens;
  11846. const auto n_seqs = ubatch.n_seqs;
  11847. ggml_tensor * inp_out_ids = build_inp_out_ids();
  11848. for (int il = 0; il < n_layer; ++il) {
  11849. const llama_layer * layer = &model.layers[il];
  11850. inpL = ggml_reshape_3d(ctx0, inpL, n_embd, n_seq_tokens, n_seqs);
  11851. ggml_tensor * token_shift = build_rwkv_token_shift_load(rs_inp, ubatch, il);
  11852. ggml_tensor * att_shift = ggml_view_3d(ctx0, token_shift, n_embd, 1, n_seqs, token_shift->nb[1], token_shift->nb[2], 0);
  11853. 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));
  11854. ggml_tensor * att_norm = build_norm(inpL, layer->attn_norm, layer->attn_norm_b, LLM_NORM, il);
  11855. cb(att_norm, "attn_norm", il);
  11856. ggml_tensor * x_prev = ggml_concat(
  11857. ctx0,
  11858. att_shift,
  11859. ggml_view_3d(ctx0, att_norm, n_embd, n_seq_tokens - 1, n_seqs, att_norm->nb[1], att_norm->nb[2], 0),
  11860. 1
  11861. );
  11862. cur = build_rwkv7_time_mix(rs_inp, att_norm, x_prev, v_first, ubatch, il);
  11863. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
  11864. cb(ffn_inp, "ffn_inp", il);
  11865. ggml_tensor * ffn_norm = build_norm(ffn_inp, layer->attn_norm_2, layer->attn_norm_2_b, LLM_NORM, il);
  11866. cb(ffn_norm, "ffn_norm", il);
  11867. x_prev = ggml_concat(
  11868. ctx0,
  11869. ffn_shift,
  11870. ggml_view_3d(ctx0, ffn_norm, n_embd, n_seq_tokens - 1, n_seqs, ffn_norm->nb[1], ffn_norm->nb[2], 0),
  11871. 1
  11872. );
  11873. token_shift = ggml_concat(ctx0,
  11874. 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)),
  11875. 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)),
  11876. 1
  11877. );
  11878. ggml_build_forward_expand(gf, build_rwkv_token_shift_store(token_shift, ubatch, il));
  11879. ffn_inp = ggml_reshape_2d(ctx0, ffn_inp, n_embd, n_tokens);
  11880. ffn_norm = ggml_reshape_2d(ctx0, ffn_norm, n_embd, n_tokens);
  11881. x_prev = ggml_reshape_2d(ctx0, x_prev, n_embd, n_tokens);
  11882. if (il == n_layer - 1 && inp_out_ids) {
  11883. ffn_inp = ggml_get_rows(ctx0, ffn_inp, inp_out_ids);
  11884. ffn_norm = ggml_get_rows(ctx0, ffn_norm, inp_out_ids);
  11885. x_prev = ggml_get_rows(ctx0, x_prev, inp_out_ids);
  11886. }
  11887. cur = build_rwkv7_channel_mix(layer, ffn_norm, x_prev, LLM_ARCH_RWKV7);
  11888. cur = ggml_add(ctx0, cur, ffn_inp);
  11889. cur = build_cvec(cur, il);
  11890. cb(cur, "l_out", il);
  11891. // input for next layer
  11892. inpL = cur;
  11893. }
  11894. cur = inpL;
  11895. cur = build_norm(cur, model.output_norm, model.output_norm_b, LLM_NORM, -1);
  11896. cb(cur, "result_norm", -1);
  11897. res->t_embd = cur;
  11898. cur = build_lora_mm(model.output, cur);
  11899. cb(cur, "result_output", -1);
  11900. res->t_logits = cur;
  11901. ggml_build_forward_expand(gf, cur);
  11902. }
  11903. };
  11904. struct llm_build_arwkv7 : public llm_build_rwkv7_base {
  11905. llm_build_arwkv7(const llama_model & model, const llm_graph_params & params) : llm_build_rwkv7_base(model, params) {
  11906. GGML_ASSERT(n_embd == hparams.n_embd_r());
  11907. ggml_tensor * cur;
  11908. ggml_tensor * inpL;
  11909. ggml_tensor * v_first = nullptr;
  11910. inpL = build_inp_embd(model.tok_embd);
  11911. auto * rs_inp = build_rs_inp();
  11912. const auto n_embd = hparams.n_embd;
  11913. const auto n_seq_tokens = ubatch.n_seq_tokens;
  11914. const auto n_seqs = ubatch.n_seqs;
  11915. ggml_tensor * inp_out_ids = build_inp_out_ids();
  11916. for (int il = 0; il < n_layer; ++il) {
  11917. const llama_layer * layer = &model.layers[il];
  11918. inpL = ggml_reshape_3d(ctx0, inpL, n_embd, n_seq_tokens, n_seqs);
  11919. ggml_tensor * token_shift = build_rwkv_token_shift_load(rs_inp, ubatch, il);
  11920. ggml_tensor * att_norm = build_norm(inpL, layer->attn_norm, layer->attn_norm_b, LLM_NORM_RMS, il);
  11921. cb(att_norm, "attn_norm", il);
  11922. ggml_tensor * x_prev = ggml_concat(
  11923. ctx0,
  11924. token_shift,
  11925. ggml_view_3d(ctx0, att_norm, n_embd, n_seq_tokens - 1, n_seqs, att_norm->nb[1], att_norm->nb[2], 0),
  11926. 1
  11927. );
  11928. cur = build_rwkv7_time_mix(rs_inp, att_norm, x_prev, v_first, ubatch, il);
  11929. 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));
  11930. ggml_build_forward_expand(gf, build_rwkv_token_shift_store(token_shift, ubatch, il));
  11931. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
  11932. cb(ffn_inp, "ffn_inp", il);
  11933. cur = ggml_reshape_2d(ctx0, cur, n_embd, n_tokens);
  11934. ffn_inp = ggml_reshape_2d(ctx0, ffn_inp, n_embd, n_tokens);
  11935. if (il == n_layer - 1 && inp_out_ids) {
  11936. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  11937. ffn_inp = ggml_get_rows(ctx0, ffn_inp, inp_out_ids);
  11938. }
  11939. // feed-forward network
  11940. cur = build_norm(ffn_inp,
  11941. model.layers[il].ffn_norm, NULL,
  11942. LLM_NORM_RMS, il);
  11943. cb(cur, "ffn_norm", il);
  11944. cur = build_ffn(cur,
  11945. model.layers[il].ffn_up, NULL, NULL,
  11946. model.layers[il].ffn_gate, NULL, NULL,
  11947. model.layers[il].ffn_down, NULL, NULL,
  11948. NULL,
  11949. LLM_FFN_SILU, LLM_FFN_PAR, il);
  11950. cb(cur, "ffn_out", il);
  11951. cur = ggml_add(ctx0, cur, ffn_inp);
  11952. cur = build_cvec(cur, il);
  11953. cb(cur, "l_out", il);
  11954. // input for next layer
  11955. inpL = cur;
  11956. }
  11957. cur = inpL;
  11958. cur = build_norm(cur, model.output_norm, model.output_norm_b, LLM_NORM_RMS, -1);
  11959. cb(cur, "result_norm", -1);
  11960. res->t_embd = cur;
  11961. cur = build_lora_mm(model.output, cur);
  11962. cb(cur, "result_output", -1);
  11963. res->t_logits = cur;
  11964. ggml_build_forward_expand(gf, cur);
  11965. }
  11966. };
  11967. struct llm_build_granite : public llm_graph_context {
  11968. llm_build_granite(
  11969. const llama_model & model,
  11970. const llm_graph_params & params)
  11971. : llm_graph_context(params) {
  11972. const int64_t n_embd_head = hparams.n_embd_head_v;
  11973. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  11974. GGML_ASSERT(n_embd_head == hparams.n_rot);
  11975. ggml_tensor * cur;
  11976. ggml_tensor * inpL;
  11977. inpL = build_inp_embd(model.tok_embd);
  11978. // inp_pos - built only if rope enabled
  11979. ggml_tensor * inp_pos = nullptr;
  11980. if (hparams.rope_finetuned) {
  11981. inp_pos = build_inp_pos();
  11982. }
  11983. auto * inp_attn = build_attn_inp_kv_unified();
  11984. ggml_tensor * inp_out_ids = build_inp_out_ids();
  11985. for (int il = 0; il < n_layer; ++il) {
  11986. ggml_tensor * inpSA = inpL;
  11987. // norm
  11988. cur = build_norm(inpL,
  11989. model.layers[il].attn_norm, NULL,
  11990. LLM_NORM_RMS, il);
  11991. cb(cur, "attn_norm", il);
  11992. // self-attention
  11993. cur = build_attention_layer(
  11994. cur, inp_pos, inp_attn,
  11995. model, n_embd_head, il);
  11996. if (il == n_layer - 1 && inp_out_ids) {
  11997. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  11998. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  11999. }
  12000. // ffn
  12001. cur = build_layer_ffn(cur, inpSA, model, il);
  12002. // input for next layer
  12003. inpL = cur;
  12004. }
  12005. cur = inpL;
  12006. cur = build_norm(cur,
  12007. model.output_norm, NULL,
  12008. LLM_NORM_RMS, -1);
  12009. cb(cur, "result_norm", -1);
  12010. res->t_embd = cur;
  12011. // lm_head
  12012. cur = build_lora_mm(model.output, cur);
  12013. // For Granite architectures - scale logits
  12014. cur = ggml_scale(ctx0, cur, 1.0f / hparams.f_logit_scale);
  12015. cb(cur, "result_output", -1);
  12016. res->t_logits = cur;
  12017. ggml_build_forward_expand(gf, cur);
  12018. }
  12019. ggml_tensor * build_attention_layer(
  12020. ggml_tensor * cur,
  12021. ggml_tensor * inp_pos,
  12022. llm_graph_input_attn_kv_unified * inp_attn,
  12023. const llama_model & model,
  12024. const int64_t n_embd_head,
  12025. const int il) {
  12026. // compute Q and K and (optionally) RoPE them
  12027. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  12028. cb(Qcur, "Qcur", il);
  12029. if (model.layers[il].bq) {
  12030. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  12031. cb(Qcur, "Qcur", il);
  12032. }
  12033. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  12034. cb(Kcur, "Kcur", il);
  12035. if (model.layers[il].bk) {
  12036. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  12037. cb(Kcur, "Kcur", il);
  12038. }
  12039. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  12040. cb(Vcur, "Vcur", il);
  12041. if (model.layers[il].bv) {
  12042. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  12043. cb(Vcur, "Vcur", il);
  12044. }
  12045. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, hparams.n_head(il), n_tokens);
  12046. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, hparams.n_head_kv(il), n_tokens);
  12047. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, hparams.n_head_kv(il), n_tokens);
  12048. const bool use_rope = hparams.rope_finetuned;
  12049. if (use_rope) {
  12050. ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
  12051. Qcur = ggml_rope_ext(
  12052. ctx0, Qcur, inp_pos, rope_factors,
  12053. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  12054. ext_factor, attn_factor, beta_fast, beta_slow
  12055. );
  12056. Kcur = ggml_rope_ext(
  12057. ctx0, Kcur, inp_pos, rope_factors,
  12058. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  12059. ext_factor, attn_factor, beta_fast, beta_slow
  12060. );
  12061. }
  12062. cb(Qcur, "Qcur", il);
  12063. cb(Kcur, "Kcur", il);
  12064. cb(Vcur, "Vcur", il);
  12065. const float kq_scale = hparams.f_attention_scale == 0.0f ? 1.0f/sqrtf(float(n_embd_head)) : hparams.f_attention_scale;
  12066. cur = build_attn(inp_attn,
  12067. model.layers[il].wo, model.layers[il].bo,
  12068. Qcur, Kcur, Vcur, nullptr, nullptr, kq_scale, il);
  12069. cb(cur, "attn_out", il);
  12070. return cur;
  12071. }
  12072. ggml_tensor * build_layer_ffn(
  12073. ggml_tensor * cur,
  12074. ggml_tensor * inpSA,
  12075. const llama_model & model,
  12076. const int il) {
  12077. // For Granite architectures - scale residual
  12078. if (hparams.f_residual_scale) {
  12079. cur = ggml_scale(ctx0, cur, hparams.f_residual_scale);
  12080. }
  12081. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  12082. cb(ffn_inp, "ffn_inp", il);
  12083. // feed-forward network (non-MoE)
  12084. if (model.layers[il].ffn_gate_inp == nullptr) {
  12085. cur = build_norm(ffn_inp,
  12086. model.layers[il].ffn_norm, NULL,
  12087. LLM_NORM_RMS, il);
  12088. cb(cur, "ffn_norm", il);
  12089. cur = build_ffn(cur,
  12090. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  12091. model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, NULL,
  12092. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  12093. NULL,
  12094. LLM_FFN_SILU, LLM_FFN_PAR, il);
  12095. cb(cur, "ffn_out", il);
  12096. } else {
  12097. // MoE branch
  12098. cur = build_norm(ffn_inp,
  12099. model.layers[il].ffn_norm, NULL,
  12100. LLM_NORM_RMS, il);
  12101. cb(cur, "ffn_norm", il);
  12102. ggml_tensor * moe_out = build_moe_ffn(cur,
  12103. model.layers[il].ffn_gate_inp,
  12104. model.layers[il].ffn_up_exps,
  12105. model.layers[il].ffn_gate_exps,
  12106. model.layers[il].ffn_down_exps,
  12107. nullptr,
  12108. n_expert, n_expert_used,
  12109. LLM_FFN_SILU, true,
  12110. false, 0.0,
  12111. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  12112. il);
  12113. cb(moe_out, "ffn_moe_out", il);
  12114. // For Granite MoE Shared
  12115. if (hparams.n_ff_shexp > 0) {
  12116. ggml_tensor * ffn_shexp = build_ffn(cur,
  12117. model.layers[il].ffn_up_shexp, NULL, NULL,
  12118. model.layers[il].ffn_gate_shexp, NULL, NULL,
  12119. model.layers[il].ffn_down_shexp, NULL, NULL,
  12120. NULL,
  12121. LLM_FFN_SILU, LLM_FFN_PAR, il);
  12122. cb(ffn_shexp, "ffn_shexp", il);
  12123. cur = ggml_add(ctx0, moe_out, ffn_shexp);
  12124. cb(cur, "ffn_out", il);
  12125. } else {
  12126. cur = moe_out;
  12127. }
  12128. }
  12129. // For Granite architectures - scale residual
  12130. if (hparams.f_residual_scale) {
  12131. cur = ggml_scale(ctx0, cur, hparams.f_residual_scale);
  12132. }
  12133. cur = ggml_add(ctx0, cur, ffn_inp);
  12134. cb(cur, "ffn_out", il);
  12135. cur = build_cvec(cur, il);
  12136. cb(cur, "l_out", il);
  12137. return cur;
  12138. }
  12139. };
  12140. struct llm_build_granite_hybrid : public llm_graph_context_mamba {
  12141. llm_build_granite_hybrid(
  12142. const llama_model & model,
  12143. const llm_graph_params & params) :
  12144. llm_graph_context_mamba(params) {
  12145. const int64_t n_embd_head = hparams.n_embd_head_v;
  12146. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  12147. ggml_tensor * cur;
  12148. ggml_tensor * inpL;
  12149. inpL = build_inp_embd(model.tok_embd);
  12150. auto * inp = build_inp_mem_hybrid();
  12151. ggml_tensor * inp_out_ids = build_inp_out_ids();
  12152. // Positional embeddings populated if rope enabled
  12153. ggml_tensor * inp_pos = nullptr;
  12154. if (hparams.rope_finetuned) {
  12155. inp_pos = build_inp_pos();
  12156. }
  12157. for (int il = 0; il < n_layer; ++il) {
  12158. struct ggml_tensor * inpSA = inpL;
  12159. // norm
  12160. cur = build_norm(inpL,
  12161. model.layers[il].attn_norm, NULL,
  12162. LLM_NORM_RMS, il);
  12163. cb(cur, "attn_norm", il);
  12164. if (hparams.is_recurrent(il)) {
  12165. // ssm layer //
  12166. cur = build_mamba2_layer(inp->get_recr(), cur, model, ubatch, il);
  12167. } else {
  12168. // attention layer //
  12169. cur = build_attention_layer(
  12170. cur, inp_pos, inp->get_attn(), model,
  12171. n_embd_head, il);
  12172. }
  12173. if (il == n_layer - 1 && inp_out_ids) {
  12174. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  12175. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  12176. }
  12177. // ffn
  12178. cur = build_layer_ffn(cur, inpSA, model, il);
  12179. // input for next layer
  12180. inpL = cur;
  12181. }
  12182. cur = inpL;
  12183. cur = build_norm(cur,
  12184. model.output_norm, NULL,
  12185. LLM_NORM_RMS, -1);
  12186. cb(cur, "result_norm", -1);
  12187. res->t_embd = cur;
  12188. // lm_head
  12189. cur = build_lora_mm(model.output, cur);
  12190. // For Granite architectures - scale logits
  12191. if (hparams.f_logit_scale) {
  12192. cur = ggml_scale(ctx0, cur, 1.0f / hparams.f_logit_scale);
  12193. }
  12194. cb(cur, "result_output", -1);
  12195. res->t_logits = cur;
  12196. ggml_build_forward_expand(gf, cur);
  12197. }
  12198. ggml_tensor * build_attention_layer(
  12199. ggml_tensor * cur,
  12200. ggml_tensor * inp_pos,
  12201. llm_graph_input_attn_kv_unified * inp_attn,
  12202. const llama_model & model,
  12203. const int64_t n_embd_head,
  12204. const int il) {
  12205. // compute Q and K and (optionally) RoPE them
  12206. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  12207. cb(Qcur, "Qcur", il);
  12208. if (model.layers[il].bq) {
  12209. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  12210. cb(Qcur, "Qcur", il);
  12211. }
  12212. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  12213. cb(Kcur, "Kcur", il);
  12214. if (model.layers[il].bk) {
  12215. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  12216. cb(Kcur, "Kcur", il);
  12217. }
  12218. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  12219. cb(Vcur, "Vcur", il);
  12220. if (model.layers[il].bv) {
  12221. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  12222. cb(Vcur, "Vcur", il);
  12223. }
  12224. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, hparams.n_head(il), n_tokens);
  12225. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, hparams.n_head_kv(il), n_tokens);
  12226. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, hparams.n_head_kv(il), n_tokens);
  12227. const bool use_rope = hparams.rope_finetuned;
  12228. if (use_rope) {
  12229. ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
  12230. Qcur = ggml_rope_ext(
  12231. ctx0, Qcur, inp_pos, rope_factors,
  12232. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  12233. ext_factor, attn_factor, beta_fast, beta_slow
  12234. );
  12235. Kcur = ggml_rope_ext(
  12236. ctx0, Kcur, inp_pos, rope_factors,
  12237. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  12238. ext_factor, attn_factor, beta_fast, beta_slow
  12239. );
  12240. }
  12241. cb(Qcur, "Qcur", il);
  12242. cb(Kcur, "Kcur", il);
  12243. cb(Vcur, "Vcur", il);
  12244. const float kq_scale = hparams.f_attention_scale == 0.0f ? 1.0f/sqrtf(float(n_embd_head)) : hparams.f_attention_scale;
  12245. cur = build_attn(inp_attn,
  12246. model.layers[il].wo, model.layers[il].bo,
  12247. Qcur, Kcur, Vcur, nullptr, nullptr, kq_scale, il);
  12248. cb(cur, "attn_out", il);
  12249. return cur;
  12250. }
  12251. ggml_tensor * build_layer_ffn(
  12252. ggml_tensor * cur,
  12253. ggml_tensor * inpSA,
  12254. const llama_model & model,
  12255. const int il) {
  12256. // For Granite architectures - scale residual
  12257. if (hparams.f_residual_scale) {
  12258. cur = ggml_scale(ctx0, cur, hparams.f_residual_scale);
  12259. }
  12260. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  12261. cb(ffn_inp, "ffn_inp", il);
  12262. // feed-forward network (non-MoE)
  12263. if (model.layers[il].ffn_gate_inp == nullptr) {
  12264. cur = build_norm(ffn_inp,
  12265. model.layers[il].ffn_norm, NULL,
  12266. LLM_NORM_RMS, il);
  12267. cb(cur, "ffn_norm", il);
  12268. cur = build_ffn(cur,
  12269. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  12270. model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, NULL,
  12271. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  12272. NULL,
  12273. LLM_FFN_SILU, LLM_FFN_PAR, il);
  12274. cb(cur, "ffn_out", il);
  12275. } else {
  12276. // MoE branch
  12277. cur = build_norm(ffn_inp,
  12278. model.layers[il].ffn_norm, NULL,
  12279. LLM_NORM_RMS, il);
  12280. cb(cur, "ffn_norm", il);
  12281. ggml_tensor * moe_out = build_moe_ffn(cur,
  12282. model.layers[il].ffn_gate_inp,
  12283. model.layers[il].ffn_up_exps,
  12284. model.layers[il].ffn_gate_exps,
  12285. model.layers[il].ffn_down_exps,
  12286. nullptr,
  12287. n_expert, n_expert_used,
  12288. LLM_FFN_SILU, true,
  12289. false, 0.0,
  12290. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  12291. il);
  12292. cb(moe_out, "ffn_moe_out", il);
  12293. // For Granite MoE Shared
  12294. if (hparams.n_ff_shexp > 0) {
  12295. ggml_tensor * ffn_shexp = build_ffn(cur,
  12296. model.layers[il].ffn_up_shexp, NULL, NULL,
  12297. model.layers[il].ffn_gate_shexp, NULL, NULL,
  12298. model.layers[il].ffn_down_shexp, NULL, NULL,
  12299. NULL,
  12300. LLM_FFN_SILU, LLM_FFN_PAR, il);
  12301. cb(ffn_shexp, "ffn_shexp", il);
  12302. cur = ggml_add(ctx0, moe_out, ffn_shexp);
  12303. cb(cur, "ffn_out", il);
  12304. } else {
  12305. cur = moe_out;
  12306. }
  12307. }
  12308. // For Granite architectures - scale residual
  12309. if (hparams.f_residual_scale) {
  12310. cur = ggml_scale(ctx0, cur, hparams.f_residual_scale);
  12311. }
  12312. cur = ggml_add(ctx0, cur, ffn_inp);
  12313. cb(cur, "ffn_out", il);
  12314. cur = build_cvec(cur, il);
  12315. cb(cur, "l_out", il);
  12316. return cur;
  12317. }
  12318. };
  12319. // ref: https://github.com/facebookresearch/chameleon
  12320. // based on the original build_llama() function, changes:
  12321. // * qk-norm
  12322. // * swin-norm
  12323. // * removed bias
  12324. // * removed MoE
  12325. struct llm_build_chameleon : public llm_graph_context {
  12326. llm_build_chameleon(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  12327. const int64_t n_embd_head = hparams.n_embd_head_v;
  12328. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  12329. GGML_ASSERT(n_embd_head == hparams.n_rot);
  12330. ggml_tensor * cur;
  12331. ggml_tensor * inpL;
  12332. inpL = build_inp_embd(model.tok_embd);
  12333. // inp_pos - contains the positions
  12334. ggml_tensor * inp_pos = build_inp_pos();
  12335. auto * inp_attn = build_attn_inp_kv_unified();
  12336. ggml_tensor * inp_out_ids = build_inp_out_ids();
  12337. for (int il = 0; il < n_layer; ++il) {
  12338. ggml_tensor * inpSA = inpL;
  12339. // norm
  12340. if (hparams.swin_norm) {
  12341. cur = inpL;
  12342. } else {
  12343. cur = build_norm(inpL,
  12344. model.layers[il].attn_norm, NULL,
  12345. LLM_NORM_RMS, il);
  12346. cb(cur, "attn_norm", il);
  12347. }
  12348. // self-attention
  12349. {
  12350. // compute Q and K and RoPE them
  12351. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  12352. cb(Qcur, "Qcur", il);
  12353. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  12354. cb(Kcur, "Kcur", il);
  12355. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  12356. cb(Vcur, "Vcur", il);
  12357. if (model.layers[il].attn_q_norm) {
  12358. Qcur = ggml_view_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens,
  12359. ggml_element_size(Qcur) * n_embd_head,
  12360. ggml_element_size(Qcur) * n_embd_head * n_head,
  12361. 0);
  12362. cb(Qcur, "Qcur", il);
  12363. Qcur = build_norm(Qcur,
  12364. model.layers[il].attn_q_norm,
  12365. model.layers[il].attn_q_norm_b,
  12366. LLM_NORM, il);
  12367. cb(Qcur, "Qcur", il);
  12368. }
  12369. if (model.layers[il].attn_k_norm) {
  12370. Kcur = ggml_view_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens,
  12371. ggml_element_size(Kcur) * n_embd_head,
  12372. ggml_element_size(Kcur) * n_embd_head * n_head_kv,
  12373. 0);
  12374. cb(Kcur, "Kcur", il);
  12375. Kcur = build_norm(Kcur,
  12376. model.layers[il].attn_k_norm,
  12377. model.layers[il].attn_k_norm_b,
  12378. LLM_NORM, il);
  12379. cb(Kcur, "Kcur", il);
  12380. }
  12381. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  12382. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  12383. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  12384. Qcur = ggml_rope_ext(
  12385. ctx0, Qcur, inp_pos, nullptr,
  12386. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  12387. ext_factor, attn_factor, beta_fast, beta_slow
  12388. );
  12389. Kcur = ggml_rope_ext(
  12390. ctx0, Kcur, inp_pos, nullptr,
  12391. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  12392. ext_factor, attn_factor, beta_fast, beta_slow
  12393. );
  12394. cb(Qcur, "Qcur", il);
  12395. cb(Kcur, "Kcur", il);
  12396. cb(Vcur, "Vcur", il);
  12397. cur = build_attn(inp_attn,
  12398. model.layers[il].wo, nullptr,
  12399. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  12400. }
  12401. if (il == n_layer - 1 && inp_out_ids) {
  12402. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  12403. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  12404. }
  12405. if (hparams.swin_norm) {
  12406. cur = build_norm(cur,
  12407. model.layers[il].attn_norm, NULL,
  12408. LLM_NORM_RMS, il);
  12409. }
  12410. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  12411. cb(ffn_inp, "ffn_inp", il);
  12412. // feed-forward network
  12413. if (!hparams.swin_norm) {
  12414. cur = build_norm(ffn_inp,
  12415. model.layers[il].ffn_norm, NULL,
  12416. LLM_NORM_RMS, il);
  12417. cb(cur, "ffn_norm", il);
  12418. }
  12419. cur = build_ffn(cur,
  12420. model.layers[il].ffn_up, NULL, NULL,
  12421. model.layers[il].ffn_gate, NULL, NULL,
  12422. model.layers[il].ffn_down, NULL, NULL,
  12423. NULL,
  12424. LLM_FFN_SILU, LLM_FFN_PAR, il);
  12425. cb(cur, "ffn_out", il);
  12426. if (hparams.swin_norm) {
  12427. cur = build_norm(cur,
  12428. model.layers[il].ffn_norm, NULL,
  12429. LLM_NORM_RMS, il);
  12430. cb(cur, "ffn_norm", il);
  12431. }
  12432. cur = ggml_add(ctx0, cur, ffn_inp);
  12433. cb(cur, "ffn_out", il);
  12434. cur = build_cvec(cur, il);
  12435. cb(cur, "l_out", il);
  12436. // input for next layer
  12437. inpL = cur;
  12438. }
  12439. cur = inpL;
  12440. cur = build_norm(cur,
  12441. model.output_norm, NULL,
  12442. LLM_NORM_RMS, -1);
  12443. cb(cur, "result_norm", -1);
  12444. res->t_embd = cur;
  12445. // lm_head
  12446. cur = build_lora_mm(model.output, cur);
  12447. cb(cur, "result_output_with_img_logits", -1);
  12448. // TODO: this suppresses the output of image tokens, which is required to enable text-only outputs.
  12449. // Needs to be removed once image outputs are supported.
  12450. int img_token_end_idx = 8196;
  12451. int img_token_start_idx = 4;
  12452. int num_img_tokens = img_token_end_idx - img_token_start_idx;
  12453. // creates 1d tensor of size num_img_tokens and values -FLT_MAX,
  12454. // which ensures that text token values are always at least larger than image token values
  12455. ggml_tensor * img_logits = ggml_new_tensor_1d(ctx0, GGML_TYPE_F32, num_img_tokens);
  12456. img_logits = ggml_clamp(ctx0, img_logits, -FLT_MAX, -FLT_MAX);
  12457. cb(img_logits, "img_logits", -1);
  12458. cur = ggml_set_1d(ctx0, cur, img_logits, ggml_element_size(cur) * img_token_start_idx);
  12459. cb(cur, "result_output", -1);
  12460. res->t_logits = cur;
  12461. ggml_build_forward_expand(gf, cur);
  12462. }
  12463. };
  12464. struct llm_build_wavtokenizer_dec : public llm_graph_context {
  12465. llm_build_wavtokenizer_dec(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  12466. ggml_tensor * cur;
  12467. ggml_tensor * inpL;
  12468. inpL = build_inp_embd(model.tok_embd);
  12469. cur = ggml_cont(ctx0, ggml_transpose(ctx0, inpL));
  12470. cur = ggml_conv_1d_ph(ctx0, model.conv1d, cur, 1, 1);
  12471. cur = ggml_add(ctx0, cur, model.conv1d_b);
  12472. // posnet
  12473. for (uint32_t il = 0; il < hparams.posnet.n_layer; ++il) {
  12474. const auto & layer = model.layers[il].posnet;
  12475. inpL = cur;
  12476. switch (il) {
  12477. case 0:
  12478. case 1:
  12479. case 3:
  12480. case 4:
  12481. {
  12482. cur = build_norm(cur,
  12483. layer.norm1,
  12484. layer.norm1_b,
  12485. LLM_NORM_GROUP, 0);
  12486. cur = ggml_mul(ctx0, ggml_sigmoid(ctx0, cur), cur);
  12487. cur = ggml_conv_1d_ph(ctx0, layer.conv1, cur, 1, 1);
  12488. cur = ggml_add(ctx0, cur, layer.conv1_b);
  12489. cur = build_norm(cur,
  12490. layer.norm2,
  12491. layer.norm2_b,
  12492. LLM_NORM_GROUP, 0);
  12493. cur = ggml_mul(ctx0, ggml_sigmoid(ctx0, cur), cur);
  12494. cur = ggml_conv_1d_ph(ctx0, layer.conv2, cur, 1, 1);
  12495. cur = ggml_add(ctx0, cur, layer.conv2_b);
  12496. cur = ggml_add(ctx0, cur, inpL);
  12497. } break;
  12498. case 2:
  12499. {
  12500. cur = build_norm(cur,
  12501. layer.attn_norm,
  12502. layer.attn_norm_b,
  12503. LLM_NORM_GROUP, 0);
  12504. ggml_tensor * q;
  12505. ggml_tensor * k;
  12506. ggml_tensor * v;
  12507. q = ggml_conv_1d_ph(ctx0, layer.attn_q, cur, 1, 1);
  12508. k = ggml_conv_1d_ph(ctx0, layer.attn_k, cur, 1, 1);
  12509. v = ggml_conv_1d_ph(ctx0, layer.attn_v, cur, 1, 1);
  12510. q = ggml_add(ctx0, q, layer.attn_q_b);
  12511. k = ggml_add(ctx0, k, layer.attn_k_b);
  12512. v = ggml_add(ctx0, v, layer.attn_v_b);
  12513. q = ggml_cont(ctx0, ggml_transpose(ctx0, q));
  12514. k = ggml_cont(ctx0, ggml_transpose(ctx0, k));
  12515. ggml_tensor * kq = ggml_mul_mat(ctx0, k, q);
  12516. kq = ggml_soft_max_ext(ctx0, kq, nullptr, 1.0f/sqrtf(float(hparams.posnet.n_embd)), 0.0f);
  12517. cur = ggml_mul_mat(ctx0, kq, v);
  12518. cur = ggml_conv_1d_ph(ctx0, layer.attn_o, cur, 1, 1);
  12519. cur = ggml_add(ctx0, cur, layer.attn_o_b);
  12520. cur = ggml_add(ctx0, cur, inpL);
  12521. } break;
  12522. case 5:
  12523. {
  12524. cur = build_norm(cur,
  12525. layer.norm,
  12526. layer.norm_b,
  12527. LLM_NORM_GROUP, 0);
  12528. } break;
  12529. default: GGML_ABORT("unknown posnet layer");
  12530. };
  12531. }
  12532. cur = ggml_cont(ctx0, ggml_transpose(ctx0, cur));
  12533. cur = build_norm(cur,
  12534. model.tok_norm,
  12535. model.tok_norm_b,
  12536. LLM_NORM, -1);
  12537. cur = ggml_cont(ctx0, ggml_transpose(ctx0, cur));
  12538. inpL = cur;
  12539. // convnext
  12540. for (uint32_t il = 0; il < hparams.convnext.n_layer; ++il) {
  12541. const auto & layer = model.layers[il].convnext;
  12542. cur = inpL;
  12543. cur = ggml_conv_1d_dw_ph(ctx0, layer.dw, cur, 1, 1);
  12544. cur = ggml_add(ctx0, cur, layer.dw_b);
  12545. cur = ggml_cont(ctx0, ggml_transpose(ctx0, cur));
  12546. cur = build_norm(cur,
  12547. layer.norm,
  12548. layer.norm_b,
  12549. LLM_NORM, -1);
  12550. cur = build_ffn(cur,
  12551. layer.pw1, layer.pw1_b, NULL,
  12552. NULL, NULL, NULL,
  12553. layer.pw2, layer.pw2_b, NULL,
  12554. NULL,
  12555. LLM_FFN_GELU, LLM_FFN_SEQ, il);
  12556. cur = ggml_mul(ctx0, cur, layer.gamma);
  12557. cur = ggml_cont(ctx0, ggml_transpose(ctx0, cur));
  12558. inpL = ggml_add(ctx0, cur, inpL);
  12559. }
  12560. cur = inpL;
  12561. cur = ggml_cont(ctx0, ggml_transpose(ctx0, cur));
  12562. cur = build_norm(cur,
  12563. model.output_norm,
  12564. model.output_norm_b,
  12565. LLM_NORM, -1);
  12566. // lm_head
  12567. cur = build_lora_mm(model.output, cur);
  12568. cur = ggml_add(ctx0, cur, model.output_b);
  12569. cb(cur, "result_embd", -1);
  12570. res->t_embd = cur;
  12571. ggml_build_forward_expand(gf, cur);
  12572. }
  12573. };
  12574. struct llm_build_plm : public llm_graph_context {
  12575. llm_build_plm(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  12576. const float kq_scale = 1.0f/sqrtf(float(hparams.n_embd_head_k));
  12577. const uint32_t n_embd_head_qk_rope = hparams.n_rot;
  12578. const uint32_t n_embd_head_qk_nope = hparams.n_embd_head_k - hparams.n_rot;
  12579. const uint32_t kv_lora_rank = hparams.n_lora_kv;
  12580. ggml_tensor * cur;
  12581. ggml_tensor * inpL;
  12582. // {n_embd, n_tokens}
  12583. inpL = build_inp_embd(model.tok_embd);
  12584. // inp_pos - contains the positions
  12585. ggml_tensor * inp_pos = build_inp_pos();
  12586. auto * inp_attn = build_attn_inp_kv_unified();
  12587. ggml_tensor * inp_out_ids = build_inp_out_ids();
  12588. for (int il = 0; il < n_layer; ++il) {
  12589. ggml_tensor * inpSA = inpL;
  12590. // norm
  12591. cur = build_norm(inpL,
  12592. model.layers[il].attn_norm, NULL,
  12593. LLM_NORM_RMS, il);
  12594. cb(cur, "attn_norm", il);
  12595. // self_attention
  12596. {
  12597. ggml_tensor * q = NULL;
  12598. q = ggml_mul_mat(ctx0, model.layers[il].wq, cur);
  12599. cb(q, "q", il);
  12600. // split into {n_head * n_embd_head_qk_nope, n_tokens}
  12601. ggml_tensor * q_nope = ggml_view_3d(ctx0, q, n_embd_head_qk_nope, n_head, n_tokens,
  12602. ggml_row_size(q->type, hparams.n_embd_head_k),
  12603. ggml_row_size(q->type, hparams.n_embd_head_k * n_head),
  12604. 0);
  12605. cb(q_nope, "q_nope", il);
  12606. // and {n_head * n_embd_head_qk_rope, n_tokens}
  12607. ggml_tensor * q_pe = ggml_view_3d(ctx0, q, n_embd_head_qk_rope, n_head, n_tokens,
  12608. ggml_row_size(q->type, hparams.n_embd_head_k),
  12609. ggml_row_size(q->type, hparams.n_embd_head_k * n_head),
  12610. ggml_row_size(q->type, n_embd_head_qk_nope));
  12611. cb(q_pe, "q_pe", il);
  12612. // {n_embd, kv_lora_rank + n_embd_head_qk_rope} * {n_embd, n_tokens} -> {kv_lora_rank + n_embd_head_qk_rope, n_tokens}
  12613. ggml_tensor * kv_pe_compresseed = ggml_mul_mat(ctx0, model.layers[il].wkv_a_mqa, cur);
  12614. cb(kv_pe_compresseed, "kv_pe_compresseed", il);
  12615. // split into {kv_lora_rank, n_tokens}
  12616. ggml_tensor * kv_compressed = ggml_view_2d(ctx0, kv_pe_compresseed, kv_lora_rank, n_tokens,
  12617. kv_pe_compresseed->nb[1],
  12618. 0);
  12619. cb(kv_compressed, "kv_compressed", il);
  12620. // and {n_embd_head_qk_rope, n_tokens}
  12621. ggml_tensor * k_pe = ggml_view_3d(ctx0, kv_pe_compresseed, n_embd_head_qk_rope, 1, n_tokens,
  12622. kv_pe_compresseed->nb[1],
  12623. kv_pe_compresseed->nb[1],
  12624. ggml_row_size(kv_pe_compresseed->type, kv_lora_rank));
  12625. cb(k_pe, "k_pe", il);
  12626. kv_compressed = build_norm(kv_compressed,
  12627. model.layers[il].attn_kv_a_norm, NULL,
  12628. LLM_NORM_RMS, il);
  12629. cb(kv_compressed, "kv_compressed", il);
  12630. // {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}
  12631. ggml_tensor * kv = ggml_mul_mat(ctx0, model.layers[il].wkv_b, kv_compressed);
  12632. cb(kv, "kv", il);
  12633. // split into {n_head * n_embd_head_qk_nope, n_tokens}
  12634. ggml_tensor * k_nope = ggml_view_3d(ctx0, kv, n_embd_head_qk_nope, n_head, n_tokens,
  12635. ggml_row_size(kv->type, n_embd_head_qk_nope + hparams.n_embd_head_v),
  12636. ggml_row_size(kv->type, n_head * (n_embd_head_qk_nope + hparams.n_embd_head_v)),
  12637. 0);
  12638. cb(k_nope, "k_nope", il);
  12639. // and {n_head * n_embd_head_v, n_tokens}
  12640. ggml_tensor * v_states = ggml_view_3d(ctx0, kv, hparams.n_embd_head_v, n_head, n_tokens,
  12641. ggml_row_size(kv->type, (n_embd_head_qk_nope + hparams.n_embd_head_v)),
  12642. ggml_row_size(kv->type, (n_embd_head_qk_nope + hparams.n_embd_head_v)*n_head),
  12643. ggml_row_size(kv->type, (n_embd_head_qk_nope)));
  12644. cb(v_states, "v_states", il);
  12645. v_states = ggml_cont(ctx0, v_states);
  12646. cb(v_states, "v_states", il);
  12647. v_states = ggml_view_2d(ctx0, v_states, hparams.n_embd_head_v * n_head, n_tokens,
  12648. ggml_row_size(kv->type, hparams.n_embd_head_v * n_head),
  12649. 0);
  12650. cb(v_states, "v_states", il);
  12651. q_pe = ggml_rope_ext(
  12652. ctx0, q_pe, inp_pos, nullptr,
  12653. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  12654. ext_factor, attn_factor, beta_fast, beta_slow
  12655. );
  12656. cb(q_pe, "q_pe", il);
  12657. // shared RoPE key
  12658. k_pe = ggml_rope_ext(
  12659. ctx0, k_pe, inp_pos, nullptr,
  12660. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  12661. ext_factor, attn_factor, beta_fast, beta_slow
  12662. );
  12663. cb(k_pe, "k_pe", il);
  12664. ggml_tensor * q_states = ggml_concat(ctx0, q_nope, q_pe, 0);
  12665. cb(q_states, "q_states", il);
  12666. ggml_tensor * k_states = ggml_concat(ctx0, k_nope, ggml_repeat(ctx0, k_pe, q_pe), 0);
  12667. cb(k_states, "k_states", il);
  12668. cur = build_attn(inp_attn,
  12669. model.layers[il].wo, NULL,
  12670. q_states, k_states, v_states, nullptr, nullptr, kq_scale, il);
  12671. }
  12672. if (il == n_layer - 1 && inp_out_ids) {
  12673. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  12674. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  12675. }
  12676. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  12677. cb(ffn_inp, "ffn_inp", il);
  12678. cur = build_norm(ffn_inp,
  12679. model.layers[il].ffn_norm, NULL,
  12680. LLM_NORM_RMS, il);
  12681. cb(cur, "ffn_norm", il);
  12682. cur = build_ffn(cur,
  12683. model.layers[il].ffn_up, NULL, NULL,
  12684. NULL, NULL, NULL,
  12685. model.layers[il].ffn_down, NULL, NULL,
  12686. NULL,
  12687. LLM_FFN_RELU_SQR, LLM_FFN_SEQ, il);
  12688. cb(cur, "ffn_out", il);
  12689. cur = ggml_add(ctx0, cur, ffn_inp);
  12690. cur = build_cvec(cur, il);
  12691. cb(cur, "l_out", il);
  12692. // input for next layer
  12693. inpL = cur;
  12694. }
  12695. cur = inpL;
  12696. cur = build_norm(cur,
  12697. model.output_norm, NULL,
  12698. LLM_NORM_RMS, -1);
  12699. cb(cur, "result_norm", -1);
  12700. res->t_embd = cur;
  12701. cur = build_lora_mm(model.output, cur);
  12702. cb(cur, "result_output", -1);
  12703. res->t_logits = cur;
  12704. ggml_build_forward_expand(gf, cur);
  12705. }
  12706. };
  12707. struct llm_build_bailingmoe : public llm_graph_context {
  12708. llm_build_bailingmoe(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  12709. ggml_tensor * cur;
  12710. ggml_tensor * inpL;
  12711. inpL = build_inp_embd(model.tok_embd);
  12712. // inp_pos - contains the positions
  12713. ggml_tensor * inp_pos = build_inp_pos();
  12714. auto * inp_attn = build_attn_inp_kv_unified();
  12715. ggml_tensor * inp_out_ids = build_inp_out_ids();
  12716. for (int il = 0; il < n_layer; ++il) {
  12717. ggml_tensor * inpSA = inpL;
  12718. // norm
  12719. cur = build_norm(inpL,
  12720. model.layers[il].attn_norm, NULL,
  12721. LLM_NORM_RMS, il);
  12722. cb(cur, "attn_norm", il);
  12723. // self-attention
  12724. {
  12725. // rope freq factors for llama3; may return nullptr for llama2 and other models
  12726. ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
  12727. // compute Q and K and RoPE them
  12728. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  12729. cb(Qcur, "Qcur", il);
  12730. if (model.layers[il].bq) {
  12731. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  12732. cb(Qcur, "Qcur", il);
  12733. }
  12734. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  12735. cb(Kcur, "Kcur", il);
  12736. if (model.layers[il].bk) {
  12737. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  12738. cb(Kcur, "Kcur", il);
  12739. }
  12740. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  12741. cb(Vcur, "Vcur", il);
  12742. if (model.layers[il].bv) {
  12743. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  12744. cb(Vcur, "Vcur", il);
  12745. }
  12746. Qcur = ggml_reshape_3d(ctx0, Qcur, n_rot, n_head, n_tokens);
  12747. Kcur = ggml_reshape_3d(ctx0, Kcur, n_rot, n_head_kv, n_tokens);
  12748. Vcur = ggml_reshape_3d(ctx0, Vcur, n_rot, n_head_kv, n_tokens);
  12749. Qcur = ggml_rope_ext(
  12750. ctx0, Qcur, inp_pos, rope_factors,
  12751. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  12752. ext_factor, attn_factor, beta_fast, beta_slow
  12753. );
  12754. Kcur = ggml_rope_ext(
  12755. ctx0, Kcur, inp_pos, rope_factors,
  12756. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  12757. ext_factor, attn_factor, beta_fast, beta_slow
  12758. );
  12759. cb(Qcur, "Qcur", il);
  12760. cb(Kcur, "Kcur", il);
  12761. cb(Vcur, "Vcur", il);
  12762. cur = build_attn(inp_attn,
  12763. model.layers[il].wo, model.layers[il].bo,
  12764. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_rot)), il);
  12765. }
  12766. if (il == n_layer - 1 && inp_out_ids) {
  12767. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  12768. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  12769. }
  12770. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  12771. cb(ffn_inp, "ffn_inp", il);
  12772. cur = build_norm(ffn_inp,
  12773. model.layers[il].ffn_norm, NULL,
  12774. LLM_NORM_RMS, il);
  12775. cb(cur, "ffn_norm", il);
  12776. ggml_tensor * moe_out =
  12777. build_moe_ffn(cur,
  12778. model.layers[il].ffn_gate_inp,
  12779. model.layers[il].ffn_up_exps,
  12780. model.layers[il].ffn_gate_exps,
  12781. model.layers[il].ffn_down_exps,
  12782. nullptr,
  12783. n_expert, n_expert_used,
  12784. LLM_FFN_SILU, hparams.expert_weights_norm,
  12785. false, hparams.expert_weights_scale,
  12786. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  12787. il);
  12788. cb(moe_out, "ffn_moe_out", il);
  12789. // FFN shared expert
  12790. {
  12791. ggml_tensor * ffn_shexp = build_ffn(cur,
  12792. model.layers[il].ffn_up_shexp, NULL, NULL,
  12793. model.layers[il].ffn_gate_shexp, NULL, NULL,
  12794. model.layers[il].ffn_down_shexp, NULL, NULL,
  12795. NULL,
  12796. LLM_FFN_SILU, LLM_FFN_PAR, il);
  12797. cb(ffn_shexp, "ffn_shexp", il);
  12798. cur = ggml_add(ctx0, moe_out, ffn_shexp);
  12799. cb(cur, "ffn_out", il);
  12800. }
  12801. cur = ggml_add(ctx0, cur, ffn_inp);
  12802. cur = build_cvec(cur, il);
  12803. cb(cur, "l_out", il);
  12804. // input for next layer
  12805. inpL = cur;
  12806. }
  12807. cur = inpL;
  12808. cur = build_norm(cur,
  12809. model.output_norm, NULL,
  12810. LLM_NORM_RMS, -1);
  12811. cb(cur, "result_norm", -1);
  12812. res->t_embd = cur;
  12813. // lm_head
  12814. cur = build_lora_mm(model.output, cur);
  12815. cb(cur, "result_output", -1);
  12816. res->t_logits = cur;
  12817. ggml_build_forward_expand(gf, cur);
  12818. }
  12819. };
  12820. struct llm_build_dots1 : public llm_graph_context {
  12821. llm_build_dots1(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  12822. const int64_t n_embd_head = hparams.n_embd_head_v;
  12823. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  12824. GGML_ASSERT(n_embd_head == hparams.n_rot);
  12825. ggml_tensor * cur;
  12826. ggml_tensor * inpL;
  12827. inpL = build_inp_embd(model.tok_embd);
  12828. // inp_pos - contains the positions
  12829. ggml_tensor * inp_pos = build_inp_pos();
  12830. auto * inp_attn = build_attn_inp_kv_unified();
  12831. ggml_tensor * inp_out_ids = build_inp_out_ids();
  12832. for (int il = 0; il < n_layer; ++il) {
  12833. ggml_tensor * inpSA = inpL;
  12834. // norm
  12835. cur = build_norm(inpL,
  12836. model.layers[il].attn_norm, NULL,
  12837. LLM_NORM_RMS, il);
  12838. cb(cur, "attn_norm", il);
  12839. // self_attention
  12840. {
  12841. // compute Q and K and RoPE them
  12842. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  12843. cb(Qcur, "Qcur", il);
  12844. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  12845. cb(Kcur, "Kcur", il);
  12846. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  12847. cb(Vcur, "Vcur", il);
  12848. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  12849. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  12850. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  12851. Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);
  12852. cb(Qcur, "Qcur_normed", il);
  12853. Qcur = ggml_rope_ext(
  12854. ctx0, Qcur, inp_pos, nullptr,
  12855. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  12856. ext_factor, attn_factor, beta_fast, beta_slow
  12857. );
  12858. Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);
  12859. cb(Kcur, "Kcur_normed", il);
  12860. Kcur = ggml_rope_ext(
  12861. ctx0, Kcur, inp_pos, nullptr,
  12862. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  12863. ext_factor, attn_factor, beta_fast, beta_slow
  12864. );
  12865. cb(Qcur, "Qcur", il);
  12866. cb(Kcur, "Kcur", il);
  12867. cb(Vcur, "Vcur", il);
  12868. cur = build_attn(inp_attn,
  12869. model.layers[il].wo, model.layers[il].bo,
  12870. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  12871. }
  12872. if (il == n_layer - 1 && inp_out_ids) {
  12873. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  12874. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  12875. }
  12876. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  12877. cb(ffn_inp, "ffn_inp", il);
  12878. // MoE branch
  12879. cur = build_norm(ffn_inp,
  12880. model.layers[il].ffn_norm, NULL,
  12881. LLM_NORM_RMS, il);
  12882. cb(cur, "ffn_norm", il);
  12883. if ((uint32_t) il < hparams.n_layer_dense_lead) {
  12884. cur = build_ffn(cur,
  12885. model.layers[il].ffn_up, NULL, NULL,
  12886. model.layers[il].ffn_gate, NULL, NULL,
  12887. model.layers[il].ffn_down, NULL, NULL,
  12888. NULL,
  12889. LLM_FFN_SILU, LLM_FFN_PAR, il);
  12890. cb(cur, "ffn_out", il);
  12891. } else {
  12892. ggml_tensor * moe_out =
  12893. build_moe_ffn(cur,
  12894. model.layers[il].ffn_gate_inp,
  12895. model.layers[il].ffn_up_exps,
  12896. model.layers[il].ffn_gate_exps,
  12897. model.layers[il].ffn_down_exps,
  12898. model.layers[il].ffn_exp_probs_b,
  12899. n_expert, n_expert_used,
  12900. LLM_FFN_SILU, hparams.expert_weights_norm,
  12901. true, hparams.expert_weights_scale,
  12902. (llama_expert_gating_func_type) hparams.expert_gating_func,
  12903. il);
  12904. cb(moe_out, "ffn_moe_out", il);
  12905. {
  12906. ggml_tensor * ffn_shexp = build_ffn(cur,
  12907. model.layers[il].ffn_up_shexp, NULL, NULL,
  12908. model.layers[il].ffn_gate_shexp, NULL, NULL,
  12909. model.layers[il].ffn_down_shexp, NULL, NULL,
  12910. NULL,
  12911. LLM_FFN_SILU, LLM_FFN_PAR, il);
  12912. cb(ffn_shexp, "ffn_shexp", il);
  12913. cur = ggml_add(ctx0, moe_out, ffn_shexp);
  12914. cb(cur, "ffn_out", il);
  12915. }
  12916. }
  12917. cur = ggml_add(ctx0, cur, ffn_inp);
  12918. cur = build_cvec(cur, il);
  12919. cb(cur, "l_out", il);
  12920. // input for next layer
  12921. inpL = cur;
  12922. }
  12923. cur = inpL;
  12924. cur = build_norm(cur,
  12925. model.output_norm, NULL,
  12926. LLM_NORM_RMS, -1);
  12927. cb(cur, "result_norm", -1);
  12928. res->t_embd = cur;
  12929. // lm_head
  12930. cur = build_lora_mm(model.output, cur);
  12931. cb(cur, "result_output", -1);
  12932. res->t_logits = cur;
  12933. ggml_build_forward_expand(gf, cur);
  12934. }
  12935. };
  12936. struct llm_build_ernie4_5 : public llm_graph_context {
  12937. llm_build_ernie4_5(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  12938. const int64_t n_embd_head = hparams.n_embd_head_v;
  12939. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  12940. GGML_ASSERT(n_embd_head == hparams.n_rot);
  12941. ggml_tensor * cur;
  12942. ggml_tensor * inpL;
  12943. inpL = build_inp_embd(model.tok_embd);
  12944. // inp_pos - contains the positions
  12945. ggml_tensor * inp_pos = build_inp_pos();
  12946. auto * inp_attn = build_attn_inp_kv_unified();
  12947. for (int il = 0; il < n_layer; ++il) {
  12948. ggml_tensor * inpSA = inpL;
  12949. // norm
  12950. {
  12951. cur = build_norm(inpL,
  12952. model.layers[il].attn_norm, NULL,
  12953. LLM_NORM_RMS, il);
  12954. cb(cur, "attn_norm", il);
  12955. }
  12956. // self-attention
  12957. {
  12958. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  12959. cb(Qcur, "Qcur", il);
  12960. if (model.layers[il].bq) {
  12961. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  12962. cb(Qcur, "Qcur", il);
  12963. }
  12964. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  12965. cb(Kcur, "Kcur", il);
  12966. if (model.layers[il].bk) {
  12967. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  12968. cb(Kcur, "Kcur", il);
  12969. }
  12970. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  12971. cb(Vcur, "Vcur", il);
  12972. if (model.layers[il].bv) {
  12973. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  12974. cb(Vcur, "Vcur", il);
  12975. }
  12976. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  12977. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  12978. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  12979. Qcur = ggml_rope_ext(
  12980. ctx0, Qcur, inp_pos, nullptr,
  12981. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  12982. ext_factor, attn_factor, beta_fast, beta_slow
  12983. );
  12984. Kcur = ggml_rope_ext(
  12985. ctx0, Kcur, inp_pos, nullptr,
  12986. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  12987. ext_factor, attn_factor, beta_fast, beta_slow
  12988. );
  12989. cb(Qcur, "Qcur", il);
  12990. cb(Kcur, "Kcur", il);
  12991. cb(Vcur, "Vcur", il);
  12992. cur = build_attn(inp_attn,
  12993. model.layers[il].wo, NULL,
  12994. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  12995. }
  12996. if (il == n_layer - 1) {
  12997. // skip computing output for unused tokens
  12998. ggml_tensor * inp_out_ids = build_inp_out_ids();
  12999. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  13000. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  13001. }
  13002. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  13003. cb(ffn_inp, "ffn_inp", il);
  13004. // feed-forward network
  13005. {
  13006. cur = build_norm(ffn_inp,
  13007. model.layers[il].ffn_norm, NULL,
  13008. LLM_NORM_RMS, il);
  13009. cb(cur, "ffn_norm", il);
  13010. cur = build_ffn(cur,
  13011. model.layers[il].ffn_up, NULL, NULL,
  13012. model.layers[il].ffn_gate, NULL, NULL,
  13013. model.layers[il].ffn_down, NULL, NULL,
  13014. NULL,
  13015. LLM_FFN_SILU, LLM_FFN_PAR, il);
  13016. cb(cur, "ffn_out", il);
  13017. }
  13018. cur = ggml_add(ctx0, cur, ffn_inp);
  13019. cur = build_cvec(cur, il);
  13020. cb(cur, "l_out", il);
  13021. // input for next layer
  13022. inpL = cur;
  13023. }
  13024. cur = inpL;
  13025. cur = build_norm(cur,
  13026. model.output_norm, NULL,
  13027. LLM_NORM_RMS, -1);
  13028. cb(cur, "result_norm", -1);
  13029. res->t_embd = cur;
  13030. // lm_head
  13031. cur = build_lora_mm(model.output, cur);
  13032. cb(cur, "result_output", -1);
  13033. res->t_logits = cur;
  13034. ggml_build_forward_expand(gf, cur);
  13035. }
  13036. };
  13037. struct llm_build_ernie4_5_moe : public llm_graph_context {
  13038. llm_build_ernie4_5_moe(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  13039. const int64_t n_embd_head = hparams.n_embd_head_v;
  13040. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  13041. GGML_ASSERT(n_embd_head == hparams.n_rot);
  13042. ggml_tensor * cur;
  13043. ggml_tensor * inpL;
  13044. inpL = build_inp_embd(model.tok_embd);
  13045. // inp_pos - contains the positions
  13046. ggml_tensor * inp_pos = build_inp_pos();
  13047. auto * inp_attn = build_attn_inp_kv_unified();
  13048. ggml_tensor * inp_out_ids = build_inp_out_ids();
  13049. GGML_ASSERT(hparams.n_moe_layer_step > 0 && "Ernie 4.5 MoE requires n_moe_layer_step > 0");
  13050. for (int il = 0; il < n_layer; ++il) {
  13051. ggml_tensor * inpSA = inpL;
  13052. // norm
  13053. {
  13054. cur = build_norm(inpL,
  13055. model.layers[il].attn_norm, NULL,
  13056. LLM_NORM_RMS, il);
  13057. cb(cur, "attn_norm", il);
  13058. }
  13059. // self-attention
  13060. {
  13061. // compute Q and K and RoPE them
  13062. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  13063. cb(Qcur, "Qcur", il);
  13064. if (model.layers[il].bq) {
  13065. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  13066. cb(Qcur, "Qcur", il);
  13067. }
  13068. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  13069. cb(Kcur, "Kcur", il);
  13070. if (model.layers[il].bk) {
  13071. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  13072. cb(Kcur, "Kcur", il);
  13073. }
  13074. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  13075. cb(Vcur, "Vcur", il);
  13076. if (model.layers[il].bv) {
  13077. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  13078. cb(Vcur, "Vcur", il);
  13079. }
  13080. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  13081. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  13082. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  13083. Qcur = ggml_rope_ext(
  13084. ctx0, Qcur, inp_pos, nullptr,
  13085. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  13086. ext_factor, attn_factor, beta_fast, beta_slow
  13087. );
  13088. Kcur = ggml_rope_ext(
  13089. ctx0, Kcur, inp_pos, nullptr,
  13090. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  13091. ext_factor, attn_factor, beta_fast, beta_slow
  13092. );
  13093. cb(Qcur, "Qcur", il);
  13094. cb(Kcur, "Kcur", il);
  13095. cb(Vcur, "Vcur", il);
  13096. cur = build_attn(inp_attn,
  13097. model.layers[il].wo, NULL,
  13098. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  13099. cb(cur, "attn_out", il);
  13100. }
  13101. if (il == n_layer - 1 && inp_out_ids) {
  13102. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  13103. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  13104. }
  13105. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  13106. cb(ffn_inp, "ffn_inp", il);
  13107. // feed-forward network
  13108. bool is_moe_layer = static_cast<uint32_t>(il) >= hparams.n_layer_dense_lead && (il + 1) % hparams.n_moe_layer_step == 0;
  13109. if (!is_moe_layer) {
  13110. cur = build_norm(ffn_inp,
  13111. model.layers[il].ffn_norm, NULL,
  13112. LLM_NORM_RMS, il);
  13113. cb(cur, "ffn_norm", il);
  13114. cur = build_ffn(cur,
  13115. model.layers[il].ffn_up, NULL, NULL,
  13116. model.layers[il].ffn_gate, NULL, NULL,
  13117. model.layers[il].ffn_down, NULL, NULL,
  13118. NULL,
  13119. LLM_FFN_SILU, LLM_FFN_PAR, il);
  13120. cb(cur, "ffn_out", il);
  13121. } else {
  13122. // MoE branch
  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. ggml_tensor * moe_out = build_moe_ffn(cur,
  13128. model.layers[il].ffn_gate_inp,
  13129. model.layers[il].ffn_up_exps,
  13130. model.layers[il].ffn_gate_exps,
  13131. model.layers[il].ffn_down_exps,
  13132. model.layers[il].ffn_exp_probs_b,
  13133. n_expert, n_expert_used,
  13134. LLM_FFN_SILU, true,
  13135. false, 0.0,
  13136. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  13137. il);
  13138. cb(moe_out, "ffn_moe_out", il);
  13139. // Shared expert (if present)
  13140. if (hparams.n_ff_shexp > 0) {
  13141. ggml_tensor * ffn_shexp = build_ffn(cur,
  13142. model.layers[il].ffn_up_shexp, NULL, NULL,
  13143. model.layers[il].ffn_gate_shexp, NULL, NULL,
  13144. model.layers[il].ffn_down_shexp, NULL, NULL,
  13145. NULL,
  13146. LLM_FFN_SILU, LLM_FFN_PAR, il);
  13147. cb(ffn_shexp, "ffn_shexp", il);
  13148. cur = ggml_add(ctx0, moe_out, ffn_shexp);
  13149. } else {
  13150. cur = moe_out;
  13151. }
  13152. cb(cur, "ffn_out", il);
  13153. }
  13154. cur = ggml_add(ctx0, cur, ffn_inp);
  13155. cb(cur, "ffn_out", il);
  13156. cur = build_cvec(cur, il);
  13157. cb(cur, "l_out", il);
  13158. // input for next layer
  13159. inpL = cur;
  13160. }
  13161. cur = inpL;
  13162. cur = build_norm(cur,
  13163. model.output_norm, NULL,
  13164. LLM_NORM_RMS, -1);
  13165. cb(cur, "result_norm", -1);
  13166. res->t_embd = cur;
  13167. // lm_head
  13168. cur = build_lora_mm(model.output, cur);
  13169. cb(cur, "result_output", -1);
  13170. res->t_logits = cur;
  13171. ggml_build_forward_expand(gf, cur);
  13172. }
  13173. };
  13174. struct llm_build_falcon_h1 : public llm_graph_context_mamba {
  13175. llm_build_falcon_h1(const llama_model & model, const llm_graph_params & params) : llm_graph_context_mamba(params) {
  13176. const int64_t n_embd_head = hparams.n_embd_head_v;
  13177. ggml_tensor * cur;
  13178. ggml_tensor * inpL;
  13179. inpL = build_inp_embd(model.tok_embd);
  13180. // inp_pos - contains the positions
  13181. ggml_tensor * inp_pos = build_inp_pos();
  13182. // Build the inputs in the recurrent & kv cache
  13183. auto * inp = build_inp_mem_hybrid();
  13184. const float kq_scale = hparams.f_attention_scale == 0.0f ? 1.0f/sqrtf(float(n_embd_head)) : hparams.f_attention_scale;
  13185. ggml_tensor * inp_out_ids = build_inp_out_ids();
  13186. for (int il = 0; il < n_layer; ++il) {
  13187. ggml_tensor * inpSA = inpL;
  13188. cur = build_norm(inpL,
  13189. model.layers[il].attn_norm, NULL,
  13190. LLM_NORM_RMS, il);
  13191. cb(cur, "attn_norm", il);
  13192. // self-attention
  13193. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  13194. cb(Qcur, "Qcur", il);
  13195. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  13196. cb(Kcur, "Kcur", il);
  13197. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  13198. cb(Vcur, "Vcur", il);
  13199. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  13200. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  13201. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  13202. Qcur = ggml_rope_ext(
  13203. ctx0, Qcur, inp_pos, nullptr,
  13204. n_rot, hparams.rope_type, n_ctx_orig, freq_base, freq_scale,
  13205. ext_factor, attn_factor, beta_fast, beta_slow);
  13206. Kcur = ggml_rope_ext(
  13207. ctx0, Kcur, inp_pos, nullptr,
  13208. n_rot, hparams.rope_type, n_ctx_orig, freq_base, freq_scale,
  13209. ext_factor, attn_factor, beta_fast, beta_slow
  13210. );
  13211. cb(Qcur, "Qcur-post-rope", il);
  13212. cb(Kcur, "Kcur-post-rope", il);
  13213. cb(Vcur, "Vcur-post-rope", il);
  13214. ggml_tensor * attn_out = build_attn(inp->get_attn(),
  13215. model.layers[il].wo, NULL,
  13216. Qcur, Kcur, Vcur, nullptr, nullptr, kq_scale, il);
  13217. cb(attn_out, "attn_out", il);
  13218. cur = build_norm(inpL,
  13219. model.layers[il].attn_norm, NULL,
  13220. LLM_NORM_RMS, il);
  13221. // Mamba2 layer
  13222. cb(cur, "ssm_in", il);
  13223. ggml_tensor * ssm_out = build_mamba2_layer(inp->get_recr(), cur, model, ubatch, il);
  13224. cb(ssm_out, "ssm_out", il);
  13225. // // Aggregation
  13226. cur = ggml_add(ctx0, attn_out, ssm_out);
  13227. inpSA = ggml_add(ctx0, cur, inpSA);
  13228. cb(cur, "layer_out", il);
  13229. if (il == n_layer - 1 && inp_out_ids) {
  13230. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  13231. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  13232. }
  13233. ggml_tensor * ffn_inp = inpSA;
  13234. cb(ffn_inp, "ffn_inp", il);
  13235. // feed-forward network
  13236. cur = build_norm(ffn_inp,
  13237. model.layers[il].ffn_norm, NULL,
  13238. LLM_NORM_RMS, il);
  13239. cb(cur, "ffn_norm", il);
  13240. cur = build_ffn(cur,
  13241. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  13242. model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, NULL,
  13243. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  13244. NULL,
  13245. LLM_FFN_SILU, LLM_FFN_PAR, il);
  13246. cb(cur, "ffn_out", il);
  13247. cur = ggml_add(ctx0, cur, inpSA);
  13248. cur = build_cvec(cur, il);
  13249. cb(cur, "l_out", il);
  13250. // input for next layer
  13251. inpL = cur;
  13252. }
  13253. cur = inpL;
  13254. cur = build_norm(cur,
  13255. model.output_norm, NULL,
  13256. LLM_NORM_RMS, -1);
  13257. cb(cur, "result_norm", -1);
  13258. res->t_embd = cur;
  13259. // lm_head
  13260. cur = build_lora_mm(model.output, cur);
  13261. cb(cur, "result_output", -1);
  13262. res->t_logits = cur;
  13263. ggml_build_forward_expand(gf, cur);
  13264. }
  13265. };
  13266. struct llm_build_plamo2 : public llm_graph_context_mamba {
  13267. llm_build_plamo2(const llama_model & model, const llm_graph_params & params) : llm_graph_context_mamba(params) {
  13268. ggml_tensor * cur;
  13269. ggml_tensor * inpL;
  13270. // {n_embd, n_tokens}
  13271. inpL = build_inp_embd(model.tok_embd);
  13272. cb(inpL, "embedding_output", -1);
  13273. ggml_tensor * inp_pos = build_inp_pos();
  13274. auto * inp_hybrid = build_inp_mem_hybrid();
  13275. ggml_tensor * inp_out_ids = build_inp_out_ids();
  13276. for (int il = 0; il < n_layer; ++il) {
  13277. ggml_tensor * residual = inpL;
  13278. // ggml_graph_add_node(gf, model.layers[il].attn_norm);
  13279. // cb(model.layers[il].attn_norm, "attn_norm", il);
  13280. // pre_mixer_norm
  13281. cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
  13282. // check if this layer is Mamba or Attention
  13283. bool is_mamba_layer = hparams.is_recurrent(il);
  13284. if (is_mamba_layer) {
  13285. // PLaMo-2 Mamba layer
  13286. cur = build_plamo2_mamba_layer(inp_hybrid->get_recr(), cur, model, ubatch, il);
  13287. } else {
  13288. // PLaMo-2 Attention layer
  13289. cur = build_plamo2_attn_layer(inp_hybrid->get_attn(), inp_pos, cur, model, il);
  13290. }
  13291. // post_mixer_norm
  13292. cur = build_norm(cur, model.layers[il].attn_post_norm, NULL, LLM_NORM_RMS, il);
  13293. cb(cur, "attn_post_norm", il);
  13294. // residual connection
  13295. cur = ggml_add(ctx0, cur, residual);
  13296. cb(cur, "attn_residual", il);
  13297. residual = cur;
  13298. // pre-ffn norm
  13299. cur = build_norm(cur, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);
  13300. cb(cur, "ffn_pre_norm", il);
  13301. // feed-forward network
  13302. cur = build_ffn(cur,
  13303. model.layers[il].ffn_up, NULL, NULL,
  13304. NULL, NULL, NULL,
  13305. model.layers[il].ffn_down, NULL, NULL,
  13306. NULL,
  13307. LLM_FFN_SWIGLU, LLM_FFN_SEQ, il);
  13308. cb(cur, "ffn_out", il);
  13309. // post ffn norm
  13310. cur = build_norm(cur, model.layers[il].ffn_post_norm, NULL, LLM_NORM_RMS, il);
  13311. cb(cur, "ffn_post_norm", il);
  13312. if (il == n_layer - 1 && inp_out_ids) {
  13313. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  13314. residual = ggml_get_rows(ctx0, residual, inp_out_ids);
  13315. }
  13316. // residual connection
  13317. cur = ggml_add(ctx0, cur, residual);
  13318. cb(cur, "ffn_residual", il);
  13319. inpL = cur;
  13320. }
  13321. cur = inpL;
  13322. // final norm
  13323. cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1);
  13324. cb(cur, "result_norm", -1);
  13325. // lm_head
  13326. cur = build_lora_mm(model.output, cur);
  13327. cb(cur, "result_output", -1);
  13328. // Explicitly mark as output tensor to ensure proper backend assignment
  13329. ggml_set_output(cur);
  13330. res->t_logits = cur;
  13331. ggml_build_forward_expand(gf, cur);
  13332. }
  13333. private:
  13334. ggml_tensor * build_plamo2_attn_layer(
  13335. llm_graph_input_attn_kv_unified * inp,
  13336. ggml_tensor * inp_pos,
  13337. ggml_tensor * cur,
  13338. const llama_model & model,
  13339. int il) {
  13340. // self-attention
  13341. {
  13342. // PLaMo-2 uses combined QKV tensor
  13343. ggml_tensor * qkv = build_lora_mm(model.layers[il].wqkv, cur);
  13344. cb(qkv, "wqkv", il);
  13345. // split QKV tensor into Q, K, V
  13346. const int64_t n_embd_head_q = hparams.n_embd_head_k;
  13347. const int64_t n_embd_head_k = hparams.n_embd_head_k;
  13348. const int64_t n_embd_head_v = hparams.n_embd_head_v;
  13349. int32_t n_head_kv = hparams.n_head_kv(il);
  13350. const int64_t q_offset = 0;
  13351. const int64_t k_offset = n_embd_head_q * n_head;
  13352. const int64_t v_offset = k_offset + n_embd_head_k * n_head_kv;
  13353. 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));
  13354. 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));
  13355. ggml_tensor * Vcur = ggml_view_2d(ctx0, qkv, n_embd_head_v * n_head_kv, n_tokens, qkv->nb[1], v_offset * ggml_element_size(qkv));
  13356. cb(Qcur, "Qcur", il);
  13357. cb(Kcur, "Kcur", il);
  13358. cb(Vcur, "Vcur", il);
  13359. Vcur = ggml_cont_3d(ctx0, Vcur, n_embd_head_v, n_head_kv, n_tokens);
  13360. Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);
  13361. cb(Qcur, "Qcur_normed", il);
  13362. Qcur = ggml_rope_ext(
  13363. ctx0, Qcur, inp_pos, nullptr,
  13364. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  13365. ext_factor, attn_factor, beta_fast, beta_slow
  13366. );
  13367. Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);
  13368. cb(Kcur, "Kcur_normed", il);
  13369. Kcur = ggml_rope_ext(
  13370. ctx0, Kcur, inp_pos, nullptr,
  13371. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  13372. ext_factor, attn_factor, beta_fast, beta_slow
  13373. );
  13374. cur = build_attn(inp, model.layers[il].wo, NULL, Qcur, Kcur, Vcur, NULL, NULL, 1.0f/sqrtf(float(n_embd_head_v)), il);
  13375. }
  13376. cb(cur, "attn_out", il);
  13377. return cur;
  13378. }
  13379. ggml_tensor * build_plamo2_mamba_layer(
  13380. llm_graph_input_rs * inp,
  13381. ggml_tensor * cur,
  13382. const llama_model & model,
  13383. const llama_ubatch & ubatch,
  13384. int il) {
  13385. const auto * mctx_cur = inp->mctx;
  13386. const auto kv_head = mctx_cur->get_head();
  13387. const int64_t d_conv = hparams.ssm_d_conv;
  13388. const int64_t d_inner = hparams.ssm_d_inner;
  13389. const int64_t d_state = hparams.ssm_d_state;
  13390. const int64_t n_heads = hparams.ssm_dt_rank;
  13391. const int64_t head_dim = d_inner / n_heads;
  13392. const int64_t n_group = hparams.ssm_n_group;
  13393. const int64_t n_seqs = ubatch.n_seqs;
  13394. const int64_t n_seq_tokens = ubatch.n_seq_tokens;
  13395. GGML_ASSERT(n_seqs != 0);
  13396. GGML_ASSERT(ubatch.equal_seqs());
  13397. GGML_ASSERT(ubatch.n_tokens == n_seq_tokens * n_seqs);
  13398. ggml_tensor * conv_states_all = mctx_cur->get_r_l(il);
  13399. ggml_tensor * ssm_states_all = mctx_cur->get_s_l(il);
  13400. ggml_tensor * conv = build_rs(inp, conv_states_all, hparams.n_embd_r(), n_seqs);
  13401. conv = ggml_reshape_3d(ctx0, conv, d_conv - 1, d_inner + 2*n_group*d_state, n_seqs);
  13402. // {n_embd, n_tokens} => {n_embd, n_seq_tokens, n_seqs}
  13403. cur = ggml_reshape_3d(ctx0, cur, cur->ne[0], n_seq_tokens, n_seqs);
  13404. // in_proj: {n_embd, 2*d_inner} @ {n_embd, n_seq_tokens, n_seqs} => {2*d_inner, n_seq_tokens, n_seqs}
  13405. ggml_tensor * zx = build_lora_mm(model.layers[il].ssm_in, cur);
  13406. cb(zx, "mamba_in_proj", il);
  13407. // {8192, 5, 1, 1} -> {8192, 1, 5, 1}
  13408. zx = ggml_permute(ctx0, zx, 0, 2, 1, 3);
  13409. zx = ggml_cont_4d(ctx0, zx, head_dim * 2, n_heads, n_seq_tokens, n_seqs);
  13410. cb(zx, "mamba_in_proj_out", il);
  13411. // split into z and x
  13412. // => {head_dim * n_heads, n_seq_tokens, n_seqs}
  13413. 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));
  13414. x = ggml_cont_3d(ctx0, x, head_dim * n_heads, n_seq_tokens, n_seqs);
  13415. // x = ggml_permute(ctx0, x, 0, 2, 1, 3);
  13416. cb(x, "mamba_x_split", il);
  13417. 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);
  13418. cb(z, "mamba_z_split", il);
  13419. // conv1d
  13420. {
  13421. // => {d_conv - 1 + n_seq_tokens, d_inner, n_seqs}
  13422. ggml_tensor * conv_x = ggml_concat(ctx0, conv, ggml_transpose(ctx0, x), 0);
  13423. cb(conv_x, "mamba_conv1d_input", il);
  13424. // copy last (d_conv - 1) columns back into the state cache
  13425. ggml_tensor * last_conv = ggml_view_3d(ctx0, conv_x, d_conv - 1, d_inner, n_seqs,
  13426. conv_x->nb[1], conv_x->nb[2], n_seq_tokens*(conv_x->nb[0]));
  13427. ggml_build_forward_expand(gf,
  13428. ggml_cpy(ctx0, last_conv,
  13429. ggml_view_1d(ctx0, conv_states_all,
  13430. (d_conv - 1)*(d_inner + 2*n_group*d_state)*(n_seqs),
  13431. kv_head*(d_conv - 1)*(d_inner + 2*n_group*d_state)*ggml_element_size(conv_states_all))));
  13432. cb(conv_states_all, "mamba_conv1d_state", il);
  13433. // 1D convolution
  13434. x = ggml_ssm_conv(ctx0, conv_x, model.layers[il].ssm_conv1d);
  13435. cb(x, "mamba_conv1d", il);
  13436. x = ggml_silu(ctx0, x);
  13437. cb(x, "mamba_conv1d_silu", il);
  13438. }
  13439. // SSM
  13440. {
  13441. // 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}
  13442. ggml_tensor * x_bcdt = build_lora_mm(model.layers[il].ssm_x, x);
  13443. cb(x_bcdt, "mamba_bcdt_proj", il);
  13444. // split into dt, B, C
  13445. const int64_t dt_dim = std::max(64, int(hparams.n_embd / 16));
  13446. 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);
  13447. 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);
  13448. 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));
  13449. cb(B, "mamba_B_raw", il);
  13450. cb(C, "mamba_C_raw", il);
  13451. cb(dt, "mamba_dt_raw", il);
  13452. // Apply RMS norm to dt, B, C (PLaMo-2 specific)
  13453. B = build_norm(B, model.layers[il].ssm_b_norm, NULL, LLM_NORM_RMS, il);
  13454. C = build_norm(C, model.layers[il].ssm_c_norm, NULL, LLM_NORM_RMS, il);
  13455. dt = build_norm(dt, model.layers[il].ssm_dt_norm, NULL, LLM_NORM_RMS, il);
  13456. cb(B, "mamba_B_normed", il);
  13457. cb(C, "mamba_C_normed", il);
  13458. cb(dt, "mamba_dt_normed", il);
  13459. // dt_proj: {dt_rank, d_inner} @ {dt_rank, n_seq_tokens, n_seqs} => {d_inner, n_seq_tokens, n_seqs}
  13460. dt = build_lora_mm(model.layers[il].ssm_dt, dt);
  13461. dt = ggml_add(ctx0, dt, model.layers[il].ssm_dt_b);
  13462. cb(dt, "mamba_dt_proj", il);
  13463. ggml_tensor * A = ggml_reshape_2d(ctx0, model.layers[il].ssm_a, 1, n_heads);
  13464. cb(A, "mamba_A", il);
  13465. 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);
  13466. 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);
  13467. 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);
  13468. // use the states and the indices provided by build_recurrent_state
  13469. // (this is necessary in order to properly use the states before they are overwritten,
  13470. // while avoiding to make unnecessary copies of the states)
  13471. auto get_ssm_rows = [&](ggml_context * ctx, ggml_tensor * states, ggml_tensor * ids) {
  13472. ggml_tensor * ssm = ggml_reshape_4d(ctx, states, d_state, head_dim, n_heads, mctx_cur->get_size());
  13473. // Custom operator to optimize the parallel associative scan
  13474. // as described in the Annex D of the Mamba paper.
  13475. // => {d_inner, n_seq_tokens, n_seqs} and {d_state, d_inner, n_seqs}
  13476. return ggml_ssm_scan(ctx, ssm, x, dt, A, B, C, ids);
  13477. };
  13478. ggml_tensor * y_ssm = build_rs(inp, ssm_states_all, hparams.n_embd_s(), ubatch.n_seqs, get_ssm_rows);
  13479. cb(y_ssm, "mamba_ssm_scan", il);
  13480. // store last states
  13481. ggml_build_forward_expand(gf,
  13482. ggml_cpy(ctx0,
  13483. 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)),
  13484. 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))));
  13485. cb(ssm_states_all, "mamba_ssm_states", il);
  13486. 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);
  13487. cb(y, "mamba_y_view", il);
  13488. // Add D parameter and apply gating with z
  13489. // {d_inner, n_seq_tokens, n_seqs} * {d_inner} => {d_inner, n_seq_tokens, n_seqs}
  13490. ggml_tensor * D = ggml_reshape_2d(ctx0, model.layers[il].ssm_d, 1, n_heads);
  13491. y = ggml_add(ctx0, y, ggml_mul(ctx0, x, D));
  13492. cb(y, "mamba_y_add_d", il);
  13493. y = ggml_swiglu_split(ctx0, ggml_cont(ctx0, z), y);
  13494. cb(y, "mamba_y_swiglu_z", il);
  13495. // out_proj: {d_inner, n_embd} @ {d_inner, n_seq_tokens, n_seqs} => {n_embd, n_seq_tokens, n_seqs}
  13496. y = ggml_view_3d(ctx0, y, head_dim * n_heads, n_seq_tokens, n_seqs, y->nb[2], y->nb[3], 0);
  13497. cur = build_lora_mm(model.layers[il].ssm_out, y);
  13498. cb(cur, "mamba_out_proj", il);
  13499. }
  13500. // {n_embd, n_seq_tokens, n_seqs} => {n_embd, n_tokens}
  13501. cur = ggml_reshape_2d(ctx0, cur, cur->ne[0], n_seq_tokens * n_seqs);
  13502. cb(cur, "mamba_out", il);
  13503. return cur;
  13504. }
  13505. };
  13506. struct llm_build_arcee : public llm_graph_context {
  13507. llm_build_arcee(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  13508. const int64_t n_embd_head = hparams.n_embd_head_v;
  13509. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  13510. GGML_ASSERT(n_embd_head == hparams.n_rot);
  13511. ggml_tensor * cur;
  13512. ggml_tensor * inpL;
  13513. inpL = build_inp_embd(model.tok_embd);
  13514. // inp_pos - contains the positions
  13515. ggml_tensor * inp_pos = build_inp_pos();
  13516. auto * inp_attn = build_attn_inp_kv_unified();
  13517. const float kq_scale = hparams.f_attention_scale == 0.0f ? 1.0f/sqrtf(float(n_embd_head)) : hparams.f_attention_scale;
  13518. ggml_tensor * inp_out_ids = build_inp_out_ids();
  13519. for (int il = 0; il < n_layer; ++il) {
  13520. ggml_tensor * inpSA = inpL;
  13521. // norm
  13522. cur = build_norm(inpL,
  13523. model.layers[il].attn_norm, NULL,
  13524. LLM_NORM_RMS, il);
  13525. cb(cur, "attn_norm", il);
  13526. // self-attention
  13527. {
  13528. // rope freq factors for llama3; may return nullptr for llama2 and other models
  13529. ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
  13530. // compute Q and K and RoPE them
  13531. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  13532. cb(Qcur, "Qcur", il);
  13533. if (model.layers[il].bq) {
  13534. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  13535. cb(Qcur, "Qcur", il);
  13536. }
  13537. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  13538. cb(Kcur, "Kcur", il);
  13539. if (model.layers[il].bk) {
  13540. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  13541. cb(Kcur, "Kcur", il);
  13542. }
  13543. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  13544. cb(Vcur, "Vcur", il);
  13545. if (model.layers[il].bv) {
  13546. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  13547. cb(Vcur, "Vcur", il);
  13548. }
  13549. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  13550. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  13551. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  13552. Qcur = ggml_rope_ext(
  13553. ctx0, Qcur, inp_pos, rope_factors,
  13554. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  13555. ext_factor, attn_factor, beta_fast, beta_slow
  13556. );
  13557. Kcur = ggml_rope_ext(
  13558. ctx0, Kcur, inp_pos, rope_factors,
  13559. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  13560. ext_factor, attn_factor, beta_fast, beta_slow
  13561. );
  13562. cb(Qcur, "Qcur", il);
  13563. cb(Kcur, "Kcur", il);
  13564. cb(Vcur, "Vcur", il);
  13565. cur = build_attn(inp_attn,
  13566. model.layers[il].wo, model.layers[il].bo,
  13567. Qcur, Kcur, Vcur, nullptr, nullptr, kq_scale, il);
  13568. cb(cur, "attn_out", il);
  13569. }
  13570. if (il == n_layer - 1 && inp_out_ids) {
  13571. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  13572. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  13573. }
  13574. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  13575. cb(ffn_inp, "ffn_inp", il);
  13576. // feed-forward network
  13577. // ARCEE uses relu^2 instead of silu
  13578. cur = build_norm(ffn_inp,
  13579. model.layers[il].ffn_norm, NULL,
  13580. LLM_NORM_RMS, il);
  13581. cb(cur, "ffn_norm", il);
  13582. cur = build_ffn(cur,
  13583. model.layers[il].ffn_up, NULL, NULL,
  13584. NULL, NULL, NULL,
  13585. model.layers[il].ffn_down, NULL, NULL,
  13586. NULL,
  13587. LLM_FFN_RELU_SQR, LLM_FFN_SEQ, il);
  13588. cb(cur, "ffn_out", il);
  13589. cur = ggml_add(ctx0, cur, ffn_inp);
  13590. cb(cur, "ffn_out", il);
  13591. cur = build_cvec(cur, il);
  13592. cb(cur, "l_out", il);
  13593. // input for next layer
  13594. inpL = cur;
  13595. }
  13596. cur = inpL;
  13597. cur = build_norm(cur,
  13598. model.output_norm, NULL,
  13599. LLM_NORM_RMS, -1);
  13600. cb(cur, "result_norm", -1);
  13601. res->t_embd = cur;
  13602. // lm_head
  13603. cur = build_lora_mm(model.output, cur);
  13604. cb(cur, "result_output", -1);
  13605. res->t_logits = cur;
  13606. ggml_build_forward_expand(gf, cur);
  13607. }
  13608. };
  13609. struct llm_build_hunyuan_moe : public llm_graph_context {
  13610. llm_build_hunyuan_moe(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  13611. const int64_t n_embd_head = hparams.n_embd_head_v;
  13612. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  13613. GGML_ASSERT(n_embd_head == hparams.n_rot);
  13614. ggml_tensor * cur;
  13615. ggml_tensor * inpL;
  13616. inpL = build_inp_embd(model.tok_embd);
  13617. // inp_pos - contains the positions
  13618. ggml_tensor * inp_pos = build_inp_pos();
  13619. auto * inp_attn = build_attn_inp_kv_unified();
  13620. const float kq_scale = 1.0f / sqrtf(float(n_embd_head));
  13621. ggml_tensor * inp_out_ids = build_inp_out_ids();
  13622. for (int il = 0; il < n_layer; ++il) {
  13623. ggml_tensor * inpSA = inpL;
  13624. // norm
  13625. cur = build_norm(inpL,
  13626. model.layers[il].attn_norm, NULL,
  13627. LLM_NORM_RMS, il);
  13628. cb(cur, "attn_norm", il);
  13629. // self-attention
  13630. {
  13631. // rope freq factors for llama3; may return nullptr for llama2 and other models
  13632. ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
  13633. // compute Q and K and RoPE them
  13634. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  13635. cb(Qcur, "Qcur", il);
  13636. if (model.layers[il].bq) {
  13637. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  13638. cb(Qcur, "Qcur", il);
  13639. }
  13640. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  13641. cb(Kcur, "Kcur", il);
  13642. if (model.layers[il].bk) {
  13643. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  13644. cb(Kcur, "Kcur", il);
  13645. }
  13646. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  13647. cb(Vcur, "Vcur", il);
  13648. if (model.layers[il].bv) {
  13649. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  13650. cb(Vcur, "Vcur", il);
  13651. }
  13652. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  13653. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  13654. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  13655. Qcur = ggml_rope_ext(
  13656. ctx0, Qcur, inp_pos, rope_factors,
  13657. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  13658. ext_factor, attn_factor, beta_fast, beta_slow
  13659. );
  13660. cb(Qcur, "Qcur", il);
  13661. cb(Kcur, "Kcur", il);
  13662. cb(Vcur, "Vcur", il);
  13663. Kcur = ggml_rope_ext(
  13664. ctx0, Kcur, inp_pos, rope_factors,
  13665. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  13666. ext_factor, attn_factor, beta_fast, beta_slow
  13667. );
  13668. Kcur = build_norm(Kcur,
  13669. model.layers[il].attn_k_norm, nullptr,
  13670. LLM_NORM_RMS, il);
  13671. cb(Kcur, "Kcur_norm", il);
  13672. Qcur = build_norm(Qcur,
  13673. model.layers[il].attn_q_norm, nullptr,
  13674. LLM_NORM_RMS, il);
  13675. cb(Qcur, "Qcur_norm", il);
  13676. cur = build_attn(inp_attn,
  13677. model.layers[il].wo, model.layers[il].bo,
  13678. Qcur, Kcur, Vcur, nullptr, nullptr, kq_scale, il);
  13679. cb(cur, "attn_out", il);
  13680. }
  13681. if (il == n_layer - 1 && inp_out_ids) {
  13682. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  13683. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  13684. }
  13685. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  13686. cb(ffn_inp, "ffn_inp", il);
  13687. cur = build_norm(ffn_inp,
  13688. model.layers[il].ffn_norm, NULL,
  13689. LLM_NORM_RMS, il);
  13690. cb(cur, "ffn_norm", il);
  13691. // feed-forward network (non-MoE)
  13692. ggml_tensor * cur_mlp = build_ffn(cur,
  13693. model.layers[il].ffn_up_shexp, NULL, NULL,
  13694. model.layers[il].ffn_gate_shexp, NULL, NULL,
  13695. model.layers[il].ffn_down_shexp, NULL, NULL,
  13696. NULL,
  13697. LLM_FFN_SILU, LLM_FFN_PAR, il);
  13698. cb(cur_mlp, "ffn_mlp", il);
  13699. // MoE branch
  13700. ggml_tensor * cur_moe = build_moe_ffn(cur,
  13701. model.layers[il].ffn_gate_inp,
  13702. model.layers[il].ffn_up_exps,
  13703. model.layers[il].ffn_gate_exps,
  13704. model.layers[il].ffn_down_exps,
  13705. nullptr,
  13706. n_expert, n_expert_used,
  13707. LLM_FFN_SILU,
  13708. true, // norm_topk_prob
  13709. false,
  13710. 0.0,
  13711. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  13712. il);
  13713. cb(cur_moe, "ffn_moe_out", il);
  13714. ggml_tensor * ffn_out = ggml_add(ctx0, cur_moe, cur_mlp);
  13715. cb(ffn_out, "ffn_out", il);
  13716. cur = ggml_add(ctx0, ffn_out, ffn_inp);
  13717. cur = build_cvec(cur, il);
  13718. cb(cur, "l_out", il);
  13719. // input for next layer
  13720. inpL = cur;
  13721. }
  13722. cur = inpL;
  13723. cur = build_norm(cur,
  13724. model.output_norm, NULL,
  13725. LLM_NORM_RMS, -1);
  13726. cb(cur, "result_norm", -1);
  13727. res->t_embd = cur;
  13728. // lm_head
  13729. cur = build_lora_mm(model.output, cur);
  13730. cb(cur, "result_output", -1);
  13731. res->t_logits = cur;
  13732. ggml_build_forward_expand(gf, cur);
  13733. }
  13734. };
  13735. struct llm_build_hunyuan_dense : public llm_graph_context {
  13736. llm_build_hunyuan_dense(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  13737. const int64_t n_embd_head = hparams.n_embd_head_v;
  13738. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  13739. GGML_ASSERT(n_embd_head == hparams.n_rot);
  13740. ggml_tensor * cur;
  13741. ggml_tensor * inpL;
  13742. inpL = build_inp_embd(model.tok_embd);
  13743. // inp_pos - contains the positions
  13744. ggml_tensor * inp_pos = build_inp_pos();
  13745. auto * inp_attn = build_attn_inp_kv_unified();
  13746. const float kq_scale = 1.0f / sqrtf(float(n_embd_head));
  13747. ggml_tensor * inp_out_ids = build_inp_out_ids();
  13748. for (int il = 0; il < n_layer; ++il) {
  13749. ggml_tensor * inpSA = inpL;
  13750. // norm
  13751. cur = build_norm(inpL,
  13752. model.layers[il].attn_norm, NULL,
  13753. LLM_NORM_RMS, il);
  13754. cb(cur, "attn_norm", il);
  13755. // self-attention
  13756. {
  13757. // rope freq factors for llama3; may return nullptr for llama2 and other models
  13758. ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
  13759. // compute Q and K and RoPE them
  13760. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  13761. cb(Qcur, "Qcur", il);
  13762. if (model.layers[il].bq) {
  13763. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  13764. cb(Qcur, "Qcur", il);
  13765. }
  13766. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  13767. cb(Kcur, "Kcur", il);
  13768. if (model.layers[il].bk) {
  13769. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  13770. cb(Kcur, "Kcur", il);
  13771. }
  13772. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  13773. cb(Vcur, "Vcur", il);
  13774. if (model.layers[il].bv) {
  13775. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  13776. cb(Vcur, "Vcur", il);
  13777. }
  13778. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  13779. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  13780. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  13781. Qcur = ggml_rope_ext(
  13782. ctx0, Qcur, inp_pos, rope_factors,
  13783. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  13784. ext_factor, attn_factor, beta_fast, beta_slow
  13785. );
  13786. cb(Qcur, "Qcur", il);
  13787. cb(Kcur, "Kcur", il);
  13788. cb(Vcur, "Vcur", il);
  13789. Kcur = ggml_rope_ext(
  13790. ctx0, Kcur, inp_pos, rope_factors,
  13791. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  13792. ext_factor, attn_factor, beta_fast, beta_slow
  13793. );
  13794. Kcur = build_norm(Kcur,
  13795. model.layers[il].attn_k_norm, nullptr,
  13796. LLM_NORM_RMS, il);
  13797. cb(Kcur, "Kcur_norm", il);
  13798. Qcur = build_norm(Qcur,
  13799. model.layers[il].attn_q_norm, nullptr,
  13800. LLM_NORM_RMS, il);
  13801. cb(Qcur, "Qcur_norm", il);
  13802. cur = build_attn(inp_attn,
  13803. model.layers[il].wo, model.layers[il].bo,
  13804. Qcur, Kcur, Vcur, nullptr, nullptr, kq_scale, il);
  13805. cb(cur, "attn_out", il);
  13806. }
  13807. if (il == n_layer - 1 && inp_out_ids) {
  13808. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  13809. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  13810. }
  13811. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  13812. cb(ffn_inp, "ffn_inp", il);
  13813. cur = build_norm(ffn_inp,
  13814. model.layers[il].ffn_norm, NULL,
  13815. LLM_NORM_RMS, il);
  13816. cb(cur, "ffn_norm", il);
  13817. // feed-forward network (non-MoE)
  13818. ggml_tensor * cur_mlp = build_ffn(cur,
  13819. model.layers[il].ffn_up, NULL, NULL,
  13820. model.layers[il].ffn_gate, NULL, NULL,
  13821. model.layers[il].ffn_down, NULL, NULL,
  13822. NULL,
  13823. LLM_FFN_SILU, LLM_FFN_PAR, il);
  13824. cb(cur_mlp, "ffn_out", il);
  13825. cur = ggml_add(ctx0, cur_mlp, ffn_inp);
  13826. cur = build_cvec(cur, il);
  13827. cb(cur, "l_out", il);
  13828. // input for next layer
  13829. inpL = cur;
  13830. }
  13831. cur = inpL;
  13832. cur = build_norm(cur,
  13833. model.output_norm, NULL,
  13834. LLM_NORM_RMS, -1);
  13835. cb(cur, "result_norm", -1);
  13836. res->t_embd = cur;
  13837. // lm_head
  13838. cur = build_lora_mm(model.output, cur);
  13839. cb(cur, "result_output", -1);
  13840. res->t_logits = cur;
  13841. ggml_build_forward_expand(gf, cur);
  13842. }
  13843. };
  13844. struct llm_build_smollm3 : public llm_graph_context {
  13845. llm_build_smollm3(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  13846. const int64_t n_embd_head = hparams.n_embd_head_v;
  13847. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  13848. GGML_ASSERT(n_embd_head == hparams.n_rot);
  13849. ggml_tensor * cur;
  13850. ggml_tensor * inpL;
  13851. inpL = build_inp_embd(model.tok_embd);
  13852. // inp_pos - contains the positions
  13853. ggml_tensor * inp_pos = build_inp_pos();
  13854. auto * inp_attn = build_attn_inp_kv_unified();
  13855. const float kq_scale = hparams.f_attention_scale == 0.0f ? 1.0f/sqrtf(float(n_embd_head)) : hparams.f_attention_scale;
  13856. ggml_tensor * inp_out_ids = build_inp_out_ids();
  13857. for (int il = 0; il < n_layer; ++il) {
  13858. ggml_tensor * inpSA = inpL;
  13859. const bool use_rope = (il + 1) % hparams.n_no_rope_layer_step != 0;
  13860. // norm
  13861. cur = build_norm(inpL,
  13862. model.layers[il].attn_norm, NULL,
  13863. LLM_NORM_RMS, il);
  13864. cb(cur, "attn_norm", il);
  13865. // self-attention
  13866. {
  13867. // compute Q and K and RoPE them
  13868. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  13869. cb(Qcur, "Qcur", il);
  13870. if (model.layers[il].bq) {
  13871. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  13872. cb(Qcur, "Qcur", il);
  13873. }
  13874. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  13875. cb(Kcur, "Kcur", il);
  13876. if (model.layers[il].bk) {
  13877. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  13878. cb(Kcur, "Kcur", il);
  13879. }
  13880. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  13881. cb(Vcur, "Vcur", il);
  13882. if (model.layers[il].bv) {
  13883. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  13884. cb(Vcur, "Vcur", il);
  13885. }
  13886. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  13887. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  13888. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  13889. if (use_rope) {
  13890. Qcur = ggml_rope_ext(
  13891. ctx0, Qcur, inp_pos, nullptr,
  13892. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  13893. ext_factor, attn_factor, beta_fast, beta_slow
  13894. );
  13895. Kcur = ggml_rope_ext(
  13896. ctx0, Kcur, inp_pos, nullptr,
  13897. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  13898. ext_factor, attn_factor, beta_fast, beta_slow
  13899. );
  13900. }
  13901. cb(Qcur, "Qcur", il);
  13902. cb(Kcur, "Kcur", il);
  13903. cb(Vcur, "Vcur", il);
  13904. cur = build_attn(inp_attn,
  13905. model.layers[il].wo, model.layers[il].bo,
  13906. Qcur, Kcur, Vcur, nullptr, nullptr, kq_scale, il);
  13907. cb(cur, "attn_out", il);
  13908. }
  13909. if (il == n_layer - 1 && inp_out_ids) {
  13910. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  13911. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  13912. }
  13913. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  13914. cb(ffn_inp, "ffn_inp", il);
  13915. // feed-forward network
  13916. {
  13917. cur = build_norm(ffn_inp,
  13918. model.layers[il].ffn_norm, NULL,
  13919. LLM_NORM_RMS, il);
  13920. cb(cur, "ffn_norm", il);
  13921. cur = build_ffn(cur,
  13922. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  13923. model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, NULL,
  13924. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  13925. NULL,
  13926. LLM_FFN_SILU, LLM_FFN_PAR, il);
  13927. cb(cur, "ffn_out", il);
  13928. }
  13929. cur = ggml_add(ctx0, cur, ffn_inp);
  13930. cb(cur, "ffn_out", il);
  13931. cur = build_cvec(cur, il);
  13932. cb(cur, "l_out", il);
  13933. // input for next layer
  13934. inpL = cur;
  13935. }
  13936. cur = inpL;
  13937. cur = build_norm(cur,
  13938. model.output_norm, NULL,
  13939. LLM_NORM_RMS, -1);
  13940. cb(cur, "result_norm", -1);
  13941. res->t_embd = cur;
  13942. // lm_head
  13943. cur = build_lora_mm(model.output, cur);
  13944. cb(cur, "result_output", -1);
  13945. res->t_logits = cur;
  13946. ggml_build_forward_expand(gf, cur);
  13947. }
  13948. };
  13949. struct llm_build_openai_moe_iswa : public llm_graph_context {
  13950. llm_build_openai_moe_iswa(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
  13951. ggml_tensor * cur;
  13952. ggml_tensor * inpL;
  13953. inpL = build_inp_embd(model.tok_embd);
  13954. // inp_pos - contains the positions
  13955. ggml_tensor * inp_pos = build_inp_pos();
  13956. auto * inp_attn = build_attn_inp_kv_unified_iswa();
  13957. for (int il = 0; il < n_layer; ++il) {
  13958. ggml_tensor * inpSA = inpL;
  13959. // norm
  13960. cur = build_norm(inpL,
  13961. model.layers[il].attn_norm, nullptr,
  13962. LLM_NORM_RMS, il);
  13963. cb(cur, "attn_norm", il);
  13964. // self-attention
  13965. {
  13966. // compute Q and K and RoPE them
  13967. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  13968. cb(Qcur, "Qcur", il);
  13969. if (model.layers[il].bq) {
  13970. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  13971. cb(Qcur, "Qcur", il);
  13972. }
  13973. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  13974. cb(Kcur, "Kcur", il);
  13975. if (model.layers[il].bk) {
  13976. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  13977. cb(Kcur, "Kcur", il);
  13978. }
  13979. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  13980. cb(Vcur, "Vcur", il);
  13981. if (model.layers[il].bv) {
  13982. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  13983. cb(Vcur, "Vcur", il);
  13984. }
  13985. Qcur = ggml_reshape_3d(ctx0, Qcur, n_rot, n_head, n_tokens);
  13986. Kcur = ggml_reshape_3d(ctx0, Kcur, n_rot, n_head_kv, n_tokens);
  13987. Vcur = ggml_reshape_3d(ctx0, Vcur, n_rot, n_head_kv, n_tokens);
  13988. Qcur = ggml_rope_ext(
  13989. ctx0, Qcur, inp_pos, nullptr,
  13990. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  13991. ext_factor, attn_factor, beta_fast, beta_slow
  13992. );
  13993. Kcur = ggml_rope_ext(
  13994. ctx0, Kcur, inp_pos, nullptr,
  13995. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  13996. ext_factor, attn_factor, beta_fast, beta_slow
  13997. );
  13998. cb(Qcur, "Qcur", il);
  13999. cb(Kcur, "Kcur", il);
  14000. cb(Vcur, "Vcur", il);
  14001. cur = build_attn_with_sinks(inp_attn,
  14002. model.layers[il].wo, model.layers[il].bo,
  14003. Qcur, Kcur, Vcur, nullptr, nullptr, model.layers[il].attn_sinks, 1.0f/sqrtf(float(n_rot)), il);
  14004. cb(cur, "attn_out", il);
  14005. }
  14006. if (il == n_layer - 1) {
  14007. // skip computing output for unused tokens
  14008. ggml_tensor * inp_out_ids = build_inp_out_ids();
  14009. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  14010. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  14011. }
  14012. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  14013. cb(ffn_inp, "ffn_inp", il);
  14014. cur = ffn_inp;
  14015. cur = build_norm(cur,
  14016. model.layers[il].attn_post_norm, nullptr,
  14017. LLM_NORM_RMS, il);
  14018. cb(cur, "attn_post_norm", il);
  14019. // MoE branch
  14020. cur = build_moe_ffn(cur,
  14021. model.layers[il].ffn_gate_inp, model.layers[il].ffn_gate_inp_b,
  14022. model.layers[il].ffn_up_exps, model.layers[il].ffn_up_exps_b,
  14023. model.layers[il].ffn_gate_exps, model.layers[il].ffn_gate_exps_b,
  14024. model.layers[il].ffn_down_exps, model.layers[il].ffn_down_exps_b,
  14025. nullptr,
  14026. n_expert, n_expert_used,
  14027. LLM_FFN_SWIGLU_OAI_MOE, false,
  14028. false, 0.0,
  14029. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX_WEIGHT,
  14030. il);
  14031. cb(cur, "ffn_moe_out", il);
  14032. cur = ggml_add(ctx0, cur, ffn_inp);
  14033. cur = build_cvec(cur, il);
  14034. cb(cur, "l_out", il);
  14035. // input for next layer
  14036. inpL = cur;
  14037. }
  14038. cur = inpL;
  14039. cur = build_norm(cur,
  14040. model.output_norm, NULL,
  14041. LLM_NORM_RMS, -1);
  14042. cb(cur, "result_norm", -1);
  14043. res->t_embd = cur;
  14044. // lm_head
  14045. cur = build_lora_mm(model.output, cur);
  14046. cb(cur, "result_output", -1);
  14047. res->t_logits = cur;
  14048. ggml_build_forward_expand(gf, cur);
  14049. }
  14050. };
  14051. struct llm_build_lfm2 : public llm_graph_context {
  14052. const llama_model & model;
  14053. llm_build_lfm2(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params), model(model) {
  14054. ggml_tensor * cur = build_inp_embd(model.tok_embd);
  14055. cb(cur, "model.embed_tokens", -1);
  14056. ggml_tensor * inp_pos = build_inp_pos();
  14057. auto * inp_hybrid = build_inp_mem_hybrid();
  14058. ggml_tensor * inp_out_ids = build_inp_out_ids();
  14059. for (int il = 0; il < n_layer; ++il) {
  14060. auto * prev_cur = cur;
  14061. cur = build_norm(cur, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
  14062. cb(cur, "model.layers.{}.operator_norm", il);
  14063. cur = hparams.is_recurrent(il) ?
  14064. build_shortconv_block(cur, inp_hybrid->get_recr(), il) :
  14065. build_attn_block(cur, inp_pos, inp_hybrid->get_attn(), il) ;
  14066. if (il == n_layer - 1 && inp_out_ids) {
  14067. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  14068. prev_cur = ggml_get_rows(ctx0, prev_cur, inp_out_ids);
  14069. }
  14070. cur = ggml_add(ctx0, prev_cur, cur);
  14071. cur = ggml_add(ctx0, cur, build_feed_forward(cur, il));
  14072. }
  14073. cur = build_norm(cur, model.tok_norm, NULL, LLM_NORM_RMS, -1);
  14074. cb(cur, "model.embedding_norm", -1);
  14075. res->t_embd = cur;
  14076. // lm_head is tied with embeddings
  14077. cur = build_lora_mm(model.tok_embd, cur);
  14078. cb(cur, "lm_head", -1);
  14079. res->t_logits = cur;
  14080. ggml_build_forward_expand(gf, cur);
  14081. }
  14082. ggml_tensor * build_feed_forward(ggml_tensor * cur,
  14083. int il) const {
  14084. cur = build_norm(cur, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);
  14085. cb(cur, "model.layers.{}.ffn_norm", il);
  14086. GGML_ASSERT(!model.layers[il].ffn_up_b);
  14087. GGML_ASSERT(!model.layers[il].ffn_gate_b);
  14088. GGML_ASSERT(!model.layers[il].ffn_down_b);
  14089. cur = build_ffn(cur,
  14090. model.layers[il].ffn_up, NULL, NULL,
  14091. model.layers[il].ffn_gate, NULL, NULL,
  14092. model.layers[il].ffn_down, NULL, NULL,
  14093. NULL,
  14094. LLM_FFN_SILU, LLM_FFN_PAR, il);
  14095. cb(cur, "model.layers.{}.feed_forward.w2", il);
  14096. return cur;
  14097. }
  14098. ggml_tensor * build_attn_block(ggml_tensor * cur,
  14099. ggml_tensor * inp_pos,
  14100. llm_graph_input_attn_kv_unified * inp_attn,
  14101. int il) const {
  14102. GGML_ASSERT(hparams.n_embd_v_gqa(il) == hparams.n_embd_k_gqa(il));
  14103. auto const n_embd_head = hparams.n_embd_head_v;
  14104. auto const n_head_kv = hparams.n_head_kv(il);
  14105. auto * q = build_lora_mm(model.layers[il].wq, cur);
  14106. cb(q, "model.layers.{}.self_attn.q_proj", il);
  14107. auto * k = build_lora_mm(model.layers[il].wk, cur);
  14108. cb(k, "model.layers.{}.self_attn.k_proj", il);
  14109. auto * v = build_lora_mm(model.layers[il].wv, cur);
  14110. cb(v, "model.layers.{}.self_attn.v_proj", il);
  14111. q = ggml_reshape_3d(ctx0, q, n_embd_head, n_head, n_tokens);
  14112. k = ggml_reshape_3d(ctx0, k, n_embd_head, n_head_kv, n_tokens);
  14113. v = ggml_reshape_3d(ctx0, v, n_embd_head, n_head_kv, n_tokens);
  14114. // qk norm
  14115. q = build_norm(q, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);
  14116. cb(q, "model.layers.{}.self_attn.q_layernorm", il);
  14117. k = build_norm(k, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);
  14118. cb(k, "model.layers.{}.self_attn.k_layernorm", il);
  14119. // RoPE
  14120. q = ggml_rope_ext(
  14121. ctx0, q, inp_pos, nullptr,
  14122. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  14123. ext_factor, attn_factor, beta_fast, beta_slow
  14124. );
  14125. k = ggml_rope_ext(
  14126. ctx0, k, inp_pos, nullptr,
  14127. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  14128. ext_factor, attn_factor, beta_fast, beta_slow
  14129. );
  14130. cur = build_attn(inp_attn, model.layers[il].wo, NULL,
  14131. q, k, v, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  14132. cb(cur, "model.layers.{}.self_attn.out_proj", il);
  14133. return cur;
  14134. }
  14135. ggml_tensor * build_shortconv_block(ggml_tensor * cur,
  14136. llm_graph_input_rs * inp_recr,
  14137. int il) {
  14138. const auto * mctx_cur = static_cast<const llama_memory_hybrid_context *>(mctx)->get_recr();
  14139. const uint32_t kv_head = mctx_cur->get_head();
  14140. const int64_t n_seq_tokens = ubatch.n_seq_tokens;
  14141. const int64_t n_seqs = ubatch.n_seqs;
  14142. GGML_ASSERT(n_seqs != 0);
  14143. GGML_ASSERT(ubatch.equal_seqs());
  14144. GGML_ASSERT(ubatch.n_tokens == n_seq_tokens * n_seqs);
  14145. GGML_ASSERT(hparams.n_shortconv_l_cache > 1);
  14146. const uint32_t d_conv = hparams.n_shortconv_l_cache - 1;
  14147. // {n_embd, n_tokens} => {n_embd, n_seq_tokens, n_seqs}
  14148. cur = ggml_reshape_3d(ctx0, cur, cur->ne[0], n_seq_tokens, n_seqs);
  14149. auto * bcx = build_lora_mm(model.layers[il].shortconv.in_proj, cur);
  14150. cb(bcx, "model.layers.{}.conv.in_proj", il);
  14151. constexpr auto n_chunks = 3;
  14152. GGML_ASSERT(bcx->ne[0] % n_chunks == 0);
  14153. auto const chunk_size = bcx->ne[0] / n_chunks;
  14154. 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));
  14155. 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));
  14156. 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));
  14157. auto * bx = ggml_transpose(ctx0, ggml_mul(ctx0, b, x));
  14158. // read conv state
  14159. auto * conv_state = mctx_cur->get_r_l(il);
  14160. auto * conv_rs = build_rs(inp_recr, conv_state, hparams.n_embd_r(), n_seqs);
  14161. auto * conv = ggml_reshape_3d(ctx0, conv_rs, d_conv, hparams.n_embd, n_seqs);
  14162. bx = ggml_concat(ctx0, conv, bx, 0);
  14163. GGML_ASSERT(bx->ne[0] > conv->ne[0]);
  14164. // last d_conv columns is a new conv state
  14165. 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));
  14166. GGML_ASSERT(ggml_are_same_shape(conv, new_conv));
  14167. // write new conv conv state
  14168. ggml_build_forward_expand(
  14169. gf,
  14170. ggml_cpy(
  14171. ctx0,
  14172. new_conv,
  14173. ggml_view_1d(
  14174. ctx0,
  14175. conv_state,
  14176. ggml_nelements(new_conv),
  14177. kv_head*d_conv*n_embd*ggml_element_size(new_conv)
  14178. )
  14179. )
  14180. );
  14181. auto * conv_kernel = model.layers[il].shortconv.conv;
  14182. auto * conv_out = ggml_ssm_conv(ctx0, bx, conv_kernel);
  14183. cb(conv_out, "model.layers.{}.conv.conv", il);
  14184. auto * y = ggml_mul(ctx0, c, conv_out);
  14185. y = build_lora_mm(model.layers[il].shortconv.out_proj, y);
  14186. cb(y, "model.layers.{}.conv.out_proj", il);
  14187. // {n_embd, n_seq_tokens, n_seqs} => {n_embd, n_tokens}
  14188. y = ggml_reshape_2d(ctx0, y, y->ne[0], n_seq_tokens * n_seqs);
  14189. return y;
  14190. }
  14191. };
  14192. template <bool iswa>
  14193. struct llm_build_smallthinker : public llm_graph_context{
  14194. llm_build_smallthinker(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params){
  14195. const int64_t n_embd_head = hparams.n_embd_head_v;
  14196. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  14197. GGML_ASSERT(n_embd_head == hparams.n_rot);
  14198. ggml_tensor * cur;
  14199. ggml_tensor * inpL;
  14200. inpL = build_inp_embd(model.tok_embd);
  14201. // inp_pos - contains the positions
  14202. ggml_tensor * inp_pos = build_inp_pos();
  14203. using inp_attn_type = std::conditional_t<iswa, llm_graph_input_attn_kv_unified_iswa, llm_graph_input_attn_kv_unified>;
  14204. inp_attn_type * inp_attn = nullptr;
  14205. if constexpr (iswa) {
  14206. inp_attn = build_attn_inp_kv_unified_iswa();
  14207. } else {
  14208. inp_attn = build_attn_inp_kv_unified();
  14209. }
  14210. ggml_tensor * inp_out_ids = build_inp_out_ids();
  14211. for (int il = 0; il < n_layer; ++il) {
  14212. ggml_tensor * inpSA = inpL;
  14213. ggml_tensor * probs = nullptr;
  14214. probs = build_lora_mm(model.layers[il].ffn_gate_inp, inpL); // [n_expert, n_tokens]
  14215. cb(probs, "ffn_moe_logits", il);
  14216. // norm
  14217. cur = build_norm(inpL,model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
  14218. cb(cur, "attn_norm", il);
  14219. // self_attention
  14220. {
  14221. // compute Q and K and RoPE them
  14222. struct ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  14223. cb(Qcur, "Qcur", il);
  14224. struct ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  14225. cb(Kcur, "Kcur", il);
  14226. struct ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  14227. cb(Vcur, "Vcur", il);
  14228. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  14229. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  14230. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  14231. if (hparams.n_no_rope_layer_step == n_layer || il % hparams.n_no_rope_layer_step != 0) {
  14232. Qcur = ggml_rope_ext(ctx0, Qcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  14233. ext_factor, attn_factor, beta_fast, beta_slow);
  14234. Kcur = ggml_rope_ext(ctx0, Kcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  14235. ext_factor, attn_factor, beta_fast, beta_slow);
  14236. }
  14237. cb(Qcur, "Qcur", il);
  14238. cb(Kcur, "Kcur", il);
  14239. cur = build_attn(inp_attn,
  14240. model.layers[il].wo, model.layers[il].bo,
  14241. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f / sqrtf(float(n_embd_head)), il);
  14242. }
  14243. if (il == n_layer - 1 && inp_out_ids) {
  14244. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  14245. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  14246. probs = ggml_get_rows(ctx0, probs, inp_out_ids);
  14247. }
  14248. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  14249. cb(ffn_inp, "ffn_inp", il);
  14250. // MoE branch
  14251. cur = build_norm(ffn_inp, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);
  14252. cb(cur, "ffn_norm", il);
  14253. ggml_tensor * ffn_out =
  14254. build_moe_ffn(cur,
  14255. nullptr,
  14256. model.layers[il].ffn_up_exps,
  14257. model.layers[il].ffn_gate_exps,
  14258. model.layers[il].ffn_down_exps,
  14259. nullptr,
  14260. n_expert, n_expert_used,
  14261. LLM_FFN_RELU, true,
  14262. false, 0.0,
  14263. static_cast<llama_expert_gating_func_type>(hparams.expert_gating_func),
  14264. il, probs);
  14265. cb(ffn_out, "ffn_out", il);
  14266. cur = ffn_out;
  14267. cur = ggml_add(ctx0, cur, ffn_inp);
  14268. cur = build_cvec(cur, il);
  14269. cb(cur, "l_out", il);
  14270. // input for next layer
  14271. inpL = cur;
  14272. }
  14273. cur = inpL;
  14274. cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1);
  14275. cb(cur, "result_norm", -1);
  14276. // lm_head
  14277. cur = build_lora_mm(model.output, cur);
  14278. cb(cur, "result_output", -1);
  14279. res->t_logits = cur;
  14280. ggml_build_forward_expand(gf, cur);
  14281. }
  14282. };
  14283. llama_memory_i * llama_model::create_memory(const llama_memory_params & params, llama_cparams & cparams) const {
  14284. llama_memory_i * res;
  14285. switch (arch) {
  14286. // Models that need specific instantiation should be handled in the
  14287. // switch statement
  14288. case LLM_ARCH_BERT:
  14289. case LLM_ARCH_JINA_BERT_V2:
  14290. case LLM_ARCH_NOMIC_BERT:
  14291. case LLM_ARCH_NOMIC_BERT_MOE:
  14292. case LLM_ARCH_NEO_BERT:
  14293. case LLM_ARCH_WAVTOKENIZER_DEC:
  14294. case LLM_ARCH_DREAM:
  14295. case LLM_ARCH_LLADA:
  14296. {
  14297. res = nullptr;
  14298. } break;
  14299. // Models that need standard caching should rely on recurrent/hybrid
  14300. // checks
  14301. default:
  14302. {
  14303. if (llm_arch_is_recurrent(arch)) {
  14304. res = new llama_memory_recurrent(
  14305. *this,
  14306. nullptr,
  14307. GGML_TYPE_F32,
  14308. GGML_TYPE_F32,
  14309. cparams.offload_kqv,
  14310. std::max((uint32_t) 1, cparams.n_seq_max),
  14311. cparams.n_seq_max);
  14312. } else if (llm_arch_is_hybrid(arch)) {
  14313. const auto padding = llama_kv_cache_unified::get_padding(cparams);
  14314. cparams.n_ctx = GGML_PAD(cparams.n_ctx, padding);
  14315. res = new llama_memory_hybrid(
  14316. /* model */ *this,
  14317. /* attn_type_k */ params.type_k,
  14318. /* attn_type_v */ params.type_v,
  14319. /* attn_v_trans */ !cparams.flash_attn,
  14320. /* attn_kv_size */ cparams.n_ctx,
  14321. /* attn_n_pad */ padding,
  14322. /* attn_n_swa */ hparams.n_swa,
  14323. /* attn_swa_type */ hparams.swa_type,
  14324. /* recurrent_type_k */ GGML_TYPE_F32,
  14325. /* recurrent_type_v */ GGML_TYPE_F32,
  14326. /* recurrent_kv_size */ std::max((uint32_t) 1, cparams.n_seq_max),
  14327. /* n_seq_max */ cparams.n_seq_max,
  14328. /* offload */ cparams.offload_kqv,
  14329. /* unified */ cparams.kv_unified,
  14330. /* filter_attn */ (arch == LLM_ARCH_FALCON_H1) ? [&](int32_t) { return true; } : (llama_memory_hybrid::layer_filter_cb)nullptr,
  14331. /* filter_recr */ (arch == LLM_ARCH_FALCON_H1) ? [&](int32_t) { return true; } : (llama_memory_hybrid::layer_filter_cb)nullptr);
  14332. } else {
  14333. const auto padding = llama_kv_cache_unified::get_padding(cparams);
  14334. uint32_t n_ctx_per_stream = cparams.n_ctx;
  14335. if (!cparams.kv_unified) {
  14336. n_ctx_per_stream = (cparams.n_ctx + cparams.n_seq_max - 1)/cparams.n_seq_max;
  14337. n_ctx_per_stream = GGML_PAD(n_ctx_per_stream, padding);
  14338. cparams.n_ctx = n_ctx_per_stream*cparams.n_seq_max;
  14339. } else {
  14340. n_ctx_per_stream = GGML_PAD(n_ctx_per_stream, padding);
  14341. cparams.n_ctx = n_ctx_per_stream;
  14342. }
  14343. LLAMA_LOG_DEBUG("%s: n_ctx = %u (padded)\n", __func__, cparams.n_ctx);
  14344. if (hparams.swa_type != LLAMA_SWA_TYPE_NONE) {
  14345. GGML_ASSERT(hparams.is_swa_any());
  14346. res = new llama_kv_cache_unified_iswa(
  14347. *this,
  14348. params.type_k,
  14349. params.type_v,
  14350. !cparams.flash_attn,
  14351. cparams.offload_kqv,
  14352. params.swa_full,
  14353. cparams.kv_unified,
  14354. n_ctx_per_stream,
  14355. cparams.n_seq_max,
  14356. cparams.n_ubatch,
  14357. padding);
  14358. } else {
  14359. GGML_ASSERT(!hparams.is_swa_any());
  14360. res = new llama_kv_cache_unified(
  14361. *this,
  14362. nullptr,
  14363. params.type_k,
  14364. params.type_v,
  14365. !cparams.flash_attn,
  14366. cparams.offload_kqv,
  14367. cparams.kv_unified,
  14368. n_ctx_per_stream,
  14369. cparams.n_seq_max,
  14370. padding,
  14371. hparams.n_swa,
  14372. hparams.swa_type);
  14373. }
  14374. }
  14375. }
  14376. }
  14377. return res;
  14378. }
  14379. ggml_cgraph * llama_model::build_graph(const llm_graph_params & params) const {
  14380. std::unique_ptr<llm_graph_context> llm;
  14381. switch (arch) {
  14382. case LLM_ARCH_LLAMA:
  14383. {
  14384. llm = std::make_unique<llm_build_llama>(*this, params);
  14385. } break;
  14386. case LLM_ARCH_LLAMA4:
  14387. {
  14388. llm = std::make_unique<llm_build_llama_iswa>(*this, params);
  14389. } break;
  14390. case LLM_ARCH_DECI:
  14391. {
  14392. llm = std::make_unique<llm_build_deci>(*this, params);
  14393. } break;
  14394. case LLM_ARCH_BAICHUAN:
  14395. {
  14396. llm = std::make_unique<llm_build_baichuan>(*this, params);
  14397. } break;
  14398. case LLM_ARCH_FALCON:
  14399. {
  14400. llm = std::make_unique<llm_build_falcon>(*this, params);
  14401. } break;
  14402. case LLM_ARCH_GROK:
  14403. {
  14404. llm = std::make_unique<llm_build_grok>(*this, params);
  14405. } break;
  14406. case LLM_ARCH_STARCODER:
  14407. {
  14408. llm = std::make_unique<llm_build_starcoder>(*this, params);
  14409. } break;
  14410. case LLM_ARCH_REFACT:
  14411. {
  14412. llm = std::make_unique<llm_build_refact>(*this, params);
  14413. } break;
  14414. case LLM_ARCH_BERT:
  14415. case LLM_ARCH_JINA_BERT_V2:
  14416. case LLM_ARCH_NOMIC_BERT:
  14417. case LLM_ARCH_NOMIC_BERT_MOE:
  14418. {
  14419. llm = std::make_unique<llm_build_bert>(*this, params);
  14420. } break;
  14421. case LLM_ARCH_NEO_BERT:
  14422. {
  14423. llm = std::make_unique<llm_build_neo_bert>(*this, params);
  14424. } break;
  14425. case LLM_ARCH_BLOOM:
  14426. {
  14427. llm = std::make_unique<llm_build_bloom>(*this, params);
  14428. } break;
  14429. case LLM_ARCH_MPT:
  14430. {
  14431. llm = std::make_unique<llm_build_mpt>(*this, params);
  14432. } break;
  14433. case LLM_ARCH_STABLELM:
  14434. {
  14435. llm = std::make_unique<llm_build_stablelm>(*this, params);
  14436. } break;
  14437. case LLM_ARCH_QWEN:
  14438. {
  14439. llm = std::make_unique<llm_build_qwen>(*this, params);
  14440. } break;
  14441. case LLM_ARCH_QWEN2:
  14442. {
  14443. llm = std::make_unique<llm_build_qwen2>(*this, params);
  14444. } break;
  14445. case LLM_ARCH_DREAM:
  14446. {
  14447. llm = std::make_unique<llm_build_dream>(*this, params);
  14448. }
  14449. break;
  14450. case LLM_ARCH_LLADA:
  14451. {
  14452. llm = std::make_unique<llm_build_llada>(*this, params);
  14453. }
  14454. break;
  14455. case LLM_ARCH_QWEN2VL:
  14456. {
  14457. llm = std::make_unique<llm_build_qwen2vl>(*this, params);
  14458. } break;
  14459. case LLM_ARCH_QWEN2MOE:
  14460. {
  14461. llm = std::make_unique<llm_build_qwen2moe>(*this, params);
  14462. } break;
  14463. case LLM_ARCH_QWEN3:
  14464. {
  14465. llm = std::make_unique<llm_build_qwen3>(*this, params);
  14466. } break;
  14467. case LLM_ARCH_QWEN3MOE:
  14468. {
  14469. llm = std::make_unique<llm_build_qwen3moe>(*this, params);
  14470. } break;
  14471. case LLM_ARCH_PHI2:
  14472. {
  14473. llm = std::make_unique<llm_build_phi2>(*this, params);
  14474. } break;
  14475. case LLM_ARCH_PHI3:
  14476. case LLM_ARCH_PHIMOE:
  14477. {
  14478. if (hparams.swa_type != LLAMA_SWA_TYPE_NONE) {
  14479. llm = std::make_unique<llm_build_phi3<true>> (*this, params);
  14480. } else {
  14481. llm = std::make_unique<llm_build_phi3<false>>(*this, params);
  14482. }
  14483. } break;
  14484. case LLM_ARCH_PLAMO:
  14485. {
  14486. llm = std::make_unique<llm_build_plamo>(*this, params);
  14487. } break;
  14488. case LLM_ARCH_PLAMO2:
  14489. {
  14490. llm = std::make_unique<llm_build_plamo2>(*this, params);
  14491. } break;
  14492. case LLM_ARCH_GPT2:
  14493. {
  14494. llm = std::make_unique<llm_build_gpt2>(*this, params);
  14495. } break;
  14496. case LLM_ARCH_CODESHELL:
  14497. {
  14498. llm = std::make_unique<llm_build_codeshell>(*this, params);
  14499. } break;
  14500. case LLM_ARCH_ORION:
  14501. {
  14502. llm = std::make_unique<llm_build_orion>(*this, params);
  14503. } break;
  14504. case LLM_ARCH_INTERNLM2:
  14505. {
  14506. llm = std::make_unique<llm_build_internlm2>(*this, params);
  14507. } break;
  14508. case LLM_ARCH_MINICPM3:
  14509. {
  14510. llm = std::make_unique<llm_build_minicpm3>(*this, params);
  14511. } break;
  14512. case LLM_ARCH_GEMMA:
  14513. {
  14514. llm = std::make_unique<llm_build_gemma>(*this, params);
  14515. } break;
  14516. case LLM_ARCH_GEMMA2:
  14517. {
  14518. llm = std::make_unique<llm_build_gemma2_iswa>(*this, params);
  14519. } break;
  14520. case LLM_ARCH_GEMMA3:
  14521. {
  14522. llm = std::make_unique<llm_build_gemma3_iswa>(*this, params);
  14523. } break;
  14524. case LLM_ARCH_GEMMA3N:
  14525. {
  14526. llm = std::make_unique<llm_build_gemma3n_iswa>(*this, params);
  14527. } break;
  14528. case LLM_ARCH_STARCODER2:
  14529. {
  14530. llm = std::make_unique<llm_build_starcoder2>(*this, params);
  14531. } break;
  14532. case LLM_ARCH_MAMBA:
  14533. case LLM_ARCH_MAMBA2:
  14534. {
  14535. llm = std::make_unique<llm_build_mamba>(*this, params);
  14536. } break;
  14537. case LLM_ARCH_JAMBA:
  14538. {
  14539. llm = std::make_unique<llm_build_jamba>(*this, params);
  14540. } break;
  14541. case LLM_ARCH_XVERSE:
  14542. {
  14543. llm = std::make_unique<llm_build_xverse>(*this, params);
  14544. } break;
  14545. case LLM_ARCH_COMMAND_R:
  14546. {
  14547. llm = std::make_unique<llm_build_command_r>(*this, params);
  14548. } break;
  14549. case LLM_ARCH_COHERE2:
  14550. {
  14551. llm = std::make_unique<llm_build_cohere2_iswa>(*this, params);
  14552. } break;
  14553. case LLM_ARCH_DBRX:
  14554. {
  14555. llm = std::make_unique<llm_build_dbrx>(*this, params);
  14556. } break;
  14557. case LLM_ARCH_OLMO:
  14558. {
  14559. llm = std::make_unique<llm_build_olmo>(*this, params);
  14560. } break;
  14561. case LLM_ARCH_OLMO2:
  14562. {
  14563. llm = std::make_unique<llm_build_olmo2>(*this, params);
  14564. } break;
  14565. case LLM_ARCH_OLMOE:
  14566. {
  14567. llm = std::make_unique<llm_build_olmoe>(*this, params);
  14568. } break;
  14569. case LLM_ARCH_OPENELM:
  14570. {
  14571. llm = std::make_unique<llm_build_openelm>(*this, params);
  14572. } break;
  14573. case LLM_ARCH_GPTNEOX:
  14574. {
  14575. llm = std::make_unique<llm_build_gptneox>(*this, params);
  14576. } break;
  14577. case LLM_ARCH_ARCTIC:
  14578. {
  14579. llm = std::make_unique<llm_build_arctic>(*this, params);
  14580. } break;
  14581. case LLM_ARCH_DEEPSEEK:
  14582. {
  14583. llm = std::make_unique<llm_build_deepseek>(*this, params);
  14584. } break;
  14585. case LLM_ARCH_DEEPSEEK2:
  14586. {
  14587. llm = std::make_unique<llm_build_deepseek2>(*this, params);
  14588. } break;
  14589. case LLM_ARCH_CHATGLM:
  14590. {
  14591. llm = std::make_unique<llm_build_chatglm>(*this, params);
  14592. } break;
  14593. case LLM_ARCH_GLM4:
  14594. {
  14595. llm = std::make_unique<llm_build_glm4>(*this, params);
  14596. } break;
  14597. case LLM_ARCH_GLM4_MOE:
  14598. {
  14599. llm = std::make_unique<llm_build_glm4_moe>(*this, params);
  14600. } break;
  14601. case LLM_ARCH_BITNET:
  14602. {
  14603. llm = std::make_unique<llm_build_bitnet>(*this, params);
  14604. } break;
  14605. case LLM_ARCH_T5:
  14606. {
  14607. switch (params.gtype) {
  14608. case LLM_GRAPH_TYPE_ENCODER:
  14609. llm = std::make_unique<llm_build_t5_enc>(*this, params);
  14610. break;
  14611. case LLM_GRAPH_TYPE_DEFAULT:
  14612. case LLM_GRAPH_TYPE_DECODER:
  14613. llm = std::make_unique<llm_build_t5_dec>(*this, params);
  14614. break;
  14615. default:
  14616. GGML_ABORT("invalid graph type");
  14617. };
  14618. } break;
  14619. case LLM_ARCH_T5ENCODER:
  14620. {
  14621. llm = std::make_unique<llm_build_t5_enc>(*this, params);
  14622. }
  14623. break;
  14624. case LLM_ARCH_JAIS:
  14625. {
  14626. llm = std::make_unique<llm_build_jais>(*this, params);
  14627. } break;
  14628. case LLM_ARCH_NEMOTRON:
  14629. {
  14630. llm = std::make_unique<llm_build_nemotron>(*this, params);
  14631. } break;
  14632. case LLM_ARCH_EXAONE:
  14633. {
  14634. llm = std::make_unique<llm_build_exaone>(*this, params);
  14635. } break;
  14636. case LLM_ARCH_EXAONE4:
  14637. {
  14638. if (hparams.swa_type == LLAMA_SWA_TYPE_STANDARD) {
  14639. llm = std::make_unique<llm_build_exaone4<true>>(*this, params);
  14640. } else {
  14641. llm = std::make_unique<llm_build_exaone4<false>>(*this, params);
  14642. }
  14643. } break;
  14644. case LLM_ARCH_RWKV6:
  14645. {
  14646. llm = std::make_unique<llm_build_rwkv6>(*this, params);
  14647. } break;
  14648. case LLM_ARCH_RWKV6QWEN2:
  14649. {
  14650. llm = std::make_unique<llm_build_rwkv6qwen2>(*this, params);
  14651. } break;
  14652. case LLM_ARCH_RWKV7:
  14653. {
  14654. llm = std::make_unique<llm_build_rwkv7>(*this, params);
  14655. } break;
  14656. case LLM_ARCH_ARWKV7:
  14657. {
  14658. llm = std::make_unique<llm_build_arwkv7>(*this, params);
  14659. } break;
  14660. case LLM_ARCH_GRANITE:
  14661. case LLM_ARCH_GRANITE_MOE:
  14662. case LLM_ARCH_MINICPM:
  14663. {
  14664. llm = std::make_unique<llm_build_granite>(*this, params);
  14665. } break;
  14666. case LLM_ARCH_GRANITE_HYBRID:
  14667. {
  14668. llm = std::make_unique<llm_build_granite_hybrid>(*this, params);
  14669. } break;
  14670. case LLM_ARCH_CHAMELEON:
  14671. {
  14672. llm = std::make_unique<llm_build_chameleon>(*this, params);
  14673. } break;
  14674. case LLM_ARCH_WAVTOKENIZER_DEC:
  14675. {
  14676. llm = std::make_unique<llm_build_wavtokenizer_dec>(*this, params);
  14677. } break;
  14678. case LLM_ARCH_PLM:
  14679. {
  14680. llm = std::make_unique<llm_build_plm>(*this, params);
  14681. } break;
  14682. case LLM_ARCH_BAILINGMOE:
  14683. {
  14684. llm = std::make_unique<llm_build_bailingmoe>(*this, params);
  14685. } break;
  14686. case LLM_ARCH_DOTS1:
  14687. {
  14688. llm = std::make_unique<llm_build_dots1>(*this, params);
  14689. } break;
  14690. case LLM_ARCH_ARCEE:
  14691. {
  14692. llm = std::make_unique<llm_build_arcee>(*this, params);
  14693. } break;
  14694. case LLM_ARCH_ERNIE4_5:
  14695. {
  14696. llm = std::make_unique<llm_build_ernie4_5>(*this, params);
  14697. } break;
  14698. case LLM_ARCH_ERNIE4_5_MOE:
  14699. {
  14700. llm = std::make_unique<llm_build_ernie4_5_moe>(*this, params);
  14701. } break;
  14702. case LLM_ARCH_HUNYUAN_MOE:
  14703. {
  14704. llm = std::make_unique<llm_build_hunyuan_moe>(*this, params);
  14705. } break;
  14706. case LLM_ARCH_HUNYUAN_DENSE:
  14707. {
  14708. llm = std::make_unique<llm_build_hunyuan_dense>(*this, params);
  14709. } break;
  14710. case LLM_ARCH_SMOLLM3:
  14711. {
  14712. llm = std::make_unique<llm_build_smollm3>(*this, params);
  14713. } break;
  14714. case LLM_ARCH_OPENAI_MOE:
  14715. {
  14716. llm = std::make_unique<llm_build_openai_moe_iswa>(*this, params);
  14717. } break;
  14718. case LLM_ARCH_FALCON_H1:
  14719. {
  14720. llm = std::make_unique<llm_build_falcon_h1>(*this, params);
  14721. } break;
  14722. case LLM_ARCH_LFM2:
  14723. {
  14724. llm = std::make_unique<llm_build_lfm2>(*this, params);
  14725. } break;
  14726. case LLM_ARCH_SMALLTHINKER:
  14727. {
  14728. if (hparams.swa_type == LLAMA_SWA_TYPE_STANDARD) {
  14729. llm = std::make_unique<llm_build_smallthinker<true>> (*this, params);
  14730. } else {
  14731. llm = std::make_unique<llm_build_smallthinker<false>>(*this, params);
  14732. }
  14733. } break;
  14734. default:
  14735. GGML_ABORT("fatal error");
  14736. }
  14737. // add on pooling layer
  14738. llm->build_pooling(cls, cls_b, cls_out, cls_out_b);
  14739. return llm->res->get_gf();
  14740. }
  14741. //
  14742. // interface implementation
  14743. //
  14744. llama_model_params llama_model_default_params() {
  14745. llama_model_params result = {
  14746. /*.devices =*/ nullptr,
  14747. /*.tensor_buft_overrides =*/ nullptr,
  14748. /*.n_gpu_layers =*/ 0,
  14749. /*.split_mode =*/ LLAMA_SPLIT_MODE_LAYER,
  14750. /*.main_gpu =*/ 0,
  14751. /*.tensor_split =*/ nullptr,
  14752. /*.progress_callback =*/ nullptr,
  14753. /*.progress_callback_user_data =*/ nullptr,
  14754. /*.kv_overrides =*/ nullptr,
  14755. /*.vocab_only =*/ false,
  14756. /*.use_mmap =*/ true,
  14757. /*.use_mlock =*/ false,
  14758. /*.check_tensors =*/ false,
  14759. /*.use_extra_bufts =*/ true,
  14760. };
  14761. #ifdef GGML_USE_METAL
  14762. // note: we usually have plenty of VRAM, so by default offload all layers to the GPU
  14763. result.n_gpu_layers = 999;
  14764. #endif
  14765. return result;
  14766. }
  14767. const llama_vocab * llama_model_get_vocab(const llama_model * model) {
  14768. return &model->vocab;
  14769. }
  14770. void llama_free_model(llama_model * model) {
  14771. llama_model_free(model);
  14772. }
  14773. void llama_model_free(llama_model * model) {
  14774. delete model;
  14775. }
  14776. int32_t llama_model_n_ctx_train(const llama_model * model) {
  14777. return model->hparams.n_ctx_train;
  14778. }
  14779. int32_t llama_model_n_embd(const llama_model * model) {
  14780. return model->hparams.n_embd;
  14781. }
  14782. int32_t llama_model_n_layer(const llama_model * model) {
  14783. return model->hparams.n_layer;
  14784. }
  14785. int32_t llama_model_n_head(const llama_model * model) {
  14786. return model->hparams.n_head();
  14787. }
  14788. int32_t llama_model_n_head_kv(const llama_model * model) {
  14789. return model->hparams.n_head_kv();
  14790. }
  14791. int32_t llama_model_n_swa(const llama_model * model) {
  14792. return model->hparams.n_swa;
  14793. }
  14794. uint32_t llama_model_n_cls_out(const struct llama_model * model) {
  14795. return model->hparams.n_cls_out;
  14796. }
  14797. const char * llama_model_cls_label(const struct llama_model * model, uint32_t i) {
  14798. if (i < model->classifier_labels.size()) {
  14799. return model->classifier_labels[i].c_str();
  14800. }
  14801. return nullptr;
  14802. }
  14803. // deprecated
  14804. int32_t llama_n_ctx_train(const llama_model * model) {
  14805. return llama_model_n_ctx_train(model);
  14806. }
  14807. // deprecated
  14808. int32_t llama_n_embd(const llama_model * model) {
  14809. return llama_model_n_embd(model);
  14810. }
  14811. // deprecated
  14812. int32_t llama_n_layer(const llama_model * model) {
  14813. return llama_model_n_layer(model);
  14814. }
  14815. // deprecated
  14816. int32_t llama_n_head(const llama_model * model) {
  14817. return llama_model_n_head(model);
  14818. }
  14819. llama_rope_type llama_model_rope_type(const llama_model * model) {
  14820. switch (model->arch) {
  14821. // these models do not use RoPE
  14822. case LLM_ARCH_GPT2:
  14823. case LLM_ARCH_GPTJ:
  14824. case LLM_ARCH_MPT:
  14825. case LLM_ARCH_REFACT:
  14826. case LLM_ARCH_BLOOM:
  14827. case LLM_ARCH_MAMBA:
  14828. case LLM_ARCH_MAMBA2:
  14829. case LLM_ARCH_JAMBA:
  14830. case LLM_ARCH_JINA_BERT_V2:
  14831. case LLM_ARCH_T5:
  14832. case LLM_ARCH_T5ENCODER:
  14833. case LLM_ARCH_JAIS:
  14834. case LLM_ARCH_RWKV6:
  14835. case LLM_ARCH_RWKV6QWEN2:
  14836. case LLM_ARCH_RWKV7:
  14837. case LLM_ARCH_ARWKV7:
  14838. case LLM_ARCH_WAVTOKENIZER_DEC:
  14839. return LLAMA_ROPE_TYPE_NONE;
  14840. // use what we call a normal RoPE, operating on pairs of consecutive head values
  14841. case LLM_ARCH_LLAMA:
  14842. case LLM_ARCH_LLADA:
  14843. case LLM_ARCH_LLAMA4:
  14844. case LLM_ARCH_DECI:
  14845. case LLM_ARCH_BAICHUAN:
  14846. case LLM_ARCH_STARCODER:
  14847. case LLM_ARCH_INTERNLM2:
  14848. case LLM_ARCH_MINICPM:
  14849. case LLM_ARCH_XVERSE:
  14850. case LLM_ARCH_COMMAND_R:
  14851. case LLM_ARCH_COHERE2:
  14852. case LLM_ARCH_OLMO:
  14853. case LLM_ARCH_ARCTIC:
  14854. case LLM_ARCH_DEEPSEEK:
  14855. case LLM_ARCH_DEEPSEEK2:
  14856. case LLM_ARCH_PLM:
  14857. case LLM_ARCH_CHATGLM:
  14858. case LLM_ARCH_GLM4:
  14859. case LLM_ARCH_GRANITE:
  14860. case LLM_ARCH_GRANITE_MOE:
  14861. case LLM_ARCH_GRANITE_HYBRID:
  14862. case LLM_ARCH_CHAMELEON:
  14863. case LLM_ARCH_BAILINGMOE:
  14864. case LLM_ARCH_NEO_BERT:
  14865. case LLM_ARCH_SMOLLM3:
  14866. case LLM_ARCH_ARCEE:
  14867. case LLM_ARCH_ERNIE4_5:
  14868. case LLM_ARCH_ERNIE4_5_MOE:
  14869. return LLAMA_ROPE_TYPE_NORM;
  14870. // the pairs of head values are offset by n_rot/2
  14871. case LLM_ARCH_FALCON:
  14872. case LLM_ARCH_FALCON_H1:
  14873. case LLM_ARCH_GROK:
  14874. case LLM_ARCH_DBRX:
  14875. case LLM_ARCH_BERT:
  14876. case LLM_ARCH_NOMIC_BERT:
  14877. case LLM_ARCH_NOMIC_BERT_MOE:
  14878. case LLM_ARCH_STABLELM:
  14879. case LLM_ARCH_BITNET:
  14880. case LLM_ARCH_QWEN:
  14881. case LLM_ARCH_QWEN2:
  14882. case LLM_ARCH_DREAM:
  14883. case LLM_ARCH_QWEN2MOE:
  14884. case LLM_ARCH_QWEN3:
  14885. case LLM_ARCH_QWEN3MOE:
  14886. case LLM_ARCH_OLMO2:
  14887. case LLM_ARCH_OLMOE:
  14888. case LLM_ARCH_PHI2:
  14889. case LLM_ARCH_PHI3:
  14890. case LLM_ARCH_PHIMOE:
  14891. case LLM_ARCH_PLAMO:
  14892. case LLM_ARCH_PLAMO2:
  14893. case LLM_ARCH_GEMMA:
  14894. case LLM_ARCH_GEMMA2:
  14895. case LLM_ARCH_GEMMA3:
  14896. case LLM_ARCH_GEMMA3N:
  14897. case LLM_ARCH_STARCODER2:
  14898. case LLM_ARCH_OPENELM:
  14899. case LLM_ARCH_GPTNEOX:
  14900. case LLM_ARCH_CODESHELL:
  14901. case LLM_ARCH_ORION:
  14902. case LLM_ARCH_NEMOTRON:
  14903. case LLM_ARCH_EXAONE:
  14904. case LLM_ARCH_EXAONE4:
  14905. case LLM_ARCH_MINICPM3:
  14906. case LLM_ARCH_DOTS1:
  14907. case LLM_ARCH_HUNYUAN_MOE:
  14908. case LLM_ARCH_OPENAI_MOE:
  14909. case LLM_ARCH_HUNYUAN_DENSE:
  14910. case LLM_ARCH_LFM2:
  14911. case LLM_ARCH_SMALLTHINKER:
  14912. case LLM_ARCH_GLM4_MOE:
  14913. return LLAMA_ROPE_TYPE_NEOX;
  14914. case LLM_ARCH_QWEN2VL:
  14915. return LLAMA_ROPE_TYPE_MROPE;
  14916. // all model arches should be listed explicitly here
  14917. case LLM_ARCH_UNKNOWN:
  14918. GGML_ABORT("unknown architecture");
  14919. }
  14920. return LLAMA_ROPE_TYPE_NONE;
  14921. }
  14922. float llama_model_rope_freq_scale_train(const llama_model * model) {
  14923. return model->hparams.rope_freq_scale_train;
  14924. }
  14925. int32_t llama_model_meta_val_str(const llama_model * model, const char * key, char * buf, size_t buf_size) {
  14926. const auto & it = model->gguf_kv.find(key);
  14927. if (it == model->gguf_kv.end()) {
  14928. if (buf_size > 0) {
  14929. buf[0] = '\0';
  14930. }
  14931. return -1;
  14932. }
  14933. return snprintf(buf, buf_size, "%s", it->second.c_str());
  14934. }
  14935. int32_t llama_model_meta_count(const llama_model * model) {
  14936. return (int)model->gguf_kv.size();
  14937. }
  14938. int32_t llama_model_meta_key_by_index(const llama_model * model, int i, char * buf, size_t buf_size) {
  14939. if (i < 0 || i >= (int)model->gguf_kv.size()) {
  14940. if (buf_size > 0) {
  14941. buf[0] = '\0';
  14942. }
  14943. return -1;
  14944. }
  14945. auto it = model->gguf_kv.begin();
  14946. std::advance(it, i);
  14947. return snprintf(buf, buf_size, "%s", it->first.c_str());
  14948. }
  14949. int32_t llama_model_meta_val_str_by_index(const llama_model * model, int32_t i, char * buf, size_t buf_size) {
  14950. if (i < 0 || i >= (int)model->gguf_kv.size()) {
  14951. if (buf_size > 0) {
  14952. buf[0] = '\0';
  14953. }
  14954. return -1;
  14955. }
  14956. auto it = model->gguf_kv.begin();
  14957. std::advance(it, i);
  14958. return snprintf(buf, buf_size, "%s", it->second.c_str());
  14959. }
  14960. int32_t llama_model_desc(const llama_model * model, char * buf, size_t buf_size) {
  14961. return snprintf(buf, buf_size, "%s", model->desc().c_str());
  14962. }
  14963. uint64_t llama_model_size(const llama_model * model) {
  14964. return model->size();
  14965. }
  14966. const char * llama_model_chat_template(const llama_model * model, const char * name) {
  14967. const auto key = name ? LLM_KV(model->arch, name)(LLM_KV_TOKENIZER_CHAT_TEMPLATE)
  14968. : LLM_KV(model->arch)(LLM_KV_TOKENIZER_CHAT_TEMPLATE);
  14969. const auto & it = model->gguf_kv.find(key);
  14970. if (it == model->gguf_kv.end()) {
  14971. // one-off fix for very popular models (so we are not flooded with issues)
  14972. // do not extend this list unless absolutely necessary
  14973. // Mistral-Small-2503 does not have built-in chat template
  14974. llama_vocab_pre_type pre_type = model->vocab.get_pre_type();
  14975. if (!name && pre_type == LLAMA_VOCAB_PRE_TYPE_TEKKEN && model->layers.size() == 40) {
  14976. return "mistral-v7-tekken";
  14977. }
  14978. return nullptr;
  14979. }
  14980. return it->second.c_str();
  14981. }
  14982. uint64_t llama_model_n_params(const llama_model * model) {
  14983. return model->n_elements();
  14984. }
  14985. bool llama_model_has_encoder(const llama_model * model) {
  14986. switch (model->arch) {
  14987. case LLM_ARCH_T5: return true;
  14988. case LLM_ARCH_T5ENCODER: return true;
  14989. default: return false;
  14990. }
  14991. }
  14992. bool llama_model_has_decoder(const llama_model * model) {
  14993. switch (model->arch) {
  14994. case LLM_ARCH_T5ENCODER: return false;
  14995. default: return true;
  14996. }
  14997. }
  14998. llama_token llama_model_decoder_start_token(const llama_model * model) {
  14999. return model->hparams.dec_start_token_id;
  15000. }
  15001. bool llama_model_is_recurrent(const llama_model * model) {
  15002. return llm_arch_is_recurrent(model->arch);
  15003. }
  15004. bool llama_model_is_diffusion(const llama_model * model) {
  15005. return llm_arch_is_diffusion(model->arch);
  15006. }
  15007. const std::vector<std::pair<std::string, ggml_tensor *>> & llama_internal_get_tensor_map(const llama_model * model) {
  15008. return model->tensors_by_name;
  15009. }