From 39bd6beafa81af47d828d74a3f8a3f32944ce39c Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Sat, 29 Jun 2024 01:10:38 -0400 Subject: [PATCH 1/5] fix(pt): change fitting_attr variable scope reuse to AUTO_REUSE Fix #3928. Prevent `fitting_attr` from becoming `fitting_attr_1`. Signed-off-by: Jinzhe Zeng --- deepmd/tf/descriptor/se_a_mask.py | 2 +- deepmd/tf/fit/dos.py | 2 +- deepmd/tf/fit/ener.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/deepmd/tf/descriptor/se_a_mask.py b/deepmd/tf/descriptor/se_a_mask.py index b79e806fca..d9891cf0da 100644 --- a/deepmd/tf/descriptor/se_a_mask.py +++ b/deepmd/tf/descriptor/se_a_mask.py @@ -311,7 +311,7 @@ def build( aparam[:, :] is the real/virtual sign for each atom. """ aparam = input_dict["aparam"] - with tf.variable_scope("fitting_attr" + suffix, reuse=reuse): + with tf.variable_scope("fitting_attr" + suffix, reuse=tf.AUTO_REUSE): t_aparam_nall = tf.constant(True, name="aparam_nall", dtype=tf.bool) self.mask = tf.cast(aparam, tf.int32) self.mask = tf.reshape(self.mask, [-1, natoms[1]]) diff --git a/deepmd/tf/fit/dos.py b/deepmd/tf/fit/dos.py index 382d11f45e..928c8f5b32 100644 --- a/deepmd/tf/fit/dos.py +++ b/deepmd/tf/fit/dos.py @@ -439,7 +439,7 @@ def build( if self.aparam_inv_std is None: self.aparam_inv_std = 1.0 - with tf.variable_scope("fitting_attr" + suffix, reuse=reuse): + with tf.variable_scope("fitting_attr" + suffix, reuse=tf.AUTO_REUSE): t_dfparam = tf.constant(self.numb_fparam, name="dfparam", dtype=tf.int32) t_daparam = tf.constant(self.numb_aparam, name="daparam", dtype=tf.int32) t_numb_dos = tf.constant(self.numb_dos, name="numb_dos", dtype=tf.int32) diff --git a/deepmd/tf/fit/ener.py b/deepmd/tf/fit/ener.py index c2aef0610a..8e856bada4 100644 --- a/deepmd/tf/fit/ener.py +++ b/deepmd/tf/fit/ener.py @@ -537,7 +537,7 @@ def build( if "t_bias_atom_e" in nvnmd_cfg.weight.keys(): self.bias_atom_e = nvnmd_cfg.weight["t_bias_atom_e"] - with tf.variable_scope("fitting_attr" + suffix, reuse=reuse): + with tf.variable_scope("fitting_attr" + suffix, reuse=tf.AUTO_REUSE): t_dfparam = tf.constant(self.numb_fparam, name="dfparam", dtype=tf.int32) t_daparam = tf.constant(self.numb_aparam, name="daparam", dtype=tf.int32) self.t_bias_atom_e = tf.get_variable( From b82d4342bda6189308577fcc37e964b9c808420f Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Sat, 29 Jun 2024 01:18:46 -0400 Subject: [PATCH 2/5] Revert "fix(pt): change fitting_attr variable scope reuse to AUTO_REUSE" This reverts commit 39bd6beafa81af47d828d74a3f8a3f32944ce39c. --- deepmd/tf/descriptor/se_a_mask.py | 2 +- deepmd/tf/fit/dos.py | 2 +- deepmd/tf/fit/ener.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/deepmd/tf/descriptor/se_a_mask.py b/deepmd/tf/descriptor/se_a_mask.py index d9891cf0da..b79e806fca 100644 --- a/deepmd/tf/descriptor/se_a_mask.py +++ b/deepmd/tf/descriptor/se_a_mask.py @@ -311,7 +311,7 @@ def build( aparam[:, :] is the real/virtual sign for each atom. """ aparam = input_dict["aparam"] - with tf.variable_scope("fitting_attr" + suffix, reuse=tf.AUTO_REUSE): + with tf.variable_scope("fitting_attr" + suffix, reuse=reuse): t_aparam_nall = tf.constant(True, name="aparam_nall", dtype=tf.bool) self.mask = tf.cast(aparam, tf.int32) self.mask = tf.reshape(self.mask, [-1, natoms[1]]) diff --git a/deepmd/tf/fit/dos.py b/deepmd/tf/fit/dos.py index 928c8f5b32..382d11f45e 100644 --- a/deepmd/tf/fit/dos.py +++ b/deepmd/tf/fit/dos.py @@ -439,7 +439,7 @@ def build( if self.aparam_inv_std is None: self.aparam_inv_std = 1.0 - with tf.variable_scope("fitting_attr" + suffix, reuse=tf.AUTO_REUSE): + with tf.variable_scope("fitting_attr" + suffix, reuse=reuse): t_dfparam = tf.constant(self.numb_fparam, name="dfparam", dtype=tf.int32) t_daparam = tf.constant(self.numb_aparam, name="daparam", dtype=tf.int32) t_numb_dos = tf.constant(self.numb_dos, name="numb_dos", dtype=tf.int32) diff --git a/deepmd/tf/fit/ener.py b/deepmd/tf/fit/ener.py index 8e856bada4..c2aef0610a 100644 --- a/deepmd/tf/fit/ener.py +++ b/deepmd/tf/fit/ener.py @@ -537,7 +537,7 @@ def build( if "t_bias_atom_e" in nvnmd_cfg.weight.keys(): self.bias_atom_e = nvnmd_cfg.weight["t_bias_atom_e"] - with tf.variable_scope("fitting_attr" + suffix, reuse=tf.AUTO_REUSE): + with tf.variable_scope("fitting_attr" + suffix, reuse=reuse): t_dfparam = tf.constant(self.numb_fparam, name="dfparam", dtype=tf.int32) t_daparam = tf.constant(self.numb_aparam, name="daparam", dtype=tf.int32) self.t_bias_atom_e = tf.get_variable( From 2ccaa222daf545f300e44c5f4d401938fe68d0d1 Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Sat, 29 Jun 2024 01:19:48 -0400 Subject: [PATCH 3/5] fix according to https://stackoverflow.com/a/40884220/9567349 Signed-off-by: Jinzhe Zeng --- deepmd/tf/descriptor/se_a_mask.py | 3 ++- deepmd/tf/fit/dos.py | 3 ++- deepmd/tf/fit/ener.py | 3 ++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/deepmd/tf/descriptor/se_a_mask.py b/deepmd/tf/descriptor/se_a_mask.py index b79e806fca..f6f504a138 100644 --- a/deepmd/tf/descriptor/se_a_mask.py +++ b/deepmd/tf/descriptor/se_a_mask.py @@ -311,7 +311,8 @@ def build( aparam[:, :] is the real/virtual sign for each atom. """ aparam = input_dict["aparam"] - with tf.variable_scope("fitting_attr" + suffix, reuse=reuse): + # https://stackoverflow.com/a/40884220/9567349 + with tf.variable_scope(f"fitting_attr{suffix}/", reuse=reuse): t_aparam_nall = tf.constant(True, name="aparam_nall", dtype=tf.bool) self.mask = tf.cast(aparam, tf.int32) self.mask = tf.reshape(self.mask, [-1, natoms[1]]) diff --git a/deepmd/tf/fit/dos.py b/deepmd/tf/fit/dos.py index 382d11f45e..b6c873389a 100644 --- a/deepmd/tf/fit/dos.py +++ b/deepmd/tf/fit/dos.py @@ -439,7 +439,8 @@ def build( if self.aparam_inv_std is None: self.aparam_inv_std = 1.0 - with tf.variable_scope("fitting_attr" + suffix, reuse=reuse): + # https://stackoverflow.com/a/40884220/9567349 + with tf.variable_scope(f"fitting_attr{suffix}/", reuse=reuse): t_dfparam = tf.constant(self.numb_fparam, name="dfparam", dtype=tf.int32) t_daparam = tf.constant(self.numb_aparam, name="daparam", dtype=tf.int32) t_numb_dos = tf.constant(self.numb_dos, name="numb_dos", dtype=tf.int32) diff --git a/deepmd/tf/fit/ener.py b/deepmd/tf/fit/ener.py index c2aef0610a..485b176d99 100644 --- a/deepmd/tf/fit/ener.py +++ b/deepmd/tf/fit/ener.py @@ -537,7 +537,8 @@ def build( if "t_bias_atom_e" in nvnmd_cfg.weight.keys(): self.bias_atom_e = nvnmd_cfg.weight["t_bias_atom_e"] - with tf.variable_scope("fitting_attr" + suffix, reuse=reuse): + # https://stackoverflow.com/a/40884220/9567349 + with tf.variable_scope(f"fitting_attr{suffix}/", reuse=reuse): t_dfparam = tf.constant(self.numb_fparam, name="dfparam", dtype=tf.int32) t_daparam = tf.constant(self.numb_aparam, name="daparam", dtype=tf.int32) self.t_bias_atom_e = tf.get_variable( From 5ac42c39c0ae7bb18cec043b1e76a0ea2802860c Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Sat, 29 Jun 2024 04:11:35 -0400 Subject: [PATCH 4/5] Revert "fix according to https://stackoverflow.com/a/40884220/9567349" This reverts commit 2ccaa222daf545f300e44c5f4d401938fe68d0d1. --- deepmd/tf/descriptor/se_a_mask.py | 3 +-- deepmd/tf/fit/dos.py | 3 +-- deepmd/tf/fit/ener.py | 3 +-- 3 files changed, 3 insertions(+), 6 deletions(-) diff --git a/deepmd/tf/descriptor/se_a_mask.py b/deepmd/tf/descriptor/se_a_mask.py index f6f504a138..b79e806fca 100644 --- a/deepmd/tf/descriptor/se_a_mask.py +++ b/deepmd/tf/descriptor/se_a_mask.py @@ -311,8 +311,7 @@ def build( aparam[:, :] is the real/virtual sign for each atom. """ aparam = input_dict["aparam"] - # https://stackoverflow.com/a/40884220/9567349 - with tf.variable_scope(f"fitting_attr{suffix}/", reuse=reuse): + with tf.variable_scope("fitting_attr" + suffix, reuse=reuse): t_aparam_nall = tf.constant(True, name="aparam_nall", dtype=tf.bool) self.mask = tf.cast(aparam, tf.int32) self.mask = tf.reshape(self.mask, [-1, natoms[1]]) diff --git a/deepmd/tf/fit/dos.py b/deepmd/tf/fit/dos.py index b6c873389a..382d11f45e 100644 --- a/deepmd/tf/fit/dos.py +++ b/deepmd/tf/fit/dos.py @@ -439,8 +439,7 @@ def build( if self.aparam_inv_std is None: self.aparam_inv_std = 1.0 - # https://stackoverflow.com/a/40884220/9567349 - with tf.variable_scope(f"fitting_attr{suffix}/", reuse=reuse): + with tf.variable_scope("fitting_attr" + suffix, reuse=reuse): t_dfparam = tf.constant(self.numb_fparam, name="dfparam", dtype=tf.int32) t_daparam = tf.constant(self.numb_aparam, name="daparam", dtype=tf.int32) t_numb_dos = tf.constant(self.numb_dos, name="numb_dos", dtype=tf.int32) diff --git a/deepmd/tf/fit/ener.py b/deepmd/tf/fit/ener.py index 485b176d99..c2aef0610a 100644 --- a/deepmd/tf/fit/ener.py +++ b/deepmd/tf/fit/ener.py @@ -537,8 +537,7 @@ def build( if "t_bias_atom_e" in nvnmd_cfg.weight.keys(): self.bias_atom_e = nvnmd_cfg.weight["t_bias_atom_e"] - # https://stackoverflow.com/a/40884220/9567349 - with tf.variable_scope(f"fitting_attr{suffix}/", reuse=reuse): + with tf.variable_scope("fitting_attr" + suffix, reuse=reuse): t_dfparam = tf.constant(self.numb_fparam, name="dfparam", dtype=tf.int32) t_daparam = tf.constant(self.numb_aparam, name="daparam", dtype=tf.int32) self.t_bias_atom_e = tf.get_variable( From 70d4d4e9f43445b3a6ade548a431ca6c2fcfdf64 Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Sat, 29 Jun 2024 04:12:23 -0400 Subject: [PATCH 5/5] avoid using namespace Signed-off-by: Jinzhe Zeng --- deepmd/tf/descriptor/se_a_mask.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/deepmd/tf/descriptor/se_a_mask.py b/deepmd/tf/descriptor/se_a_mask.py index b79e806fca..fa47920697 100644 --- a/deepmd/tf/descriptor/se_a_mask.py +++ b/deepmd/tf/descriptor/se_a_mask.py @@ -311,8 +311,9 @@ def build( aparam[:, :] is the real/virtual sign for each atom. """ aparam = input_dict["aparam"] - with tf.variable_scope("fitting_attr" + suffix, reuse=reuse): - t_aparam_nall = tf.constant(True, name="aparam_nall", dtype=tf.bool) + t_aparam_nall = tf.constant( + True, name=f"fitting_attr{suffix}/aparam_nall", dtype=tf.bool + ) self.mask = tf.cast(aparam, tf.int32) self.mask = tf.reshape(self.mask, [-1, natoms[1]])