T5 Architecture › Working with T5

Michael BrenndoerferAugust 14, 20255 min read
Language AI HandbookAll chapters
Part 25 of 67Encoder-Decoder Models
Chapter 177T5 Architecture › Working with T5
Reading section 8 of 12 · Working with T5 · 5 minSection 8 of 12 · 5 min

Book contents

Section 8 of 12 · 5 min read

T5 Architecture › Working with T5One section · Sections in this chapter8 / 12 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

Let's use T5 for various tasks to see the text-to-text framework in action. We'll use T5-small for these examples to keep computational requirements modest.

Translation

In[14]:
Code
import torch

# Translation example
input_text = "translate English to German: The weather is beautiful today."

# Tokenize
inputs = tokenizer(input_text, return_tensors="pt", padding=True)

# Generate
with torch.no_grad():
    outputs = model.generate(
        inputs.input_ids, max_length=50, num_beams=4, early_stopping=True
    )

# Decode
translation = tokenizer.decode(outputs[0], skip_special_tokens=True)
print(f"Input: {input_text}")
print(f"Output: {translation}")
Out[14]:
Console
Input: translate English to German: The weather is beautiful today.
Output: Das Wetter ist heute schön.

Summarization

In[15]:
Code
# Summarization example
article = """
The Amazon rainforest produces about 20% of the world's oxygen.
It spans across nine countries in South America and contains
10% of all species on Earth. Deforestation threatens this vital
ecosystem, with an area the size of a football field being cleared
every minute. Conservation efforts are necessary to preserve
biodiversity and combat climate change.
"""

input_text = f"summarize: {article}"
inputs = tokenizer(
    input_text, return_tensors="pt", padding=True, truncation=True
)

with torch.no_grad():
    outputs = model.generate(
        inputs.input_ids, max_length=50, num_beams=4, early_stopping=True
    )

summary = tokenizer.decode(outputs[0], skip_special_tokens=True)
print(f"Summary: {summary}")
Out[15]:
Console
Summary: the amazon rainforest produces 20% of the world's oxygen. it spans across nine countries in south America and contains 10% of all species on earth.

Examining Internal Representations

Let's look at how T5 processes input through its encoder:

In[16]:
Code
# Get encoder hidden states
text = "The model learns to understand language through pretraining."
inputs = tokenizer(text, return_tensors="pt")

with torch.no_grad():
    encoder_outputs = model.encoder(
        input_ids=inputs.input_ids, output_hidden_states=True
    )

# Shape of hidden states from each layer
print("Encoder hidden state shapes:")
for i, hidden in enumerate(encoder_outputs.hidden_states):
    print(f"  Layer {i}: {hidden.shape}")
Out[16]:
Console
Encoder hidden state shapes:
  Layer 0: torch.Size([1, 12, 512])
  Layer 1: torch.Size([1, 12, 512])
  Layer 2: torch.Size([1, 12, 512])
  Layer 3: torch.Size([1, 12, 512])
  Layer 4: torch.Size([1, 12, 512])
  Layer 5: torch.Size([1, 12, 512])
  Layer 6: torch.Size([1, 12, 512])

Each layer produces a tensor of shape (batch_size, sequence_length, hidden_dim). The sequence has 11 tokens, and each token gets a 512-dimensional representation. Layer 0 is the embedding layer, and layers 1-6 are the transformer blocks.

The progression across layers shows how the model builds meaning. Early layers capture surface-level features: token identity, simple co-occurrence patterns, and basic syntax. Middle layers develop richer syntactic structure, resolving phrase boundaries and grammatical relationships. Late layers encode semantic content most strongly, bringing together context from across the sequence into representations that encode meaning rather than form. This layer-by-layer refinement is not unique to T5 but is a general property of deep transformer stacks; it makes representations from different layers useful for different downstream tasks.

Attention Pattern Visualization

Let's visualize how attention patterns differ between encoder and decoder:

Out[17]:
Visualization
Heatmap of T5 encoder attention weights showing full bidirectional pattern.
Encoder self-attention weights for the first layer and head, showing the bidirectional pattern where every token can attend to every other token. Bright cells indicate high attention weight between those positions.
Heatmap of T5 decoder attention weights showing lower triangular causal pattern.
Decoder self-attention weights for the first layer and head, showing the causal lower-triangular pattern enforced by masking. Each token can only attend to itself and tokens that came before it, preventing information leakage from future positions during generation.

The encoder attention shows bidirectional patterns where tokens can attend freely to any position. The decoder attention shows the characteristic lower-triangular pattern from causal masking. Each token can only attend to itself and previous tokens, preventing information leakage from future positions during generation.

Out[18]:
Visualization
Heatmap of T5 cross-attention weights showing decoder output tokens attending to encoder input tokens, with strong focus on semantically aligned words.
Cross-attention from the decoder to the encoder shows how each output token attends to the input. When generating 'Hallo Welt', the decoder focuses on the relevant English words 'Hello world' while largely ignoring the task prefix 'translate English to German:'. This selective focus is what allows the model to perform accurate translation without being distracted by the instruction tokens.