MosaicBERT: mosaic-bert-base-seqlen-256 Pretrained Model
MosaicBERT-Base is a new BERT architecture and training recipe optimized for fast pretraining.
MosaicBERT trains faster and achieves higher pretraining and finetuning accuracy when benchmarked against
Hugging Face's
bert-base-uncased
. It incorporates efficiency insights
from the past half a decade of transformers research, from RoBERTa to T5 and GPT.
This particular model was trained with
ALiBi
on a sequence length of 256 tokens.
import torch
import transformers
from transformers import AutoModelForMaskedLM, BertTokenizer, pipeline
from transformers import BertTokenizer, BertConfig
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased') # MosaicBERT uses the standard BERT tokenizer
config = transformers.BertConfig.from_pretrained('mosaicml/mosaic-bert-base-seqlen-256') # the config needs to be passed in
mosaicbert = AutoModelForMaskedLM.from_pretrained('mosaicml/mosaic-bert-base-seqlen-256',config=config,trust_remote_code=True)
# To use this model directly for masked language modeling
mosaicbert_classifier = pipeline('fill-mask', model=mosaicbert, tokenizer=tokenizer,device="cpu")
mosaicbert_classifier("I [MASK] to the store yesterday.")
Note that the tokenizer for this model is simply the Hugging Face
bert-base-uncased
tokenizer.
In order to take advantage of ALiBi by extrapolating to longer sequence lengths, simply change the
alibi_starting_size
flag in the
config file and reload the model.
config = transformers.BertConfig.from_pretrained('mosaicml/mosaic-bert-base-seqlen-256')
config.alibi_starting_size = 512# maximum sequence length updated to 512 from config default of 256
mosaicbert = AutoModelForMaskedLM.from_pretrained('mosaicml/mosaic-bert-base-seqlen-512',config=config,trust_remote_code=True)
This simply presets the non-learned linear bias matrix in every attention block to 512 tokens (note that this particular model was trained with a sequence length of 256 tokens).
[Update 1/2/2024] Triton Flash Attention with ALiBi
Note that by default, triton Flash Attention is
not
enabled or required. In order to enable our custom implementation of triton Flash Attention with ALiBi from March 2023,
set
attention_probs_dropout_prob: 0.0
. We are currently working on supporting Flash Attention 2 (see
PR here
).
Remote Code
This model requires that
trust_remote_code=True
be passed to the
from_pretrained
method. This is because we train using
FlashAttention (Dao et al. 2022)
, which is not part of the
transformers
library and depends on
Triton
and some custom PyTorch code. Since this involves executing arbitrary code, you should consider passing a git
revision
argument that specifies the exact commit of the code, for example:
However, if there are updates to this model or code and you specify a revision, you will need to manually check for them and update the commit hash accordingly.
FlashAttention
: Attention layers are core components of the transformer architecture. The recently proposed FlashAttention layer
reduces the number of read/write operations between the GPU HBM (high bandwidth memory, i.e. long-term memory) and the GPU SRAM
(i.e. short-term memory)
[Dao et al. 2022]
. We used the FlashAttention module built by
hazy research
with
OpenAI’s triton library
.
Attention with Linear Biases (ALiBi)
: In most BERT models, the positions of tokens in a sequence are encoded with a position embedding layer;
this embedding allows subsequent layers to keep track of the order of tokens in a sequence. ALiBi eliminates position embeddings and
instead conveys this information using a bias matrix in the attention operation. It modifies the attention mechanism such that nearby
tokens strongly attend to one another
[Press et al. 2021]
. In addition to improving the performance of the final model, ALiBi helps the
model to handle sequences longer than it saw during training. Details on our ALiBi implementation can be found
in the mosaicml/examples repo here
.
Unpadding
: Standard NLP practice is to combine text sequences of different lengths into a batch, and pad the sequences with empty
tokens so that all sequence lengths are the same. During training, however, this can lead to many superfluous operations on those
padding tokens. In MosaicBERT, we take a different approach: we concatenate all the examples in a minibatch into a single sequence
of batch size 1. Results from NVIDIA and others have shown that this approach leads to speed improvements during training, since
operations are not performed on padding tokens (see for example
Zeng et al. 2022
).
Details on our “unpadding” implementation can be found
in the mosaicml/examples repo here
.
Low Precision LayerNorm
: this small tweak forces LayerNorm modules to run in float16 or bfloat16 precision instead of float32, improving utilization.
Our implementation can be found
in the mosaicml/examples repo here
.
Modifications to the Feedforward Layers
Gated Linear Units (GLU)
: We used Gated Linear Units for the feedforward sublayer of a transformer. GLUs were first proposed in 2016
[Dauphin et al. 2016]
,
and incorporate an extra learnable matrix that “gates” the outputs of the feedforward layer. More recent work has shown that
GLUs can improve performance quality in transformers [
Shazeer, 2020
,
Narang et al. 2021
]. We used the GeLU (Gaussian-error Linear Unit)
activation function with GLU, which is sometimes referred to as GeGLU. The GeLU activation function is a smooth, fully differentiable
approximation to ReLU; we found that this led to a nominal improvement over ReLU. More details on our implementation of GLU can be found here.
The extra gating matrix in a GLU model potentially adds additional parameters to a model; we chose to augment our BERT-Base model with
additional parameters due to GLU modules as it leads to a Pareto improvement across all timescales (which is not true of all larger
models such as BERT-Large). While BERT-Base has 110 million parameters, MosaicBERT-Base has 137 million parameters. Note that
MosaicBERT-Base trains faster than BERT-Base despite having more parameters.
Training data
MosaicBERT is pretrained using a standard Masked Language Modeling (MLM) objective: the model is given a sequence of
text with some tokens hidden, and it has to predict these masked tokens. MosaicBERT is trained on
the English
“Colossal, Cleaned, Common Crawl” C4 dataset
, which contains roughly 365 million curated text documents scraped
from the internet (equivalent to 156 billion tokens). We used this more modern dataset in place of traditional BERT pretraining
corpora like English Wikipedia and BooksCorpus.
MosaicML Streaming Dataset
: As part of our efficiency pipeline, we converted the C4 dataset to
MosaicML’s StreamingDataset format
and used this
for both MosaicBERT-Base and the baseline BERT-Base. For all BERT-Base models, we chose the training duration to be 286,720,000 samples of
sequence length 256
; this covers 78.6% of C4.
Higher Masking Ratio for the Masked Language Modeling Objective
: We used the standard Masked Language Modeling (MLM) pretraining objective.
While the original BERT paper also included a Next Sentence Prediction (NSP) task in the pretraining objective,
subsequent papers have shown this to be unnecessary
Liu et al. 2019
.
However, we found that a 30% masking ratio led to slight accuracy improvements in both pretraining MLM and downstream GLUE performance.
We therefore included this simple change as part of our MosaicBERT training recipe. Recent studies have also found that this simple
change can lead to downstream improvements
Wettig et al. 2022
.
Bfloat16 Precision
: We use
bf16 (bfloat16) mixed precision training
for all the models, where a matrix multiplication layer uses bf16
for the multiplication and 32-bit IEEE floating point for gradient accumulation. We found this to be more stable than using float16 mixed precision.
Vocab Size as a Multiple of 64
: We increased the vocab size to be a multiple of 8 as well as 64 (i.e. from 30,522 to 30,528).
This small constraint is something of
a magic trick among ML practitioners
, and leads to a throughput speedup.
Hyperparameters
: For all models, we use Decoupled AdamW with Beta_1=0.9 and Beta_2=0.98, and a weight decay value of 1.0e-5.
The learning rate schedule begins with a warmup to a maximum learning rate of 5.0e-4 followed by a linear decay to zero.
Warmup lasted for 6% of the full training duration. Global batch size was set to 4096, and microbatch size was
256
; since global batch size was 4096, full pretraining consisted of 70,000 batches.
We set the
maximum sequence length during pretraining to 256
, and we used the standard embedding dimension of 768.
For MosaicBERT, we applied 0.1 dropout to the feedforward layers but no dropout to the FlashAttention module, as this was not possible with the OpenAI triton implementation.
Full configuration details for pretraining MosaicBERT-Base can be found in the configuration yamls
in the mosaicml/examples repo here
.
Intended uses & limitations
This model is intended to be finetuned on downstream tasks.
Citation
Please cite this model using the following format:
@article{portes2023MosaicBERT,
title={MosaicBERT: A Bidirectional Encoder Optimized for Fast Pretraining},
author={Jacob Portes, Alexander R Trott, Sam Havens, Daniel King, Abhinav Venigalla,
Moin Nadeem, Nikhil Sardana, Daya Khudia, Jonathan Frankle},
journal={NeuRIPS https://openreview.net/pdf?id=5zipcfLC2Z},
year={2023},
}
Runs of mosaicml mosaic-bert-base-seqlen-256 on huggingface.co
16
Total runs
0
24-hour runs
-3
3-day runs
-4
7-day runs
5
30-day runs
More Information About mosaic-bert-base-seqlen-256 huggingface.co Model
More mosaic-bert-base-seqlen-256 license Visit here:
mosaic-bert-base-seqlen-256 huggingface.co is an AI model on huggingface.co that provides mosaic-bert-base-seqlen-256's model effect (), which can be used instantly with this mosaicml mosaic-bert-base-seqlen-256 model. huggingface.co supports a free trial of the mosaic-bert-base-seqlen-256 model, and also provides paid use of the mosaic-bert-base-seqlen-256. Support call mosaic-bert-base-seqlen-256 model through api, including Node.js, Python, http.
mosaic-bert-base-seqlen-256 huggingface.co is an online trial and call api platform, which integrates mosaic-bert-base-seqlen-256's modeling effects, including api services, and provides a free online trial of mosaic-bert-base-seqlen-256, you can try mosaic-bert-base-seqlen-256 online for free by clicking the link below.
mosaicml mosaic-bert-base-seqlen-256 online free url in huggingface.co:
mosaic-bert-base-seqlen-256 is an open source model from GitHub that offers a free installation service, and any user can find mosaic-bert-base-seqlen-256 on GitHub to install. At the same time, huggingface.co provides the effect of mosaic-bert-base-seqlen-256 install, users can directly use mosaic-bert-base-seqlen-256 installed effect in huggingface.co for debugging and trial. It also supports api for free installation.
mosaic-bert-base-seqlen-256 install url in huggingface.co: