Abstract
Large-scale language model pretraining is a very successful form of self-supervised learning in natural language processing, but it is increasingly expensive to perform as the models and pretraining corpora have become larger over time. We propose NarrowBERT, a modified transformer encoder that increases the throughput for masked language model pretraining by more than 2x. NarrowBERT sparsifies the transformer model such that the self-attention queries and feedforward layers only operate on the masked tokens of each sentence during pretraining, rather than all of the tokens as with the usual transformer encoder. We also show that NarrowBERT increases the throughput at inference time by as much as 3.5x with minimal (or no) performance degradation on sentence encoding tasks like MNLI. Finally, we examine the performance of NarrowBERT on the IMDB and Amazon reviews classification and CoNLL NER tasks and show that it is also comparable to standard BERT performance.
Introduction
The training and deployment of large-scale language models like BERT have become central to modern NLP, yet they remain computationally intensive. A primary source of this inefficiency lies in the standard Transformer encoder architecture, which applies uniform computational effort to every token in a sequence across all layers.
This paper identifies two major inefficiencies in this paradigm:
- Pretraining Redundancy: The Masked Language Modeling (MLM) objective requires predicting only a small fraction (typically 15%) of tokens. However, standard models update representations for the remaining 85% of "unmasked" tokens in every layer.
- Inference Redundancy: For many downstream tasks, such as sentiment analysis or natural language inference, only the final representation of a single token (e.g., the
[CLS]token) is utilized for the final prediction.
NarrowBERT addresses these issues by "narrowing" the sequence length in later layers, focusing computation exclusively on the tokens relevant to the task at hand.
Methodology
NarrowBERT introduces a "narrowing" operation that gathers specific tokens into a reduced tensor for processing in subsequent layers. The authors explore two primary architectural variants:
ContextFirst (sfsf{10,s}:{10,f})
This approach reorders the standard interleaved self-attention (s) and feedforward (f) layers. All contextualization (self-attention) layers are moved to the beginning of the model, allowing every token to interact with every other token early on. Following this phase, unmasked tokens are dropped, and the remaining feedforward computations are performed only on the masked tokens.
SparseQueries (sf:{n,sf})
This variant maintains the standard layer order but modifies the attention mechanism in later "narrowed" layers. In these layers, only the masked tokens serve as queries. While they can still attend to the keys and values of all tokens (which were computed in earlier, "wide" layers), the model avoids calculating new representations for non-masked tokens.
Experimental Setup
The models were pretrained on a combination of English Wikipedia and the BooksCorpus. Evaluation was conducted across several benchmarks:
- GLUE Benchmark: Including MNLI (Inference), QNLI (Question Answering), SST-2 (Sentiment), STS-B (Similarity), QQP (Paraphrase), and WNLI.
- CoNLL-2003: For Named Entity Recognition (NER).
- IMDB & Amazon Reviews: For large-scale sentiment classification.
Key Results and Insights
NarrowBERT demonstrates that high-level feature extraction for all tokens is not strictly necessary for effective MLM or downstream performance.
Efficiency Gains
- Pretraining: NarrowBERT achieves a >2× speedup in pretraining throughput compared to a standard BERT-base model.
- Inference: For sentence classification tasks, inference throughput increases by up to 3.5×.
- Memory Usage: By reducing sequence lengths in later layers, NarrowBERT significantly lowers memory requirements, enabling larger batch sizes on existing hardware.
Performance Parity
Despite the significant reduction in FLOPs, NarrowBERT maintains performance comparable to standard BERT. For instance, on the MNLI task, the SparseQueries variant achieved a score of 0.82, nearly matching the baseline's 0.83. In some instances, such as the WNLI task, NarrowBERT variants even outperformed the standard baseline. Furthermore, NarrowBERT demonstrated superior speed and accuracy trade-offs compared to other efficiency-focused models like the Funnel Transformer.
Discussion and Future Work
The success of the ContextFirst model suggests that global contextualization is "front-loaded" in Transformer encoders. Once initial layers have established the necessary context, subsequent layers can focus on refining specific token representations.
While this study focused on BERT-base models and the MLM objective, future work could explore scaling these techniques to "Large" architectures, applying narrowing strategies to alternative pretraining objectives like ELECTRA, or extending the approach to generative tasks.
Conclusion
NarrowBERT offers a practical path toward more sustainable and efficient large-scale language modeling. By identifying and eliminating redundant computations on "unimportant" tokens, it provides substantial speedups in both pretraining and inference without sacrificing downstream task performance.
Summary updated