本文档记录 openai-community/roberta-base-openai-detector AI 生成文本检测器在昇腾 NPU(Ascend 910B3)上的迁移适配、精度评测与性能验证结果。
该模型基于 RoBERTa-base(12 层,768 维),通过 GPT-2 生成文本与人类文本的二分类训练,输出 Human(人类撰写)和 AI-generated(AI 生成)两个类别。适用于检测 ChatGPT、GPT-3、GPT-2 等大语言模型生成的文本,在教育防作弊、新闻真实性验证、内容溯源等场景有广泛应用。
相关获取地址:
| 组件 | 版本 |
|---|---|
torch | 2.8.0 |
torch_npu | 2.8.0.post4 |
transformers | 5.8.1 |
CANN | 8.5.1 |
8 × Ascend 910B3conda create -n openai-community_roberta-base-openai-detector python=3.11 -y
conda activate openai-community_roberta-base-openai-detector
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 sample text that may be AI-generated." --device npu编程接口:
from inference import AIDetector
detector = AIDetector(
model_path="./openai-community_roberta-base-openai-detector", device="npu"
)
results, probs = detector.predict(["This text was written by a human."])
# results[0] → {'Human': 0.92, 'AI-generated': 0.08}python inference.py --text "This is a sample text." --device npu预期输出:Human/AI-generated 两类概率(和为 1.0),无运行时错误。
测试条件:10 条混合来源文本(人类/AI 各半),batch_size=16,NPU 预热 1 轮。
| 指标 | 数值 |
|---|---|
| CPU 吞吐量 | 37.9 texts/s |
| NPU 吞吐量 | 416.8 texts/s |
| CPU/NPU 加速比 | 11.0 × |
分别在 CPU 和 NPU 上推理,比较二分类 softmax 概率向量的余弦相似度、MAE 和 Top-1 一致性。
| 指标 | 数值 |
|---|---|
| 平均余弦相似度 | 0.999997 |
| MAE | 0.001061 |
| 精度误差率 | 0.0003% |
| Top-1 准确率 | 100.0% |
结论:精度误差率 0.0003%,Top-1 完全一致,评测通过。
vocab.json + merges.txtAutoModelForSequenceClassification.from_pretrained() 加载model.to("npu:0") 迁移,RoBERTa 12 层预热约 3-5 秒.cpu().numpy() 返回import torch, torch_npu
from transformers import AutoModelForSequenceClassification, AutoTokenizer
model = AutoModelForSequenceClassification.from_pretrained(
"roberta-base-openai-detector"
).to("npu:0")
tokenizer = AutoTokenizer.from_pretrained("roberta-base-openai-detector")
text = "This text could be AI-generated."
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)
result = model.config.id2label[int(torch.argmax(probs))]skip_special_tokens=True 避免输出特殊字符。