anning-2026/FoMo-0D
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

YuchenShen/FoMo-0D 昇腾 NPU 部署文档

1. 模型简介

模型名称: YuchenShen/FoMo-0D(FoMo-0D: A Foundation Model for Zero-shot Tabular Outlier Detection) 模型链接: https://huggingface.co/YuchenShen/FoMo-0D 论文: OpenReview 模型描述: 零样本表格异常检测(tabular outlier detection)基础模型。输入训练(上下文)表格数据 + 待检测表格数据,输出每个待检测样本的 2 类 logits(正常/异常),softmax 后取异常类概率作为异常分数,无需针对特定数据集训练即可迁移检测。 模型架构: PFN prior-style Transformer(encoder 为归一化 uniform Linear 特征编码 + y_encoder + TransformerEncoder 4 层),decoder 输出 num_class=2 参数规模: ~18MB(model.safetensors 19,559,720 字节,4.9M params) 适配引擎: torch_npu(昇腾 NPU 推理引擎)

2. 环境依赖

依赖项版本说明
torch2.9.0
torch_npu2.9.0.post1
huggingface_hub>= 0.24PyTorchModelHubMixin(from_pretrained)
einops>= 0.6pfns 包依赖
fastapi / uvicorn / pydantic-服务化推理(server.py)

注:本模型是 PyTorchModelHubMixin 格式(非 transformers safetensors),需 GitHub custom code(FoMo-0D 仓库:fomo_hub.py + pfns 包),权重与 config 放 /tmp/FoMo-0D,代码放 /tmp/FoMo-repo。

3. 推理步骤

# 随机生成 200 训练样本 + 20 测试样本做零样本异常检测
python inference.py --device npu:0

# 自定义样本数与特征维度
python inference.py --train_samples 500 --test_samples 50 --num_features 100 --device npu:0

推理流程:FoMo0DHub.from_pretrained(local_dir)(PyTorchModelHubMixin 加载 safetensors)→ model.to('npu:0') → 构造 train_x (n_train, 1, d) / test_x (n_test, 1, d) → model(train_x, test_x)(bf16 autocast)→ softmax 取异常类概率。

4. 测试样例及输出结果

以下输出为在昇腾 Ascend 910(CANN 8.5.1 + torch_npu 2.9.0)上实测获得(随机高斯表格数据,num_features=100)。

命令:

python inference.py --device npu:0

输出:

[INFO] 模型加载完成: FoMo0DHub (4.9M params, device=npu:0) [INFO] train_x: (200, 1, 100) test_x: (20, 1, 100) [结果] logits shape: (20, 1, 2) [结果] 异常分数 top5: ['0.0967', '0.6222', '0.8093', '0.2134', '0.2898'] [结果] 预测为异常样本数: 11 / 20 [耗时] 0.175s(20 个测试样本)

结果说明: 20 个随机高斯测试样本经 FoMo-0D 零样本检测,输出每个样本的异常分数(异常类概率),0.175s 完成,验证了 FoMo-0D 在昇腾 NPU 上的零样本表格异常检测推理链路通畅。

5. Agent 适配截图

Agent 适配流程 NPU 设备调用 模型适配结果

6. 服务化推理

python server.py --port 8000 --device npu:0

接口:

  • GET /health → {"status":"ok","model":"YuchenShen/FoMo-0D","device":"npu:0","loaded":true}
  • POST /infer(JSON:{"train_data": [[...]], "test_data": [[...]]} 或 {"train_samples":200,"test_samples":10,"num_features":100} 随机数据)→ {"logits_shape":[...],"anomaly_scores":[...],"elapsed_s":...}

实测(服务化):

  • POST /infer {"train_samples":200,"test_samples":10} → {"logits_shape":[10,1,2],"anomaly_scores":[0.1366,0.7237,...],"elapsed_s":0.172}

7. 注意事项

  • 模型权重位于 /tmp/FoMo-0D/(18MB safetensors + config.json),代码位于 /tmp/FoMo-repo/(GitHub A-Chicharito-S/FoMo-0D custom code:fomo_hub.py + pfns 包),避免占用有限的 /opt 空间,不纳入本仓库
  • 输入形状——模型 forward 内部 cat(train_x, test_x, dim=0) 拼接训练与测试段(single_eval_pos 分隔),两段除序列维度外形状须一致:train_x (n_train, 1, d)、test_x (n_test, 1, d)(batch=1)
  • 官方推理为逐样本前向,本部署支持整批 test_x 一次性前向(cat 拼接),NPU 上 bf16 autocast 推理
  • 大文件(本模型仅 18MB)用 hf-mirror hf_hub_download 下载