g
gcw_Pp8UzTBn/mitra-classifier-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

autogluon/mitra-classifier 昇腾 NPU 适配部署文档

一、模型简介

  • 模型名称:autogluon/mitra-classifier
  • 模型来源:https://huggingface.co/autogluon/mitra-classifier
  • 官方镜像:https://ai.gitcode.com/hf_mirrors/autogluon/mitra-classifier
  • 模型类型:表格 In-Context Learning 分类基础模型(Tab2D 架构:12 层 d_model=512、4 头,quantile 嵌入 + 类嵌入,few-shot support/query 双流)
  • 参数量:75.7M
  • 模型特性:Mitra 是 AutoGluon 的表格少样本分类模型,通过 support 样本上下文学习直接对新 query 表格行分类,输出类别 logits
  • 适配状态:✅ 已在昇腾 NPU(Ascend910,CANN 8.5.1)跑通推理,CPU/NPU 精度对比 cosine_similarity = 1.0(误差 < 1%)

二、环境依赖

组件版本说明
CANN8.5.1source /usr/local/Ascend/ascend-toolkit/set_env.sh
Python3.11
PyTorch2.9.0torch==2.9.0(pin)
torch_npu2.9.0.post1torch-npu==2.9.0.post1(pin)
transformers4.57.6transformers==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=Truefinite=True固定 few-shot 输入(support 4×5 + query 2×5)
cosine_similarity—1.000000CPU/NPU 逐元素一致
relative_error—0.000000误差 < 1% 达标
推理延迟536.3ms516.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] 逐位一致。