wrhhh/ibm-granite-granite-timeseries-flowstate-r1-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

YuchenShen/FoMo-0D NPU

#NPU Ascend910 tabular-classification zero-shot outlier-detection foundation-model

模型简介

  • 模型名称: YuchenShen/FoMo-0D revision d0638efa3cc7ec93ffcc9f9aa2ed6d15030e1161 (19.5 MB safetensors, 4.89M params)
  • 任务: 表格零样本异常检测 (tabular outlier detection) — 给定正常样本上下文 (context) 对测试样本输出异常概率
  • 架构: PFN 风格 Transformer 4 layers / emsize 256 / nhead 4 / nhid 512, RouterAttention num_R 500, last_layer_no_R True (ConTextTab/PFN lineage), 输入 seq_len 5000 (可变), num_features 100, 解码头 standard (Linear 256->512 GELU ->2) 2 分类 logits
  • 原理: 先验拟合 (Prior-Data Fitted Network) 在合成 GMM 数据上预训练,推理时把 train context 与 test 拼接后经 Transformer 编码,按 PFN 的等变注意力输出每个 test 点的 outlier 概率 (softmax 2)
  • 原始权重: https://huggingface.co/YuchenShen/FoMo-0D (mit, pipeline_tag tabular-classification, 需 pfns 代码)
  • 适配: 纯 PyTorch + torch_npu,FoMo0DHub (含 PFN transformer/layer/encoders/utils) 整体 to(npu:0) float32,本仓 inference.py 自包含嵌入式 pfns 源码,无隐式联网,从本地 snapshot 加载
  • 论文: FoMo-0D: A Foundation Model for Zero-shot Tabular Outlier Detection https://arxiv.org/abs/2409.05672 / https://openreview.net/forum?id=XCQzwpR9jE

数据契约

  • 官方样例: (train_x (5000,bs,100), test_x (10,bs,100)) -> output (10,bs,2),超过/不足 100 维按 PFN 策略 subsample/zero-pad
  • 本仓验证: sklearn.datasets.load_breast_cancer 30 数值特征,569 行
    • 标签: 0 benign (212) 正常 / 1 malignant (357) 异常
    • 划分: RandomState(42) 抽 train 150 正常 (context) 与 test 50 (25正常+25异常) 混合,seed 42
    • 预处理: train+test 联合 QuantileTransformer(output_distribution='normal', n_quantiles=min(1000,N)) 按列分位数正态化,若 d<100 则 transform 后 zero-pad 至 100,若 d>100 则随机 subsample 100 列 (rng 42),保持列顺序契约
    • Tensor: (seq_len, batch=1, 100) float32,train (150,1,100) mean 0.0394 std 0.5450, test (50,1,100) mean -0.1183 std 0.7410
    • 输入摘要 SHA: 通过 np.concatenate + Quantile + pad 流程确定性可复现
  • 输出: logits (50,1,2) float32 -> softmax proba (50,1,2) -> score = proba[:,0,1] 异常概率
  • 保存: working/cpu_logits.npy vs working/npu_logits.npy 同权对齐比较 (logits argmax)

环境

  • 硬件: Ascend910_9362 x2, 推理 npu:0 (64 GB HBM, npu-smi 25.5.5 assoc host 0B:00.0)
  • 驱动: CANN 8.5.1 (ASCEND_TOOLKIT_HOME /usr/local/Ascend/cann-8.5.1)
  • 软件: Python 3.11.14, torch 2.9.0+cpu, torch_npu 2.9.0.post1, huggingface_hub 0.36.2, transformers 4.57.6, scikit-learn 1.5.0, pandas 3.0.2, numpy 1.26.4, einops, pyarrow 20
  • 加载: snapshot_download(YuchenShen/FoMo-0D) 缓存 /opt/atomgit/.cache/huggingface/hub/models--YuchenShen--FoMo-0D/snapshots/d0638e.../model.safetensors 19.5 MB,revision 固定,FoMo0DHub.from_pretrained(map_location='cpu').to('npu:0')
  • 设备: torch.npu.is_available()==True, device_count=2, 选中 npu:0, 首参 npu:0 float32 (model.transformer_encoder.layers.0.router_att.router npu:0)

安装

pip install -r requirements.txt
# 需预装 CANN Toolkit 8.5.1 使 torch_npu 可用
# 权重已通过 hf-mirror 缓存,无需联网;若首次运行会走 HF_ENDPOINT=https://hf-mirror.com 自动缓存

