DeepSeek LLM Inference Demystified: Auto-Regressive Token Generation via Matrix Math
The article explains how large language models like DeepSeek perform inference, detailing auto-regressive token generation, tokenization, embeddings, transformer layers with self-attention and feed-forward networks, matrix operations, and the role of weights, while contrasting inference simplicity with training complexity.
Reading font

Introduction: Inference vs. Training
Large language model development has two scenarios: training (high difficulty) and inference (low difficulty). After DeepSeek open-sourced its models, many users deployed them on modest hardware — a 7B model runs on a $300 PC, while the 671B version runs on a $6,000 machine. The article aims to demystify inference using only matrix operations, avoiding the 30+ prerequisite concepts usually required to explain Transformers.
Auto-Regressive Text Generation
LLMs generate text auto-regressively: given a prompt, the model produces a token, appends it to the prompt, and repeats until an <EOF> token or length limit is reached. At each step the model outputs a probability distribution (logits) over the vocabulary (e.g., 129,280 tokens for DeepSeek V3). The next token is sampled from this distribution, controlled by a temperature parameter (low temperature = deterministic, high = diverse). The author illustrates with an example: prompt "Quantum mechanics is a fundamental theory in physics that" yields token "provides", then "insights", etc., forming a complete sentence.
Tokens and Embeddings
Input text is split into tokens (subword units). DeepSeek V3 uses a vocabulary of 129,280 tokens. Each token is mapped to a high-dimensional vector via an embedding matrix. For DeepSeek V3 (671B), the embedding dimension is 7,168; for LLaMa 7B it is 4,096. The embedding matrix size is vocab_size × dim (e.g., 129,280 × 7,168). A prompt of n_tokens becomes an n_tokens × dim matrix, the starting point for all subsequent matrix computations.
Open-Source Code and Weights
DeepSeek open-sourced inference code (~1,500 lines of Python for V3) and model weights (the "encrypted compression of human knowledge"). The code uses PyTorch and includes fp8 GEMM (8-bit floating-point matrix multiplication) for acceleration. LLaMa's C++ inference (llama.cpp) is ~30k lines plus the ggml tensor library. Inference does not require NVIDIA GPUs; it runs on CPUs, AMD, Huawei Ascend, etc., because the computation is relatively simple matrix multiplication.
Transformer Layer Matrix Operations
DeepSeek V3 671B has 61 layers; LLaMa 7B has 32. Each layer (a Transformer decoder block) performs identical structure but with its own learned parameter matrices. The input matrix ( n_tokens × dim) passes through:
RMSNorm : root-mean-square normalization per token vector, then scaled by a learned weight vector (size dim).
Self-Attention : four weight matrices W_k, W_q, W_v, W_o (each dim × dim). The normalized input multiplies W_k, W_q, W_v to produce K, Q, V matrices ( n_tokens × dim). Q × Kᵀ yields an n_tokens × n_tokens attention-score matrix (scaled by √dim). A causal mask (upper triangle = -∞) ensures each token attends only to previous tokens. Softmax converts scores to probabilities; multiplied by V gives the attention output ( n_tokens × dim). Finally, W_o projects back to dim (multi-head attention splits this into multiple heads: 32 for LLaMa 7B, 128 for DeepSeek V3).
Residual Connection : attention output added to original input (helps gradient flow during training).
Feed-Forward Network (FFN) : another RMSNorm, then two linear layers with a hidden dimension inter_dim (11,008 for LLaMa 7B, 18,432 for DeepSeek V3). The first layer expands dim → inter_dim, applies SiLU (or ReLU) activation, the second contracts back to dim. Another residual connection adds the FFN output.
All operations are matrix multiplications and additions, highly parallelizable on GPUs.
Final Logits and Token Sampling
After the last layer, the final n_tokens × dim matrix is multiplied by an output matrix ( dim × vocab_size) to produce logits ( n_tokens × vocab_size). Only the last row (corresponding to the most recent token) is used to sample the next token.
Web Search and Deep Thinking
Web Search : When enabled, the model generates search queries, retrieves results, and dynamically integrates them into its context (not simple concatenation) to reduce hallucinations. Deep Thinking (Chain-of-Thought) : DeepSeek R1 was trained via reinforcement learning on V3, generating reasoning tokens wrapped in <think>...</think> without human-labeled CoT data. The model learns to reason by trial and error, guided only by final answer correctness. This "aha moment" mirrors the emergence phenomenon. R1-Zero emerged with strong logic but messy output; further training added readability and language consistency.
Conclusion
LLM inference is fundamentally a sequence of matrix operations — embedding, repeated layer-wise norm/attention/FFN, and final projection. Understanding this matrix view removes the mystery and shows why inference is far simpler than training (which requires massive GPU clusters, pipeline orchestration, and gradient optimization). The article encourages readers to build intuition from basic linear algebra rather than memorizing Transformer jargon.
For questions about attribution, corrections or content rights, please contactand we will review it promptly.