Skip to content

[WIP] pd: support dpa3 dynamic shape for pd backend #4828

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

Open
wants to merge 1 commit into
base: devel
Choose a base branch
from
Open
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
39 changes: 21 additions & 18 deletions deepmd/pd/model/descriptor/repflow_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@
# n_edge x e_dim
flat_edge_ebd = flat_edge_ebd * flat_sw.unsqueeze(-1)
# n_edge x 3 x e_dim
flat_h2g2 = (flat_h2[..., None] * flat_edge_ebd[:, None, :]).reshape(
flat_h2g2 = (flat_h2.unsqueeze(-1) * flat_edge_ebd.unsqueeze(-2)).reshape(

Check warning on line 375 in deepmd/pd/model/descriptor/repflow_layer.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pd/model/descriptor/repflow_layer.py#L375

Added line #L375 was not covered by tests
[-1, 3 * e_dim]
)
# nf x nloc x 3 x e_dim
Expand Down Expand Up @@ -586,7 +586,9 @@
sub_node_update = paddle.matmul(node_ebd, sub_node)
# n_angle * angle_dim
sub_node_update = paddle.index_select(
sub_node_update.reshape(nf * nloc, sub_node_update.shape[-1]), n2a_index, 0
sub_node_update.reshape([nf * nloc, sub_node_update.shape[-1]]),
n2a_index,
0,
)

# n_edge * angle_dim
Expand Down Expand Up @@ -666,7 +668,7 @@
sub_node_update = paddle.matmul(node_ebd, node)
# n_edge * node/edge_dim
sub_node_update = paddle.index_select(
sub_node_update.reshape(nf * nloc, sub_node_update.shape[-1]),
sub_node_update.reshape([nf * nloc, sub_node_update.shape[-1]]),
n2e_index,
0,
)
Expand All @@ -675,7 +677,7 @@
sub_node_ext_update = paddle.matmul(node_ebd_ext, node_ext)
# n_edge * node/edge_dim
sub_node_ext_update = paddle.index_select(
sub_node_ext_update.reshape(nf * nall, sub_node_update.shape[-1]),
sub_node_ext_update.reshape([nf * nall, sub_node_update.shape[-1]]),
n_ext2e_index,
0,
)
Expand All @@ -698,8 +700,8 @@
a_nlist: paddle.Tensor, # nf x nloc x a_nnei
a_nlist_mask: paddle.Tensor, # nf x nloc x a_nnei
a_sw: paddle.Tensor, # switch func, nf x nloc x a_nnei
edge_index: paddle.Tensor, # n_edge x 2
angle_index: paddle.Tensor, # n_angle x 3
edge_index: paddle.Tensor, # 2 x n_edge
angle_index: paddle.Tensor, # 3 x n_angle
):
"""
Parameters
Expand All @@ -724,12 +726,12 @@
Masks of the neighbor list for angle. real nei 1 otherwise 0
a_sw : nf x nloc x a_nnei
Switch function for angle.
edge_index : Optional for dynamic sel, n_edge x 2
edge_index : Optional for dynamic sel, 2 x n_edge
n2e_index : n_edge
Broadcast indices from node(i) to edge(ij), or reduction indices from edge(ij) to node(i).
n_ext2e_index : n_edge
Broadcast indices from extended node(j) to edge(ij).
angle_index : Optional for dynamic sel, n_angle x 3
angle_index : Optional for dynamic sel, 3 x n_angle
n2a_index : n_angle
Broadcast indices from extended node(j) to angle(ijk).
eij2a_index : n_angle
Expand All @@ -746,25 +748,24 @@
a_updated : nf x nloc x a_nnei x a_nnei x a_dim
Updated angle embedding.
"""
nb, nloc, nnei, _ = edge_ebd.shape
nb, nloc, nnei = nlist.shape
nall = node_ebd_ext.shape[1]
node_ebd = node_ebd_ext[:, :nloc, :]
n_edge = int(nlist_mask.sum().item())
if paddle.in_dynamic_mode():
assert [nb, nloc] == node_ebd.shape[:2]
if not self.use_dynamic_sel:
if paddle.in_dynamic_mode():
assert [nb, nloc, nnei, 3] == h2.shape
n_edge = None
else:
if paddle.in_dynamic_mode():
assert [n_edge, 3] == h2.shape
n_edge = h2.shape[0]

Check warning on line 761 in deepmd/pd/model/descriptor/repflow_layer.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pd/model/descriptor/repflow_layer.py#L761

Added line #L761 was not covered by tests
del a_nlist # may be used in the future

n2e_index, n_ext2e_index = edge_index[:, 0], edge_index[:, 1]
n2e_index, n_ext2e_index = edge_index[0], edge_index[1]
n2a_index, eij2a_index, eik2a_index = (
angle_index[:, 0],
angle_index[:, 1],
angle_index[:, 2],
angle_index[0],
angle_index[1],
angle_index[2],
)

