Skip to content

26. Attention and Positional Encoding

Understand how Attention enables neural networks to dynamically focus on relevant information, why attention became a major breakthrough for sequence modeling, how Query-Key-Value representations work, and why positional encoding is required when sequence order is not inherently represented.


๐ŸŽฏ Learning Objectives

After completing this chapter, you will be able to:

  • Explain why attention was introduced
  • Understand the limitations of fixed-size recurrent representations
  • Explain the intuition behind attention mechanisms
  • Understand Query, Key, and Value representations
  • Explain the attention scoring process
  • Understand scaled dot-product attention
  • Explain attention weights
  • Understand softmax normalization in attention
  • Implement attention conceptually using matrix operations
  • Understand self-attention
  • Distinguish self-attention from cross-attention
  • Understand causal attention
  • Explain multi-head attention conceptually
  • Understand why Transformers need positional information
  • Explain positional encoding
  • Understand sinusoidal positional encoding
  • Understand learned positional embeddings
  • Compare different positional representation approaches
  • Understand the relationship between attention and RNNs
  • Understand how attention leads to the Transformer architecture
  • Implement basic attention using PyTorch
  • Understand attention masks
  • Understand padding masks and causal masks
  • Analyze attention complexity
  • Understand production considerations for attention-based systems

๐Ÿ“– Overview

Recurrent Neural Networks process sequences step by step:

xโ‚ โ†’ RNN โ†’ hโ‚
          โ†“
xโ‚‚ โ†’ RNN โ†’ hโ‚‚
          โ†“
xโ‚ƒ โ†’ RNN โ†’ hโ‚ƒ
          โ†“
xโ‚„ โ†’ RNN โ†’ hโ‚„

LSTM and GRU improve the ability to preserve information across time.

However, recurrent architectures still have an important limitation:

Sequential Processing
+
Long Information Paths
+
Limited Parallelism

Attention introduced a different idea:

Instead of forcing the model to rely only on a recurrent hidden state, allow it to directly look at relevant parts of the input.

This creates a flexible mechanism for selecting information based on the current context.


๐Ÿง  The Core Attention Idea

Suppose we want to understand:

"The animal didn't cross the street because it was too tired."

To understand:

"it"

the model needs to determine which previous words are relevant.

Attention allows the model to assign different importance to different tokens.

Conceptually:

The       โ†’ Low Attention
animal    โ†’ High Attention
didn't    โ†’ Low Attention
cross     โ†’ Medium Attention
the       โ†’ Low Attention
street    โ†’ Medium Attention
because   โ†’ Low Attention
it        โ†’ Query
was       โ†’ Low Attention
too       โ†’ Low Attention
tired     โ†’ High Attention

The model learns these relationships during training.


๐Ÿง  Attention as Information Retrieval

A useful mental model is:

Query
  โ†“
Search Relevant Information
  โ†“
Keys
  โ†“
Retrieve Associated Information
  โ†“
Values

This resembles a learned retrieval process.

Query
   โ†“
"Which information do I need?"
   โ†“
Keys
   โ†“
"Which positions are relevant?"
   โ†“
Values
   โ†“
"Retrieve the relevant content."

๐Ÿง  Query, Key, Value

Attention uses three representations:

Query (Q)
Key   (K)
Value (V)

The basic idea is:

Q
 โ†“
Compare with K
 โ†“
Attention Scores
 โ†“
Softmax
 โ†“
Attention Weights
 โ†“
Weighted V
 โ†“
Attention Output

๐Ÿง  Query

The Query represents:

What information am I looking for?

For example:

Current token
        โ†“
Query
        โ†“
Find relevant context

๐Ÿง  Key

The Key represents:

What information does this position contain or represent?

Keys are compared with Queries to determine relevance.


๐Ÿง  Value

The Value represents:

What information should actually be retrieved if this position is considered relevant?

Therefore:

Query
+
Key
โ†’
Relevance

Relevance
+
Value
โ†’
Retrieved Information

๐Ÿง  Query-Key-Value Flow

flowchart LR

    INPUT["Input Representations"]

    Q["Query Q"]
    K["Key K"]
    V["Value V"]

    SCORE["Q-K Similarity"]

    SOFTMAX["Softmax"]

    WEIGHT["Attention Weights"]

    OUTPUT["Weighted Values"]

    INPUT --> Q
    INPUT --> K
    INPUT --> V

    Q --> SCORE
    K --> SCORE

    SCORE --> SOFTMAX
    SOFTMAX --> WEIGHT

    WEIGHT --> OUTPUT
    V --> OUTPUT

๐Ÿง  Attention Scoring

The first step is to calculate how relevant each Key is to a Query.

A common method is the dot product:

[ score(Q,K)=QK^T ]

A larger score generally means:

Query
and
Key

are more aligned.


๐Ÿง  Why Dot Product?

The dot product measures alignment between vectors.

Conceptually:

Q
 โ†“
[0.8, 0.2, 0.1]

Kโ‚
 โ†“
