ALISY/aimnet2-rxn-npu
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

aimnet2-rxn-npu

isayevlab/aimnet2-rxn 神经网络原子间势(NNP)的 华为昇腾 NPU 适配版。

AIMNet2-rxn 面向广义有机化学反应建模——沿反应坐标的能量 / 原子力 / 原子电荷 / 静电矩预测(H/C/N/O 四种元素,元素号 [1,6,7,8])。本仓库将其迁移到华为昇腾 NPU(torch_npu, device='npu:0'),提供命令行与 FastAPI 双模式推理,并附带 4 个集成成员平均(ensemble average)单点能推理。

  • 原始仓库:isayevlab/aimnet2-rxn(weights from https://hf-mirror.com/isayevlab/aimnet2-rxn)
  • 权重文件:config.json + ensemble_0..3.safetensors(4 个集成成员),本地缓存于 model_weights/isayevlab-aimnet2-rxn/
  • 官方安装:pip install aimnet[hf],官方接口:AIMNet2Calculator

昇腾 NPU 适配说明

  1. 全域无 CUDA:工程内不残留任何 torch.cuda.* / .cuda() 调用;模型与输入张量全部迁移到 npu:0(import torch_npu 必须,并带 NPU 初始化异常捕获)。
  2. 算子逐 tensor 迁移:
    • nvalchemiops 的 neighbor_list 为 CUDA/Warp 算子,NPU/CPU 均不可用 → 用纯 PyTorch 等价实现(_npu_neighbor_list)替换,逐 tensor 计算邻居矩阵(同 batch、不含自对、dist²<cutoff²)后迁回 npu:0。
    • aimnet 能量路径使用 float64 scatter_add,昇腾 NPU 不支持该算子(vector core exception / CheckDtypeValid failed)→ 整条能量链路降为 float32 跑通(SR/外部库仑、原子电荷累积均为 float32),并修复 nse 中对 mol_sizes 的 inplace 修改以避免 autograd 版本冲突。
    • 官方 needs_dispersion=false(VV10 已内嵌于训练参考),不附加 D3,避免重复计入色散。
  3. 推理已在 npu:0(Ascend910)实测跑通 CO2 / H2,能量、力、电荷结果与 CPU 一致(float32 精度)。

快速开始

# 1) 依赖(建议清华镜像)
export PIP_INDEX_URL=https://pypi.tuna.tsinghua.edu.cn/simple
pip install "aimnet[hf]" ase fastapi uvicorn
# torch / torch_npu 需按昇腾 CANN 环境预装(torch 2.9.0+cpu + torch_npu + CANN 8.5.1)

# 2) 命令行单次推理(内置分子 co2 / h2,或任意 ASE xyz 文件)
python inference.py --xyz co2
python inference.py --xyz h2
python inference.py --xyz co2.xyz

# 3) FastAPI 服务
uvicorn inference:app --host 0.0.0.0 --port 8000

# 4) 测试接口
curl -s -X POST -H 'Content-Type: application/json' \
  -d '{"coord":[[0.0,0.0,0.0],[1.16,0.0,0.0],[-1.16,0.0,0.0]],"numbers":[6,8,8],"charge":0}' \
  http://127.0.0.1:8000/predict

若 8000 端口被占用(多 worker 并行时),可换端口:uvicorn inference:app --port 8001。

输入 / 输出

命令行:python inference.py --xyz <结构文件|co2|h2> 单次推理,输出 JSON:

{
  "energy": -1.7468,
  "energy_ensemble": [-1.7419, -1.7491, -1.7478, -1.7485],
  "forces": [[0.0,0.0,0.0],[-0.2141,0.0,0.0],[0.2141,0.0,0.0]],
  "charges": [0.6585, -0.3292, -0.3292]
}

FastAPI:POST /predict 接收 JSON {"coord":[[...]], "numbers":[6,8], "charge":0}(charge 可省略,默认 0),返回与命令行相同的 energy / forces / charges。

能量约定

AIMNet2-rxn 输出为学习到的每元素平移电子能量(eV),仅供同模型不同几何间的差值比较(势垒、反应能、IRC 曲线等),不是绝对电子能量或原子化能。请勿跨模型族比较能量。模型仅支持净电荷中性(charge=0)的闭壳层 H/C/N/O 体系。

文件结构

aimnet2-rxn-npu/
├── inference.py            # 双模式推理(CLI --xyz + FastAPI /predict),NPU 适配补丁内嵌
├── requirements.txt        # 依赖清单
├── README.md
├── co2.xyz / h2.xyz        # 示例结构文件
├── assets/                 # 部署 / 推理截图
└── model_weights/          # 权重本地缓存(.gitignore 排除,不上传)

验证记录

  • agent_workflow.png — NPU 环境部署 / 依赖安装 / 任务启动总日志
  • npu_device_call.png — python inference.py --xyz co2 命令行推理成功截图
  • model_result.png — curl -X POST /predict FastAPI 返回预测结果截图