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 | 无条件预训练:密度图生成/去噪 | — |
emhancer | 3 | 密度图增强(EMhancer 风格) | 类别嵌入 class_embedding(5→256) |
emready | 3 | 密度图增强(EMReady 风格) | 类别嵌入 class_embedding(5→256) |
DownBlock3D×2 + AttnDownBlock3D×2(block 通道 64/128/256/512)AttnUpBlock3D×2 + UpBlock3D×2pip 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# 默认变体 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)。uvicorn inference:app --host 0.0.0.0 --port 8104GET /:服务信息。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"PIL.Image.convert("L"))。x_t。to(npu:0)。init_npu() 使用 try-except,torch_npu 导入失败或 npu:0 不可用时打印清晰中文错误并优雅退出。Conv3d / GroupNorm / SiLU / Linear / softmax / bmm / interpolate 组成,float32 在昇腾上可跑;注意力打分使用 matmul 而非 baddbmm,避免部分 NPU 算子兼容问题。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。