Chapter 9: Stacking Transformer Blocks

By ·

Chapter 9: Stacking Transformer Blocks

The Complete Block

One transformer block consists of:

Input
  ↓
Multi-Head Attention
  ↓
Dropout
  ↓
Add (Residual) + LayerNorm
  ↓
Feed-Forward Network
  ↓
Dropout
  ↓
Add (Residual) + LayerNorm
  ↓
Output

Stacking Multiple Blocks

Hyperparameter: Number of layers N

The depth of a transformer (how many identical blocks we stack) is crucial to its capability:

  • GPT-2 Small: 12 layers (117M parameters)
  • GPT-2 Medium: 24 layers (345M parameters)
  • GPT-2 Large: 36 layers (774M parameters)
  • GPT-2 XL: 48 layers (1.5B parameters)
  • GPT-3: 96 layers (175B parameters)
  • GPT-4: architecture not publicly disclosed, but widely believed to be significantly larger and more complex than GPT-3

The Abstraction Ladder: From Letters to Meaning

Why stack so many layers? Think of understanding language like building a pyramid of understanding:

Early Layers (1-20): Learning the Alphabet and Grammar

What happens here: The model learns the building blocks.

Like a child learning to read:

  • Age 2-3: "A is for Apple! B is for Ball!"
  • Recognizes individual letters and sounds
  • Can identify simple word patterns

What the model learns:

  • Layer 1-3: "This is the letter 't', this is 'h', together they make a pattern"
  • Layer 4-7: "These patterns form parts of speech - nouns, verbs, adjectives"
  • Layer 8-12: "Subjects usually come before verbs in English"
  • Layer 13-20: "These words often appear together: 'peanut butter', 'New York'"

Example: After Layer 10, when seeing "The cat":

Model thinks: "Okay, 'The' is a determiner (article), 'cat' is a noun. This is a noun phrase pattern."

Still very mechanical - recognizing patterns, not understanding meaning!

Middle Layers (20-60): Understanding What Words Mean

What happens here: The model starts connecting words to concepts and meanings.

Like a child in elementary school:

  • Age 7-10: Reads full sentences and understands stories
  • Learns that "big", "large", "huge" mean similar things
  • Understands relationships: "A dog is an animal", "Paris is a city in France"

What the model learns:

  • Layer 20-30: "Dog, puppy, canine all refer to the same type of creature"
  • Layer 31-40: "Bank (financial) is different from bank (river edge) based on context"
  • Layer 41-50: "Paris is the capital of France - entity relationships"
  • Layer 51-60: "If it's raining, people use umbrellas - causal understanding"

Example: After Layer 40, when seeing "The cat sat on the":

Model thinks: "Cat is a specific type of animal. They sit on physical objects. 
Next word is probably a noun like 'mat', 'chair', 'sofa' - something cat-sized and sittable."

Now understanding MEANING, not just patterns!

Late Layers (60-96): Abstract Reasoning and Logic

What happens here: The model performs complex reasoning and makes inferences.

Like a teenager/adult:

  • Age 15+: Can debate, understand metaphors, make logical arguments
  • Understands implied information
  • Can reason about hypotheticals

What the model learns:

  • Layer 60-70: "If A implies B, and B implies C, then A must imply C" (logical chains)
  • Layer 71-80: "The keys that I put on the table this morning are gone" (resolve long dependencies)
  • Layer 81-90: Understanding sarcasm, tone, implied meanings
  • Layer 91-96: Complex multi-step reasoning, integrating knowledge across domains

Example: After Layer 90, when seeing "The lawyer said the contract was ironclad, but":

Model thinks: "'Ironclad' means very strong/unbreakable. 'But' signals a contradiction coming.
Next word likely expresses doubt or finding a problem: 'unfortunately', 'however', 'the', 'she'...
This requires understanding legal contexts, metaphor ('ironclad'), and discourse structure ('but')."

Performing genuine abstract reasoning!

Visualizing the Abstraction Pyramid

Layer 96 →  🎓 Philosophy & Abstract Reasoning
Layer 80 →  📚 Complex Understanding & Inference
Layer 60 →  💡 Meaning & Concepts
Layer 40 →  📖 Word Relationships & Semantics
Layer 20 →  ✏️ Grammar & Syntax Patterns
Layer 10 →  🔤 Parts of Speech
Layer 1  →  🅰️ Basic Letter Patterns

Input: Raw word embeddings + positions

Each layer builds on the previous! Just like you can't do calculus before learning algebra, the model can't do reasoning (layer 90) without first learning word meanings (layer 40)!

The progression for "I love pizza":

After Layer 1:

"I" = [Simple pattern: pronoun marker]
"love" = [Simple pattern: verb marker]  
"pizza" = [Simple pattern: noun marker]

After Layer 20:

"I" = [Subject of sentence, first person singular]
"love" = [Present tense verb, expresses positive emotion, subject='I']
"pizza" = [Object of verb, food category, singular noun]

After Layer 50:

"I" = [Speaker making a statement about personal preference]
"love" = [Strong positive emotion/preference toward object]
"pizza" = [Specific food item, Italian cuisine, commonly liked, object of affection]

After Layer 96:

"I" = [Subject expressing personal preference, casual context, probably informal conversation,
       indicates speaker has tried pizza before, positive past experiences]
"love" = [Strong preference indicator, suggests repeated positive experiences,
          not literal love but intense liking, informal register]
"pizza" = [End of statement about food preference, suggests context might continue with
           elaboration or related food discussion, creates expectation for continuation]

See how understanding deepens layer by layer? That's the power of stacking!

Our example: Let's use N = 3 layers to stay manageable

Watch how the embedding for "I" evolves:

After Block 1: [0.583, -1.651, 1.185, 0.214, -1.010, 0.680]

  • The model has gathered basic information: "I" is a pronoun, it's at the start of a sentence, it's the subject

After Block 2: [0.721, -0.893, 0.567, 0.991, -0.445, 0.289]

  • Now it knows: "I" is connected to "love," which suggests positive sentiment; "pizza" comes later, indicating this is about food preferences

After Block 3: [0.834, -0.671, 0.403, 1.124, -0.289, 0.156]

  • The representation has been refined: "I" in the context of "I love pizza" conveys enthusiasm about food, informal conversational tone, personal preference statement

Each block adds a layer of sophistication! The representation progressively captures more nuanced meaning.


Course navigation: Previous: Chapter 8 - Residual Connections & Layer Normalization | Next: Chapter 10 - The Output Head