Llama models, developed by Meta AI, are trained using a multi-stage process common to many large language models (LLMs), but with specific implementations and datasets that have evolved across versions (Llama 1, Llama 2, Llama 3). Here's a breakdown of the general training process:
-
Pre-training:
- Architecture: Llama models use a standard transformer architecture (specifically, a decoder-only transformer)[1]. Key components include pre-normalization with RMSNorm, the SwiGLU activation function, and rotary positional embeddings[2]. Llama 3 introduced grouped-query attention (GQA) to improve inference efficiency[1][3][4].
- Objective: The primary goal during pre-training is to predict the next token (word or sub-word) in a sequence. This is known as an autoregressive objective. The model learns grammar, facts, reasoning abilities, and coding skills from the vast dataset it processes.
- Data: Llama models are pre-trained on massive datasets consisting of text and code collected from publicly available sources[1][2][3].
- Llama 1: Trained on 1.4 trillion tokens from sources like CommonCrawl, C4, GitHub, Wikipedia, books, and ArXiv papers.
- Llama 2: Trained on 2 trillion tokens from publicly available online data, with increased data curation to remove sites with high personal information and upsample trustworthy sources[5]. Context length was doubled compared to Llama 1 (to 4096 tokens)[6].
- Llama 3: Pre-trained on over 15 trillion tokens, seven times larger than Llama 2's dataset, including four times more code[1]. Over 5% of the data covers 30+ non-English languages[1][5]. Meta invested heavily in data filtering pipelines and used Llama 2 to help classify data quality for Llama 3 training[7]. The context length was doubled again to 8,192 tokens. Data freshness cutoffs are March 2023 (8B model) and December 2023 (70B model)[3].
- Tokenization: Llama models use a byte-pair encoding (BPE) algorithm for tokenization[2]. Llama 3 uses an improved tokenizer with a larger vocabulary (128K tokens compared to Llama 2's 32K tokens) for more efficient language encoding.
- Infrastructure: Training these models requires significant computational resources, utilizing Meta's custom training libraries and large GPU clusters (like the Research SuperCluster). For Llama 3, Meta reported using 7.7 million GPU hours on H100 hardware.
-
Fine-tuning (for Instruct/Chat models):
- Starting with Llama 2, Meta released instruction-tuned models (e.g., Llama Chat) designed for dialogue. This involves further training stages:
- Supervised Fine-Tuning (SFT): The pre-trained model is fine-tuned on a smaller, high-quality dataset of instruction-prompt and response pairs. This teaches the model to follow instructions and respond in a helpful manner[6]. For Llama 2, Meta used publicly available instruction datasets plus over 1 million new human annotations[5]. For Llama 3, the fine-tuning data included public datasets and over 10 million human-annotated examples[3][5][7]. During SFT, the model is trained to predict only the response tokens, not the prompt tokens.
- Reinforcement Learning with Human Feedback (RLHF): This stage aligns the model's behavior more closely with human preferences for helpfulness and safety[3][4].
- Reward Modeling: A separate reward model is trained on human preference data, where annotators compare and rank different model responses to the same prompt. Llama 2 used two reward models (one for helpfulness, one for safety)[8].
- RL Optimization: The language model is further fine-tuned using reinforcement learning algorithms (like Proximal Policy Optimization - PPO or Direct Preference Optimization - DPO) to maximize the score given by the reward model, encouraging safer and more helpful responses. Llama 3's fine-tuning combined rejection sampling, PPO, and DPO[9].
In essence, Llama models are first pre-trained on a massive, diverse dataset to gain general language understanding and capabilities[2][10][11]. Then, specific versions (like chat models) undergo fine-tuning using curated datasets and human feedback to align them better with desired conversational behavior and safety standards[4].