200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > LLMs之Baichuan 2:Baichuan 2的简介 安装 使用方法之详细攻略

LLMs之Baichuan 2:Baichuan 2的简介 安装 使用方法之详细攻略

时间:2021-04-16 00:59:56

相关推荐

LLMs之Baichuan 2:Baichuan 2的简介 安装 使用方法之详细攻略

LLMs之Baichuan 2:Baichuan 2的简介、安装、使用方法之详细攻略

目录

相关文章

LLMs之Baichuan:Baichuan-7B/13B模型的简介、安装、使用方法之详细攻略

LLMs:LLaMA Efficient Tuning(一款可高效微调【全参数/LoRA/QLoRA】主流大模型【ChatGLM2/LLaMA2/Baichuan等】的高效工具【预训练+指令监督微调+奖励模型训练+PPO 训练+DPO 训练】)的简介、安装、使用方法之详细攻略

LLMs之Baichuan 2:Baichuan 2的简介、安装、使用方法之详细攻略

LLMs之Baichuan 2:《Baichuan 2: Open Large-scale Language Models》翻译与解读

Baichuan 2的简介

1、Benchmark 结果

1.1、通用领域

7B 模型结果

13B 模型结果

1.2、法律、医疗

7B 模型结果

13B 模型结果

1.3、数学、代码

7B 模型结果

13B 模型结果

1.4、多语言翻译

7B 模型结果

13B 模型结果

Baichuan 2的安装

1、安装依赖

2、Python 代码方式

Chat 模型推理方法示范

Base 模型推理方法示范

3、demo实现

T1、命令行工具方式

T2、网页 demo 方式

4、量化部署

量化方法

在线量化

离线量化

量化效果

5、CPU 部署

6、对 Baichuan 1 的推理优化迁移到 Baichuan 2

Baichuan 2的使用方法

1、模型微调

依赖安装:支持xFormers 加速训练

单机训练

LLMs之Baichuan2:源码解读(fine-tune.py文件,基于deepspeed )—解析命令行参数→数据预处理(对消息内容进行分词处理+if判断对话角色(来自用户还是助手)并为对话内容添加各自角色对应的特殊token进而生成对应的输入和标签+添加结束符token+截断或填充操作)→模型训练(LoRA优化)

多机训练

轻量化微调

相关文章

LLMs之Baichuan:Baichuan-7B/13B模型的简介、安装、使用方法之详细攻略

LLMs之Baichuan:Baichuan-13B模型的简介(包括Baichuan-7B)、安装、使用方法之详细攻略_一个处女座的程序猿的博客-CSDN博客

LLMs:LLaMA Efficient Tuning(一款可高效微调【全参数/LoRA/QLoRA】主流大模型【ChatGLM2/LLaMA2/Baichuan等】的高效工具【预训练+指令监督微调+奖励模型训练+PPO 训练+DPO 训练】)的简介、安装、使用方法之详细攻略

