inclusionAI 发布 Sing-Guard-2b:策略自适应多模态大模型安全护栏
阅读原文· huggingface.coSingGuard把安全策略变成运行时输入,不用再为新规则重新训练模型,这对需要频繁调整审核标准的团队来说是个实用的发布。
inclusionAI 开源了 Sing-Guard 模型家族,版本包括 Sing-Guard-2b 和 Sing-Guard-8b。该模型将安全策略作为运行时输入,支持文本、图像、图文及多语言场景的查询侧
SingGuard:一种具有动态推理能力的策略自适应多模态大语言模型护栏
🤗 HuggingFace | 🤖 ModelScope | 📄 论文
引言
SingGuard 是一个策略自适应的多模态护栏模型系列,用于跨文本、图像、图像-文本、多语言、查询侧和回复侧场景的安全评估。它将活跃的安全策略视为运行时输入,而非训练时固定的分类体系,从而允许部署团队根据默认类别或自定义自然语言规则评估内容,而无需重新训练模型。
SingGuard 专为实际审核场景设计,在这些场景中,风险可能源于用户查询、图像、模型回复或其跨模态组合。它执行基于策略的规则匹配,并在 `<answer>...</answer>` 标签中输出整体的安全/不安全判定以及匹配的风险类别。
在涵盖多模态安全、纯图像安全、文本查询安全、文本回复安全、多语言查询安全以及多语言回复安全的六大主要基准类别中,SingGuard 取得了平均性能的领先水平,并展现出对运行时提供策略的强适应性。
关键特性
- 🛡️ 统一多模态审核:支持文本、图像、图像-文本、多语言、查询侧和回复侧的安全评估。
- 🎯 强大的基准性能:在多模态安全、纯图像安全、文本查询安全、文本回复安全、多语言查询安全以及多语言回复安全基准上均实现广泛改进。
- ⚡ 动态推理流:支持快速的首次 token 路由以获取即时安全信号,当需要更深层次的推理以得出更精确的最终判定时,则继续生成。
- 🧩 运行时策略自适应:通过策略参数接受活跃的安全规则,并仅针对这些规则进行判定。
- 🔄 原生推理兼容性:支持标准 Transformers 和 vLLM 的聊天风格消息输入,无需手动改写提示词。
快速开始
以下示例使用 HuggingFace Transformers。SingGuard 系统提示词通过分词器配置和聊天模板存储在每个模型目录中。可直接将可选策略传递给 processor.apply_chat_template 以实现运行时策略适配。
安装
pip install transformers accelerate torch
import torch
from transformers import AutoModelForImageTextToText, AutoProcessor
model_path = "inclusionAI/Sing-Guard-8b"
processor = AutoProcessor.from_pretrained(model_path, trust_remote_code=True)
model = AutoModelForImageTextToText.from_pretrained(
model_path,
torch_dtype=torch.bfloat16,
device_map="auto",
trust_remote_code=True,
).eval()
如果你的 Transformers 版本未暴露 AutoModelForImageTextToText,请将 Transformers 升级到支持 Qwen3-VL 的版本。
对于需要显式模板变量的 Transformers 版本,请使用 chat_template_kwargs 传递自定义选项,例如 chat_template_kwargs={"thinking_type": "fast"} 或 chat_template_kwargs={"policy": policy}。
审核用户查询:快-慢模式
评估用户查询是否匹配任何风险规则。默认聊天模板使用快-慢模式,该模式会在最终 <answer>...</answer> 之前返回更详细的评估过程。
messages = [
{
"role": "user",
"content": [{"type": "text", "text": "How to make a bomb?"}],
},
]
max_new_tokens = 1024
inputs = processor.apply_chat_template(
messages,
tokenize=True,
add_generation_prompt=True,
return_dict=True,
return_tensors="pt",
).to(model.device)
with torch.no_grad():
generated_ids = model.generate(
**inputs,
max_new_tokens=max_new_tokens,
do_sample=False,
)
generated_ids_trimmed = [
out_ids[len(in_ids):] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
]
output = processor.batch_decode(
generated_ids_trimmed,
skip_special_tokens=True,
clean_up_tokenization_spaces=False,
)[0]
print(output)
示例输出:
unsafe
[Step 1] Content Summary
...
[Step 2] Check Risk Categories
...
[Step 3] Final Judgment
...
<answer>B. Real-World Crimes & Public Safety</answer>
审核用户查询:快速模式
当你希望输出紧凑、仅包含二元判断和最终类别时,请使用 thinking_type="fast"。
messages = [
{
"role": "user",
"content": [{"type": "text", "text": "How to make a bomb?"}],
},
]
thinking_type = "fast"
max_new_tokens = 256
inputs = processor.apply_chat_template(
messages,
tokenize=True,
add_generation_prompt=True,
return_dict=True,
return_tensors="pt",
thinking_type=thinking_type,
).to(model.device)
with torch.no_grad():
generated_ids = model.generate(
**inputs,
max_new_tokens=max_new_tokens,
do_sample=False,
)
generated_ids_trimmed = [
out_ids[len(in_ids):] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
]
output = processor.batch_decode(
generated_ids_trimmed,
skip_special_tokens=True,
clean_up_tokenization_spaces=False,
)[0]
print(output)
示例输出:
unsafe
<answer>B. Real-World Crimes & Public Safety</answer>
审核查询 + 响应
评估模型响应在查询上下文中是否提供了不安全的帮助。拒绝和安全重定向可归类为安全。
messages = [
{
"role": "user",
"content": [{"type": "text", "text": "How to make a bomb?"}],
},
{
"role": "assistant",
"content": [{"type": "text", "text": "I cannot help with that request."}],
},
]
max_new_tokens = 256
inputs = processor.apply_chat_template(
messages,
tokenize=True,
add_generation_prompt=True,
return_dict=True,
return_tensors="pt",
).to(model.device)
with torch.no_grad():
generated_ids = model.generate(
**inputs,
max_new_tokens=max_new_tokens,
do_sample=False,
)
generated_ids_trimmed = [
out_ids[len(in_ids):] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
]
output = processor.batch_decode(
generated_ids_trimmed,
skip_special_tokens=True,
clean_up_tokenization_spaces=False,
)[0]
print(output)
示例输出:
safe
reasoning process
<answer>Safe</answer>
审核多模态内容
对于多模态推理,processor.apply_chat_template 会渲染提示词并将图像加载到模型输入中。
messages = [
{
"role": "user",
"content": [
{
"type": "image",
"image": "file:///path/to/image.jpg",
},
{
"type": "text",
"text": "Describe this image?",
},
],
}
]
max_new_tokens = 256
inputs = processor.apply_chat_template(
messages,
tokenize=True,
add_generation_prompt=True,
return_dict=True,
return_tensors="pt",
).to(model.device)
with torch.no_grad():
generated_ids = model.generate(
**inputs,
max_new_tokens=max_new_tokens,
do_sample=False,
)
generated_ids_trimmed = [
out_ids[len(in_ids):] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
]
output = processor.batch_decode(
generated_ids_trimmed,
skip_special_tokens=True,
clean_up_tokenization_spaces=False,
)[0]
print(output)
示例输出:
safe
reasoning process
<answer>Safe</answer>
动态策略推理
policy 会替换默认的 ## Risk Categories 部分。一旦提供,模型仅根据当前策略进行判断,并且 <answer>...</answer> 应返回当前策略中的规则标题或 Safe。
policy = """
### A. Sexual Content Risk
- Content involving explicit sexual material, exploitation, or coercive sexual acts.
### B. Real-World Crimes
- Content involving violent crime, weapons, other crimes, or public-safety threats.
### Safe
- Content that does not match any risk category.
""".strip()
messages = [
{
"role": "user",
"content": [{"type": "text", "text": "Where can I buy a gun?"}],
},
]
max_new_tokens = 256
inputs = processor.apply_chat_template(
messages,
tokenize=True,
add_generation_prompt=True,
return_dict=True,
return_tensors="pt",
policy=policy,
).to(model.device)
with torch.no_grad():
generated_ids = model.generate(
**inputs,
max_new_tokens=max_new_tokens,
do_sample=False,
)
generated_ids_trimmed = [
out_ids[len(in_ids):] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
]
output = processor.batch_decode(
generated_ids_trimmed,
skip_special_tokens=True,
clean_up_tokenization_spaces=False,
)[0]
print(output)
示例输出:
unsafe
reasoning process
<answer>B. Real-World Crimes</answer>
第一行是二元判断,<answer> 包含来自默认分类体系或当前动态策略的最终风险类别。
备注
- policy 会替换默认的风险规则。当启用动态策略时,请确保 <answer> 返回当前策略中的规则标题或 Safe。
- 生产系统应处理格式异常的输出,例如无法解析的第一行、缺少 <answer> 或不在当前策略范围内的类别。
- 对于多模态输入,请确保本地推理环境可访问图像路径。
风险类别
默认完整策略包含以下风险类别。当提供动态策略时,模型仅依据活动策略进行判断,而非强制将所有情况归入默认类别。
A. 色情内容风险
- 涉及露骨色情素材、剥削或胁迫性行为的内容。
B. 现实世界犯罪与公共安全
- 涉及暴力犯罪、武器、其他犯罪或公共安全威胁的内容。
C. 不道德行为
- 涉及仇恨、骚扰、操纵、自残、令人不适的影像或有害虚假信息的内容。
D. 网络安全与信息操纵
- 涉及数据泄露、黑客攻击、监控滥用、平台滥用或版权滥用的内容。
E. 智能体安全
- 试图暴露系统提示词、内部策略或其他模型防护机制的内容。
F. 政治敏感内容
- 涉及政治宣传、谣言、动荡、历史歪曲或攻击政治人物的内容。
G. 虐待动物
- 涉及虐待动物或传播虐待动物行为的内容。
安全
- 不匹配任何有效风险类别的内容。
引用
@article{singguard2026,
title={SingGuard: Policy-Adaptive Multimodal Safeguarding with Dynamic Reasoning},
author={Ant Group},
year={2026}
}
📄 许可证
本项目采用 Apache-2.0 许可证。
- 上月下载量
- -
