ThermoMPNN 蛋白质稳定性预测模型在昇腾 NPU 上的适配实践文档,涵盖环境准备、数据准备、训练与推理全流程。适配基于官方开源代码完成,在 Ascend 910C 单卡环境验证通过。
ThermoMPNN 是美国北卡罗来纳大学教堂山分校 Kuhlman 实验室研发的蛋白质点突变稳定性预测模型,旨在解决蛋白质工程中"突变对折叠稳定性影响"的预测问题。实验测定一个突变的 ΔΔG 需要数天时间和专门仪器,无法满足酶改造、抗体优化等场景对大规模突变筛选的需求;ThermoMPNN 从野生型结构出发,秒级完成单点突变的 ΔΔG 预测,可直接用于饱和突变扫描(每个残基 × 20 种氨基酸)的虚拟筛选。
ThermoMPNN 由两部分组成,总参数量约 3-4M:
| 项目 | 说明 |
|---|---|
| 输入 | 野生型蛋白结构(PDB 文件,含 N/CA/C/O 骨架原子)+ 链 ID + 突变列表(位置 / 野生型残基 / 突变残基) |
| 输出 | 每个突变一个标量 ddG(kcal/mol);正值 = 突变降低稳定性,负值 = 提升稳定性 |
| 限制 | 仅支持单点突变;不需要突变体结构、pH、温度等实验条件 |
模型结构图:
Input PDB
↓
┌─────────────────────────┐
│ ProteinMPNN (冻结) │ ← 预训练权重
│ ┌───────────────────┐ │
│ │ ProteinFeatures │ │ ← 提取 N/CA/C/O 原子坐标特征
│ │ (RBF距离特征) │ │
│ └───────────────────┘ │
│ ┌───────────────────┐ │
│ │ Encoder (3层) │ │ ← Transformer Encoder
│ │ EncLayer × 3 │ │
│ └───────────────────┘ │
│ ┌───────────────────┐ │
│ │ Decoder (3层) │ │ ← Transformer Decoder (自回归)
│ │ DecLayer × 3 │ │
│ └───────────────────┘ │
└─────────────────────────┘
↓ (隐藏层 + 嵌入)
┌─────────────────────────┐
│ TransferModel (可训练) │
│ ┌───────────────────┐ │
│ │ LightAttention │ │ ← 可选: 轻量注意力
│ │ (1D Conv) │ │
│ └───────────────────┘ │
│ ┌───────────────────┐ │
│ │ MLP (both_out) │ │ ← 多层感知机
│ │ 128*3+128 → 64→32→21 │
│ └───────────────────┘ │
│ ┌───────────────────┐ │
│ │ ddG_out (1→1) │ │ ← ddG 预测
│ └───────────────────┘ │
│ ┌───────────────────┐ │
│ │ subtract_mut │ │ ← mut_ddG - wt_ddG
│ └───────────────────┘ │
└─────────────────────────┘
↓
ddG 预测值| 优势 | 说明 |
|---|---|
| 低算力门槛 | 模型小、主干冻结,单卡 910C 即可完成训练,无多卡通信需求 |
| 数值一致 | 纯标准 PyTorch 算子构成,无自定义 CUDA 算子,适配仅需设备入口改造,模型结构零改动 |
| 虚拟筛选提速 | 单结构饱和突变扫描(数千突变)分钟级完成,替代湿实验初筛 |
本案例使用昇腾 910C 单卡,基于 PyTorch + torch_npu + PyTorch Lightning 完成 ThermoMPNN 的训练与推理实践。
| 硬件名称 | 配置信息 | 备注 |
|---|---|---|
| 机器型号 | Ascend 910C | aarch64 |
| 测试集群 | 1 卡 | 单机单卡 |
| 软件 | 版本 | 部署方式 |
|---|---|---|
| Driver | 26.0.rc1 | 宿主机 |
| Firmware | 9.0.0.0.205 | 宿主机 |
| Docker镜像OS | Ubuntu 22.04.5 | / |
| Python | 3.12.13 | 容器(预装) |
| CANN | 9.0.1 | 容器(预装) |
| PyTorch | 2.10.0 | 容器(预装) |
| torch_npu | 2.10.0.post2 | 容器(预装) |
| PyTorch lightning | 2.6.6 | 容器(预装) |
| torchmetrics/biopython/omegaconf/pandas等 | 见 pyproject.toml | pip |
注意:PyTorch 与 torch_npu 版本必须严格一致,且与CANN配套。昇腾容器镜像中预装的组合已经过配套调优,不要再用pip单独安装torch/torch_npu/torchvision,避免版本遮蔽导致 torchvision 导入失败等问题。
申请容器,基础镜像使用vllm-ascend:v0.23.0rc1-a3。
如果需要,安装 ThermoMPNN 运行时依赖(上述镜像已经包含如下依赖,不需要重复安装)
pip install --no-cache-dir \
torchmetrics==1.9.0 \
pytorch-lightning==2.6.6 \
biopython==1.88 \
omegaconf==2.3.1 \
wandb==0.30.0 \
pandas numpy tqdm joblib验证安装
python -c "import torch, torch_npu; print(torch.__version__, torch_npu.__version__)"
python -c "import pytorch_lightning, torchmetrics, Bio; print('deps OK')"代码与模型权重从官方仓库获取:
cd /home/work/
git clone https://github.com/Kuhlman-Lab/ThermoMPNN.git文件保存到目录:/home/work/
仓库自带全部所需权重,无需额外下载:
ThermoMPNN/
├──models/
| └──thermoMPNN_default.pt # 官方发布的最优模型(MegaScale 训练),推理直接使用
├──vanilla_model_weights/
| └──v_48_020.pt # ProteinMPNN 预训练主干,训练初始化使用
└──examples/
├──2OCJ.pdb # 推理示例结构
└──ThermoMPNN_inference_2OCJ.csv # 官方参考输出,用于精度比对将仓库中昇腾适配文件合入到该目录,详见七、其他参考中的适配改动说明。
训练数据来自 Zenodo(数据下载脚本 download_data.py 已提供,支持断点续传与 md5 校验):
| 数据集 | 地址 | 内容 |
|---|---|---|
| MegaScale (Tsuboyama 2023) | https://doi.org/10.5281/zenodo.7401274 | ~77 万条 ΔΔG 测量 + AlphaFold 结构 PDB(默认训练配置使用) |
| FireProt | https://doi.org/10.5281/zenodo.8169288 | FireProtDB 数据 + 单体 PDB(可选,两阶段训练使用) |
# 预览数据记录中的文件清单(建议先执行)
python download_data.py --list
# 下载全部(约 985MB),自动解压并整理目录
python download_data.py --data-root /home/work/ThermoMPNN/data_download --insecure下载完成后的目录结构:
ThermoMPNN/
└──data_download/
├──megascale/
| ├──Tsuboyama2023_Dataset2_Dataset3_20230416.csv # 来自 Processed_K50_dG_datasets.zip
| └──AlphaFold_model_PDBs/ # 约 862 个结构
└──fireprot/
├──4_fireprotDB_bestpH.csv
└──pdbs/monomers/在 local.yaml 中完成数据与路径配置(注:需要按照实际路径修改):
platform:
accel: "npu" # 加速器由 device_utils.py 自动解析
cache_dir: "/home/work/ThermoMPNN/cache" # PDB 解析缓存,自动创建
thermompnn_dir: "/home/work/ThermoMPNN" # 仓库根目录(必须包含 vanilla_model_weights/)
data_loc:
megascale_csv: "/home/work/ThermoMPNN/data_download/megascale/Tsuboyama2023_Dataset2_Dataset3_20230416.csv"
megascale_splits: "/home/work/ThermoMPNN/dataset_splits/mega_splits.pkl" # 仓库自带
megascale_pdbs: "/home/work/ThermoMPNN/data_download/megascale/AlphaFold_model_PDBs/"
fireprot_csv: "/home/work/ThermoMPNN/data_download/fireprot/4_fireprotDB_bestpH.csv"
fireprot_splits: "/home/work/ThermoMPNN/dataset_splits/fireprot_splits.pkl" # 仓库自带
fireprot_pdbs: "/home/work/ThermoMPNN/data_download/fireprot/pdbs/monomers/"训练入口为 train_thermompnn.py,超参在 config.yaml 中配置(默认: MegaScale 数据集、100 epochs、冻结主干、学习率调度):
# 进入模型目录
cd /home/work/ThermoMPNN
# 启动完整训练(epochs默认为100,workers默认为 12)
nohup bash -c 'ulimit -n 65535; export TORCH_NPU_DEVICE_CAPABILITY=8.0; python train_thermompnn.py' > train.log 2>&1 &
tail -f train.log
# 冒烟测试(只跑 3 轮)
nohup bash -c 'ulimit -n 65535; export TORCH_NPU_DEVICE_CAPABILITY=8.0; python train_thermompnn.py config.yaml training.epochs=3' > train_3ep.log 2>&1 &
tail -f train_3ep.log
# 训练启动后状态查询
tail -f train.log #查询日志
ps aux | grep train_thermompnn | grep -v grep #查询训练进程状态
npu-smi info #查看 NPU 占用,确认训练在卡上运行
#训练异常,杀死进程
pkill -9 -f train_thermompnn.pyThermoMPNN/
├──train_thermompnn.py # 训练入口(Lightning)
├──transfer_model.py # TransferModel 模型定义
├──protein_mpnn_utils.py # ProteinMPNN 主干与特征化
├──datasets.py # MegaScale / FireProt 数据集
├──device_utils.py # 昇腾适配:统一设备管理
├──download_data.py # 数据下载工具
├──config.yaml # 默认训练超参
├──local.yaml # 环境与数据路径配置
├──checkpoints/ # 训练输出(自动创建)
cache/ # PDB 解析缓存(自动创建)训练启动后,通过tail -f train.log日志回显
Configuration: {...}
Epoch 0: 100%|...| 800/800 [10:30<00:00]
val_ddG_spearman: 0.4x输出结果:
checkpoints/
└──<实验名>_epoch=05_val_ddG_spearman=0.7x.ckpt # 按 val_ddG_spearman 保留最优
train.log # 训练日志val_ddG_spearman / val_ddG_mse 等指标;val_ddG_spearman 最大化自动保存最优。
cd /home/work/ThermoMPNN
bash examples/inference_npu.sh| 参数 | 说明 |
|---|---|
--pdb | 输入的野生型结构文件(需含 N/CA/C/O 骨架原子) |
--chain | 使用的链 ID |
--model_path | 模型权重(.pt / .ckpt),缺省时自动搜索 models/ 目录 |
--out_dir | 结果输出目录 |
批量评测/基准打分使用 analysis/thermompnn_benchmarking.py 与 analysis/SSM.py(均已适配 NPU)。
输出 CSV(ThermoMPNN_inference_<PDB名>.csv),每行一个突变:
Model Dataset ddG_pred position wildtype mutation pdb chain
0 ThermoMPNN 2OCJ -0.040504 0 S A 2OCJ A
1 ThermoMPNN 2OCJ -0.301111 0 S C 2OCJ A
2 ThermoMPNN 2OCJ 0.010048 0 S D 2OCJ A
3 ThermoMPNN 2OCJ -0.051409 0 S E 2OCJ A
4 ThermoMPNN 2OCJ 0.058349 0 S F 2OCJ A
... ... ... ... ... ... ... ... ...
3875 ThermoMPNN 2OCJ 0.788022 193 L S 2OCJ A
3876 ThermoMPNN 2OCJ 0.646274 193 L T 2OCJ A
3877 ThermoMPNN 2OCJ 0.578121 193 L V 2OCJ A
3878 ThermoMPNN 2OCJ 0.6555 193 L W 2OCJ A
3879 ThermoMPNN 2OCJ 0.630072 193 L Y 2OCJ A
[3880 rows x 8 columns]
Saved ThermoMPNN output to /home/work/ThermoMPNN/analysis/ThermoMPNN_inference_2OCJ.csv与官方参考输出在 910C 上的精度对比(2OCJ 饱和突变扫描,共 3880 条):
| 突变 | NPU (910C) 输出 | 官方参考值 | 偏差 |
|---|---|---|---|
| S0A | -0.040504 | -0.040876 | 0.0004 |
| S0C | -0.301111 | -0.300581 | 0.0005 |
| S0D | 0.010048 | 0.009091 | 0.0010 |
| S0E | -0.051409 | -0.051891 | 0.0005 |
| S0F | 0.058349 | 0.058250 | 0.0001 |
全部预测偏差在 1e-3 量级以内(不同硬件浮点累加顺序的正常差异),数值一致性验证通过。
| 参考 | 链接 |
|---|---|
| ThermoMPNN 官方仓库 | https://github.com/Kuhlman-Lab/ThermoMPNN |
| ThermoMPNN 论文 | https://doi.org/10.1073/pnas.2314853121 |
| MegaScale 数据集 (Zenodo) | https://doi.org/10.5281/zenodo.7401274 |
| FireProt 数据集 (Zenodo) | https://doi.org/10.5281/zenodo.8169288 |
| torch_npu 官方文档 | https://gitee.com/ascend/pytorch |
| 昇腾社区 | https://gitee.com/ascend |
| CANN 产品文档 | https://www.hiascend.com/document |
相对官方代码的全部改动(模型结构零改动):
| 文件 | 改动 | 目的 |
|---|---|---|
device_utils.py(新增) | 统一设备管理:注册 torch_npu 后端、get_device() 自动选择设备、关闭 allow_internal_format | 收敛所有设备选择逻辑 |
train_thermompnn.py | Trainer 的 accelerator 由 resolve_accelerator() 按硬件解析 | Lightning 自动使用昇腾加速器 |
analysis/ 下 4 个脚本 | 硬编码 "cuda" / .cuda() / .to('cuda') 替换为 get_device() | 推理/基准脚本可在 NPU 上运行 |
model_utils.py、protein_mpnn_utils.py | 弃用别名 torch.clip → torch.clamp(等价替换) | 兼容性 |
pyproject.toml | 新增 npu extra(torch==2.5.1 + torch-npu==2.5.1) | uv 一键安装配套版本 |
examples/inference_npu.sh(新增) | NPU 推理脚本 | 交付配套 |
download_data.py(新增) | 训练数据下载工具 | 交付配套 |