m
mengsihan/YuchenShen-FoMo-0D-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

FoMo-0D — 昇腾 NPU 零样本表格异常检测

标签:#NPU #Ascend #Ascend910

模型:YuchenShen/FoMo-0D(revision d0638efa) 任务:表格数据零样本异常检测(tabular classification / zero-shot outlier detection) 框架:PyTorch 2.9 + torch_npu 2.9(CANN 8.5.1) 运行设备:单卡 npu:0(Ascend 910B / Ascend910_9362)

FoMo-0D 是基于 PFN/TabPFN 主干 Transformer 的表格异常检测基础模型,通过 PyTorchModelHubMixin 发布。本仓库将其适配为可在昇腾 NPU 上真实运行的自包含推理脚本:默认执行 python inference.py 即完成权重加载、数据预处理、NPU 推理与结果输出。

模型简介

  • 架构:TransformerModel(4 层 Transformer 编码器,emsize=256、nhid=512、nhead=4、num_R=500 Router-Attention)
  • 输入:[seq, batch, num_features=100],输出:[seq, batch, num_classes=2] 对数几率
  • 参数量:4,886,786(约 4.9M)
  • 权重格式:model.safetensors(约 19.6 MB)
  • 功能:给定一组无标签参考行(context),为每个查询行输出其属于异常(class 1)的概率

数据契约

推理使用 scikit-learn 内置真实公开数据集 breast_cancer(569 行 × 30 特征,无联网下载,随 scikit-learn 发布):

项值
数据来源sklearn.datasets.load_breast_cancer(真实公开,SHA-256 6b202a2072f9a0385f405a8f8605b1b06f6f36ae6d23d9cd6cbbc0974a416bc7)
列数 / 特征30 个数值特征(mean radius, mean texture, …)
语义0=恶性(异常 / outlier),1=良性(正常 / normal)
context 行数200(从良性样本固定抽样,种子 42)
测试行数369(含 212 个异常)
预处理z-score → 逐列 QuantileTransformer → 特征缩放 → 零填充至 100 特征(与官方 zero_shot_on_adbench.py 一致)
推理输入形状context=(200, 1, 100),test=(369, 1, 100),dtype=fp32

推理脚本会在预处理阶段校验列数、数值可转换性,并按固定种子切分,保证可复现。

环境

  • 操作系统:Linux aarch64(openEuler)
  • CANN:8.5.1(/usr/local/Ascend/cann-8.5.1)
  • PyTorch:2.9.0,torch_npu:2.9.0.post1
  • NPU:Ascend 910B(Ascend910_9362),torch.npu.is_available() == True

安装

pip install -r requirements.txt

依赖:torch、torch-npu、numpy、pandas、scikit-learn、huggingface_hub、einops。

NPU 推理

默认命令(模型权重从 HuggingFace 在线加载):

python inference.py --device npu

离线(使用本地权重目录):

python inference.py --device npu --model-dir /path/to/YuchenShen--FoMo-0D

指定权重 URL(默认 YuchenShen/FoMo-0D):

MODEL_WEIGHT_URL=YuchenShen/FoMo-0D python inference.py --device npu

脚本自包含:模型定义(FoMo0DHub 及全部 pfns 源码)已内嵌于 inference.py,不依赖任何未提交的本地模块。成功时打印设备、输入形状、首轮耗时、稳定耗时与 AUROC,并退出码 0;异常退出非零。

真实 NPU 结果

在单卡 npu:0 上运行(本次适配实测):

[task]   FoMo-0D zero-shot tabular outlier detection  device=npu
[weights] YuchenShen/FoMo-0D
[device]  first-param-device=npu:0 dtype=torch.float32
[model]   params=4886786 config={num_features:100, emsize:256, nhid:512, nlayers:4, nhead:4, num_R:500, seq_len:5000}
[input]   context=(200, 1, 100) test=(369, 1, 100) device=npu:0
[run]     first-forward 0.163s output=(369, 1, 2)
[bench]   avg=4.74ms p50=4.74ms p95=4.79ms rows/s=77768
[result]  n_test=369 outliers=212 AUROC=0.9523
[status]  OK

任务指标(零样本异常检测):AUROC = 0.9523(无任何在目标数据上的训练,模型权重仅来自 HF 官方 checkpoint)。

CPU-NPU 一致性

同一权重、同一预处理、同一输入、eval 模式、fp32:

指标CPUNPU
AUROC0.95220.9523
概率输出最大绝对误差—0.0
argmax 一致率—100%

一致性阈值说明:任务输出为类别概率([0,1]),本次适配实测 CPU 与 NPU 概率输出完全一致(compare_outputs.py --atol 0.01 --rtol 0.05 判定 passed=true)。同时额外报告 logits 的 argmax 一致率 1.0。判定脚本返回 0。

性能

预热 3 次、正式计时 10 次,每次前后 torch.npu.synchronize():

项CPUNPU加速比
平均延迟87.24 ms4.74 ms18.4×
p50 / p9587.08 / 88.24 ms4.74 / 4.79 ms—
吞吐4,230 rows/s77,768 rows/s18.4×
  • 首轮编译耗时:0.163 s(仅首次,已单独报告,不含在稳定指标内)
  • 输入:context 200 行 + test 369 行,batch=1,shape (369, 1, 100),dtype=fp32
  • 计时包含整个 model() 前向(不含数据预处理与权重加载)

证据图

三张证据图由 xterm.js 依据本次真实运行日志渲染生成,非模拟/手绘:

  • assets/agent_workflow.png:侦察 → 数据 → 加载 → NPU 推理 → 一致性 → 性能 → 校验全流程
  • assets/npu_device_call.png:npu-smi info 与 torch.npu.is_available() 设备证据
  • assets/model_result.png:默认 python inference.py 的真实完整输出

限制

  • 本演示使用 breast_cancer 作为零样本异常检测样例;对任意表格数据需按数据契约做同样的 z-score/分位数/零填充预处理(特征数 > 100 时按官方实现随机子采样)。
  • 模型为推理-only 适配;未包含任何训练流程。
  • 权重不随仓库分发,运行时从 MODEL_WEIGHT_URL(默认 HuggingFace YuchenShen/FoMo-0D)加载;离线场景请使用 --model-dir 指向本地权重。