| Model | Organization | Date | Size (# params) |
|---|---|---|---|
| ELMo | AI2 | Feb 2018 | 94,000,000 |
| GPT | OpenAI | Jun 2018 | 110,000,000 |
| BERT | Oct 2018 | 340,000,000 | |
| XLM | Jan 2019 | 655,000,000 | |
| GPT-2 | OpenAI | Mar 2019 | 1,500,000,000 |
| RoBERTa | Jul 2019 | 355,000,000 | |
| Megatron-LM | NVIDIA | Sep 2019 | 8,300,000,000 |
| T5 | Oct 2019 | 11,000,000,000 | |
| Turing-NLG | Microsoft | Feb 2020 | 17,000,000,000 |
| GPT-3 | OpenAI | May 2020 | 175,000,000,000 |
| Megatron-Turing NLG | Microsoft, NVIDIA | Oct 2021 | 530,000,000,000 |
| Gopher | DeepMind | Dec 2021 | 280,000,000,000 |
相关benchmark:
有害性(上)
有害性(下)



好的分词:
分词方法:
text.split(' '),最简单的方法,如果是英文是可行,但是中文词语之间没有空格,即使在英语,也有连字符词(例如father-in-law)和缩略词(例如don’t),它们需要被正确拆分。例如,Penn Treebank将don’t拆分为do和n’t,这是一个在语言上基于信息的选择,但不太明显。因此,仅仅通过空格来划分单词会带来很多问题。
【栗子】
假设我们有一个英文语料库,其中包含以下两个句子:
现在,我们想要使用这些句子来构建一个子词词表,以便进行分词。
BPE(Byte Pair Encoding):
使用 BPE 进行分词时,我们首先将每个句子划分为单个字符,得到以下字符序列:
“I like playing soccer.”
“I like playing basketball.”
然后,我们计算字符序列中最频繁出现的字符组合,并将其合并为一个新的子词。在这个例子中,“pl” 是最频繁出现的字符组合,我们将其合并为一个新的子词,得到以下序列:
“I like playing soccer.”
“I like playing basketball.”
通过 BPE,我们将 “pl” 作为一个子词,成功地将 “playing” 分割为 “play” 和 “ing”。
WordPiece:
对于 WordPiece 分词,我们也首先将每个句子划分为单个字符。然后,我们根据字符序列的频率和语言模型得分来选择合并操作。
对于上述例子,我们计算字符序列的频率,并选择最频繁出现的合并操作。假设我们选择合并 “pl”:
“I like playing soccer.”
“I like playing basketball.”
通过 WordPiece,我们成功地将 “playing” 分割为 “p”, “lay” 和 “ing”。
总结:
上下文向量表征 (Contextual Embedding)举例如下,标记的上下文向量表征取决于其上下文(周围的单词);例如,考虑mouse的向量表示需要关注到周围某个窗口大小的其他单词:
[
t
h
e
,
m
o
u
s
e
,
a
t
e
,
t
h
e
,
c
h
e
e
s
e
]
⇒
ϕ
[
(
1
0.1
)
,
(
0
1
)
,
(
1
1
)
,
(
1
−
0.1
)
,
(
0
−
1
)
]
.
[the, mouse, ate, the, cheese] \stackrel{\phi}{\Rightarrow}\left[\left(
单头注意力矩阵形式:
def
A
t
t
e
n
t
i
o
n
(
x
1
:
L
:
R
d
×
L
,
y
:
R
d
)
→
R
d
Attention(x_{1:L}:ℝ^{d×L},y:ℝ^d)→ℝ^d
Attention(x1:L:Rd×L,y:Rd)→Rd:
多头注意力机制:
def
M
u
l
t
i
H
e
a
d
e
d
A
t
t
e
n
t
i
o
n
(
x
1
:
L
:
R
d
×
L
,
y
:
R
d
)
→
R
d
MultiHeadedAttention(x_{1:L}:ℝ^{d×L},y:ℝ^{d})→ℝ^{d}
MultiHeadedAttention(x1:L:Rd×L,y:Rd)→Rd:
对于自注意层,我们将用 x i x_{i} xi替换 y y y作为查询参数来产生,其本质上就是将自身的 x i x_{i} xi对句子的其他上下文内容进行 A t t e n t i o n Attention Attention的运算:
def S e l f A t t e n t i o n ( x 1 : L : R d × L ) → R d × L ) SelfAttention(x_{1:L}:ℝ_{d×L})→ℝ_{d×L}) SelfAttention(x1:L:Rd×L)→Rd×L):
自注意力使得所有的标记都可以“相互通信”,而前馈层提供进一步的连接:
def F e e d F o r w a r d ( x 1 : L : R d × L ) → R d × L FeedForward(x_{1:L}:ℝ^{d×L})→ℝ^{d×L} FeedForward(x1:L:Rd×L)→Rd×L:

(1)残差链接:添加残差(跳跃)连接,防止
f
f
f梯度消失时还可以使用
x
1
:
L
x_{1:L}
x1:L计算
(2)层归一化:def
L
a
y
e
r
N
o
r
m
(
x
1
:
L
:
R
d
×
L
)
→
R
d
×
L
LayerNorm(x_{1:L}:ℝ^{d×L})→ℝ^{d×L}
LayerNorm(x1:L:Rd×L)→Rd×L,具体而言定义函数接受一个序列模型
f
f
f并使其“鲁棒”:
def A d d N o r m ( f : ( R d × L → R d × L ) , x 1 : L : R d × L ) → R d × L AddNorm(f:(ℝd^{×L}→ℝ^{d×L}),x_{1:L}:ℝ_{d×L})→ℝ^{d×L} AddNorm(f:(Rd×L→Rd×L),x1:L:Rd×L)→Rd×L:
最后定义Transformer块如下:
def T r a n s f o r m e r B l o c k ( x 1 : L : R d × L ) → R d × L TransformerBlock(x_{1:L}:ℝ^{d×L})→ℝ^{d×L} TransformerBlock(x1:L:Rd×L)→Rd×L:
(3)位置嵌入
def
E
m
b
e
d
T
o
k
e
n
W
i
t
h
P
o
s
i
t
i
o
n
(
x
1
:
L
:
R
d
×
L
)
EmbedTokenWithPosition(x_{1:L}:ℝ^{d×L})
EmbedTokenWithPosition(x1:L:Rd×L):
注意:现在LLM大模型还有使用ROPE旋转位置编码、ALiBi位置编码等扩大窗口大小。
(1)GPT3架构:
(2)llama模型架构
自回归语言模型的条件分布 p ( x i ∣ x 1 : i − 1 ) p(x_i \mid x_{1:i-1}) p(xi∣x1:i−1):
最大似然:设
θ
\theta
θ是大语言模型的所有参数。设
D
D
D是由一组序列组成的训练数据。然后可以遵循最大似然原理,定义以下负对数似然目标函数:
O
(
θ
)
=
∑
x
∈
D
−
log
p
θ
(
x
)
=
∑
x
∈
D
∑
i
=
1
L
−
log
p
θ
(
x
i
∣
x
1
:
i
−
1
)
.
O(\theta) = \sum_{x \in D} - \log p_\theta(x) = \sum_{x \in D} \sum_{i=1}^L -\log p_\theta(x_i \mid x_{1:i-1}).
O(θ)=x∈D∑−logpθ(x)=x∈D∑i=1∑L−logpθ(xi∣x1:i−1).
已知:自回归语言模型的目标函数: O ( θ ) = ∑ x ∈ D − log p θ ( x ) . O(\theta) = \sum_{x \in D} -\log p_\theta(x). O(θ)=x∈D∑−logpθ(x).
Adam算法拥有以下两个创新:
它的步骤如下:
初始化参数 θ 0 \theta_0 θ0
初始化动量 m 0 , v 0 ← 0 m_0, v_0 \leftarrow 0 m0,v0←0
重复以下步骤:
采样小批量 B t ⊂ D B_t \subset D Bt⊂D
按照如下步骤更新参数:
g t ← 1 ∣ B t ∣ ∑ x ∈ B t ∇ θ ( − log p θ ( x ) ) . g_t \leftarrow \frac{1}{|B_t|} \sum_{x \in B_t} \nabla_\theta (-\log p_\theta(x)). gt←∣Bt∣1x∈Bt∑∇θ(−logpθ(x)).
m t ← β 1 m t − 1 + ( 1 − β 1 ) g t v t ← β 2 v t − 1 + ( 1 − β 2 ) g t 2 m_t \leftarrow \beta_1 m_{t-1} + (1 - \beta_1) g_t \\ v_t \leftarrow \beta_2 v_{t-1} + (1 - \beta_2) g_t^2 mt←β1mt−1+(1−β1)gtvt←β2vt−1+(1−β2)gt2
m ^ t ← m t / ( 1 − β 1 t ) v ^ t ← v t / ( 1 − β 2 t ) \hat m_t \leftarrow m_t / (1 - \beta_1^t) \\ \hat v_t \leftarrow v_t / (1 - \beta_2^t) m^t←mt/(1−β1t)v^t←vt/(1−β2t)
θ t ← θ t − 1 − η m ^ t / ( v ^ t + ϵ ) . \theta_t \leftarrow \theta_{t-1} - \eta \, \hat m_t / (\sqrt{\hat v_t} + \epsilon). θt←θt−1−ηm^t/(v^t+ϵ).
存储占用分析:
Adam将存储从2倍的模型参数( θ t , g t \theta_t,g_t θt,gt)增加到了4倍( θ t , g t , m t , v t \theta_t,g_t,m_t,v_t θt,gt,mt,vt)。
AdaFactor是一种为减少存储占用的优化算法。特点:
以GPT-3为例,使用的参数如下:
【背景】假设神经网络中某一层是做矩阵乘法, 其中的输入
x
x
x 的形状为
4
×
5
4 \times 5
4×5, 模型参数
w
w
w 的形状为
5
×
8
5 \times 8
5×8, 那么, 矩阵乘法输出形状为
4
×
8
4 \times 8
4×8 。如下:

数据并行:切分数据 x x x到不同设备上,在反向传播过程中,需要对各个设备上的梯度进行 AllReduce,以确保各个设备上的模型始终保持一致。适合数据较大,模型较小的情况,如resnet50

模型并行:省去了多个设备之间的梯度 AllReduce;但是, 由于每个设备都需要完整的数据输入,因此,数据会在多个设备之间进行广播,产生通信代价。比如,上图中的最终得到的 out
(
4
×
8
)
(4 \times 8)
(4×8) ,如果它作为下一层网络的输入,那么它就需要被广播发送到两个设备上。如bert

流水并行:模型网络过大,除了用模型并行,还能用流水并行(如下的网络有4层,T1-T4)

混合并行:综合上面的多种策略一起训练,如GPT3:
参考:
参考资料:
参考资料:
预训练语言模型(Pre-trained LM):
用参数
θ
L
M
θLM
θLM表示
下游任务数据集(Downstream Task Dataset):
下游任务分布
P
t
a
s
k
P_{task}
Ptask的样本数据。可以是文本分类、情感分析等任务的特定实例,每个样本由输入x和目标输出y组成,如:
(
x
(
1
)
,
y
(
1
)
)
,
…
,
(
x
(
n
)
,
y
(
n
)
)
\left(x^{(1)}, y^{(1)}\right), \ldots,\left(x^{(n)}, y^{(n)}\right)
(x(1),y(1)),…,(x(n),y(n))。
适配参数(Adaptation Parameters):
为了使预训练的LM适合特定的下游任务,现在需要找到一组参数
γ
\gamma
γ,这组参数可以来自现有参数的子集或引入的新的参数,
Γ
\Gamma
Γ。这些参数将用于调整模型,以便它在特定任务上的表现更好。
任务损失函数(Task Loss Function):
定义一个损失函数
ℓ
task
\ell_{\text {task }}
ℓtask 来衡量模型在下游任务上的表现。例如,交叉熵损失是一种常见的选择,用于衡量模型预测的概率分布与真实分布之间的差异。
优化问题(Optimization Problem):
目标是找到一组适配参数
γ
adapt
\gamma_{\text {adapt }}
γadapt ,使得任务损失在整个下游数据集上最小化。数学上,这可以通过以下优化问题表示:
γ
adapt
=
argmin
γ
∈
Γ
1
n
∑
i
=
1
n
ℓ
task
(
γ
,
θ
L
M
,
x
i
,
y
i
)
.
\gamma_{\text {adapt }}=\operatorname{argmin}_{\gamma \in \Gamma} \frac{1}{n} \sum_{i=1}^n \ell_{\text {task }}\left(\gamma, \theta_{\mathrm{LM}}, x_i, y_i\right) .
γadapt =argminγ∈Γn1i=1∑nℓtask (γ,θLM,xi,yi).
最后取得一组适配参数
γ
adapt
\gamma_{\text {adapt }}
γadapt ,用于参数化适配后的模型
p
a
d
a
p
t
p_{adapt}
padapt。
人类对齐的fine-tuning

