零一万物Yi-34B-Chat 微调模型及量化版开源!魔搭社区最佳实践教程!

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

导读

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 和生成策略来评测表中所有模型以获得一个较为公正的结果,供大家参考)。

模型效果如下,

文本创作:

image.png

科技话题:

image.png

中文语义理解:

image.png

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

模型链接和下载

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)

资源消耗:

image.png

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)

资源消耗

image.png

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 \

微调的可视化结果

训练损失:

image.png

评估损失

image.png

训练后生成样例

[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

image.png

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

相关文章
|
4天前
|
自然语言处理 数据可视化 物联网
Qwen1.5-MoE开源,魔搭社区推理训练最佳实践教程来啦
通义千问团队推出Qwen系列的首个MoE模型,Qwen1.5-MoE-A2.7B。
|
4天前
|
人工智能 自然语言处理 机器人
Jina AI 发布中英和英德双语 8K 向量模型,魔搭社区开源最佳实践!
在 Jina Embeddings 英语向量模型突破百万下载后,今天,Jina AI正式开源了两款双语向量模型:中英双语(Chinese-English)和英德双语(English-German)向量模型,这也是全球首次推出支持 8K 双语文本的开源向量模型。
|
4天前
|
自然语言处理 物联网 Swift
零一万物开源Yi-VL多模态大模型,魔搭社区推理&微调最佳实践来啦!
近期,零一万物Yi系列模型家族发布了其多模态大模型系列,Yi Vision Language(Yi-VL)多模态语言大模型正式面向全球开源。
|
4天前
|
数据可视化 物联网 测试技术
零一万物Yi-1.5系列模型发布并开源!34B/9B/6B 多尺寸魔搭社区推理微调最佳实践教程来啦!
Yi-1.5是Yi的升级版本。 它使用 500B tokens的高质量语料库在 Yi 上持续进行预训练,并在 3M 个多样化的微调样本上进行微调。
|
4天前
|
数据可视化 物联网 关系型数据库
幻方开源第二代MoE模型 DeepSeek-V2,魔搭社区推理、微调最佳实践教程
5月6日,幻方继1月份推出首个国产MoE模型,历时4个月,带来第二代MoE模型DeepSeek-V2,并开源了技术报告和模型权重,魔搭社区可下载体验。
|
4天前
|
存储 自然语言处理 负载均衡
元象开源首个MoE大模型:4.2B激活参数,效果堪比13B模型,魔搭社区最佳实践来了
近日,元象发布其首个Moe大模型 XVERSE-MoE-A4.2B, 采用混合专家模型架构 (Mixture of Experts),激活参数4.2B,效果即可媲美13B模型。该模型全开源,无条件免费商用,支持中小企业、研究者和开发者可在元象高性能“全家桶”中按需选用,推动低成本部署。
|
4天前
|
数据可视化 物联网 Swift
澜舟科技开源孟子3-13B大模型,魔搭社区推理训练最佳实践!
4月1日,澜舟科技宣布开源Mengzi3-13B大模型,对学术研究完全开放,同时支持免费商用。
|
4天前
|
人工智能 知识图谱 Windows
Mistral 7B v0.2 基础模型开源,魔搭社区微调教程和评测来啦!
Mistral AI在3月24日突然发布并开源了 Mistral 7B v0.2模型,有如下几个特点
|
4天前
|
编解码 JSON 数据可视化
DeepSeek VL系列开源,魔搭社区模型微调最佳实践教程来啦!
3月11日,DeepSeek-AI开源了全新多模态大模型DeepSeek-VL系列,包含1.3b、7b两种不同规模的4个版本的模型。
|
4天前
|
机器学习/深度学习 人工智能 物联网
Mixtral 8X7B MoE模型基于阿里云人工智能平台PAI实践合集
本文介绍如何在PAI平台针对Mixtral 8x7B大模型的微调和推理服务的最佳实践,助力AI开发者快速开箱。以下我们将分别展示具体使用步骤。
http://www.vxiaotou.com