From Text Embeddings to Transformers: Core LLM Matrix Operations Overview
메뉴

AI Development

From Text Embeddings to Transformers: Core LLM Matrix Operations Overview

Covers the mathematical journey of LLM operations, including text embeddings, one-hot encoding limitations, RNN vs Transformer architectural differences, and Q, K, V Self-Attention matrix computations.

From Text Embeddings to Transformers: Core LLM Matrix Operations Overview hero image

Computers have never directly read or understood human text. Words like "apple," "dog," or "transformer" on a terminal or web interface are translated into mathematical arrays of floating-point numbers and matrix operations the moment they enter a software environment.

For general software developers and non-ML technical professionals working with Large Language Models (LLMs) like ChatGPT, Claude, or Llama, terms like Embedding, One-Hot Encoding, RNN, Transformer, and Q, K, V Self-Attention appear constantly.

This article explores why these core mechanisms were invented, how they operate step-by-step through matrix operations, and what practical engineering takeaways they offer.


1. The Initial Barrier: Translating Words to Numbers

In standard software development, evaluating if (word1 === word2) simply compares UTF-8 byte sequences. The computer has no intrinsic knowledge of whether "apple" and "pear" are related fruits, or if "apple" and "automobile" belong to entirely different categories.

For neural networks to process human language, words must be converted into numerical arrays called Tensors. Early Natural Language Processing (NLP) researchers first attempted the simplest approach: One-Hot Encoding.

One-Hot Encoding Structure and Limitations

If a vocabulary contains 100,000 words, a 100,000-dimensional array is created. Each word is assigned a single index set to 1, while all other indices remain 0.

apple      = [1, 0, 0, 0, 0, ..., 0]  (Index 1 is 1)pear       = [0, 1, 0, 0, 0, ..., 0]  (Index 2 is 1)automobile = [0, 0, 1, 0, 0, ..., 0]  (Index 3 is 1)

While intuitive, this approach created two critical engineering bottlenecks:

  1. Memory Explosion (Sparse Vectors): Representing even a small sentence generated multi-hundred-thousand dimensional sparse vectors where 99.99% of values were zeros, severely wasting memory.
  2. Semantic Orthogonality (Zero Similarity): Mathematically, all one-hot vectors are orthogonal to one another. The dot product of any two one-hot vectors is always 0. To the computer, the relationship between "apple" and "pear" was identical to the relationship between "apple" and "automobile"—completely unrelated.

2. Breakthrough: Dense Embeddings in Semantic Space

Dense Embedding solved this dilemma by replacing 100,000-dimensional sparse arrays with continuous floating-point coordinates across lower-dimensional spaces (e.g., 512 or 1,536 dimensions).

apple      = [0.24, -0.81,  0.45,  0.12, ...]  (512-dimensional floats)pear       = [0.21, -0.79,  0.41,  0.09, ...]  (Close coordinates to apple)automobile = [-0.91,  0.15, -0.88,  0.73, ...] (Far coordinates from apple)

Multidimensional Coordinate Analogy

Just as city locations are identified using 3 axes (Latitude, Longitude, Altitude), word embeddings map tokens across hundreds of continuous coordinate axes in a Vector Space.

  • Proximity: Words appearing in similar contexts sit near each other in coordinate space.
  • Vector Arithmetic: Mathematical addition and subtraction reflect intuitive human concepts (e.g., King - Man + Woman ≈ Queen).

Lookup Tables and Backpropagation

Initial coordinate values in an Embedding Lookup Table start as random floats. As the neural network trains to predict upcoming words, prediction errors trigger Backpropagation, using Gradient Descent to iteratively adjust coordinate positions.

After processing millions of sentences, words with similar meanings converge toward one another, forging a dense semantic map.

Practical Developer Connection: Vector Databases

In modern LLM applications, Vector Databases (Pinecone, pgvector, Qdrant) store and index these dense embedding vectors.

When a user submits a search query, the system converts the query into a dense embedding vector and computes Cosine Similarity against stored document vectors. Finding semantically relevant passages relies directly on this vector space math.


3. The Second Barrier: The Sequential Loop Bottleneck (RNN vs. LSTM)

Converting individual words into continuous vectors solved representation, but single words cannot resolve context. For example, "bank" in "river bank" versus "bank account" requires examining the surrounding sentence.

Recurrent Neural Networks (RNNs)

To process sequential context, early architectures used RNNs. An RNN processes sentences word-by-word in order, passing a Hidden State vector ($h_t$) to subsequent steps.

