双卡3090消费级显卡推理微调OpenBuddy-LLaMA2-70B最佳实践

简介: 9月4日,OpenBuddy发布700亿参数跨语言大模型 OpenBuddy-LLaMA2-70B,并以可商用的形态全面开源!现在已经全面上架魔搭ModelScope社区。

导读


9月4日,OpenBuddy发布700亿参数跨语言大模型 OpenBuddy-LLaMA2-70B,并以可商用的形态全面开源!现在已经全面上架魔搭ModelScope社区。


70B模型在能力表现上,相较于早前发布的较小规模模型,在文本生成、复杂逻辑推理以及自然语言处理等任务有了非常显著的提升。据其内测用户及多项能力测试指标反馈,目前70B模型在语言能力和逻辑推理能力可对标为GPT3.5的开源平替!OpenBuddy社区希望用开源激发中国大模型行业的潜能。


GitHub链接:https://github.com/OpenBuddy/OpenBuddy


结合官方展示的几个测试案例,70B在 对语言语义深度理解、认知能力和代码能力、复杂内容创作能力均有不俗的表现。



环境配置与安装

  • 本文在8*3090的环境配置下运行 (可以单卡运行, 显存要求16G)
  • 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-llama2-70b模型现已在ModelScope社区开源:

模型链接:https://modelscope.cn/models/OpenBuddy/openbuddy-llama2-70b-v10.1-bf16


模型weights下载

from modelscope.hub.snapshot_download import snapshot_download
model_dir = snapshot_download('OpenBuddy/openbuddy-llama2-70b-v10.1-bf16', 'v1.0.0')



模型推理


推理代码

import torch
from modelscope import AutoTokenizer, snapshot_download
from modelscope import AutoModelForCausalLM
from transformers import BitsAndBytesConfig, TextStreamer
model_id = 'OpenBuddy/openbuddy-llama2-70b-v10.1-bf16'
model_dir = snapshot_download(model_id, 'v1.0.0')
quantization_config = BitsAndBytesConfig(
    load_in_4bit=True,
    bnb_4bit_compute_dtype=torch.bfloat16,
    bnb_4bit_quant_type='nf4',
    bnb_4bit_use_double_quant=True)
