Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

Hugging Face 7 min read How-to hardMaster

How to train a Language Model with Megatron-LM

Megatron-LM, NVIDIA's GPU-optimized framework for pre-training transformers, can train a GPT2-style model end to end: environment setup in an NGC PyTorch container, preprocessing data into tokenized binary mmap files, launching distributed training, and converting checkpoints to Hugging Face Transformers. Its speed comes from a DataLoader that pre-computes shuffled sequence indexes across epochs and from fused CUDA kernels, including Apex's fused AdamW, which cut memory movement. A 110M-parameter CodeParrot model trains in about 12 hours on 8 GPUs with data parallelism, and tensor and pipeline parallelism are available for larger models. The extra preprocessing and conversion overhead makes it worth using mainly for pre-training or extended fine-tuning rather than short fine-tunes.

Assumes: PyTorchtransformer language modelsdistributed GPU trainingDocker basics

Related posts

  • Training CodeParrot 🦜 from Scratch

    Hugging Face mediumAdept

    CodeParrot is a GPT-2 model (110M and 1.5B parameters) trained from scratch for Python code completion, using a 180 GB GitHub Python dump from BigQuery. Heavy duplication in that dump (10% of unique files made up 66% of all files) hurt model quality, so deduplication plus Codex-style filtering cut it to a 50 GB clean dataset. The guide walks through training a code-specific tokenizer, initializing a GPT-2-large config with attention stability flags, a streaming constant-length dataset, and a ~150-line distributed training loop with 🤗 Accelerate using gradient accumulation and checkpoints pushed to the Hub. Training ran on 16 A100s for 1 day (small) and 1 week (large) on about 25-30B tokens, and the models were evaluated on HumanEval with the pass@k metric.

  • Tricks from OpenAI gpt-oss YOU 🫵 can use with transformers

    Hugging Face hardMaster

    Hugging Face upgraded transformers to support OpenAI's gpt-oss models, and most of the new features carry over to other models. These include pre-built kernels downloaded from the Hub (Liger RMSNorm, MegaBlocks MoE, Flash Attention 3 with attention sinks), native MXFP4 4-bit quantization via Triton kernels, tensor and expert parallelism, dynamic sliding window caching, continuous batching with paged attention, and faster loading. MXFP4 stores E2M1 values in 32-element blocks with shared scales. That lets gpt-oss-20B run in about 16 GB of VRAM and 120B in about 80 GB, versus roughly 4× the memory in the bf16 fallback. Benchmarks show the custom and MXFP4 kernels pay off mainly at larger batch sizes.

  • Pre-Train BERT with Hugging Face Transformers and Habana Gaudi

    Hugging Face mediumAdept

    BERT-base can be pre-trained from scratch with masked-language modeling on an AWS DL1 instance (Habana Gaudi), using Hugging Face Transformers, Datasets and Optimum Habana. The workflow merges BookCorpus and Wikipedia, trains a 32k-vocab tokenizer, and packs the text into 512-token chunks on a CPU instance. Training then runs data-parallel across 8 HPUs by swapping Trainer for GaudiTrainer, launched remotely with EC2 Remote Runner. 100k steps at global batch 256 took about 12.5 hours, which projects a full 1M-step pre-training to roughly $1,650. That is about 25% cheaper than a DeepSpeed V100 baseline estimated at around $2,075.

  • Large Language Models: A New Moore's Law?

    Hugging Face easyNovice

    Megatron-Turing NLG 530B and the 10x-per-year growth in language model size look like a new Moore's Law heading for diminishing returns, with roughly $100M of DGX A100 hardware, 6.5 kW per server and a large carbon footprint buying only small benchmark gains. Most teams are better served by starting from pretrained models and choosing the smallest one that meets their accuracy target. Distilled or compact models such as DistilBERT (97% of BERT's language understanding, 40% smaller, 60% faster) and T0 (beats GPT-3 on many tasks at 16x smaller) show this is practical. Fine-tuning instead of training from scratch, running on cloud infrastructure, and applying pruning, layer fusion and quantization further cut cost and energy.

  • Fine-tuning Llama 2 70B using PyTorch FSDP

    Hugging Face hardMaster

    Full fine-tuning of Llama 2 70B across two nodes of 8×A100 80GB GPUs with PyTorch FSDP, Hugging Face Accelerate, Transformers and TRL, addressing three obstacles. CPU RAM exhaustion (~2TB if every rank loads the model) is avoided by loading weights only on rank 0 via the meta device and broadcasting with sync_module_states. Slow, NCCL-timeout-prone checkpointing is fixed by saving intermediate checkpoints as SHARDED_STATE_DICT and switching to FULL_STATE_DICT only for the final save. Flash Attention V2 (kernel fusion, tiling, online softmax) plus gradient checkpointing cut VRAM and time; the run finished in ~13.5 hours, with a paged AdamW optimizer needed on a single node.

The quest for software engineering knowledge.

Look:
Layout: