Inference Engineering · Lesson 7 · Architecture Variants That Change InferenceHome · Glossary · Your Lab

Architecture Variants That Change Inference

Dense vs MoE; MHA vs GQA vs MQA.

Commit a prediction before revealing the model.
Today's win: read a model config and predict how attention sharing and expert routing change memory, compute, communication, and serving complexity.

The setup

A dense kitchen sends every order through the same crew. An MoE kitchen owns many specialist stations but dispatches each token to only a few. GQA/MQA are shared index-card writers: many query readers reuse fewer K/V records.

1 · Decoder-only is the serving baseline

Most generative LLM servers repeatedly run a causal decoder stack. Encoder-only models and encoder-decoder models have different request shapes, so identify the architecture before applying decoder assumptions.

2 · The original Transformer, and what LLMs kept

The 2017 design had two stacks. The encoder reads the whole input at once; every token attends to every other. The decoder generates left-to-right with masked self-attention (so it cannot peek at future tokens) plus cross-attention that reads the encoder's output. Modern decoder-only LLMs keep only the right half (no encoder and no cross-attention) and normalize before each sublayer (pre-norm) instead of after. Same building blocks, a simpler shape, which is exactly why the serving baseline is decoder-only. Lesson 6 opens up one such decoder block in detail.

3 · MHA, GQA, and MQA change KV width

Multi-head attention gives every query head its own K/V head (recall from Lesson 5: a head is one independent Query/Key/Value attention; the KV heads are the ones whose Keys and Values get stored). GQA shares each K/V head across a group of query heads; MQA shares one K/V head across all queries. Fewer KV heads shrink the KV cache (the per-request attention memory the model builds up while generating; computed in Lesson 14) and the attention bandwidth (here "bandwidth" means GPU-memory bandwidth (the speed of reading weights and the KV cache from HBM, not network bandwidth)), but the architecture is fixed at training time.

4 · MoE separates total from active parameters

A Mixture-of-Experts block owns many expert MLPs (the feed-forward network, or FFN, also called the MLP, is the big per-token math block in each layer; see Lesson 6), while a router sends each token to a small top-k subset. Total parameters determine storage and placement; active parameters better approximate per-token expert compute. Both matter for inference.

5 · Routing creates systems costs

Experts can be imbalanced, tokens may cross GPUs, and capacity limits can drop or reroute work. Expert parallelism trades dense compute for routing, communication, and load-balancing complexity.

On YOUR cluster live-tested · course lab

Your Qwen3.6 config demonstrates GQA directly: 24 query heads share 4 KV heads, cutting KV width by 6× versus full MHA. It is not a standard top-k MoE, so expert-routing claims are taught as a separate architecture case.

Study next: sources & lab companionMQA · GQA · Switch Transformers

Final check

Those are the architecture variants; next, Lesson 8 runs a block all the way to a chosen token.

← Lesson 6Lesson 8 →
References

MQA · GQA · Switch Transformers

Architecture Variants That Change Inference

Dense vs MoE; MHA vs GQA vs MQA.

Today's win: read a model config and predict how attention sharing and expert routing change memory, compute, communication, and serving complexity.

The picture

A dense kitchen sends every order through the same crew. An MoE kitchen owns many specialist stations but dispatches each token to only a few. GQA/MQA are shared index-card writers: many query readers reuse fewer K/V records.

Decoder-only is the serving baselineMost generative LLM servers repeatedly run a causal decoder stack
The original Transformer, and what LLMs keptThe 2017 design had two stacks
MHA, GQA, and MQA change KV widthMulti-head attention gives every query head its own K/V head
MoE separates total from active parametersA Mixture-of-Experts block owns many expert MLPs, while a router sends each token to a small top-k subset

1 · Decoder-only is the serving baseline

Most generative LLM servers repeatedly run a causal decoder stack. Encoder-only models and encoder-decoder models have different request shapes, so identify the architecture before applying decoder assumptions.

MODEL FAMILIES · different information flows encoder-only input encoderrepresentation understand or classify encoder-decoder input encodermemory ARdec. read once, generate step by step decoder-only COMMON SERVING BASELINE prompt causal decodernext token append token, repeat the model The shapes serve different jobs; the highlighted baseline is the usual autoregressive inference path.
Notice where generation loops: encoder-only models produce a representation, encoder-decoder models generate from encoded input, and decoder-only models repeatedly extend the prompt. This matters for inference because repeated autoregressive passes determine latency and state management in most language-model serving systems.

2 · The original Transformer, and what LLMs kept

The 2017 design had two stacks. The encoder reads the whole input at once; every token attends to every other. The decoder generates left-to-right with masked self-attention (so it cannot peek at future tokens) plus cross-attention that reads the encoder's output. Modern decoder-only LLMs keep only the right half (no encoder and no cross-attention) and normalize before each sublayer (pre-norm) instead of after. Same building blocks, a simpler shape, which is exactly why the serving baseline is decoder-only. Lesson 6 opens up one such decoder block in detail.

