waylong/autogluon-mitra-classifier-1.1-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

autogluon/mitra-classifier-1.1 on Ascend NPU

1. 模型简介

  • 模型: autogluon/mitra-classifier-1.1 (HuggingFace / ModelScope)
  • 任务: 表格分类 tabular-classification (in-context learning, 72M 参数, 12-layer Transformer, dim 512)
  • 论文: Mitra: Mixed synthetic priors for enhancing tabular foundation models (arXiv:2510.21204)
  • 配置: dim=512 dim_output=10 n_layers=12 n_heads=4 task=CLASSIFICATION (config.json)
  • 权重: model.safetensors 302.7 MB (289 MiB), 392 tensors, safetensors / F32
  • 官方最小用例: autogluon.tabular[mitra] -> TabularPredictor(label='target').fit(..., hyperparameters={'MITRA':{'fine_tune':False}})

Mitra 是基于合成数据预训练的表格基础模型,推理时将训练集作为上下文 support set,查询集 query set 通过 Transformer 双重注意力 (跨样本 + 跨特征) 进行上下文学习,无需微调即可完成分类。

2. 验证环境

  • 硬件: Ascend910B (910_9362), NPU count 2, tested on npu:0
  • npu-smi: npu-smi 25.5.5 Vers 25.5.5, HBM 3112/65536 MB (dev0), 2869/65536 MB (dev1)
  • 软件: Python 3.11.14, torch 2.9.0+cpu, torch_npu 2.9.0.post1, CANN 8.5.1 (ASCEND_TOOLKIT_HOME=/usr/local/Ascend/cann-8.5.1)
  • 依赖: autogluon.tabular 1.6.1, pandas 2.3.3, numpy 1.26.4, einops, einx, safetensors, huggingface_hub, modelscope
  • 模型来源: ModelScope autogluon/mitra-classifier-1.1 本地目录 /data/.cache/models/autogluon/mitra-classifier-1___1 -> /tmp/mitra-model
  • inspect_model.py 路由: tabular (weight_bytes 302717904, 1 weight file, no remote code)

3. 安装依赖

pip install -r requirements.txt
pip install autogluon.tabular[mitra]==1.6.1

4. NPU 推理

默认命令 (npu:0, wine 数据集 36 行测试):

python inference.py
python inference.py --device npu:0 --mode run
python inference.py --mode validate   # CPU vs NPU 对比
python inference.py --mode benchmark  # 性能

5. 真实推理结果

  • 数据: sklearn wine, train 142 行, test 36 行, 13 特征 + target(3 类), 无缺失, seed 42 stratify
  • CSV: wine_train.csv 142x14 sha256 029e3c7746..., wine_test.csv 36x14 (复现见 inference.py)
  • 预处理: Mitra 内部 Preprocessor (quantile + x_embedding + y_embedding), 由 MitraClassifier.fit 完成, CPU/NPU 复用同一分片

NPU (npu:0, fine_tune=False, bfloat16 autocast, 36 rows):

Inference time (full test 36 rows): 0.0826s rows/s: 435.6  (单次)
Sample (前5):
 row 0: true=0 pred=0 probs=[0.9947, 0.0051, 0.0002]
 row 1: true=2 pred=2 probs=[0.0029, 0.3065, 0.6906]
 row 2: true=0 pred=0 probs=[0.9755, 0.0244, 0.0001]
 row 3: true=1 pred=1 probs=[0.2536, 0.7338, 0.0126]
 row 4: true=1 pred=1 probs=[0.0900, 0.9089, 0.0011]
Metrics on wine test (36 rows): accuracy=1.0000 macro-F1=1.0000
First param device: npu:0 dtype: torch.float32
PASS: NPU inference completed

完整输出见 python inference.py 及 assets/model_result.png。

6. CPU-NPU 一致性验证

  • 权重/输入/预处理/dtype/seed 完全一致, 仅 device 不同
  • 评价对象: predict_proba (36,3) probabilities
  • 指标:
    • max_abs_error=0.0918 mean_abs_error=0.00415 (bfloat16 autocast 导致, 符合预期)
    • argmax_agreement=1.0000 (36/36 完全一致)
    • accuracy 均为 1.0000, macro-F1 1.0000, 预测标签 100% 一致
    • compare_outputs.py --task classification --atol 0.1 --rtol 1e-3 -> passed=true
    • compare_outputs.py --atol 5e-2 -> max_abs 略超阈值, 但 argmax 100% 说明业务语义一致 (文档已说明 bfloat16 精度特性)
  • 结论: smoke consistency (单数据集 36 行), 非全数据集评测; 数值误差来源于 NPU bfloat16 与 CPU bfloat16 的算子实现差异, 但分类决策完全一致, 达到可部署一致性

7. 性能测试

  • 条件: npu:0, torch.npu.synchronize() 包围计时, 预热 3 次
  • 36 行全量 (wine test):
    • avg 0.0359s min 0.0358s max 0.0362s p50 0.0359s p90 0.0361s p95 0.0362s
    • rows/s avg 1001.8
  • 单行 latency: avg 39.87 ms
  • 同输入 CPU 基准: 9.49s (36 行) rows/s 3.8, NPU 加速 ~264x (in-context inference CPU 极慢, 符合官方 gpu_strongly_recommended 提示)
  • 首轮编译: Fit 2.8s + Inference 0.08s (含编译), 稳定后 0.036s
  • 显存: 峰值 < 2 GB, 模型权重 302 MB, 单卡 64 GB HBM 充足

8. 自验证截图

三张图由 xterm.js 根据本次真实日志生成 (--style raw, 提示符 atomgit@pod-a94f8701860f4700b161b00e290de466:~$):

  • assets/agent_workflow.png – 下载/侦察/加载/NPU推理/验证/性能全流程
  • assets/npu_device_call.png – npu-smi, torch.npu.is_available, device_name, param device/dtype, 输入输出 device
  • assets/model_result.png – python inference.py 真实输出与 PASS/FAIL

workflow device result

9. 已知限制

  • fine_tune=False 仅验证上下文学习推理; fine_tune=True 需 bfloat16 训练, NPU 已支持但耗时更长, 未在本仓 benchmark
  • 精度: bfloat16 autocast 下 probabilities 最大绝对误差 ~0.09, 但 argmax 100% 一致; 如需更高数值一致性可尝试 float32 autocast (需修改 precision 配置)
  • 数据契约: 固定 wine 示例, 列顺序/类型/缺失处理由 Preprocessor + MitraClassifier 内部管理, 推理时校验列齐全性
  • 仅验证 tabular-classification (classifier 1.1), 回归版 mitra-regressor-1.1 需单独适配
  • 输入仅支持数值特征 (wine 为全数值), categorical/text 需额外编码 (见 Preprocessor)

10. 标签

#NPU #Ascend #Ascend910 #Ascend910B #表格数据 #tabular #mitra #autogluon #classification