Inferensys

Glossary

Attention Score Matrix

An N x N matrix where each entry represents the raw compatibility score between a Query and a Key token, computed before softmax normalization to derive the final attention weights.
Stylish WeWork-like workspace with hot desks and document wall, professional searching through enterprise knowledge base on a mounted ultrawide display, warm industrial pendants overhead.
RAW COMPATIBILITY TENSOR

What is Attention Score Matrix?

The Attention Score Matrix is the raw, unnormalized N x N tensor of compatibility scores computed before the softmax operation, representing the pre-softmax dot-product interactions between every Query and Key token in a sequence.

The Attention Score Matrix is the direct output of the scaled dot-product operation between the Query (Q) and Key (K) vectors, computed as QK^T / √d_k. Each entry S_{ij} represents the raw compatibility score between the i-th Query token and the j-th Key token, quantifying how relevant token j is to token i before any probabilistic normalization is applied.

This matrix is the critical intermediate tensor that feeds into the softmax function to produce the final Attention Weights. The scaling factor √d_k prevents the dot products from growing too large in magnitude, which would push the softmax function into regions of extremely small gradients and hinder training stability. The matrix inherently exhibits quadratic complexity, scaling with O(N²) memory and computation relative to sequence length N.

INTERNAL MECHANICS

Key Properties of the Attention Score Matrix

Before the softmax normalization produces final attention weights, the raw attention scores form an N x N matrix that encodes the unnormalized compatibility between every pair of tokens in a sequence. Understanding its structural properties is essential for debugging attention patterns and optimizing Transformer inference.

01

Quadratic Complexity Bottleneck

The attention score matrix has dimensions N × N, where N is the sequence length. Computing and storing this matrix requires O(N²) memory and time complexity. For a 32k token context window, this results in over 1 billion pairwise scores. This quadratic scaling is the primary computational bottleneck that techniques like FlashAttention, sparse attention, and KV caching aim to mitigate.

O(N²)
Memory Complexity
1B+
Scores at 32k Tokens
02

Asymmetry Between Query and Key Projections

Each entry S_{ij} represents the dot product between the Query vector of token i and the Key vector of token j. Because Queries and Keys use separate learned projection matrices (W_Q and W_K), the matrix is not necessarily symmetric. Token A attending to Token B can yield a different raw score than Token B attending to Token A, reflecting directional semantic relationships.

03

Causal Masking for Autoregressive Decoding

In decoder-only architectures, a lower triangular mask is applied to the score matrix before softmax. This sets all entries where j > i to negative infinity, preventing token i from attending to future tokens. The resulting matrix is upper-triangular with -∞ values, enforcing the autoregressive property essential for text generation in models like GPT.

04

Scaling Factor Prevents Gradient Vanishing

Raw dot-product scores grow in magnitude as the dimensionality d_k of Query/Key vectors increases. Large score values push the softmax into regions of extremely small gradients. Dividing all scores by √d_k keeps the variance of the dot products stable at approximately 1, ensuring healthy gradient flow during backpropagation. Without this scaling, training deep Transformers becomes unstable.

05

Sparsity Patterns Reduce Computation

Not all N² entries are equally important. Sliding window attention computes only a banded diagonal of the matrix, where each token attends to a fixed neighborhood of ±W tokens. Dilated attention uses strided patterns. These sparsity masks transform the O(N²) complexity to O(N·W), enabling processing of sequences with hundreds of thousands of tokens without quadratic memory growth.

06

Attention Head Specialization

In multi-head attention, each head maintains its own independent score matrix. Empirical analysis reveals that different heads specialize in distinct linguistic patterns: some heads focus on syntactic dependencies, others on coreference resolution, and some become no-op heads that contribute minimally. Head pruning techniques exploit this redundancy by removing heads whose score matrices show consistently uniform or low-variance patterns.

ATTENTION MECHANICS

Frequently Asked Questions

Clear, technical answers to the most common questions about the attention score matrix, its computation, and its role in Transformer architectures.

An attention score matrix is an N x N matrix where each entry represents the raw compatibility score between a Query token and a Key token, computed before softmax normalization. It is calculated by taking the dot product of the Query matrix (Q) and the transposed Key matrix (K^T). In scaled dot-product attention, this product is divided by √d_k (the square root of the key dimension) to prevent the dot products from growing too large in magnitude, which would push the softmax function into regions with extremely small gradients. The resulting matrix encodes how much each token should "attend to" every other token in the sequence, forming the foundation for all subsequent attention weight calculations.

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.