Skip to content

Commit e5f2ef5

Browse files
committed
fix
1 parent aca34df commit e5f2ef5

4 files changed

Lines changed: 24 additions & 5 deletions

File tree

deepmd/pt/utils/utils.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,10 @@ def to_numpy_array(xx: torch.Tensor) -> np.ndarray: ...
227227
def to_numpy_array(xx: None) -> None: ...
228228

229229

230+
@overload
231+
def to_numpy_array(xx: float) -> np.ndarray: ...
232+
233+
230234
def to_numpy_array(
231235
xx: torch.Tensor | np.ndarray | float | None,
232236
) -> np.ndarray | None:

deepmd/tf/train/trainer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ def _build_lr(self) -> None:
244244
self.global_step = tf.train.get_or_create_global_step()
245245
if self.stop_batch == 0:
246246
# Use constant start_lr when stop_batch is zero (no training)
247-
self.learning_rate = tf.cast(self.lr.start_lr(), tf.float64)
247+
self.learning_rate = tf.cast(self.lr.start_lr(), GLOBAL_TF_FLOAT_PRECISION)
248248
log.info("built lr (constant start_lr for stop_batch=0)")
249249
else:
250250
self.learning_rate = self.lr.build(self.global_step, self.stop_batch)
@@ -809,7 +809,7 @@ def _get_place_holders(self, data_dict) -> None:
809809
prec = GLOBAL_ENER_FLOAT_PRECISION
810810
self.place_holders[kk] = tf.placeholder(prec, [None], name="t_" + kk)
811811
self.place_holders["find_" + kk] = tf.placeholder(
812-
tf.float32, name="t_find_" + kk
812+
GLOBAL_TF_FLOAT_PRECISION, name="t_find_" + kk
813813
)
814814

815815
def _init_from_frz_model(self) -> None:

deepmd/tf/utils/learning_rate.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ class LearningRateSchedule:
2121
"""
2222
TensorFlow wrapper for BaseLR.
2323
24+
The learning rate is computed via :func:`tf.numpy_function`, which prevents
25+
TensorFlow from optimizing this operation in the graph. This overhead is
26+
typically negligible compared to forward/backward passes.
27+
2428
Parameters
2529
----------
2630
params : dict[str, Any]
@@ -86,11 +90,18 @@ def build(self, global_step: tf.Tensor, num_steps: int) -> tf.Tensor:
8690
self._base_lr = BaseLR(**params)
8791

8892
# === Step 2. Bind a numpy_function for runtime evaluation ===
93+
from deepmd.tf.env import (
94+
GLOBAL_TF_FLOAT_PRECISION,
95+
)
96+
8997
def _lr_value(step: np.ndarray) -> np.ndarray:
90-
return np.asarray(self._base_lr.value(step), dtype=np.float64)
98+
return np.asarray(
99+
self._base_lr.value(step),
100+
dtype=GLOBAL_TF_FLOAT_PRECISION.as_numpy_dtype,
101+
)
91102

92103
lr = tf.numpy_function(
93-
_lr_value, [global_step], Tout=tf.float64, name="lr_schedule"
104+
_lr_value, [global_step], Tout=GLOBAL_TF_FLOAT_PRECISION, name="lr_schedule"
94105
)
95106
lr.set_shape(global_step.get_shape())
96107
return lr

source/tests/universal/dpmodel/utils/test_learning_rate.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,11 @@ def test_warmup_steps_exp(self) -> None:
9090
np.testing.assert_allclose(lr.value(0), 0.0, rtol=1e-10)
9191
np.testing.assert_allclose(lr.value(500), 0.5e-3, rtol=1e-10)
9292
np.testing.assert_allclose(lr.value(1000), 1e-3, rtol=1e-10)
93-
self.assertLess(to_numpy_array(lr.value(2000)), 1e-3)
93+
# Step 2000: 1000 steps into decay phase (1 decay period with decay_steps=1000)
94+
# lr = start_lr * decay_rate^1 = 1e-3 * exp(log(0.01)/9) ≈ 5.995e-4
95+
np.testing.assert_allclose(
96+
to_numpy_array(lr.value(2000)), 1e-3 * np.exp(np.log(0.01) / 9), rtol=1e-5
97+
)
9498

9599
def test_warmup_steps_cosine(self) -> None:
96100
"""Test warmup with cosine annealing."""

0 commit comments

Comments
 (0)