# Overview of LLaMA series > 梳理 Meta LLaMA 系列(LLaMA 1/2/3/3.2/4)的模型架构、预训练配方、剪枝蒸馏与多模态演进。 - **URL**: https://maosong.website/blog/llama/ - **Authors**: Mao Song - **Published**: 2024-04-22 16:22:19+0800 --- ## Introduction | | | | | | | | -------------- | ------------------------------ | ------------------ | ------------------------------------------ | ---- | ----------- | | 时间 | series | model | infra | blog | Tech report | | 2023.2 | [LLaMA](/blog/llama/#llama) 1 | 7B,13B,33B,65B | - | | | | 2023.7 | [LLaMA 2](/blog/llama/#llama2)LLaMA 2 | 7B,13B,70B | - | | | | 2023.8-2023.12 | Code LLaMA,Purple LlaMA | 8B,70B | LLaMA Guard,CyberSec Eval | | | | 2024.4 | [LLaMA 3](/blog/llama/#llama3)LLaMA 3 | 8B 70B,405B | LLaMA Guarc 2,CyberSec Eval 2,Code Shield | | | | 2024.7 | LLaMA 3.1 | 1B,3B,LLaMA Vision | LLaMA Guard 3,CyberSec Eval 3,Prompt Guard | | | | 2024.9 | [LLaMA3.2](https://huggingface.co/meta-llama/Llama-3.2-11B-Vision-Instruct) | - | LlaMA Stack | | | | | | | | | | | | | | | | ----------------- | ----------------- | ---------------------- | ------------------------------------------ | | | LLaMA 1 | LLaMA 2 | LLaMA 3 | | License | Research Only | Commercial | Commercial | | Model sizes | 7b, 13b, 30b, 65b | 7b, 13b, 70b | 1b, 3b, 7b, 70b, 405b, multimodal | | Training data | 1~1.4t tokens | 2t tokens (+40%) | 15t tokens | | Context length | 2k | 4k | 128k | | Models avaliable | Pretraining only | Pretraining, finetuned | + llama guard, cybersec eval, prompt guard | | Pretraining flops | 5.4e23 | 8.3e23 | 3.8e25 | raining 1. Initial pre-training 2. Data mix adjustments 3. Long context pre-training 4. annealing Data - Data grows with compute - If the trend continues,the next 100 times of FLOPs scale, would require ~10 times model parameters and ~10x data Post-training challenges - Need for higher quality data - Need to carefully balance different objectives - Scaling laws aren't as developed - Evaluation difficuties Eval challenges - The lifecycle of a benchmark is too short - One model cycle from adoption to saturation - Training data pollution - some things are to measure - Quality of generated images - Quality of speech - Tone of response - General shortage of good benchmarks How to help make the next LLaMA better - Better community driven benchmarks - Measure what really matters to you - Not leaked into the training data - Better post training - DPO wasn't invented in a large industry lab - Scaling laws for post training - Better data ## LLaMA LLaMA [@LLaMA] 是 Meta 在2023年发布的一款开源大语言模型系列 ## LLaMA2 LLaMA2 [@LLaMA2] 是 Meta 在 2023 年 7月 发布的一款开源大语言模型系列。 ## LLaMA3 LLaMA3 [@LLaMA3] 是 meta 在24年4月发布的一款开源大语言模型系列。 ### Model Architecture Several improvements are made on Llama3 compared to llama2: 1. Llama3 uses a tokenizer with a vocabulary of 128K tokens. 2. Llama3 adopts [grouped query attention (GQA)](/blog/attention/#grouped-query-attention-gqa) across both the 8B and 70B sizes. 3. Llama3 uses to context window of size 8192 tokens ### Traning Llama3 uses 15T tokens for pre-training. Compares to Llama2, it is seven times larger and includes four times more code. 5% data of the training dataset are non-English to support multi-lingual use cases. Data processing includes: 1. Heuristic filters 2. NSFW filters 3. Semantic deduplication approaches 4. Text classifiers to predict data quality. Llama2 is used to generate training data for the text classifiers. Data mixing strategy is explored to improve the performance of Llama3. ### Scaling up pretraining Llama3 developed a series of scaling laws for downstream benchmark evaluations. Scaling laws help: 1. Select an optimal data mix and to make informed decisions on how to best use training compute. 2. Scaling laws allow Llama3 to predict the performance of the largest models on key tasks without training the models. The authors finds our that the performance of the model continues to improve log-linearly as the training tokens increase. It is seen that Larger models can match the performance of these smaller models with less training compute, but smaller models are generally preferred because they are much more efficient during inference. The authors combine three types of parallelization: 1. Data parallelization 2. Model parallelization 3. Pipeline parallelization ### Instruction fine-tuning The fine-tuning of Llama3 contains: 1. Supervised fine-tuning 2. Rejection sampling 3. Proximal Policy Optimization 4. Direct Preference Optimization Learning from perference rankings via PPO and [DPO](/blog/rlhf/#dpo) also greatly improved the performance of LLma3 on reasoning and coding tasks. Since perference ranking helps the model to select answer when it is in a dilemma. ## Instruct model performance The performance of Llama3 8B compared with Gemma and Mistral: | Model | Llama3 8B | Gemma 7B - It | Mistral &B Instruct | | ---------------------- | --------- | ------------- | ------------------- | | _MMLU_ (5 shot) | **68.4** | 53.3 | 58.4 | | _GPQA_ (0 shot) | **34.2** | 21.4 | 26.3 | | _HumanEval_ (0 shot) | **62.2** | 30.5 | 36.6 | | _GSM-8K_ (8 shot, CoT) | **79.6** | 30.6 | 39.9 | | _MATH_ (4 shot, CoT) | **30.0** | 12.2 | 11.0 | performance of Llama3 70B compared with Gemini Pro 1.5 and Claude Sonnet: | Model | Llama3 70B | Gemini Pro 1.5 (Published) | Claude 3 Sonnet (Published) | | ---------------------- | ---------- | -------------------------- | --------------------------- | | _MMLU_ (5 shot) | **82.0** | 81.9 | 79.0 | | _GPQA_ (0 shot) | 39.5 | **41.5** (CoT) | 38.5 (CoT) | | _HumanEval_ (0 shot) | **81.7** | 71.9 | 73.0 | | _GSM-8K_ (8 shot, CoT) | **93.0** | 91.7 (11 shot) | 92.3 (0 shot) | | _MATH_ (4 shot, CoT) | 50.4 | **58.5** (Minerva prompt) | 40.5 | ## Pre-trained model performance The performance of Llama3 8B compared with Gemma and Mistral: | Model | Llama3 8B | Gemma 7B (Published) | Gemma 7B (Measured) | Mistral 7B (Published) | Mistral 7B (Measured) | | ------------------------------ | --------- | -------------------- | ------------------- | ---------------------- | --------------------- | | _MMLU_ (5 shot) | **66.6** | 64.3 | 64.4 | 62.5 | 63.9 | | _AGIEval English_ (3-5 shot) | **45.9** | 41.7 | 44.9 | - | 44.0 | | _BIG-Bench Hard_ (3 shot, CoT) | **61.1** | 55.1 | 59.0 | - | 56.0 | | _ARC-Challenge_ (25 shot) | 78.6 | 53.2(0 shot) | **79.1** | 78.1 | 78.7 | | _DROP_ (3 shot, F1) | **58.4** | - | 56.3 | - | 54.4 | performance of Llama3 70B compared with Gemini Pro 1.5 and Claude Sonnet: | Model | Llama3 70B | Gemini Pro 1.0 (Published) | Mixtral 8 $\times$ 22B (Measured) | | ------------------------------ | ---------- | -------------------------- | --------------------------------- | | _MMLU_ (5-shot) | **79.5** | 71.8 | 77.7 | | _AGIEval English_ (3-5 shot) | **63.0** | - | 61.2 | | _BIG-Bench Hard_ (3 shot, CoT) | **81.3** | 75.0 | 79.2 | | _ARC-Challenge_ (25 shot) | **93.0** | - | 90.7 | | _DROP_ (3 shot, F1) | **79.7** | 74.1 (variable shot) | 77.6 | ## LLaMA4 Meta 在 2025 年 4 月 10 号发布了 LLaMA4 系列 [@LLaMA4],包含三个模型:Llama 4 Scout, Llama 4 Maverick 以及Llama 4 Behemoth, 三个模型都基于 MoE 架构,且支持多模态。 模型信息如下表所示 | Model | Layers | Heads (Q / KV) | Context Length | #Parameters (activated/total) | #Experts (activated/total) | #Tokens | | ---------------- | ------ | -------------- | -------------- | ----------------------------- | --------------------------- | ------- | | LLaMA 4 Behemoth | - | - | - | 288B / 2T | (1 shared + 1 routed) / 16 | - | | LLaMA 4 Maverick | 48 | 40/8 | 1M | 17B / 109B | (1 shared + 1 routed) / 128 | ~22T | | LLaMA 4 Scout | 48 | 40/8 | 10M | 17B / 400B | (1 shared + 1 routed) / 16 | ~40T | ### Architecture Attention, 作者采用了 iRoPE, 即 attention layer 交错使用 RoPE [@kazemnejad2023the] / NoPE [@kazemnejad2023the]. 作者还使用了 SSMax [@SSMax] 来提高 length generalization. FFN layer, LLaMA 4 采用了 interleaving MoE and FFN 的设计,MoE layer 中,LLaMA 4 的共享专家个数为 1, 路由专家个数为 128, 其中激活个数为 1. 多模态这一部分,vision encoder 基于 MetaCLIP, 其不同点在于 patch embedding 没有使用 convolution, 而是使用 `nn.Unfold` 直接进行展平,然后使用一个线性层与 vision encoder 进行对齐。[代码](https://github.com/huggingface/transformers/blob/5f8d17268ced2ca5f51b0216782356b16be0d6f4/src/transformers/models/llama4/modeling_llama4.py#L1409) 如下 ```python class Llama4UnfoldConvolution(nn.Module): def __init__(self, config): super().__init__() kernel_size = config.patch_size if isinstance(kernel_size, int): kernel_size = (kernel_size, kernel_size) self.unfold = torch.nn.Unfold(kernel_size=kernel_size, stride=config.patch_size) self.linear = nn.Linear( config.num_channels * kernel_size[0] * kernel_size[1], config.hidden_size, bias=False, ) def forward(self, hidden_states: torch.Tensor) -> torch.Tensor: hidden_states = self.unfold(hidden_states) hidden_states = hidden_states.permute(0, 2, 1) hidden_states = self.linear(hidden_states) return hidden_states ``` ### Training pre-training 的训练优化技巧如下: 1. MetaP:用于选择超参数 2. FP8 precision:与 [DeepSeek-V3](/blog/deepseek/#deepseek-v3) 一样,使用FP8精度进行训练 3. mid-training:在预训练阶段之后,额外增加了一个训练阶段,来提高模型的长上下文等关键能力 post-training 包括三个阶段: 1. SFT 2. online RL 3. [DPO](/blog/rlhf/#dpo) 作者发现 SFT 和 DPO 会限制模型的探索能力,特别是在 math, coding 等 domain. 为了解决这个问题,作者使用LlaMA对问题进行难度分级,然后移除了 50% 的简单数据,进而进行一个 light SFT 训练。 在 online RL 阶段,作者设计了一个 continuous online RL 策略,让模型在训练和筛选问题两种模式之间进行切换,以平衡效率和准确率。 DPO 的目的是为了提升模型输出的质量。 作者还使用 LLaMA 4 Behemoth 作为 teacher model, 来蒸馏 LLaMA 4 Maverick, 训练时,作者基于训练情况动态调整了 soft targets 和 hard targets 的权重。