Skip to content

Commit 48ecc68

Browse files
committed
fix: miscalculation of num_steps when using num_epoch and lmdb
1 parent 27a18b6 commit 48ecc68

4 files changed

Lines changed: 47 additions & 10 deletions

File tree

deepmd/dpmodel/utils/lmdb_data.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -751,11 +751,17 @@ def set_noise(self, noise_settings: dict[str, Any]) -> None:
751751
@property
752752
def index(self) -> list[int]:
753753
"""Number of batches per system (single system)."""
754-
return [max(1, self.nframes // self.batch_size)]
754+
return [self.total_batch]
755755

756756
@property
757757
def total_batch(self) -> int:
758-
return self.index[0]
758+
if self.mixed_batch:
759+
return math.ceil(self.nframes / self.batch_size) if self.nframes else 0
760+
total = 0
761+
for nloc, indices in self._nloc_groups.items():
762+
bs = self.get_batch_size_for_nloc(nloc)
763+
total += (len(indices) + bs - 1) // bs
764+
return total
759765

760766
@property
761767
def batch_sizes(self) -> list[int]:
@@ -1304,10 +1310,13 @@ def _partition_batches(self, all_batches: list[list[int]]) -> list[list[int]]:
13041310

13051311
def __len__(self) -> int:
13061312
"""Number of batches for this rank."""
1307-
total = 0
1308-
for nloc, indices in self._reader.nloc_groups.items():
1309-
bs = self._reader.get_batch_size_for_nloc(nloc)
1310-
total += (len(indices) + bs - 1) // bs
1313+
total = len(
1314+
SameNlocBatchSampler(
1315+
self._reader,
1316+
shuffle=False,
1317+
block_targets=self._block_targets,
1318+
)
1319+
)
13111320
return math.ceil(total / self._world_size)
13121321

13131322
@property

deepmd/pt/train/training.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,7 @@ def get_lr(lr_params: dict[str, Any]) -> BaseLR:
649649
if self.num_epoch <= 0:
650650
raise ValueError("training.num_epoch must be positive.")
651651
if isinstance(training_data, LmdbDataset):
652-
total_numb_batch = training_data.total_batch
652+
total_numb_batch = len(self.training_dataloader)
653653
else:
654654
sampler_weights = to_numpy_array(
655655
self.training_dataloader.sampler.weights
@@ -678,7 +678,7 @@ def get_lr(lr_params: dict[str, Any]) -> BaseLR:
678678
)
679679
for model_key in self.model_keys:
680680
if isinstance(training_data[model_key], LmdbDataset):
681-
per_task_total.append(training_data[model_key].total_batch)
681+
per_task_total.append(len(self.training_dataloader[model_key]))
682682
else:
683683
sampler_weights = to_numpy_array(
684684
self.training_dataloader[model_key].sampler.weights

deepmd/pt/utils/lmdb_dataset.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,11 +311,11 @@ def set_noise(self, noise_settings: dict[str, Any]) -> None:
311311

312312
@property
313313
def index(self) -> list[int]:
314-
return self._reader.index
314+
return [self.total_batch]
315315

316316
@property
317317
def total_batch(self) -> int:
318-
return self._reader.total_batch
318+
return len(self._batch_sampler)
319319

320320
@property
321321
def batch_sizes(self) -> list[int]:

source/tests/pt/test_lmdb_dataloader.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,34 @@ def test_dataset_auto_prob_iteration(self, auto_prob_lmdb):
624624
count = sum(len(batch) for batch in ds._batch_sampler)
625625
assert count > 300 # expanded
626626

627+
def test_total_batch_matches_auto_prob_sampler(self, auto_prob_lmdb):
628+
ds = LmdbDataset(
629+
auto_prob_lmdb,
630+
type_map=["O", "H"],
631+
batch_size=4,
632+
auto_prob_style="prob_sys_size;0:1:0.5;1:3:0.5",
633+
)
634+
assert ds.total_batch == len(ds._batch_sampler)
635+
636+
def test_distributed_len_includes_auto_prob_expansion(self, auto_prob_lmdb):
637+
import math
638+
639+
ds = LmdbDataset(
640+
auto_prob_lmdb,
641+
type_map=["O", "H"],
642+
batch_size=4,
643+
auto_prob_style="prob_sys_size;0:1:0.5;1:3:0.5",
644+
)
645+
global_batches = len(ds._batch_sampler)
646+
dist_sampler = DistributedSameNlocBatchSampler(
647+
ds._reader,
648+
rank=0,
649+
world_size=2,
650+
shuffle=False,
651+
block_targets=ds._block_targets,
652+
)
653+
assert len(dist_sampler) == math.ceil(global_batches / 2)
654+
627655

628656
class TestMergeLmdbSystemIds:
629657
"""Test merge_lmdb propagates frame_system_ids."""

0 commit comments

Comments
 (0)