Ascend-SACT/ThermoMPNN
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

ThermoMPNN 昇腾 NPU 适配指导(训练与推理)

ThermoMPNN 蛋白质稳定性预测模型在昇腾 NPU 上的适配实践文档,涵盖环境准备、数据准备、训练与推理全流程。适配基于官方开源代码完成,在 Ascend 910C 单卡环境验证通过。


一、业务背景

ThermoMPNN 是美国北卡罗来纳大学教堂山分校 Kuhlman 实验室研发的蛋白质点突变稳定性预测模型,旨在解决蛋白质工程中"突变对折叠稳定性影响"的预测问题。实验测定一个突变的 ΔΔG 需要数天时间和专门仪器,无法满足酶改造、抗体优化等场景对大规模突变筛选的需求;ThermoMPNN 从野生型结构出发,秒级完成单点突变的 ΔΔG 预测,可直接用于饱和突变扫描(每个残基 × 20 种氨基酸)的虚拟筛选。

模型架构

ThermoMPNN 由两部分组成,总参数量约 3-4M:

  • ProteinMPNN 主干:3 层 encoder + 3 层 decoder 的图 Transformer(hidden=128),以骨架坐标为输入学习残基结构表征,预训练权重来自蛋白质序列设计任务,训练时默认冻结;
  • ddG 输出头:取突变位点的主干隐层表征(256 维)与序列嵌入(128 维)拼接,经 MLP(可选 LightAttention)输出 21 维氨基酸伪能量,突变型与野生型做差得到 ΔΔG。
项目说明
输入野生型蛋白结构(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 预测值

训练范式与技术要点

  • 迁移学习:利用蛋白质序列设计任务预训练的 ProteinMPNN 主干,冻结后仅训练输出头,以大规模 MegaScale 数据(~77 万条 ΔΔG 测量,Tsuboyama 2023)为主任务训练,可选 FireProt 数据集两阶段微调;
  • 参考态差分:模型输出对 20 种氨基酸各给一个伪能量,ΔΔG = 伪能量(突变型) − 伪能量(野生型),使预测天然以野生型为零点;
  • 评估指标:验证集 Spearman 相关系数(论文最优约 0.79)、MSE / RMSE。

昇腾适配价值

优势说明
低算力门槛模型小、主干冻结,单卡 910C 即可完成训练,无多卡通信需求
数值一致纯标准 PyTorch 算子构成,无自定义 CUDA 算子,适配仅需设备入口改造,模型结构零改动
虚拟筛选提速单结构饱和突变扫描(数千突变)分钟级完成,替代湿实验初筛

本案例使用昇腾 910C 单卡,基于 PyTorch + torch_npu + PyTorch Lightning 完成 ThermoMPNN 的训练与推理实践。


二、环境准备

2.1 硬件环境

硬件名称配置信息备注
机器型号Ascend 910Caarch64
测试集群1 卡单机单卡

2.2 软件环境

软件版本部署方式
Driver26.0.rc1宿主机
Firmware9.0.0.0.205宿主机
Docker镜像OSUbuntu 22.04.5/
Python3.12.13容器(预装)
CANN9.0.1容器(预装)
PyTorch2.10.0容器(预装)
torch_npu2.10.0.post2容器(预装)
PyTorch lightning2.6.6容器(预装)
torchmetrics/biopython/omegaconf/pandas等见 pyproject.tomlpip

注意:PyTorch 与 torch_npu 版本必须严格一致,且与CANN配套。昇腾容器镜像中预装的组合已经过配套调优,不要再用pip单独安装torch/torch_npu/torchvision,避免版本遮蔽导致 torchvision 导入失败等问题。

2.3 镜像准备

申请容器,基础镜像使用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(默认训练配置使用)
FireProthttps://doi.org/10.5281/zenodo.8169288FireProtDB 数据 + 单体 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/"

五、运行训练任务

5.1 启动脚本

训练入口为 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.py

5.2 托管环境验证

5.2.1 脚本目录

ThermoMPNN/
 ├──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 解析缓存(自动创建)

5.2.2 运行结果

训练启动后,通过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                                         # 训练日志
  • 每 epoch 打印 val_ddG_spearman / val_ddG_mse 等指标;
  • checkpoint 按 val_ddG_spearman 最大化自动保存最优。

5.2.3 loss曲线

ThermoMPNN_loss_curve


六、业务数据推理评测

6.1 一键推理脚本样例

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)。

6.2 推理结果

输出 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.0408760.0004
S0C-0.301111-0.3005810.0005
S0D0.0100480.0090910.0010
S0E-0.051409-0.0518910.0005
S0F0.0583490.0582500.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.pyTrainer 的 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(新增)训练数据下载工具交付配套