TAI BUI
Back to curriculum
LearnPython

DeepSeek-V3 Architecture Walkthrough

Phase 10 · Lesson 14 named the six architectural knobs that open models turn. DeepSeek-V3 (December 2024, 671B parameters total, 37B active) turns all six and adds four more: Multi-Head Latent Attention, auxiliary-loss-free load balancing, Multi-Token Prediction, and DualPipe training. This lesson reads the architecture top to bottom and derives the parameter counts from the published config. By the end you can explain why the 671B/37B ratio is the right bet and why MLA and MoE together beat either alone at the frontier.

TIME: ~75 minutesPREREQUISITES: Phase 10 · 14 (open-model walkthroughs), Phase 10 · 17 (NSA), Phase 10 · 18 (MTP), Phase 10 · 19 (DualPipe)

Learning Objectives

  • Read the DeepSeek-V3 config top to bottom and explain each field in terms of the six GPT-2 knobs plus four DeepSeek-specific additions.
  • Derive the total parameter count (671B), active parameter count (37B), and the components that contribute to each.
  • Compute the KV cache footprint of MLA at 128k context and compare to what a same-active-param dense model with GQA would pay.
  • State the four DeepSeek-specific innovations (MLA, MTP, auxiliary-loss-free routing, DualPipe) and name which part of the architecture/training stack each one targets.

The Problem

DeepSeek-V3 is the first open model at the frontier whose architecture looks meaningfully different from the Llama family. Llama 3 405B is "GPT-2 with six knobs turned." DeepSeek-V3 turns the same six and adds four more. Reading the Llama 3 config is a good warmup, but the shape of the attention block, the routing logic, and the training-time objective differ enough that this model needs its own walkthrough.

The open-weights release shifted what "frontier capability" means for open models, and many 2026 training runs are copying the blueprint. If your work touches frontier LLM training or inference, you need to be able to read this config cold.

Warm-up0/3
1. In the Phase 10 · 14 framing, what does GQA do compared with full multi-head attention?Warm-up
A term card naming Grouped-Query Attention (GQA). Context note: Context: from Phase 10, Lesson 14, a scheme between full multi-head attention and one shared KV head. A question asks: What does it do compared with full multi-head attention?
The term as the lesson names it; its mechanism is the question.
2. What does a bidirectional pipeline schedule such as DualPipe (Phase 10 · 19) mainly try to reduce?Warm-up
A term card naming DualPipe. Context note: Context: a bidirectional pipeline training schedule, named in Phase 10, Lesson 19. A question asks: What does it mainly try to reduce?
The term as the lesson names it; its mechanism is the question.
3. A mixture-of-experts transformer block replaces the single feed-forward network of a dense block with what?Warm-up
A term card naming a Mixture-of-Experts (MoE) block. Context note: Context: replaces the single feed-forward network of a dense transformer block. A question asks: What does it replace that network with?
The term as the lesson names it; its mechanism is the question.
Check: The Problem0/1
1. Why does DeepSeek-V3 need its own walkthrough after the Llama 3 reading?Explain
Llama 3, labelled GPT-2 with six knobs turned, with an arrow to DeepSeek-V3, left blank. A question asks why DeepSeek-V3 needs its own walkthrough rather than just the Llama 3 reading.
Llama 3's own description is the lesson's framing; what else DeepSeek-V3 needs is the question.

The Concept

The invariant core, again

DeepSeek-V3 is still autoregressive and still a stack of decoder blocks. Each block has attention, an MLP, and two RMSNorms. The MLP is SwiGLU, positions use RoPE, norms are pre-norm, and embeddings are weight-tied. That is the same baseline as the Llama and Mistral families.

The twist: MLA instead of GQA

From Phase 10 · 14 you know GQA shrinks the KV cache by sharing K and V across groups of Q heads. Multi-Head Latent Attention (MLA) goes further. K and V are compressed into one shared low-rank latent (the kv_lora_rank) and decompressed per head on the fly, so the cache stores only the latent: typically 512 floats per token per layer, against 8 x 128 = 1024 floats for the GQA shape below.

At 128k context, DeepSeek-V3 with MLA stores one shared latent c^{KV} per token per layer. K and V are both derived from it through up-projections that can be absorbed into the subsequent matmul.

