2 Architecture

本文为论文《DeepSeek-V3:强大高效的开源混合专家大模型》「论文原文」板块的对应章节,按 arXiv 官方全文逐字保留,层级与「中文全文翻译」板块一一对应。

We first introduce the basic architecture of DeepSeek-V3, featured by Multi-head Latent Attention (MLA) (DeepSeek-AI, 2024c) for efficient inference and DeepSeekMoE (Dai et al., 2024) for economical training. Then, we present a Multi-Token Prediction (MTP) training objective, which we have observed to enhance the overall performance on evaluation benchmarks. For other minor details not explicitly mentioned, DeepSeek-V3 adheres to the settings of DeepSeek-V2 (DeepSeek-AI, 2024c).

图

Figure 2: Illustration of the basic architecture of DeepSeek-V3. Following DeepSeek-V2, we adopt MLA and DeepSeekMoE for efficient inference and economical training.

2.1 Basic Architecture

The basic architecture of DeepSeek-V3 is still within the Transformer (Vaswani et al., 2017) framework. For efficient inference and economical training, DeepSeek-V3 also adopts MLA and DeepSeekMoE, which have been thoroughly validated by DeepSeek-V2. Compared with DeepSeek-V2, an exception is that we additionally introduce an auxiliary-loss-free load balancing strategy (Wang et al., 2024a) for DeepSeekMoE to mitigate the performance degradation induced by the effort to ensure load balance. Figure 2 illustrates the basic architecture of DeepSeek-V3, and we will briefly review the details of MLA and DeepSeekMoE in this section.

2.1.1 Multi-Head Latent Attention

For attention, DeepSeek-V3 adopts the MLA architecture. Let $d$ denote the embedding dimension, $n_{h}$ denote the number of attention heads, $d_{h}$ denote the dimension per head, and $\mathbf{h}_{t}\in\mathbb{R}^{d}$ denote the attention input for the $t$-th token at a given attention layer. The core of MLA is the low-rank joint compression for attention keys and values to reduce Key-Value (KV) cache during inference:

$\displaystyle\boxed{\color[rgb]{0,0,1}\mathbf{c}_{t}^{KV}}$ $\displaystyle=W^{DKV}\mathbf{h}_{t},$ (1)
$\displaystyle[\mathbf{k}{t,1}^{C};\mathbf{k}{t,2}^{C};...;\mathbf{k}{t,n{h}}^{C}]=\mathbf{k}_{t}^{C}$ $\displaystyle=W^{UK}\mathbf{c}_{t}^{KV},$ (2)
$\displaystyle\boxed{\color[rgb]{0,0,1}\mathbf{k}_{t}^{R}}$ $\displaystyle=\operatorname{RoPE}({W^{KR}}\mathbf{h}_{t}),$ (3)
$\displaystyle\mathbf{k}_{t,i}$ $\displaystyle=[\mathbf{k}{t,i}^{C};\mathbf{k}{t}^{R}],$ (4)
$\displaystyle[\mathbf{v}{t,1}^{C};\mathbf{v}{t,2}^{C};...;\mathbf{v}{t,n{h}}^{C}]=\mathbf{v}_{t}^{C}$ $\displaystyle=W^{UV}\mathbf{c}_{t}^{KV},$ (5)

where $\mathbf{c}{t}^{KV}\in\mathbb{R}^{d{c}}$ is the compressed latent vector for keys and values; $d_{c}(\ll d_{h}n_{h})$ indicates the KV compression dimension; $W^{DKV}\in\mathbb{R}^{d_{c}\times d}$ denotes the down-projection matrix; $W^{UK},W^{UV}\in\mathbb{R}^{d_{h}n_{h}\times d_{c}}$ are the up-projection matrices for keys and values, respectively; $W^{KR}\in\mathbb{R}^{d_{h}^{R}\times d}$ is the matrix used to produce the decoupled key that carries Rotary Positional Embedding (RoPE) (Su et al., 2024); $\operatorname{RoPE}(\cdot)$ denotes the operation that applies RoPE matrices; and $[\cdot;\cdot]$ denotes concatenation. Note that for MLA, only the blue-boxed vectors (i.e., $\color[rgb]{0,0,1}\mathbf{c}{t}^{KV}$ and $\color[rgb]{0,0,1}\mathbf{k}{t}^{R}$) need to be cached during generation, which results in significantly reduced KV cache while maintaining performance comparable to standard Multi-Head Attention (MHA) (Vaswani et al., 2017).

For the attention queries, we also perform a low-rank compression, which can reduce the activation memory during training:

