Overview

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

The chapter introduces how modern small language models reuse the core Transformer ideas popularized by larger Llama-style models, but reshape them to better fit compact, fast inference settings. It uses Google’s Gemma-3-270M as a concrete example of this trend, showing that even very small models still rely on attention and MLP blocks, yet make architectural choices that emphasize memory efficiency and stable training. The main theme is that model design is now highly size-dependent: techniques that work well at hundreds of billions of parameters may need to be revised when the model shrinks to only a few hundred million.

A central focus is attention compression. By comparing projection sizes and configuration settings, the text shows how to identify whether a model uses multi-head attention, grouped-query attention, or multi-query attention. In Gemma-3-270M, the query projection is four times larger than the key and value projections, but the configuration reveals only four query heads and a single key/value head. That means the model uses multi-query attention, where all query heads share one set of key and value vectors, maximizing KV-cache savings and reducing memory use during generation.

The chapter also explains that Gemma refines this compact design with extra normalization layers inside attention and a GLU-based MLP. The added query and key normalization help prevent one head from dominating when many query heads share the same key/value state, keeping attention scores balanced across heads. Meanwhile, the MLP preserves the gated structure seen in larger families, showing that compact models do not simply remove sophistication; instead, they selectively simplify where memory matters most and add stabilization where training dynamics need it. Overall, Gemma-3-270M illustrates how SLMs compress the standard Transformer while preserving effectiveness.

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 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 blocks of Gemma-3-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 difference 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

With 18 blocks and an expansion ratio of 3.2x, Gemma-3-270M has the deep and narrow shape described in section 3.1.4, while Llama-3.2-1B, with 16 blocks and 4.0x, is wider and shallower.

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 A.1), but 18 blocks, 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.

None of these refinements changes the attention-plus-MLP structure of the Transformer block or the GLU of the MLP module. The stack of blocks remains available for depth pruning, and the GLU, which expands and contracts the information, for the Width Pruning techniques of Chapter 5.

FAQ

What is the main architectural focus of the Gemma-3-270M case study?The chapter focuses on how Gemma-3-270M refines modern Transformer design for a small language model, emphasizing compactness and fast inference while still using common optimizations like GQA-like attention and a GLU-based MLP.
Why do small language models need different architectural trade-offs than very large models?Because optimizations that work well in models with hundreds of billions of parameters are not always suitable for models with only a few hundred million parameters. Small models prioritize memory efficiency, speed, and compactness.
How can you tell whether a model uses Grouped-Query Attention or Multi-Query Attention from its projections?By comparing the output dimensions of the Query, Key, and Value projections. If the Query projection is larger than the Key and Value projections, the model is sharing KV information across multiple query heads. If there is only one KV head shared by all query heads, that is Multi-Query Attention.
What does the 4:1 ratio between q_proj and k_proj/v_proj indicate in Gemma-3-270M?It means the model has 4 Query heads sharing a single set of Key and Value vectors. This is a 4-to-1 Query-to-KV ratio.
How many Query heads and Key/Value heads does Gemma-3-270M use?Gemma-3-270M uses 4 Query heads and 1 Key/Value head, which makes it a Multi-Query Attention architecture.
Why is Gemma-3-270M considered an extreme form of KV compression?Because all 4 attention heads share a single Key and Value head. This minimizes the KV cache as much as possible for that head configuration.
What memory advantage does Multi-Query Attention provide over standard multi-head attention?Multi-Query Attention stores far fewer Key and Value vectors per token. In the chapter’s explanation, it compresses the KV cache so that one set of KV vectors can serve multiple query heads, greatly reducing memory use.
Why does Gemma-3-270M include q_norm and k_norm layers in attention?These normalization layers help keep Query and Key vectors at similar magnitudes. That prevents one head from dominating the attention scores and helps all heads contribute more evenly.
What problem can occur when multiple Query heads share a single set of Keys without normalization?The magnitudes of the Query heads can drift apart during training, causing the softmax attention weights to be overly influenced by the largest-magnitude head. This can make the model behave as if it had only one effective head.
What other Transformer component in Gemma-3-270M reflects modern architectural refinement besides attention?The MLP uses a GLU architecture, with gate_proj and up_proj layers along with a GELU-based activation, which improves contextual processing compared with simpler feedforward designs.

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


Close
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 Rearchitecting LLMs ebook for free
Close
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 Rearchitecting LLMs ebook for free