kv_cache = num_layers * kv_lora_rank * max_seq_len * bytes_per_element
         = 61 * 512 * 131072 * 2
         = 7.6 GB

A hypothetical GQA baseline (Llama 3 70B shape, 8 KV heads, head dim 128) would pay:

kv_cache = 2 * 61 * 8 * 128 * 131072 * 2
         = 30.5 GB

MLA comes out 4x smaller than a Llama-3-70B-style GQA cache at 128k context.

The cost is a decompression step per head in each attention computation. That extra compute is small next to the bandwidth saved, so MLA is a net win for long-context inference.

The routing: auxiliary-loss-free load balancing

MoE routers pick the top-k experts for each token. A naive router piles work onto a few experts and leaves the rest idle. The standard fix is an auxiliary loss term that penalizes load imbalance. It works, but it slightly degrades main-task performance.

DeepSeek-V3 drops the auxiliary loss. Each expert gets a bias term added to its router logits, and training adjusts it with a simple rule: if expert e is overloaded, decrease bias_e; if underloaded, increase it. There is no extra loss term, so the objective stays clean and expert load stays balanced. The effect on the main loss is not measurable, and the MoE has one less hyperparameter to tune.

The MTP: denser training and a free draft

From Phase 10 · 18 you know DeepSeek-V3 adds a D=1 MTP module that predicts the token two positions ahead. In training, each hidden state is supervised on D+1 = 2 targets, which is a denser signal. At inference the trained module doubles as a speculative-decoding draft with 80%+ acceptance.

It costs 14B parameters on top of the 671B main model, a 2.1% overhead.

The training: DualPipe

From Phase 10 · 19 you know DualPipe is a bidirectional pipeline that overlaps forward and backward chunks with cross-node all-to-all communication. At DeepSeek-V3's 2,048-H800 scale it recovers roughly 245k GPU-hours that 1F1B would have lost to pipeline bubbles.

The config, field by field

Here is the DeepSeek-V3 config (simplified):

hidden_size: 7168
intermediate_size: 18432   (dense MLP hidden size, used on first few layers)
moe_intermediate_size: 2048 (expert MLP hidden size)
num_hidden_layers: 61
first_k_dense_layers: 3    (first 3 layers use dense MLP)
num_attention_heads: 128
num_key_value_heads: 128   (formally equal to num_heads under MLA, but
                           the real compression is in kv_lora_rank)
kv_lora_rank: 512          (MLA latent dimension)
num_experts: 256            (MoE expert count per block)
num_experts_per_tok: 8      (top-8 routing)
shared_experts: 1           (always-on shared expert per block)
max_position_embeddings: 163840
rope_theta: 10000.0
vocab_size: 129280
mtp_module: 1               (1 MTP module at depth 1)

Parse it:

  • hidden_size=7168 is the embedding dimension.
  • num_hidden_layers=61 is the total block depth.
  • first_k_dense_layers=3: the first 3 blocks use a dense MLP of size 18432, and the remaining 58 use MoE.
  • num_attention_heads=128 gives 128 query heads.
  • kv_lora_rank=512: K and V are compressed to this latent dimension and decompressed per head.
  • num_experts=256, num_experts_per_tok=8: each MoE block holds 256 routed experts and sends each token to the top 8.
  • shared_experts=1: on top of the 256 routed experts, one always-on expert sees every token. Think of it as a "dense floor" that gives each token something reliable.
  • moe_intermediate_size=2048 is each expert's MLP hidden size. It is smaller than the dense MLP because there are 256 of them.

Parameter accounting

The full calculation lives in code/main.py. The headline:

  • Embedding: vocab * hidden = 129280 * 7168 = ~0.93B.
  • First 3 dense blocks: attention with MLA (~84M per block) + dense MLP (~396M per block) + norms. About 481M per block, 1.4B total.
  • 58 MoE blocks: attention with MLA (~84M) + 256 experts (44M apiece) + 1 shared expert (44M) + router (1.8M) + norms. That is ~11.4B per block including all experts, and ~661B for the 58 MoE blocks.
  • MTP module: the calculator's dense approximation comes to 0.7B, against the 14B in the published model.

The grand total is 664.5B. Without the MTP approximation the core architecture is 663.8B, about 1% under the published 671B. The calculator uses the simple head dimension hidden / heads = 56 for MLA and leaves out bias tensors and other fine-grained pieces that the appendix of Section 2 of DeepSeek's report itemizes, so a small gap is expected.

