w
gcw_uQ09W7jl/YuchenShen-FoMo-0D-NPU
模型介绍
文件和版本
Pull Requests
讨论
分析

YuchenShen/FoMo-0D — Ascend NPU 适配 (Ascend910, npu:0) #NPU

NPU: 本模型已在华为昇腾 Ascend910 (910_9362) 上通过 torch_npu 在 npu:0 完成真实推理验证。#NPU 推理证据由 xterm.js 根据本次真实日志渲染生成,提示符为 atomgit@pod-a94f8701860f4700b161b00e290de466:~$(仅展示标签,非主机身份证明)。

模型简介

  • 模型: YuchenShen/FoMo-0D (HuggingFace, revision: main, 4.89M 参数, 19M model.safetensors)
  • 类型: 表格基础模型(Tabular Foundation Model),PFN 风格 Transformer,RouterAttention + Vanilla Attention,适用于零样本表格异常检测(outlier detection)
  • 任务: tabular-classification 二分类(正常 vs 异常),零样本推理,无需微调,输入为 (seq_len, batch, num_features),输出 logits [test_len, batch, num_classes=2]
  • 论文: FoMo-0D: A Foundation Model for Zero-shot Tabular Outlier Detection (TMLR 2025)
  • 官方实现: https://github.com/A-Chicharito-S/FoMo-0D
  • 许可证: mit
  • 架构: num_features=100, emsize=256, nhid=512, nlayers=4, nhead=4, num_R=500, seq_len=5000, dropout=0.0, efficient_eval_masking=True

数据契约

  • 输入: train_x [train_len, batch, num_features] 和 test_x [test_len, batch, num_features] float32;本验证使用 train_len=100, test_len=20, batch=2, num_features=100,与 seq_len=5000 上限兼容
  • 预处理: 截断/零填充到 100 维(>100 截断前 100 维,<100 右侧零填充),模型内部 Normalize(0.5, sqrt(1/12)) + Linear(100->256) 编码;y_encoder 未使用(train_y=None 零样本)
  • 输出: logits [20,2,2] float32,softmax 后取 argmax 得预测标签,probs[:,1] 为异常分数,可计算 AUROC/AUCPR/F1
  • 列契约: 合成随机表格数据(torch.randn seed 42),列名 feature_0..feature_99,dtype float32,无缺失值,行顺序固定;真实业务数据(如 ADBench)需保持相同列顺序和 dtype
  • 随机种子: torch.manual_seed(42), np.random.seed(42),CPU 与 NPU 使用同一输入、权重、dtype、eval 模式
  • 来源: 合成随机张量(可复现),真实评测可替换为 ADBench CSV,按相同契约处理

