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

Chore: migrate and refactor polar and dos bias #3662

Merged
merged 28 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
774f2ce
chore: try rename to atom_
anyangml Apr 10, 2024
2aff780
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 10, 2024
d6a6571
fix: UTs
anyangml Apr 10, 2024
bd75e48
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 10, 2024
14a002c
Merge branch 'devel' into chore/migrate-bias
anyangml Apr 11, 2024
f0baf2e
fix: data shape
anyangml Apr 12, 2024
5300d98
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 12, 2024
90ede06
fix: var name
anyangml Apr 12, 2024
8f9dc5b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 12, 2024
d8d3f16
fix: var_name
anyangml Apr 12, 2024
ed5c543
fix: loss name
anyangml Apr 15, 2024
88da7ce
fix: dp var name
anyangml Apr 15, 2024
7176a39
fix: dp var name
anyangml Apr 15, 2024
3136c10
Merge branch 'devel' into chore/migrate-bias
anyangml Apr 16, 2024
88e41e5
chore: remove bias in fitting
anyangml Apr 16, 2024
c94608a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 16, 2024
23c7fdf
chore: remove UTs
anyangml Apr 16, 2024
ead2a38
fix: UT import
anyangml Apr 16, 2024
ec89624
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 16, 2024
5698bfc
chore: move polar bias
anyangml Apr 17, 2024
3f11f7b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 17, 2024
d7036b8
feat: add UT on out_std
anyangml Apr 17, 2024
09d775d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 17, 2024
cd2e78e
Merge branch 'devel' into chore/migrate-bias
anyangml Apr 17, 2024
0eacfe9
fix: UTs
anyangml Apr 18, 2024
c0c08ef
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 18, 2024
ae709c4
fix: UTs
anyangml Apr 18, 2024
e57dd7d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 18, 2024
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
8 changes: 4 additions & 4 deletions deepmd/entrypoints/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -897,7 +897,7 @@
polar = polar.reshape((polar.shape[0], -1, 9))[:, sel_mask, :].reshape(
(polar.shape[0], -1)
)
rmse_f = rmse(polar - test_data["atomic_polarizability"][:numb_test])
rmse_f = rmse(polar - test_data["atom_polarizability"][:numb_test])

Check warning on line 900 in deepmd/entrypoints/test.py

View check run for this annotation

Codecov / codecov/patch

deepmd/entrypoints/test.py#L900

Added line #L900 was not covered by tests