[Word 1] → [RNN Cell] → Passes Hidden State h1[Word 2] → [RNN Cell] → Passes Hidden State h2[Word 3] → [RNN Cell] → Passes Hidden State h3

Two Severe Bottlenecks of RNNs and LSTMs

  1. Inability to Parallelize on GPUs: Computing step N requires the output of step N-1. Thousands of GPU cores sat idle because the sequential loop prevented parallel matrix execution.
  2. Vanishing Gradient and Memory Loss: As sentence length increased, information from early words diluted over recurrent steps. LSTMs added a Cell State highway to mitigate this, but maintaining long-range context across long passages remained difficult.

4. The Turning Point: Transformers and Self-Attention

In 2017, Vaswani et al. published "Attention Is All You Need," abandoning sequential recurrent processing.

Instead of reading words step-by-step in sequence, the Transformer ingests the entire sentence simultaneously through matrix multiplication, allowing every word to attend to every other word concurrently.


5. Deconstructing Q, K, V Self-Attention Matrix Operations

Understanding how Self-Attention evaluates sentence-wide relationships in a single matrix pass requires looking at Query, Key, and Value vectors.

The Classroom Analogy (Q, K, V Concepts)

Imagine students (words) sitting in a classroom:

  • Q (Query): A student asking a specific question ("Who understands Redis caching?").
  • K (Key): Public badge tags worn by each student ("DB Expert," "UI Expert," "Caching Expert").
  • V (Value): The actual knowledge payload held in each student's mind.

The asking student (Query) scans all student badges (Keys) simultaneously. Higher tag compatibility yields higher Attention weight, drawing more knowledge payload (Value) from relevant peers.

Step-by-Step Breakdown of the Self-Attention Formula

Attention(Q, K, V) = softmax( (Q × Kᵀ) / √d_k ) × V

Input embedding matrix X multiplies learned weight matrices W_Q, W_K, and W_V to generate Q, K, and V matrices. The formula executes across four matrix operations:

  1. Parallel Relevance Matrix (Q × Kᵀ): Multiplying Query (Q) by Key transpose (Kᵀ) evaluates all pairwise token relationships across the sentence in a single matrix multiplication.
  2. Scaling (/ √d_k): Dividing by the square root of key dimension prevents dot products from blowing up in high dimensions, protecting Softmax gradients from vanishing.
  3. Probability Normalization (Softmax): Softmax converts scores into probability weights that sum to 1 (e.g., Token A attends 70% to Token B and 20% to Token C).
  4. Contextual Weighted Sum (× V): Multiplying normalized weights by Value matrix V synthesizes a updated, context-aware representation vector for every token concurrently.

6. Multi-Head Attention and Positional Encoding

Multi-Head Attention: Viewing Context from Multiple Angles

A single attention pass cannot capture all relationship types at once. In "She handed him the apple":

  • Head 1: Grammatical Subject-Verb relationship (She → handed)
  • Head 2: Pronoun reference relationship (She → Who?)
  • Head 3: Object-Verb relationship (apple → handed)

Multi-Head Attention splits Q, K, and V across multiple subspaces (e.g., 8 or 16 heads), processing distinct grammatical and semantic relationships simultaneously.

Positional Encoding

Because Transformers process all tokens at once, "dog bit man" and "man bit dog" would appear identical without ordering information. Positional Encoding adds sinusoidal coordinate values to token embeddings, preserving word order without requiring sequential loops.


7. Summary of Engineering Takeaways for Developers

  1. Quadratic Attention Cost ($O(N^2)$): Because every token attends to every other token, Self-Attention generates $O(N^2)$ matrix dimensions relative to sentence length N. This quadratic scaling is why long Context Windows consume significant GPU memory and API costs.
  2. Why GPUs Are Essential: Over 90% of Transformer computation consists of large-scale parallel matrix multiplications ($A \times B$). GPUs and NPUs with thousands of small arithmetic cores are essential for LLM training and inference servers.
  3. The Core of RAG and Prompting: Retrieval-Augmented Generation (RAG) injects relevant context directly into the model's Q, K, V attention space, guiding Self-Attention weights to focus on factual source tokens.

From dense embeddings to parallel Self-Attention, modern LLMs execute language comprehension through continuous, structured matrix operations.

댓글

GitHub 계정으로 로그인하면 댓글을 남길 수 있습니다. 댓글은 GitHub Discussions를 통해 운영됩니다.

TOP