[0.7, 0.3, 0.2]

Similarity
 โ†“
High

while:

Kโ‚‚
 โ†“
[-0.4, 0.1, 0.8]

Similarity
 โ†“
Lower

The model can therefore compare a Query against multiple Keys.


๐Ÿง  Attention Score Matrix

Suppose there are four tokens:

xโ‚
xโ‚‚
xโ‚ƒ
xโ‚„

Each token can compare its Query against every Key.

This creates:

             Keys
          Kโ‚ Kโ‚‚ Kโ‚ƒ Kโ‚„

Query Qโ‚   โ€ข  โ€ข  โ€ข  โ€ข
Query Qโ‚‚   โ€ข  โ€ข  โ€ข  โ€ข
Query Qโ‚ƒ   โ€ข  โ€ข  โ€ข  โ€ข
Query Qโ‚„   โ€ข  โ€ข  โ€ข  โ€ข

This becomes an attention score matrix.


๐Ÿง  Attention Matrix

flowchart TD

    Q["Queries"]

    MAT["Attention Score Matrix"]

    K["Keys"]

    Q --> MAT
    K --> MAT

    MAT --> WEIGHTS["Normalized Attention Weights"]

    WEIGHTS --> VALUES["Weighted Values"]

    VALUES --> OUTPUT["Attention Output"]

๐Ÿง  Scaled Dot-Product Attention

Raw dot products can become large as the vector dimension increases.

Therefore Transformer-style attention uses scaling:

[ Attention(Q,K,V)=softmax\left(\frac{QK^T}{\sqrt{d_k}}\right)V ]

where:

Q   = Queries
K   = Keys
V   = Values
dโ‚–  = Key dimension

๐Ÿง  Why Divide by โˆšdโ‚–?

Without scaling:

Large Vector Dimension
        โ†“
Large Dot Products
        โ†“
Softmax Saturation
        โ†“
Small Effective Gradients

Scaling helps keep the score distribution in a more manageable range.

The factor is:

[ \sqrt{d_k} ]


๐Ÿง  Softmax in Attention

The score matrix is converted into normalized attention weights using softmax.

For a vector of scores:

[ softmax(z_i)=\frac{e^{z_i}}{\sum_j e^{z_j}} ]

The resulting weights satisfy:

Weight โ‰ฅ 0

and:

[ \sum_i weight_i=1 ]


๐Ÿง  Attention Weight Example

Suppose the model produces:

Raw Scores:

[2.0, 1.0, 0.1, 3.0]

Softmax converts them into something like:

Attention Weights:

[0.24, 0.09, 0.04, 0.63]

The fourth position receives the highest attention.

Therefore:

Valueโ‚„

contributes more strongly to the output.


๐Ÿง  Weighted Sum of Values

The final attention representation is:

Attention Weightโ‚ ร— Valueโ‚
+
Attention Weightโ‚‚ ร— Valueโ‚‚
+
...
+
Attention Weightโ‚™ ร— Valueโ‚™

Conceptually:

          Attention Weights
                 โ”‚
       โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
       โ–ผ         โ–ผ         โ–ผ
      Vโ‚        Vโ‚‚        Vโ‚ƒ
       โ”‚         โ”‚         โ”‚
       โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                 โ–ผ
          Weighted Sum
                 โ”‚
                 โ–ผ
        Attention Output

๐Ÿง  Self-Attention

Self-attention is attention where:

Q
K
V

are derived from the same input sequence.

For:

X = [xโ‚, xโ‚‚, xโ‚ƒ, xโ‚„]

we compute:

Q = XWQ
K = XWK
V = XWV

๐Ÿง  Self-Attention Architecture

flowchart TD

    X["Input Sequence"]

    WQ["WQ"]

    WK["WK"]

    WV["WV"]

    Q["Queries"]

    K["Keys"]

    V["Values"]

    ATTENTION["Scaled Dot-Product Attention"]

    OUTPUT["Contextual Representations"]

    X --> WQ --> Q
    X --> WK --> K
    X --> WV --> V

    Q --> ATTENTION
    K --> ATTENTION
    V --> ATTENTION

    ATTENTION --> OUTPUT

๐Ÿง  Why Self-Attention Is Powerful

In a recurrent network:

xโ‚
 โ†“
xโ‚‚
 โ†“
xโ‚ƒ
 โ†“
xโ‚„

information from xโ‚ must travel through intermediate states to influence xโ‚„.

With self-attention:

xโ‚ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–บ xโ‚„

A token can directly attend to another token.

This creates much shorter information paths.


๐Ÿง  Information Path Length

RNN

xโ‚ โ†’ hโ‚ โ†’ hโ‚‚ โ†’ hโ‚ƒ โ†’ hโ‚„

Self-Attention

xโ‚ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–บ xโ‚„

This is one reason attention is effective at modeling long-range relationships.


๐Ÿง  Self-Attention Example

Consider:

"The animal didn't cross the street because it was tired."

When processing:

"it"

