零一万物大模型部署+微调总结
零一万物官网:https://www.lingyiwanwu.com/
一、部署篇
1.准备虚拟环境
该部分不详细讲解。但是本次的部署使用的是(ps:可以考虑租用服务器,系统镜像选择ubuntu22&cuda11.8)
Nvidia-cuda-11.8.89
#此为显卡驱动,一定要保证驱动安装成功
#查看驱动版本
nvcc -V
#如果没有就自己安装
#查看自己的显卡配置以及运行情况
nvidia-smi
Anaconda-23.5.1
#此为为模型创建虚拟环境
#查看版本号
conda -V
Python-3.10.13
#python的安装是在创建conda虚拟环境的时候创建的
#查看python版本
python -V
2.准备环境
-
安装零一万物的软件包
git clone https://github.com/01-ai/Yi.git
-
创建虚拟环境(这里我选择的是python3.10,Yi可以自己随便写,这个是自己环境的名字)
conda create -n Yi python=3.10 -y
-
启动环境
conda activate Yi
-
设置需要的环境
pip install -r requirements.txt
3.下载模型
根据自己的计算机配置来选择下载的模型:https://huggingface.co/01-ai(需要科学上网)
Yi-XB-Chat是基准模型,而带bits是量化模型,所有量化模型的使用门槛都很低,因为它们可以部署在消费级GPU上(例如,3090、4090)。
4.执行推断
①创建quick_start.py文件,然后写入以下内容
from transformers import AutoModelForCausalLM, AutoTokenizer
model_path = '此处填入你下载的模型路径'
tokenizer = AutoTokenizer.from_pretrained(model_path, use_fast=False)
# Since transformers 4.35.0, the GPT-Q/AWQ model can be loaded using AutoModelForCausalLM.
model = AutoModelForCausalLM.from_pretrained(
model_path,
device_map="auto",
torch_dtype='auto'
).eval()
# Prompt content: "hi"
messages = [
{"role": "user", "content": "此处填入你的问题"}
]
input_ids = tokenizer.apply_chat_template(conversation=messages, tokenize=True, add_generation_prompt=True, return_tensors='pt')
output_ids = model.generate(input_ids.to('cuda'))
response = tokenizer.decode(output_ids[0][input_ids.shape[1]:], skip_special_tokens=True)
# Model response: "Hello! How can I assist you today?"
print(response)
②零一万物,启动!
运行python文件
python quick_start.py
然后就是等待,此时可以在另一个终端上输入nvidia-smi
查看自己的显卡的状态
最后你可以看到一个类似下面的输出。(Congratulation!)
Hello! How can I assist you today?
二、微调篇
该篇章还有很多地方不完善,请多多见谅
参考文档:https://github.com/01-ai/Yi/tree/main/finetune
1.环境部署
本篇章紧接上面的内容,请参考篇章一的1.准备虚拟环境 2.准备环境
在Yi的conda环境中下载依赖
pip install torch==2.0.1 deepspeed==0.10 tensorboard transformers datasets sentencepiece accelerate ray==2.7
(ps:需要注意的是原来的deepspeed要求的版本是0.10,但是在安装的时候可能会出现报错,所以建议改为0.12.6下载)
2.建立数据集
①使用自带的数据集
finetune/yi_example_dataset
有示例数据集,这些数据集是从BAAI/COIG修改而来的
|-- $DATA_PATH
|--data
|-- train.jsonl
|-- eval.jsonl
②自己微调的数据集
如需自己微调数据集请参考:https://huggingface.co/datasets/BAAI/COIG
不在此做过多缀诉
|-- $DATA_PATH
| |-- data
| | |-- train-00000-of-00001-2a1df75c6bce91ab.parquet
| | |-- test-00000-of-00001-8c7c51afc6d45980.parquet
| |-- dataset_infos.json
| |-- README.md
3.选则需要训练的模型
将基于LLM-base的模型下载到MODEL_PATH(6 B和34 B,注意不要使用量化后的模型)。典型的模型文件夹如下所示
|-- $MODEL_PATH
| |-- config.json
| |-- pytorch_model-00001-of-00002.bin
| |-- pytorch_model-00002-of-00002.bin
| |-- pytorch_model.bin.index.json
| |-- tokenizer_config.json
| |-- tokenizer.model
| |-- ...
4.修改文件
1.finetune/utils/model/model_utils.py
...............................
if not eval_mode:
model = model_class.from_pretrained(
model_name_or_path,
from_tf=bool(".ckpt" in model_name_or_path),
config=model_config,
trust_remote_code=True,
#将原来的use_flash_attention_2=True删掉,并添加下面两行代码
attn_implementation="flash_attention_2",
torch_dtype="auto",
)
.....................................
2.finetune/scripts/run_sft_Yi_6b.sh
#/usr/bin/env bash
cd "$(dirname "${BASH_SOURCE[0]}")/../sft/"
deepspeed main.py \
--data_path 请在此处填上你的训练集的路径($DATA_PATH) \
--model_name_or_path 请在此处填上你的模型的路径($MODEL_PATH) \
--per_device_train_batch_size 1 \
--per_device_eval_batch_size 1 \
--max_seq_len 4096 \
--learning_rate 2e-6 \
--weight_decay 0. \
--num_train_epochs 4 \
--training_debug_steps 20 \
--gradient_accumulation_steps 1 \
--lr_scheduler_type cosine \
--num_warmup_steps 0 \
--seed 1234 \
--gradient_checkpointing \
--zero_stage 2 \
--deepspeed \
--offload \
--output_dir ./finetuned_model
5.微调,启动!
cd finetune/scripts
bash run_sft_Yi_6b.sh
三、报错篇
1.flash_attn
如果出现未安装就直接安装
pip install flash_attn
但是有时候在安装时也会出现报错,所以需要根据cuda和pytorch版本进行本地的flash-attention安装
请在此处查找适应你的计算机的文档:https://github.com/Dao-AILab/flash-attention/releases
如:cuda11.7 torch1.13.1 python3.9
pip install flash_attn-2.3.0+cu117torch1.13cxx11abiFALSE-cp39-cp39-linux_x86_64.whl
2.deepspeed
AttributeError: 'DeepSpeedCPUAdam' object has no attribute 'ds_opt_adam'
出现这句话一般是cuda版本与deepspeed的版本不符合而出现的报错,需要调整版本