| 组件 | 版本 | 说明 |
|---|---|---|
| CANN | 8.5.1 | source /usr/local/Ascend/ascend-toolkit/set_env.sh |
| Python | 3.11 | |
| PyTorch | 2.9.0 | torch==2.9.0(pin) |
| torch_npu | 2.9.0.post1 | torch-npu==2.9.0.post1(pin) |
| transformers | 4.57.6 | transformers==4.57.6(pin,未用原生类) |
| einx/loguru/omegaconf 等 | 见 requirements.txt | 源码本地化依赖(--no-deps 预审后装) |
安装命令:
pip install -r requirements.txt以下命令均在模型仓根目录(含 inference.py 与 model_files/)执行;./model 为权重目录。
一键复现:bash reproduce.sh ./model(环境检查 → 模型加载 → NPU 推理 → CPU/NPU 精度对比)。
# 1. 克隆模型仓并进入根目录
git clone https://gitcode.com/gcw_Pp8UzTBn/mitra-classifier-NPU.git
cd mitra-classifier-NPU
# 2. 检查 NPU 设备在线
npu-smi info
# 3. 验证 torch_npu 可用(期望输出 True)
python3 -c "import torch, torch_npu; print(torch.npu.is_available())"
# 4. 下载权重(国内镜像)
export HF_ENDPOINT=https://hf-mirror.com
huggingface-cli download autogluon/mitra-classifier --local-dir ./model
# 5. NPU 推理(固定 seed 合成 few-shot 表格输入)
python3 inference.py --model ./model --device npu --output /tmp/npu_result.json
# 6. CPU 基线推理(同一输入,输出至 /tmp 不入仓库)
python3 inference.py --model ./model --device cpu --output /tmp/cpu_result.json
# 7. 精度对比(期望 ✅ 通过,误差 < 1%)
python3 compare.py --baseline /tmp/cpu_result.json --npu /tmp/npu_result.json --model "autogluon/mitra-classifier"| 指标 | CPU 基线 | NPU | 说明 |
|---|---|---|---|
| 分类 logits (1,2,10) | finite=True | finite=True | 固定 few-shot 输入(support 4×5 + query 2×5) |
| cosine_similarity | — | 1.000000 | CPU/NPU 逐元素一致 |
| relative_error | — | 0.000000 | 误差 < 1% 达标 |
| 推理延迟 | 536.3ms | 516.7ms | 含首次算子编译 |
| 权重校验 | — | — | model.safetensors (e06a055e…) — 与下载源一致(CP-A8) |
输入: 固定 seed=42 合成表格 few-shot:x_support (1,4,5) / y_support (1,4) / x_query (1,2,5)
输出: 分类 logits (1,2,10),实测 finite=True,CPU/NPU 前 4 元素 logits_head=[1.0174, 1.0732, 1.0240, 1.3471] 逐位一致。