Overview

Appendix A. A Gemma-3 case study in SLM architectural refinements

This case study shows how modern small language models keep the Transformer foundation while refining it for efficiency and stability. The text begins by emphasizing that, although GQA and GLU became popular design patterns, newer model families still introduce their own adjustments, especially as model sizes shrink from massive LLMs to compact SLMs. Using Gemma-3-270M as the example, the discussion frames SLM design as a set of architectural trade-offs aimed at preserving capability while prioritizing compactness and faster inference.

The main attention-side refinement is a move from GQA to MQA. By inspecting Gemma’s layer shapes and configuration, the text shows that the model uses four query heads but only one key/value head, meaning all query heads share the same K and V vectors. This is presented as the most aggressive form of KV-cache compression, and the practical effect is dramatic: memory use drops sharply while the number of processable tokens increases substantially. The comparison with MHA and GQA makes clear that this sharing strategy is a major reason small models can handle longer contexts efficiently on limited hardware.

The chapter also explains why Gemma adds q_norm and k_norm inside attention: when multiple queries share a single KV set, normalization helps keep vector magnitudes balanced so no single head dominates the softmax calculation. Without this, the attention mechanism could become unstable or behave too much like a single-head model, reducing expressiveness. The text closes by noting that these shared-component designs complicate head pruning, since removing one head can disrupt the shared projections, and then transitions toward the model’s MLP refinements as the next part of the analysis.

Figure A.1 Attention mechanisms and memory optimization strategies. Multi-Head Attention (MHA) maintains independent K,V pairs for each Q head — one pair per processed token, forming a growing cache. Grouped-Query Attention (GQA) groups Q heads to share K,V pairs (here 6 Q heads in 3 groups, each sharing one K,V pair per token). Multi-Query Attention (MQA) compresses to the extreme: all Q heads

A.2 Refinements in the MLP module: expansion and stability

Throughout this book you've worked with GLU structures across multiple chapters, so its function as an expansion-contraction mechanism should already be familiar, and Gemma doesn't change these fundamental premises. Its MLP module has the same foundations, but introduces some interesting modifications, designed specifically to optimize performance in a model of such ”small” dimensions.

To start, let's recall the structure of Llama-3.2-1B's MLP Module:

We compare it with the relevant and equivalent layers of Gemma-3.2-270M:

As you can see, we've included the normalization layers in the structure. Although they are defined outside the MLP Module, they work together with it inside the Transformer block.

The first key difference we'll analyze is the expansion ratio, a parameter that defines the "width" of the processing in the MLP. Let's calculate it for both models using the structures we've just seen:

Llama-3.2-1B: 8192 (intermediate dimension) / 2048 (embedding dimension) = 4.0x

Gemma-3-270M: 2048 (intermediate dimension) / 640 (embedding dimension) = 3.2x

Gemma, with its 18 blocks and a more conservative expansion ratio, exemplifies a deeper and narrower model. It performs more sequential transformations, but each one is computationally less intensive.

In contrast, Llama-3.2-1B, with 16 layers but a much larger embedding and expansion ratio, represents a less deep and wider model. It executes fewer transformation steps, but each one has much greater processing capacity.

These characteristics are already giving us a hint about what type of pruning can be more effective in each model.

Along with the expansion ratio, the other variation we find is the activation function. While Llama uses SiLU(), Gemma opts for PytorchGELUTanh(), a variant of the GELU function. The GELU-Tanh function offers smoother gradients and more stable convergence, which is especially valuable in smaller models where each parameter has a greater impact.

Finally, we find the pre_feedforward_layernorm and post_feedforward_layernorm normalization layers. Although in the structure they appear listed at the Gemma3DecoderLayer level, their function is intimately linked to the MLP. These layers act like a "corset" that wraps around the MLP module, stabilizing the data that enters and exits it. In a "thin and deep" architecture like Gemma's, with a small embedding of 640 (you can see the embedding size in listing 3.1), but 18 layers, maintaining numerical stability throughout the entire stack of blocks is a challenge, and these additional layers are a safeguard to prevent problems during training and inference.

The most important aspect for us is that none of these optimizations alter the fundamental principle of the MLP module. The GLU structure that expands and contracts the information, and which is the target of our Width Pruning, remains intact, ready to be optimized with the techniques we discuss in Chapter 5.

FAQ

What is the main architectural goal behind the refinements in Gemma-3-270M?The goal is to keep the modern Transformer structure while adapting it for small language models (SLMs) through compactness and faster inference. Gemma-3-270M preserves the familiar attention-plus-MLP pattern, but uses memory-saving refinements such as MQA and extra normalization layers to make the model efficient on limited hardware.
How does Gemma-3-270M differ from Llama-style attention optimization?Llama popularized Grouped-Query Attention (GQA), where multiple query heads share key/value pairs in groups. Gemma-3-270M goes a step further and uses Multi-Query Attention (MQA), where all query heads share a single key/value head, reducing KV-cache memory even more.
What does Multi-Query Attention (MQA) mean in Gemma-3-270M?MQA means that all attention heads use one shared set of key and value vectors. In Gemma-3-270M, the configuration shows 4 query heads and 1 key/value head, which is the extreme form of KV sharing and provides maximum KV-cache compression.
How can you tell from the model structure whether attention is using GQA or MQA?You can inspect the projection layer sizes. If the query projection has a much larger output dimension than the key and value projections, the model is using KV sharing. The ratio between query size and key/value size indicates the grouping factor. In Gemma-3-270M, 1024 / 256 = 4, showing strong sharing, but the exact head counts come from the configuration file.
What are the attention head counts in Gemma-3-270M?The configuration shows 4 query heads and 1 key/value head. This confirms that Gemma-3-270M uses MQA rather than GQA, since all query heads share the same key and value vectors.
Why is MQA so important for small language models?MQA dramatically reduces KV-cache memory usage, which lets the model process much longer sequences without increasing memory cost. This is especially valuable for SLMs, which are designed to run efficiently on constrained hardware.
How much memory efficiency can MQA provide compared with MHA in the example from the chapter?In the table, a hypothetical model with MHA could process only 8 tokens with the given memory budget, while MQA could process 96 tokens. That is more than a 1100% increase in processable tokens at the same memory cost.
Why does Gemma-3-270M include q_norm and k_norm layers in attention?These normalization layers stabilize attention when multiple query heads share keys and values. They help keep vector magnitudes balanced so that no single head dominates the softmax calculation, preserving the benefits of multi-head behavior.
Why can shared attention components make head pruning more difficult?Because in GQA and MQA, heads are no longer fully independent. Removing one head can affect shared projections and cause misalignment in the attention mechanism, making pruning technically risky and harder to implement safely.
What broader lesson does the Gemma-3 case study illustrate about SLM architecture?The case study shows that architectural innovation continues even after a standard Transformer pattern is established. Small models may use different trade-offs than large models, and refinements like MQA and additional normalization can make them much more practical for real-world deployment.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Rearchitecting LLMs ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Rearchitecting LLMs ebook for free