$\displaystyle\mathbf{c}_{t}^{Q}$ $\displaystyle=W^{DQ}\mathbf{h}_{t},$ (6)
$\displaystyle[\mathbf{q}{t,1}^{C};\mathbf{q}{t,2}^{C};...;\mathbf{q}{t,n{h}}^{C}]=\mathbf{q}_{t}^{C}$ $\displaystyle=W^{UQ}\mathbf{c}_{t}^{Q},$ (7)
$\displaystyle[\mathbf{q}{t,1}^{R};\mathbf{q}{t,2}^{R};...;\mathbf{q}{t,n{h}}^{R}]=\mathbf{q}_{t}^{R}$ $\displaystyle=\operatorname{RoPE}({W^{QR}}\mathbf{c}_{t}^{Q}),$ (8)
$\displaystyle\mathbf{q}_{t,i}$ $\displaystyle=[\mathbf{q}{t,i}^{C};\mathbf{q}{t,i}^{R}],$ (9)

where $\mathbf{c}{t}^{Q}\in\mathbb{R}^{d{c}^{\prime}}$ is the compressed latent vector for queries; $d_{c}^{\prime}(\ll d_{h}n_{h})$ denotes the query compression dimension; $W^{DQ}\in\mathbb{R}^{d_{c}^{\prime}\times d},W^{UQ}\in\mathbb{R}^{d_{h}n_{h}\times d_{c}^{\prime}}$ are the down-projection and up-projection matrices for queries, respectively; and $W^{QR}\in\mathbb{R}^{d_{h}^{R}n_{h}\times d_{c}^{\prime}}$ is the matrix to produce the decoupled queries that carry RoPE.

Ultimately, the attention queries ($\mathbf{q}{t,i}$), keys ($\mathbf{k}{j,i}$), and values ($\mathbf{v}{j,i}^{C}$) are combined to yield the final attention output $\mathbf{u}{t}$:

$\displaystyle\mathbf{o}_{t,i}$ $\displaystyle=\sum_{j=1}^{t}\operatorname{Softmax}{j}(\frac{\mathbf{q}{t,i}^{T}\mathbf{k}{j,i}}{\sqrt{d{h}+d_{h}^{R}}})\mathbf{v}_{j,i}^{C},$ (10)
$\displaystyle\mathbf{u}_{t}$ $\displaystyle=W^{O}[\mathbf{o}{t,1};\mathbf{o}{t,2};...;\mathbf{o}{t,n{h}}],$ (11)

where $W^{O}\in\mathbb{R}^{d\times d_{h}n_{h}}$ denotes the output projection matrix.

2.1.2 DeepSeekMoE with Auxiliary-Loss-Free Load Balancing

Basic Architecture of DeepSeekMoE.

For Feed-Forward Networks (FFNs), DeepSeek-V3 employs the DeepSeekMoE architecture (Dai et al., 2024). Compared with traditional MoE architectures like GShard (Lepikhin et al., 2021), DeepSeekMoE uses finer-grained experts and isolates some experts as shared ones. Let $\mathbf{u}{t}$ denote the FFN input of the $t$-th token, we compute the FFN output $\mathbf{h}{t}^{\prime}$ as follows:

$\displaystyle\mathbf{h}_{t}^{\prime}$ $\displaystyle=\mathbf{u}{t}+\sum{i=1}^{N_{s}}{\operatorname{FFN}^{(s)}{i}\left(\mathbf{u}{t}\right)}+\sum_{i=1}^{N_{r}}{g_{i,t}\operatorname{FFN}^{(r)}{i}\left(\mathbf{u}{t}\right)},$ (12)
$\displaystyle g_{i,t}$ $\displaystyle=\frac{g^{\prime}{i,t}}{\sum{j=1}^{N_{r}}g^{\prime}_{j,t}},$ (13)
$\displaystyle g^{\prime}_{i,t}$ $\displaystyle=\begin{cases}s_{i,t},&s_{i,t}\in\operatorname{Topk}({s_{j,t}|1\leqslant j\leqslant N_{r}},K_{r}),\ 0,&\text{otherwise},\end{cases}$ (14)
$\displaystyle s_{i,t}$ $\displaystyle=\operatorname{Sigmoid}\left({\mathbf{u}{t}}^{T}\mathbf{e}{i}\right),$ (15)

