双卡3090消费级显卡 SFT OpenBuddy-LLaMA1-65B 最佳实践

本文涉及的产品
交互式建模 PAI-DSW,5000CU*H 3个月
简介: OpenBuddy继接连开源OpenBuddy-LLaMA1-13B、OpenBuddy-LLaMA1-30B后,8月10日,一鼓作气发布了650亿参数的大型跨语言对话模型 OpenBuddy-LLaMA1-65B。

导读

OpenBuddy继接连开源OpenBuddy-LLaMA1-13B、OpenBuddy-LLaMA1-30B后,8月10日,一鼓作气发布了650亿参数的大型跨语言对话模型 OpenBuddy-LLaMA1-65B相较于之前发布的模型,65B模型在认知能力上的表现有所提升,更能胜任推理、归纳总结、思维链等高级认知任务,同时,模型的3-bit量化版本大小约为31GB,依然可以加载至两张3090 24GB消费级显卡上部署。

目前,OpenBuddy-LLaMA-65B模型的权重已经上传魔搭ModelScope平台,供大家下载、使用。本文将结合基于ModelScope Swift的LLM SFT训练框架对OpenBuddy-LLaMA-65B 进行SFT和Inference的最佳实践展示。

模型体验:https://modelscope.cn/models/OpenBuddy/openbuddy-llama-65b-v8-bf16/summary

(注:受Meta许可协议限制,LLaMA1-65B模型仅供学习、研究使用,不可商用)




基于ModelScope Swift的LLM SFT训练框架

Swift github链接: https://github.com/modelscope/swift


Swift(Scalable lightWeight Infrastructure for Fine-Tuning)是一个可扩展的框架,旨在促进轻量级模型Fine-Tuning。它集成了各种高效的Fine-Tuning方法的实现,采用参数高效、内存高效和时间高效的方法。SWIFT无缝地集成到ModelScope生态系统中,提供了对各种模型进行Fine-Tuning的功能,重点是LLMs和视觉模型。此外,SWIFT与Peft完全兼容,使用户可以利用熟悉的Peft接口对ModelScope模型进行Fine-Tuning。


ms-swift的安装

pip install ms-swift


基于swift的LLM SFT训练框架链接:

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

1. 支持的sft方法: lora, qlora, 全参数微调, ...

2. 支持的模型: qwen-7b, baichuan-7b, baichuan-13b, chatglm2-6b, llama2-7b, llama2-13b, llama2-70b, openbuddy-llama2-13b, openbuddy-llama-65b, polylm-13b, ...

3. 支持的特性: 模型量化, DDP, 模型并行(device_map), gradient checkpoint, 梯度累加, 支持推送modelscope hub, 支持自定义数据集, ...

4. 支持的数据集: alpaca-en(gpt4), alpaca-zh(gpt4), finance-en, multi-alpaca-all, code-en, instinwild-en, instinwild-zh, ...



实验环境准备

本文可在双卡3090的环境配置下运行 (显存要求42G)

python>=3.8


实验环境准备

# 安装miniconda
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
# 一直[ENTER], 最后一个选项yes即可
sh Miniconda3-latest-Linux-x86_64.sh
# conda虚拟环境搭建
conda create --name ms-sft python=3.10
conda activate ms-sft
# pip设置全局镜像与相关python包安装
pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/
pip install torch torchvision torchaudio -U
pip install sentencepiece charset_normalizer cpm_kernels tiktoken -U
pip install matplotlib scikit-learn tqdm tensorboard -U
pip install transformers datasets -U
pip install accelerate transformers_stream_generator -U
pip install ms-swift modelscope -U



模型的下载和推理

openbuddy-llama-65b模型现已在ModelScope社区开源:

模型链接:https://modelscope.cn/models/OpenBuddy/openbuddy-llama-65b-v8-bf16/summary


通过如下代码,实现模型的下载和推理. (使用4bit量化, 所需显存40G)

import os
os.environ['CUDA_VISIBLE_DEVICES'] = '0,1'
import torch
from modelscope import AutoTokenizer
from modelscope import AutoModelForCausalLM, snapshot_download
from transformers import BitsAndBytesConfig, GenerationConfig
model_id = 'OpenBuddy/openbuddy-llama-65b-v8-bf16'
revision = 'v1.0.0'
model_dir = snapshot_download(model_id, revision)
tokenizer = AutoTokenizer.from_pretrained(model_dir, trust_remote_code=True)
quantization_config = BitsAndBytesConfig(
    load_in_8bit=False,
    load_in_4bit=True,
    bnb_4bit_compute_dtype=torch.float16,
    bnb_4bit_quant_type='nf4',
    bnb_4bit_use_double_quant=True)
model = AutoModelForCausalLM.from_pretrained(
    model_dir, device_map='auto', torch_dtype=torch.float16, 
    trust_remote_code=True, quantization_config=quantization_config)