the model can attend strongly to:

"animal"

rather than relying only on a recurrent hidden state.

The attention mechanism learns these relationships from data.


๐Ÿง  Self-Attention Matrix

For four tokens:

             Key
           1    2    3    4

Query 1   0.2  0.5  0.1  0.2
Query 2   0.1  0.7  0.1  0.1
Query 3   0.6  0.1  0.2  0.1
Query 4   0.5  0.1  0.1  0.3

Each row represents:

How one token distributes attention across the sequence.

๐Ÿง  Attention Visualization

A useful conceptual visualization is a heatmap:

             Tokens

        The Animal Cross Street

The      โ–ˆโ–ˆโ–ˆ  โ–ˆโ–ˆโ–ˆ  โ–ˆ    โ–ˆ
Animal   โ–ˆ    โ–ˆโ–ˆโ–ˆ  โ–ˆ    โ–ˆ
Cross    โ–ˆ    โ–ˆ    โ–ˆโ–ˆโ–ˆ  โ–ˆโ–ˆ
Street   โ–ˆ    โ–ˆโ–ˆ   โ–ˆ    โ–ˆโ–ˆโ–ˆ

Darker regions conceptually represent stronger attention.

In real Transformer analysis, attention matrices can be visualized as heatmaps.


๐Ÿง  Cross-Attention

Self-attention uses:

Q, K, V

from the same sequence.

Cross-attention uses:

Queries
from one representation

Keys + Values
from another representation

Conceptually:

Decoder Queries
       โ†“
Encoder Keys + Values
       โ†“
Cross-Attention

๐Ÿง  Cross-Attention Architecture

flowchart LR

    ENCODER["Encoder Representations"]

    DECODER["Decoder Representations"]

    Q["Queries"]

    KV["Keys + Values"]

    ATTENTION["Cross-Attention"]

    OUTPUT["Context-Aware Decoder Representation"]

    DECODER --> Q
    ENCODER --> KV

    Q --> ATTENTION
    KV --> ATTENTION

    ATTENTION --> OUTPUT

๐Ÿง  Self-Attention vs Cross-Attention

Self-Attention Cross-Attention
Q, K, V from same sequence Q and K/V from different representations
Models internal relationships Connects two representations
Common in Transformer encoder Common in encoder-decoder architectures
Used for contextualization Used for information retrieval from another sequence

๐Ÿง  Causal Attention

For autoregressive language modeling, a token must not attend to future tokens.

For example:

Token 1

can attend to:

Token 1

but not:

Token 2
Token 3
Token 4

๐Ÿง  Causal Attention Mask

For four tokens:

       K1 K2 K3 K4

Q1     โœ“  โœ—  โœ—  โœ—
Q2     โœ“  โœ“  โœ—  โœ—
Q3     โœ“  โœ“  โœ“  โœ—
Q4     โœ“  โœ“  โœ“  โœ“

This creates a lower-triangular attention pattern.


๐Ÿง  Causal Mask

flowchart TD

    MASK["Causal Mask"]

    VALID["Past + Current Tokens"]

    BLOCKED["Future Tokens"]

    MASK --> VALID
    MASK --> BLOCKED

Conceptually:

Allowed:

โ–ˆโ–ˆโ–ˆโ–ˆ
โ–ˆโ–ˆโ–ˆ
โ–ˆโ–ˆ
โ–ˆ

depending on matrix orientation.


๐Ÿง  Why Causal Masking Matters

Without causal masking:

Current Token
      โ†“
Could See Future Token
      โ†“
Information Leakage

This would make autoregressive training invalid.

Therefore:

Causal Mask
     โ†“
Prevent Future Information
     โ†“
Valid Next-Token Prediction

๐Ÿง  Padding Mask

Batch sequences often have padding.

Example:

Sequence A:
[The, cat, sleeps, PAD, PAD]

Sequence B:
[The, dog, runs, fast, today]

The model should not attend to:

PAD

positions.

A padding mask prevents padded tokens from influencing attention.


๐Ÿง  Causal Mask vs Padding Mask

Mask Purpose
Causal Mask Prevent future-token access
Padding Mask Ignore padding positions
Combined Mask Enforce both constraints

๐Ÿง  Attention with Masking

The attention computation can be conceptualized as:

QKแต€
 โ†“
Apply Mask
 โ†“
Scaled Scores
 โ†“
Softmax
 โ†“
Attention Weights
 โ†“
Weighted Values

๐Ÿง  Masked Attention

flowchart LR

    SCORES["QKแต€ Scores"]

    MASK["Attention Mask"]

    MASKED["Masked Scores"]

    SOFTMAX["Softmax"]

    WEIGHTS["Attention Weights"]

    VALUES["Values"]

    OUTPUT["Attention Output"]

    SCORES --> MASKED
    MASK --> MASKED

    MASKED --> SOFTMAX
    SOFTMAX --> WEIGHTS

    WEIGHTS --> OUTPUT
    VALUES --> OUTPUT