log.info(f"# number of test data : {numb_test:d} ")
log.info(f"Polarizability RMSE : {rmse_f:e}")
Expand Down Expand Up @@ -926,7 +926,7 @@
pe = np.concatenate(
(
np.reshape(
test_data["atomic_polarizability"][:numb_test],
test_data["atom_polarizability"][:numb_test],
[-1, 9 * sel_natoms],
),
np.reshape(polar, [-1, 9 * sel_natoms]),
Expand Down Expand Up @@ -1037,7 +1037,7 @@
dipole = dipole.reshape((dipole.shape[0], -1, 3))[:, sel_mask, :].reshape(
(dipole.shape[0], -1)
)
rmse_f = rmse(dipole - test_data["atomic_dipole"][:numb_test])
rmse_f = rmse(dipole - test_data["atom_dipole"][:numb_test])

Check warning on line 1040 in deepmd/entrypoints/test.py

View check run for this annotation

Codecov / codecov/patch

deepmd/entrypoints/test.py#L1040

Added line #L1040 was not covered by tests

log.info(f"# number of test data : {numb_test:d}")
log.info(f"Dipole RMSE : {rmse_f:e}")
Expand All @@ -1061,7 +1061,7 @@
pe = np.concatenate(
(
np.reshape(
test_data["atomic_dipole"][:numb_test], [-1, 3 * sel_natoms]
test_data["atom_dipole"][:numb_test], [-1, 3 * sel_natoms]
),
np.reshape(dipole, [-1, 3 * sel_natoms]),
),
Expand Down
6 changes: 3 additions & 3 deletions deepmd/pt/loss/tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,14 @@
if (
self.has_local_weight
and self.tensor_name in model_pred
and "atomic_" + self.label_name in label
and "atom_" + self.label_name in label
):
find_local = label.get("find_" + "atomic_" + self.label_name, 0.0)
find_local = label.get("find_" + "atom_" + self.label_name, 0.0)

Check warning on line 98 in deepmd/pt/loss/tensor.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/loss/tensor.py#L98

Added line #L98 was not covered by tests
local_weight = self.local_weight * find_local
local_tensor_pred = model_pred[self.tensor_name].reshape(
[-1, natoms, self.tensor_size]
)
local_tensor_label = label["atomic_" + self.label_name].reshape(
local_tensor_label = label["atom_" + self.label_name].reshape(

Check warning on line 103 in deepmd/pt/loss/tensor.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/loss/tensor.py#L103

Added line #L103 was not covered by tests
[-1, natoms, self.tensor_size]
)
diff = (local_tensor_pred - local_tensor_label).reshape(
Expand Down
4 changes: 2 additions & 2 deletions deepmd/pt/model/task/polarizability.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,9 @@
for sys in range(len(sampled)):
nframs = sampled[sys]["atype"].shape[0]

if sampled[sys]["find_atomic_polarizability"] > 0.0:
if sampled[sys]["find_atom_polarizability"] > 0.0:

Check warning on line 234 in deepmd/pt/model/task/polarizability.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/task/polarizability.py#L234

Added line #L234 was not covered by tests
sys_atom_polar = compute_stats_from_atomic(
sampled[sys]["atomic_polarizability"].numpy(force=True),
sampled[sys]["atom_polarizability"].numpy(force=True),
sampled[sys]["atype"].numpy(force=True),
)[0]
else:
Expand Down
4 changes: 2 additions & 2 deletions deepmd/tf/fit/polar.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@
atom_has_polar = [
w for w in all_stat["type"][ss][0] if (w in self.sel_type)
] # select atom with polar
if all_stat["find_atomic_polarizability"][ss] > 0.0:
if all_stat["find_atom_polarizability"][ss] > 0.0:

Check warning on line 196 in deepmd/tf/fit/polar.py

View check run for this annotation

Codecov / codecov/patch

deepmd/tf/fit/polar.py#L196

Added line #L196 was not covered by tests
for itype in range(
len(self.sel_type)
): # Atomic polar mode, should specify the atoms
Expand All @@ -208,7 +208,7 @@

polar_bias.append(
np.sum(
all_stat["atomic_polarizability"][ss].reshape(
all_stat["atom_polarizability"][ss].reshape(
nframes, len(atom_has_polar), -1
)[:, index_lis, :]
/ nframes,
Expand Down
4 changes: 2 additions & 2 deletions deepmd/tf/loss/tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@

def build(self, learning_rate, natoms, model_dict, label_dict, suffix):
polar_hat = label_dict[self.label_name]
atomic_polar_hat = label_dict["atomic_" + self.label_name]
atomic_polar_hat = label_dict["atom_" + self.label_name]

Check warning on line 73 in deepmd/tf/loss/tensor.py

View check run for this annotation

Codecov / codecov/patch

deepmd/tf/loss/tensor.py#L73

Added line #L73 was not covered by tests
polar = tf.reshape(model_dict[self.tensor_name], [-1])

find_global = label_dict["find_" + self.label_name]
find_atomic = label_dict["find_atomic_" + self.label_name]
find_atomic = label_dict["find_atom_" + self.label_name]

Check warning on line 77 in deepmd/tf/loss/tensor.py

View check run for this annotation

Codecov / codecov/patch

deepmd/tf/loss/tensor.py#L77

Added line #L77 was not covered by tests

# YHT: added for global / local dipole combination
l2_loss = global_cvt_2_tf_float(0.0)
Expand Down
2 changes: 2 additions & 0 deletions deepmd/utils/data.py
anyangml marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,8 @@
else:
data["type"] = np.tile(self.atom_type[self.idx_map], (nframes, 1))

# standardize keys
data = {kk.replace("atomic", "atom"): vv for kk, vv in data.items()}

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

View check run for this annotation

Codecov / codecov/patch

deepmd/utils/data.py#L581

Added line #L581 was not covered by tests
return data

def _load_data(
Expand Down
16 changes: 8 additions & 8 deletions source/tests/pt/model/test_polar_stat.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ def setUp(self) -> None:
self.sampled = [
{
"atype": types,
"find_atomic_polarizability": find_atomic_polarizability,
"atomic_polarizability": atomic_polarizability,
"find_atom_polarizability": find_atomic_polarizability,
"atom_polarizability": atomic_polarizability,
"polarizability": polarizability,
"find_polarizability": find_polarizability,
}
Expand Down Expand Up @@ -61,13 +61,13 @@ def test_atomic_consistency(self):
np.testing.assert_allclose(tfbias, to_numpy_array(ptbias))

def test_global_consistency(self):
self.sampled[0]["find_atomic_polarizability"] = -1
self.sampled[0]["polarizability"] = self.sampled[0][
"atomic_polarizability"
].sum(dim=1)
self.all_stat["find_atomic_polarizability"] = [-1]
self.sampled[0]["find_atom_polarizability"] = -1
self.sampled[0]["polarizability"] = self.sampled[0]["atom_polarizability"].sum(
dim=1
)
self.all_stat["find_atom_polarizability"] = [-1]
self.all_stat["polarizability"] = [
self.all_stat["atomic_polarizability"][0].sum(axis=1)
self.all_stat["atom_polarizability"][0].sum(axis=1)
]
self.tfpolar.compute_output_stats(self.all_stat)
tfbias = self.tfpolar.constant_matrix
Expand Down
16 changes: 8 additions & 8 deletions source/tests/tf/test_deepmd_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,8 @@ def test_load_set(self):
self.assertEqual(data["type"][ii][1], 1)
self.assertEqual(data["find_coord"], 1)
self._comp_np_mat2(data["coord"], self.coord)
self.assertEqual(data["find_test_atomic"], 1)
self._comp_np_mat2(data["test_atomic"], self.test_atomic)
self.assertEqual(data["find_test_atom"], 1)
self._comp_np_mat2(data["test_atom"], self.test_atomic)
self.assertEqual(data["find_test_frame"], 1)
self._comp_np_mat2(data["test_frame"], self.test_frame)
self.assertEqual(data["find_test_null"], 0)
Expand All @@ -207,7 +207,7 @@ def test_shuffle(self):
data_bk = copy.deepcopy(data)
data, idx = dd._shuffle_data(data)
self._comp_np_mat2(data_bk["coord"][idx, :], data["coord"])
self._comp_np_mat2(data_bk["test_atomic"][idx, :], data["test_atomic"])
self._comp_np_mat2(data_bk["test_atom"][idx, :], data["test_atom"])
self._comp_np_mat2(data_bk["test_frame"][idx, :], data["test_frame"])

def test_shuffle_with_numb_copy(self):
Expand All @@ -224,25 +224,25 @@ def test_shuffle_with_numb_copy(self):
data, idx = dd._shuffle_data(data)
assert idx.size == np.sum(prob)
self._comp_np_mat2(data_bk["coord"][idx, :], data["coord"])
self._comp_np_mat2(data_bk["test_atomic"][idx, :], data["test_atomic"])
self._comp_np_mat2(data_bk["test_atom"][idx, :], data["test_atom"])
self._comp_np_mat2(data_bk["test_frame"][idx, :], data["test_frame"])

def test_reduce(self):
dd = DeepmdData(self.data_name).add("test_atomic", 7, atomic=True, must=True)
dd.reduce("redu", "test_atomic")
data = dd._load_set(os.path.join(self.data_name, "set.foo"))
self.assertEqual(data["find_test_atomic"], 1)
self._comp_np_mat2(data["test_atomic"], self.test_atomic)
self.assertEqual(data["find_test_atom"], 1)
self._comp_np_mat2(data["test_atom"], self.test_atomic)
self.assertEqual(data["find_redu"], 1)
self._comp_np_mat2(data["redu"], self.redu_atomic)

def test_reduce_null(self):
dd = DeepmdData(self.data_name).add("test_atomic_1", 7, atomic=True, must=False)
dd.reduce("redu", "test_atomic_1")
data = dd._load_set(os.path.join(self.data_name, "set.foo"))
self.assertEqual(data["find_test_atomic_1"], 0)
self.assertEqual(data["find_test_atom_1"], 0)
self._comp_np_mat2(
data["test_atomic_1"], np.zeros([self.nframes, self.natoms * 7])
data["test_atom_1"], np.zeros([self.nframes, self.natoms * 7])
)
self.assertEqual(data["find_redu"], 0)
self._comp_np_mat2(data["redu"], np.zeros([self.nframes, 7]))
Expand Down
Loading