qq_34566203/madhurjindal--autonlp-Gibberish-Detector-492513457-ascend
模型介绍文件和版本Pull Requests讨论分析
下载使用量0

Gibberish Detector on Ascend NPU

1. 简介

本文档记录 madhurjindal/autonlp-Gibberish-Detector-492513457 在 Ascend 910B3 NPU 环境下的适配与验证结果。

该模型基于 DistilBERT(distilbert-base-uncased)在 AutoNLP 平台上微调,用于文本乱码/垃圾内容检测。模型可将输入文本分类为 4 个类别:

  • clean:正常文本
  • mild gibberish:轻微乱码
  • noise:噪声文本
  • word salad:无意义词语组合

模型在测试集上达到 97.36% 的准确率和 F1 分数,适用于聊天机器人输入验证、内容审核、文本质量控制等场景。

本仓库提供:

  • inference.py:NPU 推理脚本,支持单条/批量文本分类
  • eval.py:精度与性能评测脚本
  • log.txt:评测运行日志

相关获取地址:

  • 权重下载地址(HuggingFace):https://huggingface.co/madhurjindal/autonlp-Gibberish-Detector-492513457
  • 镜像加速:https://hf-mirror.com

2. 验证环境

组件版本
torch2.8.0
torch_npu2.8.0.post4
transformers4.57.6
  • NPU:Ascend 910B3,1 逻辑卡
  • 模型架构:DistilBertForSequenceClassification
  • 隐藏层维度:768
  • 注意力头数:12
  • 隐藏层数:6
  • 参数量:约 67M
  • 词汇表大小:30,522
  • 分类数:4 类

3. 推理启动

启动前可先检查 NPU 可用性:

python3 -c "import torch; print(f'NPU available: {torch.npu.is_available()}')"

环境准备:

pip install torch torch_npu transformers
export ASCEND_RT_VISIBLE_DEVICES=0

已验证通过的推理命令:

单条文本推理:

python inference.py --text "I love machine learning and artificial intelligence!"

批量推理:

python inference.py --input-file texts.txt

CPU 推理(参考基准):

python inference.py --device cpu --text "test text"

输出格式:

{
  "model": "madhurjindal--autonlp-Gibberish-Detector-492513457",
  "device": "npu:0",
  "num_texts": 1,
  "inference_time_seconds": 0.0012,
  "results": [
    {
      "text": "I love machine learning!",
      "prediction": "clean",
      "confidence": 0.9985,
      "probabilities": {
        "clean": 0.9985,
        "mild gibberish": 0.0012,
        "noise": 0.0002,
        "word salad": 0.0001
      }
    }
  ]
}

4. Smoke 验证

python inference.py --text "I love machine learning!"
python inference.py --text "asdfghjkl qwertyuiop zxcvbnm"

预期输出:

  • 正常文本应被分类为 clean,置信度 > 0.95
  • 乱码文本应被分类为 noise 或 word salad
  • 推理时间应在毫秒级别
  • 返回 JSON 格式包含 4 个类别的概率分布

验证结果:

  • 正常文本正确分类为 clean
  • 乱码文本正确分类为 noise
  • 推理可在 NPU 上正常完成
  • 输出格式符合预期

5. 性能参考

测试条件:8 条文本,batch_size=32,max_length=64。

指标CPUNPU
avg_time0.3222 s0.0117 s
throughput24.83 texts/s685.12 texts/s
speedup-27.54x

6. 精度评测

精度评测采用预测一致率和概率误差双指标。

指标数值
测试样本数8
最大概率绝对误差1.0577e-03
平均概率绝对误差1.6567e-04
预测一致率8/8 (100.00%)
错误率0.00%
精度要求(错误率 < 1%)通过

结论:NPU 与 CPU 的分类预测结果完全一致(100%),概率误差极小,精度通过验证。

7. 注意事项

  1. 序列长度:模型训练时的 max_length=64,超出部分会被截断,建议输入不超过 64 token。
  2. 类别定义:clean 为正常文本;mild gibberish 为轻微乱码(如部分错别字);noise 为纯噪声(如随机按键);word salad 为有语法但无意义的词语组合。
  3. 应用场景:适用于聊天机器人输入过滤、表单输入验证、评论审核等需要检测无意义文本的场景。
  4. DistilBERT:相比 BERT-base 参数量减少 40%,推理速度提升约 60%,适合对延迟敏感的生产环境。