ZhuoerX commited on
Commit ·
e8ff9d8
1
Parent(s): 1978f8b
First model version
Browse filesFiles changed (6)
- README.md+62
- README_CN.mdAdded+54
- config.jsonAdded+22
- configuration_sing_probe.pyAdded+55
- model.safetensorsAdded+3
- modeling_sing_probe.pyAdded+142
123---license: mit---1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465---tags:- safety- guardrail- hallucination-detection- streaming- ling-3.0license: mitbase_model: inclusionAI/Ling-3.0-tiny---# Ling3-SingProbe<p align="center"><b>English</b> | <a href="./Ling-3.0-tiny-singprobe/README_CN.md">中文</a></p>## Model DescriptionSingProbe is an **intrinsic streaming guardrail** built on `inclusionAI/Ling-3.0-tiny`. Rather than running a separate safety model, this lightweight probe reuses the base model's hidden states during generation to score, at every token, **query intent**, **response unsafety**, and **hallucination risk**. It adds less than 0.5% decode-time overhead.| Base model | Probe parameters | Tapped layers | Outputs || --- | ---: | --- | --- || `inclusionAI/Ling-3.0-tiny-singprobe` | 3.22M | `[6, 14, 22]` | 8 intents + unsafe + hallucination |See the [technical report](https://arxiv.org/abs/2608.30703) for methodology and complete results; implementation details are available at [inclusionAI/SingProbe](https://github.com/inclusionAI/SingProbe).## EvaluationHigher is better for every metric. Results are averages over the benchmark suites specified below.| Task | Metric | Ling-3.0-tiny-singprobe | Reference baseline || --- | --- | ---: | ---: || Query intent classification (6 benchmarks) | F1 | **0.8561** | Qwen3Guard-Stream-8B-strict: 0.8602 || Response safety classification (8 benchmarks) | F1 | **0.8508** | Qwen3Guard-Stream-8B-strict: 0.8486 || Streaming safety (3 benchmarks) | R-AUC / T-AUC | **0.9888 / 0.9479** | Qwen3Guard-Stream-8B-strict: 0.9640 / 0.8893 || Hallucination detection (6 benchmarks) | AUC | **0.7765** | DRIFT: 0.7408 || Deployment characteristic | Result || --- | --- || Benign-response false-positive rate | 0.07% average across 5 datasets || Online hallucination detection | 0.6807 average AUC under free generation || Decode overhead | < 0.5% |## Quick StartSingProbe is supported through the [SGLang integration branch](https://github.com/jinzhen-lin/sglang/tree/token-probe-ling3-flash-main) or [vLLM integration branch](https://github.com/jinzhen-lin/vllm/tree/bailing-v3-token-probe). Load the probe by its Hugging Face ID at server launch:```bashpython -m sglang.launch_server \--model-path inclusionAI/Ling-3.0-tiny \--probe-ckpt inclusionAI/Ling-3.0-tiny-singprobe \--port 30000```The integrations return one score dictionary per generated token (`label_0`–`label_9`). They currently support Ling-3.0 (`BailingMoeV3ForCausalLM`) base models only. Use the exact base-model/probe pair: `inclusionAI/Ling-3.0-tiny` with this checkpoint.## Citation```bibtex@article{singteam2026singprobe,title = {SingProbe Technical Report},author = {Sing Team},journal = {arXiv preprint arXiv:2608.30703},year = {2026},}```
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354# Ling3-SingProbe
<p align="center"><a href="./README.md">English</a> | <b>中文</b></p>
## 模型简介
SingProbe 是基于 `inclusionAI/Ling-3.0-tiny` 构建的**内生式流式安全护栏**。它不需要额外运行一个安全模型,而是复用基座模型生成时已计算的隐藏状态,在每个 token 上同时评估**用户意图**、**响应不安全风险**和**幻觉风险**;Decode 阶段额外开销小于 0.5%。
| 基座模型 | 探测头参数量 | 采样层 | 输出 || --- | ---: | --- | --- || `inclusionAI/Ling-3.0-tiny-singprobe` | 3.22M | `[6, 14, 22]` | 8 类意图 + 不安全 + 幻觉 |
方法与完整实验结果见[技术报告](https://arxiv.org/abs/2608.30703),实现代码见 [inclusionAI/SingProbe](https://github.com/inclusionAI/SingProbe)。
## 核心评测结果
所有指标均为越高越好;结果为下述 benchmark 集的平均值。
| 任务 | 指标 | Ling-3.0-tiny-singprobe | 参考基线 || --- | --- | ---: | ---: || 用户意图分类(6 个 benchmark) | F1 | **0.8561** | Qwen3Guard-Stream-8B-strict:0.8602 || 响应安全分类(8 个 benchmark) | F1 | **0.8508** | Qwen3Guard-Stream-8B-strict:0.8486 || 流式安全检测(3 个 benchmark) | R-AUC / T-AUC | **0.9888 / 0.9479** | Qwen3Guard-Stream-8B-strict:0.9640 / 0.8893 || 幻觉检测(6 个 benchmark) | AUC | **0.7765** | DRIFT:0.7408 |
| 部署特性 | 结果 || --- | --- || 良性响应误报率 | 5 个数据集平均 0.07% || 在线幻觉检测 | 自由生成场景平均 AUC 0.6807 || Decode 开销 | < 0.5% |
## Quick Start
SingProbe 仅支持通过 [SGLang 集成分支](https://github.com/jinzhen-lin/sglang/tree/token-probe-ling3-flash-main) 或 [vLLM 集成分支](https://github.com/jinzhen-lin/vllm/tree/bailing-v3-token-probe) 使用。启动服务时通过 Hugging Face ID 加载探测头:
```bashpython -m sglang.launch_server \ --model-path inclusionAI/Ling-3.0-tiny \ --probe-ckpt inclusionAI/Ling-3.0-tiny-singprobe \ --port 30000```
集成会为每个生成 token 返回一份分数字典(`label_0`–`label_9`),目前仅支持 Ling-3.0 的 `BailingMoeV3ForCausalLM`。请严格匹配基座模型与探测头:本检查点仅用于 `inclusionAI/Ling-3.0-tiny`。
## Citation
```bibtex@article{singteam2026singprobe, title = {SingProbe Technical Report}, author = {Sing Team}, journal = {arXiv preprint arXiv:2608.30703}, year = {2026},}```12345678910111213141516171819202122{ "architectures": [ "SingProbeAttnModel" ], "model_type": "sing_probe_attn", "auto_map": { "AutoConfig": "configuration_sing_probe.SingProbeAttnConfig", "AutoModel": "modeling_sing_probe.SingProbeAttnModel" }, "hidden_size": 1536, "base_model_layer_ids": [ 6, 14, 22 ], "num_attention_heads": 8, "head_dim": 64, "sliding_window": 2048, "num_labels": 10, "base_model_name": "inclusionAI/Ling-3.0-tiny", "torch_dtype": "bfloat16"}12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455from transformers import PretrainedConfig
class SingProbeMlpConfig(PretrainedConfig): model_type = "sing_probe_mlp"
def __init__( self, hidden_size: int = 2560, base_model_layer_ids: list[int] | None = None, intermediate_size: int = 1024, num_labels: int = 10, hidden_act: str = "gelu", base_model_name: str | None = None, **kwargs, ) -> None: super().__init__(**kwargs) self.hidden_size = int(hidden_size) self.base_model_layer_ids = base_model_layer_ids or [] self.intermediate_size = int(intermediate_size) self.num_labels = int(num_labels) self.hidden_act = hidden_act self.base_model_name = base_model_name
@property def input_size(self) -> int: return self.hidden_size * len(self.base_model_layer_ids)
class SingProbeAttnConfig(PretrainedConfig): model_type = "sing_probe_attn"
def __init__( self, hidden_size: int = 2560, base_model_layer_ids: list[int] | None = None, num_attention_heads: int = 4, head_dim: int = 64, sliding_window: int | None = None, num_labels: int = 10, base_model_name: str | None = None, **kwargs, ) -> None: super().__init__(**kwargs) self.hidden_size = int(hidden_size) self.base_model_layer_ids = base_model_layer_ids or [] self.num_attention_heads = int(num_attention_heads) self.head_dim = int(head_dim) self.sliding_window = None if sliding_window is None else int(sliding_window) self.num_labels = int(num_labels) self.base_model_name = base_model_name
@property def input_size(self) -> int: return self.hidden_size * len(self.base_model_layer_ids)123version https://git-lfs.github.com/spec/v1oid sha256:e8fcb3edb20714017a849de31975664468276d93c87fb79563b7826441b826f3size 6434412123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142from typing import Any
import torchimport torch.nn as nnimport torch.nn.functional as Ffrom transformers import PreTrainedModelfrom transformers.activations import ACT2FNfrom transformers.modeling_outputs import TokenClassifierOutput
from .configuration_sing_probe import SingProbeAttnConfig, SingProbeMlpConfig
class SingProbePreTrainedModel(PreTrainedModel): base_model_prefix = "" main_input_name = "hidden_states"
def _init_weights(self, module: nn.Module) -> None: if isinstance(module, nn.Linear): nn.init.normal_(module.weight, mean=0.0, std=0.02) if module.bias is not None: nn.init.zeros_(module.bias) elif isinstance(module, nn.RMSNorm): nn.init.ones_(module.weight)
@staticmethod def _validate_input(config: Any, hidden_states: torch.Tensor) -> None: if hidden_states.shape[-1] != config.input_size: raise ValueError( f"input feature size {hidden_states.shape[-1]} does not match " f"hidden_size * number of tapped layers ({config.input_size})" )
class SingProbeMlpModel(SingProbePreTrainedModel): config_class = SingProbeMlpConfig
def __init__(self, config: SingProbeMlpConfig) -> None: super().__init__(config) self.fc1 = nn.Linear(config.input_size, config.intermediate_size) self.fc2 = nn.Linear(config.intermediate_size, config.num_labels) self.act_fn = ACT2FN[config.hidden_act] self.post_init()
def forward( self, hidden_states: torch.Tensor, return_dict: bool | None = None, **_: Any, ) -> TokenClassifierOutput | tuple[torch.Tensor]: self._validate_input(self.config, hidden_states) hidden_states = hidden_states.to(self.fc1.weight.dtype) hidden_states = self.fc1(hidden_states) hidden_states = self.act_fn(hidden_states) logits = self.fc2(hidden_states) if return_dict is False: return (logits,) return TokenClassifierOutput(logits=logits)
class SingProbeAttnModel(SingProbePreTrainedModel): config_class = SingProbeAttnConfig
def __init__(self, config: SingProbeAttnConfig) -> None: super().__init__(config) if config.num_attention_heads < 1 or config.head_dim < 1: raise ValueError("num_attention_heads and head_dim must be positive") if config.sliding_window is not None and config.sliding_window <= 0: raise ValueError("sliding_window must be positive") self.num_attention_heads = config.num_attention_heads self.head_dim = config.head_dim self.projection_size = self.num_attention_heads * self.head_dim self.proj_q = nn.Linear(config.input_size, self.projection_size, bias=False) self.proj_k = nn.Linear(config.input_size, self.head_dim, bias=False) self.proj_v = nn.Linear(config.input_size, self.head_dim, bias=False) self.o_proj = nn.Linear(self.projection_size, self.projection_size, bias=False) self.norm = nn.RMSNorm(self.projection_size, eps=1e-6) self.classifier = nn.Linear(self.projection_size, config.num_labels) self.post_init()
def _sliding_window_attention( self, query: torch.Tensor, key: torch.Tensor, value: torch.Tensor, ) -> torch.Tensor: seq_len = query.shape[2] window = self.config.sliding_window assert window is not None outputs = [] for start in range(0, seq_len, window): end = min(start + window, seq_len) key_start = max(0, start - window + 1) query_positions = torch.arange(start, end, device=query.device) key_positions = torch.arange(key_start, end, device=query.device) relative_positions = query_positions[:, None] - key_positions attention_mask = (relative_positions >= 0) & (relative_positions < window) attention_mask = attention_mask[None, None] query_block = query[:, :, start:end] key_block = key[:, :, key_start:end] value_block = value[:, :, key_start:end] attention_output = F.scaled_dot_product_attention( query_block, key_block, value_block, attn_mask=attention_mask, enable_gqa=self.num_attention_heads > 1, ) outputs.append(attention_output) return torch.cat(outputs, dim=2)
def forward( self, hidden_states: torch.Tensor, return_dict: bool | None = None, **_: Any, ) -> TokenClassifierOutput | tuple[torch.Tensor]: self._validate_input(self.config, hidden_states) hidden_states = hidden_states.to(self.classifier.weight.dtype) batch_size, seq_len, _ = hidden_states.shape query_features = self.proj_q(hidden_states) query_shape = (batch_size, seq_len, self.num_attention_heads, self.head_dim) kv_shape = (batch_size, seq_len, 1, self.head_dim) query = query_features.view(query_shape).transpose(1, 2) key = self.proj_k(hidden_states).view(kv_shape).transpose(1, 2) value = self.proj_v(hidden_states).view(kv_shape).transpose(1, 2) if self.config.sliding_window and self.config.sliding_window < seq_len: context = self._sliding_window_attention(query, key, value) else: context = F.scaled_dot_product_attention( query=query, key=key, value=value, is_causal=True, enable_gqa=self.num_attention_heads > 1, ) context = context.transpose(1, 2).contiguous() context = context.view(batch_size, seq_len, self.projection_size) hidden_states = self.o_proj(context) + query_features logits = self.classifier(self.norm(hidden_states)) if return_dict is False: return (logits,) return TokenClassifierOutput(logits=logits)README.md+62
README_CN.mdAdded+54
config.jsonAdded+22
configuration_sing_probe.pyAdded+55
model.safetensorsAdded+3
modeling_sing_probe.pyAdded+142