tokenizer = AutoTokenizer.from_pretrained(model_dir, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(model_dir, device_map='auto',
                                             torch_dtype=torch.bfloat16, 
                                             quantization_config=quantization_config,
                                             trust_remote_code=True)
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").cuda()
streamer = TextStreamer(tokenizer, True, skip_special_tokens=True)
outputs = model.generate(inputs, max_length=512, streamer=streamer)
response = tokenizer.decode(outputs[0])
# print(response)



资源消耗




模型微调和微调后推理


对openbuddy-llama2-70b的微调,使用魔搭ModelScope社区的微调框架swift。


SWIFT(Scalable lightWeight Infrastructure for Fine-Tuning)是一个可扩展的框架,旨在促进轻量级模型Fine-Tuning。它集成了各种高效的Fine-Tuning方法的实现,采用参数高效、内存高效和时间高效的方法。


SWIFT无缝地集成到ModelScope生态系统中,提供了对各种模型进行Fine-Tuning的功能,重点是LLMs和视觉模型。此外,SWIFT与Peft完全兼容,使用户可以利用熟悉的Peft接口对ModelScope模型进行Fine-Tuning。




微调代码开源地址:

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


clone swift仓库并安装swift

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



模型微调脚本 (qlora)

# 42G VRAM
CUDA_VISIBLE_DEVICES=0,1 \
python src/llm_sft.py \
    --model_type openbuddy-llama2-70b \
    --sft_type lora \
    --template_type openbuddy-llama \
    --dtype bf16 \
    --output_dir runs \
    --dataset alpaca-en,alpaca-zh \
    --dataset_sample 20000 \
    --num_train_epochs 1 \
    --max_length 1024 \
    --quantization_bit 4 \
    --bnb_4bit_comp_dtype bf16 \
    --lora_rank 8 \
    --lora_alpha 32 \
    --lora_dropout_p 0.1 \
    --gradient_checkpointing true \
    --batch_size 1 \
    --weight_decay 0. \
    --learning_rate 1e-4 \
    --gradient_accumulation_steps 16 \
    --max_grad_norm 0.5 \
    --warmup_ratio 0.03 \
    --eval_steps 50 \
    --save_steps 50 \
    --save_total_limit 2 \
    --logging_steps 10 \
    --push_to_hub false \
    --hub_model_id openbuddy-llama2-70b-qlora \
    --hub_private_repo true \
    --hub_token 'your-sdk-token' \


模型微调后的推理脚本

# 40G
CUDA_VISIBLE_DEVICES=0,1 \
python src/llm_infer.py \
    --model_type openbuddy-llama2-70b \
    --sft_type lora \
    --template_type openbuddy-llama \
    --dtype bf16 \
    --ckpt_dir "runs/openbuddy-llama2-70b/vx_xxx/checkpoint-xxx" \
    --eval_human true \
    --quantization_bit 4 \
    --bnb_4bit_comp_dtype bf16 \
    --max_new_tokens 1024 \
    --temperature 0.9 \
    --top_k 50 \
    --top_p 0.9 \
    --do_sample true \


微调的可视化结果

训练损失(部分):



资源消耗:



相关文章
|
数据可视化 物联网 PyTorch
双卡3090消费级显卡 SFT OpenBuddy-LLaMA1-65B 最佳实践
OpenBuddy继接连开源OpenBuddy-LLaMA1-13B、OpenBuddy-LLaMA1-30B后,8月10日,一鼓作气发布了650亿参数的大型跨语言对话模型 OpenBuddy-LLaMA1-65B。
|
物联网 测试技术 API
用消费级显卡微调属于自己的Agent
本文为魔搭社区轻量级训练推理工具SWIFT微调实战教程系列
|
Ubuntu Shell Python
Ubuntu学习笔记(一):pycharm设置快捷启动图标详解
这篇博客详细讲解了如何在Ubuntu 20.04系统中为PyCharm设置快捷启动图标,包括创建.desktop文件、编辑文件内容以及添加到收藏夹的步骤。
1232 0
Ubuntu学习笔记(一):pycharm设置快捷启动图标详解
|
搜索推荐 物联网 PyTorch
Qwen2.5-7B-Instruct Lora 微调
本教程介绍如何基于Transformers和PEFT框架对Qwen2.5-7B-Instruct模型进行LoRA微调。
13263 34
Qwen2.5-7B-Instruct Lora 微调
|
12月前
|
人工智能 物联网 测试技术
FireRedASR:精准识别普通话、方言和歌曲歌词!小红书开源工业级自动语音识别模型
小红书开源的工业级自动语音识别模型,支持普通话、中文方言和英语,采用 Encoder-Adapter-LLM 和 AED 架构,实现 SOTA 性能。
3800 17
FireRedASR:精准识别普通话、方言和歌曲歌词!小红书开源工业级自动语音识别模型
|
人工智能 安全 Cloud Native
|
人工智能 自然语言处理 物联网
llama factory 从数据集起步 跑通 qwen系列开源生成式大模型 微调
`dataset_info.json` 文件用于管理 llama factory 中的所有数据集,支持 `alpaca` 和 `sharegpt` 格式。通过配置此文件,可以轻松添加自定义数据集。数据集的相关参数包括数据源地址、数据集格式、样本数量等,支持 Hugging Face 和 ModelScope 两个平台的数据集仓库。针对不同格式的数据集,提供了详细的配置示例,如 `alpaca` 格式的指令监督微调数据集、偏好数据集等,以及 `sharegpt` 格式的多模态数据集等。今天我们通过自定义数据集的方式来进行qwen2.5_14B_instruct模型进行微调
7204 7
|
缓存 人工智能 PyTorch
LMDeploy 部署 VLMs 的方法与探讨
LMDeploy 部署 VLMs 的方法与探讨 LMDeploy 是一个高效且友好的大型语言模型(LLMs)和视觉-语言模型(VLMs)部署工具箱,由上海人工智能实验室模型压缩和部署团队开发,涵盖了模型量化、离线推理和在线服务等功能。
LMDeploy 部署 VLMs 的方法与探讨