1
gcw_xUvjaUyi/cryofm-v2-npu
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

ByteDance-Seed/cryofm-v2 — 昇腾 Ascend NPU 推理适配

ByteDance-Seed/cryofm-v2(CryoFM2)是字节跳动 Seed 团队发布的冷冻电镜(cryo-EM)密度图流匹配(flow-matching)基础模型。 本工程将 CryoFM2 适配到 昇腾 Ascend 910 NPU(torch_npu),全流程不依赖 CUDA/GPU,模型与输入张量全部运行在 npu:0。

重要说明:CryoFM2 的真实输入是 3D 密度图(64×64×64 体素),而非 2D 图像。 官方 HF 仓库只发布 config.yaml + model.safetensors(无 model 代码),本工程依据 state_dict 结构重建了 3D UNet。 由于模板要求输入 2D 图片(--img sample.png),此处把 2D 灰度图按深度复制并加轻微梯度,合成为一个 3D 密度图代理输入(synthetic proxy),用于演示 NPU 推理链路。真实使用时请替换为真实 3D 密度图/MRC 数据。

模型信息

变体输入通道用途额外模块
pretrain(默认)2无条件预训练:密度图生成/去噪—
emhancer3密度图增强(EMhancer 风格)类别嵌入 class_embedding(5→256)
emready3密度图增强(EMReady 风格)类别嵌入 class_embedding(5→256)
  • 架构:3D UNet,输入 64×64×64 体素
  • Down:DownBlock3D×2 + AttnDownBlock3D×2(block 通道 64/128/256/512)
  • Mid:ResNet×2 + Attention×1;Up:AttnUpBlock3D×2 + UpBlock3D×2
  • 时间嵌入:sin/cos 位置编码(64 维)+ MLP(→256)
  • 归一化:GroupNorm(32);激活:SiLU;输出:1 通道速度场 v
  • 参数量:约 168M;权重格式:safetensors (float32)

环境依赖

pip install -r requirements.txt

需要已安装昇腾 CANN 与 torch_npu(版本与本机 torch_npu 2.9.0 兼容即可)。验证 NPU:

python3 -c "import torch, torch_npu; print(torch.npu.device_count(), torch.npu.is_available())"
# 期望: 2 True

双模式使用

1) CLI

# 默认变体 pretrain(无条件流匹配去噪/速度场预测)
python3 inference.py --img sample.png

# 指定微调变体
python3 inference.py --img sample.png --variant emhancer --steps 5
python3 inference.py --img sample.png --variant emready --steps 5
  • --img:输入 2D 灰度图(cryo-EM 样)。未提供真实图片时自动合成一张并保存为 sample.png(已在 README 中如实标注为 synthetic)。
  • --variant:pretrain / emhancer / emready。
  • --steps:短程 Euler 流匹配积分步数(默认 5)。
  • 输出:模型信息、速度场 v 的统计量、v-prediction 去噪/重构估计、多步 Euler 重构估计。

2) FastAPI

uvicorn inference:app --host 0.0.0.0 --port 8104
  • GET /:服务信息。
  • POST /predict:multipart/form-data 上传图片(字段名 file),可选查询参数 variant、steps,返回推理 JSON:
curl -s -X POST -F "file=@sample.png" "http://127.0.0.1:8104/predict?variant=pretrain&steps=3"
curl -s -X POST -F "file=@sample.png" "http://127.0.0.1:8104/predict?variant=emhancer&steps=5"

图片预处理

  1. 读取 2D 灰度图(PIL.Image.convert("L"))。
  2. 缩放到 64×64。
  3. 归一化到 [0,1],按深度复制成 64×64×64 体素(带轻微线性渐变,模拟 3D 密度)。
  4. 按变体拼成 2 或 3 通道输入,在 t=0.5 处与高斯噪声做流匹配插值得到 x_t。

昇腾 NPU 适配说明

  • 无 CUDA:全文件删除 CUDA 相关调用,模型与输入 to(npu:0)。
  • NPU 初始化:入口 init_npu() 使用 try-except,torch_npu 导入失败或 npu:0 不可用时打印清晰中文错误并优雅退出。
  • 算子:3D UNet 全部由 Conv3d / GroupNorm / SiLU / Linear / softmax / bmm / interpolate 组成,float32 在昇腾上可跑;注意力打分使用 matmul 而非 baddbmm,避免部分 NPU 算子兼容问题。
  • ASCEND 日志:ASCEND_PROCESS_LOG_PATH 落到工程内 logs/ascend/。

权重缓存

  • 缓存路径固定为 ./model_weights/ByteDance-Seed__cryofm-v2(/ → __)。
  • 启动时优先加载本地缓存;未命中才通过 HF_ENDPOINT=https://hf-mirror.com 从镜像下载(huggingface.co 直连不可达)。
  • model_weights/ 已被 .gitignore 忽略,不会入库。

目录结构

cryofm-v2-npu/
├── inference.py          # 单文件双模式推理(CLI + FastAPI)
├── README.md
├── requirements.txt
├── sample.png            # 合成示例图(synthetic)
├── model_weights/        # 权重缓存(gitignore)
├── logs/                 # 运行日志
└── assets/               # 截图

版权与许可

模型权重来自 ByteDance-Seed/cryofm-v2,许可协议为 Apache 2.0。