本文档记录 ElKulako/cryptobert 加密货币情绪分类模型在昇腾 NPU(Ascend 910B3)上的迁移适配、精度评测与性能验证结果。
该模型基于 RoBERTa-base(12 层 Transformer,768 维)在加密货币/金融推文数据上微调,支持 3 种市场情绪分类:Bearish(看跌)、Neutral(中性)、Bullish(看涨)。输入为英文金融文本,输出 3 维 softmax 概率分布。RoBERTa 使用 BPE tokenizer(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 cryptobert python=3.11 -y
conda activate cryptobert
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/simpleHF_ENDPOINT=https://hf-mirror.com \
huggingface-cli download ElKulako/cryptobert \
--local-dir ./cryptobertpython inference.py --text "Bitcoin is going to the moon!" --device npu
python inference.py --batch_file tweets.txt --device npu编程接口:
from inference import CryptoBertClassifier
clf = CryptoBertClassifier(model_path="./cryptobert", device="npu")
results, probs = clf.predict(["Bitcoin price surges 20% today."])
# results[0] → {'Bullish': 0.92, 'Neutral': 0.06, 'Bearish': 0.02}python inference.py --text "Bitcoin is going to the moon!" --device npu预期输出:3 种情绪标签按概率降序排列,无运行时错误。
测试条件:10 条加密货币相关推文,batch_size=16,NPU 预热 1 轮。
| 指标 | 数值 |
|---|---|
| CPU 吞吐量 | 38.3 texts/s |
| NPU 吞吐量 | 354.9 texts/s |
| CPU/NPU 加速比 | 9.3 × |
分别在 CPU 和 NPU 上对 10 条加密货币相关文本推理,比较 3 维 softmax 概率向量的余弦相似度、MAE 和 Top-1 分类一致性。
| 指标 | 数值 |
|---|---|
| 平均余弦相似度 | 0.999996 |
| MAE | 0.000820 |
| 最大误差 | 0.004101 |
| 精度误差率 | 0.0004% |
| Top-1 准确率 | 100.0% |
结论:精度误差率 0.0004%,远低于 1% 要求,评测通过。
merges.txt + vocab.json(GPT-2 风格,空格用 Ġ 前缀表示)AutoModelForSequenceClassification.from_pretrained() 加载model.to("npu:0") 一步迁移到 NPU,RoBERTa 算子(LayerNorm, GELU, Linear)NPU 原生支持.cpu().numpy() 返回from_pretrained 自动选择 safetensorsimport torch, torch_npu
from transformers import AutoModelForSequenceClassification, AutoTokenizer
model = AutoModelForSequenceClassification.from_pretrained("cryptobert").to("npu:0")
tokenizer = AutoTokenizer.from_pretrained("cryptobert")
text = "Bitcoin is going to the moon!"
inputs = tokenizer(text, return_tensors="pt", truncation=True)
inputs = {k: v.to("npu:0") for k, v in inputs.items()}
with torch.no_grad():
logits = model(**inputs).logits
probs = torch.softmax(logits, dim=-1)
sentiment = model.config.id2label[int(torch.argmax(probs))]skip_special_tokens=True 避免特殊字符。from_pretrained 会优先加载 safetensors。