xnn.gnn.models.tensornet.TensorNet#
- class xnn.gnn.models.tensornet.TensorNet(n_features=128, n_interactions=2, n_rbf=32, cutoff=4.5, radial_basis='expnorm', trainable_rbf=False, activation='silu', equivariance_group='O(3)', n_elements=128, energy_shift=0.0, energy_scale=1.0, species=None, atomic_energies=None, readout='mlp', readout_hidden=None, self_pairs=True, rbf_max=None, gamma=None)[source]#
Bases:
ExactFloat64Constants,InteratomicPotentialTensorNet interatomic potential (rank-2 Cartesian tensor features).
See the module docstring for the equations. The defaults are the reference architecture:
F = 128, two interaction layers, 32 fixed exponential-normal radial functions with a cosine cutoff at 4.5 Angstrom,O(3)products.- Parameters:
n_features (int, optional) – Number of tensor features
F, by default 128.n_interactions (int, optional) – Number of interaction layers (
0keeps the embedding alone), by default 2.n_rbf (int, optional) – Number of radial basis functions, by default 32.
cutoff (float, optional) – Neighbor-list radius and cosine cutoff (Angstrom), by default 4.5.
radial_basis (str, optional) – Any basis of
make_radial_basis():"expnorm"(default),"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 filter MLPs and the readout, by default
"silu".equivariance_group (str, optional) –
"O(3)"(default; energies invariant under rotations and reflections) or"SO(3)"(rotations only, so enantiomers can differ).n_elements (int, optional) – Size of the embedding table, indexed by
Z, by default 128.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.atomic_energies (array-like or None, optional) – Per-species reference energies loaded into
atom_ref.readout (str, optional) – The atom-wise energy network:
"mlp"(default; the activated3F -> Fprojection, then dense layers) or"gated"(the projection itself, then aGatedMLP).readout_hidden (sequence of int or None, optional) – Hidden widths of the energy network; by default
(F/2,)for"mlp"and(F, F, F)for"gated".self_pairs (bool, optional) – Whether every atom is its own neighbor (at distance 0, with no direction) in the embedding and the messages, by default
True.rbf_max (float or None, optional) – Position of the last Gaussian of the
"gaussian"basis;None(default) is the cutoff.gamma (float or None, optional) – Exponent of the Gaussians of the
"gaussian"basis,exp(-gamma (r - mu)^2);None(default) takes the center spacing as width.
- Variables:
cutoff (float) – Neighbor-list cutoff radius.
rbf (torch.nn.Module) – The radial basis.
cutoff_fn (torch.nn.Module) – The cosine cutoff of the embedding and the filters.
tensor_embedding (torch.nn.Module) – The tensor embedding layer.
layers (torch.nn.ModuleList) – The interaction layers.
out_norm (torch.nn.LayerNorm) – Layer norm of the
(3 I^2, |A|^2, |S|^2)readout features.linear (torch.nn.Linear) – The
3F -> Freadout projection.readout (torch.nn.Module) – Atom-wise energy network.
atom_ref (torch.nn.Embedding) – Learnable per-element energy reference, initialized to zero.
- readout_extra_inputs = 0#
Per-atom inputs of the energy network besides the
Ffeatures (a subclass’s own).
- set_use_fast(use_fast)[source]#
Choose the implementation of the embedding and interaction layers.
- Parameters:
use_fast (bool or str) –
"auto"(the default): the fused GPU kernels ofxnn.gnn.fast.tensornetwhere 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:
- receptive_field()[source]#
The distance (Angstrom) beyond which atoms no longer change an atom’s energy: one cutoff for the embedding and one per interaction.
- Return type:
- index_planner()[source]#
The planner of a captured evaluation or training step: TensorNet needs nothing beyond its edges (
NoIndexPlanner).
- set_energy_scale_shift(scale, shift)[source]#
Set the per-atom energy standardization from training statistics.
- set_atomic_energies(species, values)[source]#
Initialize the per-element reference energies
atom_ref.- Parameters:
- Raises:
ValueError – If the number of values does not match the number of species.
- Return type:
None
- tensors(atomic_numbers, edge_index, edge_vec, total_charge)[source]#
TorchScript-compatible trunk: the tensor features
Xof every atom.- Parameters:
atomic_numbers (Tensor) – Per-atom atomic numbers
(N,).edge_index (Tensor) – Edge index
(2, E); row 0 the neighborj, row 1 the centeri.edge_vec (Tensor) – Edge vectors
pos[i] - pos[j]of shape(E, 3)(with any periodic image shift applied).total_charge (Tensor) – Total charge of every atom’s structure,
(N,).
- Returns:
The tensors
Xof shape(N, 3, 3, F)after the last layer.- Return type:
Tensor
- node_features_energy(atomic_numbers, edge_index, edge_vec)[source]#
TorchScript-compatible core of a neutral system: 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 invariant features
(N, F)and the per-atom energy(N,).- Return type:
tuple of Tensor
- node_features_energy_charges(atomic_numbers, edge_index, edge_vec, pos, cell, pbc, total_charge, spin_multiplicity)[source]#
TorchScript-compatible core of a charged system (the deployment ABI).
- Parameters:
atomic_numbers (Tensor) – Per-atom atomic numbers
(N,).edge_index (Tensor) – Edge index
(2, E).edge_vec (Tensor) – Edge vectors
(E, 3).pos (Tensor) – Unused (part of the shared ABI).
cell (Tensor) – Unused (part of the shared ABI).
pbc (Tensor) – Unused (part of the shared ABI).
total_charge (float) – Net charge of the system.
spin_multiplicity (float) – Unused.
- Returns:
The invariant features
(N, F), the per-atom energy(N,)and an empty(0,)tensor (the model predicts no charges).- Return type:
tuple of Tensor
- node_energy(atomic_numbers, edge_index, edge_vec)[source]#
Per-atom energy
(N,)of a neutral system (the LAMMPS entry point).
- forward(data)[source]#
Energies of a batched graph.
- Parameters:
data (AtomicGraph) – Batched atomic graph; its
total_charge(per structure, zero when absent) enters the interaction layers.- Returns:
"node_energy"(N,),"energy"(B,),"node_features"(the invariant readout features,(N, F)) and"node_tensors"((N, 3, 3, F)).- Return type:
dict of str to Tensor
- classmethod from_foundation(name='tensornet-matpes-pbe-2025.2', dtype=None, cache_dir=None)[source]#
Load a MatGL TensorNet foundation potential (MatPES PBE or r2SCAN).
See
xnn.gnn.models.tensornet_foundation.- Parameters:
name (str, optional) –
"tensornet-matpes-pbe-2025.2"(default) or"tensornet-matpes-r2scan-2025.2".dtype (torch.dtype or str, optional) – Final dtype;
Nonekeeps the checkpoint’s float32.cache_dir (str or pathlib.Path, optional) – Model cache directory.
- Returns:
The converted potential, in evaluation mode.
- Return type:
- classmethod from_config(cfg)[source]#
Build a
TensorNetfrom a configuration object.Core fields:
cfg.n_features,cfg.n_interactions,cfg.n_rbf,cfg.cutoff. Everything else is read fromcfg.extra(radial_basis,trainable_rbf,activation,equivariance_group,n_elements,energy_shift,energy_scale,species,atomic_energies,readout,readout_hidden,self_pairs,rbf_max,gamma); the reference code’s key spellings are translated byxnn.common.config.translate.- Parameters:
cfg (xnn.common.config.schema.ModelConfig) – The core model config.
- Returns:
Instantiated model.
- Return type: