r
redannancy/cryofm-v2
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

CryoFM2: Ascend NPU Deployment for Cryo-EM Density Map Generation

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.

Validated Environment

ItemSpecification
NPU HardwareAscend 910A (x2)
NPU Memory64 GB HBM per card
CPU Architectureaarch64
PyTorch2.9.0
torch_npu2.9.0.post1
CANN Toolkit8.5.1
Python Runtime3.11.14

Service Deployment

Quick Start

# 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 8000

The server loads the model onto NPU:0 and exposes a RESTful API for both conditional and unconditional generation tasks.

Command-Line Inference

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

Smoke Test Validation

# 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 Benchmarks

Performance data collected on Ascend 910A with bfloat16 mixed precision:

TaskSampling StepsLatencyNPU Memory
Unconditional100.31s~5.5 GB
Unconditional502.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.

Accuracy Assessment

The ported model maintains numerical fidelity with the reference implementation:

  • bfloat16 vs float32 deviation: < 0.01 in mean and std for generated density maps
  • Output shape: [batch, 1, 64, 64, 64] consistent with reference
  • Sampling method: Euler ODE solver with configurable step count (50-500)
  • Value range: Typical output range [-7.0, 6.0] for 50-step generation

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.

Cautions and Limitations

  1. Model constraints: The architecture is fixed at 64x64x64 voxel resolution; input maps must be preprocessed to match this dimension.

  2. 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.

  3. 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.

  4. 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.

  5. 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.

  6. 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.

  7. 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.

Citation

@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}
}

License

Apache 2.0