导读

11月24日,零一万物基正式发布并开源微调模型 Yi-34B-Chat,可申请免费商用。同时,零一万物还为开发者提供了 4bit/8bit 量化版模型,Yi-34B-Chat 4bit 量化版模型可以直接在消费级显卡(如RTX3090)上使用。魔搭社区已支持下载、推理训练体验,并推出相关教程,欢迎大家来玩!

 

评测效果

Model MMLU CMMLU C-Eval(val) BBH GSM8k
Yi-34B-Chat 79.11 77.04 51.41 71.65  
Yi-6B-Chat 69.44 68.80 39.70 38.44  
LLaMA2-70B-Chat 36.10 34.99 42.36 47.08  
AquilaChat-34B v1.2 67.51 82.99 20.12 11.52  
InternLM-chat-20B 53.55 51.19 42.41 15.69  
Qwen-14B-Chat 67.73 66.12 49.65 59.51  
Baichuan2-13B-Chat 58.64 56.02 38.81 45.72  
LLaMA2-13B-Chat 27.47 27.93 32.90 36.85  

 

在 MMLU 英文知识水平评测集,C-Eval、CMMLU 中文综合考试评测集,以及 GSM8K、BBH 两个常用的评估大模型数学及推理能力的评测集中,Yi-34B-Chat 在开源模型中取得多项优异成绩(评测结果均采用 zero-shot 的方式,结果会受到 prompt 设计的影响,官方使用了相同的 prompt 和生成策略来评测表中所有模型以获得一个较为公正的结果,供大家参考)。

模型效果如下,

文本创作:

 

科技话题:

 

中文语义理解:

以下为大家带来魔搭社区推理、微调最佳实践教程:

 

模型链接和下载

 

Yi系列模型现已在ModelScope社区开源,包括:

 

Yi-34B-Chat模型:

https://modelscope.cn/models/01ai/Yi-34B-Chat/summary

Yi-34B-Chat-4bits模型:

https://modelscope.cn/models/01ai/Yi-34B-Chat-4bits/summary

Yi-34B-Chat-8bits模型:

https://modelscope.cn/models/01ai/Yi-34B-Chat-8bits/summary

 

社区支持直接下载模型的repo:

from modelscope import snapshot_download
model_dir = snapshot_download("01ai/Yi-34B-Chat", revision = "master")
model_dir_int4 = snapshot_download("01ai/Yi-34B-Chat-4bits", revision = "master")

model_dir_int8 = snapshot_download("01ai/Yi-34B-Chat-8bits", revision = "master")

 

 

Yi系列模型推理

Yi-34B-Chat:

推理代码

from modelscope import AutoModelForCausalLM, AutoTokenizer

model_dir = '01ai/Yi-34B-Chat'

tokenizer = AutoTokenizer.from_pretrained(model_dir, use_fast=False)

model = AutoModelForCausalLM.from_pretrained(
    model_dir,
    device_map='auto',
    torch_dtype='auto'
).eval()

messages = [
    {"role": "user", "content": "hi"}
]

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? If you have any questions or need information on a specific topic, feel free to ask."
print(response)

 

 

资源消耗:

 

Yi-34B-Chat-4bits

环境安装

!pip install transformers -U

 !wget https://github.com/casper-hansen/AutoAWQ/releases/download/v0.1.7/autoawq-0.1.7+cu118-cp310-cp310-linux_x86_64.whl
 !pip install autoawq-0.1.7+cu118-cp310-cp310-linux_x86_64.whl

 

推理代码

from modelscope import AutoModelForCausalLM, AutoTokenizer

model_dir = '01ai/Yi-34B-Chat-4bits'

tokenizer = AutoTokenizer.from_pretrained(model_dir, use_fast=False)

model = AutoModelForCausalLM.from_pretrained(
    model_dir,
    device_map='auto',
    torch_dtype='auto'
).eval()

messages = [
    {"role": "user", "content": "hi"}
]

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? If you have any questions or need information on a specific topic, feel free to ask."
print(response)

 

资源消耗

 

 

Yi系列模型微调和微调后推理

微调代码开源地址:

https://github.com/modelscope/swift/tree/main/examples/pytorch/llm

clone swift仓库并安装SWIFT(魔搭官方提供的训练推理框架)

# 设置pip全局镜像和安装相关的python包
pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/
git clone https://github.com/modelscope/swift.git

cd swift
pip install -e .
pip install deepspeed -U

# 下面的脚本需要在此目录下执行
cd examples/pytorch/llm

 

使用LoRA+DDP+Deepspeed微调的脚本可以查看:

https://github.com/modelscope/swift/tree/main/examples/pytorch/llm/scripts/yi_34b_chat/lora_ddp_ds

 

使用QLoRA微调的脚本可以查看:

https://github.com/modelscope/swift/tree/main/examples/pytorch/llm/scripts/yi_34b_chat/qlora

 

以下具体介绍使用LoRA+DDP+Deepspeed的脚本:

 

微调脚本

# Experimental environment: 2 * A100
# 2 * 72GB GPU memory
nproc_per_node=2

