ACNN(Atomic Convolutional Networks,原子卷积网络)用于蛋白-配体结合亲和力预测,核心思想是对配体(ligand)、蛋白(protein)及二者复合体(complex)三套原子图分别做基于最近邻距离的径向基(RBF)卷积,聚合原子周围化学环境后预测各原子能量,再由 ligand / protein 能量相对 complex 能量的差得到结合亲和力。本仓库针对 ACNN 完成昇腾 NPU 适配,并验证其在 NPU 上的端到端训练、收敛与推理性能。
ACNN(dgllife/model/model_zoo/acnn.py)结构与数据处理流程如下:
| 阶段 | 模块 | 说明 |
|---|---|---|
| 图构造 | ACNN_graph_construction_and_featurization | 由配体/蛋白分子 + 3D 坐标,经 k_nearest_neighbors 构造 ligand / protein 内部最近邻图 + 复合体图,边存原子间欧氏距离 distance |
| 原子卷积 | AtomicConv(dgl.nn.pytorch.AtomicConv) | 对 ligand / protein / complex 三张图分别做基于 RBF 径向基函数的邻居聚合(支持 cutoffs / means / scaling 参数网格) |
| 能量预测 | ACNNPredictor | 拼接三个卷积输出,经 MLP(默认 [32,32,16])逐原子预测能量 |
| 亲和力 | 求和相减 | pred = complex_energy - (ligand_energy + protein_energy) |
输入(ACNN.forward(graph))为一个 DGLHeteroGraph,含两类节点与三类边:
| 节点 / 边类型 | 含义 | 特征 |
|---|---|---|
节点 ligand_atom / protein_atom | 配体 / 蛋白原子 | atomic_number((V,1)) |
边 ('ligand_atom','ligand','ligand_atom') | 配体内部最近邻连接 | distance((E,1)) |
边 ('protein_atom','protein','protein_atom') | 蛋白内部最近邻连接 | distance |
边 ('ligand_atom','complex',...) / ('protein_atom','complex',...) | 复合体内连接(含对接间) | distance |
| 项目 | 规格 |
|---|---|
| 任务类型 | 蛋白-配体结合亲和力回归 |
| 框架依赖 | dgllife + dgl-ascend + torch_npu + rdkit |
| 图结构特点 | 异构图(ligand/protein 两套节点 + ligand/protein/complex 三类边),边为欧氏距离 |
默认 hidden_sizes | [32, 32, 16] |
默认 radial | [[12.0], [0.0, 4.0, 8.0], [4.0]](configure.py) |
默认 dropouts | [0., 0., 0.] |
默认 neighbor_cutoff | 12.0 Å |
默认 max_num_neighbors | 12 |
num_tasks | 1 |
| 参数量 | 约 1.8K(轻量) |
| 运行设备 | Atlas 800T A2 / Ascend 910B3(单卡 NPU) |
本章节为快速部署指引,详细参数见 §3 准备运行环境 与 §4 运行指导。
| 路径 | 用途 |
|---|---|
pip install dgl-ascend | dgl-ascend 预编译包(阿里云源,见 §4.1),无需手工编译 |
main.py(当前目录) | 即 dgl-lifesci 源仓 examples/binding_affinity_prediction/main.py 的 NPU 适配版(-m ACNN 运行,见 §5.1);改动仅设备判断,其余逐行一致 |
acnn_smoke.py / acnn_train.py(当前目录) | 方案 C 自验证脚本(基于标准 ACNN + 标准图构造的 forward / 训练 / 推理性能) |
pip 安装 dgl-ascend(见 §4.1)| 组件 | 版本 |
|---|---|
| Python | 3.11.17 |
| PyTorch | 2.8.0+cpu |
| torch_npu | 2.8.0.post2 |
| CANN | 9.2.0 |
| dgl-ascend | 0.0.3(pip install dgl-ascend 安装) |
| dgllife | 最新(含 ACNN/ACNNPredictor,见 §4.2) |
| rdkit | 最新(分子图 / 坐标构造依赖) |
| numpy | 最新(k_nearest_neighbors 逻辑依赖) |
| 设备型号 | NPU 配置 |
|---|---|
| Atlas 800T A2 / Ascend 910B3 | 单卡 |
| 镜像环境 | 镜像地址 |
|---|---|
| 公网 | swr.cn-south-1.myhuaweicloud.com/ascendhub/cann:9.2.0-910b-openeuler24.03-py3.11 |
注意:容器内 PyTorch 须为 CPU 版(如
2.8.0+cpu),NPU 算力由torch_npu提供,不要替换为 GPU 版 PyTorch。
IMAGE_ID=swr.cn-south-1.myhuaweicloud.com/ascendhub/cann:9.2.0-910b-openeuler24.03-py3.11
CONTAINER_NAME=acnn
docker run -u root --privileged \
--name ${CONTAINER_NAME} \
--device /dev/davinci0 \
--device /dev/davinci_manager \
--device /dev/devmm_svm \
--device /dev/hisi_hdc \
-v /usr/local/dcmi:/usr/local/dcmi \
-v /usr/local/bin/npu-smi:/usr/local/bin/npu-smi \
-v /usr/local/Ascend/driver:/usr/local/Ascend/driver \
-v /etc/ascend_install.info:/etc/ascend_install.info \
-itd ${IMAGE_ID} /bin/bashpip install 方式)无需编译,直接从阿里云 PyPI 安装预编译的 dgl-ascend:
# 安装 dgl-ascend 预编译包(阿里云源)
pip install dgl-ascend==0.0.3 -i https://mirrors.aliyun.com/pypi/simple/
# 该包依赖 torch==2.8.0 与 torch_npu==2.8.0.post2,需保持版本匹配
pip install torch==2.8.0 -i https://download.pytorch.org/whl/cpu
pip install torch-npu==2.8.0.post2 -i https://mirrors.aliyun.com/pypi/simple/版本匹配提醒:
dgl-ascend0.0.3 的METADATA明确Requires-Dist: torch==2.8.0, torch_npu==2.8.0.post2。容器内 torch / torch_npu 必须同步匹配,否则将因 ABI 不兼容而运行报错。
如需在 torch 2.9.x 等非 2.8.0 环境使用,可走源码编译:
cd /home
git clone --recursive https://github.com/BUPT-GAMMA/dgl-ascend.git
yum install gcc-gfortran
ln -s /usr/lib64/libgfortran.so.5 /usr/lib64/libgfortran.so
ln -s /usr/lib64/libgomp.so.1 /usr/lib64/libgomp.so
pip install pydantic transformers -i https://mirrors.aliyun.com/pypi/simple/
cd dgl-ascend/script/ && bash build_dgl_ascend.sh # SOC=Ascend910B3 BUILD_EXIT=0
cd ../python/ && pip install -e . -i https://mirrors.aliyun.com/pypi/simple/本文档默认采用 4.1 的 pip install 方式。
安装 dgllife(含 ACNN 及 ACNNPredictor):
pip install dgllife -i https://mirrors.aliyun.com/pypi/simple/
# 或从本仓库源码安装
pip install -e . -i https://mirrors.aliyun.com/pypi/simple/运行前需在容器内设置 CANN 环境与库路径:
source /usr/local/Ascend/ascend-toolkit/set_env.sh export TORCH_LIBS=<python>/site-packages/torch.libs export LD_LIBRARY_PATH=$TORCH_LIBS:<python>/site-packages/torch/lib:<python>/site-packages/torch_npu/lib:$LD_LIBRARY_PATH
本验证复用 ACNN 标准模型 + 标准 ACNN_graph_construction_and_featurization,用合成配体/蛋白分子 + 坐标直接构造 ACNN 异构图(ligand/protein/complex 三元图),在 NPU 上跑通图构造、forward、训练(backward)、推理。不依赖真实 PDBBind 大数据下载(深入见 §5.2)。
脚本为 acnn_smoke.py(图构造 + forward 冒烟)与 acnn_train.py(训练 + 推理性能)。
运行所有脚本前,需先在容器内设置环境变量并激活 CANN(缺一不可):
# 1) 激活 CANN 环境
source /usr/local/Ascend/ascend-toolkit/set_env.sh
# 2) 把 torch 预编译依赖目录与 torch_npu 库加入 LD_LIBRARY_PATH
TL=<python>/site-packages/torch.libs
export LD_LIBRARY_PATH=$TL:<python>/site-packages/torch/lib:<python>/site-packages/torch_npu/lib:$LD_LIBRARY_PATH
# 3) 使能 DGL 在 NPU 上的内部 stream 调度桥
export DGL_ASCEND_STREAM_BRIDGE=1
# 4) 绑定空闲卡(多卡容器内避免用显存被残留占满的卡,见 §5.2)
export ASCEND_RT_VISIBLE_DEVICES=<空闲卡逻辑ID>随后执行:
cd <本仓库目录>
python3 acnn_smoke.py # 方案C 图构造 + forward 冒烟
python3 acnn_train.py # 方案C 训练 + 推理性能
# 标准例程(本仓库 main.py = binding_affinity_prediction/main.py 的 NPU 适配版;需真实 PDBBind 数据)
# 需与 utils.py / configure.py 同目录(源仓 examples/binding_affinity_prediction/ 下)
python3 main.py -m ACNN -d PDBBind_core_pocket_random # 真实基准训练/评估脚本的依赖、来源与前置小结:
| 脚本 | 来源/适配属性 | 依赖包 | 运行前置 |
|---|---|---|---|
main.py(-m ACNN) | 本仓库即 examples/binding_affinity_prediction/main.py 的 NPU 适配版(原始源仓仅 cuda→cpu,无 NPU 分支,直接跑会落 CPU;本版已加 get_device()/_has_npu() 回退 npu→cuda→cpu,见 §5.1)。其余训练/评估逻辑与源仓逐行一致 | dgl-ascend、dgllife、torch/torch_npu、rdkit、numpy | CANN 环境 + LD_LIBRARY_PATH + DGL_ASCEND_STREAM_BRIDGE=1;需真实 PDBBind 数据 |
acnn_smoke.py | 新增脚本(非源仓自带),内置 torch_npu.npu.set_device(0) NPU 绑定 | dgl-ascend、dgllife、torch/torch_npu、rdkit、numpy | 同上;分子图由脚本内 SMILES + 合成坐标自动构造,无需下载数据 |
acnn_train.py | 新增脚本,内置 NPU 绑定 + 训练/推理性能 | dgl-ascend、dgllife、torch/torch_npu | 同上 |
重要——脚本来源说明(方案C):
acnn_smoke.py/acnn_train.py为为 NPU 验证新增的脚本(源仓binding_affinity_prediction/目录中不存在),需从本仓库/交付物拷贝后使用。它们直接复用标准ACNN与ACNN_graph_construction_and_featurization,用真实小分子 SMILES + 合成坐标自构造图数据,可在不下载真实 PDBBind 数据的情况下验证 NPU 全链路。
| 步骤 | 说明 |
|---|---|
| 1. dgl-ascend 安装 | pip install dgl-ascend==0.0.3 -i https://mirrors.aliyun.com/pypi/simple/(预编译包,含 Ascend 算子后端) |
| 2. torch 版本匹配 | 同步安装 torch==2.8.0 与 torch_npu==2.8.0.post2(dgl-ascend 依赖约束) |
| 3. 库路径 | 预编译 dgl 依赖 libgfortran.so.5 位于 torch.libs 下,加入 LD_LIBRARY_PATH |
| 4. dgllife 安装 | 安装含 ACNN/ACNNPredictor 的 dgllife |
| 5. NPU 设备绑定 | 标准例程需按 MGCN 方式加入 get_device() 绑定 npu:0;acnn_*.py 内置 torch_npu.npu.set_device(0) |
| 6. DGL stream 桥 | os.environ["DGL_ASCEND_STREAM_BRIDGE"]="1" 使能 NPU 上的 dgl 内部调度 |
| 7. 显存规避 | 多卡容器内避免使用显存被残留占满的卡;用 ASCEND_RT_VISIBLE_DEVICES 指定空闲卡(见 §5.2 显存注意事项) |
dgl.nn.pytorch.AtomicConv 与异构图操作:edge_type_subgraph、to_homogeneous、k_nearest_neighbors 等均在 dgl-ascend 上可用;AtomicConv 的 RBF 聚合由 scatter/segment 类算子覆盖,NPU 可跑,无需额外算子开发。ACNN_graph_construction_and_featurization 产出含 ligand/protein/complex 三类边的 heterograph,节点 atomic_number、边 distance 均被 AtomicConv 读取;验证中 ligand 3 原子 / protein 8 原子,ligand 6 边 / protein 56 边,图构造约 0.3–0.4s/对。ACNN.findOne 单前向在 NPU 需对三张原子图分别卷积 + 剖分复合图(to_homogeneous 后按 _TYPE 区分 ligand/protein 原子),故单对时延偏高(§6.3 推理约 63ms/对)。ASCEND_RT_VISIBLE_DEVICES=<空闲卡> 绑定空闲卡。dgllife/model/model_zoo/acnn.py(ACNN、ACNNPredictor)dgl.nn.pytorch.AtomicConv(dgl 内置)dgllife/utils/complex_to_graph.py(ACNN_graph_construction_and_featurization、k_nearest_neighbors)dgllife/data/pdbbind.py(PDBBind,真实亲和力数据)examples/binding_affinity_prediction/(main.py、configure.py、utils.py)pip install dgl-ascend)ACNN(默认 hidden_sizes=[32,32,16],参数量约 1.8K,num_tasks=1)图构造:ACNN_graph_construction_and_featurization 在 NPU 兼容性摘要:
graph types: ['complex','complex','ligand','complex','complex','protein']
num ligand_atom nodes: 3, protein_atom nodes: 8
ligand edges: 6, protein edges: 56
node atomic_number (ligand) shape: (3,1)前向:pred=[[-0.00021]](shape (1,1)),输出合理。
训练收敛(4 对数据,MSELoss + Adam,8 epoch):
| 指标 | 数值 |
|---|---|
| 迭代数 | 32 |
| 总耗时 | 3.51 s |
| 平均 loss | 1.855 |
说明:方案 C 使用合成分子 + 伪造标签演示 ACNN 在 NPU 可运行/可训练;这不等于 PDBBind 基准 R²/MAE 精度。如需真实基准,需接入
PDBBind数据 + 标准例程main.py -m ACNN(见 §7 Q1)。
ACNN(默认配置),单对(配体 ~3 原子 + 蛋白 ~8 原子):
| 阶段 | 时延 |
|---|---|
| 训练迭代(fwd+bwd+step) | 109.8 ms/iter |
| 推理(forward-only) | 62.6 ms/对 |
测量方式:训练循环内
torch.npu.synchronize()保证实测 NPU 执行;推理跑多轮取稳。时延主要来自对 ligand/protein/complex 三张图分别做AtomicConv卷积 + 复合体的to_homogeneous剖分;参数量仅 1.8K,时延以图算子调度/剖分开销为主而非算力。
ACNN 图构造、前向、反向、优化、推理全流程在 Ascend 910B3 上稳定复现。AtomicConv(dgl.nn.pytorch)与异构图操作(edge_type_subgraph、to_homogeneous、k_nearest_neighbors)在 dgl-ascend 均受支持。Q1:为何不用标准 PDBBind 数据集?
A:PDBBind 需下载真实蛋白 PDB + 配体结构(几百 MB ~ GB 级),并结合口袋解析(load_binding_pocket)较重;方案 C 用合成坐标直接构造图验证模型在 NPU 全链路可用(ACNN_graph_construction_and_featurization 对 synthetic 分子/坐标一视同仁)。如需真实基准 R²/MAE,可接入 PDBBind + 标准例程 main.py -m ACNN。
Q2:ACNN 为什么比同样量级的图模型慢?
A:ACNN.forward 需对 ligand / protein / complex 三套原子图分别跑一次 AtomicConv,且复合图要 edge_type_subgraph + to_homogeneous 剖分并回查 _TYPE 切分 ligand/protein 原子,图算子调度/剖分开销占比高;这是 ACNN 结构固有特性,非 NPU 适配缺陷。
Q3:AtomicConv 在 NPU 是否需要额外开发?
A:不需要。dgl.nn.pytorch.AtomicConv 是 dgl 内置模块,其 RBF 邻居聚合在 dgl-ascend 上可用,NPU 直接调用即可。
Q4:pip install dgl-ascend 后 import dgl 报 libgfortran.so.5: cannot open shared object file?
A:预编译 dgl 依赖 libgfortran.so.5,位于 torch.libs 下。将 torch.libs、torch/lib、torch_npu/lib 加入 LD_LIBRARY_PATH 即可(见 §4.4)。
Q5:运行时报 accidental runtime / EZ3002(算子不支持)/ ABI 不兼容?
A:先确认版本匹配 torch==2.8.0 + torch_npu==2.8.0.post2 + dgl-ascend 0.0.3(三者 ABI 强耦合);再 source set_env.sh 并确保 LD_LIBRARY_PATH 含 torch.libs / torch_npu/lib。