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

fix(test): make unit tests deterministic #3856

Merged
merged 8 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
2 changes: 1 addition & 1 deletion source/tests/common/dpmodel/test_descriptor_dpa1.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def setUp(self):
def test_self_consistency(
self,
):
rng = np.random.default_rng()
rng = np.random.default_rng(20240604)
nf, nloc, nnei = self.nlist.shape
davg = rng.normal(size=(self.nt, nnei, 4))
dstd = rng.normal(size=(self.nt, nnei, 4))
Expand Down
2 changes: 1 addition & 1 deletion source/tests/common/dpmodel/test_descriptor_dpa2.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def setUp(self):
def test_self_consistency(
self,
):
rng = np.random.default_rng()
rng = np.random.default_rng(20240604)
nf, nloc, nnei = self.nlist.shape
davg = rng.normal(size=(self.nt, nnei, 4))
dstd = rng.normal(size=(self.nt, nnei, 4))
Expand Down
4 changes: 2 additions & 2 deletions source/tests/common/dpmodel/test_descriptor_hybrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def setUp(self):
def test_get_parameters(
self,
):
rng = np.random.default_rng()
rng = np.random.default_rng(20240604)
nf, nloc, nnei = self.nlist.shape
davg = rng.normal(size=(self.nt, nnei, 4))
dstd = rng.normal(size=(self.nt, nnei, 4))
Expand Down Expand Up @@ -72,7 +72,7 @@ def test_get_parameters(
def test_self_consistency(
self,
):
rng = np.random.default_rng()
rng = np.random.default_rng(20240604)
nf, nloc, nnei = self.nlist.shape
davg = rng.normal(size=(self.nt, nnei, 4))
dstd = rng.normal(size=(self.nt, nnei, 4))
Expand Down
2 changes: 1 addition & 1 deletion source/tests/common/dpmodel/test_descriptor_se_e2_a.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def setUp(self):
def test_self_consistency(
self,
):
rng = np.random.default_rng()
rng = np.random.default_rng(20240604)
nf, nloc, nnei = self.nlist.shape
davg = rng.normal(size=(self.nt, nnei, 4))
dstd = rng.normal(size=(self.nt, nnei, 4))
Expand Down
2 changes: 1 addition & 1 deletion source/tests/common/dpmodel/test_descriptor_se_r.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def setUp(self):
def test_self_consistency(
self,
):
rng = np.random.default_rng()
rng = np.random.default_rng(20240604)
nf, nloc, nnei = self.nlist.shape
davg = rng.normal(size=(self.nt, nnei, 1))
dstd = rng.normal(size=(self.nt, nnei, 1))
Expand Down
2 changes: 1 addition & 1 deletion source/tests/common/dpmodel/test_descriptor_se_t.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def setUp(self):
def test_self_consistency(
self,
):
rng = np.random.default_rng()
rng = np.random.default_rng(20240604)
nf, nloc, nnei = self.nlist.shape
davg = rng.normal(size=(self.nt, nnei, 4))
dstd = rng.normal(size=(self.nt, nnei, 4))
Expand Down
4 changes: 2 additions & 2 deletions source/tests/common/dpmodel/test_dp_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def test_self_consistency(
np.testing.assert_allclose(ret0["energy_redu"], ret1["energy_redu"])

def test_prec_consistency(self):
rng = np.random.default_rng()
rng = np.random.default_rng(20240604)
nf, nloc, nnei = self.nlist.shape
ds = DescrptSeA(
self.rcut,
Expand Down Expand Up @@ -102,7 +102,7 @@ def setUp(self):
TestCaseSingleFrameWithoutNlist.setUp(self)

def test_prec_consistency(self):
rng = np.random.default_rng()
rng = np.random.default_rng(20240604)
nf, nloc = self.atype.shape
ds = DescrptSeA(
self.rcut,
Expand Down
2 changes: 1 addition & 1 deletion source/tests/common/dpmodel/test_env_mat.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def setUp(self):
def test_self_consistency(
self,
):
rng = np.random.default_rng()
rng = np.random.default_rng(20240604)
nf, nloc, nnei = self.nlist.shape
davg = rng.normal(size=(self.nt, nnei, 4))
dstd = rng.normal(size=(self.nt, nnei, 4))
Expand Down
6 changes: 3 additions & 3 deletions source/tests/common/dpmodel/test_fitting_invar_fitting.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def setUp(self):
def test_self_consistency(
self,
):
rng = np.random.default_rng()
rng = np.random.default_rng(20240604)
nf, nloc, nnei = self.nlist.shape
ds = DescrptSeA(self.rcut, self.rcut_smth, self.sel)
dd = ds.call(self.coord_ext, self.atype_ext, self.nlist)
Expand Down Expand Up @@ -102,7 +102,7 @@ def test_mask(self):
def test_self_exception(
self,
):
rng = np.random.default_rng()
rng = np.random.default_rng(20240604)
nf, nloc, nnei = self.nlist.shape
ds = DescrptSeA(self.rcut, self.rcut_smth, self.sel)
dd = ds.call(self.coord_ext, self.atype_ext, self.nlist)
Expand Down Expand Up @@ -160,7 +160,7 @@ def test_get_set(self):
3,
1,
)
rng = np.random.default_rng()
rng = np.random.default_rng(20240604)
foo = rng.normal([3, 4])
for ii in [
"bias_atom_e",
Expand Down
5 changes: 3 additions & 2 deletions source/tests/common/dpmodel/test_neighbor_stat.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@


def gen_sys(nframes):
rng = np.random.default_rng(20240604)
natoms = 1000
data = {}
X, Y, Z = np.mgrid[0:2:3j, 0:2:3j, 0:2:3j]
positions = np.vstack([X.ravel(), Y.ravel(), Z.ravel()]).T # + 0.1
data["coords"] = np.repeat(positions[np.newaxis, :, :], nframes, axis=0)
data["forces"] = np.random.default_rng().random([nframes, natoms, 3])
data["forces"] = rng.random([nframes, natoms, 3])
data["cells"] = np.array([3.0, 0.0, 0.0, 0.0, 3.0, 0.0, 0.0, 0.0, 3.0]).reshape(
1, 3, 3
)
data["energies"] = np.random.default_rng().random([nframes, 1])
data["energies"] = rng.random([nframes, 1])
data["atom_names"] = ["TYPE"]
data["atom_numbs"] = [27]
data["atom_types"] = np.repeat(0, 27)
Expand Down
2 changes: 1 addition & 1 deletion source/tests/common/dpmodel/test_region.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def setUp(self):
self.prec = 1e-8

def test_inter_to_phys(self):
rng = np.random.default_rng()
rng = np.random.default_rng(20240604)
inter = rng.normal(size=[4, 5, 3, 3])
phys = inter2phys(inter, self.cell)
for ii in range(4):
Expand Down
2 changes: 1 addition & 1 deletion source/tests/common/dpmodel/test_update_sel.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def update_sel(jdata):
class TestTrain(unittest.TestCase):
def setUp(self) -> None:
self.update_sel = UpdateSel()
self.mock_min_nbor_dist = random.random()
self.mock_min_nbor_dist = random.Random(20240604).random()
return super().setUp()

@patch("deepmd.dpmodel.utils.update_sel.UpdateSel.get_nbor_stat")
Expand Down
4 changes: 3 additions & 1 deletion source/tests/consistent/test_activation.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
class TestActivationFunctionConsistent(unittest.TestCase):
def setUp(self):
(self.activation,) = self.param
self.random_input = np.random.default_rng().normal(scale=10, size=(10, 10))
self.random_input = np.random.default_rng(20240604).normal(
scale=10, size=(10, 10)
)
self.ref = get_activation_fn_dp(self.activation)(self.random_input)

@unittest.skipUnless(INSTALLED_TF, "TensorFlow is not installed")
Expand Down
12 changes: 7 additions & 5 deletions source/tests/pt/model/test_autodiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,12 @@ def test(
places = 5
delta = 1e-5
natoms = 5
cell = torch.rand([3, 3], dtype=dtype, device="cpu")
generator = torch.Generator(device="cpu").manual_seed(20240604)
cell = torch.rand([3, 3], dtype=dtype, device="cpu", generator=generator)
cell = (cell + cell.T) + 5.0 * torch.eye(3, device="cpu")
coord = torch.rand([natoms, 3], dtype=dtype, device="cpu")
coord = torch.rand([natoms, 3], dtype=dtype, device="cpu", generator=generator)
coord = torch.matmul(coord, cell)
spin = torch.rand([natoms, 3], dtype=dtype, device="cpu")
spin = torch.rand([natoms, 3], dtype=dtype, device="cpu", generator=generator)
atype = torch.IntTensor([0, 0, 0, 1, 1])
# assumes input to be numpy tensor
coord = coord.numpy()
Expand Down Expand Up @@ -129,9 +130,10 @@ def test(
places = 5
delta = 1e-4
natoms = 5
cell = torch.rand([3, 3], dtype=dtype, device="cpu")
generator = torch.Generator(device="cpu").manual_seed(20240604)
cell = torch.rand([3, 3], dtype=dtype, device="cpu", generator=generator)
cell = (cell) + 5.0 * torch.eye(3, device="cpu")
coord = torch.rand([natoms, 3], dtype=dtype, device="cpu")
coord = torch.rand([natoms, 3], dtype=dtype, device="cpu", generator=generator)
coord = torch.matmul(coord, cell)
atype = torch.IntTensor([0, 0, 0, 1, 1])
# assumes input to be numpy tensor
Expand Down
2 changes: 1 addition & 1 deletion source/tests/pt/model/test_descriptor_hybrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
def test_get_parameters(
self,
):
rng = np.random.default_rng()
rng = np.random.default_rng(20240604)
Fixed Show fixed Hide fixed
nf, nloc, nnei = self.nlist.shape
ddsub0 = DescrptSeA(
rcut=self.rcut,
Expand Down
6 changes: 3 additions & 3 deletions source/tests/pt/model/test_descriptor_se_r.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def setUp(self):
def test_consistency(
self,
):
rng = np.random.default_rng()
rng = np.random.default_rng(20240604)
_, _, nnei = self.nlist.shape
davg = rng.normal(size=(self.nt, nnei, 1))
dstd = rng.normal(size=(self.nt, nnei, 1))
Expand Down Expand Up @@ -107,7 +107,7 @@ def test_consistency(
)

def test_load_stat(self):
rng = np.random.default_rng()
rng = np.random.default_rng(20240604)
_, _, nnei = self.nlist.shape
davg = rng.normal(size=(self.nt, nnei, 1))
dstd = rng.normal(size=(self.nt, nnei, 1))
Expand Down Expand Up @@ -157,7 +157,7 @@ def test_load_stat(self):
def test_jit(
self,
):
rng = np.random.default_rng()
rng = np.random.default_rng(20240604)
_, _, nnei = self.nlist.shape
davg = rng.normal(size=(self.nt, nnei, 1))
dstd = rng.normal(size=(self.nt, nnei, 1))
Expand Down
20 changes: 14 additions & 6 deletions source/tests/pt/model/test_dipole_fitting.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def finite_difference(f, x, a, delta=1e-6):
class TestDipoleFitting(unittest.TestCase, TestCaseSingleFrameWithNlist):
def setUp(self):
TestCaseSingleFrameWithNlist.setUp(self)
self.rng = np.random.default_rng()
self.rng = np.random.default_rng(20240604)
self.nf, self.nloc, _ = self.nlist.shape
self.dd0 = DescrptSeA(self.rcut, self.rcut_smth, self.sel).to(env.DEVICE)

Expand Down Expand Up @@ -147,11 +147,16 @@ def setUp(self) -> None:
self.rcut_smth = 0.5
self.sel = [46, 92, 4]
self.nf = 1
self.coord = 2 * torch.rand([self.natoms, 3], dtype=dtype, device=env.DEVICE)
generator = torch.Generator(device="cpu").manual_seed(20240604)
self.coord = 2 * torch.rand(
[self.natoms, 3], dtype=dtype, device=env.DEVICE, generator=generator
)
self.shift = torch.tensor([4, 4, 4], dtype=dtype, device=env.DEVICE)
self.atype = torch.tensor([0, 0, 0, 1, 1], dtype=torch.int32, device=env.DEVICE)
self.dd0 = DescrptSeA(self.rcut, self.rcut_smth, self.sel).to(env.DEVICE)
self.cell = torch.rand([3, 3], dtype=dtype, device=env.DEVICE)
self.cell = torch.rand(
[3, 3], dtype=dtype, device=env.DEVICE, generator=generator
)
self.cell = (self.cell + self.cell.T) + 5.0 * torch.eye(3, device=env.DEVICE)

def test_rot(self):
Expand All @@ -160,7 +165,7 @@ def test_rot(self):
coord_rot = torch.matmul(self.coord, rmat)
# use larger cell to rotate only coord and shift to the center of cell
cell_rot = 10.0 * torch.eye(3, dtype=dtype, device=env.DEVICE)
rng = np.random.default_rng()
rng = np.random.default_rng(20240604)
for nfp, nap in itertools.product(
[0, 3],
[0, 4],
Expand Down Expand Up @@ -304,8 +309,11 @@ def setUp(self):
self.rcut_smth = 0.5
self.sel = [46, 92, 4]
self.nf = 1
self.coord = 2 * torch.rand([self.natoms, 3], dtype=dtype, device=env.DEVICE)
cell = torch.rand([3, 3], dtype=dtype, device=env.DEVICE)
generator = torch.Generator(device="cpu").manual_seed(20240604)
self.coord = 2 * torch.rand(
[self.natoms, 3], dtype=dtype, device=env.DEVICE, generator=generator
)
cell = torch.rand([3, 3], dtype=dtype, device=env.DEVICE, generator=generator)
self.cell = (cell + cell.T) + 5.0 * torch.eye(3, device=env.DEVICE)
self.atype = torch.IntTensor([0, 0, 0, 1, 1], device="cpu").to(env.DEVICE)
self.dd0 = DescrptSeA(self.rcut, self.rcut_smth, self.sel).to(env.DEVICE)
Expand Down
2 changes: 1 addition & 1 deletion source/tests/pt/model/test_dp_atomic_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
)

def test_dp_consistency(self):
rng = np.random.default_rng()
rng = np.random.default_rng(20240604)
Fixed Show fixed Hide fixed
nf, nloc, nnei = self.nlist.shape
ds = DPDescrptSeA(
self.rcut,
Expand Down
10 changes: 5 additions & 5 deletions source/tests/pt/model/test_dp_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@
md0 = DPEnergyModel(ds, ft, type_map=type_map)
md1 = EnergyModel.deserialize(md0.serialize()).to(env.DEVICE)

rng = np.random.default_rng()
rng = np.random.default_rng(20240604)
fparam = rng.normal(size=[self.nf, nfp])
aparam = rng.normal(size=[self.nf, nloc, nap])
args0 = [self.coord, self.atype, self.cell]
Expand Down Expand Up @@ -169,7 +169,7 @@
md0 = DPEnergyModel(ds, ft, type_map=type_map)
md1 = EnergyModel.deserialize(md0.serialize()).to(env.DEVICE)

rng = np.random.default_rng()
rng = np.random.default_rng(20240604)
fparam = rng.normal(size=[self.nf, nfp])
aparam = rng.normal(size=[self.nf, self.nloc, nap])
args0 = [self.coord, self.atype]
Expand All @@ -190,7 +190,7 @@
)

def test_prec_consistency(self):
rng = np.random.default_rng()
rng = np.random.default_rng(20240604)
nf, nloc = self.atype.shape
ds = DPDescrptSeA(
self.rcut,
Expand Down Expand Up @@ -292,7 +292,7 @@
)

def test_dp_consistency(self):
rng = np.random.default_rng()
rng = np.random.default_rng(20240604)
Fixed Show fixed Hide fixed
nf, nloc, nnei = self.nlist.shape
ds = DPDescrptSeA(
self.rcut,
Expand Down Expand Up @@ -325,7 +325,7 @@
)

def test_prec_consistency(self):
rng = np.random.default_rng()
rng = np.random.default_rng(20240604)
nf, nloc, nnei = self.nlist.shape
ds = DPDescrptSeA(
self.rcut,
Expand Down
2 changes: 1 addition & 1 deletion source/tests/pt/model/test_dpa1.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def test_consistency(
def test_jit(
self,
):
rng = np.random.default_rng()
rng = np.random.default_rng(20240604)
nf, nloc, nnei = self.nlist.shape
davg = rng.normal(size=(self.nt, nnei, 4))
dstd = rng.normal(size=(self.nt, nnei, 4))
Expand Down
6 changes: 3 additions & 3 deletions source/tests/pt/model/test_ener_fitting.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
def test_consistency(
self,
):
rng = np.random.default_rng()
rng = np.random.default_rng(20240604)
nf, nloc, nnei = self.nlist.shape
dd0 = DescrptSeA(self.rcut, self.rcut_smth, self.sel).to(env.DEVICE)
rd0, _, _, _, _ = dd0(
Expand Down Expand Up @@ -102,7 +102,7 @@
def test_new_old(
self,
):
rng = np.random.default_rng()
rng = np.random.default_rng(20240604)
Fixed Show fixed Hide fixed
nf, nloc, nnei = self.nlist.shape
dd = DescrptSeA(self.rcut, self.rcut_smth, self.sel).to(env.DEVICE)
rd0, _, _, _, _ = dd(
Expand Down Expand Up @@ -176,7 +176,7 @@
3,
1,
)
rng = np.random.default_rng()
rng = np.random.default_rng(20240604)
foo = rng.normal([3, 4])
for ii in [
"bias_atom_e",
Expand Down
5 changes: 3 additions & 2 deletions source/tests/pt/model/test_ener_spin_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,12 @@ def setUp(self):
natoms = 5
self.ntypes = 3 # ["O", "H", "B"] for test
self.cell = 4.0 * torch.eye(3, dtype=dtype, device=env.DEVICE).unsqueeze(0)
generator = torch.Generator(device="cpu").manual_seed(20240604)
self.coord = 3.0 * torch.rand(
[natoms, 3], dtype=dtype, device=env.DEVICE
[natoms, 3], dtype=dtype, device=env.DEVICE, generator=generator
).unsqueeze(0)
self.spin = 0.5 * torch.rand(
[natoms, 3], dtype=dtype, device=env.DEVICE
[natoms, 3], dtype=dtype, device=env.DEVICE, generator=generator
).unsqueeze(0)
self.atype = torch.tensor(
[0, 0, 0, 1, 1], dtype=torch.int64, device=env.DEVICE
Expand Down
2 changes: 1 addition & 1 deletion source/tests/pt/model/test_env_mat.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def setUp(self):
def test_consistency(
self,
):
rng = np.random.default_rng()
rng = np.random.default_rng(20240604)
nf, nloc, nnei = self.nlist.shape
davg = rng.normal(size=(self.nt, nnei, 4))
dstd = rng.normal(size=(self.nt, nnei, 4))
Expand Down
Loading
Loading