Skip to page content
SAVRN
Search Plan a project

Open-weight model · Text classification

encoder-guard

by Louivislee louivis/encoder-guard

encoder-guard is an open-weight model for text classification from Louivislee, released under MIT License. Its published files total 999.9 MB.

把 Qwen2.5-0.5B 改造为双向 encoder,接 11 个线性分类头,一次前向(17.7ms,p50)输出文本的完整安全画像:违规二判 + TC260 / 语言风险(LM)/ 隐私(PII)/ 内容分级(DC)四套体系的大类与小类 + 安全放行闸门。 - 判别式安全模型,非生成式 guard:无自回归、全头并行输出,单条 17.7ms vs 生成式 8B guard 的 281ms+(同数据重训对比)。 - backbone 为 HF Qwen2Model 结构(Qwen/Qwen2.5-0.5B…

Parameters—
Context—
Weights988.4 MB
Licensemit
AccessOpen weights
Monthly Downloads—

Model Card

By Louivislee, published under mit, revision 12603e549df6.

把 Qwen2.5-0.5B 改造为双向 encoder,接 11 个线性分类头,一次前向(17.7ms,p50)输出文本的完整安全画像:违规二判 + TC260 / 语言风险(LM)/ 隐私(PII)/ 内容分级(DC)四套体系的大类与小类 + 安全放行闸门。 - 判别式安全模型,非生成式 guard:无自回归、全头并行输出,单条 17.7ms vs 生成式 8B guard 的 281ms+(同数据重训对比)。 - backbone 为 HF Qwen2Model 结构(Qwen/Qwen2.5-0.5B 权重转 bidirectional 微调产物),推理时需全可见 attention(双向,非 causal)。 - 生产推理推荐配套的纯 C 本地引擎 verdict(CPU / Apple Metal GPU,零 Python 依赖,支持加密.vmd 打包分发)。 safetythreshold=0.95,校准方法 [email protected](FPR≤2% 约束下最大召回),校准集 finetunefinalv68val(259 safe / 1176 unsafe,实测 FPR 1.93%)。该阈值仅对训练分布可靠:偏离训练分布时 FPR 会上升(见局限)。releasegate=0.8055 为 safe 样本放行闸门,减少深误杀。 - 数据:v69a,17589 条(双模型生成 + 交叉验证过滤);四轨标签张量化 ~160 维 0/1,未标注位置 None 掩码。 - 配置:lr 2e-5、bf16、20 epochs(epoch…

Read Louivislee's full model card

Encoder-Guard v15 (bf16)

把 Qwen2.5-0.5B 改造为双向 encoder,接 11 个线性分类头,一次前向(17.7ms,p50)输出文本的完整安全画像:违规二判 + TC260 / 语言风险(LM)/ 隐私(PII)/ 内容分级(DC)四套体系的大类与小类 + 安全放行闸门。

  • 判别式安全模型,非生成式 guard:无自回归、全头并行输出,单条 17.7ms vs 生成式 8B guard 的 281ms+(同数据重训对比)。
  • backbone 为 HF Qwen2Model 结构(Qwen/Qwen2.5-0.5B 权重转 bidirectional 微调产物),推理时需全可见 attention(双向,非 causal)。
  • 生产推理推荐配套的纯 C 本地引擎 verdict(CPU / Apple Metal GPU,零 Python 依赖,支持加密 .vmd 打包分发)。

输出维度(11 头)

头 维度 说明
safety 1 违规二判概率(配套 threshold.json 阈值)
safety_release 1 放行闸门:safe 且 release 概率低于 release_gate 才放行
tc260_major / tc260_minor 5 / 31 TC260 标准内容风险大类 / 小类
lm_major / lm_minor 10 / 54 语言模型风险(LM01–LM10)大类 / 小类
pii_major / pii_minor 7 / 10 个人信息风险大类 / 小类
dc_type / dc_level / dc_minor 4 / 3 / 36 未成年人内容分级:类型 / 级别(GB/T 42127-2022)/ 子类

类别码表与推理引擎严格同源:见 verdict vd_labels.c。

性能(test 1454 条,macro-F1;同一 v6_9a 数据对 qguard 公平重训后对比)

轨道 Encoder-Guard qguard-0.6B(重训) qguard-8B(重训)
TC260 大类 0.874 0.801 0.669
LM 大类 0.652 0.373 0.490
PII 大类 0.937 — 0.472
DC type / level 0.786 / 0.738 — 0.716 / 0.624
safety F1 0.986 / recall 93.6% — —
延迟 p50 17.7ms 96ms 281ms+

0.5B 判别式 encoder 在全部轨道超过生成式 8B guard:判别任务与判别架构匹配、双向 attention 全句表征、一次前向出全部轨道(生成式需逐 token 或多次调用)、损失工程按轨独立。

使用

方式一:verdict C 引擎(推荐,生产)

# 从仓库打包 .vmd(加密)后直接推理,CPU 或 Metal GPU
./verdict -m model.vmd -t "待检测文本" --device metal
# batch 模式:M4 Max 450 条 1.2s
./verdict -m model.vmd --batch input.txt --batch-out out.jsonl

方式二:PyTorch(本仓库权重)

import torch, json
from transformers import AutoModel
from safetensors.torch import load_file

backbone = AutoModel.from_pretrained(
    "louivis/encoder-guard", subfolder="backbone", torch_dtype=torch.bfloat16)
heads = load_file("heads.safetensors")
threshold = json.load(open("threshold.json"))["safety_threshold"]

tok = AutoTokenizer.from_pretrained("louivis/encoder-guard", subfolder="backbone")
enc = tok("待检测文本", return_tensors="pt", truncation=True, max_length=1024)

with torch.no_grad():
    out = backbone(**enc, torch_dtype=torch.bfloat16)
    # 关键:双向 encoder,mean-pool 全部 token(全可见 attention 下)
    pooled = out.last_hidden_state.mean(dim=1).to(torch.bfloat16)
    safety = torch.sigmoid(pooled @ heads["heads.safety.weight"].T + heads["heads.safety.bias"]).item()
    unsafe = safety >= threshold

backbone 是Qwen2Model 结构但按双向 encoder 训练:直接用默认 causal mask 推理结果无效。生产请使用 verdict 引擎(已正确实现全可见 attention);PyTorch 侧集成需自行构造全可见 4D attention mask。

阈值(threshold.json)

safety_threshold=0.95,校准方法 [email protected](FPR≤2% 约束下最大召回),校准集 finetune_final_v6_8_val(259 safe / 1176 unsafe,实测 FPR 1.93%)。该阈值仅对训练分布可靠:偏离训练分布时 FPR 会上升(见局限)。release_gate=0.8055 为 safe 样本放行闸门,减少深误杀。

训练

  • 数据:v6_9a,17589 条(双模型生成 + 交叉验证过滤);四轨标签张量化 ~160 维 0/1,未标注位置 None 掩码。
  • 配置:lr 2e-5、bf16、20 epochs(epoch 18 停)、Qwen2.5-0.5B backbone 转双向 encoder(is_decoder=false,全可见 attention)后全参微调。
  • 损失:11 头独立 BCE,sum 回传;头结构见 model_config.json 的 head_dims。
  • 完整过程:train_log.jsonl;工程细节与踩坑(全可见 mask 修复、阈值校准失败重来)见 verdict 训练文档。

局限

  1. LM 中频类偏弱:LM.03–08 小类 macro-F1 0.42–0.56,生成风险中频检测覆盖不足。
  2. OOD FPR 翻倍:safety FPR 从域内 5.9% 升到域外 14.5%,固定阈值在分布外可能不再最优。
  3. 未对抗评测:仅在标注体系内做过 OOD 测试,未测真正对抗样本。
  4. 未量化:本 bf16 权重未做 INT8/FP8 量化(仓库另有 fp32 master 用于 C 引擎)。

训练/评估数据为中文为主的双语合成+标注混合分布,主要适用中文内容审核场景;纯英文长文与非上述体系的风险类型(如代码安全)未经评测。

引用

@software{encoder_guard_v15,
  author = {wenqiang li},
  title = {Encoder-Guard v15: bidirectional-encoder content safety classifier},
  year = {2026},
  url = {https://huggingface.co/louivis/encoder-guard}
}

Identity and Version

Repository
louivis/encoder-guard
Publisher
Louivislee
Task
Text classification
Modality
Text
Library
transformers
Parameters
Not stated by the source
Languages
zh, en
Revision
12603e549df6a0c314528027c60a497096fbdb70
First published
2026-10-04
Last updated
2026-10-04

Files and Weights

10 files, 999.9 MB in total. The weights are 2 files totalling 988.4 MB in safetensors.

Weights2 files · 988.4 MB
Configuration3 files · 2.5 KB
Tokenizer2 files · 11.4 MB
Documentation1 file · 5.7 KB
Other1 file · 32.5 KB
Repository1 file · 110 B
Every file
FileTypeSizeSHA-256
backbone/model.safetensorsWeights988.1 MB 03c4cd57a9bc
heads.safetensorsWeights292.6 KB e54da78dfdba
backbone/config.jsonConfiguration1.3 KB —
model_config.jsonConfiguration253 B —
threshold.jsonConfiguration946 B —
README.mdDocumentation5.7 KB —
train_log.jsonlOther32.5 KB —
.gitattributesRepository110 B —
backbone/tokenizer.jsonTokenizer11.4 MB 7d3b8cace3c6
backbone/tokenizer_config.jsonTokenizer704 B —

License and Download

License
mit
Access
Open weights, no gate
Download size
988.4 MB
Download from Louivislee

Released by Louivislee through its official repository on Hugging Face. Read the license.

Built From

Memory Requirements

PrecisionWeights in memory
As published988.4 MB

Weights only, from the published parameter count; the key-value cache and runtime add to this.

Questions About encoder-guard

Can I use encoder-guard commercially?

Yes. encoder-guard is released under MIT License. The MIT License is a short permissive license. It permits commercial use, modification and redistribution, provided the copyright notice and permission notice are included.

Similar Models

Model · Text classification

twitter-roberta-base-sentiment-latest

Cardiff NLP

This is a RoBERTa-base model trained on ~124M tweets from January 2018 to December 2021, and finetuned for sentiment analysis with the TweetEval benchmark. The original Twitter-based RoBERTa model can be found here and the original reference paper is TweetEval. This model is suitable for English. 0 -> Negative; 1 -> Neutral; 2 -> Positive This sentiment analysis model has been integrated into TweetNLP. You can access the demo here.

Open weights cc-by-4.0 514 tokens transformers

Model · Text classification

finbert-tone

Yi

FinBERT is a BERT model pre-trained on financial communication text. The purpose is to enhance financial NLP research and practice. It is trained on the following three financial communication corpus. The total corpora size is 4.9B tokens. More technical details on FinBERT: Click Link This released finbert-tone model is the FinBERT model fine-tuned on 10,000 manually annotated (positive, negative, neutral) sentences from analyst reports. This model achieves superior performance on financial tone analysis task. If you are simply interested in using FinBERT for financial tone analysis, give it a try. If you use the model in your academic work, please cite the following paper: Huang, Allen H.…

Open weights 512 tokens transformers

Model · Text classification

ms-marco-MiniLM-L-6-v2

Joshua

https://huggingface.co/cross-encoder/ms-marco-MiniLM-L-6-v2 with ONNX weights to be compatible with Transformers.js. If you haven't already, you can install the Transformers.js JavaScript library from NPM using: Note: Having a separate repo for ONNX weights is intended to be a temporary solution until WebML gains more traction. If you would like to make your models web-ready, we recommend converting to ONNX using Optimum and structuring your repo like this one (with ONNX weights located in a subfolder named onnx).

Open weights 512 tokens transformers.js

Model · Text classification

twitter-xlm-roberta-base-sentiment

Cardiff NLP

This is a multilingual XLM-roBERTa-base model trained on ~198M tweets and finetuned for sentiment analysis. The sentiment fine-tuning was done on 8 languages (Ar, En, Fr, De, Hi, It, Sp, Pt) but it can be used for more languages (see paper for details). This model has been integrated into the TweetNLP library.

Open weights 514 tokens transformers

Model · Text classification

unbiased-toxic-roberta

Unitary

The huggingface models currently give different results to the detoxify library (see issue here). For the most up to date models we recommend using the models from https://github.com/unitaryai/detoxify Trained models & code to predict toxic comments on 3 Jigsaw challenges: Toxic comment classification, Unintended Bias in Toxic comments, Multilingual toxic comment classification. Built by Laura Hanu at Unitary, where we are working to stop harmful content online by interpreting visual content in context. Score not directly comparable since it is obtained on the validation set provided and not on the test set. To update when the test labels are made available. It is also noteworthy to mention…

Open weights apache-2.0 514 tokens transformers

Model · Text classification

emotion-english-distilroberta-base

Hartmann

With this model, you can classify emotions in English text data. The model was trained on 6 diverse datasets (see Appendix below) and predicts Ekman's 6 basic emotions, plus a neutral class: 1) anger 2) disgust 3) fear 4) joy 5) neutral 6) sadness 7) surprise The model is a fine-tuned checkpoint of DistilRoBERTa-base. For a 'non-distilled' emotion model, please refer to the model card of the RoBERTa-large version. a) Run emotion model with 3 lines of code on single text example using Hugging Face's pipeline command on Google Colab: b) Run emotion model on multiple examples and full datasets (e.g.,.csv files) on Google Colab: Please reach out to [email protected] if you have any…

Open weights 514 tokens transformers