LLM - 配置 ModelScope SWIFT 测试 Qwen2-VL 模型推理(Infer) 教程 (1)

欢迎关注我的CSDN:https://spike.blog.csdn.net/

本文地址:https://spike.blog.csdn.net/article/details/142827217

免责声明:本文来源于个人知识与公开资料,仅用于学术交流,欢迎讨论,不支持转载。


SWIFT 即 Scalable lightWeight Infrastructure for FineTuning (可扩展轻量级微调基础设施),是高效、轻量级的模型微调和推理框架,支持大语言模型(LLM) 和 多模态大型模型(MLLM) 的训练、推理、评估和部署。可以将 SWIFT 框架直接应用到研究和生产环境中,实现从模型训练和评估到应用的完整工作流程。

1. 配置环境

构建 Conda 环境:

bash 复制代码
conda create -n swift python=3.10
conda activate swift
pip3 install torch torchvision torchaudio

验证 Torch 环境:

bash 复制代码
# python
import torch
print(torch.__version__)
print(torch.cuda.is_available())

使用源码,安装 swift 依赖包:

bash 复制代码
git clone https://github.com/modelscope/ms-swift.git
cd ms-swift
pip install -e .[llm]

pip install git+https://github.com/huggingface/transformers.git@21fac7abba2a37fae86106f87fcf9974fd1e3830
pip install pyav qwen_vl_utils

安装 LMDeploy 和 vLLM 依赖包:

bash 复制代码
# 需要依赖 torch<=2.3.1
pip install lmdeploy==0.6.1 -i http://mirrors.aliyun.com/pypi/simple/
# 需要依赖 torch==2.4.0
pip install vllm==0.6.2 -i http://mirrors.aliyun.com/pypi/simple/

注意:优先安装 lmdeploy,再安装 vllm,否则 vllm 无法使用,报错。

2. 准备模型

导出 ModelScope 模型位置的环境变量 MODELSCOPE_CACHE,即:

bash 复制代码
export MODELSCOPE_CACHE="[your path]/modelscope_models/"

注意:模型是存储在 MODELSCOPE_CACHEhub 文件夹中

目录结构:

bash 复制代码
[your path]/modelscope_models
├── [4.0K]  AI-ModelScope
│   └── [4.0K]  LaTeX_OCR
├── [4.0K]  hub
│   └── [  33]  qwen -> [your path]/llm/Qwen
├── [4.0K]  offload_cache

之前下载过 Qwen2-VL-7B-Instruct,直接使用 软链接 至 hub 目录,即可:

bash 复制代码
cd [your path]/modelscope_models/hub
ln -s [your path]/llm/Qwen qwen  # 构建软链接

注意:模型的类型 Qwen 需要小写 qwen

3. 命令行调用模型

直接使用 swift infer 即可调用:

bash 复制代码
CUDA_VISIBLE_DEVICES=0 swift infer --model_type qwen2-vl-7b-instruct

参考 infer 脚本的全部参数:

bash 复制代码
python [your path]/llm/ms-swift/swift/cli/infer.py --help

测试文本:

bash 复制代码
<<< 你是谁?
我是来自阿里云的大规模语言模型,我叫通义千问。

测试图像:

bash 复制代码
<<< <image><image>这两张图片有什么区别
Input an image path or URL <<< [your path]/llm/vision_test_data/image1.png
Input an image path or URL <<< [your path]/llm/vision_test_data/image2.png
这两张图片的区别在于场景和人物的姿势不同。第一张图片中,人物站在木梯旁边,脚踩在木梯上,脚上穿着一双米色的凉鞋,凉鞋上有花朵装饰。第二张图片中,人物坐在床上,脚上穿着白色的袜子,旁边有一个蓝色的哆啦A梦玩偶。

两张图像,如下:

测试数学:

bash 复制代码
<<< <image>计算结果是多少
Input an image path or URL <<< [your path]/llm/vision_test_data/math.png

输入复杂的数学运算,即:

输出 (非常厉害):

首先,我们逐步计算这个表达式:

1.计算除法部分:

  • 6 13 ÷ 9 26 = 6 13 × 26 9 = 6 × 26 13 × 9 = 2 × 26 13 × 3 = 52 39 = 4 3 \frac{6}{13} \div \frac{9}{26} = \frac{6}{13} \times \frac{26}{9} = \frac{6 \times 26}{13 \times 9} = \frac{2 \times 26}{13 \times 3} = \frac{52}{39} = \frac{4}{3} 136÷269=136×926=13×96×26=13×32×26=3952=34
  • 18 35 ÷ 0.6 × 2 3 = 18 35 ÷ 3 5 × 2 3 = 18 35 × 5 3 × 2 3 = 18 × 5 × 2 35 × 3 × 3 = 180 315 = 4 7 \frac{18}{35} \div 0.6 \times \frac{2}{3} = \frac{18}{35} \div \frac{3}{5} \times \frac{2}{3} = \frac{18}{35} \times \frac{5}{3} \times \frac{2}{3} = \frac{18 \times 5 \times 2}{35 \times 3 \times 3} = \frac{180}{315} = \frac{4}{7} 3518÷0.6×32=3518÷53×32=3518×35×32=35×3×318×5×2=315180=74

