Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(pt): use user seed in DpLoaderSet #4015

Merged
merged 5 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion deepmd/pt/entrypoints/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@
assert dist.is_nccl_available()
dist.init_process_group(backend="nccl")

def prepare_trainer_input_single(model_params_single, data_dict_single, rank=0):
def prepare_trainer_input_single(

Check warning on line 112 in deepmd/pt/entrypoints/main.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/entrypoints/main.py#L112

Added line #L112 was not covered by tests
model_params_single, data_dict_single, rank=0, seed=None
):
training_dataset_params = data_dict_single["training_data"]
validation_dataset_params = data_dict_single.get("validation_data", None)
validation_systems = (
Expand All @@ -134,11 +136,14 @@
stat_file_path_single = DPPath(stat_file_path_single, "a")

# validation and training data
# avoid the same batch sequence among devices
rank_seed = (seed + rank) % (2**32) if seed is not None else None

Check warning on line 140 in deepmd/pt/entrypoints/main.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/entrypoints/main.py#L140

Added line #L140 was not covered by tests
validation_data_single = (
DpLoaderSet(
validation_systems,
validation_dataset_params["batch_size"],
model_params_single["type_map"],
seed=rank_seed,
)
if validation_systems
else None
Expand All @@ -147,6 +152,7 @@
training_systems,
training_dataset_params["batch_size"],
model_params_single["type_map"],
seed=rank_seed,
)
return (
train_data_single,
Expand All @@ -155,6 +161,7 @@
)

rank = dist.get_rank() if dist.is_available() and dist.is_initialized() else 0
data_seed = config["training"].get("seed", None)

Check warning on line 164 in deepmd/pt/entrypoints/main.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/entrypoints/main.py#L164

Added line #L164 was not covered by tests
if not multi_task:
(
train_data,
Expand All @@ -164,6 +171,7 @@
config["model"],
config["training"],
rank=rank,
seed=data_seed,
)
else:
train_data, validation_data, stat_file_path = {}, {}, {}
Expand All @@ -176,6 +184,7 @@
config["model"]["model_dict"][model_key],
config["training"]["data_dict"][model_key],
rank=rank,
seed=data_seed,
)

trainer = training.Trainer(
Expand Down
5 changes: 0 additions & 5 deletions deepmd/pt/train/training.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,10 +274,6 @@ def get_lr(lr_params):
self.opt_type, self.opt_param = get_opt_param(training_params)

# Model
dp_random.seed(training_params["seed"])
if training_params["seed"] is not None:
torch.manual_seed(training_params["seed"])

self.model = get_model_for_wrapper(model_params)

# Loss
Expand All @@ -302,7 +298,6 @@ def get_lr(lr_params):
)

# Data
dp_random.seed(training_params["seed"])
if not self.multi_task:
self.get_sample_func = single_model_stat(
self.model,
Expand Down
3 changes: 2 additions & 1 deletion deepmd/pt/utils/dataloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@
seed=10,
iProzd marked this conversation as resolved.
Show resolved Hide resolved
shuffle=True,
):
setup_seed(seed)
if seed is not None:
setup_seed(seed)

Check warning on line 84 in deepmd/pt/utils/dataloader.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/utils/dataloader.py#L83-L84

Added lines #L83 - L84 were not covered by tests
if isinstance(systems, str):
with h5py.File(systems) as file:
systems = [os.path.join(systems, item) for item in file.keys()]
Expand Down