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

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

相关文章
|
安全 jenkins 网络安全
Jenkins中node节点添加之SSH方式2
Jenkins中node节点添加之SSH方式2
773 1
|
前端开发 JavaScript Java
校园二手交易系统 毕业设计 JAVA+Vue+SpringBoot+MySQL(一)
校园二手交易系统 毕业设计 JAVA+Vue+SpringBoot+MySQL
835 1
|
7月前
|
人工智能 数据管理 关系型数据库
数据管理DMS支持托管Dify正式开始公测啦!
数据管理DMS支持托管Dify正式开始公测啦!实现Data+AI深度集成开发。提供一站式解决方案,通过DMS使用Dify具备无需登录Dify账号、精选模型、一站式服务等优势,简化开发流程,提升效率。
|
数据库连接 API 数据库
SqlAlchemy 2.0 中文文档(三十)(2)
SqlAlchemy 2.0 中文文档(三十)
224 0
|
自然语言处理 数据可视化 PyTorch
双卡3090消费级显卡推理微调OpenBuddy-LLaMA2-70B最佳实践
9月4日,OpenBuddy发布700亿参数跨语言大模型 OpenBuddy-LLaMA2-70B,并以可商用的形态全面开源!现在已经全面上架魔搭ModelScope社区。
双卡3090消费级显卡推理微调OpenBuddy-LLaMA2-70B最佳实践
|
安全 算法 前端开发
《深入解析Java虚拟机:从JVM体系结构到垃圾回收算法》(一)
《深入解析Java虚拟机:从JVM体系结构到垃圾回收算法》(一)
134 0
|
数据采集 算法 物联网
【算法精讲系列】阿里云百炼SFT微调实践分享
本内容为您提供了百炼平台SFT微调的实践案例,帮助您方便并快速借助模型微调定制化您自己的专属模型。
3342 14
|
机器学习/深度学习 算法 Python
从零开始:构建你的第一个机器学习模型
【7月更文第16天】在机器学习的浩瀚宇宙中,迈出第一步总是充满挑战又激动人心的。本文旨在通过一个简单而经典的案例——线性回归,引领你动手构建首个机器学习模型,让你从零开始,逐步掌握模型构建的基本流程。
426 3
|
域名解析 网络协议 安全
【域名解析DNS专栏】DNS-over-TLS与DNS-over-HTTPS:安全升级新标准
【5月更文挑战第26天】随着网络技术的发展,DNS协议面临安全挑战,DNS-over-TLS (DoT) 和 DNS-over-HTTPS (DoH) 作为解决方案出现,旨在通过加密增强隐私和安全。DoT使用TLS封装DNS查询,防止流量被窥探或篡改;DoH则利用HTTPS隐藏DNS查询。实施DoT需在客户端和服务器间建立TLS连接,DoH需DNS服务器支持HTTPS接口。这两种技术为网络安全提供支持,未来有望更广泛部署,提升网络环境的安全性。
1623 0

热门文章

最新文章