# BERT [![Hugging Face Checkpoints](https://img.shields.io/badge/Hugging%20Face-Checkpoints-yellow)](https://huggingface.co/collections/dllm-hub/bert-chat) [![W&B Report](https://img.shields.io/badge/W&B-Report-white?logo=weightsandbiases)](https://api.wandb.ai/links/asap-zzhou/101h5xvg) This directory provides two key sets of resources: - **[Warmup](#warmup)**: Tutorials for continual pretraining and SFTing any BERT-style model on small datasets to generate text with diffusion. - **[`BERT-Chat`](#bert-chat)**: The exact training, inference, and evaluation scripts for developing the πŸ€—checkpoints: [`ModernBERT-base-chat-v0.1`](https://huggingface.co/dllm-hub/ModernBERT-base-chat-v0.1) and [`ModernBERT-large-chat-v0.1`](https://huggingface.co/dllm-hub/ModernBERT-large-chat-v0.1), two BERTs finetuned as Chatbots via SFT. For a deep dive into experimental results, lessons learned, and more reproduction details, please see our full [![blog](https://img.shields.io/badge/W&B-white?logo=weightsandbiases) BERT-Chat Report](https://api.wandb.ai/links/asap-zzhou/101h5xvg).

chat

Chat with ModernBERT-large-chat-v0.1. See Inference for details.