Active parameters per forward:

  • Attention: 84M per layer * 61 = 5.1B (every layer fires).
  • MLP active: the first 3 layers are dense (3 * 396M = 1.2B), and each of the 58 MoE layers runs 8 routed experts, 1 shared expert, and the router, about 398M per layer (the MLP part only: the "moe layer, active" line the script prints is 482.6M because it adds the 84M of attention). Total: ~24.3B.
  • Embedding + norms: 0.93B.
  • Total active: ~30.4B, which is 4.6% of the calculator's total. The published figure is 37B, and the MTP module is trained but not always run at inference.

The 671B / 37B ratio

That is an 18x sparsity ratio: active params are 5.5% of total. DeepSeek-V3 is one of the sparsest frontier MoE models to ship open weights. Mixtral 8x7B at 13/47 (28%) is much denser, and Llama 4 Maverick at 17B/400B (4.25%) is in the same range. The DeepSeek bet is that at frontier scale, more experts with a lower activation ratio give better quality per active FLOP.

Where DeepSeek-V3 sits

ModelTotalActiveRatioAttentionNovel ideas
Llama 3 70B70B70B100%GQA 64/8—
Llama 4 Maverick400B17B4.25%GQA—
Mixtral 8x22B141B39B27%GQA—
DeepSeek V3671B37B5.5%MLA 512MLA + MTP + aux-free + DualPipe
Qwen 2.5 72B72B72B100%GQA 64/8YaRN extension

The follow-on: R1, V4

DeepSeek-R1 (2025) is a reasoning-training run on the V3 backbone with the same architecture. What changed is the post-training recipe (large-scale RL on verifiable tasks), not the pretraining architecture.

DeepSeek-V4 (if it ships) is expected to keep MLA + MoE + MTP and add DSA (DeepSeek Sparse Attention), the successor to NSA from Phase 10 · 17. The lineage is stable: architecture-level innovations accumulate, and each version turns more knobs.

