Lesson 21 Transformer Architecture
Theme:

Homework

Complete these tasks before Lesson 22. Work in a notebook or script. The goal is to deepen your understanding of transformer internals by reasoning through concrete scenarios.

Task 1: KV Cache Memory Calculator

Goal: Build intuition for the memory costs of serving transformer models by calculating KV cache requirements for real model configurations.

Instructions

Write a Python function kv_cache_memory(n_layers, n_kv_heads, d_head, seq_len, batch_size, dtype_bytes=2) that computes the total KV cache memory in bytes. The formula is:

memory = 2 * n_layers * n_kv_heads * d_head * seq_len * batch_size * dtype_bytes
         ^                                                              ^
    (K and V)                                                    (float16 = 2 bytes)

Calculate KV cache memory for each of the following configurations at a sequence length of 4096 tokens and batch size of 1:

ModelLayersKV Headsd_head
Llama 2 7B3232128
Llama 2 13B4040128
Llama 2 70B (GQA)808128
Mistral 7B (GQA)328128

Then answer these questions:

  1. How much KV cache memory does Llama 2 70B save compared to what it would use with full MHA (80 KV heads instead of 8)?
  2. If you are serving Llama 2 70B with 80 GB of GPU memory available and the model weights take 35 GB (in 4-bit quantisation), how many concurrent requests at 4096 tokens can you fit in the remaining memory?
  3. If you double the context length to 8192, how does the concurrent request capacity change?

Self-check

Task 2: Attention Complexity Analysis

Goal: Quantify the computational cost of self-attention at different sequence lengths and understand why efficient attention matters.

Instructions

The FLOPs for standard self-attention (single head, single layer) are approximately:

FLOPs = 2 * n^2 * d_k   (for Q @ K^T)
      + 2 * n^2 * d_v   (for weights @ V)
      + n^2             (softmax, approximate)

Write a function that computes the total attention FLOPs for a full model (all heads, all layers). Then fill in this table:

Sequence lengthAttention FLOPs (Llama 2 7B)Ratio vs 2K
2,048?1.0x
4,096??
16,384??
131,072 (128K)??

Use Llama 2 7B parameters: 32 layers, 32 heads, d_head = 128.

Then answer:

  1. By what factor do attention FLOPs increase when going from 4K to 128K context? Does this match your expectation from O(n²)?
  2. If Flash Attention reduces memory from O(n²) to O(n) but does not change the FLOPs, why is it still faster?
  3. Research: what is the approximate ratio of attention FLOPs to total model FLOPs for Llama 2 7B at 4K context? (Hint: the FFN uses approximately 2x the FLOPs of attention.)

Self-check

Task 3: Architecture Trade-offs Essay

Goal: Articulate when you would choose each transformer architecture family and why.

Instructions

Write a short response (200–300 words) that addresses the following scenario:

Your team needs to build three different ML-powered features: (1) a semantic search engine over internal documents, (2) a customer-facing chatbot that answers questions in natural language, and (3) a system that translates user queries from English to SQL. For each feature, recommend a transformer architecture family (encoder-only, decoder-only, or encoder-decoder) and justify your choice. Consider factors like task structure, latency requirements, model size, and available pre-trained models.

Self-check

Submission

There is no formal submission for this course. These tasks are for your own development. However:

Next lesson: Lesson 22 — Pre-Training Objectives