Chapter 7. Transformer Architecture
The transformer architecture was introduced in 2017 in the seminal paper “Attention is All You Need” (Vaswani et al., 2017), and has since revolutionized machine learning. Today, transformers form the backbone of state-of-the-art models.
In this chapter, we will review how the transformer architecture works, from its core building blocks to the design choices used in modern large language models. We begin with the three main transformer variants, and then examine the transformer block itself, including self-attention, masked attention, cross-attention, multi-head attention, feed-forward networks, residual connections, and normalization.
We then look at how transformers represent token order through positional encoding. Finally, we connect these components to modern LLM design choices, such as why decoder-only architectures became dominant, how models such as LLaMA and Mistral modify the original Transformer recipe, and how newer approaches such as state-space and hybrid architectures compare. The interview Q&A section goes deeper into the trade-offs, implementation details, and common architecture questions you are likely to encounter in technical interviews.
Transformer Architecture Types
Transformer models can be encoder-based, decoder-based, or encoder-decoder-based.
Encoder-based models consist of a stack of encoder layers that transform the input sequence into context-aware embeddings. This structure is used in models such as BERT for ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access