forked from cotes2020/jekyll-theme-chirpy
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0c87139
commit 0fe83fd
Showing
7 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
--- | ||
title: "Transformer 笔记" | ||
date: 2025-02-02 08:00:00 +0800 | ||
published: true | ||
mermaid: true | ||
math: true | ||
categories: [技术] | ||
tags: [AI, transformer] | ||
--- | ||
|
||
## RNN 介绍 | ||
|
||
RNN 实际完成的就是从一个序列到另一个序列。对于自然语言,序列的单位就是一个词,序列中的某个词 t 有一个隐藏状态 $h_t$ ,它是由 t 本身和前一个词的隐藏状态 $h_{t-1}$ 作为参数来生成的,所以该隐藏状态能够包含之前所有词的信息(上下文)。 | ||
|
||
缺点: | ||
|
||
1. 较难进行并行化处理,无法充分利用 GPU。 | ||
2. 当上下文信息很长是,隐藏状态需要占用大量内存空间。 | ||
|
||
## 架构 | ||
|
||
自然语言翻译一般使用 encoder-decoder 模型。 | ||
|
||
encoder 就是将一个序列 $(x_1,x_2,...,x_n)$ 转为计算机可以识别的 Tensor 序列 $ z = (z_1,z_2,...,z_n)$。比如 $x_t$ 是一个英文单词,$z_t$ 就是和该单词关联的一个向量。 | ||
|
||
decoder 就是将 encoder 的输出作为其输入,转为 $(y_1,y_2,...,y_m)$ 序列。长度可能和输入序列不同,比如英文翻译为中文。 | ||
|
||
这样的模型也是 auto-regressive(自回归) 的。每一次的输出都会使用之前的输出作为输入。 | ||
|
||
 | ||
|
||
本架构图中,左边是 encoder ,右边是 decoder。注意到右边下面的 output 作为输入说明它是 auto-regressive 的。Nx 表示方框内的同样的层有 N 个。“Add & Norm” 层有一个输入是跳过它之前的层的,这个就是残差连接(residual connection)。 | ||
|
||
### Encoder | ||
|
||
简化的表示形式是 $LayerNorm(x + Sublayer(x))$。比如第一部分,Sublayer 就是 Multi-Head Attiontion 层,加上 x 表示是残差连接需要原始数据作为输入,这两个参数作为输入传递到 (Add & Norm) 层,上面一部分也是同理。 | ||
|
||
其中的 Normalization(标准化) 使用的是 LayerNorm 而不是 BatchNorm。 | ||
|
||
> Normalization(标准化)是让一组数据减去其均值再除以标准差得到的,得到的结果就是均值为 0 ,标准差为 1 的一组数据。通过这种方式让每一层的输出都相对标准化,类似于应用间进行数据通信的 API。 | ||
BatchNorm 是将一个 batch 内不同样本间的同一个的特征进行标准化,而 LayerNorm 是将一个 batch 内同一个样本的所有特征进行标准化。 | ||
|
||
### Decoder | ||
|
||
decoder 相比于 Encoder 增加了一个 Masked Multi-Head Attention 层,该层用于实现 auto-regressive ,用于处理之前的输出结果来作为输入。因为 auto-regressive 的机制是将已经产生过的输出作为输入来提高预测的准确性,所以我们在训练时不希望让其看到完整的输出(训练时因为已经有完成的训练集了,可以看到完整的输出,但我们希望让其模拟在推理时将产生的输出作为输入的过程),所以增加一个 Mask 去遮盖后面的部分。 | ||
|
||
## Attention | ||
|
||
注意力函数需要一个 query 和 一组 key-value 作为输入,它会根据 query 和 key 的相似度作为权重将对应的 value 进行相加。不同的注意力函数的区别就是计算相似度的**相似函数**。 | ||
|
||
在上面的模型图中, 下面的两个 Attention 层有三个输入,也就是 query、key、value,不过这三个参数其实是原始输入序列的三份相同的复制。此时对于 query 中的一项,也就是原始序列的一个词,计算注意力函数时它和 key 中相同位置的自身相似度肯定最高,权重也最高,然后如果序列中还有其他相似度较高的词,也会被考虑进来(较高权重),最后得到输出。这就是 self-attention (自注意力)机制。自注意力机制实现了为 序列中的每一个词 附加同一个序列中其他它感兴趣的部分的信息。 | ||
|
||
上面的 Attention 层,它的 key ,value 来自于 Encoder 层,query 来自于下面的 Masked Multi-Head Attention 层。Encoder 和 Decoder 沟通的桥梁就是这个层。 | ||
|
||
### Multi-Head Attention | ||
|
||
将 query 、key 和 value 都投影到各自的 h 个低维空间中,再进行 Attention 操作,可以增加可学习的参数数量(增加了 3\*h)。投影的概念就是降低维度,比如三维物体的三视图就是降低一个维度的投影,通过 3 个二维图来展现三维物体。 | ||
|
||
通过在不同的维度分别计算 Attention 后再进行组合,有什么好处?首先以三视图举例子,在三维物体中的一个点,投影到三个平面上,会出现3个点,原来我们是去三维空间找两点(query 和 key)的距离信息(attention计算value的权重),现在是在三个平面上分别去找这6个点的距离信息($query_x$和$key_x$,$query_y$和$key_y$,$query_z$和$key_z$),而且我们会为每个平面分配权重,这样假设 y 平面上的两个点($query_y$和$key_y$)距离很远,而在其他平面上距离较近,但因为我们并不关心 y 平面也就是权重较小,那么这样计算出来的两个点间的最终距离就会很小,符合我们的预期。换个说法就是分成多个通道去找特征。 | ||
|
||
## Position-wise Feed-Forward Networks | ||
|
||
Feed-Forward 层实际就是一个 MLP,它对输入序列的每个词单独进行 MLP 的计算。因为其输入是 attention 层的输出,而通过 attention 层的处理,每个词都已经被附加了整个原始序列中它感兴趣的部分(与之相关的部分),所以不需要像 RNN 一样把前一个词的 MLP 计算结果作为输入,只要每个词单独输入就行,没有任何依赖。 | ||
|
||
## Embeddings and SOftmax | ||
|
||
Embedding 就是将输入数据映射成计算机可以进行计算的向量,这里固定每个词映射成 512 长度的向量。 | ||
|
||
## Positional Encoding | ||
|
||
attention 机制带来的一个问题是其注意力信息是位置无关的,因为权重信息中没有位置信息,这也导致在翻译过程中将输入的词任意打乱,其注意力信息也是相同的,即使语义已经发生了很大的变化。 | ||
|
||
此时该模型在输入中加入了位置信息来解决该问题。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
--- | ||
title: "DeepSeek 笔记" | ||
date: 2025-02-03 08:00:00 +0800 | ||
published: true | ||
mermaid: true | ||
math: true | ||
categories: [技术] | ||
tags: [AI, transformer] | ||
--- | ||
|
||
## DeepSeekMoE | ||
|
||
分为 Shared Expert(常驻) 和 Router Expert(共享) | ||
|
||
Router Expert 会被分为几组,通过 Router 进行负载均衡,将问题分配到合适的组中。这是为了使用多节点训练是避免通信消耗,一般一个节点存放一组专家,同一个问题放在一个节点上处理,避免了多节点间的通信。 | ||
|
||
多节点间的前向和反向传播(pipeline parallel 流水线并行): | ||
|
||
 | ||
