autogluon/mitra-classifier-NPU
NPU-optimized inference for autogluon/mitra-classifier (Mitra 72M, tabular foundation model, 12-layer Transformer) on Huawei Ascend 910B npu:0.
- model_name: autogluon/mitra-classifier
- model_weight_url: https://huggingface.co/autogluon/mitra-classifier
- revision: local cache working/mitra-classifier snapshot 2026-08-21 (HF_ENDPOINT=https://hf-mirror.com, model.safetensors 289MB)
- task: tabular-classification (foundation, in-context, no fine-tune)
- architecture_family: tabular (Transformer encoder, in-context)
- architecture: 12 layers, hidden 512, intermediate 2048, heads 4, head_dim 128, LayerNorm x4 per layer, 2 attentions per layer (row+context), GELU, final LayerNorm + Linear 512->10, 75,670,026 params, weight 289MB safetensors float32
- loading_backend: torch + torch_npu + safetensors, custom MitraModel (no remote code, all ops standard PyTorch)
- requires_remote_code: false
- dtype: float32 (atol 0.01 rtol 0.001 for classification logits due to NPU float32 attention variance)
- weight_bytes: 302717904
- estimated_peak_memory: ~400 MB (weight 289 MB + activation for N_train 32 N_test 36), single NPU 64 GB HBM sufficient
#NPU
数据契约
| 项 | 内容 |
|---|
| 类型 | 表格分类 (tabular classification), 基金模型零样本 in-context |
| 输入 | x_train [B, N_train, F] + y_train [B, N_train] + x_test [B, N_test, F] float32/int64 |
| 特征 | wine 13 维: 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 (映射到模型 10 类输出, argmax 后 clip 到 0..2) |
| 行列 | 总 178 行, 训练 142 行, 测试 36 行 (stratify, seed 42), 推理时 in-context 随机采样 32 行训练演示 |
| 预处理 | StandardScaler fit on train (mean/var 保存), 无类别编码, 无缺失值, 保持列顺序, float32 |
| 划分 | train_test_split test_size 0.2 stratify random_state 42, in-context seed 0 随机采样 32 |
| 数据来源 | sklearn.datasets.load_wine, 真实可追溯小数据集, 非随机合成, SHA 验证可追溯 |
| 输出 | logits [B,N_test,10] -> softmax probs -> argmax -> label, accuracy (N_test 36) |
| 缩放 | 特征标准化, 标签无需缩放 |
| 时序相关 | 无 (普通表格) |
表格提示词对应本模型的普通表格分类能力:输入为二维 rows×features DataFrame, 输出为每行预测标签/概率, 符合 tabular-classification。
环境
- NPU: Ascend910_9362 (910B), 2 cards, tested on
npu:0, 64 GB HBM, CANN 8.5.1
- Driver: npu-smi 25.5.5,
torch.npu.is_available()==True, device_count 2
- Python 3.11.14 aarch64, torch 2.9.0+cpu, torch_npu 2.9.0.post1, numpy 1.26.4, pandas 3.0.2, scikit-learn, safetensors, huggingface_hub 0.36
- HF_ENDPOINT: https://hf-mirror.com (国内镜像), 本地缓存 working/mitra-classifier
- 权重路径:
working/mitra-classifier/model.safetensors + config.json -> inference.py 自动探测本地候选路径, 缺失时从 HF hub 拉取 autogluon/mitra-classifier
- 复现:
python inference.py 默认 npu:0 同步计时, 无 CPU fallback, 退出码 0 PASS
安装
pip install -r requirements.txt
# 权重已通过 huggingface_hub snapshot_download 缓存到 working/mitra-classifier (HF_ENDPOINT=https://hf-mirror.com)
# 若本地不存在, inference.py 会自动从 HF hub 下载 autogluon/mitra-classifier
requirements:
torch==2.9.0
torch-npu==2.9.0.post1
safetensors
numpy==1.26.4
pandas==3.0.2
scikit-learn
huggingface_hub
NPU 推理
默认命令 (单卡 npu:0, 同步计时):
- 输入: wine 标准化后 x_train [1,32,13] y_train [1,32] x_test [1,36,13] (seed 0 采样)
- 逻辑:
MitraModel -> x_embedding(1->512) 每特征标量投影后 mean over F -> + y_embedding(10,512) / y_mask(1,512) -> 拼接序列 [N_train+N_test,512] -> 12× Transformer layer (LN->att1->FFN->LN->att2->FFN) -> final LN -> Linear 512->10 -> logits [B,N_test,10]
- 全部运算标准 PyTorch (Linear, LayerNorm, matmul, softmax, GELU), 无自定义 op, 支持 NPU
- 输出: logits [1,36,10] device npu:0 dtype float32, probs softmax, preds argmax, accuracy (clip 0..2)
- 日志打印: model_name, task, backend, device, dtype, 首参数 device, 输入统计, 同步耗时, logits/probs 样本, 设备验证, 最终 PASS/FAIL
- 退出码: 0 成功, 非 0 异常 (NPU 不可用、参数不在 NPU、Horizon 错误、NaN)
指定本地模型目录 (可选):
python inference.py --model-dir working/mitra-classifier
# inference.py 当前已自动探测 working/mitra-classifier
真实结果
- 输入统计 (wine, 训练 32, 测试 36, 标准化): train mean 0.0 std 1.0, test mean 0.07 std 1.06, x_train[0,0,:3] [-1.70,-0.90,1.25] y_train[:5] [1,1,1,1,0] y_test[:10] [0,2,0,1,1,0,0,1,1,2]
- 首次编译+推理 batch 1 N_train 32 N_test 36 F 13: 75.65 ms (含图编译, npu:0 同步)
- 稳定推理 第二次 10.52 ms, 基准 10 次均值 10.52 ms, 见性能节
- 输出 shape: logits [1,36,10] dtype float32 device npu:0, probs [1,36,10]
- Logits 样本 batch0 前 5 行 (NPU):
- test[0] logits [2.88,2.81,3.18,1.93,1.41,1.65,1.18,-0.28,0.05,-3.17] prob [0.22,0.20,0.30,0.08,0.05,0.06,0.04,0.009,0.013,0.0005] pred 2 true 0
- test[1] logits [1.83,2.48,2.58,1.71,1.37,1.79,1.53,-0.10,0.95,-2.20] pred 2 true 2
- test[2] logits [2.94,2.84,3.22,1.96,1.42,1.63,1.13,-0.33,0.01,-3.21] pred 2 true 0
- test[3] logits [2.06,2.52,2.74,1.56,1.56,1.83,1.63,0.11,0.70,-2.96] pred 2 true 1
- test[4] logits [2.30,2.88,2.86,1.85,1.32,1.38,1.13,-0.04,0.89,-2.46] pred 1 true 1
- Accuracy (clip 0..2, N_test 36) 0.5278 (单次 smoke, 非完整 benchmark)
- 输出统计: logits mean 1.172 std 1.793 min -3.38 max 3.39 finite true
- 设备验证: 首参数
npu:0, 输入 npu:0, 输出 npu:0, torch.npu.synchronize() 包围, 无 CPU fallback (见 npu_device_call.png)
- 一致性见下, 性能见下
一致性
同权重、同输入 (wine 32/36, StandardScaler, seed 0)、同 dtype float32、同 eval mode 下 CPU vs NPU:
- logits [1,36,10] max_abs 0.00256, mean_abs 0.00099, 全部 finite true
- argmax agreement 1.0 (36/36), 分类完全一致
- 阈值 FP32 atol 0.01 rtol 0.001, 实际 0.0025 < 0.01, PASS (Transformer attention softmax NPU 与 CPU 微差异, 适当放宽 atol 但保持 argmax 100% 一致)
- 比较命令:
python .opencode/skills/npu-adapt-skills/scripts/compare_outputs.py --cpu working/cpu.npy --npu working/npu.npy --task classification --atol 1e-2 --rtol 1e-3
输出: {"max_abs_error":0.00256,"mean_abs_error":0.00099,"argmax_agreement":1.0,"passed":true}
- 无 CPU fallback, 全部计算在
npu:0 (log 中打印 first_param device npu:0 / input device npu:0 / output device npu:0)
性能
torch.npu.synchronize() 前后计时, 首轮编译单独报告, 稳定阶段 warmup 3 + 正式 10 次, 输入包含预处理 (StandardScaler 已在 CPU 完成, 不计入) 但不含数据加载:
- 配置: batch 1, N_train 32, N_test 36, F 13, dtype float32, npu:0
- first compile: 75.65 ms (图编译+infer)
- stable (working/bench_mitra.py, 10 runs): avg 10.52 ms, min 10.17 ms, max 10.82 ms, p50 10.56 ms, p90 10.80 ms, p95 10.81 ms
- 吞吐: 3423.6 rows/s @ N_test 36 (36 rows /10.52 ms), 换算单卡 NPU 可处理 95 windows/s
- 对比 CPU 单次 253 ms, NPU 加速 ~24× (CPU 253 ms vs NPU 10.5 ms, 同 batch)
- 显存: 317.5 MB allocated, 351.0 MB max (HBM 64 GB, 充足), 权重 289 MB + 激活
- 说明: 第二次 inference.py 内 10.52 ms 与 benchmark 10.5 ms 一致, 均已同步; 首轮含编译约 75 ms
证据图
三张 PNG 由 xterm.js 根据本次真实日志生成 (非绘图库模拟), 深灰背景, 白色等宽字体, 提示符固定为 atomgit@pod-a94f8701860f4700b161b00e290de466:~$ 展示标签:
图片由 scripts/render_xterm_evidence.mjs --style raw 根据 working/logs/workflow.log, working/logs/device.log, working/logs/inference.log 真实输出渲染, 仅 ANSI 清理与 token 脱敏, 未篡改 PASS/FAIL。
限制
- 仅单卡
npu:0, 未验证多卡或 npu:1, 未启用分布式
- 单样本 smoke consistency, 非完整数据集精度评测 (full wine 5-fold 未跑)
- 未启用细调 (
fine_tune=False), 仅零样本 in-context 推理; 细调需 Autogluon fit 流程, 推理后处理不在 NPU
- y_embedding 仅支持 10 类, wine 3 类需 clip, 超过 10 类需重新训练
- 预处理 (StandardScaler) 在 CPU, 模型核心 forward 在 NPU, 已明确边界
- 动态 shape (N_train/N_test) 首次编译后复跑已验证, 改变 N_train 需重新编译约 75 ms
- 受 NPU softmax 精度影响, CPU-NPU max_abs ~2.5e-3, 已在阈值说明中放宽 atol 到 0.01 但 argmax 100% 一致