deepmd.infer.deep_eval

Contents

deepmd.infer.deep_eval#

Classes#

DeepEvalBackend

Low-level Deep Evaluator interface.

DeepEval

High-level Deep Evaluator interface.

Module Contents#

class deepmd.infer.deep_eval.DeepEvalBackend(model_file: str, output_def: deepmd.dpmodel.output_def.ModelOutputDef, *args: Any, auto_batch_size: bool | int | deepmd.utils.batch_size.AutoBatchSize = True, neighbor_list: ase.neighborlist.NewPrimitiveNeighborList | None = None, **kwargs: Any)[source]#

Bases: abc.ABC

Low-level Deep Evaluator interface.

Backends should inherbit implement this interface. High-level interface will be built on top of this.

Parameters:
model_filePath

The name of the frozen model file.

*argslist

Positional arguments.

auto_batch_sizebool or int or AutoBatchSize, default: True

If True, automatic batch size will be used. If int, it will be used as the initial batch size.

neighbor_listase.neighborlist.NewPrimitiveNeighborList, optional

The ASE neighbor list class to produce the neighbor list. If None, the neighbor list will be built natively in the model.

**kwargsdict

Keyword arguments.

_OUTDEF_DP2BACKEND: ClassVar[dict][source]#
abstractmethod eval(coords: numpy.ndarray, cells: numpy.ndarray | None, atom_types: numpy.ndarray, atomic: bool = False, fparam: numpy.ndarray | None = None, aparam: numpy.ndarray | None = None, **kwargs: Any) dict[str, numpy.ndarray][source]#

Evaluate the energy, force and virial by using this DP.

Parameters:
coords

The coordinates of atoms. The array should be of size nframes x natoms x 3

cells

The cell of the region. If None then non-PBC is assumed, otherwise using PBC. The array should be of size nframes x 9

atom_types

The atom types The list should contain natoms ints

atomic

Calculate the atomic energy and virial

fparam

The frame parameter. The array can be of size : - nframes x dim_fparam. - dim_fparam. Then all frames are assumed to be provided with the same fparam.

aparam

The atomic parameter The array can be of size : - nframes x natoms x dim_aparam. - natoms x dim_aparam. Then all frames are assumed to be provided with the same aparam. - dim_aparam. Then all frames and atoms are provided with the same aparam.

**kwargs

Other parameters

Returns:
output_dictdict

The output of the evaluation. The keys are the names of the output variables, and the values are the corresponding output arrays.

abstractmethod get_rcut() float[source]#

Get the cutoff radius of this model.

abstractmethod get_ntypes() int[source]#

Get the number of atom types of this model.

abstractmethod get_type_map() list[str][source]#

Get the type map (element name of the atom types) of this model.

abstractmethod get_dim_fparam() int[source]#

Get the number (dimension) of frame parameters of this DP.

has_default_fparam() bool[source]#

Check if the model has default frame parameters.

has_chg_spin_ebd() bool[source]#

Check if the model has charge spin embedding.

has_default_chg_spin() bool[source]#

Check if the model has default charge_spin values.

abstractmethod get_dim_aparam() int[source]#

Get the number (dimension) of atomic parameters of this DP.

abstractmethod eval_descriptor(coords: numpy.ndarray, cells: numpy.ndarray | None, atom_types: numpy.ndarray, fparam: numpy.ndarray | None = None, aparam: numpy.ndarray | None = None, efield: numpy.ndarray | None = None, mixed_type: bool = False, **kwargs: Any) numpy.ndarray[source]#

Evaluate descriptors by using this DP.

Parameters:
coords

The coordinates of atoms. The array should be of size nframes x natoms x 3

cells

The cell of the region. If None then non-PBC is assumed, otherwise using PBC. The array should be of size nframes x 9

atom_types

The atom types The list should contain natoms ints

fparam

The frame parameter. The array can be of size : - nframes x dim_fparam. - dim_fparam. Then all frames are assumed to be provided with the same fparam.

aparam

The atomic parameter The array can be of size : - nframes x natoms x dim_aparam. - natoms x dim_aparam. Then all frames are assumed to be provided with the same aparam. - dim_aparam. Then all frames and atoms are provided with the same aparam.

efield

The external field on atoms. The array should be of size nframes x natoms x 3

mixed_type

Whether to perform the mixed_type mode. If True, the input data has the mixed_type format (see doc/model/train_se_atten.md), in which frames in a system may have different natoms_vec(s), with the same nloc.

Returns:
descriptor

Descriptors.

abstractmethod eval_fitting_last_layer(coords: numpy.ndarray, cells: numpy.ndarray | None, atom_types: numpy.ndarray, fparam: numpy.ndarray | None = None, aparam: numpy.ndarray | None = None, **kwargs: Any) numpy.ndarray[source]#

Evaluate fitting before last layer by using this DP.