PYTHONPATH=../../.. \
CUDA_VISIBLE_DEVICES=0,1 \
torchrun \
    --nproc_per_node=$nproc_per_node \
    --master_port 29500 \
    llm_sft.py \
    --model_type yi-34b-chat \
    --sft_type lora \
    --tuner_backend swift \
    --template_type AUTO \
    --dtype AUTO \
    --output_dir output \
    --dataset blossom-math-zh \
    --train_dataset_sample -1 \
    --num_train_epochs 1 \
    --max_length 2048 \
    --check_dataset_strategy warning \
    --lora_rank 8 \
    --lora_alpha 32 \
    --lora_dropout_p 0.05 \
    --lora_target_modules DEFAULT \
    --gradient_checkpointing true \
    --batch_size 1 \
    --weight_decay 0.01 \
    --learning_rate 1e-4 \
    --gradient_accumulation_steps $(expr 16 / $nproc_per_node) \
    --max_grad_norm 0.5 \
    --warmup_ratio 0.03 \
    --eval_steps 100 \
    --save_steps 100 \
    --save_total_limit 2 \
    --logging_steps 10 \
    --use_flash_attn true \
    --push_to_hub false \
    --hub_model_id yi-34b-chat-lora \
    --hub_private_repo true \
    --hub_token 'your-sdk-token' \

 

 

如果显卡显存较低,可以增加如下参数,来支持量化训练:

--quantization_bit 4 \
--bnb_4bit_comp_dtype AUTO \

 

训练过程也支持本地数据集,需要指定如下参数:

--custom_train_dataset_path xxx.jsonl \
--custom_val_dataset_path yyy.jsonl \

 

自定义数据集的格式可以参考:

https://github.com/modelscope/swift/blob/main/examples/pytorch/llm/README_CN.md#-%E6%8E%A8%E8%8D%90%E5%91%BD%E4%BB%A4%E8%A1%8C%E5%8F%82%E6%95%B0%E7%9A%84%E5%BD%A2%E5%BC%8F

 

微调后的推理脚本:

这里的ckpt_dir需要修改为训练生成的checkpoint文件夹:

# Experimental environment: A100
PYTHONPATH=../../.. \
CUDA_VISIBLE_DEVICES=0 \
python llm_infer.py \
    --ckpt_dir "output/yi-34b-chat/vx_xxx/checkpoint-xxx" \
    --load_args_from_ckpt_dir true \
    --eval_human false \
    --max_length 2048 \
    --use_flash_attn true \
    --max_new_tokens 2048 \
    --temperature 0.1 \
    --top_p 0.7 \
    --repetition_penalty 1.05 \
    --do_sample true \
    --merge_lora_and_save false \

 

微调的可视化结果

训练损失:

评估损失

训练后生成样例

[PROMPT]<|im_start|> system
you are a helpful assistant!<|im_end|> 
<|im_start|> user
Jacob的水箱能容纳50升水。Jacob从河里和雨水中收集水来填满他的水箱。他每天可以从雨水中收集800毫升的水,从河里收集1700毫升的水。Jacob需要多少天才能填满他的水箱?<|im_end|> 
<|im_start|> assistant
[OUTPUT]Jacob每天从雨水中收集的水量为800毫升,即0.8升。
Jacob每天从河里收集的水量为1700毫升,即1.7升。
所以,Jacob每天总共收集的水量为0.8 + 1.7 = 2.5升。
要填满50升的水箱,Jacob需要的天数为50 / 2.5 = 20天。

Answer: 20<|im_end|>

[LABELS]每天从雨水和河水中收集到的水总量为800毫升+1700毫升=2500毫升=2.5升。
所以,填满50升水箱需要的天数为50升/2.5升=20天。

Answer: 20
--------------------------------------------------------------------------------
[PROMPT]<|im_start|> system
you are a helpful assistant!<|im_end|> 
<|im_start|> user
依法纳税是每个公民的义务.孙老师月收入2400元,按照个人所得税征收标准,孙老师上月应缴纳个人所得税多少元?(个人所得税按1%)<|im_end|> 
<|im_start|> assistant
[OUTPUT]根据题目中的信息,孙老师的月收入为2400元,个人所得税征收标准为1%。
因此,孙老师上月应缴纳的个人所得税为:
2400元 * 1% = 24元
所以,孙老师上月应缴纳的个人所得税为24元。

Answer: 24<|im_end|>

[LABELS]孙老师月收入为2400元,按照个人所得税征收标准,个人所得税按照1%来计算。
个人所得税 = 收入 × 税率
              = 2400元 × 1%
              = 2400元 × 0.01
              = 24元
所以,孙老师上月应缴纳个人所得税为24元。

Answer: 24

 

资源消耗:

2 * 70G

 

 

点击直达模型开源卡片:https://www.modelscope.cn/models/01ai/Yi-34B-Chat/summary

Logo

ModelScope旨在打造下一代开源的模型即服务共享平台,为泛AI开发者提供灵活、易用、低成本的一站式模型服务产品,让模型应用更简单!

更多推荐