A deployment-ready adaptation of ByteDance Seed Team's CryoFM2 foundation model, optimized for running on Huawei Ascend 910 NPU accelerators. This repository provides the complete toolchain for serving cryo-EM density map generation and enhancement tasks through a lightweight HTTP API.
| Item | Specification |
|---|---|
| NPU Hardware | Ascend 910A (x2) |
| NPU Memory | 64 GB HBM per card |
| CPU Architecture | aarch64 |
| PyTorch | 2.9.0 |
| torch_npu | 2.9.0.post1 |
| CANN Toolkit | 8.5.1 |
| Python Runtime | 3.11.14 |
# Install dependencies
pip install torch torch_npu numpy safetensors diffusers pyyaml
# Launch inference server
python3 inference.py \
--model-dir /path/to/cryofm-v2 \
--variant cryofm2-pretrain \
--mode server \
--port 8000The server loads the model onto NPU:0 and exposes a RESTful API for both conditional and unconditional generation tasks.
For direct inference without a server:
# Unconditional generation (pretrained model)
python3 inference.py \
--model-dir /path/to/cryofm-v2 \
--variant cryofm2-pretrain \
--mode generate \
--num-samples 3 \
--num-steps 200 \
--output-dir ./output
# Conditional generation (EMhancer style)
python3 inference.py \
--model-dir /path/to/cryofm-v2 \
--variant cryofm2-emhancer \
--mode cond_generate \
--output-tag 1 \
--output-dir ./output_emhancer# Health check
curl http://localhost:8000/
# Generate a density map (50 sampling steps)
curl -X POST http://localhost:8000/ \
-H "Content-Type: application/json" \
-d '{"mode": "generate", "num_samples": 1, "num_steps": 50}'Expected response includes the output tensor shape [1, 1, 64, 64, 64] with statistical descriptors (mean, std, min, max) of the generated density map.
Performance data collected on Ascend 910A with bfloat16 mixed precision:
| Task | Sampling Steps | Latency | NPU Memory |
|---|---|---|---|
| Unconditional | 10 | 0.31s | ~5.5 GB |
| Unconditional | 50 | 2.41s | ~8.7 GB |
| Conditional (EMhancer) | 20 | ~1.0s | ~8.7 GB |
| Conditional (EMReady) | 20 | ~1.0s | ~8.7 GB |
The model achieves approximately 20 steps per second on a single Ascend 910A with the 3D UNet architecture.
The ported model maintains numerical fidelity with the reference implementation:
The simplified 3D UNet implementation achieves parameter count parity (380 groups) with the original safetensors checkpoint, and the forward pass produces consistent velocity field predictions throughout the flow matching trajectory.
Model constraints: The architecture is fixed at 64x64x64 voxel resolution; input maps must be preprocessed to match this dimension.
NPU environment: Requires torch_npu with compatible CANN drivers. The current validation was performed on CANN 8.5.1 with Ascend 910A. Older CANN versions may encounter operator compatibility issues with 3D convolutions.
Memory footprint: The 3D attention mechanism at 16x16x16 resolution consumes significant NPU memory. The implementation uses chunked attention (512-token chunks) to prevent OOM. For reference, the attention matrix at 16^3 resolution requires approximately 2.1 GB in fp32.
Sampling quality: Fewer steps (e.g., 10) produce faster but lower-quality density maps. The recommended step count is 200 for production use, balancing quality and inference time.
MRC file handling: The provided save_mrc function implements a minimal MRC writer. For production use, consider using the mrcfile Python library for standards-compliant output.
Service reliability: The HTTP server runs as a single-threaded implementation. For production deployment, wrap with a WSGI server (gunicorn/uvicorn) and add rate limiting.
Multi-GPU scaling: The current implementation uses single NPU. For batch processing, the inference.py script can be adapted with accelerate or torch.nn.DataParallel.
@article{Li2025.12.29.696802,
author={Li, Yilai and Yuan, Jing and Zhou, Yi and Wang, Zhenghua and others},
title={A Generative Foundation Model for Cryo-EM Densities},
year={2025},
doi={10.64898/2025.12.29.696802},
journal={bioRxiv}
}Apache 2.0