Inferensys

Glossary

Unembedding Matrix

The final linear layer in a language model that maps the high-dimensional residual stream state to a probability distribution over the vocabulary of tokens.
ML engineer managing model training cluster on laptop, GPU utilization visible, technical deep learning setup.
DECODING THE VOCABULARY PROJECTION

What is Unembedding Matrix?

The unembedding matrix is the final linear transformation in a language model that maps the high-dimensional residual stream state to a vector of logits, one for each token in the vocabulary, to produce a probability distribution for the next-token prediction.

The unembedding matrix is the final linear layer in a transformer-based language model, responsible for projecting the processed hidden state from the model's internal representation space back into the vocabulary space. After the final transformer block writes its output to the residual stream, the unembedding matrix multiplies this state vector to produce a raw score, or logit, for every token in the model's dictionary. This operation is the inverse conceptual counterpart to the embedding matrix, which maps discrete tokens into dense vectors at the input layer.

Crucially, the unembedding matrix is not merely a mechanical decoder but a key object of study in mechanistic interpretability. Because it directly connects internal representations to output tokens, its weights can be used as a lens to decode intermediate states. Techniques like the logit lens apply the unembedding matrix to the residual stream at earlier layers to reveal the model's iterative predictions, effectively reading its 'thought process' as it refines the next token before the final output.

DECODING THE VOCABULARY PROJECTION

Key Properties of the Unembedding Matrix

The unembedding matrix is the final linear transformation in a language model, mapping the high-dimensional residual stream state to a probability distribution over the entire vocabulary. Its properties directly govern the model's output behavior and are a primary target for interpretability research.

01

Dual of the Embedding Matrix

The unembedding matrix W_U is often tied to the transpose of the embedding matrix W_E through weight tying. This parameter sharing forces the input and output representations to live in the same space.

  • Weight Tying: Reduces parameter count and enforces a shared semantic geometry between input tokens and output logits.
  • Folding Norm: The unembedding vector for a token is its 'ideal' representation that the residual stream must align with to predict that token.
  • Interpretation: The logit for a token is the dot product between the final residual stream state and that token's unembedding vector.
02

Logit Lens Application

The Logit Lens technique applies the unembedding matrix to intermediate residual stream states, not just the final layer. This reveals the model's iterative prediction process.

  • Early Layer Predictions: Applying W_U at early layers often shows the model predicting nonsensical or highly generic tokens.
  • Convergence: As layers progress, the top predicted token converges toward the final output, showing how the model refines its belief.
  • Interpretability Tool: Researchers use this to diagnose where specific factual recall or syntactic decisions occur in the forward pass.
03

Softmax Bottleneck

The unembedding matrix is followed by a softmax function to produce a probability distribution. This creates a well-known representational bottleneck.

  • Rank Limitation: The logit for a token is a linear function of the hidden state. The model cannot express arbitrary probability distributions; it is constrained by the dimensionality of the residual stream.
  • Mixture of Softmaxes: Advanced architectures break this bottleneck by using multiple unembedding matrices and mixing their probabilities, allowing for more expressive output distributions.
  • Impact: This limitation is a key reason why language models struggle with tasks requiring precise, multi-modal output distributions.
04

Unembedding as a Semantic Space

The rows of the unembedding matrix form a semantic basis for the vocabulary. The geometry of this space encodes linguistic relationships.

  • Cosine Similarity: The cosine similarity between unembedding vectors for two tokens correlates with their semantic relatedness.
  • Linear Analogies: Famous analogies like 'king - man + woman = queen' can be performed in the unembedding space, demonstrating that it captures structured relational knowledge.
  • Concept Directions: Directions in the unembedding space can correspond to interpretable concepts, such as sentiment or tense, which can be used for steering.
05

Logit Attribution and Saliency

The unembedding matrix is the final step for logit attribution methods that trace a prediction back to input tokens or model components.

  • Direct Attribution: The contribution of a specific attention head or MLP neuron to the final logit is computed by multiplying its output by the unembedding matrix.
  • Gradient-Based Saliency: The gradient of a target logit with respect to the input embeddings is computed through the unembedding matrix, revealing which input tokens were most influential.
  • Causal Tracing: In causal tracing for factual recall, the unembedding matrix is used to project intermediate states to logits to measure the restoration of a corrupted fact.
06

Logit Suppression and Steering

By manipulating the logits before the softmax, the unembedding matrix enables direct control over the model's output distribution.

  • Logit Bias: Adding a constant bias vector to the logits can suppress or promote specific tokens, a common technique in API-based inference.
  • Contrastive Decoding: Methods like DoLa subtract the logits from an early layer's unembedding projection from the final layer's logits to amplify factual knowledge and reduce hallucinations.
  • Steering Vectors: A direction computed in the residual stream can be mapped through the unembedding matrix to understand which tokens it promotes, validating the vector's semantic effect.
UNEMBEDDING MATRIX DECODING

Frequently Asked Questions

Explore the critical final layer that translates a language model's internal state into a probability distribution over its vocabulary, enabling token prediction.

The unembedding matrix is the final linear layer in a transformer-based language model that maps the high-dimensional residual stream state to a probability distribution over the entire vocabulary of tokens. It functions as the inverse operation of the embedding matrix. While the embedding layer converts discrete input tokens into dense vectors, the unembedding matrix (often denoted as W_U) projects the final hidden state vector back into logit space. Mathematically, if the residual stream has a dimension d_model, and the vocabulary size is V, the unembedding matrix has dimensions [d_model, V]. The dot product between the final hidden state and each column of this matrix produces a raw score, or logit, for the corresponding token. These logits are then normalized by a softmax function to produce a valid probability distribution, from which the next token is sampled.

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.