From 80337f72999e16e136367552d7e38eed4ace3d88 Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Sat, 2 Mar 2024 11:46:56 -0500 Subject: [PATCH 1/8] fix compute_smooth_weight nan output when rmin==rmax, the previous implementation of compute_smooth_weight will give all nan. In theory, it should not happen. Signed-off-by: Jinzhe Zeng --- deepmd/dpmodel/utils/env_mat.py | 8 +++++--- deepmd/pt/utils/preprocess.py | 2 +- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/deepmd/dpmodel/utils/env_mat.py b/deepmd/dpmodel/utils/env_mat.py index 0e861d9f38..423f26c227 100644 --- a/deepmd/dpmodel/utils/env_mat.py +++ b/deepmd/dpmodel/utils/env_mat.py @@ -20,9 +20,11 @@ def compute_smooth_weight( min_mask = distance <= rmin max_mask = distance >= rmax mid_mask = np.logical_not(np.logical_or(min_mask, max_mask)) - uu = (distance - rmin) / (rmax - rmin) - vv = uu * uu * uu * (-6.0 * uu * uu + 15.0 * uu - 10.0) + 1.0 - return vv * mid_mask + min_mask + with np.errstate(divide="ignore"): + uu = (distance - rmin) / (rmax - rmin) + with np.errstate(invalid="ignore"): + vv = uu * uu * uu * (-6.0 * uu * uu + 15.0 * uu - 10.0) + 1.0 + return np.where(mid_mask, vv, min_mask) def _make_env_mat( diff --git a/deepmd/pt/utils/preprocess.py b/deepmd/pt/utils/preprocess.py index 806bacdcd2..a29a2fbcff 100644 --- a/deepmd/pt/utils/preprocess.py +++ b/deepmd/pt/utils/preprocess.py @@ -233,7 +233,7 @@ def compute_smooth_weight(distance, rmin: float, rmax: float): mid_mask = torch.logical_not(torch.logical_or(min_mask, max_mask)) uu = (distance - rmin) / (rmax - rmin) vv = uu * uu * uu * (-6 * uu * uu + 15 * uu - 10) + 1 - return vv * mid_mask + min_mask + return torch.where(mid_mask, vv, min_mask) def make_env_mat( From 9e55925be114d7e75b6e3484b6e5ace207f05e54 Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Sat, 2 Mar 2024 11:54:29 -0500 Subject: [PATCH 2/8] dtype Signed-off-by: Jinzhe Zeng --- deepmd/dpmodel/utils/env_mat.py | 2 +- deepmd/pt/utils/preprocess.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/deepmd/dpmodel/utils/env_mat.py b/deepmd/dpmodel/utils/env_mat.py index 423f26c227..0fa51f5b83 100644 --- a/deepmd/dpmodel/utils/env_mat.py +++ b/deepmd/dpmodel/utils/env_mat.py @@ -24,7 +24,7 @@ def compute_smooth_weight( uu = (distance - rmin) / (rmax - rmin) with np.errstate(invalid="ignore"): vv = uu * uu * uu * (-6.0 * uu * uu + 15.0 * uu - 10.0) + 1.0 - return np.where(mid_mask, vv, min_mask) + return np.where(mid_mask, vv, min_mask.astype(distance.dtype)) def _make_env_mat( diff --git a/deepmd/pt/utils/preprocess.py b/deepmd/pt/utils/preprocess.py index a29a2fbcff..b8b59f48ac 100644 --- a/deepmd/pt/utils/preprocess.py +++ b/deepmd/pt/utils/preprocess.py @@ -233,7 +233,7 @@ def compute_smooth_weight(distance, rmin: float, rmax: float): mid_mask = torch.logical_not(torch.logical_or(min_mask, max_mask)) uu = (distance - rmin) / (rmax - rmin) vv = uu * uu * uu * (-6 * uu * uu + 15 * uu - 10) + 1 - return torch.where(mid_mask, vv, min_mask) + return torch.where(mid_mask, vv, min_mask.to(dtype=distance.dtype)) def make_env_mat( From 4ef1032ba4bcf4fb94a05a11d192257e79e1f2fe Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Mon, 4 Mar 2024 19:14:37 -0500 Subject: [PATCH 3/8] Revert "dtype" This reverts commit 9e55925be114d7e75b6e3484b6e5ace207f05e54. --- deepmd/dpmodel/utils/env_mat.py | 2 +- deepmd/pt/utils/preprocess.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/deepmd/dpmodel/utils/env_mat.py b/deepmd/dpmodel/utils/env_mat.py index 0fa51f5b83..423f26c227 100644 --- a/deepmd/dpmodel/utils/env_mat.py +++ b/deepmd/dpmodel/utils/env_mat.py @@ -24,7 +24,7 @@ def compute_smooth_weight( uu = (distance - rmin) / (rmax - rmin) with np.errstate(invalid="ignore"): vv = uu * uu * uu * (-6.0 * uu * uu + 15.0 * uu - 10.0) + 1.0 - return np.where(mid_mask, vv, min_mask.astype(distance.dtype)) + return np.where(mid_mask, vv, min_mask) def _make_env_mat( diff --git a/deepmd/pt/utils/preprocess.py b/deepmd/pt/utils/preprocess.py index b8b59f48ac..a29a2fbcff 100644 --- a/deepmd/pt/utils/preprocess.py +++ b/deepmd/pt/utils/preprocess.py @@ -233,7 +233,7 @@ def compute_smooth_weight(distance, rmin: float, rmax: float): mid_mask = torch.logical_not(torch.logical_or(min_mask, max_mask)) uu = (distance - rmin) / (rmax - rmin) vv = uu * uu * uu * (-6 * uu * uu + 15 * uu - 10) + 1 - return torch.where(mid_mask, vv, min_mask.to(dtype=distance.dtype)) + return torch.where(mid_mask, vv, min_mask) def make_env_mat( From c765cdbc1b81cabb75831f273d43821c3f7b96fa Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Mon, 4 Mar 2024 19:14:46 -0500 Subject: [PATCH 4/8] Revert "fix compute_smooth_weight nan output" This reverts commit 80337f72999e16e136367552d7e38eed4ace3d88. --- deepmd/dpmodel/utils/env_mat.py | 8 +++----- deepmd/pt/utils/preprocess.py | 2 +- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/deepmd/dpmodel/utils/env_mat.py b/deepmd/dpmodel/utils/env_mat.py index 423f26c227..0e861d9f38 100644 --- a/deepmd/dpmodel/utils/env_mat.py +++ b/deepmd/dpmodel/utils/env_mat.py @@ -20,11 +20,9 @@ def compute_smooth_weight( min_mask = distance <= rmin max_mask = distance >= rmax mid_mask = np.logical_not(np.logical_or(min_mask, max_mask)) - with np.errstate(divide="ignore"): - uu = (distance - rmin) / (rmax - rmin) - with np.errstate(invalid="ignore"): - vv = uu * uu * uu * (-6.0 * uu * uu + 15.0 * uu - 10.0) + 1.0 - return np.where(mid_mask, vv, min_mask) + uu = (distance - rmin) / (rmax - rmin) + vv = uu * uu * uu * (-6.0 * uu * uu + 15.0 * uu - 10.0) + 1.0 + return vv * mid_mask + min_mask def _make_env_mat( diff --git a/deepmd/pt/utils/preprocess.py b/deepmd/pt/utils/preprocess.py index a29a2fbcff..806bacdcd2 100644 --- a/deepmd/pt/utils/preprocess.py +++ b/deepmd/pt/utils/preprocess.py @@ -233,7 +233,7 @@ def compute_smooth_weight(distance, rmin: float, rmax: float): mid_mask = torch.logical_not(torch.logical_or(min_mask, max_mask)) uu = (distance - rmin) / (rmax - rmin) vv = uu * uu * uu * (-6 * uu * uu + 15 * uu - 10) + 1 - return torch.where(mid_mask, vv, min_mask) + return vv * mid_mask + min_mask def make_env_mat( From d8fefb409a9d81534571aa5e11bc7b9a548b42c6 Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Mon, 4 Mar 2024 19:16:17 -0500 Subject: [PATCH 5/8] throw erros when rmin is no less than rmax Signed-off-by: Jinzhe Zeng --- deepmd/dpmodel/utils/env_mat.py | 2 ++ deepmd/pt/utils/preprocess.py | 2 ++ 2 files changed, 4 insertions(+) diff --git a/deepmd/dpmodel/utils/env_mat.py b/deepmd/dpmodel/utils/env_mat.py index 0e861d9f38..5fb4ac4107 100644 --- a/deepmd/dpmodel/utils/env_mat.py +++ b/deepmd/dpmodel/utils/env_mat.py @@ -17,6 +17,8 @@ def compute_smooth_weight( rmax: float, ): """Compute smooth weight for descriptor elements.""" + if rmin >= rmax: + raise ValueError("rmin should be less than rmax.") min_mask = distance <= rmin max_mask = distance >= rmax mid_mask = np.logical_not(np.logical_or(min_mask, max_mask)) diff --git a/deepmd/pt/utils/preprocess.py b/deepmd/pt/utils/preprocess.py index 806bacdcd2..ed46292f84 100644 --- a/deepmd/pt/utils/preprocess.py +++ b/deepmd/pt/utils/preprocess.py @@ -228,6 +228,8 @@ def build_neighbor_list( def compute_smooth_weight(distance, rmin: float, rmax: float): """Compute smooth weight for descriptor elements.""" + if rmin >= rmax: + raise ValueError("rmin should be less than rmax.") min_mask = distance <= rmin max_mask = distance >= rmax mid_mask = torch.logical_not(torch.logical_or(min_mask, max_mask)) From 5392fcccbdf247612e5da4c12b1b11048dbd5fc4 Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Mon, 4 Mar 2024 21:28:01 -0500 Subject: [PATCH 6/8] set rcut=2.2 rcut_smth=0.4 Signed-off-by: Jinzhe Zeng --- .../tests/common/dpmodel/case_single_frame_with_nlist.py | 8 ++++---- source/tests/common/dpmodel/test_linear_atomic_model.py | 4 ++-- source/tests/pt/model/test_env_mat.py | 8 ++++---- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/source/tests/common/dpmodel/case_single_frame_with_nlist.py b/source/tests/common/dpmodel/case_single_frame_with_nlist.py index ecdf3590a8..584b7c562d 100644 --- a/source/tests/common/dpmodel/case_single_frame_with_nlist.py +++ b/source/tests/common/dpmodel/case_single_frame_with_nlist.py @@ -19,8 +19,8 @@ def setUp(self): self.cell = 2.0 * np.eye(3).reshape([1, 9]) # sel = [5, 2] self.sel = [5, 2] - self.rcut = 0.4 - self.rcut_smth = 2.2 + self.rcut_smth = 0.4 + self.rcut = 2.2 class TestCaseSingleFrameWithNlist: @@ -51,8 +51,8 @@ def setUp(self): ], dtype=int, ).reshape([1, self.nloc, sum(self.sel)]) - self.rcut = 0.4 - self.rcut_smth = 2.2 + self.rcut_smth = 0.4 + self.rcut = 2.2 # permutations self.perm = np.array([2, 0, 1, 3], dtype=np.int32) inv_perm = np.array([1, 2, 0, 3], dtype=np.int32) diff --git a/source/tests/common/dpmodel/test_linear_atomic_model.py b/source/tests/common/dpmodel/test_linear_atomic_model.py index 79eef46b8a..5136a374c6 100644 --- a/source/tests/common/dpmodel/test_linear_atomic_model.py +++ b/source/tests/common/dpmodel/test_linear_atomic_model.py @@ -119,8 +119,8 @@ def setUp(self, mock_loadtxt): ], dtype=int, ).reshape([1, self.nloc, sum(self.sel)]) - self.rcut = 0.4 - self.rcut_smth = 2.2 + self.rcut_smth = 0.4 + self.rcut = 2.2 file_path = "dummy_path" mock_loadtxt.return_value = np.array( diff --git a/source/tests/pt/model/test_env_mat.py b/source/tests/pt/model/test_env_mat.py index fee3fd6fea..771950c1c6 100644 --- a/source/tests/pt/model/test_env_mat.py +++ b/source/tests/pt/model/test_env_mat.py @@ -43,8 +43,8 @@ def setUp(self): ], dtype=int, ).reshape([1, self.nloc, sum(self.sel)]) - self.rcut = 0.4 - self.rcut_smth = 2.2 + self.rcut_smth = 0.4 + self.rcut = 2.2 # permutations self.perm = np.array([2, 0, 1, 3], dtype=np.int32) inv_perm = np.array([1, 2, 0, 3], dtype=np.int32) @@ -80,8 +80,8 @@ def setUp(self): self.cell = 2.0 * np.eye(3).reshape([1, 9]) # sel = [5, 2] self.sel = [5, 2] - self.rcut = 0.4 - self.rcut_smth = 2.2 + self.rcut_smth = 0.4 + self.rcut = 2.2 # to be merged with the tf test case From 7afa8d80f43deef3d6258d533bb699dc63309976 Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Mon, 4 Mar 2024 22:05:24 -0500 Subject: [PATCH 7/8] fix rcut_smth>rcut Signed-off-by: Jinzhe Zeng --- source/tests/common/dpmodel/test_linear_atomic_model.py | 4 ++-- source/tests/pt/model/test_linear_atomic_model.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/source/tests/common/dpmodel/test_linear_atomic_model.py b/source/tests/common/dpmodel/test_linear_atomic_model.py index 5136a374c6..ea51da9777 100644 --- a/source/tests/common/dpmodel/test_linear_atomic_model.py +++ b/source/tests/common/dpmodel/test_linear_atomic_model.py @@ -40,8 +40,8 @@ def test_pairwise(self, mock_loadtxt): nlist = np.array([[[1], [-1]]]) ds = DescrptSeA( - rcut=0.3, - rcut_smth=0.4, + rcut_smth=0.3, + rcut=0.4, sel=[3], ) ft = InvarFitting( diff --git a/source/tests/pt/model/test_linear_atomic_model.py b/source/tests/pt/model/test_linear_atomic_model.py index aae1a66618..ac70307e43 100644 --- a/source/tests/pt/model/test_linear_atomic_model.py +++ b/source/tests/pt/model/test_linear_atomic_model.py @@ -56,8 +56,8 @@ def test_pairwise(self, mock_loadtxt): nlist = torch.tensor([[[1], [-1]]], device=env.DEVICE) ds = DescrptSeA( - rcut=0.3, - rcut_smth=0.4, + rcut_smth=0.3, + rcut=0.4, sel=[3], ).to(env.DEVICE) ft = InvarFitting( From bb64fb6eef5802990a684d866373a793ec933e95 Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Tue, 5 Mar 2024 03:05:08 -0500 Subject: [PATCH 8/8] typo --- source/tests/common/dpmodel/case_single_frame_with_nlist.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/tests/common/dpmodel/case_single_frame_with_nlist.py b/source/tests/common/dpmodel/case_single_frame_with_nlist.py index ac9357ffc0..c260a18527 100644 --- a/source/tests/common/dpmodel/case_single_frame_with_nlist.py +++ b/source/tests/common/dpmodel/case_single_frame_with_nlist.py @@ -17,7 +17,7 @@ def setUp(self): ).reshape([1, self.nloc * 3]) self.atype = np.array([0, 0, 1], dtype=int).reshape([1, self.nloc]) self.cell = 2.0 * np.eye(3).reshape([1, 9]) - # sel = [5, 2]======= + # sel = [5, 2] self.sel = [16, 8] self.rcut = 2.2 self.rcut_smth = 0.4