๐Ÿ”ฅLimited Offer: Get 50% OFFon AI & Full Stack Courses๐Ÿ”ฅ
Back to Deep Learning Notes
Topic #276

Transformer Blocks

This note focuses specifically on the "block" as the fundamental unit of LLM scale โ€” the exact same structure from Transformer Decoder, repeated \(N\) times, where \(N\) is one of the most consequential design choices in an LLM's architecture.

What "Number of Layers" Actually Means

Every time you read that a model has "96 layers" or "32 layers," this refers directly to how many times the same block structure (masked self-attention + feed-forward, each with residual connection and normalization) is stacked sequentially. Each block has its own independently learned weights โ€” deeper stacking means more sequential transformation steps, not more parallel capacity within a single step.

Depth vs Width โ€” Revisited at LLM Scale

Recall the depth/width tradeoff from Neural Network Architecture โ€” it applies directly here. An LLM's total capacity is shaped by both its depth (number of blocks) and its width (\(d_{\text{model}}\), the hidden dimension carried through every block). Different model families make different tradeoffs between the two, and neither dimension alone determines a model's quality.

How Information Flows Through the Stack

Each block's residual connection (see Residual Connections) means the hidden state at block \(l\) is the hidden state at block \(l-1\) plus whatever that block's self-attention and feed-forward sublayers computed โ€” information doesn't get replaced at each block so much as it gets progressively refined and added to, layer after layer, all the way through the stack.

Code โ€” Inspecting the Block Stack Directly

from transformers import AutoModelForCausalLM

model = AutoModelForCausalLM.from_pretrained("gpt2")
print(len(model.transformer.h))   # 12 -- the number of stacked decoder blocks

for i, block in enumerate(model.transformer.h):
    if i < 2:   # just inspect the first couple
        print(f"Block {i}:", block)

Common Mistakes

  • Assuming every block in a stack learns something "higher level" than the previous one, the way stacked convolutional layers progressively capture larger receptive fields โ€” the relationship between depth and what each layer "learns" in a Transformer is less strictly hierarchical, since self-attention at every layer can already relate any two positions directly.
  • Confusing the number of transformer blocks with the number of attention heads within a single block โ€” these are two entirely separate hyperparameters, one controlling sequential depth, the other controlling parallel attention specialization within each block (see Multi-Head Attention).

Interview Relevance

Q: "If two LLMs have the same total parameter count but different depth/width tradeoffs, would you expect them to behave identically?" Not necessarily โ€” depth and width represent different kinds of capacity (sequential transformation steps versus per-step representational richness), and empirical research has found the balance between them affects both training dynamics and downstream task performance, even at matched total parameter counts.

Practice Question

What's the difference between increasing an LLM's number of transformer blocks versus increasing its number of attention heads per block?

Want to go beyond the notes?

Join CodingNow 2.0's Deep Learning course โ€” live mentorship, real projects, and 100% placement support.

Enroll Now โ€” Free Demo Available

Transformer Blocks โ€“ FAQs

Quick answers about learning Transformer Blocks in Deep Learning.

This free note from CodingNow 2.0 explains Transformer Blocks in Deep Learning โ€” concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Deep Learning topic on CodingNow 2.0, including Transformer Blocks, is 100% free with no signup required.
With focused practice, most students grasp Transformer Blocks in 1โ€“3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) โ€” expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now