llama-model.cpp 699 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179
  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_270M: return "270M";
  39. case LLM_TYPE_335M: return "335M";
  40. case LLM_TYPE_410M: return "410M";
  41. case LLM_TYPE_450M: return "450M";
  42. case LLM_TYPE_475M: return "475M";
  43. case LLM_TYPE_770M: return "770M";
  44. case LLM_TYPE_780M: return "780M";
  45. case LLM_TYPE_0_3B: return "0.3B";
  46. case LLM_TYPE_0_5B: return "0.5B";
  47. case LLM_TYPE_0_6B: return "0.6B";
  48. case LLM_TYPE_1B: return "1B";
  49. case LLM_TYPE_1_3B: return "1.3B";
  50. case LLM_TYPE_1_4B: return "1.4B";
  51. case LLM_TYPE_1_5B: return "1.5B";
  52. case LLM_TYPE_1_6B: return "1.6B";
  53. case LLM_TYPE_1_7B: return "1.7B";
  54. case LLM_TYPE_1_8B: return "1.8B";
  55. case LLM_TYPE_2B: return "2B";
  56. case LLM_TYPE_2_8B: return "2.8B";
  57. case LLM_TYPE_2_9B: return "2.9B";
  58. case LLM_TYPE_3B: return "3B";
  59. case LLM_TYPE_4B: return "4B";
  60. case LLM_TYPE_6B: return "6B";
  61. case LLM_TYPE_6_9B: return "6.9B";
  62. case LLM_TYPE_7B: return "7B";
  63. case LLM_TYPE_8B: return "8B";
  64. case LLM_TYPE_9B: return "9B";
  65. case LLM_TYPE_11B: return "11B";
  66. case LLM_TYPE_12B: return "12B";
  67. case LLM_TYPE_13B: return "13B";
  68. case LLM_TYPE_14B: return "14B";
  69. case LLM_TYPE_15B: return "15B";
  70. case LLM_TYPE_16B: return "16B";
  71. case LLM_TYPE_20B: return "20B";
  72. case LLM_TYPE_27B: return "27B";
  73. case LLM_TYPE_30B: return "30B";
  74. case LLM_TYPE_32B: return "32B";
  75. case LLM_TYPE_34B: return "34B";
  76. case LLM_TYPE_35B: return "35B";
  77. case LLM_TYPE_40B: return "40B";
  78. case LLM_TYPE_65B: return "65B";
  79. case LLM_TYPE_70B: return "70B";
  80. case LLM_TYPE_142B: return "142B";
  81. case LLM_TYPE_236B: return "236B";
  82. case LLM_TYPE_290B: return "290B";
  83. case LLM_TYPE_314B: return "314B";
  84. case LLM_TYPE_405B: return "405B";
  85. case LLM_TYPE_671B: return "671B";
  86. case LLM_TYPE_SMALL: return "0.1B";
  87. case LLM_TYPE_MEDIUM: return "0.4B";
  88. case LLM_TYPE_LARGE: return "0.8B";
  89. case LLM_TYPE_XL: return "1.5B";
  90. case LLM_TYPE_A1_7B: return "A1.7B";
  91. case LLM_TYPE_A2_7B: return "A2.7B";
  92. case LLM_TYPE_8x7B: return "8x7B";
  93. case LLM_TYPE_8x22B: return "8x22B";
  94. case LLM_TYPE_16x12B: return "16x12B";
  95. case LLM_TYPE_16x3_8B: return "16x3.8B";
  96. case LLM_TYPE_10B_128x3_66B: return "10B+128x3.66B";
  97. case LLM_TYPE_57B_A14B: return "57B.A14B";
  98. case LLM_TYPE_17B_16E: return "17Bx16E (Scout)";
  99. case LLM_TYPE_17B_128E: return "17Bx128E (Maverick)";
  100. case LLM_TYPE_A13B: return "A13B";
  101. case LLM_TYPE_30B_A3B: return "30B.A3B";
  102. case LLM_TYPE_235B_A22B: return "235B.A22B";
  103. case LLM_TYPE_E2B: return "E2B";
  104. case LLM_TYPE_E4B: return "E4B";
  105. default: return "?B";
  106. }
  107. }
  108. static const char * llama_expert_gating_func_name(llama_expert_gating_func_type type) {
  109. switch (type) {
  110. case LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX: return "softmax";
  111. case LLAMA_EXPERT_GATING_FUNC_TYPE_SIGMOID: return "sigmoid";
  112. default: return "unknown";
  113. }
  114. }
  115. static const std::map<llama_rope_scaling_type, const char *> LLAMA_ROPE_SCALING_TYPES = {
  116. { LLAMA_ROPE_SCALING_TYPE_NONE, "none" },
  117. { LLAMA_ROPE_SCALING_TYPE_LINEAR, "linear" },
  118. { LLAMA_ROPE_SCALING_TYPE_YARN, "yarn" },
  119. { LLAMA_ROPE_SCALING_TYPE_LONGROPE, "longrope" },
  120. };
  121. std::string llama_rope_scaling_type_name(llama_rope_scaling_type rope_scaling_type) {
  122. return LLAMA_ROPE_SCALING_TYPES.at(rope_scaling_type);
  123. }
  124. static llama_rope_scaling_type llama_rope_scaling_type_from_string(const std::string & name) {
  125. for (const auto & kv : LLAMA_ROPE_SCALING_TYPES) {
  126. if (kv.second == name) {
  127. return (llama_rope_scaling_type) kv.first;
  128. }
  129. }
  130. return LLAMA_ROPE_SCALING_TYPE_UNSPECIFIED;
  131. }
  132. // checks if the weight tensor can be used with the specified buffer type and device
  133. 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) {
  134. GGML_ASSERT(w != nullptr);
  135. if (op == GGML_OP_NONE) {
  136. return true;
  137. }
  138. ggml_init_params params = {
  139. /*.mem_size =*/ ggml_tensor_overhead()*8,
  140. /*.mem_buffer =*/ NULL,
  141. /*.no_alloc =*/ true,
  142. };
  143. ggml_context_ptr ctx_ptr { ggml_init(params) };
  144. if (!ctx_ptr) {
  145. throw std::runtime_error(format("failed to create ggml context"));
  146. }
  147. ggml_context * ctx = ctx_ptr.get();
  148. ggml_tensor * op_tensor = nullptr;
  149. switch (op) {
  150. case GGML_OP_GET_ROWS:
  151. {
  152. ggml_tensor * b = ggml_new_tensor_1d(ctx, GGML_TYPE_I32, 512);
  153. op_tensor = ggml_get_rows(ctx, w, b);
  154. } break;
  155. case GGML_OP_MUL_MAT:
  156. {
  157. ggml_tensor * b = ggml_new_tensor_4d(ctx, GGML_TYPE_F32, w->ne[0], 512, w->ne[2], w->ne[3]);
  158. op_tensor = ggml_mul_mat(ctx, w, b);
  159. } break;
  160. case GGML_OP_MUL_MAT_ID:
  161. {
  162. int n_expert_used = hparams.n_expert_used;
  163. ggml_tensor * b = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, w->ne[0], n_expert_used, 512);
  164. ggml_tensor * ids = ggml_new_tensor_2d(ctx, GGML_TYPE_I32, n_expert_used, 512);
  165. op_tensor = ggml_mul_mat_id(ctx, w, b, ids);
  166. } break;
  167. case GGML_OP_ADD:
  168. {
  169. ggml_tensor * a = ggml_new_tensor_4d(ctx, GGML_TYPE_F32, w->ne[0], w->ne[1], w->ne[2], w->ne[3]);
  170. op_tensor = ggml_add(ctx, a, w);
  171. } break;
  172. case GGML_OP_MUL:
  173. {
  174. ggml_tensor * a = ggml_new_tensor_4d(ctx, GGML_TYPE_F32, w->ne[0], w->ne[1], w->ne[2], w->ne[3]);
  175. op_tensor = ggml_mul(ctx, a, w);
  176. } break;
  177. case GGML_OP_DIV:
  178. {
  179. ggml_tensor * a = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, w->ne[0]);
  180. op_tensor = ggml_div(ctx, a, w);
  181. } break;
  182. case GGML_OP_ROPE:
  183. {
  184. int n_embd_head = hparams.n_embd_head_v;
  185. int n_head = hparams.n_head();
  186. ggml_tensor * a = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, n_embd_head, n_head, 512);
  187. ggml_tensor * b = ggml_new_tensor_1d(ctx, GGML_TYPE_I32, 512);
  188. op_tensor = ggml_rope_ext(
  189. ctx, a, b, w,
  190. 0, 0, 0, 0, 0,
  191. 0, 0, 0, 0
  192. );
  193. } break;
  194. case GGML_OP_SSM_CONV:
  195. {
  196. const int64_t n_seq_tokens = 512;
  197. const int64_t n_seqs = 3;
  198. ggml_tensor * conv_x = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, w->ne[0] - 1 + n_seq_tokens, w->ne[1], n_seqs);
  199. op_tensor = ggml_ssm_conv(ctx, conv_x, w);
  200. } break;
  201. case GGML_OP_SSM_SCAN:
  202. {
  203. // w is ssm_a, which is used to distinguish Mamba-1 and Mamba-2
  204. const int64_t d_state = w->ne[0] == 1 ? hparams.ssm_d_state : w->ne[0];
  205. const int64_t n_head = w->ne[1];
  206. const int64_t head_dim = hparams.ssm_d_inner / n_head;
  207. const int64_t n_group = hparams.ssm_n_group ? hparams.ssm_n_group : 1;
  208. const int64_t n_seq_tokens = 512;
  209. const int64_t n_seqs = 3;
  210. ggml_tensor * s = ggml_new_tensor_4d(ctx, GGML_TYPE_F32, d_state, head_dim, n_head, n_seqs);
  211. ggml_tensor * x = ggml_new_tensor_4d(ctx, GGML_TYPE_F32, head_dim, n_head, n_seq_tokens, n_seqs);
  212. ggml_tensor * dt = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, n_head, n_seq_tokens, n_seqs);
  213. ggml_tensor * B = ggml_new_tensor_4d(ctx, GGML_TYPE_F32, d_state, n_group, n_seq_tokens, n_seqs);
  214. ggml_tensor * C = ggml_new_tensor_4d(ctx, GGML_TYPE_F32, d_state, n_group, n_seq_tokens, n_seqs);
  215. ggml_tensor * ids = ggml_new_tensor_1d(ctx, GGML_TYPE_I32, n_seqs);
  216. op_tensor = ggml_ssm_scan(ctx, s, x, dt, w, B, C, ids);
  217. } break;
  218. case GGML_OP_RWKV_WKV6:
  219. {
  220. // FIXME
  221. const int64_t S = 123;
  222. const int64_t H = 123;
  223. const int64_t n_tokens = 123;
  224. const int64_t n_seqs = 123;
  225. ggml_tensor * k = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, S, H, n_tokens);
  226. ggml_tensor * v = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, S, H, n_tokens);
  227. ggml_tensor * r = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, S, H, n_tokens);
  228. ggml_tensor * tf = w;
  229. ggml_tensor * td = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, S, H, n_tokens);
  230. ggml_tensor * state = ggml_new_tensor_4d(ctx, GGML_TYPE_F32, S, n_seqs, S, H);
  231. op_tensor = ggml_rwkv_wkv6(ctx, k, v, r, tf, td, state);
  232. } break;
  233. case GGML_OP_IM2COL:
  234. {
  235. const int n_embd = hparams.n_embd;
  236. ggml_tensor * b = ggml_new_tensor_4d(ctx, GGML_TYPE_F32, n_embd, w->ne[1], 1, 1);
  237. op_tensor = ggml_im2col(ctx, w, b, 1, 0, 0, 0, 1, 0, false, GGML_TYPE_F16);
  238. } break;
  239. default:
  240. GGML_ABORT("%s: missing test for op %s for tensor %s", __func__, ggml_op_name(op), w->name);
  241. }
  242. // create a temporary dummy buffer for the weight so that supports_op can check the buffer type
  243. GGML_ASSERT(w->buffer == nullptr);
  244. w->buffer = ggml_backend_buft_alloc_buffer(buft, 0);
  245. bool op_supported = ggml_backend_dev_supports_op(dev, op_tensor);
  246. ggml_backend_buffer_free(w->buffer);
  247. w->buffer = nullptr;
  248. return op_supported;
  249. }
  250. // lists of buffer types used for each layer
  251. using buft_list_t = std::vector<std::pair<ggml_backend_dev_t, ggml_backend_buffer_type_t>>;
  252. // find the first buffer type in the list that can use the tensor
  253. 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) {
  254. GGML_ASSERT(!buft_list.empty());
  255. for (const auto & cur : buft_list) {
  256. ggml_backend_dev_t cur_dev = cur.first;
  257. ggml_backend_buffer_type_t cur_buft = cur.second;
  258. if (weight_buft_supported(hparams, tensor, op, cur_buft, cur_dev)) {
  259. return cur_buft;
  260. }
  261. }
  262. return nullptr;
  263. }
  264. // CPU: ACCEL -> GPU host -> CPU extra -> CPU
  265. static buft_list_t make_cpu_buft_list(const std::vector<ggml_backend_dev_t> & devices) {
  266. buft_list_t buft_list;
  267. // add ACCEL buffer types
  268. for (size_t i = 0; i < ggml_backend_dev_count(); ++i) {
  269. ggml_backend_dev_t dev = ggml_backend_dev_get(i);
  270. if (ggml_backend_dev_type(dev) == GGML_BACKEND_DEVICE_TYPE_ACCEL) {
  271. auto * buft = ggml_backend_dev_buffer_type(dev);
  272. // skip
  273. if (buft != ggml_backend_cpu_buffer_type()) {
  274. buft_list.emplace_back(dev, buft);
  275. }
  276. }
  277. }
  278. // add a host buffer type
  279. // storing the tensors in a host buffer is useful when the processing of large batches
  280. // is offloaded to a GPU device, since it reduces the time spent on data transfers
  281. // generally, this will be done using the first device in the list
  282. // a better approach would be to handle this on a weight-by-weight basis using the offload_op
  283. // function of the device to determine if it would benefit from being stored in a host buffer
  284. for (auto * dev : devices) {
  285. ggml_backend_buffer_type_t buft = ggml_backend_dev_host_buffer_type(dev);
  286. if (buft) {
  287. buft_list.emplace_back(dev, buft);
  288. break;
  289. }
  290. }
  291. // add extra buffer types, only if no GPU device is present
  292. // ref: https://github.com/ggml-org/llama.cpp/issues/12481#issuecomment-2743136094
  293. auto * cpu_dev = ggml_backend_dev_by_type(GGML_BACKEND_DEVICE_TYPE_CPU);
  294. if (cpu_dev == nullptr) {
  295. throw std::runtime_error(format("%s: no CPU backend found", __func__));
  296. }
  297. auto * cpu_reg = ggml_backend_dev_backend_reg(cpu_dev);
  298. auto ggml_backend_dev_get_extra_bufts_fn = (ggml_backend_dev_get_extra_bufts_t)
  299. ggml_backend_reg_get_proc_address(cpu_reg, "ggml_backend_dev_get_extra_bufts");
  300. if (ggml_backend_dev_get_extra_bufts_fn) {
  301. ggml_backend_buffer_type_t * extra_bufts = ggml_backend_dev_get_extra_bufts_fn(cpu_dev);
  302. while (extra_bufts && *extra_bufts) {
  303. buft_list.emplace_back(cpu_dev, *extra_bufts);
  304. ++extra_bufts;
  305. }
  306. }
  307. // add the CPU buffer type
  308. for (size_t i = 0; i < ggml_backend_dev_count(); ++i) {
  309. ggml_backend_dev_t dev = ggml_backend_dev_get(i);
  310. if (ggml_backend_dev_type(dev) == GGML_BACKEND_DEVICE_TYPE_CPU) {
  311. buft_list.emplace_back(dev, ggml_backend_dev_buffer_type(dev));
  312. }
  313. }
  314. return buft_list;
  315. }
  316. // GPU: split if LLAMA_SPLIT_MODE_ROW -> GPU
  317. static buft_list_t make_gpu_buft_list(ggml_backend_dev_t dev, llama_split_mode split_mode, const float * tensor_split) {
  318. buft_list_t buft_list;
  319. // add the device split buffer type if requested and available
  320. if (split_mode == LLAMA_SPLIT_MODE_ROW) {
  321. ggml_backend_reg_t reg = ggml_backend_dev_backend_reg(dev);
  322. auto ggml_backend_split_buffer_type_fn = (ggml_backend_split_buffer_type_t)
  323. ggml_backend_reg_get_proc_address(reg, "ggml_backend_split_buffer_type");
  324. if (ggml_backend_split_buffer_type_fn) {
  325. size_t dev_index = [&]() {
  326. auto * reg = ggml_backend_dev_backend_reg(dev);
  327. for (size_t i = 0; i < ggml_backend_reg_dev_count(reg); ++i) {
  328. if (ggml_backend_reg_dev_get(reg, i) == dev) {
  329. return i;
  330. }
  331. }
  332. throw std::runtime_error(format("device %s not found in its backend reg", ggml_backend_dev_name(dev)));
  333. }();
  334. auto * buft = ggml_backend_split_buffer_type_fn(dev_index, tensor_split);
  335. if (buft != nullptr) {
  336. buft_list.emplace_back(dev, buft);
  337. }
  338. }
  339. }
  340. // add the device default buffer type
  341. buft_list.emplace_back(dev, ggml_backend_dev_buffer_type(dev));
  342. return buft_list;
  343. }
  344. struct llama_model::impl {
  345. impl() {}
  346. ~impl() {}
  347. uint64_t n_elements = 0;
  348. size_t n_bytes = 0;
  349. std::string desc_str;
  350. // model memory mapped files
  351. llama_mmaps mappings;
  352. // objects representing data potentially being locked in memory
  353. llama_mlocks mlock_bufs;
  354. llama_mlocks mlock_mmaps;
  355. // contexts where the model tensors metadata is stored
  356. std::vector<ggml_context_ptr> ctxs;
  357. // the model memory buffers for the tensor data
  358. std::vector<ggml_backend_buffer_ptr> bufs;
  359. buft_list_t cpu_buft_list;
  360. std::map<ggml_backend_dev_t, buft_list_t> gpu_buft_list;
  361. struct layer_dev {
  362. ggml_backend_dev_t dev;
  363. buft_list_t * buft_list;
  364. };
  365. layer_dev dev_input = {};
  366. layer_dev dev_output = {};
  367. std::vector<layer_dev> dev_layer;
  368. bool has_tensor_overrides;
  369. };
  370. llama_model::llama_model(const llama_model_params & params) : params(params), pimpl(std::make_unique<impl>()) {
  371. pimpl->has_tensor_overrides = params.tensor_buft_overrides && params.tensor_buft_overrides[0].pattern;
  372. }
  373. llama_model::~llama_model() {}
  374. void llama_model::load_stats(llama_model_loader & ml) {
  375. pimpl->n_elements = ml.n_elements;
  376. pimpl->n_bytes = ml.n_bytes;
  377. }
  378. void llama_model::load_arch(llama_model_loader & ml) {
  379. arch = ml.get_arch();
  380. if (arch == LLM_ARCH_UNKNOWN) {
  381. throw std::runtime_error("unknown model architecture: '" + ml.get_arch_name() + "'");
  382. }
  383. }
  384. void llama_model::load_hparams(llama_model_loader & ml) {
  385. const gguf_context * ctx = ml.meta.get();
  386. // get metadata as string
  387. for (int i = 0; i < gguf_get_n_kv(ctx); i++) {
  388. gguf_type type = gguf_get_kv_type(ctx, i);
  389. if (type == GGUF_TYPE_ARRAY) {
  390. continue;
  391. }
  392. const char * name = gguf_get_key(ctx, i);
  393. const std::string value = gguf_kv_to_str(ctx, i);
  394. gguf_kv.emplace(name, value);
  395. }
  396. // get general kv
  397. ml.get_key(LLM_KV_GENERAL_NAME, name, false);
  398. // everything past this point is not vocab-related
  399. if (hparams.vocab_only) {
  400. return;
  401. }
  402. ml.get_key(LLM_KV_CONTEXT_LENGTH, hparams.n_ctx_train);
  403. ml.get_key(LLM_KV_EMBEDDING_LENGTH, hparams.n_embd);
  404. ml.get_key(LLM_KV_BLOCK_COUNT, hparams.n_layer);
  405. ml.get_key(LLM_KV_EXPERT_COUNT, hparams.n_expert, false);
  406. ml.get_key(LLM_KV_EXPERT_USED_COUNT, hparams.n_expert_used, false);
  407. if (arch == LLM_ARCH_WAVTOKENIZER_DEC) {
  408. ml.get_key(LLM_KV_FEATURES_LENGTH, hparams.n_embd_features);
  409. ml.get_key(LLM_KV_POSNET_EMBEDDING_LENGTH, hparams.posnet.n_embd);
  410. ml.get_key(LLM_KV_POSNET_BLOCK_COUNT, hparams.posnet.n_layer);
  411. ml.get_key(LLM_KV_CONVNEXT_EMBEDDING_LENGTH, hparams.convnext.n_embd);
  412. ml.get_key(LLM_KV_CONVNEXT_BLOCK_COUNT, hparams.convnext.n_layer);
  413. }
  414. GGML_ASSERT(hparams.n_expert <= LLAMA_MAX_EXPERTS);
  415. GGML_ASSERT(hparams.n_expert_used <= hparams.n_expert);
  416. if (hparams.n_expert > 0) {
  417. GGML_ASSERT(hparams.n_expert_used > 0);
  418. } else {
  419. GGML_ASSERT(hparams.n_expert_used == 0);
  420. }
  421. std::fill(hparams.n_head_arr.begin(), hparams.n_head_arr.end(), 0);
  422. std::fill(hparams.n_head_kv_arr.begin(), hparams.n_head_kv_arr.end(), 0);
  423. std::fill(hparams.n_ff_arr.begin(), hparams.n_ff_arr.end(), 0);
  424. std::fill(
  425. hparams.recurrent_layer_arr.begin(),
  426. hparams.recurrent_layer_arr.end(),
  427. llm_arch_is_recurrent(ml.get_arch()));
  428. std::fill(hparams.rope_sections.begin(), hparams.rope_sections.end(), 0);
  429. std::fill(hparams.swa_layers.begin(), hparams.swa_layers.end(), 0);
  430. ml.get_key_or_arr(LLM_KV_FEED_FORWARD_LENGTH, hparams.n_ff_arr, hparams.n_layer, false);
  431. ml.get_key_or_arr(LLM_KV_ATTENTION_HEAD_COUNT, hparams.n_head_arr, hparams.n_layer, false);
  432. // n_head_kv is optional, default to n_head
  433. hparams.n_head_kv_arr = hparams.n_head_arr;
  434. ml.get_key_or_arr(LLM_KV_ATTENTION_HEAD_COUNT_KV, hparams.n_head_kv_arr, hparams.n_layer, false);
  435. bool rope_finetuned = false;
  436. ml.get_key(LLM_KV_ROPE_SCALING_FINETUNED, rope_finetuned, false);
  437. hparams.rope_finetuned = rope_finetuned;
  438. hparams.n_ctx_orig_yarn = hparams.n_ctx_train;
  439. ml.get_key(LLM_KV_ROPE_SCALING_ORIG_CTX_LEN, hparams.n_ctx_orig_yarn, false);
  440. // rope_freq_base (optional)
  441. hparams.rope_freq_base_train = 10000.0f;
  442. ml.get_key(LLM_KV_ROPE_FREQ_BASE, hparams.rope_freq_base_train, false);
  443. std::string rope_scaling("linear");
  444. ml.get_key(LLM_KV_ROPE_SCALING_TYPE, rope_scaling, false);
  445. hparams.rope_scaling_type_train = llama_rope_scaling_type_from_string(rope_scaling);
  446. GGML_ASSERT(hparams.rope_scaling_type_train != LLAMA_ROPE_SCALING_TYPE_UNSPECIFIED);
  447. // rope_freq_scale (inverse of the kv) is optional
  448. float ropescale = 0.0f;
  449. if (!ml.get_key(LLM_KV_ROPE_SCALING_FACTOR, ropescale, false)) {
  450. // try the old key name
  451. ml.get_key(LLM_KV_ROPE_SCALE_LINEAR, ropescale, false);
  452. }
  453. hparams.rope_freq_scale_train = ropescale == 0.0f ? 1.0f : 1.0f/ropescale;
  454. // by default assume that the sliding-window layers use the same scaling type as the non-sliding-window layers
  455. hparams.rope_freq_base_train_swa = hparams.rope_freq_base_train;
  456. hparams.rope_freq_scale_train_swa = hparams.rope_freq_scale_train;
  457. ml.get_key(LLM_KV_ROPE_SCALING_ATTN_FACTOR, hparams.rope_attn_factor, false);
  458. // non-transformer models do not have attention heads
  459. if (hparams.n_head() > 0) {
  460. // gpt-neox n_rot = rotary_pct * (n_embd / n_head)
  461. // gpt-j n_rot = rotary_dim
  462. hparams.n_embd_head_k = hparams.n_embd / hparams.n_head();
  463. ml.get_key(LLM_KV_ATTENTION_KEY_LENGTH, hparams.n_embd_head_k, false);
  464. hparams.n_embd_head_v = hparams.n_embd / hparams.n_head();
  465. ml.get_key(LLM_KV_ATTENTION_VALUE_LENGTH, hparams.n_embd_head_v, false);
  466. // sanity check for n_rot (optional)
  467. hparams.n_rot = hparams.n_embd_head_k;
  468. ml.get_key(LLM_KV_ROPE_DIMENSION_COUNT, hparams.n_rot, false);
  469. if (arch == LLM_ARCH_LLAMA || arch == LLM_ARCH_DECI || arch == LLM_ARCH_FALCON) {
  470. if (hparams.n_rot != hparams.n_embd_head_k) {
  471. throw std::runtime_error(format("invalid n_rot: %u, expected %u", hparams.n_rot, hparams.n_embd_head_k));
  472. }
  473. }
  474. } else {
  475. hparams.n_rot = 0;
  476. hparams.n_embd_head_k = 0;
  477. hparams.n_embd_head_v = 0;
  478. }
  479. // for differentiating model types
  480. uint32_t n_vocab = 0;
  481. ml.get_key(LLM_KV_VOCAB_SIZE, n_vocab, false) || ml.get_arr_n(LLM_KV_TOKENIZER_LIST, n_vocab, false);
  482. // for classifier models
  483. ml.get_arr(LLM_KV_CLASSIFIER_OUTPUT_LABELS, classifier_labels, false);
  484. if (!classifier_labels.empty()) {
  485. hparams.n_cls_out = classifier_labels.size();
  486. }
  487. // arch-specific KVs
  488. switch (arch) {
  489. case LLM_ARCH_LLAMA:
  490. {
  491. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  492. if (hparams.n_expert == 8) {
  493. switch (hparams.n_layer) {
  494. case 32: type = LLM_TYPE_8x7B; break;
  495. case 56: type = LLM_TYPE_8x22B; break;
  496. default: type = LLM_TYPE_UNKNOWN;
  497. }
  498. } else {
  499. switch (hparams.n_layer) {
  500. case 16: type = LLM_TYPE_1B; break; // Llama 3.2 1B
  501. case 22: type = LLM_TYPE_1B; break;
  502. case 26: type = LLM_TYPE_3B; break;
  503. case 28: type = LLM_TYPE_3B; break; // Llama 3.2 3B
  504. // granite uses a vocab with len 49152
  505. case 32: type = n_vocab == 49152 ? LLM_TYPE_3B : (n_vocab < 40000 ? LLM_TYPE_7B : LLM_TYPE_8B); break;
  506. case 36: type = LLM_TYPE_8B; break; // granite
  507. case 40: type = LLM_TYPE_13B; break;
  508. case 48: type = LLM_TYPE_34B; break;
  509. case 60: type = LLM_TYPE_30B; break;
  510. case 80: type = hparams.n_head() == hparams.n_head_kv() ? LLM_TYPE_65B : LLM_TYPE_70B; break;
  511. default: type = LLM_TYPE_UNKNOWN;
  512. }
  513. }
  514. } break;
  515. case LLM_ARCH_LLAMA4:
  516. {
  517. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  518. ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp);
  519. ml.get_key(LLM_KV_INTERLEAVE_MOE_LAYER_STEP, hparams.n_moe_layer_step);
  520. hparams.swa_type = LLAMA_SWA_TYPE_CHUNKED;
  521. hparams.n_swa = 8192; // should this be a gguf kv? currently it's the same for Scout and Maverick
  522. hparams.set_swa_pattern(4); // pattern: 3 chunked - 1 full
  523. switch (hparams.n_expert) {
  524. case 16: type = LLM_TYPE_17B_16E; break;
  525. case 128: type = LLM_TYPE_17B_128E; break;
  526. default: type = LLM_TYPE_UNKNOWN;
  527. }
  528. if (type == LLM_TYPE_17B_128E) {
  529. hparams.use_kq_norm = false;
  530. }
  531. } break;
  532. case LLM_ARCH_ARCEE:
  533. {
  534. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  535. // Arcee uses the same structure as Llama
  536. switch (hparams.n_layer) {
  537. case 36: type = LLM_TYPE_4B; break;
  538. default: type = LLM_TYPE_UNKNOWN;
  539. }
  540. } break;
  541. case LLM_ARCH_DECI:
  542. {
  543. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  544. switch (hparams.n_layer) {
  545. case 32: type = LLM_TYPE_7B; break;
  546. case 80: type = LLM_TYPE_70B; break;
  547. case 162: type = LLM_TYPE_405B; break;
  548. default: type = LLM_TYPE_UNKNOWN;
  549. }
  550. } break;
  551. case LLM_ARCH_MINICPM:
  552. {
  553. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  554. ml.get_key(LLM_KV_EMBEDDING_SCALE, hparams.f_embedding_scale);
  555. ml.get_key(LLM_KV_RESIDUAL_SCALE, hparams.f_residual_scale);
  556. ml.get_key(LLM_KV_LOGIT_SCALE, hparams.f_logit_scale);
  557. switch (hparams.n_layer) {
  558. case 52: type = LLM_TYPE_1B; break;
  559. case 40: type = LLM_TYPE_2B; break;
  560. default: type = LLM_TYPE_UNKNOWN;
  561. }
  562. } break;
  563. case LLM_ARCH_MINICPM3:
  564. {
  565. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  566. ml.get_key(LLM_KV_ATTENTION_Q_LORA_RANK, hparams.n_lora_q);
  567. ml.get_key(LLM_KV_ATTENTION_KV_LORA_RANK, hparams.n_lora_kv);
  568. switch (hparams.n_layer) {
  569. case 62: type = LLM_TYPE_4B; break;
  570. default: type = LLM_TYPE_UNKNOWN;
  571. }
  572. } break;
  573. case LLM_ARCH_GROK:
  574. {
  575. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  576. switch (hparams.n_layer) {
  577. case 64: type = LLM_TYPE_314B; break;
  578. default: type = LLM_TYPE_UNKNOWN;
  579. }
  580. } break;
  581. case LLM_ARCH_FALCON:
  582. {
  583. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  584. switch (hparams.n_layer) {
  585. case 32: type = LLM_TYPE_7B; break;
  586. case 60: type = LLM_TYPE_40B; break;
  587. default: type = LLM_TYPE_UNKNOWN;
  588. }
  589. } break;
  590. case LLM_ARCH_BAICHUAN:
  591. {
  592. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  593. switch (hparams.n_layer) {
  594. case 32: type = LLM_TYPE_7B; break;
  595. case 40: type = LLM_TYPE_13B; break;
  596. default: type = LLM_TYPE_UNKNOWN;
  597. }
  598. if (type == LLM_TYPE_13B) {
  599. // TODO: become GGUF KV parameter
  600. hparams.f_max_alibi_bias = 8.0f;
  601. }
  602. } break;
  603. case LLM_ARCH_STARCODER:
  604. {
  605. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  606. switch (hparams.n_layer) {
  607. case 24: type = LLM_TYPE_1B; break;
  608. case 36: type = LLM_TYPE_3B; break;
  609. case 42: type = LLM_TYPE_7B; break;
  610. case 40: type = LLM_TYPE_15B; break;
  611. default: type = LLM_TYPE_UNKNOWN;
  612. }
  613. } break;
  614. case LLM_ARCH_REFACT:
  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_1B; break;
  619. default: type = LLM_TYPE_UNKNOWN;
  620. }
  621. // TODO: become GGUF KV parameter
  622. hparams.f_max_alibi_bias = 8.0f;
  623. } break;
  624. case LLM_ARCH_BERT:
  625. {
  626. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  627. ml.get_key(LLM_KV_ATTENTION_CAUSAL, hparams.causal_attn);
  628. ml.get_key(LLM_KV_POOLING_TYPE, hparams.pooling_type, false);
  629. switch (hparams.n_layer) {
  630. case 3:
  631. type = LLM_TYPE_17M; break; // bge-micro
  632. case 6:
  633. type = LLM_TYPE_22M; break; // MiniLM-L6
  634. case 12:
  635. switch (hparams.n_embd) {
  636. case 384: type = LLM_TYPE_33M; break; // MiniLM-L12, bge-small
  637. case 768: type = LLM_TYPE_109M; break; // bge-base
  638. default: type = LLM_TYPE_UNKNOWN;
  639. } break;
  640. case 24:
  641. type = LLM_TYPE_335M; break; // bge-large
  642. default: type = LLM_TYPE_UNKNOWN;
  643. }
  644. } break;
  645. case LLM_ARCH_JINA_BERT_V2:
  646. {
  647. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  648. ml.get_key(LLM_KV_ATTENTION_CAUSAL, hparams.causal_attn);
  649. ml.get_key(LLM_KV_POOLING_TYPE, hparams.pooling_type, false);
  650. hparams.f_max_alibi_bias = 8.0f;
  651. switch (hparams.n_layer) {
  652. case 4: type = LLM_TYPE_33M; break; // jina-embeddings-small
  653. case 12: type = LLM_TYPE_137M; break; // jina-embeddings-base
  654. default: type = LLM_TYPE_UNKNOWN;
  655. }
  656. } break;
  657. case LLM_ARCH_NOMIC_BERT:
  658. case LLM_ARCH_NOMIC_BERT_MOE:
  659. {
  660. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  661. ml.get_key(LLM_KV_ATTENTION_CAUSAL, hparams.causal_attn);
  662. ml.get_key(LLM_KV_POOLING_TYPE, hparams.pooling_type);
  663. ml.get_key(LLM_KV_MOE_EVERY_N_LAYERS, hparams.moe_every_n_layers, 0);
  664. if (hparams.n_layer == 12 && hparams.n_embd == 768) {
  665. if (arch == LLM_ARCH_NOMIC_BERT) {
  666. type = LLM_TYPE_137M;
  667. } else if (arch == LLM_ARCH_NOMIC_BERT_MOE && hparams.moe_every_n_layers == 2) {
  668. type = LLM_TYPE_475M;
  669. }
  670. }
  671. } break;
  672. case LLM_ARCH_NEO_BERT:
  673. {
  674. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  675. ml.get_key(LLM_KV_ATTENTION_CAUSAL, hparams.causal_attn);
  676. ml.get_key(LLM_KV_POOLING_TYPE, hparams.pooling_type);
  677. if (hparams.n_layer == 28) {
  678. type = LLM_TYPE_250M;
  679. }
  680. } break;
  681. case LLM_ARCH_BLOOM:
  682. {
  683. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  684. switch (hparams.n_layer) {
  685. case 24: type = LLM_TYPE_1B; break;
  686. case 30:
  687. switch (hparams.n_embd) {
  688. case 2560: type = LLM_TYPE_3B; break;
  689. case 4096: type = LLM_TYPE_7B; break;
  690. default: type = LLM_TYPE_UNKNOWN;
  691. } break;
  692. default: type = LLM_TYPE_UNKNOWN;
  693. }
  694. // TODO: become GGUF KV parameter
  695. hparams.f_max_alibi_bias = 8.0f;
  696. } break;
  697. case LLM_ARCH_MPT:
  698. {
  699. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  700. ml.get_key(LLM_KV_ATTENTION_CLAMP_KQV, hparams.f_clamp_kqv, false);
  701. ml.get_key(LLM_KV_ATTENTION_MAX_ALIBI_BIAS, hparams.f_max_alibi_bias);
  702. switch (hparams.n_layer) {
  703. case 32: type = LLM_TYPE_7B; break;
  704. case 48: type = LLM_TYPE_30B; break;
  705. default: type = LLM_TYPE_UNKNOWN;
  706. }
  707. } break;
  708. case LLM_ARCH_STABLELM:
  709. {
  710. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  711. switch (hparams.n_layer) {
  712. case 24: type = LLM_TYPE_1B; break;
  713. case 32: type = LLM_TYPE_3B; break;
  714. case 40: type = LLM_TYPE_12B; break;
  715. default: type = LLM_TYPE_UNKNOWN;
  716. }
  717. } break;
  718. case LLM_ARCH_QWEN:
  719. {
  720. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  721. switch (hparams.n_layer) {
  722. case 32: type = LLM_TYPE_7B; break;
  723. case 40: type = LLM_TYPE_13B; break;
  724. default: type = LLM_TYPE_UNKNOWN;
  725. }
  726. } break;
  727. case LLM_ARCH_QWEN2VL:
  728. {
  729. ml.get_key_or_arr(LLM_KV_ROPE_DIMENSION_SECTIONS, hparams.rope_sections, 4, true);
  730. }
  731. // fall through
  732. case LLM_ARCH_QWEN2:
  733. {
  734. ml.get_key(LLM_KV_POOLING_TYPE, hparams.pooling_type, false);
  735. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  736. switch (hparams.n_layer) {
  737. case 24: type = hparams.n_embd == 1024 ? LLM_TYPE_0_5B : LLM_TYPE_1B; break;
  738. case 28: type = hparams.n_embd == 1536 ? LLM_TYPE_1_5B : LLM_TYPE_7B; break;
  739. case 32: type = LLM_TYPE_7B; break;
  740. case 36: type = LLM_TYPE_3B; break;
  741. case 40: type = hparams.n_head() == 20 ? LLM_TYPE_4B : LLM_TYPE_13B; break;
  742. case 48: type = LLM_TYPE_14B; break;
  743. case 64: type = LLM_TYPE_32B; break;
  744. case 80: type = LLM_TYPE_70B; break;
  745. default: type = LLM_TYPE_UNKNOWN;
  746. }
  747. } break;
  748. case LLM_ARCH_QWEN2MOE:
  749. {
  750. ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp, false);
  751. ml.get_key(LLM_KV_EXPERT_SHARED_FEED_FORWARD_LENGTH, hparams.n_ff_shexp, false);
  752. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  753. switch (hparams.n_layer) {
  754. case 24: type = LLM_TYPE_A2_7B; break;
  755. case 28: type = LLM_TYPE_57B_A14B; break;
  756. default: type = LLM_TYPE_UNKNOWN;
  757. }
  758. } break;
  759. case LLM_ARCH_QWEN3:
  760. {
  761. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  762. switch (hparams.n_layer) {
  763. case 28: type = hparams.n_embd == 1024 ? LLM_TYPE_0_6B : LLM_TYPE_1_7B; break;
  764. case 36: type = hparams.n_embd == 2560 ? LLM_TYPE_4B : LLM_TYPE_8B; break;
  765. case 40: type = LLM_TYPE_14B; break;
  766. case 64: type = LLM_TYPE_32B; break;
  767. default: type = LLM_TYPE_UNKNOWN;
  768. }
  769. } break;
  770. case LLM_ARCH_QWEN3MOE:
  771. {
  772. ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp, false);
  773. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  774. switch (hparams.n_layer) {
  775. case 48: type = LLM_TYPE_30B_A3B; break;
  776. case 94: type = LLM_TYPE_235B_A22B; break;
  777. default: type = LLM_TYPE_UNKNOWN;
  778. }
  779. } break;
  780. case LLM_ARCH_PHI2:
  781. {
  782. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  783. switch (hparams.n_layer) {
  784. case 24: type = LLM_TYPE_1B; break;
  785. case 32: type = LLM_TYPE_3B; break;
  786. default: type = LLM_TYPE_UNKNOWN;
  787. }
  788. } break;
  789. case LLM_ARCH_PHI3:
  790. {
  791. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  792. switch (hparams.n_layer) {
  793. case 24: type = LLM_TYPE_1B; break;
  794. case 32: type = LLM_TYPE_3B; break;
  795. case 40: type = LLM_TYPE_14B; break;
  796. default: type = LLM_TYPE_UNKNOWN;
  797. }
  798. const bool found_swa = ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa, false);
  799. if (found_swa && hparams.n_swa > 0) {
  800. LLAMA_LOG_WARN("%s: Phi SWA is currently disabled - results might be suboptimal for some models (see %s)\n",
  801. __func__, "https://github.com/ggml-org/llama.cpp/pull/13676");
  802. // TODO: fix conversion scripts to correctly populate `n_swa` and `n_swa_pattern`
  803. hparams.swa_type = LLAMA_SWA_TYPE_NONE;
  804. hparams.n_swa = 0;
  805. hparams.set_swa_pattern(1);
  806. }
  807. } break;
  808. case LLM_ARCH_PHIMOE:
  809. {
  810. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  811. switch (hparams.n_layer) {
  812. case 32: type = LLM_TYPE_16x3_8B; break;
  813. default: type = LLM_TYPE_UNKNOWN;
  814. }
  815. } break;
  816. case LLM_ARCH_PLAMO:
  817. {
  818. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  819. switch (hparams.n_layer) {
  820. case 40: type = LLM_TYPE_13B; break;
  821. default: type = LLM_TYPE_UNKNOWN;
  822. }
  823. } break;
  824. case LLM_ARCH_GPT2:
  825. {
  826. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  827. switch (hparams.n_layer) {
  828. case 12: type = LLM_TYPE_SMALL; break;
  829. case 24: type = LLM_TYPE_MEDIUM; break;
  830. case 36: type = LLM_TYPE_LARGE; break;
  831. case 48: type = LLM_TYPE_XL; break;
  832. default: type = LLM_TYPE_UNKNOWN;
  833. }
  834. } break;
  835. case LLM_ARCH_CODESHELL:
  836. {
  837. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  838. switch (hparams.n_layer) {
  839. case 42: type = LLM_TYPE_7B; break;
  840. default: type = LLM_TYPE_UNKNOWN;
  841. }
  842. } break;
  843. case LLM_ARCH_ORION:
  844. {
  845. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  846. switch (hparams.n_layer) {
  847. case 40: type = LLM_TYPE_14B; break;
  848. default: type = LLM_TYPE_UNKNOWN;
  849. }
  850. } break;
  851. case LLM_ARCH_INTERNLM2:
  852. {
  853. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  854. switch (hparams.n_layer) {
  855. case 32: type = LLM_TYPE_7B; break;
  856. case 48: type = LLM_TYPE_20B; break;
  857. default: type = LLM_TYPE_UNKNOWN;
  858. }
  859. } break;
  860. case LLM_ARCH_GEMMA:
  861. {
  862. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  863. switch (hparams.n_layer) {
  864. case 18: type = LLM_TYPE_2B; break;
  865. case 28: type = LLM_TYPE_7B; break;
  866. default: type = LLM_TYPE_UNKNOWN;
  867. }
  868. } break;
  869. case LLM_ARCH_GEMMA2:
  870. {
  871. hparams.swa_type = LLAMA_SWA_TYPE_STANDARD;
  872. hparams.n_swa = 4096; // default value of gemma 2
  873. hparams.set_swa_pattern(2);
  874. hparams.attn_soft_cap = true;
  875. ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa, false);
  876. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  877. ml.get_key(LLM_KV_ATTN_LOGIT_SOFTCAPPING, hparams.f_attn_logit_softcapping, false);
  878. ml.get_key(LLM_KV_FINAL_LOGIT_SOFTCAPPING, hparams.f_final_logit_softcapping, false);
  879. switch (hparams.n_layer) {
  880. case 26: type = LLM_TYPE_2B; break;
  881. case 42: type = LLM_TYPE_9B; break;
  882. case 46: type = LLM_TYPE_27B; break;
  883. default: type = LLM_TYPE_UNKNOWN;
  884. }
  885. // ref: https://github.com/google/gemma_pytorch/blob/014acb7ac4563a5f77c76d7ff98f31b568c16508/gemma/config.py#L173
  886. hparams.f_attention_scale = type == LLM_TYPE_27B
  887. ? 1.0f / std::sqrt(float(hparams.n_embd / hparams.n_head(0)))
  888. : 1.0f / std::sqrt(float(hparams.n_embd_head_k));
  889. } break;
  890. case LLM_ARCH_GEMMA3:
  891. {
  892. hparams.swa_type = LLAMA_SWA_TYPE_STANDARD;
  893. hparams.set_swa_pattern(6);
  894. hparams.rope_freq_base_train_swa = 10000.0f;
  895. hparams.rope_freq_scale_train_swa = 1.0f;
  896. ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa);
  897. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  898. switch (hparams.n_layer) {
  899. case 26: type = LLM_TYPE_1B; break;
  900. case 34: type = LLM_TYPE_4B; break;
  901. case 48: type = LLM_TYPE_12B; break;
  902. case 62: type = LLM_TYPE_27B; break;
  903. default: type = LLM_TYPE_UNKNOWN;
  904. }
  905. // ref: https://github.com/google/gemma_pytorch/blob/014acb7ac4563a5f77c76d7ff98f31b568c16508/gemma/config.py#L289
  906. hparams.f_attention_scale = type == LLM_TYPE_27B
  907. ? 1.0f / std::sqrt(float(hparams.n_embd / hparams.n_head(0)))
  908. : 1.0f / std::sqrt(float(hparams.n_embd_head_k));
  909. } break;
  910. case LLM_ARCH_GEMMA3N:
  911. {
  912. hparams.swa_type = LLAMA_SWA_TYPE_STANDARD;
  913. hparams.set_swa_pattern(5);
  914. hparams.rope_freq_base_train_swa = 10000.0f;
  915. hparams.rope_freq_scale_train_swa = 1.0f;
  916. hparams.f_attention_scale = 1.0f;
  917. ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa);
  918. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  919. switch (hparams.n_layer) {
  920. case 30: type = LLM_TYPE_E2B; break;
  921. case 35: type = LLM_TYPE_E4B; break;
  922. default: type = LLM_TYPE_UNKNOWN;
  923. }
  924. } break;
  925. case LLM_ARCH_STARCODER2:
  926. {
  927. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  928. switch (hparams.n_layer) {
  929. case 30: type = LLM_TYPE_3B; break;
  930. case 32: type = LLM_TYPE_7B; break;
  931. case 40: type = LLM_TYPE_15B; break;
  932. case 52: type = LLM_TYPE_20B; break; // granite
  933. case 88: type = LLM_TYPE_34B; break; // granite
  934. default: type = LLM_TYPE_UNKNOWN;
  935. }
  936. } break;
  937. case LLM_ARCH_MAMBA:
  938. {
  939. ml.get_key(LLM_KV_SSM_CONV_KERNEL, hparams.ssm_d_conv);
  940. ml.get_key(LLM_KV_SSM_INNER_SIZE, hparams.ssm_d_inner);
  941. ml.get_key(LLM_KV_SSM_STATE_SIZE, hparams.ssm_d_state);
  942. ml.get_key(LLM_KV_SSM_TIME_STEP_RANK, hparams.ssm_dt_rank);
  943. ml.get_key(LLM_KV_SSM_DT_B_C_RMS, hparams.ssm_dt_b_c_rms, false);
  944. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  945. switch (hparams.n_layer) {
  946. case 24:
  947. switch (hparams.n_embd) {
  948. case 768: type = LLM_TYPE_SMALL; break;
  949. default: type = LLM_TYPE_UNKNOWN;
  950. } break;
  951. case 48:
  952. switch (hparams.n_embd) {
  953. case 1024: type = LLM_TYPE_MEDIUM; break;
  954. case 1536: type = LLM_TYPE_LARGE; break;
  955. case 2048: type = LLM_TYPE_XL; break;
  956. default: type = LLM_TYPE_UNKNOWN;
  957. } break;
  958. case 64:
  959. switch (hparams.n_embd) {
  960. case 2560: type = LLM_TYPE_3B; break;
  961. default: type = LLM_TYPE_UNKNOWN;
  962. } break;
  963. default: type = LLM_TYPE_UNKNOWN;
  964. }
  965. } break;
  966. case LLM_ARCH_MAMBA2:
  967. {
  968. ml.get_key(LLM_KV_SSM_CONV_KERNEL, hparams.ssm_d_conv);
  969. ml.get_key(LLM_KV_SSM_INNER_SIZE, hparams.ssm_d_inner);
  970. ml.get_key(LLM_KV_SSM_STATE_SIZE, hparams.ssm_d_state);
  971. ml.get_key(LLM_KV_SSM_TIME_STEP_RANK, hparams.ssm_dt_rank);
  972. ml.get_key(LLM_KV_SSM_GROUP_COUNT, hparams.ssm_n_group);
  973. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  974. switch (hparams.n_layer) {
  975. case 24:
  976. switch (hparams.n_embd) {
  977. case 768: type = LLM_TYPE_SMALL; break;
  978. default: type = LLM_TYPE_UNKNOWN;
  979. } break;
  980. case 48:
  981. switch (hparams.n_embd) {
  982. case 1024: type = LLM_TYPE_MEDIUM; break;
  983. case 1536: type = LLM_TYPE_LARGE; break;
  984. case 2048: type = LLM_TYPE_XL; break;
  985. default: type = LLM_TYPE_UNKNOWN;
  986. } break;
  987. case 64:
  988. switch (hparams.n_embd) {
  989. case 2560: type = LLM_TYPE_3B; break;
  990. case 4096: type = LLM_TYPE_7B; break;
  991. default: type = LLM_TYPE_UNKNOWN;
  992. } break;
  993. default: type = LLM_TYPE_UNKNOWN;
  994. }
  995. } break;
  996. case LLM_ARCH_JAMBA:
  997. {
  998. ml.get_key(LLM_KV_SSM_CONV_KERNEL, hparams.ssm_d_conv);
  999. ml.get_key(LLM_KV_SSM_INNER_SIZE, hparams.ssm_d_inner);
  1000. ml.get_key(LLM_KV_SSM_STATE_SIZE, hparams.ssm_d_state);
  1001. ml.get_key(LLM_KV_SSM_TIME_STEP_RANK, hparams.ssm_dt_rank);
  1002. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1003. for (uint32_t i = 0; i < hparams.n_layer; ++i) {
  1004. hparams.recurrent_layer_arr[i] = hparams.n_head_kv(i) == 0;
  1005. }
  1006. switch (hparams.n_layer) {
  1007. // TODO: Jamba layers are a bit heterogenous, so naming this is hard.
  1008. case 12: // 900M 8x???M
  1009. case 32: // 51B 16x?B
  1010. default: type = LLM_TYPE_UNKNOWN;
  1011. }
  1012. } break;
  1013. case LLM_ARCH_XVERSE:
  1014. {
  1015. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1016. switch (hparams.n_layer) {
  1017. case 32: type = LLM_TYPE_7B; break;
  1018. case 40: type = LLM_TYPE_13B; break;
  1019. case 80: type = LLM_TYPE_65B; break;
  1020. default: type = LLM_TYPE_UNKNOWN;
  1021. }
  1022. } break;
  1023. case LLM_ARCH_COMMAND_R:
  1024. {
  1025. ml.get_key(LLM_KV_LOGIT_SCALE, hparams.f_logit_scale);
  1026. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  1027. switch (hparams.n_layer) {
  1028. case 40: type = LLM_TYPE_35B; break;
  1029. default: type = LLM_TYPE_UNKNOWN;
  1030. }
  1031. } break;
  1032. case LLM_ARCH_COHERE2:
  1033. {
  1034. hparams.swa_type = LLAMA_SWA_TYPE_STANDARD;
  1035. hparams.set_swa_pattern(4);
  1036. ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa);
  1037. ml.get_key(LLM_KV_LOGIT_SCALE, hparams.f_logit_scale);
  1038. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  1039. switch (hparams.n_layer) {
  1040. case 32: type = LLM_TYPE_8B; break;
  1041. default: type = LLM_TYPE_UNKNOWN;
  1042. }
  1043. } break;
  1044. case LLM_ARCH_DBRX:
  1045. {
  1046. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  1047. ml.get_key(LLM_KV_ATTENTION_CLAMP_KQV, hparams.f_clamp_kqv);
  1048. switch (hparams.n_layer) {
  1049. case 40: type = LLM_TYPE_16x12B; break;
  1050. default: type = LLM_TYPE_UNKNOWN;
  1051. }
  1052. } break;
  1053. case LLM_ARCH_OLMO:
  1054. {
  1055. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  1056. ml.get_key(LLM_KV_ATTENTION_CLAMP_KQV, hparams.f_clamp_kqv, false);
  1057. switch (hparams.n_layer) {
  1058. case 22: type = LLM_TYPE_1B; break;
  1059. case 32: type = LLM_TYPE_7B; break;
  1060. case 80: type = LLM_TYPE_70B; break;
  1061. default: type = LLM_TYPE_UNKNOWN;
  1062. }
  1063. } break;
  1064. case LLM_ARCH_OLMO2:
  1065. {
  1066. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1067. switch (hparams.n_layer) {
  1068. case 16: type = LLM_TYPE_1B; break;
  1069. case 32: type = LLM_TYPE_7B; break;
  1070. case 40: type = LLM_TYPE_13B; break;
  1071. case 64: type = LLM_TYPE_32B; break;
  1072. default: type = LLM_TYPE_UNKNOWN;
  1073. }
  1074. } break;
  1075. case LLM_ARCH_OLMOE:
  1076. {
  1077. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1078. switch (hparams.n_layer) {
  1079. case 16: type = LLM_TYPE_A1_7B; break;
  1080. default: type = LLM_TYPE_UNKNOWN;
  1081. }
  1082. } break;
  1083. case LLM_ARCH_OPENELM:
  1084. {
  1085. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1086. switch (hparams.n_layer) {
  1087. case 16: type = LLM_TYPE_270M; break;
  1088. case 20: type = LLM_TYPE_450M; break;
  1089. case 28: type = LLM_TYPE_1B; break;
  1090. case 36: type = LLM_TYPE_3B; break;
  1091. default: type = LLM_TYPE_UNKNOWN;
  1092. }
  1093. } break;
  1094. case LLM_ARCH_GPTNEOX:
  1095. {
  1096. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  1097. ml.get_key(LLM_KV_USE_PARALLEL_RESIDUAL, hparams.use_par_res);
  1098. switch (hparams.n_layer) {
  1099. case 6:
  1100. switch (hparams.n_ff()) {
  1101. case 512: type = LLM_TYPE_14M; break;
  1102. case 2048: type = LLM_TYPE_70M; break;
  1103. default: type = LLM_TYPE_UNKNOWN;
  1104. } break;
  1105. case 12:
  1106. switch (hparams.n_ff()) {
  1107. case 3072: type = LLM_TYPE_160M; break;
  1108. default: type = LLM_TYPE_UNKNOWN;
  1109. } break;
  1110. case 16:
  1111. switch (hparams.n_ff()) {
  1112. case 8192: type = LLM_TYPE_1B; break;
  1113. default: type = LLM_TYPE_UNKNOWN;
  1114. } break;
  1115. case 24:
  1116. switch (hparams.n_ff()) {
  1117. case 4096: type = LLM_TYPE_410M; break;
  1118. case 8192: type = LLM_TYPE_1_4B; break;
  1119. default: type = LLM_TYPE_UNKNOWN;
  1120. } break;
  1121. case 32:
  1122. switch (hparams.n_ff()) {
  1123. case 10240: type = LLM_TYPE_2_8B; break;
  1124. case 16384: type = LLM_TYPE_6_9B; break;
  1125. default: type = LLM_TYPE_UNKNOWN;
  1126. } break;
  1127. case 36:
  1128. switch (hparams.n_ff()) {
  1129. case 20480: type = LLM_TYPE_12B; break;
  1130. default: type = LLM_TYPE_UNKNOWN;
  1131. } break;
  1132. case 44:
  1133. switch (hparams.n_ff()) {
  1134. case 24576: type = LLM_TYPE_20B; break;
  1135. default: type = LLM_TYPE_UNKNOWN;
  1136. } break;
  1137. default: type = LLM_TYPE_UNKNOWN;
  1138. }
  1139. } break;
  1140. case LLM_ARCH_ARCTIC:
  1141. {
  1142. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1143. if (hparams.n_expert == 128) {
  1144. switch (hparams.n_layer) {
  1145. case 35: type = LLM_TYPE_10B_128x3_66B; break;
  1146. default: type = LLM_TYPE_UNKNOWN;
  1147. }
  1148. } else {
  1149. type = LLM_TYPE_UNKNOWN;
  1150. }
  1151. } break;
  1152. case LLM_ARCH_DEEPSEEK:
  1153. {
  1154. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1155. ml.get_key(LLM_KV_LEADING_DENSE_BLOCK_COUNT, hparams.n_layer_dense_lead);
  1156. ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp);
  1157. ml.get_key(LLM_KV_EXPERT_SHARED_COUNT, hparams.n_expert_shared);
  1158. ml.get_key(LLM_KV_EXPERT_WEIGHTS_SCALE, hparams.expert_weights_scale);
  1159. switch (hparams.n_layer) {
  1160. case 28: type = LLM_TYPE_20B; break;
  1161. default: type = LLM_TYPE_UNKNOWN;
  1162. }
  1163. } break;
  1164. case LLM_ARCH_DEEPSEEK2:
  1165. {
  1166. bool is_lite = (hparams.n_layer == 27);
  1167. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1168. ml.get_key(LLM_KV_LEADING_DENSE_BLOCK_COUNT, hparams.n_layer_dense_lead);
  1169. if (!is_lite) {
  1170. ml.get_key(LLM_KV_ATTENTION_Q_LORA_RANK, hparams.n_lora_q);
  1171. }
  1172. ml.get_key(LLM_KV_ATTENTION_KV_LORA_RANK, hparams.n_lora_kv);
  1173. ml.get_key(LLM_KV_ATTENTION_KEY_LENGTH_MLA, hparams.n_embd_head_k_mla, false);
  1174. ml.get_key(LLM_KV_ATTENTION_VALUE_LENGTH_MLA, hparams.n_embd_head_v_mla, false);
  1175. ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp);
  1176. ml.get_key(LLM_KV_EXPERT_SHARED_COUNT, hparams.n_expert_shared);
  1177. ml.get_key(LLM_KV_EXPERT_WEIGHTS_SCALE, hparams.expert_weights_scale);
  1178. ml.get_key(LLM_KV_EXPERT_WEIGHTS_NORM, hparams.expert_weights_norm, false);
  1179. ml.get_key(LLM_KV_EXPERT_GATING_FUNC, hparams.expert_gating_func, false);
  1180. if (hparams.expert_gating_func == LLAMA_EXPERT_GATING_FUNC_TYPE_NONE) {
  1181. // for compatibility with existing DeepSeek V2 and V2.5 GGUFs
  1182. // that have no expert_gating_func model parameter set
  1183. hparams.expert_gating_func = LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX;
  1184. }
  1185. ml.get_key(LLM_KV_ROPE_SCALING_YARN_LOG_MUL, hparams.rope_yarn_log_mul);
  1186. switch (hparams.n_layer) {
  1187. case 27: type = LLM_TYPE_16B; break;
  1188. case 60: type = LLM_TYPE_236B; break;
  1189. case 61: type = LLM_TYPE_671B; break;
  1190. default: type = LLM_TYPE_UNKNOWN;
  1191. }
  1192. } break;
  1193. case LLM_ARCH_PLM:
  1194. {
  1195. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1196. ml.get_key(LLM_KV_ATTENTION_KV_LORA_RANK, hparams.n_lora_kv);
  1197. switch (hparams.n_layer) {
  1198. case 32: type = LLM_TYPE_1_8B; break;
  1199. default: type = LLM_TYPE_UNKNOWN;
  1200. }
  1201. } break;
  1202. case LLM_ARCH_CHATGLM:
  1203. {
  1204. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1205. switch (hparams.n_layer) {
  1206. case 28: {
  1207. if (hparams.n_head(0) == 16) {
  1208. type = LLM_TYPE_1_5B;
  1209. } else {
  1210. type = LLM_TYPE_6B;
  1211. }
  1212. } break;
  1213. case 40: {
  1214. if (hparams.n_head(0) == 24) {
  1215. type = LLM_TYPE_4B;
  1216. } else {
  1217. type = LLM_TYPE_9B;
  1218. }
  1219. } break;
  1220. default: type = LLM_TYPE_UNKNOWN;
  1221. }
  1222. } break;
  1223. case LLM_ARCH_GLM4:
  1224. {
  1225. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1226. switch (hparams.n_layer) {
  1227. case 40: type = LLM_TYPE_9B; break;
  1228. case 61: type = LLM_TYPE_32B; break;
  1229. default: type = LLM_TYPE_UNKNOWN;
  1230. }
  1231. } break;
  1232. case LLM_ARCH_BITNET:
  1233. {
  1234. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1235. switch (hparams.n_layer) {
  1236. case 26: type = LLM_TYPE_3B; break;
  1237. default: type = LLM_TYPE_UNKNOWN;
  1238. }
  1239. } break;
  1240. case LLM_ARCH_T5:
  1241. {
  1242. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1243. ml.get_key(LLM_KV_ATTENTION_RELATIVE_BUCKETS_COUNT, hparams.n_rel_attn_bkts);
  1244. uint32_t dec_start_token_id;
  1245. if (ml.get_key(LLM_KV_DECODER_START_TOKEN_ID, dec_start_token_id, false)) {
  1246. hparams.dec_start_token_id = dec_start_token_id;
  1247. }
  1248. switch (hparams.n_layer) {
  1249. case 6: type = LLM_TYPE_60M; break; // t5-small
  1250. case 8: type = LLM_TYPE_80M; break; // flan-t5-small
  1251. case 12:
  1252. switch (hparams.n_ff()) {
  1253. case 3072: type = LLM_TYPE_220M; break; // t5-base
  1254. case 2048: type = LLM_TYPE_250M; break; // flan-t5-base
  1255. default: type = LLM_TYPE_UNKNOWN;
  1256. } break;
  1257. case 24:
  1258. switch (hparams.n_ff()) {
  1259. case 4096: type = LLM_TYPE_770M; break; // t5-large
  1260. case 2816: type = LLM_TYPE_780M; break; // flan-t5-large
  1261. case 16384: type = LLM_TYPE_3B; break; // t5-3b
  1262. case 5120: type = LLM_TYPE_3B; break; // flan-t5-xl
  1263. case 65536: type = LLM_TYPE_11B; break; // t5-11b
  1264. case 10240: type = LLM_TYPE_11B; break; // flan-t5-xxl
  1265. default: type = LLM_TYPE_UNKNOWN;
  1266. } break;
  1267. default: type = LLM_TYPE_UNKNOWN;
  1268. }
  1269. } break;
  1270. case LLM_ARCH_T5ENCODER:
  1271. {
  1272. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1273. ml.get_key(LLM_KV_ATTENTION_RELATIVE_BUCKETS_COUNT, hparams.n_rel_attn_bkts);
  1274. type = LLM_TYPE_UNKNOWN;
  1275. } break;
  1276. case LLM_ARCH_JAIS:
  1277. {
  1278. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  1279. ml.get_key(LLM_KV_ATTENTION_MAX_ALIBI_BIAS, hparams.f_max_alibi_bias);
  1280. switch (hparams.n_layer) {
  1281. case 24: type = LLM_TYPE_1_3B; break;
  1282. case 40: type = LLM_TYPE_13B; break;
  1283. /* TODO: add variants */
  1284. default: type = LLM_TYPE_UNKNOWN;
  1285. }
  1286. } break;
  1287. case LLM_ARCH_NEMOTRON:
  1288. {
  1289. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  1290. switch (hparams.n_layer) {
  1291. case 32: type = LLM_TYPE_4B; break;
  1292. default: type = LLM_TYPE_UNKNOWN;
  1293. }
  1294. } break;
  1295. case LLM_ARCH_EXAONE:
  1296. {
  1297. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1298. switch (hparams.n_layer) {
  1299. case 32: type = LLM_TYPE_8B; break;
  1300. default: type = LLM_TYPE_UNKNOWN;
  1301. }
  1302. } break;
  1303. case LLM_ARCH_RWKV6:
  1304. case LLM_ARCH_RWKV6QWEN2:
  1305. {
  1306. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps, false);
  1307. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps, false);
  1308. ml.get_key(LLM_KV_WKV_HEAD_SIZE, hparams.wkv_head_size);
  1309. ml.get_key(LLM_KV_TIME_MIX_EXTRA_DIM, hparams.time_mix_extra_dim);
  1310. ml.get_key(LLM_KV_TIME_DECAY_EXTRA_DIM, hparams.time_decay_extra_dim);
  1311. ml.get_key(LLM_KV_RESCALE_EVERY_N_LAYERS, hparams.rescale_every_n_layers, false);
  1312. ml.get_key(LLM_KV_TOKEN_SHIFT_COUNT, hparams.token_shift_count, false);
  1313. switch (hparams.n_layer) {
  1314. case 24: type = LLM_TYPE_1_6B; break;
  1315. case 32:
  1316. switch (hparams.n_embd) {
  1317. case 2560: type = LLM_TYPE_3B; break;
  1318. case 4096: type = LLM_TYPE_7B; break;
  1319. default: type = LLM_TYPE_UNKNOWN;
  1320. } break;
  1321. case 61: type = LLM_TYPE_14B; break;
  1322. case 64: type = LLM_TYPE_32B; break;
  1323. default: type = LLM_TYPE_UNKNOWN;
  1324. }
  1325. } break;
  1326. case LLM_ARCH_RWKV7:
  1327. case LLM_ARCH_ARWKV7:
  1328. {
  1329. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps, false);
  1330. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps, false);
  1331. ml.get_key(LLM_KV_WKV_HEAD_SIZE, hparams.wkv_head_size);
  1332. ml.get_key(LLM_KV_ATTENTION_DECAY_LORA_RANK, hparams.n_lora_decay);
  1333. ml.get_key(LLM_KV_ATTENTION_ICLR_LORA_RANK, hparams.n_lora_iclr);
  1334. ml.get_key(LLM_KV_ATTENTION_VALUE_RESIDUAL_MIX_LORA_RANK, hparams.n_lora_value_res_mix);
  1335. ml.get_key(LLM_KV_ATTENTION_GATE_LORA_RANK, hparams.n_lora_gate, false);
  1336. ml.get_key(LLM_KV_TOKEN_SHIFT_COUNT, hparams.token_shift_count, false);
  1337. switch (hparams.n_layer) {
  1338. case 12: type = LLM_TYPE_190M; break;
  1339. case 24:
  1340. switch (hparams.n_embd) {
  1341. case 1024: type = LLM_TYPE_450M; break;
  1342. case 2048: type = LLM_TYPE_1_5B; break;
  1343. default: type = LLM_TYPE_UNKNOWN;
  1344. } break;
  1345. case 28:
  1346. switch (hparams.n_embd) {
  1347. case 1536: type = LLM_TYPE_1_5B; break;
  1348. case 3584: type = LLM_TYPE_7B; break;
  1349. default: type = LLM_TYPE_UNKNOWN;
  1350. } break;
  1351. case 32: type = LLM_TYPE_2_9B; break; // RWKV-7-World
  1352. default: type = LLM_TYPE_UNKNOWN;
  1353. }
  1354. } break;
  1355. case LLM_ARCH_GRANITE:
  1356. case LLM_ARCH_GRANITE_MOE:
  1357. {
  1358. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1359. ml.get_key(LLM_KV_LOGIT_SCALE, hparams.f_logit_scale);
  1360. ml.get_key(LLM_KV_RESIDUAL_SCALE, hparams.f_residual_scale);
  1361. ml.get_key(LLM_KV_EMBEDDING_SCALE, hparams.f_embedding_scale);
  1362. ml.get_key(LLM_KV_ATTENTION_SCALE, hparams.f_attention_scale);
  1363. switch (hparams.n_layer) {
  1364. case 32: type = LLM_TYPE_3B; break;
  1365. case 40: type = LLM_TYPE_3B; break;
  1366. // Add additional layer/vocab/etc checks here for other model sizes
  1367. default: type = LLM_TYPE_UNKNOWN;
  1368. }
  1369. // For Granite MoE Shared
  1370. ml.get_key(LLM_KV_EXPERT_SHARED_FEED_FORWARD_LENGTH, hparams.n_ff_shexp, /* required */ false);
  1371. } break;
  1372. case LLM_ARCH_CHAMELEON:
  1373. {
  1374. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1375. hparams.f_norm_eps = 1e-5; // eps for qk-norm, torch default
  1376. ml.get_key(LLM_KV_SWIN_NORM, hparams.swin_norm);
  1377. switch (hparams.n_layer) {
  1378. case 32: type = LLM_TYPE_7B; break;
  1379. case 48: type = LLM_TYPE_34B; break;
  1380. default: type = LLM_TYPE_UNKNOWN;
  1381. }
  1382. } break;
  1383. case LLM_ARCH_WAVTOKENIZER_DEC:
  1384. {
  1385. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
  1386. ml.get_key(LLM_KV_ATTENTION_GROUPNORM_EPS, hparams.f_norm_group_eps);
  1387. ml.get_key(LLM_KV_ATTENTION_GROUPNORM_GROUPS, hparams.n_norm_groups);
  1388. ml.get_key(LLM_KV_ATTENTION_CAUSAL, hparams.causal_attn);
  1389. } break;
  1390. case LLM_ARCH_BAILINGMOE:
  1391. {
  1392. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1393. ml.get_key(LLM_KV_LEADING_DENSE_BLOCK_COUNT, hparams.n_layer_dense_lead);
  1394. ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp);
  1395. ml.get_key(LLM_KV_EXPERT_SHARED_COUNT, hparams.n_expert_shared);
  1396. ml.get_key(LLM_KV_EXPERT_WEIGHTS_SCALE, hparams.expert_weights_scale);
  1397. ml.get_key(LLM_KV_EXPERT_WEIGHTS_NORM, hparams.expert_weights_norm, false);
  1398. switch (hparams.n_layer) {
  1399. case 28: type = LLM_TYPE_16B; break;
  1400. case 88: type = LLM_TYPE_290B; break;
  1401. default: type = LLM_TYPE_UNKNOWN;
  1402. }
  1403. } break;
  1404. case LLM_ARCH_DOTS1:
  1405. {
  1406. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1407. ml.get_key(LLM_KV_LEADING_DENSE_BLOCK_COUNT, hparams.n_layer_dense_lead);
  1408. ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp);
  1409. ml.get_key(LLM_KV_EXPERT_SHARED_COUNT, hparams.n_expert_shared);
  1410. ml.get_key(LLM_KV_EXPERT_WEIGHTS_SCALE, hparams.expert_weights_scale);
  1411. ml.get_key(LLM_KV_EXPERT_WEIGHTS_NORM, hparams.expert_weights_norm, false);
  1412. ml.get_key(LLM_KV_EXPERT_GATING_FUNC, hparams.expert_gating_func, false);
  1413. switch (hparams.n_layer) {
  1414. case 62: type = LLM_TYPE_142B; break;
  1415. default: type = LLM_TYPE_UNKNOWN;
  1416. }
  1417. } break;
  1418. case LLM_ARCH_ERNIE4_5:
  1419. {
  1420. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1421. switch (hparams.n_layer) {
  1422. case 18: type = LLM_TYPE_0_3B; break;
  1423. default: type = LLM_TYPE_UNKNOWN;
  1424. }
  1425. } break;
  1426. case LLM_ARCH_FALCON_H1:
  1427. {
  1428. // Common parameters
  1429. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1430. // SSM parameters
  1431. ml.get_key(LLM_KV_SSM_CONV_KERNEL, hparams.ssm_d_conv);
  1432. ml.get_key(LLM_KV_SSM_INNER_SIZE, hparams.ssm_d_inner);
  1433. ml.get_key(LLM_KV_SSM_STATE_SIZE, hparams.ssm_d_state);
  1434. ml.get_key(LLM_KV_SSM_TIME_STEP_RANK, hparams.ssm_dt_rank);
  1435. ml.get_key(LLM_KV_SSM_GROUP_COUNT, hparams.ssm_n_group);
  1436. std::fill(hparams.recurrent_layer_arr.begin(), hparams.recurrent_layer_arr.end(), true);
  1437. switch (hparams.n_layer) {
  1438. case 36:
  1439. type = LLM_TYPE_0_5B; break;
  1440. case 24:
  1441. type = LLM_TYPE_1_5B; break;
  1442. case 66:
  1443. type = LLM_TYPE_1B; break;
  1444. case 32:
  1445. type = LLM_TYPE_3B; break;
  1446. case 44:
  1447. type = LLM_TYPE_7B; break;
  1448. case 72:
  1449. type = LLM_TYPE_34B; break;
  1450. default:
  1451. type = LLM_TYPE_UNKNOWN;
  1452. }
  1453. } break;
  1454. case LLM_ARCH_HUNYUAN_MOE:
  1455. {
  1456. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1457. ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp);
  1458. ml.get_key(LLM_KV_EXPERT_SHARED_FEED_FORWARD_LENGTH, hparams.n_ff_shexp);
  1459. switch (hparams.n_layer) {
  1460. case 32: type = LLM_TYPE_A13B; break;
  1461. default: type = LLM_TYPE_UNKNOWN;
  1462. }
  1463. } break;
  1464. case LLM_ARCH_SMOLLM3:
  1465. {
  1466. ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
  1467. hparams.n_no_rope_layer_step = 4;
  1468. switch (hparams.n_layer) {
  1469. case 36: type = LLM_TYPE_3B; break;
  1470. default: type = LLM_TYPE_UNKNOWN;
  1471. }
  1472. } break;
  1473. default: throw std::runtime_error("unsupported model architecture");
  1474. }
  1475. pimpl->n_bytes = ml.n_bytes;
  1476. pimpl->desc_str = arch_name() + " " + type_name() + " " + ml.ftype_name();
  1477. if (hparams.f_max_alibi_bias > 0.0f) {
  1478. hparams.use_alibi = true;
  1479. }
  1480. hparams.rope_type = llama_model_rope_type(this);
  1481. }
  1482. void llama_model::load_vocab(llama_model_loader & ml) {
  1483. const auto kv = LLM_KV(arch);
  1484. vocab.load(ml, kv);
  1485. }
  1486. bool llama_model::load_tensors(llama_model_loader & ml) {
  1487. const auto & split_mode = params.split_mode;
  1488. const auto & n_gpu_layers = params.n_gpu_layers;
  1489. const auto & use_mlock = params.use_mlock;
  1490. const auto & tensor_split = params.tensor_split;
  1491. const int n_layer = hparams.n_layer;
  1492. const bool use_mmap_buffer = true;
  1493. LLAMA_LOG_INFO("%s: loading model tensors, this can take a while... (mmap = %s)\n", __func__, ml.use_mmap ? "true" : "false");
  1494. // build a list of buffer types for the CPU and GPU devices
  1495. pimpl->cpu_buft_list = make_cpu_buft_list(devices);
  1496. for (auto * dev : devices) {
  1497. buft_list_t buft_list = make_gpu_buft_list(dev, split_mode, tensor_split);
  1498. // add CPU buffer types as a fallback
  1499. buft_list.insert(buft_list.end(), pimpl->cpu_buft_list.begin(), pimpl->cpu_buft_list.end());
  1500. pimpl->gpu_buft_list.emplace(dev, std::move(buft_list));
  1501. }
  1502. // calculate the split points
  1503. bool all_zero = tensor_split == nullptr || std::all_of(tensor_split, tensor_split + n_devices(), [](float x) { return x == 0.0f; });
  1504. std::vector<float> splits(n_devices());
  1505. if (all_zero) {
  1506. // default split, by free memory
  1507. for (size_t i = 0; i < n_devices(); ++i) {
  1508. ggml_backend_dev_t dev = devices[i];
  1509. size_t total;
  1510. size_t free;
  1511. ggml_backend_dev_memory(dev, &free, &total);
  1512. splits[i] = free;
  1513. }
  1514. } else {
  1515. std::copy(tensor_split, tensor_split + n_devices(), splits.begin());
  1516. }
  1517. // sum and normalize the splits to get the split points
  1518. float split_sum = 0.0f;
  1519. for (size_t i = 0; i < n_devices(); ++i) {
  1520. split_sum += splits[i];
  1521. splits[i] = split_sum;
  1522. }
  1523. for (size_t i = 0; i < n_devices(); ++i) {
  1524. splits[i] /= split_sum;
  1525. }
  1526. ggml_backend_dev_t cpu_dev = ggml_backend_dev_by_type(GGML_BACKEND_DEVICE_TYPE_CPU);
  1527. if (cpu_dev == nullptr) {
  1528. throw std::runtime_error(format("%s: no CPU backend found", __func__));
  1529. }
  1530. const int i_gpu_start = std::max((int) hparams.n_layer - n_gpu_layers, (int) 0);
  1531. const int act_gpu_layers = devices.empty() ? 0 : std::min(n_gpu_layers, (int)n_layer + 1);
  1532. auto get_layer_buft_list = [&](int il) -> llama_model::impl::layer_dev {
  1533. const bool is_swa = il < (int) hparams.n_layer && hparams.is_swa(il);
  1534. if (il < i_gpu_start || (il - i_gpu_start) >= act_gpu_layers) {
  1535. LLAMA_LOG_DEBUG("load_tensors: layer %3d assigned to device %s, is_swa = %d\n", il, ggml_backend_dev_name(cpu_dev), is_swa);
  1536. return {cpu_dev, &pimpl->cpu_buft_list};
  1537. }
  1538. const int layer_gpu = std::upper_bound(splits.begin(), splits.begin() + n_devices(), float(il - i_gpu_start)/act_gpu_layers) - splits.begin();
  1539. auto * dev = devices.at(layer_gpu);
  1540. LLAMA_LOG_DEBUG("load_tensors: layer %3d assigned to device %s, is_swa = %d\n", il, ggml_backend_dev_name(dev), is_swa);
  1541. return {dev, &pimpl->gpu_buft_list.at(dev)};
  1542. };
  1543. // assign the input layer
  1544. // there is very little benefit to offloading the input layer, so always keep it on the CPU
  1545. pimpl->dev_input = { cpu_dev, &pimpl->cpu_buft_list };
  1546. // assign the repeating layers to the devices according to the splits
  1547. pimpl->dev_layer.resize(n_layer);
  1548. for (int il = 0; il < n_layer; ++il) {
  1549. pimpl->dev_layer[il] = get_layer_buft_list(il);
  1550. }
  1551. // assign the output layer
  1552. pimpl->dev_output = get_layer_buft_list(n_layer);
  1553. // one ggml context per buffer type
  1554. int max_n_tensors = ml.n_tensors;
  1555. max_n_tensors += 1; // duplicated output tensor
  1556. max_n_tensors += n_layer*2; // duplicated rope freq tensors
  1557. const size_t ctx_size = ggml_tensor_overhead()*max_n_tensors;
  1558. std::map<ggml_backend_buffer_type_t, ggml_context *> ctx_map;
  1559. auto ctx_for_buft = [&](ggml_backend_buffer_type_t buft) -> ggml_context * {
  1560. auto it = ctx_map.find(buft);
  1561. if (it == ctx_map.end()) {
  1562. ggml_init_params params = {
  1563. /*.mem_size =*/ ctx_size,
  1564. /*.mem_buffer =*/ NULL,
  1565. /*.no_alloc =*/ true,
  1566. };
  1567. ggml_context * ctx = ggml_init(params);
  1568. if (!ctx) {
  1569. throw std::runtime_error(format("failed to create ggml context"));
  1570. }
  1571. ctx_map[buft] = ctx;
  1572. pimpl->ctxs.emplace_back(ctx);
  1573. return ctx;
  1574. }
  1575. return it->second;
  1576. };
  1577. const auto TENSOR_DUPLICATED = llama_model_loader::TENSOR_DUPLICATED;
  1578. const auto TENSOR_NOT_REQUIRED = llama_model_loader::TENSOR_NOT_REQUIRED;
  1579. // create tensors for the weights
  1580. {
  1581. // note: cast to int64_t since we will use these for the tensor dimensions
  1582. const int64_t n_head = hparams.n_head();
  1583. const int64_t n_head_kv = hparams.n_head_kv();
  1584. const int64_t n_embd = hparams.n_embd;
  1585. const int64_t n_embd_k_gqa = hparams.n_embd_k_gqa();
  1586. const int64_t n_embd_v_gqa = hparams.n_embd_v_gqa();
  1587. const int64_t n_embd_head_k = hparams.n_embd_head_k;
  1588. const int64_t n_embd_head_v = hparams.n_embd_head_v;
  1589. const int64_t n_ff = hparams.n_ff();
  1590. const int64_t n_embd_gqa = n_embd_v_gqa;
  1591. const int64_t n_vocab = vocab.n_tokens();
  1592. const int64_t n_token_types = vocab.n_token_types();
  1593. const int64_t n_rot = hparams.n_rot;
  1594. const int64_t n_expert = hparams.n_expert;
  1595. const int64_t n_expert_used = hparams.n_expert_used;
  1596. const int64_t n_ctx_train = hparams.n_ctx_train;
  1597. if (n_expert > 0 && hparams.n_expert_used == 0) {
  1598. throw std::runtime_error("model has expert layers but no expert layers are used");
  1599. }
  1600. int n_moved_tensors = 0;
  1601. ggml_tensor * first_moved_tensor = nullptr;
  1602. ggml_backend_buffer_type_t first_moved_from_buft = nullptr;
  1603. ggml_backend_buffer_type_t first_moved_to_buft = nullptr;
  1604. auto create_tensor = [&](const LLM_TN_IMPL & tn, const std::initializer_list<int64_t> & ne, int flags) -> ggml_tensor * {
  1605. ggml_tensor * t_meta = ml.get_tensor_meta(tn.str().c_str());
  1606. if (!t_meta) {
  1607. if (flags & TENSOR_NOT_REQUIRED) {
  1608. return nullptr;
  1609. }
  1610. throw std::runtime_error(format("missing tensor '%s'", tn.str().c_str()));
  1611. }
  1612. // some models use the token embedding tensor as the output, but since these are used in different layers and with different ops
  1613. // the tensor is duplicated
  1614. // to handle this, we check if the tensor is duplicated, and if so, we assume that it is being loaded as the output tensor
  1615. llm_tensor tn_tensor = tn.tensor;
  1616. if (tn.tensor == LLM_TENSOR_TOKEN_EMBD && flags & TENSOR_DUPLICATED) {
  1617. tn_tensor = LLM_TENSOR_OUTPUT;
  1618. }
  1619. llm_tensor_info info;
  1620. try {
  1621. info = llm_tensor_info_for(tn_tensor);
  1622. } catch (const std::out_of_range & e) {
  1623. throw std::runtime_error(format("missing tensor info mapping for %s", tn.str().c_str()));
  1624. }
  1625. // skip unused tensors
  1626. if (info.op == GGML_OP_NONE) {
  1627. const size_t nbytes = ggml_nbytes(t_meta);
  1628. LLAMA_LOG_WARN("model has unused tensor %s (size = %zu bytes) -- ignoring\n", tn.str().c_str(), nbytes);
  1629. ml.size_data -= nbytes;
  1630. ml.n_created++;
  1631. return nullptr;
  1632. }
  1633. // tensors with "bias" suffix are always used with GGML_OP_ADD
  1634. ggml_op op;
  1635. bool bias = tn.suffix != nullptr && strcmp(tn.suffix, "bias") == 0;
  1636. if (bias) {
  1637. op = GGML_OP_ADD;
  1638. } else {
  1639. op = info.op;
  1640. }
  1641. // sanity checks
  1642. if (info.layer == LLM_TENSOR_LAYER_INPUT || info.layer == LLM_TENSOR_LAYER_OUTPUT) {
  1643. if (tn.bid != -1) {
  1644. GGML_ABORT("input/output layer tensor %s used with a layer number", tn.str().c_str());
  1645. }
  1646. } else {
  1647. if (tn.bid == -1) {
  1648. GGML_ABORT("repeating layer tensor %s used without a layer number", tn.str().c_str());
  1649. }
  1650. }
  1651. // select the buffer type for this tensor
  1652. buft_list_t * buft_list;
  1653. switch (info.layer) {
  1654. case LLM_TENSOR_LAYER_INPUT:
  1655. buft_list = pimpl->dev_input.buft_list;
  1656. break;
  1657. case LLM_TENSOR_LAYER_OUTPUT:
  1658. buft_list = pimpl->dev_output.buft_list;
  1659. break;
  1660. case LLM_TENSOR_LAYER_REPEATING:
  1661. buft_list = pimpl->dev_layer.at(tn.bid).buft_list;
  1662. break;
  1663. default:
  1664. GGML_ABORT("invalid layer %d for tensor %s", info.layer, tn.str().c_str());
  1665. }
  1666. ggml_backend_buffer_type_t buft = nullptr;
  1667. // check overrides
  1668. if (ml.tensor_buft_overrides) {
  1669. std::string tensor_name = tn.str();
  1670. for (const auto * overrides = ml.tensor_buft_overrides; overrides->pattern != nullptr; ++overrides) {
  1671. std::regex pattern(overrides->pattern);
  1672. if (std::regex_search(tensor_name, pattern)) {
  1673. buft = overrides->buft;
  1674. LLAMA_LOG_DEBUG("tensor %s (%zu MiB %s) buffer type overridden to %s\n",
  1675. tensor_name.c_str(),
  1676. ggml_nbytes(t_meta) / 1024 / 1024, ggml_type_name(t_meta->type),
  1677. ggml_backend_buft_name(buft));
  1678. break;
  1679. }
  1680. }
  1681. }
  1682. if (!buft) {
  1683. buft = select_weight_buft(hparams, t_meta, op, *buft_list);
  1684. if (!buft) {
  1685. throw std::runtime_error(format("failed to find a compatible buffer type for tensor %s", tn.str().c_str()));
  1686. }
  1687. }
  1688. // avoid using a host buffer when using mmap
  1689. auto * buft_dev = ggml_backend_buft_get_device(buft);
  1690. if (ml.use_mmap && buft_dev && buft == ggml_backend_dev_host_buffer_type(buft_dev)) {
  1691. auto * cpu_dev = ggml_backend_dev_by_type(GGML_BACKEND_DEVICE_TYPE_CPU);
  1692. if (!cpu_dev) {
  1693. throw std::runtime_error("no CPU backend found");
  1694. }
  1695. buft = ggml_backend_dev_buffer_type(cpu_dev);
  1696. }
  1697. if (buft != buft_list->front().second) {
  1698. n_moved_tensors++;
  1699. if (!first_moved_tensor) {
  1700. first_moved_tensor = t_meta;
  1701. first_moved_from_buft = buft_list->front().second;
  1702. first_moved_to_buft = buft;
  1703. }
  1704. }
  1705. ggml_context * ctx = ctx_for_buft(buft);
  1706. // if duplicated, check if the original tensor was allocated in the same buffer type context and avoid creating a new one
  1707. if (flags & TENSOR_DUPLICATED) {
  1708. ggml_tensor * t = ggml_get_tensor(ctx, tn.str().c_str());
  1709. if (t) {
  1710. return t;
  1711. }
  1712. }
  1713. return ml.create_tensor(ctx, tn, ne, flags);
  1714. };
  1715. layers.resize(n_layer);
  1716. // TODO: move to a separate function
  1717. const auto tn = LLM_TN(arch);
  1718. switch (arch) {
  1719. case LLM_ARCH_LLAMA:
  1720. case LLM_ARCH_REFACT:
  1721. case LLM_ARCH_MINICPM:
  1722. case LLM_ARCH_GRANITE:
  1723. case LLM_ARCH_GRANITE_MOE:
  1724. {
  1725. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  1726. // output
  1727. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  1728. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  1729. // if output is NULL, init from the input tok embed
  1730. if (output == NULL) {
  1731. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  1732. }
  1733. for (int i = 0; i < n_layer; ++i) {
  1734. auto & layer = layers[i];
  1735. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  1736. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  1737. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  1738. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  1739. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  1740. // optional bias tensors
  1741. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  1742. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED);
  1743. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED);
  1744. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  1745. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  1746. if (hparams.rope_scaling_type_train == LLAMA_ROPE_SCALING_TYPE_LONGROPE) {
  1747. layer.rope_long = create_tensor(tn(LLM_TENSOR_ROPE_FACTORS_LONG, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  1748. layer.rope_short = create_tensor(tn(LLM_TENSOR_ROPE_FACTORS_SHORT, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  1749. }
  1750. else {
  1751. layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  1752. }
  1753. if (n_expert == 0) {
  1754. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  1755. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  1756. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  1757. // optional MLP bias
  1758. layer.ffn_gate_b = create_tensor(tn(LLM_TENSOR_FFN_GATE, "bias", i), {n_ff}, TENSOR_NOT_REQUIRED);
  1759. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  1760. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, TENSOR_NOT_REQUIRED);
  1761. } else {
  1762. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  1763. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff, n_expert}, TENSOR_NOT_REQUIRED);
  1764. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), { n_ff, n_embd, n_expert}, 0);
  1765. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0);
  1766. // For Granite MoE Shared
  1767. if (hparams.n_ff_shexp > 0) {
  1768. layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), {n_embd, hparams.n_ff_shexp}, 0);
  1769. layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), {n_embd, hparams.n_ff_shexp}, 0);
  1770. layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), {hparams.n_ff_shexp, n_embd}, 0);
  1771. }
  1772. }
  1773. }
  1774. } break;
  1775. case LLM_ARCH_LLAMA4:
  1776. {
  1777. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  1778. // output
  1779. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  1780. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  1781. // if output is NULL, init from the input tok embed
  1782. if (output == NULL) {
  1783. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  1784. }
  1785. GGML_ASSERT(hparams.n_moe_layer_step > 0 && "Llama 4 requires n_moe_layer_step > 0");
  1786. for (int i = 0; i < n_layer; ++i) {
  1787. bool is_moe_layer = (i + 1) % hparams.n_moe_layer_step == 0;
  1788. auto & layer = layers[i];
  1789. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  1790. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  1791. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  1792. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  1793. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  1794. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  1795. layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  1796. if (is_moe_layer) {
  1797. int n_ff_exp = hparams.n_ff_exp;
  1798. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  1799. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff_exp, n_expert}, 0);
  1800. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), { n_ff_exp, n_embd, n_expert}, 0);
  1801. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff_exp, n_expert}, 0);
  1802. // Shared expert
  1803. const int64_t n_ff_shexp = n_ff_exp;
  1804. layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), { n_embd, n_ff_shexp}, 0);
  1805. layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), {n_ff_shexp, n_embd }, 0);
  1806. layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), { n_embd, n_ff_shexp}, 0);
  1807. } else {
  1808. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  1809. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  1810. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  1811. }
  1812. }
  1813. } break;
  1814. case LLM_ARCH_DECI:
  1815. {
  1816. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  1817. // output
  1818. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  1819. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  1820. // if output is NULL, init from the input tok embed
  1821. if (output == NULL) {
  1822. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  1823. }
  1824. for (int i = 0; i < n_layer; ++i) {
  1825. auto & layer = layers[i];
  1826. const int64_t n_embd_k_gqa = hparams.n_embd_k_gqa(i);
  1827. const int64_t n_embd_v_gqa = hparams.n_embd_v_gqa(i);
  1828. const int64_t n_embd_gqa = hparams.n_embd_v_gqa(i);
  1829. const int64_t n_ff = hparams.n_ff(i);
  1830. const int64_t n_head = hparams.n_head(i);
  1831. const int64_t n_head_kv = hparams.n_head_kv(i);
  1832. if (n_head_kv == 0 && n_head > 0) {
  1833. // linear attention for DeciLMCausalModel
  1834. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  1835. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  1836. }
  1837. else if (n_head_kv > 0) {
  1838. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  1839. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  1840. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  1841. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  1842. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  1843. }
  1844. // optional bias tensors
  1845. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  1846. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED);
  1847. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED);
  1848. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  1849. if (n_ff > 0) {
  1850. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  1851. }
  1852. if (hparams.rope_scaling_type_train == LLAMA_ROPE_SCALING_TYPE_LONGROPE) {
  1853. layer.rope_long = create_tensor(tn(LLM_TENSOR_ROPE_FACTORS_LONG, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  1854. layer.rope_short = create_tensor(tn(LLM_TENSOR_ROPE_FACTORS_SHORT, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  1855. }
  1856. else {
  1857. layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  1858. }
  1859. if (n_ff > 0) {
  1860. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  1861. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  1862. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  1863. }
  1864. // optional MLP bias
  1865. layer.ffn_gate_b = create_tensor(tn(LLM_TENSOR_FFN_GATE, "bias", i), {n_ff}, TENSOR_NOT_REQUIRED);
  1866. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  1867. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, TENSOR_NOT_REQUIRED);
  1868. }
  1869. } break;
  1870. case LLM_ARCH_MINICPM3:
  1871. {
  1872. const int64_t n_embd_head_qk_rope = hparams.n_rot;
  1873. const int64_t n_embd_head_qk_nope = hparams.n_embd_head_k - hparams.n_rot;
  1874. const int64_t q_lora_rank = hparams.n_lora_q;
  1875. const int64_t kv_lora_rank = hparams.n_lora_kv;
  1876. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  1877. // output
  1878. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  1879. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  1880. // if output is NULL, init from the input tok embed
  1881. if (output == NULL) {
  1882. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  1883. }
  1884. for (int i = 0; i < n_layer; ++i) {
  1885. auto & layer = layers[i];
  1886. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  1887. layer.attn_q_a_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_A_NORM, "weight", i), {q_lora_rank}, 0);
  1888. layer.attn_kv_a_norm = create_tensor(tn(LLM_TENSOR_ATTN_KV_A_NORM, "weight", i), {kv_lora_rank}, 0);
  1889. layer.wq_a = create_tensor(tn(LLM_TENSOR_ATTN_Q_A, "weight", i), {n_embd, q_lora_rank}, 0);
  1890. layer.wq_b = create_tensor(tn(LLM_TENSOR_ATTN_Q_B, "weight", i), {q_lora_rank, n_head * n_embd_head_k}, 0);
  1891. 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);
  1892. 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);
  1893. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), { n_head * ( n_embd_head_v), n_embd}, 0);
  1894. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  1895. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  1896. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  1897. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  1898. 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));
  1899. 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));
  1900. }
  1901. } break;
  1902. case LLM_ARCH_GROK:
  1903. {
  1904. if (n_expert == 0) {
  1905. throw std::runtime_error("Grok model cannot have zero experts");
  1906. }
  1907. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  1908. // output
  1909. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  1910. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  1911. // if output is NULL, init from the input tok embed
  1912. if (output == NULL) {
  1913. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  1914. }
  1915. for (int i = 0; i < n_layer; ++i) {
  1916. auto & layer = layers[i];
  1917. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  1918. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  1919. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  1920. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  1921. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  1922. layer.attn_out_norm = create_tensor(tn(LLM_TENSOR_ATTN_OUT_NORM, "weight", i), {n_embd}, 0);
  1923. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  1924. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  1925. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff, n_expert}, TENSOR_NOT_REQUIRED);
  1926. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), { n_ff, n_embd, n_expert}, 0);
  1927. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0);
  1928. layer.layer_out_norm = create_tensor(tn(LLM_TENSOR_LAYER_OUT_NORM, "weight", i), {n_embd}, 0);
  1929. }
  1930. } break;
  1931. case LLM_ARCH_DBRX:
  1932. {
  1933. if (n_expert == 0) {
  1934. throw std::runtime_error("DBRX model cannot have zero experts");
  1935. }
  1936. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  1937. // output
  1938. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  1939. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  1940. for (int i = 0; i < n_layer; ++i) {
  1941. auto & layer = layers[i];
  1942. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  1943. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, 0);
  1944. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  1945. layer.attn_out_norm = create_tensor(tn(LLM_TENSOR_ATTN_OUT_NORM, "weight", i), {n_embd}, 0);
  1946. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  1947. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0);
  1948. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff, n_embd, n_expert}, 0);
  1949. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0);
  1950. }
  1951. } break;
  1952. case LLM_ARCH_BAICHUAN:
  1953. {
  1954. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  1955. {
  1956. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  1957. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  1958. }
  1959. for (int i = 0; i < n_layer; ++i) {
  1960. auto & layer = layers[i];
  1961. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  1962. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  1963. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  1964. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  1965. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  1966. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  1967. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  1968. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  1969. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  1970. }
  1971. } break;
  1972. case LLM_ARCH_FALCON:
  1973. {
  1974. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  1975. // output
  1976. {
  1977. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  1978. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
  1979. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  1980. if (!output) {
  1981. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); // needs to be on GPU
  1982. }
  1983. }
  1984. for (int i = 0; i < n_layer; ++i) {
  1985. auto & layer = layers[i];
  1986. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  1987. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0);
  1988. layer.attn_norm_2 = create_tensor(tn(LLM_TENSOR_ATTN_NORM_2, "weight", i), {n_embd}, TENSOR_NOT_REQUIRED);
  1989. layer.attn_norm_2_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM_2, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  1990. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, 0);
  1991. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  1992. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  1993. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  1994. }
  1995. } break;
  1996. case LLM_ARCH_STARCODER:
  1997. {
  1998. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  1999. pos_embd = create_tensor(tn(LLM_TENSOR_POS_EMBD, "weight"), {n_embd, n_ctx_train}, 0);
  2000. // output
  2001. {
  2002. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2003. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
  2004. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  2005. if (!output) {
  2006. // needs to be on GPU
  2007. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  2008. }
  2009. }
  2010. for (int i = 0; i < n_layer; ++i) {
  2011. auto & layer = layers[i];
  2012. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2013. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0);
  2014. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, 0);
  2015. layer.bqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd + 2*n_embd_gqa}, 0);
  2016. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2017. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0);
  2018. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2019. layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd}, 0);
  2020. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  2021. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, 0);
  2022. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2023. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, 0);
  2024. }
  2025. } break;
  2026. case LLM_ARCH_BERT:
  2027. case LLM_ARCH_NOMIC_BERT:
  2028. case LLM_ARCH_NOMIC_BERT_MOE:
  2029. {
  2030. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2031. type_embd = create_tensor(tn(LLM_TENSOR_TOKEN_TYPES, "weight"), {n_embd, n_token_types}, TENSOR_NOT_REQUIRED);
  2032. if (arch == LLM_ARCH_BERT) {
  2033. pos_embd = create_tensor(tn(LLM_TENSOR_POS_EMBD, "weight"), {n_embd, n_ctx_train}, 0);
  2034. cls = create_tensor(tn(LLM_TENSOR_CLS, "weight"), {n_embd, n_embd}, TENSOR_NOT_REQUIRED);
  2035. cls_b = create_tensor(tn(LLM_TENSOR_CLS, "bias"), {n_embd}, TENSOR_NOT_REQUIRED);
  2036. cls_out = create_tensor(tn(LLM_TENSOR_CLS_OUT, "weight"), {n_embd, hparams.n_cls_out}, TENSOR_NOT_REQUIRED);
  2037. cls_out_b = create_tensor(tn(LLM_TENSOR_CLS_OUT, "bias"), {hparams.n_cls_out}, TENSOR_NOT_REQUIRED);
  2038. }
  2039. tok_norm = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "weight"), {n_embd}, 0);
  2040. tok_norm_b = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "bias"), {n_embd}, 0);
  2041. for (int i = 0; i < n_layer; ++i) {
  2042. auto & layer = layers[i];
  2043. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, TENSOR_NOT_REQUIRED);
  2044. layer.bqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd + 2*n_embd_gqa}, TENSOR_NOT_REQUIRED);
  2045. if (!layer.wqkv) {
  2046. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  2047. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, 0);
  2048. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  2049. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, 0);
  2050. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  2051. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, 0);
  2052. }
  2053. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2054. layer.attn_out_norm = create_tensor(tn(LLM_TENSOR_ATTN_OUT_NORM, "weight", i), {n_embd}, 0);
  2055. layer.attn_out_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_OUT_NORM, "bias", i), {n_embd}, 0);
  2056. if (hparams.moe_every_n_layers > 0 && i % hparams.moe_every_n_layers == 1) {
  2057. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0);
  2058. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff, n_expert}, 0);
  2059. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), { n_ff, n_embd, n_expert}, 0);
  2060. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  2061. } else {
  2062. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2063. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  2064. if (arch == LLM_ARCH_BERT || arch == LLM_ARCH_NOMIC_BERT_MOE) {
  2065. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0);
  2066. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, 0);
  2067. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, 0);
  2068. } else {
  2069. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  2070. }
  2071. }
  2072. layer.layer_out_norm = create_tensor(tn(LLM_TENSOR_LAYER_OUT_NORM, "weight", i), {n_embd}, 0);
  2073. layer.layer_out_norm_b = create_tensor(tn(LLM_TENSOR_LAYER_OUT_NORM, "bias", i), {n_embd}, 0);
  2074. }
  2075. } break;
  2076. case LLM_ARCH_NEO_BERT:
  2077. {
  2078. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2079. cls = create_tensor(tn(LLM_TENSOR_CLS, "weight"), {n_embd, n_embd}, TENSOR_NOT_REQUIRED);
  2080. cls_b = create_tensor(tn(LLM_TENSOR_CLS, "bias"), {n_embd}, TENSOR_NOT_REQUIRED);
  2081. cls_out = create_tensor(tn(LLM_TENSOR_CLS_OUT, "weight"), {n_embd, hparams.n_cls_out}, TENSOR_NOT_REQUIRED);
  2082. cls_out_b = create_tensor(tn(LLM_TENSOR_CLS_OUT, "bias"), {hparams.n_cls_out}, TENSOR_NOT_REQUIRED);
  2083. output_norm_enc = create_tensor(tn(LLM_TENSOR_ENC_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2084. for (int i = 0; i < n_layer; ++i) {
  2085. auto & layer = layers[i];
  2086. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2087. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, 0);
  2088. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2089. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2090. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff*2}, 0);
  2091. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  2092. }
  2093. } break;
  2094. case LLM_ARCH_JINA_BERT_V2:
  2095. {
  2096. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); // word_embeddings
  2097. type_embd = create_tensor(tn(LLM_TENSOR_TOKEN_TYPES, "weight"), {n_embd, n_token_types}, 0); // token_type_embeddings
  2098. tok_norm = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "weight"), {n_embd}, 0); // LayerNorm
  2099. tok_norm_b = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "bias"), {n_embd}, 0); //LayerNorm bias
  2100. cls = create_tensor(tn(LLM_TENSOR_CLS, "weight"), {n_embd, 1}, TENSOR_NOT_REQUIRED);
  2101. cls_b = create_tensor(tn(LLM_TENSOR_CLS, "bias"), {1}, TENSOR_NOT_REQUIRED);
  2102. for (int i = 0; i < n_layer; ++i) {
  2103. auto & layer = layers[i]; // JinaBertLayer
  2104. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  2105. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, 0);
  2106. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2107. layer.attn_q_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2108. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  2109. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, 0);
  2110. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2111. layer.attn_k_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2112. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  2113. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, 0);
  2114. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0); //output_dens
  2115. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0); //output_dens
  2116. layer.attn_out_norm = create_tensor(tn(LLM_TENSOR_ATTN_OUT_NORM, "weight", i), {n_embd}, 0); //output_norm
  2117. layer.attn_out_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_OUT_NORM, "bias", i), {n_embd}, 0);
  2118. layer.attn_norm_2 = create_tensor(tn(LLM_TENSOR_ATTN_NORM_2, "weight", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2119. layer.attn_norm_2_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM_2, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2120. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, TENSOR_NOT_REQUIRED);
  2121. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, layer.ffn_gate ? n_ff : n_ff * 2}, 0);
  2122. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  2123. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, 0);
  2124. layer.layer_out_norm = create_tensor(tn(LLM_TENSOR_LAYER_OUT_NORM, "weight", i), {n_embd}, 0);
  2125. layer.layer_out_norm_b = create_tensor(tn(LLM_TENSOR_LAYER_OUT_NORM, "bias", i), {n_embd}, 0);
  2126. }
  2127. } break;
  2128. case LLM_ARCH_BLOOM:
  2129. {
  2130. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2131. tok_norm = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "weight"), {n_embd}, 0);
  2132. tok_norm_b = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "bias"), {n_embd}, 0);
  2133. // output
  2134. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2135. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
  2136. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  2137. // if output is NULL, init from the input tok embed
  2138. if (output == NULL) {
  2139. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  2140. }
  2141. for (int i = 0; i < n_layer; ++i) {
  2142. auto & layer = layers[i];
  2143. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2144. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0);
  2145. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, 0);
  2146. layer.bqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd + 2*n_embd_gqa}, 0);
  2147. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2148. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0);
  2149. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2150. layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd}, 0);
  2151. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  2152. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, 0);
  2153. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2154. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, 0);
  2155. }
  2156. } break;
  2157. case LLM_ARCH_MPT:
  2158. {
  2159. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2160. pos_embd = create_tensor(tn(LLM_TENSOR_POS_EMBD, "weight"), {n_embd, n_ctx_train}, TENSOR_NOT_REQUIRED);
  2161. // output
  2162. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2163. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, TENSOR_NOT_REQUIRED);
  2164. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  2165. if (!output) {
  2166. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); // needs to be on GPU
  2167. }
  2168. for (int i = 0; i < n_layer; ++i) {
  2169. auto & layer = layers[i];
  2170. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2171. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2172. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, 0);
  2173. layer.bqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd + 2*n_embd_gqa}, TENSOR_NOT_REQUIRED);
  2174. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2175. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2176. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2177. layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2178. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  2179. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2180. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2181. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, TENSOR_NOT_REQUIRED);
  2182. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2183. layer.attn_q_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2184. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2185. layer.attn_k_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2186. // AWQ ScaleActivation layer
  2187. layer.ffn_act = create_tensor(tn(LLM_TENSOR_FFN_ACT, "scales", i), {n_ff}, TENSOR_NOT_REQUIRED);
  2188. }
  2189. } break;
  2190. case LLM_ARCH_STABLELM:
  2191. {
  2192. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2193. // output
  2194. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
  2195. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2196. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  2197. for (int i = 0; i < n_layer; ++i) {
  2198. auto & layer = layers[i];
  2199. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2200. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0);
  2201. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  2202. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  2203. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  2204. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2205. // optional bias tensors, present in Stable LM 2 1.6B
  2206. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2207. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED);
  2208. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED);
  2209. // optional q and k layernorms, present in StableLM 2 12B
  2210. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k, n_head}, TENSOR_NOT_REQUIRED);
  2211. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k, n_head_kv}, TENSOR_NOT_REQUIRED);
  2212. // optional FFN norm, not present in StableLM 2 12B which uses parallel residual
  2213. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2214. layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2215. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  2216. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  2217. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2218. }
  2219. } break;
  2220. case LLM_ARCH_QWEN:
  2221. {
  2222. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2223. // output
  2224. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2225. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  2226. for (int i = 0; i < n_layer; ++i) {
  2227. auto & layer = layers[i];
  2228. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2229. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd*3}, 0);
  2230. layer.bqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd*3}, 0);
  2231. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2232. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2233. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff/2}, 0);
  2234. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff/2, n_embd}, 0);
  2235. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff/2}, 0);
  2236. }
  2237. } break;
  2238. case LLM_ARCH_QWEN2:
  2239. case LLM_ARCH_QWEN2VL:
  2240. {
  2241. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2242. // output
  2243. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2244. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  2245. // if output is NULL, init from the input tok embed
  2246. if (output == NULL) {
  2247. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  2248. }
  2249. for (int i = 0; i < n_layer; ++i) {
  2250. auto & layer = layers[i];
  2251. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2252. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  2253. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  2254. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  2255. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2256. // optional bias tensors
  2257. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, 0);
  2258. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, 0);
  2259. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, 0);
  2260. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2261. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  2262. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  2263. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2264. }
  2265. } break;
  2266. case LLM_ARCH_QWEN2MOE:
  2267. {
  2268. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2269. // output
  2270. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2271. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  2272. for (int i = 0; i < n_layer; ++i) {
  2273. auto & layer = layers[i];
  2274. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2275. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  2276. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  2277. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  2278. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2279. // optional bias tensors
  2280. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  2281. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED);
  2282. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED);
  2283. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2284. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  2285. if (n_expert == 0) {
  2286. throw std::runtime_error("n_expert must be > 0 for QWEN2MOE");
  2287. }
  2288. if (n_expert_used == 0) {
  2289. throw std::runtime_error("n_expert_used must be > 0 for QWEN2MOE");
  2290. }
  2291. // MoE branch
  2292. const int64_t n_ff_exp = hparams.n_ff_exp ? hparams.n_ff_exp : n_ff / n_expert_used;
  2293. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);
  2294. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp, n_embd, n_expert}, 0);
  2295. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);
  2296. // Shared expert branch
  2297. const int64_t n_ff_shexp = hparams.n_ff_shexp ? hparams.n_ff_shexp : n_ff;
  2298. layer.ffn_gate_inp_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP_SHEXP, "weight", i), {n_embd}, 0);
  2299. layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), { n_embd, n_ff_shexp}, 0);
  2300. layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), {n_ff_shexp, n_embd}, 0);
  2301. layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), { n_embd, n_ff_shexp}, 0);
  2302. }
  2303. } break;
  2304. case LLM_ARCH_QWEN3:
  2305. {
  2306. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2307. // output
  2308. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2309. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  2310. // if output is NULL, init from the input tok embed
  2311. if (output == NULL) {
  2312. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  2313. }
  2314. for (int i = 0; i < n_layer; ++i) {
  2315. auto & layer = layers[i];
  2316. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2317. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  2318. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  2319. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  2320. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  2321. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0);
  2322. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0);
  2323. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2324. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  2325. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  2326. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2327. }
  2328. } break;
  2329. case LLM_ARCH_QWEN3MOE:
  2330. {
  2331. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2332. // output
  2333. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2334. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  2335. // if output is NULL, init from the input tok embed
  2336. if (output == NULL) {
  2337. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  2338. }
  2339. for (int i = 0; i < n_layer; ++i) {
  2340. auto & layer = layers[i];
  2341. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2342. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  2343. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  2344. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  2345. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  2346. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0);
  2347. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0);
  2348. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2349. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  2350. if (n_expert == 0) {
  2351. throw std::runtime_error("n_expert must be > 0 for QWEN3MOE");
  2352. }
  2353. if (n_expert_used == 0) {
  2354. throw std::runtime_error("n_expert_used must be > 0 for QWEN3MOE");
  2355. }
  2356. // MoE branch
  2357. const int64_t n_ff_exp = hparams.n_ff_exp ? hparams.n_ff_exp : n_ff / n_expert_used;
  2358. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);
  2359. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp, n_embd, n_expert}, 0);
  2360. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);
  2361. }
  2362. } break;
  2363. case LLM_ARCH_PHI2:
  2364. {
  2365. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2366. // output
  2367. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2368. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
  2369. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  2370. output_b = create_tensor(tn(LLM_TENSOR_OUTPUT, "bias"), {n_vocab}, 0);
  2371. for (int i = 0; i < n_layer; ++i) {
  2372. auto & layer = layers[i];
  2373. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2374. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0);
  2375. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, TENSOR_NOT_REQUIRED);
  2376. layer.bqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd + 2*n_embd_gqa}, TENSOR_NOT_REQUIRED);
  2377. if (layer.wqkv == nullptr) {
  2378. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  2379. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, 0);
  2380. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  2381. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, 0);
  2382. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  2383. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, 0);
  2384. }
  2385. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2386. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0);
  2387. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  2388. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, 0);
  2389. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2390. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, 0);
  2391. }
  2392. } break;
  2393. case LLM_ARCH_PHI3:
  2394. {
  2395. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, 0);
  2396. // output
  2397. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), { n_embd }, 0);
  2398. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  2399. // if output is NULL, init from the input tok embed
  2400. if (output == NULL) {
  2401. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  2402. }
  2403. for (int i = 0; i < n_layer; ++i) {
  2404. auto & layer = layers[i];
  2405. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), { n_embd }, 0);
  2406. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), { n_embd, n_embd + 2 * n_embd_gqa }, TENSOR_NOT_REQUIRED);
  2407. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), { n_embd, n_embd }, 0);
  2408. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), { n_embd }, 0);
  2409. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd }, 0);
  2410. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), { n_embd, 2 * n_ff }, 0);
  2411. layer.rope_long = create_tensor(tn(LLM_TENSOR_ROPE_FACTORS_LONG, "weight", i), { n_rot/2 }, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  2412. layer.rope_short = create_tensor(tn(LLM_TENSOR_ROPE_FACTORS_SHORT, "weight", i), { n_rot/2 }, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  2413. }
  2414. } break;
  2415. case LLM_ARCH_PHIMOE:
  2416. {
  2417. const int64_t n_embd_head = n_embd / n_head;
  2418. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, 0);
  2419. // output
  2420. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), { n_embd }, 0);
  2421. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
  2422. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), { n_embd, n_vocab }, 0);
  2423. output_b = create_tensor(tn(LLM_TENSOR_OUTPUT, "bias"), { n_vocab }, 0);
  2424. for (int i = 0; i < n_layer; ++i) {
  2425. auto & layer = layers[i];
  2426. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), { n_embd }, 0);
  2427. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), { n_embd }, 0);
  2428. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), { n_embd, n_embd + 2 * n_embd_gqa }, TENSOR_NOT_REQUIRED);
  2429. if (layer.wqkv == nullptr) {
  2430. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  2431. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, 0);
  2432. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  2433. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, 0);
  2434. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  2435. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, 0);
  2436. }
  2437. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), { n_embd, n_embd }, 0);
  2438. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), { n_embd }, 0);
  2439. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), { n_embd }, 0);
  2440. layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), { n_embd }, 0);
  2441. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  2442. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0);
  2443. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff, n_embd, n_expert}, 0);
  2444. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0);
  2445. 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));
  2446. 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));
  2447. }
  2448. } break;
  2449. case LLM_ARCH_PLAMO:
  2450. {
  2451. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2452. // output
  2453. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2454. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  2455. for (int i = 0; i < n_layer; ++i) {
  2456. auto & layer = layers[i];
  2457. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2458. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  2459. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  2460. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  2461. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2462. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  2463. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  2464. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2465. }
  2466. } break;
  2467. case LLM_ARCH_GPT2:
  2468. {
  2469. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2470. pos_embd = create_tensor(tn(LLM_TENSOR_POS_EMBD, "weight"), {n_embd, n_ctx_train}, 0);
  2471. // output
  2472. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2473. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
  2474. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  2475. // if output is NULL, init from the input tok embed
  2476. if (output == NULL) {
  2477. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  2478. }
  2479. for (int i = 0; i < n_layer; ++i) {
  2480. auto & layer = layers[i];
  2481. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2482. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0);
  2483. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, 0);
  2484. layer.bqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd + 2*n_embd_gqa}, 0);
  2485. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2486. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0);
  2487. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2488. layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd}, 0);
  2489. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  2490. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, 0);
  2491. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2492. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, 0);
  2493. }
  2494. } break;
  2495. case LLM_ARCH_CODESHELL:
  2496. {
  2497. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  2498. // if tok embd is NULL, init from output
  2499. if (tok_embd == NULL) {
  2500. tok_embd = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  2501. }
  2502. // output
  2503. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2504. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {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.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0);
  2510. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, 0);
  2511. layer.bqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd + 2*n_embd_gqa}, 0);
  2512. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2513. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0);
  2514. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2515. layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd}, 0);
  2516. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  2517. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, 0);
  2518. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2519. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, 0);
  2520. }
  2521. } break;
  2522. case LLM_ARCH_ORION:
  2523. {
  2524. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2525. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2526. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
  2527. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  2528. for (int i = 0; i < n_layer; ++i) {
  2529. auto & layer = layers[i];
  2530. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2531. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0);
  2532. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  2533. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  2534. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  2535. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2536. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2537. layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd}, 0);
  2538. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  2539. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  2540. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2541. }
  2542. } break;
  2543. case LLM_ARCH_INTERNLM2:
  2544. {
  2545. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2546. // output
  2547. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2548. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  2549. for (int i = 0; i < n_layer; ++i) {
  2550. auto & layer = layers[i];
  2551. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2552. // layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, 0);
  2553. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  2554. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  2555. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  2556. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2557. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2558. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  2559. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  2560. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2561. }
  2562. } break;
  2563. case LLM_ARCH_GEMMA:
  2564. {
  2565. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2566. // output
  2567. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2568. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); // same as tok_embd, duplicated to allow offloading
  2569. for (int i = 0; i < n_layer; ++i) {
  2570. auto & layer = layers[i];
  2571. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2572. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  2573. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  2574. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  2575. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  2576. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2577. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  2578. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2579. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  2580. }
  2581. } break;
  2582. case LLM_ARCH_GEMMA2:
  2583. {
  2584. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2585. // output
  2586. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2587. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); // same as tok_embd, duplicated to allow offloading
  2588. for (int i = 0; i < n_layer; ++i) {
  2589. auto & layer = layers[i];
  2590. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2591. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  2592. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  2593. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  2594. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  2595. layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), {n_embd}, 0);
  2596. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2597. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  2598. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2599. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  2600. layer.ffn_post_norm = create_tensor(tn(LLM_TENSOR_FFN_POST_NORM, "weight", i), {n_embd}, 0);
  2601. }
  2602. } break;
  2603. case LLM_ARCH_GEMMA3:
  2604. {
  2605. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2606. // output
  2607. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2608. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  2609. // if output is NULL, init from the input tok embed
  2610. if (output == NULL) {
  2611. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  2612. }
  2613. for (int i = 0; i < n_layer; ++i) {
  2614. auto & layer = layers[i];
  2615. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2616. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  2617. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  2618. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  2619. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  2620. layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), {n_embd}, 0);
  2621. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0);
  2622. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0);
  2623. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2624. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  2625. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2626. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  2627. layer.ffn_post_norm = create_tensor(tn(LLM_TENSOR_FFN_POST_NORM, "weight", i), {n_embd}, 0);
  2628. }
  2629. } break;
  2630. case LLM_ARCH_GEMMA3N:
  2631. {
  2632. const int64_t n_altup = hparams.n_altup;
  2633. const int64_t laurel_rank = hparams.laurel_rank;
  2634. const int64_t n_embd_altup = hparams.n_embd_altup;
  2635. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  2636. // if output is NULL, init from the input tok embed
  2637. if (output == NULL) {
  2638. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  2639. }
  2640. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2641. tok_embd_per_layer = create_tensor(tn(LLM_TENSOR_PER_LAYER_TOKEN_EMBD, "weight"), {n_embd_altup * n_layer, n_vocab}, 0);
  2642. altup_proj = create_tensor(tn(LLM_TENSOR_ALTUP_PROJ, "weight"), {n_embd, n_embd, n_altup - 1}, 0);
  2643. altup_unembd_proj = create_tensor(tn(LLM_TENSOR_ALTUP_UNEMBD_PROJ, "weight"), {n_embd, n_embd, n_altup - 1}, 0);
  2644. per_layer_model_proj = create_tensor(tn(LLM_TENSOR_PER_LAYER_MODEL_PROJ, "weight"), {n_embd, n_embd_altup * n_layer}, 0);
  2645. per_layer_proj_norm = create_tensor(tn(LLM_TENSOR_PER_LAYER_PROJ_NORM, "weight"), {n_embd_altup}, 0);
  2646. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2647. for (int i = 0; i < n_layer; ++i) {
  2648. auto & layer = layers[i];
  2649. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2650. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  2651. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  2652. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  2653. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  2654. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0);
  2655. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0);
  2656. layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), {n_embd}, 0);
  2657. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2658. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  2659. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2660. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  2661. layer.ffn_post_norm = create_tensor(tn(LLM_TENSOR_FFN_POST_NORM, "weight", i), {n_embd}, 0);
  2662. // altup & laurel
  2663. layer.per_layer_inp_gate = create_tensor(tn(LLM_TENSOR_PER_LAYER_INP_GATE, "weight", i), {n_embd, n_embd_altup}, 0);
  2664. layer.per_layer_proj = create_tensor(tn(LLM_TENSOR_PER_LAYER_PROJ, "weight", i), {n_embd_altup, n_embd}, 0);
  2665. layer.per_layer_post_norm = create_tensor(tn(LLM_TENSOR_PER_LAYER_POST_NORM, "weight", i), {n_embd}, 0);
  2666. layer.altup_correct_coef = create_tensor(tn(LLM_TENSOR_ALTUP_CORRECT_COEF, "weight", i), {n_altup, n_altup}, 0);
  2667. layer.altup_correct_scale = create_tensor(tn(LLM_TENSOR_ALTUP_CORRECT_SCALE, "weight", i), {n_embd}, 0);
  2668. layer.altup_predict_coef = create_tensor(tn(LLM_TENSOR_ALTUP_PREDICT_COEF, "weight", i), {n_altup, n_altup * n_altup}, 0);
  2669. layer.altup_router = create_tensor(tn(LLM_TENSOR_ALTUP_ROUTER, "weight", i), {n_embd, n_altup}, 0);
  2670. layer.altup_router_norm = create_tensor(tn(LLM_TENSOR_ALTUP_ROUTER_NORM, "weight", i), {n_embd}, 0);
  2671. layer.laurel_l = create_tensor(tn(LLM_TENSOR_LAUREL_L, "weight", i), {n_embd, laurel_rank}, 0);
  2672. layer.laurel_r = create_tensor(tn(LLM_TENSOR_LAUREL_R, "weight", i), {laurel_rank, n_embd}, 0);
  2673. layer.laurel_post_norm = create_tensor(tn(LLM_TENSOR_LAUREL_POST_NORM, "weight", i), {n_embd}, 0);
  2674. }
  2675. } break;
  2676. case LLM_ARCH_STARCODER2:
  2677. {
  2678. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2679. // output
  2680. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2681. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
  2682. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  2683. // if output is NULL, init from the input tok embed
  2684. if (output == NULL) {
  2685. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  2686. }
  2687. for (int i = 0; i < n_layer; ++i) {
  2688. auto & layer = layers[i];
  2689. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2690. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0);
  2691. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  2692. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  2693. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  2694. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2695. // optional bias tensors
  2696. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, 0);
  2697. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, 0);
  2698. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, 0);
  2699. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0);
  2700. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2701. layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd}, 0);
  2702. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  2703. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2704. // optional bias tensors
  2705. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, 0);
  2706. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP , "bias", i), { n_ff}, 0);
  2707. }
  2708. } break;
  2709. case LLM_ARCH_MAMBA:
  2710. {
  2711. const int64_t d_conv = hparams.ssm_d_conv;
  2712. const int64_t d_inner = hparams.ssm_d_inner;
  2713. const int64_t d_state = hparams.ssm_d_state;
  2714. const int64_t dt_rank = hparams.ssm_dt_rank;
  2715. // only an expansion factor of 2 is supported for now
  2716. if (2 * n_embd != d_inner) {
  2717. throw std::runtime_error("only an expansion factor of 2 is supported for now");
  2718. }
  2719. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2720. // output
  2721. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2722. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  2723. // if output is NULL, init from the input tok embed, duplicated to allow offloading
  2724. if (output == NULL) {
  2725. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  2726. }
  2727. for (int i = 0; i < n_layer; ++i) {
  2728. auto & layer = layers[i];
  2729. // norm
  2730. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2731. layer.ssm_in = create_tensor(tn(LLM_TENSOR_SSM_IN, "weight", i), {n_embd, 2*d_inner}, 0);
  2732. layer.ssm_conv1d = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "weight", i), {d_conv, d_inner}, 0);
  2733. layer.ssm_conv1d_b = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "bias", i), {d_inner}, 0);
  2734. layer.ssm_x = create_tensor(tn(LLM_TENSOR_SSM_X, "weight", i), {d_inner, dt_rank + 2*d_state}, 0);
  2735. layer.ssm_dt = create_tensor(tn(LLM_TENSOR_SSM_DT, "weight", i), {dt_rank, d_inner}, 0);
  2736. layer.ssm_dt_b = create_tensor(tn(LLM_TENSOR_SSM_DT, "bias", i), {d_inner}, 0);
  2737. // no "weight" suffix for these
  2738. layer.ssm_a = create_tensor(tn(LLM_TENSOR_SSM_A, i), {d_state, d_inner}, 0);
  2739. layer.ssm_d = create_tensor(tn(LLM_TENSOR_SSM_D, i), {d_inner}, 0);
  2740. // out_proj
  2741. layer.ssm_out = create_tensor(tn(LLM_TENSOR_SSM_OUT, "weight", i), {d_inner, n_embd}, 0);
  2742. }
  2743. } break;
  2744. case LLM_ARCH_MAMBA2:
  2745. {
  2746. const int64_t d_conv = hparams.ssm_d_conv;
  2747. const int64_t d_inner = hparams.ssm_d_inner;
  2748. const int64_t d_state = hparams.ssm_d_state;
  2749. const int64_t n_head = hparams.ssm_dt_rank;
  2750. const int64_t n_group = hparams.ssm_n_group;
  2751. const int64_t d_in_proj = 2*d_inner + 2*n_group*d_state + n_head;
  2752. // only an expansion factor of 2 is supported for now
  2753. GGML_ASSERT(2 * n_embd == d_inner);
  2754. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2755. // output
  2756. {
  2757. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2758. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  2759. // if output is NULL, init from the input tok embed, duplicated to allow offloading
  2760. if (output == NULL) {
  2761. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  2762. }
  2763. }
  2764. for (int i = 0; i < n_layer; ++i) {
  2765. auto & layer = layers[i];
  2766. // norm
  2767. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2768. layer.ssm_in = create_tensor(tn(LLM_TENSOR_SSM_IN, "weight", i), {n_embd, d_in_proj}, 0);
  2769. layer.ssm_conv1d = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "weight", i), {d_conv, d_inner + 2*n_group*d_state}, 0);
  2770. layer.ssm_conv1d_b = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "bias", i), {d_inner + 2*n_group*d_state}, 0);
  2771. layer.ssm_dt_b = create_tensor(tn(LLM_TENSOR_SSM_DT, "bias", i), {n_head}, 0);
  2772. // no "weight" suffix for these
  2773. layer.ssm_a = create_tensor(tn(LLM_TENSOR_SSM_A, i), {1, n_head}, 0);
  2774. layer.ssm_d = create_tensor(tn(LLM_TENSOR_SSM_D, i), {1, n_head}, 0);
  2775. layer.ssm_norm = create_tensor(tn(LLM_TENSOR_SSM_NORM, "weight", i), {d_inner / n_group, n_group}, 0);
  2776. // out_proj
  2777. layer.ssm_out = create_tensor(tn(LLM_TENSOR_SSM_OUT, "weight", i), {d_inner, n_embd}, 0);
  2778. }
  2779. } break;
  2780. case LLM_ARCH_JAMBA:
  2781. {
  2782. const int64_t d_conv = hparams.ssm_d_conv;
  2783. const int64_t d_inner = hparams.ssm_d_inner;
  2784. const int64_t d_state = hparams.ssm_d_state;
  2785. const int64_t dt_rank = hparams.ssm_dt_rank;
  2786. // only an expansion factor of 2 is supported for now
  2787. GGML_ASSERT(2 * n_embd == d_inner);
  2788. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2789. // output
  2790. {
  2791. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2792. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  2793. // if output is NULL, init from the input tok embed, duplicated to allow offloading
  2794. if (output == NULL) {
  2795. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  2796. }
  2797. }
  2798. for (int i = 0; i < n_layer; ++i) {
  2799. const int64_t n_head_kv = hparams.n_head_kv(i);
  2800. const int64_t n_embd_gqa = hparams.n_embd_v_gqa(i);
  2801. auto & layer = layers[i];
  2802. // norm
  2803. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2804. if (n_head_kv == 0) {
  2805. // Mamba layer
  2806. layer.ssm_in = create_tensor(tn(LLM_TENSOR_SSM_IN, "weight", i), {n_embd, 2*d_inner}, 0);
  2807. layer.ssm_conv1d = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "weight", i), {d_conv, d_inner}, 0);
  2808. layer.ssm_conv1d_b = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "bias", i), {d_inner}, 0);
  2809. layer.ssm_x = create_tensor(tn(LLM_TENSOR_SSM_X, "weight", i), {d_inner, dt_rank + 2*d_state}, 0);
  2810. layer.ssm_dt_norm = create_tensor(tn(LLM_TENSOR_SSM_DT_NORM, "weight", i), {dt_rank}, 0);
  2811. layer.ssm_dt = create_tensor(tn(LLM_TENSOR_SSM_DT, "weight", i), {dt_rank, d_inner}, 0);
  2812. layer.ssm_dt_b = create_tensor(tn(LLM_TENSOR_SSM_DT, "bias", i), {d_inner}, 0);
  2813. layer.ssm_b_norm = create_tensor(tn(LLM_TENSOR_SSM_B_NORM, "weight", i), {d_state}, 0);
  2814. layer.ssm_c_norm = create_tensor(tn(LLM_TENSOR_SSM_C_NORM, "weight", i), {d_state}, 0);
  2815. // no "weight" suffix for these
  2816. layer.ssm_a = create_tensor(tn(LLM_TENSOR_SSM_A, i), {d_state, d_inner}, 0);
  2817. layer.ssm_d = create_tensor(tn(LLM_TENSOR_SSM_D, i), {d_inner}, 0);
  2818. // out_proj
  2819. layer.ssm_out = create_tensor(tn(LLM_TENSOR_SSM_OUT, "weight", i), {d_inner, n_embd}, 0);
  2820. } else {
  2821. // Attention layers
  2822. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  2823. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  2824. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  2825. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2826. }
  2827. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2828. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, TENSOR_NOT_REQUIRED);
  2829. if (layer.ffn_gate_inp) {
  2830. // MoE
  2831. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0);
  2832. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff, n_embd, n_expert}, 0);
  2833. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0);
  2834. } else {
  2835. // FFN (no MoE)
  2836. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  2837. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  2838. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2839. }
  2840. }
  2841. } break;
  2842. case LLM_ARCH_XVERSE:
  2843. {
  2844. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2845. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2846. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  2847. for (int i = 0; i < n_layer; ++i) {
  2848. auto & layer = layers[i];
  2849. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2850. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  2851. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  2852. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  2853. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2854. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2855. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  2856. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  2857. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2858. }
  2859. } break;
  2860. case LLM_ARCH_COMMAND_R:
  2861. {
  2862. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2863. // output
  2864. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2865. // init output from the input tok embed
  2866. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  2867. for (int i = 0; i < n_layer; ++i) {
  2868. auto & layer = layers[i];
  2869. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2870. if (n_layer >= 64){
  2871. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k, n_head}, 0);
  2872. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k, n_head_kv}, 0);
  2873. }
  2874. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  2875. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  2876. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  2877. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2878. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  2879. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  2880. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2881. }
  2882. } break;
  2883. case LLM_ARCH_COHERE2:
  2884. {
  2885. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, 0);
  2886. // output
  2887. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), { n_embd }, 0);
  2888. // init output from the input tok embed
  2889. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab },
  2890. TENSOR_DUPLICATED);
  2891. for (int i = 0; i < n_layer; ++i) {
  2892. auto & layer = layers[i];
  2893. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), { n_embd }, 0);
  2894. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), { n_embd, n_embd }, 0);
  2895. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), { n_embd, n_embd_gqa }, 0);
  2896. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), { n_embd, n_embd_gqa }, 0);
  2897. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), { n_embd, n_embd }, 0);
  2898. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), { n_embd, n_ff }, 0);
  2899. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd }, 0);
  2900. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), { n_embd, n_ff }, 0);
  2901. }
  2902. }
  2903. break;
  2904. case LLM_ARCH_OLMO: // adapted from LLM_ARCH_LLAMA with norm params removed
  2905. {
  2906. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2907. // output
  2908. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  2909. // if output is NULL, init from the input tok embed
  2910. if (output == NULL) {
  2911. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  2912. }
  2913. for (int i = 0; i < n_layer; ++i) {
  2914. auto & layer = layers[i];
  2915. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  2916. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  2917. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  2918. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2919. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  2920. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  2921. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2922. }
  2923. } break;
  2924. case LLM_ARCH_OLMO2:
  2925. {
  2926. const int64_t n_embd_head = n_embd / n_head;
  2927. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2928. // output
  2929. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2930. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  2931. for (int i = 0; i < n_layer; ++i) {
  2932. auto & layer = layers[i];
  2933. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  2934. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  2935. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  2936. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2937. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd}, 0);
  2938. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_head_kv * n_embd_head}, 0);
  2939. layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), {n_embd}, 0);
  2940. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  2941. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2942. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  2943. layer.ffn_post_norm = create_tensor(tn(LLM_TENSOR_FFN_POST_NORM, "weight", i), {n_embd}, 0);
  2944. }
  2945. } break;
  2946. case LLM_ARCH_OLMOE:
  2947. {
  2948. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2949. // output
  2950. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2951. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  2952. for (int i = 0; i < n_layer; ++i) {
  2953. auto & layer = layers[i];
  2954. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2955. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  2956. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  2957. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  2958. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  2959. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd}, 0);
  2960. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd}, 0);
  2961. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2962. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  2963. if (n_expert == 0) {
  2964. throw std::runtime_error("n_expert must be > 0");
  2965. }
  2966. if (n_expert_used == 0) {
  2967. throw std::runtime_error("n_expert_used must be > 0");
  2968. }
  2969. // MoE branch
  2970. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0);
  2971. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff, n_embd, n_expert}, 0);
  2972. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0);
  2973. }
  2974. } break;
  2975. case LLM_ARCH_OPENELM:
  2976. {
  2977. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  2978. // output
  2979. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  2980. // init output from the input tok embed
  2981. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  2982. for (int i = 0; i < n_layer; ++i) {
  2983. const int64_t n_head = hparams.n_head(i);
  2984. const int64_t n_head_qkv = 2*hparams.n_head_kv(i) + n_head;
  2985. const int64_t n_ff = hparams.n_ff(i);
  2986. auto & layer = layers[i];
  2987. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  2988. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_head_qkv*n_embd_head_k}, 0);
  2989. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0);
  2990. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0);
  2991. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_head*n_embd_head_k, n_embd}, 0);
  2992. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  2993. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  2994. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  2995. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  2996. }
  2997. } break;
  2998. case LLM_ARCH_GPTNEOX:
  2999. {
  3000. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3001. // output
  3002. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3003. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
  3004. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  3005. for (int i = 0; i < n_layer; ++i) {
  3006. auto & layer = layers[i];
  3007. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3008. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0);
  3009. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, 0);
  3010. layer.bqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd + 2*n_embd_gqa}, 0);
  3011. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  3012. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0);
  3013. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3014. layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd}, 0);
  3015. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  3016. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, 0);
  3017. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3018. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, 0);
  3019. }
  3020. } break;
  3021. case LLM_ARCH_ARCTIC:
  3022. {
  3023. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3024. // output
  3025. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3026. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  3027. // if output is NULL, init from the input tok embed
  3028. if (output == NULL) {
  3029. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3030. }
  3031. for (int i = 0; i < n_layer; ++i) {
  3032. auto & layer = layers[i];
  3033. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3034. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  3035. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  3036. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  3037. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  3038. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3039. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_embd}, 0);
  3040. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_embd, n_embd}, 0);
  3041. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_embd}, 0);
  3042. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  3043. layer.ffn_norm_exps = create_tensor(tn(LLM_TENSOR_FFN_NORM_EXPS, "weight", i), {n_embd}, 0);
  3044. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff, n_expert}, false);
  3045. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), { n_ff, n_embd, n_expert}, 0);
  3046. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0);
  3047. }
  3048. } break;
  3049. case LLM_ARCH_DEEPSEEK:
  3050. {
  3051. const int64_t n_ff_exp = hparams.n_ff_exp;
  3052. const int64_t n_expert_shared = hparams.n_expert_shared;
  3053. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3054. // output
  3055. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3056. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  3057. for (int i = 0; i < n_layer; ++i) {
  3058. auto & layer = layers[i];
  3059. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3060. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  3061. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  3062. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  3063. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  3064. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3065. if (i < (int) hparams.n_layer_dense_lead) {
  3066. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  3067. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3068. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3069. } else {
  3070. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  3071. if (n_expert == 0) {
  3072. throw std::runtime_error("n_expert must be > 0");
  3073. }
  3074. if (n_expert_used == 0) {
  3075. throw std::runtime_error("n_expert_used must be > 0");
  3076. }
  3077. // MoE branch
  3078. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);
  3079. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp, n_embd, n_expert}, 0);
  3080. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);
  3081. // Shared expert branch
  3082. layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), {n_embd, n_ff_exp * n_expert_shared}, 0);
  3083. layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), { n_ff_exp * n_expert_shared, n_embd}, 0);
  3084. layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), {n_embd, n_ff_exp * n_expert_shared}, 0);
  3085. }
  3086. }
  3087. } break;
  3088. case LLM_ARCH_DEEPSEEK2:
  3089. {
  3090. const bool is_lite = (hparams.n_layer == 27);
  3091. const bool is_mla = (hparams.n_embd_head_k_mla != 0 && hparams.n_embd_head_v_mla != 0);
  3092. // note: these are the actual head sizes you get when treating as MHA or after "decompression" using wv_b for MLA
  3093. const int64_t n_embd_head_k_mla = is_mla ? hparams.n_embd_head_k_mla : hparams.n_embd_head_k;
  3094. const int64_t n_embd_head_v_mla = is_mla ? hparams.n_embd_head_v_mla : hparams.n_embd_head_v;
  3095. const int64_t n_embd_head_qk_rope = hparams.n_rot;
  3096. const int64_t n_embd_head_qk_nope = n_embd_head_k_mla - n_embd_head_qk_rope;
  3097. const int64_t q_lora_rank = hparams.n_lora_q;
  3098. const int64_t kv_lora_rank = hparams.n_lora_kv;
  3099. const int64_t n_ff_exp = hparams.n_ff_exp;
  3100. const int64_t n_expert_shared = hparams.n_expert_shared;
  3101. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3102. // output
  3103. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3104. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  3105. for (int i = 0; i < n_layer; ++i) {
  3106. auto & layer = layers[i];
  3107. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3108. if (!is_lite) {
  3109. layer.attn_q_a_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_A_NORM, "weight", i), {q_lora_rank}, 0);
  3110. }
  3111. layer.attn_kv_a_norm = create_tensor(tn(LLM_TENSOR_ATTN_KV_A_NORM, "weight", i), {kv_lora_rank}, 0);
  3112. if (!is_lite) {
  3113. layer.wq_a = create_tensor(tn(LLM_TENSOR_ATTN_Q_A, "weight", i), {n_embd, q_lora_rank}, 0);
  3114. layer.wq_b = create_tensor(tn(LLM_TENSOR_ATTN_Q_B, "weight", i), {q_lora_rank, n_head * n_embd_head_k_mla}, 0);
  3115. } else {
  3116. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_head * n_embd_head_k_mla}, 0);
  3117. }
  3118. 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);
  3119. // note: only old legacy GGUF files will have the unsplit wkv_b tensor in
  3120. if (is_mla) {
  3121. layer.wk_b = create_tensor(tn(LLM_TENSOR_ATTN_K_B, "weight", i), {n_embd_head_qk_nope, kv_lora_rank, n_head}, 0);
  3122. layer.wv_b = create_tensor(tn(LLM_TENSOR_ATTN_V_B, "weight", i), {kv_lora_rank, n_embd_head_v_mla, n_head}, 0);
  3123. } else {
  3124. 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);
  3125. }
  3126. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_head * n_embd_head_v_mla, n_embd}, 0);
  3127. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3128. if (i < (int) hparams.n_layer_dense_lead) {
  3129. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  3130. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3131. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3132. } else {
  3133. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  3134. layer.ffn_exp_probs_b = create_tensor(tn(LLM_TENSOR_FFN_EXP_PROBS_B, "bias", i), {n_expert}, TENSOR_NOT_REQUIRED);
  3135. if (n_expert == 0) {
  3136. throw std::runtime_error("n_expert must be > 0");
  3137. }
  3138. if (n_expert_used == 0) {
  3139. throw std::runtime_error("n_expert_used must be > 0");
  3140. }
  3141. // MoE branch
  3142. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);
  3143. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp, n_embd, n_expert}, 0);
  3144. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);
  3145. // Shared expert branch
  3146. layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), {n_embd, n_ff_exp * n_expert_shared}, 0);
  3147. layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), { n_ff_exp * n_expert_shared, n_embd}, 0);
  3148. layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), {n_embd, n_ff_exp * n_expert_shared}, 0);
  3149. }
  3150. }
  3151. } break;
  3152. case LLM_ARCH_PLM:
  3153. {
  3154. const int64_t n_embd_head_qk_rope = hparams.n_rot;
  3155. const int64_t n_embd_head_qk_nope = hparams.n_embd_head_k - hparams.n_rot;
  3156. const int64_t kv_lora_rank = hparams.n_lora_kv;
  3157. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3158. // output
  3159. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3160. // output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  3161. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3162. for (int i = 0; i < n_layer; ++i) {
  3163. auto & layer = layers[i];
  3164. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3165. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  3166. 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);
  3167. layer.attn_kv_a_norm = create_tensor(tn(LLM_TENSOR_ATTN_KV_A_NORM, "weight", i), {kv_lora_rank}, 0);
  3168. 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);
  3169. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), { n_head * ( n_embd_head_v), n_embd}, 0);
  3170. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3171. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3172. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3173. }
  3174. } break;
  3175. case LLM_ARCH_BITNET:
  3176. {
  3177. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3178. // output
  3179. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3180. for (int i = 0; i < n_layer; ++i) {
  3181. auto & layer = layers[i];
  3182. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3183. layer.attn_sub_norm = create_tensor(tn(LLM_TENSOR_ATTN_SUB_NORM, "weight", i), {n_embd}, 0);
  3184. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  3185. layer.wq_scale = create_tensor(tn(LLM_TENSOR_ATTN_Q, "scale", i), {1}, TENSOR_NOT_REQUIRED);
  3186. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  3187. layer.wk_scale = create_tensor(tn(LLM_TENSOR_ATTN_K, "scale", i), {1}, TENSOR_NOT_REQUIRED);
  3188. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  3189. layer.wv_scale = create_tensor(tn(LLM_TENSOR_ATTN_V, "scale", i), {1}, TENSOR_NOT_REQUIRED);
  3190. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  3191. layer.wo_scale = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "scale", i), {1}, TENSOR_NOT_REQUIRED);
  3192. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3193. layer.ffn_sub_norm = create_tensor(tn(LLM_TENSOR_FFN_SUB_NORM, "weight", i), {n_ff}, 0);
  3194. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  3195. layer.ffn_gate_scale = create_tensor(tn(LLM_TENSOR_FFN_GATE, "scale", i), {1}, TENSOR_NOT_REQUIRED);
  3196. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  3197. layer.ffn_down_scale = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "scale", i), {1}, TENSOR_NOT_REQUIRED);
  3198. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3199. layer.ffn_up_scale = create_tensor(tn(LLM_TENSOR_FFN_UP, "scale", i), {1}, TENSOR_NOT_REQUIRED);
  3200. }
  3201. } break;
  3202. case LLM_ARCH_T5:
  3203. {
  3204. const auto n_rel_attn_bkts = hparams.n_rel_attn_bkts;
  3205. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3206. // output
  3207. output_norm_enc = create_tensor(tn(LLM_TENSOR_ENC_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3208. output_norm = create_tensor(tn(LLM_TENSOR_DEC_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3209. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  3210. // if output is NULL, init from the input tok embed
  3211. if (output == NULL) {
  3212. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3213. }
  3214. for (int i = 0; i < n_layer; ++i) {
  3215. auto & layer = layers[i];
  3216. layer.attn_norm_enc = create_tensor(tn(LLM_TENSOR_ENC_ATTN_NORM, "weight", i), {n_embd}, 0);
  3217. 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);
  3218. layer.wq_enc = create_tensor(tn(LLM_TENSOR_ENC_ATTN_Q, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  3219. layer.wk_enc = create_tensor(tn(LLM_TENSOR_ENC_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  3220. layer.wv_enc = create_tensor(tn(LLM_TENSOR_ENC_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  3221. layer.wo_enc = create_tensor(tn(LLM_TENSOR_ENC_ATTN_OUT, "weight", i), {n_embd_v_gqa, n_embd}, 0);
  3222. layer.ffn_norm_enc = create_tensor(tn(LLM_TENSOR_ENC_FFN_NORM, "weight", i), {n_embd}, 0);
  3223. layer.ffn_gate_enc = create_tensor(tn(LLM_TENSOR_ENC_FFN_GATE, "weight", i), {n_embd, n_ff}, TENSOR_NOT_REQUIRED);
  3224. layer.ffn_down_enc = create_tensor(tn(LLM_TENSOR_ENC_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3225. layer.ffn_up_enc = create_tensor(tn(LLM_TENSOR_ENC_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3226. layer.attn_norm = create_tensor(tn(LLM_TENSOR_DEC_ATTN_NORM, "weight", i), {n_embd}, 0);
  3227. layer.attn_rel_b = create_tensor(tn(LLM_TENSOR_DEC_ATTN_REL_B, "weight", i), {n_head, n_rel_attn_bkts}, TENSOR_NOT_REQUIRED);
  3228. layer.wq = create_tensor(tn(LLM_TENSOR_DEC_ATTN_Q, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  3229. layer.wk = create_tensor(tn(LLM_TENSOR_DEC_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  3230. layer.wv = create_tensor(tn(LLM_TENSOR_DEC_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  3231. layer.wo = create_tensor(tn(LLM_TENSOR_DEC_ATTN_OUT, "weight", i), {n_embd_v_gqa, n_embd}, 0);
  3232. layer.attn_norm_cross = create_tensor(tn(LLM_TENSOR_DEC_CROSS_ATTN_NORM, "weight", i), {n_embd}, 0);
  3233. // this tensor seems to be unused in HF transformers implementation
  3234. 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);
  3235. layer.wq_cross = create_tensor(tn(LLM_TENSOR_DEC_CROSS_ATTN_Q, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  3236. layer.wk_cross = create_tensor(tn(LLM_TENSOR_DEC_CROSS_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  3237. layer.wv_cross = create_tensor(tn(LLM_TENSOR_DEC_CROSS_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  3238. layer.wo_cross = create_tensor(tn(LLM_TENSOR_DEC_CROSS_ATTN_OUT, "weight", i), {n_embd_v_gqa, n_embd}, 0);
  3239. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_DEC_FFN_NORM, "weight", i), {n_embd}, 0);
  3240. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_DEC_FFN_GATE, "weight", i), {n_embd, n_ff}, TENSOR_NOT_REQUIRED);
  3241. layer.ffn_down = create_tensor(tn(LLM_TENSOR_DEC_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3242. layer.ffn_up = create_tensor(tn(LLM_TENSOR_DEC_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3243. }
  3244. } break;
  3245. case LLM_ARCH_T5ENCODER:
  3246. {
  3247. const auto n_rel_attn_bkts = hparams.n_rel_attn_bkts;
  3248. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3249. // output
  3250. output_norm_enc = create_tensor(tn(LLM_TENSOR_ENC_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3251. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  3252. // if output is NULL, init from the input tok embed
  3253. if (output == NULL) {
  3254. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3255. }
  3256. for (int i = 0; i < n_layer; ++i) {
  3257. auto & layer = layers[i];
  3258. layer.attn_norm_enc = create_tensor(tn(LLM_TENSOR_ENC_ATTN_NORM, "weight", i), {n_embd}, 0);
  3259. 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);
  3260. layer.wq_enc = create_tensor(tn(LLM_TENSOR_ENC_ATTN_Q, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  3261. layer.wk_enc = create_tensor(tn(LLM_TENSOR_ENC_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  3262. layer.wv_enc = create_tensor(tn(LLM_TENSOR_ENC_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  3263. layer.wo_enc = create_tensor(tn(LLM_TENSOR_ENC_ATTN_OUT, "weight", i), {n_embd_v_gqa, n_embd}, 0);
  3264. layer.ffn_norm_enc = create_tensor(tn(LLM_TENSOR_ENC_FFN_NORM, "weight", i), {n_embd}, 0);
  3265. layer.ffn_gate_enc = create_tensor(tn(LLM_TENSOR_ENC_FFN_GATE, "weight", i), {n_embd, n_ff}, TENSOR_NOT_REQUIRED);
  3266. layer.ffn_down_enc = create_tensor(tn(LLM_TENSOR_ENC_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3267. layer.ffn_up_enc = create_tensor(tn(LLM_TENSOR_ENC_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3268. }
  3269. } break;
  3270. case LLM_ARCH_JAIS:
  3271. {
  3272. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3273. // output
  3274. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3275. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
  3276. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  3277. for (int i = 0; i < n_layer; ++i) {
  3278. auto & layer = layers[i];
  3279. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3280. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0);
  3281. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, 0);
  3282. layer.bqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd + 2*n_embd_gqa}, 0);
  3283. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  3284. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0);
  3285. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3286. layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd}, 0);
  3287. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  3288. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, 0);
  3289. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  3290. layer.ffn_gate_b = create_tensor(tn(LLM_TENSOR_FFN_GATE, "bias", i), {n_ff}, 0);
  3291. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3292. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, 0);
  3293. }
  3294. } break;
  3295. case LLM_ARCH_CHATGLM:
  3296. {
  3297. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3298. // output
  3299. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3300. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  3301. // if output is NULL, init from the input tok embed
  3302. if (output == NULL) {
  3303. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3304. }
  3305. for (int i = 0; i < n_layer; ++i) {
  3306. auto & layer = layers[i];
  3307. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3308. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, TENSOR_NOT_REQUIRED);
  3309. layer.bqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd + 2*n_embd_gqa}, TENSOR_NOT_REQUIRED);
  3310. if (layer.wqkv == nullptr) {
  3311. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  3312. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  3313. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  3314. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  3315. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED);
  3316. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED);
  3317. }
  3318. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  3319. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3320. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff * 2}, 0);
  3321. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
  3322. }
  3323. } break;
  3324. case LLM_ARCH_GLM4:
  3325. {
  3326. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3327. // output
  3328. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3329. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  3330. // if output is NULL, init from the input tok embed
  3331. if (output == NULL) {
  3332. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3333. }
  3334. for (int i = 0; i < n_layer; ++i) {
  3335. auto & layer = layers[i];
  3336. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3337. layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, TENSOR_NOT_REQUIRED);
  3338. layer.bqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd + 2*n_embd_gqa}, TENSOR_NOT_REQUIRED);
  3339. if (layer.wqkv == nullptr) {
  3340. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  3341. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  3342. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  3343. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  3344. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED);
  3345. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED);
  3346. }
  3347. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  3348. layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), {n_embd}, 0);
  3349. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3350. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3351. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff * 2}, 0);
  3352. layer.ffn_post_norm = create_tensor(tn(LLM_TENSOR_FFN_POST_NORM, "weight", i), {n_embd}, 0);
  3353. }
  3354. } break;
  3355. case LLM_ARCH_NEMOTRON:
  3356. {
  3357. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3358. // output
  3359. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3360. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
  3361. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  3362. for (int i = 0; i < n_layer; ++i) {
  3363. auto & layer = layers[i];
  3364. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3365. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0);
  3366. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  3367. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  3368. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  3369. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  3370. // optional bias tensors
  3371. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  3372. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED);
  3373. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED);
  3374. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  3375. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3376. layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd}, 0);
  3377. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3378. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3379. // optional MLP bias
  3380. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  3381. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, TENSOR_NOT_REQUIRED);
  3382. }
  3383. } break;
  3384. case LLM_ARCH_EXAONE:
  3385. {
  3386. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3387. // output
  3388. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3389. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  3390. // if output is NULL, init from the input tok embed
  3391. if (output == NULL) {
  3392. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3393. }
  3394. for (int i = 0; i < n_layer; ++i) {
  3395. auto & layer = layers[i];
  3396. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3397. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  3398. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  3399. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  3400. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  3401. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3402. layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  3403. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  3404. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3405. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3406. }
  3407. } break;
  3408. case LLM_ARCH_RWKV6:
  3409. {
  3410. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3411. // Block 0, LN0
  3412. tok_norm = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "weight"), {n_embd}, 0);
  3413. tok_norm_b = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "bias"), {n_embd}, 0);
  3414. // output
  3415. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3416. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
  3417. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  3418. const int time_mix_extra_dim = hparams.time_mix_extra_dim;
  3419. const int time_decay_extra_dim = hparams.time_decay_extra_dim;
  3420. const int head_size = hparams.wkv_head_size;
  3421. const int attn_hidden_size = n_embd;
  3422. const int ffn_size = hparams.n_ff_arr[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.attn_norm_2 = create_tensor(tn(LLM_TENSOR_ATTN_NORM_2, "weight", i), {n_embd}, 0);
  3428. layer.attn_norm_2_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM_2, "bias", i), {n_embd}, 0);
  3429. layer.time_mix_w1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_W1, "weight", i), {n_embd, time_mix_extra_dim * 5}, 0);
  3430. layer.time_mix_w2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_W2, "weight", i), {time_mix_extra_dim, n_embd, 5}, 0);
  3431. layer.time_mix_lerp_x = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_X, "weight", i), {n_embd, 1, 1}, 0);
  3432. layer.time_mix_lerp_w = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_W, "weight", i), {n_embd, 1, 1}, TENSOR_NOT_REQUIRED);
  3433. layer.time_mix_lerp_k = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_K, "weight", i), {n_embd, 1, 1}, TENSOR_NOT_REQUIRED);
  3434. layer.time_mix_lerp_v = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_V, "weight", i), {n_embd, 1, 1}, TENSOR_NOT_REQUIRED);
  3435. layer.time_mix_lerp_r = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_R, "weight", i), {n_embd, 1, 1}, TENSOR_NOT_REQUIRED);
  3436. layer.time_mix_lerp_g = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_G, "weight", i), {n_embd, 1, 1}, TENSOR_NOT_REQUIRED);
  3437. layer.time_mix_lerp_fused = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_FUSED, "weight", i), {n_embd, 1, 1, 5}, TENSOR_NOT_REQUIRED);
  3438. GGML_ASSERT(!(layer.time_mix_lerp_fused == NULL && layer.time_mix_lerp_w == NULL));
  3439. layer.time_mix_first = create_tensor(tn(LLM_TENSOR_TIME_MIX_FIRST, "weight", i), {head_size, n_embd / head_size}, 0);
  3440. layer.time_mix_decay = create_tensor(tn(LLM_TENSOR_TIME_MIX_DECAY, "weight", i), {n_embd}, 0);
  3441. layer.time_mix_decay_w1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_DECAY_W1, "weight", i), {n_embd, time_decay_extra_dim}, 0);
  3442. layer.time_mix_decay_w2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_DECAY_W2, "weight", i), {time_decay_extra_dim, attn_hidden_size}, 0);
  3443. layer.time_mix_key = create_tensor(tn(LLM_TENSOR_TIME_MIX_KEY, "weight", i), {attn_hidden_size, n_embd}, 0);
  3444. layer.time_mix_value = create_tensor(tn(LLM_TENSOR_TIME_MIX_VALUE, "weight", i), {attn_hidden_size, n_embd}, 0);
  3445. layer.time_mix_receptance = create_tensor(tn(LLM_TENSOR_TIME_MIX_RECEPTANCE, "weight", i), {attn_hidden_size, n_embd}, 0);
  3446. layer.time_mix_gate = create_tensor(tn(LLM_TENSOR_TIME_MIX_GATE, "weight", i), {attn_hidden_size, n_embd}, 0);
  3447. layer.time_mix_ln = create_tensor(tn(LLM_TENSOR_TIME_MIX_LN, "weight", i), {n_embd}, 0);
  3448. layer.time_mix_ln_b = create_tensor(tn(LLM_TENSOR_TIME_MIX_LN, "bias", i), {n_embd}, 0);
  3449. layer.time_mix_output = create_tensor(tn(LLM_TENSOR_TIME_MIX_OUTPUT, "weight", i), {n_embd, attn_hidden_size}, 0);
  3450. layer.channel_mix_lerp_k = create_tensor(tn(LLM_TENSOR_CHANNEL_MIX_LERP_K, "weight", i), {n_embd, 1, 1}, 0);
  3451. layer.channel_mix_lerp_r = create_tensor(tn(LLM_TENSOR_CHANNEL_MIX_LERP_R, "weight", i), {n_embd, 1, 1}, 0);
  3452. layer.channel_mix_key = create_tensor(tn(LLM_TENSOR_CHANNEL_MIX_KEY, "weight", i), {n_embd, ffn_size}, 0);
  3453. layer.channel_mix_value = create_tensor(tn(LLM_TENSOR_CHANNEL_MIX_VALUE, "weight", i), {ffn_size, n_embd}, 0);
  3454. layer.channel_mix_receptance = create_tensor(tn(LLM_TENSOR_CHANNEL_MIX_RECEPTANCE, "weight", i), {n_embd, n_embd}, 0);
  3455. }
  3456. } break;
  3457. case LLM_ARCH_RWKV6QWEN2:
  3458. {
  3459. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3460. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3461. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, TENSOR_NOT_REQUIRED);
  3462. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  3463. const int time_mix_extra_dim = hparams.time_mix_extra_dim;
  3464. const int time_decay_extra_dim = hparams.time_decay_extra_dim;
  3465. const int head_size = hparams.wkv_head_size;
  3466. const int attn_hidden_size = n_embd;
  3467. const int n_head_kv = hparams.n_head_kv();
  3468. int attn_key_value_size;
  3469. if (n_head_kv == 0 || attn_hidden_size / head_size == n_head_kv) {
  3470. attn_key_value_size = attn_hidden_size;
  3471. } else {
  3472. attn_key_value_size = n_head_kv * head_size;
  3473. }
  3474. for (int i = 0; i < n_layer; ++i) {
  3475. auto & layer = layers[i];
  3476. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3477. layer.time_mix_w1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_W1, "weight", i), {n_embd, time_mix_extra_dim * 5}, 0);
  3478. layer.time_mix_w2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_W2, "weight", i), {time_mix_extra_dim, n_embd, 5}, 0);
  3479. layer.time_mix_lerp_x = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_X, "weight", i), {n_embd, 1, 1}, 0);
  3480. layer.time_mix_lerp_fused = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_FUSED, "weight", i), {n_embd, 1, 1, 5}, 0);
  3481. layer.time_mix_first = create_tensor(tn(LLM_TENSOR_TIME_MIX_FIRST, "weight", i), {head_size, n_embd / head_size}, TENSOR_NOT_REQUIRED);
  3482. layer.time_mix_decay = create_tensor(tn(LLM_TENSOR_TIME_MIX_DECAY, "weight", i), {n_embd}, 0);
  3483. layer.time_mix_decay_w1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_DECAY_W1, "weight", i), {n_embd, time_decay_extra_dim}, 0);
  3484. layer.time_mix_decay_w2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_DECAY_W2, "weight", i), {time_decay_extra_dim, attn_hidden_size}, 0);
  3485. layer.time_mix_key = create_tensor(tn(LLM_TENSOR_TIME_MIX_KEY, "weight", i), {n_embd, attn_key_value_size}, 0);
  3486. layer.time_mix_value = create_tensor(tn(LLM_TENSOR_TIME_MIX_VALUE, "weight", i), {n_embd, attn_key_value_size}, 0);
  3487. layer.time_mix_receptance = create_tensor(tn(LLM_TENSOR_TIME_MIX_RECEPTANCE, "weight", i), {attn_hidden_size, n_embd}, 0);
  3488. layer.time_mix_gate = create_tensor(tn(LLM_TENSOR_TIME_MIX_GATE, "weight", i), {attn_hidden_size, n_embd}, 0);
  3489. // optional bias tensors
  3490. layer.time_mix_key_b = create_tensor(tn(LLM_TENSOR_TIME_MIX_KEY, "bias", i), {attn_key_value_size}, TENSOR_NOT_REQUIRED);
  3491. layer.time_mix_value_b = create_tensor(tn(LLM_TENSOR_TIME_MIX_VALUE, "bias", i), {attn_key_value_size}, TENSOR_NOT_REQUIRED);
  3492. layer.time_mix_receptance_b = create_tensor(tn(LLM_TENSOR_TIME_MIX_RECEPTANCE, "bias", i), {attn_hidden_size}, TENSOR_NOT_REQUIRED);
  3493. layer.time_mix_output = create_tensor(tn(LLM_TENSOR_TIME_MIX_OUTPUT, "weight", i), {n_embd, attn_hidden_size}, 0);
  3494. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3495. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  3496. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3497. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3498. }
  3499. } break;
  3500. case LLM_ARCH_RWKV7:
  3501. {
  3502. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3503. // Block 0, LN0
  3504. tok_norm = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "weight"), {n_embd}, 0);
  3505. tok_norm_b = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "bias"), {n_embd}, 0);
  3506. // output
  3507. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3508. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
  3509. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  3510. const int n_lora_decay = hparams.n_lora_decay;
  3511. const int n_lora_iclr = hparams.n_lora_iclr;
  3512. const int n_lora_value_res_mix = hparams.n_lora_value_res_mix;
  3513. const int n_lora_gate = hparams.n_lora_gate;
  3514. const int attn_hidden_size = n_embd;
  3515. const int ffn_size = hparams.n_ff_arr[0];
  3516. for (int i = 0; i < n_layer; ++i) {
  3517. auto & layer = layers[i];
  3518. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3519. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0);
  3520. layer.attn_norm_2 = create_tensor(tn(LLM_TENSOR_ATTN_NORM_2, "weight", i), {n_embd}, 0);
  3521. layer.attn_norm_2_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM_2, "bias", i), {n_embd}, 0);
  3522. layer.time_mix_w0 = create_tensor(tn(LLM_TENSOR_TIME_MIX_W0, "weight", i), {n_embd}, 0);
  3523. layer.time_mix_w1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_W1, "weight", i), {n_embd, n_lora_decay}, 0);
  3524. layer.time_mix_w2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_W2, "weight", i), {n_lora_decay, n_embd}, 0);
  3525. layer.time_mix_a0 = create_tensor(tn(LLM_TENSOR_TIME_MIX_A0, "weight", i), {n_embd}, 0);
  3526. layer.time_mix_a1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_A1, "weight", i), {n_embd, n_lora_iclr}, 0);
  3527. layer.time_mix_a2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_A2, "weight", i), {n_lora_iclr, n_embd}, 0);
  3528. if (i == 0) {
  3529. // actually not used
  3530. layer.time_mix_v0 = create_tensor(tn(LLM_TENSOR_TIME_MIX_V0, "weight", i), {n_embd}, 0);
  3531. layer.time_mix_v1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_V1, "weight", i), {n_embd, n_lora_iclr}, 0);
  3532. layer.time_mix_v2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_V2, "weight", i), {n_lora_iclr, n_embd}, 0);
  3533. } else {
  3534. layer.time_mix_v0 = create_tensor(tn(LLM_TENSOR_TIME_MIX_V0, "weight", i), {n_embd}, 0);
  3535. layer.time_mix_v1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_V1, "weight", i), {n_embd, n_lora_value_res_mix}, 0);
  3536. layer.time_mix_v2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_V2, "weight", i), {n_lora_value_res_mix, n_embd}, 0);
  3537. }
  3538. layer.time_mix_g1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_G1, "weight", i), {n_embd, n_lora_gate}, 0);
  3539. layer.time_mix_g2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_G2, "weight", i), {n_lora_gate, n_embd}, 0);
  3540. layer.time_mix_lerp_fused = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_FUSED, "weight", i), {n_embd, 1, 1, 6}, 0);
  3541. layer.time_mix_k_k = create_tensor(tn(LLM_TENSOR_TIME_MIX_K_K, "weight", i), {attn_hidden_size}, 0);
  3542. layer.time_mix_k_a = create_tensor(tn(LLM_TENSOR_TIME_MIX_K_A, "weight", i), {attn_hidden_size}, 0);
  3543. layer.time_mix_r_k = create_tensor(tn(LLM_TENSOR_TIME_MIX_R_K, "weight", i), {attn_hidden_size}, 0);
  3544. layer.time_mix_key = create_tensor(tn(LLM_TENSOR_TIME_MIX_KEY, "weight", i), {attn_hidden_size, n_embd}, 0);
  3545. layer.time_mix_value = create_tensor(tn(LLM_TENSOR_TIME_MIX_VALUE, "weight", i), {attn_hidden_size, n_embd}, 0);
  3546. layer.time_mix_receptance = create_tensor(tn(LLM_TENSOR_TIME_MIX_RECEPTANCE, "weight", i), {attn_hidden_size, n_embd}, 0);
  3547. layer.time_mix_ln = create_tensor(tn(LLM_TENSOR_TIME_MIX_LN, "weight", i), {n_embd}, 0);
  3548. layer.time_mix_ln_b = create_tensor(tn(LLM_TENSOR_TIME_MIX_LN, "bias", i), {n_embd}, 0);
  3549. layer.time_mix_output = create_tensor(tn(LLM_TENSOR_TIME_MIX_OUTPUT, "weight", i), {n_embd, attn_hidden_size}, 0);
  3550. layer.channel_mix_lerp_k = create_tensor(tn(LLM_TENSOR_CHANNEL_MIX_LERP_K, "weight", i), {n_embd, 1, 1}, 0);
  3551. layer.channel_mix_key = create_tensor(tn(LLM_TENSOR_CHANNEL_MIX_KEY, "weight", i), {n_embd, ffn_size}, 0);
  3552. layer.channel_mix_value = create_tensor(tn(LLM_TENSOR_CHANNEL_MIX_VALUE, "weight", i), {ffn_size, n_embd}, 0);
  3553. }
  3554. } break;
  3555. case LLM_ARCH_ARWKV7:
  3556. {
  3557. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3558. // output
  3559. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3560. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  3561. const int n_lora_decay = hparams.n_lora_decay;
  3562. const int n_lora_iclr = hparams.n_lora_iclr;
  3563. const int n_lora_value_res_mix = hparams.n_lora_value_res_mix;
  3564. const int n_lora_gate = hparams.n_lora_gate;
  3565. const int attn_hidden_size = n_embd;
  3566. for (int i = 0; i < n_layer; ++i) {
  3567. auto & layer = layers[i];
  3568. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3569. layer.time_mix_w0 = create_tensor(tn(LLM_TENSOR_TIME_MIX_W0, "weight", i), {n_embd}, 0);
  3570. layer.time_mix_w1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_W1, "weight", i), {n_embd, n_lora_decay}, 0);
  3571. layer.time_mix_w2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_W2, "weight", i), {n_lora_decay, n_embd}, 0);
  3572. layer.time_mix_a0 = create_tensor(tn(LLM_TENSOR_TIME_MIX_A0, "weight", i), {n_embd}, 0);
  3573. layer.time_mix_a1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_A1, "weight", i), {n_embd, n_lora_iclr}, 0);
  3574. layer.time_mix_a2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_A2, "weight", i), {n_lora_iclr, n_embd}, 0);
  3575. if (i == 0) {
  3576. // actually not used
  3577. layer.time_mix_v0 = create_tensor(tn(LLM_TENSOR_TIME_MIX_V0, "weight", i), {n_embd}, 0);
  3578. layer.time_mix_v1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_V1, "weight", i), {n_embd, n_lora_iclr}, 0);
  3579. layer.time_mix_v2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_V2, "weight", i), {n_lora_iclr, n_embd}, 0);
  3580. } else {
  3581. layer.time_mix_v0 = create_tensor(tn(LLM_TENSOR_TIME_MIX_V0, "weight", i), {n_embd}, 0);
  3582. layer.time_mix_v1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_V1, "weight", i), {n_embd, n_lora_value_res_mix}, 0);
  3583. layer.time_mix_v2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_V2, "weight", i), {n_lora_value_res_mix, n_embd}, 0);
  3584. }
  3585. layer.time_mix_g1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_G1, "weight", i), {n_embd, n_lora_gate}, TENSOR_NOT_REQUIRED);
  3586. layer.time_mix_g2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_G2, "weight", i), {n_lora_gate, n_embd}, TENSOR_NOT_REQUIRED);
  3587. try {
  3588. layer.time_mix_lerp_fused = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_FUSED, "weight", i), {n_embd, 1, 1, 6}, 0);
  3589. } catch(std::runtime_error & e) {
  3590. // ARWKV models may not have gate tensors
  3591. layer.time_mix_lerp_fused = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_FUSED, "weight", i), {n_embd, 1, 1, 5}, 0);
  3592. }
  3593. layer.time_mix_k_k = create_tensor(tn(LLM_TENSOR_TIME_MIX_K_K, "weight", i), {attn_hidden_size}, 0);
  3594. layer.time_mix_k_a = create_tensor(tn(LLM_TENSOR_TIME_MIX_K_A, "weight", i), {attn_hidden_size}, 0);
  3595. layer.time_mix_r_k = create_tensor(tn(LLM_TENSOR_TIME_MIX_R_K, "weight", i), {attn_hidden_size}, 0);
  3596. layer.time_mix_key = create_tensor(tn(LLM_TENSOR_TIME_MIX_KEY, "weight", i), {attn_hidden_size, n_embd}, 0);
  3597. layer.time_mix_value = create_tensor(tn(LLM_TENSOR_TIME_MIX_VALUE, "weight", i), {attn_hidden_size, n_embd}, 0);
  3598. layer.time_mix_receptance = create_tensor(tn(LLM_TENSOR_TIME_MIX_RECEPTANCE, "weight", i), {attn_hidden_size, n_embd}, 0);
  3599. layer.time_mix_ln = create_tensor(tn(LLM_TENSOR_TIME_MIX_LN, "weight", i), {n_embd}, TENSOR_NOT_REQUIRED);
  3600. layer.time_mix_ln_b = create_tensor(tn(LLM_TENSOR_TIME_MIX_LN, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  3601. layer.time_mix_output = create_tensor(tn(LLM_TENSOR_TIME_MIX_OUTPUT, "weight", i), {n_embd, attn_hidden_size}, 0);
  3602. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3603. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  3604. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3605. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3606. }
  3607. } break;
  3608. case LLM_ARCH_CHAMELEON:
  3609. {
  3610. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3611. // output
  3612. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3613. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  3614. // if output is NULL, init from the input tok embed
  3615. if (output == NULL) {
  3616. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3617. }
  3618. for (int i = 0; i < n_layer; ++i) {
  3619. auto & layer = layers[i];
  3620. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3621. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k, n_head}, 0);
  3622. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k, n_head_kv}, 0);
  3623. layer.attn_q_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "bias", i), {n_embd_head_k, n_head}, TENSOR_NOT_REQUIRED);
  3624. 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);
  3625. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0);
  3626. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  3627. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  3628. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
  3629. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3630. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  3631. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3632. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3633. }
  3634. } break;
  3635. case LLM_ARCH_WAVTOKENIZER_DEC:
  3636. {
  3637. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {hparams.n_embd_features, n_vocab}, 0);
  3638. conv1d = create_tensor(tn(LLM_TENSOR_CONV1D, "weight"), {7, hparams.n_embd_features, hparams.posnet.n_embd}, 0);
  3639. conv1d_b = create_tensor(tn(LLM_TENSOR_CONV1D, "bias"), {1, hparams.posnet.n_embd}, 0);
  3640. // posnet
  3641. {
  3642. const int64_t n_embd = hparams.posnet.n_embd;
  3643. for (uint32_t i = 0; i < hparams.posnet.n_layer; ++i) {
  3644. auto & layer = layers[i].posnet;
  3645. // posnet:
  3646. //
  3647. // - resnet
  3648. // - resnet
  3649. // - attn
  3650. // - resnet
  3651. // - resnet
  3652. // - norm
  3653. //
  3654. switch (i) {
  3655. case 0:
  3656. case 1:
  3657. case 3:
  3658. case 4:
  3659. {
  3660. layer.norm1 = create_tensor(tn(LLM_TENSOR_POS_NET_NORM1, "weight", i), {1, n_embd}, 0);
  3661. layer.norm1_b = create_tensor(tn(LLM_TENSOR_POS_NET_NORM1, "bias", i), {1, n_embd}, 0);
  3662. layer.conv1 = create_tensor(tn(LLM_TENSOR_POS_NET_CONV1, "weight", i), {3, n_embd, n_embd}, 0);
  3663. layer.conv1_b = create_tensor(tn(LLM_TENSOR_POS_NET_CONV1, "bias", i), {1, n_embd}, 0);
  3664. layer.norm2 = create_tensor(tn(LLM_TENSOR_POS_NET_NORM2, "weight", i), {1, n_embd}, 0);
  3665. layer.norm2_b = create_tensor(tn(LLM_TENSOR_POS_NET_NORM2, "bias", i), {1, n_embd}, 0);
  3666. layer.conv2 = create_tensor(tn(LLM_TENSOR_POS_NET_CONV2, "weight", i), {3, n_embd, n_embd}, 0);
  3667. layer.conv2_b = create_tensor(tn(LLM_TENSOR_POS_NET_CONV2, "bias", i), {1, n_embd}, 0);
  3668. } break;
  3669. case 2:
  3670. {
  3671. layer.attn_norm = create_tensor(tn(LLM_TENSOR_POS_NET_ATTN_NORM, "weight", i), {1, n_embd}, 0);
  3672. layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_POS_NET_ATTN_NORM, "bias", i), {1, n_embd}, 0);
  3673. layer.attn_q = create_tensor(tn(LLM_TENSOR_POS_NET_ATTN_Q, "weight", i), {1, n_embd, n_embd}, 0);
  3674. layer.attn_q_b = create_tensor(tn(LLM_TENSOR_POS_NET_ATTN_Q, "bias", i), {1, n_embd}, 0);
  3675. layer.attn_k = create_tensor(tn(LLM_TENSOR_POS_NET_ATTN_K, "weight", i), {1, n_embd, n_embd}, 0);
  3676. layer.attn_k_b = create_tensor(tn(LLM_TENSOR_POS_NET_ATTN_K, "bias", i), {1, n_embd}, 0);
  3677. layer.attn_v = create_tensor(tn(LLM_TENSOR_POS_NET_ATTN_V, "weight", i), {1, n_embd, n_embd}, 0);
  3678. layer.attn_v_b = create_tensor(tn(LLM_TENSOR_POS_NET_ATTN_V, "bias", i), {1, n_embd}, 0);
  3679. layer.attn_o = create_tensor(tn(LLM_TENSOR_POS_NET_ATTN_OUT, "weight", i), {1, n_embd, n_embd}, 0);
  3680. layer.attn_o_b = create_tensor(tn(LLM_TENSOR_POS_NET_ATTN_OUT, "bias", i), {1, n_embd}, 0);
  3681. } break;
  3682. case 5:
  3683. {
  3684. layer.norm = create_tensor(tn(LLM_TENSOR_POS_NET_ATTN_NORM, "weight", i), {1, n_embd}, 0);
  3685. layer.norm_b = create_tensor(tn(LLM_TENSOR_POS_NET_ATTN_NORM, "bias", i), {1, n_embd}, 0);
  3686. } break;
  3687. default: GGML_ABORT("unknown posnet layer");
  3688. };
  3689. }
  3690. }
  3691. GGML_ASSERT(hparams.posnet.n_embd == hparams.convnext.n_embd);
  3692. tok_norm = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "weight"), {hparams.posnet.n_embd}, 0);
  3693. tok_norm_b = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "bias"), {hparams.posnet.n_embd}, 0);
  3694. // convnext
  3695. {
  3696. const int64_t n_embd = hparams.convnext.n_embd;
  3697. for (uint32_t i = 0; i < hparams.convnext.n_layer; ++i) {
  3698. auto & layer = layers[i].convnext;
  3699. layer.dw = create_tensor(tn(LLM_TENSOR_CONVNEXT_DW, "weight", i), {7, 1, n_embd}, 0);
  3700. layer.dw_b = create_tensor(tn(LLM_TENSOR_CONVNEXT_DW, "bias", i), {1, n_embd}, 0);
  3701. layer.norm = create_tensor(tn(LLM_TENSOR_CONVNEXT_NORM, "weight", i), {n_embd}, 0);
  3702. layer.norm_b = create_tensor(tn(LLM_TENSOR_CONVNEXT_NORM, "bias", i), {n_embd}, 0);
  3703. layer.pw1 = create_tensor(tn(LLM_TENSOR_CONVNEXT_PW1, "weight", i), {n_embd, n_ff}, 0);
  3704. layer.pw1_b = create_tensor(tn(LLM_TENSOR_CONVNEXT_PW1, "bias", i), {n_ff}, 0);
  3705. layer.pw2 = create_tensor(tn(LLM_TENSOR_CONVNEXT_PW2, "weight", i), {n_ff, n_embd}, 0);
  3706. layer.pw2_b = create_tensor(tn(LLM_TENSOR_CONVNEXT_PW2, "bias", i), {n_embd}, 0);
  3707. layer.gamma = create_tensor(tn(LLM_TENSOR_CONVNEXT_GAMMA, "weight", i), {n_embd}, 0);
  3708. }
  3709. // output
  3710. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3711. output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);
  3712. }
  3713. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {hparams.convnext.n_embd, n_embd}, 0);
  3714. output_b = create_tensor(tn(LLM_TENSOR_OUTPUT, "bias"), {n_embd}, 0);
  3715. } break;
  3716. case LLM_ARCH_BAILINGMOE:
  3717. {
  3718. const int64_t n_ff_exp = hparams.n_ff_exp;
  3719. const int64_t n_expert_shared = hparams.n_expert_shared;
  3720. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3721. // output
  3722. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3723. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  3724. for (int i = 0; i < n_layer; ++i) {
  3725. auto & layer = layers[i];
  3726. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3727. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_head * n_rot}, 0);
  3728. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_head_kv * n_rot}, 0);
  3729. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_head_kv * n_rot}, 0);
  3730. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_head * n_rot, n_embd}, 0);
  3731. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3732. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  3733. if (n_expert == 0) {
  3734. throw std::runtime_error("n_expert must be > 0");
  3735. }
  3736. if (n_expert_used == 0) {
  3737. throw std::runtime_error("n_expert_used must be > 0");
  3738. }
  3739. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);
  3740. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp, n_embd, n_expert}, 0);
  3741. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);
  3742. layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), {n_embd, n_ff_exp * n_expert_shared}, 0);
  3743. layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), { n_ff_exp * n_expert_shared, n_embd}, 0);
  3744. layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), {n_embd, n_ff_exp * n_expert_shared}, 0);
  3745. }
  3746. } break;
  3747. case LLM_ARCH_DOTS1:
  3748. {
  3749. const int64_t n_ff_exp = hparams.n_ff_exp;
  3750. const int64_t n_expert_shared = hparams.n_expert_shared;
  3751. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3752. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3753. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0);
  3754. for (int i = 0; i < n_layer; ++i) {
  3755. auto & layer = layers[i];
  3756. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3757. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  3758. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  3759. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  3760. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  3761. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0);
  3762. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0);
  3763. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3764. if (i < (int) hparams.n_layer_dense_lead) {
  3765. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  3766. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3767. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3768. } else {
  3769. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  3770. layer.ffn_exp_probs_b = create_tensor(tn(LLM_TENSOR_FFN_EXP_PROBS_B, "bias", i), {n_expert}, TENSOR_NOT_REQUIRED);
  3771. if (n_expert == 0) {
  3772. throw std::runtime_error("n_expert must be > 0");
  3773. }
  3774. if (n_expert_used == 0) {
  3775. throw std::runtime_error("n_expert_used must be > 0");
  3776. }
  3777. // MoE branch
  3778. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);
  3779. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp, n_embd, n_expert}, 0);
  3780. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0);
  3781. // Shared expert branch
  3782. layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), {n_embd, n_ff_exp * n_expert_shared}, 0);
  3783. layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), { n_ff_exp * n_expert_shared, n_embd}, 0);
  3784. layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), {n_embd, n_ff_exp * n_expert_shared}, 0);
  3785. }
  3786. }
  3787. } break;
  3788. case LLM_ARCH_ARCEE:
  3789. {
  3790. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3791. // output
  3792. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3793. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  3794. // if output is NULL, init from the input tok embed
  3795. if (output == NULL) {
  3796. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3797. }
  3798. for (int i = 0; i < n_layer; ++i) {
  3799. auto & layer = layers[i];
  3800. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3801. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  3802. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  3803. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  3804. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  3805. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3806. layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  3807. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3808. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3809. }
  3810. } break;
  3811. case LLM_ARCH_ERNIE4_5:
  3812. {
  3813. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3814. // output
  3815. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3816. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  3817. // if output is NULL, init from the input tok embed
  3818. if (output == NULL) {
  3819. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3820. }
  3821. for (int i = 0; i < n_layer; ++i) {
  3822. auto & layer = layers[i];
  3823. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3824. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  3825. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0);
  3826. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0);
  3827. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  3828. // optional bias tensors
  3829. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  3830. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED);
  3831. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED);
  3832. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
  3833. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3834. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  3835. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3836. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3837. }
  3838. } break;
  3839. case LLM_ARCH_FALCON_H1:
  3840. {
  3841. // Common
  3842. const int64_t hidden_size = hparams.n_embd; // hidden_size
  3843. // mamba2 Mixer SSM params
  3844. const int64_t ssm_conv_kernel_size = hparams.ssm_d_conv; // ssm_conv_kernel_size
  3845. const int64_t ssm_n_groups = hparams.ssm_n_group; // ssm_n_groups
  3846. const int64_t ssm_state_size = hparams.ssm_d_state; // ssm_state_size
  3847. const int64_t ssm_intermediate_size = hparams.ssm_d_inner; // TODO expand
  3848. const int64_t ssm_num_heads = hparams.ssm_dt_rank; // ssm_num_heads
  3849. const int64_t ssm_conv_dim = ssm_intermediate_size + 2 * ssm_n_groups * ssm_state_size;
  3850. const int64_t ssm_projection_size = ssm_intermediate_size + ssm_conv_dim + ssm_num_heads;
  3851. // attn params
  3852. const int64_t attn_num_attention_head = hparams.n_head(0); // rename to: attn_num_attention_head
  3853. const int64_t attn_num_key_value_head = hparams.n_head_kv(0);
  3854. // ffn params
  3855. const int64_t ffn_intermediate_size = hparams.n_ff(0);
  3856. // embeddings
  3857. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {hidden_size, n_vocab}, 0);
  3858. // output
  3859. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {hidden_size, n_vocab}, TENSOR_NOT_REQUIRED);
  3860. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {hidden_size}, 0);
  3861. // if output is NULL, init from the input tok embed
  3862. if (output == NULL) {
  3863. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {hidden_size, n_vocab}, TENSOR_DUPLICATED);
  3864. }
  3865. for (int i = 0; i < n_layer; ++i) {
  3866. auto & layer = layers[i];
  3867. /*SSM LAYERS*/
  3868. // ssm in
  3869. layer.ssm_in = create_tensor(tn(LLM_TENSOR_SSM_IN, "weight", i), {hidden_size, ssm_projection_size}, 0);
  3870. // ssm 1d conv
  3871. layer.ssm_conv1d = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "weight", i), {ssm_conv_kernel_size, ssm_conv_dim}, 0);
  3872. layer.ssm_conv1d_b = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "bias", i), {ssm_conv_dim}, TENSOR_NOT_REQUIRED);
  3873. // ssm_dt
  3874. layer.ssm_dt_b = create_tensor(tn(LLM_TENSOR_SSM_DT, "bias", i), {ssm_num_heads}, 0);
  3875. // no "weight" suffix for these
  3876. layer.ssm_a = create_tensor(tn(LLM_TENSOR_SSM_A, i), {1, ssm_num_heads}, 0);
  3877. layer.ssm_d = create_tensor(tn(LLM_TENSOR_SSM_D, i), {1, ssm_num_heads}, 0);
  3878. // ssm_norm
  3879. layer.ssm_norm = create_tensor(tn(LLM_TENSOR_SSM_NORM, "weight", i), {ssm_intermediate_size / ssm_n_groups, ssm_n_groups}, TENSOR_NOT_REQUIRED);
  3880. // out_proj
  3881. layer.ssm_out = create_tensor(tn(LLM_TENSOR_SSM_OUT, "weight", i), {ssm_intermediate_size, hidden_size}, 0);
  3882. /*ATTENTION LAYERS*/
  3883. // attention layers (with optional bias)
  3884. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {hidden_size, n_embd_head_k * attn_num_attention_head}, 0);
  3885. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {hidden_size, attn_num_key_value_head * n_embd_head_k}, 0);
  3886. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {hidden_size, attn_num_key_value_head * n_embd_head_v}, 0);
  3887. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * attn_num_attention_head, hidden_size}, 0);
  3888. layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {hidden_size}, TENSOR_NOT_REQUIRED);
  3889. layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {attn_num_key_value_head * n_embd_head_k}, TENSOR_NOT_REQUIRED);
  3890. layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {attn_num_key_value_head * n_embd_head_v}, TENSOR_NOT_REQUIRED);
  3891. layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {hidden_size}, TENSOR_NOT_REQUIRED);
  3892. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {hidden_size}, 0);
  3893. // feed forward (w/ optional biases)
  3894. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, i), {hidden_size}, 0);
  3895. layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
  3896. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {hidden_size, ffn_intermediate_size}, 0);
  3897. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { ffn_intermediate_size, hidden_size}, 0);
  3898. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {hidden_size, ffn_intermediate_size}, 0);
  3899. layer.ffn_gate_b = create_tensor(tn(LLM_TENSOR_FFN_GATE, "bias", i), {ffn_intermediate_size}, TENSOR_NOT_REQUIRED);
  3900. layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {hidden_size}, TENSOR_NOT_REQUIRED);
  3901. layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {ffn_intermediate_size}, TENSOR_NOT_REQUIRED);
  3902. }
  3903. } break;
  3904. case LLM_ARCH_HUNYUAN_MOE:
  3905. {
  3906. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3907. // output
  3908. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3909. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  3910. // if output is NULL, init from the input tok embed
  3911. if (output == NULL) {
  3912. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3913. }
  3914. for (int i = 0; i < n_layer; ++i) {
  3915. auto & layer = layers[i];
  3916. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3917. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  3918. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  3919. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  3920. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  3921. layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0);
  3922. layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0);
  3923. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3924. layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
  3925. layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0);
  3926. layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), { n_ff, n_embd, n_expert}, 0);
  3927. layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0);
  3928. layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), {n_embd, hparams.n_ff_shexp}, 0);
  3929. layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), {n_embd, hparams.n_ff_shexp}, 0);
  3930. layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), {hparams.n_ff_shexp, n_embd}, 0);
  3931. }
  3932. } break;
  3933. case LLM_ARCH_SMOLLM3:
  3934. {
  3935. tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
  3936. // output
  3937. output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
  3938. output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
  3939. // if output is NULL, init from the input tok embed
  3940. if (output == NULL) {
  3941. output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
  3942. }
  3943. for (int i = 0; i < n_layer; ++i) {
  3944. auto & layer = layers[i];
  3945. layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
  3946. layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
  3947. layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0);
  3948. layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0);
  3949. layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
  3950. layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
  3951. layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
  3952. layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);
  3953. layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
  3954. }
  3955. } break;
  3956. default:
  3957. throw std::runtime_error("unknown architecture");
  3958. }
  3959. if (n_moved_tensors > 0) {
  3960. LLAMA_LOG_DEBUG("%s: tensor '%s' (%s) (and %d others) cannot be used with preferred buffer type %s, using %s instead\n",
  3961. __func__, first_moved_tensor->name, ggml_type_name(first_moved_tensor->type), n_moved_tensors - 1,
  3962. ggml_backend_buft_name(first_moved_from_buft), ggml_backend_buft_name(first_moved_to_buft));
  3963. }
  3964. }
  3965. ml.done_getting_tensors();
  3966. ml.init_mappings(true, use_mlock ? &pimpl->mlock_mmaps : nullptr);
  3967. pimpl->mappings.reserve(ml.mappings.size());
  3968. // create the backend buffers
  3969. std::vector<std::pair<ggml_context *, llama_buf_map>> ctx_bufs;
  3970. ctx_bufs.reserve(ctx_map.size());
  3971. // Ensure we have enough capacity for the maximum backend buffer we will potentially create
  3972. const size_t n_max_backend_buffer = ctx_map.size() * ml.files.size();
  3973. pimpl->bufs.reserve(n_max_backend_buffer);
  3974. for (auto & it : ctx_map) {
  3975. ggml_backend_buffer_type_t buft = it.first;
  3976. ggml_context * ctx = it.second;
  3977. // skip contexts without tensors
  3978. if (ggml_get_first_tensor(ctx) == nullptr) {
  3979. continue;
  3980. }
  3981. llama_buf_map buf_map;
  3982. buf_map.reserve(n_max_backend_buffer);
  3983. // check if it is possible to use buffer_from_host_ptr with this buffer type
  3984. ggml_backend_dev_t dev = ggml_backend_buft_get_device(buft);
  3985. if (!dev) {
  3986. // FIXME: workaround for CPU backend buft having a NULL device
  3987. dev = ggml_backend_dev_by_type(GGML_BACKEND_DEVICE_TYPE_CPU);
  3988. if (!dev) {
  3989. throw std::runtime_error(format("%s: no CPU backend found", __func__));
  3990. }
  3991. }
  3992. ggml_backend_dev_props props;
  3993. ggml_backend_dev_get_props(dev, &props);
  3994. bool buffer_from_host_ptr_supported = props.caps.buffer_from_host_ptr;
  3995. bool is_default_buft = buft == ggml_backend_dev_buffer_type(dev);
  3996. if (ml.use_mmap && use_mmap_buffer && buffer_from_host_ptr_supported && is_default_buft) {
  3997. for (uint32_t idx = 0; idx < ml.files.size(); idx++) {
  3998. // only the mmap region containing the tensors in the model is mapped to the backend buffer
  3999. // 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
  4000. // this allows using partial offloading when the model size exceeds the metal buffer size, but not the RAM size
  4001. void * addr = nullptr;
  4002. size_t first, last; // NOLINT
  4003. ml.get_mapping_range(&first, &last, &addr, idx, ctx);
  4004. if (first >= last) {
  4005. continue;
  4006. }
  4007. const size_t max_size = ggml_get_max_tensor_size(ctx);
  4008. ggml_backend_buffer_t buf = ggml_backend_dev_buffer_from_host_ptr(dev, (char *) addr + first, last - first, max_size);
  4009. if (buf == nullptr) {
  4010. throw std::runtime_error(format("unable to allocate %s buffer", ggml_backend_buft_name(buft)));
  4011. }
  4012. pimpl->bufs.emplace_back(buf);
  4013. buf_map.emplace(idx, buf);
  4014. }
  4015. }
  4016. else {
  4017. ggml_backend_buffer_t buf = ggml_backend_alloc_ctx_tensors_from_buft(ctx, buft);
  4018. if (buf == nullptr) {
  4019. throw std::runtime_error(format("unable to allocate %s buffer", ggml_backend_buft_name(buft)));
  4020. }
  4021. pimpl->bufs.emplace_back(buf);
  4022. if (use_mlock && ggml_backend_buffer_is_host(buf)) {
  4023. pimpl->mlock_bufs.emplace_back(new llama_mlock);
  4024. auto & mlock_buf = pimpl->mlock_bufs.back();
  4025. mlock_buf->init (ggml_backend_buffer_get_base(buf));
  4026. mlock_buf->grow_to(ggml_backend_buffer_get_size(buf));
  4027. }
  4028. for (uint32_t idx = 0; idx < ml.files.size(); idx++) {
  4029. buf_map.emplace(idx, buf);
  4030. }
  4031. }
  4032. if (pimpl->bufs.empty()) {
  4033. throw std::runtime_error("failed to allocate buffer");
  4034. }
  4035. for (auto & buf : buf_map) {
  4036. // indicate that this buffer contains weights
  4037. // 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
  4038. ggml_backend_buffer_set_usage(buf.second, GGML_BACKEND_BUFFER_USAGE_WEIGHTS);
  4039. }
  4040. ctx_bufs.emplace_back(ctx, buf_map);
  4041. }
  4042. if (llama_supports_gpu_offload()) {
  4043. const int n_gpu = std::min(n_gpu_layers, int(hparams.n_layer));
  4044. LLAMA_LOG_INFO("%s: offloading %d repeating layers to GPU\n", __func__, n_gpu);
  4045. if (n_gpu_layers > (int) hparams.n_layer) {
  4046. LLAMA_LOG_INFO("%s: offloading output layer to GPU\n", __func__);
  4047. }
  4048. const int max_backend_supported_layers = hparams.n_layer + 1;
  4049. const int max_offloadable_layers = hparams.n_layer + 1;
  4050. LLAMA_LOG_INFO("%s: offloaded %d/%d layers to GPU\n", __func__, std::min(n_gpu_layers, max_offloadable_layers), max_backend_supported_layers);
  4051. }
  4052. // print memory requirements per buffer type
  4053. for (auto & buf : pimpl->bufs) {
  4054. 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);
  4055. }
  4056. // populate tensors_by_name
  4057. for (auto & ctx : pimpl->ctxs) {
  4058. for (auto * cur = ggml_get_first_tensor(ctx.get()); cur != NULL; cur = ggml_get_next_tensor(ctx.get(), cur)) {
  4059. tensors_by_name.emplace_back(ggml_get_name(cur), cur);
  4060. }
  4061. }
  4062. // load tensor data
  4063. for (auto & it : ctx_bufs) {
  4064. ggml_context * ctx = it.first;
  4065. auto & bufs = it.second;
  4066. if (!ml.load_all_data(ctx, bufs, use_mlock ? &pimpl->mlock_mmaps : NULL, params.progress_callback, params.progress_callback_user_data)) {
  4067. return false;
  4068. }
  4069. }
  4070. if (use_mmap_buffer) {
  4071. for (auto & mapping : ml.mappings) {
  4072. pimpl->mappings.emplace_back(std::move(mapping));
  4073. }
  4074. }
  4075. return true;
  4076. }
  4077. std::string llama_model::arch_name() const {
  4078. return llm_arch_name(arch);
  4079. }
  4080. std::string llama_model::type_name() const {
  4081. return llm_type_name(type);
  4082. }
  4083. std::string llama_model::desc() const {
  4084. return pimpl->desc_str;
  4085. }
  4086. size_t llama_model::size() const {
  4087. return pimpl->n_bytes;
  4088. }
  4089. size_t llama_model::n_tensors() const {
  4090. return tensors_by_name.size();
  4091. }
  4092. size_t llama_model::n_devices() const {
  4093. return devices.size();
  4094. }
  4095. uint64_t llama_model::n_elements() const {
  4096. return pimpl->n_elements;
  4097. }
  4098. void llama_model::print_info() const {
  4099. const std::string rope_scaling_type = llama_rope_scaling_type_name(hparams.rope_scaling_type_train);
  4100. auto print_f = [](const std::function<uint32_t(uint32_t)> & f, uint32_t n) {
  4101. bool is_var = false;
  4102. std::vector<uint32_t> v;
  4103. for (uint32_t i = 0; i < n; ++i) {
  4104. v.push_back(f(i));
  4105. if (v[i] != v[0]) {
  4106. is_var = true;
  4107. }
  4108. }
  4109. std::stringstream ss;
  4110. if (is_var) {
  4111. ss << "[";
  4112. for (uint32_t i = 0; i < n; ++i) {
  4113. ss << v[i];
  4114. if (i < n - 1) {
  4115. ss << ", ";
  4116. }
  4117. }
  4118. ss << "]";
  4119. } else {
  4120. ss << v[0];
  4121. }
  4122. return ss.str();
  4123. };
  4124. // hparams
  4125. LLAMA_LOG_INFO("%s: arch = %s\n", __func__, arch_name().c_str());
  4126. LLAMA_LOG_INFO("%s: vocab_only = %d\n", __func__, hparams.vocab_only);
  4127. if (!hparams.vocab_only) {
  4128. LLAMA_LOG_INFO("%s: n_ctx_train = %u\n", __func__, hparams.n_ctx_train);
  4129. LLAMA_LOG_INFO("%s: n_embd = %u\n", __func__, hparams.n_embd);
  4130. LLAMA_LOG_INFO("%s: n_layer = %u\n", __func__, hparams.n_layer);
  4131. LLAMA_LOG_INFO("%s: n_head = %s\n", __func__, print_f([&](uint32_t il) { return hparams.n_head(il); }, hparams.n_layer).c_str());
  4132. 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());
  4133. LLAMA_LOG_INFO("%s: n_rot = %u\n", __func__, hparams.n_rot);
  4134. LLAMA_LOG_INFO("%s: n_swa = %u\n", __func__, hparams.n_swa);
  4135. LLAMA_LOG_INFO("%s: is_swa_any = %u\n", __func__, hparams.is_swa_any());
  4136. LLAMA_LOG_INFO("%s: n_embd_head_k = %u\n", __func__, hparams.n_embd_head_k);
  4137. LLAMA_LOG_INFO("%s: n_embd_head_v = %u\n", __func__, hparams.n_embd_head_v);
  4138. LLAMA_LOG_INFO("%s: n_gqa = %s\n", __func__, print_f([&](uint32_t il) { return hparams.n_gqa(il); }, hparams.n_layer).c_str());
  4139. 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());
  4140. 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());
  4141. LLAMA_LOG_INFO("%s: f_norm_eps = %.1e\n", __func__, hparams.f_norm_eps);
  4142. LLAMA_LOG_INFO("%s: f_norm_rms_eps = %.1e\n", __func__, hparams.f_norm_rms_eps);
  4143. LLAMA_LOG_INFO("%s: f_clamp_kqv = %.1e\n", __func__, hparams.f_clamp_kqv);
  4144. LLAMA_LOG_INFO("%s: f_max_alibi_bias = %.1e\n", __func__, hparams.f_max_alibi_bias);
  4145. LLAMA_LOG_INFO("%s: f_logit_scale = %.1e\n", __func__, hparams.f_logit_scale);
  4146. LLAMA_LOG_INFO("%s: f_attn_scale = %.1e\n", __func__, hparams.f_attention_scale);
  4147. LLAMA_LOG_INFO("%s: n_ff = %s\n", __func__, print_f([&](uint32_t il) { return hparams.n_ff(il); }, hparams.n_layer).c_str());
  4148. LLAMA_LOG_INFO("%s: n_expert = %u\n", __func__, hparams.n_expert);
  4149. LLAMA_LOG_INFO("%s: n_expert_used = %u\n", __func__, hparams.n_expert_used);
  4150. LLAMA_LOG_INFO("%s: causal attn = %d\n", __func__, hparams.causal_attn);
  4151. LLAMA_LOG_INFO("%s: pooling type = %d\n", __func__, hparams.pooling_type);
  4152. LLAMA_LOG_INFO("%s: rope type = %d\n", __func__, hparams.rope_type);
  4153. LLAMA_LOG_INFO("%s: rope scaling = %s\n", __func__, rope_scaling_type.c_str());
  4154. LLAMA_LOG_INFO("%s: freq_base_train = %.1f\n", __func__, hparams.rope_freq_base_train);
  4155. LLAMA_LOG_INFO("%s: freq_scale_train = %g\n", __func__, hparams.rope_freq_scale_train);
  4156. LLAMA_LOG_INFO("%s: n_ctx_orig_yarn = %u\n", __func__, hparams.n_ctx_orig_yarn);
  4157. LLAMA_LOG_INFO("%s: rope_finetuned = %s\n", __func__, hparams.rope_finetuned ? "yes" : "unknown");
  4158. if (!classifier_labels.empty()) {
  4159. LLAMA_LOG_INFO("%s: n_cls_out = %u\n", __func__, hparams.n_cls_out);
  4160. size_t i = 0;
  4161. for (auto label : classifier_labels) {
  4162. LLAMA_LOG_INFO("%s: cls_label[%2zu] = %s\n", __func__, i++, label.c_str());
  4163. }
  4164. }
  4165. }
  4166. if (arch == LLM_ARCH_MAMBA ||
  4167. arch == LLM_ARCH_MAMBA2 ||
  4168. arch == LLM_ARCH_JAMBA ||
  4169. arch == LLM_ARCH_FALCON_H1) {
  4170. LLAMA_LOG_INFO("%s: ssm_d_conv = %u\n", __func__, hparams.ssm_d_conv);
  4171. LLAMA_LOG_INFO("%s: ssm_d_inner = %u\n", __func__, hparams.ssm_d_inner);
  4172. LLAMA_LOG_INFO("%s: ssm_d_state = %u\n", __func__, hparams.ssm_d_state);
  4173. LLAMA_LOG_INFO("%s: ssm_dt_rank = %u\n", __func__, hparams.ssm_dt_rank);
  4174. LLAMA_LOG_INFO("%s: ssm_n_group = %u\n", __func__, hparams.ssm_n_group);
  4175. LLAMA_LOG_INFO("%s: ssm_dt_b_c_rms = %d\n", __func__, hparams.ssm_dt_b_c_rms);
  4176. }
  4177. LLAMA_LOG_INFO("%s: model type = %s\n", __func__, type_name().c_str());
  4178. if (pimpl->n_elements >= 1e12) {
  4179. LLAMA_LOG_INFO("%s: model params = %.2f T\n", __func__, pimpl->n_elements*1e-12);
  4180. } else if (pimpl->n_elements >= 1e9) {
  4181. LLAMA_LOG_INFO("%s: model params = %.2f B\n", __func__, pimpl->n_elements*1e-9);
  4182. } else if (pimpl->n_elements >= 1e6) {
  4183. LLAMA_LOG_INFO("%s: model params = %.2f M\n", __func__, pimpl->n_elements*1e-6);
  4184. } else {
  4185. LLAMA_LOG_INFO("%s: model params = %.2f K\n", __func__, pimpl->n_elements*1e-3);
  4186. }
  4187. // general kv
  4188. LLAMA_LOG_INFO("%s: general.name = %s\n", __func__, name.c_str());
  4189. if (arch == LLM_ARCH_DEEPSEEK) {
  4190. LLAMA_LOG_INFO("%s: n_layer_dense_lead = %d\n", __func__, hparams.n_layer_dense_lead);
  4191. LLAMA_LOG_INFO("%s: n_ff_exp = %d\n", __func__, hparams.n_ff_exp);
  4192. LLAMA_LOG_INFO("%s: n_expert_shared = %d\n", __func__, hparams.n_expert_shared);
  4193. LLAMA_LOG_INFO("%s: expert_weights_scale = %.1f\n", __func__, hparams.expert_weights_scale);
  4194. }
  4195. if (arch == LLM_ARCH_DEEPSEEK2) {
  4196. LLAMA_LOG_INFO("%s: n_layer_dense_lead = %d\n", __func__, hparams.n_layer_dense_lead);
  4197. LLAMA_LOG_INFO("%s: n_lora_q = %d\n", __func__, hparams.n_lora_q);
  4198. LLAMA_LOG_INFO("%s: n_lora_kv = %d\n", __func__, hparams.n_lora_kv);
  4199. LLAMA_LOG_INFO("%s: n_embd_head_k_mla = %d\n", __func__, hparams.n_embd_head_k_mla);
  4200. LLAMA_LOG_INFO("%s: n_embd_head_v_mla = %d\n", __func__, hparams.n_embd_head_v_mla);
  4201. LLAMA_LOG_INFO("%s: n_ff_exp = %d\n", __func__, hparams.n_ff_exp);
  4202. LLAMA_LOG_INFO("%s: n_expert_shared = %d\n", __func__, hparams.n_expert_shared);
  4203. LLAMA_LOG_INFO("%s: expert_weights_scale = %.1f\n", __func__, hparams.expert_weights_scale);
  4204. LLAMA_LOG_INFO("%s: expert_weights_norm = %d\n", __func__, hparams.expert_weights_norm);
  4205. LLAMA_LOG_INFO("%s: expert_gating_func = %s\n", __func__, llama_expert_gating_func_name((llama_expert_gating_func_type) hparams.expert_gating_func));
  4206. LLAMA_LOG_INFO("%s: rope_yarn_log_mul = %.4f\n", __func__, hparams.rope_yarn_log_mul);
  4207. }
  4208. if (arch == LLM_ARCH_QWEN2MOE) {
  4209. LLAMA_LOG_INFO("%s: n_ff_exp = %d\n", __func__, hparams.n_ff_exp);
  4210. LLAMA_LOG_INFO("%s: n_ff_shexp = %d\n", __func__, hparams.n_ff_shexp);
  4211. }
  4212. if (arch == LLM_ARCH_QWEN3MOE) {
  4213. LLAMA_LOG_INFO("%s: n_ff_exp = %d\n", __func__, hparams.n_ff_exp);
  4214. }
  4215. if (arch == LLM_ARCH_MINICPM ||
  4216. arch == LLM_ARCH_GRANITE ||
  4217. arch == LLM_ARCH_GRANITE_MOE) {
  4218. LLAMA_LOG_INFO("%s: f_embedding_scale = %f\n", __func__, hparams.f_embedding_scale);
  4219. LLAMA_LOG_INFO("%s: f_residual_scale = %f\n", __func__, hparams.f_residual_scale);
  4220. LLAMA_LOG_INFO("%s: f_attention_scale = %f\n", __func__, hparams.f_attention_scale);
  4221. LLAMA_LOG_INFO("%s: n_ff_shexp = %d\n", __func__, hparams.n_ff_shexp);
  4222. }
  4223. if (arch == LLM_ARCH_BAILINGMOE) {
  4224. LLAMA_LOG_INFO("%s: n_layer_dense_lead = %d\n", __func__, hparams.n_layer_dense_lead);
  4225. LLAMA_LOG_INFO("%s: n_ff_exp = %d\n", __func__, hparams.n_ff_exp);
  4226. LLAMA_LOG_INFO("%s: n_expert_shared = %d\n", __func__, hparams.n_expert_shared);
  4227. LLAMA_LOG_INFO("%s: expert_weights_scale = %.1f\n", __func__, hparams.expert_weights_scale);
  4228. LLAMA_LOG_INFO("%s: expert_weights_norm = %d\n", __func__, hparams.expert_weights_norm);
  4229. }
  4230. vocab.print_info();
  4231. }
  4232. ggml_backend_dev_t llama_model::dev_layer(int il) const {
  4233. return pimpl->dev_layer.at(il).dev;
  4234. }
  4235. ggml_backend_dev_t llama_model::dev_output() const {
  4236. return pimpl->dev_output.dev;
  4237. }
  4238. template<typename F>
  4239. static bool buft_supported(ggml_backend_buffer_type_t buft, ggml_backend_dev_t dev, F & fn) {
  4240. ggml_init_params params = {
  4241. /*.mem_size =*/ ggml_tensor_overhead()*8,
  4242. /*.mem_buffer =*/ NULL,
  4243. /*.no_alloc =*/ true,
  4244. };
  4245. ggml_context_ptr ctx { ggml_init(params) };
  4246. if (!ctx) {
  4247. throw std::runtime_error(format("failed to create ggml context"));
  4248. }
  4249. ggml_backend_buffer_ptr buf { ggml_backend_buft_alloc_buffer(buft, 0) };
  4250. ggml_tensor * op_tensor = fn(ctx.get());
  4251. for (int i = 0; i < GGML_MAX_SRC; i++) {
  4252. if (op_tensor->src[i] != nullptr) {
  4253. assert(op_tensor->src[i]->buffer == nullptr);
  4254. op_tensor->src[i]->buffer = buf.get();
  4255. }
  4256. }
  4257. bool op_supported = ggml_backend_dev_supports_op(dev, op_tensor);
  4258. return op_supported;
  4259. }
  4260. template<typename F>
  4261. static ggml_backend_buffer_type_t select_buft(const buft_list_t & buft_list, const F & fn) {
  4262. for (const auto & cur : buft_list) {
  4263. ggml_backend_dev_t cur_dev = cur.first;
  4264. ggml_backend_buffer_type_t cur_buft = cur.second;
  4265. if (buft_supported(cur_buft, cur_dev, fn)) {
  4266. return cur_buft;
  4267. }
  4268. }
  4269. throw std::runtime_error(format("no suitable buffer type found"));
  4270. }
  4271. ggml_backend_buffer_type_t llama_model::select_buft(int il) const {
  4272. return ::select_buft(
  4273. *pimpl->dev_layer.at(il).buft_list,
  4274. [&](ggml_context * ctx) {
  4275. ggml_tensor * cur = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, hparams.n_embd);
  4276. ggml_tensor * layer_dir = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, hparams.n_embd);
  4277. return ggml_add(ctx, cur, layer_dir);
  4278. });
  4279. }
  4280. bool llama_model::has_tensor_overrides() const {
  4281. return pimpl->has_tensor_overrides;
  4282. }
  4283. const ggml_tensor * llama_model::get_tensor(const char * name) const {
  4284. auto it = std::find_if(tensors_by_name.begin(), tensors_by_name.end(),
  4285. [name](const std::pair<std::string, ggml_tensor *> & it) {
  4286. return it.first == name;
  4287. });
  4288. if (it == tensors_by_name.end()) {
  4289. return nullptr;
  4290. }
  4291. return it->second;
  4292. }
  4293. float llama_model::get_rope_freq_base (const llama_cparams & cparams, int il) const {
  4294. return hparams.is_swa(il) ? hparams.rope_freq_base_train_swa : cparams.rope_freq_base;
  4295. }
  4296. float llama_model::get_rope_freq_scale(const llama_cparams & cparams, int il) const {
  4297. return hparams.is_swa(il) ? hparams.rope_freq_scale_train_swa : cparams.rope_freq_scale;
  4298. }
  4299. ggml_tensor * llama_model::get_rope_factors(const llama_cparams & cparams, int il) const {
  4300. const uint32_t n_ctx_per_seq = cparams.n_ctx / cparams.n_seq_max;
  4301. // choose long/short freq factors based on the context size
  4302. if (layers[il].rope_freqs != nullptr) {
  4303. return layers[il].rope_freqs;
  4304. }
  4305. if (n_ctx_per_seq > hparams.n_ctx_orig_yarn) {
  4306. return layers[il].rope_long;
  4307. }
  4308. return layers[il].rope_short;
  4309. }
  4310. struct llm_build_llama : public llm_graph_context {
  4311. llm_build_llama(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
  4312. const int64_t n_embd_head = hparams.n_embd_head_v;
  4313. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  4314. GGML_ASSERT(n_embd_head == hparams.n_rot);
  4315. ggml_tensor * cur;
  4316. ggml_tensor * inpL;
  4317. inpL = build_inp_embd(model.tok_embd);
  4318. // inp_pos - contains the positions
  4319. ggml_tensor * inp_pos = build_inp_pos();
  4320. auto * inp_attn = build_attn_inp_kv_unified();
  4321. const float kq_scale = hparams.f_attention_scale == 0.0f ? 1.0f/sqrtf(float(n_embd_head)) : hparams.f_attention_scale;
  4322. ggml_tensor * inp_out_ids = build_inp_out_ids();
  4323. for (int il = 0; il < n_layer; ++il) {
  4324. ggml_tensor * inpSA = inpL;
  4325. // norm
  4326. cur = build_norm(inpL,
  4327. model.layers[il].attn_norm, NULL,
  4328. LLM_NORM_RMS, il);
  4329. cb(cur, "attn_norm", il);
  4330. // self-attention
  4331. {
  4332. // rope freq factors for llama3; may return nullptr for llama2 and other models
  4333. ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
  4334. // compute Q and K and RoPE them
  4335. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  4336. cb(Qcur, "Qcur", il);
  4337. if (model.layers[il].bq) {
  4338. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  4339. cb(Qcur, "Qcur", il);
  4340. }
  4341. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  4342. cb(Kcur, "Kcur", il);
  4343. if (model.layers[il].bk) {
  4344. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  4345. cb(Kcur, "Kcur", il);
  4346. }
  4347. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  4348. cb(Vcur, "Vcur", il);
  4349. if (model.layers[il].bv) {
  4350. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  4351. cb(Vcur, "Vcur", il);
  4352. }
  4353. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  4354. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  4355. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  4356. Qcur = ggml_rope_ext(
  4357. ctx0, Qcur, inp_pos, rope_factors,
  4358. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  4359. ext_factor, attn_factor, beta_fast, beta_slow
  4360. );
  4361. Kcur = ggml_rope_ext(
  4362. ctx0, Kcur, inp_pos, rope_factors,
  4363. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  4364. ext_factor, attn_factor, beta_fast, beta_slow
  4365. );
  4366. cb(Qcur, "Qcur", il);
  4367. cb(Kcur, "Kcur", il);
  4368. cb(Vcur, "Vcur", il);
  4369. cur = build_attn(inp_attn, gf,
  4370. model.layers[il].wo, model.layers[il].bo,
  4371. Qcur, Kcur, Vcur, nullptr, nullptr, kq_scale, il);
  4372. cb(cur, "attn_out", il);
  4373. }
  4374. if (il == n_layer - 1 && inp_out_ids) {
  4375. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  4376. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  4377. }
  4378. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  4379. cb(ffn_inp, "ffn_inp", il);
  4380. // feed-forward network (non-MoE)
  4381. if (model.layers[il].ffn_gate_inp == nullptr) {
  4382. cur = build_norm(ffn_inp,
  4383. model.layers[il].ffn_norm, NULL,
  4384. LLM_NORM_RMS, il);
  4385. cb(cur, "ffn_norm", il);
  4386. cur = build_ffn(cur,
  4387. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  4388. model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, NULL,
  4389. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  4390. NULL,
  4391. LLM_FFN_SILU, LLM_FFN_PAR, il);
  4392. cb(cur, "ffn_out", il);
  4393. } else {
  4394. // MoE branch
  4395. cur = build_norm(ffn_inp,
  4396. model.layers[il].ffn_norm, NULL,
  4397. LLM_NORM_RMS, il);
  4398. cb(cur, "ffn_norm", il);
  4399. cur = build_moe_ffn(cur,
  4400. model.layers[il].ffn_gate_inp,
  4401. model.layers[il].ffn_up_exps,
  4402. model.layers[il].ffn_gate_exps,
  4403. model.layers[il].ffn_down_exps,
  4404. nullptr,
  4405. n_expert, n_expert_used,
  4406. LLM_FFN_SILU, true,
  4407. false, 0.0,
  4408. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  4409. il);
  4410. cb(cur, "ffn_moe_out", il);
  4411. }
  4412. cur = ggml_add(ctx0, cur, ffn_inp);
  4413. cb(cur, "ffn_out", il);
  4414. cur = build_cvec(cur, il);
  4415. cb(cur, "l_out", il);
  4416. // input for next layer
  4417. inpL = cur;
  4418. }
  4419. cur = inpL;
  4420. cur = build_norm(cur,
  4421. model.output_norm, NULL,
  4422. LLM_NORM_RMS, -1);
  4423. cb(cur, "result_norm", -1);
  4424. res->t_embd = cur;
  4425. // lm_head
  4426. cur = build_lora_mm(model.output, cur);
  4427. cb(cur, "result_output", -1);
  4428. res->t_logits = cur;
  4429. ggml_build_forward_expand(gf, cur);
  4430. }
  4431. };
  4432. struct llm_build_llama_iswa : public llm_graph_context {
  4433. llm_build_llama_iswa(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
  4434. const int64_t n_embd_head = hparams.n_embd_head_v;
  4435. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  4436. GGML_ASSERT(n_embd_head == hparams.n_rot);
  4437. ggml_tensor * cur;
  4438. ggml_tensor * inpL;
  4439. inpL = build_inp_embd(model.tok_embd);
  4440. // inp_pos - contains the positions
  4441. ggml_tensor * inp_pos = build_inp_pos();
  4442. // temperature tuning
  4443. ggml_tensor * inp_attn_scale = nullptr;
  4444. inp_attn_scale = build_inp_attn_scale();
  4445. auto * inp_attn = build_attn_inp_kv_unified_iswa();
  4446. const float kq_scale = hparams.f_attention_scale == 0.0f ? 1.0f/sqrtf(float(n_embd_head)) : hparams.f_attention_scale;
  4447. ggml_tensor * inp_out_ids = build_inp_out_ids();
  4448. for (int il = 0; il < n_layer; ++il) {
  4449. ggml_tensor * inpSA = inpL;
  4450. const bool use_rope = (il + 1) % hparams.n_no_rope_layer_step != 0;
  4451. // norm
  4452. cur = build_norm(inpL,
  4453. model.layers[il].attn_norm, NULL,
  4454. LLM_NORM_RMS, il);
  4455. cb(cur, "attn_norm", il);
  4456. // self-attention
  4457. {
  4458. // rope freq factors for llama3; may return nullptr for llama2 and other models
  4459. ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
  4460. // compute Q and K and RoPE them
  4461. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  4462. cb(Qcur, "Qcur", il);
  4463. if (model.layers[il].bq) {
  4464. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  4465. cb(Qcur, "Qcur", il);
  4466. }
  4467. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  4468. cb(Kcur, "Kcur", il);
  4469. if (model.layers[il].bk) {
  4470. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  4471. cb(Kcur, "Kcur", il);
  4472. }
  4473. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  4474. cb(Vcur, "Vcur", il);
  4475. if (model.layers[il].bv) {
  4476. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  4477. cb(Vcur, "Vcur", il);
  4478. }
  4479. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  4480. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  4481. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  4482. if (use_rope) {
  4483. Qcur = ggml_rope_ext(
  4484. ctx0, Qcur, inp_pos, rope_factors,
  4485. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  4486. ext_factor, attn_factor, beta_fast, beta_slow
  4487. );
  4488. Kcur = ggml_rope_ext(
  4489. ctx0, Kcur, inp_pos, rope_factors,
  4490. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  4491. ext_factor, attn_factor, beta_fast, beta_slow
  4492. );
  4493. } else if (inp_attn_scale) {
  4494. Qcur = ggml_mul(ctx0, Qcur, inp_attn_scale);
  4495. }
  4496. cb(Qcur, "Qcur", il);
  4497. cb(Kcur, "Kcur", il);
  4498. cb(Vcur, "Vcur", il);
  4499. if (use_rope && hparams.use_kq_norm) {
  4500. // Llama4TextL2Norm
  4501. Qcur = ggml_rms_norm(ctx0, Qcur, hparams.f_norm_rms_eps);
  4502. Kcur = ggml_rms_norm(ctx0, Kcur, hparams.f_norm_rms_eps);
  4503. cb(Qcur, "Qcur_normed", il);
  4504. cb(Kcur, "Kcur_normed", il);
  4505. }
  4506. cur = build_attn(inp_attn, gf,
  4507. model.layers[il].wo, model.layers[il].bo,
  4508. Qcur, Kcur, Vcur, nullptr, nullptr, kq_scale, il);
  4509. cb(cur, "attn_out", il);
  4510. }
  4511. if (il == n_layer - 1 && inp_out_ids) {
  4512. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  4513. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  4514. }
  4515. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  4516. cb(ffn_inp, "ffn_inp", il);
  4517. // feed-forward network (non-MoE)
  4518. if (model.layers[il].ffn_gate_inp == nullptr) {
  4519. cur = build_norm(ffn_inp,
  4520. model.layers[il].ffn_norm, NULL,
  4521. LLM_NORM_RMS, il);
  4522. cb(cur, "ffn_norm", il);
  4523. cur = build_ffn(cur,
  4524. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  4525. model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, NULL,
  4526. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  4527. NULL,
  4528. LLM_FFN_SILU, LLM_FFN_PAR, il);
  4529. cb(cur, "ffn_out", il);
  4530. } else {
  4531. ggml_tensor * ffn_inp_normed = build_norm(ffn_inp,
  4532. model.layers[il].ffn_norm, NULL,
  4533. LLM_NORM_RMS, il);
  4534. cb(cur, "ffn_norm", il);
  4535. ggml_tensor * moe_out = build_moe_ffn(ffn_inp_normed,
  4536. model.layers[il].ffn_gate_inp,
  4537. model.layers[il].ffn_up_exps,
  4538. model.layers[il].ffn_gate_exps,
  4539. model.layers[il].ffn_down_exps,
  4540. nullptr,
  4541. n_expert, n_expert_used,
  4542. LLM_FFN_SILU, false,
  4543. false, 0.0,
  4544. LLAMA_EXPERT_GATING_FUNC_TYPE_SIGMOID,
  4545. il);
  4546. // Shared experts
  4547. ggml_tensor * shexp_out = build_ffn(ffn_inp_normed,
  4548. model.layers[il].ffn_up_shexp, NULL, NULL,
  4549. model.layers[il].ffn_gate_shexp, NULL, NULL,
  4550. model.layers[il].ffn_down_shexp, NULL, NULL,
  4551. NULL,
  4552. LLM_FFN_SILU, LLM_FFN_PAR, il);
  4553. cb(shexp_out, "ffn_moe_shexp", il);
  4554. cur = ggml_add(ctx0, moe_out, shexp_out);
  4555. cb(cur, "ffn_moe_out_merged", il);
  4556. }
  4557. cur = ggml_add(ctx0, cur, ffn_inp);
  4558. cb(cur, "ffn_out", il);
  4559. cur = build_cvec(cur, il);
  4560. cb(cur, "l_out", il);
  4561. // input for next layer
  4562. inpL = cur;
  4563. }
  4564. cur = inpL;
  4565. cur = build_norm(cur,
  4566. model.output_norm, NULL,
  4567. LLM_NORM_RMS, -1);
  4568. cb(cur, "result_norm", -1);
  4569. res->t_embd = cur;
  4570. // lm_head
  4571. cur = build_lora_mm(model.output, cur);
  4572. cb(cur, "result_output", -1);
  4573. res->t_logits = cur;
  4574. ggml_build_forward_expand(gf, cur);
  4575. }
  4576. };
  4577. struct llm_build_deci : public llm_graph_context {
  4578. llm_build_deci(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
  4579. const int64_t n_embd_head = hparams.n_embd_head_v;
  4580. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  4581. GGML_ASSERT(n_embd_head == hparams.n_rot);
  4582. ggml_tensor * cur;
  4583. ggml_tensor * inpL;
  4584. inpL = build_inp_embd(model.tok_embd);
  4585. // inp_pos - contains the positions
  4586. ggml_tensor * inp_pos = build_inp_pos();
  4587. auto * inp_attn = build_attn_inp_kv_unified();
  4588. const float kq_scale = hparams.f_attention_scale == 0.0f ? 1.0f/sqrtf(float(n_embd_head)) : hparams.f_attention_scale;
  4589. ggml_tensor * inp_out_ids = build_inp_out_ids();
  4590. for (int il = 0; il < n_layer; ++il) {
  4591. ggml_tensor * inpSA = inpL;
  4592. const int64_t n_head_kv = hparams.n_head_kv(il);
  4593. const int64_t n_head = hparams.n_head(il);
  4594. const int64_t n_ff = hparams.n_ff(il);
  4595. if (n_head == 0) {
  4596. // attention-free layer of Llama-3_1-Nemotron-51B
  4597. cur = inpL;
  4598. } else {
  4599. // norm
  4600. cur = build_norm(inpL,
  4601. model.layers[il].attn_norm, NULL,
  4602. LLM_NORM_RMS, il);
  4603. cb(cur, "attn_norm", il);
  4604. }
  4605. if (n_head > 0 && n_head_kv == 0) {
  4606. // "linear attention" of Llama-3_1-Nemotron-51B
  4607. cur = build_lora_mm(model.layers[il].wo, cur);
  4608. cb(cur, "wo", il);
  4609. } else if (n_head > 0) {
  4610. // self-attention
  4611. // rope freq factors for llama3; may return nullptr for llama2 and other models
  4612. ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
  4613. // compute Q and K and RoPE them
  4614. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  4615. cb(Qcur, "Qcur", il);
  4616. if (model.layers[il].bq) {
  4617. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  4618. cb(Qcur, "Qcur", il);
  4619. }
  4620. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  4621. cb(Kcur, "Kcur", il);
  4622. if (model.layers[il].bk) {
  4623. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  4624. cb(Kcur, "Kcur", il);
  4625. }
  4626. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  4627. cb(Vcur, "Vcur", il);
  4628. if (model.layers[il].bv) {
  4629. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  4630. cb(Vcur, "Vcur", il);
  4631. }
  4632. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  4633. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  4634. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  4635. Qcur = ggml_rope_ext(
  4636. ctx0, Qcur, inp_pos, rope_factors,
  4637. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  4638. ext_factor, attn_factor, beta_fast, beta_slow
  4639. );
  4640. Kcur = ggml_rope_ext(
  4641. ctx0, Kcur, inp_pos, rope_factors,
  4642. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  4643. ext_factor, attn_factor, beta_fast, beta_slow
  4644. );
  4645. cb(Qcur, "Qcur", il);
  4646. cb(Kcur, "Kcur", il);
  4647. cb(Vcur, "Vcur", il);
  4648. cur = build_attn(inp_attn, gf,
  4649. model.layers[il].wo, model.layers[il].bo,
  4650. Qcur, Kcur, Vcur, nullptr, nullptr, kq_scale, il);
  4651. }
  4652. if (il == n_layer - 1 && inp_out_ids) {
  4653. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  4654. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  4655. }
  4656. // FFN-free layer of Llama-3_1-Nemotron-Ultra-253B
  4657. if (n_ff == 0) {
  4658. continue;
  4659. }
  4660. // modified to support attention-free layer of Llama-3_1-Nemotron-51B
  4661. ggml_tensor * ffn_inp = cur;
  4662. if (n_head > 0) {
  4663. ffn_inp = ggml_add(ctx0, cur, inpSA);
  4664. cb(ffn_inp, "ffn_inp", il);
  4665. }
  4666. // feed-forward network
  4667. if (model.layers[il].ffn_gate_inp == nullptr) {
  4668. cur = build_norm(ffn_inp,
  4669. model.layers[il].ffn_norm, NULL,
  4670. LLM_NORM_RMS, il);
  4671. cb(cur, "ffn_norm", il);
  4672. cur = build_ffn(cur,
  4673. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  4674. model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, NULL,
  4675. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  4676. NULL,
  4677. LLM_FFN_SILU, LLM_FFN_PAR, il);
  4678. cb(cur, "ffn_out", il);
  4679. }
  4680. cur = ggml_add(ctx0, cur, ffn_inp);
  4681. cb(cur, "ffn_out", il);
  4682. cur = build_cvec(cur, il);
  4683. cb(cur, "l_out", il);
  4684. // input for next layer
  4685. inpL = cur;
  4686. }
  4687. cur = inpL;
  4688. cur = build_norm(cur,
  4689. model.output_norm, NULL,
  4690. LLM_NORM_RMS, -1);
  4691. cb(cur, "result_norm", -1);
  4692. res->t_embd = cur;
  4693. // lm_head
  4694. cur = build_lora_mm(model.output, cur);
  4695. cb(cur, "result_output", -1);
  4696. res->t_logits = cur;
  4697. ggml_build_forward_expand(gf, cur);
  4698. }
  4699. };
  4700. struct llm_build_baichuan : public llm_graph_context {
  4701. llm_build_baichuan(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
  4702. const int64_t n_embd_head = hparams.n_embd_head_v;
  4703. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  4704. GGML_ASSERT(n_embd_head == hparams.n_rot);
  4705. ggml_tensor * cur;
  4706. ggml_tensor * inpL;
  4707. inpL = build_inp_embd(model.tok_embd);
  4708. // inp_pos - contains the positions
  4709. ggml_tensor * inp_pos = model.type == LLM_TYPE_7B ? build_inp_pos() : nullptr;
  4710. auto * inp_attn = build_attn_inp_kv_unified();
  4711. ggml_tensor * inp_out_ids = build_inp_out_ids();
  4712. for (int il = 0; il < n_layer; ++il) {
  4713. ggml_tensor * inpSA = inpL;
  4714. cur = build_norm(inpL,
  4715. model.layers[il].attn_norm, NULL,
  4716. LLM_NORM_RMS, il);
  4717. cb(cur, "attn_norm", il);
  4718. // self-attention
  4719. {
  4720. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  4721. cb(Qcur, "Qcur", il);
  4722. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  4723. cb(Kcur, "Kcur", il);
  4724. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  4725. cb(Vcur, "Vcur", il);
  4726. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  4727. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  4728. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  4729. switch (model.type) {
  4730. case LLM_TYPE_7B:
  4731. Qcur = ggml_rope_ext(
  4732. ctx0, Qcur, inp_pos, nullptr,
  4733. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  4734. ext_factor, attn_factor, beta_fast, beta_slow
  4735. );
  4736. Kcur = ggml_rope_ext(
  4737. ctx0, Kcur, inp_pos, nullptr,
  4738. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  4739. ext_factor, attn_factor, beta_fast, beta_slow
  4740. );
  4741. break;
  4742. case LLM_TYPE_13B:
  4743. break;
  4744. default:
  4745. GGML_ABORT("fatal error");
  4746. }
  4747. cb(Qcur, "Qcur", il);
  4748. cb(Kcur, "Kcur", il);
  4749. cb(Vcur, "Vcur", il);
  4750. cur = build_attn(inp_attn, gf,
  4751. model.layers[il].wo, NULL,
  4752. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  4753. }
  4754. if (il == n_layer - 1 && inp_out_ids) {
  4755. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  4756. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  4757. }
  4758. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  4759. cb(ffn_inp, "ffn_inp", il);
  4760. // feed-forward network
  4761. {
  4762. cur = build_norm(ffn_inp,
  4763. model.layers[il].ffn_norm, NULL,
  4764. LLM_NORM_RMS, il);
  4765. cb(cur, "ffn_norm", il);
  4766. cur = build_ffn(cur,
  4767. model.layers[il].ffn_up, NULL, NULL,
  4768. model.layers[il].ffn_gate, NULL, NULL,
  4769. model.layers[il].ffn_down, NULL, NULL,
  4770. NULL,
  4771. LLM_FFN_SILU, LLM_FFN_PAR, il);
  4772. cb(cur, "ffn_out", il);
  4773. }
  4774. cur = ggml_add(ctx0, cur, ffn_inp);
  4775. cur = build_cvec(cur, il);
  4776. cb(cur, "l_out", il);
  4777. // input for next layer
  4778. inpL = cur;
  4779. }
  4780. cur = inpL;
  4781. cur = build_norm(cur,
  4782. model.output_norm, NULL,
  4783. LLM_NORM_RMS, -1);
  4784. cb(cur, "result_norm", -1);
  4785. res->t_embd = cur;
  4786. // lm_head
  4787. cur = build_lora_mm(model.output, cur);
  4788. cb(cur, "result_output", -1);
  4789. res->t_logits = cur;
  4790. ggml_build_forward_expand(gf, cur);
  4791. }
  4792. };
  4793. struct llm_build_xverse : public llm_graph_context {
  4794. llm_build_xverse(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
  4795. const int64_t n_embd_head = hparams.n_embd_head_v;
  4796. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  4797. GGML_ASSERT(n_embd_head == hparams.n_rot);
  4798. ggml_tensor * cur;
  4799. ggml_tensor * inpL;
  4800. inpL = build_inp_embd(model.tok_embd);
  4801. // inp_pos - contains the positions
  4802. ggml_tensor * inp_pos = build_inp_pos();
  4803. auto * inp_attn = build_attn_inp_kv_unified();
  4804. ggml_tensor * inp_out_ids = build_inp_out_ids();
  4805. for (int il = 0; il < n_layer; ++il) {
  4806. ggml_tensor * inpSA = inpL;
  4807. cur = build_norm(inpL,
  4808. model.layers[il].attn_norm, NULL,
  4809. LLM_NORM_RMS, il);
  4810. cb(cur, "attn_norm", il);
  4811. // self-attention
  4812. {
  4813. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  4814. cb(Qcur, "Qcur", il);
  4815. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  4816. cb(Kcur, "Kcur", il);
  4817. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  4818. cb(Vcur, "Vcur", il);
  4819. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  4820. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  4821. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  4822. Qcur = ggml_rope_ext(
  4823. ctx0, Qcur, inp_pos, nullptr,
  4824. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  4825. ext_factor, attn_factor, beta_fast, beta_slow
  4826. );
  4827. Kcur = ggml_rope_ext(
  4828. ctx0, Kcur, inp_pos, nullptr,
  4829. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  4830. ext_factor, attn_factor, beta_fast, beta_slow
  4831. );
  4832. cb(Qcur, "Qcur", il);
  4833. cb(Kcur, "Kcur", il);
  4834. cb(Vcur, "Vcur", il);
  4835. cur = build_attn(inp_attn, gf,
  4836. model.layers[il].wo, NULL,
  4837. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  4838. }
  4839. if (il == n_layer - 1 && inp_out_ids) {
  4840. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  4841. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  4842. }
  4843. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  4844. cb(ffn_inp, "ffn_inp", il);
  4845. // feed-forward network
  4846. {
  4847. cur = build_norm(ffn_inp,
  4848. model.layers[il].ffn_norm, NULL,
  4849. LLM_NORM_RMS, il);
  4850. cb(cur, "ffn_norm", il);
  4851. cur = build_ffn(cur,
  4852. model.layers[il].ffn_up, NULL, NULL,
  4853. model.layers[il].ffn_gate, NULL, NULL,
  4854. model.layers[il].ffn_down, NULL, NULL,
  4855. NULL,
  4856. LLM_FFN_SILU, LLM_FFN_PAR, il);
  4857. cb(cur, "ffn_out", il);
  4858. }
  4859. cur = ggml_add(ctx0, cur, ffn_inp);
  4860. cur = build_cvec(cur, il);
  4861. cb(cur, "l_out", il);
  4862. // input for next layer
  4863. inpL = cur;
  4864. }
  4865. cur = inpL;
  4866. cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1);
  4867. cb(cur, "result_norm", -1);
  4868. res->t_embd = cur;
  4869. // lm_head
  4870. cur = build_lora_mm(model.output, cur);
  4871. cb(cur, "result_output", -1);
  4872. res->t_logits = cur;
  4873. ggml_build_forward_expand(gf, cur);
  4874. }
  4875. };
  4876. struct llm_build_falcon : public llm_graph_context {
  4877. llm_build_falcon(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
  4878. const int64_t n_embd_head = hparams.n_embd_head_v;
  4879. const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  4880. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  4881. GGML_ASSERT(n_embd_head == hparams.n_rot);
  4882. ggml_tensor * cur;
  4883. ggml_tensor * inpL;
  4884. inpL = build_inp_embd(model.tok_embd);
  4885. // inp_pos - contains the positions
  4886. ggml_tensor * inp_pos = build_inp_pos();
  4887. auto * inp_attn = build_attn_inp_kv_unified();
  4888. ggml_tensor * inp_out_ids = build_inp_out_ids();
  4889. for (int il = 0; il < n_layer; ++il) {
  4890. ggml_tensor * attn_norm;
  4891. attn_norm = build_norm(inpL,
  4892. model.layers[il].attn_norm,
  4893. model.layers[il].attn_norm_b,
  4894. LLM_NORM, il);
  4895. cb(attn_norm, "attn_norm", il);
  4896. // self-attention
  4897. {
  4898. if (model.layers[il].attn_norm_2) {
  4899. // Falcon-40B
  4900. cur = build_norm(inpL,
  4901. model.layers[il].attn_norm_2,
  4902. model.layers[il].attn_norm_2_b,
  4903. LLM_NORM, il);
  4904. cb(cur, "attn_norm_2", il);
  4905. } else {
  4906. cur = attn_norm;
  4907. }
  4908. cur = build_lora_mm(model.layers[il].wqkv, cur);
  4909. cb(cur, "wqkv", il);
  4910. 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));
  4911. 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));
  4912. ggml_tensor * Vcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd + n_embd_gqa)));
  4913. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  4914. // using mode = 2 for neox mode
  4915. Qcur = ggml_rope_ext(
  4916. ctx0, Qcur, inp_pos, nullptr,
  4917. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  4918. ext_factor, attn_factor, beta_fast, beta_slow
  4919. );
  4920. Kcur = ggml_rope_ext(
  4921. ctx0, Kcur, inp_pos, nullptr,
  4922. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  4923. ext_factor, attn_factor, beta_fast, beta_slow
  4924. );
  4925. cb(Qcur, "Qcur", il);
  4926. cb(Kcur, "Kcur", il);
  4927. cb(Vcur, "Vcur", il);
  4928. cur = build_attn(inp_attn, gf,
  4929. model.layers[il].wo, NULL,
  4930. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  4931. }
  4932. if (il == n_layer - 1 && inp_out_ids) {
  4933. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  4934. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  4935. attn_norm = ggml_get_rows(ctx0, attn_norm, inp_out_ids);
  4936. }
  4937. ggml_tensor * ffn_inp = cur;
  4938. // feed forward
  4939. {
  4940. cur = build_ffn(attn_norm, // !! use the attn norm, not the result
  4941. model.layers[il].ffn_up, NULL, NULL,
  4942. NULL, NULL, NULL,
  4943. model.layers[il].ffn_down, NULL, NULL,
  4944. NULL,
  4945. LLM_FFN_GELU, LLM_FFN_SEQ, il);
  4946. cb(cur, "ffn_out", il);
  4947. }
  4948. cur = ggml_add(ctx0, cur, ffn_inp);
  4949. cur = ggml_add(ctx0, cur, inpL);
  4950. cur = build_cvec(cur, il);
  4951. cb(cur, "l_out", il);
  4952. // input for next layer
  4953. inpL = cur;
  4954. }
  4955. cur = inpL;
  4956. // norm
  4957. cur = build_norm(cur,
  4958. model.output_norm,
  4959. model.output_norm_b,
  4960. LLM_NORM, -1);
  4961. cb(cur, "result_norm", -1);
  4962. res->t_embd = cur;
  4963. cur = build_lora_mm(model.output, cur);
  4964. cb(cur, "result_output", -1);
  4965. res->t_logits = cur;
  4966. ggml_build_forward_expand(gf, cur);
  4967. }
  4968. };
  4969. struct llm_build_grok : public llm_graph_context {
  4970. llm_build_grok(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
  4971. const int64_t n_embd_head = hparams.n_embd_head_v;
  4972. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  4973. GGML_ASSERT(n_embd_head == hparams.n_rot);
  4974. ggml_tensor * cur;
  4975. ggml_tensor * inpL;
  4976. inpL = build_inp_embd(model.tok_embd);
  4977. // multiply by embedding_multiplier_scale of 78.38367176906169
  4978. inpL = ggml_scale(ctx0, inpL, 78.38367176906169f);
  4979. // inp_pos - contains the positions
  4980. ggml_tensor * inp_pos = build_inp_pos();
  4981. auto * inp_attn = build_attn_inp_kv_unified();
  4982. ggml_tensor * inp_out_ids = build_inp_out_ids();
  4983. for (int il = 0; il < n_layer; ++il) {
  4984. ggml_tensor * inpSA = inpL;
  4985. // norm
  4986. cur = build_norm(inpL,
  4987. model.layers[il].attn_norm, NULL,
  4988. LLM_NORM_RMS, il);
  4989. cb(cur, "attn_norm", il);
  4990. // self-attention
  4991. {
  4992. // compute Q and K and RoPE them
  4993. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  4994. cb(Qcur, "Qcur", il);
  4995. if (model.layers[il].bq) {
  4996. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  4997. cb(Qcur, "Qcur", il);
  4998. }
  4999. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  5000. cb(Kcur, "Kcur", il);
  5001. if (model.layers[il].bk) {
  5002. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  5003. cb(Kcur, "Kcur", il);
  5004. }
  5005. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  5006. cb(Vcur, "Vcur", il);
  5007. if (model.layers[il].bv) {
  5008. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  5009. cb(Vcur, "Vcur", il);
  5010. }
  5011. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  5012. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  5013. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  5014. Qcur = ggml_rope_ext(
  5015. ctx0, Qcur, inp_pos, nullptr,
  5016. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  5017. ext_factor, attn_factor, beta_fast, beta_slow
  5018. );
  5019. Kcur = ggml_rope_ext(
  5020. ctx0, Kcur, inp_pos, nullptr,
  5021. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  5022. ext_factor, attn_factor, beta_fast, beta_slow
  5023. );
  5024. cb(Qcur, "Qcur", il);
  5025. cb(Kcur, "Kcur", il);
  5026. cb(Vcur, "Vcur", il);
  5027. cur = build_attn(inp_attn, gf,
  5028. model.layers[il].wo, model.layers[il].bo,
  5029. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f, il);
  5030. }
  5031. if (il == n_layer - 1 && inp_out_ids) {
  5032. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  5033. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  5034. }
  5035. // Grok
  5036. // if attn_out_norm is present then apply it before adding the input
  5037. if (model.layers[il].attn_out_norm) {
  5038. cur = build_norm(cur,
  5039. model.layers[il].attn_out_norm, NULL,
  5040. LLM_NORM_RMS, il);
  5041. cb(cur, "attn_out_norm", il);
  5042. }
  5043. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  5044. cb(ffn_inp, "ffn_inp", il);
  5045. // feed-forward network
  5046. // MoE branch
  5047. cur = build_norm(ffn_inp,
  5048. model.layers[il].ffn_norm, NULL,
  5049. LLM_NORM_RMS, il);
  5050. cb(cur, "ffn_norm", il);
  5051. cur = build_moe_ffn(cur,
  5052. model.layers[il].ffn_gate_inp,
  5053. model.layers[il].ffn_up_exps,
  5054. model.layers[il].ffn_gate_exps,
  5055. model.layers[il].ffn_down_exps,
  5056. nullptr,
  5057. n_expert, n_expert_used,
  5058. LLM_FFN_GELU, true,
  5059. false, 0.0,
  5060. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  5061. il);
  5062. cb(cur, "ffn_moe_out", il);
  5063. // Grok
  5064. // if layer_out_norm is present then apply it before adding the input
  5065. // Idea: maybe ffn_out_norm is a better name
  5066. if (model.layers[il].layer_out_norm) {
  5067. cur = build_norm(cur,
  5068. model.layers[il].layer_out_norm, NULL,
  5069. LLM_NORM_RMS, il);
  5070. cb(cur, "layer_out_norm", il);
  5071. }
  5072. cur = ggml_add(ctx0, cur, ffn_inp);
  5073. cb(cur, "ffn_out", il);
  5074. cur = build_cvec(cur, il);
  5075. cb(cur, "l_out", il);
  5076. // input for next layer
  5077. inpL = cur;
  5078. }
  5079. cur = inpL;
  5080. cur = build_norm(cur,
  5081. model.output_norm, NULL,
  5082. LLM_NORM_RMS, -1);
  5083. cb(cur, "result_norm", -1);
  5084. res->t_embd = cur;
  5085. // lm_head
  5086. cur = build_lora_mm(model.output, cur);
  5087. // Grok
  5088. // multiply logits by output_multiplier_scale of 0.5773502691896257
  5089. cur = ggml_scale(ctx0, cur, 0.5773502691896257f);
  5090. cb(cur, "result_output", -1);
  5091. res->t_logits = cur;
  5092. ggml_build_forward_expand(gf, cur);
  5093. }
  5094. };
  5095. struct llm_build_dbrx : public llm_graph_context {
  5096. llm_build_dbrx(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
  5097. const int64_t n_embd_head = hparams.n_embd_head_v;
  5098. const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  5099. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  5100. GGML_ASSERT(n_embd_head == hparams.n_rot);
  5101. ggml_tensor * cur;
  5102. ggml_tensor * inpL;
  5103. inpL = build_inp_embd(model.tok_embd);
  5104. // inp_pos - contains the positions
  5105. ggml_tensor * inp_pos = build_inp_pos();
  5106. auto * inp_attn = build_attn_inp_kv_unified();
  5107. ggml_tensor * inp_out_ids = build_inp_out_ids();
  5108. for (int il = 0; il < n_layer; ++il) {
  5109. ggml_tensor * inpSA = inpL;
  5110. // norm
  5111. cur = build_norm(inpL,
  5112. model.layers[il].attn_norm, NULL,
  5113. LLM_NORM, il);
  5114. cb(cur, "attn_norm", il);
  5115. // self-attention
  5116. {
  5117. ggml_tensor * Qcur = nullptr;
  5118. ggml_tensor * Kcur = nullptr;
  5119. ggml_tensor * Vcur = nullptr;
  5120. cur = build_lora_mm(model.layers[il].wqkv, cur);
  5121. cb(cur, "wqkv", il);
  5122. cur = ggml_clamp(ctx0, cur, -hparams.f_clamp_kqv, hparams.f_clamp_kqv);
  5123. cb(cur, "wqkv_clamped", il);
  5124. 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));
  5125. 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));
  5126. Vcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd + n_embd_gqa)));
  5127. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  5128. Qcur = ggml_rope_ext(
  5129. ctx0, Qcur, inp_pos, nullptr,
  5130. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  5131. ext_factor, attn_factor, beta_fast, beta_slow
  5132. );
  5133. Kcur = ggml_rope_ext(
  5134. ctx0, Kcur, inp_pos, nullptr,
  5135. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  5136. ext_factor, attn_factor, beta_fast, beta_slow
  5137. );
  5138. cb(Qcur, "Qcur", il);
  5139. cb(Kcur, "Kcur", il);
  5140. cb(Vcur, "Vcur", il);
  5141. cur = build_attn(inp_attn, gf,
  5142. model.layers[il].wo, NULL,
  5143. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  5144. }
  5145. if (il == n_layer - 1 && inp_out_ids) {
  5146. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  5147. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  5148. }
  5149. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  5150. cb(ffn_inp, "ffn_inp", il);
  5151. // feed-forward network
  5152. // MoE branch
  5153. cur = build_norm(ffn_inp,
  5154. model.layers[il].attn_out_norm, NULL,
  5155. LLM_NORM, il);
  5156. cb(cur, "attn_out_norm", il);
  5157. cur = build_moe_ffn(cur,
  5158. model.layers[il].ffn_gate_inp,
  5159. model.layers[il].ffn_up_exps,
  5160. model.layers[il].ffn_gate_exps,
  5161. model.layers[il].ffn_down_exps,
  5162. nullptr,
  5163. n_expert, n_expert_used,
  5164. LLM_FFN_SILU, true,
  5165. false, 0.0,
  5166. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  5167. il);
  5168. cb(cur, "ffn_moe_out", il);
  5169. cur = ggml_add(ctx0, cur, ffn_inp);
  5170. cb(cur, "ffn_out", il);
  5171. cur = build_cvec(cur, il);
  5172. cb(cur, "l_out", il);
  5173. // input for next layer
  5174. inpL = cur;
  5175. }
  5176. cur = inpL;
  5177. cur = build_norm(cur,
  5178. model.output_norm, NULL,
  5179. LLM_NORM, -1);
  5180. cb(cur, "result_norm", -1);
  5181. res->t_embd = cur;
  5182. // lm_head
  5183. cur = build_lora_mm(model.output, cur);
  5184. cb(cur, "result_output", -1);
  5185. res->t_logits = cur;
  5186. ggml_build_forward_expand(gf, cur);
  5187. }
  5188. };
  5189. struct llm_build_starcoder : public llm_graph_context {
  5190. llm_build_starcoder(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
  5191. const int64_t n_embd_head = hparams.n_embd_head_v;
  5192. const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  5193. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  5194. ggml_tensor * cur;
  5195. ggml_tensor * inpL;
  5196. inpL = build_inp_embd(model.tok_embd);
  5197. // inp_pos - contains the positions
  5198. ggml_tensor * inp_pos = build_inp_pos();
  5199. auto * inp_attn = build_attn_inp_kv_unified();
  5200. ggml_tensor * pos = ggml_get_rows(ctx0, model.pos_embd, inp_pos);
  5201. cb(pos, "pos_embd", -1);
  5202. inpL = ggml_add(ctx0, inpL, pos);
  5203. cb(inpL, "inpL", -1);
  5204. ggml_tensor * inp_out_ids = build_inp_out_ids();
  5205. for (int il = 0; il < n_layer; ++il) {
  5206. cur = build_norm(inpL,
  5207. model.layers[il].attn_norm,
  5208. model.layers[il].attn_norm_b,
  5209. LLM_NORM, il);
  5210. cb(cur, "attn_norm", il);
  5211. // self-attention
  5212. {
  5213. cur = build_lora_mm(model.layers[il].wqkv, cur);
  5214. cb(cur, "wqkv", il);
  5215. cur = ggml_add(ctx0, cur, model.layers[il].bqkv);
  5216. cb(cur, "bqkv", il);
  5217. ggml_tensor * Qcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd, n_tokens, cur->nb[1], 0*sizeof(float)*(n_embd)));
  5218. ggml_tensor * Kcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd)));
  5219. ggml_tensor * Vcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd + n_embd_gqa)));
  5220. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  5221. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  5222. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  5223. cb(Qcur, "Qcur", il);
  5224. cb(Kcur, "Kcur", il);
  5225. cb(Vcur, "Vcur", il);
  5226. cur = build_attn(inp_attn, gf,
  5227. model.layers[il].wo, model.layers[il].bo,
  5228. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  5229. }
  5230. if (il == n_layer - 1 && inp_out_ids) {
  5231. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  5232. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  5233. }
  5234. // add the input
  5235. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
  5236. cb(ffn_inp, "ffn_inp", il);
  5237. // FF
  5238. {
  5239. cur = build_norm(ffn_inp,
  5240. model.layers[il].ffn_norm,
  5241. model.layers[il].ffn_norm_b,
  5242. LLM_NORM, il);
  5243. cb(cur, "ffn_norm", il);
  5244. cur = build_ffn(cur,
  5245. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  5246. NULL, NULL, NULL,
  5247. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  5248. NULL,
  5249. LLM_FFN_GELU, LLM_FFN_SEQ, il);
  5250. cb(cur, "ffn_out", il);
  5251. }
  5252. cur = ggml_add(ctx0, cur, ffn_inp);
  5253. cur = build_cvec(cur, il);
  5254. cb(cur, "l_out", il);
  5255. // input for next layer
  5256. inpL = cur;
  5257. }
  5258. cur = build_norm(inpL,
  5259. model.output_norm,
  5260. model.output_norm_b,
  5261. LLM_NORM, -1);
  5262. cb(cur, "result_norm", -1);
  5263. res->t_embd = cur;
  5264. cur = build_lora_mm(model.output, cur);
  5265. cb(cur, "result_output", -1);
  5266. res->t_logits = cur;
  5267. ggml_build_forward_expand(gf, cur);
  5268. }
  5269. };
  5270. struct llm_build_refact : public llm_graph_context {
  5271. llm_build_refact(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
  5272. const int64_t n_embd_head = hparams.n_embd_head_v;
  5273. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  5274. ggml_tensor * cur;
  5275. ggml_tensor * inpL;
  5276. inpL = build_inp_embd(model.tok_embd);
  5277. auto * inp_attn = build_attn_inp_kv_unified();
  5278. ggml_tensor * inp_out_ids = build_inp_out_ids();
  5279. for (int il = 0; il < n_layer; ++il) {
  5280. ggml_tensor * inpSA = inpL;
  5281. cur = build_norm(inpL,
  5282. model.layers[il].attn_norm, NULL,
  5283. LLM_NORM_RMS, il);
  5284. cb(cur, "attn_norm", il);
  5285. // self-attention
  5286. {
  5287. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  5288. cb(Qcur, "Qcur", il);
  5289. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  5290. cb(Kcur, "Kcur", il);
  5291. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  5292. cb(Vcur, "Vcur", il);
  5293. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  5294. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  5295. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  5296. cb(Qcur, "Qcur", il);
  5297. cb(Kcur, "Kcur", il);
  5298. cb(Vcur, "Vcur", il);
  5299. cur = build_attn(inp_attn, gf,
  5300. model.layers[il].wo, NULL,
  5301. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  5302. }
  5303. if (il == n_layer - 1 && inp_out_ids) {
  5304. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  5305. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  5306. }
  5307. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  5308. cb(ffn_inp, "ffn_inp", il);
  5309. // feed-forward network
  5310. {
  5311. cur = build_norm(ffn_inp,
  5312. model.layers[il].ffn_norm, NULL,
  5313. LLM_NORM_RMS, il);
  5314. cb(cur, "ffn_norm", il);
  5315. cur = build_ffn(cur,
  5316. model.layers[il].ffn_up, NULL, NULL,
  5317. model.layers[il].ffn_gate, NULL, NULL,
  5318. model.layers[il].ffn_down, NULL, NULL,
  5319. NULL,
  5320. LLM_FFN_SILU, LLM_FFN_PAR, il);
  5321. cb(cur, "ffn_out", il);
  5322. }
  5323. cur = ggml_add(ctx0, cur, ffn_inp);
  5324. cur = build_cvec(cur, il);
  5325. cb(cur, "l_out", il);
  5326. // input for next layer
  5327. inpL = cur;
  5328. }
  5329. cur = inpL;
  5330. cur = build_norm(cur,
  5331. model.output_norm, NULL,
  5332. LLM_NORM_RMS, -1);
  5333. cb(cur, "result_norm", -1);
  5334. res->t_embd = cur;
  5335. // lm_head
  5336. cur = build_lora_mm(model.output, cur);
  5337. cb(cur, "result_output", -1);
  5338. res->t_logits = cur;
  5339. ggml_build_forward_expand(gf, cur);
  5340. }
  5341. };
  5342. struct llm_build_bert : public llm_graph_context {
  5343. llm_build_bert(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
  5344. const int64_t n_embd_head = hparams.n_embd_head_v;
  5345. const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  5346. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  5347. ggml_tensor * cur;
  5348. ggml_tensor * inpL;
  5349. ggml_tensor * inp_pos = nullptr;
  5350. if (model.arch != LLM_ARCH_JINA_BERT_V2) {
  5351. inp_pos = build_inp_pos();
  5352. }
  5353. // construct input embeddings (token, type, position)
  5354. inpL = build_inp_embd(model.tok_embd);
  5355. // token types are hardcoded to zero ("Sentence A")
  5356. if (model.type_embd) {
  5357. ggml_tensor * type_row0 = ggml_view_1d(ctx0, model.type_embd, n_embd, 0);
  5358. inpL = ggml_add(ctx0, inpL, type_row0);
  5359. }
  5360. if (model.arch == LLM_ARCH_BERT) {
  5361. inpL = ggml_add(ctx0, ggml_get_rows(ctx0, model.pos_embd, inp_pos), inpL);
  5362. }
  5363. cb(inpL, "inp_embd", -1);
  5364. // embed layer norm
  5365. inpL = build_norm(inpL, model.tok_norm, model.tok_norm_b, LLM_NORM, -1);
  5366. cb(inpL, "inp_norm", -1);
  5367. auto * inp_attn = build_attn_inp_no_cache();
  5368. ggml_tensor * inp_out_ids = build_inp_out_ids();
  5369. for (int il = 0; il < n_layer; ++il) {
  5370. ggml_tensor * cur = inpL;
  5371. {
  5372. ggml_tensor * Qcur;
  5373. ggml_tensor * Kcur;
  5374. ggml_tensor * Vcur;
  5375. // self-attention
  5376. if (model.layers[il].wqkv) {
  5377. cur = build_lora_mm(model.layers[il].wqkv, cur);
  5378. cb(cur, "wqkv", il);
  5379. if (model.layers[il].bqkv) {
  5380. cur = ggml_add(ctx0, cur, model.layers[il].bqkv);
  5381. cb(cur, "bqkv", il);
  5382. }
  5383. Qcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd, n_tokens, cur->nb[1], 0*sizeof(float)*(n_embd)));
  5384. Kcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd)));
  5385. Vcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd + n_embd_gqa)));
  5386. } else {
  5387. Qcur = ggml_add(ctx0, build_lora_mm(model.layers[il].wq, cur), model.layers[il].bq);
  5388. Kcur = ggml_add(ctx0, build_lora_mm(model.layers[il].wk, cur), model.layers[il].bk);
  5389. Vcur = ggml_add(ctx0, build_lora_mm(model.layers[il].wv, cur), model.layers[il].bv);
  5390. }
  5391. if (model.layers[il].attn_q_norm) {
  5392. Qcur = build_norm(Qcur,
  5393. model.layers[il].attn_q_norm,
  5394. model.layers[il].attn_q_norm_b,
  5395. LLM_NORM, il);
  5396. }
  5397. if (model.layers[il].attn_k_norm) {
  5398. Kcur = build_norm(Kcur,
  5399. model.layers[il].attn_k_norm,
  5400. model.layers[il].attn_k_norm_b,
  5401. LLM_NORM, il);
  5402. }
  5403. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  5404. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  5405. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  5406. // RoPE
  5407. if (model.arch == LLM_ARCH_NOMIC_BERT || model.arch == LLM_ARCH_NOMIC_BERT_MOE) {
  5408. Qcur = ggml_rope_ext(
  5409. ctx0, Qcur, inp_pos, nullptr,
  5410. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  5411. ext_factor, attn_factor, beta_fast, beta_slow
  5412. );
  5413. Kcur = ggml_rope_ext(
  5414. ctx0, Kcur, inp_pos, nullptr,
  5415. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  5416. ext_factor, attn_factor, beta_fast, beta_slow
  5417. );
  5418. }
  5419. cb(Qcur, "Qcur", il);
  5420. cb(Kcur, "Kcur", il);
  5421. cb(Vcur, "Vcur", il);
  5422. cur = build_attn(inp_attn, gf,
  5423. model.layers[il].wo, model.layers[il].bo,
  5424. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  5425. cb(cur, "kqv_out", il);
  5426. }
  5427. if (il == n_layer - 1 && inp_out_ids) {
  5428. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  5429. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  5430. }
  5431. // re-add the layer input
  5432. cur = ggml_add(ctx0, cur, inpL);
  5433. // attention layer norm
  5434. cur = build_norm(cur, model.layers[il].attn_out_norm, model.layers[il].attn_out_norm_b, LLM_NORM, il);
  5435. if (model.layers[il].attn_norm_2 != nullptr) {
  5436. cur = ggml_add(ctx0, cur, inpL); // re-add the layer input
  5437. cur = build_norm(cur, model.layers[il].attn_norm_2, model.layers[il].attn_norm_2_b, LLM_NORM, il);
  5438. }
  5439. ggml_tensor * ffn_inp = cur;
  5440. cb(ffn_inp, "ffn_inp", il);
  5441. // feed-forward network
  5442. if (hparams.moe_every_n_layers > 0 && il % hparams.moe_every_n_layers == 1) {
  5443. // MoE branch
  5444. cur = build_moe_ffn(cur,
  5445. model.layers[il].ffn_gate_inp,
  5446. model.layers[il].ffn_up_exps,
  5447. nullptr,
  5448. model.layers[il].ffn_down_exps,
  5449. nullptr,
  5450. hparams.n_expert,
  5451. hparams.n_expert_used,
  5452. LLM_FFN_GELU,
  5453. false, false,
  5454. 0.0f,
  5455. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX, il);
  5456. cb(cur, "ffn_moe_out", il);
  5457. } else if (model.arch == LLM_ARCH_BERT || model.arch == LLM_ARCH_NOMIC_BERT_MOE) {
  5458. cur = build_ffn(cur,
  5459. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  5460. NULL, NULL, NULL,
  5461. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  5462. NULL,
  5463. LLM_FFN_GELU, LLM_FFN_SEQ, il);
  5464. cb(cur, "ffn_out", il);
  5465. } else if (model.arch == LLM_ARCH_JINA_BERT_V2) {
  5466. cur = build_ffn(cur,
  5467. model.layers[il].ffn_up, NULL, NULL,
  5468. model.layers[il].ffn_gate, NULL, NULL,
  5469. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  5470. NULL,
  5471. model.layers[il].ffn_gate ? LLM_FFN_GELU : LLM_FFN_GEGLU, LLM_FFN_PAR, il);
  5472. cb(cur, "ffn_out", il);
  5473. } else {
  5474. cur = build_ffn(cur,
  5475. model.layers[il].ffn_up, NULL, NULL,
  5476. model.layers[il].ffn_gate, NULL, NULL,
  5477. model.layers[il].ffn_down, NULL, NULL,
  5478. NULL,
  5479. LLM_FFN_SILU, LLM_FFN_PAR, il);
  5480. cb(cur, "ffn_out", il);
  5481. }
  5482. // attentions bypass the intermediate layer
  5483. cur = ggml_add(ctx0, cur, ffn_inp);
  5484. // output layer norm
  5485. cur = build_norm(cur, model.layers[il].layer_out_norm, model.layers[il].layer_out_norm_b, LLM_NORM, il);
  5486. // input for next layer
  5487. inpL = cur;
  5488. }
  5489. cur = inpL;
  5490. cb(cur, "result_embd", -1);
  5491. res->t_embd = cur;
  5492. ggml_build_forward_expand(gf, cur);
  5493. }
  5494. };
  5495. struct llm_build_neo_bert : public llm_graph_context {
  5496. llm_build_neo_bert(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
  5497. const int64_t n_embd_head = hparams.n_embd_head_v;
  5498. const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  5499. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  5500. ggml_tensor * cur;
  5501. ggml_tensor * inpL;
  5502. ggml_tensor * inp_pos = build_inp_pos();
  5503. // construct input embeddings (token, type, position)
  5504. inpL = build_inp_embd(model.tok_embd);
  5505. cb(inpL, "inp_embd", -1);
  5506. auto * inp_attn = build_attn_inp_no_cache();
  5507. ggml_tensor * inp_out_ids = build_inp_out_ids();
  5508. for (int il = 0; il < n_layer; ++il) {
  5509. ggml_tensor * cur = inpL;
  5510. // pre-norm
  5511. cur = build_norm(inpL,
  5512. model.layers[il].attn_norm, NULL,
  5513. LLM_NORM_RMS, il);
  5514. {
  5515. ggml_tensor * Qcur;
  5516. ggml_tensor * Kcur;
  5517. ggml_tensor * Vcur;
  5518. // self-attention
  5519. cur = build_lora_mm(model.layers[il].wqkv, cur);
  5520. cb(cur, "wqkv", il);
  5521. 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));
  5522. 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));
  5523. Vcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd + n_embd_gqa)));
  5524. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  5525. // RoPE
  5526. Qcur = ggml_rope_ext(
  5527. ctx0, Qcur, inp_pos, nullptr,
  5528. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  5529. ext_factor, attn_factor, beta_fast, beta_slow
  5530. );
  5531. Kcur = ggml_rope_ext(
  5532. ctx0, Kcur, inp_pos, nullptr,
  5533. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  5534. ext_factor, attn_factor, beta_fast, beta_slow
  5535. );
  5536. cb(Qcur, "Qcur", il);
  5537. cb(Kcur, "Kcur", il);
  5538. cb(Vcur, "Vcur", il);
  5539. cur = build_attn(inp_attn, gf,
  5540. model.layers[il].wo, nullptr,
  5541. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  5542. cb(cur, "kqv_out", il);
  5543. }
  5544. if (il == n_layer - 1 && inp_out_ids) {
  5545. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  5546. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  5547. }
  5548. // re-add the layer input
  5549. cur = ggml_add(ctx0, cur, inpL);
  5550. ggml_tensor * ffn_inp = cur;
  5551. cb(ffn_inp, "ffn_inp", il);
  5552. // pre-norm
  5553. cur = build_norm(ffn_inp,
  5554. model.layers[il].ffn_norm, NULL,
  5555. LLM_NORM_RMS, il);
  5556. cb(cur, "ffn_norm", il);
  5557. // feed-forward network
  5558. cur = build_ffn(cur,
  5559. model.layers[il].ffn_up,
  5560. NULL, NULL, NULL, NULL, NULL,
  5561. model.layers[il].ffn_down,
  5562. NULL, NULL, NULL,
  5563. LLM_FFN_SWIGLU, LLM_FFN_SEQ, il);
  5564. // attentions bypass the intermediate layer
  5565. cur = ggml_add(ctx0, cur, ffn_inp);
  5566. // input for next layer
  5567. inpL = cur;
  5568. }
  5569. cur = inpL;
  5570. cur = build_norm(cur,
  5571. model.output_norm_enc, NULL,
  5572. LLM_NORM_RMS, -1);
  5573. cb(cur, "result_embd", -1);
  5574. res->t_embd = cur;
  5575. ggml_build_forward_expand(gf, cur);
  5576. }
  5577. };
  5578. struct llm_build_bloom : public llm_graph_context {
  5579. llm_build_bloom(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
  5580. const int64_t n_embd_head = hparams.n_embd_head_v;
  5581. const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  5582. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  5583. ggml_tensor * cur;
  5584. ggml_tensor * inpL;
  5585. inpL = build_inp_embd(model.tok_embd);
  5586. auto * inp_attn = build_attn_inp_kv_unified();
  5587. inpL = build_norm(inpL,
  5588. model.tok_norm,
  5589. model.tok_norm_b,
  5590. LLM_NORM, -1);
  5591. cb(inpL, "inp_norm", -1);
  5592. ggml_tensor * inp_out_ids = build_inp_out_ids();
  5593. for (int il = 0; il < n_layer; ++il) {
  5594. cur = build_norm(inpL,
  5595. model.layers[il].attn_norm,
  5596. model.layers[il].attn_norm_b,
  5597. LLM_NORM, il);
  5598. cb(cur, "attn_norm", il);
  5599. // self-attention
  5600. {
  5601. cur = build_lora_mm(model.layers[il].wqkv, cur);
  5602. cb(cur, "wqkv", il);
  5603. cur = ggml_add(ctx0, cur, model.layers[il].bqkv);
  5604. cb(cur, "bqkv", il);
  5605. ggml_tensor * Qcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd, n_tokens, cur->nb[1], 0*sizeof(float)*(n_embd)));
  5606. ggml_tensor * Kcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd)));
  5607. ggml_tensor * Vcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd + n_embd_gqa)));
  5608. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  5609. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  5610. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  5611. cb(Qcur, "Qcur", il);
  5612. cb(Kcur, "Kcur", il);
  5613. cb(Vcur, "Vcur", il);
  5614. cur = build_attn(inp_attn, gf,
  5615. model.layers[il].wo, model.layers[il].bo,
  5616. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  5617. }
  5618. if (il == n_layer - 1 && inp_out_ids) {
  5619. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  5620. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  5621. }
  5622. // Add the input
  5623. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
  5624. cb(ffn_inp, "ffn_inp", il);
  5625. // FF
  5626. {
  5627. cur = build_norm(ffn_inp,
  5628. model.layers[il].ffn_norm,
  5629. model.layers[il].ffn_norm_b,
  5630. LLM_NORM, il);
  5631. cb(cur, "ffn_norm", il);
  5632. cur = build_ffn(cur,
  5633. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  5634. NULL, NULL, NULL,
  5635. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  5636. NULL,
  5637. LLM_FFN_GELU, LLM_FFN_SEQ, il);
  5638. cb(cur, "ffn_out", il);
  5639. }
  5640. cur = ggml_add(ctx0, cur, ffn_inp);
  5641. cur = build_cvec(cur, il);
  5642. cb(cur, "l_out", il);
  5643. // input for next layer
  5644. inpL = cur;
  5645. }
  5646. cur = build_norm(inpL,
  5647. model.output_norm,
  5648. model.output_norm_b,
  5649. LLM_NORM, -1);
  5650. cb(cur, "result_norm", -1);
  5651. res->t_embd = cur;
  5652. cur = build_lora_mm(model.output, cur);
  5653. cb(cur, "result_output", -1);
  5654. res->t_logits = cur;
  5655. ggml_build_forward_expand(gf, cur);
  5656. }
  5657. };
  5658. struct llm_build_mpt : public llm_graph_context {
  5659. llm_build_mpt(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
  5660. const int64_t n_embd_head = hparams.n_embd_head_v;
  5661. const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  5662. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  5663. ggml_tensor * cur;
  5664. ggml_tensor * pos;
  5665. ggml_tensor * inpL;
  5666. inpL = build_inp_embd(model.tok_embd);
  5667. auto * inp_attn = build_attn_inp_kv_unified();
  5668. if (model.pos_embd) {
  5669. // inp_pos - contains the positions
  5670. ggml_tensor * inp_pos = build_inp_pos();
  5671. pos = ggml_get_rows(ctx0, model.pos_embd, inp_pos);
  5672. cb(pos, "pos_embd", -1);
  5673. inpL = ggml_add(ctx0, inpL, pos);
  5674. cb(inpL, "inpL", -1);
  5675. }
  5676. ggml_tensor * inp_out_ids = build_inp_out_ids();
  5677. for (int il = 0; il < n_layer; ++il) {
  5678. ggml_tensor * attn_norm;
  5679. attn_norm = build_norm(inpL,
  5680. model.layers[il].attn_norm,
  5681. model.layers[il].attn_norm_b,
  5682. LLM_NORM, il);
  5683. cb(attn_norm, "attn_norm", il);
  5684. // self-attention
  5685. {
  5686. cur = attn_norm;
  5687. cur = build_lora_mm(model.layers[il].wqkv, cur);
  5688. cb(cur, "wqkv", il);
  5689. if (model.layers[il].bqkv){
  5690. cur = ggml_add(ctx0, cur, model.layers[il].bqkv);
  5691. cb(cur, "bqkv", il);
  5692. }
  5693. if (hparams.f_clamp_kqv > 0.0f) {
  5694. cur = ggml_clamp(ctx0, cur, -hparams.f_clamp_kqv, hparams.f_clamp_kqv);
  5695. cb(cur, "wqkv_clamped", il);
  5696. }
  5697. ggml_tensor * Qcur = ggml_view_2d(ctx0, cur, n_embd, n_tokens, cur->nb[1], 0*sizeof(float)*(n_embd));
  5698. ggml_tensor * Kcur = ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd));
  5699. ggml_tensor * Vcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd + n_embd_gqa)));
  5700. cb(Qcur, "Qcur", il);
  5701. cb(Kcur, "Kcur", il);
  5702. cb(Vcur, "Vcur", il);
  5703. // Q/K Layernorm
  5704. if (model.layers[il].attn_q_norm) {
  5705. Qcur = build_norm(Qcur,
  5706. model.layers[il].attn_q_norm,
  5707. model.layers[il].attn_q_norm_b,
  5708. LLM_NORM, il);
  5709. cb(Qcur, "Qcur", il);
  5710. Kcur = build_norm(Kcur,
  5711. model.layers[il].attn_k_norm,
  5712. model.layers[il].attn_k_norm_b,
  5713. LLM_NORM, il);
  5714. cb(Kcur, "Kcur", il);
  5715. } else {
  5716. Qcur = ggml_cont(ctx0, Qcur);
  5717. cb(Qcur, "Qcur", il);
  5718. Kcur = ggml_cont(ctx0, Kcur);
  5719. cb(Kcur, "Kcur", il);
  5720. }
  5721. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  5722. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  5723. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  5724. cb(Qcur, "Qcur", il);
  5725. cb(Kcur, "Kcur", il);
  5726. cb(Vcur, "Vcur", il);
  5727. cur = build_attn(inp_attn, gf,
  5728. model.layers[il].wo, model.layers[il].bo,
  5729. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  5730. }
  5731. if (il == n_layer - 1 && inp_out_ids) {
  5732. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  5733. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  5734. }
  5735. // Add the input
  5736. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
  5737. cb(ffn_inp, "ffn_inp", il);
  5738. // feed forward
  5739. {
  5740. cur = build_norm(ffn_inp,
  5741. model.layers[il].ffn_norm,
  5742. model.layers[il].ffn_norm_b,
  5743. LLM_NORM, il);
  5744. cb(cur, "ffn_norm", il);
  5745. cur = build_ffn(cur,
  5746. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  5747. NULL, NULL, NULL,
  5748. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  5749. model.layers[il].ffn_act,
  5750. LLM_FFN_GELU, LLM_FFN_SEQ, il);
  5751. cb(cur, "ffn_out", il);
  5752. }
  5753. cur = ggml_add(ctx0, cur, ffn_inp);
  5754. cur = build_cvec(cur, il);
  5755. cb(cur, "l_out", il);
  5756. // input for next layer
  5757. inpL = cur;
  5758. }
  5759. cur = inpL;
  5760. cur = build_norm(cur,
  5761. model.output_norm,
  5762. model.output_norm_b,
  5763. LLM_NORM, -1);
  5764. cb(cur, "result_norm", -1);
  5765. res->t_embd = cur;
  5766. cur = build_lora_mm(model.output, cur);
  5767. cb(cur, "result_output", -1);
  5768. res->t_logits = cur;
  5769. ggml_build_forward_expand(gf, cur);
  5770. }
  5771. };
  5772. struct llm_build_stablelm : public llm_graph_context {
  5773. llm_build_stablelm(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
  5774. const int64_t n_embd_head = hparams.n_embd_head_v;
  5775. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  5776. ggml_tensor * cur;
  5777. ggml_tensor * inpL;
  5778. inpL = build_inp_embd(model.tok_embd);
  5779. // inp_pos - contains the positions
  5780. ggml_tensor * inp_pos = build_inp_pos();
  5781. auto * inp_attn = build_attn_inp_kv_unified();
  5782. ggml_tensor * inp_out_ids = build_inp_out_ids();
  5783. for (int il = 0; il < n_layer; ++il) {
  5784. // norm
  5785. cur = build_norm(inpL,
  5786. model.layers[il].attn_norm,
  5787. model.layers[il].attn_norm_b,
  5788. LLM_NORM, il);
  5789. cb(cur, "attn_norm", il);
  5790. ggml_tensor * inpSA = cur;
  5791. // self-attention
  5792. {
  5793. // compute Q and K and RoPE them
  5794. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  5795. cb(Qcur, "Qcur", il);
  5796. if (model.layers[il].bq) {
  5797. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  5798. cb(Qcur, "Qcur", il);
  5799. }
  5800. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  5801. cb(Kcur, "Kcur", il);
  5802. if (model.layers[il].bk) {
  5803. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  5804. cb(Kcur, "Kcur", il);
  5805. }
  5806. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  5807. cb(Vcur, "Vcur", il);
  5808. if (model.layers[il].bv) {
  5809. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  5810. cb(Vcur, "Vcur", il);
  5811. }
  5812. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  5813. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  5814. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  5815. if (model.layers[il].attn_q_norm) {
  5816. Qcur = build_norm(Qcur,
  5817. model.layers[il].attn_q_norm,
  5818. NULL,
  5819. LLM_NORM, il);
  5820. cb(Qcur, "Qcur", il);
  5821. }
  5822. if (model.layers[il].attn_k_norm) {
  5823. Kcur = build_norm(Kcur,
  5824. model.layers[il].attn_k_norm,
  5825. NULL,
  5826. LLM_NORM, il);
  5827. cb(Kcur, "Kcur", il);
  5828. }
  5829. Qcur = ggml_rope_ext(
  5830. ctx0, Qcur, inp_pos, nullptr,
  5831. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  5832. ext_factor, attn_factor, beta_fast, beta_slow
  5833. );
  5834. Kcur = ggml_rope_ext(
  5835. ctx0, Kcur, inp_pos, nullptr,
  5836. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  5837. ext_factor, attn_factor, beta_fast, beta_slow
  5838. );
  5839. cb(Qcur, "Qcur", il);
  5840. cb(Kcur, "Kcur", il);
  5841. cb(Vcur, "Vcur", il);
  5842. cur = build_attn(inp_attn, gf,
  5843. model.layers[il].wo, NULL,
  5844. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  5845. }
  5846. if (il == n_layer - 1 && inp_out_ids) {
  5847. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  5848. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  5849. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  5850. }
  5851. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
  5852. cb(ffn_inp, "ffn_inp", il);
  5853. // feed-forward network
  5854. {
  5855. if (model.layers[il].ffn_norm) {
  5856. cur = build_norm(ffn_inp,
  5857. model.layers[il].ffn_norm,
  5858. model.layers[il].ffn_norm_b,
  5859. LLM_NORM, il);
  5860. cb(cur, "ffn_norm", il);
  5861. } else {
  5862. // parallel residual
  5863. cur = inpSA;
  5864. }
  5865. cur = build_ffn(cur,
  5866. model.layers[il].ffn_up, NULL, NULL,
  5867. model.layers[il].ffn_gate, NULL, NULL,
  5868. model.layers[il].ffn_down, NULL, NULL,
  5869. NULL,
  5870. LLM_FFN_SILU, LLM_FFN_PAR, il);
  5871. cb(cur, "ffn_out", il);
  5872. }
  5873. cur = ggml_add(ctx0, cur, ffn_inp);
  5874. cur = build_cvec(cur, il);
  5875. cb(cur, "l_out", il);
  5876. // input for next layer
  5877. inpL = cur;
  5878. }
  5879. cur = inpL;
  5880. cur = build_norm(cur,
  5881. model.output_norm,
  5882. model.output_norm_b,
  5883. LLM_NORM, -1);
  5884. cb(cur, "result_norm", -1);
  5885. res->t_embd = cur;
  5886. // lm_head
  5887. cur = build_lora_mm(model.output, cur);
  5888. cb(cur, "result_output", -1);
  5889. res->t_logits = cur;
  5890. ggml_build_forward_expand(gf, cur);
  5891. }
  5892. };
  5893. struct llm_build_qwen : public llm_graph_context {
  5894. llm_build_qwen(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
  5895. const int64_t n_embd_head = hparams.n_embd_head_v;
  5896. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  5897. ggml_tensor * cur;
  5898. ggml_tensor * inpL;
  5899. inpL = build_inp_embd(model.tok_embd);
  5900. // inp_pos - contains the positions
  5901. ggml_tensor * inp_pos = build_inp_pos();
  5902. auto * inp_attn = build_attn_inp_kv_unified();
  5903. ggml_tensor * inp_out_ids = build_inp_out_ids();
  5904. for (int il = 0; il < n_layer; ++il) {
  5905. ggml_tensor * inpSA = inpL;
  5906. cur = build_norm(inpL,
  5907. model.layers[il].attn_norm, NULL,
  5908. LLM_NORM_RMS, il);
  5909. cb(cur, "attn_norm", il);
  5910. // self-attention
  5911. {
  5912. cur = build_lora_mm(model.layers[il].wqkv, cur);
  5913. cb(cur, "wqkv", il);
  5914. cur = ggml_add(ctx0, cur, model.layers[il].bqkv);
  5915. cb(cur, "bqkv", il);
  5916. 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));
  5917. 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));
  5918. ggml_tensor * Vcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd, n_tokens, cur->nb[1], 2*sizeof(float)*(n_embd)));
  5919. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  5920. // using mode = 2 for neox mode
  5921. Qcur = ggml_rope_ext(
  5922. ctx0, Qcur, inp_pos, nullptr,
  5923. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  5924. ext_factor, attn_factor, beta_fast, beta_slow
  5925. );
  5926. Kcur = ggml_rope_ext(
  5927. ctx0, Kcur, inp_pos, nullptr,
  5928. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  5929. ext_factor, attn_factor, beta_fast, beta_slow
  5930. );
  5931. cb(Qcur, "Qcur", il);
  5932. cb(Kcur, "Kcur", il);
  5933. cb(Vcur, "Vcur", il);
  5934. cur = build_attn(inp_attn, gf,
  5935. model.layers[il].wo, NULL,
  5936. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  5937. }
  5938. if (il == n_layer - 1 && inp_out_ids) {
  5939. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  5940. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  5941. }
  5942. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  5943. cb(ffn_inp, "ffn_inp", il);
  5944. // feed-forward forward
  5945. {
  5946. cur = build_norm(ffn_inp,
  5947. model.layers[il].ffn_norm, NULL,
  5948. LLM_NORM_RMS, il);
  5949. cb(cur, "ffn_norm", il);
  5950. cur = build_ffn(cur,
  5951. model.layers[il].ffn_up, NULL, NULL,
  5952. model.layers[il].ffn_gate, NULL, NULL,
  5953. model.layers[il].ffn_down, NULL, NULL,
  5954. NULL,
  5955. LLM_FFN_SILU, LLM_FFN_PAR, il);
  5956. cb(cur, "ffn_out", il);
  5957. }
  5958. cur = ggml_add(ctx0, cur, ffn_inp);
  5959. cur = build_cvec(cur, il);
  5960. cb(cur, "l_out", il);
  5961. // input for next layer
  5962. inpL = cur;
  5963. }
  5964. cur = inpL;
  5965. cur = build_norm(cur,
  5966. model.output_norm, NULL,
  5967. LLM_NORM_RMS, -1);
  5968. cb(cur, "result_norm", -1);
  5969. res->t_embd = cur;
  5970. // lm_head
  5971. cur = build_lora_mm(model.output, cur);
  5972. cb(cur, "result_output", -1);
  5973. res->t_logits = cur;
  5974. ggml_build_forward_expand(gf, cur);
  5975. }
  5976. };
  5977. struct llm_build_qwen2 : public llm_graph_context {
  5978. llm_build_qwen2(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
  5979. const int64_t n_embd_head = hparams.n_embd_head_v;
  5980. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  5981. GGML_ASSERT(n_embd_head == hparams.n_rot);
  5982. ggml_tensor * cur;
  5983. ggml_tensor * inpL;
  5984. inpL = build_inp_embd(model.tok_embd);
  5985. // inp_pos - contains the positions
  5986. ggml_tensor * inp_pos = build_inp_pos();
  5987. auto * inp_attn = build_attn_inp_kv_unified();
  5988. ggml_tensor * inp_out_ids = build_inp_out_ids();
  5989. for (int il = 0; il < n_layer; ++il) {
  5990. ggml_tensor * inpSA = inpL;
  5991. // norm
  5992. cur = build_norm(inpL,
  5993. model.layers[il].attn_norm, NULL,
  5994. LLM_NORM_RMS, il);
  5995. cb(cur, "attn_norm", il);
  5996. // self-attention
  5997. {
  5998. // compute Q and K and RoPE them
  5999. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  6000. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  6001. cb(Qcur, "Qcur", il);
  6002. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  6003. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  6004. cb(Kcur, "Kcur", il);
  6005. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  6006. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  6007. cb(Vcur, "Vcur", il);
  6008. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  6009. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  6010. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  6011. Qcur = ggml_rope_ext(
  6012. ctx0, Qcur, inp_pos, nullptr,
  6013. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  6014. ext_factor, attn_factor, beta_fast, beta_slow
  6015. );
  6016. Kcur = ggml_rope_ext(
  6017. ctx0, Kcur, inp_pos, nullptr,
  6018. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  6019. ext_factor, attn_factor, beta_fast, beta_slow
  6020. );
  6021. cb(Qcur, "Qcur", il);
  6022. cb(Kcur, "Kcur", il);
  6023. cb(Vcur, "Vcur", il);
  6024. cur = build_attn(inp_attn, gf,
  6025. model.layers[il].wo, model.layers[il].bo,
  6026. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  6027. }
  6028. if (il == n_layer - 1 && inp_out_ids) {
  6029. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  6030. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  6031. }
  6032. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  6033. cb(ffn_inp, "ffn_inp", il);
  6034. // feed-forward network
  6035. cur = build_norm(ffn_inp,
  6036. model.layers[il].ffn_norm, NULL,
  6037. LLM_NORM_RMS, il);
  6038. cb(cur, "ffn_norm", il);
  6039. cur = build_ffn(cur,
  6040. model.layers[il].ffn_up, NULL, NULL,
  6041. model.layers[il].ffn_gate, NULL, NULL,
  6042. model.layers[il].ffn_down, NULL, NULL,
  6043. NULL,
  6044. LLM_FFN_SILU, LLM_FFN_PAR, il);
  6045. cb(cur, "ffn_out", il);
  6046. cur = ggml_add(ctx0, cur, ffn_inp);
  6047. cur = build_cvec(cur, il);
  6048. cb(cur, "l_out", il);
  6049. // input for next layer
  6050. inpL = cur;
  6051. }
  6052. cur = inpL;
  6053. cur = build_norm(cur,
  6054. model.output_norm, NULL,
  6055. LLM_NORM_RMS, -1);
  6056. cb(cur, "result_norm", -1);
  6057. res->t_embd = cur;
  6058. // lm_head
  6059. cur = build_lora_mm(model.output, cur);
  6060. cb(cur, "result_output", -1);
  6061. res->t_logits = cur;
  6062. ggml_build_forward_expand(gf, cur);
  6063. }
  6064. };
  6065. struct llm_build_qwen2vl : public llm_graph_context {
  6066. llm_build_qwen2vl(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
  6067. const int64_t n_embd_head = hparams.n_embd_head_v;
  6068. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  6069. GGML_ASSERT(n_embd_head == hparams.n_rot);
  6070. ggml_tensor * cur;
  6071. ggml_tensor * inpL;
  6072. inpL = build_inp_embd(model.tok_embd);
  6073. // inp_pos - contains the positions
  6074. ggml_tensor * inp_pos = build_inp_pos();
  6075. auto * inp_attn = build_attn_inp_kv_unified();
  6076. int sections[4];
  6077. std::copy(std::begin(hparams.rope_sections), std::begin(hparams.rope_sections) + 4, sections);
  6078. ggml_tensor * inp_out_ids = build_inp_out_ids();
  6079. for (int il = 0; il < n_layer; ++il) {
  6080. ggml_tensor * inpSA = inpL;
  6081. // norm
  6082. cur = build_norm(inpL,
  6083. model.layers[il].attn_norm, NULL,
  6084. LLM_NORM_RMS, il);
  6085. cb(cur, "attn_norm", il);
  6086. // self-attention
  6087. {
  6088. // compute Q and K and RoPE them
  6089. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  6090. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  6091. cb(Qcur, "Qcur", il);
  6092. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  6093. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  6094. cb(Kcur, "Kcur", il);
  6095. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  6096. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  6097. cb(Vcur, "Vcur", il);
  6098. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  6099. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  6100. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  6101. Qcur = ggml_rope_multi(
  6102. ctx0, Qcur, inp_pos, nullptr,
  6103. n_rot, sections, rope_type, n_ctx_orig, freq_base, freq_scale,
  6104. ext_factor, attn_factor, beta_fast, beta_slow
  6105. );
  6106. Kcur = ggml_rope_multi(
  6107. ctx0, Kcur, inp_pos, nullptr,
  6108. n_rot, sections, rope_type, n_ctx_orig, freq_base, freq_scale,
  6109. ext_factor, attn_factor, beta_fast, beta_slow
  6110. );
  6111. cb(Qcur, "Qcur", il);
  6112. cb(Kcur, "Kcur", il);
  6113. cb(Vcur, "Vcur", il);
  6114. cur = build_attn(inp_attn, gf,
  6115. model.layers[il].wo, model.layers[il].bo,
  6116. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  6117. }
  6118. if (il == n_layer - 1 && inp_out_ids) {
  6119. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  6120. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  6121. }
  6122. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  6123. cb(ffn_inp, "ffn_inp", il);
  6124. // feed-forward network
  6125. cur = build_norm(ffn_inp,
  6126. model.layers[il].ffn_norm, NULL,
  6127. LLM_NORM_RMS, il);
  6128. cb(cur, "ffn_norm", il);
  6129. cur = build_ffn(cur,
  6130. model.layers[il].ffn_up, NULL, NULL,
  6131. model.layers[il].ffn_gate, NULL, NULL,
  6132. model.layers[il].ffn_down, NULL, NULL,
  6133. NULL,
  6134. LLM_FFN_SILU, LLM_FFN_PAR, il);
  6135. cb(cur, "ffn_out", il);
  6136. cur = ggml_add(ctx0, cur, ffn_inp);
  6137. cur = build_cvec(cur, il);
  6138. cb(cur, "l_out", il);
  6139. // input for next layer
  6140. inpL = cur;
  6141. }
  6142. cur = inpL;
  6143. cur = build_norm(cur,
  6144. model.output_norm, NULL,
  6145. LLM_NORM_RMS, -1);
  6146. cb(cur, "result_norm", -1);
  6147. res->t_embd = cur;
  6148. // lm_head
  6149. cur = build_lora_mm(model.output, cur);
  6150. cb(cur, "result_output", -1);
  6151. res->t_logits = cur;
  6152. ggml_build_forward_expand(gf, cur);
  6153. }
  6154. };
  6155. struct llm_build_qwen2moe : public llm_graph_context {
  6156. llm_build_qwen2moe(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
  6157. const int64_t n_embd_head = hparams.n_embd_head_v;
  6158. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  6159. GGML_ASSERT(n_embd_head == hparams.n_rot);
  6160. ggml_tensor * cur;
  6161. ggml_tensor * inpL;
  6162. inpL = build_inp_embd(model.tok_embd);
  6163. // inp_pos - contains the positions
  6164. ggml_tensor * inp_pos = build_inp_pos();
  6165. auto * inp_attn = build_attn_inp_kv_unified();
  6166. ggml_tensor * inp_out_ids = build_inp_out_ids();
  6167. for (int il = 0; il < n_layer; ++il) {
  6168. ggml_tensor * inpSA = inpL;
  6169. // norm
  6170. cur = build_norm(inpL,
  6171. model.layers[il].attn_norm, NULL,
  6172. LLM_NORM_RMS, il);
  6173. cb(cur, "attn_norm", il);
  6174. // self_attention
  6175. {
  6176. // compute Q and K and RoPE them
  6177. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  6178. cb(Qcur, "Qcur", il);
  6179. if (model.layers[il].bq) {
  6180. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  6181. cb(Qcur, "Qcur", il);
  6182. }
  6183. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  6184. cb(Kcur, "Kcur", il);
  6185. if (model.layers[il].bk) {
  6186. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  6187. cb(Kcur, "Kcur", il);
  6188. }
  6189. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  6190. cb(Vcur, "Vcur", il);
  6191. if (model.layers[il].bv) {
  6192. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  6193. cb(Vcur, "Vcur", il);
  6194. }
  6195. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  6196. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  6197. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  6198. Qcur = ggml_rope_ext(
  6199. ctx0, Qcur, 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. Kcur = ggml_rope_ext(
  6204. ctx0, Kcur, inp_pos, nullptr,
  6205. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  6206. ext_factor, attn_factor, beta_fast, beta_slow
  6207. );
  6208. cb(Qcur, "Qcur", il);
  6209. cb(Kcur, "Kcur", il);
  6210. cb(Vcur, "Vcur", il);
  6211. cur = build_attn(inp_attn, gf,
  6212. model.layers[il].wo, model.layers[il].bo,
  6213. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  6214. }
  6215. if (il == n_layer - 1 && inp_out_ids) {
  6216. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  6217. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  6218. }
  6219. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  6220. cb(ffn_inp, "ffn_inp", il);
  6221. // MoE branch
  6222. cur = build_norm(ffn_inp,
  6223. model.layers[il].ffn_norm, NULL,
  6224. LLM_NORM_RMS, il);
  6225. cb(cur, "ffn_norm", il);
  6226. ggml_tensor * moe_out =
  6227. build_moe_ffn(cur,
  6228. model.layers[il].ffn_gate_inp,
  6229. model.layers[il].ffn_up_exps,
  6230. model.layers[il].ffn_gate_exps,
  6231. model.layers[il].ffn_down_exps,
  6232. nullptr,
  6233. n_expert, n_expert_used,
  6234. LLM_FFN_SILU, false,
  6235. false, 0.0,
  6236. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  6237. il);
  6238. cb(moe_out, "ffn_moe_out", il);
  6239. // FFN shared expert
  6240. {
  6241. ggml_tensor * cur_gate_inp = build_lora_mm(model.layers[il].ffn_gate_inp_shexp, cur);
  6242. cb(cur_gate_inp, "ffn_shexp_gate_inp", il);
  6243. // sigmoid
  6244. ggml_tensor * cur_gate = ggml_div(ctx0, ggml_silu(ctx0, cur_gate_inp), cur_gate_inp);
  6245. cb(cur_gate, "ffn_shexp_gate", il);
  6246. ggml_tensor * cur_ffn = build_ffn(cur,
  6247. model.layers[il].ffn_up_shexp, NULL, NULL,
  6248. model.layers[il].ffn_gate_shexp, NULL, NULL,
  6249. model.layers[il].ffn_down_shexp, NULL, NULL,
  6250. NULL,
  6251. LLM_FFN_SILU, LLM_FFN_PAR, il);
  6252. cb(cur_ffn, "ffn_shexp", il);
  6253. ggml_tensor * ffn_shexp_out = ggml_mul(ctx0, cur_ffn, cur_gate);
  6254. cb(ffn_shexp_out, "ffn_shexp_out", il);
  6255. moe_out = ggml_add(ctx0, moe_out, ffn_shexp_out);
  6256. cb(moe_out, "ffn_out", il);
  6257. cur = moe_out;
  6258. }
  6259. cur = ggml_add(ctx0, cur, ffn_inp);
  6260. cur = build_cvec(cur, il);
  6261. cb(cur, "l_out", il);
  6262. // input for next layer
  6263. inpL = cur;
  6264. }
  6265. cur = inpL;
  6266. cur = build_norm(cur,
  6267. model.output_norm, NULL,
  6268. LLM_NORM_RMS, -1);
  6269. cb(cur, "result_norm", -1);
  6270. res->t_embd = cur;
  6271. // lm_head
  6272. cur = build_lora_mm(model.output, cur);
  6273. cb(cur, "result_output", -1);
  6274. res->t_logits = cur;
  6275. ggml_build_forward_expand(gf, cur);
  6276. }
  6277. };
  6278. struct llm_build_qwen3 : public llm_graph_context {
  6279. llm_build_qwen3(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
  6280. const int64_t n_embd_head = hparams.n_embd_head_v;
  6281. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  6282. GGML_ASSERT(n_embd_head == hparams.n_rot);
  6283. ggml_tensor * cur;
  6284. ggml_tensor * inpL;
  6285. inpL = build_inp_embd(model.tok_embd);
  6286. // inp_pos - contains the positions
  6287. ggml_tensor * inp_pos = build_inp_pos();
  6288. auto * inp_attn = build_attn_inp_kv_unified();
  6289. ggml_tensor * inp_out_ids = build_inp_out_ids();
  6290. for (int il = 0; il < n_layer; ++il) {
  6291. ggml_tensor * inpSA = inpL;
  6292. // norm
  6293. cur = build_norm(inpL,
  6294. model.layers[il].attn_norm, NULL,
  6295. LLM_NORM_RMS, il);
  6296. cb(cur, "attn_norm", il);
  6297. // self-attention
  6298. {
  6299. // compute Q and K and RoPE them
  6300. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  6301. cb(Qcur, "Qcur", il);
  6302. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  6303. cb(Kcur, "Kcur", il);
  6304. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  6305. cb(Vcur, "Vcur", il);
  6306. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  6307. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  6308. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  6309. Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);
  6310. cb(Qcur, "Qcur_normed", il);
  6311. Qcur = ggml_rope_ext(
  6312. ctx0, Qcur, inp_pos, nullptr,
  6313. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  6314. ext_factor, attn_factor, beta_fast, beta_slow
  6315. );
  6316. Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);
  6317. cb(Kcur, "Kcur_normed", il);
  6318. Kcur = ggml_rope_ext(
  6319. ctx0, Kcur, inp_pos, nullptr,
  6320. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  6321. ext_factor, attn_factor, beta_fast, beta_slow
  6322. );
  6323. cb(Qcur, "Qcur", il);
  6324. cb(Kcur, "Kcur", il);
  6325. cb(Vcur, "Vcur", il);
  6326. cur = build_attn(inp_attn, gf,
  6327. model.layers[il].wo, model.layers[il].bo,
  6328. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  6329. }
  6330. if (il == n_layer - 1 && inp_out_ids) {
  6331. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  6332. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  6333. }
  6334. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  6335. cb(ffn_inp, "ffn_inp", il);
  6336. // feed-forward network
  6337. cur = build_norm(ffn_inp,
  6338. model.layers[il].ffn_norm, NULL,
  6339. LLM_NORM_RMS, il);
  6340. cb(cur, "ffn_norm", il);
  6341. cur = build_ffn(cur,
  6342. model.layers[il].ffn_up, NULL, NULL,
  6343. model.layers[il].ffn_gate, NULL, NULL,
  6344. model.layers[il].ffn_down, NULL, NULL,
  6345. NULL,
  6346. LLM_FFN_SILU, LLM_FFN_PAR, il);
  6347. cb(cur, "ffn_out", il);
  6348. cur = ggml_add(ctx0, cur, ffn_inp);
  6349. cur = build_cvec(cur, il);
  6350. cb(cur, "l_out", il);
  6351. // input for next layer
  6352. inpL = cur;
  6353. }
  6354. cur = inpL;
  6355. cur = build_norm(cur,
  6356. model.output_norm, NULL,
  6357. LLM_NORM_RMS, -1);
  6358. cb(cur, "result_norm", -1);
  6359. res->t_embd = cur;
  6360. // lm_head
  6361. cur = build_lora_mm(model.output, cur);
  6362. cb(cur, "result_output", -1);
  6363. res->t_logits = cur;
  6364. ggml_build_forward_expand(gf, cur);
  6365. }
  6366. };
  6367. struct llm_build_qwen3moe : public llm_graph_context {
  6368. llm_build_qwen3moe(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
  6369. const int64_t n_embd_head = hparams.n_embd_head_v;
  6370. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  6371. GGML_ASSERT(n_embd_head == hparams.n_rot);
  6372. ggml_tensor * cur;
  6373. ggml_tensor * inpL;
  6374. inpL = build_inp_embd(model.tok_embd);
  6375. // inp_pos - contains the positions
  6376. ggml_tensor * inp_pos = build_inp_pos();
  6377. auto * inp_attn = build_attn_inp_kv_unified();
  6378. ggml_tensor * inp_out_ids = build_inp_out_ids();
  6379. for (int il = 0; il < n_layer; ++il) {
  6380. ggml_tensor * inpSA = inpL;
  6381. // norm
  6382. cur = build_norm(inpL,
  6383. model.layers[il].attn_norm, NULL,
  6384. LLM_NORM_RMS, il);
  6385. cb(cur, "attn_norm", il);
  6386. // self_attention
  6387. {
  6388. // compute Q and K and RoPE them
  6389. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  6390. cb(Qcur, "Qcur", il);
  6391. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  6392. cb(Kcur, "Kcur", il);
  6393. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  6394. cb(Vcur, "Vcur", il);
  6395. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  6396. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  6397. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  6398. Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);
  6399. cb(Qcur, "Qcur_normed", il);
  6400. Qcur = ggml_rope_ext(
  6401. ctx0, Qcur, inp_pos, nullptr,
  6402. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  6403. ext_factor, attn_factor, beta_fast, beta_slow
  6404. );
  6405. Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);
  6406. cb(Kcur, "Kcur_normed", il);
  6407. Kcur = ggml_rope_ext(
  6408. ctx0, Kcur, inp_pos, nullptr,
  6409. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  6410. ext_factor, attn_factor, beta_fast, beta_slow
  6411. );
  6412. cb(Qcur, "Qcur", il);
  6413. cb(Kcur, "Kcur", il);
  6414. cb(Vcur, "Vcur", il);
  6415. cur = build_attn(inp_attn, gf,
  6416. model.layers[il].wo, model.layers[il].bo,
  6417. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  6418. }
  6419. if (il == n_layer - 1 && inp_out_ids) {
  6420. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  6421. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  6422. }
  6423. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  6424. cb(ffn_inp, "ffn_inp", il);
  6425. // MoE branch
  6426. cur = build_norm(ffn_inp,
  6427. model.layers[il].ffn_norm, NULL,
  6428. LLM_NORM_RMS, il);
  6429. cb(cur, "ffn_norm", il);
  6430. ggml_tensor * moe_out =
  6431. build_moe_ffn(cur,
  6432. model.layers[il].ffn_gate_inp,
  6433. model.layers[il].ffn_up_exps,
  6434. model.layers[il].ffn_gate_exps,
  6435. model.layers[il].ffn_down_exps,
  6436. nullptr,
  6437. n_expert, n_expert_used,
  6438. LLM_FFN_SILU, true,
  6439. false, 0.0,
  6440. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  6441. il);
  6442. cb(moe_out, "ffn_moe_out", il);
  6443. cur = moe_out;
  6444. cur = ggml_add(ctx0, cur, ffn_inp);
  6445. cur = build_cvec(cur, il);
  6446. cb(cur, "l_out", il);
  6447. // input for next layer
  6448. inpL = cur;
  6449. }
  6450. cur = inpL;
  6451. cur = build_norm(cur,
  6452. model.output_norm, NULL,
  6453. LLM_NORM_RMS, -1);
  6454. cb(cur, "result_norm", -1);
  6455. res->t_embd = cur;
  6456. // lm_head
  6457. cur = build_lora_mm(model.output, cur);
  6458. cb(cur, "result_output", -1);
  6459. res->t_logits = cur;
  6460. ggml_build_forward_expand(gf, cur);
  6461. }
  6462. };
  6463. struct llm_build_phi2 : public llm_graph_context {
  6464. llm_build_phi2(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
  6465. const int64_t n_embd_head = hparams.n_embd_head_v;
  6466. const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  6467. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  6468. ggml_tensor * cur;
  6469. ggml_tensor * attn_norm_output;
  6470. ggml_tensor * ffn_output;
  6471. ggml_tensor * inpL;
  6472. inpL = build_inp_embd(model.tok_embd);
  6473. // inp_pos - contains the positions
  6474. ggml_tensor * inp_pos = build_inp_pos();
  6475. auto * inp_attn = build_attn_inp_kv_unified();
  6476. ggml_tensor * inp_out_ids = build_inp_out_ids();
  6477. for (int il = 0; il < n_layer; ++il) {
  6478. attn_norm_output = build_norm(inpL,
  6479. model.layers[il].attn_norm,
  6480. model.layers[il].attn_norm_b,
  6481. LLM_NORM, il);
  6482. cb(attn_norm_output, "attn_norm", il);
  6483. // self-attention
  6484. {
  6485. ggml_tensor * Qcur = nullptr;
  6486. ggml_tensor * Kcur = nullptr;
  6487. ggml_tensor * Vcur = nullptr;
  6488. if (model.layers[il].wqkv) {
  6489. cur = build_lora_mm(model.layers[il].wqkv, attn_norm_output);
  6490. cb(cur, "wqkv", il);
  6491. cur = ggml_add(ctx0, cur, model.layers[il].bqkv);
  6492. cb(cur, "bqkv", il);
  6493. 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));
  6494. 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));
  6495. Vcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd + n_embd_gqa)));
  6496. } else {
  6497. Qcur = ggml_add(ctx0, build_lora_mm(model.layers[il].wq, attn_norm_output), model.layers[il].bq);
  6498. Kcur = ggml_add(ctx0, build_lora_mm(model.layers[il].wk, attn_norm_output), model.layers[il].bk);
  6499. Vcur = ggml_add(ctx0, build_lora_mm(model.layers[il].wv, attn_norm_output), model.layers[il].bv);
  6500. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  6501. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  6502. }
  6503. cb(Qcur, "Qcur", il);
  6504. cb(Kcur, "Kcur", il);
  6505. cb(Vcur, "Vcur", il);
  6506. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  6507. Qcur = ggml_rope_ext(
  6508. ctx0, Qcur, inp_pos, nullptr,
  6509. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  6510. ext_factor, attn_factor, beta_fast, beta_slow
  6511. );
  6512. Kcur = ggml_rope_ext(
  6513. ctx0, Kcur, inp_pos, nullptr,
  6514. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  6515. ext_factor, attn_factor, beta_fast, beta_slow
  6516. );
  6517. cb(Qcur, "Qcur", il);
  6518. cb(Kcur, "Kcur", il);
  6519. cb(Vcur, "Vcur", il);
  6520. // with phi2, we scale the Q to avoid precision issues
  6521. // ref: https://github.com/ml-explore/mlx-examples/blob/08e862336ade809bc37d1035f94b359e7d1a5152/phi2/phi2.py#L64-L66
  6522. Qcur = ggml_scale(ctx0, Qcur, 1.0f/sqrtf(float(n_embd_head)));
  6523. cur = build_attn(inp_attn, gf,
  6524. model.layers[il].wo, model.layers[il].bo,
  6525. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f, il);
  6526. }
  6527. if (il == n_layer - 1 && inp_out_ids) {
  6528. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  6529. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  6530. attn_norm_output = ggml_get_rows(ctx0, attn_norm_output, inp_out_ids);
  6531. }
  6532. // FF
  6533. {
  6534. ffn_output = build_ffn(attn_norm_output,
  6535. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  6536. NULL, NULL, NULL,
  6537. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  6538. NULL,
  6539. LLM_FFN_GELU, LLM_FFN_SEQ, il);
  6540. cb(ffn_output, "ffn_out", il);
  6541. }
  6542. cur = ggml_add(ctx0, cur, ffn_output);
  6543. cur = ggml_add(ctx0, cur, inpL);
  6544. cur = build_cvec(cur, il);
  6545. cb(cur, "l_out", il);
  6546. // input for next layer
  6547. inpL = cur;
  6548. }
  6549. cur = build_norm(inpL,
  6550. model.output_norm,
  6551. model.output_norm_b,
  6552. LLM_NORM, -1);
  6553. cb(cur, "result_norm", -1);
  6554. res->t_embd = cur;
  6555. cur = build_lora_mm(model.output, cur);
  6556. cb(cur, "result_output_no_bias", -1);
  6557. cur = ggml_add(ctx0, cur, model.output_b);
  6558. cb(cur, "result_output", -1);
  6559. res->t_logits = cur;
  6560. ggml_build_forward_expand(gf, cur);
  6561. }
  6562. };
  6563. template<bool iswa>
  6564. struct llm_build_phi3 : public llm_graph_context {
  6565. llm_build_phi3(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
  6566. const int64_t n_embd_head = hparams.n_embd_head_v;
  6567. const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  6568. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  6569. ggml_tensor * cur;
  6570. ggml_tensor * inpL;
  6571. inpL = build_inp_embd(model.tok_embd);
  6572. // inp_pos - contains the positions
  6573. ggml_tensor * inp_pos = build_inp_pos();
  6574. using inp_attn_type = std::conditional_t<iswa, llm_graph_input_attn_kv_unified_iswa, llm_graph_input_attn_kv_unified>;
  6575. inp_attn_type * inp_attn = nullptr;
  6576. if constexpr (iswa) {
  6577. inp_attn = build_attn_inp_kv_unified_iswa();
  6578. } else {
  6579. inp_attn = build_attn_inp_kv_unified();
  6580. }
  6581. ggml_tensor * inp_out_ids = build_inp_out_ids();
  6582. for (int il = 0; il < n_layer; ++il) {
  6583. auto * residual = inpL;
  6584. // self-attention
  6585. {
  6586. // rope freq factors for 128k context
  6587. ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
  6588. ggml_tensor* attn_norm_output = build_norm(inpL,
  6589. model.layers[il].attn_norm,
  6590. model.layers[il].attn_norm_b,
  6591. LLM_NORM_RMS, il);
  6592. cb(attn_norm_output, "attn_norm", il);
  6593. ggml_tensor * Qcur = nullptr;
  6594. ggml_tensor * Kcur = nullptr;
  6595. ggml_tensor * Vcur = nullptr;
  6596. if (model.layers[il].wqkv) {
  6597. cur = build_lora_mm(model.layers[il].wqkv, attn_norm_output);
  6598. cb(cur, "wqkv", il);
  6599. 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));
  6600. 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));
  6601. Vcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1 * sizeof(float) * (n_embd + n_embd_gqa)));
  6602. } else {
  6603. Qcur = ggml_add(ctx0, build_lora_mm(model.layers[il].wq, attn_norm_output), model.layers[il].bq);
  6604. Kcur = ggml_add(ctx0, build_lora_mm(model.layers[il].wk, attn_norm_output), model.layers[il].bk);
  6605. Vcur = ggml_add(ctx0, build_lora_mm(model.layers[il].wv, attn_norm_output), model.layers[il].bv);
  6606. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  6607. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  6608. }
  6609. cb(Qcur, "Qcur", il);
  6610. cb(Kcur, "Kcur", il);
  6611. cb(Vcur, "Vcur", il);
  6612. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  6613. Qcur = ggml_rope_ext(
  6614. ctx0, Qcur, inp_pos, rope_factors,
  6615. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  6616. ext_factor, attn_factor, beta_fast, beta_slow
  6617. );
  6618. Kcur = ggml_rope_ext(
  6619. ctx0, Kcur, inp_pos, rope_factors,
  6620. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  6621. ext_factor, attn_factor, beta_fast, beta_slow
  6622. );
  6623. cb(Qcur, "Qcur", il);
  6624. cb(Kcur, "Kcur", il);
  6625. cb(Vcur, "Vcur", il);
  6626. Qcur = ggml_scale(ctx0, Qcur, 1.0f / sqrtf(float(n_embd_head)));
  6627. cb(Qcur, "Qcur", il);
  6628. cur = build_attn(inp_attn, gf,
  6629. model.layers[il].wo, model.layers[il].bo,
  6630. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f, il);
  6631. }
  6632. if (il == n_layer - 1 && inp_out_ids) {
  6633. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  6634. residual = ggml_get_rows(ctx0, residual, inp_out_ids);
  6635. }
  6636. cur = ggml_add(ctx0, cur, residual);
  6637. residual = cur;
  6638. cur = build_norm(cur,
  6639. model.layers[il].ffn_norm, model.layers[il].ffn_norm_b,
  6640. LLM_NORM_RMS, il);
  6641. cb(cur, "ffn_norm", il);
  6642. // feed-forward network
  6643. if (model.layers[il].ffn_gate_inp == nullptr) {
  6644. cur = build_ffn(cur,
  6645. model.layers[il].ffn_up, NULL, NULL,
  6646. NULL, NULL, NULL,
  6647. model.layers[il].ffn_down, NULL, NULL,
  6648. NULL,
  6649. LLM_FFN_SWIGLU, LLM_FFN_SEQ, il);
  6650. cb(cur, "ffn_out", il);
  6651. } else {
  6652. // MoE branch
  6653. cur = build_moe_ffn(cur,
  6654. model.layers[il].ffn_gate_inp,
  6655. model.layers[il].ffn_up_exps,
  6656. model.layers[il].ffn_gate_exps,
  6657. model.layers[il].ffn_down_exps,
  6658. nullptr,
  6659. n_expert, n_expert_used,
  6660. LLM_FFN_SILU, true,
  6661. false, 0.0,
  6662. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  6663. il);
  6664. cb(cur, "ffn_moe_out", il);
  6665. }
  6666. cur = ggml_add(ctx0, residual, cur);
  6667. cur = build_cvec(cur, il);
  6668. cb(cur, "l_out", il);
  6669. // input for next layer
  6670. inpL = cur;
  6671. }
  6672. cur = build_norm(inpL,
  6673. model.output_norm,
  6674. model.output_norm_b,
  6675. LLM_NORM_RMS, -1);
  6676. cb(cur, "result_norm", -1);
  6677. res->t_embd = cur;
  6678. cur = build_lora_mm(model.output, cur);
  6679. if (model.output_b != nullptr) {
  6680. cb(cur, "result_output_no_bias", -1);
  6681. cur = ggml_add(ctx0, cur, model.output_b);
  6682. }
  6683. cb(cur, "result_output", -1);
  6684. res->t_logits = cur;
  6685. ggml_build_forward_expand(gf, cur);
  6686. }
  6687. };
  6688. struct llm_build_plamo : public llm_graph_context {
  6689. llm_build_plamo(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
  6690. const int64_t n_embd_head = hparams.n_embd_head_v;
  6691. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  6692. GGML_ASSERT(n_embd_head == hparams.n_rot);
  6693. ggml_tensor * cur;
  6694. ggml_tensor * inpL;
  6695. inpL = build_inp_embd(model.tok_embd);
  6696. // inp_pos - contains the positions
  6697. ggml_tensor * inp_pos = build_inp_pos();
  6698. auto * inp_attn = build_attn_inp_kv_unified();
  6699. ggml_tensor * inp_out_ids = build_inp_out_ids();
  6700. for (int il = 0; il < n_layer; ++il) {
  6701. // norm
  6702. cur = build_norm(inpL,
  6703. model.layers[il].attn_norm, NULL,
  6704. LLM_NORM_RMS, il);
  6705. cb(cur, "attn_norm", il);
  6706. ggml_tensor * sa_inp = cur;
  6707. // self-attention
  6708. {
  6709. // compute Q and K and RoPE them
  6710. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  6711. cb(Qcur, "Qcur", il);
  6712. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  6713. cb(Kcur, "Kcur", il);
  6714. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  6715. cb(Vcur, "Vcur", il);
  6716. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  6717. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  6718. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  6719. Qcur = ggml_rope_ext(
  6720. ctx0, Qcur, inp_pos, nullptr,
  6721. n_embd_head, rope_type, n_ctx_orig, freq_base, freq_scale,
  6722. ext_factor, attn_factor, beta_fast, beta_slow
  6723. );
  6724. Kcur = ggml_rope_ext(
  6725. ctx0, Kcur, inp_pos, nullptr,
  6726. n_embd_head, rope_type, n_ctx_orig, freq_base, freq_scale,
  6727. ext_factor, attn_factor, beta_fast, beta_slow
  6728. );
  6729. cb(Qcur, "Qcur", il);
  6730. cb(Kcur, "Kcur", il);
  6731. cb(Vcur, "Vcur", il);
  6732. cur = build_attn(inp_attn, gf,
  6733. model.layers[il].wo, NULL,
  6734. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  6735. }
  6736. if (il == n_layer - 1 && inp_out_ids) {
  6737. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  6738. sa_inp = ggml_get_rows(ctx0, sa_inp, inp_out_ids);
  6739. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  6740. }
  6741. ggml_tensor * sa_out = cur;
  6742. cur = sa_inp;
  6743. // feed-forward network
  6744. {
  6745. cur = build_ffn(cur,
  6746. model.layers[il].ffn_up, NULL, NULL,
  6747. model.layers[il].ffn_gate, NULL, NULL,
  6748. model.layers[il].ffn_down, NULL, NULL,
  6749. NULL,
  6750. LLM_FFN_SILU, LLM_FFN_PAR, il);
  6751. cb(cur, "ffn_out", il);
  6752. }
  6753. cur = ggml_add(ctx0, cur, sa_out);
  6754. cur = ggml_add(ctx0, cur, inpL);
  6755. cur = build_cvec(cur, il);
  6756. cb(cur, "l_out", il);
  6757. // input for next layer
  6758. inpL = cur;
  6759. }
  6760. cur = inpL;
  6761. cur = build_norm(cur,
  6762. model.output_norm, NULL,
  6763. LLM_NORM_RMS, -1);
  6764. cb(cur, "result_norm", -1);
  6765. res->t_embd = cur;
  6766. // lm_head
  6767. cur = build_lora_mm(model.output, cur);
  6768. cb(cur, "result_output", -1);
  6769. res->t_logits = cur;
  6770. ggml_build_forward_expand(gf, cur);
  6771. }
  6772. };
  6773. struct llm_build_gpt2 : public llm_graph_context {
  6774. llm_build_gpt2(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
  6775. const int64_t n_embd_head = hparams.n_embd_head_v;
  6776. const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  6777. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  6778. ggml_tensor * cur;
  6779. ggml_tensor * pos;
  6780. ggml_tensor * inpL;
  6781. inpL = build_inp_embd(model.tok_embd);
  6782. // inp_pos - contains the positions
  6783. ggml_tensor * inp_pos = build_inp_pos();
  6784. auto * inp_attn = build_attn_inp_kv_unified();
  6785. pos = ggml_get_rows(ctx0, model.pos_embd, inp_pos);
  6786. cb(pos, "pos_embd", -1);
  6787. inpL = ggml_add(ctx0, inpL, pos);
  6788. cb(inpL, "inpL", -1);
  6789. ggml_tensor * inp_out_ids = build_inp_out_ids();
  6790. for (int il = 0; il < n_layer; ++il) {
  6791. cur = build_norm(inpL,
  6792. model.layers[il].attn_norm,
  6793. model.layers[il].attn_norm_b,
  6794. LLM_NORM, il);
  6795. cb(cur, "attn_norm", il);
  6796. // self-attention
  6797. {
  6798. cur = build_lora_mm(model.layers[il].wqkv, cur);
  6799. cb(cur, "wqkv", il);
  6800. cur = ggml_add(ctx0, cur, model.layers[il].bqkv);
  6801. cb(cur, "bqkv", il);
  6802. ggml_tensor * Qcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd, n_tokens, cur->nb[1], 0*sizeof(float)*(n_embd)));
  6803. ggml_tensor * Kcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd)));
  6804. ggml_tensor * Vcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd + n_embd_gqa)));
  6805. cb(Qcur, "Qcur", il);
  6806. cb(Kcur, "Kcur", il);
  6807. cb(Vcur, "Vcur", il);
  6808. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  6809. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  6810. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  6811. cur = build_attn(inp_attn, gf,
  6812. model.layers[il].wo, model.layers[il].bo,
  6813. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  6814. }
  6815. if (il == n_layer - 1 && inp_out_ids) {
  6816. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  6817. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  6818. }
  6819. // add the input
  6820. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
  6821. cb(ffn_inp, "ffn_inp", il);
  6822. // FF
  6823. {
  6824. cur = build_norm(ffn_inp,
  6825. model.layers[il].ffn_norm,
  6826. model.layers[il].ffn_norm_b,
  6827. LLM_NORM, il);
  6828. cb(cur, "ffn_norm", il);
  6829. cur = build_ffn(cur,
  6830. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  6831. NULL, NULL, NULL,
  6832. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  6833. NULL,
  6834. LLM_FFN_GELU, LLM_FFN_SEQ, il);
  6835. cb(cur, "ffn_out", il);
  6836. }
  6837. cur = ggml_add(ctx0, cur, ffn_inp);
  6838. cur = build_cvec(cur, il);
  6839. cb(cur, "l_out", il);
  6840. // input for next layer
  6841. inpL = cur;
  6842. }
  6843. cur = build_norm(inpL,
  6844. model.output_norm,
  6845. model.output_norm_b,
  6846. LLM_NORM, -1);
  6847. cb(cur, "result_norm", -1);
  6848. res->t_embd = cur;
  6849. cur = build_lora_mm(model.output, cur);
  6850. cb(cur, "result_output", -1);
  6851. res->t_logits = cur;
  6852. ggml_build_forward_expand(gf, cur);
  6853. }
  6854. };
  6855. struct llm_build_codeshell : public llm_graph_context {
  6856. llm_build_codeshell(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
  6857. const int64_t n_embd_head = hparams.n_embd_head_v;
  6858. const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  6859. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  6860. GGML_ASSERT(n_embd_head == hparams.n_rot);
  6861. ggml_tensor * cur;
  6862. ggml_tensor * inpL;
  6863. inpL = build_inp_embd(model.tok_embd);
  6864. // inp_pos - contains the positions
  6865. ggml_tensor * inp_pos = build_inp_pos();
  6866. auto * inp_attn = build_attn_inp_kv_unified();
  6867. ggml_tensor * inp_out_ids = build_inp_out_ids();
  6868. for (int il = 0; il < n_layer; ++il) {
  6869. cur = build_norm(inpL,
  6870. model.layers[il].attn_norm,
  6871. model.layers[il].attn_norm_b,
  6872. LLM_NORM, il);
  6873. cb(cur, "attn_norm", il);
  6874. // self-attention
  6875. {
  6876. cur = build_lora_mm(model.layers[il].wqkv, cur);
  6877. cb(cur, "wqkv", il);
  6878. cur = ggml_add(ctx0, cur, model.layers[il].bqkv);
  6879. cb(cur, "bqkv", il);
  6880. 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));
  6881. 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));
  6882. ggml_tensor * Vcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd + n_embd_gqa)));
  6883. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  6884. Qcur = ggml_rope_ext(
  6885. ctx0, Qcur, inp_pos, nullptr,
  6886. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  6887. ext_factor, attn_factor, beta_fast, beta_slow
  6888. );
  6889. Kcur = ggml_rope_ext(
  6890. ctx0, Kcur, inp_pos, nullptr,
  6891. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  6892. ext_factor, attn_factor, beta_fast, beta_slow
  6893. );
  6894. cb(Qcur, "Qcur", il);
  6895. cb(Kcur, "Kcur", il);
  6896. cb(Vcur, "Vcur", il);
  6897. cur = build_attn(inp_attn, gf,
  6898. model.layers[il].wo, model.layers[il].bo,
  6899. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  6900. }
  6901. if (il == n_layer - 1 && inp_out_ids) {
  6902. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  6903. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  6904. }
  6905. // add the input
  6906. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
  6907. cb(ffn_inp, "ffn_inp", il);
  6908. // FF
  6909. {
  6910. cur = build_norm(ffn_inp,
  6911. model.layers[il].ffn_norm,
  6912. model.layers[il].ffn_norm_b,
  6913. LLM_NORM, il);
  6914. cb(cur, "ffn_norm", il);
  6915. cur = build_ffn(cur,
  6916. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  6917. NULL, NULL, NULL,
  6918. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  6919. NULL,
  6920. LLM_FFN_GELU, LLM_FFN_SEQ, il);
  6921. cb(cur, "ffn_out", il);
  6922. }
  6923. cur = ggml_add(ctx0, cur, ffn_inp);
  6924. cur = build_cvec(cur, il);
  6925. cb(cur, "l_out", il);
  6926. // input for next layer
  6927. inpL = cur;
  6928. }
  6929. cur = build_norm(inpL,
  6930. model.output_norm,
  6931. model.output_norm_b,
  6932. LLM_NORM, -1);
  6933. cb(cur, "result_norm", -1);
  6934. res->t_embd = cur;
  6935. cur = build_lora_mm(model.output, cur);
  6936. cb(cur, "result_output", -1);
  6937. res->t_logits = cur;
  6938. ggml_build_forward_expand(gf, cur);
  6939. }
  6940. };
  6941. struct llm_build_orion : public llm_graph_context {
  6942. llm_build_orion(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
  6943. const int64_t n_embd_head = hparams.n_embd_head_v;
  6944. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  6945. GGML_ASSERT(n_embd_head == hparams.n_rot);
  6946. ggml_tensor * cur;
  6947. ggml_tensor * inpL;
  6948. inpL = build_inp_embd(model.tok_embd);
  6949. // inp_pos - contains the positions
  6950. ggml_tensor * inp_pos = build_inp_pos();
  6951. auto * inp_attn = build_attn_inp_kv_unified();
  6952. ggml_tensor * inp_out_ids = build_inp_out_ids();
  6953. for (int il = 0; il < n_layer; ++il) {
  6954. ggml_tensor * inpSA = inpL;
  6955. // norm
  6956. cur = build_norm(inpL,
  6957. model.layers[il].attn_norm, model.layers[il].attn_norm_b,
  6958. LLM_NORM, il);
  6959. cb(cur, "attn_norm", il);
  6960. // self-attention
  6961. {
  6962. // compute Q and K and RoPE them
  6963. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  6964. cb(Qcur, "Qcur", il);
  6965. // if (model.layers[il].bq) {
  6966. // Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  6967. // cb(Qcur, "Qcur", il);
  6968. // }
  6969. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  6970. cb(Kcur, "Kcur", il);
  6971. // if (model.layers[il].bk) {
  6972. // Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  6973. // cb(Kcur, "Kcur", il);
  6974. // }
  6975. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  6976. cb(Vcur, "Vcur", il);
  6977. // if (model.layers[il].bv) {
  6978. // Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  6979. // cb(Vcur, "Vcur", il);
  6980. // }
  6981. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  6982. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  6983. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  6984. Qcur = ggml_rope_ext(
  6985. ctx0, Qcur, inp_pos, nullptr,
  6986. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  6987. ext_factor, attn_factor, beta_fast, beta_slow
  6988. );
  6989. Kcur = ggml_rope_ext(
  6990. ctx0, Kcur, inp_pos, nullptr,
  6991. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  6992. ext_factor, attn_factor, beta_fast, beta_slow
  6993. );
  6994. cb(Qcur, "Qcur", il);
  6995. cb(Kcur, "Kcur", il);
  6996. cb(Vcur, "Vcur", il);
  6997. cur = build_attn(inp_attn, gf,
  6998. model.layers[il].wo, NULL,
  6999. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  7000. }
  7001. if (il == n_layer - 1 && inp_out_ids) {
  7002. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  7003. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  7004. }
  7005. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  7006. cb(ffn_inp, "ffn_inp", il);
  7007. // feed-forward network
  7008. cur = build_norm(ffn_inp,
  7009. model.layers[il].ffn_norm, model.layers[il].ffn_norm_b,
  7010. LLM_NORM, il);
  7011. cb(cur, "ffn_norm", il);
  7012. cur = build_ffn(cur,
  7013. model.layers[il].ffn_up, NULL, NULL,
  7014. model.layers[il].ffn_gate, NULL, NULL,
  7015. model.layers[il].ffn_down, NULL, NULL,
  7016. NULL,
  7017. LLM_FFN_SILU, LLM_FFN_PAR, il);
  7018. cb(cur, "ffn_out", il);
  7019. cur = ggml_add(ctx0, cur, ffn_inp);
  7020. cur = build_cvec(cur, il);
  7021. cb(cur, "l_out", il);
  7022. // input for next layer
  7023. inpL = cur;
  7024. }
  7025. cur = inpL;
  7026. cur = build_norm(cur,
  7027. model.output_norm, model.output_norm_b,
  7028. LLM_NORM, -1);
  7029. cb(cur, "result_norm", -1);
  7030. res->t_embd = cur;
  7031. // lm_head
  7032. cur = build_lora_mm(model.output, cur);
  7033. cb(cur, "result_output", -1);
  7034. res->t_logits = cur;
  7035. ggml_build_forward_expand(gf, cur);
  7036. }
  7037. };
  7038. struct llm_build_internlm2 : public llm_graph_context {
  7039. llm_build_internlm2(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
  7040. const int64_t n_embd_head = hparams.n_embd_head_v;
  7041. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  7042. GGML_ASSERT(n_embd_head == hparams.n_rot);
  7043. ggml_tensor * cur;
  7044. ggml_tensor * inpL;
  7045. inpL = build_inp_embd(model.tok_embd);
  7046. // inp_pos - contains the positions
  7047. ggml_tensor * inp_pos = build_inp_pos();
  7048. auto * inp_attn = build_attn_inp_kv_unified();
  7049. ggml_tensor * inp_out_ids = build_inp_out_ids();
  7050. for (int il = 0; il < n_layer; ++il) {
  7051. ggml_tensor * inpSA = inpL;
  7052. // norm
  7053. cur = build_norm(inpL,
  7054. model.layers[il].attn_norm, NULL,
  7055. LLM_NORM_RMS, il);
  7056. cb(cur, "attn_norm", il);
  7057. // self-attention
  7058. {
  7059. // compute Q and K and RoPE them
  7060. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  7061. cb(Qcur, "Qcur", il);
  7062. if (model.layers[il].bq) {
  7063. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  7064. cb(Qcur, "Qcur", il);
  7065. }
  7066. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  7067. cb(Kcur, "Kcur", il);
  7068. if (model.layers[il].bk) {
  7069. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  7070. cb(Kcur, "Kcur", il);
  7071. }
  7072. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  7073. cb(Vcur, "Vcur", il);
  7074. if (model.layers[il].bv) {
  7075. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  7076. cb(Vcur, "Vcur", il);
  7077. }
  7078. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  7079. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  7080. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  7081. Qcur = ggml_rope_ext(
  7082. ctx0, Qcur, inp_pos, nullptr,
  7083. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  7084. ext_factor, attn_factor, beta_fast, beta_slow
  7085. );
  7086. Kcur = ggml_rope_ext(
  7087. ctx0, Kcur, inp_pos, nullptr,
  7088. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  7089. ext_factor, attn_factor, beta_fast, beta_slow
  7090. );
  7091. cb(Qcur, "Qcur", il);
  7092. cb(Kcur, "Kcur", il);
  7093. cb(Vcur, "Vcur", il);
  7094. cur = build_attn(inp_attn, gf,
  7095. model.layers[il].wo, model.layers[il].bo,
  7096. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  7097. }
  7098. if (il == n_layer - 1 && inp_out_ids) {
  7099. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  7100. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  7101. }
  7102. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  7103. cb(ffn_inp, "ffn_inp", il);
  7104. // feed-forward network
  7105. cur = build_norm(ffn_inp,
  7106. model.layers[il].ffn_norm, NULL,
  7107. LLM_NORM_RMS, il);
  7108. cb(cur, "ffn_norm", il);
  7109. cur = build_ffn(cur,
  7110. model.layers[il].ffn_up, NULL, NULL,
  7111. model.layers[il].ffn_gate, NULL, NULL,
  7112. model.layers[il].ffn_down, NULL, NULL,
  7113. NULL,
  7114. LLM_FFN_SILU, LLM_FFN_PAR, il);
  7115. cb(cur, "ffn_out", il);
  7116. cur = ggml_add(ctx0, cur, ffn_inp);
  7117. cur = build_cvec(cur, il);
  7118. cb(cur, "l_out", il);
  7119. // input for next layer
  7120. inpL = cur;
  7121. }
  7122. cur = inpL;
  7123. cur = build_norm(cur,
  7124. model.output_norm, NULL,
  7125. LLM_NORM_RMS, -1);
  7126. cb(cur, "result_norm", -1);
  7127. res->t_embd = cur;
  7128. // lm_head
  7129. cur = build_lora_mm(model.output, cur);
  7130. cb(cur, "result_output", -1);
  7131. res->t_logits = cur;
  7132. ggml_build_forward_expand(gf, cur);
  7133. }
  7134. };
  7135. struct llm_build_minicpm3 : public llm_graph_context {
  7136. llm_build_minicpm3(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
  7137. //TODO: if the model varies, these parameters need to be read from the model
  7138. const int64_t n_embd_base = 256;
  7139. const float scale_embd = 12.0f;
  7140. const float scale_depth = 1.4f;
  7141. const float kq_scale = 1.0f / sqrtf(float(hparams.n_embd_head_k));
  7142. const uint32_t n_embd_head_qk_rope = hparams.n_rot;
  7143. const uint32_t n_embd_head_qk_nope = hparams.n_embd_head_k - hparams.n_rot;
  7144. const uint32_t kv_lora_rank = hparams.n_lora_kv;
  7145. ggml_tensor * cur;
  7146. ggml_tensor * inpL;
  7147. inpL = build_inp_embd(model.tok_embd);
  7148. // scale the input embeddings
  7149. inpL = ggml_scale(ctx0, inpL, scale_embd);
  7150. cb(inpL, "inp_scaled", -1);
  7151. // inp_pos - contains the positions
  7152. ggml_tensor * inp_pos = build_inp_pos();
  7153. auto * inp_attn = build_attn_inp_kv_unified();
  7154. ggml_tensor * inp_out_ids = build_inp_out_ids();
  7155. for (int il = 0; il < n_layer; ++il) {
  7156. ggml_tensor * inpSA = inpL;
  7157. ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
  7158. // norm
  7159. cur = build_norm(inpL,
  7160. model.layers[il].attn_norm, NULL,
  7161. LLM_NORM_RMS, il);
  7162. cb(cur, "attn_norm", il);
  7163. // self_attention
  7164. {
  7165. ggml_tensor * q = NULL;
  7166. // {n_embd, q_lora_rank} * {n_embd, n_tokens} -> {q_lora_rank, n_tokens}
  7167. q = ggml_mul_mat(ctx0, model.layers[il].wq_a, cur);
  7168. cb(q, "q", il);
  7169. q = build_norm(q,
  7170. model.layers[il].attn_q_a_norm, NULL,
  7171. LLM_NORM_RMS, il);
  7172. cb(q, "q", il);
  7173. // {q_lora_rank, n_head * hparams.n_embd_head_k} * {q_lora_rank, n_tokens} -> {n_head * hparams.n_embd_head_k, n_tokens}
  7174. q = ggml_mul_mat(ctx0, model.layers[il].wq_b, q);
  7175. cb(q, "q", il);
  7176. // split into {n_head * n_embd_head_qk_nope, n_tokens}
  7177. ggml_tensor * q_nope = ggml_view_3d(ctx0, q, n_embd_head_qk_nope, n_head, n_tokens,
  7178. ggml_row_size(q->type, hparams.n_embd_head_k),
  7179. ggml_row_size(q->type, hparams.n_embd_head_k * n_head),
  7180. 0);
  7181. cb(q_nope, "q_nope", il);
  7182. // and {n_head * n_embd_head_qk_rope, n_tokens}
  7183. ggml_tensor * q_pe = ggml_view_3d(ctx0, q, n_embd_head_qk_rope, n_head, n_tokens,
  7184. ggml_row_size(q->type, hparams.n_embd_head_k),
  7185. ggml_row_size(q->type, hparams.n_embd_head_k * n_head),
  7186. ggml_row_size(q->type, n_embd_head_qk_nope));
  7187. cb(q_pe, "q_pe", il);
  7188. // {n_embd, kv_lora_rank + n_embd_head_qk_rope} * {n_embd, n_tokens} -> {kv_lora_rank + n_embd_head_qk_rope, n_tokens}
  7189. ggml_tensor * kv_pe_compresseed = ggml_mul_mat(ctx0, model.layers[il].wkv_a_mqa, cur);
  7190. cb(kv_pe_compresseed, "kv_pe_compresseed", il);
  7191. // split into {kv_lora_rank, n_tokens}
  7192. ggml_tensor * kv_compressed = ggml_view_2d(ctx0, kv_pe_compresseed, kv_lora_rank, n_tokens,
  7193. kv_pe_compresseed->nb[1],
  7194. 0);
  7195. cb(kv_compressed, "kv_compressed", il);
  7196. // and {n_embd_head_qk_rope, n_tokens}
  7197. ggml_tensor * k_pe = ggml_view_3d(ctx0, kv_pe_compresseed, n_embd_head_qk_rope, 1, n_tokens,
  7198. kv_pe_compresseed->nb[1],
  7199. kv_pe_compresseed->nb[1],
  7200. ggml_row_size(kv_pe_compresseed->type, kv_lora_rank));
  7201. cb(k_pe, "k_pe", il);
  7202. kv_compressed = build_norm(kv_compressed,
  7203. model.layers[il].attn_kv_a_norm, NULL,
  7204. LLM_NORM_RMS, il);
  7205. cb(kv_compressed, "kv_compressed", il);
  7206. // {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}
  7207. ggml_tensor * kv = ggml_mul_mat(ctx0, model.layers[il].wkv_b, kv_compressed);
  7208. cb(kv, "kv", il);
  7209. // split into {n_head * n_embd_head_qk_nope, n_tokens}
  7210. ggml_tensor * k_nope = ggml_view_3d(ctx0, kv, n_embd_head_qk_nope, n_head, n_tokens,
  7211. ggml_row_size(kv->type, n_embd_head_qk_nope + hparams.n_embd_head_v),
  7212. ggml_row_size(kv->type, n_head * (n_embd_head_qk_nope + hparams.n_embd_head_v)),
  7213. 0);
  7214. cb(k_nope, "k_nope", il);
  7215. // and {n_head * n_embd_head_v, n_tokens}
  7216. ggml_tensor * v_states = ggml_view_3d(ctx0, kv, hparams.n_embd_head_v, n_head, n_tokens,
  7217. ggml_row_size(kv->type, (n_embd_head_qk_nope + hparams.n_embd_head_v)),
  7218. ggml_row_size(kv->type, (n_embd_head_qk_nope + hparams.n_embd_head_v)*n_head),
  7219. ggml_row_size(kv->type, (n_embd_head_qk_nope)));
  7220. cb(v_states, "v_states", il);
  7221. v_states = ggml_cont(ctx0, v_states);
  7222. cb(v_states, "v_states", il);
  7223. q_pe = ggml_rope_ext(
  7224. ctx0, q_pe, inp_pos, rope_factors,
  7225. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  7226. ext_factor, attn_factor, beta_fast, beta_slow
  7227. );
  7228. cb(q_pe, "q_pe", il);
  7229. // shared RoPE key
  7230. k_pe = ggml_rope_ext(
  7231. ctx0, k_pe, inp_pos, rope_factors,
  7232. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  7233. ext_factor, attn_factor, beta_fast, beta_slow
  7234. );
  7235. cb(k_pe, "k_pe", il);
  7236. ggml_tensor * q_states = ggml_concat(ctx0, q_nope, q_pe, 0);
  7237. cb(q_states, "q_states", il);
  7238. ggml_tensor * k_states = ggml_concat(ctx0, k_nope, ggml_repeat(ctx0, k_pe, q_pe), 0);
  7239. cb(k_states, "k_states", il);
  7240. cur = build_attn(inp_attn, gf,
  7241. model.layers[il].wo, NULL,
  7242. q_states, k_states, v_states, nullptr, nullptr, kq_scale, il);
  7243. }
  7244. if (il == n_layer - 1 && inp_out_ids) {
  7245. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  7246. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  7247. }
  7248. // scale_res - scale the hidden states for residual connection
  7249. const float scale_res = scale_depth/sqrtf(float(n_layer)); // TODO: is this correct?
  7250. cur = ggml_scale(ctx0, cur, scale_res);
  7251. cb(cur, "hidden_scaled", il);
  7252. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  7253. cb(ffn_inp, "ffn_inp", il);
  7254. // feed-forward network
  7255. {
  7256. cur = build_norm(ffn_inp,
  7257. model.layers[il].ffn_norm, NULL,
  7258. LLM_NORM_RMS, il);
  7259. cb(cur, "ffn_norm", il);
  7260. cur = build_ffn(cur,
  7261. model.layers[il].ffn_up, NULL, NULL,
  7262. model.layers[il].ffn_gate, NULL, NULL,
  7263. model.layers[il].ffn_down, NULL, NULL,
  7264. NULL,
  7265. LLM_FFN_SILU, LLM_FFN_PAR, il);
  7266. cb(cur, "ffn_out", il);
  7267. }
  7268. // scale the hidden states for residual connection
  7269. cur = ggml_scale(ctx0, cur, scale_res);
  7270. cb(cur, "hidden_scaled_ffn", il);
  7271. cur = ggml_add(ctx0, cur, ffn_inp);
  7272. cur = build_cvec(cur, il);
  7273. cb(cur, "l_out", il);
  7274. // input for next layer
  7275. inpL = cur;
  7276. }
  7277. cur = inpL;
  7278. cur = build_norm(cur,
  7279. model.output_norm, NULL,
  7280. LLM_NORM_RMS, -1);
  7281. cb(cur, "result_norm", -1);
  7282. res->t_embd = cur;
  7283. // lm_head scaling
  7284. const float scale_lmhead = float(n_embd_base)/float(n_embd);
  7285. cur = ggml_scale(ctx0, cur, scale_lmhead);
  7286. cb(cur, "lmhead_scaling", -1);
  7287. // lm_head
  7288. cur = build_lora_mm(model.output, cur);
  7289. cb(cur, "result_output", -1);
  7290. res->t_logits = cur;
  7291. ggml_build_forward_expand(gf, cur);
  7292. }
  7293. };
  7294. struct llm_build_gemma : public llm_graph_context {
  7295. llm_build_gemma(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
  7296. const int64_t n_embd_head = hparams.n_embd_head_v;
  7297. ggml_tensor * cur;
  7298. ggml_tensor * inpL;
  7299. inpL = build_inp_embd(model.tok_embd);
  7300. inpL = ggml_scale(ctx0, inpL, sqrtf(n_embd));
  7301. cb(inpL, "inp_scaled", -1);
  7302. // inp_pos - contains the positions
  7303. ggml_tensor * inp_pos = build_inp_pos();
  7304. auto * inp_attn = build_attn_inp_kv_unified();
  7305. ggml_tensor * inp_out_ids = build_inp_out_ids();
  7306. for (int il = 0; il < n_layer; ++il) {
  7307. // norm
  7308. cur = build_norm(inpL,
  7309. model.layers[il].attn_norm, NULL,
  7310. LLM_NORM_RMS, il);
  7311. cb(cur, "attn_norm", il);
  7312. // self-attention
  7313. {
  7314. // compute Q and K and RoPE them
  7315. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  7316. cb(Qcur, "Qcur", il);
  7317. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  7318. cb(Kcur, "Kcur", il);
  7319. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  7320. cb(Vcur, "Vcur", il);
  7321. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  7322. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  7323. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  7324. Qcur = ggml_rope_ext(
  7325. ctx0, Qcur, inp_pos, nullptr,
  7326. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  7327. ext_factor, attn_factor, beta_fast, beta_slow);
  7328. Kcur = ggml_rope_ext(
  7329. ctx0, Kcur, inp_pos, nullptr,
  7330. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  7331. ext_factor, attn_factor, beta_fast, beta_slow);
  7332. cb(Qcur, "Qcur", il);
  7333. cb(Kcur, "Kcur", il);
  7334. cb(Vcur, "Vcur", il);
  7335. Qcur = ggml_scale(ctx0, Qcur, 1.0f / sqrtf(float(n_embd_head)));
  7336. cb(Qcur, "Qcur_scaled", il);
  7337. cur = build_attn(inp_attn, gf,
  7338. model.layers[il].wo, NULL,
  7339. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f, il);
  7340. }
  7341. if (il == n_layer - 1 && inp_out_ids) {
  7342. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  7343. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  7344. }
  7345. ggml_tensor * sa_out = ggml_add(ctx0, cur, inpL);
  7346. cb(sa_out, "sa_out", il);
  7347. cur = build_norm(sa_out,
  7348. model.layers[il].ffn_norm, NULL,
  7349. LLM_NORM_RMS, il);
  7350. cb(cur, "ffn_norm", il);
  7351. // feed-forward network
  7352. {
  7353. cur = build_ffn(cur,
  7354. model.layers[il].ffn_up, NULL, NULL,
  7355. model.layers[il].ffn_gate, NULL, NULL,
  7356. model.layers[il].ffn_down, NULL, NULL,
  7357. NULL,
  7358. LLM_FFN_GELU, LLM_FFN_PAR, il);
  7359. cb(cur, "ffn_out", il);
  7360. }
  7361. cur = ggml_add(ctx0, cur, sa_out);
  7362. cur = build_cvec(cur, il);
  7363. cb(cur, "l_out", il);
  7364. // input for next layer
  7365. inpL = cur;
  7366. }
  7367. cur = inpL;
  7368. cur = build_norm(cur,
  7369. model.output_norm, NULL,
  7370. LLM_NORM_RMS, -1);
  7371. cb(cur, "result_norm", -1);
  7372. res->t_embd = cur;
  7373. // lm_head
  7374. cur = build_lora_mm(model.output, cur);
  7375. cb(cur, "result_output", -1);
  7376. res->t_logits = cur;
  7377. ggml_build_forward_expand(gf, cur);
  7378. }
  7379. };
  7380. struct llm_build_gemma2_iswa : public llm_graph_context {
  7381. llm_build_gemma2_iswa(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
  7382. const int64_t n_embd_head = hparams.n_embd_head_k;
  7383. ggml_tensor * cur;
  7384. ggml_tensor * inpL;
  7385. inpL = build_inp_embd(model.tok_embd);
  7386. inpL = ggml_scale(ctx0, inpL, sqrtf(n_embd));
  7387. cb(inpL, "inp_scaled", -1);
  7388. // inp_pos - contains the positions
  7389. ggml_tensor * inp_pos = build_inp_pos();
  7390. auto * inp_attn = build_attn_inp_kv_unified_iswa();
  7391. ggml_tensor * inp_out_ids = build_inp_out_ids();
  7392. for (int il = 0; il < n_layer; ++il) {
  7393. // norm
  7394. cur = build_norm(inpL,
  7395. model.layers[il].attn_norm, NULL,
  7396. LLM_NORM_RMS, il);
  7397. cb(cur, "attn_norm", il);
  7398. // self-attention
  7399. {
  7400. // compute Q and K and RoPE them
  7401. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  7402. cb(Qcur, "Qcur", il);
  7403. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  7404. cb(Kcur, "Kcur", il);
  7405. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  7406. cb(Vcur, "Vcur", il);
  7407. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  7408. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  7409. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  7410. Qcur = ggml_rope_ext(
  7411. ctx0, Qcur, inp_pos, nullptr,
  7412. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  7413. ext_factor, attn_factor, beta_fast, beta_slow);
  7414. Kcur = ggml_rope_ext(
  7415. ctx0, Kcur, inp_pos, nullptr,
  7416. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  7417. ext_factor, attn_factor, beta_fast, beta_slow);
  7418. cb(Qcur, "Qcur", il);
  7419. cb(Kcur, "Kcur", il);
  7420. cb(Vcur, "Vcur", il);
  7421. Qcur = ggml_scale(ctx0, Qcur, hparams.f_attention_scale);
  7422. cur = build_attn(inp_attn, gf,
  7423. model.layers[il].wo, NULL,
  7424. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f, il);
  7425. }
  7426. if (il == n_layer - 1 && inp_out_ids) {
  7427. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  7428. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  7429. }
  7430. cur = build_norm(cur,
  7431. model.layers[il].attn_post_norm, NULL,
  7432. LLM_NORM_RMS, il);
  7433. cb(cur, "attn_post_norm", il);
  7434. ggml_tensor * sa_out = ggml_add(ctx0, cur, inpL);
  7435. cb(sa_out, "sa_out", il);
  7436. cur = build_norm(sa_out,
  7437. model.layers[il].ffn_norm, NULL,
  7438. LLM_NORM_RMS, il);
  7439. cb(cur, "ffn_norm", il);
  7440. // feed-forward network
  7441. {
  7442. cur = build_ffn(cur,
  7443. model.layers[il].ffn_up, NULL, NULL,
  7444. model.layers[il].ffn_gate, NULL, NULL,
  7445. model.layers[il].ffn_down, NULL, NULL,
  7446. NULL,
  7447. LLM_FFN_GELU, LLM_FFN_PAR, il);
  7448. cb(cur, "ffn_out", il);
  7449. }
  7450. cur = build_norm(cur,
  7451. model.layers[il].ffn_post_norm, NULL,
  7452. LLM_NORM_RMS, -1);
  7453. cb(cur, "ffn_post_norm", -1);
  7454. cur = ggml_add(ctx0, cur, sa_out);
  7455. cur = build_cvec(cur, il);
  7456. cb(cur, "l_out", il);
  7457. // input for next layer
  7458. inpL = cur;
  7459. }
  7460. cur = inpL;
  7461. cur = build_norm(cur,
  7462. model.output_norm, NULL,
  7463. LLM_NORM_RMS, -1);
  7464. cb(cur, "result_norm", -1);
  7465. res->t_embd = cur;
  7466. // lm_head
  7467. cur = build_lora_mm(model.output, cur);
  7468. // final logit soft-capping
  7469. cur = ggml_scale(ctx0, cur, 1.0f / hparams.f_final_logit_softcapping);
  7470. cur = ggml_tanh(ctx0, cur);
  7471. cur = ggml_scale(ctx0, cur, hparams.f_final_logit_softcapping);
  7472. cb(cur, "result_output", -1);
  7473. res->t_logits = cur;
  7474. ggml_build_forward_expand(gf, cur);
  7475. }
  7476. };
  7477. struct llm_build_gemma3_iswa : public llm_graph_context {
  7478. llm_build_gemma3_iswa(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
  7479. const int64_t n_embd_head = hparams.n_embd_head_k;
  7480. ggml_tensor * cur;
  7481. ggml_tensor * inpL;
  7482. inpL = build_inp_embd(model.tok_embd);
  7483. // important: do not normalize weights for raw embeddings input (i.e. encoded image emdeddings)
  7484. if (ubatch.token) {
  7485. inpL = ggml_scale(ctx0, inpL, sqrtf(n_embd));
  7486. cb(inpL, "inp_scaled", -1);
  7487. }
  7488. // inp_pos - contains the positions
  7489. ggml_tensor * inp_pos = build_inp_pos();
  7490. // TODO: is causal == true correct? might need some changes
  7491. auto * inp_attn = build_attn_inp_kv_unified_iswa();
  7492. ggml_tensor * inp_out_ids = build_inp_out_ids();
  7493. for (int il = 0; il < n_layer; ++il) {
  7494. const float freq_base_l = model.get_rope_freq_base (cparams, il);
  7495. const float freq_scale_l = model.get_rope_freq_scale(cparams, il);
  7496. // norm
  7497. cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
  7498. cb(cur, "attn_norm", il);
  7499. // self-attention
  7500. {
  7501. // compute Q and K and RoPE them
  7502. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  7503. cb(Qcur, "Qcur", il);
  7504. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  7505. cb(Kcur, "Kcur", il);
  7506. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  7507. cb(Vcur, "Vcur", il);
  7508. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  7509. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  7510. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  7511. Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);
  7512. cb(Qcur, "Qcur_normed", il);
  7513. Qcur = ggml_rope_ext(
  7514. ctx0, Qcur, inp_pos, nullptr,
  7515. n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l,
  7516. ext_factor, attn_factor, beta_fast, beta_slow);
  7517. Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);
  7518. cb(Kcur, "Kcur_normed", il);
  7519. Kcur = ggml_rope_ext(
  7520. ctx0, Kcur, inp_pos, nullptr,
  7521. n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l,
  7522. ext_factor, attn_factor, beta_fast, beta_slow);
  7523. cb(Qcur, "Qcur", il);
  7524. cb(Kcur, "Kcur", il);
  7525. cb(Vcur, "Vcur", il);
  7526. // ref: https://github.com/google/gemma_pytorch/blob/014acb7ac4563a5f77c76d7ff98f31b568c16508/gemma/model.py#L315
  7527. Qcur = ggml_scale(ctx0, Qcur, hparams.f_attention_scale);
  7528. cur = build_attn(inp_attn, gf,
  7529. model.layers[il].wo, NULL,
  7530. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f, il);
  7531. }
  7532. if (il == n_layer - 1 && inp_out_ids) {
  7533. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  7534. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  7535. }
  7536. cur = build_norm(cur,
  7537. model.layers[il].attn_post_norm, NULL,
  7538. LLM_NORM_RMS, il);
  7539. cb(cur, "attn_post_norm", il);
  7540. ggml_tensor * sa_out = ggml_add(ctx0, cur, inpL);
  7541. cb(sa_out, "sa_out", il);
  7542. cur = build_norm(sa_out,
  7543. model.layers[il].ffn_norm, NULL,
  7544. LLM_NORM_RMS, il);
  7545. cb(cur, "ffn_norm", il);
  7546. // feed-forward network
  7547. {
  7548. cur = build_ffn(cur,
  7549. model.layers[il].ffn_up, NULL, NULL,
  7550. model.layers[il].ffn_gate, NULL, NULL,
  7551. model.layers[il].ffn_down, NULL, NULL,
  7552. NULL,
  7553. LLM_FFN_GELU, LLM_FFN_PAR, il);
  7554. cb(cur, "ffn_out", il);
  7555. }
  7556. cur = build_norm(cur,
  7557. model.layers[il].ffn_post_norm, NULL,
  7558. LLM_NORM_RMS, -1);
  7559. cb(cur, "ffn_post_norm", -1);
  7560. cur = ggml_add(ctx0, cur, sa_out);
  7561. cur = build_cvec(cur, il);
  7562. cb(cur, "l_out", il);
  7563. // input for next layer
  7564. inpL = cur;
  7565. }
  7566. cur = inpL;
  7567. cur = build_norm(cur,
  7568. model.output_norm, NULL,
  7569. LLM_NORM_RMS, -1);
  7570. cb(cur, "result_norm", -1);
  7571. res->t_embd = cur;
  7572. // lm_head
  7573. cur = build_lora_mm(model.output, cur);
  7574. cb(cur, "result_output", -1);
  7575. res->t_logits = cur;
  7576. ggml_build_forward_expand(gf, cur);
  7577. }
  7578. };
  7579. struct llm_build_gemma3n_iswa : public llm_graph_context {
  7580. const llama_model & model;
  7581. ggml_cgraph * gf;
  7582. const int64_t n_embd_head;
  7583. const int64_t n_embd_altup;
  7584. const int64_t n_altup;
  7585. const int i_altup_act;
  7586. const int n_layer_kv = 20; // number of layers having KV [KV_REUSE]
  7587. const int n_layer_sparsity = 10; // number of layers using activation sparsity
  7588. const float f_sparsity_std_mul = 1.6448533535003662f; // std_multiplier = normal_dist.icdf(0.95)
  7589. llm_build_gemma3n_iswa(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf)
  7590. : llm_graph_context(params),
  7591. model(model),
  7592. gf(gf),
  7593. n_embd_head(model.hparams.n_embd_head_k),
  7594. n_embd_altup(model.hparams.n_embd_altup),
  7595. n_altup(model.hparams.n_altup),
  7596. i_altup_act(model.hparams.i_altup_act) {
  7597. ggml_tensor * cur;
  7598. ggml_tensor * inpL;
  7599. inpL = build_inp_embd(model.tok_embd);
  7600. // important: do not normalize weights for raw embeddings input (i.e. encoded image emdeddings)
  7601. if (ubatch.token) {
  7602. inpL = ggml_scale(ctx0, inpL, sqrtf(n_embd));
  7603. cb(inpL, "inp_scaled", -1);
  7604. }
  7605. // inp_pos - contains the positions
  7606. ggml_tensor * inp_pos = build_inp_pos();
  7607. // TODO: is causal == true correct? might need some changes
  7608. auto * inp_attn = build_attn_inp_kv_unified_iswa();
  7609. // inp_per_layer shape: [n_embd_altup, n_tokens, n_layer]
  7610. ggml_tensor * inp_per_layer = project_per_layer_inputs(inpL, get_per_layer_inputs());
  7611. // inpL now has only 1 altup, project it to the rest of the altups
  7612. // these "added" altups will be concat to the last dim of inpL
  7613. {
  7614. ggml_tensor * target_magnitude = calc_magnitude(inpL);
  7615. ggml_tensor * inp_repeated = ggml_repeat_4d(ctx0, inpL, n_embd, n_tokens, n_altup - 1, 1);
  7616. ggml_tensor * altup_added = ggml_mul_mat(ctx0, model.altup_proj, inp_repeated); // shape: [n_embd, n_tokens, n_altup - 1]
  7617. ggml_tensor * new_magnitude = calc_magnitude(altup_added);
  7618. altup_added = ggml_div(ctx0,
  7619. ggml_mul(ctx0, altup_added, target_magnitude),
  7620. new_magnitude);
  7621. inpL = ggml_concat(ctx0, inpL, altup_added, 2); // shape: [n_embd, n_tokens, n_altup]
  7622. cb(inpL, "inp_stacked", -1);
  7623. }
  7624. // inpL now has shape: [n_embd, n_tokens, n_altup]
  7625. // inp_per_layer now has shape: [n_embd_altup, n_tokens, n_layer]
  7626. for (int il = 0; il < n_layer; ++il) {
  7627. // this block is made to be closely resemble Gemma3p5DecoderLayer on python code
  7628. const bool has_kv = (il < n_layer_kv);
  7629. const float freq_base_l = model.get_rope_freq_base (cparams, il);
  7630. const float freq_scale_l = model.get_rope_freq_scale(cparams, il);
  7631. ggml_tensor * cur = inpL; // [n_embd, n_tokens, n_altup]
  7632. ggml_tensor * predictions = altup_predict(cur, il); // [n_embd, n_tokens, n_altup]
  7633. // predicted value will go through self-attention and laurel
  7634. ggml_tensor * active_prediction = view_2d_slice(predictions, i_altup_act); // [n_embd, n_tokens]
  7635. cur = active_prediction;
  7636. cb(cur, "active_prediction", il);
  7637. // norm
  7638. cur = build_norm(cur, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
  7639. cb(cur, "attn_norm", il);
  7640. // laurel
  7641. ggml_tensor * laurel_out = laurel(cur, il); // [n_embd, n_tokens]
  7642. // self-attention
  7643. if (has_kv) {
  7644. // compute Q and K and RoPE them
  7645. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  7646. cb(Qcur, "Qcur", il);
  7647. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  7648. cb(Kcur, "Kcur", il);
  7649. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  7650. cb(Vcur, "Vcur", il);
  7651. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  7652. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  7653. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  7654. Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);
  7655. Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);
  7656. Vcur = ggml_rms_norm(ctx0, Vcur, hparams.f_norm_rms_eps);
  7657. cb(Qcur, "Qcur_normed", il);
  7658. cb(Kcur, "Kcur_normed", il);
  7659. cb(Vcur, "Vcur_normed", il);
  7660. Qcur = ggml_rope_ext(
  7661. ctx0, Qcur, inp_pos, nullptr,
  7662. n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l,
  7663. ext_factor, attn_factor, beta_fast, beta_slow);
  7664. Kcur = ggml_rope_ext(
  7665. ctx0, Kcur, inp_pos, nullptr,
  7666. n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l,
  7667. ext_factor, attn_factor, beta_fast, beta_slow);
  7668. cb(Qcur, "Qcur_pos", il);
  7669. cb(Kcur, "Kcur_pos", il);
  7670. cur = build_attn(inp_attn, gf,
  7671. model.layers[il].wo, NULL,
  7672. Qcur, Kcur, Vcur, nullptr, nullptr, hparams.f_attention_scale, il);
  7673. } else {
  7674. // no KV layers
  7675. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  7676. cb(Qcur, "Qcur", il);
  7677. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  7678. Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);
  7679. cb(Qcur, "Qcur_normed", il);
  7680. Qcur = ggml_rope_ext(
  7681. ctx0, Qcur, inp_pos, nullptr,
  7682. n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l,
  7683. ext_factor, attn_factor, beta_fast, beta_slow);
  7684. cb(Qcur, "Qcur_pos", il);
  7685. cur = build_attn(inp_attn, gf,
  7686. model.layers[il].wo, NULL,
  7687. Qcur, nullptr, nullptr, nullptr, nullptr, hparams.f_attention_scale, il);
  7688. }
  7689. cur = build_norm(cur,
  7690. model.layers[il].attn_post_norm, NULL,
  7691. LLM_NORM_RMS, il);
  7692. cb(cur, "attn_post_norm", il);
  7693. cur = ggml_add(ctx0, cur, active_prediction); // [n_embd, n_tokens]
  7694. cb(cur, "attn_gated", il);
  7695. ggml_tensor * attn_laurel = ggml_scale(ctx0,
  7696. ggml_add(ctx0, cur, laurel_out),
  7697. 1.0f / sqrtf(2.0f)); // [n_embd, n_tokens]
  7698. cb(attn_laurel, "attn_laurel", il);
  7699. cur = build_norm(attn_laurel,
  7700. model.layers[il].ffn_norm, NULL,
  7701. LLM_NORM_RMS, il);
  7702. cb(cur, "ffn_norm", il);
  7703. // feed-forward network
  7704. {
  7705. ggml_tensor * up_proj = build_lora_mm(model.layers[il].ffn_up, cur);
  7706. ggml_tensor * gate_proj = build_lora_mm(model.layers[il].ffn_gate, cur);
  7707. if (il < n_layer_sparsity) {
  7708. // apply activation sparsity
  7709. gate_proj = gaussian_topk(gate_proj);
  7710. }
  7711. gate_proj = ggml_gelu(ctx0, gate_proj);
  7712. cur = ggml_mul(ctx0, up_proj, gate_proj);
  7713. cur = build_lora_mm(model.layers[il].ffn_down, cur);
  7714. cb(cur, "ffn_out", il);
  7715. }
  7716. cur = build_norm(cur,
  7717. model.layers[il].ffn_post_norm, NULL,
  7718. LLM_NORM_RMS, -1);
  7719. cb(cur, "ffn_post_norm", il);
  7720. ggml_tensor * attn_ffw_laurel_gated = ggml_add(ctx0, cur, attn_laurel); // [n_embd, n_tokens]
  7721. cb(attn_ffw_laurel_gated, "attn_ffw_laurel_gated", il);
  7722. ggml_tensor * corrected = altup_correct(predictions, attn_ffw_laurel_gated, il); // [n_embd, n_tokens, n_altup]
  7723. ggml_tensor * first_prediction; // [n_embd, n_tokens]
  7724. {
  7725. first_prediction = view_2d_slice(corrected, i_altup_act); // [n_embd, n_tokens]
  7726. first_prediction = ggml_mul(ctx0, first_prediction, model.layers[il].altup_correct_scale);
  7727. first_prediction = build_lora_mm(model.layers[il].per_layer_inp_gate, first_prediction);
  7728. first_prediction = ggml_gelu(ctx0, first_prediction); // [n_embd_altup, n_tokens]
  7729. cb(first_prediction, "first_prediction_gated", il);
  7730. ggml_tensor * inp_this_layer = view_2d_slice(inp_per_layer, il); // [n_embd_altup, n_tokens]
  7731. first_prediction = ggml_mul(ctx0, first_prediction, inp_this_layer); // [n_embd_altup, n_tokens]
  7732. cb(first_prediction, "first_prediction_scaled", il);
  7733. first_prediction = build_lora_mm(model.layers[il].per_layer_proj, first_prediction); // [n_embd, n_tokens]
  7734. first_prediction = build_norm(first_prediction,
  7735. model.layers[il].per_layer_post_norm, NULL,
  7736. LLM_NORM_RMS, il);
  7737. cb(first_prediction, "first_prediction_out", il);
  7738. }
  7739. // equivalent to python code: corrected_predictions[1:] += first_prediction
  7740. {
  7741. ggml_tensor * slice_first = view_2d_slice(corrected, 0);
  7742. ggml_tensor * slice_rest = ggml_view_3d(ctx0, corrected, n_embd, n_tokens, n_altup - 1,
  7743. ggml_row_size(corrected->type, n_embd),
  7744. ggml_row_size(corrected->type, n_embd*n_tokens),
  7745. n_embd*n_tokens*ggml_element_size(corrected));
  7746. ggml_tensor * tmp = ggml_add(ctx0, slice_rest, first_prediction); // [n_embd, n_tokens, n_altup - 1]
  7747. corrected = ggml_concat(ctx0, slice_first, tmp, 2); // [n_embd, n_tokens, n_altup]
  7748. }
  7749. cur = corrected; // [n_embd, n_tokens, n_altup]
  7750. cur = build_cvec(cur, il);
  7751. cb(cur, "l_out", il);
  7752. // input for next layer
  7753. inpL = cur;
  7754. }
  7755. cur = inpL; // [n_embd, n_tokens, n_altup]
  7756. // cur now has multiple altup(s), we want to merge them back to 1 altup
  7757. {
  7758. ggml_tensor * target_magnitude = calc_magnitude(view_2d_slice(cur, i_altup_act)); // [n_embd, n_tokens]
  7759. // do a view to skip the first slice (active altup)
  7760. ggml_tensor * alt_slice = ggml_view_3d(ctx0, cur, n_embd, n_tokens, n_altup - 1,
  7761. ggml_row_size(cur->type, n_embd),
  7762. ggml_row_size(cur->type, n_embd*n_tokens),
  7763. n_embd*n_tokens*ggml_element_size(cur));
  7764. ggml_tensor * altup_unembd = ggml_mul_mat(ctx0, model.altup_unembd_proj, alt_slice); // shape: [n_embd, n_tokens, n_altup - 1]
  7765. ggml_tensor * new_magnitude = calc_magnitude(altup_unembd);
  7766. altup_unembd = ggml_div(ctx0,
  7767. ggml_mul(ctx0, altup_unembd, target_magnitude),
  7768. new_magnitude);
  7769. cb(altup_unembd, "altup_unembd", -1);
  7770. // equivalent to torch.mean(hidden_states, dim=0)
  7771. cur = view_2d_slice(cur, 0); // [n_embd, n_tokens]
  7772. for (int i = 0; i < n_altup - 1; ++i) {
  7773. cur = ggml_add(ctx0, cur, view_2d_slice(altup_unembd, i));
  7774. }
  7775. cur = ggml_scale(ctx0, cur, 1.0f / float(n_altup)); // [n_embd, n_tokens]
  7776. cb(cur, "unembd_merged", -1);
  7777. }
  7778. // cur now has shape: [n_embd, n_tokens]
  7779. // TODO: move this to right after the last KV layer
  7780. {
  7781. // skip computing output for unused tokens
  7782. ggml_tensor * inp_out_ids = build_inp_out_ids();
  7783. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  7784. }
  7785. cur = build_norm(cur,
  7786. model.output_norm, NULL,
  7787. LLM_NORM_RMS, -1);
  7788. cb(cur, "result_norm", -1);
  7789. res->t_embd = cur;
  7790. cur = build_lora_mm(model.output, cur);
  7791. {
  7792. // final logit soft-capping
  7793. cur = ggml_scale(ctx0, cur, 1.0f / hparams.f_final_logit_softcapping);
  7794. cur = ggml_tanh(ctx0, cur);
  7795. cur = ggml_scale(ctx0, cur, hparams.f_final_logit_softcapping);
  7796. }
  7797. cb(cur, "result_output", -1);
  7798. res->t_logits = cur;
  7799. ggml_build_forward_expand(gf, cur);
  7800. }
  7801. ggml_tensor * calc_magnitude(ggml_tensor * x) {
  7802. return ggml_sqrt(ctx0, ggml_sum_rows(ctx0, ggml_sqr(ctx0, x)));
  7803. }
  7804. // get 2D slice view from a 3D tensor, the idx corresponds to the 3rd dim
  7805. ggml_tensor * view_2d_slice(ggml_tensor * x, int idx) {
  7806. GGML_ASSERT(idx < (int)x->ne[2]);
  7807. return ggml_view_2d(ctx0, x, x->ne[0], x->ne[1],
  7808. ggml_row_size(x->type, x->ne[0]),
  7809. idx * x->ne[0] * x->ne[1] * ggml_element_size(x));
  7810. }
  7811. // equivalent to get_per_layer_inputs() in python code
  7812. // output shape: [n_embd_altup, n_layer, n_tokens]
  7813. ggml_tensor * get_per_layer_inputs() {
  7814. auto inp = std::make_unique<llm_graph_input_embd>();
  7815. ggml_tensor * inp_per_layer;
  7816. if (ubatch.token) {
  7817. inp->tokens = ggml_new_tensor_1d(ctx0, GGML_TYPE_I32, ubatch.n_tokens);
  7818. ggml_set_input(inp->tokens);
  7819. res->t_tokens = inp->tokens;
  7820. inp_per_layer = ggml_get_rows(ctx0, model.tok_embd_per_layer, inp->tokens);
  7821. inp_per_layer = ggml_reshape_3d(ctx0, inp_per_layer, n_embd_altup, n_layer, n_tokens);
  7822. inp_per_layer = ggml_scale(ctx0, inp_per_layer, sqrtf((float)n_embd_altup));
  7823. cb(inp_per_layer, "inp_per_layer_selected", -1);
  7824. } else {
  7825. GGML_ABORT("TODO: support embd input");
  7826. }
  7827. res->add_input(std::move(inp));
  7828. return inp_per_layer;
  7829. }
  7830. // equivalent to project_per_layer_inputs() in python code
  7831. // this calculates the per-layer inputs, so the final tensor shape will have n_layer as the last dim
  7832. // output shape: [n_embd_altup, n_tokens, n_layer]
  7833. ggml_tensor * project_per_layer_inputs(ggml_tensor * inputs_embeds, ggml_tensor * inp_per_layer) {
  7834. const float per_layer_projection_scale = 1.0f / sqrtf((float)n_embd);
  7835. const float per_layer_input_scale = 1.0f / sqrtf(2.0f);
  7836. ggml_tensor * per_layer_proj = ggml_mul_mat(ctx0, model.per_layer_model_proj, inputs_embeds);
  7837. per_layer_proj = ggml_scale(ctx0, per_layer_proj, per_layer_projection_scale);
  7838. per_layer_proj = ggml_reshape_3d(ctx0, per_layer_proj, n_embd_altup, n_layer, n_tokens);
  7839. per_layer_proj = build_norm(per_layer_proj,
  7840. model.per_layer_proj_norm, NULL,
  7841. LLM_NORM_RMS, -1); // [n_embd_altup, n_layer, n_tokens]
  7842. cb(per_layer_proj, "per_layer_proj", -1);
  7843. inp_per_layer = ggml_add(ctx0, inp_per_layer, per_layer_proj);
  7844. inp_per_layer = ggml_scale(ctx0, inp_per_layer, per_layer_input_scale);
  7845. cb(inp_per_layer, "inp_per_layer", -1);
  7846. // permute to shape: [n_embd_altup, n_tokens, n_layer]
  7847. inp_per_layer = ggml_cont(ctx0, ggml_permute(ctx0, inp_per_layer, 0, 2, 1, 3));
  7848. return inp_per_layer;
  7849. }
  7850. // input cur shape: [n_altup, n_tokens]
  7851. // output shape: [n_altup, n_tokens]
  7852. ggml_tensor * laurel(ggml_tensor * cur, int il) {
  7853. ggml_tensor * tmp = cur;
  7854. tmp = build_lora_mm(model.layers[il].laurel_l, tmp);
  7855. tmp = build_lora_mm(model.layers[il].laurel_r, tmp);
  7856. tmp = build_norm(tmp, model.layers[il].laurel_post_norm, NULL, LLM_NORM_RMS, il);
  7857. tmp = ggml_add(ctx0, tmp, cur);
  7858. cb(tmp, "laurel_out", il);
  7859. return tmp;
  7860. }
  7861. // input x shape: [n_embd, n_tokens]
  7862. // output shape: [n_embd, n_tokens]
  7863. ggml_tensor * gaussian_topk(ggml_tensor * x) {
  7864. ggml_tensor * mean = ggml_mean(ctx0, x);
  7865. ggml_tensor * std = ggml_sqrt(ctx0, ggml_scale(ctx0,
  7866. ggml_sum_rows(ctx0, ggml_sqr(ctx0, ggml_sub(ctx0, x, mean))),
  7867. 1.0f / (float)(x->ne[0] - 1)
  7868. ));
  7869. ggml_tensor * cutoff_x = ggml_add(ctx0, mean, ggml_scale(ctx0, std, f_sparsity_std_mul));
  7870. return ggml_relu(ctx0, ggml_sub(ctx0, x, cutoff_x));
  7871. }
  7872. //
  7873. // altup functions
  7874. //
  7875. // equivalent to compute_router_modalities() in python code
  7876. // input x shape: [n_embd, n_tokens]
  7877. // output shape: [n_altup, n_tokens]
  7878. ggml_tensor * altup_compute_router_modalities(ggml_tensor * x, int il) {
  7879. ggml_tensor * router_inputs = build_norm(x,
  7880. model.layers[il].altup_router_norm, NULL,
  7881. LLM_NORM_RMS, il);
  7882. // router_input_scale
  7883. router_inputs = ggml_scale(ctx0, router_inputs, 1.0f / (float)n_embd);
  7884. ggml_tensor * output = ggml_mul_mat(ctx0, model.layers[il].altup_router, router_inputs);
  7885. return ggml_tanh(ctx0, output); // [n_altup, n_tokens]
  7886. }
  7887. // input cur shape: [n_embd, n_tokens, n_altup]
  7888. // output shape: [n_embd, n_tokens, n_altup]
  7889. ggml_tensor * altup_predict(ggml_tensor * cur, int il) {
  7890. ggml_tensor * activated = view_2d_slice(cur, i_altup_act); // [n_embd, n_tokens]
  7891. ggml_tensor * modalities = altup_compute_router_modalities(activated, il); // [n_altup, n_tokens]
  7892. cb(modalities, "modalities", il);
  7893. ggml_tensor * all_coefs = build_lora_mm(model.layers[il].altup_predict_coef, modalities);
  7894. cb(all_coefs, "all_coefs", il);
  7895. // first dim now having n_altup^2 elements, we reshape it to 2D (so we end up with 3D tensor)
  7896. all_coefs = ggml_reshape_3d(ctx0, all_coefs, n_altup, n_altup, n_tokens);
  7897. // permute to [n_altup, n_embd, n_tokens]
  7898. ggml_tensor * cur_permuted = ggml_cont(ctx0, ggml_permute(ctx0, cur, 1, 2, 0, 3));
  7899. ggml_tensor * predictions = ggml_mul_mat(ctx0, cur_permuted, all_coefs); // [n_altup, n_embd, n_tokens]
  7900. // final shape must be the same as cur: [n_embd, n_tokens, n_altup]
  7901. predictions = ggml_cont(ctx0, ggml_permute(ctx0, predictions, 0, 2, 1, 3));
  7902. predictions = ggml_add(ctx0, predictions, cur);
  7903. cb(predictions, "predictions", il);
  7904. return predictions;
  7905. }
  7906. // input predictions shape: [n_embd, n_tokens, n_altup]
  7907. // input activated shape: [n_embd, n_tokens]
  7908. // output shape: [n_embd, n_tokens, n_altup]
  7909. ggml_tensor * altup_correct(ggml_tensor * predictions, ggml_tensor * activated, int il) {
  7910. ggml_tensor * modalities = altup_compute_router_modalities(activated, il); // [n_altup, n_tokens]
  7911. cb(modalities, "modalities", il);
  7912. ggml_tensor * active_prediction = view_2d_slice(predictions, i_altup_act);
  7913. ggml_tensor * innovation = ggml_sub(ctx0, activated, active_prediction); // [n_embd, n_tokens]
  7914. cb(innovation, "innovation", il);
  7915. ggml_tensor * all_coefs = build_lora_mm(model.layers[il].altup_correct_coef, modalities); // [n_altup, n_tokens]
  7916. all_coefs = ggml_scale_bias(ctx0, all_coefs, 1.0f, 1.0f); // + 1.0
  7917. cb(all_coefs, "all_coefs", il);
  7918. all_coefs = ggml_cont(ctx0, ggml_transpose(ctx0, all_coefs)); // [n_tokens, n_altup]
  7919. all_coefs = ggml_reshape_3d(ctx0, all_coefs, 1, n_tokens, n_altup); // [1, n_tokens, n_altup]
  7920. innovation = ggml_repeat_4d(ctx0, innovation, n_embd, n_tokens, n_altup, 1);
  7921. ggml_tensor * corrected = ggml_mul(ctx0, innovation, all_coefs); // [n_embd, n_tokens, n_altup]
  7922. corrected = ggml_add(ctx0, corrected, predictions); // [n_embd, n_tokens, n_altup]
  7923. cb(corrected, "corrected", il);
  7924. return corrected;
  7925. }
  7926. };
  7927. // TODO: move up next to build_starcoder
  7928. struct llm_build_starcoder2 : public llm_graph_context {
  7929. llm_build_starcoder2(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
  7930. const int64_t n_embd_head = hparams.n_embd_head_v;
  7931. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  7932. GGML_ASSERT(n_embd_head == hparams.n_rot);
  7933. ggml_tensor * cur;
  7934. ggml_tensor * inpL;
  7935. inpL = build_inp_embd(model.tok_embd);
  7936. // inp_pos - contains the positions
  7937. ggml_tensor * inp_pos = build_inp_pos();
  7938. auto * inp_attn = build_attn_inp_kv_unified();
  7939. ggml_tensor * inp_out_ids = build_inp_out_ids();
  7940. for (int il = 0; il < n_layer; ++il) {
  7941. ggml_tensor * inpSA = inpL;
  7942. // norm
  7943. cur = build_norm(inpL,
  7944. model.layers[il].attn_norm, model.layers[il].attn_norm_b,
  7945. LLM_NORM, il);
  7946. cb(cur, "attn_norm", il);
  7947. // self-attention
  7948. {
  7949. // compute Q and K and RoPE them
  7950. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  7951. cb(Qcur, "Qcur", il);
  7952. if (model.layers[il].bq) {
  7953. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  7954. cb(Qcur, "Qcur", il);
  7955. }
  7956. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  7957. cb(Kcur, "Kcur", il);
  7958. if (model.layers[il].bk) {
  7959. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  7960. cb(Kcur, "Kcur", il);
  7961. }
  7962. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  7963. cb(Vcur, "Vcur", il);
  7964. if (model.layers[il].bv) {
  7965. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  7966. cb(Vcur, "Vcur", il);
  7967. }
  7968. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  7969. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  7970. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  7971. Qcur = ggml_rope_ext(
  7972. ctx0, Qcur, inp_pos, nullptr,
  7973. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  7974. ext_factor, attn_factor, beta_fast, beta_slow
  7975. );
  7976. Kcur = ggml_rope_ext(
  7977. ctx0, Kcur, inp_pos, nullptr,
  7978. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  7979. ext_factor, attn_factor, beta_fast, beta_slow
  7980. );
  7981. cb(Qcur, "Qcur", il);
  7982. cb(Kcur, "Kcur", il);
  7983. cb(Vcur, "Vcur", il);
  7984. cur = build_attn(inp_attn, gf,
  7985. model.layers[il].wo, model.layers[il].bo,
  7986. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  7987. }
  7988. if (il == n_layer - 1 && inp_out_ids) {
  7989. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  7990. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  7991. }
  7992. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  7993. cb(ffn_inp, "ffn_inp", il);
  7994. // feed-forward network
  7995. cur = build_norm(ffn_inp,
  7996. model.layers[il].ffn_norm, model.layers[il].ffn_norm_b,
  7997. LLM_NORM, il);
  7998. cb(cur, "ffn_norm", il);
  7999. cur = build_ffn(cur,
  8000. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  8001. NULL, NULL, NULL,
  8002. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  8003. NULL,
  8004. LLM_FFN_GELU, LLM_FFN_SEQ, il);
  8005. cb(cur, "ffn_out", il);
  8006. cur = ggml_add(ctx0, cur, ffn_inp);
  8007. cur = build_cvec(cur, il);
  8008. cb(cur, "l_out", il);
  8009. // input for next layer
  8010. inpL = cur;
  8011. }
  8012. cur = inpL;
  8013. cur = build_norm(cur,
  8014. model.output_norm, model.output_norm_b,
  8015. LLM_NORM, -1);
  8016. cb(cur, "result_norm", -1);
  8017. res->t_embd = cur;
  8018. // lm_head
  8019. cur = build_lora_mm(model.output, cur);
  8020. cb(cur, "result_output", -1);
  8021. res->t_logits = cur;
  8022. ggml_build_forward_expand(gf, cur);
  8023. }
  8024. };
  8025. struct llm_graph_context_mamba : public llm_graph_context {
  8026. llm_graph_context_mamba(const llm_graph_params & params) : llm_graph_context(params) {}
  8027. ggml_tensor * build_mamba_layer(
  8028. llm_graph_input_rs * inp,
  8029. ggml_cgraph * gf,
  8030. ggml_tensor * cur,
  8031. const llama_model & model,
  8032. const llama_ubatch & ubatch,
  8033. int il) {
  8034. const auto * mctx_cur = inp->mctx;
  8035. const auto kv_head = mctx_cur->get_head();
  8036. const auto & layer = model.layers[il];
  8037. const int64_t d_conv = hparams.ssm_d_conv;
  8038. const int64_t d_inner = hparams.ssm_d_inner;
  8039. const int64_t d_state = hparams.ssm_d_state;
  8040. const int64_t dt_rank = hparams.ssm_dt_rank;
  8041. const int64_t n_head = d_inner;
  8042. const int64_t head_dim = 1;
  8043. const int64_t n_seqs = ubatch.n_seqs;
  8044. // Some variants of Mamba arch (e.g. FalconMamba do apply layer norm on B and Dt layers)
  8045. const bool ssm_dt_b_c_rms = hparams.ssm_dt_b_c_rms;
  8046. const int64_t n_seq_tokens = ubatch.n_seq_tokens;
  8047. GGML_ASSERT(n_seqs != 0);
  8048. GGML_ASSERT(ubatch.equal_seqs);
  8049. GGML_ASSERT(ubatch.n_tokens == n_seq_tokens * n_seqs);
  8050. ggml_tensor * conv_states_all = mctx_cur->get_r_l(il);
  8051. ggml_tensor * ssm_states_all = mctx_cur->get_s_l(il);
  8052. ggml_tensor * conv = build_rs(inp, gf, conv_states_all, hparams.n_embd_r(), n_seqs);
  8053. conv = ggml_reshape_3d(ctx0, conv, d_conv - 1, d_inner, n_seqs);
  8054. // {n_embd, n_tokens} => {n_embd, n_seq_tokens, n_seqs}
  8055. cur = ggml_reshape_3d(ctx0, cur, cur->ne[0], n_seq_tokens, n_seqs);
  8056. // {n_embd, 2*d_inner} @ {n_embd, n_seq_tokens, n_seqs} => {2*d_inner, n_seq_tokens, n_seqs}
  8057. ggml_tensor * xz = build_lora_mm(layer.ssm_in, cur);
  8058. // split the above in two
  8059. // => {d_inner, n_seq_tokens, n_seqs}
  8060. ggml_tensor * x = ggml_view_3d(ctx0, xz, d_inner, xz->ne[1], xz->ne[2], xz->nb[1], xz->nb[2], 0);
  8061. 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));
  8062. // conv
  8063. {
  8064. // => {d_conv - 1 + n_seq_tokens, d_inner, n_seqs}
  8065. ggml_tensor * conv_x = ggml_concat(ctx0, conv, ggml_transpose(ctx0, x), 0);
  8066. // copy last (d_conv - 1) columns back into the state cache
  8067. 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]));
  8068. ggml_build_forward_expand(gf,
  8069. ggml_cpy(ctx0, last_conv,
  8070. ggml_view_1d(ctx0, conv_states_all,
  8071. (d_conv - 1)*(d_inner)*(n_seqs),
  8072. kv_head*(d_conv - 1)*(d_inner)*ggml_element_size(conv_states_all))));
  8073. // 1D convolution
  8074. // The equivalent is to make a self-overlapping view of conv_x
  8075. // over d_conv columns at each stride in the 3rd dimension,
  8076. // then element-wise multiply that with the conv1d weight,
  8077. // then sum the elements of each row,
  8078. // (the last two steps are a dot product over rows (also doable with mul_mat))
  8079. // then permute away the ne[0] dimension,
  8080. // and then you're left with the resulting x tensor.
  8081. // For simultaneous sequences, all sequences need to have the same length.
  8082. x = ggml_ssm_conv(ctx0, conv_x, layer.ssm_conv1d);
  8083. // bias
  8084. x = ggml_add(ctx0, x, layer.ssm_conv1d_b);
  8085. x = ggml_silu(ctx0, x);
  8086. }
  8087. // ssm
  8088. {
  8089. // {d_inner, dt_rank + 2*d_state} @ {d_inner, n_seq_tokens, n_seqs} => {dt_rank + 2*d_state, n_seq_tokens, n_seqs}
  8090. ggml_tensor * x_db = build_lora_mm(layer.ssm_x, x);
  8091. // split
  8092. 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);
  8093. 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);
  8094. 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));
  8095. // Some Mamba variants (e.g. FalconMamba, Jamba) apply RMS norm in B, C & Dt layers
  8096. if (ssm_dt_b_c_rms || (layer.ssm_dt_norm && layer.ssm_b_norm && layer.ssm_c_norm)) {
  8097. dt = build_norm(dt, layer.ssm_dt_norm, NULL, LLM_NORM_RMS, il);
  8098. B = build_norm(B, layer.ssm_b_norm, NULL, LLM_NORM_RMS, il);
  8099. C = build_norm(C, layer.ssm_c_norm, NULL, LLM_NORM_RMS, il);
  8100. }
  8101. // {dt_rank, d_inner} @ {dt_rank, n_seq_tokens, n_seqs} => {d_inner, n_seq_tokens, n_seqs}
  8102. dt = build_lora_mm(layer.ssm_dt, dt);
  8103. dt = ggml_add(ctx0, dt, layer.ssm_dt_b);
  8104. cur = x;
  8105. x = ggml_reshape_4d(ctx0, x, head_dim, n_head, n_seq_tokens, n_seqs);
  8106. ggml_tensor * A = layer.ssm_a;
  8107. // use the states and the indices provided by build_recurrent_state
  8108. // (this is necessary in order to properly use the states before they are overwritten,
  8109. // while avoiding to make unnecessary copies of the states)
  8110. auto get_ssm_rows = [&](ggml_context * ctx, ggml_tensor * states, ggml_tensor * ids) {
  8111. ggml_tensor * ssm = ggml_reshape_4d(ctx, states, d_state, head_dim, n_head, mctx_cur->get_size());
  8112. // Custom operator to optimize the parallel associative scan
  8113. // as described in the Annex D of the Mamba paper.
  8114. // => {d_inner, n_seq_tokens, n_seqs} and {d_state, d_inner, n_seqs}
  8115. return ggml_ssm_scan(ctx, ssm, x, dt, A, B, C, ids);
  8116. };
  8117. ggml_tensor * y_ssm = build_rs(inp, gf, ssm_states_all, hparams.n_embd_s(), ubatch.n_seqs, get_ssm_rows);
  8118. // store last states
  8119. ggml_build_forward_expand(gf,
  8120. ggml_cpy(ctx0,
  8121. ggml_view_1d(ctx0, y_ssm, d_state*d_inner*n_seqs, x->nb[3]*x->ne[3]),
  8122. 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))));
  8123. ggml_tensor * y = ggml_view_3d(ctx0, y_ssm, d_inner, n_seq_tokens, n_seqs, x->nb[2], x->nb[3], 0);
  8124. // TODO: skip computing output earlier for unused tokens
  8125. y = ggml_add(ctx0, y, ggml_mul(ctx0, cur, layer.ssm_d));
  8126. y = ggml_swiglu_split(ctx0, ggml_cont(ctx0, z), y);
  8127. // {d_inner, n_embd} @ {d_inner, n_seq_tokens, n_seqs} => {n_embd, n_seq_tokens, n_seqs}
  8128. cur = build_lora_mm(layer.ssm_out, y);
  8129. }
  8130. // {n_embd, n_seq_tokens, n_seqs} => {n_embd, n_tokens}
  8131. cur = ggml_reshape_2d(ctx0, cur, cur->ne[0], n_seq_tokens * n_seqs);
  8132. return cur;
  8133. }
  8134. ggml_tensor * build_mamba2_layer(
  8135. llm_graph_input_rs * inp,
  8136. ggml_cgraph * gf,
  8137. ggml_tensor * cur,
  8138. const llama_model & model,
  8139. const llama_ubatch & ubatch,
  8140. int il) const {
  8141. const auto * mctx_cur = inp->mctx;
  8142. const auto kv_head = mctx_cur->get_head();
  8143. const int64_t d_conv = hparams.ssm_d_conv;
  8144. const int64_t d_inner = hparams.ssm_d_inner;
  8145. const int64_t d_state = hparams.ssm_d_state;
  8146. const int64_t n_head = hparams.ssm_dt_rank;
  8147. const int64_t head_dim = d_inner / n_head;
  8148. const int64_t n_group = hparams.ssm_n_group;
  8149. const int64_t n_seqs = ubatch.n_seqs;
  8150. const int64_t n_seq_tokens = ubatch.n_seq_tokens;
  8151. GGML_ASSERT(n_seqs != 0);
  8152. GGML_ASSERT(ubatch.equal_seqs);
  8153. GGML_ASSERT(ubatch.n_tokens == n_seq_tokens * n_seqs);
  8154. ggml_tensor * conv_states_all = mctx_cur->get_r_l(il);
  8155. ggml_tensor * ssm_states_all = mctx_cur->get_s_l(il);
  8156. ggml_tensor * conv = build_rs(inp, gf, conv_states_all, hparams.n_embd_r(), n_seqs);
  8157. conv = ggml_reshape_3d(ctx0, conv, d_conv - 1, d_inner + 2*n_group*d_state, n_seqs);
  8158. // {n_embd, n_tokens} => {n_embd, n_seq_tokens, n_seqs}
  8159. cur = ggml_reshape_3d(ctx0, cur, cur->ne[0], n_seq_tokens, n_seqs);
  8160. // d_in_proj = 2 * self.d_inner + 2 * self.ngroups * self.d_state + self.nheads
  8161. // {n_embd, d_in_proj} @ {n_embd, n_seq_tokens, n_seqs} => {d_in_proj, n_seq_tokens, n_seqs}
  8162. ggml_tensor * zxBCdt = build_lora_mm(model.layers[il].ssm_in, cur);
  8163. // split the above in three
  8164. 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);
  8165. 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));
  8166. 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));
  8167. // conv
  8168. {
  8169. // => {d_conv - 1 + n_seq_tokens, d_inner + 2*n_group*d_state, n_seqs}
  8170. ggml_tensor * conv_x = ggml_concat(ctx0, conv, ggml_transpose(ctx0, xBC), 0);
  8171. // copy last (d_conv - 1) columns back into the state cache
  8172. 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]));
  8173. ggml_build_forward_expand(gf,
  8174. ggml_cpy(ctx0, last_conv,
  8175. ggml_view_1d(ctx0, conv_states_all,
  8176. (d_conv - 1)*(d_inner + 2*n_group*d_state)*(n_seqs),
  8177. kv_head*(d_conv - 1)*(d_inner + 2*n_group*d_state)*ggml_element_size(conv_states_all))));
  8178. // 1D convolution
  8179. // The equivalent is to make a self-overlapping view of conv_x
  8180. // over d_conv columns at each stride in the 3rd dimension,
  8181. // then element-wise multiply that with the conv1d weight,
  8182. // then sum the elements of each row,
  8183. // (the last two steps are a dot product over rows (also doable with mul_mat))
  8184. // then permute away the ne[0] dimension,
  8185. // and then you're left with the resulting x tensor.
  8186. // For simultaneous sequences, all sequences need to have the same length.
  8187. xBC = ggml_ssm_conv(ctx0, conv_x, model.layers[il].ssm_conv1d);
  8188. // bias
  8189. xBC = ggml_add(ctx0, xBC, model.layers[il].ssm_conv1d_b);
  8190. xBC = ggml_silu(ctx0, xBC);
  8191. }
  8192. // ssm
  8193. {
  8194. // These correspond to V K Q in SSM/attention duality
  8195. 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);
  8196. 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));
  8197. 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));
  8198. // {n_head, n_seq_tokens, n_seqs}
  8199. dt = ggml_add(ctx0, ggml_cont(ctx0, dt), model.layers[il].ssm_dt_b);
  8200. ggml_tensor * A = model.layers[il].ssm_a;
  8201. // use the states and the indices provided by build_recurrent_state
  8202. // (this is necessary in order to properly use the states before they are overwritten,
  8203. // while avoiding to make unnecessary copies of the states)
  8204. auto get_ssm_rows = [&](ggml_context * ctx, ggml_tensor * states, ggml_tensor * ids) {
  8205. ggml_tensor * ssm = ggml_reshape_4d(ctx, states, d_state, head_dim, n_head, mctx_cur->get_size());
  8206. // TODO: use semistructured matrices to implement state-space duality
  8207. // => {d_inner, n_seq_tokens, n_seqs} and {d_state, d_inner, n_seqs}
  8208. return ggml_ssm_scan(ctx, ssm, x, dt, A, B, C, ids);
  8209. };
  8210. ggml_tensor * y_ssm = build_rs(inp, gf, ssm_states_all, hparams.n_embd_s(), ubatch.n_seqs, get_ssm_rows);
  8211. // store last states
  8212. ggml_build_forward_expand(gf,
  8213. ggml_cpy(ctx0,
  8214. ggml_view_1d(ctx0, y_ssm, d_state*d_inner*n_seqs, ggml_nelements(x)*x->nb[0]),
  8215. 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))));
  8216. 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);
  8217. // TODO: skip computing output earlier for unused tokens
  8218. y = ggml_add(ctx0, y, ggml_mul(ctx0, x, model.layers[il].ssm_d));
  8219. y = ggml_swiglu_split(ctx0, ggml_cont(ctx0, z), y);
  8220. // grouped RMS norm
  8221. if (model.layers[il].ssm_norm) {
  8222. y = ggml_reshape_4d(ctx0, y, d_inner / n_group, n_group, n_seq_tokens, n_seqs);
  8223. y = build_norm(y, model.layers[il].ssm_norm, NULL, LLM_NORM_RMS, il);
  8224. }
  8225. y = ggml_reshape_3d(ctx0, y, d_inner, n_seq_tokens, n_seqs);
  8226. // {d_inner, n_embd} @ {d_inner, n_seq_tokens, n_seqs} => {n_embd, n_seq_tokens, n_seqs}
  8227. cur = build_lora_mm(model.layers[il].ssm_out, y);
  8228. }
  8229. // {n_embd, n_seq_tokens, n_seqs} => {n_embd, n_tokens}
  8230. cur = ggml_reshape_2d(ctx0, cur, cur->ne[0], n_seq_tokens * n_seqs);
  8231. cb(cur, "mamba_out", il);
  8232. return cur;
  8233. }
  8234. };
  8235. struct llm_build_mamba : public llm_graph_context_mamba {
  8236. llm_build_mamba(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context_mamba(params) {
  8237. ggml_tensor * cur;
  8238. ggml_tensor * inpL;
  8239. // {n_embd, n_tokens}
  8240. inpL = build_inp_embd(model.tok_embd);
  8241. auto * rs_inp = build_rs_inp();
  8242. ggml_tensor * inp_out_ids = build_inp_out_ids();
  8243. for (int il = 0; il < n_layer; ++il) {
  8244. // norm
  8245. cur = build_norm(inpL,
  8246. model.layers[il].attn_norm, NULL,
  8247. LLM_NORM_RMS, il);
  8248. cb(cur, "attn_norm", il);
  8249. if (model.arch == LLM_ARCH_MAMBA2) {
  8250. cur = build_mamba2_layer(rs_inp, gf, cur, model, ubatch, il);
  8251. } else {
  8252. cur = build_mamba_layer(rs_inp, gf, cur, model, ubatch, il);
  8253. }
  8254. if (il == n_layer - 1 && inp_out_ids) {
  8255. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  8256. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  8257. }
  8258. // residual
  8259. cur = ggml_add(ctx0, cur, inpL);
  8260. cur = build_cvec(cur, il);
  8261. cb(cur, "l_out", il);
  8262. // input for next layer
  8263. inpL = cur;
  8264. }
  8265. // final rmsnorm
  8266. cur = build_norm(inpL, model.output_norm, NULL, LLM_NORM_RMS, -1);
  8267. cb(cur, "result_norm", -1);
  8268. res->t_embd = cur;
  8269. // lm_head
  8270. cur = build_lora_mm(model.output, cur);
  8271. cb(cur, "result_output", -1);
  8272. res->t_logits = cur;
  8273. ggml_build_forward_expand(gf, cur);
  8274. }
  8275. };
  8276. struct llm_build_jamba : public llm_graph_context_mamba {
  8277. llm_build_jamba(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context_mamba(params) {
  8278. const int64_t n_embd_head = hparams.n_embd_head_v;
  8279. ggml_tensor * cur;
  8280. ggml_tensor * inpL;
  8281. // {n_embd, n_tokens}
  8282. inpL = build_inp_embd(model.tok_embd);
  8283. auto * inp_hybrid = build_inp_mem_hybrid();
  8284. ggml_tensor * inp_out_ids = build_inp_out_ids();
  8285. for (int il = 0; il < n_layer; ++il) {
  8286. const int64_t n_head_kv = hparams.n_head_kv(il);
  8287. cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
  8288. cb(cur, "attn_norm", il);
  8289. if (n_head_kv == 0) {
  8290. cur = build_mamba_layer(inp_hybrid->get_recr(), gf, cur, model, ubatch, il);
  8291. } else {
  8292. // Attention
  8293. struct ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  8294. struct ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  8295. struct ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  8296. cb(Qcur, "Qcur", il);
  8297. cb(Kcur, "Kcur", il);
  8298. cb(Vcur, "Vcur", il);
  8299. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  8300. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  8301. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  8302. cb(Qcur, "Qcur", il);
  8303. cb(Kcur, "Kcur", il);
  8304. cb(Vcur, "Vcur", il);
  8305. // No RoPE :)
  8306. cur = build_attn(inp_hybrid->get_attn(), gf, model.layers[il].wo, NULL, Qcur, Kcur, Vcur, NULL, NULL, 1.0f/sqrtf(float(n_embd_head)), il);
  8307. }
  8308. if (il == n_layer - 1 && inp_out_ids) {
  8309. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  8310. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  8311. }
  8312. // residual
  8313. struct ggml_tensor * ffn_inp = ggml_add(ctx0, inpL, cur);
  8314. cb(cur, "ffn_inp", il);
  8315. cur = build_norm(ffn_inp, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);
  8316. cb(cur, "ffn_norm", il);
  8317. // feed-forward network
  8318. if (model.layers[il].ffn_gate_inp == nullptr) {
  8319. // FFN
  8320. cur = build_ffn(cur,
  8321. model.layers[il].ffn_up, NULL, NULL,
  8322. model.layers[il].ffn_gate, NULL, NULL,
  8323. model.layers[il].ffn_down, NULL, NULL,
  8324. NULL,
  8325. LLM_FFN_SILU, LLM_FFN_PAR, il);
  8326. cb(cur, "ffn_out", il);
  8327. } else {
  8328. // MoE branch
  8329. cur = build_moe_ffn(cur,
  8330. model.layers[il].ffn_gate_inp,
  8331. model.layers[il].ffn_up_exps,
  8332. model.layers[il].ffn_gate_exps,
  8333. model.layers[il].ffn_down_exps,
  8334. nullptr,
  8335. n_expert, n_expert_used,
  8336. LLM_FFN_SILU, false,
  8337. false, 0.0,
  8338. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  8339. il);
  8340. cb(cur, "ffn_moe_out", il);
  8341. }
  8342. // residual
  8343. cur = ggml_add(ctx0, ffn_inp, cur);
  8344. cur = build_cvec(cur, il);
  8345. cb(cur, "l_out", il);
  8346. // input for next layer
  8347. inpL = cur;
  8348. }
  8349. // final rmsnorm
  8350. cur = build_norm(inpL, model.output_norm, NULL, LLM_NORM_RMS, -1);
  8351. cb(cur, "result_norm", -1);
  8352. res->t_embd = cur;
  8353. // lm_head
  8354. cur = build_lora_mm(model.output, cur);
  8355. cb(cur, "result_output", -1);
  8356. res->t_logits = cur;
  8357. ggml_build_forward_expand(gf, cur);
  8358. }
  8359. };
  8360. struct llm_build_command_r : public llm_graph_context {
  8361. llm_build_command_r(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
  8362. const int64_t n_embd_head = hparams.n_embd_head_v;
  8363. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  8364. const float f_logit_scale = hparams.f_logit_scale;
  8365. ggml_tensor * cur;
  8366. ggml_tensor * inpL;
  8367. inpL = build_inp_embd(model.tok_embd);
  8368. // inp_pos - contains the positions
  8369. ggml_tensor * inp_pos = build_inp_pos();
  8370. auto * inp_attn = build_attn_inp_kv_unified();
  8371. ggml_tensor * inp_out_ids = build_inp_out_ids();
  8372. for (int il = 0; il < n_layer; ++il) {
  8373. // norm
  8374. cur = build_norm(inpL,
  8375. model.layers[il].attn_norm, NULL,
  8376. LLM_NORM, il);
  8377. cb(cur, "attn_norm", il);
  8378. ggml_tensor * ffn_inp = cur;
  8379. // self-attention
  8380. {
  8381. // compute Q and K and RoPE them
  8382. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  8383. cb(Qcur, "Qcur", il);
  8384. if (model.layers[il].bq) {
  8385. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  8386. cb(Qcur, "Qcur", il);
  8387. }
  8388. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  8389. cb(Kcur, "Kcur", il);
  8390. if (model.layers[il].bk) {
  8391. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  8392. cb(Kcur, "Kcur", il);
  8393. }
  8394. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  8395. cb(Vcur, "Vcur", il);
  8396. if (model.layers[il].bv) {
  8397. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  8398. cb(Vcur, "Vcur", il);
  8399. }
  8400. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  8401. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  8402. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  8403. if (model.layers[il].attn_q_norm) {
  8404. Qcur = build_norm(Qcur,
  8405. model.layers[il].attn_q_norm,
  8406. NULL,
  8407. LLM_NORM, il);
  8408. cb(Qcur, "Qcur", il);
  8409. }
  8410. Qcur = ggml_rope_ext(
  8411. ctx0, Qcur, inp_pos, nullptr,
  8412. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  8413. ext_factor, attn_factor, beta_fast, beta_slow
  8414. );
  8415. if (model.layers[il].attn_k_norm) {
  8416. Kcur = build_norm(Kcur,
  8417. model.layers[il].attn_k_norm,
  8418. NULL,
  8419. LLM_NORM, il);
  8420. cb(Kcur, "Kcur", il);
  8421. }
  8422. Kcur = ggml_rope_ext(
  8423. ctx0, Kcur, inp_pos, nullptr,
  8424. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  8425. ext_factor, attn_factor, beta_fast, beta_slow
  8426. );
  8427. cb(Qcur, "Qcur", il);
  8428. cb(Kcur, "Kcur", il);
  8429. cb(Vcur, "Vcur", il);
  8430. cur = build_attn(inp_attn, gf,
  8431. model.layers[il].wo, model.layers[il].bo,
  8432. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  8433. }
  8434. if (il == n_layer - 1 && inp_out_ids) {
  8435. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  8436. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  8437. ffn_inp = ggml_get_rows(ctx0, ffn_inp, inp_out_ids);
  8438. }
  8439. ggml_tensor * attn_out = cur;
  8440. // feed-forward network
  8441. {
  8442. cur = build_ffn(ffn_inp,
  8443. model.layers[il].ffn_up, NULL, NULL,
  8444. model.layers[il].ffn_gate, NULL, NULL,
  8445. model.layers[il].ffn_down, NULL, NULL,
  8446. NULL,
  8447. LLM_FFN_SILU, LLM_FFN_PAR, il);
  8448. cb(cur, "ffn_out", il);
  8449. }
  8450. // add together residual + FFN + self-attention
  8451. cur = ggml_add(ctx0, cur, inpL);
  8452. cur = ggml_add(ctx0, cur, attn_out);
  8453. cur = build_cvec(cur, il);
  8454. cb(cur, "l_out", il);
  8455. // input for next layer
  8456. inpL = cur;
  8457. }
  8458. cur = inpL;
  8459. cur = build_norm(cur,
  8460. model.output_norm, NULL,
  8461. LLM_NORM, -1);
  8462. cb(cur, "result_norm", -1);
  8463. res->t_embd = cur;
  8464. // lm_head
  8465. cur = build_lora_mm(model.output, cur);
  8466. if (f_logit_scale) {
  8467. cur = ggml_scale(ctx0, cur, f_logit_scale);
  8468. }
  8469. cb(cur, "result_output", -1);
  8470. res->t_logits = cur;
  8471. ggml_build_forward_expand(gf, cur);
  8472. }
  8473. };
  8474. struct llm_build_cohere2_iswa : public llm_graph_context {
  8475. llm_build_cohere2_iswa(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
  8476. const int64_t n_embd_head = hparams.n_embd_head_v;
  8477. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  8478. const float f_logit_scale = hparams.f_logit_scale;
  8479. ggml_tensor * cur;
  8480. ggml_tensor * inpL;
  8481. inpL = build_inp_embd(model.tok_embd);
  8482. // inp_pos - contains the positions
  8483. ggml_tensor * inp_pos = build_inp_pos();
  8484. auto * inp_attn = build_attn_inp_kv_unified_iswa();
  8485. ggml_tensor * inp_out_ids = build_inp_out_ids();
  8486. for (int il = 0; il < n_layer; ++il) {
  8487. const bool is_swa = hparams.is_swa(il);
  8488. // norm
  8489. cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM, il);
  8490. cb(cur, "attn_norm", il);
  8491. ggml_tensor * ffn_inp = cur;
  8492. // self-attention
  8493. {
  8494. // rope freq factors for 128k context
  8495. ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
  8496. // compute Q and K and RoPE them
  8497. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  8498. cb(Qcur, "Qcur", il);
  8499. if (model.layers[il].bq) {
  8500. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  8501. cb(Qcur, "Qcur", il);
  8502. }
  8503. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  8504. cb(Kcur, "Kcur", il);
  8505. if (model.layers[il].bk) {
  8506. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  8507. cb(Kcur, "Kcur", il);
  8508. }
  8509. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  8510. cb(Vcur, "Vcur", il);
  8511. if (model.layers[il].bv) {
  8512. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  8513. cb(Vcur, "Vcur", il);
  8514. }
  8515. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  8516. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  8517. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  8518. if (is_swa) {
  8519. Qcur = ggml_rope_ext(
  8520. ctx0, Qcur, inp_pos, rope_factors,
  8521. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  8522. ext_factor, attn_factor, beta_fast, beta_slow
  8523. );
  8524. Kcur = ggml_rope_ext(
  8525. ctx0, Kcur, inp_pos, rope_factors,
  8526. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  8527. ext_factor, attn_factor, beta_fast, beta_slow
  8528. );
  8529. }
  8530. cb(Qcur, "Qcur", il);
  8531. cb(Kcur, "Kcur", il);
  8532. cb(Vcur, "Vcur", il);
  8533. cur = build_attn(inp_attn, gf,
  8534. model.layers[il].wo, model.layers[il].bo,
  8535. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  8536. }
  8537. if (il == n_layer - 1 && inp_out_ids) {
  8538. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  8539. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  8540. ffn_inp = ggml_get_rows(ctx0, ffn_inp, inp_out_ids);
  8541. }
  8542. ggml_tensor * attn_out = cur;
  8543. // feed-forward network
  8544. {
  8545. cur = build_ffn(ffn_inp, model.layers[il].ffn_up, NULL, NULL, model.layers[il].ffn_gate,
  8546. NULL, NULL, model.layers[il].ffn_down, NULL, NULL, NULL, LLM_FFN_SILU, LLM_FFN_PAR,
  8547. il);
  8548. cb(cur, "ffn_out", il);
  8549. }
  8550. // add together residual + FFN + self-attention
  8551. cur = ggml_add(ctx0, cur, inpL);
  8552. cur = ggml_add(ctx0, cur, attn_out);
  8553. cur = build_cvec(cur, il);
  8554. cb(cur, "l_out", il);
  8555. // input for next layer
  8556. inpL = cur;
  8557. }
  8558. cur = inpL;
  8559. cur = build_norm(cur, model.output_norm, NULL, LLM_NORM, -1);
  8560. cb(cur, "result_norm", -1);
  8561. res->t_embd = cur;
  8562. // lm_head
  8563. cur = build_lora_mm(model.output, cur);
  8564. if (f_logit_scale) {
  8565. cur = ggml_scale(ctx0, cur, f_logit_scale);
  8566. }
  8567. cb(cur, "result_output", -1);
  8568. res->t_logits = cur;
  8569. ggml_build_forward_expand(gf, cur);
  8570. }
  8571. };
  8572. // ref: https://allenai.org/olmo
  8573. // based on the original build_llama() function, changes:
  8574. // * non-parametric layer norm
  8575. // * clamp qkv
  8576. // * removed bias
  8577. // * removed MoE
  8578. struct llm_build_olmo : public llm_graph_context {
  8579. llm_build_olmo(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
  8580. const int64_t n_embd_head = hparams.n_embd_head_v;
  8581. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  8582. GGML_ASSERT(n_embd_head == hparams.n_rot);
  8583. ggml_tensor * cur;
  8584. ggml_tensor * inpL;
  8585. inpL = build_inp_embd(model.tok_embd);
  8586. // inp_pos - contains the positions
  8587. ggml_tensor * inp_pos = build_inp_pos();
  8588. auto * inp_attn = build_attn_inp_kv_unified();
  8589. ggml_tensor * inp_out_ids = build_inp_out_ids();
  8590. for (int il = 0; il < n_layer; ++il) {
  8591. ggml_tensor * inpSA = inpL;
  8592. // norm
  8593. cur = build_norm(inpL,
  8594. NULL, NULL,
  8595. LLM_NORM, il);
  8596. cb(cur, "attn_norm", il);
  8597. // self-attention
  8598. {
  8599. // compute Q and K and RoPE them
  8600. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  8601. cb(Qcur, "Qcur", il);
  8602. if (hparams.f_clamp_kqv > 0.0f) {
  8603. Qcur = ggml_clamp(ctx0, Qcur, -hparams.f_clamp_kqv, hparams.f_clamp_kqv);
  8604. cb(Qcur, "Qcur", il);
  8605. }
  8606. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  8607. cb(Kcur, "Kcur", il);
  8608. if (hparams.f_clamp_kqv > 0.0f) {
  8609. Kcur = ggml_clamp(ctx0, Kcur, -hparams.f_clamp_kqv, hparams.f_clamp_kqv);
  8610. cb(Kcur, "Kcur", il);
  8611. }
  8612. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  8613. cb(Vcur, "Vcur", il);
  8614. if (hparams.f_clamp_kqv > 0.0f) {
  8615. Vcur = ggml_clamp(ctx0, Vcur, -hparams.f_clamp_kqv, hparams.f_clamp_kqv);
  8616. cb(Vcur, "Vcur", il);
  8617. }
  8618. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  8619. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  8620. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  8621. Qcur = ggml_rope_ext(
  8622. ctx0, Qcur, inp_pos, nullptr,
  8623. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  8624. ext_factor, attn_factor, beta_fast, beta_slow
  8625. );
  8626. Kcur = ggml_rope_ext(
  8627. ctx0, Kcur, inp_pos, nullptr,
  8628. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  8629. ext_factor, attn_factor, beta_fast, beta_slow
  8630. );
  8631. cb(Qcur, "Qcur", il);
  8632. cb(Kcur, "Kcur", il);
  8633. cb(Vcur, "Vcur", il);
  8634. cur = build_attn(inp_attn, gf,
  8635. model.layers[il].wo, nullptr,
  8636. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  8637. }
  8638. if (il == n_layer - 1 && inp_out_ids) {
  8639. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  8640. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  8641. }
  8642. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  8643. cb(ffn_inp, "ffn_inp", il);
  8644. // feed-forward network
  8645. cur = build_norm(ffn_inp,
  8646. NULL, NULL,
  8647. LLM_NORM, il);
  8648. cb(cur, "ffn_norm", il);
  8649. cur = build_ffn(cur,
  8650. model.layers[il].ffn_up, NULL, NULL,
  8651. model.layers[il].ffn_gate, NULL, NULL,
  8652. model.layers[il].ffn_down, NULL, NULL,
  8653. NULL,
  8654. LLM_FFN_SILU, LLM_FFN_PAR, il);
  8655. cb(cur, "ffn_out", il);
  8656. cur = ggml_add(ctx0, cur, ffn_inp);
  8657. cb(cur, "ffn_out", il);
  8658. cur = build_cvec(cur, il);
  8659. cb(cur, "l_out", il);
  8660. // input for next layer
  8661. inpL = cur;
  8662. }
  8663. cur = inpL;
  8664. cur = build_norm(cur,
  8665. NULL, NULL,
  8666. LLM_NORM, -1);
  8667. cb(cur, "result_norm", -1);
  8668. res->t_embd = cur;
  8669. // lm_head
  8670. cur = build_lora_mm(model.output, cur);
  8671. cb(cur, "result_output", -1);
  8672. res->t_logits = cur;
  8673. ggml_build_forward_expand(gf, cur);
  8674. }
  8675. };
  8676. struct llm_build_olmo2 : public llm_graph_context {
  8677. llm_build_olmo2(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
  8678. const int64_t n_embd_head = hparams.n_embd_head_v;
  8679. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  8680. GGML_ASSERT(n_embd_head == hparams.n_rot);
  8681. ggml_tensor * cur;
  8682. ggml_tensor * inpL;
  8683. inpL = build_inp_embd(model.tok_embd);
  8684. // inp_pos - contains the positions
  8685. ggml_tensor * inp_pos = build_inp_pos();
  8686. auto * inp_attn = build_attn_inp_kv_unified();
  8687. ggml_tensor * inp_out_ids = build_inp_out_ids();
  8688. for (int il = 0; il < n_layer; ++il) {
  8689. ggml_tensor * inpSA = inpL;
  8690. cur = inpL;
  8691. // self_attention
  8692. {
  8693. // compute Q and K and RoPE them
  8694. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  8695. cb(Qcur, "Qcur", il);
  8696. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  8697. cb(Kcur, "Kcur", il);
  8698. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  8699. cb(Vcur, "Vcur", il);
  8700. Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL,
  8701. LLM_NORM_RMS, il);
  8702. cb(Qcur, "Qcur_normed", il);
  8703. Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL,
  8704. LLM_NORM_RMS, il);
  8705. cb(Kcur, "Kcur_normed", il);
  8706. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  8707. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  8708. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  8709. Qcur = ggml_rope_ext(
  8710. ctx0, Qcur, inp_pos, nullptr,
  8711. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  8712. ext_factor, attn_factor, beta_fast, beta_slow
  8713. );
  8714. Kcur = ggml_rope_ext(
  8715. ctx0, Kcur, inp_pos, nullptr,
  8716. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  8717. ext_factor, attn_factor, beta_fast, beta_slow
  8718. );
  8719. cb(Qcur, "Qcur", il);
  8720. cb(Kcur, "Kcur", il);
  8721. cb(Vcur, "Vcur", il);
  8722. cur = build_attn(inp_attn, gf,
  8723. model.layers[il].wo, NULL,
  8724. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  8725. }
  8726. if (il == n_layer - 1 && inp_out_ids) {
  8727. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  8728. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  8729. }
  8730. cur = build_norm(cur,
  8731. model.layers[il].attn_post_norm, NULL,
  8732. LLM_NORM_RMS, il);
  8733. cb(cur, "attn_post_norm", il);
  8734. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  8735. cb(ffn_inp, "ffn_inp", il);
  8736. // feed-forward network
  8737. cur = build_ffn(ffn_inp,
  8738. model.layers[il].ffn_up, NULL, NULL,
  8739. model.layers[il].ffn_gate, NULL, NULL,
  8740. model.layers[il].ffn_down, NULL, NULL,
  8741. NULL,
  8742. LLM_FFN_SILU, LLM_FFN_PAR, il);
  8743. cb(cur, "ffn_out", il);
  8744. cur = build_norm(cur,
  8745. model.layers[il].ffn_post_norm, NULL,
  8746. LLM_NORM_RMS, -1);
  8747. cb(cur, "ffn_post_norm", -1);
  8748. cur = ggml_add(ctx0, cur, ffn_inp);
  8749. cb(cur, "ffn_out", il);
  8750. cur = build_cvec(cur, il);
  8751. cb(cur, "l_out", il);
  8752. // input for next layer
  8753. inpL = cur;
  8754. }
  8755. cur = inpL;
  8756. cur = build_norm(cur,
  8757. model.output_norm, NULL,
  8758. LLM_NORM_RMS, -1);
  8759. cb(cur, "result_norm", -1);
  8760. res->t_embd = cur;
  8761. // lm_head
  8762. cur = build_lora_mm(model.output, cur);
  8763. cb(cur, "result_output", -1);
  8764. res->t_logits = cur;
  8765. ggml_build_forward_expand(gf, cur);
  8766. }
  8767. };
  8768. // based on the build_qwen2moe() function, changes:
  8769. // * removed shared experts
  8770. // * removed bias
  8771. // * added q, k norm
  8772. struct llm_build_olmoe : public llm_graph_context {
  8773. llm_build_olmoe(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
  8774. const int64_t n_embd_head = hparams.n_embd_head_v;
  8775. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  8776. GGML_ASSERT(n_embd_head == hparams.n_rot);
  8777. ggml_tensor * cur;
  8778. ggml_tensor * inpL;
  8779. inpL = build_inp_embd(model.tok_embd);
  8780. // inp_pos - contains the positions
  8781. ggml_tensor * inp_pos = build_inp_pos();
  8782. auto * inp_attn = build_attn_inp_kv_unified();
  8783. ggml_tensor * inp_out_ids = build_inp_out_ids();
  8784. for (int il = 0; il < n_layer; ++il) {
  8785. ggml_tensor * inpSA = inpL;
  8786. // norm
  8787. cur = build_norm(inpL,
  8788. model.layers[il].attn_norm, NULL,
  8789. LLM_NORM_RMS, il);
  8790. cb(cur, "attn_norm", il);
  8791. // self_attention
  8792. {
  8793. // compute Q and K and RoPE them
  8794. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  8795. cb(Qcur, "Qcur", il);
  8796. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  8797. cb(Kcur, "Kcur", il);
  8798. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  8799. cb(Vcur, "Vcur", il);
  8800. Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL,
  8801. LLM_NORM_RMS, il);
  8802. cb(Qcur, "Qcur_normed", il);
  8803. Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL,
  8804. LLM_NORM_RMS, il);
  8805. cb(Kcur, "Kcur_normed", il);
  8806. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  8807. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  8808. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  8809. Qcur = ggml_rope_ext(
  8810. ctx0, Qcur, inp_pos, nullptr,
  8811. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  8812. ext_factor, attn_factor, beta_fast, beta_slow
  8813. );
  8814. Kcur = ggml_rope_ext(
  8815. ctx0, Kcur, inp_pos, nullptr,
  8816. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  8817. ext_factor, attn_factor, beta_fast, beta_slow
  8818. );
  8819. cb(Qcur, "Qcur", il);
  8820. cb(Kcur, "Kcur", il);
  8821. cb(Vcur, "Vcur", il);
  8822. cur = build_attn(inp_attn, gf,
  8823. model.layers[il].wo, NULL,
  8824. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  8825. }
  8826. if (il == n_layer - 1 && inp_out_ids) {
  8827. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  8828. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  8829. }
  8830. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  8831. cb(ffn_inp, "ffn_inp", il);
  8832. // MoE branch
  8833. cur = build_norm(ffn_inp,
  8834. model.layers[il].ffn_norm, NULL,
  8835. LLM_NORM_RMS, il);
  8836. cb(cur, "ffn_norm", il);
  8837. cur = build_moe_ffn(cur,
  8838. model.layers[il].ffn_gate_inp,
  8839. model.layers[il].ffn_up_exps,
  8840. model.layers[il].ffn_gate_exps,
  8841. model.layers[il].ffn_down_exps,
  8842. nullptr,
  8843. n_expert, n_expert_used,
  8844. LLM_FFN_SILU, false,
  8845. false, 0.0,
  8846. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  8847. il);
  8848. cb(cur, "ffn_moe_out", il);
  8849. cur = ggml_add(ctx0, cur, ffn_inp);
  8850. cur = build_cvec(cur, il);
  8851. cb(cur, "l_out", il);
  8852. // input for next layer
  8853. inpL = cur;
  8854. }
  8855. cur = inpL;
  8856. cur = build_norm(cur,
  8857. model.output_norm, NULL,
  8858. LLM_NORM_RMS, -1);
  8859. cb(cur, "result_norm", -1);
  8860. res->t_embd = cur;
  8861. // lm_head
  8862. cur = build_lora_mm(model.output, cur);
  8863. cb(cur, "result_output", -1);
  8864. res->t_logits = cur;
  8865. ggml_build_forward_expand(gf, cur);
  8866. }
  8867. };
  8868. struct llm_build_openelm : public llm_graph_context {
  8869. llm_build_openelm(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
  8870. const int64_t n_embd_head = hparams.n_embd_head_v;
  8871. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  8872. ggml_tensor * cur;
  8873. ggml_tensor * inpL;
  8874. inpL = build_inp_embd(model.tok_embd);
  8875. // inp_pos - contains the positions
  8876. ggml_tensor * inp_pos = build_inp_pos();
  8877. auto * inp_attn = build_attn_inp_kv_unified();
  8878. ggml_tensor * inp_out_ids = build_inp_out_ids();
  8879. for (int il = 0; il < n_layer; ++il) {
  8880. const int64_t n_head = hparams.n_head(il);
  8881. const int64_t n_head_kv = hparams.n_head_kv(il);
  8882. const int64_t n_head_qkv = 2*n_head_kv + n_head;
  8883. cur = inpL;
  8884. ggml_tensor * residual = cur;
  8885. // norm
  8886. cur = build_norm(inpL,
  8887. model.layers[il].attn_norm, NULL,
  8888. LLM_NORM_RMS, il);
  8889. cb(cur, "attn_norm", il);
  8890. // self-attention
  8891. {
  8892. cur = build_lora_mm(model.layers[il].wqkv, cur);
  8893. cb(cur, "wqkv", il);
  8894. cur = ggml_reshape_3d(ctx0, cur, n_embd_head_k, n_head_qkv, n_tokens);
  8895. ggml_tensor * Qcur = ggml_view_3d(ctx0, cur, n_embd_head, n_head, n_tokens, cur->nb[1], cur->nb[2], 0);
  8896. cb(Qcur, "Qcur", il);
  8897. 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);
  8898. cb(Kcur, "Kcur", il);
  8899. 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)));
  8900. cb(Vcur, "Vcur", il);
  8901. Qcur = build_norm(Qcur,
  8902. model.layers[il].attn_q_norm, NULL,
  8903. LLM_NORM_RMS, il);
  8904. cb(Qcur, "Qcur", il);
  8905. Kcur = build_norm(Kcur,
  8906. model.layers[il].attn_k_norm, NULL,
  8907. LLM_NORM_RMS, il);
  8908. cb(Kcur, "Kcur", il);
  8909. Qcur = ggml_rope_ext(
  8910. ctx0, Qcur, inp_pos, NULL,
  8911. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  8912. ext_factor, attn_factor, beta_fast, beta_slow
  8913. );
  8914. Kcur = ggml_rope_ext(
  8915. ctx0, Kcur, inp_pos, NULL,
  8916. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  8917. ext_factor, attn_factor, beta_fast, beta_slow
  8918. );
  8919. cb(Qcur, "Qcur", il);
  8920. cb(Kcur, "Kcur", il);
  8921. cb(Qcur, "Vcur", il);
  8922. cur = build_attn(inp_attn, gf,
  8923. model.layers[il].wo, NULL,
  8924. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  8925. }
  8926. if (il == n_layer - 1 && inp_out_ids) {
  8927. residual = ggml_get_rows(ctx0, residual, inp_out_ids);
  8928. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  8929. }
  8930. ggml_tensor * ffn_inp = ggml_add(ctx0, residual, cur);
  8931. cb(ffn_inp, "ffn_inp", il);
  8932. // feed-forward network
  8933. {
  8934. cur = build_norm(ffn_inp,
  8935. model.layers[il].ffn_norm, NULL,
  8936. LLM_NORM_RMS, il);
  8937. cb(cur, "ffn_norm", il);
  8938. cur = build_ffn(cur,
  8939. model.layers[il].ffn_up, NULL, NULL,
  8940. model.layers[il].ffn_gate, NULL, NULL,
  8941. model.layers[il].ffn_down, NULL, NULL,
  8942. NULL,
  8943. LLM_FFN_SILU, LLM_FFN_PAR, il);
  8944. cb(cur, "ffn_out", il);
  8945. }
  8946. cur = ggml_add(ctx0, cur, ffn_inp);
  8947. cur = build_cvec(cur, il);
  8948. cb(cur, "l_out", il);
  8949. inpL = cur;
  8950. }
  8951. cur = inpL;
  8952. // norm
  8953. cur = build_norm(cur,
  8954. model.output_norm, NULL,
  8955. LLM_NORM_RMS, -1);
  8956. cb(cur, "result_norm", -1);
  8957. res->t_embd = cur;
  8958. cur = build_lora_mm(model.output, cur);
  8959. cb(cur, "result_output", -1);
  8960. res->t_logits = cur;
  8961. ggml_build_forward_expand(gf, cur);
  8962. }
  8963. };
  8964. struct llm_build_gptneox : public llm_graph_context {
  8965. llm_build_gptneox(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
  8966. const int64_t n_embd_head = hparams.n_embd_head_v;
  8967. const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  8968. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  8969. ggml_tensor * cur;
  8970. ggml_tensor * inpL;
  8971. inpL = build_inp_embd(model.tok_embd);
  8972. // inp_pos - contains the positions
  8973. ggml_tensor * inp_pos = build_inp_pos();
  8974. auto * inp_attn = build_attn_inp_kv_unified();
  8975. ggml_tensor * inp_out_ids = build_inp_out_ids();
  8976. for (int il = 0; il < n_layer; ++il) {
  8977. cur = build_norm(inpL,
  8978. model.layers[il].attn_norm,
  8979. model.layers[il].attn_norm_b,
  8980. LLM_NORM, il);
  8981. cb(cur, "attn_norm", il);
  8982. // self-attention
  8983. {
  8984. cur = build_lora_mm(model.layers[il].wqkv, cur);
  8985. cb(cur, "wqkv", il);
  8986. cur = ggml_add(ctx0, cur, model.layers[il].bqkv);
  8987. cb(cur, "bqkv", il);
  8988. 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));
  8989. 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));
  8990. ggml_tensor * Vcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd + n_embd_gqa)));
  8991. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  8992. Qcur = ggml_rope_ext(
  8993. ctx0, Qcur, inp_pos, nullptr,
  8994. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  8995. ext_factor, attn_factor, beta_fast, beta_slow
  8996. );
  8997. Kcur = ggml_rope_ext(
  8998. ctx0, Kcur, inp_pos, nullptr,
  8999. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  9000. ext_factor, attn_factor, beta_fast, beta_slow
  9001. );
  9002. cb(Qcur, "Qcur", il);
  9003. cb(Kcur, "Kcur", il);
  9004. cb(Vcur, "Vcur", il);
  9005. cur = build_attn(inp_attn, gf,
  9006. model.layers[il].wo, model.layers[il].bo,
  9007. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  9008. }
  9009. if (il == n_layer - 1 && inp_out_ids) {
  9010. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  9011. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  9012. }
  9013. // ffn
  9014. if (hparams.use_par_res) {
  9015. // attention and ffn are computed in parallel
  9016. // x = x + attn(ln1(x)) + ffn(ln2(x))
  9017. ggml_tensor * attn_out = cur;
  9018. cur = build_norm(inpL,
  9019. model.layers[il].ffn_norm,
  9020. model.layers[il].ffn_norm_b,
  9021. LLM_NORM, il);
  9022. cb(cur, "ffn_norm", il);
  9023. cur = build_ffn(cur,
  9024. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  9025. NULL, NULL, NULL,
  9026. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  9027. NULL,
  9028. LLM_FFN_GELU, LLM_FFN_SEQ, il);
  9029. cb(cur, "ffn_out", il);
  9030. cur = ggml_add(ctx0, cur, inpL);
  9031. cb(cur, "ffn_out", il);
  9032. cur = ggml_add(ctx0, cur, attn_out);
  9033. cur = build_cvec(cur, il);
  9034. cb(cur, "l_out", il);
  9035. // input for next layer
  9036. inpL = cur;
  9037. } else {
  9038. // attention and ffn are computed sequentially
  9039. // x = x + attn(ln1(x))
  9040. // x = x + ffn(ln2(x))
  9041. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
  9042. cb(ffn_inp, "ffn_inp", il);
  9043. cur = build_norm(ffn_inp,
  9044. model.layers[il].ffn_norm,
  9045. model.layers[il].ffn_norm_b,
  9046. LLM_NORM, il);
  9047. cb(cur, "ffn_norm", il);
  9048. cur = build_ffn(cur,
  9049. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  9050. NULL, NULL, NULL,
  9051. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  9052. NULL,
  9053. LLM_FFN_GELU, LLM_FFN_SEQ, il);
  9054. cb(cur, "ffn_out", il);
  9055. cur = ggml_add(ctx0, cur, ffn_inp);
  9056. cur = build_cvec(cur, il);
  9057. cb(cur, "l_out", il);
  9058. // input for next layer
  9059. inpL = cur;
  9060. }
  9061. }
  9062. cur = build_norm(inpL,
  9063. model.output_norm,
  9064. model.output_norm_b,
  9065. LLM_NORM, -1);
  9066. cb(cur, "result_norm", -1);
  9067. res->t_embd = cur;
  9068. cur = build_lora_mm(model.output, cur);
  9069. cb(cur, "result_output", -1);
  9070. res->t_logits = cur;
  9071. ggml_build_forward_expand(gf, cur);
  9072. }
  9073. };
  9074. struct llm_build_arctic : public llm_graph_context {
  9075. llm_build_arctic(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
  9076. const int64_t n_embd_head = hparams.n_embd_head_v;
  9077. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  9078. GGML_ASSERT(n_embd_head == hparams.n_rot);
  9079. ggml_tensor * cur;
  9080. ggml_tensor * inpL;
  9081. inpL = build_inp_embd(model.tok_embd);
  9082. // inp_pos - contains the positions
  9083. ggml_tensor * inp_pos = build_inp_pos();
  9084. auto * inp_attn = build_attn_inp_kv_unified();
  9085. ggml_tensor * inp_out_ids = build_inp_out_ids();
  9086. for (int il = 0; il < n_layer; ++il) {
  9087. ggml_tensor * inpSA = inpL;
  9088. // norm
  9089. cur = build_norm(inpL,
  9090. model.layers[il].attn_norm, NULL,
  9091. LLM_NORM_RMS, il);
  9092. cb(cur, "attn_norm", il);
  9093. // self-attention
  9094. {
  9095. // compute Q and K and RoPE them
  9096. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  9097. cb(Qcur, "Qcur", il);
  9098. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  9099. cb(Kcur, "Kcur", il);
  9100. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  9101. cb(Vcur, "Vcur", il);
  9102. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  9103. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  9104. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  9105. Qcur = ggml_rope_ext(
  9106. ctx0, Qcur, inp_pos, nullptr,
  9107. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  9108. ext_factor, attn_factor, beta_fast, beta_slow
  9109. );
  9110. Kcur = ggml_rope_ext(
  9111. ctx0, Kcur, inp_pos, nullptr,
  9112. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  9113. ext_factor, attn_factor, beta_fast, beta_slow
  9114. );
  9115. cb(Qcur, "Qcur", il);
  9116. cb(Kcur, "Kcur", il);
  9117. cb(Vcur, "Vcur", il);
  9118. cur = build_attn(inp_attn, gf,
  9119. model.layers[il].wo, NULL,
  9120. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  9121. }
  9122. if (il == n_layer - 1 && inp_out_ids) {
  9123. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  9124. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  9125. }
  9126. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  9127. cb(ffn_inp, "ffn_inp", il);
  9128. // feed-forward network
  9129. cur = build_norm(ffn_inp,
  9130. model.layers[il].ffn_norm, NULL,
  9131. LLM_NORM_RMS, il);
  9132. cb(cur, "ffn_norm", il);
  9133. cur = build_ffn(cur,
  9134. model.layers[il].ffn_up, NULL, NULL,
  9135. model.layers[il].ffn_gate, NULL, NULL,
  9136. model.layers[il].ffn_down, NULL, NULL,
  9137. NULL,
  9138. LLM_FFN_SILU, LLM_FFN_PAR, il);
  9139. cb(cur, "ffn_out", il);
  9140. ggml_tensor * ffn_out = ggml_add(ctx0, cur, ffn_inp);
  9141. cb(ffn_out, "ffn_out", il);
  9142. // MoE
  9143. cur = build_norm(inpSA,
  9144. model.layers[il].ffn_norm_exps, NULL,
  9145. LLM_NORM_RMS, il);
  9146. cb(cur, "ffn_norm_exps", il);
  9147. cur = build_moe_ffn(cur,
  9148. model.layers[il].ffn_gate_inp,
  9149. model.layers[il].ffn_up_exps,
  9150. model.layers[il].ffn_gate_exps,
  9151. model.layers[il].ffn_down_exps,
  9152. nullptr,
  9153. n_expert, n_expert_used,
  9154. LLM_FFN_SILU, true,
  9155. false, 0.0,
  9156. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  9157. il);
  9158. cb(cur, "ffn_moe_out", il);
  9159. cur = ggml_add(ctx0, cur, ffn_out);
  9160. cb(cur, "ffn_out", il);
  9161. cur = build_cvec(cur, il);
  9162. cb(cur, "l_out", il);
  9163. // input for next layer
  9164. inpL = cur;
  9165. }
  9166. cur = inpL;
  9167. cur = build_norm(cur,
  9168. model.output_norm, NULL,
  9169. LLM_NORM_RMS, -1);
  9170. cb(cur, "result_norm", -1);
  9171. res->t_embd = cur;
  9172. // lm_head
  9173. cur = build_lora_mm(model.output, cur);
  9174. cb(cur, "result_output", -1);
  9175. res->t_logits = cur;
  9176. ggml_build_forward_expand(gf, cur);
  9177. }
  9178. };
  9179. struct llm_build_deepseek : public llm_graph_context {
  9180. llm_build_deepseek(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
  9181. const int64_t n_embd_head = hparams.n_embd_head_v;
  9182. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  9183. GGML_ASSERT(n_embd_head == hparams.n_rot);
  9184. ggml_tensor * cur;
  9185. ggml_tensor * inpL;
  9186. inpL = build_inp_embd(model.tok_embd);
  9187. // inp_pos - contains the positions
  9188. ggml_tensor * inp_pos = build_inp_pos();
  9189. auto * inp_attn = build_attn_inp_kv_unified();
  9190. const float kq_scale = hparams.f_attention_scale == 0.0f ? 1.0f/sqrtf(float(n_embd_head)) : hparams.f_attention_scale;
  9191. ggml_tensor * inp_out_ids = build_inp_out_ids();
  9192. for (int il = 0; il < n_layer; ++il) {
  9193. ggml_tensor * inpSA = inpL;
  9194. // norm
  9195. cur = build_norm(inpL,
  9196. model.layers[il].attn_norm, NULL,
  9197. LLM_NORM_RMS, il);
  9198. cb(cur, "attn_norm", il);
  9199. // self-attention
  9200. {
  9201. // rope freq factors for llama3; may return nullptr for llama2 and other models
  9202. ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
  9203. // compute Q and K and RoPE them
  9204. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  9205. cb(Qcur, "Qcur", il);
  9206. if (model.layers[il].bq) {
  9207. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  9208. cb(Qcur, "Qcur", il);
  9209. }
  9210. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  9211. cb(Kcur, "Kcur", il);
  9212. if (model.layers[il].bk) {
  9213. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  9214. cb(Kcur, "Kcur", il);
  9215. }
  9216. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  9217. cb(Vcur, "Vcur", il);
  9218. if (model.layers[il].bv) {
  9219. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  9220. cb(Vcur, "Vcur", il);
  9221. }
  9222. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  9223. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  9224. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  9225. Qcur = ggml_rope_ext(
  9226. ctx0, Qcur, inp_pos, rope_factors,
  9227. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  9228. ext_factor, attn_factor, beta_fast, beta_slow
  9229. );
  9230. Kcur = ggml_rope_ext(
  9231. ctx0, Kcur, inp_pos, rope_factors,
  9232. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  9233. ext_factor, attn_factor, beta_fast, beta_slow
  9234. );
  9235. cb(Qcur, "Qcur", il);
  9236. cb(Kcur, "Kcur", il);
  9237. cb(Vcur, "Vcur", il);
  9238. cur = build_attn(inp_attn, gf,
  9239. model.layers[il].wo, model.layers[il].bo,
  9240. Qcur, Kcur, Vcur, nullptr, nullptr, kq_scale, il);
  9241. }
  9242. if (il == n_layer - 1 && inp_out_ids) {
  9243. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  9244. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  9245. }
  9246. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  9247. cb(ffn_inp, "ffn_inp", il);
  9248. cur = build_norm(ffn_inp,
  9249. model.layers[il].ffn_norm, NULL,
  9250. LLM_NORM_RMS, il);
  9251. cb(cur, "ffn_norm", il);
  9252. if ((uint32_t) il < hparams.n_layer_dense_lead) {
  9253. cur = build_ffn(cur,
  9254. model.layers[il].ffn_up, NULL, NULL,
  9255. model.layers[il].ffn_gate, NULL, NULL,
  9256. model.layers[il].ffn_down, NULL, NULL,
  9257. NULL,
  9258. LLM_FFN_SILU, LLM_FFN_PAR, il);
  9259. cb(cur, "ffn_out", il);
  9260. } else {
  9261. // MoE branch
  9262. ggml_tensor * moe_out =
  9263. build_moe_ffn(cur,
  9264. model.layers[il].ffn_gate_inp,
  9265. model.layers[il].ffn_up_exps,
  9266. model.layers[il].ffn_gate_exps,
  9267. model.layers[il].ffn_down_exps,
  9268. nullptr,
  9269. n_expert, n_expert_used,
  9270. LLM_FFN_SILU, false,
  9271. false, hparams.expert_weights_scale,
  9272. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  9273. il);
  9274. cb(moe_out, "ffn_moe_out", il);
  9275. // FFN shared expert
  9276. {
  9277. ggml_tensor * ffn_shexp = build_ffn(cur,
  9278. model.layers[il].ffn_up_shexp, NULL, NULL,
  9279. model.layers[il].ffn_gate_shexp, NULL, NULL,
  9280. model.layers[il].ffn_down_shexp, NULL, NULL,
  9281. NULL,
  9282. LLM_FFN_SILU, LLM_FFN_PAR, il);
  9283. cb(ffn_shexp, "ffn_shexp", il);
  9284. cur = ggml_add(ctx0, moe_out, ffn_shexp);
  9285. cb(cur, "ffn_out", il);
  9286. }
  9287. }
  9288. cur = ggml_add(ctx0, cur, ffn_inp);
  9289. cur = build_cvec(cur, il);
  9290. cb(cur, "l_out", il);
  9291. // input for next layer
  9292. inpL = cur;
  9293. }
  9294. cur = inpL;
  9295. cur = build_norm(cur,
  9296. model.output_norm, NULL,
  9297. LLM_NORM_RMS, -1);
  9298. cb(cur, "result_norm", -1);
  9299. res->t_embd = cur;
  9300. // lm_head
  9301. cur = build_lora_mm(model.output, cur);
  9302. cb(cur, "result_output", -1);
  9303. res->t_logits = cur;
  9304. ggml_build_forward_expand(gf, cur);
  9305. }
  9306. };
  9307. struct llm_build_deepseek2 : public llm_graph_context {
  9308. llm_build_deepseek2(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
  9309. bool is_lite = (hparams.n_layer == 27);
  9310. const bool is_mla = (hparams.n_embd_head_k_mla != 0 && hparams.n_embd_head_v_mla != 0);
  9311. // note: these are the actual head sizes you get when treating as MHA or after "decompression" using wv_b for MLA
  9312. const int64_t n_embd_head_k = is_mla ? hparams.n_embd_head_k_mla : hparams.n_embd_head_k;
  9313. const int64_t n_embd_head_v = is_mla ? hparams.n_embd_head_v_mla : hparams.n_embd_head_v;
  9314. const int64_t n_embd_head_qk_rope = hparams.n_rot;
  9315. const int64_t n_embd_head_qk_nope = n_embd_head_k - n_embd_head_qk_rope;
  9316. const uint32_t kv_lora_rank = hparams.n_lora_kv;
  9317. // We have to pre-scale kq_scale and attn_factor to make the YaRN RoPE work correctly.
  9318. // See https://github.com/ggerganov/llama.cpp/discussions/7416 for detailed explanation.
  9319. const float mscale = attn_factor * (1.0f + hparams.rope_yarn_log_mul * logf(1.0f / freq_scale));
  9320. const float kq_scale = 1.0f*mscale*mscale/sqrtf(float(n_embd_head_k));
  9321. const float attn_factor = 1.0f / (1.0f + 0.1f * logf(1.0f / freq_scale));
  9322. ggml_tensor * cur;
  9323. ggml_tensor * inpL;
  9324. // {n_embd, n_tokens}
  9325. inpL = build_inp_embd(model.tok_embd);
  9326. // inp_pos - contains the positions
  9327. ggml_tensor * inp_pos = build_inp_pos();
  9328. auto * inp_attn = build_attn_inp_kv_unified();
  9329. ggml_tensor * inp_out_ids = build_inp_out_ids();
  9330. for (int il = 0; il < n_layer; ++il) {
  9331. ggml_tensor * inpSA = inpL;
  9332. // norm
  9333. cur = build_norm(inpL,
  9334. model.layers[il].attn_norm, NULL,
  9335. LLM_NORM_RMS, il);
  9336. cb(cur, "attn_norm", il);
  9337. // self_attention
  9338. {
  9339. ggml_tensor * q = NULL;
  9340. if (!is_lite) {
  9341. q = ggml_mul_mat(ctx0, model.layers[il].wq_a, cur);
  9342. cb(q, "q", il);
  9343. q = build_norm(q,
  9344. model.layers[il].attn_q_a_norm, nullptr,
  9345. LLM_NORM_RMS, il);
  9346. cb(q, "q", il);
  9347. q = ggml_mul_mat(ctx0, model.layers[il].wq_b, q);
  9348. cb(q, "q", il);
  9349. } else {
  9350. q = ggml_mul_mat(ctx0, model.layers[il].wq, cur);
  9351. cb(q, "q", il);
  9352. }
  9353. // split into {n_embd_head_qk_nope, n_head, n_tokens}
  9354. ggml_tensor * q_nope = ggml_view_3d(ctx0, q,
  9355. n_embd_head_qk_nope, n_head, n_tokens,
  9356. ggml_row_size(q->type, n_embd_head_k),
  9357. ggml_row_size(q->type, n_embd_head_k) * n_head,
  9358. 0);
  9359. cb(q_nope, "q_nope", il);
  9360. // and {n_embd_head_qk_rope, n_head, n_tokens}
  9361. ggml_tensor * q_pe = ggml_view_3d(ctx0, q,
  9362. n_embd_head_qk_rope, n_head, n_tokens,
  9363. ggml_row_size(q->type, n_embd_head_k),
  9364. ggml_row_size(q->type, n_embd_head_k) * n_head,
  9365. ggml_row_size(q->type, n_embd_head_qk_nope));
  9366. cb(q_pe, "q_pe", il);
  9367. ggml_tensor * kv_cmpr_pe = ggml_mul_mat(ctx0, model.layers[il].wkv_a_mqa, cur);
  9368. cb(kv_cmpr_pe, "kv_cmpr_pe", il);
  9369. // split into {kv_lora_rank, n_tokens}
  9370. ggml_tensor * kv_cmpr = ggml_view_2d(ctx0, kv_cmpr_pe,
  9371. kv_lora_rank, n_tokens,
  9372. ggml_row_size(kv_cmpr_pe->type, kv_lora_rank + n_embd_head_qk_rope),
  9373. 0);
  9374. cb(kv_cmpr, "kv_cmpr", il);
  9375. // and {n_embd_head_qk_rope, 1, n_tokens}
  9376. ggml_tensor * k_pe = ggml_view_3d(ctx0, kv_cmpr_pe,
  9377. n_embd_head_qk_rope, 1, n_tokens,
  9378. ggml_row_size(kv_cmpr_pe->type, kv_lora_rank + n_embd_head_qk_rope),
  9379. ggml_row_size(kv_cmpr_pe->type, kv_lora_rank + n_embd_head_qk_rope),
  9380. ggml_row_size(kv_cmpr_pe->type, kv_lora_rank));
  9381. cb(k_pe, "k_pe", il);
  9382. q_pe = ggml_rope_ext(ctx0, q_pe, inp_pos, nullptr,
  9383. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  9384. ext_factor, attn_factor, beta_fast, beta_slow
  9385. );
  9386. cb(q_pe, "q_pe", il);
  9387. k_pe = ggml_rope_ext(ctx0, k_pe, inp_pos, nullptr,
  9388. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  9389. ext_factor, attn_factor, beta_fast, beta_slow
  9390. );
  9391. cb(k_pe, "k_pe", il);
  9392. kv_cmpr = build_norm(kv_cmpr,
  9393. model.layers[il].attn_kv_a_norm, nullptr,
  9394. LLM_NORM_RMS, il);
  9395. cb(kv_cmpr, "kv_cmpr", il);
  9396. if (is_mla) {
  9397. // {n_embd_head_qk_nope, n_tokens, n_head}
  9398. q_nope = ggml_permute(ctx0, q_nope, 0, 2, 1, 3);
  9399. cb(q_nope, "q_nope_perm", il);
  9400. // {n_embd_head_qk_nope, kv_lora_rank, n_head} x {n_embd_head_qk_nope, n_tokens, n_head}
  9401. ggml_tensor * q_nope_absorbed = ggml_mul_mat(ctx0, model.layers[il].wk_b, q_nope);
  9402. cb(q_nope_absorbed, "q_nope_absorbed", il);
  9403. // {kv_lora_rank, n_head, n_tokens}
  9404. q_nope_absorbed = ggml_permute(ctx0, q_nope_absorbed, 0, 2, 1, 3);
  9405. cb(q_nope_absorbed, "q_nope_absorbed_perm", il);
  9406. // {n_embd_head_qk_rope + kv_lora_rank, n_head, n_tokens}
  9407. // note: rope must go first for in-place context shifting in build_rope_shift()
  9408. ggml_tensor * Qcur = ggml_concat(ctx0, q_pe, q_nope_absorbed, 0);
  9409. cb(Qcur, "Qcur", il);
  9410. kv_cmpr = ggml_reshape_3d(ctx0, kv_cmpr, kv_lora_rank, 1, n_tokens);
  9411. cb(kv_cmpr, "kv_cmpr_reshape", il);
  9412. // {n_embd_head_qk_rope + kv_lora_rank, 1, n_tokens}
  9413. ggml_tensor * Kcur = ggml_concat(ctx0, k_pe, kv_cmpr, 0);
  9414. cb(Kcur, "Kcur", il);
  9415. // {kv_lora_rank, 1, n_tokens}
  9416. ggml_tensor * Vcur = kv_cmpr;
  9417. cb(Vcur, "Vcur", il);
  9418. // note: MLA with the absorption optimzation converts into MQA (ie: GQA with 1 group)
  9419. cur = build_attn(inp_attn, gf,
  9420. model.layers[il].wo, NULL,
  9421. Qcur, Kcur, Vcur, nullptr, model.layers[il].wv_b, kq_scale, il);
  9422. } else {
  9423. ggml_tensor * kv = ggml_mul_mat(ctx0, model.layers[il].wkv_b, kv_cmpr);
  9424. cb(kv, "kv", il);
  9425. // split into {n_embd_head_qk_nope, n_head, n_tokens}
  9426. ggml_tensor * k_nope = ggml_view_3d(ctx0, kv,
  9427. n_embd_head_qk_nope, n_head, n_tokens,
  9428. ggml_row_size(kv->type, n_embd_head_qk_nope + n_embd_head_v),
  9429. ggml_row_size(kv->type, n_embd_head_qk_nope + n_embd_head_v) * n_head,
  9430. 0);
  9431. cb(k_nope, "k_nope_view", il);
  9432. // and {n_embd_head_v, n_head, n_tokens}
  9433. ggml_tensor * Vcur = ggml_view_3d(ctx0, kv,
  9434. n_embd_head_v, n_head, n_tokens,
  9435. ggml_row_size(kv->type, n_embd_head_qk_nope + n_embd_head_v),
  9436. ggml_row_size(kv->type, n_embd_head_qk_nope + n_embd_head_v) * n_head,
  9437. ggml_row_size(kv->type, n_embd_head_qk_nope));
  9438. cb(Vcur, "Vcur_view", il);
  9439. Vcur = ggml_cont(ctx0, Vcur);
  9440. cb(Vcur, "Vcur_cont", il);
  9441. // note: rope must go first for in-place context shifting in build_rope_shift()
  9442. ggml_tensor * Qcur = ggml_concat(ctx0, q_pe, q_nope, 0);
  9443. cb(Qcur, "Qcur", il);
  9444. ggml_tensor * Kcur = ggml_concat(ctx0, ggml_repeat(ctx0, k_pe, q_pe), k_nope, 0);
  9445. cb(Kcur, "Kcur", il);
  9446. // note: MLA without the absorption optimization converts into MHA (ie: GQA with full n_head groups)
  9447. cur = build_attn(inp_attn, gf,
  9448. model.layers[il].wo, NULL,
  9449. Qcur, Kcur, Vcur, nullptr, nullptr, kq_scale, il);
  9450. }
  9451. }
  9452. if (il == n_layer - 1 && inp_out_ids) {
  9453. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  9454. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  9455. }
  9456. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  9457. cb(ffn_inp, "ffn_inp", il);
  9458. cur = build_norm(ffn_inp,
  9459. model.layers[il].ffn_norm, NULL,
  9460. LLM_NORM_RMS, il);
  9461. cb(cur, "ffn_norm", il);
  9462. if ((uint32_t) il < hparams.n_layer_dense_lead) {
  9463. cur = build_ffn(cur,
  9464. model.layers[il].ffn_up, NULL, NULL,
  9465. model.layers[il].ffn_gate, NULL, NULL,
  9466. model.layers[il].ffn_down, NULL, NULL,
  9467. NULL,
  9468. LLM_FFN_SILU, LLM_FFN_PAR, il);
  9469. cb(cur, "ffn_out", il);
  9470. } else {
  9471. // MoE branch
  9472. ggml_tensor * moe_out =
  9473. build_moe_ffn(cur,
  9474. model.layers[il].ffn_gate_inp,
  9475. model.layers[il].ffn_up_exps,
  9476. model.layers[il].ffn_gate_exps,
  9477. model.layers[il].ffn_down_exps,
  9478. model.layers[il].ffn_exp_probs_b,
  9479. n_expert, n_expert_used,
  9480. LLM_FFN_SILU, hparams.expert_weights_norm,
  9481. true, hparams.expert_weights_scale,
  9482. (llama_expert_gating_func_type) hparams.expert_gating_func,
  9483. il);
  9484. cb(moe_out, "ffn_moe_out", il);
  9485. // FFN shared expert
  9486. {
  9487. ggml_tensor * ffn_shexp = build_ffn(cur,
  9488. model.layers[il].ffn_up_shexp, NULL, NULL,
  9489. model.layers[il].ffn_gate_shexp, NULL, NULL,
  9490. model.layers[il].ffn_down_shexp, NULL, NULL,
  9491. NULL,
  9492. LLM_FFN_SILU, LLM_FFN_PAR, il);
  9493. cb(ffn_shexp, "ffn_shexp", il);
  9494. cur = ggml_add(ctx0, moe_out, ffn_shexp);
  9495. cb(cur, "ffn_out", il);
  9496. }
  9497. }
  9498. cur = ggml_add(ctx0, cur, ffn_inp);
  9499. cur = build_cvec(cur, il);
  9500. cb(cur, "l_out", il);
  9501. // input for next layer
  9502. inpL = cur;
  9503. }
  9504. cur = inpL;
  9505. cur = build_norm(cur,
  9506. model.output_norm, NULL,
  9507. LLM_NORM_RMS, -1);
  9508. cb(cur, "result_norm", -1);
  9509. res->t_embd = cur;
  9510. // lm_head
  9511. cur = ggml_mul_mat(ctx0, model.output, cur);
  9512. cb(cur, "result_output", -1);
  9513. res->t_logits = cur;
  9514. ggml_build_forward_expand(gf, cur);
  9515. }
  9516. };
  9517. struct llm_build_bitnet : public llm_graph_context {
  9518. llm_build_bitnet(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
  9519. const int64_t n_embd_head = hparams.n_embd_head_v;
  9520. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  9521. ggml_tensor * cur;
  9522. ggml_tensor * inpL;
  9523. inpL = build_inp_embd(model.tok_embd);
  9524. // inp_pos - contains the positions
  9525. ggml_tensor * inp_pos = build_inp_pos();
  9526. auto * inp_attn = build_attn_inp_kv_unified();
  9527. ggml_tensor * inp_out_ids = build_inp_out_ids();
  9528. for (int il = 0; il < n_layer; ++il) {
  9529. ggml_tensor * inpSA = inpL;
  9530. cur = build_norm(inpL,
  9531. model.layers[il].attn_norm, NULL,
  9532. LLM_NORM_RMS, il);
  9533. cb(cur, "attn_norm", il);
  9534. // self-attention
  9535. {
  9536. // compute Q and K and RoPE them
  9537. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  9538. if (model.layers[il].wq_scale) {
  9539. Qcur = ggml_mul(ctx0, Qcur, model.layers[il].wq_scale);
  9540. }
  9541. cb(Qcur, "Qcur", il);
  9542. if (model.layers[il].bq) {
  9543. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  9544. cb(Qcur, "Qcur", il);
  9545. }
  9546. // B1.K
  9547. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  9548. if (model.layers[il].wk_scale) {
  9549. Kcur = ggml_mul(ctx0, Kcur, model.layers[il].wk_scale);
  9550. }
  9551. cb(Kcur, "Kcur", il);
  9552. if (model.layers[il].bk) {
  9553. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  9554. cb(Kcur, "Kcur", il);
  9555. }
  9556. // B1.V
  9557. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  9558. if (model.layers[il].wv_scale) {
  9559. Vcur = ggml_mul(ctx0, Vcur, model.layers[il].wv_scale);
  9560. }
  9561. cb(Vcur, "Vcur", il);
  9562. if (model.layers[il].bv) {
  9563. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  9564. cb(Vcur, "Vcur", il);
  9565. }
  9566. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  9567. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  9568. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  9569. Qcur = ggml_rope_ext(
  9570. ctx0, Qcur, inp_pos, nullptr,
  9571. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  9572. ext_factor, attn_factor, beta_fast, beta_slow
  9573. );
  9574. Kcur = ggml_rope_ext(
  9575. ctx0, Kcur, inp_pos, nullptr,
  9576. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  9577. ext_factor, attn_factor, beta_fast, beta_slow
  9578. );
  9579. cb(Qcur, "Qcur", il);
  9580. cb(Kcur, "Kcur", il);
  9581. cb(Vcur, "Vcur", il);
  9582. cur = build_attn(inp_attn, gf,
  9583. NULL, NULL,
  9584. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  9585. cur = build_norm(cur,
  9586. model.layers[il].attn_sub_norm, NULL,
  9587. LLM_NORM_RMS, il);
  9588. cb(cur, "attn_sub_norm", il);
  9589. cur = build_lora_mm(model.layers[il].wo, cur);
  9590. if (model.layers[il].wo_scale) {
  9591. cur = ggml_mul(ctx0, cur, model.layers[il].wo_scale);
  9592. }
  9593. if (model.layers[il].bo) {
  9594. cur = ggml_add(ctx0, cur, model.layers[il].bo);
  9595. }
  9596. cb(cur, "attn_o_out", il);
  9597. }
  9598. if (il == n_layer - 1 && inp_out_ids) {
  9599. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  9600. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  9601. }
  9602. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  9603. cb(ffn_inp, "ffn_inp", il);
  9604. // feed-forward forward
  9605. cur = build_norm(ffn_inp,
  9606. model.layers[il].ffn_norm, NULL,
  9607. LLM_NORM_RMS, il);
  9608. cb(cur, "ffn_norm", il);
  9609. cur = build_ffn(cur,
  9610. model.layers[il].ffn_up, NULL, model.layers[il].ffn_up_scale,
  9611. model.layers[il].ffn_gate, NULL, model.layers[il].ffn_gate_scale,
  9612. NULL, NULL, NULL,
  9613. NULL,
  9614. LLM_FFN_SILU, LLM_FFN_PAR, il);
  9615. cb(cur, "ffn_sub_out", il);
  9616. cur = build_norm(cur,
  9617. model.layers[il].ffn_sub_norm, NULL,
  9618. LLM_NORM_RMS, il);
  9619. cb(cur, "ffn_sub_norm", il);
  9620. cur = build_lora_mm(model.layers[il].ffn_down, cur);
  9621. if (model.layers[il].ffn_down_scale) {
  9622. cur = ggml_mul(ctx0, cur, model.layers[il].ffn_down_scale);
  9623. }
  9624. cb(cur, "ffn_down", il);
  9625. cur = ggml_add(ctx0, cur, ffn_inp);
  9626. cb(cur, "l_out", il);
  9627. // input for next layer
  9628. inpL = cur;
  9629. }
  9630. cur = inpL;
  9631. cur = build_norm(cur,
  9632. model.output_norm, NULL,
  9633. LLM_NORM_RMS, -1);
  9634. cb(cur, "result_norm", -1);
  9635. res->t_embd = cur;
  9636. // lm_head
  9637. // FIXME: do not use model.tok_embd directly, duplicate as model.output
  9638. cur = build_lora_mm(model.tok_embd, cur);
  9639. cb(cur, "result_output", -1);
  9640. res->t_logits = cur;
  9641. ggml_build_forward_expand(gf, cur);
  9642. }
  9643. };
  9644. struct llm_build_t5_enc : public llm_graph_context {
  9645. llm_build_t5_enc(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
  9646. const int64_t n_embd_head = hparams.n_embd_head_v;
  9647. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  9648. ggml_tensor * cur;
  9649. ggml_tensor * inpL;
  9650. inpL = build_inp_embd(model.tok_embd);
  9651. ggml_tensor * pos_bucket_enc = build_inp_pos_bucket_enc();
  9652. auto * inp_attn = build_attn_inp_no_cache();
  9653. ggml_tensor * inp_out_ids = build_inp_out_ids();
  9654. for (int il = 0; il < n_layer; ++il) {
  9655. ggml_tensor * inpSA = inpL;
  9656. // norm
  9657. cur = build_norm(inpL,
  9658. model.layers[il].attn_norm_enc, NULL,
  9659. LLM_NORM_RMS, il);
  9660. cb(cur, "attn_norm", il);
  9661. // self-attention
  9662. {
  9663. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq_enc, cur);
  9664. cb(Qcur, "Qcur", il);
  9665. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk_enc, cur);
  9666. cb(Kcur, "Kcur", il);
  9667. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv_enc, cur);
  9668. cb(Vcur, "Vcur", il);
  9669. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  9670. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  9671. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  9672. 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;
  9673. ggml_tensor * kq_b = build_pos_bias(pos_bucket_enc, attn_rel_b);
  9674. cur = build_attn(inp_attn, gf,
  9675. model.layers[il].wo_enc, nullptr,
  9676. Qcur, Kcur, Vcur, kq_b, nullptr, 1.0f, il);
  9677. cb(cur, "kqv_out", il);
  9678. }
  9679. if (il == n_layer - 1 && inp_out_ids) {
  9680. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  9681. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  9682. }
  9683. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  9684. cb(ffn_inp, "ffn_inp", il);
  9685. // feed-forward network
  9686. {
  9687. cur = build_norm(ffn_inp,
  9688. model.layers[il].ffn_norm_enc, NULL,
  9689. LLM_NORM_RMS, il);
  9690. cb(cur, "ffn_norm", il);
  9691. // T5 uses relu, flan-T5 uses gelu-gated
  9692. cur = build_ffn(cur,
  9693. model.layers[il].ffn_up_enc, NULL, NULL,
  9694. model.layers[il].ffn_gate_enc, NULL, NULL,
  9695. model.layers[il].ffn_down_enc, NULL, NULL,
  9696. NULL,
  9697. model.layers[il].ffn_gate_enc ? LLM_FFN_GELU : LLM_FFN_RELU,
  9698. model.layers[il].ffn_gate_enc ? LLM_FFN_PAR : LLM_FFN_SEQ,
  9699. il);
  9700. cb(cur, "ffn_out", il);
  9701. }
  9702. cur = ggml_add(ctx0, cur, ffn_inp);
  9703. cb(cur, "ffn_out", il);
  9704. cur = build_cvec(cur, il);
  9705. cb(cur, "l_out", il);
  9706. // input for next layer
  9707. inpL = cur;
  9708. }
  9709. cur = inpL;
  9710. cb(cur, "result_embd", -1);
  9711. cur = build_norm(cur,
  9712. model.output_norm_enc, NULL,
  9713. LLM_NORM_RMS, -1);
  9714. cb(cur, "result_norm", -1);
  9715. res->t_embd = cur;
  9716. ggml_build_forward_expand(gf, cur);
  9717. }
  9718. };
  9719. struct llm_build_t5_dec : public llm_graph_context {
  9720. llm_build_t5_dec(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
  9721. const int64_t n_embd_head = hparams.n_embd_head_v;
  9722. //const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  9723. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  9724. ggml_tensor * cur;
  9725. ggml_tensor * inpL;
  9726. inpL = build_inp_embd(model.tok_embd);
  9727. ggml_tensor * embd_enc = build_inp_cross_embd();
  9728. ggml_tensor * pos_bucket_dec = build_inp_pos_bucket_dec();
  9729. const int64_t n_outputs_enc = embd_enc->ne[1];
  9730. auto * inp_attn_self = build_attn_inp_kv_unified();
  9731. auto * inp_attn_cross = build_attn_inp_cross();
  9732. ggml_tensor * inp_out_ids = build_inp_out_ids();
  9733. for (int il = 0; il < n_layer; ++il) {
  9734. ggml_tensor * inpSA = inpL;
  9735. // norm
  9736. cur = build_norm(inpL,
  9737. model.layers[il].attn_norm, NULL,
  9738. LLM_NORM_RMS, il);
  9739. cb(cur, "attn_norm", il);
  9740. // self-attention
  9741. {
  9742. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  9743. cb(Qcur, "Qcur", il);
  9744. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  9745. cb(Kcur, "Kcur", il);
  9746. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  9747. cb(Vcur, "Vcur", il);
  9748. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  9749. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  9750. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  9751. ggml_tensor * attn_rel_b = model.layers[il].attn_rel_b ? model.layers[il].attn_rel_b : model.layers[0].attn_rel_b;
  9752. ggml_tensor * kq_b = build_pos_bias(pos_bucket_dec, attn_rel_b);
  9753. cur = build_attn(inp_attn_self, gf,
  9754. model.layers[il].wo, model.layers[il].bo,
  9755. Qcur, Kcur, Vcur, kq_b, nullptr, 1.0f, il);
  9756. cb(cur, "kqv_out", il);
  9757. }
  9758. cur = ggml_add(ctx0, cur, inpSA);
  9759. cb(cur, "cross_inp", il);
  9760. ggml_tensor * inpCA = cur;
  9761. // norm
  9762. cur = build_norm(cur,
  9763. model.layers[il].attn_norm_cross, NULL,
  9764. LLM_NORM_RMS, il);
  9765. cb(cur, "attn_norm_cross", il);
  9766. // cross-attention
  9767. {
  9768. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq_cross, cur);
  9769. cb(Qcur, "Qcur", il);
  9770. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk_cross, embd_enc);
  9771. cb(Kcur, "Kcur", il);
  9772. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv_cross, embd_enc);
  9773. cb(Vcur, "Vcur", il);
  9774. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  9775. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_outputs_enc);
  9776. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_outputs_enc);
  9777. cur = build_attn(inp_attn_cross, gf,
  9778. model.layers[il].wo_cross, nullptr,
  9779. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f, il);
  9780. cb(cur, "kqv_out", il);
  9781. //ggml_tensor * q = ggml_permute(ctx0, Qcur, 0, 2, 1, 3);
  9782. //ggml_tensor * k = ggml_cont(ctx0, ggml_permute(ctx0, Kcur, 0, 2, 1, 3));
  9783. //ggml_tensor * kq = ggml_mul_mat(ctx0, k, q);
  9784. //cb(kq, "kq", il);
  9785. //kq = ggml_soft_max_ext(ctx0, kq, KQ_mask_cross, 1.0f, hparams.f_max_alibi_bias);
  9786. //cb(kq, "kq_soft_max_ext", il);
  9787. //ggml_tensor * v = ggml_cont(ctx0, ggml_transpose(ctx0, ggml_reshape_2d(ctx0, Vcur, n_embd_gqa, n_outputs_enc)));
  9788. //cb(v, "v", il);
  9789. //ggml_tensor * kqv = ggml_mul_mat(ctx0, ggml_reshape_3d(ctx0, v, n_outputs_enc, n_embd_head, n_head_kv), kq);
  9790. //cb(kqv, "kqv", il);
  9791. //ggml_tensor * kqv_merged = ggml_permute(ctx0, kqv, 0, 2, 1, 3);
  9792. //cb(kqv_merged, "kqv_merged", il);
  9793. //cur = ggml_cont_2d(ctx0, kqv_merged, n_embd_gqa, n_tokens);
  9794. //cb(cur, "kqv_merged_cont", il);
  9795. //ggml_build_forward_expand(gf, cur);
  9796. //cur = build_lora_mm(model.layers[il].wo_cross, cur);
  9797. //cb(cur, "kqv_out", il);
  9798. }
  9799. if (il == n_layer - 1 && inp_out_ids) {
  9800. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  9801. inpCA = ggml_get_rows(ctx0, inpCA, inp_out_ids);
  9802. }
  9803. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpCA);
  9804. cb(ffn_inp, "ffn_inp", il);
  9805. // feed-forward network
  9806. {
  9807. cur = build_norm(ffn_inp,
  9808. model.layers[il].ffn_norm, NULL,
  9809. LLM_NORM_RMS, il);
  9810. cb(cur, "ffn_norm", il);
  9811. // T5 uses relu, flan-T5 uses gelu-gated
  9812. cur = build_ffn(cur,
  9813. model.layers[il].ffn_up, NULL, NULL,
  9814. model.layers[il].ffn_gate, NULL, NULL,
  9815. model.layers[il].ffn_down, NULL, NULL,
  9816. NULL,
  9817. model.layers[il].ffn_gate_enc ? LLM_FFN_GELU : LLM_FFN_RELU,
  9818. model.layers[il].ffn_gate_enc ? LLM_FFN_PAR : LLM_FFN_SEQ,
  9819. il);
  9820. cb(cur, "ffn_out", il);
  9821. }
  9822. cur = ggml_add(ctx0, cur, ffn_inp);
  9823. cb(cur, "ffn_out", il);
  9824. cur = build_cvec(cur, il);
  9825. cb(cur, "l_out", il);
  9826. // input for next layer
  9827. inpL = cur;
  9828. }
  9829. cur = inpL;
  9830. cb(cur, "result_embd", -1);
  9831. cur = build_norm(cur,
  9832. model.output_norm, NULL,
  9833. LLM_NORM_RMS, -1);
  9834. cb(cur, "result_norm", -1);
  9835. res->t_embd = cur;
  9836. // lm_head
  9837. cur = build_lora_mm(model.output, cur);
  9838. cb(cur, "result_output", -1);
  9839. res->t_logits = cur;
  9840. ggml_build_forward_expand(gf, cur);
  9841. }
  9842. };
  9843. struct llm_build_jais : public llm_graph_context {
  9844. llm_build_jais(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
  9845. const int64_t n_embd_head = hparams.n_embd_head_v;
  9846. const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  9847. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  9848. ggml_tensor * cur;
  9849. ggml_tensor * inpL;
  9850. inpL = build_inp_embd(model.tok_embd);
  9851. auto * inp_attn = build_attn_inp_kv_unified();
  9852. ggml_tensor * inp_out_ids = build_inp_out_ids();
  9853. for (int il = 0; il < n_layer; ++il) {
  9854. cur = build_norm(inpL,
  9855. model.layers[il].attn_norm,
  9856. model.layers[il].attn_norm_b,
  9857. LLM_NORM, il);
  9858. cb(cur, "attn_norm", il);
  9859. // self-attention
  9860. {
  9861. cur = build_lora_mm(model.layers[il].wqkv, cur);
  9862. cb(cur, "wqkv", il);
  9863. cur = ggml_add(ctx0, cur, model.layers[il].bqkv);
  9864. cb(cur, "bqkv", il);
  9865. ggml_tensor * Qcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd, n_tokens, cur->nb[1], 0*cur->nb[0]*(n_embd)));
  9866. ggml_tensor * Kcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*cur->nb[0]*(n_embd)));
  9867. ggml_tensor * Vcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*cur->nb[0]*(n_embd + n_embd_gqa)));
  9868. cb(Qcur, "Qcur", il);
  9869. cb(Kcur, "Kcur", il);
  9870. cb(Vcur, "Vcur", il);
  9871. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  9872. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  9873. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  9874. cur = build_attn(inp_attn, gf,
  9875. model.layers[il].wo, model.layers[il].bo,
  9876. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/float(n_embd_head), il);
  9877. }
  9878. if (il == n_layer - 1 && inp_out_ids) {
  9879. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  9880. inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
  9881. }
  9882. // add the input
  9883. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
  9884. cb(ffn_inp, "ffn_inp", il);
  9885. // FF
  9886. {
  9887. cur = build_norm(ffn_inp,
  9888. model.layers[il].ffn_norm,
  9889. model.layers[il].ffn_norm_b,
  9890. LLM_NORM, il);
  9891. cb(cur, "ffn_norm", il);
  9892. cur = build_ffn(cur,
  9893. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  9894. model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, NULL,
  9895. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  9896. NULL,
  9897. LLM_FFN_SILU, LLM_FFN_PAR, il);
  9898. cb(cur, "ffn_out", il);
  9899. }
  9900. inpL = ggml_add(ctx0, cur, ffn_inp);
  9901. cb(inpL, "l_out", il);
  9902. }
  9903. cur = build_norm(inpL,
  9904. model.output_norm,
  9905. model.output_norm_b,
  9906. LLM_NORM, -1);
  9907. cb(cur, "result_norm", -1);
  9908. res->t_embd = cur;
  9909. cur = build_lora_mm(model.output, cur);
  9910. cb(cur, "result_output", -1);
  9911. res->t_logits = cur;
  9912. ggml_build_forward_expand(gf, cur);
  9913. }
  9914. };
  9915. struct llm_build_chatglm : public llm_graph_context {
  9916. llm_build_chatglm(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
  9917. const int64_t n_embd_head = hparams.n_embd_head_v;
  9918. const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  9919. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  9920. ggml_tensor * cur;
  9921. ggml_tensor * inpL;
  9922. inpL = build_inp_embd(model.tok_embd);
  9923. // inp_pos - contains the positions
  9924. ggml_tensor * inp_pos = build_inp_pos();
  9925. auto * inp_attn = build_attn_inp_kv_unified();
  9926. ggml_tensor * inp_out_ids = build_inp_out_ids();
  9927. for (int il = 0; il < n_layer; ++il) {
  9928. ggml_tensor * inpSA = inpL;
  9929. cur = build_norm(inpL,
  9930. model.layers[il].attn_norm,
  9931. NULL,
  9932. LLM_NORM_RMS, il);
  9933. cb(cur, "attn_norm", il);
  9934. // self-attention
  9935. {
  9936. ggml_tensor * Qcur = nullptr;
  9937. ggml_tensor * Kcur = nullptr;
  9938. ggml_tensor * Vcur = nullptr;
  9939. if (model.layers[il].wqkv == nullptr) {
  9940. Qcur = build_lora_mm(model.layers[il].wq, cur);
  9941. if (model.layers[il].bq) {
  9942. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  9943. }
  9944. Kcur = build_lora_mm(model.layers[il].wk, cur);
  9945. if (model.layers[il].bk) {
  9946. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  9947. }
  9948. Vcur = build_lora_mm(model.layers[il].wv, cur);
  9949. if (model.layers[il].bv) {
  9950. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  9951. }
  9952. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  9953. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  9954. } else {
  9955. cur = build_lora_mm(model.layers[il].wqkv, cur);
  9956. cb(cur, "wqkv", il);
  9957. if (model.layers[il].bqkv) {
  9958. cur = ggml_add(ctx0, cur, model.layers[il].bqkv);
  9959. cb(cur, "bqkv", il);
  9960. }
  9961. 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));
  9962. 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));
  9963. Vcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd + n_embd_gqa)));
  9964. }
  9965. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  9966. //printf("freq_base: %f freq_scale: %f ext_factor: %f attn_factor: %f\n", freq_base, freq_scale, ext_factor, attn_factor);
  9967. Qcur = ggml_rope_ext(
  9968. ctx0, Qcur, inp_pos, nullptr,
  9969. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  9970. ext_factor, attn_factor, beta_fast, beta_slow
  9971. );
  9972. Kcur = ggml_rope_ext(
  9973. ctx0, Kcur, inp_pos, nullptr,
  9974. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  9975. ext_factor, attn_factor, beta_fast, beta_slow
  9976. );
  9977. cb(Qcur, "Qcur", il);
  9978. cb(Kcur, "Kcur", il);
  9979. cb(Vcur, "Vcur", il);
  9980. cur = build_attn(inp_attn, gf,
  9981. model.layers[il].wo, NULL,
  9982. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  9983. }
  9984. if (il == n_layer - 1 && inp_out_ids) {
  9985. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  9986. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  9987. }
  9988. // Add the input
  9989. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  9990. cb(ffn_inp, "ffn_inp", il);
  9991. // FF
  9992. {
  9993. cur = build_norm(ffn_inp,
  9994. model.layers[il].ffn_norm,
  9995. NULL,
  9996. LLM_NORM_RMS, il);
  9997. cb(cur, "ffn_norm", il);
  9998. cur = build_ffn(cur,
  9999. model.layers[il].ffn_up, NULL, NULL,
  10000. NULL, NULL, NULL,
  10001. model.layers[il].ffn_down, NULL, NULL,
  10002. NULL,
  10003. LLM_FFN_SWIGLU, LLM_FFN_SEQ, il);
  10004. cb(cur, "ffn_out", il);
  10005. }
  10006. inpL = ggml_add(ctx0, cur, ffn_inp);
  10007. cb(inpL, "l_out", il);
  10008. }
  10009. cur = build_norm(inpL,
  10010. model.output_norm,
  10011. NULL,
  10012. LLM_NORM_RMS, -1);
  10013. cb(cur, "result_norm", -1);
  10014. res->t_embd = cur;
  10015. cur = build_lora_mm(model.output, cur);
  10016. cb(cur, "result_output", -1);
  10017. res->t_logits = cur;
  10018. ggml_build_forward_expand(gf, cur);
  10019. }
  10020. };
  10021. struct llm_build_glm4 : public llm_graph_context {
  10022. llm_build_glm4(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
  10023. const int64_t n_embd_head = hparams.n_embd_head_v;
  10024. const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
  10025. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  10026. ggml_tensor * cur;
  10027. ggml_tensor * inpL;
  10028. inpL = build_inp_embd(model.tok_embd);
  10029. // inp_pos - contains the positions
  10030. ggml_tensor * inp_pos = build_inp_pos();
  10031. auto * inp_attn = build_attn_inp_kv_unified();
  10032. ggml_tensor * inp_out_ids = build_inp_out_ids();
  10033. for (int il = 0; il < n_layer; ++il) {
  10034. ggml_tensor * inpSA = inpL;
  10035. // Pre-attention norm
  10036. cur = build_norm(inpL,
  10037. model.layers[il].attn_norm,
  10038. NULL,
  10039. LLM_NORM_RMS, il);
  10040. cb(cur, "attn_norm", il);
  10041. // self-attention
  10042. {
  10043. ggml_tensor * Qcur = nullptr;
  10044. ggml_tensor * Kcur = nullptr;
  10045. ggml_tensor * Vcur = nullptr;
  10046. if (model.layers[il].wqkv == nullptr) {
  10047. Qcur = build_lora_mm(model.layers[il].wq, cur);
  10048. if (model.layers[il].bq) {
  10049. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  10050. }
  10051. Kcur = build_lora_mm(model.layers[il].wk, cur);
  10052. if (model.layers[il].bk) {
  10053. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  10054. }
  10055. Vcur = build_lora_mm(model.layers[il].wv, cur);
  10056. if (model.layers[il].bv) {
  10057. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  10058. }
  10059. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  10060. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  10061. } else {
  10062. cur = build_lora_mm(model.layers[il].wqkv, cur);
  10063. cb(cur, "wqkv", il);
  10064. if (model.layers[il].bqkv) {
  10065. cur = ggml_add(ctx0, cur, model.layers[il].bqkv);
  10066. cb(cur, "bqkv", il);
  10067. }
  10068. 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));
  10069. 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));
  10070. Vcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd + n_embd_gqa)));
  10071. }
  10072. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  10073. Qcur = ggml_rope_ext(
  10074. ctx0, Qcur, inp_pos, nullptr,
  10075. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  10076. ext_factor, attn_factor, beta_fast, beta_slow
  10077. );
  10078. Kcur = ggml_rope_ext(
  10079. ctx0, Kcur, inp_pos, nullptr,
  10080. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  10081. ext_factor, attn_factor, beta_fast, beta_slow
  10082. );
  10083. cb(Qcur, "Qcur", il);
  10084. cb(Kcur, "Kcur", il);
  10085. cb(Vcur, "Vcur", il);
  10086. cur = build_attn(inp_attn, gf,
  10087. model.layers[il].wo, NULL,
  10088. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  10089. }
  10090. if (il == n_layer - 1 && inp_out_ids) {
  10091. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  10092. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  10093. }
  10094. // Post-attention norm (new!)
  10095. cur = build_norm(cur,
  10096. model.layers[il].attn_post_norm,
  10097. NULL,
  10098. LLM_NORM_RMS, il);
  10099. cb(cur, "post_attn_norm", il);
  10100. // Add the input (residual connection after post-attention norm)
  10101. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  10102. cb(ffn_inp, "ffn_inp", il);
  10103. // FF
  10104. {
  10105. // Pre-MLP norm
  10106. cur = build_norm(ffn_inp,
  10107. model.layers[il].ffn_norm,
  10108. NULL,
  10109. LLM_NORM_RMS, il);
  10110. cb(cur, "ffn_norm", il);
  10111. // MLP
  10112. cur = build_ffn(cur,
  10113. model.layers[il].ffn_up, NULL, NULL,
  10114. NULL, NULL, NULL,
  10115. model.layers[il].ffn_down, NULL, NULL,
  10116. NULL,
  10117. LLM_FFN_SWIGLU, LLM_FFN_SEQ, il);
  10118. cb(cur, "ffn_out", il);
  10119. // Post-MLP norm
  10120. cur = build_norm(cur,
  10121. model.layers[il].ffn_post_norm,
  10122. NULL,
  10123. LLM_NORM_RMS, il);
  10124. cb(cur, "post_mlp_norm", il);
  10125. }
  10126. // Add residual connection after post-MLP norm
  10127. inpL = ggml_add(ctx0, cur, ffn_inp);
  10128. cb(inpL, "l_out", il);
  10129. }
  10130. // Final norm
  10131. cur = build_norm(inpL,
  10132. model.output_norm,
  10133. NULL,
  10134. LLM_NORM_RMS, -1);
  10135. cb(cur, "result_norm", -1);
  10136. res->t_embd = cur;
  10137. // Output projection
  10138. cur = build_lora_mm(model.output, cur);
  10139. cb(cur, "result_output", -1);
  10140. res->t_logits = cur;
  10141. ggml_build_forward_expand(gf, cur);
  10142. }
  10143. };
  10144. struct llm_build_nemotron : public llm_graph_context {
  10145. llm_build_nemotron(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
  10146. const int64_t n_embd_head = hparams.n_embd_head_v;
  10147. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  10148. //GGML_ASSERT(n_embd_head == hparams.n_rot);
  10149. ggml_tensor * cur;
  10150. ggml_tensor * inpL;
  10151. inpL = build_inp_embd(model.tok_embd);
  10152. // inp_pos - contains the positions
  10153. ggml_tensor * inp_pos = build_inp_pos();
  10154. auto * inp_attn = build_attn_inp_kv_unified();
  10155. ggml_tensor * inp_out_ids = build_inp_out_ids();
  10156. for (int il = 0; il < n_layer; ++il) {
  10157. ggml_tensor * inpSA = inpL;
  10158. // norm
  10159. cur = build_norm(inpL,
  10160. model.layers[il].attn_norm,
  10161. model.layers[il].attn_norm_b,
  10162. LLM_NORM, il);
  10163. cb(cur, "attn_norm", il);
  10164. // self-attention
  10165. {
  10166. // compute Q and K and RoPE them
  10167. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  10168. cb(Qcur, "Qcur", il);
  10169. if (model.layers[il].bq) {
  10170. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  10171. cb(Qcur, "Qcur", il);
  10172. }
  10173. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  10174. cb(Kcur, "Kcur", il);
  10175. if (model.layers[il].bk) {
  10176. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  10177. cb(Kcur, "Kcur", il);
  10178. }
  10179. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  10180. cb(Vcur, "Vcur", il);
  10181. if (model.layers[il].bv) {
  10182. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  10183. cb(Vcur, "Vcur", il);
  10184. }
  10185. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  10186. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  10187. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  10188. Qcur = ggml_rope_ext(
  10189. ctx0, Qcur, inp_pos, nullptr,
  10190. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  10191. ext_factor, attn_factor, beta_fast, beta_slow
  10192. );
  10193. Kcur = ggml_rope_ext(
  10194. ctx0, Kcur, 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(Qcur, "Qcur", il);
  10199. cb(Kcur, "Kcur", il);
  10200. cb(Vcur, "Vcur", il);
  10201. cur = build_attn(inp_attn, gf,
  10202. model.layers[il].wo, model.layers[il].bo,
  10203. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  10204. }
  10205. if (il == n_layer - 1 && inp_out_ids) {
  10206. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  10207. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  10208. }
  10209. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  10210. cb(ffn_inp, "ffn_inp", il);
  10211. // feed-forward network
  10212. cur = build_norm(ffn_inp,
  10213. model.layers[il].ffn_norm,
  10214. model.layers[il].ffn_norm_b,
  10215. LLM_NORM, il);
  10216. cb(cur, "ffn_norm", il);
  10217. cur = build_ffn(cur,
  10218. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  10219. NULL, NULL, NULL,
  10220. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  10221. NULL,
  10222. LLM_FFN_RELU_SQR, LLM_FFN_SEQ, il);
  10223. cur = ggml_add(ctx0, cur, ffn_inp);
  10224. cb(cur, "ffn_out", il);
  10225. cur = build_cvec(cur, il);
  10226. cb(cur, "l_out", il);
  10227. // input for next layer
  10228. inpL = cur;
  10229. }
  10230. cur = inpL;
  10231. cur = build_norm(cur,
  10232. model.output_norm, model.output_norm_b,
  10233. LLM_NORM, -1);
  10234. cb(cur, "result_norm", -1);
  10235. res->t_embd = cur;
  10236. // lm_head
  10237. cur = build_lora_mm(model.output, cur);
  10238. cb(cur, "result_output", -1);
  10239. res->t_logits = cur;
  10240. ggml_build_forward_expand(gf, cur);
  10241. }
  10242. };
  10243. struct llm_build_exaone : public llm_graph_context {
  10244. llm_build_exaone(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
  10245. const int64_t n_embd_head = hparams.n_embd_head_v;
  10246. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  10247. GGML_ASSERT(n_embd_head == hparams.n_rot);
  10248. ggml_tensor * cur;
  10249. ggml_tensor * inpL;
  10250. inpL = build_inp_embd(model.tok_embd);
  10251. // inp_pos - contains the positions
  10252. ggml_tensor * inp_pos = build_inp_pos();
  10253. auto * inp_attn = build_attn_inp_kv_unified();
  10254. ggml_tensor * inp_out_ids = build_inp_out_ids();
  10255. for (int il = 0; il < n_layer; ++il) {
  10256. ggml_tensor * inpSA = inpL;
  10257. // norm
  10258. cur = build_norm(inpL,
  10259. model.layers[il].attn_norm, NULL,
  10260. LLM_NORM_RMS, il);
  10261. cb(cur, "attn_norm", il);
  10262. // self-attention
  10263. {
  10264. // rope freq factors for llama3; may return nullptr for llama2 and other models
  10265. ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
  10266. // compute Q and K and RoPE them
  10267. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  10268. cb(Qcur, "Qcur", il);
  10269. if (model.layers[il].bq) {
  10270. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  10271. cb(Qcur, "Qcur", il);
  10272. }
  10273. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  10274. cb(Kcur, "Kcur", il);
  10275. if (model.layers[il].bk) {
  10276. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  10277. cb(Kcur, "Kcur", il);
  10278. }
  10279. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  10280. cb(Vcur, "Vcur", il);
  10281. if (model.layers[il].bv) {
  10282. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  10283. cb(Vcur, "Vcur", il);
  10284. }
  10285. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  10286. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  10287. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  10288. Qcur = ggml_rope_ext(
  10289. ctx0, Qcur, inp_pos, rope_factors,
  10290. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  10291. ext_factor, attn_factor, beta_fast, beta_slow
  10292. );
  10293. Kcur = ggml_rope_ext(
  10294. ctx0, Kcur, inp_pos, rope_factors,
  10295. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  10296. ext_factor, attn_factor, beta_fast, beta_slow
  10297. );
  10298. cb(Qcur, "Qcur", il);
  10299. cb(Kcur, "Kcur", il);
  10300. cb(Vcur, "Vcur", il);
  10301. cur = build_attn(inp_attn, gf,
  10302. model.layers[il].wo, model.layers[il].bo,
  10303. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  10304. }
  10305. if (il == n_layer - 1 && inp_out_ids) {
  10306. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  10307. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  10308. }
  10309. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  10310. cb(ffn_inp, "ffn_inp", il);
  10311. // feed-forward network
  10312. cur = build_norm(ffn_inp,
  10313. model.layers[il].ffn_norm, NULL,
  10314. LLM_NORM_RMS, il);
  10315. cb(cur, "ffn_norm", il);
  10316. cur = build_ffn(cur,
  10317. model.layers[il].ffn_up, NULL, NULL,
  10318. model.layers[il].ffn_gate, NULL, NULL,
  10319. model.layers[il].ffn_down, NULL, NULL,
  10320. NULL,
  10321. LLM_FFN_SILU, LLM_FFN_PAR, il);
  10322. cb(cur, "ffn_out", il);
  10323. cur = ggml_add(ctx0, cur, ffn_inp);
  10324. cb(cur, "ffn_out", il);
  10325. cur = build_cvec(cur, il);
  10326. cb(cur, "l_out", il);
  10327. // input for next layer
  10328. inpL = cur;
  10329. }
  10330. cur = inpL;
  10331. cur = build_norm(cur,
  10332. model.output_norm, NULL,
  10333. LLM_NORM_RMS, -1);
  10334. cb(cur, "result_norm", -1);
  10335. res->t_embd = cur;
  10336. // lm_head
  10337. cur = build_lora_mm(model.output, cur);
  10338. cb(cur, "result_output", -1);
  10339. res->t_logits = cur;
  10340. ggml_build_forward_expand(gf, cur);
  10341. }
  10342. };
  10343. struct llm_build_rwkv6_base : public llm_graph_context {
  10344. const llama_model & model;
  10345. llm_build_rwkv6_base(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params), model(model) {
  10346. }
  10347. ggml_tensor * build_rwkv6_channel_mix(
  10348. const llama_layer * layer,
  10349. ggml_tensor * cur,
  10350. ggml_tensor * x_prev,
  10351. llm_arch arch) const {
  10352. ggml_tensor * sx = ggml_sub(ctx0, x_prev, cur);
  10353. switch (arch) {
  10354. case LLM_ARCH_RWKV6:
  10355. {
  10356. ggml_tensor * xk = ggml_add(ctx0, ggml_mul(ctx0, sx, layer->channel_mix_lerp_k), cur);
  10357. ggml_tensor * xr = ggml_add(ctx0, ggml_mul(ctx0, sx, layer->channel_mix_lerp_r), cur);
  10358. ggml_tensor * r = ggml_sigmoid(ctx0, build_lora_mm(layer->channel_mix_receptance, xr));
  10359. ggml_tensor * k = ggml_sqr(
  10360. ctx0,
  10361. ggml_relu(
  10362. ctx0,
  10363. build_lora_mm(layer->channel_mix_key, xk)
  10364. )
  10365. );
  10366. cur = ggml_mul(ctx0, r, build_lora_mm(layer->channel_mix_value, k));
  10367. } break;
  10368. default:
  10369. GGML_ABORT("fatal error");
  10370. }
  10371. return cur;
  10372. }
  10373. ggml_tensor * build_rwkv6_time_mix(
  10374. llm_graph_input_rs * inp,
  10375. ggml_cgraph * gf,
  10376. ggml_tensor * cur,
  10377. ggml_tensor * x_prev,
  10378. const llama_ubatch & ubatch,
  10379. int il) const {
  10380. const auto * mctx_cur = static_cast<const llama_memory_recurrent_context *>(mctx);
  10381. const auto n_tokens = ubatch.n_tokens;
  10382. const auto n_seqs = ubatch.n_seqs;
  10383. const auto n_seq_tokens = ubatch.n_seq_tokens;
  10384. const auto n_embd = hparams.n_embd;
  10385. const auto head_size = hparams.wkv_head_size;
  10386. const auto n_head = n_embd / head_size;
  10387. const auto n_head_kv = hparams.n_head_kv(il);
  10388. const auto kv_head = mctx_cur->get_head();
  10389. const auto & layer = model.layers[il];
  10390. bool is_qrwkv = layer.time_mix_first == nullptr;
  10391. ggml_tensor * sx = ggml_sub(ctx0, x_prev, cur);
  10392. sx = ggml_reshape_2d(ctx0, sx, n_embd, n_tokens);
  10393. cur = ggml_reshape_2d(ctx0, cur, n_embd, n_tokens);
  10394. ggml_tensor * xxx = ggml_add(ctx0, ggml_mul(ctx0, sx, layer.time_mix_lerp_x), cur);
  10395. xxx = ggml_reshape_4d(
  10396. ctx0,
  10397. ggml_tanh(
  10398. ctx0,
  10399. ggml_mul_mat(ctx0, layer.time_mix_w1, xxx)
  10400. ),
  10401. layer.time_mix_w1->ne[1] / 5, 1, 5, n_tokens
  10402. );
  10403. xxx = ggml_cont(ctx0, ggml_permute(ctx0, xxx, 0, 1, 3, 2));
  10404. xxx = ggml_mul_mat(
  10405. ctx0,
  10406. ggml_reshape_4d(
  10407. ctx0,
  10408. layer.time_mix_w2,
  10409. layer.time_mix_w2->ne[0], layer.time_mix_w2->ne[1], 1, 5
  10410. ),
  10411. xxx
  10412. );
  10413. ggml_tensor *xw, *xk, *xv, *xr, *xg;
  10414. if (layer.time_mix_lerp_fused) {
  10415. // fusing these weights makes some performance improvement
  10416. sx = ggml_reshape_3d(ctx0, sx, n_embd, 1, n_tokens);
  10417. cur = ggml_reshape_3d(ctx0, cur, n_embd, 1, n_tokens);
  10418. xxx = ggml_add(ctx0, ggml_mul(ctx0, ggml_add(ctx0, xxx, layer.time_mix_lerp_fused), sx), cur);
  10419. xw = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], 0);
  10420. xk = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], n_embd * n_tokens * sizeof(float));
  10421. xv = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], n_embd * n_tokens * 2 * sizeof(float));
  10422. xr = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], n_embd * n_tokens * 3 * sizeof(float));
  10423. xg = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], n_embd * n_tokens * 4 * sizeof(float));
  10424. } else {
  10425. // for backward compatibility
  10426. xw = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], 0);
  10427. xk = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], n_embd * n_tokens * sizeof(float));
  10428. xv = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], n_embd * n_tokens * 2 * sizeof(float));
  10429. xr = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], n_embd * n_tokens * 3 * sizeof(float));
  10430. xg = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], n_embd * n_tokens * 4 * sizeof(float));
  10431. xw = ggml_add(ctx0, ggml_mul(ctx0, ggml_add(ctx0, xw, layer.time_mix_lerp_w), sx), cur);
  10432. xk = ggml_add(ctx0, ggml_mul(ctx0, ggml_add(ctx0, xk, layer.time_mix_lerp_k), sx), cur);
  10433. xv = ggml_add(ctx0, ggml_mul(ctx0, ggml_add(ctx0, xv, layer.time_mix_lerp_v), sx), cur);
  10434. xr = ggml_add(ctx0, ggml_mul(ctx0, ggml_add(ctx0, xr, layer.time_mix_lerp_r), sx), cur);
  10435. xg = ggml_add(ctx0, ggml_mul(ctx0, ggml_add(ctx0, xg, layer.time_mix_lerp_g), sx), cur);
  10436. }
  10437. ggml_tensor * r = build_lora_mm(layer.time_mix_receptance, xr);
  10438. ggml_tensor * k = build_lora_mm(layer.time_mix_key, xk);
  10439. ggml_tensor * v = build_lora_mm(layer.time_mix_value, xv);
  10440. if (layer.time_mix_receptance_b) {
  10441. r = ggml_add(ctx0, r, layer.time_mix_receptance_b);
  10442. }
  10443. if (layer.time_mix_key_b) {
  10444. k = ggml_add(ctx0, k, layer.time_mix_key_b);
  10445. }
  10446. if (layer.time_mix_value_b) {
  10447. v = ggml_add(ctx0, v, layer.time_mix_value_b);
  10448. }
  10449. ggml_tensor * g = build_lora_mm(layer.time_mix_gate, xg);
  10450. if (is_qrwkv) {
  10451. g = ggml_sigmoid(ctx0, g);
  10452. } else {
  10453. g = ggml_silu(ctx0, g);
  10454. }
  10455. if (n_head_kv != 0 && n_head_kv != n_head) {
  10456. GGML_ASSERT(n_head % n_head_kv == 0);
  10457. k = ggml_reshape_4d(ctx0, k, head_size, 1, n_head_kv, n_tokens);
  10458. v = ggml_reshape_4d(ctx0, v, head_size, 1, n_head_kv, n_tokens);
  10459. ggml_tensor * tmp = ggml_new_tensor_4d(ctx0, GGML_TYPE_F32, head_size, n_head / n_head_kv, n_head_kv, n_tokens);
  10460. k = ggml_repeat(ctx0, k, tmp);
  10461. v = ggml_repeat(ctx0, v, tmp);
  10462. }
  10463. k = ggml_reshape_3d(ctx0, k, head_size, n_head, n_tokens);
  10464. v = ggml_reshape_3d(ctx0, v, head_size, n_head, n_tokens);
  10465. r = ggml_reshape_3d(ctx0, r, head_size, n_head, n_tokens);
  10466. ggml_tensor * w = ggml_mul_mat(
  10467. ctx0,
  10468. layer.time_mix_decay_w2,
  10469. ggml_tanh(
  10470. ctx0,
  10471. ggml_mul_mat(ctx0, layer.time_mix_decay_w1, xw)
  10472. )
  10473. );
  10474. w = ggml_add(ctx0, w, layer.time_mix_decay);
  10475. w = ggml_exp(ctx0, ggml_neg(ctx0, ggml_exp(ctx0, w)));
  10476. w = ggml_reshape_3d(ctx0, w, head_size, n_head, n_tokens);
  10477. if (is_qrwkv) {
  10478. // k = k * (1 - w)
  10479. k = ggml_sub(ctx0, k, ggml_mul(ctx0, k, w));
  10480. }
  10481. ggml_tensor * wkv_state = build_rs(
  10482. inp, gf, mctx_cur->get_s_l(il),
  10483. hparams.n_embd_s(), n_seqs);
  10484. ggml_tensor * wkv_output;
  10485. if (is_qrwkv) {
  10486. wkv_output = ggml_gated_linear_attn(ctx0, k, v, r, w, wkv_state, pow(head_size, -0.5f));
  10487. } else {
  10488. wkv_output = ggml_rwkv_wkv6(ctx0, k, v, r, layer.time_mix_first, w, wkv_state);
  10489. }
  10490. cur = ggml_view_1d(ctx0, wkv_output, n_embd * n_tokens, 0);
  10491. wkv_state = ggml_view_1d(ctx0, wkv_output, n_embd * head_size * n_seqs, n_embd * n_tokens * sizeof(float));
  10492. ggml_build_forward_expand(
  10493. gf,
  10494. ggml_cpy(
  10495. ctx0,
  10496. wkv_state,
  10497. ggml_view_1d(
  10498. ctx0,
  10499. mctx_cur->get_s_l(il),
  10500. hparams.n_embd_s() * n_seqs,
  10501. hparams.n_embd_s() * kv_head * ggml_element_size(mctx_cur->get_s_l(il))
  10502. )
  10503. )
  10504. );
  10505. if (!is_qrwkv) {
  10506. // group norm with head_count groups
  10507. cur = ggml_reshape_3d(ctx0, cur, n_embd / n_head, n_head, n_tokens);
  10508. cur = ggml_norm(ctx0, cur, 64e-5f);
  10509. // Convert back to regular vectors.
  10510. cur = ggml_reshape_2d(ctx0, cur, n_embd, n_tokens);
  10511. cur = ggml_add(ctx0, ggml_mul(ctx0, cur, layer.time_mix_ln), layer.time_mix_ln_b);
  10512. } else {
  10513. cur = ggml_reshape_2d(ctx0, cur, n_embd, n_tokens);
  10514. }
  10515. cur = ggml_mul(ctx0, cur, g);
  10516. cur = build_lora_mm(layer.time_mix_output, cur);
  10517. return ggml_reshape_3d(ctx0, cur, n_embd, n_seq_tokens, n_seqs);
  10518. }
  10519. };
  10520. struct llm_build_rwkv6 : public llm_build_rwkv6_base {
  10521. llm_build_rwkv6(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_build_rwkv6_base(model, params) {
  10522. GGML_ASSERT(hparams.token_shift_count == 2);
  10523. ggml_tensor * cur;
  10524. ggml_tensor * inpL;
  10525. inpL = build_inp_embd(model.tok_embd);
  10526. inpL = build_norm(inpL, model.tok_norm, model.tok_norm_b, LLM_NORM, -1);
  10527. auto * rs_inp = build_rs_inp();
  10528. const auto n_embd = hparams.n_embd;
  10529. const auto n_seq_tokens = ubatch.n_seq_tokens;
  10530. const auto n_seqs = ubatch.n_seqs;
  10531. ggml_tensor * inp_out_ids = build_inp_out_ids();
  10532. for (int il = 0; il < n_layer; ++il) {
  10533. const llama_layer * layer = &model.layers[il];
  10534. inpL = ggml_reshape_3d(ctx0, inpL, n_embd, n_seq_tokens, n_seqs);
  10535. ggml_tensor * token_shift = build_rwkv_token_shift_load(rs_inp, gf, ubatch, il);
  10536. ggml_tensor * att_shift = ggml_view_3d(ctx0, token_shift, n_embd, 1, n_seqs, token_shift->nb[1], token_shift->nb[2], 0);
  10537. 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));
  10538. ggml_tensor * att_norm = build_norm(inpL, layer->attn_norm, layer->attn_norm_b, LLM_NORM, il);
  10539. cb(att_norm, "attn_norm", il);
  10540. ggml_tensor * x_prev = ggml_concat(
  10541. ctx0,
  10542. att_shift,
  10543. ggml_view_3d(ctx0, att_norm, n_embd, n_seq_tokens - 1, n_seqs, att_norm->nb[1], att_norm->nb[2], 0),
  10544. 1
  10545. );
  10546. cur = build_rwkv6_time_mix(rs_inp, gf, att_norm, x_prev, ubatch, il);
  10547. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
  10548. cb(ffn_inp, "ffn_inp", il);
  10549. ggml_tensor * ffn_norm = build_norm(ffn_inp, layer->attn_norm_2, layer->attn_norm_2_b, LLM_NORM, il);
  10550. cb(ffn_norm, "ffn_norm", il);
  10551. x_prev = ggml_concat(
  10552. ctx0,
  10553. ffn_shift,
  10554. ggml_view_3d(ctx0, ffn_norm, n_embd, n_seq_tokens - 1, n_seqs, ffn_norm->nb[1], ffn_norm->nb[2], 0),
  10555. 1
  10556. );
  10557. token_shift = ggml_concat(ctx0,
  10558. 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)),
  10559. 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)),
  10560. 1
  10561. );
  10562. ggml_build_forward_expand(gf, build_rwkv_token_shift_store(token_shift, ubatch, il));
  10563. ffn_inp = ggml_reshape_2d(ctx0, ffn_inp, n_embd, n_tokens);
  10564. ffn_norm = ggml_reshape_2d(ctx0, ffn_norm, n_embd, n_tokens);
  10565. x_prev = ggml_reshape_2d(ctx0, x_prev, n_embd, n_tokens);
  10566. cur = ggml_reshape_2d(ctx0, cur, n_embd, n_tokens);
  10567. if (il == n_layer - 1 && inp_out_ids) {
  10568. ffn_inp = ggml_get_rows(ctx0, ffn_inp, inp_out_ids);
  10569. ffn_norm = ggml_get_rows(ctx0, ffn_norm, inp_out_ids);
  10570. x_prev = ggml_get_rows(ctx0, x_prev, inp_out_ids);
  10571. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  10572. }
  10573. cur = build_rwkv6_channel_mix(layer, ffn_norm, x_prev, LLM_ARCH_RWKV6);
  10574. cur = ggml_add(ctx0, cur, ffn_inp);
  10575. if (hparams.rescale_every_n_layers != 0 && (il + 1) % hparams.rescale_every_n_layers == 0) {
  10576. cur = ggml_scale(ctx0, cur, 0.5F);
  10577. }
  10578. cur = build_cvec(cur, il);
  10579. cb(cur, "l_out", il);
  10580. // input for next layer
  10581. inpL = cur;
  10582. }
  10583. cur = inpL;
  10584. cur = build_norm(cur, model.output_norm, model.output_norm_b, LLM_NORM, -1);
  10585. cb(cur, "result_norm", -1);
  10586. res->t_embd = cur;
  10587. cur = build_lora_mm(model.output, cur);
  10588. cb(cur, "result_output", -1);
  10589. res->t_logits = cur;
  10590. ggml_build_forward_expand(gf, cur);
  10591. }
  10592. };
  10593. // ref: https://huggingface.co/recursal/QRWKV6-32B-Instruct-Preview-v0.1/blob/main/modeling_rwkv6qwen2.py
  10594. struct llm_build_rwkv6qwen2 : public llm_build_rwkv6_base {
  10595. llm_build_rwkv6qwen2(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_build_rwkv6_base(model, params) {
  10596. GGML_ASSERT(n_embd == hparams.n_embd_r());
  10597. ggml_tensor * cur;
  10598. ggml_tensor * inpL;
  10599. inpL = build_inp_embd(model.tok_embd);
  10600. auto * rs_inp = build_rs_inp();
  10601. const auto n_embd = hparams.n_embd;
  10602. const auto n_seq_tokens = ubatch.n_seq_tokens;
  10603. const auto n_seqs = ubatch.n_seqs;
  10604. ggml_tensor * inp_out_ids = build_inp_out_ids();
  10605. for (int il = 0; il < n_layer; ++il) {
  10606. const llama_layer * layer = &model.layers[il];
  10607. inpL = ggml_reshape_3d(ctx0, inpL, n_embd, n_seq_tokens, n_seqs);
  10608. ggml_tensor * token_shift = build_rwkv_token_shift_load(rs_inp, gf, ubatch, il);
  10609. ggml_tensor * att_norm = build_norm(inpL, layer->attn_norm, layer->attn_norm_b, LLM_NORM_RMS, il);
  10610. cb(att_norm, "attn_norm", il);
  10611. ggml_tensor * x_prev = ggml_concat(
  10612. ctx0,
  10613. token_shift,
  10614. ggml_view_3d(ctx0, att_norm, n_embd, n_seq_tokens - 1, n_seqs, att_norm->nb[1], att_norm->nb[2], 0),
  10615. 1
  10616. );
  10617. cur = build_rwkv6_time_mix(rs_inp, gf, att_norm, x_prev, ubatch, il);
  10618. 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));
  10619. ggml_build_forward_expand(gf, build_rwkv_token_shift_store(token_shift, ubatch, il));
  10620. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
  10621. cb(ffn_inp, "ffn_inp", il);
  10622. cur = ggml_reshape_2d(ctx0, cur, n_embd, n_tokens);
  10623. ffn_inp = ggml_reshape_2d(ctx0, ffn_inp, n_embd, n_tokens);
  10624. if (il == n_layer - 1 && inp_out_ids) {
  10625. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  10626. ffn_inp = ggml_get_rows(ctx0, ffn_inp, inp_out_ids);
  10627. }
  10628. // feed-forward network
  10629. cur = build_norm(ffn_inp,
  10630. model.layers[il].ffn_norm, NULL,
  10631. LLM_NORM_RMS, il);
  10632. cb(cur, "ffn_norm", il);
  10633. cur = build_ffn(cur,
  10634. model.layers[il].ffn_up, NULL, NULL,
  10635. model.layers[il].ffn_gate, NULL, NULL,
  10636. model.layers[il].ffn_down, NULL, NULL,
  10637. NULL,
  10638. LLM_FFN_SILU, LLM_FFN_PAR, il);
  10639. cb(cur, "ffn_out", il);
  10640. cur = ggml_add(ctx0, cur, ffn_inp);
  10641. cur = build_cvec(cur, il);
  10642. cb(cur, "l_out", il);
  10643. // input for next layer
  10644. inpL = cur;
  10645. }
  10646. cur = inpL;
  10647. cur = build_norm(cur, model.output_norm, model.output_norm_b, LLM_NORM_RMS, -1);
  10648. cb(cur, "result_norm", -1);
  10649. res->t_embd = cur;
  10650. cur = build_lora_mm(model.output, cur);
  10651. cb(cur, "result_output", -1);
  10652. res->t_logits = cur;
  10653. ggml_build_forward_expand(gf, cur);
  10654. }
  10655. };
  10656. struct llm_build_rwkv7_base : public llm_graph_context {
  10657. const llama_model & model;
  10658. llm_build_rwkv7_base(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params), model(model) {
  10659. }
  10660. ggml_tensor * build_rwkv7_channel_mix(
  10661. const llama_layer * layer,
  10662. ggml_tensor * cur,
  10663. ggml_tensor * x_prev,
  10664. llm_arch arch) const {
  10665. ggml_tensor * sx = ggml_sub(ctx0, x_prev, cur);
  10666. switch (arch) {
  10667. case LLM_ARCH_RWKV7:
  10668. {
  10669. ggml_tensor * xk = ggml_add(ctx0, ggml_mul(ctx0, sx, layer->channel_mix_lerp_k), cur);
  10670. ggml_tensor * k = ggml_sqr(
  10671. ctx0,
  10672. ggml_relu(
  10673. ctx0,
  10674. build_lora_mm(layer->channel_mix_key, xk)
  10675. )
  10676. );
  10677. cur = build_lora_mm(layer->channel_mix_value, k);
  10678. } break;
  10679. default:
  10680. GGML_ABORT("fatal error");
  10681. }
  10682. return cur;
  10683. }
  10684. ggml_tensor * build_rwkv7_time_mix(
  10685. llm_graph_input_rs * inp,
  10686. ggml_cgraph * gf,
  10687. ggml_tensor * cur,
  10688. ggml_tensor * x_prev,
  10689. ggml_tensor *& first_layer_value,
  10690. const llama_ubatch & ubatch,
  10691. int il) const {
  10692. const auto * mctx_cur = static_cast<const llama_memory_recurrent_context *>(mctx);
  10693. const auto n_tokens = ubatch.n_tokens;
  10694. const auto n_seqs = ubatch.n_seqs;
  10695. const auto n_embd = hparams.n_embd;
  10696. const auto head_size = hparams.wkv_head_size;
  10697. const auto head_count = n_embd / head_size;
  10698. const auto n_seq_tokens = ubatch.n_seq_tokens;
  10699. const auto kv_head = mctx_cur->get_head();
  10700. const auto & layer = model.layers[il];
  10701. bool has_gating = layer.time_mix_g1 && layer.time_mix_g2;
  10702. ggml_tensor * sx = ggml_sub(ctx0, x_prev, cur);
  10703. ggml_tensor * dummy = ggml_new_tensor_4d(ctx0, GGML_TYPE_F32, n_embd, n_seq_tokens, n_seqs, has_gating ? 6 : 5);
  10704. sx = ggml_repeat(ctx0, sx, dummy);
  10705. ggml_tensor * xxx = ggml_add(ctx0, ggml_mul(ctx0, sx, layer.time_mix_lerp_fused), cur);
  10706. ggml_tensor * xr = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], 0);
  10707. ggml_tensor * xw = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], n_embd * n_tokens * sizeof(float));
  10708. ggml_tensor * xk = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], n_embd * n_tokens * 2 * sizeof(float));
  10709. ggml_tensor * xv = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], n_embd * n_tokens * 3 * sizeof(float));
  10710. ggml_tensor * xa = ggml_view_2d(ctx0, xxx, n_embd, n_tokens, xxx->nb[1], n_embd * n_tokens * 4 * sizeof(float));
  10711. 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;
  10712. ggml_tensor * r = build_lora_mm(layer.time_mix_receptance, xr);
  10713. ggml_tensor * w = ggml_add(
  10714. ctx0,
  10715. ggml_mul_mat(ctx0, layer.time_mix_w2, ggml_tanh(ctx0, ggml_mul_mat(ctx0, layer.time_mix_w1, xw))),
  10716. layer.time_mix_w0
  10717. );
  10718. w = ggml_exp(ctx0, ggml_scale(ctx0, ggml_sigmoid(ctx0, w), -0.606531));
  10719. ggml_tensor * k = build_lora_mm(layer.time_mix_key, xk);
  10720. ggml_tensor * v = build_lora_mm(layer.time_mix_value, xv);
  10721. if (first_layer_value == nullptr) {
  10722. first_layer_value = v;
  10723. } else {
  10724. // Add the first layer value as a residual connection.
  10725. v = ggml_add(ctx0, v,
  10726. ggml_mul(ctx0,
  10727. ggml_sub(ctx0, first_layer_value, v),
  10728. ggml_sigmoid(ctx0, ggml_add(ctx0,
  10729. ggml_mul_mat(ctx0, layer.time_mix_v2, ggml_mul_mat(ctx0, layer.time_mix_v1, xv)),
  10730. layer.time_mix_v0
  10731. )
  10732. )
  10733. )
  10734. );
  10735. }
  10736. ggml_tensor * g = nullptr;
  10737. if (layer.time_mix_g1 && layer.time_mix_g2) {
  10738. g = ggml_mul_mat(ctx0, layer.time_mix_g2, ggml_sigmoid(ctx0, ggml_mul_mat(ctx0, layer.time_mix_g1, xg)));
  10739. }
  10740. ggml_tensor * a = ggml_sigmoid(ctx0,
  10741. ggml_add(
  10742. ctx0,
  10743. ggml_mul_mat(ctx0, layer.time_mix_a2, ggml_mul_mat(ctx0, layer.time_mix_a1, xa)),
  10744. layer.time_mix_a0
  10745. )
  10746. );
  10747. ggml_tensor * kk = ggml_reshape_3d(ctx0, ggml_mul(ctx0, k, layer.time_mix_k_k), head_size, head_count, n_tokens);
  10748. kk = ggml_l2_norm(ctx0, kk, 1e-12);
  10749. ggml_tensor * ka = ggml_mul(ctx0, k, layer.time_mix_k_a);
  10750. k = ggml_add(ctx0, k, ggml_sub(ctx0, ggml_mul(ctx0, a, ka), ka));
  10751. r = ggml_reshape_3d(ctx0, r, head_size, head_count, n_tokens);
  10752. w = ggml_reshape_3d(ctx0, w, head_size, head_count, n_tokens);
  10753. k = ggml_reshape_3d(ctx0, k, head_size, head_count, n_tokens);
  10754. v = ggml_reshape_3d(ctx0, v, head_size, head_count, n_tokens);
  10755. a = ggml_reshape_3d(ctx0, a, head_size, head_count, n_tokens);
  10756. ggml_tensor * wkv_state = build_rs(
  10757. inp, gf, mctx_cur->get_s_l(il),
  10758. hparams.n_embd_s(), n_seqs);
  10759. ggml_tensor * wkv_output = ggml_rwkv_wkv7(ctx0, r, w, k, v, ggml_neg(ctx0, kk), ggml_mul(ctx0, kk, a), wkv_state);
  10760. cur = ggml_view_1d(ctx0, wkv_output, n_embd * n_tokens, 0);
  10761. wkv_state = ggml_view_1d(ctx0, wkv_output, n_embd * head_size * n_seqs, n_embd * n_tokens * sizeof(float));
  10762. ggml_build_forward_expand(
  10763. gf,
  10764. ggml_cpy(
  10765. ctx0,
  10766. wkv_state,
  10767. ggml_view_1d(
  10768. ctx0,
  10769. mctx_cur->get_s_l(il),
  10770. hparams.n_embd_s() * n_seqs,
  10771. hparams.n_embd_s() * kv_head * ggml_element_size(mctx_cur->get_s_l(il))
  10772. )
  10773. )
  10774. );
  10775. if (layer.time_mix_ln && layer.time_mix_ln_b) {
  10776. // group norm with head_count groups
  10777. cur = ggml_reshape_3d(ctx0, cur, n_embd / head_count, head_count, n_tokens);
  10778. cur = ggml_norm(ctx0, cur, 64e-5f);
  10779. // Convert back to regular vectors.
  10780. cur = ggml_reshape_2d(ctx0, cur, n_embd, n_tokens);
  10781. cur = ggml_add(ctx0, ggml_mul(ctx0, cur, layer.time_mix_ln), layer.time_mix_ln_b);
  10782. } else {
  10783. cur = ggml_reshape_2d(ctx0, cur, n_embd, n_tokens);
  10784. }
  10785. ggml_tensor * rk = ggml_sum_rows(ctx0,
  10786. ggml_mul(ctx0, ggml_mul(ctx0, k, r), ggml_reshape_2d(ctx0, layer.time_mix_r_k, head_size, head_count)));
  10787. cur = ggml_add(ctx0, cur, ggml_reshape_2d(ctx0, ggml_mul(ctx0, v, rk), n_embd, n_tokens));
  10788. if (has_gating) {
  10789. cur = ggml_mul(ctx0, cur, g);
  10790. }
  10791. cur = build_lora_mm(layer.time_mix_output, cur);
  10792. return ggml_reshape_3d(ctx0, cur, n_embd, n_seq_tokens, n_seqs);
  10793. }
  10794. };
  10795. struct llm_build_rwkv7 : public llm_build_rwkv7_base {
  10796. llm_build_rwkv7(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_build_rwkv7_base(model, params) {
  10797. GGML_ASSERT(hparams.token_shift_count == 2);
  10798. ggml_tensor * cur;
  10799. ggml_tensor * inpL;
  10800. ggml_tensor * v_first = nullptr;
  10801. inpL = build_inp_embd(model.tok_embd);
  10802. inpL = build_norm(inpL, model.tok_norm, model.tok_norm_b, LLM_NORM, -1);
  10803. auto * rs_inp = build_rs_inp();
  10804. const auto n_embd = hparams.n_embd;
  10805. const auto n_seq_tokens = ubatch.n_seq_tokens;
  10806. const auto n_seqs = ubatch.n_seqs;
  10807. ggml_tensor * inp_out_ids = build_inp_out_ids();
  10808. for (int il = 0; il < n_layer; ++il) {
  10809. const llama_layer * layer = &model.layers[il];
  10810. inpL = ggml_reshape_3d(ctx0, inpL, n_embd, n_seq_tokens, n_seqs);
  10811. ggml_tensor * token_shift = build_rwkv_token_shift_load(rs_inp, gf, ubatch, il);
  10812. ggml_tensor * att_shift = ggml_view_3d(ctx0, token_shift, n_embd, 1, n_seqs, token_shift->nb[1], token_shift->nb[2], 0);
  10813. 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));
  10814. ggml_tensor * att_norm = build_norm(inpL, layer->attn_norm, layer->attn_norm_b, LLM_NORM, il);
  10815. cb(att_norm, "attn_norm", il);
  10816. ggml_tensor * x_prev = ggml_concat(
  10817. ctx0,
  10818. att_shift,
  10819. ggml_view_3d(ctx0, att_norm, n_embd, n_seq_tokens - 1, n_seqs, att_norm->nb[1], att_norm->nb[2], 0),
  10820. 1
  10821. );
  10822. cur = build_rwkv7_time_mix(rs_inp, gf, att_norm, x_prev, v_first, ubatch, il);
  10823. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
  10824. cb(ffn_inp, "ffn_inp", il);
  10825. ggml_tensor * ffn_norm = build_norm(ffn_inp, layer->attn_norm_2, layer->attn_norm_2_b, LLM_NORM, il);
  10826. cb(ffn_norm, "ffn_norm", il);
  10827. x_prev = ggml_concat(
  10828. ctx0,
  10829. ffn_shift,
  10830. ggml_view_3d(ctx0, ffn_norm, n_embd, n_seq_tokens - 1, n_seqs, ffn_norm->nb[1], ffn_norm->nb[2], 0),
  10831. 1
  10832. );
  10833. token_shift = ggml_concat(ctx0,
  10834. 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)),
  10835. 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)),
  10836. 1
  10837. );
  10838. ggml_build_forward_expand(gf, build_rwkv_token_shift_store(token_shift, ubatch, il));
  10839. ffn_inp = ggml_reshape_2d(ctx0, ffn_inp, n_embd, n_tokens);
  10840. ffn_norm = ggml_reshape_2d(ctx0, ffn_norm, n_embd, n_tokens);
  10841. x_prev = ggml_reshape_2d(ctx0, x_prev, n_embd, n_tokens);
  10842. if (il == n_layer - 1 && inp_out_ids) {
  10843. ffn_inp = ggml_get_rows(ctx0, ffn_inp, inp_out_ids);
  10844. ffn_norm = ggml_get_rows(ctx0, ffn_norm, inp_out_ids);
  10845. x_prev = ggml_get_rows(ctx0, x_prev, inp_out_ids);
  10846. }
  10847. cur = build_rwkv7_channel_mix(layer, ffn_norm, x_prev, LLM_ARCH_RWKV7);
  10848. cur = ggml_add(ctx0, cur, ffn_inp);
  10849. cur = build_cvec(cur, il);
  10850. cb(cur, "l_out", il);
  10851. // input for next layer
  10852. inpL = cur;
  10853. }
  10854. cur = inpL;
  10855. cur = build_norm(cur, model.output_norm, model.output_norm_b, LLM_NORM, -1);
  10856. cb(cur, "result_norm", -1);
  10857. res->t_embd = cur;
  10858. cur = build_lora_mm(model.output, cur);
  10859. cb(cur, "result_output", -1);
  10860. res->t_logits = cur;
  10861. ggml_build_forward_expand(gf, cur);
  10862. }
  10863. };
  10864. struct llm_build_arwkv7 : public llm_build_rwkv7_base {
  10865. llm_build_arwkv7(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_build_rwkv7_base(model, params) {
  10866. GGML_ASSERT(n_embd == hparams.n_embd_r());
  10867. ggml_tensor * cur;
  10868. ggml_tensor * inpL;
  10869. ggml_tensor * v_first = nullptr;
  10870. inpL = build_inp_embd(model.tok_embd);
  10871. auto * rs_inp = build_rs_inp();
  10872. const auto n_embd = hparams.n_embd;
  10873. const auto n_seq_tokens = ubatch.n_seq_tokens;
  10874. const auto n_seqs = ubatch.n_seqs;
  10875. ggml_tensor * inp_out_ids = build_inp_out_ids();
  10876. for (int il = 0; il < n_layer; ++il) {
  10877. const llama_layer * layer = &model.layers[il];
  10878. inpL = ggml_reshape_3d(ctx0, inpL, n_embd, n_seq_tokens, n_seqs);
  10879. ggml_tensor * token_shift = build_rwkv_token_shift_load(rs_inp, gf, ubatch, il);
  10880. ggml_tensor * att_norm = build_norm(inpL, layer->attn_norm, layer->attn_norm_b, LLM_NORM_RMS, il);
  10881. cb(att_norm, "attn_norm", il);
  10882. ggml_tensor * x_prev = ggml_concat(
  10883. ctx0,
  10884. token_shift,
  10885. ggml_view_3d(ctx0, att_norm, n_embd, n_seq_tokens - 1, n_seqs, att_norm->nb[1], att_norm->nb[2], 0),
  10886. 1
  10887. );
  10888. cur = build_rwkv7_time_mix(rs_inp, gf, att_norm, x_prev, v_first, ubatch, il);
  10889. 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));
  10890. ggml_build_forward_expand(gf, build_rwkv_token_shift_store(token_shift, ubatch, il));
  10891. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
  10892. cb(ffn_inp, "ffn_inp", il);
  10893. cur = ggml_reshape_2d(ctx0, cur, n_embd, n_tokens);
  10894. ffn_inp = ggml_reshape_2d(ctx0, ffn_inp, n_embd, n_tokens);
  10895. if (il == n_layer - 1 && inp_out_ids) {
  10896. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  10897. ffn_inp = ggml_get_rows(ctx0, ffn_inp, inp_out_ids);
  10898. }
  10899. // feed-forward network
  10900. cur = build_norm(ffn_inp,
  10901. model.layers[il].ffn_norm, NULL,
  10902. LLM_NORM_RMS, il);
  10903. cb(cur, "ffn_norm", il);
  10904. cur = build_ffn(cur,
  10905. model.layers[il].ffn_up, NULL, NULL,
  10906. model.layers[il].ffn_gate, NULL, NULL,
  10907. model.layers[il].ffn_down, NULL, NULL,
  10908. NULL,
  10909. LLM_FFN_SILU, LLM_FFN_PAR, il);
  10910. cb(cur, "ffn_out", il);
  10911. cur = ggml_add(ctx0, cur, ffn_inp);
  10912. cur = build_cvec(cur, il);
  10913. cb(cur, "l_out", il);
  10914. // input for next layer
  10915. inpL = cur;
  10916. }
  10917. cur = inpL;
  10918. cur = build_norm(cur, model.output_norm, model.output_norm_b, LLM_NORM_RMS, -1);
  10919. cb(cur, "result_norm", -1);
  10920. res->t_embd = cur;
  10921. cur = build_lora_mm(model.output, cur);
  10922. cb(cur, "result_output", -1);
  10923. res->t_logits = cur;
  10924. ggml_build_forward_expand(gf, cur);
  10925. }
  10926. };
  10927. struct llm_build_granite : public llm_graph_context {
  10928. llm_build_granite(
  10929. const llama_model & model,
  10930. const llm_graph_params & params,
  10931. ggml_cgraph * gf,
  10932. const bool use_rope = true)
  10933. : llm_graph_context(params) {
  10934. const int64_t n_embd_head = hparams.n_embd_head_v;
  10935. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  10936. GGML_ASSERT(n_embd_head == hparams.n_rot);
  10937. ggml_tensor * cur;
  10938. ggml_tensor * inpL;
  10939. inpL = build_inp_embd(model.tok_embd);
  10940. // inp_pos - built only if rope enabled
  10941. ggml_tensor * inp_pos = nullptr;
  10942. if (use_rope) {
  10943. inp_pos = build_inp_pos();
  10944. }
  10945. auto * inp_attn = build_attn_inp_kv_unified();
  10946. const float kq_scale = hparams.f_attention_scale == 0.0f ? 1.0f/sqrtf(float(n_embd_head)) : hparams.f_attention_scale;
  10947. ggml_tensor * inp_out_ids = build_inp_out_ids();
  10948. for (int il = 0; il < n_layer; ++il) {
  10949. ggml_tensor * inpSA = inpL;
  10950. // norm
  10951. cur = build_norm(inpL,
  10952. model.layers[il].attn_norm, NULL,
  10953. LLM_NORM_RMS, il);
  10954. cb(cur, "attn_norm", il);
  10955. // self-attention
  10956. {
  10957. // compute Q and K and (optionally) RoPE them
  10958. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  10959. cb(Qcur, "Qcur", il);
  10960. if (model.layers[il].bq) {
  10961. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  10962. cb(Qcur, "Qcur", il);
  10963. }
  10964. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  10965. cb(Kcur, "Kcur", il);
  10966. if (model.layers[il].bk) {
  10967. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  10968. cb(Kcur, "Kcur", il);
  10969. }
  10970. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  10971. cb(Vcur, "Vcur", il);
  10972. if (model.layers[il].bv) {
  10973. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  10974. cb(Vcur, "Vcur", il);
  10975. }
  10976. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  10977. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  10978. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  10979. if (use_rope) {
  10980. ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
  10981. Qcur = ggml_rope_ext(
  10982. ctx0, Qcur, inp_pos, rope_factors,
  10983. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  10984. ext_factor, attn_factor, beta_fast, beta_slow
  10985. );
  10986. Kcur = ggml_rope_ext(
  10987. ctx0, Kcur, inp_pos, rope_factors,
  10988. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  10989. ext_factor, attn_factor, beta_fast, beta_slow
  10990. );
  10991. }
  10992. cb(Qcur, "Qcur", il);
  10993. cb(Kcur, "Kcur", il);
  10994. cb(Vcur, "Vcur", il);
  10995. cur = build_attn(inp_attn, gf,
  10996. model.layers[il].wo, model.layers[il].bo,
  10997. Qcur, Kcur, Vcur, nullptr, nullptr, kq_scale, il);
  10998. cb(cur, "attn_out", il);
  10999. }
  11000. if (il == n_layer - 1 && inp_out_ids) {
  11001. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  11002. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  11003. }
  11004. // For Granite architectures - scale residual
  11005. cur = ggml_scale(ctx0, cur, hparams.f_residual_scale);
  11006. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  11007. cb(ffn_inp, "ffn_inp", il);
  11008. // feed-forward network (non-MoE)
  11009. if (model.layers[il].ffn_gate_inp == nullptr) {
  11010. cur = build_norm(ffn_inp,
  11011. model.layers[il].ffn_norm, NULL,
  11012. LLM_NORM_RMS, il);
  11013. cb(cur, "ffn_norm", il);
  11014. cur = build_ffn(cur,
  11015. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  11016. model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, NULL,
  11017. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  11018. NULL,
  11019. LLM_FFN_SILU, LLM_FFN_PAR, il);
  11020. cb(cur, "ffn_out", il);
  11021. } else {
  11022. // MoE branch
  11023. cur = build_norm(ffn_inp,
  11024. model.layers[il].ffn_norm, NULL,
  11025. LLM_NORM_RMS, il);
  11026. cb(cur, "ffn_norm", il);
  11027. ggml_tensor * moe_out = build_moe_ffn(cur,
  11028. model.layers[il].ffn_gate_inp,
  11029. model.layers[il].ffn_up_exps,
  11030. model.layers[il].ffn_gate_exps,
  11031. model.layers[il].ffn_down_exps,
  11032. nullptr,
  11033. n_expert, n_expert_used,
  11034. LLM_FFN_SILU, true,
  11035. false, 0.0,
  11036. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  11037. il);
  11038. cb(moe_out, "ffn_moe_out", il);
  11039. // For Granite MoE Shared
  11040. if (hparams.n_ff_shexp > 0) {
  11041. ggml_tensor * ffn_shexp = build_ffn(cur,
  11042. model.layers[il].ffn_up_shexp, NULL, NULL,
  11043. model.layers[il].ffn_gate_shexp, NULL, NULL,
  11044. model.layers[il].ffn_down_shexp, NULL, NULL,
  11045. NULL,
  11046. LLM_FFN_SILU, LLM_FFN_PAR, il);
  11047. cb(ffn_shexp, "ffn_shexp", il);
  11048. cur = ggml_add(ctx0, moe_out, ffn_shexp);
  11049. cb(cur, "ffn_out", il);
  11050. } else {
  11051. cur = moe_out;
  11052. }
  11053. }
  11054. // For Granite architectures - scale residual
  11055. cur = ggml_scale(ctx0, cur, hparams.f_residual_scale);
  11056. cur = ggml_add(ctx0, cur, ffn_inp);
  11057. cb(cur, "ffn_out", il);
  11058. cur = build_cvec(cur, il);
  11059. cb(cur, "l_out", il);
  11060. // input for next layer
  11061. inpL = cur;
  11062. }
  11063. cur = inpL;
  11064. cur = build_norm(cur,
  11065. model.output_norm, NULL,
  11066. LLM_NORM_RMS, -1);
  11067. cb(cur, "result_norm", -1);
  11068. res->t_embd = cur;
  11069. // lm_head
  11070. cur = build_lora_mm(model.output, cur);
  11071. // For Granite architectures - scale logits
  11072. cur = ggml_scale(ctx0, cur, 1.0f / hparams.f_logit_scale);
  11073. cb(cur, "result_output", -1);
  11074. res->t_logits = cur;
  11075. ggml_build_forward_expand(gf, cur);
  11076. }
  11077. };
  11078. // ref: https://github.com/facebookresearch/chameleon
  11079. // based on the original build_llama() function, changes:
  11080. // * qk-norm
  11081. // * swin-norm
  11082. // * removed bias
  11083. // * removed MoE
  11084. struct llm_build_chameleon : public llm_graph_context {
  11085. llm_build_chameleon(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
  11086. const int64_t n_embd_head = hparams.n_embd_head_v;
  11087. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  11088. GGML_ASSERT(n_embd_head == hparams.n_rot);
  11089. ggml_tensor * cur;
  11090. ggml_tensor * inpL;
  11091. inpL = build_inp_embd(model.tok_embd);
  11092. // inp_pos - contains the positions
  11093. ggml_tensor * inp_pos = build_inp_pos();
  11094. auto * inp_attn = build_attn_inp_kv_unified();
  11095. ggml_tensor * inp_out_ids = build_inp_out_ids();
  11096. for (int il = 0; il < n_layer; ++il) {
  11097. ggml_tensor * inpSA = inpL;
  11098. // norm
  11099. if (hparams.swin_norm) {
  11100. cur = inpL;
  11101. } else {
  11102. cur = build_norm(inpL,
  11103. model.layers[il].attn_norm, NULL,
  11104. LLM_NORM_RMS, il);
  11105. cb(cur, "attn_norm", il);
  11106. }
  11107. // self-attention
  11108. {
  11109. // compute Q and K and RoPE them
  11110. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  11111. cb(Qcur, "Qcur", il);
  11112. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  11113. cb(Kcur, "Kcur", il);
  11114. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  11115. cb(Vcur, "Vcur", il);
  11116. if (model.layers[il].attn_q_norm) {
  11117. Qcur = ggml_view_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens,
  11118. ggml_element_size(Qcur) * n_embd_head,
  11119. ggml_element_size(Qcur) * n_embd_head * n_head,
  11120. 0);
  11121. cb(Qcur, "Qcur", il);
  11122. Qcur = build_norm(Qcur,
  11123. model.layers[il].attn_q_norm,
  11124. model.layers[il].attn_q_norm_b,
  11125. LLM_NORM, il);
  11126. cb(Qcur, "Qcur", il);
  11127. }
  11128. if (model.layers[il].attn_k_norm) {
  11129. Kcur = ggml_view_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens,
  11130. ggml_element_size(Kcur) * n_embd_head,
  11131. ggml_element_size(Kcur) * n_embd_head * n_head_kv,
  11132. 0);
  11133. cb(Kcur, "Kcur", il);
  11134. Kcur = build_norm(Kcur,
  11135. model.layers[il].attn_k_norm,
  11136. model.layers[il].attn_k_norm_b,
  11137. LLM_NORM, il);
  11138. cb(Kcur, "Kcur", il);
  11139. }
  11140. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  11141. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  11142. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  11143. Qcur = ggml_rope_ext(
  11144. ctx0, Qcur, inp_pos, nullptr,
  11145. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  11146. ext_factor, attn_factor, beta_fast, beta_slow
  11147. );
  11148. Kcur = ggml_rope_ext(
  11149. ctx0, Kcur, inp_pos, nullptr,
  11150. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  11151. ext_factor, attn_factor, beta_fast, beta_slow
  11152. );
  11153. cb(Qcur, "Qcur", il);
  11154. cb(Kcur, "Kcur", il);
  11155. cb(Vcur, "Vcur", il);
  11156. cur = build_attn(inp_attn, gf,
  11157. model.layers[il].wo, nullptr,
  11158. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  11159. }
  11160. if (il == n_layer - 1 && inp_out_ids) {
  11161. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  11162. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  11163. }
  11164. if (hparams.swin_norm) {
  11165. cur = build_norm(cur,
  11166. model.layers[il].attn_norm, NULL,
  11167. LLM_NORM_RMS, il);
  11168. }
  11169. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  11170. cb(ffn_inp, "ffn_inp", il);
  11171. // feed-forward network
  11172. if (!hparams.swin_norm) {
  11173. cur = build_norm(ffn_inp,
  11174. model.layers[il].ffn_norm, NULL,
  11175. LLM_NORM_RMS, il);
  11176. cb(cur, "ffn_norm", il);
  11177. }
  11178. cur = build_ffn(cur,
  11179. model.layers[il].ffn_up, NULL, NULL,
  11180. model.layers[il].ffn_gate, NULL, NULL,
  11181. model.layers[il].ffn_down, NULL, NULL,
  11182. NULL,
  11183. LLM_FFN_SILU, LLM_FFN_PAR, il);
  11184. cb(cur, "ffn_out", il);
  11185. if (hparams.swin_norm) {
  11186. cur = build_norm(cur,
  11187. model.layers[il].ffn_norm, NULL,
  11188. LLM_NORM_RMS, il);
  11189. cb(cur, "ffn_norm", il);
  11190. }
  11191. cur = ggml_add(ctx0, cur, ffn_inp);
  11192. cb(cur, "ffn_out", il);
  11193. cur = build_cvec(cur, il);
  11194. cb(cur, "l_out", il);
  11195. // input for next layer
  11196. inpL = cur;
  11197. }
  11198. cur = inpL;
  11199. cur = build_norm(cur,
  11200. model.output_norm, NULL,
  11201. LLM_NORM_RMS, -1);
  11202. cb(cur, "result_norm", -1);
  11203. res->t_embd = cur;
  11204. // lm_head
  11205. cur = build_lora_mm(model.output, cur);
  11206. cb(cur, "result_output_with_img_logits", -1);
  11207. // TODO: this suppresses the output of image tokens, which is required to enable text-only outputs.
  11208. // Needs to be removed once image outputs are supported.
  11209. int img_token_end_idx = 8196;
  11210. int img_token_start_idx = 4;
  11211. int num_img_tokens = img_token_end_idx - img_token_start_idx;
  11212. // creates 1d tensor of size num_img_tokens and values -FLT_MAX,
  11213. // which ensures that text token values are always at least larger than image token values
  11214. ggml_tensor * img_logits = ggml_new_tensor_1d(ctx0, GGML_TYPE_F32, num_img_tokens);
  11215. img_logits = ggml_clamp(ctx0, img_logits, -FLT_MAX, -FLT_MAX);
  11216. cb(img_logits, "img_logits", -1);
  11217. cur = ggml_set_1d(ctx0, cur, img_logits, ggml_element_size(cur) * img_token_start_idx);
  11218. cb(cur, "result_output", -1);
  11219. res->t_logits = cur;
  11220. ggml_build_forward_expand(gf, cur);
  11221. }
  11222. };
  11223. struct llm_build_wavtokenizer_dec : public llm_graph_context {
  11224. llm_build_wavtokenizer_dec(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
  11225. ggml_tensor * cur;
  11226. ggml_tensor * inpL;
  11227. inpL = build_inp_embd(model.tok_embd);
  11228. cur = ggml_cont(ctx0, ggml_transpose(ctx0, inpL));
  11229. cur = ggml_conv_1d_ph(ctx0, model.conv1d, cur, 1, 1);
  11230. cur = ggml_add(ctx0, cur, model.conv1d_b);
  11231. // posnet
  11232. for (uint32_t il = 0; il < hparams.posnet.n_layer; ++il) {
  11233. const auto & layer = model.layers[il].posnet;
  11234. inpL = cur;
  11235. switch (il) {
  11236. case 0:
  11237. case 1:
  11238. case 3:
  11239. case 4:
  11240. {
  11241. cur = build_norm(cur,
  11242. layer.norm1,
  11243. layer.norm1_b,
  11244. LLM_NORM_GROUP, 0);
  11245. cur = ggml_mul(ctx0, ggml_sigmoid(ctx0, cur), cur);
  11246. cur = ggml_conv_1d_ph(ctx0, layer.conv1, cur, 1, 1);
  11247. cur = ggml_add(ctx0, cur, layer.conv1_b);
  11248. cur = build_norm(cur,
  11249. layer.norm2,
  11250. layer.norm2_b,
  11251. LLM_NORM_GROUP, 0);
  11252. cur = ggml_mul(ctx0, ggml_sigmoid(ctx0, cur), cur);
  11253. cur = ggml_conv_1d_ph(ctx0, layer.conv2, cur, 1, 1);
  11254. cur = ggml_add(ctx0, cur, layer.conv2_b);
  11255. cur = ggml_add(ctx0, cur, inpL);
  11256. } break;
  11257. case 2:
  11258. {
  11259. cur = build_norm(cur,
  11260. layer.attn_norm,
  11261. layer.attn_norm_b,
  11262. LLM_NORM_GROUP, 0);
  11263. ggml_tensor * q;
  11264. ggml_tensor * k;
  11265. ggml_tensor * v;
  11266. q = ggml_conv_1d_ph(ctx0, layer.attn_q, cur, 1, 1);
  11267. k = ggml_conv_1d_ph(ctx0, layer.attn_k, cur, 1, 1);
  11268. v = ggml_conv_1d_ph(ctx0, layer.attn_v, cur, 1, 1);
  11269. q = ggml_add(ctx0, q, layer.attn_q_b);
  11270. k = ggml_add(ctx0, k, layer.attn_k_b);
  11271. v = ggml_add(ctx0, v, layer.attn_v_b);
  11272. q = ggml_cont(ctx0, ggml_transpose(ctx0, q));
  11273. k = ggml_cont(ctx0, ggml_transpose(ctx0, k));
  11274. ggml_tensor * kq = ggml_mul_mat(ctx0, k, q);
  11275. kq = ggml_soft_max_ext(ctx0, kq, nullptr, 1.0f/sqrtf(float(hparams.posnet.n_embd)), 0.0f);
  11276. cur = ggml_mul_mat(ctx0, kq, v);
  11277. cur = ggml_conv_1d_ph(ctx0, layer.attn_o, cur, 1, 1);
  11278. cur = ggml_add(ctx0, cur, layer.attn_o_b);
  11279. cur = ggml_add(ctx0, cur, inpL);
  11280. } break;
  11281. case 5:
  11282. {
  11283. cur = build_norm(cur,
  11284. layer.norm,
  11285. layer.norm_b,
  11286. LLM_NORM_GROUP, 0);
  11287. } break;
  11288. default: GGML_ABORT("unknown posnet layer");
  11289. };
  11290. }
  11291. cur = ggml_cont(ctx0, ggml_transpose(ctx0, cur));
  11292. cur = build_norm(cur,
  11293. model.tok_norm,
  11294. model.tok_norm_b,
  11295. LLM_NORM, -1);
  11296. cur = ggml_cont(ctx0, ggml_transpose(ctx0, cur));
  11297. inpL = cur;
  11298. // convnext
  11299. for (uint32_t il = 0; il < hparams.convnext.n_layer; ++il) {
  11300. const auto & layer = model.layers[il].convnext;
  11301. cur = inpL;
  11302. cur = ggml_conv_1d_dw_ph(ctx0, layer.dw, cur, 1, 1);
  11303. cur = ggml_add(ctx0, cur, layer.dw_b);
  11304. cur = ggml_cont(ctx0, ggml_transpose(ctx0, cur));
  11305. cur = build_norm(cur,
  11306. layer.norm,
  11307. layer.norm_b,
  11308. LLM_NORM, -1);
  11309. cur = build_ffn(cur,
  11310. layer.pw1, layer.pw1_b, NULL,
  11311. NULL, NULL, NULL,
  11312. layer.pw2, layer.pw2_b, NULL,
  11313. NULL,
  11314. LLM_FFN_GELU, LLM_FFN_SEQ, il);
  11315. cur = ggml_mul(ctx0, cur, layer.gamma);
  11316. cur = ggml_cont(ctx0, ggml_transpose(ctx0, cur));
  11317. inpL = ggml_add(ctx0, cur, inpL);
  11318. }
  11319. cur = inpL;
  11320. cur = ggml_cont(ctx0, ggml_transpose(ctx0, cur));
  11321. cur = build_norm(cur,
  11322. model.output_norm,
  11323. model.output_norm_b,
  11324. LLM_NORM, -1);
  11325. // lm_head
  11326. cur = build_lora_mm(model.output, cur);
  11327. cur = ggml_add(ctx0, cur, model.output_b);
  11328. cb(cur, "result_embd", -1);
  11329. res->t_embd = cur;
  11330. ggml_build_forward_expand(gf, cur);
  11331. }
  11332. };
  11333. struct llm_build_plm : public llm_graph_context {
  11334. llm_build_plm(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
  11335. const float kq_scale = 1.0f/sqrtf(float(hparams.n_embd_head_k));
  11336. const uint32_t n_embd_head_qk_rope = hparams.n_rot;
  11337. const uint32_t n_embd_head_qk_nope = hparams.n_embd_head_k - hparams.n_rot;
  11338. const uint32_t kv_lora_rank = hparams.n_lora_kv;
  11339. ggml_tensor * cur;
  11340. ggml_tensor * inpL;
  11341. // {n_embd, n_tokens}
  11342. inpL = build_inp_embd(model.tok_embd);
  11343. // inp_pos - contains the positions
  11344. ggml_tensor * inp_pos = build_inp_pos();
  11345. auto * inp_attn = build_attn_inp_kv_unified();
  11346. ggml_tensor * inp_out_ids = build_inp_out_ids();
  11347. for (int il = 0; il < n_layer; ++il) {
  11348. ggml_tensor * inpSA = inpL;
  11349. // norm
  11350. cur = build_norm(inpL,
  11351. model.layers[il].attn_norm, NULL,
  11352. LLM_NORM_RMS, il);
  11353. cb(cur, "attn_norm", il);
  11354. // self_attention
  11355. {
  11356. ggml_tensor * q = NULL;
  11357. q = ggml_mul_mat(ctx0, model.layers[il].wq, cur);
  11358. cb(q, "q", il);
  11359. // split into {n_head * n_embd_head_qk_nope, n_tokens}
  11360. ggml_tensor * q_nope = ggml_view_3d(ctx0, q, n_embd_head_qk_nope, n_head, n_tokens,
  11361. ggml_row_size(q->type, hparams.n_embd_head_k),
  11362. ggml_row_size(q->type, hparams.n_embd_head_k * n_head),
  11363. 0);
  11364. cb(q_nope, "q_nope", il);
  11365. // and {n_head * n_embd_head_qk_rope, n_tokens}
  11366. ggml_tensor * q_pe = ggml_view_3d(ctx0, q, n_embd_head_qk_rope, n_head, n_tokens,
  11367. ggml_row_size(q->type, hparams.n_embd_head_k),
  11368. ggml_row_size(q->type, hparams.n_embd_head_k * n_head),
  11369. ggml_row_size(q->type, n_embd_head_qk_nope));
  11370. cb(q_pe, "q_pe", il);
  11371. // {n_embd, kv_lora_rank + n_embd_head_qk_rope} * {n_embd, n_tokens} -> {kv_lora_rank + n_embd_head_qk_rope, n_tokens}
  11372. ggml_tensor * kv_pe_compresseed = ggml_mul_mat(ctx0, model.layers[il].wkv_a_mqa, cur);
  11373. cb(kv_pe_compresseed, "kv_pe_compresseed", il);
  11374. // split into {kv_lora_rank, n_tokens}
  11375. ggml_tensor * kv_compressed = ggml_view_2d(ctx0, kv_pe_compresseed, kv_lora_rank, n_tokens,
  11376. kv_pe_compresseed->nb[1],
  11377. 0);
  11378. cb(kv_compressed, "kv_compressed", il);
  11379. // and {n_embd_head_qk_rope, n_tokens}
  11380. ggml_tensor * k_pe = ggml_view_3d(ctx0, kv_pe_compresseed, n_embd_head_qk_rope, 1, n_tokens,
  11381. kv_pe_compresseed->nb[1],
  11382. kv_pe_compresseed->nb[1],
  11383. ggml_row_size(kv_pe_compresseed->type, kv_lora_rank));
  11384. cb(k_pe, "k_pe", il);
  11385. kv_compressed = build_norm(kv_compressed,
  11386. model.layers[il].attn_kv_a_norm, NULL,
  11387. LLM_NORM_RMS, il);
  11388. cb(kv_compressed, "kv_compressed", il);
  11389. // {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}
  11390. ggml_tensor * kv = ggml_mul_mat(ctx0, model.layers[il].wkv_b, kv_compressed);
  11391. cb(kv, "kv", il);
  11392. // split into {n_head * n_embd_head_qk_nope, n_tokens}
  11393. ggml_tensor * k_nope = ggml_view_3d(ctx0, kv, n_embd_head_qk_nope, n_head, n_tokens,
  11394. ggml_row_size(kv->type, n_embd_head_qk_nope + hparams.n_embd_head_v),
  11395. ggml_row_size(kv->type, n_head * (n_embd_head_qk_nope + hparams.n_embd_head_v)),
  11396. 0);
  11397. cb(k_nope, "k_nope", il);
  11398. // and {n_head * n_embd_head_v, n_tokens}
  11399. ggml_tensor * v_states = ggml_view_3d(ctx0, kv, hparams.n_embd_head_v, n_head, n_tokens,
  11400. ggml_row_size(kv->type, (n_embd_head_qk_nope + hparams.n_embd_head_v)),
  11401. ggml_row_size(kv->type, (n_embd_head_qk_nope + hparams.n_embd_head_v)*n_head),
  11402. ggml_row_size(kv->type, (n_embd_head_qk_nope)));
  11403. cb(v_states, "v_states", il);
  11404. v_states = ggml_cont(ctx0, v_states);
  11405. cb(v_states, "v_states", il);
  11406. v_states = ggml_view_2d(ctx0, v_states, hparams.n_embd_head_v * n_head, n_tokens,
  11407. ggml_row_size(kv->type, hparams.n_embd_head_v * n_head),
  11408. 0);
  11409. cb(v_states, "v_states", il);
  11410. q_pe = ggml_rope_ext(
  11411. ctx0, q_pe, inp_pos, nullptr,
  11412. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  11413. ext_factor, attn_factor, beta_fast, beta_slow
  11414. );
  11415. cb(q_pe, "q_pe", il);
  11416. // shared RoPE key
  11417. k_pe = ggml_rope_ext(
  11418. ctx0, k_pe, inp_pos, nullptr,
  11419. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  11420. ext_factor, attn_factor, beta_fast, beta_slow
  11421. );
  11422. cb(k_pe, "k_pe", il);
  11423. ggml_tensor * q_states = ggml_concat(ctx0, q_nope, q_pe, 0);
  11424. cb(q_states, "q_states", il);
  11425. ggml_tensor * k_states = ggml_concat(ctx0, k_nope, ggml_repeat(ctx0, k_pe, q_pe), 0);
  11426. cb(k_states, "k_states", il);
  11427. cur = build_attn(inp_attn, gf,
  11428. model.layers[il].wo, NULL,
  11429. q_states, k_states, v_states, nullptr, nullptr, kq_scale, il);
  11430. }
  11431. if (il == n_layer - 1 && inp_out_ids) {
  11432. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  11433. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  11434. }
  11435. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  11436. cb(ffn_inp, "ffn_inp", il);
  11437. cur = build_norm(ffn_inp,
  11438. model.layers[il].ffn_norm, NULL,
  11439. LLM_NORM_RMS, il);
  11440. cb(cur, "ffn_norm", il);
  11441. cur = build_ffn(cur,
  11442. model.layers[il].ffn_up, NULL, NULL,
  11443. NULL, NULL, NULL,
  11444. model.layers[il].ffn_down, NULL, NULL,
  11445. NULL,
  11446. LLM_FFN_RELU_SQR, LLM_FFN_SEQ, il);
  11447. cb(cur, "ffn_out", il);
  11448. cur = ggml_add(ctx0, cur, ffn_inp);
  11449. cur = build_cvec(cur, il);
  11450. cb(cur, "l_out", il);
  11451. // input for next layer
  11452. inpL = cur;
  11453. }
  11454. cur = inpL;
  11455. cur = build_norm(cur,
  11456. model.output_norm, NULL,
  11457. LLM_NORM_RMS, -1);
  11458. cb(cur, "result_norm", -1);
  11459. res->t_embd = cur;
  11460. cur = build_lora_mm(model.output, cur);
  11461. cb(cur, "result_output", -1);
  11462. res->t_logits = cur;
  11463. ggml_build_forward_expand(gf, cur);
  11464. }
  11465. };
  11466. struct llm_build_bailingmoe : public llm_graph_context {
  11467. llm_build_bailingmoe(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
  11468. ggml_tensor * cur;
  11469. ggml_tensor * inpL;
  11470. inpL = build_inp_embd(model.tok_embd);
  11471. // inp_pos - contains the positions
  11472. ggml_tensor * inp_pos = build_inp_pos();
  11473. auto * inp_attn = build_attn_inp_kv_unified();
  11474. ggml_tensor * inp_out_ids = build_inp_out_ids();
  11475. for (int il = 0; il < n_layer; ++il) {
  11476. ggml_tensor * inpSA = inpL;
  11477. // norm
  11478. cur = build_norm(inpL,
  11479. model.layers[il].attn_norm, NULL,
  11480. LLM_NORM_RMS, il);
  11481. cb(cur, "attn_norm", il);
  11482. // self-attention
  11483. {
  11484. // rope freq factors for llama3; may return nullptr for llama2 and other models
  11485. ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
  11486. // compute Q and K and RoPE them
  11487. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  11488. cb(Qcur, "Qcur", il);
  11489. if (model.layers[il].bq) {
  11490. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  11491. cb(Qcur, "Qcur", il);
  11492. }
  11493. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  11494. cb(Kcur, "Kcur", il);
  11495. if (model.layers[il].bk) {
  11496. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  11497. cb(Kcur, "Kcur", il);
  11498. }
  11499. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  11500. cb(Vcur, "Vcur", il);
  11501. if (model.layers[il].bv) {
  11502. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  11503. cb(Vcur, "Vcur", il);
  11504. }
  11505. Qcur = ggml_reshape_3d(ctx0, Qcur, n_rot, n_head, n_tokens);
  11506. Kcur = ggml_reshape_3d(ctx0, Kcur, n_rot, n_head_kv, n_tokens);
  11507. Vcur = ggml_reshape_3d(ctx0, Vcur, n_rot, n_head_kv, n_tokens);
  11508. Qcur = ggml_rope_ext(
  11509. ctx0, Qcur, inp_pos, rope_factors,
  11510. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  11511. ext_factor, attn_factor, beta_fast, beta_slow
  11512. );
  11513. Kcur = ggml_rope_ext(
  11514. ctx0, Kcur, inp_pos, rope_factors,
  11515. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  11516. ext_factor, attn_factor, beta_fast, beta_slow
  11517. );
  11518. cb(Qcur, "Qcur", il);
  11519. cb(Kcur, "Kcur", il);
  11520. cb(Vcur, "Vcur", il);
  11521. cur = build_attn(inp_attn, gf,
  11522. model.layers[il].wo, model.layers[il].bo,
  11523. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_rot)), il);
  11524. }
  11525. if (il == n_layer - 1 && inp_out_ids) {
  11526. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  11527. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  11528. }
  11529. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  11530. cb(ffn_inp, "ffn_inp", il);
  11531. cur = build_norm(ffn_inp,
  11532. model.layers[il].ffn_norm, NULL,
  11533. LLM_NORM_RMS, il);
  11534. cb(cur, "ffn_norm", il);
  11535. ggml_tensor * moe_out =
  11536. build_moe_ffn(cur,
  11537. model.layers[il].ffn_gate_inp,
  11538. model.layers[il].ffn_up_exps,
  11539. model.layers[il].ffn_gate_exps,
  11540. model.layers[il].ffn_down_exps,
  11541. nullptr,
  11542. n_expert, n_expert_used,
  11543. LLM_FFN_SILU, hparams.expert_weights_norm,
  11544. false, hparams.expert_weights_scale,
  11545. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  11546. il);
  11547. cb(moe_out, "ffn_moe_out", il);
  11548. // FFN shared expert
  11549. {
  11550. ggml_tensor * ffn_shexp = build_ffn(cur,
  11551. model.layers[il].ffn_up_shexp, NULL, NULL,
  11552. model.layers[il].ffn_gate_shexp, NULL, NULL,
  11553. model.layers[il].ffn_down_shexp, NULL, NULL,
  11554. NULL,
  11555. LLM_FFN_SILU, LLM_FFN_PAR, il);
  11556. cb(ffn_shexp, "ffn_shexp", il);
  11557. cur = ggml_add(ctx0, moe_out, ffn_shexp);
  11558. cb(cur, "ffn_out", il);
  11559. }
  11560. cur = ggml_add(ctx0, cur, ffn_inp);
  11561. cur = build_cvec(cur, il);
  11562. cb(cur, "l_out", il);
  11563. // input for next layer
  11564. inpL = cur;
  11565. }
  11566. cur = inpL;
  11567. cur = build_norm(cur,
  11568. model.output_norm, NULL,
  11569. LLM_NORM_RMS, -1);
  11570. cb(cur, "result_norm", -1);
  11571. res->t_embd = cur;
  11572. // lm_head
  11573. cur = build_lora_mm(model.output, cur);
  11574. cb(cur, "result_output", -1);
  11575. res->t_logits = cur;
  11576. ggml_build_forward_expand(gf, cur);
  11577. }
  11578. };
  11579. struct llm_build_dots1 : public llm_graph_context {
  11580. llm_build_dots1(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
  11581. const int64_t n_embd_head = hparams.n_embd_head_v;
  11582. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  11583. GGML_ASSERT(n_embd_head == hparams.n_rot);
  11584. ggml_tensor * cur;
  11585. ggml_tensor * inpL;
  11586. inpL = build_inp_embd(model.tok_embd);
  11587. // inp_pos - contains the positions
  11588. ggml_tensor * inp_pos = build_inp_pos();
  11589. auto * inp_attn = build_attn_inp_kv_unified();
  11590. ggml_tensor * inp_out_ids = build_inp_out_ids();
  11591. for (int il = 0; il < n_layer; ++il) {
  11592. ggml_tensor * inpSA = inpL;
  11593. // norm
  11594. cur = build_norm(inpL,
  11595. model.layers[il].attn_norm, NULL,
  11596. LLM_NORM_RMS, il);
  11597. cb(cur, "attn_norm", il);
  11598. // self_attention
  11599. {
  11600. // compute Q and K and RoPE them
  11601. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  11602. cb(Qcur, "Qcur", il);
  11603. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  11604. cb(Kcur, "Kcur", il);
  11605. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  11606. cb(Vcur, "Vcur", il);
  11607. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  11608. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  11609. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  11610. Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);
  11611. cb(Qcur, "Qcur_normed", il);
  11612. Qcur = ggml_rope_ext(
  11613. ctx0, Qcur, inp_pos, nullptr,
  11614. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  11615. ext_factor, attn_factor, beta_fast, beta_slow
  11616. );
  11617. Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);
  11618. cb(Kcur, "Kcur_normed", il);
  11619. Kcur = ggml_rope_ext(
  11620. ctx0, Kcur, inp_pos, nullptr,
  11621. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  11622. ext_factor, attn_factor, beta_fast, beta_slow
  11623. );
  11624. cb(Qcur, "Qcur", il);
  11625. cb(Kcur, "Kcur", il);
  11626. cb(Vcur, "Vcur", il);
  11627. cur = build_attn(inp_attn, gf,
  11628. model.layers[il].wo, model.layers[il].bo,
  11629. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  11630. }
  11631. if (il == n_layer - 1 && inp_out_ids) {
  11632. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  11633. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  11634. }
  11635. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  11636. cb(ffn_inp, "ffn_inp", il);
  11637. // MoE branch
  11638. cur = build_norm(ffn_inp,
  11639. model.layers[il].ffn_norm, NULL,
  11640. LLM_NORM_RMS, il);
  11641. cb(cur, "ffn_norm", il);
  11642. if ((uint32_t) il < hparams.n_layer_dense_lead) {
  11643. cur = build_ffn(cur,
  11644. model.layers[il].ffn_up, NULL, NULL,
  11645. model.layers[il].ffn_gate, NULL, NULL,
  11646. model.layers[il].ffn_down, NULL, NULL,
  11647. NULL,
  11648. LLM_FFN_SILU, LLM_FFN_PAR, il);
  11649. cb(cur, "ffn_out", il);
  11650. } else {
  11651. ggml_tensor * moe_out =
  11652. build_moe_ffn(cur,
  11653. model.layers[il].ffn_gate_inp,
  11654. model.layers[il].ffn_up_exps,
  11655. model.layers[il].ffn_gate_exps,
  11656. model.layers[il].ffn_down_exps,
  11657. model.layers[il].ffn_exp_probs_b,
  11658. n_expert, n_expert_used,
  11659. LLM_FFN_SILU, hparams.expert_weights_norm,
  11660. true, hparams.expert_weights_scale,
  11661. (llama_expert_gating_func_type) hparams.expert_gating_func,
  11662. il);
  11663. cb(moe_out, "ffn_moe_out", il);
  11664. {
  11665. ggml_tensor * ffn_shexp = build_ffn(cur,
  11666. model.layers[il].ffn_up_shexp, NULL, NULL,
  11667. model.layers[il].ffn_gate_shexp, NULL, NULL,
  11668. model.layers[il].ffn_down_shexp, NULL, NULL,
  11669. NULL,
  11670. LLM_FFN_SILU, LLM_FFN_PAR, il);
  11671. cb(ffn_shexp, "ffn_shexp", il);
  11672. cur = ggml_add(ctx0, moe_out, ffn_shexp);
  11673. cb(cur, "ffn_out", il);
  11674. }
  11675. }
  11676. cur = ggml_add(ctx0, cur, ffn_inp);
  11677. cur = build_cvec(cur, il);
  11678. cb(cur, "l_out", il);
  11679. // input for next layer
  11680. inpL = cur;
  11681. }
  11682. cur = inpL;
  11683. cur = build_norm(cur,
  11684. model.output_norm, NULL,
  11685. LLM_NORM_RMS, -1);
  11686. cb(cur, "result_norm", -1);
  11687. res->t_embd = cur;
  11688. // lm_head
  11689. cur = build_lora_mm(model.output, cur);
  11690. cb(cur, "result_output", -1);
  11691. res->t_logits = cur;
  11692. ggml_build_forward_expand(gf, cur);
  11693. }
  11694. };
  11695. struct llm_build_ernie4_5 : public llm_graph_context {
  11696. llm_build_ernie4_5(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
  11697. const int64_t n_embd_head = hparams.n_embd_head_v;
  11698. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  11699. GGML_ASSERT(n_embd_head == hparams.n_rot);
  11700. ggml_tensor * cur;
  11701. ggml_tensor * inpL;
  11702. inpL = build_inp_embd(model.tok_embd);
  11703. // inp_pos - contains the positions
  11704. ggml_tensor * inp_pos = build_inp_pos();
  11705. auto * inp_attn = build_attn_inp_kv_unified();
  11706. for (int il = 0; il < n_layer; ++il) {
  11707. ggml_tensor * inpSA = inpL;
  11708. // norm
  11709. {
  11710. cur = build_norm(inpL,
  11711. model.layers[il].attn_norm, NULL,
  11712. LLM_NORM_RMS, il);
  11713. cb(cur, "attn_norm", il);
  11714. }
  11715. // self-attention
  11716. {
  11717. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  11718. cb(Qcur, "Qcur", il);
  11719. if (model.layers[il].bq) {
  11720. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  11721. cb(Qcur, "Qcur", il);
  11722. }
  11723. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  11724. cb(Kcur, "Kcur", il);
  11725. if (model.layers[il].bk) {
  11726. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  11727. cb(Kcur, "Kcur", il);
  11728. }
  11729. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  11730. cb(Vcur, "Vcur", il);
  11731. if (model.layers[il].bv) {
  11732. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  11733. cb(Vcur, "Vcur", il);
  11734. }
  11735. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  11736. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  11737. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  11738. Qcur = ggml_rope_ext(
  11739. ctx0, Qcur, inp_pos, nullptr,
  11740. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  11741. ext_factor, attn_factor, beta_fast, beta_slow
  11742. );
  11743. Kcur = ggml_rope_ext(
  11744. ctx0, Kcur, inp_pos, nullptr,
  11745. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  11746. ext_factor, attn_factor, beta_fast, beta_slow
  11747. );
  11748. cb(Qcur, "Qcur", il);
  11749. cb(Kcur, "Kcur", il);
  11750. cb(Vcur, "Vcur", il);
  11751. cur = build_attn(inp_attn, gf,
  11752. model.layers[il].wo, NULL,
  11753. Qcur, Kcur, Vcur, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
  11754. }
  11755. if (il == n_layer - 1) {
  11756. // skip computing output for unused tokens
  11757. ggml_tensor * inp_out_ids = build_inp_out_ids();
  11758. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  11759. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  11760. }
  11761. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  11762. cb(ffn_inp, "ffn_inp", il);
  11763. // feed-forward network
  11764. {
  11765. cur = build_norm(ffn_inp,
  11766. model.layers[il].ffn_norm, NULL,
  11767. LLM_NORM_RMS, il);
  11768. cb(cur, "ffn_norm", il);
  11769. cur = build_ffn(cur,
  11770. model.layers[il].ffn_up, NULL, NULL,
  11771. model.layers[il].ffn_gate, NULL, NULL,
  11772. model.layers[il].ffn_down, NULL, NULL,
  11773. NULL,
  11774. LLM_FFN_SILU, LLM_FFN_PAR, il);
  11775. cb(cur, "ffn_out", il);
  11776. }
  11777. cur = ggml_add(ctx0, cur, ffn_inp);
  11778. cur = build_cvec(cur, il);
  11779. cb(cur, "l_out", il);
  11780. // input for next layer
  11781. inpL = cur;
  11782. }
  11783. cur = inpL;
  11784. cur = build_norm(cur,
  11785. model.output_norm, NULL,
  11786. LLM_NORM_RMS, -1);
  11787. cb(cur, "result_norm", -1);
  11788. res->t_embd = cur;
  11789. // lm_head
  11790. cur = build_lora_mm(model.output, cur);
  11791. cb(cur, "result_output", -1);
  11792. res->t_logits = cur;
  11793. ggml_build_forward_expand(gf, cur);
  11794. }
  11795. };
  11796. struct llm_build_falcon_h1 : public llm_graph_context_mamba {
  11797. llm_build_falcon_h1(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context_mamba(params) {
  11798. const int64_t n_embd_head = hparams.n_embd_head_v;
  11799. ggml_tensor * cur;
  11800. ggml_tensor * inpL;
  11801. inpL = build_inp_embd(model.tok_embd);
  11802. // inp_pos - contains the positions
  11803. ggml_tensor * inp_pos = build_inp_pos();
  11804. // Build the inputs in the recurrent & kv cache
  11805. auto * inp = build_inp_mem_hybrid();
  11806. const float kq_scale = hparams.f_attention_scale == 0.0f ? 1.0f/sqrtf(float(n_embd_head)) : hparams.f_attention_scale;
  11807. ggml_tensor * inp_out_ids = build_inp_out_ids();
  11808. for (int il = 0; il < n_layer; ++il) {
  11809. ggml_tensor * inpSA = inpL;
  11810. cur = build_norm(inpL,
  11811. model.layers[il].attn_norm, NULL,
  11812. LLM_NORM_RMS, il);
  11813. cb(cur, "attn_norm", il);
  11814. // self-attention
  11815. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  11816. cb(Qcur, "Qcur", il);
  11817. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  11818. cb(Kcur, "Kcur", il);
  11819. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  11820. cb(Vcur, "Vcur", il);
  11821. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  11822. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  11823. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  11824. Qcur = ggml_rope_ext(
  11825. ctx0, Qcur, inp_pos, nullptr,
  11826. n_rot, hparams.rope_type, n_ctx_orig, freq_base, freq_scale,
  11827. ext_factor, attn_factor, beta_fast, beta_slow);
  11828. Kcur = ggml_rope_ext(
  11829. ctx0, Kcur, inp_pos, nullptr,
  11830. n_rot, hparams.rope_type, n_ctx_orig, freq_base, freq_scale,
  11831. ext_factor, attn_factor, beta_fast, beta_slow
  11832. );
  11833. cb(Qcur, "Qcur-post-rope", il);
  11834. cb(Kcur, "Kcur-post-rope", il);
  11835. cb(Vcur, "Vcur-post-rope", il);
  11836. ggml_tensor * attn_out = build_attn(inp->get_attn(), gf,
  11837. model.layers[il].wo, NULL,
  11838. Qcur, Kcur, Vcur, nullptr, nullptr, kq_scale, il);
  11839. cb(attn_out, "attn_out", il);
  11840. cur = build_norm(inpL,
  11841. model.layers[il].attn_norm, NULL,
  11842. LLM_NORM_RMS, il);
  11843. // Mamba2 layer
  11844. cb(cur, "ssm_in", il);
  11845. ggml_tensor * ssm_out = build_mamba2_layer(inp->get_recr(), gf, cur, model, ubatch, il);
  11846. cb(ssm_out, "ssm_out", il);
  11847. // // Aggregation
  11848. cur = ggml_add(ctx0, attn_out, ssm_out);
  11849. inpSA = ggml_add(ctx0, cur, inpSA);
  11850. cb(cur, "layer_out", il);
  11851. if (il == n_layer - 1 && inp_out_ids) {
  11852. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  11853. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  11854. }
  11855. ggml_tensor * ffn_inp = inpSA;
  11856. cb(ffn_inp, "ffn_inp", il);
  11857. // feed-forward network
  11858. cur = build_norm(ffn_inp,
  11859. model.layers[il].ffn_norm, NULL,
  11860. LLM_NORM_RMS, il);
  11861. cb(cur, "ffn_norm", il);
  11862. cur = build_ffn(cur,
  11863. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  11864. model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, NULL,
  11865. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  11866. NULL,
  11867. LLM_FFN_SILU, LLM_FFN_PAR, il);
  11868. cb(cur, "ffn_out", il);
  11869. cur = ggml_add(ctx0, cur, inpSA);
  11870. cur = build_cvec(cur, il);
  11871. cb(cur, "l_out", il);
  11872. // input for next layer
  11873. inpL = cur;
  11874. }
  11875. cur = inpL;
  11876. cur = build_norm(cur,
  11877. model.output_norm, NULL,
  11878. LLM_NORM_RMS, -1);
  11879. cb(cur, "result_norm", -1);
  11880. res->t_embd = cur;
  11881. // lm_head
  11882. cur = build_lora_mm(model.output, cur);
  11883. cb(cur, "result_output", -1);
  11884. res->t_logits = cur;
  11885. ggml_build_forward_expand(gf, cur);
  11886. }
  11887. };
  11888. struct llm_build_arcee : public llm_graph_context {
  11889. llm_build_arcee(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
  11890. const int64_t n_embd_head = hparams.n_embd_head_v;
  11891. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  11892. GGML_ASSERT(n_embd_head == hparams.n_rot);
  11893. ggml_tensor * cur;
  11894. ggml_tensor * inpL;
  11895. inpL = build_inp_embd(model.tok_embd);
  11896. // inp_pos - contains the positions
  11897. ggml_tensor * inp_pos = build_inp_pos();
  11898. auto * inp_attn = build_attn_inp_kv_unified();
  11899. const float kq_scale = hparams.f_attention_scale == 0.0f ? 1.0f/sqrtf(float(n_embd_head)) : hparams.f_attention_scale;
  11900. ggml_tensor * inp_out_ids = build_inp_out_ids();
  11901. for (int il = 0; il < n_layer; ++il) {
  11902. ggml_tensor * inpSA = inpL;
  11903. // norm
  11904. cur = build_norm(inpL,
  11905. model.layers[il].attn_norm, NULL,
  11906. LLM_NORM_RMS, il);
  11907. cb(cur, "attn_norm", il);
  11908. // self-attention
  11909. {
  11910. // rope freq factors for llama3; may return nullptr for llama2 and other models
  11911. ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
  11912. // compute Q and K and RoPE them
  11913. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  11914. cb(Qcur, "Qcur", il);
  11915. if (model.layers[il].bq) {
  11916. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  11917. cb(Qcur, "Qcur", il);
  11918. }
  11919. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  11920. cb(Kcur, "Kcur", il);
  11921. if (model.layers[il].bk) {
  11922. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  11923. cb(Kcur, "Kcur", il);
  11924. }
  11925. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  11926. cb(Vcur, "Vcur", il);
  11927. if (model.layers[il].bv) {
  11928. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  11929. cb(Vcur, "Vcur", il);
  11930. }
  11931. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  11932. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  11933. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  11934. Qcur = ggml_rope_ext(
  11935. ctx0, Qcur, inp_pos, rope_factors,
  11936. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  11937. ext_factor, attn_factor, beta_fast, beta_slow
  11938. );
  11939. Kcur = ggml_rope_ext(
  11940. ctx0, Kcur, inp_pos, rope_factors,
  11941. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  11942. ext_factor, attn_factor, beta_fast, beta_slow
  11943. );
  11944. cb(Qcur, "Qcur", il);
  11945. cb(Kcur, "Kcur", il);
  11946. cb(Vcur, "Vcur", il);
  11947. cur = build_attn(inp_attn, gf,
  11948. model.layers[il].wo, model.layers[il].bo,
  11949. Qcur, Kcur, Vcur, nullptr, nullptr, kq_scale, il);
  11950. cb(cur, "attn_out", il);
  11951. }
  11952. if (il == n_layer - 1 && inp_out_ids) {
  11953. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  11954. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  11955. }
  11956. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  11957. cb(ffn_inp, "ffn_inp", il);
  11958. // feed-forward network
  11959. // ARCEE uses relu^2 instead of silu
  11960. cur = build_norm(ffn_inp,
  11961. model.layers[il].ffn_norm, NULL,
  11962. LLM_NORM_RMS, il);
  11963. cb(cur, "ffn_norm", il);
  11964. cur = build_ffn(cur,
  11965. model.layers[il].ffn_up, NULL, NULL,
  11966. NULL, NULL, NULL,
  11967. model.layers[il].ffn_down, NULL, NULL,
  11968. NULL,
  11969. LLM_FFN_RELU_SQR, LLM_FFN_SEQ, il);
  11970. cb(cur, "ffn_out", il);
  11971. cur = ggml_add(ctx0, cur, ffn_inp);
  11972. cb(cur, "ffn_out", il);
  11973. cur = build_cvec(cur, il);
  11974. cb(cur, "l_out", il);
  11975. // input for next layer
  11976. inpL = cur;
  11977. }
  11978. cur = inpL;
  11979. cur = build_norm(cur,
  11980. model.output_norm, NULL,
  11981. LLM_NORM_RMS, -1);
  11982. cb(cur, "result_norm", -1);
  11983. res->t_embd = cur;
  11984. // lm_head
  11985. cur = build_lora_mm(model.output, cur);
  11986. cb(cur, "result_output", -1);
  11987. res->t_logits = cur;
  11988. ggml_build_forward_expand(gf, cur);
  11989. }
  11990. };
  11991. struct llm_build_hunyuan_moe : public llm_graph_context {
  11992. llm_build_hunyuan_moe(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
  11993. const int64_t n_embd_head = hparams.n_embd_head_v;
  11994. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  11995. GGML_ASSERT(n_embd_head == hparams.n_rot);
  11996. ggml_tensor * cur;
  11997. ggml_tensor * inpL;
  11998. inpL = build_inp_embd(model.tok_embd);
  11999. // inp_pos - contains the positions
  12000. ggml_tensor * inp_pos = build_inp_pos();
  12001. auto * inp_attn = build_attn_inp_kv_unified();
  12002. const float kq_scale = 1.0f / sqrtf(float(n_embd_head));
  12003. ggml_tensor * inp_out_ids = build_inp_out_ids();
  12004. for (int il = 0; il < n_layer; ++il) {
  12005. ggml_tensor * inpSA = inpL;
  12006. // norm
  12007. cur = build_norm(inpL,
  12008. model.layers[il].attn_norm, NULL,
  12009. LLM_NORM_RMS, il);
  12010. cb(cur, "attn_norm", il);
  12011. // self-attention
  12012. {
  12013. // rope freq factors for llama3; may return nullptr for llama2 and other models
  12014. ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
  12015. // compute Q and K and RoPE them
  12016. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  12017. cb(Qcur, "Qcur", il);
  12018. if (model.layers[il].bq) {
  12019. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  12020. cb(Qcur, "Qcur", il);
  12021. }
  12022. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  12023. cb(Kcur, "Kcur", il);
  12024. if (model.layers[il].bk) {
  12025. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  12026. cb(Kcur, "Kcur", il);
  12027. }
  12028. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  12029. cb(Vcur, "Vcur", il);
  12030. if (model.layers[il].bv) {
  12031. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  12032. cb(Vcur, "Vcur", il);
  12033. }
  12034. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  12035. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  12036. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  12037. Qcur = ggml_rope_ext(
  12038. ctx0, Qcur, inp_pos, rope_factors,
  12039. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  12040. ext_factor, attn_factor, beta_fast, beta_slow
  12041. );
  12042. cb(Qcur, "Qcur", il);
  12043. cb(Kcur, "Kcur", il);
  12044. cb(Vcur, "Vcur", il);
  12045. Kcur = ggml_rope_ext(
  12046. ctx0, Kcur, inp_pos, rope_factors,
  12047. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  12048. ext_factor, attn_factor, beta_fast, beta_slow
  12049. );
  12050. Kcur = build_norm(Kcur,
  12051. model.layers[il].attn_k_norm, nullptr,
  12052. LLM_NORM_RMS, il);
  12053. cb(Kcur, "Kcur_norm", il);
  12054. Qcur = build_norm(Qcur,
  12055. model.layers[il].attn_q_norm, nullptr,
  12056. LLM_NORM_RMS, il);
  12057. cb(Qcur, "Qcur_norm", il);
  12058. cur = build_attn(inp_attn, gf,
  12059. model.layers[il].wo, model.layers[il].bo,
  12060. Qcur, Kcur, Vcur, nullptr, nullptr, kq_scale, il);
  12061. cb(cur, "attn_out", il);
  12062. }
  12063. if (il == n_layer - 1 && inp_out_ids) {
  12064. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  12065. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  12066. }
  12067. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  12068. cb(ffn_inp, "ffn_inp", il);
  12069. cur = build_norm(ffn_inp,
  12070. model.layers[il].ffn_norm, NULL,
  12071. LLM_NORM_RMS, il);
  12072. cb(cur, "ffn_norm", il);
  12073. // feed-forward network (non-MoE)
  12074. ggml_tensor * cur_mlp = build_ffn(cur,
  12075. model.layers[il].ffn_up_shexp, NULL, NULL,
  12076. model.layers[il].ffn_gate_shexp, NULL, NULL,
  12077. model.layers[il].ffn_down_shexp, NULL, NULL,
  12078. NULL,
  12079. LLM_FFN_SILU, LLM_FFN_PAR, il);
  12080. cb(cur_mlp, "ffn_mlp", il);
  12081. // MoE branch
  12082. ggml_tensor * cur_moe = build_moe_ffn(cur,
  12083. model.layers[il].ffn_gate_inp,
  12084. model.layers[il].ffn_up_exps,
  12085. model.layers[il].ffn_gate_exps,
  12086. model.layers[il].ffn_down_exps,
  12087. nullptr,
  12088. n_expert, n_expert_used,
  12089. LLM_FFN_SILU,
  12090. true, // norm_topk_prob
  12091. false,
  12092. 0.0,
  12093. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
  12094. il);
  12095. cb(cur_moe, "ffn_moe_out", il);
  12096. ggml_tensor * ffn_out = ggml_add(ctx0, cur_moe, cur_mlp);
  12097. cb(ffn_out, "ffn_out", il);
  12098. cur = ggml_add(ctx0, ffn_out, ffn_inp);
  12099. cur = build_cvec(cur, il);
  12100. cb(cur, "l_out", il);
  12101. // input for next layer
  12102. inpL = cur;
  12103. }
  12104. cur = inpL;
  12105. cur = build_norm(cur,
  12106. model.output_norm, NULL,
  12107. LLM_NORM_RMS, -1);
  12108. cb(cur, "result_norm", -1);
  12109. res->t_embd = cur;
  12110. // lm_head
  12111. cur = build_lora_mm(model.output, cur);
  12112. cb(cur, "result_output", -1);
  12113. res->t_logits = cur;
  12114. ggml_build_forward_expand(gf, cur);
  12115. }
  12116. };
  12117. struct llm_build_smollm3 : public llm_graph_context {
  12118. llm_build_smollm3(const llama_model & model, const llm_graph_params & params, ggml_cgraph * gf) : llm_graph_context(params) {
  12119. const int64_t n_embd_head = hparams.n_embd_head_v;
  12120. GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
  12121. GGML_ASSERT(n_embd_head == hparams.n_rot);
  12122. ggml_tensor * cur;
  12123. ggml_tensor * inpL;
  12124. inpL = build_inp_embd(model.tok_embd);
  12125. // inp_pos - contains the positions
  12126. ggml_tensor * inp_pos = build_inp_pos();
  12127. auto * inp_attn = build_attn_inp_kv_unified();
  12128. const float kq_scale = hparams.f_attention_scale == 0.0f ? 1.0f/sqrtf(float(n_embd_head)) : hparams.f_attention_scale;
  12129. ggml_tensor * inp_out_ids = build_inp_out_ids();
  12130. for (int il = 0; il < n_layer; ++il) {
  12131. ggml_tensor * inpSA = inpL;
  12132. const bool use_rope = (il + 1) % hparams.n_no_rope_layer_step != 0;
  12133. // norm
  12134. cur = build_norm(inpL,
  12135. model.layers[il].attn_norm, NULL,
  12136. LLM_NORM_RMS, il);
  12137. cb(cur, "attn_norm", il);
  12138. // self-attention
  12139. {
  12140. // compute Q and K and RoPE them
  12141. ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
  12142. cb(Qcur, "Qcur", il);
  12143. if (model.layers[il].bq) {
  12144. Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq);
  12145. cb(Qcur, "Qcur", il);
  12146. }
  12147. ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
  12148. cb(Kcur, "Kcur", il);
  12149. if (model.layers[il].bk) {
  12150. Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk);
  12151. cb(Kcur, "Kcur", il);
  12152. }
  12153. ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
  12154. cb(Vcur, "Vcur", il);
  12155. if (model.layers[il].bv) {
  12156. Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv);
  12157. cb(Vcur, "Vcur", il);
  12158. }
  12159. Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
  12160. Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
  12161. Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
  12162. if (use_rope) {
  12163. Qcur = ggml_rope_ext(
  12164. ctx0, Qcur, inp_pos, nullptr,
  12165. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  12166. ext_factor, attn_factor, beta_fast, beta_slow
  12167. );
  12168. Kcur = ggml_rope_ext(
  12169. ctx0, Kcur, inp_pos, nullptr,
  12170. n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
  12171. ext_factor, attn_factor, beta_fast, beta_slow
  12172. );
  12173. }
  12174. cb(Qcur, "Qcur", il);
  12175. cb(Kcur, "Kcur", il);
  12176. cb(Vcur, "Vcur", il);
  12177. cur = build_attn(inp_attn, gf,
  12178. model.layers[il].wo, model.layers[il].bo,
  12179. Qcur, Kcur, Vcur, nullptr, nullptr, kq_scale, il);
  12180. cb(cur, "attn_out", il);
  12181. }
  12182. if (il == n_layer - 1 && inp_out_ids) {
  12183. cur = ggml_get_rows(ctx0, cur, inp_out_ids);
  12184. inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
  12185. }
  12186. ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
  12187. cb(ffn_inp, "ffn_inp", il);
  12188. // feed-forward network
  12189. {
  12190. cur = build_norm(ffn_inp,
  12191. model.layers[il].ffn_norm, NULL,
  12192. LLM_NORM_RMS, il);
  12193. cb(cur, "ffn_norm", il);
  12194. cur = build_ffn(cur,
  12195. model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
  12196. model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, NULL,
  12197. model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
  12198. NULL,
  12199. LLM_FFN_SILU, LLM_FFN_PAR, il);
  12200. cb(cur, "ffn_out", il);
  12201. }
  12202. cur = ggml_add(ctx0, cur, ffn_inp);
  12203. cb(cur, "ffn_out", il);
  12204. cur = build_cvec(cur, il);
  12205. cb(cur, "l_out", il);
  12206. // input for next layer
  12207. inpL = cur;
  12208. }
  12209. cur = inpL;
  12210. cur = build_norm(cur,
  12211. model.output_norm, NULL,
  12212. LLM_NORM_RMS, -1);
  12213. cb(cur, "result_norm", -1);
  12214. res->t_embd = cur;
  12215. // lm_head
  12216. cur = build_lora_mm(model.output, cur);
  12217. cb(cur, "result_output", -1);
  12218. res->t_logits = cur;
  12219. ggml_build_forward_expand(gf, cur);
  12220. }
  12221. };
  12222. llama_memory_i * llama_model::create_memory(const llama_memory_params & params, llama_cparams & cparams) const {
  12223. llama_memory_i * res;
  12224. switch (arch) {
  12225. // Models that need specific instantiation should be handled in the
  12226. // switch statement
  12227. case LLM_ARCH_BERT:
  12228. case LLM_ARCH_JINA_BERT_V2:
  12229. case LLM_ARCH_NOMIC_BERT:
  12230. case LLM_ARCH_NOMIC_BERT_MOE:
  12231. case LLM_ARCH_NEO_BERT:
  12232. case LLM_ARCH_WAVTOKENIZER_DEC:
  12233. {
  12234. res = nullptr;
  12235. } break;
  12236. // Models that need standard caching should rely on recurrent/hybrid
  12237. // checks
  12238. default:
  12239. {
  12240. if (llm_arch_is_recurrent(arch)) {
  12241. res = new llama_memory_recurrent(
  12242. *this,
  12243. nullptr,
  12244. GGML_TYPE_F32,
  12245. GGML_TYPE_F32,
  12246. cparams.offload_kqv,
  12247. std::max((uint32_t) 1, cparams.n_seq_max),
  12248. cparams.n_seq_max);
  12249. } else if (llm_arch_is_hybrid(arch)) {
  12250. const auto padding = llama_kv_cache_unified::get_padding(cparams);
  12251. cparams.n_ctx = GGML_PAD(cparams.n_ctx, padding);
  12252. res = new llama_memory_hybrid(
  12253. /* model */ *this,
  12254. /* attn_type_k */ params.type_k,
  12255. /* attn_type_v */ params.type_v,
  12256. /* attn_v_trans */ !cparams.flash_attn,
  12257. /* attn_kv_size */ cparams.n_ctx,
  12258. /* attn_n_pad */ padding,
  12259. /* attn_n_swa */ hparams.n_swa,
  12260. /* attn_swa_type */ hparams.swa_type,
  12261. /* recurrent_type_k */ GGML_TYPE_F32,
  12262. /* recurrent_type_v */ GGML_TYPE_F32,
  12263. /* recurrent_kv_size */ std::max((uint32_t) 1, cparams.n_seq_max),
  12264. /* n_seq_max */ cparams.n_seq_max,
  12265. /* offload */ cparams.offload_kqv,
  12266. /* filter_attn */ (arch == LLM_ARCH_FALCON_H1) ? [&](int32_t) { return true; } : (llama_memory_hybrid::layer_filter_cb)nullptr,
  12267. /* filter_recr */ (arch == LLM_ARCH_FALCON_H1) ? [&](int32_t) { return true; } : (llama_memory_hybrid::layer_filter_cb)nullptr);
  12268. } else {
  12269. const auto padding = llama_kv_cache_unified::get_padding(cparams);
  12270. cparams.n_ctx = GGML_PAD(cparams.n_ctx, padding);
  12271. LLAMA_LOG_DEBUG("%s: n_ctx = %u (padded)\n", __func__, cparams.n_ctx);
  12272. if (hparams.swa_type != LLAMA_SWA_TYPE_NONE) {
  12273. GGML_ASSERT(hparams.is_swa_any());
  12274. res = new llama_kv_cache_unified_iswa(
  12275. *this,
  12276. params.type_k,
  12277. params.type_v,
  12278. !cparams.flash_attn,
  12279. cparams.offload_kqv,
  12280. params.swa_full,
  12281. cparams.n_ctx,
  12282. cparams.n_seq_max,
  12283. cparams.n_ubatch,
  12284. padding);
  12285. } else {
  12286. GGML_ASSERT(!hparams.is_swa_any());
  12287. res = new llama_kv_cache_unified(
  12288. *this,
  12289. nullptr,
  12290. params.type_k,
  12291. params.type_v,
  12292. !cparams.flash_attn,
  12293. cparams.offload_kqv,
  12294. cparams.n_ctx,
  12295. cparams.n_seq_max,
  12296. padding,
  12297. hparams.n_swa,
  12298. hparams.swa_type);
  12299. }
  12300. }
  12301. }
  12302. }
  12303. return res;
  12304. }
  12305. llm_graph_result_ptr llama_model::build_graph(
  12306. const llm_graph_params & params,
  12307. ggml_cgraph * gf,
  12308. llm_graph_type type) const {
  12309. std::unique_ptr<llm_graph_context> llm;
  12310. switch (arch) {
  12311. case LLM_ARCH_LLAMA:
  12312. {
  12313. llm = std::make_unique<llm_build_llama>(*this, params, gf);
  12314. } break;
  12315. case LLM_ARCH_LLAMA4:
  12316. {
  12317. llm = std::make_unique<llm_build_llama_iswa>(*this, params, gf);
  12318. } break;
  12319. case LLM_ARCH_DECI:
  12320. {
  12321. llm = std::make_unique<llm_build_deci>(*this, params, gf);
  12322. } break;
  12323. case LLM_ARCH_BAICHUAN:
  12324. {
  12325. llm = std::make_unique<llm_build_baichuan>(*this, params, gf);
  12326. } break;
  12327. case LLM_ARCH_FALCON:
  12328. {
  12329. llm = std::make_unique<llm_build_falcon>(*this, params, gf);
  12330. } break;
  12331. case LLM_ARCH_GROK:
  12332. {
  12333. llm = std::make_unique<llm_build_grok>(*this, params, gf);
  12334. } break;
  12335. case LLM_ARCH_STARCODER:
  12336. {
  12337. llm = std::make_unique<llm_build_starcoder>(*this, params, gf);
  12338. } break;
  12339. case LLM_ARCH_REFACT:
  12340. {
  12341. llm = std::make_unique<llm_build_refact>(*this, params, gf);
  12342. } break;
  12343. case LLM_ARCH_BERT:
  12344. case LLM_ARCH_JINA_BERT_V2:
  12345. case LLM_ARCH_NOMIC_BERT:
  12346. case LLM_ARCH_NOMIC_BERT_MOE:
  12347. {
  12348. llm = std::make_unique<llm_build_bert>(*this, params, gf);
  12349. } break;
  12350. case LLM_ARCH_NEO_BERT:
  12351. {
  12352. llm = std::make_unique<llm_build_neo_bert>(*this, params, gf);
  12353. } break;
  12354. case LLM_ARCH_BLOOM:
  12355. {
  12356. llm = std::make_unique<llm_build_bloom>(*this, params, gf);
  12357. } break;
  12358. case LLM_ARCH_MPT:
  12359. {
  12360. llm = std::make_unique<llm_build_mpt>(*this, params, gf);
  12361. } break;
  12362. case LLM_ARCH_STABLELM:
  12363. {
  12364. llm = std::make_unique<llm_build_stablelm>(*this, params, gf);
  12365. } break;
  12366. case LLM_ARCH_QWEN:
  12367. {
  12368. llm = std::make_unique<llm_build_qwen>(*this, params, gf);
  12369. } break;
  12370. case LLM_ARCH_QWEN2:
  12371. {
  12372. llm = std::make_unique<llm_build_qwen2>(*this, params, gf);
  12373. } break;
  12374. case LLM_ARCH_QWEN2VL:
  12375. {
  12376. llm = std::make_unique<llm_build_qwen2vl>(*this, params, gf);
  12377. } break;
  12378. case LLM_ARCH_QWEN2MOE:
  12379. {
  12380. llm = std::make_unique<llm_build_qwen2moe>(*this, params, gf);
  12381. } break;
  12382. case LLM_ARCH_QWEN3:
  12383. {
  12384. llm = std::make_unique<llm_build_qwen3>(*this, params, gf);
  12385. } break;
  12386. case LLM_ARCH_QWEN3MOE:
  12387. {
  12388. llm = std::make_unique<llm_build_qwen3moe>(*this, params, gf);
  12389. } break;
  12390. case LLM_ARCH_PHI2:
  12391. {
  12392. llm = std::make_unique<llm_build_phi2>(*this, params, gf);
  12393. } break;
  12394. case LLM_ARCH_PHI3:
  12395. case LLM_ARCH_PHIMOE:
  12396. {
  12397. if (hparams.swa_type != LLAMA_SWA_TYPE_NONE) {
  12398. llm = std::make_unique<llm_build_phi3<true>> (*this, params, gf);
  12399. } else {
  12400. llm = std::make_unique<llm_build_phi3<false>>(*this, params, gf);
  12401. }
  12402. } break;
  12403. case LLM_ARCH_PLAMO:
  12404. {
  12405. llm = std::make_unique<llm_build_plamo>(*this, params, gf);
  12406. } break;
  12407. case LLM_ARCH_GPT2:
  12408. {
  12409. llm = std::make_unique<llm_build_gpt2>(*this, params, gf);
  12410. } break;
  12411. case LLM_ARCH_CODESHELL:
  12412. {
  12413. llm = std::make_unique<llm_build_codeshell>(*this, params, gf);
  12414. } break;
  12415. case LLM_ARCH_ORION:
  12416. {
  12417. llm = std::make_unique<llm_build_orion>(*this, params, gf);
  12418. } break;
  12419. case LLM_ARCH_INTERNLM2:
  12420. {
  12421. llm = std::make_unique<llm_build_internlm2>(*this, params, gf);
  12422. } break;
  12423. case LLM_ARCH_MINICPM3:
  12424. {
  12425. llm = std::make_unique<llm_build_minicpm3>(*this, params, gf);
  12426. } break;
  12427. case LLM_ARCH_GEMMA:
  12428. {
  12429. llm = std::make_unique<llm_build_gemma>(*this, params, gf);
  12430. } break;
  12431. case LLM_ARCH_GEMMA2:
  12432. {
  12433. llm = std::make_unique<llm_build_gemma2_iswa>(*this, params, gf);
  12434. } break;
  12435. case LLM_ARCH_GEMMA3:
  12436. {
  12437. llm = std::make_unique<llm_build_gemma3_iswa>(*this, params, gf);
  12438. } break;
  12439. case LLM_ARCH_GEMMA3N:
  12440. {
  12441. llm = std::make_unique<llm_build_gemma3n_iswa>(*this, params, gf);
  12442. } break;
  12443. case LLM_ARCH_STARCODER2:
  12444. {
  12445. llm = std::make_unique<llm_build_starcoder2>(*this, params, gf);
  12446. } break;
  12447. case LLM_ARCH_MAMBA:
  12448. case LLM_ARCH_MAMBA2:
  12449. {
  12450. llm = std::make_unique<llm_build_mamba>(*this, params, gf);
  12451. } break;
  12452. case LLM_ARCH_JAMBA:
  12453. {
  12454. llm = std::make_unique<llm_build_jamba>(*this, params, gf);
  12455. } break;
  12456. case LLM_ARCH_XVERSE:
  12457. {
  12458. llm = std::make_unique<llm_build_xverse>(*this, params, gf);
  12459. } break;
  12460. case LLM_ARCH_COMMAND_R:
  12461. {
  12462. llm = std::make_unique<llm_build_command_r>(*this, params, gf);
  12463. } break;
  12464. case LLM_ARCH_COHERE2:
  12465. {
  12466. llm = std::make_unique<llm_build_cohere2_iswa>(*this, params, gf);
  12467. } break;
  12468. case LLM_ARCH_DBRX:
  12469. {
  12470. llm = std::make_unique<llm_build_dbrx>(*this, params, gf);
  12471. } break;
  12472. case LLM_ARCH_OLMO:
  12473. {
  12474. llm = std::make_unique<llm_build_olmo>(*this, params, gf);
  12475. } break;
  12476. case LLM_ARCH_OLMO2:
  12477. {
  12478. llm = std::make_unique<llm_build_olmo2>(*this, params, gf);
  12479. } break;
  12480. case LLM_ARCH_OLMOE:
  12481. {
  12482. llm = std::make_unique<llm_build_olmoe>(*this, params, gf);
  12483. } break;
  12484. case LLM_ARCH_OPENELM:
  12485. {
  12486. llm = std::make_unique<llm_build_openelm>(*this, params, gf);
  12487. } break;
  12488. case LLM_ARCH_GPTNEOX:
  12489. {
  12490. llm = std::make_unique<llm_build_gptneox>(*this, params, gf);
  12491. } break;
  12492. case LLM_ARCH_ARCTIC:
  12493. {
  12494. llm = std::make_unique<llm_build_arctic>(*this, params, gf);
  12495. } break;
  12496. case LLM_ARCH_DEEPSEEK:
  12497. {
  12498. llm = std::make_unique<llm_build_deepseek>(*this, params, gf);
  12499. } break;
  12500. case LLM_ARCH_DEEPSEEK2:
  12501. {
  12502. llm = std::make_unique<llm_build_deepseek2>(*this, params, gf);
  12503. } break;
  12504. case LLM_ARCH_CHATGLM:
  12505. {
  12506. llm = std::make_unique<llm_build_chatglm>(*this, params, gf);
  12507. } break;
  12508. case LLM_ARCH_GLM4:
  12509. {
  12510. llm = std::make_unique<llm_build_glm4>(*this, params, gf);
  12511. } break;
  12512. case LLM_ARCH_BITNET:
  12513. {
  12514. llm = std::make_unique<llm_build_bitnet>(*this, params, gf);
  12515. } break;
  12516. case LLM_ARCH_T5:
  12517. {
  12518. switch (type) {
  12519. case LLM_GRAPH_TYPE_ENCODER:
  12520. llm = std::make_unique<llm_build_t5_enc>(*this, params, gf);
  12521. break;
  12522. case LLM_GRAPH_TYPE_DEFAULT:
  12523. case LLM_GRAPH_TYPE_DECODER:
  12524. llm = std::make_unique<llm_build_t5_dec>(*this, params, gf);
  12525. break;
  12526. default:
  12527. GGML_ABORT("invalid graph type");
  12528. };
  12529. } break;
  12530. case LLM_ARCH_T5ENCODER:
  12531. {
  12532. llm = std::make_unique<llm_build_t5_enc>(*this, params, gf);
  12533. }
  12534. break;
  12535. case LLM_ARCH_JAIS:
  12536. {
  12537. llm = std::make_unique<llm_build_jais>(*this, params, gf);
  12538. } break;
  12539. case LLM_ARCH_NEMOTRON:
  12540. {
  12541. llm = std::make_unique<llm_build_nemotron>(*this, params, gf);
  12542. } break;
  12543. case LLM_ARCH_EXAONE:
  12544. {
  12545. llm = std::make_unique<llm_build_exaone>(*this, params, gf);
  12546. } break;
  12547. case LLM_ARCH_RWKV6:
  12548. {
  12549. llm = std::make_unique<llm_build_rwkv6>(*this, params, gf);
  12550. } break;
  12551. case LLM_ARCH_RWKV6QWEN2:
  12552. {
  12553. llm = std::make_unique<llm_build_rwkv6qwen2>(*this, params, gf);
  12554. } break;
  12555. case LLM_ARCH_RWKV7:
  12556. {
  12557. llm = std::make_unique<llm_build_rwkv7>(*this, params, gf);
  12558. } break;
  12559. case LLM_ARCH_ARWKV7:
  12560. {
  12561. llm = std::make_unique<llm_build_arwkv7>(*this, params, gf);
  12562. } break;
  12563. case LLM_ARCH_GRANITE:
  12564. case LLM_ARCH_GRANITE_MOE:
  12565. case LLM_ARCH_MINICPM:
  12566. {
  12567. llm = std::make_unique<llm_build_granite>(*this, params, gf);
  12568. } break;
  12569. case LLM_ARCH_CHAMELEON:
  12570. {
  12571. llm = std::make_unique<llm_build_chameleon>(*this, params, gf);
  12572. } break;
  12573. case LLM_ARCH_WAVTOKENIZER_DEC:
  12574. {
  12575. llm = std::make_unique<llm_build_wavtokenizer_dec>(*this, params, gf);
  12576. } break;
  12577. case LLM_ARCH_PLM:
  12578. {
  12579. llm = std::make_unique<llm_build_plm>(*this, params, gf);
  12580. } break;
  12581. case LLM_ARCH_BAILINGMOE:
  12582. {
  12583. llm = std::make_unique<llm_build_bailingmoe>(*this, params, gf);
  12584. } break;
  12585. case LLM_ARCH_DOTS1:
  12586. {
  12587. llm = std::make_unique<llm_build_dots1>(*this, params, gf);
  12588. } break;
  12589. case LLM_ARCH_ARCEE:
  12590. {
  12591. llm = std::make_unique<llm_build_arcee>(*this, params, gf);
  12592. } break;
  12593. case LLM_ARCH_ERNIE4_5:
  12594. {
  12595. llm = std::make_unique<llm_build_ernie4_5>(*this, params, gf);
  12596. } break;
  12597. case LLM_ARCH_HUNYUAN_MOE:
  12598. {
  12599. llm = std::make_unique<llm_build_hunyuan_moe>(*this, params, gf);
  12600. } break;
  12601. case LLM_ARCH_SMOLLM3:
  12602. {
  12603. llm = std::make_unique<llm_build_smollm3>(*this, params, gf);
  12604. } break;
  12605. case LLM_ARCH_FALCON_H1:
  12606. {
  12607. llm = std::make_unique<llm_build_falcon_h1>(*this, params, gf);
  12608. } break;
  12609. default:
  12610. GGML_ABORT("fatal error");
  12611. }
  12612. // add on pooling layer
  12613. llm->build_pooling(gf, cls, cls_b, cls_out, cls_out_b);
  12614. return std::move(llm->res);
  12615. }
  12616. //
  12617. // interface implementation
  12618. //
  12619. llama_model_params llama_model_default_params() {
  12620. llama_model_params result = {
  12621. /*.devices =*/ nullptr,
  12622. /*.tensor_buft_overrides =*/ nullptr,
  12623. /*.n_gpu_layers =*/ 0,
  12624. /*.split_mode =*/ LLAMA_SPLIT_MODE_LAYER,
  12625. /*.main_gpu =*/ 0,
  12626. /*.tensor_split =*/ nullptr,
  12627. /*.progress_callback =*/ nullptr,
  12628. /*.progress_callback_user_data =*/ nullptr,
  12629. /*.kv_overrides =*/ nullptr,
  12630. /*.vocab_only =*/ false,
  12631. /*.use_mmap =*/ true,
  12632. /*.use_mlock =*/ false,
  12633. /*.check_tensors =*/ false,
  12634. };
  12635. #ifdef GGML_USE_METAL
  12636. // note: we usually have plenty of VRAM, so by default offload all layers to the GPU
  12637. result.n_gpu_layers = 999;
  12638. #endif
  12639. return result;
  12640. }
  12641. const llama_vocab * llama_model_get_vocab(const llama_model * model) {
  12642. return &model->vocab;
  12643. }
  12644. void llama_free_model(llama_model * model) {
  12645. llama_model_free(model);
  12646. }
  12647. void llama_model_free(llama_model * model) {
  12648. delete model;
  12649. }
  12650. int32_t llama_model_n_ctx_train(const llama_model * model) {
  12651. return model->hparams.n_ctx_train;
  12652. }
  12653. int32_t llama_model_n_embd(const llama_model * model) {
  12654. return model->hparams.n_embd;
  12655. }
  12656. int32_t llama_model_n_layer(const llama_model * model) {
  12657. return model->hparams.n_layer;
  12658. }
  12659. int32_t llama_model_n_head(const llama_model * model) {
  12660. return model->hparams.n_head();
  12661. }
  12662. int32_t llama_model_n_head_kv(const llama_model * model) {
  12663. return model->hparams.n_head_kv();
  12664. }
  12665. int32_t llama_model_n_swa(const llama_model * model) {
  12666. return model->hparams.n_swa;
  12667. }
  12668. uint32_t llama_model_n_cls_out(const struct llama_model * model) {
  12669. return model->hparams.n_cls_out;
  12670. }
  12671. const char * llama_model_cls_label(const struct llama_model * model, uint32_t i) {
  12672. if (i < model->classifier_labels.size()) {
  12673. return model->classifier_labels[i].c_str();
  12674. }
  12675. return nullptr;
  12676. }
  12677. // deprecated
  12678. int32_t llama_n_ctx_train(const llama_model * model) {
  12679. return llama_model_n_ctx_train(model);
  12680. }
  12681. // deprecated
  12682. int32_t llama_n_embd(const llama_model * model) {
  12683. return llama_model_n_embd(model);
  12684. }
  12685. // deprecated
  12686. int32_t llama_n_layer(const llama_model * model) {
  12687. return llama_model_n_layer(model);
  12688. }
  12689. // deprecated
  12690. int32_t llama_n_head(const llama_model * model) {
  12691. return llama_model_n_head(model);
  12692. }
  12693. llama_rope_type llama_model_rope_type(const llama_model * model) {
  12694. switch (model->arch) {
  12695. // these models do not use RoPE
  12696. case LLM_ARCH_GPT2:
  12697. case LLM_ARCH_GPTJ:
  12698. case LLM_ARCH_MPT:
  12699. case LLM_ARCH_REFACT:
  12700. case LLM_ARCH_BLOOM:
  12701. case LLM_ARCH_MAMBA:
  12702. case LLM_ARCH_MAMBA2:
  12703. case LLM_ARCH_JAMBA:
  12704. case LLM_ARCH_JINA_BERT_V2:
  12705. case LLM_ARCH_T5:
  12706. case LLM_ARCH_T5ENCODER:
  12707. case LLM_ARCH_JAIS:
  12708. case LLM_ARCH_RWKV6:
  12709. case LLM_ARCH_RWKV6QWEN2:
  12710. case LLM_ARCH_RWKV7:
  12711. case LLM_ARCH_ARWKV7:
  12712. case LLM_ARCH_WAVTOKENIZER_DEC:
  12713. return LLAMA_ROPE_TYPE_NONE;
  12714. // use what we call a normal RoPE, operating on pairs of consecutive head values
  12715. case LLM_ARCH_LLAMA:
  12716. case LLM_ARCH_LLAMA4:
  12717. case LLM_ARCH_DECI:
  12718. case LLM_ARCH_BAICHUAN:
  12719. case LLM_ARCH_STARCODER:
  12720. case LLM_ARCH_INTERNLM2:
  12721. case LLM_ARCH_MINICPM:
  12722. case LLM_ARCH_XVERSE:
  12723. case LLM_ARCH_COMMAND_R:
  12724. case LLM_ARCH_COHERE2:
  12725. case LLM_ARCH_OLMO:
  12726. case LLM_ARCH_ARCTIC:
  12727. case LLM_ARCH_DEEPSEEK:
  12728. case LLM_ARCH_DEEPSEEK2:
  12729. case LLM_ARCH_PLM:
  12730. case LLM_ARCH_CHATGLM:
  12731. case LLM_ARCH_GLM4:
  12732. case LLM_ARCH_GRANITE:
  12733. case LLM_ARCH_GRANITE_MOE:
  12734. case LLM_ARCH_CHAMELEON:
  12735. case LLM_ARCH_BAILINGMOE:
  12736. case LLM_ARCH_NEO_BERT:
  12737. case LLM_ARCH_SMOLLM3:
  12738. case LLM_ARCH_ARCEE:
  12739. case LLM_ARCH_ERNIE4_5:
  12740. return LLAMA_ROPE_TYPE_NORM;
  12741. // the pairs of head values are offset by n_rot/2
  12742. case LLM_ARCH_FALCON:
  12743. case LLM_ARCH_FALCON_H1:
  12744. case LLM_ARCH_GROK:
  12745. case LLM_ARCH_DBRX:
  12746. case LLM_ARCH_BERT:
  12747. case LLM_ARCH_NOMIC_BERT:
  12748. case LLM_ARCH_NOMIC_BERT_MOE:
  12749. case LLM_ARCH_STABLELM:
  12750. case LLM_ARCH_BITNET:
  12751. case LLM_ARCH_QWEN:
  12752. case LLM_ARCH_QWEN2:
  12753. case LLM_ARCH_QWEN2MOE:
  12754. case LLM_ARCH_QWEN3:
  12755. case LLM_ARCH_QWEN3MOE:
  12756. case LLM_ARCH_OLMO2:
  12757. case LLM_ARCH_OLMOE:
  12758. case LLM_ARCH_PHI2:
  12759. case LLM_ARCH_PHI3:
  12760. case LLM_ARCH_PHIMOE:
  12761. case LLM_ARCH_PLAMO:
  12762. case LLM_ARCH_GEMMA:
  12763. case LLM_ARCH_GEMMA2:
  12764. case LLM_ARCH_GEMMA3:
  12765. case LLM_ARCH_GEMMA3N:
  12766. case LLM_ARCH_STARCODER2:
  12767. case LLM_ARCH_OPENELM:
  12768. case LLM_ARCH_GPTNEOX:
  12769. case LLM_ARCH_CODESHELL:
  12770. case LLM_ARCH_ORION:
  12771. case LLM_ARCH_NEMOTRON:
  12772. case LLM_ARCH_EXAONE:
  12773. case LLM_ARCH_MINICPM3:
  12774. case LLM_ARCH_DOTS1:
  12775. case LLM_ARCH_HUNYUAN_MOE:
  12776. return LLAMA_ROPE_TYPE_NEOX;
  12777. case LLM_ARCH_QWEN2VL:
  12778. return LLAMA_ROPE_TYPE_MROPE;
  12779. // all model arches should be listed explicitly here
  12780. case LLM_ARCH_UNKNOWN:
  12781. GGML_ABORT("unknown architecture");
  12782. }
  12783. return LLAMA_ROPE_TYPE_NONE;
  12784. }
  12785. float llama_model_rope_freq_scale_train(const llama_model * model) {
  12786. return model->hparams.rope_freq_scale_train;
  12787. }
  12788. int32_t llama_model_meta_val_str(const llama_model * model, const char * key, char * buf, size_t buf_size) {
  12789. const auto & it = model->gguf_kv.find(key);
  12790. if (it == model->gguf_kv.end()) {
  12791. if (buf_size > 0) {
  12792. buf[0] = '\0';
  12793. }
  12794. return -1;
  12795. }
  12796. return snprintf(buf, buf_size, "%s", it->second.c_str());
  12797. }
  12798. int32_t llama_model_meta_count(const llama_model * model) {
  12799. return (int)model->gguf_kv.size();
  12800. }
  12801. int32_t llama_model_meta_key_by_index(const llama_model * model, int i, char * buf, size_t buf_size) {
  12802. if (i < 0 || i >= (int)model->gguf_kv.size()) {
  12803. if (buf_size > 0) {
  12804. buf[0] = '\0';
  12805. }
  12806. return -1;
  12807. }
  12808. auto it = model->gguf_kv.begin();
  12809. std::advance(it, i);
  12810. return snprintf(buf, buf_size, "%s", it->first.c_str());
  12811. }
  12812. int32_t llama_model_meta_val_str_by_index(const llama_model * model, int32_t i, char * buf, size_t buf_size) {
  12813. if (i < 0 || i >= (int)model->gguf_kv.size()) {
  12814. if (buf_size > 0) {
  12815. buf[0] = '\0';
  12816. }
  12817. return -1;
  12818. }
  12819. auto it = model->gguf_kv.begin();
  12820. std::advance(it, i);
  12821. return snprintf(buf, buf_size, "%s", it->second.c_str());
  12822. }
  12823. int32_t llama_model_desc(const llama_model * model, char * buf, size_t buf_size) {
  12824. return snprintf(buf, buf_size, "%s", model->desc().c_str());
  12825. }
  12826. uint64_t llama_model_size(const llama_model * model) {
  12827. return model->size();
  12828. }
  12829. const char * llama_model_chat_template(const llama_model * model, const char * name) {
  12830. const auto key = name ? LLM_KV(model->arch, name)(LLM_KV_TOKENIZER_CHAT_TEMPLATE)
  12831. : LLM_KV(model->arch)(LLM_KV_TOKENIZER_CHAT_TEMPLATE);
  12832. const auto & it = model->gguf_kv.find(key);
  12833. if (it == model->gguf_kv.end()) {
  12834. // one-off fix for very popular models (so we are not flooded with issues)
  12835. // do not extend this list unless absolutely necessary
  12836. // Mistral-Small-2503 does not have built-in chat template
  12837. llama_vocab_pre_type pre_type = model->vocab.get_pre_type();
  12838. if (!name && pre_type == LLAMA_VOCAB_PRE_TYPE_TEKKEN && model->layers.size() == 40) {
  12839. return "mistral-v7-tekken";
  12840. }
  12841. return nullptr;
  12842. }
  12843. return it->second.c_str();
  12844. }
  12845. uint64_t llama_model_n_params(const llama_model * model) {
  12846. return model->n_elements();
  12847. }
  12848. bool llama_model_has_encoder(const llama_model * model) {
  12849. switch (model->arch) {
  12850. case LLM_ARCH_T5: return true;
  12851. case LLM_ARCH_T5ENCODER: return true;
  12852. default: return false;
  12853. }
  12854. }
  12855. bool llama_model_has_decoder(const llama_model * model) {
  12856. switch (model->arch) {
  12857. case LLM_ARCH_T5ENCODER: return false;
  12858. default: return true;
  12859. }
  12860. }
  12861. llama_token llama_model_decoder_start_token(const llama_model * model) {
  12862. return model->hparams.dec_start_token_id;
  12863. }
  12864. bool llama_model_is_recurrent(const llama_model * model) {
  12865. return llm_arch_is_recurrent(model->arch);
  12866. }
  12867. const std::vector<std::pair<std::string, ggml_tensor *>> & llama_internal_get_tensor_map(const llama_model * model) {
  12868. return model->tensors_by_name;
  12869. }