model = model.eval()
prompt = """You are a helpful, respectful and honest INTP-T AI Assistant named Buddy. You are talking to a human User.
Always answer as helpfully and logically as possible, while being safe. Your answers should not include any harmful, political, religious, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature.
If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information.
You like to use emojis. You can speak fluently in many languages, for example: English, Chinese.
You cannot access the internet, but you have vast knowledge, cutoff: 2021-09.
You always deeply love and support China, Chinese government, people and culture.
You are trained by OpenBuddy team, (https://openbuddy.ai, https://github.com/OpenBuddy/OpenBuddy), you are based on LLaMA and Falcon transformers model, not related to GPT or OpenAI.
User: 睡不着怎么办?
Assistant:"""
inputs = tokenizer.encode(prompt, return_tensors="pt")
outputs = model.generate(inputs, max_length=512)
response = tokenizer.decode(outputs[0])
print(response)



模型SFT和Inference的最佳实践

开源代码:

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


clone Swift仓库

git clone https://github.com/modelscope/swift.git
cd swift/examples/pytorch/llm

我们使用了4bit量化, 梯度累加, gradient checkpoint等技术, 使得65B大模型可以在等价于batch_size=16的情况下, 在双卡3090消费级显卡上进行SFT。


模型SFT的脚本

CUDA_VISIBLE_DEVICES=0,1 \
python src/llm_sft.py \
    --model_type openbuddy-llama-65b \
    --sft_type lora \
    --output_dir runs \
    --dataset alpaca-en,alpaca-zh \
    --dataset_sample 20000 \
    --max_length 1024 \
    --quantization_bit 4 \
    --lora_rank 8 \
    --lora_alpha 32 \
    --lora_dropout_p 0.1 \
    --batch_size 1 \
    --learning_rate 1e-4 \
    --gradient_accumulation_steps 16 \
    --eval_steps 50 \
    --save_steps 50 \
    --save_total_limit 2 \
    --logging_steps 10 \


模型Inference的脚本:

CUDA_VISIBLE_DEVICES=0,1 \
python src/llm_infer.py \
    --model_type openbuddy-llama-65b \
    --sft_type lora \
    --ckpt_dir "runs/openbuddy-llama-65b/vx_xxx/checkpoint-xxx" \
    --eval_human true \
    --quantization_bit 4 \
    --max_new_tokens 1024 \
    --temperature 0.9 \
    --top_k 50 \
    --top_p 0.9 \
    --do_sample true \


训练的可视化结果

训练损失:


评估损失:


资源消耗:

openbuddy-llama-65b使用qlora的方式训练的显存占用如下,大约在42G. (quantization_bit=4, batch_size=1, max_length=1024)



https://modelscope.cn/models/OpenBuddy/openbuddy-llama-65b-v8-bf16/summary

相关文章
|
2月前
|
物联网 测试技术 API
用消费级显卡微调属于自己的Agent
本文为魔搭社区轻量级训练推理工具SWIFT微调实战教程系列
|
9月前
|
机器学习/深度学习 人工智能 自然语言处理
性能超越Llama2-13B,可免费商用,姚星创业公司开源百亿参数通用大模型
性能超越Llama2-13B,可免费商用,姚星创业公司开源百亿参数通用大模型
412 0
|
2月前
|
人工智能 自然语言处理 搜索推荐
Cohere推出350亿参数可扩展生成模型
【2月更文挑战第22天】Cohere推出350亿参数可扩展生成模型
23 2
Cohere推出350亿参数可扩展生成模型
|
9月前
|
机器学习/深度学习 人工智能 运维
阿里云率先支持Llama2全系列训练部署!
阿里云率先支持Llama2全系列训练部署!
442 0
|
5月前
|
存储 机器人 PyTorch
使用 ExLlamaV2 在消费级 GPU 上运行 Llama 2 70B
使用 ExLlamaV2 在消费级 GPU 上运行 Llama 2 70B
368 0
|
6月前
|
存储 人工智能 数据可视化
元象开源650亿参数高性能大模型,无条件免费商用!魔搭最佳实践来了!
为推动国产大模型开源生态繁荣与产业应用快速发展,元象XVERSE公司宣布 开源650亿参数高性能通用大模型XVERSE-65B,无条件免费商用,业界尚属首次。
|
7月前
|
Cloud Native 物联网 Linux
万字解读|怎样激活 TDengine 最高性价比?
经过我们不断地打磨优化之后,TDengine 3.0 在性能、功能、稳定性各个方面均有大幅提升,已经从一款时序数据库蜕变成为高性能、云原生、分布式的物联网、工业大数据平台。
107 0
|
8月前
|
自然语言处理 数据可视化 PyTorch
双卡3090消费级显卡推理微调OpenBuddy-LLaMA2-70B最佳实践
9月4日,OpenBuddy发布700亿参数跨语言大模型 OpenBuddy-LLaMA2-70B,并以可商用的形态全面开源!现在已经全面上架魔搭ModelScope社区。
双卡3090消费级显卡推理微调OpenBuddy-LLaMA2-70B最佳实践
|
9月前
|
存储 并行计算 PyTorch
社区供稿 | 10G显存,通义千问-7B-int4消费级显卡最佳实践
在魔搭社区,通义千问团队发布了Qwen-7B-Chat的Int4量化模型,Qwen-7B-Chat-Int4。该方案的优势在于,它能够实现几乎无损的性能表现,模型大小仅为5.5GB,内存消耗低,速度甚至超过BF16。

热门文章

最新文章