Parameters:
coords

The coordinates of atoms. The array should be of size nframes x natoms x 3

cells

The cell of the region. If None then non-PBC is assumed, otherwise using PBC. The array should be of size nframes x 9

atom_types

The atom types The list should contain natoms ints

fparam

The frame parameter. The array can be of size : - nframes x dim_fparam. - dim_fparam. Then all frames are assumed to be provided with the same fparam.

aparam

The atomic parameter The array can be of size : - nframes x natoms x dim_aparam. - natoms x dim_aparam. Then all frames are assumed to be provided with the same aparam. - dim_aparam. Then all frames and atoms are provided with the same aparam.

Returns:
fitting

Fitting output before last layer.

abstractmethod eval_typeebd() numpy.ndarray[source]#

Evaluate output of type embedding network by using this model.

Returns:
np.ndarray

The output of type embedding network. The shape is [ntypes, o_size], where ntypes is the number of types, and o_size is the number of nodes in the output layer.

Raises:
KeyError

If the model does not enable type embedding.

_check_mixed_types(atom_types: numpy.ndarray) bool[source]#

Check if atom types of all frames are the same.

Traditional descriptors like se_e2_a requires all the frames to have the same atom types.

Parameters:
atom_typesnp.ndarray

The atom types of all frames, in shape nframes * natoms.

property model_type: type[DeepEval][source]#
Abstractmethod:

The the evaluator of the model type.

abstractmethod get_sel_type() list[int][source]#

Get the selected atom types of this model.

Only atoms with selected atom types have atomic contribution to the result of the model. If returning an empty list, all atom types are selected.

abstractmethod get_numb_dos() int[source]#

Get the number of DOS.

get_has_efield() bool[source]#

Check if the model has efield.

get_has_spin() bool[source]#

Check if the model has spin atom types.

get_use_spin() list[bool][source]#

Get the per-type spin usage of this model.

Returns:
list[bool]

A list of bool indicating whether each atom type uses spin. Empty list if the model does not have spin.

get_has_hessian() bool[source]#

Check if the model has hessian.

abstractmethod get_var_name() str[source]#

Get the name of the fitting property.

abstractmethod get_ntypes_spin() int[source]#

Get the number of spin atom types of this model. Only used in old implement.

abstractmethod get_model_def_script() dict[source]#

Get model definition script.

abstractmethod get_model_size() dict[source]#

Get model parameter count.

abstractmethod get_observed_types() dict[source]#

Get observed types (elements) of the model during data statistics.

abstractmethod get_model() Any[source]#

Get the model module implemented by the deep learning framework.

For PyTorch, this returns the nn.Module. For Paddle, this returns the paddle.nn.Layer. For TensorFlow, this returns the graph. For dpmodel, this returns the BaseModel.

Returns:
model

The model module implemented by the deep learning framework.

class deepmd.infer.deep_eval.DeepEval(model_file: str, *args: Any, auto_batch_size: bool | int | deepmd.utils.batch_size.AutoBatchSize = True, neighbor_list: ase.neighborlist.NewPrimitiveNeighborList | None = None, **kwargs: Any)[source]#

Bases: abc.ABC

High-level Deep Evaluator interface.

The specific DeepEval, such as DeepPot and DeepTensor, should inherit from this class. This class provides a high-level interface on the top of the low-level interface.

Parameters:
model_filePath

The name of the frozen model file.

*argslist

Positional arguments.

auto_batch_sizebool or int or AutoBatchSize, default: True

If True, automatic batch size will be used. If int, it will be used as the initial batch size.

neighbor_listase.neighborlist.NewPrimitiveNeighborList, optional

The ASE neighbor list class to produce the neighbor list. If None, the neighbor list will be built natively in the model.

**kwargsdict

Keyword arguments.

deep_eval[source]#
property output_def: deepmd.dpmodel.output_def.ModelOutputDef[source]#
Abstractmethod:

Returns the output variable definitions.

get_rcut() float[source]#

Get the cutoff radius of this model.

get_ntypes() int[source]#

Get the number of atom types of this model.

get_type_map() list[str][source]#

Get the type map (element name of the atom types) of this model.

get_dim_fparam() int[source]#

Get the number (dimension) of frame parameters of this DP.

has_default_fparam() bool[source]#

Check if the model has default frame parameters.

has_chg_spin_ebd() bool[source]#

Check if the model has charge spin embedding.

has_default_chg_spin() bool[source]#

Check if the model has default charge_spin values.

get_dim_aparam() int[source]#

Get the number (dimension) of atomic parameters of this DP.