๐Ÿง  Multi-Head Attention

Instead of using a single attention mechanism, Transformers use multiple attention heads.

Each head can learn different relationships.

Conceptually:

Input
 โ†“
Head 1 โ†’ Relationship A
Head 2 โ†’ Relationship B
Head 3 โ†’ Relationship C
Head 4 โ†’ Relationship D
 โ†“
Concatenate
 โ†“
Linear Projection

๐Ÿง  Multi-Head Attention

flowchart TD

    INPUT["Input"]

    HEAD1["Attention Head 1"]

    HEAD2["Attention Head 2"]

    HEAD3["Attention Head 3"]

    HEAD4["Attention Head 4"]

    CONCAT["Concatenate"]

    PROJ["Output Projection"]

    OUTPUT["Multi-Head Output"]

    INPUT --> HEAD1
    INPUT --> HEAD2
    INPUT --> HEAD3
    INPUT --> HEAD4

    HEAD1 --> CONCAT
    HEAD2 --> CONCAT
    HEAD3 --> CONCAT
    HEAD4 --> CONCAT

    CONCAT --> PROJ
    PROJ --> OUTPUT

๐Ÿง  Why Multiple Heads?

Different attention heads can specialize in different relationships.

For example:

Head 1
โ†“
Syntactic Relationship

Head 2
โ†“
Semantic Relationship

Head 3
โ†“
Long-Range Dependency

Head 4
โ†“
Local Context

These are conceptual interpretations rather than guaranteed fixed roles.


๐Ÿง  Multi-Head Attention Formula

A multi-head attention mechanism can be represented as:

[ MultiHead(Q,K,V)=Concat(head_1,\ldots,head_h)W^O ]

where each head is:

[ head_i=Attention(QW_iQ,KW_iK,VW_i^V) ]


๐Ÿง  Attention Head Dimensions

Suppose:

Model Dimension = 512
Number of Heads = 8

A common configuration uses:

[ d_{head}=\frac{512}{8}=64 ]

Each head operates in a lower-dimensional representation.


๐Ÿง  Why Positional Information Is Needed

Attention itself does not inherently encode token order.

Consider:

"The dog chased the cat"

and:

"The cat chased the dog"

The tokens are the same, but their order changes the meaning.

A pure set of token representations does not inherently distinguish these sequences.

Therefore Transformer architectures need:

Positional Information


๐Ÿง  Sequence Order

Token Embeddings
      +
Positional Information
      โ†“
Input Representation

๐Ÿง  Positional Encoding

A positional encoding provides information about where a token occurs in the sequence.

Conceptually:

Token
 +
Position
 โ†“
Position-Aware Representation

For example:

The      + Position 0
dog      + Position 1
chased   + Position 2
the      + Position 3
cat      + Position 4

๐Ÿง  Positional Encoding Architecture

flowchart LR

    TOKENS["Token IDs"]

    EMBED["Token Embeddings"]

    POSITION["Positional Representation"]

    ADD["Element-wise Addition"]

    INPUT["Transformer Input"]

    TOKENS --> EMBED
    EMBED --> ADD

    POSITION --> ADD

    ADD --> INPUT

๐Ÿง  Sinusoidal Positional Encoding

The original Transformer architecture introduced deterministic sinusoidal positional encodings.

For even dimensions:

[ PE(pos,2i)=\sin\left(\frac{pos}{10000^{2i/d_{model}}}\right) ]

For odd dimensions:

[ PE(pos,2i+1)=\cos\left(\frac{pos}{10000^{2i/d_{model}}}\right) ]

where:

pos       = Position in sequence
i         = Dimension index
d_model   = Model embedding dimension

๐Ÿง  Why Sine and Cosine?

Sinusoidal functions provide smooth and structured positional representations.

Different dimensions use different frequencies.

Conceptually:

Dimension 1
~~~~~~~ ~~~~~~~

Dimension 2
~ ~ ~ ~ ~ ~ ~ ~

Dimension 3
^^^^^^^^^^^^^^^

Dimension 4
_/\_/\_/\_/\_

This creates a unique positional pattern across dimensions.


๐Ÿง  Positional Encoding Intuition

Position 0
 โ†“
[sinโ‚, cosโ‚, sinโ‚‚, cosโ‚‚, ...]

Position 1
 โ†“
[sinโ‚', cosโ‚', sinโ‚‚', cosโ‚‚', ...]

Position 2
 โ†“
[sinโ‚'', cosโ‚'', sinโ‚‚'', cosโ‚‚'', ...]

Each position receives a distinct vector.


๐Ÿง  Learned Positional Embeddings

Instead of calculating positions using fixed functions, the model can learn positional representations.

Conceptually:

Position 0 โ†’ Learnable Vector
Position 1 โ†’ Learnable Vector
Position 2 โ†’ Learnable Vector
...

These vectors are optimized during training.


๐Ÿง  Learned vs Sinusoidal Position