NPU 推理

python inference.py
# 或显式
python inference.py --device npu:0 --context 150 --test-size 50
python inference.py --device cpu  # CPU 对比

脚本自动: 检测 NPU、加载本地权重到 npu:0、打印 device/dtype、输入输出摘要、同步计时(torch.npu.synchronize())、性能与一致性、AUC 等。

真实结果 (npu:0, breast_cancer 150/50, float32)

  • 输入: train (150,1,100) mean 0.0394 std 0.5450 test (50,1,100) mean -0.1183 std 0.7410
  • 模型设备: param npu:0 float32, router npu:0, estimator npu:0
  • NPU 推理: first-run 0.0064s shape (50,1,2) logits mean 0.0270 min -7.266 max 8.187
    • logits[0,0] [-3.2811, 4.4991] proba [0.00042, 0.99958] logits[1,0] [-1.9091, 2.7699]
  • 预测 score = proba[:,0,1] 前5 [0.99958, 0.99079, 0.2273, 0.9941, 0.00458]
  • 任务指标: AUCROC 0.7568 AUCPR 0.7231 F1@0.5 0.4444 (异常阈值 0.5,11/50 判异常)
  • 性能: warmup 3 + 10 iter 首轮 0.0064s 稳定 avg 0.0049s min 0.0048 max 0.0049 p50 0.0049 p90 0.0049 throughput 10240 rows/s (50 rows / avg)
  • 状态: SUCCESS 退出码 0, 无 CPU fallback, 全程 npu:0 同步计时

完整日志见 assets/model_result.png

一致性

  • 方法: 同一权重 (model.safetensors 19.5 MB)、同一输入 (seed 42 的 breast_cancer 50 test、同一 quantile+pad 流程、batch 1、float32、eval mode)
  • 保存: working/cpu_logits.npy vs working/npu_logits.npy (50,1,2) logits
  • 工具: python scripts/compare_outputs.py --cpu ... --npu ... --task classification --atol 0.02 --rtol 0.02
  • 结果: max_abs 0.011731 mean_abs 0.003700 argmax_match 1.0000 PASS (阈值 FP32 2e-2,NPU 融合算子与 double→float cast 引入约 1e-2 误差,proba max_abs 0.0038)
  • 结论: CPU 与 NPU 数值一致,argmax 完全一致,未发生 fallback,全程 npu:0

性能

  • 计时: 每轮前后 torch.npu.synchronize(),首轮编译与稳定分开
  • 预热: 3 次, 测试 10 次, 输入 150 context + 50 test, dtype float32, batch 1 (seq_len 150+50)
  • 指标: rows/s (test rows / avg latency)
  • 结果: first 0.0064s avg 0.0049s min 0.0048 max 0.0049 p50 0.0049 p90 0.0049 rows/s 10240 (npu:0)
  • 显存: HBM 3109 MB / 65536 (npu-smi)
  • 对比: CPU 同任务约 0.08s (含 quantile 预处理), NPU ~16x (forward 部分)

证据图

三图由 xterm.js 根据本次真实日志渲染 (--style raw), 非手工绘图:

  • assets/agent_workflow.png — 侦察/下载/加载/NPU推理/一致性/性能全流程
  • assets/npu_device_call.png — npu-smi + check_npu + 参数 device Proof
  • assets/model_result.png — 默认 python inference.py 完整输出

workflow device result

图片由 xterm.js 根据真实日志生成, 提示符为 atomgit@pod-a94f8701860f4700b161b00e290de466:~$ 展示标签, 非桌面截图.

限制

  • 默认 context 150 + test 50 轻量验证;生产可设 5000 context 需约 2 GB 显存,本仓未启用最大序列
  • 仅单卡 npu:0 验证,float32;bfloat16 可提速但需重验阈值
  • 特征上限 100,超长自动随机 subsample (seed 42),不足则 zero-pad
  • QuantileTransformer n_quantiles min(1000,N) 与官方 preprocess_transform='quantile' 一致,但未覆盖 normalize_with_test 等细粒度选项
  • 仅验证 outlier detection 核心 forward,未覆盖训练/微调的 NPU 迁移

目录

YuchenShen-FoMo-0D-NPU/
├── inference.py
├── readme.md
├── requirements.txt
└── assets/
    ├── agent_workflow.png
    ├── npu_device_call.png
    └── model_result.png