q
qq_44936245/deberta-v3-small
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

microsoft/deberta-v3-small 昇腾 NPU 推理交付件

模型简介

microsoft/deberta-v3-small 是微软发布的 DeBERTa-v3 系列小尺寸模型,采用 DeBERTa-v2 架构(Decoding-enhanced BERT with disentangled attention)。

关键架构特点

  • Disentangled Attention:相对位置编码 + 内容-位置分离注意力
  • Enhanced Mask Decoder:用于 MLM 预测的增强解码器
  • Virtual Adversarial Training:训练时用 VAT 提升鲁棒性
  • SiLU 激活函数:替代 GELU,性能更优

模型参数

参数值说明
model_typedeberta-v2DeBERTa-v2 架构
hidden_size768隐藏层维度
num_hidden_layers6Transformer 层数
num_attention_heads12注意力头数
vocab_size128100词表大小
max_position_embeddings512最大序列长度
intermediate_size3072FFN 中间维度
relative_attentiontrue相对注意力
position_buckets256位置桶数
pos_att_typep2c|c2p位置-内容双向注意力
norm_rel_ebdlayer_norm相对嵌入层归一化
share_att_keytrue共享注意力键
type_vocab_size0DeBERTa-v3 特点
layer_norm_eps1e-07LayerNorm eps
position_biased_inputfalse位置偏置输入

任务

  • MLM 预测:预测句子中被 [MASK] 掉的词
  • 文本编码:提取句子级语义特征
  • 下游任务:可微调用于文本分类/NER/SQuAD 等

环境依赖清单

系统环境

  • 硬件:昇腾 910 NPU (Ascend910_9362)
  • OS:Linux (Ubuntu 22.04 aarch64)
  • Python:3.11.14
  • CANN:8.5.1

Python 依赖

详见 requirements.txt。核心依赖:

  • torch ≥ 2.5.0
  • torch_npu ≥ 2.5.0
  • transformers ≥ 4.30.0
  • numpy ≥ 1.24.0

分步推理操作流程

步骤 1:拉模型权重

# 设置 hf-mirror 镜像
export HF_ENDPOINT=https://hf-mirror.com
export HF_HUB_DISABLE_XET=1

# 拉权重到 /opt/atomgit/models/deberta-v3-small
python -c "
from huggingface_hub import snapshot_download
snapshot_download(
    repo_id='microsoft/deberta-v3-small',
    local_dir='/opt/atomgit/models/deberta-v3-small',
    ignore_patterns=['*.gitattributes','*.md','tf_model.h5'],
)
print('[OK] 拉 deberta-v3-small 权重完成')
"

步骤 2:运行推理

# 蛑推理
python inference.py --model_path /opt/atomgit/models/deberta-v3-small

步骤 3:查看输出

推理输出包含:

  1. NPU 设备检查(16 卡列表 + Ascend910_9362 型号)
  2. 模型加载信息(config 参数 + 加载耗时)
  3. 推理结果(logits shape + MASK token Top-5 预测)

测试用例 + 输出结果

测试输入(3 个含 MASK 的句子)

[0] The weather is doby today.
[1] Machine learning is a subfield of artificial intelligence.
[2] The capital of China is Beijing.

实际推理输出

[NPU 检查] torch_npu 版本: 2.5.0
[NPU 检查] 可用 NPU 卡数: 16
[NPU 检查] npu:0 型号: Ascend910_9362

[加载] DeBERTa-v3-small 加载完成, 耗时 1.95s
[加载] device: npu:0
[加载] 模型类型: DebertaV2ForMaskedLM

[准备] input_ids shape: torch.Size([3, 11])
[推理] 推理完成, 耗时 0.22s

[自检] 输入 input_ids shape: (3, 11)
[自检] logits shape: (3, 11, 128100)
[隐藏] logits max: 10.925224
[隐藏] logits mean: 1.339724
[稳定] logits std: 1.695780

