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

简介: 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月前
|
人工智能 JavaScript Java
一个奇怪的现象:Grok 4.5 与 GLM-5.3 Coding,性能相近价格却差一个量级
JeecgBoot AI专题研究 基于 Grok 4.5 与 GLM5.3 Coding 实测对比的 AI 编程工具选型解析 开头先说那个奇怪的现象最近在整理手头的 AI 编程工具时,发现了一个挺有意思的现象:同样是 2026 年的顶级编程模型,一个是海外的通用旗舰、一个是国产的 Coding
386 0
|
程序员 Windows
markdown的基本语法
markdown的基本语法
557 0
|
人工智能 物联网 测试技术
Qwen-Image-Edit:全能图像编辑,驱动内容创作提质增效
通义千问团队开源Qwen-Image-Edit,基于20B模型,支持语义与外观双重编辑、精准中英文文字修改,具备SOTA图像编辑能力,可用于IP创作、风格迁移、文字修复等。
7760 6
|
Shell 测试技术 API
Claude Code 官方内部团队最佳实践!
Immerse,独立开发者、内容创作者、AGI实践者,分享编程、AI、开源等内容。关注公众号“沉浸式趣谈”及个人网站获取更新。欢迎点赞、评论、转发支持!本文介绍Claude Code——智能编程命令行工具及其使用技巧。
8459 0
|
存储 机器学习/深度学习 人工智能
2025年阿里云GPU服务器租用价格、选型策略与应用场景详解
随着AI与高性能计算需求的增长,阿里云提供了多种GPU实例,如NVIDIA V100、A10、T4等,适配不同场景。2025年重点实例中,V100实例GN6v单月3830元起,适合大规模训练;A10实例GN7i单月3213.99元起,适用于混合负载。计费模式有按量付费和包年包月,后者成本更低。针对AI训练、图形渲染及轻量级推理等场景,推荐不同配置以优化成本和性能。阿里云还提供抢占式实例、ESSD云盘等资源优化策略,支持eRDMA网络加速和倚天ARM架构,助力企业在2025年实现智能计算的效率与成本最优平衡。 (该简介为原文内容的高度概括,符合要求的字符限制。)
|
机器学习/深度学习 存储 人工智能
2025年NVIDIA RTX 4090云服务器租赁价格与选型指南
本文探讨了在主流云服务商尚未提供RTX 4090实例的背景下,如何选择高性能GPU服务器。分析了市场现状、替代方案性能,并推荐阿里云的GN7i(NVIDIA A10)、GN6v(NVIDIA V100)等实例,提供了成本优化策略与选型建议,确保用户在AI训练、图形渲染等场景中实现效率和成本的最佳平衡。
|
自然语言处理 数据可视化 PyTorch
双卡3090消费级显卡推理微调OpenBuddy-LLaMA2-70B最佳实践
9月4日,OpenBuddy发布700亿参数跨语言大模型 OpenBuddy-LLaMA2-70B,并以可商用的形态全面开源!现在已经全面上架魔搭ModelScope社区。
双卡3090消费级显卡推理微调OpenBuddy-LLaMA2-70B最佳实践
|
移动开发 JavaScript API
Sprunki Game 实现技术分析及介绍
**Sprunki** 是一款基于音乐创作的游戏,作为经典游戏 **Incredibox** 的粉丝改版,它采用 HTML5 和 JavaScript 构建,通过拖拽式 UI 和模块化声音系统,提供了一个创意十足的音乐创作平台。游戏支持多种设备,并融入了 CSS3 动画和 Web Audio API,增强视觉与音效同步。玩家还可以通过社交媒体分享作品,参与社区互动。Sprunki 不仅是一款游戏,更是一个开放的创作平台。
|
分布式计算 并行计算 编译器
Ascend C的编程模型
Ascend C采用SPMD编程模型,实现多核并行计算。通过将数据切分为多个分片,分配给不同的计算核心(或称为block)并行处理,每个核心根据自身的block_idx执行相同代码但处理不同的数据分片,从而提高计算效率。此模型支持高效的数据并行处理,适用于大规模数据计算任务。
|
小程序 JavaScript Java
微信小程序+SpringBoot接入后台服务,接口数据来自后端
这篇文章介绍了如何将微信小程序与SpringBoot后端服务进行数据交互,包括后端接口的编写、小程序获取接口数据的方法,以及数据在小程序中的展示。同时,还涉及到了使用Vue搭建后台管理系统,方便数据的查看和管理。
微信小程序+SpringBoot接入后台服务,接口数据来自后端

热门文章

最新文章