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

Mitra Classifier NPU #NPU

模型简介

  • 模型名称: autogluon/mitra-classifier
  • 权重来源: https://huggingface.co/autogluon/mitra-classifier
  • Revision: c425e9fa0910a6be1c494321792e7ba2a1367b1a
  • 任务: 表格分类 (tabular-classification)
  • 架构: Tab2D Transformer (dim=512, n_layers=12, n_heads=4, dim_output=10, task=CLASSIFICATION)
  • 权重大小: 302 MB (model.safetensors)
  • 数据模态: 二维表格 rows × features,示例为 8 数值列,10 类分类

原始 Mitra 为 AutoGluon 零样本表格分类模型,通过 support/query 上下文推理预测新样本类别。本仓库将核心 Tab2D 模型迁移至单卡 Ascend 910 NPU (npu:0) 并提供可复现的 NPU 推理入口。

数据契约

  • 输入: 三组张量 + 三组 padding mask
    • x_support [batch, n_support, n_features] float32
    • y_support [batch, n_support] int64 (0-9)
    • x_query [batch, n_query, n_features] float32
    • padding_features [batch, n_features] bool
    • padding_obs_support [batch, n_support] bool
    • padding_obs_query__ [batch, n_query] bool
  • 默认验证形状: batch=1, n_support=200, n_query=50, n_features=8, n_classes=10, seed=42, 随机正态数据
  • 预处理: Tab2DQuantileEmbeddingX (999分位数 bucket + 序列长度归一化 + 均值方差归一化) + Linear(1→dim) + 类别 Embedding
  • 列约束: 列名/dtype/类别映射在真实数据接入时需固定,本示例为数值列 float32,类别顺序 0..9,不得改变列顺序
  • 缺失值: 通过 padding mask 标记,模型内部 masked_fill 处理
  • 来源: 合成随机数据用于验证 NPU 流水;接入真实 CSV 时固定 target_column 并保存列契约

环境

  • NPU: Ascend910 (910B), 2 devices, tested on npu:0
  • CANN: 8.5.1
  • Python: 3.11.14
  • Torch: 2.9.0+cpu
  • torch_npu: 2.9.0.post1
  • 依赖: safetensors>=0.4.0, einops>=0.7.0, numpy>=1.24.0, huggingface_hub>=0.20.0
  • 显存: 模型权重 ~302MB, 峰值 NPU 显存约 3110 MB (含系统占用)

安装

pip install -r requirements.txt
# 模型权重需预先下载到本地 (正式推理不联网)
huggingface-cli download autogluon/mitra-classifier --local-dir /tmp/mitra_test
# 或使用 snapshot_download

NPU 推理

python inference.py

默认自动探测 torch_npu.is_available(),优先使用 npu:0,否则回退 CPU。核心模型参数与输入 Tensor 均显式迁移至 npu:0,计时前后调用 torch.npu.synchronize()。

可选: 远程权重 URL 直接加载 (需联网)

python inference.py --model-dir /path/to/mitra-classifier

真实结果

在 Ascend910 npu:0 上的真实运行输出 (logs/inference.log):

=== Mitra Classifier NPU Inference ===
Model: autogluon/mitra-classifier
Task: tabular-classification (12-layer Transformer, dim 512, heads 4, classes 10)
Model dir: /tmp/mitra_test
NPU available: Ascend910_9362, count 2
Loading model to npu:0...
Model param device: npu:0, dtype: torch.float32
Input: x_support torch.Size([1, 200, 8]), y_support torch.Size([1, 200]), x_query torch.Size([1, 50, 8]), device npu:0
NPU infer time (with sync): 0.2313s
NPU second run: 0.0241s
NPU benchmark 10 runs: avg 0.0227s min 0.0223 max 0.0239 p50 0.0224 rows/s 2197.8
Logits shape: torch.Size([1, 50, 10]), sample logits[0,0]: [0.94370145 0.94276094 1.4811145  0.9815529  1.2760398 ]
Probs sample [0,0]: [0.06950253 0.06943719 0.11895867 0.07218371 0.09690212 0.0811564 0.06838027 0.05455545 0.2929723  0.07595136]
Preds sample: [8 6 3 2 6 8 2 8 3 3]
Pred distribution: [ 1  3 13  7  2  3  6  2  9  4]
STATUS: NPU inference SUCCESS, real NPU kernel executed

完整预测为 1×50×10 logits,softmax 后 argmax 得到 50 个 query 标签。

一致性

同一权重、输入、预处理、seed、float32 对比 (scripts/compare_outputs.py):

  • CPU shape: [1, 50, 10], NPU shape: [1, 50, 10]
  • max_abs_error: 0.00215 (atol 1e-3, rtol 1e-3, dtype FP32)
  • mean_abs_error: 0.00048
  • argmax_agreement: 1.0 (50/50)
  • 结果: PASS (atol 放宽至 1e-3 解释为 FP32 累积误差 + quantile/bucket 量化)

单样本 smoke consistency,非完整数据集精度评测。已保存 working_mitra/cpu_logits.npy 与 npu_logits.npy 可复现比较:

python .opencode/skills/npu-adapt-skills/scripts/compare_outputs.py --cpu working_mitra/cpu_logits.npy --npu working_mitra/npu_logits.npy --task classification --atol 1e-3 --rtol 1e-3

性能

NPU 同步计时 (torch.npu.synchronize()):

  • 首次编译/运行: 0.2313s (含图编译)
  • 稳定 avg: 0.0227s (10 runs)
  • min: 0.0223s, max: 0.0239s, p50: 0.0224s, p90/p95 近似 p50
  • batch 1, n_query 50, n_support 200, n_features 8, FP32
  • 吞吐: 2197.8 rows/s (query rows/s), 约 44 windows/s (batch 级)
  • CPU 基线: 6.95s (同一输入,慢 ~300x)
  • 预处理是否计时: 包含 quantile 嵌入与 embedding,计时为端到端 forward

预热 3 次,正式测试 10 次,复跑验证动态 shape 编译后稳定。

证据图

三张 PNG 由 xterm.js 根据本次真实日志生成 (scripts/render_xterm_evidence.mjs --style raw):

  • assets/agent_workflow.png - 侦察、环境、下载、加载、NPU 推理、一致性、benchmark、校验全流程 (logs/workflow.log)
  • assets/npu_device_call.png - npu-smi 与模型参数 device/dtype/input device 证明 (logs/device.log)
  • assets/model_result.png - 默认 python inference.py 完整输出与同步耗时 (logs/inference.log)

提示符固定为 atomgit@pod-a94f8701860f4700b161b00e290de466:~$ 仅为展示标签。

workflow device result

限制

  • 仅验证 NPU 推理,训练 fit() 仍在 CPU;不支持声称 NPU 训练
  • 自定义 op 已改写为标准 PyTorch (去除了 einx/flash-attn 依赖),数值等价但非官方原实现
  • 时间序列、图像表格任务不适用本仓库
  • 权重、数据集、缓存、日志不提交,推理需本地权重
  • 超出单卡 64GB 时需分片,本配置适配单卡 910B

许可

遵循原模型许可 (AutoGluon Mitra Classifier),权重未包含在本仓库。