跳至主要內容

Instruction Tuning 后时代的模型笔记(三)

Kevin 吴嘉文大约 10 分钟知识笔记NLPAIGC

在前两篇笔记中,笔者记录了早些年的部分指令微调模型,他们大多数使用大规模的,多种类的公开数据集进行训练。在接下来的笔记中,我们会对 LLaMa 系列的一些指令微调模型进行整理,包括 Alpaca, Vicuna, WizardLM, WizardVicunaLM 等。

笔记 - Instruction Tuning 时代的模型 83 赞同 · 6 评论文章open in new window

笔记 - Instruction Tuning 时代的模型 (二)44 赞同 · 1 评论文章open in new window

InstructGPT

在 ChatGPT 刚出来时,不少网文宣称 ChatGPT 使用的技术就是 InstructGPTopen in new window。印象中 InstructGPT 论文提出时大概是 21 年底至 2022 年初,当时 instruction tuning 的一系列模型,如 FLAN,T0,Zero-Prompt,FLAN-T5 等,大多主张在大规模的,多种类的公开数据集上进行指令微调。InstructGPT 的数据集是人工标注的,并且对数据质量要求高。此外,InstructGPT 还使用了 RLHF 对模型进行对齐训练open in new window

相比于人工收集数据,以下的几个模型都选择使用 GPT-4 来代替人工,生成模型的训练数据集。

Alpaca

standford_alpacaopen in new window 对 LLaMa 进行了微调,并尝试使用 self-instruct 方法对 gpt-turbo-3.5 进行蒸馏学习。

数据: Alpaca 修改了 self-instructopen in new window 提供的方案,并使用 OPENAI API 生成了 52k 的 instruction tuningopen in new window 数据集,共花费 $500。Alpaca 的 self-instruct 方案相对简单一些,大致使用的 prompt 为:

You are asked to come up with a set of 20 diverse task instructions. These task instructions will be given to a GPT model and we will evaluate the GPT model for completing the instructions.

Here are the requirements:
1. Try not to repeat the verb for each instruction to maximize diversity.
2. The language used for the instruction also should be diverse. For example, you should combine questions with imperative instructions.
... 这里会有一些其他的限制
9. The output should be an appropriate response to the instruction and the input. Make sure the output is less than 100 words.

List of 20 tasks:

###
1. Instruction: Is there anything I can eat for a breakfast that doesn't include eggs, yet includes protein, and has roughly 700-1000 calories?
1. Inputs: <noinput>
1. Output: Yes, you can have 1 oatmeal banana protein shake and 4 strips of bacon. The oatmeal banana protein shake may contain 1/2 cup oatmeal, 60 grams whey protein powder, 1/2 medium banana, 1tbsp flaxseed oil and 1/2 cup water, totalling about 550 calories. The 4 strips of bacon contains about 200 calories.

###
2. Instruction: Generate an appropriate subjective title for the following email:
2. Inputs: Hi [person name],I'm writing to ask you if you are happy to be a panelist of our workshop on multimodality at CVPR. The workshop will be held on June 20, 2023. \n\nBest,\n[my name]
2. Output: Invitition to be a panelist for CVPR 2023 workshop on Multimodality

### 
3. Instruction: 

上述 prompt 模板中,只给了两个案例(在 self-instruct 中称为 qa_seed),实际上 Alpaca 默认的配置是使用 4 个 seed example 来尝试生成 16 个任务。

基于以上的 prompt,每次发送 OPENAI API 请求后,GPT-4 或者 gpt-turbo-3.5 会续写 10+ 个相关的新任务和输出。

训练: 基于 LLaMa 7B 和 13B 进行了全参数微调open in new window,优化目标和传统的 CasualLM 优化目标一样。微调总共训练 3 个 epochopen in new window,在 8 个 80 GB A100 上面训练 3 个小时。

效果: 官方仓库中并没有很严谨的评判,仅让 5 位裁判员根据几个示例样本来对 Alpaca,llama 还有 text-davinci-003 进行打分。参考 HF LLM Leaderboardopen in new window 还有 chat Arenaopen in new window,Alpaca 相对 LLaMa 的提升还是有限。

