前言
今天我们将介绍并复现 Deepmind 的一篇关于 LLM Speculative Sampling 的论文:Accelerating large language model decoding with speculative sampling1. 我们将用不到 100 行代码来复现这篇论文,并得到 2 倍以上的速度提升。
比如以当前的 Prompt 为例 (试验采用的 temperature 为 0 以保证确定性):
Prompt: Alan Turing theorized that computers would one day becomeGPT2-XLARGE 的推理的时间为 16.8244 秒:
-------------------- Naive GPT2 Auto-Regressive --------------------Naive GPT2 Auto-Regressive completed in 16.8244 sGenerated: Alan Turing theorized that computers would one day become so powerful that they would be able to think like humans.
In the 1950s, he proposed a way to build a computer that could think like a human.(skip ...)-------------------- Naive GPT2 Auto-Regressive --------------------推测采样可以生成完全一样的推理结果,且其推理的时间为 7.9031 秒,提升了 2.12 倍:
-------------------- Speculative Sampling --------------------Speculative Sampling completed in 7.9031 sGenerated: Alan Turing theorized that computers would one day become so powerful that they would be able to think like humans.
In the 1950s, he proposed a way to build a computer that could think like a human.(skip ...)-------------------- Speculative Sampling --------------------简介
Speculative Sampling 中文翻译有多个,这里我们将统一使用推测采样。 推测采样是一种大语言模型推理加速的方法。具体来说,推测采样是一种空间换时间的推理加速算法。 我们知道,在做一个参数较大的 LLM 模型推理的时候,其推理速度一般会较慢,我们希望能够优化其推理速度。 这里我们称待优化的模型为 Target Model。
为了加速其推理速度,推测采样通过引入一个参数量很小的 Draft Model 来一次做多步推理的预测, 然后通过一种类似于 Rejection Sampling(拒绝采样) 的方法来保证算法生成的分布和 Target Model 是一致的。 推测采样能够生效的关键有两点:Draft Model 的推理速度比 Target Model 要快得多; Draft Model 和 Target Model 具有较高的相似性。在后续的介绍中我们将对这两点进行展开介绍。 接下来我们来梳理一下算法的流程。 本文中的实验全部基于 GPT2 模型完成,其中 Target Model 为 GPT2-XLARGE,Draft Model 为 GPT2-SMALL。算法实现的全部代码开源在 GitHub: ai-glimpse/toyllm
算法原理
Given lookahead
and minimum target sequence length .
这里是算法的两个参数
Given auto-regressive target model
, and auto-regressive draft model , initial prompt sequence
这里 target model
<Alan>< Turing>< theor><ized>< that>< computers>< would>< one>< day>< become>下面是算法运行的主要逻辑:
总体流程很简单,就是不断的生成 token 直到生成的 token 长度达到
# Initialise n <- tn = prompt_tokens.size(0)# while n < T dowhile n < target_seq_len: ...接下来进入到 while 循环的内部,首先是第一个 for 循环。
这里的就是对 Draft Model 通过自回归进行采样,共采样
for _ in range(self.lookahead): draft_model_probs = self.draft_model.inference(draft_prompt_tokens, config) next_token_id = torch.multinomial(draft_model_probs[-1], num_samples=1) draft_prompt_tokens = torch.cat([draft_prompt_tokens, next_token_id], dim=0)之后是同时计算 target model 的
这里其实我们在 target model 进行一次 Forward就可以得到全部的需要的
target_model_probs = self.target_model.inference(draft_prompt_tokens, config)接下来就是推测采样的重头戏了。如果说前面的过程是在做出推测,那么接下来的过程就是在验证这个推测的可靠性,并根据可靠与否做出不同的处理。
下面的过程是对 draft model 推理出来的 K 个 token 进行逐一验证,我们以第一个 token 为例进行说明,此时
其实就是给定当前已经生成的 token,比较 target model 和 draft model 在 token
这里
其实就是分布做差,按 0 clip 之后再归一化的过程。
小结一下,在这
all_accept = Truefor t in range(self.lookahead): r = self.rng.random()
x = draft_generate_tokens[t] px = draft_model_probs[t, x] qx = target_model_probs[t, x]
if r < min(1.0, (qx / px).cpu().item()): next_token_id = x.unsqueeze(0) prompt_tokens = torch.cat([prompt_tokens, next_token_id], dim=0) n += 1 else: all_accept = False prob_diff = target_model_probs[t] - draft_model_probs[t] prob_diff = torch.clamp(prob_diff, min=0) prob_diff = prob_diff / torch.sum(prob_diff) next_token_id = torch.multinomial(prob_diff, num_samples=1) prompt_tokens = torch.cat((prompt_tokens, next_token_id), dim=0) n += 1 break接下来还有 while 循环内最后一小步。
如果 draft model 推测出来的
注意,这里论文中的公式应该是有些小的笔误,下面给出的是修正后的版本。下面是对应的代码实现。
if all_accept: next_token_id = torch.multinomial(target_model_probs[-1], num_samples=1) prompt_tokens = torch.cat([prompt_tokens, next_token_id], dim=0) n += 1至此,算法全部的流程就全部走完一遍了。此时再回顾一下之前的问题——为什么 draft model 生成
完整算法代码
在 toyllm项目中可以找到采用此代码实现生成前言中例子的代码。
text_id_list = self.tokenizer.encode(prompt)prompt_tokens = torch.tensor(text_id_list).to(self.target_model.device())
target_seq_len = config.max_new_tokens + len(prompt_tokens)n = prompt_tokens.size(0)while n < target_seq_len: with torch.inference_mode(): draft_prompt_tokens = prompt_tokens.clone() for _ in range(self.lookahead): draft_model_probs = self.draft_model.inference(draft_prompt_tokens, config) next_token_id = torch.multinomial(draft_model_probs[-1], num_samples=1) draft_prompt_tokens = torch.cat([draft_prompt_tokens, next_token_id], dim=0)
draft_generate_tokens = draft_prompt_tokens[-self.lookahead :] draft_model_probs = draft_model_probs[-self.lookahead :, :]
target_model_probs = self.target_model.inference(draft_prompt_tokens, config) target_model_probs = target_model_probs[-(self.lookahead + 1) :, :]
all_accept = True for t in range(self.lookahead): r = self.rng.random()
x = draft_generate_tokens[t] px = draft_model_probs[t, x] qx = target_model_probs[t, x]
if r < min(1.0, (qx / px).cpu().item()): next_token_id = x.unsqueeze(0) prompt_tokens = torch.cat([prompt_tokens, next_token_id], dim=0) n += 1 else: all_accept = False prob_diff = target_model_probs[t] - draft_model_probs[t] prob_diff = torch.clamp(prob_diff, min=0) prob_diff = prob_diff / torch.sum(prob_diff) next_token_id = torch.multinomial(prob_diff, num_samples=1) prompt_tokens = torch.cat((prompt_tokens, next_token_id), dim=0) n += 1 break
if all_accept: next_token_id = torch.multinomial(target_model_probs[-1], num_samples=1) prompt_tokens = torch.cat([prompt_tokens, next_token_id], dim=0) n += 1
generate_text = self.tokenizer.decode(prompt_tokens.tolist())复杂度分析
在原论文中,其实没有什么复杂度分析的内容,不过 Jay 对复杂度分析的部分做了补充,对于进一步理解推测采样很有帮助。
首先,我们定义以下内容:
: target model 一次推理 (产生一个 token) 需要的时间。论文中的 target model 为 DeepMind 的 70B Chinchilla 模型,推理一个 token 的耗时为 14.1ms : draft model 一次推理需要的时间。论文中的 target model 为 DeepMind 的 7B Chinchilla 模型,推理一个 token 的耗时为 1.8ms : Acceptance rate, 接受率。其计算方式为每轮推测采样 (while 循环内) 生成的 token 数量除以 .
因此,不采用推测采样的情况下,target model 进行自回归产生
在采用推测采样的情况下,需要进行多轮循环。每轮循环都涉及到 draft model 的
有了两种方式的时间复杂度,我们就可以算出采样推测采样理论上可以达到的加速倍数:
观察最后的等式,回顾之前提到的推测采样之所以能够加速的两个关键点:draft Model 的推理速度比 target model 要快得多;draft model 和 target model 具有较高的相似性。首先看第一点,draft model 越快,即
也就是说,加速比与接受率成线性正相关。所以这就引出第二点,即要求 draft model 和 target model 具有较高的相似性。这里的相似性越高,
下面我们再从试验的角度来分析一下算法的加速比是否符合预期。论文在 HumanEval 和 XSum 分别做了测试 (
同样地,我们也对前面的例子进行一下复杂度的计算。我们在例子中的 target model 为 GPT-XLARGE(1558M 参数量),其推理速度为 73.3ms/token,即
而我们实测的加速比为 2.12(注意这里我们只是用一个 prompt 做个简单的验算,并没有做严格的 benchmark),总体还是可以的。
Insight
到这里为止,我们已经把论文中算法的原理梳理清楚,并用代码从头复现了论文,而且最后做了复杂度分析。看起来我们已经完成了复现,但是我在进行到这里的是否总感觉并未“完成”——因为我仍然没有得到对 Speculative Sampling 预期的 insight。或者说,我不觉着自己真正理解了 Speculative Sampling 本身,我知道了算法的 What 和 How,但是对算法为什么真的可以 work 仍然有些说不清的疑惑。所以我试图从别的角度来深入挖掘一下算法本身。
第一个尝试是做数学证明。论文最后给出了当前推测采样得到的分布和 target model 采样得到的分布一致的证明。这个证明很简单,也很容易理解。但是推导完成后对理解算法的本质好像帮助不大。
第二个尝试是深入算法的细节。总览全部算法流程,其实最令人迷惑的地方还是拒绝采样的部分:
怎么直观理解这里的采纳条件呢?与此同时,还有另外一个问题,推测采样只保证了分布的相似性,为什么我们之前的测试可以得到完全一致的输出呢?
其实把这两个问题结合起来看,一些 insight 就呼之欲出了。这里的关键在于我们将推理时的 temperature 设置为 0. 当 temperature 为 0 的时候,
也就是说,这时候采纳的条件退化为
让我们再深入一些,放开 temperature 为 0 的限制,回归到更加普遍的场景下看这个采纳条件:
可以看到这样的性质:对于 draft model 推测得到的 token
好了,到了这里相信大家对算法的理解又深入了一个层次,我们此次旅程也即将达到终点!最后我来讲一个偶然想到的好玩的问题,供大家讨论娱乐~
我们其实可以调节这里
其代价就是采样出来的分布不再和 targe model 一致,而是会更加偏向于 draft model,是为“降智”。这种降智来得其实很简单,工程上实现起来也很灵活,比如给某些地区/某些用户都单独配置一个自定义的
结语
我们本次旅程到此就结束了,祝大家学习愉快!
Footnotes
Charlie Chen et al., “Accelerating Large Language Model Decoding with Speculative Sampling,” arXiv Preprint arXiv:2302.01318, 2023.↩