deepmd.pt_expt.utils.network#
Classes#
Parameter subclass that supports | |
PyTorch layer wrapping dpmodel's | |
Native representation of a neural network. | |
The embedding network. | |
The fitting network. It may be implemented as an embedding | |
A collection of networks for multiple elements. | |
Implementation of Layer Normalization layer. | |
The unit operation of a native model. |
Functions#
| Apply activation function using native torch ops. |
Module Contents#
- class deepmd.pt_expt.utils.network.TorchArrayParam[source]#
Bases:
torch.nn.ParameterParameter subclass that supports
np.array(param)conversion.Note: this class is intentionally NOT used for model parameters.
make_fx(torch.fx.experimental.proxy_tensor) usesProxyTorchDispatchModeto intercept tensor operations. When an operand is a subclass oftorch.Tensor(including subclasses oftorch.nn.Parameter), PyTorch invokes the__torch_function__protocol which the proxy dispatch mode does not handle, causingaten.mmand other ops to fail with “Multiple dispatch failed … returned NotImplemented”. Using plaintorch.nn.Parameteravoids this because the proxy mode is designed to work with the baseParametertype.TorchArrayParamis kept only for backward compatibility and should not be used for new code.- __array__(dtype: Any | None = None) numpy.ndarray[source]#
- class deepmd.pt_expt.utils.network.NativeLayer(*args: Any, **kwargs: Any)[source]#
Bases:
deepmd.dpmodel.utils.network.NativeLayer,torch.nn.ModulePyTorch layer wrapping dpmodel’s
NativeLayer.Two aspects of the inherited dpmodel
call()are incompatible withmake_fxtracing (used to exportforward_lowerwithautograd.grad-based force/virial computation):Ellipsis indexing (
self.w[...]): On atorch.Tensorthis triggersaten.alias, an op thatProxyTorchDispatchModedoes not support, resulting in “Multiple dispatch failed foraten.alias.default”.``array_api_compat`` wrappers (
xp = array_api_compat .array_namespace(x); xp.matmul(…)): The wrappers re-entertorch.matmulthrough Python, which goes through the__torch_function__protocol. Under the proxy dispatch mode this path also fails with “Multiple dispatch failed”.
This class therefore overrides
call()with an implementation that uses plaintorchops exclusively (torch.matmul,torch.tanh, etc.), avoiding both issues.Trainable weights are stored as plain
torch.nn.Parameter(notTorchArrayParam) for the samemake_fxcompatibility reason — see theTorchArrayParamdocstring.- call(x: torch.Tensor) torch.Tensor[source]#
Forward pass using pure torch ops.
Overrides dpmodel’s
call()to ensure compatibility withmake_fx(torch.fx.experimental.proxy_tensor).The dpmodel implementation uses
self.w[...]andarray_api_compat.array_namespace(x).matmul(…)for backend-agnostic array operations. Both patterns break undermake_fx’sProxyTorchDispatchMode:self.w[...]emitsaten.aliaswhich the proxy mode cannot dispatch.array_api_compatre-enterstorch.matmulvia Python, hitting__torch_function__which the proxy mode returnsNotImplementedfor.
This override uses
torch.matmul,torch.cat, and_torch_activationdirectly, sidestepping both issues.
- forward(x: torch.Tensor) torch.Tensor[source]#
- deepmd.pt_expt.utils.network._torch_activation(x: torch.Tensor, name: str) torch.Tensor[source]#
Apply activation function using native torch ops.
The dpmodel
get_activation_fnreturns closures that callarray_api_compat.array_namespace(x).tanh(x)etc. Undermake_fxproxy tracing, thearray_api_compatindirection triggers__torch_function__dispatch failures. This function callstorch.tanhand friends directly to avoid the issue.
- class deepmd.pt_expt.utils.network.NativeNet(layers: list[dict] | None = None)[source]#
Bases:
make_multilayer_network(NativeLayer,NativeOP)Native representation of a neural network.
- Parameters:
- layers
list[NativeLayer],optional The layers of the network.
- layers
- class deepmd.pt_expt.utils.network.EmbeddingNet(*args: Any, **kwargs: Any)[source]#
Bases:
deepmd.dpmodel.utils.network.EmbeddingNet,torch.nn.ModuleThe embedding network.
- Parameters:
- in_dim
Input dimension.
- neuron
The number of neurons in each layer. The output dimension is the same as the dimension of the last layer.
- activation_function
The activation function.
- resnet_dt
Use time step at the resnet architecture.
- precision
Floating point precision for the model parameters.
- seed
int,optional Random seed.
- biasbool,
Optional Whether to use bias in the embedding layer.
- trainablebool or
list[bool],Optional Whether the weights are trainable. If a list, each element corresponds to a layer.
- forward(x: torch.Tensor) torch.Tensor[source]#
- class deepmd.pt_expt.utils.network.FittingNet(*args: Any, **kwargs: Any)[source]#
Bases:
deepmd.dpmodel.utils.network.FittingNet,torch.nn.ModuleThe fitting network. It may be implemented as an embedding net connected with a linear output layer.
- Parameters:
- in_dim
Input dimension.
- out_dim
Output dimension
- neuron
The number of neurons in each hidden layer.
- activation_function
The activation function.
- resnet_dt
Use time step at the resnet architecture.
- precision
Floating point precision for the model parameters.
- bias_out
The last linear layer has bias.
- seed
int,optional Random seed.
- trainablebool or
list[bool],optional Whether the network is trainable.
- forward(x: torch.Tensor) torch.Tensor[source]#
- class deepmd.pt_expt.utils.network.NetworkCollection(*args: Any, **kwargs: Any)[source]#
Bases:
deepmd.dpmodel.utils.network.NetworkCollectionA collection of networks for multiple elements.
The number of dimensions for types might be 0, 1, or 2. - 0: embedding or fitting with type embedding, in () - 1: embedding with type_one_side, or fitting, in (type_i) - 2: embedding without type_one_side, in (type_i, type_j)
- Parameters:
- class deepmd.pt_expt.utils.network.LayerNorm(num_in: int, eps: float = 1e-05, uni_init: bool = True, trainable: bool = True, precision: str = DEFAULT_PRECISION, seed: int | list[int] | None = None)[source]#
Bases:
deepmd.dpmodel.utils.network.LayerNorm,NativeLayerImplementation of Layer Normalization layer.
- Parameters:
- num_in
int The input dimension of the layer.
- eps
float,optional A small value added to prevent division by zero in calculations.
- uni_initbool,
optional If initialize the weights to be zeros and ones.
- trainablebool,
optional If the weights are trainable.
- precision
str,optional The precision of the layer.
- seed
int,optional Random seed.
- num_in
- class deepmd.pt_expt.utils.network.Identity[source]#
Bases:
deepmd.dpmodel.utils.network.IdentityThe unit operation of a native model.
- forward(x: torch.Tensor) torch.Tensor[source]#