DeePMD-kit Precision Control#
Overview#
DeePMD-kit exposes two independent layers of floating-point precision control:
DP_INTERFACE_PREC(environment variable) — governs the interface precision: the environmental matrix, descriptor statistics, fitting normalisation parameters, and the data boundary between components.precision(per-network training parameter) — governs the dtype used inside each neural network (descriptor, fitting net, type embedding) and subsequent operations on its output.
The reduced scalar output (e.g., total energy) is always float64, regardless of either setting .
DP_INTERFACE_PREC Environment Variable#
Set at Python import time in deepmd/env.py.
| Value | GLOBAL_NP_FLOAT_PRECISION | GLOBAL_ENER_FLOAT_PRECISION | global_float_prec |
|---|---|---|---|
high (default) | np.float64 | np.float64 | "double" |
low | np.float32 | np.float64 | "float" |
Any other value raises a RuntimeError .
GLOBAL_NP_FLOAT_PRECISION is the dtype used for the environmental matrix, descriptor mean/std statistics (davg/dstd), and atom-energy biases. GLOBAL_ENER_FLOAT_PRECISION remains float64 even in low mode, keeping reduced-energy outputs in double precision.
The "default" precision string in PRECISION_DICT resolves to GLOBAL_NP_FLOAT_PRECISION at import time , so changing DP_INTERFACE_PREC after import has no effect.
PRECISION_DICT — the Central Dtype Registry#
Defined in deepmd/dpmodel/common.py, this dict maps human-readable precision strings to NumPy (or ml_dtypes) dtype objects:
| String | dtype |
|---|---|
"float16" / "half" | np.float16 |
"float32" / "single" | np.float32 |
"float64" / "double" | np.float64 |
"bfloat16" | ml_dtypes.bfloat16 |
"default" | ← GLOBAL_NP_FLOAT_PRECISION |
"int32" / "int64" / "bool" | integer/bool types |
DEFAULT_PRECISION is hard-coded to "float64" and is used as the default value for all per-network precision constructor arguments.
Per-Network precision Parameter#
Each major model component accepts a precision string in its constructor. The string is validated against PRECISION_DICT and resolved to a dtype:
- Descriptor (e.g.,
DescrptSeA,DPA-1,DPA-2,DPA-3): controls dtype fordavg/dstdstatistics arrays and post-network operations. - Fitting net (
GeneralFitting): controls dtype for frame/atomic parameter statistics (fparam_avg,aparam_inv_std, etc.) and layer weight initialisation. Stored as bothself.precision(string, for serialization) andself.prec = PRECISION_DICT[self.precision.lower()](dtype, for array ops). - Type embedding (
TypeEmbedNet): controls dtype for the embedding table and eye-matrix construction during type-map changes.
The precision string propagates from these top-level components down through FittingNet/EmbeddingNet/NativeLayer constructors unchanged, so all weight tensors in a network share the same dtype.
cast_precision decorator#
cast_precision in common.py is a method decorator used to bridge the interface (global) precision and per-network precision:
- Casts inputs from
"global"→self.precisionbefore the forward pass. - Casts outputs from
self.precision→"global"after the forward pass. - Only affects tensors whose current dtype matches the source precision; integer/bool arrays are left untouched.
The get_xp_precision helper translates precision strings to array-API–compatible dtype objects (supporting NumPy, JAX, and other backends).
Recommended Configurations#
From the official precision docs :
| Use case | DP_INTERFACE_PREC | per-network precision |
|---|---|---|
| Default / highest accuracy | high | float64 |
| Faster training / lower memory | high | float32 |
Note: The Python and C++ inference interfaces accept both
float64andfloat32inputs regardless of the model's internal precision. MD programs such as LAMMPS operate infloat64.
Key Source Files#
| File | Role |
|---|---|
deepmd/env.py | Reads DP_INTERFACE_PREC; sets GLOBAL_NP_FLOAT_PRECISION, GLOBAL_ENER_FLOAT_PRECISION, global_float_prec |
deepmd/dpmodel/common.py | PRECISION_DICT, DEFAULT_PRECISION, cast_precision, get_xp_precision |
deepmd/dpmodel/fitting/general_fitting.py | Consumes precision in fitting net construction and array initialization |
doc/model/precision.md | Official documentation explaining the two-layer precision model |
deepmd/dpmodel/utils/network.py | NativeLayer, EmbeddingNet, FittingNet — propagate precision to weight tensors |