xnn.gnn.models.painn.PaiNN#

class xnn.gnn.models.painn.PaiNN(n_features=128, n_interactions=3, n_rbf=20, cutoff=5.0, radial_basis='gaussian', trainable_rbf=False, shared_filters=False, shared_interactions=False, epsilon=1e-08, dipole=False, polarizability=False, n_output_blocks=2, correct_charges=True, atomic_dipoles=False, weight_init='xavier_uniform', scalar_product=True, vector_propagation=True, vector_features=True, energy_shift=0.0, energy_scale=1.0, species=None, atomic_energies=None)[source]#

Bases: ExactFloat64Constants, InteratomicPotential

PaiNN interatomic potential with optional dipole and polarizability heads.

Faithful to the ICML 2021 manuscript (see the module docstring for the equation-by-equation walk-through): embeds atoms by nuclear charge, refines scalar and vector features through T message/update block pairs and reads out per-atom energies through a two-layer network with a SiLU nonlinearity. Per-atom energies are standardized (energy_scale, energy_shift) and shifted by the per-element reference atom_ref, then sum-pooled into the total energy.

The defaults are F = 128 features, T = 3 blocks and 20 Gaussian radial functions with a cosine cutoff at 5 Angstrom; the paper’s model uses 20 Bessel functions (radial_basis="bessel") and, for dipoles, atomic dipoles (atomic_dipoles=True).

Parameters:
  • n_features (int, optional) – Width F of the scalar and vector features, by default 128.

  • n_interactions (int, optional) – Number of message/update block pairs T, by default 3.

  • n_rbf (int, optional) – Number of radial basis functions, by default 20.

  • cutoff (float, optional) – Neighbor-list radius and cosine cutoff (Angstrom), by default 5.0.

  • radial_basis (str, optional) – Any basis of make_radial_basis(): "gaussian" (default; Gaussians on [0, cutoff] with the center spacing as width), "bessel" (the paper’s sin(n pi r / r_cut) / r, without a normalization) or "expnorm".

  • trainable_rbf (bool, optional) – Learn the parameters of the radial basis, by default False.

  • shared_filters (bool, optional) – The same filters for every block instead of a set per block. Default False.

  • shared_interactions (bool, optional) – One set of message and update networks for all blocks (each block keeps its filters unless shared_filters). Default False.

  • epsilon (float, optional) – Stabilizer of the vector norm in the update block, by default 1e-8.

  • dipole (bool, optional) – Add a dipole head (outputs "dipole" (B, 3) and the latent "charges" (N,)): eq 12, or eq 13 with atomic_dipoles. Default False.

  • polarizability (bool, optional) – Add the polarizability head of eq 14 (output "polarizability" (B, 3, 3)). Default False.

  • n_output_blocks (int, optional) – Layers per output head (gated equivariant blocks of the tensorial heads, dense layers of the charge network), widths halving from F to 1; by default 2.

  • correct_charges (bool, optional) – Shift the latent charges so they sum to the structure’s net charge (total_charge, zero when absent). Default True.

  • atomic_dipoles (bool, optional) – The dipole model of eq 13: latent charges and atomic dipoles mu_i(v_i) from gated equivariant blocks (the paper’s dipole model). False (default) keeps latent charges alone, computed from the scalars by an atom-wise network (eq 12).

  • weight_init (str, optional) – Initialization of every dense layer (a name of WEIGHT_INITS; biases start at zero), by default "xavier_uniform"; "torch" keeps PyTorch’s.

  • scalar_product (bool, optional) – Keep the <U v, V v> term of eq 9 (Table IV ablation when off).

  • vector_propagation (bool, optional) – Keep the v_j o phi_vv term of eq 8 (Table IV ablation when off).

  • vector_features (bool, optional) – Keep the vector features at all; False is the invariant model of the last Table IV row (scalar messages of eq 7 and ds = a_ss(s) updates; the tensorial heads then see zero vectors). Ablated terms carry no parameters.

  • energy_shift (float, optional) – Additive per-atom energy standardization, by default 0.0.

  • energy_scale (float, optional) – Multiplicative per-atom energy standardization, by default 1.0.

  • species (list of int or None, optional) – Only used to interpret atomic_energies; the model handles all elements up to Z = 99.

  • atomic_energies (array-like or None, optional) – Per-species reference energies loaded into atom_ref.

