Designing the Model
local graph
Module 20 — Designing the Model
Section titled “Module 20 — Designing the Model”Chapter IV — Training Systems and Model Development
Before a single gradient is computed, the structure of a large language model must be decided. The decisions made at the architecture stage determine how many parameters the model will contain, how it will distribute computation across its layers, and what input lengths it can accept. These choices constrain everything that follows, from training duration and inference cost to the kinds of tasks the model can perform well. A poorly sized model wastes compute, while a poorly shaped model may fail to learn patterns that a better configuration would capture with ease. This module examines the core structural decisions that define a transformer-based language model. It covers parameter count and the balance between depth and width, along with context length selection and notable departures from the standard transformer design.
Depth vs. Width
Section titled “Depth vs. Width”Two models can contain the same number of parameters while differing substantially in shape. One might be deep, with many layers and a relatively narrow hidden dimension. The other might be wide, with fewer layers and a larger hidden dimension. The original transformer architecture proposed by Vaswani et al. (2017) used 6 layers with a model dimension of 512, establishing an early reference point. Modern large language models often use 32 or more layers with hidden dimensions of 4096 and above, but the optimal ratio between these dimensions is a matter of ongoing investigation.
Kaplan et al. (2020) observed that, when total parameter count is held constant, performance depends only weakly on the specific allocation between depth and width. A model with 24 layers and a hidden dimension of 1024 performed comparably to one with 12 layers and a hidden dimension that produced the same total parameter count. This finding suggested that, within a broad range, the overall number of parameters matters more than how they are distributed. The feedforward sub-layers within each transformer block typically account for roughly two-thirds of the parameters in that block, making the feedforward dimension a dominant factor in total model size regardless of how many layers are used.
| Deep & narrow | Shallow & wide | |
|---|---|---|
| Layers (depth) | 24 | 6 |
| Width | d_model = 1024 | d_model = 2048 |
| Total parameters | same | same |
[!visual-conversion] In print two stacks of rounded layer boxes side by side: 24 thin, narrow boxes against 6 taller, wider boxes, each with a depth arrow and a width arrow. The print draws about 12 boxes for the “6 Layers” stack (a printed inconsistency); the labels are carried here.
Figure 4-1. Trade-Off Between Depth and Width at Constant Parameter Count
Deeper models do offer certain advantages. Each additional layer provides another opportunity for the model to compose and refine its internal representations, which can support the learning of more abstract patterns. Deeper models can exhibit stronger out-of-distribution performance compared to wider models of the same size. This advantage appears to stem from the hierarchical processing that depth enables, where lower layers capture surface-level patterns and higher layers capture more complex dependencies.
The trade-off is that deeper models become harder to train. Gradient flow through many sequential layers can become unstable, and training may require careful tuning of learning rates and normalization strategies. Wider models, by contrast, increase representational capacity within each layer but impose higher per-token memory and latency costs during inference. The choice between a deep, narrow configuration and a shallow, wide one depends on the intended deployment environment and the training infrastructure available.
Attention heads represent another axis of configuration. In multi-head attention, the model dimension is split across multiple parallel attention operations, each with its own learned projection. The original transformer used 8 heads (Vaswani et al., 2017), while modern models often use 32 or more. Increasing the number of heads allows the model to capture a wider variety of relational patterns between tokens within a single layer. Recent work has explored whether reallocating parameters from depth into additional attention heads can maintain accuracy while reducing the total layer count, suggesting that the conventional emphasis on stacking many layers may overstate the importance of depth relative to attention parallelism.
Context Length
Section titled “Context Length”The maximum context length of a language model is set at design time and determines how many tokens the model can process in a single forward pass. A model with a context length of 2048 tokens can attend to roughly 1500 words of input, while one with a context length of 128,000 tokens can process long technical documents. This parameter has a direct impact on what the model can do, since tasks like document summarization and long-form reasoning require the model to hold large amounts of text in working memory simultaneously.
The cost of longer context windows is substantial. The standard self-attention mechanism computes pairwise interactions between all tokens in the input sequence, which means that its computational and memory requirements grow quadratically with sequence length (Tay et al., 2022). Doubling the context length from 4096 to 8192 tokens quadruples the attention cost. This quadratic scaling imposes a hard constraint on what context lengths are practically achievable with standard attention on a given hardware budget. Training a model with a 128,000-token context window using full self-attention would require considerably more memory and compute than training the same model with a 4096-token window.
For this reason, the choice of context length must be guided by the model’s intended application. A model designed for code completion or short-form question answering may function well with a context window of 4096 or 8192 tokens. A model intended for legal document analysis or multi-turn conversational agents will require much longer contexts. The designer must weigh the benefit of a longer window against the training cost it imposes and the inference latency it produces. In many practical settings, the right context length is the shortest one that reliably accommodates the intended input distribution. Choosing a context length that far exceeds what the application requires wastes memory and compute during both training and inference without delivering a proportional benefit.
Architecture Variants and Design Decisions
Section titled “Architecture Variants and Design Decisions”The standard transformer configuration, with full multi-head self-attention and dense feedforward layers, is not the only viable design. Several architectural modifications have emerged that offer meaningful efficiency gains or capability improvements for specific use cases. These modifications typically target one of two goals: reducing the cost of attention computation or increasing total model capacity without increasing per-token compute.
Grouped query attention, introduced by Ainslie et al. (2023), addresses the key inefficiency in the standard multi-head attention mechanism (Module 04). Grouped query attention reduces the cost of memory bandwidth demands by sharing key-value projections across groups of query heads. The result is inference speed comparable to multi-query attention, where all heads share a single key-value pair, while preserving quality much closer to full multi-head attention. This technique was adopted rapidly after its publication, appearing in widely used open-weight models within months of the original paper. The speed of adoption reflected the practical urgency of reducing inference costs, particularly for models deployed in interactive applications where latency is a critical concern.
flowchart TB
subgraph MHA[Multi-head attention]
direction TB
a1[Q1] --> k1[KV]
a2[Q2] --> k2[KV]
a3[Q3] --> k3[KV]
a4[Q4] --> k4[KV]
end
subgraph GQA[Grouped-query attention]
direction TB
subgraph G1[group]
b1[Q1] ~~~ b2[Q2]
end
subgraph G2[group]
b3[Q3] ~~~ b4[Q4]
end
G1 --> m1[KV]
G2 --> m2[KV]
end
subgraph MQA[Multi-query attention]
direction TB
c1[Q1] & c2[Q2] & c3[Q3] & c4[Q4] --> s1[KV]
end
Figure 4-2. Key-Value Sharing in Multi-Head, Grouped-Query, and Multi-Query Attention
Sliding window attention offers a different kind of efficiency gain by restricting the attention pattern itself. In this approach, each token attends only to a fixed-size window of neighboring tokens, which reduces the attention cost from quadratic to linear in sequence length. Beltagy et al. (2020) demonstrated this approach in the Longformer architecture, which combined local sliding window attention with a small number of global attention tokens that could attend to the full sequence. This hybrid design enabled the processing of documents with thousands of tokens at a fraction of the cost of full attention, while retaining the ability to propagate information across the entire input through the global tokens. The key insight was that most tokens in a long document primarily need local context, and only a few designated positions need to gather information from the full sequence.
Mixture-of-experts designs take a fundamentally different approach to scaling. In a standard dense transformer, every parameter participates in the computation for every input token. In a mixture-of-experts model, each token is routed to a small subset of specialized sub-networks, or experts, within each layer. Fedus et al. (2022) demonstrated this approach with the Switch Transformer, which simplified the routing mechanism by selecting a single expert per token. This design allowed the model to scale to over a trillion total parameters while keeping the per-token computational cost comparable to a much smaller dense model. The efficiency gains reported were significant, with models based on T5 achieving up to seven times faster pre-training speed using the same computational resources (Fedus et al., 2022). The trade-off is added complexity in routing and distributed training, but the efficiency gains can be dramatic for organizations with access to large-scale hardware.
Each of these variants addresses a specific limitation of the standard transformer. Grouped query attention reduces inference memory costs, while sliding window attention lowers the cost of processing long sequences. Mixture-of-experts decouples total model capacity from per-token compute. The choice of which modifications to adopt depends on the deployment constraints and the engineering resources available to implement and maintain the additional complexity that each variant introduces. In practice, many modern models combine several of these techniques simultaneously, using grouped query attention alongside sliding window patterns within a single architecture to achieve both inference efficiency and long-context capability.