ModernBERT Documentation

repository·main·Indexed 23 days ago

https://github.com/answerdotai/modernbert

A research repository for modernizing the BERT architecture through architectural changes and scaling. It utilizes the Composer framework and FlexBERT modular building blocks for pre-training and evaluation. The repository includes tools for training single vector (DPR) and multi vector (ColBERT) retrieval models, GLUE benchmark fine-tuning, and utilities for managing MDS-format datasets and Flash Attention integration.

Tokens
12.8K
Snippets
30
Records
73
Agent score
83%

What's inside ModernBERT

  1. Configure dataset types: StreamingTextDataset vs NoStreamingDataset

    main

    ModernBERT supports two dataset classes located in src/text_data.py. You can switch between them by setting the streaming key in your configuration.

    • StreamingTextDataset: Inherits from StreamingDataset. Supports MDS, CSV/TSV, or JSONL formats for both text and tokenized data. Works with local data but may have uneven memory distribution over accelerators.
    • NoStreamingDataset: Requires decompressed MDS-format data. It is recommended for local data access as it enables higher training throughput.

    To decompress MDS data, use src/data/mds_conversion.py with the --decompress flag.

    train_loader:
      name: text
      dataset:
        streaming: false
  2. Install Flash Attention 3 for H100 GPUs

    main

    If you are using H100 GPUs, it is recommended to clone and build Flash Attention 3 manually from the source.

    git clone https://github.com/Dao-AILab/flash-attention.git
    cd flash-attention/hopper
    python setup.py install
  3. Authenticate with Hugging Face

    main

    If your checkpoints are stored in a private or gated Hugging Face repository, you must authenticate your environment. You can do this via the CLI or by providing a hub_token directly to the evaluation scripts.

    To authenticate via CLI:

    huggingface-cli login
  4. Skip specific evaluations in ablation configs

    main

    When generating an evaluation config with generate_eval_config_from_checkpoint.py, you can skip specific evaluation tasks by adding the --skip_<eval_name> flag. For example, to skip the MNLI evaluation, use --skip_mnli.

    python generate_eval_config_from_checkpoint.py \
    --checkpoint /path/to/checkpoint/folder \
    --wandb_entity entity_name \
    --wandb_project project_name \
    --track_run \
    --skip_mnli
  5. Re-generating the training data

    main

    To re-generate the training data used in BERT24, follow these steps:

    1. Install the necessary dependencies from requirements.txt and requirements-data.txt.
    2. Convert a Hugging Face (HF) dataset into the MDS format using hf_to_mds.py.
    3. Sample each dataset using sample_dataset_from_config.py (Note: implementation details for this step are currently marked as TODO in the source).
  6. Generate ablation evaluation configs

    main

    Use generate_eval_config_from_checkpoint.py to create evaluation configuration files based on a model checkpoint. You can generate configs by providing a local training configuration file or by matching the checkpoint to a Weights & Biases (wandb) run.

    To create a config using a local training config:

    python generate_eval_config_from_checkpoint.py \
    --checkpoint /path/to/checkpoint/folder \
    --train_config /path/to/config.yaml

    To create a config from a matching wandb run and enable wandb tracking:

    python generate_eval_config_from_checkpoint.py \
    --checkpoint /path/to/checkpoint/folder \
    --wandb_entity entity_name \
    --wandb_project project_name \
    --track_run
  7. Train single vector retrieval models with ModernBERT

    main

    To train a single vector retrieval model (DPR) using ModernBERT as the backbone, use the train_st.py script. This script utilizes Sentence Transformers and performs contrastive learning on the MS-MARCO dataset with mined hard negatives.

    Alternatively, you can use train_st_gooaq.py to train a single vector model specifically on the GooAQ question-answer dataset.

  8. Train and evaluate retrieval models

    main

    The examples directory contains scripts for training and evaluating retrieval models:

    ColBERT models (via PyLate):

    • examples/train_pylate.py: Boilerplate for training.
    • examples/evaluate_pylate.py: Boilerplate for evaluation.

    Dense models (via Sentence Transformers):

    • examples/train_st.py: Boilerplate for training.
    • examples/evaluate_st.py: Boilerplate for evaluation.
  9. Run evaluations for all checkpoints using `run_evals.py`

    main

    Use run_evals.py to automate evaluations across all checkpoints found in a directory. This script can automatically download checkpoints from the Hugging Face Hub, generate task configurations, and run evaluations in parallel across specified GPUs.

    To simplify execution, it is recommended to use a YAML configuration file instead of passing numerous command-line arguments.

    Workflow:

    1. Create a YAML configuration file (e.g., run_evals_args.yaml).
    2. Execute the script pointing to that config: python run_evals.py --config run_evals_args.yaml.

    Key Configuration Options (YAML):

    • checkpoints: Directory containing the checkpoints.
    • train_config: Path to the training configuration YAML (optional).
    • model_size: The FlexBert model config to use (e.g., base).
    • hub_repo: Hugging Face Hub repository ID ({org}/{repo}).
    • hub_token: HF token for private/gated repos.
    • tasks: List of tasks to evaluate (e.g., mnli, sst2, cola, mrpc).
    • parallel: Set to true to run evaluations on one checkpoint in parallel (note: may be unstable).
    • seeds: List of random seeds to use.
    • wandb_run: The name of the W&B run containing the pretraining config (required to download non-default configs).
    • wandb_project: The W&B project for pretraining config.
    • track_run: Set to true to log evaluation results to W&B.
    • wandb_entity: Your W&B username or team name.
    • track_run_project: The W&B project for evaluation logs.
    • gpu_ids: List of GPU indices to use (e.g., [0, 1]).