where $N_{s}$ and $N_{r}$ denote the numbers of shared experts and routed experts, respectively; $\operatorname{FFN}^{(s)}{i}(\cdot)$ and $\operatorname{FFN}^{(r)}{i}(\cdot)$ denote the $i$-th shared expert and the $i$-th routed expert, respectively; $K_{r}$ denotes the number of activated routed experts; $g_{i,t}$ is the gating value for the $i$-th expert; $s_{i,t}$ is the token-to-expert affinity; $\mathbf{e}_{i}$ is the centroid vector of the $i$-th routed expert; and $\operatorname{Topk}(\cdot,K)$ denotes the set comprising $K$ highest scores among the affinity scores calculated for the $t$-th token and all routed experts. Slightly different from DeepSeek-V2, DeepSeek-V3 uses the sigmoid function to compute the affinity scores, and applies a normalization among all selected affinity scores to produce the gating values.

Auxiliary-Loss-Free Load Balancing.

For MoE models, an unbalanced expert load will lead to routing collapse (Shazeer et al., 2017) and diminish computational efficiency in scenarios with expert parallelism. Conventional solutions usually rely on the auxiliary loss (Fedus et al., 2021; Lepikhin et al., 2021) to avoid unbalanced load. However, too large an auxiliary loss will impair the model performance (Wang et al., 2024a). To achieve a better trade-off between load balance and model performance, we pioneer an auxiliary-loss-free load balancing strategy (Wang et al., 2024a) to ensure load balance. To be specific, we introduce a bias term $b_{i}$ for each expert and add it to the corresponding affinity scores $s_{i,t}$ to determine the top-K routing:

| | $\displaystyle g^{\prime}{i,t}$ | $\displaystyle=\begin{cases}s{i,t},&s_{i,t}+b_{i}\in\operatorname{Topk}({s_{j,t}+b_{j}|1\leqslant j\leqslant N_{r}},K_{r}),\ 0,&\text{otherwise}.\end{cases}$ | | (16) |

Note that the bias term is only used for routing. The gating value, which will be multiplied with the FFN output, is still derived from the original affinity score $s_{i,t}$. During training, we keep monitoring the expert load on the whole batch of each training step. At the end of each step, we will decrease the bias term by $\gamma$ if its corresponding expert is overloaded, and increase it by $\gamma$ if its corresponding expert is underloaded, where $\gamma$ is a hyper-parameter called bias update speed. Through the dynamic adjustment, DeepSeek-V3 keeps balanced expert load during training, and achieves better performance than models that encourage load balance through pure auxiliary losses.

Complementary Sequence-Wise Auxiliary Loss.

Although DeepSeek-V3 mainly relies on the auxiliary-loss-free strategy for load balance, to prevent extreme imbalance within any single sequence, we also employ a complementary sequence-wise balance loss:

$\displaystyle\mathcal{L}_{\mathrm{Bal}}$ $\displaystyle=\alpha\sum_{i=1}^{N_{r}}{f_{i}P_{i}},$ (17)
$\displaystyle f_{i}=\frac{N_{r}}{K_{r}T}\sum_{t=1}^{T}\mathds{1}$ $\displaystyle\left(s_{i,t}\in\operatorname{Topk}({s_{j,t}|1\leqslant j\leqslant N_{r}},K_{r})\right),$ (18)
$\displaystyle s^{\prime}_{i,t}$ $\displaystyle=\frac{s_{i,t}}{\sum_{j=1}^{N_{r}}s_{j,t}},$ (19)
$\displaystyle P_{i}$ $\displaystyle=\frac{1}{T}\sum_{t=1}^{T}{s^{\prime}_{i,t}},$ (20)

where the balance factor $\alpha$ is a hyper-parameter, which will be assigned an extremely small value for DeepSeek-V3; $\mathds{1}(\cdot)$ denotes the indicator function; and $T$ denotes the number of tokens in a sequence. The sequence-wise balance loss encourages the expert load on each sequence to be balanced.

Node-Limited Routing.

Like the device-limited routing used by DeepSeek-V2, DeepSeek-V3 also uses a restricted routing mechanism to limit communication costs during training. In short, we ensure that each token will be sent to at most $M$ nodes, which are selected according to the sum of the highest $\frac{K_{r}}{M}$ affinity scores of the experts distributed on each node. Under this constraint, our MoE training framework can nearly achieve full computation-communication overlap.

No Token-Dropping.

Due to the effective load balancing strategy, DeepSeek-V3 keeps a good load balance during its full training. Therefore, DeepSeek-V3 does not drop any tokens during training. In addition, we also implement specific deployment strategies to ensure inference load balance, so DeepSeek-V3 also does not drop tokens during inference.

图

Figure 3: Illustration of our Multi-Token Prediction (MTP) implementation. We keep the complete causal chain for the prediction of each token at each depth.

2.2 Multi-Token Prediction

