本文档记录 madhurjindal/autonlp-Gibberish-Detector-492513457 Gibberish(乱码)检测模型在昇腾 NPU(Ascend 910B3)上的迁移适配、精度评测与性能验证结果。
该模型基于 DistilBERT(6 层 Transformer,768 维隐藏层),在 AutoNLP 平台上训练,支持 4 种文本质量分类:clean(正常文本)、mild gibberish(轻度乱码)、noise(噪声)、word salad(词语拼盘)。适用于文本预处理管道中自动过滤乱码和噪声输入,提升下游 NLP 任务的数据质量。
DistilBERT 是 BERT 的知识蒸馏版本(6 层 vs 12 层),参数量减半但保持 95% 的精度,推理速度提升约 2×。
相关获取地址:
| 组件 | 版本 |
|---|---|
torch | 2.8.0 |
torch_npu | 2.8.0.post4 |
transformers | 5.8.1 |
CANN | 8.5.1 |
8 × Ascend 910B3conda create -n madhurjindal--autonlp-Gibberish-Detector-492513457 python=3.11 -y
conda activate madhurjindal--autonlp-Gibberish-Detector-492513457
pip install torch==2.8.0 torch_npu==2.8.0.post4 \
-i https://pypi.tuna.tsinghua.edu.cn/simple
pip install transformers numpy \
-i https://pypi.tuna.tsinghua.edu.cn/simplepython inference.py --text "This is a normal sentence." --device npu
python inference.py --text "asdf jkl; qwerty poiu yxcv." --device npu编程接口:
from inference import PersonalityClassifier as GibberishDetector
clf = GibberishDetector(
model_path="./madhurjindal--autonlp-Gibberish-Detector-492513457", device="npu"
)
results, probs = clf.predict(["This is clean text.", "asdfghjkl qwerty"])
# results[0] → {'clean': 0.98, 'mild gibberish': 0.01, ...}python inference.py --text "The cat sat on the mat." --device npu预期输出:分类为 clean,置信度高;输入乱码文本时分类为 noise 或 word salad。无运行时错误。
测试条件:10 条混合质量文本(含 clean/gibberish/noise),batch_size=16,NPU 预热 1 轮。
| 指标 | 数值 |
|---|---|
| CPU 吞吐量 | 64.9 texts/s |
| NPU 吞吐量 | 584.3 texts/s |
| CPU/NPU 加速比 | 9.0 × |
DistilBERT 轻量架构(6 层)在 NPU 上吞吐高达 584 texts/s,适合高吞吐文本过滤管线。
分别在 CPU 和 NPU 上对 10 条混合质量文本推理,比较 4 维 softmax 概率向量的余弦相似度、MAE 和 Top-1 一致性。
| 指标 | 数值 |
|---|---|
| 平均余弦相似度 | 1.000000 |
| MAE | 0.000113 |
| 最大误差 | 0.000496 |
| 精度误差率 | 0.0000% |
| Top-1 准确率 | 100.0% |
结论:精度误差率 0.0000%,Top-1 标签完全一致,评测通过。
AutoModelForSequenceClassification.from_pretrained() 加载,model.to("npu:0") 迁移from_pretrained 自动兼容import torch, torch_npu
from transformers import AutoModelForSequenceClassification, AutoTokenizer
model = AutoModelForSequenceClassification.from_pretrained(
"Gibberish-Detector"
).to("npu:0")
tokenizer = AutoTokenizer.from_pretrained("Gibberish-Detector")
text = "asdfghjkl qwerty uiop"
inputs = tokenizer(text, return_tensors="pt", truncation=True)
inputs = {k: v.to("npu:0") for k, v in inputs.items()}
with torch.no_grad():
probs = torch.softmax(model(**inputs).logits, dim=-1)
quality = model.config.id2label[int(torch.argmax(probs))]
# quality → 'word salad'