[Paper Review] MosaicBERT: A Bidirectional Encoder Optimized for Fast Pretraining
MosaicBERT is a BERT-style bidirectional encoder optimized for fast pretraining by integrating FlashAttention, ALiBi, Gated Linear Units (GLU), dynamic unpadding, and low-precision LayerNorm. It achieves a SOTA 79.6 average GLUE score in just 1.13 hours on 8 A100 80GB GPUs at a cost of ~$20, demonstrating Pareto-optimality in accuracy vs. training time compared to standard BERT models.
Although BERT-style encoder models are heavily used in NLP research, many researchers do not pretrain their own BERTs from scratch due to the high cost of training. In the past half-decade since BERT first rose to prominence, many advances have been made with other transformer architectures and training configurations that have yet to be systematically incorporated into BERT. Here, we introduce MosaicBERT, a BERT-style encoder architecture and training recipe that is empirically optimized for fast pretraining. This efficient architecture incorporates FlashAttention, Attention with Linear Biases (ALiBi), Gated Linear Units (GLU), a module to dynamically remove padded tokens, and low precision LayerNorm into the classic transformer encoder block. The training recipe includes a 30% masking ratio for the Masked Language Modeling (MLM) objective, bfloat16 precision, and vocabulary size optimized for GPU throughput, in addition to best-practices from RoBERTa and other encoder models. When pretrained from scratch on the C4 dataset, this base model achieves a downstream average GLUE (dev) score of 79.6 in 1.13 hours on 8 A100 80 GB GPUs at a cost of roughly $20. We plot extensive accuracy vs. pretraining speed Pareto curves and show that MosaicBERT base and large are consistently Pareto optimal when compared to a competitive BERT base and large. This empirical speed up in pretraining enables researchers and engineers to pretrain custom BERT-style models at low cost instead of finetune on existing generic models. We open source our model weights and code.
Motivation & Objective
- To address the high computational cost and long training times of pretraining BERT-style models from scratch.
- To systematically incorporate recent transformer advancements—like FlashAttention, ALiBi, and GLU—into the classic BERT architecture for improved efficiency.
- To empirically demonstrate Pareto improvements in accuracy versus pretraining speed across multiple model sizes.
- To enable researchers and engineers to fine-tune custom BERT-style models affordably instead of relying on generic pre-trained models.
- To open-source model weights and code to foster reproducibility and broader adoption.
Proposed method
- Integrates FlashAttention to accelerate self-attention computation via optimized kernel operations.
- Applies Attention with Linear Biases (ALiBi) to enable relative positional encoding without positional embeddings.
- Uses Gated Linear Units (GLU) in feedforward layers with a fused implementation to improve representational capacity.
- Incorporates a dynamic unpadding module to skip computation on padded tokens, improving throughput on variable-length sequences.
- Employs low-precision LayerNorm (bfloat16) to reduce memory bandwidth and computation cost.
- Employs a 30% masking ratio for the Masked Language Modeling (MLM) objective and optimizes vocabulary size for GPU throughput.

Experimental results
Research questions
- RQ1Can architectural innovations from modern transformer variants be effectively integrated into the classic BERT architecture to accelerate pretraining without sacrificing accuracy?
- RQ2What is the trade-off between model accuracy and pretraining speed when combining multiple efficiency techniques in BERT-style models?
- RQ3Is MosaicBERT Pareto-optimal compared to standard BERT-Base and BERT-Large across different pretraining durations?
- RQ4How do individual architectural components (e.g., FlashAttention, GLU, unpadding) contribute to throughput and convergence speed?
- RQ5Does training a BERT-Base model to BERT-Large performance levels require significantly longer training, and can this be mitigated with architectural improvements?
Key findings
- MosaicBERT-Base achieves a downstream average GLUE (dev) score of 79.6 in just 1.13 hours on 8 A100 80GB GPUs, costing approximately $20 on standard cloud infrastructure.
- The model reaches this performance at a training cost of $22.60 using 8× A100 80GB GPUs, with a 30% MLM masking ratio and bfloat16 precision.
- MosaicBERT-Base and MosaicBERT-Large are empirically Pareto-optimal compared to standard BERT-Base and BERT-Large across multiple pretraining durations.
- Throughput ablation studies show that FlashAttention, ALiBi, and dynamic unpadding each contribute significantly to training speedup.
- The Fused GLU implementation, while slightly slower than standard GLU in some regimes, is used for its improved representational capacity and is compatible with high-batch-size training.
- BERT-Large only surpasses BERT-Base in performance after extensive training, highlighting the efficiency gains of architectural optimization.

Read this paper
Open the full text in the Nubint reader and ask about any passage as you read.
Read this paper on NubintThis review was generated by AI from the original paper. Please check the paper before citing it.