# Copyright (c) Meta Platforms, Inc. and affiliates.# All rights reserved.## This source code is licensed under the BSD-style license found in the# LICENSE file in the root directory of this source tree.from__future__importannotationsfromdataclassesimportdataclass,fieldfromtypingimportFinal,Literalfromfairseq2.runtime.config_registryimportConfigRegistrarfromfairseq2.runtime.dependencyimportDependencyContainerLLAMA_FAMILY:Final="llama"
[docs]@dataclass(kw_only=True)classLLaMAConfig:"""Holds the configuration of a LLaMA model. The default values correspond to the base architecture as described in :cite:t:`https://doi.org/10.48550/arxiv.2302.13971`. """model_dim:int=4096"""The dimensionality of the model."""max_seq_len:int=2048"""The maximum sequence length."""vocab_size:int=32_000"""The size of the vocabulary."""pad_idx:int|None=None"""The index of the PAD symbol in the vocabulary."""tied_embeddings:bool=False"""If ``True``, ties the embedding table and the output projection layer."""num_layers:int=32"""The number of decoder layers."""num_attn_heads:int=32"""The number of attention heads in decoder layers."""num_key_value_heads:int=32"""The number of key/value heads for Grouped Query Attention."""ffn_inner_dim:int=4096*4"""The dimensionality of inner projection layers in feed-forward networks."""ffn_inner_dim_scale:float=2/3""" The scale factor for the dimensionality of inner projection layers in feed-forward networks. """ffn_inner_dim_multiplier:float=1.0""" The multiplier for the dimensionality of inner projection layers in feed-forward networks. """ffn_inner_dim_multiple_of:int=256"""The dimensionality of inner projection layers in feed-forward networks is rounded up to the nearest multiple of this value."""rope_theta:float=10_000.0"""The coefficient of the long-term decay of the Rotary position encoder."""use_scaled_rope:bool=False"""If ``True``, scales Rotary encoder frequencies to the resolver length."""rope_scale:LLaMARoPEScaleConfig=field(default_factory=lambda:LLaMARoPEScaleConfig())""" If not ``None``, specifies scaling parameters for the Rotary position encoder, aiming to increase the resolver length. """dropout_p:float=0.0"""The dropout probability on outputs of Transformer layers."""init_std:float|None=None""" If not ``None``, the standard deviation to initialize input embeddings and projection weights; otherwise, ``model_dim ** -0.5`` will be used instead. """init_std_scale:Literal["none","layer","stack"]="layer"""" The method to use to scale ``init_std`` per layer. If 'none', no scaling will be applied. If 'layer', ``init_std`` will be scaled by the depth of the layer. If 'stack', ``init_std`` will be scaled by the total depth of the decoder. """shard_embed_dim:bool=True"""If ``True``, shards the embedding dimension for tensor parallelism."""
@dataclassclassLLaMARoPEScaleConfig:""" Holds the frequency scaling configuration for the Rotary position encoder in LLaMA models. """factor:float=8.0""" The ratio between the intended maximum resolver length and the original maximum resolver length of the model. """frequency_factors:tuple[float,float]=(1.0,4.0)"""The factor used to define low and high frequencies."""original_context_length:int=8192"""The original resolver length. Defaults to LLaMA 3's resolver length."""defregister_llama_configs(container:DependencyContainer)->None:arch=ConfigRegistrar(container,LLaMAConfig)@arch("7b")def_7b()->LLaMAConfig:returnLLaMAConfig()@arch("13b")def_13b()->LLaMAConfig:config=LLaMAConfig()config.model_dim=5120config.num_attn_heads=40config.num_key_value_heads=40config.ffn_inner_dim=5120*4returnconfig@arch("33b")def_33b()->LLaMAConfig:config=LLaMAConfig()config.model_dim=6656config.num_layers=60config.num_attn_heads=52config.num_key_value_heads=52config.ffn_inner_dim=6656*4returnconfig@arch("65b")def_65b()->LLaMAConfig:config=LLaMAConfig()config.model_dim=8192config.num_layers=80config.num_attn_heads=64config.num_key_value_heads=64config.ffn_inner_dim=8192*4returnconfig@arch("llama2_7b")defllama2_7b()->LLaMAConfig:config=LLaMAConfig()config.max_seq_len=4096returnconfig@arch("llama2_13b")defllama2_13b()->LLaMAConfig:config=_13b()config.max_seq_len=4096returnconfig@arch("llama2_70b")defllama2_70b()->LLaMAConfig:config=_65b()config.max_seq_len=4096config.num_key_value_heads=8config.ffn_inner_dim=8192*4config.ffn_inner_dim_multiplier=1.3# See A.2.1 in LLaMA 2config.ffn_inner_dim_multiple_of=4096returnconfig@arch("llama3_8b")defllama3_8b()->LLaMAConfig:config=llama2_7b()config.max_seq_len=8192config.vocab_size=128_256config.pad_idx=128_004config.num_key_value_heads=8config.ffn_inner_dim=4096*4config.ffn_inner_dim_multiplier=1.3config.ffn_inner_dim_multiple_of=1024config.rope_theta=500_000.0config.shard_embed_dim=Falsereturnconfig@arch("llama3_70b")defllama3_70b()->LLaMAConfig:config=llama2_70b()config.max_seq_len=8192config.vocab_size=128_256config.pad_idx=128_004config.rope_theta=500_000.0config.shard_embed_dim=Falsereturnconfig@arch("llama3_1_8b")defllama3_1_8b()->LLaMAConfig:config=llama3_8b()config.max_seq_len=131_072config.use_scaled_rope=Truereturnconfig@arch("llama3_1_70b")defllama3_1_70b()->LLaMAConfig:config=llama3_70b()config.max_seq_len=131_072config.use_scaled_rope=Truereturnconfig@arch("llama3_2_3b")defllama3_2_3b()->LLaMAConfig:config=llama3_1_8b()config.model_dim=3072config.tied_embeddings=Trueconfig.ffn_inner_dim=3072*4config.ffn_inner_dim_multiplier=1.0config.ffn_inner_dim_multiple_of=256config.num_attn_heads=24config.num_key_value_heads=8config.num_layers=28config.use_scaled_rope=Trueconfig.rope_scale.factor=32.0returnconfig@arch("llama3_2_1b")defllama3_2_1b()->LLaMAConfig:config=llama3_1_8b()config.model_dim=2048config.tied_embeddings=Trueconfig.ffn_inner_dim=2048*4config.ffn_inner_dim_multiplier=1.5config.ffn_inner_dim_multiple_of=256config.num_attn_heads=32config.num_key_value_heads=8config.num_layers=16config.use_scaled_rope=Trueconfig.rope_scale.factor=32.0returnconfig