本文档记录 iic/nlp_structbert_sentence-similarity_chinese-retail-base 在昇腾 NPU 上的适配验证结果。
| 组件 | 版本 |
|---|---|
torch | 2.9.0 |
torch-npu | 2.9.0.post1 |
transformers | 4.57.6 |
CANN | 8.5.1 |
python inference.py --model-id iic/nlp_structbert_sentence-similarity_chinese-retail-base --device npu:0或使用 evaluate.py 进行完整验证:
python evaluate.py --model-id iic/nlp_structbert_sentence-similarity_chinese-retail-base --device npu:0 --output report.json运行 inference.py 的实际输出:
$ python3 inference.py --model-id iic/nlp_structbert_sentence-similarity_chinese-retail-base --device npu:0
[1/5] 加载模型: iic/nlp_structbert_sentence-similarity_chinese-retail-base
参数量: 102,267,648
[2/5] 迁移到 npu:0
[3/5] Tokenize 输入 (max_length=128)
[4/5] 运行推理验证
输出形状: [1, 128, 768]
是否有 NaN: False
[5/5] 性能基准测试 (10 轮)
平均延迟: 8.58 ms
峰值显存: 0.21 GB
[额外] CPU vs NPU 精度对比
Cosine Similarity: 0.999996
Max Abs Error: 0.002330
精度误差: 0.0004%
✅ 精度满足要求(< 1%)
✓ 验证通过| 指标 | 结果 |
|---|---|
| 输出形状 | [1, 128, 768] |
| 是否有 NaN | 否 ✅ |
| 推理状态 | 正常 ✅ |
| 指标 | 数值 |
|---|---|
| 平均延迟 | 8.58 ms |
| 峰值显存 | 0.21 GB |
| 测试轮数 | 10 |
✅ NPU vs CPU 精度对比
| 指标 | 数值 |
|---|---|
| Cosine Similarity | 0.999996 |
| Max Abs Error | 0.002330 |
| 精度误差 | 0.0004% |
| 是否满足要求 | 是(< 1%)✅ |
| 材料 | 文件 | 说明 |
|---|---|---|
| 推理脚本 | inference.py | 独立可运行的 NPU 推理代码 |
| 精度评测代码 | evaluate.py | CPU vs NPU cosine similarity 对比 |
| 环境检查 | env_check.py | NPU 环境验证脚本 |
| 运行日志 | logs/*.log | 完整执行日志(可复现) |
| 自验证截图 | screenshots/ | 终端验证截图 |
| 精度报告 | report.json | 结构化评测数据 |
| 部署文档 | DEPLOY.md | 环境搭建与验证指南 |
| 依赖清单 | requirements.txt | Python 依赖(uv/pip 安装) |
本模型适配由以下 Agent Skill 完成(6.2 必填)
| 项目 | 内容 |
|---|---|
| Skill 名称 | nlp-encoder-npu-adapt |
| 触发条件 | BERT/GTE/StructBERT 句子编码器适配到昇腾 NPU |
| 覆盖模型 | 2 个 NLP Sentence Encoder 模型 |
| 核心能力 | 文本编码、Mean Pooling、FP16 推理、精度验证、性能基准 |
Agent 自动执行:
# 下载模型
python -c "from modelscope.hub.snapshot_download import snapshot_download; snapshot_download('iic/nlp_structbert_sentence-similarity_chinese-retail-base', cache_dir='./models')"
# 权重重映射(StructBERT 特有:strip encoder. prefix)
python -c "
import torch
sd = torch.load('./models/iic/nlp_structbert_sentence-similarity_chinese-retail-base/pytorch_model.bin', map_location='cpu')
new_sd = {k[len('encoder.'):] if k.startswith('encoder.') else k: v for k, v in sd.items() if not k.startswith('classifier.')}
torch.save(new_sd, './models/iic/nlp_structbert_sentence-similarity_chinese-retail-base/pytorch_model.bin')
"
# 运行验证
python wave1/1h_nlp_encoder/evaluate_nlp.py \
--model-id iic/nlp_structbert_sentence-similarity_chinese-retail-base \
--device npu:0 --dtype float16 --max-length 128 \
--cache-dir ./models/iic/nlp_structbert_sentence-similarity_chinese-retail-base \
--output report.json# Step 1: 环境检查
python3 env_check.py
# Step 2: 验证模型
python3 evaluate.py --model-id iic/nlp_structbert_sentence-similarity_chinese-retail-base --device npu:0 --output report.json
# Step 3: 运行推理
python3 inference.py --model-id iic/nlp_structbert_sentence-similarity_chinese-retail-base --device npu:0encoder. 前缀,需重映射后才能被 BertModel 正确加载贡献者: xujiashuai 参赛赛道: 模型适配赛道 提交时间: 2026-05-17