Sinusoidal Learned
Fixed mathematical function Learned parameters
No additional learned position parameters Requires trainable position embeddings
Used in original Transformer Common in many Transformer architectures
Structured across frequencies Learned from data

๐Ÿง  Relative Positional Information

Absolute position answers:

Where is this token?

Relative position answers:

How far is this token from another token?

For example:

Token A
      โ†“
3 positions away
      โ†“
Token B

Relative position can be especially useful when the relationship between tokens matters more than their absolute location.

Modern Transformer architectures use several approaches to encode positional information.


๐Ÿง  Absolute vs Relative Position

Absolute Position

Token A โ†’ Position 5
Token B โ†’ Position 8

versus:

Relative Position

Token B is
3 positions after
Token A

๐Ÿง  Position Representation Evolution

Sinusoidal Position
        โ†“
Learned Position Embeddings
        โ†“
Relative Position Methods
        โ†“
Rotary / Other Position Mechanisms

The exact positional strategy depends on the Transformer architecture.


๐Ÿง  Attention + Position

The overall idea becomes:

Token Embedding
       +
Positional Information
       โ†“
Transformer Input
       โ†“
Self-Attention
       โ†“
Contextual Representation

๐Ÿง  Attention vs Recurrence

RNN / LSTM Attention
Processes sequentially Processes relationships directly
Hidden state carries context Attention weights retrieve context
Long information path Short direct paths
Limited parallelism Highly parallelizable
State-based memory Dynamic contextual lookup

๐Ÿง  RNN Information Flow

xโ‚
 โ†“
hโ‚
 โ†“
hโ‚‚
 โ†“
hโ‚ƒ
 โ†“
hโ‚„

Information must propagate through intermediate states.


๐Ÿง  Attention Information Flow

xโ‚ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–บ xโ‚„
xโ‚‚ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–บ xโ‚„
xโ‚ƒ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–บ xโ‚„
xโ‚„ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–บ xโ‚„

Each token can directly interact with the others.


๐Ÿง  Attention Complexity

For a sequence of length:

n

the attention score matrix has:

[ n\times n ]

entries.

Therefore the core attention computation has approximately quadratic complexity with respect to sequence length:

[ O(n^2d) ]

where:

n = Sequence Length
d = Representation Dimension

โš  Attention Complexity Problem

As sequence length increases:

n
 โ†“
2n

the pairwise relationships grow approximately as:

nยฒ
 โ†“
4nยฒ

Therefore:

Long Context
     โ†“
Large Attention Matrix
     โ†“
Higher Memory
     โ†“
Higher Compute

This is one of the major challenges in scaling standard attention.


๐Ÿง  Attention Complexity Visualization

Sequence Length

n       โ†’ nยฒ relationships
2n      โ†’ 4nยฒ relationships
4n      โ†’ 16nยฒ relationships
8n      โ†’ 64nยฒ relationships

This explains why long-context attention requires careful engineering.


๐Ÿง  PyTorch Scaled Dot-Product Attention

Modern PyTorch provides:

torch.nn.functional.scaled_dot_product_attention

A conceptual implementation is:

import torch
import torch.nn.functional as F


output = F.scaled_dot_product_attention(
    query,
    key,
    value
)

The implementation can use optimized kernels depending on the hardware and configuration.


๐Ÿงช Basic Attention Implementation

A simplified implementation can be written as:

import math
import torch


def scaled_dot_product_attention(
    query,
    key,
    value,
    mask=None
):

    scores = (
        query @ key.transpose(-2, -1)
    )

    scores = (
        scores /
        math.sqrt(
            key.size(-1)
        )
    )

    if mask is not None:

        scores = scores.masked_fill(
            mask == 0,
            float("-inf")
        )

    weights = torch.softmax(
        scores,
        dim=-1
    )

    output = (
        weights @ value
    )

    return output, weights

This implementation demonstrates the mathematical concept but is not necessarily the most efficient production implementation.


๐Ÿง  Attention Implementation Flow

flowchart LR

    Q["Query"]

    K["Key"]

    V["Value"]

    DOT["Q ร— Kแต€"]

    SCALE["Scale by โˆšdโ‚–"]

    MASK["Optional Mask"]

    SOFTMAX["Softmax"]

    WEIGHTS["Attention Weights"]

    MATMUL["Weights ร— V"]

    OUTPUT["Output"]

    Q --> DOT
    K --> DOT

    DOT --> SCALE
    SCALE --> MASK
    MASK --> SOFTMAX
    SOFTMAX --> WEIGHTS

    WEIGHTS --> MATMUL
    V --> MATMUL

    MATMUL --> OUTPUT

๐Ÿงช Self-Attention Module

A simple self-attention module can be constructed using linear projections:

class SelfAttention(
    torch.nn.Module
):

    def __init__(
        self,
        d_model
    ):

        super().__init__()

        self.query = torch.nn.Linear(
            d_model,
            d_model
        )

        self.key = torch.nn.Linear(
            d_model,
            d_model
        )

        self.value = torch.nn.Linear(
            d_model,
            d_model
        )

    def forward(
        self,
        x
    ):

        q = self.query(x)
        k = self.key(x)
        v = self.value(x)

        output, weights = (
            scaled_dot_product_attention(
                q,
                k,
                v
            )
        )

        return output, weights