2.现在,我们将这些结果代入原始表达式:

  • 2 − 4 3 − 2 3 + 4 7 2 - \frac{4}{3} - \frac{2}{3} + \frac{4}{7} 2−34−32+74

3.将分数转换为相同的分母,以便进行加减运算:

  • 2 − 4 3 − 2 3 + 4 7 = 14 7 − 4 3 − 2 3 + 4 7 2 - \frac{4}{3} - \frac{2}{3} + \frac{4}{7} = \frac{14}{7} - \frac{4}{3} - \frac{2}{3} + \frac{4}{7} 2−34−32+74=714−34−32+74

4.将分数相加:

  • 14 7 + 4 7 = 18 7 \frac{14}{7} + \frac{4}{7} = \frac{18}{7} 714+74=718
  • − 4 3 − 2 3 = − 6 3 = − 2 -\frac{4}{3} - \frac{2}{3} = -\frac{6}{3} = -2 −34−32=−36=−2

5.将结果相减: 18 7 − 2 = 18 7 − 14 7 = 4 7 \frac{18}{7} - 2 = \frac{18}{7} - \frac{14}{7} = \frac{4}{7} 718−2=718−714=74

所以,计算结果是 4 7 \frac{4}{7} 74。

测试OCR:

bash 复制代码
<<< <image>OCR识别图像文字
Input an image path or URL <<< [your path]/llm/vision_test_data/ocr.png
Top Tips For
Pole Dancing In Heels

测试图像:

测试视频:

bash 复制代码
<<< <video>描述视频内容
Input a video path or URL <<< [your path]/llm/vision_test_data/video4.mp4
[INFO:swift] Using environment variable `NFRAMES`, Setting nframes: 50.
视频中展示了一位穿着白色裤子和拖鞋的人,她正在用拖鞋摩擦地面,然后将拖鞋放在地上,接着她用拖鞋摩擦地面,最后她将拖鞋放在地上。

注意:避免显存溢出,只使用 50 帧,export NFRAMES=50

视频:

4. 源码调用模型

调用源码:

bash 复制代码
python my_test.py

参考 my_test.py 源码:

python 复制代码
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '0'

from swift.llm import (
    get_model_tokenizer, get_template, inference, ModelType,
    get_default_template_type, inference_stream
)
from swift.utils import seed_everything
import torch
print(f"[Info] torch: {torch.__version__}")

model_type = ModelType.qwen2_vl_7b_instruct
template_type = get_default_template_type(model_type)
print(f'template_type: {template_type}')

model, tokenizer = get_model_tokenizer(
    model_type, torch.bfloat16,
    model_kwargs={
        'device_map': 'auto', 
    },
)
model.generation_config.max_new_tokens = 256
template = get_template(template_type, tokenizer)
seed_everything(42)

query = """<img>[your path]/llm/vision_test_data/image2.png</img>女孩的袜子是什么颜色?"""
response, history = inference(model, template, query)
print(f'query: {query}')
print(f'response: {response}')

# 流式
query = '女孩在做什么事情?'
gen = inference_stream(model, template, query, history)
print_idx = 0
print(f'query: {query}\nresponse: ', end='')
for response, history in gen:
    delta = response[print_idx:]
    print(delta, end='', flush=True)
    print_idx = len(response)
print()
print(f'history: {history}')

注意:推理 python 不能使用 JupyterLab 调用,会有异常。

输出:

bash 复制代码
query: <img>[your path]/llm/vision_test_data/image2.png</img>女孩的袜子是什么颜色?
response: 女孩的袜子是白色的。
query: 女孩在做什么事情?
response: 女孩正在弹奏一把电吉他。
history: [['<img>[your path]/llm/vision_test_data/image2.png</img>女孩的袜子是什么颜色?', '女孩的袜子是白色的。'], ['女孩在做什么事情?', '女孩正在弹奏一把电吉他。']]

视频:

其他:

相关推荐
NAGNIP3 小时前
一文搞懂深度学习中的通用逼近定理!
人工智能·算法·面试
冬奇Lab4 小时前
一天一个开源项目(第36篇):EverMemOS - 跨 LLM 与平台的长时记忆 OS,让 Agent 会记忆更会推理
人工智能·开源·资讯
冬奇Lab4 小时前
OpenClaw 源码深度解析(一):Gateway——为什么需要一个"中枢"
人工智能·开源·源码阅读
AngelPP8 小时前
OpenClaw 架构深度解析:如何把 AI 助手搬到你的个人设备上
人工智能
宅小年8 小时前
Claude Code 换成了Kimi K2.5后,我再也回不去了
人工智能·ai编程·claude
九狼8 小时前
Flutter URL Scheme 跨平台跳转
人工智能·flutter·github
ZFSS8 小时前
Kimi Chat Completion API 申请及使用
前端·人工智能
天翼云开发者社区9 小时前
春节复工福利就位!天翼云息壤2500万Tokens免费送,全品类大模型一键畅玩!
人工智能·算力服务·息壤
知识浅谈9 小时前
教你如何用 Gemini 将课本图片一键转为精美 PPT
人工智能
Ray Liang10 小时前
被低估的量化版模型,小身材也能干大事
人工智能·ai·ai助手·mindx