Check: The Concept0/28
1. What is Multi-Head Latent Attention (MLA)?Define
A term card naming Multi-Head Latent Attention (MLA). Context note: Context: DeepSeek-V3's attention scheme, the step beyond GQA. A question asks: What is it?
The term as the lesson names it; its mechanism is the question.
2. What is kv_lora_rank in the DeepSeek-V3 config?Define
A term card naming kv_lora_rank. Context note: Context: a field in DeepSeek-V3's attention config, set to 512. A question asks: What does it control?
The term as the lesson names it; its mechanism is the question.
3. What does first_k_dense_layers=3 mean in DeepSeek-V3's config?Define
An excerpt of DeepSeek-V3's config showing first_k_dense_layers at 3 and num_hidden_layers at 61, with nothing else filled in. A question asks what first_k_dense_layers=3 means for these 61 blocks.
Both fields are quoted from the lesson's own config; what the first one means for the rest of the stack is the question.
4. What does num_experts_per_tok=8 control in an MoE block with 256 routed experts?Define
An excerpt of DeepSeek-V3's config showing num_experts at 256 and num_experts_per_tok at 8. A question asks what num_experts_per_tok=8 controls in a block of 256 routed experts.
Both fields are quoted from the lesson's own config; what the second one controls is the question.
5. What is a shared expert in DeepSeek-V3?Define
A term card naming a shared expert. Context note: Context: an expert that sits beside DeepSeek-V3's 256 routed experts, one per block. A question asks: What is it?
The term as the lesson names it; its mechanism is the question.
6. How does DeepSeek-V3's auxiliary-loss-free load balancing work?Define
A term card naming auxiliary-loss-free load balancing. Context note: Context: DeepSeek-V3's replacement for an auxiliary loss term that used to balance MoE expert load. A question asks: How does it work?
The term as the lesson names it; its mechanism is the question.
7. What is the MTP module in DeepSeek-V3?Define
A term card naming the MTP module. Context note: Context: an extra module trained alongside DeepSeek-V3's main model. A question asks: What is it?
The term as the lesson names it; its mechanism is the question.
8. What is DualPipe?Define
A term card naming DualPipe. Context note: Context: a training-time pipeline schedule, named in Phase 10, Lesson 19. A question asks: What is it?
The term as the lesson names it; its mechanism is the question.
9. What is DeepSeek-V3's active-to-total parameter ratio?Define
A field card giving DeepSeek-V3's total parameters as 671B, with its active parameter count and active ratio left blank. A question asks what the active-to-total parameter ratio is.
The total is the lesson's own published figure; the active count and ratio are the question.
10. What does FP8 training mean for a model like DeepSeek-V3?Define
A term card naming FP8 training. Context note: Context: how DeepSeek-V3 stores and computes many operations during training. A question asks: What does it mean for a model like this?
The term as the lesson names it; its mechanism is the question.
11. Which parts of DeepSeek-V3's block does it share with the Llama and Mistral families?Explain
A column of five block settings, MLP activation, positions, norm placement, attention, and embeddings, each with an arrow to a blank box. A question asks which option correctly states what carries over from the Llama and Mistral families.
The five settings named are the ones the lesson compares; which carry over unchanged is the question.
12. At 128k context, how does MLA's roughly 7.6 GB KV cache compare with a Llama-3-70B-style GQA cache over the same 61 layers?Explain
The MLA cache at 128k context given as about 7.6 GB, next to a Llama-3-70B-style GQA formula over the same 61 layers with 8 KV heads and head dim 128, whose own cache total is left blank. A question asks how the MLA cache compares with this GQA baseline.
MLA's own figure is the stem's given number; the GQA baseline's total is left for the question.
13. MLA adds a decompression step per head. Why is that acceptable for long-context inference?Explain
A three-step flow, cached latent, decompress per head, attention score, representing MLA's extra step on every attention computation. A question asks why that extra step is acceptable for long-context inference.
The extra step itself is the setup; why it is worth paying is the question.
14. Why is each routed expert's MLP hidden size (2048) so much smaller than the dense MLP's 18432?Explain
A field card giving the dense MLP hidden size at 18,432 and the expert MLP hidden size at 2,048. A question asks why the expert MLP's hidden size is so much smaller than the dense MLP's.
Both widths are the lesson's own config values; the reason for the gap is the question.
15. What is the point of the one shared expert that sits beside the 256 routed experts?Explain
A grid of 256 small dots representing routed experts, with 8 of them highlighted as the top-8 selected for one token, next to a separate always-on shared-expert box. A question asks what the point of that shared expert is, beside the 256 routed ones.
The 256-expert grid and the always-on label are given facts; the shared expert's purpose is the question.
16. What does DualPipe buy at DeepSeek-V3's 2,048-H800 scale compared with a 1F1B schedule?Explain
A field card giving DeepSeek-V3's training scale, 2,048 H800 GPUs, comparing DualPipe against a 1F1B schedule, with the GPU-hours recovered left blank. A question asks what DualPipe buys at this scale compared with 1F1B.
The GPU count and the two schedules being compared are the stem's own setup; the recovered time is the question.
17. How does DeepSeek-R1 relate to DeepSeek-V3?Explain
DeepSeek-V3, labelled as the MLA plus MoE plus MTP backbone, with an arrow to DeepSeek-R1 from 2025, left blank. A question asks how R1 relates to V3 in architecture and in training.
V3's backbone is the given starting point; what changed for R1 is the question.
18. What is DeepSeek-V4 expected to add on top of keeping MLA, MoE and MTP?Explain
DeepSeek-V3, labelled as keeping MLA, MoE and MTP, with an arrow to a future DeepSeek-V4 left blank. A question asks what V4 is expected to add on top of keeping those three.
V3's three kept pieces are the given starting point; V4's addition is the question.
19. In the Where DeepSeek-V3 sits table, which model has a 4.25% active ratio, GQA attention, and no novel ideas listed?Apply
A four-row excerpt of the lesson's model comparison table with model names replaced by numbers 1 through 4, each row showing total parameters, active parameters, active ratio, attention scheme, and novel ideas. DeepSeek-V3's own row is left out of the excerpt. A question asks which numbered row has a 4.25% active ratio, GQA attention, and no novel ideas.
All four rows are the lesson's own table values; matching one back to its model name is the question.
20. Using vocab_size and hidden_size from the config, roughly how large is DeepSeek-V3's embedding table?Apply
A field card with vocab_size 129,280 and hidden_size 7,168 from the config, and the embedding product left blank. A question asks roughly how large the embedding table is.
Both config fields are given; the product is the question.
21. The MTP module adds 14B parameters on top of the 671B main model. What overhead is that?Apply
A field card giving the MTP module at 14B parameters and the main model at 671B, with the resulting overhead percentage left blank. A question asks what overhead 14B on top of 671B represents.
Both parameter counts are the stem's own given numbers; the percentage is the question.
22. Each MoE block has 256 routed experts, top-8 routing and 1 shared expert. How many expert MLPs run for one token in one MoE block?Apply
A field card giving 8 routed experts selected by top-8 routing and 1 always-on shared expert, with the total that run left blank. A question asks how many expert MLPs run for one token in one block.
Both counts are the stem's own given numbers; their total is the question.
23. Using the lesson's formula (layers x kv_lora_rank x tokens x bytes), a GQA baseline of 30.5 GB and a latent halved to 256, how much smaller is the MLA cache at 128k?Apply
The MLA cache formula with its inputs, 61 layers, kv_lora_rank 256, 131,072 tokens, 2 bytes per element, and the cache total left blank, next to a given 30.5 GB GQA baseline with the comparison left blank. A question asks how much smaller the MLA cache is than that baseline.
The formula's inputs and the GQA baseline are given; the MLA total and the comparison are the question.
24. Training shows a few experts overloaded and others idle, and adding an auxiliary balancing loss slightly hurts main-task quality. Which DeepSeek-V3 mechanism targets this exactly?Analyze
Three expert boxes, one marked overloaded and two marked idle, with a note that adding an auxiliary balancing loss here slightly hurts main-task quality. A question asks which DeepSeek-V3 mechanism targets this without that cost.
The imbalance and the auxiliary-loss cost are the stem's own setup; the mechanism is the question.
25. A reader sees num_key_value_heads: 128 equal to num_attention_heads and concludes MLA gives no KV cache saving. What is the mistake?Analyze
An excerpt of DeepSeek-V3's config showing num_attention_heads and num_key_value_heads both at 128, with a note that a reader sees these two fields equal and concludes MLA saves no KV cache. A question asks what the mistake is.
Both fields and the reader's conclusion are the stem's own setup; the mistake is the question.
26. A teammate says a 671B-parameter MoE needs 671B parameters of compute for each token, so DeepSeek-V3 is as costly per token as a dense 671B model. What did they overlook?Analyze
A quoted claim that a 671B MoE needs 671B of compute for every token, as costly per token as a dense model of that size. A question asks what the claim overlooks.
The claim is the stem's own quoted misconception; what it overlooks is the question.
27. You must serve 128k-token requests on GPUs where the KV cache is the memory bottleneck. Which design lever from the lesson fits best?Design
A note that for 128k-token requests, the KV cache is the GPU memory bottleneck. A question asks which design lever from the lesson fits best for this situation.
The bottleneck itself is the stem's own setup; the lever to pull is the question.
28. You want faster decoding without training or hosting a separate small draft model. Which part of the DeepSeek-V3 design gives you a draft?Design
A note that the goal is faster decoding without training or hosting a separate draft model. A question asks which already-trained part of DeepSeek-V3 gives a draft for speculative decoding.
The constraint is the stem's own setup; which part already does this job is the question.