๐Ÿง  Attention Tensor Shapes

Suppose:

Batch = B
Heads = H
Sequence Length = T
Head Dimension = D

Then:

Q
[B, H, T, D]

K
[B, H, T, D]

V
[B, H, T, D]

The attention scores become:

[B, H, T, T]

This is why attention memory grows rapidly with sequence length.


๐Ÿง  Multi-Head Attention Shapes

Conceptually:

Input
[B, T, Dmodel]

      โ†“

Q, K, V
[B, T, Dmodel]

      โ†“

Split into Heads

[B, H, T, Dhead]

      โ†“

Attention

[B, H, T, T]

      โ†“

Concatenate Heads

[B, T, Dmodel]

๐Ÿง  Attention Mask Example

A causal mask can be created using a lower-triangular matrix.

seq_len = 5

mask = torch.tril(
    torch.ones(
        seq_len,
        seq_len
    )
)

The result conceptually represents:

1 0 0 0 0
1 1 0 0 0
1 1 1 0 0
1 1 1 1 0
1 1 1 1 1

where:

1 = Allowed
0 = Blocked

๐Ÿง  Attention Masking with -inf

Before softmax, blocked positions can be assigned:

-โˆž

Then:

softmax(-โˆž) โ‰ˆ 0

This effectively removes those positions from attention.


๐Ÿง  Attention and Information Retrieval

Attention can be understood as a differentiable retrieval system:

Query
 โ†“
Similarity Search
 โ†“
Relevant Keys
 โ†“
Weights
 โ†“
Retrieve Values

This conceptual connection becomes particularly useful when moving into:

Transformers
RAG
Cross-Attention
Multimodal Models
LLMs

๐Ÿง  Attention in Encoder-Decoder Systems

Attention can connect an encoder and decoder:

Input Sequence
      โ†“
Encoder
      โ†“
Encoder States
      โ†“
Attention
      โ†‘
Decoder Query
      โ†“
Decoder Output

The decoder can dynamically select relevant encoder information.


๐Ÿง  Attention Before Transformers

Attention originally appeared as a mechanism used with recurrent encoder-decoder systems.

The progression was:

Encoder RNN
     โ†“
Fixed Context Vector
     โ†“
Decoder RNN

then:

Encoder RNN
     โ†“
All Hidden States
     โ†“
Attention
     โ†“
Decoder RNN

This significantly improved sequence-to-sequence modeling.


๐Ÿง  Transformer Breakthrough

The Transformer architecture took the attention mechanism much further.

Instead of relying on recurrence as the primary sequence-processing mechanism:

Transformer
=
Attention
+
Feed-Forward Networks
+
Positional Information
+
Residual Connections
+
Normalization

This is the foundation of modern Transformer-based AI systems.


๐Ÿง  From Attention to Transformer

flowchart LR

    RNN["RNN"]

    LSTM["LSTM / GRU"]

    ATTENTION["Attention"]

    SELF["Self-Attention"]

    TRANSFORMER["Transformer"]

    LLM["Large Language Models"]

    RNN --> LSTM
    LSTM --> ATTENTION
    ATTENTION --> SELF
    SELF --> TRANSFORMER
    TRANSFORMER --> LLM

๐Ÿข Enterprise Perspective

Attention changed sequence modeling because it transformed context handling from:

Sequential Memory

into:

Dynamic Context Selection

This idea became foundational for:

Machine Translation
Search
Question Answering
Large Language Models
Vision Transformers
Multimodal AI
Retrieval-Augmented Generation
Agentic AI

๐Ÿข Attention in Enterprise AI

A simplified enterprise AI pipeline can look like:

User Request
      โ†“
Tokenization
      โ†“
Embeddings
      โ†“
Transformer
      โ†“
Self-Attention
      โ†“
Contextual Representation
      โ†“
Task Head / Generation
      โ†“
Business Application

๐Ÿข Attention + RAG

In Retrieval-Augmented Generation:

User Query
      โ†“
Embedding
      โ†“
Retriever
      โ†“
Relevant Documents
      โ†“
Context
      โ†“
LLM
      โ†“
Attention
      โ†“
Generated Response

Attention allows the model to dynamically combine information from the provided context.

However:

Attention itself is not a vector database or retrieval system.

A production RAG system still requires an external retrieval mechanism.


๐Ÿข Attention and Production Cost

Standard attention has approximately:

[ O(n^2d) ]

Therefore production systems need to consider:

Context Length
+
Batch Size
+
Number of Heads
+
Head Dimension
+
GPU Memory
+
Latency

Longer context is not free.


๐Ÿข Production Attention Optimization

Common optimization directions include:

Efficient Attention Kernels
+
Flash Attention
+
KV Caching
+
Quantization
+
Context Management
+
Batching
+
Sequence Packing

