Skip to content

Commit 8e43346

Browse files
author
Han Wang
committed
fix similar issues in models
1 parent 14f3d16 commit 8e43346

4 files changed

Lines changed: 21 additions & 5 deletions

File tree

deepmd/dpmodel/array_api.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@ def xp_take_along_axis(arr: Array, indices: Array, axis: int) -> Array:
5151
else:
5252
indices = xp.reshape(indices, (0, 0))
5353

54-
offset = (xp.arange(indices.shape[0], dtype=indices.dtype) * m)[:, xp.newaxis]
54+
dev = array_api_compat.device(indices)
55+
offset = (xp.arange(indices.shape[0], dtype=indices.dtype, device=dev) * m)[
56+
:, xp.newaxis
57+
]
5558
indices = xp.reshape(offset + indices, (-1,))
5659

5760
out = xp.take(arr, indices)

deepmd/dpmodel/atomic_model/linear_atomic_model.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,9 +337,11 @@ def _compute_weight(
337337
xp = array_api_compat.array_namespace(extended_coord, extended_atype, nlists_)
338338
nmodels = len(self.models)
339339
nframes, nloc, _ = nlists_[0].shape
340+
dev = array_api_compat.device(extended_coord)
340341
# the dtype of weights is the interface data type.
341342
return [
342-
xp.ones((nframes, nloc, 1), dtype=GLOBAL_NP_FLOAT_PRECISION) / nmodels
343+
xp.ones((nframes, nloc, 1), dtype=GLOBAL_NP_FLOAT_PRECISION, device=dev)
344+
/ nmodels
343345
for _ in range(nmodels)
344346
]
345347

deepmd/dpmodel/atomic_model/pairtab_atomic_model.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,11 @@ def forward_atomic(
220220
) # (nframes, nloc, nnei)
221221

222222
# (nframes, nloc, nnei), index type is int64.
223+
dev = array_api_compat.device(extended_atype)
223224
j_type = extended_atype[
224-
xp.arange(extended_atype.shape[0], dtype=xp.int64)[:, None, None],
225+
xp.arange(extended_atype.shape[0], dtype=xp.int64, device=dev)[
226+
:, None, None
227+
],
225228
masked_nlist,
226229
]
227230

@@ -327,8 +330,11 @@ def _get_pairwise_dist(coords: Array, nlist: Array) -> Array:
327330
The pairwise distance between the atoms (nframes, nloc, nnei).
328331
"""
329332
xp = array_api_compat.array_namespace(coords, nlist)
333+
dev = array_api_compat.device(nlist)
330334
# index type is int64
331-
batch_indices = xp.arange(nlist.shape[0], dtype=xp.int64)[:, None, None]
335+
batch_indices = xp.arange(nlist.shape[0], dtype=xp.int64, device=dev)[
336+
:, None, None
337+
]
332338
neighbor_atoms = coords[batch_indices, nlist]
333339
loc_atoms = coords[:, : nlist.shape[1], :]
334340
pairwise_dr = loc_atoms[:, :, None, :] - neighbor_atoms

deepmd/dpmodel/model/make_model.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,12 @@ def _format_nlist(
540540
ret = xp.concat(
541541
[
542542
nlist,
543-
-1 * xp.ones([n_nf, n_nloc, nnei - n_nnei], dtype=nlist.dtype),
543+
-1
544+
* xp.ones(
545+
[n_nf, n_nloc, nnei - n_nnei],
546+
dtype=nlist.dtype,
547+
device=array_api_compat.device(nlist),
548+
),
544549
],
545550
axis=-1,
546551
)

0 commit comments

Comments
 (0)