Use It

code/main.py is the parameter calculator specialized to DeepSeek-V3's shape. Run it, compare its output to the paper's numbers, then point it at variants: 256 experts vs 512, top-8 vs top-16, MLA rank 512 vs 1024.

What to look at:

  • Total parameter count vs the published 671B.
  • Active parameter count vs the published 37B.
  • KV cache at 128k context, MLA vs GQA.
  • The per-layer breakdown, to see where the parameter budget goes.
Check: Use It0/3
1. Per the calculator's what-if block, the baseline 128k MLA cache is 7.6 GB. What does it print after kv_lora_rank drops to 256?Apply
A field card giving the baseline 128k MLA cache at 7.6 GB for kv_lora_rank 512, with the cache for kv_lora_rank 256 left blank. A question asks what the calculator's what-if block prints for the dropped rank.
The baseline figure is the stem's own given number; the dropped-rank figure is the question.
2. The what-if block reports 30.5B active for 512 experts at top-8 and 50.8B active when going to top-16. Which knob drives active parameters?Apply
A table of three calculator runs, baseline, 512 experts, and top-16, each with its num_experts, num_experts_per_tok, and resulting active parameter count. A question asks which knob tracks the active-parameter changes across the three rows.
All three rows are the calculator's own given numbers; which knob explains the pattern is the question.
3. Your team wants to try 512 routed experts at top-8 instead of 256 at top-8 on the DeepSeek-V3 shape. Going by the calculator runs, what should you expect?Design
A table comparing the current DeepSeek-V3 shape, 256 experts at top-8, with a proposed 512 experts at top-8, giving the current total and active parameter counts and leaving the proposed column's total and active blank. A question asks what to expect for the proposed column.
The current column's own figures are given; the proposed column's totals are the question.