Inspired by Gloeckle et al. (2024), we investigate and set a Multi-Token Prediction (MTP) objective for DeepSeek-V3, which extends the prediction scope to multiple future tokens at each position. On the one hand, an MTP objective densifies the training signals and may improve data efficiency. On the other hand, MTP may enable the model to pre-plan its representations for better prediction of future tokens. Figure 3 illustrates our implementation of MTP. Different from Gloeckle et al. (2024), which parallelly predicts $D$ additional tokens using independent output heads, we sequentially predict additional tokens and keep the complete causal chain at each prediction depth. We introduce the details of our MTP implementation in this section.

MTP Modules.

To be specific, our MTP implementation uses $D$ sequential modules to predict $D$ additional tokens. The $k$-th MTP module consists of a shared embedding layer $\operatorname{Emb}(\cdot)$, a shared output head $\operatorname{OutHead}(\cdot)$, a Transformer block $\operatorname{TRM}{k}(\cdot)$, and a projection matrix $M{k}\in\mathbb{R}^{d\times 2d}$. For the $i$-th input token $t_{i}$, at the $k$-th prediction depth, we first combine the representation of the $i$-th token at the $(k-1)$-th depth $\mathbf{h}{i}^{k-1}\in\mathbb{R}^{d}$ and the embedding of the $(i+k)$-th token $Emb(t{i+k})\in\mathbb{R}^{d}$ with the linear projection:

| | $\mathbf{h}{i}^{\prime k}=M{k}[\operatorname{RMSNorm}(\mathbf{h}{i}^{k-1});\operatorname{RMSNorm}(\operatorname{Emb}(t{i+k}))],$ | | (21) |

where $[\cdot;\cdot]$ denotes concatenation. Especially, when $k=1$, $\mathbf{h}{i}^{k-1}$ refers to the representation given by the main model. Note that for each MTP module, its embedding layer is shared with the main model. The combined $\mathbf{h}{i}^{\prime k}$ serves as the input of the Transformer block at the $k$-th depth to produce the output representation at the current depth $\mathbf{h}_{i}^{k}$:

| | $\mathbf{h}{1:T-k}^{k}=\operatorname{TRM}{k}(\mathbf{h}_{1:T-k}^{\prime k}),$ | | (22) |

where $T$ represents the input sequence length and i:j denotes the slicing operation (inclusive of both the left and right boundaries). Finally, taking $\mathbf{h}{i}^{k}$ as the input, the shared output head will compute the probability distribution for the $k$-th additional prediction token $P{i+1+k}^{k}\in\mathbb{R}^{V}$, where $V$ is the vocabulary size:

| | $P_{i+k+1}^{k}=\operatorname{OutHead}(\mathbf{h}_{i}^{k}).$ | | (23) |

The output head $\operatorname{OutHead}(\cdot)$ linearly maps the representation to logits and subsequently applies the $\operatorname{Softmax}(\cdot)$ function to compute the prediction probabilities of the $k$-th additional token. Also, for each MTP module, its output head is shared with the main model. Our principle of maintaining the causal chain of predictions is similar to that of EAGLE (Li et al., 2024b), but its primary objective is speculative decoding (Xia et al., 2023; Leviathan et al., 2023), whereas we utilize MTP to improve training.

MTP Training Objective.

For each prediction depth, we compute a cross-entropy loss $\mathcal{L}_{\text{MTP}}^{k}$:

| | $\mathcal{L}{\text{MTP}}^{k}=\operatorname{CrossEntropy}(P{2+k:T+1}^{k},t_{2+k:T+1})=-\frac{1}{T}\sum_{i=2+k}^{T+1}\log P_{i}^{k}[t_{i}],$ | | (24) |

where $T$ denotes the input sequence length, $t_{i}$ denotes the ground-truth token at the $i$-th position, and $P_{i}^{k}[t_{i}]$ denotes the corresponding prediction probability of $t_{i}$, given by the $k$-th MTP module. Finally, we compute the average of the MTP losses across all depths and multiply it by a weighting factor $\lambda$ to obtain the overall MTP loss $\mathcal{L}_{\text{MTP}}$, which serves as an additional training objective for DeepSeek-V3:

| | $\mathcal{L}{\text{MTP}}=\frac{\lambda}{D}\sum{k=1}^{D}\mathcal{L}_{\text{MTP}}^{k}.$ | | (25) |

MTP in Inference.

Our MTP strategy mainly aims to improve the performance of the main model, so during inference, we can directly discard the MTP modules and the main model can function independently and normally. Additionally, we can also repurpose these MTP modules for speculative decoding to further improve the generation latency.