InstructGPT对GPT-3模型进行微调的三个步骤:
收集人类书写的示范行为:这一步骤涉及收集符合人类期望的示例,并对这些示例进行监督微调。
基于指令的采样与人类偏好:对于每个指令,从步骤1的LM中采样k个输出。然后收集人类对哪个采样输出最优先的反馈。与步骤1相比,这些数据更便宜。
使用强化学习目标微调LM:通过强化学习目标微调步骤1中的LM,以最大化人类偏好奖励。
经过这样的微调,1.3B的InstructGPT模型在85%的时间里被优先于175B的GPT-3,使用少样本提示时为71%。在封闭领域的问答/摘要方面,InstructGPT 21%的时间会产生虚构信息,相比GPT-3的41%有所改善。在被提示要尊重时,InstructGPT比GPT-3减少了25%的有毒输出。
peft库(Parameter-Efficient Fine-Tuning)进行微调,支持如下tuning:

Lightweight Fine-tuning节省资源的同时保持和全参微调相同的效果:
Patterson et al., 2021
简单形式:
emissions
=
R
power
→
emit
(
energy-train
+
queries
⋅
energy-inference
)
\text{emissions} = R_{\text{power} \to \text{emit}} (\text{energy-train} + \text{queries} \cdot \text{energy-inference})
emissions=Rpower→emit(energy-train+queries⋅energy-inference)
对于训练:
emissions = hours-to-train ⋅ num-processors ⋅ power-per-processor ⋅ PUE ⋅ R power → emit \text{emissions} = \text{hours-to-train} \cdot \text{num-processors} \cdot \text{power-per-processor} \cdot \text{PUE} \cdot R_{\text{power} \to \text{emit}} emissions=hours-to-train⋅num-processors⋅power-per-processor⋅PUE⋅Rpower→emit
不同模型的估计值:
[1] 斯坦福大学CS324课程:https://stanford-cs324.github.io/winter2022/lectures/introduction/#a-brief-history
[2] CS224N lecture notes on language models
[3] Language Models are Few-Shot Learners. Tom B. Brown, Benjamin Mann, Nick Ryder, Melanie Subbiah, J. Kaplan, Prafulla Dhariwal, Arvind Neelakantan, Pranav Shyam, Girish Sastry, Amanda Askell, Sandhini Agarwal, Ariel Herbert-Voss, Gretchen Krueger, T. Henighan, R. Child, A. Ramesh, Daniel M. Ziegler, Jeff Wu, Clemens Winter, Christopher Hesse, Mark Chen, Eric Sigler, Mateusz Litwin, Scott Gray, Benjamin Chess, Jack Clark, Christopher Berner, Sam McCandlish, Alec Radford, Ilya Sutskever, Dario Amodei. NeurIPS 2020.
[4] Challenges in Detoxifying Language Models. Johannes Welbl, Amelia Glaese, Jonathan Uesato, Sumanth Dathathri, John F. J. Mellor, Lisa Anne Hendricks, Kirsty Anderson, P. Kohli, Ben Coppin, Po-Sen Huang. EMNLP 2021.
[5] Scaling Language Models: Methods, Analysis&Insights from Training Gopher
[5] CommonCrawl
[6] OpenWebText Similar to WebText, used to train GPT-2.
[7] An Empirical Exploration in Quality Filtering of Text Data. Leo Gao. 2021.
[8] Deduplicating Training Data Makes Language Models Better. Katherine Lee, Daphne Ippolito, Andrew Nystrom, Chiyuan Zhang, D. Eck, Chris Callison-Burch, Nicholas Carlini. 2021.
[9] Foundation models report (legality section)
[10] A Survey of Large Language Models:http://arxiv.org/abs/2303.18223
[11] Attention is All you Need. Ashish Vaswani, Noam M. Shazeer, Niki Parmar, Jakob Uszkoreit, Llion Jones, Aidan N. Gomez, Lukasz Kaiser, Illia Polosukhin. NIPS 2017.
[12] CS224N slides on Transformers
[13] Rethinking Attention with Performers. K. Choromanski, Valerii Likhosherstov, David Dohan, Xingyou Song, Andreea Gane, Tamás Sarlós, Peter Hawkins, Jared Davis, Afroz Mohiuddin, Lukasz Kaiser, David Belanger, Lucy J. Colwell, Adrian Weller. ICLR 2020. Introduces Performers.
[14] Efficient Transformers: A Survey. Yi Tay, M. Dehghani, Dara Bahri, Donald Metzler. 2020.
[15] BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding. Jacob Devlin, Ming-Wei Chang, Kenton Lee, Kristina Toutanova. NAACL 2019. Introduces BERT from Google.
[16] RoBERTa: A Robustly Optimized BERT Pretraining Approach. Yinhan Liu, Myle Ott, Naman Goyal, Jingfei Du, Mandar Joshi, Danqi Chen, Omer Levy, M. Lewis, Luke Zettlemoyer, Veselin Stoyanov. 2019. Introduces RoBERTa from Facebook.
[17] BART: Denoising Sequence-to-Sequence Pre-training for Natural Language Generation, Translation, and Comprehension. M. Lewis, Yinhan Liu, Naman Goyal, Marjan Ghazvininejad, Abdelrahman Mohamed, Omer Levy, Veselin Stoyanov, Luke Zettlemoyer. ACL 2019. Introduces BART from Facebook.
[18] Language Models are Few-Shot Learners. Tom B. Brown, Benjamin Mann, Nick Ryder, Melanie Subbiah, J. Kaplan, Prafulla Dhariwal, Arvind Neelakantan, Pranav Shyam, Girish Sastry, Amanda Askell, Sandhini Agarwal, Ariel Herbert-Voss, Gretchen Krueger, T. Henighan, R. Child, A. Ramesh, Daniel M. Ziegler, Jeff Wu, Clemens Winter, Christopher Hesse, Mark Chen, Eric Sigler, Mateusz Litwin, Scott Gray, Benjamin Chess, Jack Clark, Christopher Berner, Sam McCandlish, Alec Radford, Ilya Sutskever, Dario Amodei. NeurIPS 2020. Introduces GPT-3 from OpenAI.
[19] Fixing Weight Decay Regularization in Adam. I. Loshchilov, F. Hutter. 2017. 介绍了AdamW
[20] 混合精度训练
[21] 通俗易懂讲解大模型:Tokenizer
[22] huggingface官方文档解释:Summary of the tokenizers
[23] 微软guidance:https://github.com/guidance-ai/guidance#token-healing-notebook
[24] Understanding Byte-Pair Encoding (BPE) Word Piece And Unigram
[25] 【西湖大学 张岳老师|自然语言处理在线课程 第十六章 - 4节】BPE(Byte-Pair Encoding)编码
[26] 【LLM系列之Tokenizer】如何科学地训练一个LLM分词器
[27] 大词表语言模型在续写任务上的一个问题及对策. 苏剑林(比如LLM使用超大词表后,“白云”、“白云山”、“白云机场”都是一个独立的token时,用户输入“广州的白云”后,接下来也几乎不会续写出“广州的白云机场”、“广州的白云山”)
[28] 随机分词浅探:从Viterbi Decoding到Viterbi Sampling. 苏剑林
[29] Subword Regularization: Improving Neural Network Translation Models with Multiple Subword Candidates:https://arxiv.org/abs/1804.10959
[30] 常见的分布式并行策略.oneflow
| 任务信息 | 截止时间 | 注意事项 |
|---|---|---|
| task1:引言 | 9.11周一 | 完成 |
| task2:大模型的能力篇 | 9.12周二 | 完成 |
| task3:大模型的有害性 | 9.13周三 | 完成 |
| task4:大模型的数据篇 | 9.14周四 | 完成 |
| task5:大模型的法律篇 | 9.15周五 | 完成 |
| task6:模型架构篇 | 9.16周六 | 完成 |
| task7:模型训练篇 | 9.17周日 | 完成 |
| task8:分布式训练篇 | 9.18周一 | |
| task9:新的模型架构篇 | 9.19周二 | |
| task10:大模型之adaptation | 9.20周三 | |
| task11:大模型之环境影响 | 9.21周四 | 完成 |