{"id":13994103,"url":"https://github.com/bigcode-project/Megatron-LM","last_synced_at":"2025-07-22T18:33:25.823Z","repository":{"id":61602821,"uuid":"547349651","full_name":"bigcode-project/Megatron-LM","owner":"bigcode-project","description":"Ongoing research training transformer models at scale","archived":false,"fork":true,"pushed_at":"2024-08-20T12:14:29.000Z","size":6679,"stargazers_count":376,"open_issues_count":30,"forks_count":49,"subscribers_count":8,"default_branch":"multi-query-attention","last_synced_at":"2024-11-29T15:50:56.454Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"NVIDIA/Megatron-LM","license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bigcode-project.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-10-07T14:34:53.000Z","updated_at":"2024-11-25T02:55:48.000Z","dependencies_parsed_at":"2023-02-16T11:31:34.403Z","dependency_job_id":null,"html_url":"https://github.com/bigcode-project/Megatron-LM","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/bigcode-project/Megatron-LM","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bigcode-project%2FMegatron-LM","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bigcode-project%2FMegatron-LM/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bigcode-project%2FMegatron-LM/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bigcode-project%2FMegatron-LM/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bigcode-project","download_url":"https://codeload.github.com/bigcode-project/Megatron-LM/tar.gz/refs/heads/multi-query-attention","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bigcode-project%2FMegatron-LM/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266552569,"owners_count":23947179,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-22T02:00:09.085Z","response_time":66,"last_error":null,"robots_txt_status":null,"robots_txt_updated_at":null,"robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-09T14:02:42.505Z","updated_at":"2025-07-22T18:33:20.784Z","avatar_url":"https://github.com/bigcode-project.png","language":"Python","readme":"Megatron ([1](https://arxiv.org/pdf/1909.08053.pdf), [2](https://arxiv.org/pdf/2104.04473.pdf), and [3](https://arxiv.org/pdf/2205.05198)) is a large, powerful transformer developed by the Applied Deep Learning Research team at NVIDIA. This repository is for ongoing research on training large transformer language models at scale. We developed efficient, model-parallel ([tensor](https://arxiv.org/pdf/1909.08053.pdf), [sequence](https://arxiv.org/pdf/2205.05198), and [pipeline](https://arxiv.org/pdf/2104.04473.pdf)), and multi-node pre-training of transformer based models such as [GPT](https://arxiv.org/abs/2005.14165), [BERT](https://arxiv.org/pdf/1810.04805.pdf), and [T5](https://arxiv.org/abs/1910.10683) using mixed precision.\n\nBelow are some of the projects where we have directly used Megatron:\n* [BERT and GPT Studies Using Megatron](https://arxiv.org/pdf/1909.08053.pdf)\n* [BioMegatron: Larger Biomedical Domain Language Model](https://www.aclweb.org/anthology/2020.emnlp-main.379.pdf)\n* [End-to-End Training of Neural Retrievers for Open-Domain Question Answering](https://arxiv.org/abs/2101.00408)\n* [Large Scale Multi-Actor Generative Dialog Modeling](https://www.aclweb.org/anthology/2020.acl-main.8.pdf)\n* [Local Knowledge Powered Conversational Agents](https://arxiv.org/abs/2010.10150)\n* [MEGATRON-CNTRL: Controllable Story Generation with External Knowledge Using Large-Scale Language Models](https://www.aclweb.org/anthology/2020.emnlp-main.226.pdf)\n* [RACE Reading Comprehension Dataset Leaderboard](http://www.qizhexie.com/data/RACE_leaderboard.html)\n* [Training Question Answering Models From Synthetic Data](https://www.aclweb.org/anthology/2020.emnlp-main.468.pdf)\n* [Few-shot Instruction Prompts for Pretrained Language Models to Detect Social Biases](https://arxiv.org/abs/2112.07868)\n* [Exploring the Limits of Domain-Adaptive Training for Detoxifying Large-Scale Language Models](https://arxiv.org/abs/2202.04173)\n* [Using DeepSpeed and Megatron to Train Megatron-Turing NLG 530B, A Large-Scale Generative Language Model](https://arxiv.org/abs/2201.11990)\n* [Multi-Stage Prompting for Knowledgeable Dialogue Generation](https://arxiv.org/abs/2203.08745)\n\nMegatron is also used in [NeMo Megatron](https://developer.nvidia.com/nvidia-nemo#nemo-megatron), a framework to help enterprises overcome the challenges of building and training sophisticated natural language processing models with billions and trillions of parameters.\n\nOur codebase is capable of efficiently training very large (hundreds of billions of parameters) language models with both model and data parallelism. To demonstrate how the code scales with multiple GPUs and model sizes, we consider GPT models from 1 billion all the way to 1 trillion parameters. All models use a vocabulary size of 51,200 and a sequence length of 2048. We vary hidden size, number of attention heads, and number of layers to arrive at a specifc model size. As the model size increases, we also modestly increase the batch size. We leverage [NVIDIA's Selene supercomputer](https://www.top500.org/system/179842/) to perform scaling studies and use up to 3072 [A100](https://www.nvidia.com/en-us/data-center/a100/) GPUs for the largest model. Each cluster node has 8 NVIDIA 80GB A100 GPUs. The graph below shows that we scale nearly linear up to 1 trillion parameter models running on 3072 GPUs. Note that these results are from benchmark runs and these models were not trained to convergence; however, the FLOPs are measured for end-to-end training, i.e., includes all operations including data loading, optimization, and even logging.\n\n![Scaling Graph](images/Achieved_petaFLOPs.png)\n\nThe following table shows both model (MFU) and hardware (HFU) FLOPs utilization for select configurations up to 1T parameters (see [our paper](https://arxiv.org/pdf/2205.05198) for a description of how these are calculated). As the model size increases, we achieve better GPU utilization and for the one trillion parameter model, we reach a MFU and HFU of 56.3% and 57.0%, respectively. Note that these numbers are also measured on benchmark runs and in this case are measured using a data parallel size of one. Data parallelism introduces some overhead due to the gradient all-reduce required between the data parallel groups. However, for large transformer models, this overhead is not large and can almost entirely eliminted by overlapping the gradient all-reduce with backpropagation.\n\n| Model Size | Model FLOPs Utilization | Hardware FLOPs Utilization |\n| :---: | :---: | :---: |\n| 22B   | 41.5% | 43.7% |\n| 175B  | 51.4% | 52.8% |\n| 530B  | 56.0% | 57.0% |\n| 1T    | 56.3% | 57.0% |\n\n# Contents\n   * [Contents](#contents)\n   * [Setup](#setup)\n      * [Downloading Checkpoints](#downloading-checkpoints)\n   * [Usage](#usage)\n   * [Training](#training)\n      * [Data Preprocessing](#data-preprocessing)\n      * [BERT Pretraining](#bert-pretraining)\n      * [GPT Pretraining](#gpt-pretraining)\n      * [T5 Pretraining](#t5-pretraining)\n      * [Distributed Pretraining](#distributed-pretraining)\n      * [Activation Checkpointing and Recomputation](#activation-checkpointing-and-recomputation)\n      * [Distributed Optimizer](#distributed-optimizer)\n      * [FlashAttention](#flashattention)\n      * [GPT-3 Example](#gpt-3-example)\n      * [Retro](#retro)\n   * [Evaluation and Tasks](#evaluation-and-tasks)\n      * [GPT Text Generation](#gpt-text-generation)\n      * [GPT Evaluation](#gpt-evaluation)\n         * [WikiText Perplexity Evaluation](#wikitext-perplexity-evaluation)\n         * [LAMBADA Cloze Accuracy](#lambada-cloze-accuracy)\n      * [BERT Task Evaluation](#bert-task-evaluation)\n         * [RACE Evaluation](#race-evaluation)\n         * [MNLI Evaluation](#mnli-evaluation)\n   * [Datasets](#datasets)\n      * [Collecting Wikipedia Training Data](#collecting-wikipedia-training-data)\n      * [Collecting GPT Webtext Data](#collecting-gpt-webtext-data)\n\n# Setup\nWe strongly recommend using the latest release of [NGC's PyTorch container](https://ngc.nvidia.com/catalog/containers/nvidia:pytorch) with DGX nodes. If you can't use this for some reason, use the latest pytorch, cuda, nccl, and NVIDIA [APEX](https://github.com/NVIDIA/apex#quick-start) releases.  Data preprocessing requires [NLTK](https://www.nltk.org/install.html), though this is not required for training, evaluation, or downstream tasks.\n\nYou can launch an instance of the PyTorch container and mount Megatron, your dataset, and checkpoints with the following Docker commands:\n```\ndocker pull nvcr.io/nvidia/pytorch:xx.xx-py3\ndocker run --gpus all -it --rm -v /path/to/megatron:/workspace/megatron -v /path/to/dataset:/workspace/dataset -v /path/to/checkpoints:/workspace/checkpoints nvcr.io/nvidia/pytorch:xx.xx-py3\n```\n\n## Downloading Checkpoints\nWe have provided pretrained [BERT-345M](https://ngc.nvidia.com/catalog/models/nvidia:megatron_bert_345m) and [GPT-345M](https://ngc.nvidia.com/catalog/models/nvidia:megatron_lm_345m) checkpoints for use to evaluate or finetuning downstream tasks. To access these checkpoints, first [sign up](https://ngc.nvidia.com/signup) for and [setup](https://ngc.nvidia.com/setup/installers/cli) the NVIDIA GPU Cloud (NGC) Registry CLI. Further documentation for downloading models can be found in the [NGC documentation](https://docs.nvidia.com/dgx/ngc-registry-cli-user-guide/index.html#topic_6_4_1).\n\nAlternatively, you can directly download the checkpoints using:\n\n\u003cpre\u003e\nBERT-345M-uncased: wget --content-disposition https://api.ngc.nvidia.com/v2/models/nvidia/megatron_bert_345m/versions/v0.1_uncased/zip -O megatron_bert_345m_v0.1_uncased.zip\nBERT-345M-cased: wget --content-disposition https://api.ngc.nvidia.com/v2/models/nvidia/megatron_bert_345m/versions/v0.1_cased/zip -O megatron_bert_345m_v0.1_cased.zip\nGPT-345M: wget --content-disposition https://api.ngc.nvidia.com/v2/models/nvidia/megatron_lm_345m/versions/v0.0/zip -O megatron_lm_345m_v0.0.zip\n\u003c/pre\u003e\n\nThe models require vocabulary files to run. The BERT  WordPiece vocab file can be extracted from Google's pretrained BERT models: [uncased](https://s3.amazonaws.com/models.huggingface.co/bert/bert-large-uncased-vocab.txt), [cased](https://s3.amazonaws.com/models.huggingface.co/bert/bert-large-cased-vocab.txt). The GPT [vocab file](https://s3.amazonaws.com/models.huggingface.co/bert/gpt2-vocab.json) and [merge table](https://s3.amazonaws.com/models.huggingface.co/bert/gpt2-merges.txt) can be downloaded directly.\n\n# Usage\n\nAfter installation, there are several possible workflows. The most comprehensive is:\n1. Data preprocessing\n2. Pretraining\n3. Finetuning (Optional for zero-shot tasks)\n4. Downstream task evaluation or text generation\n\nHowever, steps 1 and 2 can be replaced by using one of the pretrained models mentioned above.\n\nWe've provided several scripts for pretraining both BERT and GPT in [`examples`](./examples) directory, as well as scripts for both zero-shot and fine-tuned downstream tasks including MNLI, RACE, WikiText103, and LAMBADA evaluation. There is also a script for GPT interactive text generation.\n\n# Training\n## Data Preprocessing\nThe training data requires preprocessing. First, place your training data in a loose json format, with one json containing a text sample per line. For example:\n\u003cpre\u003e\n{\"src\": \"www.nvidia.com\", \"text\": \"The quick brown fox\", \"type\": \"Eng\", \"id\": \"0\", \"title\": \"First Part\"}\n{\"src\": \"The Internet\", \"text\": \"jumps over the lazy dog\", \"type\": \"Eng\", \"id\": \"42\", \"title\": \"Second Part\"}\n\u003c/pre\u003e\n\nThe name of the `text` field of the json can be changed by using the `--json-key` flag in [`preprocess_data.py`](./tools/preprocess_data.py) The other metadata are optional and are not used in training.\n\nThe loose json is then processed into a binary format for training. To convert the json into mmap, cached index file, or the lazy loader format use `preprocess_data.py`. Set the `--dataset-impl` flag to `mmap`, `cached`, or `lazy`, respectively (default is `mmap`). An example script to prepare data for BERT training is:\n\u003cpre\u003e\npython tools/preprocess_data.py \\\n       --input my-corpus.json \\\n       --output-prefix my-bert \\\n       --vocab bert-vocab.txt \\\n       --dataset-impl mmap \\\n       --tokenizer-type BertWordPieceLowerCase \\\n       --split-sentences\n\u003c/pre\u003e\n\nThe output will be two files named, in this case, `my-bert_text_sentence.bin` and `my-bert_text_sentence.idx`. The `--data-path` specified in later BERT training is the full path and new filename, but without the file extension.\n\nFor T5 use the same preprocessing as BERT, perhaps renaming it to:\n\u003cpre\u003e\n       --output-prefix my-t5 \\\n\u003c/pre\u003e\n\nSome minor modifications are required for GPT data preprocessing, namely, the addition of a merge table, an end-of-document token, removal of sentence splitting, and a change to the tokenizer type:\n\u003cpre\u003e\npython tools/preprocess_data.py \\\n       --input my-corpus.json \\\n       --output-prefix my-gpt2 \\\n       --vocab gpt2-vocab.json \\\n       --dataset-impl mmap \\\n       --tokenizer-type GPT2BPETokenizer \\\n       --merge-file gpt2-merges.txt \\\n       --append-eod\n\u003c/pre\u003e\n\nHere the output files are named `my-gpt2_text_document.bin` and `my-gpt2_text_document.idx`. As before, in GPT training, use the longer name without the extension as `--data-path`.\n\nFurther command line arguments are described in the source file [`preprocess_data.py`](./tools/preprocess_data.py).\n\n## BERT Pretraining\n\n\nThe [`examples/pretrain_bert.sh`](./examples/pretrain_bert.sh) script runs single GPU 345M parameter BERT pretraining. Debugging is the primary use for single GPU training, as the code base and command line arguments are optimized for highly distributed training. Most of the arguments are fairly self-explanatory. By default, the learning rate decays linearly over the training iterations starting at `--lr` to a minimum set by `--min-lr` over `--lr-decay-iters` iterations. The fraction of training iterations used for warmup is set by `--lr-warmup-fraction`. While this is single GPU training, the batch size specified by `--micro-batch-size` is a single forward-backward path batch-size and the code will perform gradient accumulation steps until it reaches `global-batch-size` which is the batch size per iteration. The data is partitioned into a 949:50:1 ratio for training/validation/test sets (default is 969:30:1). This partitioning happens on the fly, but is consistent across runs with the same random seed (1234 by default, or specified manually with `--seed`). We use `train-iters` as the training iterations requested. Alternatively, one can provide `--train-samples` which is total number of samples to train on. If this option is present, then instead of providing `--lr-decay-iters`, one will need to provide `--lr-decay-samples`.\n\nThe logging, checkpoint-saving, and evaluation intervals are specified. Checkpointing the activations facilitates the training of larger models and/or batches. Note that the `--data-path` now includes the additional `_text_sentence` suffix added in preprocessing, but does not include the file extensions.\n\nFurther command line arguments are described in the source file [`arguments.py`](./megatron/arguments.py).\n\nTo run `examples/pretrain_bert.sh`, make any desired modifications including setting the environment variables for `CHECKPOINT_PATH`, `VOCAB_FILE`, and `DATA_PATH`. Make sure to set these variables to their paths in the container. Then launch the container with Megatron and necessary paths mounted (as explained in [Setup](#setup)) and run the example script.\n\n## GPT Pretraining\n\nThe `examples/pretrain_gpt.sh` script runs single GPU 345M parameter GPT pretraining. As mentioned above, single GPU training is primarily intended for debugging purposes, as the code is optimized for distributed training.\n\nIt follows largely the same format as the previous BERT script with a few notable differences: the tokenization scheme used is BPE (which requires a merge table and a `json` vocabulary file) instead of WordPiece, the model architecture allows for longer sequences (note that the max position embedding must be greater than or equal to the maximum sequence length), and the `--lr-decay-style` has been set to cosine decay.  Note that the `--data-path` now includes the additional `_text_document` suffix added in preprocessing, but does not include the file extensions.\n\nFurther command line arguments are described in the source file [`arguments.py`](./megatron/arguments.py).\n\n`examples/pretrain_gpt.sh` can be launched the same way as described for BERT. Set the env vars and make any other modifications, launch the container with appropriate mounts, and run the script.\n\n## T5 Pretraining\n\nVery similar to BERT and GPT, the `examples/pretrain_t5.sh` script runs single GPU \"base\" (~220M parameter) T5 pretraining. The primary difference from BERT and GPT is the addition of the following arguments to accommodate the T5 architecture:\n\n* `--kv-channels` sets the inner dimension of the \"key\" and \"value\" matrices of all attention mechanisms in the model. For BERT and GPT this defaults to the hidden size divided by the number of attention heads, but can be configured for T5.\n\n* `--ffn-hidden-size` sets the hidden size in the feed-forward networks within a transformer layer. For BERT and GPT this defaults to 4 times the transformer hidden size, but can be configured for T5.\n\n* `--encoder-seq-length` and `--decoder-seq-length` set the sequence length for the encoder and decoder separately.\n\nAll of the other arguments remain as they were for BERT and GPT pretraining. Run this example with the same steps described above for the other scripts.\n\n## Distributed Pretraining\n\nThe `examples/pretrain_{bert,gpt,t5}_distributed.sh` scripts use the PyTorch distributed launcher for distributed training. As such, multi-node training can be achieved by properly setting environment variables. See the official PyTorch [documentation](https://pytorch.org/docs/stable/elastic/run.html#launcher-api) for further description of these [environment variables](https://pytorch.org/docs/stable/distributed.html#environment-variable-initialization). By default, multi-node training uses the [nccl](https://developer.nvidia.com/nccl) distributed backend. A simple set of additional arguments and the use of the PyTorch distributed module with the `torchrun` elastic launcher (equivalent to `python -m torch.distributed.run`) are the only additional requirements to adopt distributed training. See any of `examples/pretrain_{bert,gpt,t5}_distributed.sh` for more details.\n\nWe use two types of parallelism: data and model parallelism. We facilitate two distributed data parallel implementations: a simple one of our own that performs gradient all-reduce at the end of back propagation step, and Torch's distributed data parallel wrapper that overlaps gradient reduction with back propagation computation. To switch between these two options use `--DDP-impl local` or `--DDP-impl torch`, respectively. As expected, Torch distributed data parallelism is more efficient at larger model sizes. For example, for the 8.3 billion parameters model running on 512 GPUs, the scaling increases from 60% to 76% when Torch's distributed data parallel is used. However, the overlapping method requires more memory and for some configurations (e.g., 2.5 billion parameters using 2-way model parallel and 1.2 billion parameters with no model parallel) can make the overall training slower as a result. We empirically found that using a smaller model in those cases improves the training time.\n\nSecond, we developed a simple and efficient two-dimensional model-parallel approach. To use tensor model parallelism (splitting execution of a single transformer module over multiple GPUs, see Section 3 of [our paper](https://arxiv.org/pdf/1909.08053.pdf)), add the `--tensor-model-parallel-size` flag to specify the number of GPUs among which to split the model, along with the arguments passed to the distributed launcher as mentioned above. To use sequence parallelism specify `--sequence-parallel`, which requires tensor model parallel as it split among the same GPUs (more details in Section 4.2.2 of [our paper](https://arxiv.org/pdf/2205.05198.pdf)).\n\nTo use pipeline model parallelism (sharding the transformer modules into stages with an equal number of transformer modules on each stage, and then pipelining execution by breaking the batch into smaller microbatches, see Section 2.2 of [our paper](https://arxiv.org/pdf/2104.04473.pdf)), use the `--pipeline-model-parallel-size` flag to specify the number of stages to split the model into (e.g., splitting a model with 24 transformer layers across 4 stages would mean each stage gets 6 transformer layers each).\n\n\u003c!-- The number of microbatches in a per-pipeline minibatch is controlled by the `--num-microbatches-in-minibatch` argument. With `WORLD_SIZE` GPUs, `TENSOR_MP_SIZE` tensor-model-parallel size, `PIPELINE_MP_SIZE` pipeline-model-parallel-size, `WORLD_SIZE`/(`TENSOR_MP_SIZE` * `PIPELINE_MP_SIZE`) GPUs will be used for data parallelism. The default values for `--tensor-model-parallel-size` and `--pipeline-model-parallel-size` is 1, which will not implement either form of model parallelism. --\u003e\n\nWe have examples of how to use these two different forms of model parallelism the example scripts ending in `distributed_with_mp.sh`:\n\nOther than these minor changes, the distributed training is identical to the training on a single GPU.\n\nThe interleaved pipelining schedule (more details in Section 2.2.2 of [our paper](https://arxiv.org/pdf/2104.04473.pdf)) can be enabled using the `--num-layers-per-virtual-pipeline-stage` argument, which controls the number of transformer layers in a virtual stage (by default with the non-interleaved schedule, each GPU will execute a single virtual stage with `NUM_LAYERS / PIPELINE_MP_SIZE` transformer layers). The total number of layers in the transformer model should be divisible by this argument value. Additionally, the number of microbatches in the pipeline (computed as `GLOBAL_BATCH_SIZE / (DATA_PARALLEL_SIZE * MICRO_BATCH_SIZE)`) should be divisible by the `PIPELINE_MP_SIZE` when using this schedule (this condition is checked in an assertion in the code). The interleaved schedule is not supported for pipelines with 2 stages (`PIPELINE_MP_SIZE=2`).\n\n## Activation Checkpointing and Recomputation\n\nTo reduce GPU memory usage so deploy a large model to a training system, we support activation checkpointing and recomputation. We support two levels of recompute granularity: `selective` and `full`. Selective recomputation is the default and recommended in almost all cases. It saves the activations that take less space and are expensive to recompute and recomputes activations that take a lot of space but are relatively cheap to recompute (see [our paper](https://arxiv.org/pdf/2205.05198) for details). To enable selective activation recompute simply use `--recompute-activations`.\n\nFor cases where memory is very tight, `full` checkpointing saves just the inputs to a transformer layer, or a block of transformer layers, and recomputes everything else. To turn on full activation recompute use `--recompute-granularity full`. When using full activation recomputation, there are two methods: `uniform` and `block`, chosen using the `--recompute-method` argument.\n\n* Uniform method uniformly divides the Transformer layers into groups of layers and stores the input activations of each group in the memory. The baseline group size is 1 and, in this case, the input activation of each Transformer layer is checkpointed. When the GPU memory is insufficient, increasing the number of layers per group reduces the memory usage thus enables running a bigger model. For example, when using the number of layers per group of 4, the input activation of each group of 4 Transformer layers is checkpointed.\n\n* Block method checkpoints the input activations of a set number of individual Transformer layers per pipeline stage and do the rest of layers without any checkpointing. This method can be used to skip checkpointing some Transformer layers until the GPU memory is fully used, which is applicable only when there is unused GPU memory. Checkpointing fewer transformer layers avoids unnecessary activation recomputation in the backprop thus improves training performance. For example, when we specify 5 layers to checkpoint of 8 layers per pipeline stage, the input activations of only the first 5 Transformer layers are checkpointed and activation recomputation for the rest 3 layers is not needed in the backprop.\n\n\n## Distributed Optimizer\n\nUsage: `--use-distributed-optimizer`. Compatible with all model and data types.\n\nThe distributed optimizer is a memory savings technique, whereby the optimizer state is evenly distributed across data parallel ranks (versus the traditional method of replicating the optimizer state across data parallel ranks). As described in [ZeRO: Memory Optimizations Toward Training Trillion Parameter Models](https://arxiv.org/abs/1910.02054), our implementation distributes all optimizer state that does not overlap with the model state. For example, when using fp16 model params, the distributed optimizer maintains its own separate copy of fp32 main params \u0026 grads, which are distributed across DP ranks. When using bf16 model params, however, the distributed optimizer's fp32 main grads are the same as the model's fp32 grads, and so the grads in this case are not distributed (although the fp32 main params are still distributed, as they are separate from the bf16 model params).\n\nTheoretical memory savings vary depending on the combination of the model's param dtype and grad dtype. In our implementation, the theoretical number of bytes per parameter is (where 'd' is the data parallel size):\n\n| | Non-distributed optim | Distributed optim |\n|-|-|-|\n| fp16 param, fp16 grads | 20 | 4 + 16/d |\n| bf16 param, fp32 grads | 18 | 6 + 12/d |\n| fp32 param, fp32 grads | 16 | 8 + 8/d |\n\n## FlashAttention\n\nUsage: `--use-flash-attn`. Support attention head dimensions at most 128.\n\n[FlashAttention](https://github.com/HazyResearch/flash-attention) is a fast and\nmemory-efficient algorithm to compute exact attention. It speeds up model\ntraining and reduces memory requirement.\n\nTo install FlashAttention:\n```sh\npip install flash-attn\n```\n\n## GPT-3 Example\n\nIn `examples/pretrain_gpt3_175B.sh` we have provided an example of how to configure Megatron to run [GPT-3](https://arxiv.org/abs/2005.14165) with 175 billion parameters on 1024 GPUs. The script is designed for [slurm](https://slurm.schedmd.com/documentation.html) with [pyxis](https://github.com/NVIDIA/pyxis) plugin but can be easily adopted to any other scheduler. It uses 8-way and 16-way tensor and pipeline parallelism, respectively. With options `global-batch-size 1536` and `rampup-batch-size 16 16 5859375`, the training will start with global batch size 16 and linearly increase the global batch size to 1536 over 5,859,375 samples with incrmeental steps 16. The training dataset can be either a single set or a multiple datasets combined with a set of weights.\n\nWith full global batch size of 1536 on 1024 A100 GPUs, each iteration takes around 32 seconds resulting in 138 teraFLOPs per GPU which is 44% of the theoretical peak FLOPs.\n\n\n## Retro\n\nSee:\n\n- `tools/retro/README.md` for an overview.\n- `tools/retro/examples/get_preprocess_cmd.sh` for an example of common preprocessing arguments.\n- `tools/retro/examples/preprocess_data.sh` for an example of how to preprocess data.\n- `tools/retro/examples/pretrain_model.sh` for an example of how to pretrain a model.\n\nRetro is a retrieval-enhanced model that is based on GPT. As described in [Improving language models by retrieving from trillions of tokens](https://arxiv.org/abs/2112.04426), Retro retrieves from a database of document chunks by performing locality search using a sample's tokens. The retrieval database can be large -- often billions or even trillions of tokens -- and provides a more efficient storage mechanism of factual knowledge, when compared to storing factual knowledge implicitly within the network's parameters.\n\nUsing Retro requires two steps: 1) preprocessing the retrieval database and pretraining neighbors, and 2) pretraining a model using this data. Please see `tools/retro/README.md` for a detailed overview.\n\n\u003c!--\n## REALM Pipeline\nWe are working on implementing the [REALM](https://arxiv.org/pdf/2002.08909.pdf) system. The following sections (will) reflect the three stages of training it. For now it's just the ICT code.\nLoosely, they are pretraining the retriever modules, then jointly training the language model and the retriever, and then finetuning a question answering head on the language model with fixed retriever.\n\n### Inverse Cloze Task (ICT) Pretraining\n1. Have a corpus in loose JSON format with the intention of creating a collection of fixed-size blocks of text as the fundamental units of data. For a corpus like Wikipedia, this will mean multiple sentences per block but also multiple blocks per document.\nRun `tools/preprocess_data.py` to construct one or more indexed datasets with the `--split-sentences` argument to make sentences the basic unit. For the original REALM system, we construct two datasets, one with the title of every document, and another with the body.\nRefer to the following script\n\u003cpre\u003e\npython preprocess_data.py \\\n    --input /path/to/corpus.json \\\n    --json-keys text title \\\n    --split-sentences \\\n    --tokenizer-type BertWordPieceLowerCase \\\n    --vocab-file /path/to/vocab.txt \\\n    --output-prefix corpus_indexed \\\n    --workers 5  # works well for 10 CPU cores. Scale up accordingly.\n\u003c/pre\u003e\n\n2. Use a custom samples mapping function in place of `megatron/data/realm_dataset_utils.get_block_samples_mapping` if required. To do this, you will need to implement a new function in C++ inside of `megatron/data/helpers.cpp`. The samples mapping data structure is used to select the data that will constitute every training sample in advance of the training loop.\n The samples mapping is responsible for holding all of the required metadata needed to construct the sample from one or more indexed datasets. In REALM, the samples mapping contains the start and end sentence indices, as well as the document index (to find the correct title for a body) and a unique ID for every block.\n3. Pretrain a BERT language model using `pretrain_bert.py`, with the sequence length equal to the block size in token ids. This model should be trained on the same indexed dataset that is used to supply the blocks for the information retrieval task.\nIn REALM, this is an uncased bert base model trained with the standard hyperparameters.\n4. Use `pretrain_ict.py` to train an `ICTBertModel` which uses two BERT-based encoders to encode queries and blocks to perform retrieval with.\nThe script below trains the ICT model from REALM. It refrences a pretrained BERT model (step 3) in the `--bert-load` argument. The batch size used in the paper is 4096, so this would need to be run with data parallel world size 32.\n\u003cpre\u003e\npython pretrain_ict.py \\\n    --num-layers 12 \\\n    --num-attention-heads 12 \\\n    --hidden-size 768 \\\n    --batch-size 128 \\\n    --seq-length 256 \\\n    --max-position-embeddings 256 \\\n    --ict-head-size 128 \\\n    --train-iters 100000 \\\n    --activations-checkpoint-method uniform \\\n    --bert-load /path/to/pretrained_bert \\\n    --load checkpoints \\\n    --save checkpoints \\\n    --data-path /path/to/indexed_dataset \\\n    --titles-data-path /path/to/titles_indexed_dataset \\\n    --vocab-file /path/to/vocab.txt \\\n    --lr 0.0001 \\\n    --num-workers 2 \\\n    --lr-decay-style linear \\\n    --weight-decay 1e-2 \\\n    --clip-grad 1.0 \\\n    --lr-warmup-fraction .01 \\\n    --save-interval 3000 \\\n    --query-in-block-prob 0.1 \\\n    --fp16\n\n\u003c/pre\u003e\n\n### Building an Index of Block Embeddings\nAfter having trained an ICT model, you can now embed an entire dataset of blocks by creating a `BlockData` structure. After that has been saved, you can load it\nand wrap it with a `FaissMIPSIndex` to do fast similarity search which is key in the learned information retrieval pipeline. The initial index can be built with the following script, meant to be run in an interactive session. It can leverage multiple GPUs on multiple nodes to index large datasets much more quickly.\n\n\u003cpre\u003e\npython tools/create_doc_index.py \\\n    --num-layers 12 \\\n    --hidden-size 768 \\\n    --ict-head-size 128 \\\n    --num-attention-heads 12 \\\n    --batch-size 128 \\\n    --activations-checkpoint-method uniform \\\n    --seq-length 256 \\\n    --max-position-embeddings 256 \\\n    --ict-load /path/to/pretrained_ict \\\n    --data-path /path/to/indexed_dataset \\\n    --titles-data-path /path/to/titles_indexed_dataset \\\n    --block-data-path embedded_blocks.pkl \\\n    --indexer-log-interval 1000 \\\n    --indexer-batch-size 128 \\\n    --vocab-file /path/to/vocab.txt \\\n    --num-workers 2 \\\n    --fp16\n\u003c/pre\u003e\n\n--\u003e\n\n# Evaluation and Tasks\n\nWe provide several command line arguments, detailed in the scripts listed below, to handle various zero-shot and fine-tuned downstream tasks. However, you can also finetune your model from a pretrained checkpoint on other corpora as desired. To do so, simply add the `--finetune` flag and adjust the input files and training parameters within the original training script. The iteration count will be reset to zero, and the optimizer and internal state will be reinitialized. If the fine-tuning is interrupted for any reason, be sure to remove the `--finetune` flag before continuing, otherwise the training will start again from the beginning.\n\nBecause evaluation requires substantially less memory than training, it may be advantageous to merge a model trained in parallel for use on fewer GPUs in downstream tasks. The following script accomplishes this. This example reads in a GPT model with 4-way tensor and 4-way pipeline model parallelism and writes out a model with 2-way tensor and 2-way pipeline model parallelism.\n\n\u003cpre\u003e\npython tools/checkpoint_util.py \\\n        --model-type GPT \\\n        --load-dir checkpoints/gpt3_tp4_pp4 \\\n        --save-dir checkpoints/gpt3_tp2_pp2 \\\n        --target-tensor-parallel-size 2 \\\n        --target-pipeline-parallel-size 2\n\n\u003c/pre\u003e\n\nSeveral downstream tasks are described for both GPT and BERT models below. They can be run in distributed and model parallel modes with the same changes used in the training scripts.\n\n## GPT Text Generation\n\nWe have included a simple REST server to use for text generation in `tools/run_text_generation_server.py`. You run it much like you would start a pretraining job, specifying an appropriate pretrained checkpoint. There are also few optional parameters: `temperature`, `top-k`and `top-p`. See `--help` or the source file for more information. See [examples/run_text_generation_server_345M.sh](examples/run_text_generation_server_345M.sh) for an example of how to run the server.\n\nOnce the server is running you can use `tools/text_generation_cli.py` to query it, it takes one argument which is the host the server is running on.\n\n\u003cpre\u003e\ntools/text_generation_cli.py localhost:5000\n\u003c/pre\u003e\n\nYou can also use CURL or any other tools to query the server directly:\n\n\u003cpre\u003e\ncurl 'http://localhost:5000/api' -X 'PUT' -H 'Content-Type: application/json; charset=UTF-8'  -d '{\"prompts\":[\"Hello world\"], \"tokens_to_generate\":1}'\n\u003c/pre\u003e\n\nSee [megatron/text_generation_server.py](megatron/text_generation_server.py) for more API options.\n\n### Detoxify GPT via Self-generation\nWe include an example in `examples/detxoify_lm/` to detoxify language models by leveraging the generative power of language models.\n\nSee [examples/detxoify_lm/README.md](examples/detxoify_lm/README.md) for step-by-step tutorials on how to perform domain-adaptive training and detoxify LM using self-generated corpus. \n\n\n## GPT Evaluation\nWe include example scripts for GPT evaluation on WikiText perplexity evaluation and LAMBADA Cloze accuracy.\n\n### WikiText Perplexity Evaluation\nFor even comparison with prior works, we evaluate perplexity on the word-level [WikiText-103 test dataset](https://s3.amazonaws.com/research.metamind.io/wikitext/wikitext-103-v1.zip), and appropriately compute perplexity given the change in tokens when using our subword tokenizer.\n\nWe use the following command to run WikiText-103 evaluation on a 345M parameter model.\n\u003cpre\u003e\nTASK=\"WIKITEXT103\"\n\nVALID_DATA=\u0026#60;wikitext path\u0026#62;.txt\nVOCAB_FILE=gpt2-vocab.json\nMERGE_FILE=gpt2-merges.txt\nCHECKPOINT_PATH=checkpoints/gpt2_345m\n\nCOMMON_TASK_ARGS=\"--num-layers 24 \\\n                  --hidden-size 1024 \\\n                  --num-attention-heads 16 \\\n                  --seq-length 1024 \\\n                  --max-position-embeddings 1024 \\\n                  --fp16 \\\n                  --vocab-file $VOCAB_FILE\"\n\npython tasks/main.py \\\n       --task $TASK \\\n       $COMMON_TASK_ARGS \\\n       --valid-data $VALID_DATA \\\n       --tokenizer-type GPT2BPETokenizer \\\n       --merge-file $MERGE_FILE \\\n       --load $CHECKPOINT_PATH \\\n       --micro-batch-size 8 \\\n       --activations-checkpoint-method uniform \\\n       --log-interval 10 \\\n       --no-load-optim \\\n       --no-load-rng\n\u003c/pre\u003e\n\n\n### LAMBADA Cloze Accuracy\nTo compute LAMBADA cloze accuracy (the accuracy of predicting the last token given the preceding tokens) we utilize a detokenized, processed version of the [LAMBADA dataset](https://github.com/cybertronai/bflm/blob/master/lambada_test.jsonl).\n\nWe use the following command to run LAMBADA evaluation on a 345M parameter model. Note that the `--strict-lambada` flag should be used to require whole word matching. Make that `lambada` is part of the file path.\n\n\u003cpre\u003e\nTASK=\"LAMBADA\"\n\nVALID_DATA=\u0026#60;lambada path\u0026#62;.json\nVOCAB_FILE=gpt2-vocab.json\nMERGE_FILE=gpt2-merges.txt\nCHECKPOINT_PATH=checkpoints/gpt2_345m\nCOMMON_TASK_ARGS=\u0026#60;same as those in \u003ca href=\"#wikitext-perplexity-evaluation\"\u003eWikiText Perplexity Evaluation\u003c/a\u003e above\u0026#62;\n\npython tasks/main.py \\\n       --task $TASK \\\n       $COMMON_TASK_ARGS \\\n       --valid-data $VALID_DATA \\\n       --tokenizer-type GPT2BPETokenizer \\\n       --strict-lambada \\\n       --merge-file $MERGE_FILE \\\n       --load $CHECKPOINT_PATH \\\n       --micro-batch-size 8 \\\n       --activations-checkpoint-method uniform \\\n       --log-interval 10 \\\n       --no-load-optim \\\n       --no-load-rng\n\u003c/pre\u003e\n\nFurther command line arguments are described in the source file [`main.py`](./tasks/main.py)\n\n## BERT Task Evaluation\n### RACE Evaluation\nThe following script finetunes the BERT model for evaluation on the [RACE dataset](http://www.cs.cmu.edu/~glai1/data/race/). The `TRAIN_DATA` and `VALID_DATA` directory contain the RACE dataset as separate `.txt` files. Note that for RACE, the batch size is the number of RACE query's to evaluate. Since each RACE query has four samples, the effective batch size passed through the model will be four times the batch size specified on the command line.\n\n\u003cpre\u003e\nTRAIN_DATA=\"data/RACE/train/middle\"\nVALID_DATA=\"data/RACE/dev/middle \\\n            data/RACE/dev/high\"\nVOCAB_FILE=bert-vocab.txt\nPRETRAINED_CHECKPOINT=checkpoints/bert_345m\nCHECKPOINT_PATH=checkpoints/bert_345m_race\nCOMMON_TASK_ARGS=\"--num-layers 24 \\\n                  --hidden-size 1024 \\\n                  --num-attention-heads 16 \\\n                  --seq-length 512 \\\n                  --max-position-embeddings 512 \\\n                  --fp16 \\\n                  --vocab-file $VOCAB_FILE\"\n\nCOMMON_TASK_ARGS_EXT=\"--train-data $TRAIN_DATA \\\n                      --valid-data $VALID_DATA \\\n                      --pretrained-checkpoint $PRETRAINED_CHECKPOINT \\\n                      --activations-checkpoint-method uniform \\\n                      --save-interval 10000 \\\n                      --save $CHECKPOINT_PATH \\\n                      --log-interval 100 \\\n                      --eval-interval 1000 \\\n                      --eval-iters 10 \\\n                      --weight-decay 1.0e-1\"\n\npython tasks/main.py \\\n       --task RACE \\\n       $COMMON_TASK_ARGS \\\n       $COMMON_TASK_ARGS_EXT \\\n       --tokenizer-type BertWordPieceLowerCase \\\n       --epochs 3 \\\n       --micro-batch-size 4 \\\n       --lr 1.0e-5 \\\n       --lr-warmup-fraction 0.06\n\u003c/pre\u003e\n\n### MNLI Evaluation\nThe following script finetunes the BERT model for evaluation with the [MultiNLI sentence pair corpus](https://www.nyu.edu/projects/bowman/multinli/). Because the matching tasks are quite similar, the script can be quickly tweaked to work with the [Quora Question Pairs](https://www.kaggle.com/quora/question-pairs-dataset) (QQP) dataset as well.\n\n\u003cpre\u003e\n\nTRAIN_DATA=\"data/glue_data/MNLI/train.tsv\"\nVALID_DATA=\"data/glue_data/MNLI/dev_matched.tsv \\\n            data/glue_data/MNLI/dev_mismatched.tsv\"\nPRETRAINED_CHECKPOINT=checkpoints/bert_345m\nVOCAB_FILE=bert-vocab.txt\nCHECKPOINT_PATH=checkpoints/bert_345m_mnli\nCOMMON_TASK_ARGS=\u0026#60;same as those in \u003ca href=\"#race-evaluation\"\u003eRACE Evaluation\u003c/a\u003e above\u0026#62;\nCOMMON_TASK_ARGS_EXT=\u0026#60;same as those in \u003ca href=\"#race-evaluation\"\u003eRACE Evaluation\u003c/a\u003e above\u0026#62;\n\npython tasks/main.py \\\n       --task MNLI \\\n       $COMMON_TASK_ARGS \\\n       $COMMON_TASK_ARGS_EXT \\\n       --tokenizer-type BertWordPieceLowerCase \\\n       --epochs 5 \\\n       --micro-batch-size 8 \\\n       --lr 5.0e-5 \\\n       --lr-warmup-fraction 0.065\n\u003c/pre\u003e\n\n# Datasets\nWe do not host any datasets for GPT or BERT training, however, we detail their collection so that our results may be reproduced.\n\n## Collecting Wikipedia Training Data\nWe recommend following the Wikipedia data extraction process specified by Google research: \"the recommended pre-processing is to download [the latest dump](https://dumps.wikimedia.org/enwiki/latest/enwiki-latest-pages-articles.xml.bz2), extract the text with [WikiExtractor.py](https://github.com/attardi/wikiextractor), and then apply any necessary cleanup to convert it into plain text.\"\n\nWe recommend using the `--json` argument when using WikiExtractor, which will dump the Wikipedia data into loose json format (one json per line), making it more manageable on the file system and also readily consumable by our codebase. We recommend further preprocessing this json dataset by nltk punctuation standardization. For BERT training, use the `--split-sentences` flag to `preprocess_data.py` as described [above](#data-preprocessing) to include sentence breaks in the produced index. If you'd like to use Wikipedia data for GPT training you should still clean it with nltk/spacy/ftfy, but do not use the `--split-sentences` flag.\n\n## Collecting GPT Webtext Data\nWe utilize the publicly available [OpenWebText](https://github.com/eukaryote31/openwebtext) library from [jcpeterson](https://github.com/jcpeterson/openwebtext) and [eukaryote31's](https://github.com/eukaryote31/openwebtext) work to download urls. We then filtered, cleaned, and deduplicated all downloaded content according to the procedure described in our [openwebtext](./tools/openwebtext) directory. For reddit URLs corresponding to content up to October 2018 we arrived at approximately 37GB of content.\n","funding_links":[],"categories":["Python"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbigcode-project%2FMegatron-LM","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbigcode-project%2FMegatron-LM","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbigcode-project%2FMegatron-LM/lists"}