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

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


微调的可视化结果

训练损失(部分):



资源消耗:



相关文章
|
4月前
|
存储 缓存 算法
使用Mixtral-offloading在消费级硬件上运行Mixtral-8x7B
Mixtral-8x7B是最好的开放大型语言模型(LLM)之一,但它是一个具有46.7B参数的庞大模型。即使量化为4位,该模型也无法在消费级GPU上完全加载(例如,24 GB VRAM是不够的)。
199 4
|
数据可视化 物联网 PyTorch
双卡3090消费级显卡 SFT OpenBuddy-LLaMA1-65B 最佳实践
OpenBuddy继接连开源OpenBuddy-LLaMA1-13B、OpenBuddy-LLaMA1-30B后,8月10日,一鼓作气发布了650亿参数的大型跨语言对话模型 OpenBuddy-LLaMA1-65B。
|
机器学习/深度学习 人工智能 算法
阿里公开自研AI集群细节:64个GPU,百万分类训练速度提升4倍
从节点架构到网络架构,再到通信算法,阿里巴巴把自研的高性能AI集群技术细节写成了论文,并对外公布。
阿里公开自研AI集群细节:64个GPU,百万分类训练速度提升4倍
|
2月前
|
异构计算 索引
单卡A100实现百万token推理,速度快10倍,这是微软官方的大模型推理加速
【7月更文挑战第24天】针对大语言模型(LLM)处理长上下文时的计算瓶颈,微软推出MInference,基于动态稀疏注意力加速预填充,使8B参数模型处理1M token从30分钟降至3分钟,推理延迟降低10倍。通过识别注意力矩阵模式(A形、斜线、块稀疏),仅计算关键权重,无需修改预训练或微调。实验证明,MInference在多个任务和模型上保持准确度,但可能不适用所有LLM类型,存在轻微性能损失风险。
94 17
|
2月前
|
物联网
消费级显卡微调可图Kolors最佳实践!
近期,快手开源了一种名为Kolors(可图)的文本到图像生成模型,该模型具有对英语和汉语的深刻理解,并能够生成高质量、逼真的图像。
|
4月前
|
人工智能 文字识别 并行计算
面壁推出超强端侧多模态模型,推理仅需8G显存!
面壁小钢炮 MiniCPM 系列,再次推出超强端侧多模态模型 MiniCPM-Llama3-V 2.5,且支持 30+ 多种语言
|
4月前
|
测试技术 PyTorch 算法框架/工具
魔搭开源推理引擎 DashInfer,助力CPU服务器解锁大模型超强推理
ModelScope推出了预训练大语言模型(LLM)推理引擎DashInfer,采用C++ Runtime编写,提供C++和Python语言接口,具有生产级别的高性能表现,适用于多种CPU架构,包括x86和ARMv9。DashInfer支持连续批处理(Continuous Batching)和多NUMA推理(NUMA-Aware),能够充分利用服务器级CPU的算力,为推理14B及以下的LLM模型提供更多的硬件选择。该项工作已开源。
|
4月前
|
机器学习/深度学习 算法 物联网
LISA微调技术解析:比LoRA更低的显存更快的速度
LISA是Layerwise Importance Sampling for Memory-Efficient Large Language Model Fine-Tuning的简写,由UIUC联合LMFlow团队于近期提出的一项LLM微调技术,可实现把全参训练的显存使用降低到之前的三分之一左右,而使用的技术方法却是非常简单。
|
4月前
|
人工智能 自动驾驶 算法
只要千元级,人人可用百亿级多模态大模型!国产“AI模盒”秒级训练推理
云天励飞,中国AI独角兽,发布“AI模盒”,以千元成本实现多模态大模型的秒级训练推理,降低AI应用门槛。该产品凸显了公司在技术创新与普及中的努力,旨在构建智能城市并重塑日常生活,同时也面临数据安全、隐私保护及人才挑战。
71 3
只要千元级,人人可用百亿级多模态大模型!国产“AI模盒”秒级训练推理
社区供稿 | XTuner发布LLaVA-Llama-3-8B,支持单卡推理,评测和微调
日前,XTuner 团队基于 meta 最新发布的 Llama-3-8B-Instruct 模型训练并发布了最新版多模态大模型 LLaVA-Llama-3-8B, 在多个评测数据集上取得显著提升。