Variables:
  • cutoff (float) – Neighbor-list cutoff radius.

  • embedding (torch.nn.Embedding) – Nuclear-charge-to-feature embedding a_Z.

  • rbf (torch.nn.Module) – The radial basis (see radial_basis).

  • cutoff_fn (CosineCutoff) – The cosine cutoff multiplying every filter.

  • filters (torch.nn.Linear) – The filter layer of all blocks at once: block k’s filters are the k-th filter_width outputs (one block’s when shared_filters).

  • interactions (torch.nn.ModuleList) – The T message/update block pairs (message and update submodules).

  • readout (torch.nn.Sequential) – Atom-wise energy network F -> F/2 -> 1.

  • atom_ref (torch.nn.Embedding) – Learnable per-element energy reference, initialized to zero.

  • polarizability_head (dipole_head,) – The tensorial heads, when requested.

  • recompute (str) – "auto" (default): in evaluation mode from RECOMPUTE_MIN_EDGES edges, each block keeps only its inputs for the backward pass and evaluates its filters and the block again there (the same values), so the filters of all blocks, (E, T parts F), are never stored; "on": whenever a gradient is needed; "off": never. Set with set_recompute_radial(). Filters wrapped by an adapter (LoRA) are always kept.

radial_recompute = True#

recomputes its filters on request (see set_recompute_radial)

set_use_fast(use_fast)[source]#

Choose the implementation of the message blocks.

Parameters:

use_fast (bool or str) – "auto" (the default): the fused GPU kernels of xnn.gnn.fast.painn where they are available and faster for the input (a CUDA device, Triton, a graph above the size crossover measured for evaluation, AUTO_POLICY, or for training, AUTO_POLICY_TRAINING); True: wherever they are available; False: always the reference implementation. The kernels are twice differentiable, so force training takes them as well. In float32 the kernels agree with the reference to about 1e-6 relative, in float64 to about 1e-14.

Returns:

self.

Return type:

PaiNN

set_energy_scale_shift(scale, shift)[source]#

Set the per-atom energy standardization from training statistics.

Parameters:
  • scale (float) – Standard deviation of the training-set energy per atom.

  • shift (float) – Mean training-set energy per atom.

Return type:

None

set_atomic_energies(species, values)[source]#

Initialize the per-element reference energies atom_ref.

Parameters:
  • species (list of int) – Atomic numbers the values refer to, in order.

  • values (array-like) – One reference energy per entry of species.

Raises:

ValueError – If the number of values does not match the number of species.

Return type:

None

representation(atomic_numbers, edge_index, edge_vec)[source]#

TorchScript-compatible trunk: the scalar and vector features of every atom.

Parameters:
  • atomic_numbers (Tensor) – Per-atom atomic numbers (N,).

  • edge_index (Tensor) – Edge index (2, E); row 0 the neighbor j, row 1 the center i.

  • edge_vec (Tensor) – Edge vectors pos[i] - pos[j] of shape (E, 3) (with any periodic image shift applied).

Returns:

Scalars s of shape (N, F) and vectors v of shape (N, 3, F) after the last update block.

Return type:

tuple of Tensor

node_features_energy(atomic_numbers, edge_index, edge_vec)[source]#

TorchScript-compatible core: scalar features and per-atom energies.

Parameters:
  • atomic_numbers (Tensor) – Per-atom atomic numbers (N,).

  • edge_index (Tensor) – Edge index (2, E).

  • edge_vec (Tensor) – Edge vectors (E, 3).

Returns:

The scalar features (N, F) and the per-atom energy (N,) (standardized and including the per-element reference).

Return type:

tuple of Tensor

node_energy(atomic_numbers, edge_index, edge_vec)[source]#

Per-atom energy (N,) (the LAMMPS / TorchScript entry point).

Parameters:
Return type:

Tensor

forward(data)[source]#

Energies and, when the heads are present, dipoles and polarizabilities.

Parameters:

data (AtomicGraph) – Batched atomic graph.

Returns:

"node_energy" (N,), "energy" (B,), "node_features" (the scalars, (N, F)), "node_vectors" ((N, 3, F)) and, with the heads, "dipole" (B, 3) with "charges" (N,) and "polarizability" (B, 3, 3).

Return type:

dict of str to Tensor

classmethod from_config(cfg)[source]#

Build a PaiNN from a configuration object.

Core fields: cfg.n_features, cfg.n_interactions, cfg.n_rbf, cfg.cutoff. Everything else is read from cfg.extra (radial_basis, trainable_rbf, shared_filters, shared_interactions, epsilon, dipole, polarizability, n_output_blocks, correct_charges, atomic_dipoles, weight_init, scalar_product, vector_propagation, vector_features, energy_shift, energy_scale, species, atomic_energies); other codes’ key spellings are translated by xnn.common.config.translate.

Parameters:

cfg (xnn.common.config.schema.ModelConfig) – The core model config.

Returns:

Instantiated model.

Return type:

PaiNN