|
||
> 数据就像流水线一样在不同节点间通过,每个节点处理完一个数据就可以立即处理下一个数据,就像工厂里的流水线一样,每道工序都饱和运转。 | ||
多节点间同 batch 反向传播时的梯度整合(Data parallel 数据并行): | ||
|
||
 | ||
|
||
## Multi-Token Prediction | ||
|
||
 | ||
|
||
Token Prediction 的意图是通过当前 token 预测下一个(组) token,图中最左边输入为 $t_1$ 到 $t_4$ ,参考的结果为 $t_2$ 到 $t_5$,也就是用 $t_1$ 预测 $t_2$ ,以此类推。但仅有最左边部分无法做到让 $t_1$ 预测 $t_3$,换句话说,$t_1$ 无法为预测 $t_3$ 做出贡献。于是添加了后面几个层,这些层都是用前一个层的输出作为输入,所以 $t_1$ 的信息被不断传递,每个 Cross-Entropy Loss 都会被用于改进最左边的主模型的权重。 | ||
|
||
## 专家负载均衡 | ||
|
||
训练过程记录每个 Router Expert 的负载率,仅激活负载率较低且与问题较相关的专家。 | ||
|
||
## 强化学习 | ||
|
||
### Reward Model | ||
|
||
使用 DeepSeek v3 的一个检查点开始训练专用的 Reward Model ,用来给主模型进行强化训练,奖励规则: | ||
|
||
1. 代码、数学、agent等有明确过程和结果的任务,无需使用Reward Model | ||
2. 自由形式,但有明确结果的任务,由 | ||
|
||
|
||
> agent 就是模拟人类解决现实问题的方式执行对应步骤解决问题,也就是说它也要能拆分问题、运用工具、查找资料、进行决策等 | ||
## 参考 | ||
|
||
- [【深度学习】【分布式训练】一文捋顺千亿模型训练技术:流水线并行、张量并行和 3D 并行](https://zhuanlan.zhihu.com/p/617087561) | ||
- [Deepseek v3 技术报告万字硬核解读](https://zhuanlan.zhihu.com/p/16323685381) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+50.5 KB
assets/img/2025-02-03-deepseek/v2-20bf8bdded50d48b114381783a43de76_b.webp
Binary file not shown.