_get_natoms_and_nframes(coords: numpy.ndarray, atom_types: numpy.ndarray, mixed_type: bool = False) tuple[int, int][source]#
_expande_atype(atype: numpy.ndarray, nframes: int, mixed_type: bool) numpy.ndarray[source]#
eval_descriptor(coords: numpy.ndarray, cells: numpy.ndarray | None, atom_types: numpy.ndarray, fparam: numpy.ndarray | None = None, aparam: numpy.ndarray | None = None, mixed_type: bool = False, **kwargs: Any) numpy.ndarray[source]#

Evaluate descriptors by using this DP.

Parameters:
coords

The coordinates of atoms. The array should be of size nframes x natoms x 3

cells

The cell of the region. If None then non-PBC is assumed, otherwise using PBC. The array should be of size nframes x 9

atom_types

The atom types The list should contain natoms ints

fparam

The frame parameter. The array can be of size : - nframes x dim_fparam. - dim_fparam. Then all frames are assumed to be provided with the same fparam.

aparam

The atomic parameter The array can be of size : - nframes x natoms x dim_aparam. - natoms x dim_aparam. Then all frames are assumed to be provided with the same aparam. - dim_aparam. Then all frames and atoms are provided with the same aparam.

efield

The external field on atoms. The array should be of size nframes x natoms x 3

mixed_type

Whether to perform the mixed_type mode. If True, the input data has the mixed_type format (see doc/model/train_se_atten.md), in which frames in a system may have different natoms_vec(s), with the same nloc.

Returns:
descriptor

Descriptors.

eval_fitting_last_layer(coords: numpy.ndarray, cells: numpy.ndarray | None, atom_types: numpy.ndarray, fparam: numpy.ndarray | None = None, aparam: numpy.ndarray | None = None, mixed_type: bool = False, **kwargs: Any) numpy.ndarray[source]#

Evaluate fitting before last layer by using this DP.

Parameters:
coords

The coordinates of atoms. The array should be of size nframes x natoms x 3

cells

The cell of the region. If None then non-PBC is assumed, otherwise using PBC. The array should be of size nframes x 9

atom_types

The atom types The list should contain natoms ints

fparam

The frame parameter. The array can be of size : - nframes x dim_fparam. - dim_fparam. Then all frames are assumed to be provided with the same fparam.

aparam

The atomic parameter The array can be of size : - nframes x natoms x dim_aparam. - natoms x dim_aparam. Then all frames are assumed to be provided with the same aparam. - dim_aparam. Then all frames and atoms are provided with the same aparam.

efield

The external field on atoms. The array should be of size nframes x natoms x 3

mixed_type

Whether to perform the mixed_type mode. If True, the input data has the mixed_type format (see doc/model/train_se_atten.md), in which frames in a system may have different natoms_vec(s), with the same nloc.

Returns:
fitting

Fitting output before last layer.

eval_typeebd() numpy.ndarray[source]#

Evaluate output of type embedding network by using this model.

Returns:
np.ndarray

The output of type embedding network. The shape is [ntypes, o_size], where ntypes is the number of types, and o_size is the number of nodes in the output layer.

Raises:
KeyError

If the model does not enable type embedding.

See also

deepmd.tf.utils.type_embed.TypeEmbedNet

The type embedding network.

Examples

Get the output of type embedding network of graph.pb:

>>> from deepmd.infer import DeepPotential
>>> dp = DeepPotential("graph.pb")
>>> dp.eval_typeebd()
_standard_input(coords: numpy.ndarray | list, cells: numpy.ndarray | list | None, atom_types: numpy.ndarray | list, fparam: numpy.ndarray | list | None, aparam: numpy.ndarray | list | None, mixed_type: bool) tuple[numpy.ndarray, numpy.ndarray | None, numpy.ndarray, numpy.ndarray | None, numpy.ndarray | None][source]#
get_sel_type() list[int][source]#

Get the selected atom types of this model.

Only atoms with selected atom types have atomic contribution to the result of the model. If returning an empty list, all atom types are selected.

_get_sel_natoms(atype: numpy.ndarray) int[source]#
property has_efield: bool[source]#

Check if the model has efield.

property has_spin: bool[source]#

Check if the model has spin.

property use_spin: list[bool][source]#

Get the per-type spin usage of this model.

Returns:
list[bool]

A list of bool indicating whether each atom type uses spin. Empty list if the model does not have spin.

property has_hessian: bool[source]#

Check if the model has hessian.

get_ntypes_spin() int[source]#

Get the number of spin atom types of this model. Only used in old implement.

get_model_def_script() dict[source]#

Get model definition script.

get_model_size() dict[source]#

Get model parameter count.

get_observed_types() dict[source]#

Get observed types (elements) of the model during data statistics.

get_model() Any[source]#

Get the model module implemented by the deep learning framework.

For PyTorch, this returns the nn.Module. For Paddle, this returns the paddle.nn.Layer. For TensorFlow, this returns the graph. For dpmodel, this returns the BaseModel.

Returns:
model

The model module implemented by the deep learning framework.