Skip to content

Commit

Permalink
fix: fix seed with multiple ranks
Browse files Browse the repository at this point in the history
  • Loading branch information
njzjz authored Dec 19, 2024
1 parent c0914e1 commit b95718a
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 7 deletions.
2 changes: 1 addition & 1 deletion deepmd/pd/entrypoints/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def prepare_trainer_input_single(

# validation and training data
# avoid the same batch sequence among devices
rank_seed = (seed + rank) % (2**32) if seed is not None else None
rank_seed = [rank, seed % (2**32)] if seed is not None else None
validation_data_single = (
DpLoaderSet(
validation_systems,
Expand Down
2 changes: 1 addition & 1 deletion deepmd/pt/entrypoints/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def prepare_trainer_input_single(

# validation and training data
# avoid the same batch sequence among devices
rank_seed = (seed + rank) % (2**32) if seed is not None else None
rank_seed = [rank, seed % (2**32)] if seed is not None else None
validation_data_single = (
DpLoaderSet(
validation_systems,
Expand Down
6 changes: 4 additions & 2 deletions deepmd/pt/utils/dataloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@


def setup_seed(seed) -> None:
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
if isinstance(seed, (list, tuple)):
mixed_seed = mix_entropy(seed)
torch.manual_seed(mixed_seed)
torch.cuda.manual_seed_all(mixed_seed)
torch.backends.cudnn.deterministic = True
dp_random.seed(seed)

Expand Down
5 changes: 3 additions & 2 deletions deepmd/tf/entrypoints/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,10 @@ def _do_work(
seed = jdata["training"].get("seed", None)
if seed is not None:
# avoid the same batch sequence among workers
seed += run_opt.my_rank
seed = seed % (2**32)
dp_random.seed(seed)
dp_random.seed([run_opt.my_rank, seed])
else:
dp_random.seed(seed)

# setup data modifier
modifier = get_modifier(jdata["model"].get("modifier", None))
Expand Down
2 changes: 1 addition & 1 deletion deepmd/utils/random.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def random(size=None):
return _RANDOM_GENERATOR.random_sample(size)


def seed(val: Optional[int] = None) -> None:
def seed(val: Optional[Union[int, list[int]]] = None) -> None:
"""Seed the generator.
Parameters
Expand Down

0 comments on commit b95718a

Please sign in to comment.