冬
gcw_IDzXRVNw/FoMo-0D-ascend
模型介绍
文件和版本
Pull Requests
讨论
分析

FoMo-0D 昇腾 NPU 适配部署文档

1. 模型简介

FoMo-0D (Foundation Model for Zero-shot Tabular Outlier Detection) 来自论文 FoMo-0D: A Foundation Model for Zero-shot Tabular Outlier Detection。

该模型是一个零样本表格异常检测的基础模型,可以在没有见过目标数据集的情况下直接进行异常检测。

模型架构

属性值
模型类型Transformer + RouterAttention
参数量4.89M
Embedding Size256
Hidden Dimension512
Transformer 层数4
Attention 头数4
Sequence Length5000
Router Tokens (R)500
输入特征维度100
输出类别2 (正常/异常)

模型特点

  • 零样本检测: 无需在目标数据集上训练即可检测异常
  • Router Attention: 使用路由器注意力机制处理长序列
  • Transformer 编码器: 标准 Transformer 编码器架构
  • 双分类输出: 输出每个测试点的异常/正常概率

输入输出格式

  • train_x: 形状 (seq_len, batch_size, num_features) 的训练数据
  • test_x: 形状 (test_seq_len, batch_size, num_features) 的测试数据
  • 输出: 形状 (test_seq_len, batch_size, 2) 的异常检测结果

2. 环境依赖清单

2.1 硬件要求

  • 华为昇腾系列 AI 处理器(Ascend 910B/310P 等)
  • 建议 ≥ 8 GB NPU 显存(FoMo-0D 推理峰值约 2.5GB)
  • 本次实测使用 910B2(单卡 64 GB)

2.2 软件依赖

依赖项版本要求说明
操作系统CentOS 7.6+ / Ubuntu 18.04+支持主流 Linux 发行版
CANN5.0.RC2+华为昇腾计算架构
Python3.10 / 3.12推荐 3.10+
PyTorch2.0.0+推荐 2.10
torch_npu与 PyTorch 版本匹配昇腾 PyTorch 扩展
huggingface_hub0.20+模型加载

2.3 额外依赖

FoMo-0D 需要从 GitHub 克隆官方仓库以获取模型定义:

# 克隆 FoMo-0D 仓库
git clone https://github.com/A-Chicharito-S/FoMo-0D.git /tmp/FoMo-0D

2.4 安装方式

# 1) 激活已预装 torch / torch_npu 的 conda 环境
conda activate pt2100

# 2) 安装模型依赖(使用华为云源)
pip install --index-url https://repo.huaweicloud.com/repository/pypi/simple \
    torch>=2.0.0 torch_npu>=2.0.0 huggingface_hub>=0.20.0

# 3) 克隆 FoMo-0D 仓库
git clone https://github.com/A-Chicharito-S/FoMo-0D.git /tmp/FoMo-0D

# 4) 验证环境
python -c "
import torch, torch_npu
print('torch:', torch.__version__)
print('torch_npu:', torch_npu.__version__)
print('NPU 可用:', torch.npu.is_available())
"

3. 分步推理操作流程

步骤 1:准备工作目录

# 进入适配目录
cd /workspace/agent/FoMo-0D-ascend

# 确认上游权重存在
ls -la /workspace/agent/FoMo-0D/

# 期望输出:config.json model.safetensors (~19MB) ...

步骤 2:克隆 FoMo-0D 仓库

# 克隆官方仓库(获取模型定义)
git clone https://github.com/A-Chicharito-S/FoMo-0D.git /tmp/FoMo-0D

步骤 3:检查 NPU 环境

# 检查 NPU 设备状态
npu-smi info

# 验证 torch_npu 安装
python -c "import torch; import torch_npu; print('NPU 可用:', torch.npu.is_available())"

步骤 4:运行推理脚本

# 基本用法:默认参数 (seq_len=5000, test_len=10, batch_size=8)
python inference.py --device npu:0

# 查看 NPU 显存使用
python inference.py --device npu:0 --show_npu_memory

# 自定义参数
python inference.py --device npu:0 --seq_len 5000 --test_len 100 --batch_size 16

# 指定其他 NPU 卡
python inference.py --device npu:1

# CPU 模式(仅在 NPU 不可用时使用)
python inference.py --device cpu

4. 完整测试用例

测试用例 1:标准异常检测(NPU:0)

conda activate pt2100
cd /workspace/agent/FoMo-0D-ascend
python inference.py --device npu:0 --show_npu_memory

实际输出:

[W819 06:15:21.688247230 FunctionLoader.cpp:48] Warning: LD_PRELOAD detected...
[W819 06:15:27.688128510 NPUCachingAllocator.cpp:198] Warning: The current CANN and Soc Version require processing for 32 padding size...
[设备] npu:0
NPU设备数量: 1
  设备 0: Ascend910B2

[模型加载] 从: /workspace/agent/FoMo-0D
using vanilla + router
last_layer_no_R=True, is_final_layer=False
using vanilla + router
last_layer_no_R=True, is_final_layer=False
using vanilla + router
last_layer_no_R=True, is_final_layer=False
using vanilla + router
last_layer_no_R=True, is_final_layer=True
Initialized decoder for standard with (None, 2)  and nout 2
Loading weights from local directory
[模型] FoMo-0D (零样本表格异常检测)
[参数量] 4.89M
[配置] num_features: 100
[配置] emsize: 256
[配置] nhid: 512
[配置] nlayers: 4
[配置] nhead: 4
[配置] seq_len: 5000
[配置] num_R: 500
[模型已迁移至] npu:0
[NPU显存 · 模型加载后] 总显存=61.0GB | 已分配=18.7MB | 缓存=22.00MB | 峰值=18.7MB

构建测试输入:
  - train_x: (seq_len=5000, batch_size=8, num_features=100)
  - test_x: (seq_len=10, batch_size=8, num_features=100)

运行推理...
[NPU显存 · 推理完成后] 总显存=61.0GB | 已分配=2209.1MB | 缓存=2770.00MB | 峰值=2533.2MB

==============================================================================
输出摘要
==============================================================================
  模型: FoMo-0D (零样本表格异常检测)
  train_x形状: (5000, 8, 100)
  test_x形状: (10, 8, 100)
  输出形状: (10, 8, 2)
  (test_seq_len, batch_size, num_classes=2)
  总耗时: 0.31s
  输出 finite: True
  输出均值: -0.0633
  输出标准差: 1.4348
  输出范围: [-2.5050, 2.2355]
  预测类别分布: {0: 79, 1: 1}

✅ 验证要点:

  • 输出形状 (10, 8, 2): test_seq_len=10, batch_size=8, num_classes=2
  • NPU 推理稳定: 单次推理 0.31 秒
  • NPU 显存峰值: 约 2.5 GB
  • 输出有限值: 所有输出均为有限值,无 NaN/Inf
  • 预测分布合理: 随机数据下异常检测结果分布合理

5. 常见问题解答

Q1:提示 "No module named 'fomo_hub'" 怎么办?

解决方案:请确保已克隆 FoMo-0D 官方仓库到 /tmp/FoMo-0D:

git clone https://github.com/A-Chicharito-S/FoMo-0D.git /tmp/FoMo-0D

Q2:NPU 显存不足怎么办?

解决方案:

  1. 减小 batch_size 参数
  2. 减小 test_len 参数
  3. 使用 CPU 模式

Q3:输入特征的维度不是 100 怎么办?

解释:FoMo-0D 的 num_features 固定为 100。对于不同维度的输入,模型会进行下采样或零填充。

Q4:模型支持哪些任务?

解释:FoMo-0D 主要用于零样本表格异常检测。模型输出每个测试点的正常/异常二分类概率。

6. 文件结构

FoMo-0D-ascend/
├── inference.py              # 推理脚本(torch_npu 推理)
├── README.md                 # 本文档
├── requirements.txt          # 依赖列表
└── assets/                   # 资源占位目录

上游权重目录(只读,未被修改):

/workspace/agent/FoMo-0D/
├── config.json               # 模型配置
├── model.safetensors         # 权重(4.89M, ~19MB)
└── README.md                 # 原始文档

7. requirements.txt - 依赖列表

# FoMo-0D 昇腾 NPU 推理环境依赖

# 核心依赖(必须)
torch>=2.0.0
torch_npu>=2.0.0
huggingface_hub>=0.20.0
safetensors>=0.4.0
numpy>=1.21.0

# 注意: 需要从 GitHub 克隆 FoMo-0D 仓库
# git clone https://github.com/A-Chicharito-S/FoMo-0D.git /tmp/FoMo-0D

使用方式:

# 激活环境
conda activate pt2100

# 安装依赖(华为云源)
pip install --index-url https://repo.huaweicloud.com/repository/pypi/simple -r requirements.txt

# 克隆 FoMo-0D 仓库
git clone https://github.com/A-Chicharito-S/FoMo-0D.git /tmp/FoMo-0D

8. 参考资源

  • FoMo-0D 论文 (OpenReview)
  • FoMo-0D GitHub 仓库
  • HuggingFace 模型页 (YuchenShen/FoMo-0D)
  • DTE 论文
  • PFN 论文
  • 华为昇腾文档
  • torch_npu GitHub