LLMs:LLaMA Efficient Tuning(一款可高效微调【全参数/LoRA/QLoRA】主流大模型【ChatGLM2/LLaMA2/Baichuan等】的高效工具【预训练+指令监督微调+_一个处女座的程序猿的博客-CSDN博客

LLMs之Baichuan 2:Baichuan 2的简介、安装、使用方法之详细攻略

https://yunyaniu./article/details/132748260

LLMs之Baichuan 2:《Baichuan 2: Open Large-scale Language Models》翻译与解读

LLMs之Baichuan 2:《Baichuan 2: Open Large-scale Language Models》翻译与解读_一个处女座的程序猿的博客-CSDN博客

Baichuan 2的简介

Baichuan 2 是百川智能推出的新一代开源大语言模型,采用 2.6 万亿 Tokens 的高质量语料训练。Baichuan 2 在多个权威的中文、英文和多语言的通用、领域 benchmark 上取得同尺寸最佳的效果。本次发布包含有 7B、13B 的 Base 和 Chat 版本,并提供了 Chat 版本的 4bits 量化。所有版本对学术研究完全开放。同时,开发者通过邮件申请并获得官方商用许可后,即可免费商用,请参考协议章节。

1、Benchmark 结果

1.1、通用领域

在通用领域我们在以下数据集上进行了 5-shot 测试。

C-Eval是一个全面的中文基础模型评测数据集,涵盖了 52 个学科和四个难度的级别。我们使用该数据集的 dev 集作为 few-shot 的来源,在 test 集上进行测试。我们采用了Baichuan-7B的评测方案。MMLU是包含 57 个任务的英文评测数据集,涵盖了初等数学、美国历史、计算机科学、法律等,难度覆盖高中水平到专家水平,是目前主流的 LLM 评测数据集。我们采用了开源的评测方案。CMMLU是一个包含 67 个主题的综合性性中文评估基准,专门用于评估语言模型在中文语境下的知识和推理能力。我们采用了其官方的评测方案。Gaokao是一个以中国高考题作为评测大语言模型能力的数据集,用以评估模型的语言能力和逻辑推理能力。 我们只保留了其中的单项选择题,并进行了随机划分。我们采用了与 C-Eval 类似的评测方案。AGIEval旨在评估模型的认知和解决问题相关的任务中的一般能力。 我们只保留了其中的四选一单项选择题,并进行了随机划分。我们采用了与 C-Eval 类似的评测方案。BBH是一个挑战性任务 Big-Bench 的子集。Big-Bench 目前包括 204 项任务。任务主题涉及语言学、儿童发展、数学、常识推理、生物学、物理学、社会偏见、软件开发等方面。BBH 是从 204 项 Big-Bench 评测基准任务中大模型表现不好的任务单独拿出来形成的评测基准。

7B 模型结果
13B 模型结果

1.2、法律、医疗

法律领域我们使用了JEC-QA数据集。JEC-QA 数据集来源于中国国家司法考试。我们只保留了其中的单选题。我们采用了与 C-Eval 类似的评测方案。

医疗领域则使用通用领域数据集(C-Eval、MMLU、CMMLU)中的医学相关学科、MedQA和MedMCQA。我们采用了与 C-Eval 类似的评测方案。

为了测试方便,我们使用了 C-Eval 的 val 集进行测试。MedQA 数据集来源于美国、中国的医学考试。我们测试了MedQA数据集中的 USMLE 和 MCMLE 两个子集,并采用了五个候选的版本。MedMCQA 数据集来源于印度医学院的入学考试。我们只保留了其中的单选题。由于 test 集没有答案,我们使用 dev 集进行测试。通用领域数据集包含的医学相关学科如下: C-Eval: clinical_medicine, basic_medicineMMLU: clinical_knowledge, anatomy, college_medicine, college_biology, nutrition, virology, medical_genetics, professional_medicineCMMLU: anatomy, clinical_knowledge, college_medicine, genetics, nutrition, traditional_chinese_medicine, virology

我们对以上数据集进行了 5-shot 测试。

7B 模型结果
13B 模型结果

1.3、数学、代码

数学领域我们使用OpenCompass评估框架,对GSM8K和MATH数据集进行了 4-shot 测试。

GSM8K 是由 OpenAI 发布的一个由 8.5K 高质量的语言多样化的小学数学应用题组成的数据集,要求根据给定的场景和两个可能的解决方案,选择最合理的方案。MATH 数据集包含 12,500 个数学问题(其中 7500 个属于训练集,5000 个属于测试集),这些问题收集自 AMC 10、AMC 12、AIME 等数学竞赛。

代码领域则采用了HumanEval和MBPP数据集。我们使用 OpenCompass,对 HumanEval 进行了 0-shot 测试,MBPP 数据集进行了 3-shot 测试。

HumanEval 中的编程任务包括模型语言理解、推理、算法和简单数学,以评估模型功能正确性,并衡量模型的问题解决能力。MBPP 包括 974 个 Python 短函数、程序的文字描述以及用于检查功能正确性的测试用例的数据集。

7B 模型结果
13B 模型结果

1.4、多语言翻译

我们采用了Flores-101数据集来评估模型的多语言能力。Flores-101 涵盖了世界各地的 101 种语言。它的数据来源于新闻、旅游指南和书籍等多个不同领域。我们选择了联合国官方语言(阿拉伯文、中文、英文、法文、俄文和西班牙文)以及德文和日文作为测试语种。我们使用 OpenCompass 对 Flores-101 中的中-英、中-法、中-西班牙、中-阿拉伯、中-俄、中-日、中-德等七个子任务分别进行了 8-shot 测试。

7B 模型结果
13B 模型结果

Baichuan 2的安装

推理所需的模型权重、源码、配置已发布在 Hugging Face,下载链接见本文档最开始的表格。我们在此示范多种推理方式。程序会自动从 Hugging Face 下载所需资源。

1、安装依赖

pip install -r requirements.txt

2、Python 代码方式

Chat 模型推理方法示范

>>> import torch>>> from transformers import AutoModelForCausalLM, AutoTokenizer>>> from transformers.generation.utils import GenerationConfig>>> tokenizer = AutoTokenizer.from_pretrained("baichuan-inc/Baichuan2-13B-Chat", use_fast=False, trust_remote_code=True)>>> model = AutoModelForCausalLM.from_pretrained("baichuan-inc/Baichuan2-13B-Chat", device_map="auto", torch_dtype=torch.bfloat16, trust_remote_code=True)>>> model.generation_config = GenerationConfig.from_pretrained("baichuan-inc/Baichuan2-13B-Chat")>>> messages = []>>> messages.append({"role": "user", "content": "解释一下“温故而知新”"})>>> response = model.chat(tokenizer, messages)>>> print(response)"温故而知新"是一句中国古代的成语,出自《论语·为政》篇。这句话的意思是:通过回顾过去,我们可以发现新的知识和理解。换句话说,学习历史和经验可以让我们更好地理解现在和未来。

这句话鼓励我们在学习和生活中不断地回顾和反思过去的经验,从而获得新的启示和成长。通过重温旧的知识和经历,我们可以发现新的观点和理解,从而更好地应对不断变化的世界和挑战。

Base 模型推理方法示范

>>> from transformers import AutoModelForCausalLM, AutoTokenizer>>> tokenizer = AutoTokenizer.from_pretrained("baichuan-inc/Baichuan2-13B-Base", trust_remote_code=True)>>> model = AutoModelForCausalLM.from_pretrained("baichuan-inc/Baichuan2-13B-Base", device_map="auto", trust_remote_code=True)>>> inputs = tokenizer('登鹳雀楼->王之涣\n夜雨寄北->', return_tensors='pt')>>> inputs = inputs.to('cuda:0')>>> pred = model.generate(**inputs, max_new_tokens=64, repetition_penalty=1.1)>>> print(tokenizer.decode(pred.cpu()[0], skip_special_tokens=True))登鹳雀楼->王之涣夜雨寄北->李商隐

在上述两段代码中,模型加载指定device_map='auto',会使用所有可用显卡。如需指定使用的设备,可以使用类似export CUDA_VISIBLE_DEVICES=0,1(使用了0、1号显卡)的方式控制。

3、demo实现

T1、命令行工具方式

python cli_demo.py

本命令行工具是为 Chat 场景设计,因此我们不支持使用该工具调用 Base 模型。

T2、网页 demo 方式

streamlit run web_demo.py

依靠 streamlit 运行以下命令,会在本地启动一个 web 服务,把控制台给出的地址放入浏览器即可访问。本网页 demo 工具是为 Chat 场景设计,因此我们不支持使用该工具调用 Base 模型。

4、量化部署

为了让不同的用户以及不同的平台都能运行 Baichuan 2 模型,我们针对 Baichuan 2 模型做了相应地量化工作(包括 Baichuan2-7B-Chat 和 Baichuan2-13B-Chat),方便用户快速高效地在自己的平台部署 Baichuan 2 模型。

量化方法

Baichuan 2 的采用社区主流的量化方法:BitsAndBytes。该方法可以保证量化后的效果基本不掉点,目前已经集成到 transformers 库里,并在社区得到了广泛应用。BitsAndBytes 支持 8bits 和 4bits 两种量化,其中 4bits 支持 FP4 和 NF4 两种格式,Baichuan 2 选用 NF4 作为 4bits 量化的数据类型。

基于该量化方法,Baichuan 2 支持在线量化和离线量化两种模式。

在线量化

对于在线量化,我们支持 8bits 和 4bits 量化,使用方式和Baichuan-13B项目中的方式类似,只需要先加载模型到 CPU 的内存里,再调用quantize()接口量化,最后调用cuda()函数,将量化后的权重拷贝到 GPU 显存中。实现整个模型加载的代码非常简单,我们以 Baichuan2-7B-Chat 为例:

8bits 在线量化:

model = AutoModelForCausalLM.from_pretrained("baichuan-inc/Baichuan2-7B-Chat", torch_dtype=torch.float16, trust_remote_code=True)model = model.quantize(8).cuda()

4bits 在线量化:

model = AutoModelForCausalLM.from_pretrained("baichuan-inc/Baichuan2-7B-Chat", torch_dtype=torch.float16, trust_remote_code=True)model = model.quantize(4).cuda()

需要注意的是,在用from_pretrained接口的时候,用户一般会加上device_map="auto",在使用在线量化时,需要去掉这个参数,否则会报错。

离线量化

为了方便用户的使用,我们提供了离线量化好的 4bits 的版本Baichuan2-7B-Chat-4bits,供用户下载。 用户加载 Baichuan2-7B-Chat-4bits 模型很简单,只需要执行:

model = AutoModelForCausalLM.from_pretrained("baichuan-inc/Baichuan2-7B-Chat-4bits", device_map="auto", trust_remote_code=True)

对于 8bits 离线量化,我们没有提供相应的版本,因为 Hugging Face transformers 库提供了相应的 API 接口,可以很方便的实现 8bits 量化模型的保存和加载。用户可以自行按照如下方式实现 8bits 的模型保存和加载:

# Model saving: model_id is the original model directory, and quant8_saved_dir is the directory where the 8bits quantized model is saved.model = AutoModelForCausalLM.from_pretrained(model_id, load_in_8bit=True, device_map="auto", trust_remote_code=True)model.save_pretrained(quant8_saved_dir)model = AutoModelForCausalLM.from_pretrained(quant8_saved_dir, device_map="auto", trust_remote_code=True)

量化效果

量化前后显存占用对比 (GPU Mem in GB):

量化后在各个 benchmark 上的结果和原始版本对比如下:

C-Eval 是在其 val set 上进行的评测

可以看到,4bits 相对 bfloat16 精度损失在 1 - 2 个百分点左右。

5、CPU 部署

Baichuan 2 模型支持 CPU 推理,但需要强调的是,CPU 的推理速度相对较慢。需按如下方式修改模型加载的方式:

# Taking Baichuan2-7B-Chat as an examplemodel = AutoModelForCausalLM.from_pretrained("baichuan-inc/Baichuan2-7B-Chat", torch_dtype=torch.float32, trust_remote_code=True)

6、对 Baichuan 1 的推理优化迁移到 Baichuan 2

由于很多用户在 Baichuan 1 (Baichuan-7B, Baichuan-13B)上做了很多优化的工作,例如编译优化、量化等,为了将这些工作零成本地应用于 Baichuan 2,用户可以对 Baichuan 2 模型做一个离线转换,转换后就可以当做 Baichuan 1 模型来使用。具体来说,用户只需要利用以下脚本离线对 Baichuan 2 模型的最后一层 lm_head 做归一化,并替换掉lm_head.weight即可。替换完后,就可以像对 Baichuan 1 模型一样对转换后的模型做编译优化等工作了。

import torchimport osori_model_dir = 'your Baichuan 2 model directory'# To avoid overwriting the original model, it's best to save the converted model to another directory before replacing itnew_model_dir = 'your normalized lm_head weight Baichuan 2 model directory'model = torch.load(os.path.join(ori_model_dir, 'pytorch_model.bin'))lm_head_w = model['lm_head.weight']lm_head_w = torch.nn.functional.normalize(lm_head_w)model['lm_head.weight'] = lm_head_wtorch.save(model, os.path.join(new_model_dir, 'pytorch_model.bin'))

Baichuan 2的使用方法

1、模型微调

依赖安装:支持xFormers 加速训练

git clone /baichuan-inc/Baichuan2.gitcd Baichuan2/fine-tunepip install -r requirements.txt

如需使用 LoRA 等轻量级微调方法需额外安装peft如需使用 xFormers 进行训练加速需额外安装xFormers

单机训练

下面我们给一个微调 Baichuan2-7B-Base 的单机训练例子。

训练数据:data/belle_chat_ramdon_10k.json,该样例数据是从multiturn_chat_0.8M采样出 1 万条,并且做了格式转换。主要是展示多轮数据怎么训练,不保证效果。

hostfile=""deepspeed --hostfile=$hostfile fine-tune.py \--report_to "none" \--data_path "data/belle_chat_ramdon_10k.json" \--model_name_or_path "baichuan-inc/Baichuan2-7B-Base" \--output_dir "output" \--model_max_length 512 \--num_train_epochs 4 \--per_device_train_batch_size 16 \--gradient_accumulation_steps 1 \--save_strategy epoch \--learning_rate 2e-5 \--lr_scheduler_type constant \--adam_beta1 0.9 \--adam_beta2 0.98 \--adam_epsilon 1e-8 \--max_grad_norm 1.0 \--weight_decay 1e-4 \--warmup_ratio 0.0 \--logging_steps 1 \--gradient_checkpointing True \--deepspeed ds_config.json \--bf16 True \--tf32 True

LLMs之Baichuan2:源码解读(fine-tune.py文件,基于deepspeed )—解析命令行参数→数据预处理(对消息内容进行分词处理+if判断对话角色(来自用户还是助手)并为对话内容添加各自角色对应的特殊token进而生成对应的输入和标签+添加结束符token+截断或填充操作)→模型训练(LoRA优化)

https://yunyaniu./article/details/132867299

多机训练

多机训练只需要给一下 hostfile ,内容类似如下:

ip1 slots=8ip2 slots=8ip3 slots=8ip4 slots=8....

同时在训练脚本里面指定 hosftfile 的路径:

hostfile="/path/to/hostfile"deepspeed --hostfile=$hostfile fine-tune.py \--report_to "none" \--data_path "data/belle_chat_ramdon_10k.json" \--model_name_or_path "baichuan-inc/Baichuan2-7B-Base" \--output_dir "output" \--model_max_length 512 \--num_train_epochs 4 \--per_device_train_batch_size 16 \--gradient_accumulation_steps 1 \--save_strategy epoch \--learning_rate 2e-5 \--lr_scheduler_type constant \--adam_beta1 0.9 \--adam_beta2 0.98 \--adam_epsilon 1e-8 \--max_grad_norm 1.0 \--weight_decay 1e-4 \--warmup_ratio 0.0 \--logging_steps 1 \--gradient_checkpointing True \--deepspeed ds_config.json \--bf16 True \--tf32 True

轻量化微调

代码已经支持轻量化微调如 LoRA,如需使用仅需在上面的脚本中加入以下参数:

--use_lora True

LoRA 具体的配置可见fine-tune.py脚本。

使用 LoRA 微调后可以使用下面的命令加载模型:

from peft import AutoPeftModelForCausalLMmodel = AutoPeftModelForCausalLM.from_pretrained("output", trust_remote_code=True)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。