"""Radial basis expansions shared across families: the Gaussian basis of SchNet and the
equivariant GNN edge embedding, the exponential-normal basis of the TorchMD-Net models
(ET, TensorNet) and BAMBOO, the Bessel basis of NequIP, MACE, DimeNet and the Equiformers, and
the smooth spherical Bessel basis of Kocer, Mason and Erturk."""
from __future__ import annotations
import math
import torch
from torch import Tensor, nn
from .cutoff import CosineCutoff
[docs]
class GaussianRBF(nn.Module):
"""Gaussian radial basis expansion (SchNet-style).
Expands scalar interatomic distances onto a set of ``n_rbf`` Gaussians
``e_k(r) = exp(-gamma (r - mu_k)^2)`` whose centers ``mu_k`` are fixed and
evenly spaced on ``[start, cutoff]``. Registered as a non-trainable buffer.
Parameters
----------
n_rbf : int, optional
Number of Gaussian basis functions (centers). Defaults to ``50``.
cutoff : float, optional
Upper bound of the center range, in the same units as the distances.
Defaults to ``5.0``.
gamma : float or None, optional
Width parameter of the Gaussians. ``None`` (default) sets the standard
deviation to the spacing between adjacent centers, i.e.
``gamma = 0.5 / spacing**2``. SchNet (Schuett et al., NIPS 2017) fixes
``gamma = 10`` per Angstrom^2 on a 0.1 Angstrom center grid.
start : float, optional
Position of the first center, by default ``0.0``.
endpoint : bool, optional
Whether the last center sits at ``cutoff`` (default) or the grid stops
one spacing short of it, ``mu_k = start + k (cutoff - start) / n_rbf``
(the AIMNet2 layout, whose Gaussians all keep some weight inside the
cutoff envelope).
Notes
-----
With ``gamma=None`` the width is the spacing between adjacent centers, or
``1.0`` when ``n_rbf == 1``.
"""
def __init__(self, n_rbf: int = 50, cutoff: float = 5.0,
gamma: float | None = None, start: float = 0.0,
endpoint: bool = True):
super().__init__()
if endpoint:
centers = torch.linspace(start, cutoff, n_rbf)
else:
centers = torch.linspace(start, cutoff, n_rbf + 1)[:n_rbf]
self.register_buffer("centers", centers)
self.width = (centers[1] - centers[0]).item() if n_rbf > 1 else 1.0
self.gamma = float(gamma) if gamma is not None \
else 0.5 / self.width ** 2
[docs]
def forward(self, r: Tensor) -> Tensor:
"""Expand distances onto the Gaussian basis.
Parameters
----------
r : Tensor
Interatomic distances of arbitrary shape ``(...)``.
Returns
-------
Tensor
The radial basis expansion of shape ``(..., n_rbf)``.
"""
diff = r[..., None] - self.centers
return torch.exp(-self.gamma * diff ** 2)
[docs]
class ExpNormalSmearing(nn.Module):
r"""Exponential-normal radial basis (TorchMD-Net ET / TensorNet, BAMBOO).
Expands a distance ``r`` onto ``n_rbf`` Gaussians placed in exponential
distance space, dense at short range and sparse near the cutoff, times a
cosine cutoff envelope::
phi_k(r) = cos_cutoff(r) *
exp(-beta_k * (exp(alpha * (cutoff_lower - r)) - mu_k) ** 2)
with ``alpha = 5 / (cutoff - cutoff_lower)``. The centers ``mu_k`` run
linearly from ``exp(cutoff_lower - cutoff)`` to ``1`` and every width
starts at ``(2 / n_rbf * (1 - exp(cutoff_lower - cutoff))) ** -2``
(Thoelke & De Fabritiis 2022, eq 1, the PhysNet convention).
Parameters
----------
n_rbf : int, optional
Number of basis functions, by default 32.
cutoff : float, optional
Upper cutoff in the units of the distances, by default 5.0.
cutoff_lower : float, optional
Lower cutoff, by default 0.0.
trainable : bool, optional
Learn the centers and widths (``True``, the default) or keep them as
buffers.
Attributes
----------
means : Tensor
The centers ``mu_k``, shape ``(n_rbf,)``.
betas : Tensor
The inverse widths ``beta_k``, shape ``(n_rbf,)``.
alpha : float
The exponential-space scale ``5 / (cutoff - cutoff_lower)``.
"""
def __init__(self, n_rbf: int = 32, cutoff: float = 5.0,
cutoff_lower: float = 0.0, trainable: bool = True):
super().__init__()
self.n_rbf = n_rbf
self.cutoff_lower = cutoff_lower
self.cutoff_upper = cutoff
self.trainable = trainable
self.cutoff_fn = CosineCutoff(cutoff)
self.alpha = 5.0 / (self.cutoff_upper - self.cutoff_lower)
if trainable:
self.means = nn.Parameter(torch.empty(n_rbf))
self.betas = nn.Parameter(torch.empty(n_rbf))
else:
self.register_buffer("means", torch.empty(n_rbf))
self.register_buffer("betas", torch.empty(n_rbf))
self.reset_parameters()
def _initial(self, dtype: torch.dtype) -> tuple[Tensor, Tensor]:
"""The default centers and widths, evaluated in ``dtype``."""
first_center = math.exp(self.cutoff_lower - self.cutoff_upper)
shared_width = (2.0 / self.n_rbf * (1.0 - first_center)) ** -2
return (torch.linspace(first_center, 1.0, self.n_rbf, dtype=dtype),
torch.full((self.n_rbf,), shared_width, dtype=dtype))
[docs]
def reset_parameters(self) -> None:
"""Reset the centers and widths to their default values in place."""
means, betas = self._initial(self.means.dtype)
with torch.no_grad():
self.means.copy_(means)
self.betas.copy_(betas)
[docs]
def exact_constants(self) -> bool:
"""Rebuild fixed float64 centers and widths exactly.
A float32 build or checkpoint leaves them float32-rounded; trainable
ones are learned values and stay, as do stored ones that are not the
defaults up to rounding. See
:func:`~xnn.common.models.constants.exact_float64_constants`.
Returns
-------
bool
Whether any value changed.
"""
from ..models.constants import restore_exact
if isinstance(self.means, nn.Parameter) or self.means.dtype != torch.float64:
return False
means, betas = self._initial(torch.float64)
changed = restore_exact(self.means, means)
return restore_exact(self.betas, betas) or changed
[docs]
def forward(self, r: Tensor) -> Tensor:
"""Expand distances onto the basis.
Parameters
----------
r : Tensor
Interatomic distances of arbitrary shape ``(...)``.
Returns
-------
Tensor
The expansion of shape ``(..., n_rbf)``, zero at and beyond the
cutoff.
"""
r = r.unsqueeze(-1)
# the distance mapped to exponential space, where the centers live
u = torch.exp(self.alpha * (self.cutoff_lower - r))
return self.cutoff_fn(r) * torch.exp(-self.betas * (u - self.means) ** 2)
[docs]
class BesselRBF(nn.Module):
"""Bessel radial basis (NequIP/DimeNet-style), smoother & fewer functions.
Expands an interatomic distance ``r`` into a set of ``n_rbf`` invariant
radial features using the normalized sinc/Bessel functions
``prefactor * sin(n*pi*r/rc) / r`` for ``n = 1, ..., n_rbf``. Compared with
a Gaussian basis this is smoother and needs fewer functions to cover the
cutoff sphere.
Parameters
----------
n_rbf : int, optional
Number of Bessel basis functions (output width). Default is 8.
cutoff : float, optional
Cutoff radius ``rc`` used to set the basis frequencies and the
normalization. Default is 5.0.
trainable : bool, optional
If ``True``, the ``n * pi`` frequencies are a learnable
:class:`torch.nn.Parameter` (the NequIP ``BesselBasis`` default);
otherwise a fixed buffer (the MACE default). Default is ``False``.
prefactor : float, optional
Overall normalization factor. ``None`` (default) uses the
DimeNet/MACE convention ``sqrt(2 / cutoff)``; the original NequIP
uses ``2 / cutoff``.
Attributes
----------
freqs : Tensor
The angular frequencies ``n * pi`` of shape ``(n_rbf,)`` -- a
registered buffer, or a :class:`torch.nn.Parameter` when
``trainable``.
norm : float
The scalar normalization ``prefactor``.
"""
def __init__(self, n_rbf: int = 8, cutoff: float = 5.0,
trainable: bool = False, prefactor: float | None = None):
super().__init__()
self.cutoff = cutoff
freqs = math.pi * torch.arange(1, n_rbf + 1, dtype=torch.get_default_dtype())
if trainable:
self.freqs = nn.Parameter(freqs)
else:
self.register_buffer("freqs", freqs)
self.norm = math.sqrt(2.0 / cutoff) if prefactor is None else float(prefactor)
[docs]
def exact_constants(self) -> bool:
"""Rebuild fixed float64 frequencies as the exact ``n * pi``.
A float32 build or checkpoint leaves them float32-rounded; trainable
frequencies are learned values and stay, as do stored ones that are not
``n * pi`` up to rounding. See
:func:`~xnn.common.models.constants.exact_float64_constants`.
Returns
-------
bool
Whether any value changed.
"""
from ..models.constants import restore_exact
freqs = self.freqs
if isinstance(freqs, nn.Parameter) or freqs.dtype != torch.float64:
return False
return restore_exact(freqs, math.pi * torch.arange(1, freqs.numel() + 1,
dtype=torch.float64))
[docs]
def forward(self, r: Tensor) -> Tensor:
"""Expand distances into the Bessel radial basis.
Parameters
----------
r : Tensor
Interatomic distances of arbitrary shape ``(...)``. Values are
clamped to a small positive minimum to avoid division by zero.
Returns
-------
Tensor
The radial embedding of shape ``(..., n_rbf)``.
"""
r = r.clamp(min=1e-8)[..., None]
return self.norm * torch.sin(self.freqs * r / self.cutoff) / r
[docs]
def smooth_bessel_coefficients(n_rbf: int, cutoff: float) -> Tensor:
"""The smooth spherical Bessel functions as combinations of ``sinc(k r / cutoff)``.
Kocer, Mason and Erturk (AIP Adv. 10, 015021, 2020) sum two neighboring
zeroth-order spherical Bessel functions,
``f_n(r) = a_n [j0((n+1) pi r / rc) + j0((n+2) pi r / rc)]`` with
``a_n = (-1)^n sqrt(2) pi (n+1) (n+2) / (rc^1.5 sqrt((n+1)^2 + (n+2)^2))``,
so that each vanishes with its first derivative at ``rc``, and
orthogonalize them: ``g_0 = f_0`` and
``g_n = (f_n + sqrt(e_n / d_(n-1)) g_(n-1)) / sqrt(d_n)``, with
``e_n = n^2 (n+2)^2 / (4 (n+1)^4 + 1)``, ``d_0 = 1`` and
``d_n = 1 - e_n / d_(n-1)``. Here ``j0(k pi r / rc) = sinc(k r / rc)``.
Parameters
----------
n_rbf : int
Number of functions ``g_0 ... g_(n_rbf - 1)``.
cutoff : float
The cutoff radius ``rc``.
Returns
-------
Tensor
Float64 coefficients ``(n_rbf, n_rbf + 1)``: ``g_n(r)`` is the sum over
``k`` of the entry ``[n, k - 1]`` times ``sinc(k r / rc)``, ``k = 1 ... n_rbf + 1``.
"""
f = torch.zeros(n_rbf, n_rbf + 1, dtype=torch.float64)
for n in range(n_rbf):
a = ((-1) ** n * math.sqrt(2.0) * math.pi / cutoff ** 1.5 * (n + 1) * (n + 2)
/ math.sqrt((n + 1) ** 2 + (n + 2) ** 2))
f[n, n] = f[n, n + 1] = a
g = torch.zeros_like(f)
d_previous = 1.0
for n in range(n_rbf):
if n == 0:
g[0] = f[0]
continue
e = n ** 2 * (n + 2) ** 2 / (4.0 * (n + 1) ** 4 + 1.0)
d = 1.0 - e / d_previous
g[n] = (f[n] + math.sqrt(e / d_previous) * g[n - 1]) / math.sqrt(d)
d_previous = d
return g
[docs]
class SmoothBesselRBF(nn.Module):
"""Smooth spherical Bessel radial basis (Kocer, Mason and Erturk 2020).
``n_rbf`` orthogonal combinations of zeroth-order spherical Bessel
functions that vanish smoothly at ``cutoff`` (see
:func:`smooth_bessel_coefficients`); no envelope is applied. The
coefficients stay exact in float64 through casts and loads.
Parameters
----------
n_rbf : int, optional
Number of basis functions, by default 3.
cutoff : float, optional
The cutoff radius, by default 5.0.
"""
def __init__(self, n_rbf: int = 3, cutoff: float = 5.0):
super().__init__()
from ..models.constants import register_constant
self.n_rbf = int(n_rbf)
self.cutoff = float(cutoff)
register_constant(self, "coefficients", smooth_bessel_coefficients(self.n_rbf, self.cutoff))
[docs]
def forward(self, r: Tensor) -> Tensor:
"""Expand distances ``(...)`` into the basis ``(..., n_rbf)``."""
k = torch.arange(1, self.n_rbf + 2, dtype=r.dtype, device=r.device)
return torch.sinc(r.unsqueeze(-1) * k / self.cutoff) @ self.coefficients.t()
#: The radial bases a model with a choice of basis can draw from (:func:`make_radial_basis`).
RADIAL_BASES = ("gaussian", "bessel", "expnorm", "smooth_bessel")
[docs]
def make_radial_basis(name: str, n_rbf: int, cutoff: float, trainable: bool = False, *,
gamma: float | None = None, prefactor: float | None = None) -> nn.Module:
"""A radial basis by name: the bases shared by every model that lets the basis be chosen.
An option the chosen basis does not have is ignored with an
:class:`~xnn.common.config.options.IgnoredOptionWarning`.
Parameters
----------
name : str
``"gaussian"`` (:class:`GaussianRBF`: ``n_rbf`` Gaussians centered on
``[0, cutoff]``, the center spacing as width unless ``gamma`` is
given; the centers stay exact in float64 through casts and loads),
``"bessel"`` (:class:`BesselRBF`, ``prefactor * sin(n pi r / cutoff) / r``)
``"expnorm"`` (:class:`ExpNormalSmearing`, Gaussians in
``exp(-r)`` with their own cosine envelope) or ``"smooth_bessel"``
(:class:`SmoothBesselRBF`, orthogonal spherical Bessel combinations that
vanish smoothly at the cutoff).
n_rbf : int
Number of basis functions.
cutoff : float
The cutoff radius.
trainable : bool, optional
Learn the parameters of the basis (the Bessel frequencies, the
exponential-normal centers and widths); the Gaussian basis has none.
gamma : float or None, optional
The exponent of the Gaussians, ``exp(-gamma (r - mu)^2)``.
prefactor : float or None, optional
The normalization of the Bessel functions (``None``: ``sqrt(2 / cutoff)``).
Returns
-------
torch.nn.Module
Maps distances ``(...)`` to ``(..., n_rbf)``.
Raises
------
ValueError
For an unknown ``name``.
"""
if name not in RADIAL_BASES:
raise ValueError(f"radial_basis must be one of {RADIAL_BASES}, got {name!r}")
from ..config.options import warn_ignored
given = {"trainable": trainable and name in ("gaussian", "smooth_bessel"),
"gamma": gamma is not None and name != "gaussian",
"prefactor": prefactor is not None and name != "bessel"}
warn_ignored(f"radial basis {name!r}", [k for k, ignored in given.items() if ignored])
if name == "gaussian":
from ..models.constants import register_constant
if gamma is None and n_rbf > 1: # the center spacing as width, in double precision
gamma = 0.5 * ((n_rbf - 1) / cutoff) ** 2
basis = GaussianRBF(n_rbf, cutoff, gamma=gamma)
register_constant(basis, "centers", torch.linspace(0.0, cutoff, n_rbf, dtype=torch.float64))
return basis
if name == "bessel":
return BesselRBF(n_rbf, cutoff, trainable=trainable, prefactor=prefactor)
if name == "smooth_bessel":
return SmoothBesselRBF(n_rbf, cutoff)
return ExpNormalSmearing(n_rbf, cutoff, trainable=trainable)