Skip to content

Commit

Permalink
allow loading either nsel or natoms atomic tensor data
Browse files Browse the repository at this point in the history
A new parameter "output_natoms_for_type_sel" is added. (default=false)
If sel_types is given, output_natoms_for_type_sel is true, and the data dimesion is nsel, it will be converted to natoms.
If sel_types is given, output_natoms_for_type_sel is false, and the data dimesion is natoms, it will be converted to nsel.
In other situations, it keeps the original shape.

Signed-off-by: Jinzhe Zeng <[email protected]>
  • Loading branch information
njzjz committed Mar 2, 2024
1 parent 266acc1 commit 61f627e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 7 deletions.
4 changes: 4 additions & 0 deletions deepmd/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ def add_data_requirement(
repeat: int = 1,
default: float = 0.0,
dtype: Optional[np.dtype] = None,
output_natoms_for_type_sel: bool = False,
):
"""Specify data requirements for training.
Expand All @@ -103,6 +104,8 @@ def add_data_requirement(
default value of data
dtype : np.dtype, optional
the dtype of data, overwrites `high_prec` if provided
output_natoms_for_type_sel : bool, optional
if True and type_sel is True, the atomic dimension will be natoms instead of nsel
"""
data_requirement[key] = {
"ndof": ndof,
Expand All @@ -113,6 +116,7 @@ def add_data_requirement(
"repeat": repeat,
"default": default,
"dtype": dtype,
"output_natoms_for_type_sel": output_natoms_for_type_sel,
}


Expand Down
1 change: 1 addition & 0 deletions deepmd/pt/utils/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,5 @@ def add_data_requirement(self, data_requirement: List[DataRequirementItem]):
repeat=data_item["repeat"],
default=data_item["default"],
dtype=data_item["dtype"],
output_natoms_for_type_sel=data_item["output_natoms_for_type_sel"],
)
27 changes: 20 additions & 7 deletions deepmd/utils/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ def add(
repeat: int = 1,
default: float = 0.0,
dtype: Optional[np.dtype] = None,
output_natoms_for_type_sel: bool = False,
):
"""Add a data item that to be loaded.
Expand All @@ -173,6 +174,8 @@ def add(
default value of data
dtype : np.dtype, optional
the dtype of data, overwrites `high_prec` if provided
output_natoms_for_type_sel : bool, optional
if True and type_sel is True, the atomic dimension will be natoms instead of nsel
"""
self.data_dict[key] = {
"ndof": ndof,
Expand All @@ -184,6 +187,7 @@ def add(
"reduce": None,
"default": default,
"dtype": dtype,
"output_natoms_for_type_sel": output_natoms_for_type_sel,
}
return self

Expand Down Expand Up @@ -523,6 +527,9 @@ def _load_set(self, set_name: DPPath):
repeat=self.data_dict[kk]["repeat"],
default=self.data_dict[kk]["default"],
dtype=self.data_dict[kk]["dtype"],
output_natoms_for_type_sel=self.data_dict[kk][
"output_natoms_for_type_sel"
],
)
for kk in self.data_dict.keys():
if self.data_dict[kk]["reduce"] is not None:
Expand Down Expand Up @@ -600,6 +607,9 @@ def _load_data(
for jj in type_sel:
natoms_sel += np.sum(self.atom_type == jj)
idx_map_sel = self._idx_map_sel(self.atom_type, type_sel)

Check warning on line 609 in deepmd/utils/data.py

View check run for this annotation

Codecov / codecov/patch

deepmd/utils/data.py#L608-L609

Added lines #L608 - L609 were not covered by tests
else:
natoms_sel = natoms
idx_map_sel = idx_map

Check warning on line 612 in deepmd/utils/data.py

View check run for this annotation

Codecov / codecov/patch

deepmd/utils/data.py#L611-L612

Added lines #L611 - L612 were not covered by tests
ndof = ndof_ * natoms
else:
ndof = ndof_
Expand All @@ -618,18 +628,21 @@ def _load_data(
if atomic:
if type_sel is not None:

Check warning on line 629 in deepmd/utils/data.py

View check run for this annotation

Codecov / codecov/patch

deepmd/utils/data.py#L629

Added line #L629 was not covered by tests
# check the data shape is nsel or natoms
if data.size == nframes * natoms_sel * ndof:
if data.size == nframes * natoms_sel * ndof_:
if output_natoms_for_type_sel:
tmp = np.zeros(

Check warning on line 633 in deepmd/utils/data.py

View check run for this annotation

Codecov / codecov/patch

deepmd/utils/data.py#L631-L633

Added lines #L631 - L633 were not covered by tests
[nframes, natoms, ndof], dtype=data.dtype
[nframes, natoms, ndof_], dtype=data.dtype
)
sel_mask = np.isin(self.atom_type, type_sel)
tmp[:, sel_mask] = data.reshape([nframes, -1])
tmp[:, sel_mask] = data.reshape(

Check warning on line 637 in deepmd/utils/data.py

View check run for this annotation

Codecov / codecov/patch

deepmd/utils/data.py#L636-L637

Added lines #L636 - L637 were not covered by tests
[nframes, natoms_sel, ndof_]
)
data = tmp

Check warning on line 640 in deepmd/utils/data.py

View check run for this annotation

Codecov / codecov/patch

deepmd/utils/data.py#L640

Added line #L640 was not covered by tests
else:
natoms = natoms_sel
idx_map = idx_map_sel
ndof = ndof_ * natoms
elif data.size == nframes * natoms * ndof:
elif data.size == nframes * natoms * ndof_:
if output_natoms_for_type_sel:
pass

Check warning on line 647 in deepmd/utils/data.py

View check run for this annotation

Codecov / codecov/patch

deepmd/utils/data.py#L642-L647

Added lines #L642 - L647 were not covered by tests
else:
Expand All @@ -642,8 +655,8 @@ def _load_data(
raise ValueError(

Check warning on line 655 in deepmd/utils/data.py

View check run for this annotation

Codecov / codecov/patch

deepmd/utils/data.py#L655

Added line #L655 was not covered by tests
f"The shape of the data {key} in {set_name}"
f"is {data.shape}, which doesn't match either"
f"({nframes}, {natoms_sel}, {ndof}) or"
f"({nframes}, {natoms}, {ndof})"
f"({nframes}, {natoms_sel}, {ndof_}) or"
f"({nframes}, {natoms}, {ndof_})"
)
data = data.reshape([nframes, natoms, -1])
data = data[:, idx_map, :]
Expand All @@ -653,7 +666,7 @@ def _load_data(
explanation = "This error may occur when your label mismatch it's name, i.e. you might store global tensor in `atomic_tensor.npy` or atomic tensor in `tensor.npy`."
log.error(str(err_message))
log.error(explanation)
raise ValueError(str(err_message) + ". " + explanation)
raise ValueError(str(err_message) + ". " + explanation) from err_message

Check warning on line 669 in deepmd/utils/data.py

View check run for this annotation

Codecov / codecov/patch

deepmd/utils/data.py#L669

Added line #L669 was not covered by tests
if repeat != 1:
data = np.repeat(data, repeat).reshape([nframes, -1])
return np.float32(1.0), data
Expand Down
4 changes: 4 additions & 0 deletions deepmd/utils/data_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ def add_dict(self, adict: dict) -> None:
type_sel=adict[kk]["type_sel"],
repeat=adict[kk]["repeat"],
default=adict[kk]["default"],
output_natoms_for_type_sel=adict[kk]["output_natoms_for_type_sel"],
)

def add(
Expand All @@ -305,6 +306,7 @@ def add(
type_sel: Optional[List[int]] = None,
repeat: int = 1,
default: float = 0.0,
output_natoms_for_type_sel: bool = False,
):
"""Add a data item that to be loaded.
Expand All @@ -329,6 +331,8 @@ def add(
The data will be repeated `repeat` times.
default, default=0.
Default value of data
output_natoms_for_type_sel : bool
If True and type_sel is True, the atomic dimension will be natoms instead of nsel
"""
for ii in self.data_systems:
ii.add(
Expand Down

0 comments on commit 61f627e

Please sign in to comment.