deepmd.pt.model.descriptor.sezm_nn.embedding#
Embedding layers for the SeZM descriptor.
This module defines the type embedding, geometric initial embedding, and environment-seed embedding used to initialize SeZM node features.
Classes#
Minimal SeZM type embedding with Adam-routed parameter naming. | |
Geometric initial embedding that adds zonal (m=0) rotated features. | |
Environment matrix initial embedding for l=0 features. | |
Frame-level charge and spin embedding for scalar type features. | |
Per-atom spin embedding for the native spin scheme. |
Module Contents#
- class deepmd.pt.model.descriptor.sezm_nn.embedding.SeZMTypeEmbedding(*, ntypes: int, embed_dim: int, dtype: torch.dtype, seed: int | list[int] | None = None, trainable: bool, padding: bool = True)[source]#
Bases:
torch.nn.ModuleMinimal SeZM type embedding with Adam-routed parameter naming.
- Parameters:
- ntypes
Number of atom types.
- embed_dim
Embedding dimension.
- dtype
Parameter dtype.
- seed
Random seed for initialization.
- trainable
Whether parameters are trainable.
- padding
Whether to append one all-zero padding row.
Notes
The parameter is named with
adam_prefix so HybridMuon routes it to Adam.- forward(atype: torch.Tensor) torch.Tensor[source]#
Gather type embeddings.
- Parameters:
- atype
Atom types with shape (…,). Valid type range is [0, ntypes-1].
- Returns:
torch.TensorType embeddings with shape (…, embed_dim).
- class deepmd.pt.model.descriptor.sezm_nn.embedding.GeometricInitialEmbedding(*, lmax: int, channels: int, dtype: torch.dtype)[source]#
Bases:
torch.nn.ModuleGeometric initial embedding that adds zonal (m=0) rotated features.
This module rotates pre-computed radial features for each degree l >= 1 using the zonal (m=0) column of the cached inverse Wigner-D blocks (local->global). The l=0 component is not computed here since it comes from type embedding.
- Parameters:
- lmax
Maximum node degree for the initial embedding.
- channels
Number of channels per (l, m) coefficient.
- dtype
Parameter dtype.
- forward(*, n_nodes: int, edge_cache: deepmd.pt.model.descriptor.sezm_nn.edge_cache.EdgeFeatureCache, radial_feat: torch.Tensor, zonal_coupling: torch.Tensor | None = None, spin_l1_message: torch.Tensor | None = None) torch.Tensor[source]#
- Parameters:
- n_nodes
Number of nodes (nf*nloc).
- edge_cache
Per-edge cache containing geometry, weights, and Wigner-D blocks.
- radial_feat
Per-edge radial features with shape (E, lmax, C) for l=1..lmax.
- zonal_coupling
Optional precomputed zonal coupling with shape (E, D-1). If None, it is gathered from
edge_cache.Dt_full.- spin_l1_message
Optional per-edge neighbor-spin l=1 message with shape (E, 3, C) for the native spin scheme (built by
SpinEmbedding.edge_l1). It is added to the l=1 rows of the per-edge message, so it shares this module’s source gate, scatter and degree normalization with the geometric message.
- Returns:
torch.TensorInitial features to add with shape (N, D, C). l=0 is guaranteed zero.
- class deepmd.pt.model.descriptor.sezm_nn.embedding.EnvironmentInitialEmbedding(*, ntypes: int, n_radial: int, channels: int, embed_dim: int = 64, axis_dim: int = 8, type_dim: int = 16, hidden_dim: int = 64, mlp_bias: bool = False, activation_function: str = 'silu', eps: float = 1e-07, use_spin: list[bool] | None = None, dtype: torch.dtype, trainable: bool, seed: int | list[int] | None = None)[source]#
Bases:
torch.nn.ModuleEnvironment matrix initial embedding for l=0 features.
Computes an initial embedding based on the 4D environment matrix:
[s, s * rx, s * ry, s * rz]
Combined with independent type embeddings (individual type embedding), providing physical inductive bias for l=0 features.
The computation follows the environment matrix approach where:
1. Build `r_tilde = [s, s*r_hat]` where `s = edge_env / r` and `r_hat = edge_vec / r` 2. G network: `g = G(rbf_proj(edge_rbf), type_src, type_dst)` produces per-edge features - Uses independent `env_type_embed` instead of projecting from main type embedding - Uses `rbf_proj` to project edge_rbf to `rbf_out_dim` 3. env_agg: aggregate outer product `r_tilde ⊗ g` by destination node 4. D matrix: `D = env_agg^T @ env_agg[:, :, :axis_dim]` 5. Output: projection of flattened D matrix into FiLM logits
- Parameters:
- ntypes
int Number of atom types.
- n_radial
int Number of radial basis functions.
- channels
int Output channel dimension per FiLM branch (final output is 2*channels).
- embed_dim
int G network output dimension (filter width).
- axis_dim
int D matrix axis dimension (must be < embed_dim).
- type_dim
int Dimension for independent type embeddings in env_seed.
- hidden_dim
int Hidden layer size for G network.
- mlp_biasbool
Whether to enable bias terms in env-seed MLP layers (rbf_proj_layer1/2 and g_layer1/2).
- activation_function
str Activation function for G network hidden layer.
- eps
float Small epsilon for numerical stability.
- use_spin
list[bool] |None Per-type spin flags (native spin scheme). When provided, the neighbor spin is appended as extra coordinate channels of the environment matrix, so the inner product
D = M^T Madditionally yields the neighbor spin-spin invariants. A per-type mask gates the channel, so a non-magnetic neighbor contributes zero and carries zero magnetic force.- dtype
torch.dtype Parameter dtype.
- trainablebool
Whether parameters are trainable.
- seed
int|list[int] |None Random seed for reproducibility.
- ntypes
- forward(*, edge_cache: deepmd.pt.model.descriptor.sezm_nn.edge_cache.EdgeFeatureCache, atype_flat: torch.Tensor, n_nodes: int, spin: torch.Tensor | None = None) torch.Tensor[source]#
Compute environment FiLM logits for l=0 conditioning.
- Parameters:
- edge_cache
EdgeFeatureCache Edge cache containing src, dst, edge_vec, edge_rbf, edge_env.
- atype_flat
torch.Tensor Flattened atom types with shape (N,), where N = nf * nloc.
- n_nodes
int Number of nodes (N = nf * nloc).
- spin
torch.Tensor|None Per-node spin vectors with shape (N, 3) for the native spin scheme. Used only when
use_spinis set; the source (neighbor) spin is appended to the environment matrix as an envelope-gated coordinate channel. WhenNonethe spin channels are zero-padded so the coordinate dimension stays fixed.
- edge_cache
- Returns:
torch.TensorFiLM logits with shape (N, 2*channels).
- class deepmd.pt.model.descriptor.sezm_nn.embedding.ChargeSpinEmbedding(*, embed_dim: int, activation_function: str, dtype: torch.dtype, seed: int | list[int] | None = None, trainable: bool)[source]#
Bases:
torch.nn.ModuleFrame-level charge and spin embedding for scalar type features.
- Parameters:
- embed_dim
Embedding dimension.
- activation_function
Activation function used by the mixing layer.
- dtype
Parameter dtype.
- seed
Random seed for initialization.
- trainable
Whether parameters are trainable.
- forward(charge_spin: torch.Tensor) torch.Tensor[source]#
Embed frame-level charge and spin.
- Parameters:
- charge_spin
Frame charge and spin values with shape (nf, 2).
- Returns:
torch.TensorMixed condition embedding with shape (nf, embed_dim).
- class deepmd.pt.model.descriptor.sezm_nn.embedding.SpinEmbedding(*, ntypes: int, channels: int, use_spin: list[bool], activation_function: str = 'silu', dtype: torch.dtype, seed: int | list[int] | None = None, trainable: bool = True)[source]#
Bases:
torch.nn.ModulePer-atom spin embedding for the native spin scheme.
The per-atom spin vector
sis injected as an equivariant extension of the type embedding, producing two additive contributions to the descriptor node features:l = 0 (invariant): a small network of the squared magnitude
|s|^2yields a per-channel scalar added to the scalar type embedding. The squared magnitude is used (rather than|s|) so the feature is smooth ats = 0and its gradient there vanishes, keeping the magnetic force continuous as a spin crosses zero.l = 1 (equivariant): the Cartesian spin vector is mapped to the packed
l = 1coefficients through the SeZM Wigner-D convention (derived frombuild_cartesian_basis()), then scaled by a per-type per-channel weight. The map is linear ins, so the contribution vanishes ats = 0and rotates as anl = 1object under SO(3), i.e.cart_to_l1(R s) = D^1(R) cart_to_l1(s).
Both contributions are gated by a per-type spin mask, so atom types without spin contribute exactly zero regardless of their (nominally zero) input.
- Parameters:
- ntypes
Number of (real) atom types.
- channels
Number of channels per (l, m) coefficient.
- use_spin
Per-type boolean flags marking which atom types carry spin.
- activation_function
Activation used by the magnitude network.
- dtype
Parameter dtype.
- seed
Random seed for initialization.
- trainable
Whether parameters are trainable.
- forward(spin: torch.Tensor, atype: torch.Tensor) tuple[torch.Tensor, torch.Tensor][source]#
Compute the l=0 and l=1 spin contributions.
- Parameters:
- spin
Per-atom spin vectors with shape (N, 3).
- atype
Per-atom types with shape (N,).
- Returns:
tuple[torch.Tensor,torch.Tensor](scalar, vector)wherescalarhas shape (N, channels) for the l=0 contribution andvectorhas shape (N, 3, channels) for the packed l=1 contribution (orders m = -1, 0, +1). Both are exactly zero for atom types without spin.
- edge_l1(spin: torch.Tensor, atype: torch.Tensor, edge_cache: deepmd.pt.model.descriptor.sezm_nn.edge_cache.EdgeFeatureCache) torch.Tensor[source]#
Build the per-edge neighbor-spin l=1 message for the GIE aggregation.
Each edge carries the packed
l = 1coefficients of the source (neighbor) spin, scaled by a per-source-type per-channel weight and gated by the C^3 envelope. The message is returned per edge; the geometric initial embedding folds it into the l=1 rows and applies the shared source gate, scatter and degree normalization, so a neighbor’s spin direction enters an atom’s l=1 backbone before any interaction block (the spin analogue of the geometric initial embedding).- Parameters:
- spin
Per-node spin vectors with shape (N, 3).
- atype
Per-node types with shape (N,).
- edge_cache
Edge cache providing
srcandedge_env.
- Returns:
torch.TensorPer-edge packed l=1 message with shape (E, 3, channels), exactly zero for non-magnetic neighbors.
- _build_cart_to_l1_matrix() torch.Tensor[source]#
Build the
(3, 3)Cartesian-to-packed-l=1projection.The packed
l = 1coefficient of a vectorvis obtained by projecting the skew-symmetric matrix[v]_xonto the antisymmetricl = 1block ofbuild_cartesian_basis(). With packed orderm = -1, 0, +1, rowdand Cartesian componentkgiveM[d, k] = <[e_k]_x, B[1 + d]>_F, socoeff = M @ vandM @ (R v) = D^1(R) (M @ v).