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

pt: process frames in parallel for env mat stat #3293

Merged
merged 2 commits into from
Feb 19, 2024
Merged
Changes from all 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
49 changes: 19 additions & 30 deletions deepmd/pt/utils/env_mat_stat.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,38 +128,27 @@
# TODO: export rcut_smth from DescriptorBlock
self.descriptor.rcut_smth,
)
# reshape to nframes * nloc at the atom level,
# so nframes/mixed_type do not matter
env_mat = env_mat.view(
coord.shape[0], coord.shape[1], self.descriptor.get_nsel(), 4
coord.shape[0] * coord.shape[1], self.descriptor.get_nsel(), 4
)

if "real_natoms_vec" not in system:
end_indexes = torch.cumsum(natoms[0, 2:], 0)
start_indexes = torch.cat(
[
torch.zeros(1, dtype=torch.int32, device=env.DEVICE),
end_indexes[:-1],
]
)
for type_i in range(self.descriptor.get_ntypes()):
dd = env_mat[
:, start_indexes[type_i] : end_indexes[type_i], :, :
] # all descriptors for this element
env_mats = {}
env_mats[f"r_{type_i}"] = dd[:, :, :, :1]
env_mats[f"a_{type_i}"] = dd[:, :, :, 1:]
yield self.compute_stat(env_mats)
else:
for frame_item in range(env_mat.shape[0]):
dd_ff = env_mat[frame_item]
atype_frame = atype[frame_item]
for type_i in range(self.descriptor.get_ntypes()):
type_idx = atype_frame == type_i
dd = dd_ff[type_idx]
dd = dd.reshape([-1, 4]) # typen_atoms * nnei, 4
env_mats = {}
env_mats[f"r_{type_i}"] = dd[:, :1]
env_mats[f"a_{type_i}"] = dd[:, 1:]
yield self.compute_stat(env_mats)
atype = atype.view(coord.shape[0] * coord.shape[1])

Check warning on line 136 in deepmd/pt/utils/env_mat_stat.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/utils/env_mat_stat.py#L136

Added line #L136 was not covered by tests
# (1, nloc) eq (ntypes, 1), so broadcast is possible
# shape: (ntypes, nloc)
type_idx = torch.eq(

Check warning on line 139 in deepmd/pt/utils/env_mat_stat.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/utils/env_mat_stat.py#L139

Added line #L139 was not covered by tests
atype.view(1, -1),
torch.arange(
self.descriptor.get_ntypes(), device=env.DEVICE, dtype=torch.int32
).view(-1, 1),
)
for type_i in range(self.descriptor.get_ntypes()):
dd = env_mat[type_idx[type_i]]
dd = dd.reshape([-1, 4]) # typen_atoms * nnei, 4
env_mats = {}
env_mats[f"r_{type_i}"] = dd[:, :1]
env_mats[f"a_{type_i}"] = dd[:, 1:]
yield self.compute_stat(env_mats)

Check warning on line 151 in deepmd/pt/utils/env_mat_stat.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/utils/env_mat_stat.py#L145-L151

Added lines #L145 - L151 were not covered by tests

def get_hash(self) -> str:
"""Get the hash of the environment matrix.
Expand Down