z
zkx_/cryptobert-ascend
模型介绍文件和版本Pull Requests讨论分析
下载使用量0

cryptobert on Ascend NPU

1. 简介

本文档记录 ElKulako/cryptobert 加密货币情绪分类模型在昇腾 NPU(Ascend 910B3)上的迁移适配、精度评测与性能验证结果。

该模型基于 RoBERTa-base(12 层 Transformer,768 维)在加密货币/金融推文数据上微调,支持 3 种市场情绪分类:Bearish(看跌)、Neutral(中性)、Bullish(看涨)。输入为英文金融文本,输出 3 维 softmax 概率分布。RoBERTa 使用 BPE tokenizer(GPT-2 风格),对金融术语和加密货币名称有良好的分词支持。

相关获取地址:

  • 权重下载地址(HuggingFace):https://huggingface.co/ElKulako/cryptobert

2. 验证环境

组件版本
torch2.8.0
torch_npu2.8.0.post4
transformers5.8.1
CANN8.5.1
  • NPU:8 × Ascend 910B3
  • 精度对比基准:CPU(x86, PyTorch 2.8.0)

3. 部署使用流程

3.1 环境准备

conda 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/simple

3.2 模型权重下载

HF_ENDPOINT=https://hf-mirror.com \
    huggingface-cli download ElKulako/cryptobert \
    --local-dir ./cryptobert

3.3 推理脚本使用

python 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}

4. Smoke 验证

python inference.py --text "Bitcoin is going to the moon!" --device npu

预期输出:3 种情绪标签按概率降序排列,无运行时错误。

5. 性能参考

测试条件:10 条加密货币相关推文,batch_size=16,NPU 预热 1 轮。

指标数值
CPU 吞吐量38.3 texts/s
NPU 吞吐量354.9 texts/s
CPU/NPU 加速比9.3 ×

6. 精度评测

6.1 评测方法

分别在 CPU 和 NPU 上对 10 条加密货币相关文本推理,比较 3 维 softmax 概率向量的余弦相似度、MAE 和 Top-1 分类一致性。

6.2 评测结果

指标数值
平均余弦相似度0.999996
MAE0.000820
最大误差0.004101
精度误差率0.0004%
Top-1 准确率100.0%

结论:精度误差率 0.0004%,远低于 1% 要求,评测通过。

7. 迁移适配说明

7.1 模型结构

  • Backbone:RobertaModel(12 层 Transformer,768 维,RoBERTa-base 标准架构)
  • Classifier Head:线性层(768 → 3),3 类 softmax 分类
  • Tokenizer:BPE(Byte-Pair Encoding),merges.txt + vocab.json(GPT-2 风格,空格用 Ġ 前缀表示)
  • 权重格式:同时提供 safetensors 和 pytorch_model.bin,优先加载 safetensors
  • 参数量:124.7M(RoBERTa-base 标准规模)

7.2 适配要点

  1. 使用 AutoModelForSequenceClassification.from_pretrained() 加载
  2. model.to("npu:0") 一步迁移到 NPU,RoBERTa 算子(LayerNorm, GELU, Linear)NPU 原生支持
  3. RoBERTa BPE tokenizer 在 CPU 端分词(merges.txt + vocab.json),tensor 转移至 NPU
  4. 输出 softmax 后 .cpu().numpy() 返回
  5. 同时含 safetensors + pytorch_model.bin,from_pretrained 自动选择 safetensors

7.3 关键代码

import 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))]

8. 注意事项

  1. BPE tokenizer 特点:RoBERTa 使用 GPT-2 风格的 BPE tokenizer,空格前缀用 "Ġ" 字符表示。decode 时需使用 skip_special_tokens=True 避免特殊字符。
  2. 双权重格式:模型同时提供 safetensors(推荐,加载快)和 pytorch_model.bin(兼容性好),from_pretrained 会优先加载 safetensors。
  3. 加密货币领域特化:模型在加密货币推文上训练,对 BTC/ETH/SOL 等代币名称和 moon/dump/hodl 等行业术语有良好的语义理解。普通金融文本也可使用但非最佳。
  4. NPU 预热:首次推理触发算子编译(约 3-5 秒),建议生产环境预热后批量推理。
  5. 输入截断:RoBERTa max_position_embeddings=514,超长推文将被截断。加密货币推文通常在 280 字符以内,不受影响。