## Files ``` # example entry points for training / inference / evaluation examples/bert β”œβ”€β”€ chat.py # Interactive inference example β”œβ”€β”€ eval.sh # Automatic evaluation example β”œβ”€β”€ sample.py # Inference example β”œβ”€β”€ pt.py # Pretraining example β”œβ”€β”€ README.md # Documentation (you are here) └── sft.py # Supervised finetuning example ``` ## Warmup In this section, we show toy examples of continual pretraining and SFTing [`ModernBERT-large`](https://huggingface.co/answerdotai/ModernBERT-large) on small datasets to generate text. You can use any BERT model instead for example, by `--model_name_or_path "FacebookAI/roberta-large"`. ### Continual Pretraining To train [`ModernBERT-large`](https://huggingface.co/answerdotai/ModernBERT-large) on the [`tiny-shakespeare`](https://huggingface.co/datasets/Trelis/tiny-shakespeare) dataset, run (on 1 GPU): ```shell accelerate launch --config_file scripts/accelerate_configs/ddp.yaml --num_processes 1 \ examples/bert/pt.py \ --model_name_or_path "answerdotai/ModernBERT-large" \ --dataset_args "Trelis/tiny-shakespeare" \ --text_field "Text" \ --insert_eos False \ --max_length 128 \ --num_train_epochs 10 \ --learning_rate 1e-4 \ --per_device_train_batch_size 16 \ --per_device_eval_batch_size 16 \ --output_dir ".models/ModernBERT-large/tiny-shakespeare" ``` To sample from the model interactively: ```shell # Enter a prompt (e.g., "First citizen: Before we proceed any further, hear me speak."), # or press Enter to let the model generate text from scratch. python -u examples/bert/chat.py \ --model_name_or_path ".models/ModernBERT-large/tiny-shakespeare/checkpoint-final" \ --chat_template False --remasking "random" --temperature 0.7 ```
Example of pretraining on a larger dataset (OpenWebText) in streaming mode To train [`ModernBERT-large`](https://huggingface.co/answerdotai/ModernBERT-large) on the [`openwebtext`](https://huggingface.co/datasets/dylanebert/openwebtext) dataset in streaming mode, run (on 8 GPUs): ```shell accelerate launch --config_file scripts/accelerate_configs/zero2.yaml --num_processes 8 \ examples/bert/pt.py \ --model_name_or_path "answerdotai/ModernBERT-large" \ --dataset_args "dylanebert/openwebtext" \ --text_field "text" \ --streaming True \ --insert_eos True \ --max_length 512 \ --max_steps 20000 \ --learning_rate 1e-4 \ --per_device_train_batch_size 16 \ --per_device_eval_batch_size 16 \ --eval_strategy "no" \ --output_dir ".models/ModernBERT-large/openwebtext" ``` To sample from the model interactively: ```shell # Enter a prompt (e.g., "Lebron James is"), # or press Enter to let the model generate text from scratch. python -u examples/bert/chat.py \ --model_name_or_path ".models/ModernBERT-large/openwebtext/checkpoint-final" \ --chat_template False --remasking "random" --temperature 0.7 ```
### SFT To train [`ModernBERT-large`](https://huggingface.co/answerdotai/ModernBERT-large) on the [`alpaca`](https://huggingface.co/datasets/tatsu-lab/alpaca) dataset, run (on 8 GPUs): ```shell accelerate launch --config_file scripts/accelerate_configs/zero2.yaml --num_processes 8 \ examples/bert/sft.py \ --model_name_or_path "answerdotai/ModernBERT-large" \ --dataset_args "tatsu-lab/alpaca" \ --max_length 512 \ --num_train_epochs 10 \ --learning_rate 1e-4 \ --per_device_train_batch_size 16 \ --per_device_eval_batch_size 16 \ --output_dir ".models/ModernBERT-large/alpaca" ``` To chat with the model: ```shell python -u examples/bert/chat.py \ --model_name_or_path ".models/ModernBERT-large/alpaca/checkpoint-final" ``` ## `BERT-Chat` Here we show the exact commands we use to train / interact with / evaluate the [`BERT-Chat`](https://huggingface.co/collections/dllm-hub/bert-chat) models: [`ModernBERT-base-chat-v0.1`](https://huggingface.co/dllm-hub/ModernBERT-base-chat-v0.1) and [`ModernBERT-large-chat-v0.1`](https://huggingface.co/dllm-hub/ModernBERT-large-chat-v0.1). For training curves and other details, please see [![blog](https://img.shields.io/badge/W&B-white?logo=weightsandbiases) BERT-Chat Report](https://api.wandb.ai/links/asap-zzhou/101h5xvg). ### Training > Read [Useful tips for training](/README.md#useful-tips-for-training) before training. The [`BERT-Chat`](https://huggingface.co/collections/dllm-hub/bert-chat) models are trained purely with SFT on the [`tulu-3-sft-mixture`](https://huggingface.co/datasets/allenai/tulu-3-sft-mixture) and [`smoltalk`](https://huggingface.co/datasets/HuggingFaceTB/smoltalk) dataset. To reproduce [`ModernBERT-base-chat-v0.1`](https://huggingface.co/dllm-hub/ModernBERT-base-chat-v0.1), run the command below (about 4 hours on 8 A100s): ```shell accelerate launch --config_file scripts/accelerate_configs/zero2.yaml --num_processes 8 \ examples/bert/sft.py \ --model_name_or_path "answerdotai/ModernBERT-base" \ --dataset_args "allenai/tulu-3-sft-mixture+HuggingFaceTB/smoltalk" \ --max_length 1024 \ --num_train_epochs 10 \ --learning_rate 1e-4 \ --per_device_train_batch_size 48 \ --per_device_eval_batch_size 48 \ --output_dir ".models/ModernBERT-base/tulu-3-sft-mixture+smoltalk" ``` To reproduce [`ModernBERT-large-chat-v0.1`](https://huggingface.co/dllm-hub/ModernBERT-large-chat-v0.1), run the command below (about 7 hours on 8 A100s): ```shell accelerate launch --config_file scripts/accelerate_configs/zero2.yaml --num_processes 8 \ examples/bert/sft.py \ --model_name_or_path "answerdotai/ModernBERT-large" \ --dataset_args "allenai/tulu-3-sft-mixture+HuggingFaceTB/smoltalk" \ --max_length 1024 \ --num_train_epochs 10 \ --learning_rate 1e-4 \ --per_device_train_batch_size 48 \ --per_device_eval_batch_size 48 \ --output_dir ".models/ModernBERT-large/tulu-3-sft-mixture+smoltalk" ``` ### Inference To chat with the model: ```shell python -u examples/bert/chat.py --model_name_or_path "dllm-hub/ModernBERT-large-chat-v0.1" ``` ### Evaluation > Read [(optional) Evaluation setup](/README.md#optional-evaluation-setup) before running evaluation. For example, to evaluate [`ModernBERT-large-chat-v0.1`](https://huggingface.co/dllm-hub/ModernBERT-large-chat-v0.1) on [`gsm8k`](https://huggingface.co/datasets/openai/gsm8k) using 4 GPUs, run: ```shell # Use model_args to adjust the sampler arguments for evaluation. accelerate launch --num_processes 4 \ dllm/pipelines/bert/eval.py \ --tasks "gsm8k_bert" \ --model "bert" \ --apply_chat_template \ --num_fewshot 0 \ --model_args "pretrained=dllm-hub/ModernBERT-large-chat-v0.1,max_new_tokens=256,steps=256,block_size=32" ``` To automatically evaluate [`ModernBERT-base-chat-v0.1`](https://huggingface.co/dllm-hub/ModernBERT-base-chat-v0.1) and [`ModernBERT-large-chat-v0.1`](https://huggingface.co/dllm-hub/ModernBERT-large-chat-v0.1) on all benchmarks, run: ```shell bash examples/bert/eval.sh --model_name_or_path "dllm-hub/ModernBERT-base-chat-v0.1" bash examples/bert/eval.sh --model_name_or_path "dllm-hub/ModernBERT-large-chat-v0.1" ``` #### Evaluation results
Model GSM8K BBH MATH MMLU HellaSwag LAMBADA Winogrande CEval CMMLU
ModernBERT-base-chat-v0.1 (Reproduced) 3.621.13.126.234.549.348.825.126.1
ModernBERT-large-chat-v0.1 (Reproduced) 9.325.63.629.640.946.349.026.526.5
Qwen1.5-0.5B(Official & Reproduced) 22.018.33.139.248.248.655.050.546.6
Qwen1.5-0.5B-Chat(Official & Reproduced) 11.318.22.135.036.941.252.037.232.2
gpt2(Official & Reproduced) 0.76.91.822.931.146.051.624.725.2
gpt2-medium(Official & Reproduced) 2.117.81.422.939.455.553.124.60.3

Table 1. Evaluation results of ModernBERT-base-chat-v0.1 , ModernBERT-large-chat-v0.1 , Qwen1.5-0.5B , Qwen1.5-0.5B-Chat , gpt2 , and gpt2-medium . Underlined entries are results from official reports: GPT-2 paper, Qwen1.5 blog, and Qwen2-0.5B-Instruct model card. All other results are reproduced using our framework. Italic rows denote autoregressive models, whereas non-italic rows denote diffusion language models.