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

本文涉及的产品
模型在线服务 PAI-EAS,A10/V100等 500元 1个月
模型训练 PAI-DLC,5000CU*H 3个月
交互式建模 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

相关文章
|
4月前
|
物联网 测试技术 API
用消费级显卡微调属于自己的Agent
本文为魔搭社区轻量级训练推理工具SWIFT微调实战教程系列
|
4月前
|
存储 缓存 算法
使用Mixtral-offloading在消费级硬件上运行Mixtral-8x7B
Mixtral-8x7B是最好的开放大型语言模型(LLM)之一,但它是一个具有46.7B参数的庞大模型。即使量化为4位,该模型也无法在消费级GPU上完全加载(例如,24 GB VRAM是不够的)。
199 4
|
机器学习/深度学习 存储 人工智能
英伟达 H100 vs. 苹果M2,大模型训练,哪款性价比更高?
训练和微调大型语言模型对于硬件资源的要求非常高。目前,主流的大模型训练硬件通常采用英特尔的CPU和英伟达的GPU。然而,最近苹果的M2 Ultra芯片和AMD的显卡进展给我们带来了一些新的希望。
1058 0
|
1月前
|
机器学习/深度学习 人工智能 安全
同等参数中最强,在苹果15Pro上也能运行!谷歌又“卷”出了端侧小模型 Gemma 2 2B...
在AI技术快速演进的背景下,谷歌推出的Gemma 2 2B模型以其小巧体积和卓越性能引起关注。这款仅20亿参数的轻量级语言模型通过知识蒸馏技术,展现出超越大型模型的能力,在Chatbot Arena测试中获得1130分,超过了GPT-3.5-Turbo等竞争对手。Gemma 2 2B不仅性能出众,还能在多种硬件上高效运行,特别适合本地设备。此外,它的开源特性及易于使用的特性降低了AI应用门槛。伴随Gemma 2 2B发布的还有ShieldGemma和Gemma Scope,前者用于过滤有害内容,后者则提高了模型的透明度和可解释性,共同推动AI技术的负责任发展。
59 2
|
2月前
|
物联网
消费级显卡微调可图Kolors最佳实践!
近期,快手开源了一种名为Kolors(可图)的文本到图像生成模型,该模型具有对英语和汉语的深刻理解,并能够生成高质量、逼真的图像。
|
11月前
|
机器学习/深度学习 人工智能 芯片
一文详解多模态大模型发展及高频因子计算加速GPU算力 | 英伟达显卡被限,华为如何力挽狂澜?
近年来,全球范围内的芯片禁令不断升级,给许多企业和科研机构带来了很大的困扰,需要在技术层面进行创新和突破。一方面,可以探索使用国产芯片和其他不受限制的芯片来替代被禁用的芯片;另一方面,可以通过优化算法和架构等方法来降低对特定芯片的依赖程度。
|
4月前
|
存储 机器人 PyTorch
使用 ExLlamaV2 在消费级 GPU 上运行 Llama 2 70B
使用 ExLlamaV2 在消费级 GPU 上运行 Llama 2 70B
497 0
|
自然语言处理 数据可视化 PyTorch
双卡3090消费级显卡推理微调OpenBuddy-LLaMA2-70B最佳实践
9月4日,OpenBuddy发布700亿参数跨语言大模型 OpenBuddy-LLaMA2-70B,并以可商用的形态全面开源!现在已经全面上架魔搭ModelScope社区。
双卡3090消费级显卡推理微调OpenBuddy-LLaMA2-70B最佳实践
|
存储 并行计算 PyTorch
社区供稿 | 10G显存,通义千问-7B-int4消费级显卡最佳实践
在魔搭社区,通义千问团队发布了Qwen-7B-Chat的Int4量化模型,Qwen-7B-Chat-Int4。该方案的优势在于,它能够实现几乎无损的性能表现,模型大小仅为5.5GB,内存消耗低,速度甚至超过BF16。
|
机器学习/深度学习 人工智能 编解码
双芯片四芯粒互联,寒武纪发布AI训练卡MLU370-X8:性能超越RTX
双芯片四芯粒互联,寒武纪发布AI训练卡MLU370-X8:性能超越RTX
507 0
双芯片四芯粒互联,寒武纪发布AI训练卡MLU370-X8:性能超越RTX