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

AutoGluon Mitra Classifier on Ascend NPU

标签:#NPU #Ascend #Ascend910 #Tabular #Mitra #Classifier #FoundationModel

1. 模型简介

Mitra(Mixed synthetic priors for enhancing tabular foundation models, arXiv:2510.21204)是亚马逊科学团队提出的表格基础模型,在纯合成数据上预训练,通过 in-context learning(上下文学习) 范式完成表格分类/回归。其核心是 12 层 Transformer(约 72M 参数),输入为特征值嵌入(Tab2DEmbeddingX)+ 标签嵌入(Tab2DEmbeddingYClasses),行方向与列方向交替做注意力(attention across observations + attention across features)。

本仓库适配的是 autogluon/mitra-classifier 分类检查点,把 AutoGluon 内置的 MitraClassifier(sklearn 接口,Tab2D 架构)真实运行在单卡昇腾 NPU(npu:0)上:

  • 模型仓库:https://huggingface.co/autogluon/mitra-classifier
  • 官方示例数据:Wine(sklearn load_wine,13 特征、3 类)
  • 参数量:72,429,578(12 层 Transformer,dim=512, heads=4, dim_output=10)
  • 任务:表格多分类,输出类别概率(probability)与预测标签(label)

2. 验证环境

项目值
主机Atlas 800(昇腾 910B)
NPUAscend910_9362(单卡 npu:0)
torch / torch_npu2.9.0 / 2.9.0.post1
Python3.11
AutoGluon1.6.1
权重autogluon/mitra-classifier(model.safetensors 302MB, fp32)
revisionc425e9fa0910a6be1c494321792e7ba2a1367b1a

3. 数据契约

项目值
输入二维表格 [n_rows, n_features=13],float32
特征列alcohol, malic_acid, ash, alcalinity_of_ash, magnesium, total_phenols, flavanoids, nonflavanoid_phenols, proanthocyanins, color_intensity, hue, od280_od315_of_diluted_wines, proline
目标target ∈ {0, 1, 2}(类别编码按原数据保序)
划分官方示例 train_test_split(test_size=0.2, random_state=42, stratify=y)
训练集 / 测试集142 / 36 行
预处理Mitra 内置 Preprocessor(支持集/查询集标准化 + 量化嵌入);推理时特征缺失标记与列顺序保持不变
输出[n_query, 10] logits(取前 n_classes=3 做 softmax)→ [n_query, 3] 概率;预测标签为概率 argmax

4. 环境与安装

pip install -r requirements.txt

依赖见 requirements.txt:torch、torch_npu、autogluon.tabular==1.6.1、einops、einx、loguru、omegaconf、transformers、huggingface_hub[torch]、safetensors、pandas、scikit-learn。

5. NPU 推理

python inference.py

脚本流程:检测 npu:0 → 通过 huggingface_hub.snapshot_download 获取权重(自动走 HF_ENDPOINT 镜像)→ 本地加载 MitraClassifier(fine_tune=False, device='npu', hf_model=<checkpoint>) → fit()(仅预处理 + 支持集前向,不做权重更新)→ predict_proba() 在 npu:0 上完成上下文学习推理 → 输出同步耗时、准确率与样例预测。

说明:fit() 仅拟合数据预处理并执行一次支持集前向评估(fine_tune=False 时 max_epochs=0),模型核心 forward 全部在 npu:0 上执行;第一个参数 device 为 npu:0,无 CPU fallback。

6. 真实结果

使用 Wine 官方示例(36 行测试集):

指标值
测试集准确率 (accuracy)0.9722(35/36;多次运行 1.0 ~ 0.9722,单行在 bf16 autocast 噪声内翻转)
单次推理耗时(含预处理,同步后,首轮含编译)~162 ms
稳定推理耗时(预热后)~42.6 ms
吞吐~845 rows/s(稳定阶段)
NPU 峰值显存~527 MB

样例预测(true / pred / prob):

row 0: true=0 pred=0 prob=0.9972
row 1: true=2 pred=2 prob=0.8347
row 2: true=0 pred=0 prob=0.9802
row 3: true=1 pred=1 prob=0.6154
row 4: true=1 pred=1 prob=0.9229
row 5: true=0 pred=0 prob=0.9944
row 6: true=0 pred=0 prob=0.9977
row 7: true=1 pred=1 prob=0.7053
row 8: true=1 pred=1 prob=0.9890
row 9: true=2 pred=2 prob=0.9927
...

7. CPU-NPU 一致性

使用同一权重、同一输入切片、同一预处理、同一随机种子,在 CPU 与 npu:0 分别运行模型 forward(均强制 fp32):

指标值
最大绝对误差 (logits)0.00483
平均绝对误差 (logits)0.00125
argmax 一致率1.0000(36/36)
softmax 概率最大绝对误差0.00056
判定阈值atol=5e-3, rtol=1e-3(PASS)

说明:端到端 MitraClassifier.predict_proba() 在 NPU 上由框架内建 torch.autocast(device_type='npu', dtype=bfloat16) 执行,CPU 侧为 fp32,故直接端到端概率最大绝对误差约 0.074(argmax 仍 100% 一致);在受控 fp32 forward 对比下误差降至 ~0.005,满足表格分类任务的一致性要求。

8. 性能

指标值
预热 / 正式轮次3 / 10
avg / min / max42.60 / 41.84 / 43.66 ms
p50 / p90 / p9542.77 / 42.91 / 43.29 ms
batch(单样本分块)1(batch_size=1, 36 行 query)
shape / dtype[1, 142+36, 13] fp32 输入(量化嵌入后 bf16 autocast)
吞吐845.2 rows/s

每轮前后均调用 torch.npu.synchronize()。首轮编译耗时已并入预热轮次,上表为稳定推理阶段。

9. 证据图

三张图片均由 xterm.js 依据本次任务真实日志渲染生成(非原始桌面截图):

图片内容
agent_workflow侦察、数据检查、模型下载、NPU 推理、一致性、benchmark 与提交校验全流程日志
npu_device_callnpu-smi info、NPU 可用性、设备名、模型参数 device/dtype、输入输出 Tensor device
model_result默认 python inference.py 的真实完整输出:schema/窗口摘要、真实预测、同步耗时与状态

10. 限制

  • 本检查点为分类模型;autogluon/mitra-regressor 为回归版本,未在本仓库适配。
  • Mitra 为上下文学习模型,预测依赖支持集(训练行);仓库仅演示官方 Wine 示例数据集,不包含任意用户数据的预处理管线。
  • NPU 推理使用框架内建 bf16 autocast,与 CPU fp32 在概率层面存在 ~0.07 级差异(标签 100% 一致)。
  • 未做分布式/多卡推理,仅单卡 npu:0 验证。

参考

@article{zhang2025mitra,
  title={Mitra: Mixed synthetic priors for enhancing tabular foundation models},
  author={Zhang, Xiyuan and Maddix, Danielle C and Yin, Junming and Erickson, Nick and Ansari, Abdul Fatir and Han, Boran and Zhang, Shuai and Akoglu, Leman and Faloutsos, Christos and Mahoney, Michael W and others},
  journal={arXiv preprint arXiv:2510.21204},
  year={2025}
}