蚂蚁 inclusionAI 发布策略自适应多模态安全护栏模型 Sing-Guard-8b
阅读原文· huggingface.co蚂蚁集团开源的多模态内容审核模型,最大亮点是运行时动态注入安全策略而不需重训,对需要灵活定制审核规则的团队是低门槛的高分工程实现。
SingGuard 是蚂蚁 inclusionAI 推出的策略自适应多模态大语言模型安全护栏模型族(版本 Sing-Guard-8b),支持纯文本、纯图像、图文混合、多语言查询与回复的安全评估。其核心设计将安全策略作为运行时输入,部署团队可基于默认分类或自定义自然语言规则评估内容,无需重新训练模型。模型内置 fast-slow 动态推理流程:首 token 路由快速输出安全信号,需深度推理时继续生成更精确的最终判断。在涵盖多模态安全、纯图像安全、文本查询与回复安全、多语言查询与回复安全的六大基准测试上取得平均 SOTA 性能,并已开源至 HuggingFace 与 ModelScope。
SingGuard:一种具有动态推理能力的策略自适应多模态大语言模型护栏
🤗 HuggingFace | 🤖 ModelScope | 📄 论文
引言
SingGuard 是一个策略自适应的多模态护栏模型系列,用于跨文本、图像、图文、多语言、查询侧和响应侧场景的安全评估。它将生效的安全策略视为运行时输入,而非训练时固定的分类法,使得部署团队可以对照默认类别或自定义的自然语言规则来评估内容,而无需重新训练模型。
SingGuard 专为实际审核场景设计,在这些场景中风险可能来自用户查询、图像、模型响应或其跨模态组合。它执行基于策略的规则匹配,并在 `<answer>...</answer>` 标签中输出整体安全/不安全判断以及匹配的风险类别。
在覆盖多模态安全、纯图像安全、文本查询安全、文本响应安全、多语言查询安全以及多语言响应安全的六大基准类别中,SingGuard 实现了平均性能的最先进水平,并展现出对运行时提供策略的强适应能力。
主要特性
- 🛡️ 统一多模态审核:支持文本、图像、图文、多语言、查询侧和响应侧的安全评估。
- 🎯 强大的基准性能:在多模态安全、纯图像安全、文本查询安全、文本响应安全、多语言查询安全和多语言响应安全基准上实现了广泛改进。
- ⚡ 动态推理流程:支持快速首 token 路由以立即生成安全信号,在需要更深入推理以获得更精确的最终判断时,则继续生成。
- 🧩 运行时策略适应:通过 `policy` 参数接受有效安全规则,并仅对照这些规则进行判断。
- 🔄 原生推理兼容性:支持标准 Transformers 和 vLLM 聊天风格消息输入,无需手动重写提示词。
快速开始
下面示例使用 HuggingFace Transformers。SingGuard 系统提示词通过 tokenizer 配置和对话模板存储在每个模型目录中。可选择将 policy 直接传递给 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 替换默认的 ## 风险类别 部分。提供后,模型仅根据当前策略进行判断,<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 许可证。
- 上月下载量
- -