These techniques become increasingly important when serving large Transformer models.


๐Ÿง  KV Cache Preview

During autoregressive generation, previously computed:

Keys
+
Values

can be cached.

Instead of recomputing them for every generated token:

Previous K/V
      โ†“
Cache
      โ†“
Reuse

This significantly improves generation efficiency.

KV caching will be explored in greater detail in Transformer and LLM-focused chapters.


๐Ÿง  Positional Encoding vs KV Cache

These solve completely different problems.

Positional Encoding
        โ†“
Represent Sequence Order

while:

KV Cache
        โ†“
Avoid Recomputing Previous Attention States

Do not confuse them.


๐Ÿง  Important Conceptual Distinction

Attention answers:

Which information should this representation use?

Positional encoding answers:

Where does this token occur in the sequence?

Together:

Token Meaning
+
Token Position
      โ†“
Contextual Representation

๐Ÿงช Practical Exercise 1 โ€” Implement Attention

Implement:

scaled_dot_product_attention()

from scratch.

Verify:

QKแต€
โ†“
Scaling
โ†“
Softmax
โ†“
Weighted V

๐Ÿงช Practical Exercise 2 โ€” Visualize Attention

Create a small sentence and visualize:

Attention Matrix

using a heatmap.

Analyze which tokens receive the highest attention.


๐Ÿงช Practical Exercise 3 โ€” Causal Attention

Implement a causal mask.

Verify:

Token 1 โ†’ Token 1 only

Token 2 โ†’ Token 1, Token 2

Token 3 โ†’ Token 1, Token 2, Token 3

๐Ÿงช Practical Exercise 4 โ€” Padding Mask

Create variable-length sequences.

Add padding.

Implement a padding mask and verify that:

PAD

positions receive zero attention probability.


๐Ÿงช Practical Exercise 5 โ€” Self-Attention

Build a self-attention layer using:

nn.Linear

for:

Q
K
V

๐Ÿงช Practical Exercise 6 โ€” Multi-Head Attention

Implement a simplified multi-head attention layer.

Use:

d_model = 128
num_heads = 4

Verify:

[ d_{head}=\frac{128}{4}=32 ]


๐Ÿงช Practical Exercise 7 โ€” Positional Encoding

Implement sinusoidal positional encoding.

Generate:

Sequence Length = 100
Embedding Dimension = 128

Visualize the resulting positional matrix.


๐Ÿงช Practical Exercise 8 โ€” Learned Positional Embeddings

Implement:

nn.Embedding(
    max_sequence_length,
    d_model
)

Compare learned positional embeddings with sinusoidal encoding.


๐Ÿงช Practical Exercise 9 โ€” Attention vs RNN

Build:

RNN

and:

Self-Attention

for the same sequence classification problem.

Compare:

Accuracy
Training Time
Memory
Long-Range Dependency Performance

๐Ÿงช Practical Exercise 10 โ€” Causal Language Modeling

Build a small autoregressive model using causal self-attention.

Verify that:

Future Tokens

cannot influence current predictions.


๐Ÿงช Practical Exercise 11 โ€” Attention Complexity

Benchmark attention with:

Sequence Length = 128
Sequence Length = 256
Sequence Length = 512
Sequence Length = 1024

Measure:

GPU Memory
Execution Time
Attention Matrix Size

๐Ÿงช Practical Exercise 12 โ€” Cross-Attention

Build a simple:

Encoder
+
Decoder
+
Cross-Attention

pipeline.

Verify that:

Decoder Query

attends to:

Encoder Keys + Values

๐Ÿง  Interview Questions

Beginner

1. What is attention?

Attention is a mechanism that dynamically weights different parts of an input representation based on their relevance to the current query.

2. What are Query, Key, and Value?

Query โ†’ What information am I looking for?
Key   โ†’ What does each position represent?
Value โ†’ What information should be retrieved?

3. What is self-attention?

Self-attention is attention where Queries, Keys, and Values are derived from the same sequence.

4. Why is attention useful?

It allows a representation to directly access relevant information from other positions instead of relying only on sequential hidden-state propagation.

5. Why do Transformers need positional information?

Self-attention alone does not inherently encode sequence order.


Intermediate

6. What is scaled dot-product attention?

[ Attention(Q,K,V)=softmax\left(\frac{QK^T}{\sqrt{d_k}}\right)V ]

7. Why divide by โˆšdโ‚–?

To prevent dot-product scores from growing excessively with increasing dimensionality and causing problematic softmax behavior.

8. What does softmax do in attention?

It converts attention scores into normalized weights.

9. What is cross-attention?

Cross-attention uses Queries from one representation and Keys/Values from another representation.

10. What is causal attention?

Attention constrained so a token cannot access future tokens.

11. What is a padding mask?

A mask that prevents attention from being assigned to padding positions.

12. What is multi-head attention?

A mechanism that performs several attention operations in parallel using different learned projections and combines their outputs.


