冬
gcw_IDzXRVNw/cryofm-v2-ascend
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

CryoFM2 昇腾 NPU 适配部署文档

1. 模型简介

CryoFM2 是来自字节跳动 Seed 团队的冷冻电镜密度图生成基础模型,论文 A Generative Foundation Model for Cryo-EM Densities。

本模型基于 Flow Matching 机制学习从简单高斯分布到复杂 Cryo-EM 密度图分布的连续映射。

模型架构

属性值
模型类型3D UNet (Flow Matching)
输入尺寸64×64×64 体素
输入通道2 (无条件生成) / 3 (条件增强)
输出通道1
下采样块DownBlock3D, DownBlock3D, AttnDownBlock3D, AttnDownBlock3D
上采样块AttnUpBlock3D, AttnUpBlock3D, UpBlock3D, UpBlock3D
块输出通道(64, 128, 256, 512)
每块层数2
注意力头维度8
归一化GroupNorm (32 groups)
激活函数SiLU
时间嵌入Positional Encoding

模型变体

  1. cryofm2-pretrain: 无条件预训练模型,用于通用密度图生成
  2. cryofm2-emhancer: EMhancer 风格密度图增强微调模型
  3. cryofm2-emready: EMReady 风格密度图增强微调模型

模型特点

  • Flow Matching: 稳定的生成式建模,无需要害的噪声调度
  • 3D UNet: 专为 Cryo-EM 三维密度图设计
  • 多风格增强: 支持 EMhancer 和 EMReady 两种增强风格
  • Bayesian 先验: 可作为下游任务的贝叶斯先验集成

输入输出格式

  • 输入: 形状 (batch_size, 2, 64, 64, 64) 的噪声张量
  • 输出: 形状 (batch_size, 1, 64, 64, 64) 的密度图

2. 环境依赖清单

2.1 硬件要求

  • 华为昇腾系列 AI 处理器(Ascend 910B/310P 等)
  • 建议 ≥ 4 GB NPU 显存(CryoFM2 推理峰值约 2 GB)
  • 本次实测使用 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.12
torch_npu与 PyTorch 版本匹配昇腾 PyTorch 扩展
cryofm0.1.0官方库 (pip install git+https://github.com/ByteDance-Seed/cryofm.git)
diffusers0.40.0+模型组件
mmengine0.10.7+配置文件加载

2.3 安装方式

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

# 2) 安装 cryofm 官方库(使用华为云源)
pip install git+https://github.com/ByteDance-Seed/cryofm.git \
    -i https://repo.huaweicloud.com/repository/pypi/simple

# 3) 验证环境
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/agent4/cryofm-v2-ascend

# 确认上游权重存在
ls -la /workspace/agent3/ByteDance-Seed/cryofm-v2/

# 期望输出:config.yaml cryofm2-pretrain/ cryofm2-emhancer/ cryofm2-emready/

步骤 2:检查 NPU 环境

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

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

步骤 3:运行推理脚本

# 无条件生成(默认 pretrain 模型)
python inference.py --device npu:0

# EMhancer 风格增强
python inference.py --device npu:0 --variant emhancer

# EMReady 风格增强
python inference.py --device npu:0 --variant emready

# 生成多个样本
python inference.py --device npu:0 --num_samples 4

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

4. 完整测试用例

测试用例 1:无条件生成(NPU:0)

conda activate pt2100
cd /workspace/agent4/cryofm-v2-ascend
python inference.py --device npu:0 --num_samples 1

实际输出:

============================================================
CryoFM2 昇腾 NPU 推理
============================================================

[Device] npu:0
[Model Path] /workspace/agent3/ByteDance-Seed/cryofm-v2/cryofm2-pretrain

[Loading] Model config...
  Model type: unet
  Process: fm

[Loading] Model weights from safetensors...
  Loaded 380 weights

[Creating] UNet3DModel...

[Loading] Weights into model...
  Model loaded successfully

[Model Loaded] Allocated: 0.32 GB, Reserved: 0.66 GB

[npu-smi info]
+------------------------------------------------------------------------------------------------+
| npu-smi 25.5.2                   Version: 25.5.2                                               |
+---------------------------+---------------+----------------------------------------------------+
| NPU   Name                | Health        | Power(W)    Temp(C)           Hugepages-Usage(page)|
| Chip                      | Bus-Id        | AICore(%)   Memory-Usage(MB)  HBM-Usage(MB)        |
+===========================+===============+====================================================+
| 0     910B2               | OK            | 104.3       41                0    / 0             |
| 0                         | 0000:C1:00.0  | 0           0    / 0          4165 / 65536         |
+===========================+===============+====================================================+
+---------------------------+---------------+----------------------------------------------------+
| NPU     Chip              | Process id    | Process name             | Process memory(MB)      |
+===========================+===============+====================================================+
| 0       0                 | 1668840       | python                   | 796                     |
+===========================+===============+====================================================+


[Creating] Model wrapper for sampling...
  Input shape: 1x2x64x64x64

[Running] Flow matching sampling (200 steps)...
100%|██████████| 200/200 [00:11<00:00, 17.57it/s]

[npu-smi info]
+------------------------------------------------------------------------------------------------+
| npu-smi 25.5.2                   Version: 25.5.2                                               |
+---------------------------+---------------+----------------------------------------------------+
| NPU   Name                | Health        | Power(W)    Temp(C)           Hugepages-Usage(page)|
| Chip                      | Bus-Id        | AICore(%)   Memory-Usage(MB)  HBM-Usage(MB)        |
+===========================+===============+====================================================+
| 0     910B2               | OK            | 140.9       44                0    / 0             |
| 0                         | 0000:C1:00.0  | 0           0    / 0          5380 / 65536         |
+===========================+===============+====================================================+
+---------------------------+---------------+----------------------------------------------------+
| NPU     Chip              | Process id    | Process name             | Process memory(MB)      |
+===========================+===============+====================================================+
| 0       0                 | 1668840       | python                   | 2011                    |
+===========================+===============+====================================================+


[Results]
  Output shape: torch.Size([1, 64, 64, 64])
  Output dtype: torch.float32
  Output min: -4.2705, max: 12.2534
  Output mean: 0.1077, std: 1.2960

[Memory After Inference] Allocated: 0.32 GB, Reserved: 1.75 GB

[Done]

性能指标

指标值
推理耗时~11 秒 (200 steps)
NPU 峰值显存~2 GB
生成样本形状(1, 64, 64, 64)

5. 注意事项

  1. 输入格式: 模型期望 2 通道输入(无条件生成时第二通道为零)
  2. 采样步数: 默认 200 步,可根据质量/速度权衡调整
  3. 数据类型: 推理使用 bfloat16,输出转换为 float32
  4. NPU 进程: 推理期间 npu-smi info 可看到 Python 进程在 NPU 上运行

6. 参考链接

  • CryoFM 官方仓库
  • CryoFM2 论文
  • Hugging Face 模型页