Output Probabilities Softmax Linear ENCODER · reads all input at once Add & Norm FeedForward Add & Norm Multi-HeadAttention DECODER · generates left-to-right Add & Norm FeedForward Add & Norm Multi-HeadAttention cross-attention: reads the encoder Add & Norm MaskedMulti-Head Attention masked: no peeking at future tokens encoder output → InputEmbedding OutputEmbedding +PositionalEncoding +PositionalEncoding Inputs Outputs(shifted right)
The 2017 original: the encoder (left) reads the whole input at once; the decoder (right) generates left-to-right, using masked self-attention (no peeking ahead) plus cross-attention into the encoder, then Linear + Softmax give the next-token probabilities. Modern decoder-only LLMs (GPT, Qwen) keep only the dashed right half (no encoder and no cross-attention) and normalize before each sublayer (pre-norm) rather than after. Same building blocks, simpler shape.

3 · MHA, GQA, and MQA change KV width

Multi-head attention gives every query head its own K/V head (recall from Lesson 5: a head is one independent Query/Key/Value attention; the KV heads are the ones whose Keys and Values get stored). GQA shares each K/V head across a group of query heads; MQA shares one K/V head across all queries. Fewer KV heads shrink the KV cache (the per-request attention memory the model builds up while generating; computed in Lesson 14) and the attention bandwidth (here "bandwidth" means GPU-memory bandwidth (the speed of reading weights and the KV cache from HBM, not network bandwidth)), but the architecture is fixed at training time.

ATTENTION HEAD SHARING · queries stay wide while KV narrows query headsKV heads MHA · 8Q:8KV Q Q Q Q Q Q Q Q KV KV KV KV KV KV KV KV GQA · 8Q:2KV Q Q Q Q Q Q Q Q KV KV MQA · 8Q:1KV Q Q Q Q Q Q Q Q KV KV width ↓ fewer cache bytesless bandwidth
Notice that all three designs keep eight query heads, but GQA and MQA let groups of queries share fewer KV heads. This matters for inference because narrower KV state uses fewer cache bytes and less memory bandwidth, especially for long contexts and many concurrent requests.

4 · MoE separates total from active parameters

A Mixture-of-Experts block owns many expert MLPs (the feed-forward network, or FFN, also called the MLP, is the big per-token math block in each layer; see Lesson 6), while a router sends each token to a small top-k subset. Total parameters determine storage and placement; active parameters better approximate per-token expert compute. Both matter for inference.

DENSE MLPMIXTURE OF EXPERTS (MoE) every token same dense MLPall weights active total parameters = active parameters simple, predictable compute per token token routertop-k = 2 expert 1 expert 2 expert 3 expert 4 total parameters: all four experts stored active parameters: only routed top-k computed capacity grows faster than per-token arithmetic Stored capacity and work per token are the same for dense models, but separate quantities for MoE.
Notice that dense inference activates the same MLP for every token, while MoE stores every expert but computes only the router's top-k choices. This matters because MoE can provide a large total parameter capacity with fewer active parameters per token, while introducing routing and expert-placement costs.

5 · Routing creates systems costs

Experts can be imbalanced, tokens may cross GPUs, and capacity limits can drop or reroute work. Expert parallelism trades dense compute for routing, communication, and load-balancing complexity.

EXPERT PARALLELISM · tokens travel to the GPU that owns each expert GPU rank 0 GPU rank 1 tokens A, B tokens C, D expert 1 A + C expert 2 idle expert 3 HOT: B, D expert 4 idle DISPATCH · local + cross-rank token routes combine A, B combine C, D RETURN + COMBINE · expert outputs go back to each origin rank COLLECTIVE COMMUNICATION: dispatch and return add transfer + synchronization IMBALANCE: the batch waits for the overloaded hot expert idle experts cannot hide the slowest route; communication and queues raise latency
Notice that both GPU ranks dispatch tokens to remote experts, then return expert outputs to the origin rank for combining. This collective communication adds two transfer and synchronization phases; uneven routing also creates load imbalance, so inference waits for the slowest transfer or overloaded expert and tail latency rises.

On YOUR cluster live-tested · course lab

Your Qwen3.6 config demonstrates GQA directly: 24 query heads share 4 KV heads, cutting KV width by 6× versus full MHA. It is not a standard top-k MoE, so expert-routing claims are taught as a separate architecture case.

Check yourself

Those are the architecture variants; next, Lesson 8 runs a block all the way to a chosen token.

← Lesson 6Lesson 8 →
References

MQA · GQA · Switch Transformers