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 Size | 256 |
| Hidden Dimension | 512 |
| Transformer 层数 | 4 |
| Attention 头数 | 4 |
| Sequence Length | 5000 |
| Router Tokens (R) | 500 |
| 输入特征维度 | 100 |
| 输出类别 | 2 (正常/异常) |
(seq_len, batch_size, num_features) 的训练数据(test_seq_len, batch_size, num_features) 的测试数据(test_seq_len, batch_size, 2) 的异常检测结果| 依赖项 | 版本要求 | 说明 |
|---|---|---|
| 操作系统 | CentOS 7.6+ / Ubuntu 18.04+ | 支持主流 Linux 发行版 |
| CANN | 5.0.RC2+ | 华为昇腾计算架构 |
| Python | 3.10 / 3.12 | 推荐 3.10+ |
| PyTorch | 2.0.0+ | 推荐 2.10 |
| torch_npu | 与 PyTorch 版本匹配 | 昇腾 PyTorch 扩展 |
| huggingface_hub | 0.20+ | 模型加载 |
FoMo-0D 需要从 GitHub 克隆官方仓库以获取模型定义:
# 克隆 FoMo-0D 仓库
git clone https://github.com/A-Chicharito-S/FoMo-0D.git /tmp/FoMo-0D# 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())
"# 进入适配目录
cd /workspace/agent/FoMo-0D-ascend
# 确认上游权重存在
ls -la /workspace/agent/FoMo-0D/
# 期望输出:config.json model.safetensors (~19MB) ...# 克隆官方仓库(获取模型定义)
git clone https://github.com/A-Chicharito-S/FoMo-0D.git /tmp/FoMo-0D# 检查 NPU 设备状态
npu-smi info
# 验证 torch_npu 安装
python -c "import torch; import torch_npu; print('NPU 可用:', torch.npu.is_available())"# 基本用法:默认参数 (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 cpuconda 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解决方案:请确保已克隆 FoMo-0D 官方仓库到 /tmp/FoMo-0D:
git clone https://github.com/A-Chicharito-S/FoMo-0D.git /tmp/FoMo-0D解决方案:
batch_size 参数test_len 参数解释:FoMo-0D 的 num_features 固定为 100。对于不同维度的输入,模型会进行下采样或零填充。
解释:FoMo-0D 主要用于零样本表格异常检测。模型输出每个测试点的正常/异常二分类概率。
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 # 原始文档# 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