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.
Glossary
Attention Score Matrix

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.
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.
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.
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.
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.
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.
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.
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.
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.
Enabling Efficiency, Speed & Accuracy
Intelligent Analysis, Decision & Execution
We build AI systems for teams that need search across company data, workflow automation across tools, or AI features inside products and internal software.
Talk to Us
Search across company data
Give teams answers from docs, tickets, runbooks, and product data with sources and permissions.
Useful when people spend too long searching or get different answers from different systems.

Automate internal workflows
Use AI to route work, draft outputs, trigger actions, and keep approvals and logs in place.
Useful when repetitive work moves across multiple tools and teams.

Add AI to products and internal tools
Build assistants, guided actions, or decision support into the software your team or customers already use.
Useful when AI needs to be part of the product, not a separate tool.
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.
Related Terms
The attention score matrix is the computational heart of the Transformer. These related concepts define how the matrix is calculated, constrained, and optimized.
Scaled Dot-Product Attention
The exact mathematical operation that generates the raw attention score matrix. It computes the dot product of the Query matrix with the transposed Key matrix, then scales the result by 1/√dₖ (where dₖ is the key dimension) to prevent the softmax from entering regions with extremely small gradients. The formula is: Attention(Q,K,V) = softmax(QKᵀ/√dₖ)V. Without this scaling factor, high-dimensional dot products grow large in magnitude, pushing the softmax function into saturation and halting learning.
Causal Attention Mask
A constraint applied to the attention score matrix in decoder-only and autoregressive models. Before the softmax step, a mask sets all scores corresponding to future tokens to -∞, ensuring that after softmax, those positions receive a weight of zero. This enforces the autoregressive property: a token at position i can only attend to positions ≤ i. The mask is typically an upper-triangular matrix of negative infinity values, preserving the causal ordering of sequence generation.
Quadratic Complexity Bottleneck
The fundamental computational limitation of the attention score matrix. Since the matrix dimensions are N × N (where N is sequence length), both memory and compute scale as O(N²). For a 32k-token context, this yields over one billion pairwise scores to compute and store. This bottleneck drives research into sparse attention, linear attention, and FlashAttention to make long-context processing tractable without exhausting GPU high-bandwidth memory.
Attention Temperature
A hyperparameter that modulates the sharpness of the attention distribution. Applied as a divisor to the logits before softmax: softmax(scores / τ). A low temperature (τ < 1) sharpens the distribution, making the model focus heavily on the single most relevant token. A high temperature (τ > 1) flattens the distribution, giving more weight to secondary tokens. This is distinct from the scaling factor √dₖ, which is fixed by dimensionality; temperature is a tunable control for exploration vs. exploitation.
FlashAttention
An IO-aware exact attention algorithm that computes the attention score matrix in tiled blocks without ever materializing the full N × N matrix in GPU high-bandwidth memory (HBM). By fusing the softmax reduction across tiles in SRAM, it achieves significant speedups and memory savings while remaining mathematically identical to standard attention. This makes it possible to train models with long context windows without resorting to sparse approximations.
Sparse Attention Patterns
A family of techniques that reduce the O(N²) cost by computing only a subset of the attention score matrix. Common patterns include:
- Sliding Window: Each token attends only to a fixed window of ±W neighboring tokens
- Dilated Sliding Window: Gaps are introduced in the window to increase receptive field
- Global Attention: A few pre-selected tokens attend to the entire sequence
- Block-Sparse: The matrix is divided into blocks, with only certain blocks computed These patterns reduce complexity to O(N·W) or O(N·k) where k ≪ N.

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.
Partnered with leading AI, data, and software stack.
How We Work
Custom AI workflows for your Business
One-fit-all AI don't work for modern businesses. At Inferensys, we aim to understand your business & custom requirements; which we use to define most efficient agentic workflows, the data, and the tools for your business.
01
Review the use case
We understand the task, the users, and where AI can actually help.
Read more02
Pick the right approach
We define what needs search, automation, or product integration.
Read more03
Build the first useful version
We implement the part that proves the value first.
Read more04
Improve from there
We add the checks and visibility needed to keep it useful.
Read moreThe first call is a practical review of your use case and the right next step.
Talk to Us