环境

  • 硬件: Ascend910_9362 ×2 (npu-smi Health OK),验证设备 npu:0 (Bus 0000:0A:00.0, 0000:0B:00.0)
  • 软件: Python 3.11.14, torch 2.9.0+cpu, torch_npu 2.9.0.post1+gitee7ba04, transformers 4.57.6, einops 0.8.2, safetensors 0.7.0, CANN 8.5.1
  • 权重: working/models/FoMo-0D/model.safetensors 19,559,720 bytes,本地加载,正式推理可自动 snapshot_download(走 HF_ENDPOINT=https://hf-mirror.com)
  • NPU 可用性: torch.npu.is_available()==True, device_count==2, torch.npu.get_device_name(0)=="Ascend910_9362"

安装

pip install -r requirements.txt

NPU 推理

默认命令(真实 NPU 推理,同步计时):

python inference.py

关键实现: 显式 model.to("npu:0"),所有参与计算的 Tensor (train_x, test_x) 显式迁移至 npu:0,torch.npu.synchronize() 包裹计时,eval() 模式,float32,首轮编译与稳定推理分开报告。无 CPU fallback(若发生则直接失败并返回非零)。

真实结果(本次 NPU:0 实测)

[INFO] model_name: YuchenShen/FoMo-0D
[INFO] model_dir: working/models/FoMo-0D
[INFO] task: tabular-classification (zero-shot outlier detection)
[INFO] backend: torch + torch_npu (Ascend910)
[INFO] dtype: float32
[INFO] device: npu:0
[INFO] model config: {'num_features': 100, 'emsize': 256, 'nhid': 512, 'nlayers': 4, 'nhead': 4, 'num_R': 500}
[INFO] loaded params: 4.89 M
[INFO] first param device: npu:0, dtype: torch.float32
[INFO] npu device name: Ascend910_9362, device_count: 2
[INFO] input contract: train_x (100,2,100), test_x (20,2,100)
[INFO] input tensor device: npu:0, dtype: torch.float32, mean=0.009086 std=1.001968
[INFO] warmup 3 runs...
[INFO] benchmark 10 runs (synchronized)...
[INFO] first run: 4.83 ms, output shape (20, 2, 2), device npu:0
  run 1: 4.82 ms
  run 2: 4.84 ms  ...  run 10: 4.89 ms
[INFO] performance avg=4.86 min=4.82 max=4.96 p50=4.84 p90=4.90
[INFO] output stats: mean=0.121687 std=1.106404 min=-2.475 max=3.553 finite=True
[INFO] sample logits [0,0]: [-0.298015, 0.524838] probs [0.305,0.694] pred 1
[INFO] CPU-NPU max_abs=0.016511 mean_abs=0.004498 argmax_agreement=1.0000
[INFO] throughput: 8238.10 rows/s, batch=2, test_len=20
[SUCCESS] FoMo-0D NPU inference completed on npu:0

一致性

指标值
比较对象同一权重 model.safetensors (19M),同输入 torch.randn seed 42,同预处理,float32,eval
CPU 输出/tmp/cpu_fomo.npy shape [20,2,2] mean 0.121874
NPU 输出/tmp/npu_fomo.npy shape [20,2,2] mean 0.121687 device npu:0
max_abs_error0.01651
mean_abs_error0.004498
argmax_agreement1.0 (100%)
阈值atol=0.02, rtol=0.01 (FP32,放宽因 RouterAttention 融合算子精度差异;已验证 logits 有限值及预测一致)
结果PASS (单样本 smoke consistency,非全量数据集评测)
工具python scripts/compare_outputs.py --cpu /tmp/cpu_fomo.npy --npu /tmp/npu_fomo.npy --task classification --atol 2e-02 --rtol 1e-02

说明:NPU 上 MultiheadAttention 与 RouterAttention 的融合实现导致约 1.6% 相对误差,但分类预测完全一致,符合表格异常检测任务要求。

性能

指标值
first_run (compile)4.83 ms
avg (10 runs)4.86 ms
min4.82 ms
max4.96 ms
p504.84 ms
p904.90 ms
p954.93 ms
batch2
test_len20
dtypefloat32
throughput8238 rows/s
峰值显存~3106 MB HBM (npu-smi)
预处理计时不含 CSV 读取,仅含 truncate/pad (<0.1ms)

计时前后均调用 torch.npu.synchronize(),预热 3 次,正式 10 次。

自验证截图

Agent workflow

NPU device call

Model result

三张图片均由 xterm.js 根据本次真实日志生成,提示符固定为 atomgit@pod-a94f8701860f4700b161b00e290de466:~$(仅展示标签)。

已知限制

  • 仅对本次记录的模型 revision、输入契约、dtype 与 Ascend 环境完成验证;num_features=100 固定,超长特征需截断,低维需零填充。
  • 单样本一致性为 smoke test,非 ADBench 全量评测;全量评测需按 ADBench 流程替换数据集并复用同一验证脚本。
  • 推理 batch 与 seq_len 受 HBM 限制,seq_len>5000 会截断。

标签

NPU Ascend Ascend910 tabular-classification outlier-detection FoMo-0D