Advanced

13. Why is self-attention more parallelizable than RNNs?

Self-attention can compute interactions across sequence positions using matrix operations without requiring each time step to wait for the previous hidden state.

14. What is the complexity of standard self-attention?

The core attention computation scales approximately as:

[ O(n^2d) ]

with respect to sequence length n and representation dimension d.

15. Why does long context increase attention cost?

Because every token can interact with every other token, creating an approximately n ร— n attention matrix.

16. What is positional encoding?

A mechanism for injecting information about token positions into Transformer representations.

17. What is sinusoidal positional encoding?

A fixed positional representation based on sine and cosine functions with different frequencies.

18. What are learned positional embeddings?

Trainable vectors associated with different positions in a sequence.

19. What is the difference between absolute and relative position?

Absolute position identifies where a token occurs, while relative position represents the distance or relationship between tokens.

20. Why is attention important for Transformers?

It provides the core mechanism for modeling relationships between tokens while enabling highly parallelizable sequence processing during training.

21. What is the relationship between attention and RAG?

Attention helps an LLM use the provided context, while the retrieval component of RAG independently finds relevant documents.

22. Is attention itself retrieval?

Not in the production RAG sense. Attention is a neural mechanism for weighting representations; a retrieval system typically searches an external corpus or index.


๐Ÿข Enterprise Perspective

Attention is one of the most important architectural ideas in modern AI.

The progression is:

RNN
 โ†“
LSTM / GRU
 โ†“
Attention
 โ†“
Self-Attention
 โ†“
Transformer
 โ†“
LLM
 โ†“
Generative AI

The key architectural shift was from:

"Carry information forward through time"

to:

"Directly retrieve relevant context."

This shift enabled highly scalable Transformer architectures.


๐Ÿข Production Attention Architecture

A production Transformer system can be conceptualized as:

Input
 โ†“
Tokenization
 โ†“
Token Embeddings
 +
Positional Representation
 โ†“
Self-Attention
 โ†“
Feed-Forward Network
 โ†“
Residual + Normalization
 โ†“
Repeated Transformer Blocks
 โ†“
Task Head / LM Head
 โ†“
Prediction

๐Ÿข Production Attention Considerations

Before deploying attention-based systems, evaluate:

Context Length
Attention Complexity
GPU Memory
Latency
Throughput
Batch Size
Model Size
Number of Heads
KV Cache
Quantization
Inference Kernel

๐Ÿข Production Insight

Production Insight

Attention is not simply a more powerful version of an RNN. It represents a different way of modeling information flow.

RNNs primarily propagate information through sequential hidden states:

hโ‚ โ†’ hโ‚‚ โ†’ hโ‚ƒ โ†’ hโ‚„

Attention creates direct contextual interactions:

xโ‚ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–บ xโ‚„
xโ‚‚ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–บ xโ‚„
xโ‚ƒ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–บ xโ‚„

This enables strong long-range modeling and highly parallelizable training.

But attention introduces its own engineering challenge:

Sequence Length
      โ†“
O(nยฒ) Attention
      โ†“
Memory + Compute

Therefore production Transformer systems require careful context management, efficient attention implementations, caching, batching, and hardware-aware optimization.


๐Ÿ“Œ Key Takeaways

  • Attention dynamically selects relevant information from a set of representations.
  • Attention uses Query, Key, and Value representations.
  • Queries represent what information is needed.
  • Keys represent information that can be matched against Queries.
  • Values contain the information that is actually retrieved.
  • Scaled dot-product attention computes normalized weighted combinations of Values.
  • Softmax converts attention scores into normalized weights.
  • Self-attention derives Q, K, and V from the same sequence.
  • Cross-attention connects two different representations.
  • Causal attention prevents access to future tokens.
  • Padding masks prevent padded positions from contributing to attention.
  • Multi-head attention allows multiple attention mechanisms to operate in parallel.
  • Attention provides shorter information paths than recurrent sequence processing.
  • Attention is highly parallelizable during training.
  • Standard attention has approximately quadratic complexity with sequence length.
  • Positional information is necessary because attention itself does not inherently represent token order.
  • Sinusoidal positional encoding uses deterministic sine and cosine functions.
  • Learned positional embeddings use trainable position representations.
  • Relative positional methods model relationships between token positions.
  • Attention was an important bridge between recurrent sequence models and Transformers.
  • Attention is fundamental to modern Transformer architectures.
  • Attention should not be confused with external retrieval in systems such as RAG.
  • Production attention systems must account for context length, memory, latency, throughput, and computational cost.

๐Ÿ“š Further Reading

Continue with:

The next chapter brings these concepts together into the Transformer Architecture, showing how self-attention, multi-head attention, positional information, feed-forward networks, residual connections, and normalization form the architecture behind modern LLMs and many other foundation models.


โžก๏ธ Next Chapter

27. Transformer Architecture


Enterprise AI Engineering Handbook
Building Production-Grade Enterprise AI Systems โ€” One Chapter at a Time.