BERT Architecture › Practical Considerations

Michael BrenndoerferUpdated July 16, 20253 min read
Language AI HandbookAll chapters
Part 24 of 67BERT and Variants
Chapter 169BERT Architecture › Practical Considerations
Reading section 8 of 12 · Practical Considerations · 3 minSection 8 of 12 · 3 min

Book contents

Section 8 of 12 · 3 min read

BERT Architecture › Practical ConsiderationsOne section · Sections in this chapter8 / 12 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

When deploying BERT, two factors dominate resource requirements: memory consumption and computational cost. Both scale with sequence length, making long documents particularly expensive to process. Understanding these scaling properties is essential for making informed decisions about batch sizes, sequence length truncation, and hardware requirements before you begin a project.

Memory Requirements

BERT's memory usage during inference scales with sequence length squared due to the attention mechanism. For a batch of sequences, the dominant terms are:

  • Embeddings: O(batch×seq×d)O(\text{batch} \times \text{seq} \times d)
  • Attention scores: O(batch×heads×seq2)O(\text{batch} \times \text{heads} \times \text{seq}^2)
  • Intermediate activations: O(batch×seq×4d)O(\text{batch} \times \text{seq} \times 4d)

For BERT-Base with a sequence length of 512 and batch size of 1, the attention scores alone require approximately 12 × 512 × 512 × 4 bytes (float32) = 12.6 MB per layer, or about 150 MB for all 12 layers.

Out[27]:
Visualization
Line plot showing memory increasing quadratically as sequence length grows from 128 to 512.
BERT memory usage scales quadratically with sequence length due to the attention mechanism, making long sequences significantly more expensive to process.

Computational Complexity

The computational complexity of BERT is dominated by three operations:

  1. Attention: O(n2⋅d)O(n^2 \cdot d) for sequence length nn and dimension dd
  2. Feed-forward: O(n⋅d2)O(n \cdot d^2) with the 4x expansion
  3. Embeddings: O(n⋅d)O(n \cdot d)

For typical sequence lengths (128-512), attention and feed-forward costs are comparable. The quadratic attention cost becomes prohibitive only for very long sequences, motivating efficient attention variants like Longformer and BigBird.

The crossover point between attention and feed-forward dominance is worth calculating explicitly. Attention cost scales as O(n2d)O(n^2 d) and feed-forward scales as O(nd2)O(n d^2), so attention dominates when n>dn > d. For BERT-Base with d=768d = 768, attention dominates for sequences longer than 768 tokens, which is already beyond BERT's 512-token limit. This means that for all practical BERT inputs, attention and feed-forward computation are similar in magnitude, and reducing sequence length reduces both approximately linearly.

In practice, most fine-tuning workloads for classification tasks truncate sequences to 128 tokens rather than using the full 512. At 128 tokens, BERT runs roughly 16 times faster than at 512 tokens (due to the quadratic attention term, a 4x reduction in length gives a 16x reduction in attention compute), with only modest performance degradation for tasks whose inputs fit in 128 tokens. This truncation is the single most impactful optimization you can make when deploying BERT in a latency-constrained environment.