2
gcw_BOxvkDDf/aimnet2-rxn
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

AIMNet2-rxn — Ascend NPU 适配

isayevlab/aimnet2-rxn 有机反应通用神经网络原子间势(AIMNet2,4 成员集成),已适配华为昇腾 NPU(torch_npu)。

  • 支持元素:H(1)、C(6)、N(7)、O(8),仅中性体系(net charge = 0)。
  • 输出:能量 energy(eV,逐元素偏移电子能量标度,仅用于几何间差值)、力 forces(eV/Å)、原子电荷 charges(e)、分子偶极矩 dipole(e·Å)、分子四极矩 quadrupole(e·Å²)。

环境

  • torch 2.9.0 + torch_npu 2.9.0.post1,Ascend910。
  • 安装:pip install "aimnet[hf]" -i https://repo.huaweicloud.com/repository/pypi/simple/
  • 权重:python3 /opt/atomgit/adapt/_shared/download_weights.py isayevlab/aimnet2-rxn(本地缓存 ./model_weights/isayevlab/aimnet2-rxn/)。

NPU 适配说明

对安装后的 aimnet 0.2.0 包源码做了 3 处补丁(Ascend 兼容,均为最小改动):

  1. aimnet/calculators/calculator.py — AdaptiveNeighborList.__call__:nvalchemiops 邻接表后端基于 NVIDIA Warp(仅支持 CPU/GPU 设备),检测到 NPU 张量时在 CPU 上计算邻接表再搬回 npu:0(局部 CPU fallback)。
  2. aimnet/calculators/calculator.py — _apply_family_defaults:尊重 config.json 显式 needs_dispersion: false(README 明确 AIMNet2-rxn 不叠加 D3 校正,避免双重计数),不再为 family=="rxn" 强制外挂 DFTD3。
  3. aimnet/ops.py — nse():data["mol_sizes"][-1] ±= 1 就地修改会在 torch_npu 上触发 autograd 版本号校验失败(该张量被 repeat_interleave 保存用于反向),改为在副本上修改。

另外在 inference.py 中:

  • get_device() try-except,优先 npu:0,异常回退 cpu。
  • 关闭 torch.compile(compile_model=False,GELU 双反向会挂起且 NPU 不需要)。
  • torch._dynamo.config.suppress_errors = True(CPU-only torch 下 nvalchemiops 的 @torch.compile 辅助函数触发 Inductor KeyError: 'cpu',静默降级为 eager)。

工程代码本身不含任何 CUDA 设备调用(grep -rin cuda 无结果)。

用法

# CLI 推理(默认使用本地权重缓存,多帧 xyz 逐帧输出)
ASCEND_RT_VISIBLE_DEVICES=1 python inference.py --input sample.xyz

# FastAPI 服务
ASCEND_RT_VISIBLE_DEVICES=1 python inference.py --serve --port 8000
# GET  /        -> 网页(.xyz 文件上传 / JSON 文本框)
# POST /predict -> 上传 .xyz 文件或 JSON body,返回推理 JSON

sample.xyz 内含 3 帧中性小分子:水(H2O)、甲烷(CH4)、氨(NH3),覆盖 H/C/N/O。