diff --git a/doc/development/create-a-model-pt.md b/doc/development/create-a-model-pt.md index 875067e2b8..8d2b1494b5 100644 --- a/doc/development/create-a-model-pt.md +++ b/doc/development/create-a-model-pt.md @@ -137,6 +137,15 @@ class SomeAtomicModel(BaseAtomicModel, torch.nn.Module): pass ``` +### Floating-point precision + +When creating a new component, the floating-point precision should obey the [Floating-point precision of the model](../model/precision.md) section. +In implementation, the component should + +- store parameters in the component precision, except those for output normalization; +- store output normalization parameters in {py:data}`deepmd.pt.utils.env.GLOBAL_PT_FLOAT_PRECISION`; +- before input normalization, cast the input tensor to the component precision; before output normalization, cast the output tensor to the {py:data}`deepmd.pt.utils.env.GLOBAL_PT_FLOAT_PRECISION`. + ## Register new arguments To let someone uses your new component in their input file, you need to create a new method that returns some `Argument` of your new component, and then register new arguments. For example, the code below diff --git a/doc/model/index.rst b/doc/model/index.rst index 8409d4ce97..c067ea4207 100644 --- a/doc/model/index.rst +++ b/doc/model/index.rst @@ -24,3 +24,4 @@ Model linear pairtab change-bias + precision diff --git a/doc/model/precision.md b/doc/model/precision.md new file mode 100644 index 0000000000..d8643c9c61 --- /dev/null +++ b/doc/model/precision.md @@ -0,0 +1,15 @@ +# Floating-point precision of the model + +The following options control the precision of the model: + +- The environment variable {envvar}`DP_INTERFACE_PREC` controls the interface precision of the model, the descriptor, and the fitting, the precision of the environmental matrix, and the precision of the normalized parameters for the environmental matrix and the fitting output. +- The training parameters {ref}`precision ` in the descriptor, the fitting, and the type embedding control the precision of neural networks in those components, and the subsequent operations after the output of neural networks. +- The reduced output (e.g. total energy) is always `float64`. + +Usually, the following two combinations of options are recommended: + +- Setting {envvar}`DP_INTERFACE_PREC` to `high` (default) and all {ref}`precision ` options to `float64` (default). +- Setting {envvar}`DP_INTERFACE_PREC` to `high` (default) and all {ref}`precision ` options to `float32`. + +The Python and C++ inference interfaces accept both `float64` and `float32` as the input and output arguments, whatever the floating-point precision of the model interface is. +Usually, the MD programs (such as LAMMPS) only use `float64` in their interfaces. diff --git a/doc/troubleshooting/precision.md b/doc/troubleshooting/precision.md index 5ebef97122..a754dbeb53 100644 --- a/doc/troubleshooting/precision.md +++ b/doc/troubleshooting/precision.md @@ -60,6 +60,7 @@ See [FAQ: How to tune Fitting/embedding-net size](./howtoset_netsize.md) for det In some cases, one may want to use the FP32 precision to make the model faster. For some applications, FP32 is enough and thus is recommended, but one should still be aware that the precision of FP32 is not as high as that of FP64. +See [Floating-point precision of the model](../model/precision.md) section for how to set the precision. ## Training