AIMNet2-rxn 是一个专门针对有机化学反应建模的神经网络势函数模型,由 Isayev 实验室开发。该模型在 ωB97M-V/def2-TZVPP 理论水平上对约 470 万反应相关几何构型进行训练,能够准确描述化学反应中的键断裂与形成、过渡态以及最小能量路径。本仓库提供了在华为昇腾 Ascend910 NPU 上部署和运行 AIMNet2-rxn 模型的完整方案。
| 组件 | 版本 |
|---|---|
| 操作系统 | openEuler (aarch64) |
| NPU 硬件 | Ascend910 (显存 64GB) |
| CANN | 8.5.1 |
| Python | 3.11.14 |
| PyTorch | 2.9.0 |
| torch_npu | 2.9.0.post1 |
| aimnet | 0.2.0 |
| vLLM-Ascend | 0.18.0 |
pip install "aimnet[hf]>=0.2.0" fastapi uvicorn numpyexport HF_ENDPOINT=https://hf-mirror.com
huggingface-cli download isayevlab/aimnet2-rxn --local-dir ./aimnet2-rxn模型包含 4 个 ensemble 成员权重文件(每个约 8.4 MB)和配置文件。
python inference.py --port 18080 --device cpu服务默认监听 0.0.0.0:18080,提供 RESTful API 接口。
curl http://localhost:18080/health返回示例:
{"status":"running","model_loaded":true,"device":"cpu","model_path":"/tmp/aimnet2-rxn","uptime":2.41}curl -X POST http://localhost:18080/inference \
-H "Content-Type: application/json" \
-d '{
"coordinates": [[0,0,0],[1,0,0],[0,1,0],[0,0,1],[-1,0,0]],
"atomic_numbers": [6,1,1,1,1],
"charge": 0.0,
"forces": true
}'输出示例:
{
"energy": 2.5629,
"forces": [[2.86e-06,-0.0140,-0.0140],[3.6918,-1.6592,-1.6592],[6.48e-07,4.3371,-1.0047],[8.87e-07,-1.0047,4.3371],[-3.6918,-1.6592,-1.6592]],
"charges": [-0.5302,0.1432,0.1219,0.1219,0.1432],
"inference_time_ms": 84.6
}curl -X POST http://localhost:18080/batch_inference \
-H "Content-Type: application/json" \
-d '{"molecules": [{"coordinates": [[0,0,0],[1,0,0],[0,1,0],[0,0,1],[-1,0,0]], "atomic_numbers": [6,1,1,1,1], "charge": 0.0}]}'由于 AIMNet2 的邻居列表计算依赖 warp-lang 库,而该库当前不支持 Ascend NPU 设备,本仓库采用混合设备推理策略:
warp 的 nvalchemiops 库)import torch
import torch_npu
from aimnet.calculators import AIMNet2Calculator
# 在 CPU 上加载模型和邻居列表计算
calc = AIMNet2Calculator("isayevlab/aimnet2-rxn", device=torch.device("cpu"))
# 将模型权重迁移到 NPU
calc.model = calc.model.npu().to(torch.float32)
# 准备输入数据(CPU 执行邻居列表计算)
data = calc.prepare_input({"coord": coords, "numbers": numbers, "charge": 0.0})
# 将所有张量迁移到 NPU
def move_to_npu(d):
if isinstance(d, torch.Tensor):
return d.to("npu:0")
elif isinstance(d, dict):
return {k: move_to_npu(v) for k, v in d.items()}
...
data = move_to_npu(data)
results = calc.model(data) # NPU 执行神经网络前向Ascend910 不支持 float64 精度,计算过程中会自动降级为 float32。经实测对比,NPU 推理结果与 CPU 结果在能量上的偏差约为 0.2%,在化学精度可接受范围内。
| 设备 | CH4 能量 (eV) | 相对偏差 |
|---|---|---|
| CPU | 5.6669 | - |
| NPU | 5.6785 | 0.20% |
| 分子 | 原子数 | 推理耗时 (CPU) | 输出 |
|---|---|---|---|
| 甲烷 CH4 | 5 | 84.6 ms | 能量、受力、电荷 |
| 乙醇 C2H6O | 9 | 20.4 ms | 能量、受力、电荷 |
注:推理耗时主要受邻居列表计算影响(CPU 执行),神经网络前向传播在 NPU 上执行时间极短。
模型在 ωB97M-V/def2-TZVPP 参考水平上训练,官方测试指标:
NPU 推理精度对比:
| 指标 | CPU 结果 | NPU 结果 | 偏差 |
|---|---|---|---|
| CH4 能量 | 5.6669 eV | 5.6785 eV | 0.20% |
| CH4 电荷 (C) | -0.5302 | -0.5313 | 0.21% |
| CH4 电荷 (H) | 0.1432 | 0.1437 | 0.35% |
warp-lang 不支持 NPU 设备,邻居列表计算需在 CPU 上执行,带来一定的数据搬运开销compile_model=False--port 参数指定其他可用端口torch.compile 与 GELU 激活函数的双反向传播存在兼容性问题,训练/推理时建议禁用