# BERT
[](https://huggingface.co/collections/dllm-hub/bert-chat)
[](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 [ BERT-Chat Report](https://api.wandb.ai/links/asap-zzhou/101h5xvg).
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 [ 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.6 | 21.1 | 3.1 | 26.2 | 34.5 | 49.3 | 48.8 | 25.1 | 26.1 |
ModernBERT-large-chat-v0.1 (Reproduced)
|
9.3 | 25.6 | 3.6 | 29.6 | 40.9 | 46.3 | 49.0 | 26.5 | 26.5 |
|
Qwen1.5-0.5B(Official & Reproduced)
|
22.0 | 18.3 | 3.1 | 39.2 | 48.2 | 48.6 | 55.0 | 50.5 | 46.6 |
Qwen1.5-0.5B-Chat(Official & Reproduced)
|
11.3 | 18.2 | 2.1 | 35.0 | 36.9 | 41.2 | 52.0 | 37.2 | 32.2 |
gpt2(Official & Reproduced)
|
0.7 | 6.9 | 1.8 | 22.9 | 31.1 | 46.0 | 51.6 | 24.7 | 25.2 |
gpt2-medium(Official & Reproduced)
|
2.1 | 17.8 | 1.4 | 22.9 | 39.4 | 55.5 | 53.1 | 24.6 | 0.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.