# nb x nloc x nnei x n_dim [OR] n_edge x n_dim
Expand Down Expand Up @@ -896,7 +897,7 @@
n2e_index,
average=False,
num_owner=nb * nloc,
).reshape(nb, nloc, node_edge_update.shape[-1])
).reshape([nb, nloc, node_edge_update.shape[-1]])
/ self.dynamic_e_sel
)
)
Expand Down Expand Up @@ -1042,7 +1043,9 @@
if not self.use_dynamic_sel:
# nb x nloc x a_nnei x a_nnei x e_dim
weighted_edge_angle_update = (
a_sw[..., None, None] * a_sw[..., None, :, None] * edge_angle_update
a_sw.unsqueeze(-1).unsqueeze(-1)
* a_sw.unsqueeze(-2).unsqueeze(-1)
* edge_angle_update
)
# nb x nloc x a_nnei x e_dim
reduced_edge_angle_update = paddle.sum(
Expand Down
5 changes: 3 additions & 2 deletions deepmd/pd/model/descriptor/repflows.py
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,8 @@ def forward(
a_sw = (a_sw[:, :, :, None] * a_sw[:, :, None, :])[a_nlist_mask]
else:
# avoid jit assertion
edge_index = angle_index = paddle.zeros([1, 3], dtype=nlist.dtype)
edge_index = paddle.zeros([2, 1], dtype=nlist.dtype)
angle_index = paddle.zeros([3, 1], dtype=nlist.dtype)
# get edge and angle embedding
# nb x nloc x nnei x e_dim [OR] n_edge x e_dim
if not self.edge_init_use_dist:
Expand Down Expand Up @@ -566,7 +567,7 @@ def forward(
edge_ebd,
h2,
sw,
owner=edge_index[:, 0],
owner=edge_index[0],
num_owner=nframes * nloc,
nb=nframes,
nloc=nloc,
Expand Down
42 changes: 24 additions & 18 deletions deepmd/pd/model/network/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,24 @@
-------
output: [num_owner, feature_dim]
"""
bin_count = paddle.bincount(owners)
bin_count = bin_count.where(bin_count != 0, paddle.ones_like(bin_count))

if (num_owner is not None) and (bin_count.shape[0] != num_owner):
difference = num_owner - bin_count.shape[0]
bin_count = paddle.concat(
[bin_count, paddle.ones([difference], dtype=bin_count.dtype)]
)
if num_owner is None or average:

Check warning on line 32 in deepmd/pd/model/network/utils.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pd/model/network/utils.py#L32

Added line #L32 was not covered by tests
# requires bincount
bin_count = paddle.bincount(owners)
bin_count = bin_count.where(bin_count != 0, paddle.ones_like(bin_count))

Check warning on line 35 in deepmd/pd/model/network/utils.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pd/model/network/utils.py#L34-L35

Added lines #L34 - L35 were not covered by tests

if (num_owner is not None) and (bin_count.shape[0] != num_owner):
difference = num_owner - bin_count.shape[0]
bin_count = paddle.concat(

Check warning on line 39 in deepmd/pd/model/network/utils.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pd/model/network/utils.py#L37-L39

Added lines #L37 - L39 were not covered by tests
[bin_count, paddle.ones([difference], dtype=bin_count.dtype)]
)
else:
bin_count = None

Check warning on line 43 in deepmd/pd/model/network/utils.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pd/model/network/utils.py#L43

Added line #L43 was not covered by tests

# make sure this operation is done on the same device of data and owners
output = paddle.zeros([bin_count.shape[0], data.shape[1]])
output = paddle.zeros([num_owner, data.shape[1]])

Check warning on line 46 in deepmd/pd/model/network/utils.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pd/model/network/utils.py#L46

Added line #L46 was not covered by tests
output = output.index_add_(owners, 0, data)
if average:
assert bin_count is not None

Check warning on line 49 in deepmd/pd/model/network/utils.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pd/model/network/utils.py#L49

Added line #L49 was not covered by tests
output = (output.T / bin_count).T
return output

Expand All @@ -51,6 +56,7 @@
nlist_mask: paddle.Tensor,
a_nlist_mask: paddle.Tensor,
nall: int,
use_loc_mapping: bool = True,
):
"""
Get the index mapping for edge graph and angle graph, ready in `aggregate` or `index_select`.
Expand All @@ -68,12 +74,12 @@

Returns
-------
edge_index : n_edge x 2
edge_index : 2 x n_edge
n2e_index : n_edge
Broadcast indices from node(i) to edge(ij), or reduction indices from edge(ij) to node(i).
n_ext2e_index : n_edge
Broadcast indices from extended node(j) to edge(ij).
angle_index : n_angle x 3
angle_index : 3 x n_angle
n2a_index : n_angle
Broadcast indices from extended node(j) to angle(ijk).
eij2a_index : n_angle
Expand All @@ -100,7 +106,9 @@
n2e_index = n2e_index[nlist_mask] # graph node index, atom_graph[:, 0]

# node_ext(j) to edge(ij) index_select
frame_shift = paddle.arange(0, nf, dtype=nlist.dtype) * nall
frame_shift = paddle.arange(0, nf, dtype=nlist.dtype) * (

Check warning on line 109 in deepmd/pd/model/network/utils.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pd/model/network/utils.py#L109

Added line #L109 was not covered by tests
nall if not use_loc_mapping else nloc
)
shifted_nlist = nlist + frame_shift[:, None, None]
# n_edge
n_ext2e_index = shifted_nlist[nlist_mask] # graph neighbor index, atom_graph[:, 1]
Expand Down Expand Up @@ -129,9 +137,7 @@
# n_angle
eik2a_index = edge_index_ik[a_nlist_mask_3d]

return paddle.concat(
[n2e_index.unsqueeze(-1), n_ext2e_index.unsqueeze(-1)], axis=-1
), paddle.concat(
[n2a_index.unsqueeze(-1), eij2a_index.unsqueeze(-1), eik2a_index.unsqueeze(-1)],
axis=-1,
)
edge_index_result = paddle.stack([n2e_index, n_ext2e_index], axis=0)
angle_index_result = paddle.stack([n2a_index, eij2a_index, eik2a_index], axis=0)

Check warning on line 141 in deepmd/pd/model/network/utils.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pd/model/network/utils.py#L140-L141

Added lines #L140 - L141 were not covered by tests

return edge_index_result, angle_index_result

Check warning on line 143 in deepmd/pd/model/network/utils.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pd/model/network/utils.py#L143

Added line #L143 was not covered by tests