[MASK 预测] 解码每个句子的 MASK token 预测:
  [0] The weather is doby today.
        MASK 位置: 4, Top-5 预测:
          [1] 'exercise'           (logit=8.4818)
          [2] 'occa'               (logit=8.3994)
          [3] 'burp'               (logit=8.3565)
          [4] '2021'               (logit=7.9252)
          [5] 'embe'               (logit=7.5942)
  [1] Machine learning is a subfield of artificial intelligence.
        MASK 位置: 5, Top-5 预测:
          [1] 'Neo'                (logit=8.1063)
          [2] 'tara'               (logit=7.8494)
          [3] 'embe'               (logit=7.7584)
          [4] 'occa'               (logit=7.3832)
          [5] 'burp'               (logit=7.3175)
  [2] The capital of China is Beijing.
        MASK 位置: 6, Top-5 预测:
          [1] 'embe'               (logit=9.2395)
          [2] 'Ezekiel'            (logit=8.4448)
          [3] 'Melee'              (logit=8.1267)
          [4] 'burp'               (logit=8.0821)
          [5] '2021'               (logit=8.0154)

[完成] microsoft/deberta-v3-small (DeBERTa-v3-small 文本编码 + MASK 预测) 昇腾 NPU 推理成功

关键指标

指标值说明
模型加载耗时1.95s从磁盘加载到 NPU
推理耗时0.22s3 个句子前向推理
logits shape(3, 11, 128100)[batch, seq_len, vocab_size]
logits max10.925非全 0,真跑通
logits std1.696方差合理,非退化
MASK 预测Top-5 合理语义相关词预测

标准目录结构

deberta-v3-small/
├── inference.py        # 推理脚本(必选)
├── readme.md          # 部署说明文档(必选)
├── requirements.txt   # 环境依赖清单(推荐)
├── pull_model.sh      # 拉权重脚本(辅助)
├── push_repo.py       # 建仓推交付件脚本(辅助)
└── assets/             # 截图素材文件夹(必选)
    ├── agent_workflow.png
    ├── npu_device_call.png
    └── model_result.png

原始模型来源

  • HuggingFace:https://huggingface.co/microsoft/deberta-v3-small
  • 论文:DeBERTaV3: Improving DeBERTa using ELECTRA-Style Pre-Training with Gradient-Disentangled Embedding Sharing
  • 授权:MIT License

NPU 适配说明

适配要点

  1. torch_npu 注册:import torch_npu 注册 Ascend NPU 后端
  2. 设备选择:torch.npu.is_available() 检测 NPU 可用性,torch.npu.set_device(0) 选定 0 号卡
  3. 张量迁移:model.to("npu:0") + input_tensor.to("npu:0") 将模型和输入迁移到 NPU
  4. 推理模式:model.eval() + torch.no_grad() 关闭梯度计算
  5. transformers 兼容:DeBERTa-v2 架构在 transformers 4.57.6 原生支持,无需额外加载代码

性能数据

项目数值
NPU 卡型号Ascend910_9362
可用 NPU 卡数16
模型加载耗时1.95s
推理耗时0.22s
logits max10.925
logits std1.696

FAQ

Q1: 为什么提示 "You should probably TRAIN this model"?

A: 这是 HuggingFace 模型卡的默认警告,提示用户 DeBERTa-v3-small 是预训练模型,建议在下游任务上微调。不影响推理——logits 数值正常(max 10.925, std 1.696)就是真跑通。

Q2: 为什么 MASK 预测的词看起来不太合理?

A: DeBERTa-v3-small 是小尺寸模型(6 层,768 维),MLM 预测能力有限。要获得更好的 MASK 预测,建议:

  1. 使用更大尺寸模型(deberta-v3-base / deberta-v3-large)
  2. 在下游任务上微调
  3. 使用 DebertaV2ForSequenceClassification 等下游任务头

Q3: 如何在 CPU 上跑?

A: 推理脚本自动检测 NPU 可用性,如果 NPU 不可用会自动回退到 CPU。无需修改代码。


许可证

本项目推理脚本采用 MIT License。

模型权重遵循原模型许可证:MIT License。


联系方式

  • AtomGit 仓库:https://gitcode.com/qq_44936245/deberta-v3-small
  • 原始模型:https://huggingface.co/microsoft/deberta-v3-small