Llama, Mistral and other Llama-like model support in Megatron-LM
NOTE: In order to simplify code we now only support converting llama-3.x and mistral checkpoints downloaded from Hugging Face. For converting other models, see Megatron Bridge.
The Llama-2 and Llama-3.x family of models are an open-source set of pretrained & finetuned (for chat) models that have achieved strong results across a wide set of benchmarks. At their times of release, both Llama-2 and Llama-3 models achieved among the best results for open-source models, and were competitive with leading closed-source models (see https://arxiv.org/pdf/2307.09288.pdf).
Similarly, Mistral-7b is an open-source model with pretrained and finetuned (for chat) variants that achieve strong benchmark results.
Architecturally Llama-2, Llama-3 and Mistral-7b are very similar. As such Megatron can support loading checkpoints from all three for inference and finetuning. Converting the checkpoints and loading them is slightly different for each model and is detailed for each below.
Contents
- Llama, Mistral and other Llama-like model support in Megatron-LM
- Contents
- Llama-2
- Llama-3.x
- Mistral-7b
- Other Llama-like model support
- Known numerical differences
Llama-2
Llama-2 checkpoints can be loaded into Megatron for inference and for finetuning. Loading these checkpoints consists of three steps:
- Get access to download the checkpoints.
- Convert the checkpoints from Huggingface format to Megatron format.
- Setup arguments for launching the model.
The following sections detail these steps. The final section lists benchmark result comparisons between: 1) Llama-2 inference code running the Meta-format checkpoints, and 2) Megatron inference code running the converted checkpoints.
Download Huggingface checkpoints
Users must first apply for access to download the Llama-2 checkpoints either directly Huggingface (HF). The checkpoints are available in HF’s format (available only from HF). HF format can be converted to Megatron, as detailed next.
Convert checkpoint format
We recommend passing --dtype bf16 for training or finetuning. Inference can be done in bfloat16 or float16.
Huggingface format
The HF checkpoints can be converted to Megatron format by using Megatron-Bridge’s checkpoint converter for HF format see script.
After this conversion, we are ready to load the checkpoints into a Megatron GPT model.
Launch model
Launch Megatron
If loading for either inference or finetuning, use the following arguments:
Launch Huggingface
Huggingface checkpoints can be launched with: https://github.com/huggingface/transformers/blob/main/src/transformers/models/llama/modeling_llama.py
Benchmark results
The tables below list the benchmark comparisons between native Llama-2 (using Meta’s checkpoint and Meta’s inference code) and Megatron (using a converted HF checkpoint and Megatron’s inference code).
The values are the percent error between Megatron and Llama-2, calculated using the formula: |<llama_score> - <megatron_score>| / <llama_score>, where the type of score is detailed before each table. Across all tests (80 total per model size), the mean error is 0.15%. The small difference in benchmark scores between the two models is due to minor arithmetic differences in implementation that alter the numerics slightly. Some of the factors that influence this difference include:
- Megatron performs batch matrix multiplications in a couple places, such as within self attention and in SwiGLU, that Llama performs separately.
- Megatron uses
torch.baddbmmwithin self attention, versus Llama usingtorch.matmul. - Megatron uses a
sin/cosimplementation for rotary position embeddings, versus Llama using apolar/compleximplementation. - Llama calls
torch.set_default_dtype(torch.float16)during initialization, which Megatron does not.
Big Bench
Score type: multiple choice grade.
Multilingual
Score type: multiple choice grade.
LM Evaluation Harness
Score type: multiple choice grade.
MMLU
Score type: multiple choice grade.
Note: the number in brackets is the number of sub-tasks for each supercategory.
Llama-3.x
Llama-3.x checkpoints can be loaded into Megatron for inference and for finetuning. Loading these checkpoints consists of several steps:
- Get access to download the checkpoints (weights and tokenizer).
- Convert the checkpoints from Huggingface format to Megatron format.
- (Optional) Validate converted checkpoints
- Setup arguments for launching the model.
The following sections detail these steps.
Download Huggingface checkpoints
Users must first apply for access to download the Llama-3.x checkpoints from Huggingface.
Convert checkpoint format
We recommend passing --dtype bf16 for training or finetuning. Inference can be done in bfloat16 or float16.
Huggingface format
The HF checkpoints can be converted to Megatron format by using Megatron-Bridge’s checkpoint converter for HF format see script.
After this conversion, we are ready to load the checkpoints into a Megatron GPT model.
Launch model
If loading for either inference or finetuning, use the following arguments for Llama 3.0:
For Llama3.1 please use the following arguments:
Mistral-7b
Megatron currently supports loading the v0.3 release of Mistral-7b (which does not use sliding window attention and offers a larger 32768 vocabulary) for inference and finetuning. Loading these checkpoints consists of several steps:
- Get access to download the checkpoints (weights and tokenizer).
- Convert the checkpoints from HuggingFace format to Megatron format.
- (Optional) Validate converted checkpoints
- Setup arguments for launching the model.
The following sections detail these steps.
Download Huggingface checkpoints
Users must first apply for access to download the Mistral-7b checkpoints through Huggingface. Two variants are available: the base model (Mistral-7B-v0.3) and the instruct model (Mistral-7B-Instruct-v0.3).
Convert checkpoint format
The HF checkpoints can be converted to Megatron format by using Megatron-Bridge’s checkpoint converter for HF format see script.
After this conversion, we are ready to load the checkpoints into a Megatron GPT model.
Launch model
If loading for either inference or finetuning, use the following arguments:
Other Llama-like model support
Note: Experimental
Many models such as Yi-34B and Qwen2.x use the Llama architecture and may be converted from HuggingFace to Megatron using the commands in Llama-3.x.
Known numerical differences
It is not expected that the megatron and Huggingface implementations of llama3.x and mistral models will produce numerically identical results. There are multiple points where small numerical differences are expected. This is a non-exhaustive list:
- TransformerEngine (TE) uses the model params_dtype inside RMSNorm whereas the Huggingface implementation uses fp32. See for details: https://github.com/NVIDIA/TransformerEngine/issues/1132
- Huggingface
transformersimplements the q, k and v projections in self-attention as separate GEMMs whereas Megatron core combines them into a single GEMM for efficiency. This leads to small numerical differences.