【多模态】swift框架使用qwen2-vl

前言

  前几篇里面学习了常见的一些多模态模型的典型架构和源代码,上一篇里面测试使用了minicpm-v系列模型,在尝试RLHF的时候发现swift特别好用特别全,记录一下对swift的一些使用,欢迎批评指正~
  前一篇里面写了minicpm-v的使用方法,这里主要记录qwen2-vl的使用。

1.swift安装

  • python可以安装3.10版本
  • 就在这几天swift更新了3.0版本,本文使用的是swift的2.6.0版本/2.5.0版本,据群里面说swift的3.0是大更新可能差别挺大的
  • 安装方法(同时在这里安装flash-attention和vllm)
conda create -n swift pyhton==3.10
pip install torch torchvision
pip install flash-attn vllm qwen_vl_utils optimum transformers==4.46.1
#pip install 'ms-swift[llm]' -U  # 这里会安装为swift3
pip install ms-swift==2.*  # 这个是安装swift2
  • 如果网络不好,flash-attn先wget https://github.com/Dao-AILab/flash-attention/releases/download/v2.7.2.post1/flash_attn-2.7.2.post1+cu12torch2.5cxx11abiFALSE-cp310-cp310-linux_x86_64.whl,然后在pip install 这个whl【需要在这个仓库里面找符合本机的whl版本】
  • 一定要注意使用的transformer库必须是4.46的不然用qwen会报错 assertion error rope

2.模型微调

2.1 SFT

一定要注意,使用qwen2-vl需要指定MAX_PIXELS,脚本里面是MAX_PIXELS,py里面是max_pixels,602112=7682828,这个值越大模型看图片看得越清楚,显存开销越大,如果不指定,默认是非常大的可能直接爆显存了

NPROC_PER_NODE=1 CUDA_VISIBLE_DEVICES=0 MAX_PIXELS=602112 swift sft \
--model_type qwen2-vl-7b-instruct \
--model_id_or_path 模型路径 \
--dataset data.jsonl \
--sft_type lora \
--use_flash_attn true \ # 提速
--batch_size 2 \
--lora_target_modules DEFAULT \
--output_dir qwen_lora \
--max_steps 3000 \
--save_total_limit 2 \
--logging_steps 10 \
--gradient_checkpointing false # true的话训练变慢

swift和minivpm-v不一样,使用jsonl格式的数据,类似这样,<image>表示这个位置有一张图,图在后面的images里面指定:

#jsonl格式的数据
{"query": "<image>55555", "response": "66666", "images": ["image_path"]}
{"query": "eeeee<image>eeeee<image>eeeee", "response": "fffff", "history": [], "images": ["image_path1", "image_path2"]}
{"query": "EEEEE", "response": "FFFFF", "history": [["query1", "response2"], ["query2", "response2"]], "images": []}
  • 注意swift里面如果要微调的目标模块使用正则表达式,就要使用target_regex来指定,例如qwen2-vl的指定–target_regex “^(model)(?!.(lm_head|output|emb|wte|shared|mlp|norm)).” \
  • 或者–target_regex "model…*layers.\d+.self_attn.(q_proj|k_proj|v_proj|o_proj)"类似这样的,可以具体看文档和模型结构

2.2 merge lora

在SFT之后合并文件,输出的结果会在运行完之后显示,默认在lora结果存储路径里面有一个-merge的文件夹

#!/bin/bash
CUDA_VISIBLE_DEVICES=0 swift export \
    --ckpt_dir lora结果存储路径 \
    --merge_lora true

2.3 RLHF

支持CPO/DPO/SimPO等,具体参考官方文档

#!/bin/bash
source activate vllm
 
CUDA_VISIBLE_DEVICES=0 \
swift rlhf \
    --rlhf_type cpo \
    --model_type  minicpm-v-v2_6-chat \ # 模型类型,可以在官方文档支持的模型里面找,或者报错了在报错信息里面找哈哈哈
    --model_id_or_path 模型存储路径/ \
    --beta 0.1 \
    --rpo_alpha 0.1 \
    --sft_type  lora \
    --dataset dataset_dpo.jsonl \
    --lora_target_modules  DEFAULT  \
    --max_steps 500 \
    --save_steps 250 \
    --batch_size  2  \
    --learning_rate  5e-5  \
    --gradient_checkpointing false \
    --warmup_ratio  0.03  \
    --save_total_limit  2 \
    --output_dir output \
    --logging_steps 10

注意RHLF的数据集格式:

{"system": "123", "query": "11111", "response": "22222", "rejected_response": "33333", "images": ["image_path"], "history": [["query1", "response1"], ["query2", "response2"]]}
{"system": "123", "query": "aaaaa", "response": "bbbbb", "rejected_response": "ccccc", "images": ["image_path"], "history": [["query1", "response1"], ["query2", "response2"]]}
{"system": "123", "query": "AAAAA", "response": "BBBBB", "rejected_response": "CCCCC", "images": ["image_path"], "history": [["query1", "response1"], ["query2", "response2"]]}

3. 模型量化和推理

3.1 qwen2-vl量化

  • qwen2-vl的int4的模型qlora微调后不支持merge并且推理很慢,官方推荐先lora在merge再量化
  • 原始模型微调后,不支持autoawq量化,支持gptq量化,同样需要指定最大像素,否则爆显存
  • gptq量化时load_dataset_config为true使用训练时候的数据集来进行量化
  • gptq量化后准确率损失不大

CUDA_VISIBLE_DEVICES=0 MAX_PIXELS=1003520 swift export \
    --ckpt_dir '训练好的模型路径' \
    --merge_lora true --quant_bits 4 \
    --load_dataset_config true --quant_method gptq

3.2 模型推理

-----flash-attn用于swift推理
kwargs = {}
# kwargs['use_flash_attn'] = True  # use flash_attn
model_id_or_path = None
model, tokenizer = get_model_tokenizer(model_type, model_id_or_path=model_id_or_path,
                                       model_kwargs={'device_map': 'auto'}, **kwargs)
                                       
----flash-attn用于qwen2-vl
model_name = "。。。。。。"
model = Qwen2VLForConditionalGeneration.from_pretrained(model_name, torch_dtype="auto", device_map="auto",attn_implementation="flash_attention_2")
model.eval()


-----minicpm-v的推理也flash-attention-2加速
model = AutoModel.from_pretrained('。。。。。。', trust_remote_code=True,attn_implementation='flash_attention_2')

3.3 CLI推理

数据集格式和训练集一样

{"query": "<image>55555", "response": "66666", "images": ["image_path"]}
{"query": "eeeee<image>eeeee<image>eeeee", "response": "fffff", "history": [], "images": ["image_path1", "image_path2"]}
{"query": "EEEEE", "response": "FFFFF", "history": [["query1", "response2"], ["query2", "response2"]], "images": []}

推理脚本,里面的val_dataset是待推理的数据集,result_dir是输出的结果保存的地址,输出的是一个jsonl文件

CUDA_VISIBLE_DEVICES=0 MAX_PIXELS=602112 swift infer --ckpt_dir 模型地址 --use_flash_attn true --val_dataset data_need_infer.jsonl --save_result true --result_dir result_output
### Qwen2-7B 技术文档下载与使用教程 #### 创建环境并安装依赖 为了顺利运行Qwen2-VL-7B模型,建议创建一个新的Conda虚拟环境来管理项目所需的Python版本及其库文件。具体命令如下所示: ```bash conda create --name qwen2-vl python=3.10 conda activate qwen2-vl pip install git+https://github.com/huggingface/transformers pip install qwen-vl-utils pip install torch==2.4.1 torchvision==0.19.1 accelerate ``` 这些操作能够确保环境中包含了执行图像理解任务所必需的各种软件包[^1]。 #### 获取官方技术文档 对于希望深入了解该模型架构设计以及其工作原理的研究人员来说,可以访问Hugging Face平台上的[Qwen2-VL-7B Instruct页面](https://huggingface.co/Qwen/Qwen2-VL-7B-Instruct/tree/main)获取详细的API说明和技术细节描述。此链接不仅提供了关于如何加载预训练权重的信息,还列出了支持的功能列表和参数配置选项。 #### 实现图片识别功能 当一切准备就绪之后,就可以着手编写代码实现具体的视觉语言处理逻辑了。下面是一个简单的例子展示怎样利用这个强大的工具来进行多模态数据分析: ```python from transformers import AutoModelForVision2Seq, AutoProcessor model_name_or_path = "Qwen/Qwen2-VL-7B-Instruct" processor = AutoProcessor.from_pretrained(model_name_or_path) model = AutoModelForVision2Seq.from_pretrained(model_name_or_path) def recognize_image(image_path): image = Image.open(image_path).convert('RGB') inputs = processor(images=image, return_tensors="pt") outputs = model.generate(**inputs) generated_text = processor.decode(outputs[0], skip_special_tokens=True) return generated_text ``` 上述脚本定义了一个名为`recognize_image()` 的函数,它接收一张本地存储路径下的图片作为输入,并返回由模型预测得到的文字解释[^2]。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值