机器学习势:MACE、DeePMD 与 Calorine 的推理速度
区分原子数、帧数和批量大小,用 MACE 的硅超胞示例测量预热后的力评价速度。
阅读本篇目录
先看表头的单位。MACE 统计每秒完成力计算的原子数;DeePMD-kit 和 Calorine 统计帧/秒。每张表都在同一模型与输入定义内比较。三组的 CPU 均用 8 个线程,GPU 用一张 V100 16 GB 和两个 CPU 线程。
MACE 的 Si512 相对 CPU 达到 10.71 倍,Si64 为 1.31 倍。Calorine 的 8 帧任务在 CPU 上更快,到了 256 帧,GPU 批预测达到 CPU 逐帧路径的 14.41 倍。这里同时改变了执行设备和预测接口。DeePMD 的 repeat1/8/32 指同一批数据重复的倍数,表中列的是 PyTorch 模型的推理吞吐。
用 MACE 复做 Si64 的五次力评价#
在登录节点建立新的工作目录,运行 yskit,选择 MACE 0.3.16 → V100 → run,将对应的 Slurm 脚本复制到当前目录。
mkdir -p ~/benchmarks/mace-si64
cd ~/benchmarks/mace-si64
yskit
cat sub_mace_0.3.16_v100_run.sh
cat > run_mace.py <<'END_INPUT'
import time
import torch
from ase.build import bulk
from mace.calculators import MACECalculator
atoms = bulk('Si', 'diamond', a=5.43, cubic=True).repeat((2,2,2))
device = 'cuda'
atoms.calc = MACECalculator(model_paths='/public/software/mace/models/mace-mpa-0-medium.model',
device=device, default_dtype='float32')
atoms.get_forces()
if device == 'cuda':
torch.cuda.synchronize()
start = time.perf_counter()
for i in range(5):
atoms.positions[0,0] += 0.0001
forces = atoms.get_forces()
if device == 'cuda':
torch.cuda.synchronize()
seconds = time.perf_counter() - start
print('atoms/s', len(atoms)*5/seconds, 'seconds', seconds)
END_INPUT
job=$(sbatch --parsable sub_mace_0.3.16_v100_run.sh run_mace.py)
squeue -j "$job"
待作业离开队列后再读取输出。
tail -12 "$job.out"
先移动一个原子再求力会触发重新计算;预热和模型加载在计时窗口之外。把 repeat 改为 (3,3,3) 或 (4,4,4) 分别得到 216 或 512 个原子。CPU 对照选择 8173M 模板,设 --cpus-per-task=8,在程序内把 device 改为 cpu。DeePMD 提交与操作 · Calorine GPU 批量预测
MACE 不同大小硅超胞的吞吐#
| 输入 | 基线 | 对照 | 基线 · 原子/秒 | 对照 · 原子/秒 | 相对速度 |
|---|---|---|---|---|---|
| si64 | 8173M | V100 16 GB | 187.32 | 244.73 | 1.31× |
| si216 | 8173M | V100 16 GB | 180.36 | 813.68 | 4.51× |
| si512 | 8173M | V100 16 GB | 166.36 | 1782.5 | 10.71× |

DeePMD-kit 同模型的批量推理#
| 输入 | 基线 | 对照 | 基线 · 帧/秒 | 对照 · 帧/秒 | 相对速度 |
|---|---|---|---|---|---|
| repeat1 | 8173M | V100 16 GB | 5.8428 | 6.7167 | 1.15× |
| repeat8 | 8173M | V100 16 GB | 47.738 | 54.326 | 1.14× |
| repeat32 | 8173M | V100 16 GB | 180.49 | 200.99 | 1.11× |

Calorine 不同帧数的预测吞吐#
| 输入 | 基线 | 对照 | 基线 · 帧/秒 | 对照 · 帧/秒 | 相对速度 |
|---|---|---|---|---|---|
| frames8 | 8173M | V100 16 GB | 17.039 | 12.474 | 0.73× |
| frames64 | 8173M | V100 16 GB | 17.531 | 88.311 | 5.04× |
| frames256 | 8173M | V100 16 GB | 17.588 | 253.41 | 14.41× |

读取数据与画图#
配套文件 comparison.csv 保存本篇表格的完整精度。reference_median 和 comparison_median 对应基线与对照的中位数;speedup 使用同一指标计算。批工具中部分配置只做了一次。下面默认绘制 CSV 中第一组实验;要画其他组,将 family 换成对应的 experiment 值。
在有 Matplotlib 的 Python 环境中,将下列代码和 CSV 放在同一目录。也可以将自己的计时或吞吐按相同列名填入 CSV。 也可以直接在空目录建立下面的 CSV 并画图。
cat > comparison.csv <<'END_INPUT'
experiment,model,reference,comparison,metric,reference_median,comparison_median,speedup
mace inference,si64,8173M,v100,atoms_per_second,187.32341062799856,244.73343836897942,1.3065
mace inference,si216,8173M,v100,atoms_per_second,180.36289972321572,813.6755537844799,4.5113
mace inference,si512,8173M,v100,atoms_per_second,166.36455527435928,1782.45597139443,10.7142
deepmd-kit inference,repeat1,8173M,v100,frames_per_second,5.842778181457367,6.716702237680563,1.1496
deepmd-kit inference,repeat8,8173M,v100,frames_per_second,47.73837797381754,54.32630664070979,1.138
deepmd-kit inference,repeat32,8173M,v100,frames_per_second,180.49253086838803,200.98614456085556,1.1135
calorine inference,frames8,8173M,v100,frames_per_second,17.039113609638754,12.474136148885915,0.7321
calorine inference,frames64,8173M,v100,frames_per_second,17.531372788310982,88.31141433184497,5.0373
calorine inference,frames256,8173M,v100,frames_per_second,17.587961223660663,253.4092828675375,14.4081
END_INPUT
module load ase/3.29.0
cat > plot.py <<'END_INPUT'
import csv
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
with open('comparison.csv', encoding='utf-8') as stream:
data = list(csv.DictReader(stream))
family = data[0]['experiment']
rows = [r for r in data if r['experiment'] == family]
labels = [r['model'] + ' / ' + r['reference'] + ' to ' + r['comparison'] for r in rows]
ratios = [float(r['speedup']) for r in rows]
fig, ax = plt.subplots(figsize=(10, max(3, 1.5 + 0.5 * len(rows))))
ax.barh(range(len(rows)), ratios)
ax.set_yticks(range(len(rows)), labels=labels)
ax.invert_yaxis()
ax.axvline(1, color='gray', linestyle='--')
ax.set_xlabel('Speedup')
fig.savefig('comparison.png', dpi=160, bbox_inches='tight')
END_INPUT
python3 plot.py