pnflly/VisualQuality-R1-7B
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

VisualQuality-R1-7B 昇腾 NPU 适配说明

一、模型简介

VisualQuality-R1-7B 是由 TianheWu 团队提出的首个基于 RL2R(强化学习到推理,GRPO 训练)的无参考图像质量评估(NR-IQA)模型,论文以 Spotlight 收录于 NeurIPS 2025。模型既能对图像质量输出 1~5 分的量化评分,也能通过 <think> 推理过程给出质量描述,是当前 IQA 领域兼具评分与可解释性的代表性工作之一。

从技术结构上看,它是在 Qwen/Qwen2.5-VL-7B-Instruct 基础上改造的多模态语言模型,架构类为 Qwen2_5_VLForConditionalGeneration(model_type=qwen2_5_vl),关键超参如下:

超参数值
语言层数 (num_hidden_layers)28
隐层维度 (hidden_size)3584
中间层维度 (intermediate_size)18944
注意力头 / KV 头28 / 4 (GQA)
词表大小 (vocab_size)152064
最大位置编码128000
视觉编码器 hidden / patch1280 / 14
精度 (torch_dtype)bfloat16
权重总量约 16.6 GB(4 个 safetensors 分片)

模型权重结构为标准的 Qwen2.5-VL 布局:visual.* 视觉编码器(约 390 个张量)+ model.layers.*(28 层 LLM 主干)+ lm_head。官方以 MIT 许可发布,配套推理脚本在 GitHub(TianheWu/VisualQuality-R1)。

推理采用固定提示词模板:非思考模式只输出 <answer></answer> 包裹的一个浮点评分(1~5,两位小数),思考模式额外在 <think></think> 中先给出推理过程。本次适配基于该非思考模板做服务化打分,同时验证了思考模式的多模态生成链路。

二、验证环境

本次适配在昇腾 910 上完成端到端验证,环境版本如下:

组件版本
操作系统openEuler(Linux aarch64)
NPU 硬件Ascend 910B(64GB HBM/卡)
CANN8.5.1
Python3.11.14
vLLM0.18.0
vLLM-Ascend0.18.0
PyTorch / torch_npu2.9.0+cpu / 2.9.0.post1
Transformers4.57.6
OpenAI SDK2.24.0

权重通过 GitCode HF 镜像仓(gitcode.com/hf_mirrors/TianheWu/VisualQuality-R1-7B.git)拉取,与 HuggingFace 同源,文件包括 config.json、tokenizer.json、preprocessor_config.json、chat_template.json 及 4 个 model-*.safetensors 分片。

三、服务启动

inference.py 提供 serve(启动服务)、score(连接服务打分)、chat(多模态对话)、offline(离线推理)四个子命令。启动服务的命令如下:

python3 inference.py --mode serve \
  --model-dir /path/to/VisualQuality-R1-7B \
  --served-model-name TianheWu/VisualQuality-R1-7B \
  --devices 0 \
  --port 8016 \
  --dtype bfloat16 \
  --tensor-parallel-size 1 \
  --max-model-len 32768 \
  --gpu-memory-utilization 0.85

等价的手工启动方式(vllm serve):

export ASCEND_RT_VISIBLE_DEVICES=0
vllm serve /path/to/VisualQuality-R1-7B \
  --served-model-name TianheWu/VisualQuality-R1-7B \
  --dtype bfloat16 --tensor-parallel-size 1 \
  --max-model-len 32768 --gpu-memory-utilization 0.85 \
  --host 0.0.0.0 --port 8016

说明:

  • 7B 规模单卡即可承载,张量并行数取 1;如需更高吞吐可升至 2 卡并把 --devices 设为 0,1;
  • 权重为 bfloat16,须与 --dtype bfloat16 保持一致;
  • --max-model-len 32768 已覆盖 512×512 输入图(约 1.3k 视觉 token)与较长推理文本的组合;
  • 服务以 OpenAI 兼容协议暴露,模型名固定为 TianheWu/VisualQuality-R1-7B。

四、Smoke 验证

服务就绪后,先做健康检查:

curl -s http://127.0.0.1:8016/v1/models

返回的 data[0].id 应为 TianheWu/VisualQuality-R1-7B,max_model_len 为 32768。随后发起一次多模态请求:

curl -s http://127.0.0.1:8016/v1/chat/completions \
  -H 'Content-Type: application/json' \
  -d '{
    "model": "TianheWu/VisualQuality-R1-7B",
    "messages": [{"role":"user","content":[
      {"type":"image_url","image_url":{"url":"data:image/png;base64,..."}},
      {"type":"text","text":"What is your overall rating on the quality of this picture? Please only output the final answer with only one score in <answer> </answer> tags."}
    ]}],
    "max_tokens": 64, "temperature": 0.1
  }'

预期返回 choices[0].message.content 中包含 <answer>4.xx</answer> 形式的评分,usage 字段给出 prompt/completion token 计数(图像会编码为视觉 token 计入 prompt_tokens)。

五、性能参考

在单卡 Ascend 910B(64GB HBM)、ASCEND_RT_VISIBLE_DEVICES=0、--max-model-len 32768、--gpu-memory-utilization 0.85 配置下的实测数据如下:

指标实测值
权重加载耗时(4 分片)34.1 s(约 15.6 GB bf16)
启动到服务就绪(含 ACL Graph 编译)约 2 min
单图非思考打分端到端时延0.23 ~ 0.42 s
单图思考模式端到端时延(88 token)1.86 s
并发压测吞吐(4 worker × 20 请求)2.0 req/s,21.9 tok/s
平均单请求时延(压测)502 ms
NPU HBM 占用(服务驻留)51.7 GB / 64 GB
vLLM 引擎进程显存占用48.4 GB
压测期间整卡功耗峰值276 W

说明:npu-smi 每 2s 采样一次,单次打分仅几百毫秒,AICore 瞬时峰值难以被定时采样捕获;压测阶段功耗由空载 170 W 升至 276 W,可侧面反映推理负载。视觉编码器(ViT)使用 TORCH_SDPA 注意力后端,与语言主干(ACL Graph 捕获)并行调度。

六、精度测评

使用 4 张合成测试图(512×512)做非思考模式评分,max_tokens=32、temperature=0.1,结果如下:

测试图质量特征模型评分
clean_1.png高对比清晰图形3.50
blurred_1.png高斯模糊(radius=12)0.50
noisy_1.png高斯噪声(σ=60)1.00
compressed_1.jpgJPEG 低码率压缩(quality=10)3.50

评分与图像质量单调对应:清晰图最高(3.50),模糊图最低(0.50),噪声图次低(1.00),压缩图仍有较清晰主体故得 3.50。所有输出均严格落在 <answer>X.XX</answer> 模板内,未出现标签外输出。

思考模式一致性:对 clean_1.png 的思考模式输出先给出"图像清晰、轮廓分明、无可见伪影"的推理,再给出 <answer>3.80</answer>,与非思考模式的 3.50 同区间,二者一致性良好。评分绝对值的轻微浮动属采样随机性(do_sample=True),不影响相对排序。

七、注意事项

  • 提示词必须使用官方模板:非思考模式为 {Question} Please only output the final answer with only one score in <answer> </answer> tags.,其中 Question 固定为图像质量评估问题原文;不要自行改写,否则评分可能落到标签之外;
  • 服务进程对 HF Hub 不可达环境敏感,务必设置 HF_HUB_OFFLINE=1 / TRANSFORMERS_OFFLINE=1,避免 vLLM 启动时尝试访问 huggingface.co 导致超时重试;
  • 本项目使用 vllm serve 子命令启动(内部完成 model_tag 到 model 的解析);直接 python3 -m vllm.entrypoints.openai.api_server 会把位置参数留在 model_tag 而 model 落回默认值 Qwen/Qwen3-0.6B,导致误连 HF 下载;
  • 多卡并行时如遇 ACL Graph 通信受限告警(FFTS+),可设置 HCCL_OP_EXPANSION_MODE=AIV 提升可用流数量;
  • 视觉编码器使用 TORCH_SDPA 注意力后端,首次启动会进行 torch.compile 与 ACL Graph 捕获,耗时较久属正常,日志出现 Application startup complete. 后再发起请求;
  • 图像尺寸建议控制在 max_pixels 限制内(512×512 即可),过大输入会显著拉长视觉 token 数并增加 prefill 时延。