其他: Alpaca 推出后,后许多根据 Alpaca 修改的高效微调方案,比如 Alpaca-lora,Alpaca-Adapter 等等。根据笔者的尝试,采用 Alpaca-Loraopen in new window 微调后的模型,就人为的对话体验上来看,还是和 gpt-turbo-3.5 差距很大。对于不同类型的指令任务,回复质量差异大。

Vicuna

vicunaopen in new window 的效果在 Open Source LLM 中数一数二,目前公开了 7B, 13B, 33B 三个版本。经过几个月的更新, vicuna 版本已经升级到了 v1.3。

数据: vicuna 数据来源于 shareGPT 的用户多轮对话数据,vicuna v0 对原始 ShareGPT 的对话数据集open in new window 进行了清洗,筛选了约 70k 数据进行训练。(在 v1.3 中,训练数据集达到了 140k,具体查看 paperopen in new window

训练: 训练时采用了 gradient checkpointingopen in new windowflash attentionopen in new window 降低了训练设备的要求。同时对多轮对话内容进行训练,如以下的对话:

"""
human: Who are you?
assistant: I am Vicuna, a language model trained by researchers from Large Model Systems Organization (LMSYS).
human: What can you do?
assistant: I can chat with you.
"""

优化 loss 时,mask 掉 human 部分的 token,仅对所有 assistant 部分的 token 对应的 logits 计算 loss 并进行反向传播open in new window优化,优化目标与传统 CasualLM 优化目标相同。

效果: LMSYS 使用 GPT-4 作为裁判,设计了让模型和模型之间 PK 的测评方式,并依据这种方式设计了类似排位赛的 Chat arenaopen in new window (分数相当的模型相互 PK,胜者加分,败者掉分)。参考 LMSYS 发布的这个排行榜,Vicuna 的效果会比 Koala, MPT 等大多数开源模型好。

其他: 除了 Vicuna 模型和 Chat arenaopen in new window ,LMSYS 还提供了部署 LLM 服务的方式:FastChat APIopen in new window,但目前他们的 API 并未对模型推理open in new window做太多优化,推理速度会有点慢。除此外,FastChat 中还提供了 MT-Bench 指标open in new window,针对模型进行多轮对话效果测试。

WizardLM

参考各种排行榜,wizardLMopen in new window 的效果和 Vicuna 差不多,但 WizardLM 更侧重于单论对话的指令任务,vicuna 更侧重于多轮对话任务。

数据: WizardLM 通过 evol-instructopen in new window 方法,对 alpaca 的 self-instruct 数据集进行了优化(修改了使用 GPT4 生成样本时候的 prompt,让训练样本变得更复杂),整个 evol-instruct 数据open in new window集共 196k。以下是一个 evol-instrcut 使用到的 prompt template,更多的 prompt template 再 WizardLM 论文附录中可以查看到。

I want you act as a Prompt Rewriter. Your objective is to rewrite a given prompt into a more complex version to make those famous AI systems (e.g., ChatGPT and GPT4) a bit harder to handle. 

But the rewritten prompt must be reasonable and must be understood and responded by humans. 

Your rewriting cannot omit the non-text parts such as the table and code in #Given Prompt#:. Also, please do not omit the input in #Given Prompt#. You SHOULD complicate the given prompt using the following method: 
If #Given Prompt# contains inquiries about certain issues, the depth and breadth of the inquiry can be increased. 
or You should try your best not to make the #Rewritten Prompt# become verbose, #Rewritten Prompt# can only add 10 to 20 words into #Given Prompt#. 
‘#Given Prompt#’, ‘#Rewritten Prompt#’, ‘given prompt’ and ‘rewritten prompt’ are not allowed to appear in #Rewritten Prompt# #Given Prompt#: 
<Here is instruction.> 
#Rewritten Prompt#:

训练: WizardLM 使用 LLaMa-Xopen in new window 仓库提供的代码进行训练,对 LLaMa 7B 和 13B 进行了微调。除了训练时对工程化做了一些优化,超参做了调整,其余训练流程与 Alpaca、vicuna 相同。

效果: 官方指标对比了 GPT-4 评测分数,MMLU,ARC 等指标。根据官方提供的评测数据,同参数量级的 WizardLM 会和 Vicuna 效果差不多。

Wizard 仓库还开源了 WizardCoder 等模型,是基于 Startcoder 进行训练的,主打代码能力的模型。此外,笔者对几个开源的 Wizard 模型进行了 MMLU 测试(TheBloke/Wizard-Vicuna-13B-Uncensored-GPTQopen in new window,),分数都低地奇怪,只有 35+,不确定是什么原因。

WizardVicunaLM

WizardVicunaLMopen in new window 考虑到 WizardLM 数据集是单轮对话,于是将 WizardLM 和 Vicuna 的方法整合到了一起。实现了对论对话 + 更复杂的 instruction 任务训练。

数据: 官方公开了WizardVicuna 数据集open in new window,共 70k。笔者并未找到官方生成训练数据的代码,但根据官方提供的 ShareGPT 对话内容open in new window 以及 github 描述,可以猜测数据生成步骤大致如下:

  • 先用 WizardLM 提出的 evol-instruct 生成单论对话数据集。
  • 而后将生成的单论对话填充到下列模板中,生成多轮对话数据:

Here is an example of chatgpt answering a question from human. Create 10 more conversations where human asks a question and chatgpt answers. The entire generated sentence should be more then 20000 words and should continue the theme of the first conversation. Think about the intention of the human asking the question in the first conversation and make sure that the conversation is connected by the human asking the next question. Especially when generating human conversations, try to ask difficult questions in a way that requires strong reasoning, deduction, induction, and analogy, and chatgpt gives appropriate answers. The format should be human: chatgpt:

human: { A question generated by evol-instruct} chatgpt:

  • 使用规则对对话数据集进行筛选。

训练: 训练 13B LLaMa 模型,在 8 个 A100 上训练 35 小时。使用的 Vicuna 的训练代码open in new window

效果: WizardVicunaLM 在官方仓库中给出了 GPT-4 打分的测评结果,根据该结果,WizardVicunaLM 的效果会比 WizardLM 和 Vicuna 好一点点。

笔者体验了 Wizard-Vicuna-30B-Uncensored-GPTQopen in new windowWizard-Vicuna-7B-Uncensoredopen in new window 两个模型,就代码助手(代码续写、备注转代码、代码解释)、笔记助手(表述优化、内容扩展、自动续写)两个任务来说,WizardVicunaLM 在使用体验上与 Vicuna 没有多大差别。

Guanaco

Guanaco 来自于 QLoraopen in new window,基于 LLaMa 模型,对 7B, 13B, 33B 和 65B 集中型号的模型进行了训练和探索。而其中的 QLora 技术似乎比 Guanaco 模型本身更出名,它使得我们能够在消费级别的显卡上,微调一个 33B 的模型。

数据: Guanaco 使用了 oasst1 数据集的一个子集进行训练,整个数据约 9k,数据集可以在 Huggingfaceopen in new window 查看到。不同于前几个模型,oasst1open in new window 由 OpenAssistant 公开,是一个人工制造的数据集,包含了约 84k 的样本,不少开源的 LLM 使用了 oasst1 来训练。

训练: Guanaco 的亮点在于训练,其使用了

  • 4-bit NormalFloat:对正态分布数据理论上最优的量化数据类型,它比 4 位整数和 4 位浮点数有更好的实验结果
  • Double Quantization:量化量化常数的方法,平均每个参数节省约 0.37 位(对于 65B 模型大约节省 3GB)
  • Paged Optimizers:使用 NVIDIA统一内存open in new window来避免处理具有长序列长度的小批量时出现的梯度检查点内存峰值open in new window。能够减少 GPU 显存的峰值,对大模型(33B 以上)的训练很有帮助。

具体细节可以查看 paperopen in new window 或者网友笔记open in new window

效果: Guanaco 官方并没有在仓库中展示 Guanaco 的任何评测指标,但根据 Chat Arena 和 Huggingface LLM LeaderBoard,使用 QLora 技术训练的模型,在效果上还是不错的。

小结

除 InstructGPT 外,以上的几个都是基于 LLaMa 训练。羊驼大家族在 LLM 中的发展也是比较好的,从部署到训练,资源齐全。在后续的笔记中,笔者会对其他 LLaMa 系列模型的训练方式、数据、效果等继续进行记录与对比。