Ship It

This lesson produces outputs/skill-deepseek-v3-reader.md. Give it a DeepSeek-family model (V3, R1, or a future variant) and it returns a component-by-component architecture reading. It names each field of the config, derives parameter counts by component, and says which of the four DeepSeek-specific innovations the model uses.

Author's Note

I'd run main.py before trusting any hand arithmetic in this lesson. For the published config it prints 664.5B total and 30.4B active (4.6%), with 84.4M for MLA attention per layer. The accounting above uses those printed figures. When hand arithmetic and the printout disagree, believe the printout, since the code is what you can change and rerun.

The part worth your time is the what-if block. Dropping kv_lora_rank to 256 takes the 128k cache from 7.6GB to 3.8GB. Moving to 512 experts at top-8 roughly doubles total parameters to 1318.6B while active parameters barely move (30.5B), and going to top-16 instead pushes active to 50.8B. That asymmetry is the core of the case for many small experts, and you can see it in a few printed lines.

One caveat on MTP: the module in the script is a dense approximation, as its docstring says, so don't read its parameter count as the published 14B.

Exercises

  1. Run code/main.py. Compare the calculator's total-parameter estimate to the published 671B and identify where the delta comes from. The paper's Section 2 has the full itemization.

  2. Modify the config to use MLA rank 256 instead of 512. Compute the resulting KV cache size at 128k context. What percentage reduction does it buy, and at what cost to the per-head expressiveness?

  3. Compare DeepSeek-V3's (256 experts, top-8) routing to a hypothetical (512 experts, top-8) variant. Total parameters grow; active parameters stay the same. What does the extra expert capacity buy in theory, and what does it cost at inference?

  4. Read Section 2.1 of the DeepSeek-V3 technical report (arXiv:2412.19437) on MLA. Explain in three sentences why the K and V decompression matrices can be "absorbed" into the subsequent matmul for inference-time efficiency.

  5. DeepSeek-V3 uses FP8 training for most operations. Compute the memory savings of FP8 vs BF16 for storing the 671B weights. How does this intersect with the 14.8T-token training budget?

Key Terms

TermWhat people sayWhat it actually means
MLA"Multi-Head Latent Attention"Compress K and V into a shared low-rank latent (kv_lora_rank, typically 512), decompress per head on-the-fly; KV cache stores only the latent
kv_lora_rank"MLA compression dim"The size of the shared latent for K and V; DeepSeek-V3 uses 512
First k dense layers"Early layers stay dense"The first few MoE-model layers skip the MoE router and run a dense MLP for stability
num_experts_per_tok"Top-k routing"How many routed experts fire per token; DeepSeek-V3 uses 8
Shared experts"Always-on experts"Experts that process every token regardless of routing; DeepSeek-V3 uses 1
Auxiliary-loss-free routing"Bias-adjusted load balance"Per-expert bias terms adjusted during training to keep expert load balanced without adding a loss term
MTP module"Extra prediction head"Transformer block that builds h^(1) from h^(0) and E(t+1), so the shared head predicts t+2 from it; denser training, free speculative-decoding draft
DualPipe"Bidirectional pipeline"Training schedule that overlaps forward/backward compute with cross-node all-to-all
Active parameter ratio"Sparsity"active_params / total_params; DeepSeek-V3 hits 5.5%
FP8 training"8-bit training"Training storage and many compute ops in FP8; roughly halves memory vs BF16 at a small quality cost

Further Reading

Code

Ships With