xnn.common.models.dispersion_fast.cpu#
D3 and D4 on CPUs: optional C++ and OpenMP kernels (off by default, built on first use).
simple-dftd3 and dftd4 evaluate the dispersion energy in loops over the atoms and their neighbors,
energy and gradient in one pass, without tensors per pair or triangle. The reference
(DFTD3, DFTD4) evaluates the same
formulas as tensor operations; on a CPU its many operations over gathered copies take longer than
those loops. These kernels do what the Fortran codes do:
the edges are sorted by center, then neighbor (a counting sort and a sort per center);
D3: one loop over the centers forms the coordination numbers, the Gaussian reference weights and their derivatives, and with a three-body term the pair C6 and their derivatives with respect to both coordination numbers fill dense
(N, N)arrays inside each structure;one loop over the centers sums the two-body energies of their edges (D3: C6 from the weights; D4: from the dynamic polarizabilities), and one sums the three-body energies of their neighbor pairs: a triangle of three distinct atoms once, from its smallest-index corner, with a third of its energy to each atom (the reference’s bookkeeping), and a triangle with a repeated atom (periodic images) from every corner, a third each;
the gradient with respect to the edge vectors adds into the center’s own edges (no two threads write the same memory); the derivatives through the C6 collect per thread on the coordination numbers. D3 carries them to the edges in a last loop over the centers; D4 returns them, with the derivatives in the dynamic polarizabilities, to the charges, weights and coordination numbers that the reference code forms.
The forward pass forms the gradient for unit weights on every atom’s energy, which is what a sum
of energies (forces, stress) asks for; another weighting evaluates again with those weights. The
gradient is not differentiable again, so the kernels serve evaluation. They compute in float64 for
float32 inputs as well (scalar code is no slower in it, and the sums of a periodic three-body term
cancel to a few digits). The source is compiled with torch.utils.cpp_extension.load_inline()
on first use (xnn.common.models.cpu_extension).
Module Attributes
it reads the pair C6 and their derivatives from dense |
Functions
Whether the C++ kernels build here (checked once per process). |
|
|
D3 of a batched graph on the C++ kernels with its forces, without autograd: the energies per atom and per structure |
|
D4 of a batch (molecules and cells of the dense EEQ regime) on the C++ kernels with its forces and stress: the energies per atom and per structure |
|
The two-body and three-body energies per atom (hartree) and the dynamic polarizabilities |
|
The energies per atom (total, two-body, three-body; eV) and the features |
Classes
|
Energies per atom (total, two-body, three-body; eV) and the features (coordination number, homoatomic C6) from the edge vectors (Angstrom); the gradient is not differentiable again. |
|
D4's two-body and three-body energies per atom (hartree) and dynamic polarizabilities |