xnn.trfnn.models.et.EquivariantTransformer#

class xnn.trfnn.models.et.EquivariantTransformer(n_features=128, n_interactions=6, n_rbf=32, cutoff=5.0, num_heads=8, radial_basis='expnorm', trainable_rbf=False, activation='silu', attn_activation='silu', neighbor_embedding=True, distance_influence='both', vector_cutoff=True, n_elements=100, energy_shift=0.0, energy_scale=1.0, species=None, atomic_energies=None)[source]#

Bases: TransformerPotential

The TorchMD-Net equivariant transformer (ET).

See the module docstring for the equations. The defaults are the MD17 / ANI-1 architecture of the paper (Table 4): F = 128, six update layers, eight heads, 32 exponential-normal radial functions with a cosine cutoff at 5 Angstrom (1.34 M parameters); the QM9 model has F = 256, eight layers and 64 radial functions.

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

  • n_interactions (int, optional) – Number of update layers, by default 6.

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

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

  • num_heads (int, optional) – Attention heads (n_features must be divisible by it), by default 8.

  • radial_basis (str, optional) – Any basis of make_radial_basis(): "expnorm" (default, eq 1), "gaussian" or "bessel".

  • trainable_rbf (bool, optional) – Learn the parameters of the basis (the exponential-normal centers and widths, the Bessel frequencies), by default False.

  • activation (str, optional) – Nonlinearity of the distance filters and the output network, by default "silu".

  • attn_activation (str, optional) – Nonlinearity of the attention logits (eq 8), by default "silu".

  • neighbor_embedding (bool, optional) – Use the neighborhood embedding of eq 3 and 4 (the ablation of section 3.2 drops it), by default True.

  • distance_influence (str, optional) – Where the distance filters enter the attention: "both" (default), "keys", "values" or "none".

  • vector_cutoff (bool, optional) – Apply the cutoff to the whole value pathway (TorchMD-Net 2.0, a continuous energy at the cutoff), by default True; False is the original ICLR 2022 model.

  • n_elements (int, optional) – Size of the embedding tables, indexed by Z, by default 100.

  • 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 every element below n_elements.

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

Variables:
fast_path = 'xnn.trfnn.fast.et'#

the module of the model’s fast path (its AUTO_POLICY and AUTO_POLICY_TRAINING)

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:

The layer-normed scalars x of shape (N, F) and the vectors v of shape (N, 3, F).

Return type:

tuple of Tensor

attention_weights(data)[source]#

The attention weights A_ij of every update layer.

Parameters:

data (AtomicGraph) – The graph (the weights refer to its edge_index; the self-interaction terms are not included).

Returns:

One (E, num_heads) tensor per layer.

Return type:

list 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,).

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 of a batched graph.

Parameters:

data (AtomicGraph) – Batched atomic graph.

Returns:

"node_energy" (N,), "energy" (B,), "node_features" (the layer-normed scalars, (N, F)) and "node_vectors" ((N, 3, F)).

Return type:

dict of str to Tensor

classmethod from_config(cfg)[source]#

Build an EquivariantTransformer from a configuration object.

Core fields: cfg.n_features, cfg.n_interactions, cfg.n_rbf, cfg.cutoff. Everything else is read from cfg.extra (num_heads, radial_basis, trainable_rbf, activation, attn_activation, neighbor_embedding, distance_influence, vector_cutoff, n_elements, energy_shift, energy_scale, species, atomic_energies); the reference code’s 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:

EquivariantTransformer