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: improve nlist performance #3425

Merged
merged 3 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion deepmd/pt/train/training.py
Original file line number Diff line number Diff line change
Expand Up @@ -950,7 +950,7 @@
batch_data = next(iter(self.validation_data[task_key]))

for key in batch_data.keys():
if key == "sid" or key == "fid":
if key == "sid" or key == "fid" or key == "box":

Check warning on line 953 in deepmd/pt/train/training.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/train/training.py#L953

Added line #L953 was not covered by tests
continue
elif not isinstance(batch_data[key], list):
if batch_data[key] is not None:
Expand Down
33 changes: 18 additions & 15 deletions deepmd/pt/utils/nlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@
):
nframes, nloc = atype.shape[:2]
if box is not None:
box_gpu = box.to(coord.device, non_blocking=True)

Check warning on line 30 in deepmd/pt/utils/nlist.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/utils/nlist.py#L30

Added line #L30 was not covered by tests
coord_normalized = normalize_coord(
coord.view(nframes, nloc, 3),
box.reshape(nframes, 3, 3),
box_gpu.reshape(nframes, 3, 3),
)
else:
box_gpu = None

Check warning on line 36 in deepmd/pt/utils/nlist.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/utils/nlist.py#L36

Added line #L36 was not covered by tests
coord_normalized = coord.clone()
extended_coord, extended_atype, mapping = extend_coord_with_ghosts(
coord_normalized, atype, box, rcut
coord_normalized, atype, box_gpu, rcut, box
)
nlist = build_neighbor_list(
extended_coord,
Expand Down Expand Up @@ -262,6 +264,7 @@
atype: torch.Tensor,
cell: Optional[torch.Tensor],
rcut: float,
cell_cpu: Optional[torch.Tensor] = None,
):
"""Extend the coordinates of the atoms by appending peridoc images.
The number of images is large enough to ensure all the neighbors
Expand All @@ -277,6 +280,8 @@
simulation cell tensor of shape [-1, 9].
rcut : float
the cutoff radius
cell_cpu : torch.Tensor
cell on cpu for performance

Returns
-------
Expand All @@ -299,27 +304,25 @@
else:
coord = coord.view([nf, nloc, 3])
cell = cell.view([nf, 3, 3])
cell_cpu = cell_cpu.view([nf, 3, 3]) if cell_cpu is not None else cell

Check warning on line 307 in deepmd/pt/utils/nlist.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/utils/nlist.py#L307

Added line #L307 was not covered by tests
# nf x 3
to_face = to_face_distance(cell)
to_face = to_face_distance(cell_cpu)

Check warning on line 309 in deepmd/pt/utils/nlist.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/utils/nlist.py#L309

Added line #L309 was not covered by tests
# nf x 3
# *2: ghost copies on + and - directions
# +1: central cell
nbuff = torch.ceil(rcut / to_face).to(torch.long)
# 3
nbuff = torch.max(nbuff, dim=0, keepdim=False).values
xi = torch.arange(-nbuff[0], nbuff[0] + 1, 1, device=device)
yi = torch.arange(-nbuff[1], nbuff[1] + 1, 1, device=device)
zi = torch.arange(-nbuff[2], nbuff[2] + 1, 1, device=device)
xyz = xi.view(-1, 1, 1, 1) * torch.tensor(
[1, 0, 0], dtype=env.GLOBAL_PT_FLOAT_PRECISION, device=device
)
xyz = xyz + yi.view(1, -1, 1, 1) * torch.tensor(
[0, 1, 0], dtype=env.GLOBAL_PT_FLOAT_PRECISION, device=device
)
xyz = xyz + zi.view(1, 1, -1, 1) * torch.tensor(
[0, 0, 1], dtype=env.GLOBAL_PT_FLOAT_PRECISION, device=device
)
nbuff_cpu = nbuff.cpu()
xi = torch.arange(-nbuff_cpu[0], nbuff_cpu[0] + 1, 1, device="cpu")
yi = torch.arange(-nbuff_cpu[1], nbuff_cpu[1] + 1, 1, device="cpu")
zi = torch.arange(-nbuff_cpu[2], nbuff_cpu[2] + 1, 1, device="cpu")
eye_3 = torch.eye(3, dtype=env.GLOBAL_PT_FLOAT_PRECISION, device="cpu")
xyz = xi.view(-1, 1, 1, 1) * eye_3[0]
xyz = xyz + yi.view(1, -1, 1, 1) * eye_3[1]
xyz = xyz + zi.view(1, 1, -1, 1) * eye_3[2]

Check warning on line 323 in deepmd/pt/utils/nlist.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/utils/nlist.py#L316-L323

Added lines #L316 - L323 were not covered by tests
xyz = xyz.view(-1, 3)
xyz = xyz.to(device=device, non_blocking=True)

Check warning on line 325 in deepmd/pt/utils/nlist.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/utils/nlist.py#L325

Added line #L325 was not covered by tests
# ns x 3
shift_idx = xyz[torch.argsort(torch.norm(xyz, dim=1))]
ns, _ = shift_idx.shape
Expand Down
2 changes: 1 addition & 1 deletion deepmd/pt/utils/region.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
the internal coordinates

"""
rec_cell = torch.linalg.inv(cell)
rec_cell, _ = torch.linalg.inv_ex(cell)

Check warning on line 24 in deepmd/pt/utils/region.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/utils/region.py#L24

Added line #L24 was not covered by tests
return torch.matmul(coord, rec_cell)


Expand Down