YaLM 100B Documentation

repository·main·Indexed 25 days ago

https://github.com/yandex/yalm-100b

YaLM 100B is a large-scale GPT-like neural network with 100 billion parameters for processing and generating English and Russian text. Built on DeepSpeed and Megatron-LM principles, the repository includes tools for GPT-2 and BERT pretraining, distributed training with model and data parallelism, curriculum learning, and REALM ICT model training. It also provides utilities for text generation, model partition merging, and evaluation on datasets such as WikiText-103, LAMBADA, and RACE.

Tokens
7.5K
Snippets
17
Records
28
Agent score
87%

What's inside YaLM 100B

  1. Use DeepSpeed's Curriculum Learning for GPT pre-training

    main

    This implementation provides an example of how to use DeepSpeed's curriculum learning (CL) feature to achieve faster and more stable language model pre-training.

    Key constraints and details:

    • Supported Architectures: Currently, this is only integrated for GPT pre-training.
    • Implementation Context: This is one of two curriculum learning examples for Megatron-LM GPT-2 pre-training. Users should be aware that different implementations may have unique features and limitations.
    • External Resources:
  2. Preprocess data for GPT-2 training

    main

    GPT-2 data preprocessing requires specific modifications compared to BERT: you must provide a merge table, use the GPT2BPETokenizer, append an end-of-document token (--append-eod), and do not split sentences.

    The output files will be named _text_document.bin and _text_document.idx.

    python tools/preprocess_data.py \
           --input my-corpus.json \
           --output-prefix my-gpt2 \
           --vocab gpt2-vocab.json \
           --dataset-impl mmap \
           --tokenizer-type GPT2BPETokenizer \
           --merge-file gpt2-merges.txt \
           --append-eod
  3. Install required libraries for dataset preparation

    main

    To prepare the training dataset, you must install several Python libraries and the LSH (Locality Sensitive Hashing) dependency. Run the following commands to set up the environment:

        pip install ftfy langdetect numpy torch pandas nltk sentencepiece boto3 tqdm regex bs4 newspaper3k htmlmin tldextract 
        git clone https://github.com/mattilyra/LSH
        cd LSH
        python setup.py install
  4. Generate text with YaLM 100B

    main

    The repository provides several shell scripts for different generation modes:

    • Interactive Generation: Use examples/generate_interactive.sh for simple command-line interaction.
    • Conditional Sampling: Use examples/generate_conditional_sampling.sh. This uses a sampling strategy (Top-p by default, but temperature and top-k can be adjusted). Input must be in jsonlines format (e.g., examples/example_cond_input.json). The output will be jsonlines with a generated text field added to each line.
    • Conditional Greedy: Use examples/generate_conditional_greedy.sh. This is suitable for few-shot problem solving.
    • Unconditional Generation: Use examples/generate_unconditional.sh. No input is required; the output is provided in jsonlines format.
  5. Fine-tune a pretrained model

    main

    To fine-tune a model from a pretrained checkpoint on a new corpus, add the --finetune flag to your training script and adjust the input files and training parameters.

    Warning: Adding --finetune resets the iteration count to zero and reinitializes the optimizer and internal state. If a fine-tuning session is interrupted, you must remove the --finetune flag before resuming, otherwise training will restart from the beginning.

  6. Build an index of block embeddings

    main

    Once an ICT model is trained, you can build a BlockData structure to enable fast similarity search using FaissMIPSIndex. Use tools/create_doc_index.py to embed the entire dataset. This script can be run in an interactive session and supports multi-GPU/multi-node execution for large datasets.

    python tools/create_doc_index.py \
        --num-layers 12 \
        --hidden-size 768 \
        --ict-head-size 128 \
        --num-attention-heads 12 \
        --batch-size 128 \
        --checkpoint-activations \
        --seq-length 256 \
        --max-position-embeddings 256 \
        --ict-load /path/to/pretrained_ict \
        --data-path /path/to/indexed_dataset \
        --titles-data-path /path/to/titles_indexed_dataset \
        --block-data-path embedded_blocks.pkl \
        --indexer-log-interval 1000 \
        --indexer-batch-size 128 \
        --vocab-file /path/to/vocab.txt \
        --num-workers 2 \
        --fp16
  7. Pretrain BERT models

    main

    BERT pretraining can be performed using pretrain_bert.py. While optimized for distributed training, you can run it on a single GPU for debugging.

    Key Arguments:

    • --batch-size: Per-GPU batch size for data parallelism.
    • --data-path: The path to the processed data (including the _text_sentence suffix, but without the .bin or .idx extension).
    • --vocab-file: Path to the vocabulary file.
    • --lr, --min-lr, --lr-decay-iters: Learning rate and decay settings.
    • --warmup: Fraction of training iterations used for warmup.
    • --split: Ratio for training/validation/test sets (e.g., 949,50,1).
    • --fp16: Enables mixed precision training.
    • --checkpoint-activations: Facilitates training larger models/batches by checkpointing activations.
    CHECKPOINT_PATH=checkpoints/bert_345m
    VOCAB_FILE=bert-vocab.txt
    DATA_PATH=my-bert_text_sentence
    
    BERT_ARGS="--num-layers 24 \
               --hidden-size 1024 \
               --num-attention-heads 16 \
               --seq-length 512 \
               --max-position-embeddings 512 \
               --lr 0.0001 \
               --train-iters 2000000 \
               --min-lr 0.00001 \
               --lr-decay-iters 990000 \
               --warmup 0.01 \
               --batch-size 8 \
               --vocab-file $VOCAB_FILE \
               --split 949,50,1 \
               --fp16"
    
    OUTPUT_ARGS="--log-interval 10 \
                 --save-interval 500 \
                 --eval-interval 100 \
                 --eval-iters 10 \
                 --checkpoint-activations"
    
    python pretrain_bert.py \
           $BERT_ARGS \
           $OUTPUT_ARGS \
           --save $CHECKPOINT_PATH \
           --load $CHECKPOINT_PATH \
           --data-path $DATA_PATH
  8. Pretrain GPT-2 on a single GPU

    main

    To run GPT-2 pretraining for debugging purposes on a single GPU, use the pretrain_gpt2.py script. Note that GPT-2 uses a BPE tokenization scheme, which requires a vocab-file (JSON) and a merge-file (TXT). The --data-path should include the _text_document suffix added during preprocessing but should not include the file extension. The --lr-decay-style is set to cosine by default.

    CHECKPOINT_PATH=checkpoints/gpt2_345m
    VOCAB_FILE=gpt2-vocab.json
    MERGE_FILE=gpt2-merges.txt
    DATA_PATH=my-gpt2_text_document
    
    GPT2_ARGS="--num-layers 24 \
               --hidden-size 1024 \
               --num-attention-heads 16 \
               --seq-length 1024 \
               --max-position-embeddings 1024 \
               --batch-size 4 \
               --lr 0.00015 \
               --train-iters 500000 \
               --lr-decay-iters 320000 \
               --lr-decay-style cosine \
               --vocab-file $VOCAB_FILE \
               --merge-file $MERGE_FILE \
               --warmup .01 \
               --fp16"
    
    OUTPUT_ARGS=<same as those in BERT pretraining>
    
    python pretrain_gpt2.py \
           $GPT2_ARGS \
           $OUTPUT_ARGS \
           --save $CHECKPOINT_PATH \
           --load $CHECKPOINT_PATH \
           --data-path $DATA_PATH
  9. Download YaLM 100B checkpoints

    main

    You can download the model weights and vocabulary using the provided script or by cloning the Hugging Face repository.

    Option 1: Using the download script Run the bash script to download files to the default local directories.

    • Weights: ./yalm100b_checkpoint/weights/
    • Vocabulary: ./yalm100b_checkpoint/vocab/

    Option 2: Hugging Face Clone the yandex/yalm-100b repository and pull the yalm100b_checkpoint directory.

    bash download/download.sh
  10. Pretrain ICTBertModel for REALM

    main

    After preprocessing the data and pretraining a standard BERT language model, use pretrain_ict.py to train the ICTBertModel. This model uses two BERT-based encoders for query and block retrieval.

    Key arguments:

    • --bert-load: Path to the pretrained BERT model used in the previous step.
    • --data-path: Path to the indexed dataset.
    • --titles-data-path: Path to the indexed titles dataset.
    • --ict-head-size: Size of the ICT head.
    • --query-in-block-prob: Probability of a query being within a block.
    python pretrain_ict.py \
        --num-layers 12 \
        --num-attention-heads 12 \
        --hidden-size 768 \
        --batch-size 128 \
        --seq-length 256 \
        --max-position-embeddings 256 \
        --ict-head-size 128 \
        --train-iters 100000 \
        --checkpoint-activations \
        --bert-load /path/to/pretrained_bert \
        --load checkpoints \
        --save checkpoints \
        --data-path /path/to/indexed_dataset \
        --titles-data-path /path/to/titles_indexed_dataset \
        --vocab-file /path/to/vocab.txt \
        --lr 0.0001 \
        --num-workers 2 \
        --lr-decay-style linear \
        --weight-decay 1e-2 \
        --clip-grad 1.0 \
        --warmup .01 \
        --save-interval 3000 \
        --query-in-block-prob 0.1 \
        --fp16
  11. Prepare data for GPT-2 training

    main

    After obtaining the raw JSONL file, follow this pipeline to clean, deduplicate, and shuffle the data for training:

    1. Cleanup: Perform ftfy, English language detection, and remove documents with fewer than 128 tokens. This step can be sharded.
    2. Find Duplicates: Use LSH to find possible duplicates. This step cannot be sharded and typically takes 12-24 hours for OpenWebText.
    3. Group Similar URLs: Group URLs that are similar based on the is_similar function (default threshold: 0.9).
    4. Remove Duplicates: Remove the similar documents identified in the grouping step.
    5. Shuffle: Shuffle the final dataset.
  12. Install Megatron-LM

    main

    You can install Megatron-LM either by cloning the repository or via pip. Ensure that python3-dev is installed on your system.

    Requirements:

    • Python 3.6
    • PyTorch 1.5 (with GPU support)
    • CUDA 10
    • NCCL 2.6 or above
    • NVIDIA APEX
    • NLTK (required for data preprocessing, but not for training or evaluation)

    Recommended Environment: Using an NVIDIA NGC PyTorch container is strongly recommended (e.g., nvcr.io/nvidia/pytorch:20.03-py3).

    pip install megatron-lm