Skip to content

Commit

Permalink
use np.testing.assert_allclose instead of paddle.allclose for more ac…
Browse files Browse the repository at this point in the history
…curate report && remove CMAKE_PREFIX setting in read_env for paddle
  • Loading branch information
HydrogenSulfate committed Oct 29, 2024
1 parent 9b1f322 commit 2b34756
Show file tree
Hide file tree
Showing 31 changed files with 325 additions and 230 deletions.
1 change: 0 additions & 1 deletion backend/read_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ def get_argument_from_env() -> tuple[str, list, list, dict, str, str, str]:
cmake_args.extend(
[
"-DENABLE_PADDLE=ON",
f"-DCMAKE_PREFIX_PATH={pd_install_dir}",
]
)
else:
Expand Down
5 changes: 2 additions & 3 deletions source/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,14 @@ if(ENABLE_PADDLE)
if(NOT DEFINED PADDLE_INFERENCE_DIR)
# message( FATAL_ERROR "Make sure PADDLE_INFERENCE_DIR is set when
# ENABLE_PADDLE=ON")
message(
STATUS
"PADDLE_INFERENCE_DIR is not defined. Downloading and extracting...")
message(STATUS "PADDLE_INFERENCE_DIR is not defined. Downloading...")
set(DOWNLOAD_URL
"https://paddle-qa.bj.bcebos.com/paddle-pipeline/GITHUB_Docker_Compile_Test_Cuda118_cudnn860_Trt8531_D1/ce51e82e84fc97e0a55a162037f1554746159cad/paddle_inference.tgz"
)
set(TGZ_FILE "${CMAKE_BINARY_DIR}/paddle_inference.tgz")
set(EXTRACTED_DIR "${CMAKE_BINARY_DIR}/paddle_inference_install_dir")
file(DOWNLOAD ${DOWNLOAD_URL} ${TGZ_FILE})
message(STATUS "Downloading finished, extracting...")
execute_process(COMMAND ${CMAKE_COMMAND} -E tar -xzvf ${TGZ_FILE}
OUTPUT_QUIET)
file(REMOVE ${TGZ_FILE})
Expand Down
1 change: 0 additions & 1 deletion source/tests/pd/model/test_descriptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ def setUp(self):
self.ntypes = len(self.sel)
self.nnei = sum(self.sel)

@unittest.skip("Wait for https://github.com/PaddlePaddle/Paddle/pull/68961")
def test_consistency(self):
avg_zero = paddle.zeros(
[self.ntypes, self.nnei * 4],
Expand Down
9 changes: 5 additions & 4 deletions source/tests/pd/model/test_descriptor_dpa1.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
Path,
)

import numpy as np
import paddle

from deepmd.pd.model.descriptor import (
Expand Down Expand Up @@ -289,8 +290,8 @@ def test_descriptor_block(self):
self.assertAlmostEqual(6.0, des.get_rcut())
self.assertEqual(30, des.get_nsel())
self.assertEqual(2, des.get_ntypes())
assert paddle.allclose(
descriptor.reshape([-1]), self.ref_d, atol=1e-10, rtol=1e-10
np.testing.assert_allclose(
descriptor.reshape([-1]).numpy(), self.ref_d.numpy(), atol=1e-10, rtol=1e-10
)

def test_descriptor(self):
Expand Down Expand Up @@ -342,8 +343,8 @@ def test_descriptor(self):
self.assertAlmostEqual(6.0, des.get_rcut())
self.assertEqual(30, des.get_nsel())
self.assertEqual(2, des.get_ntypes())
assert paddle.allclose(
descriptor.reshape([-1]), self.ref_d, atol=1e-10, rtol=1e-10
np.testing.assert_allclose(
descriptor.reshape([-1]).numpy(), self.ref_d.numpy(), atol=1e-10, rtol=1e-10
)

dparams["concat_output_tebd"] = True
Expand Down
5 changes: 3 additions & 2 deletions source/tests/pd/model/test_descriptor_dpa2.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
Path,
)

import numpy as np
import paddle

from deepmd.pd.model.descriptor import (
Expand Down Expand Up @@ -165,8 +166,8 @@ def test_descriptor(self):
self.assertAlmostEqual(6.0, des.get_rcut())
self.assertEqual(30, des.get_nsel())
self.assertEqual(2, des.get_ntypes())
assert paddle.allclose(
descriptor.reshape([-1]), self.ref_d, atol=1e-10, rtol=1e-10
np.testing.assert_allclose(
descriptor.reshape([-1]).numpy(), self.ref_d.numpy(), atol=1e-10, rtol=1e-10
)

dparams["concat_output_tebd"] = True
Expand Down
6 changes: 3 additions & 3 deletions source/tests/pd/model/test_descriptor_hybrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def test_hybrid_mixed_and_no_mixed(self):
)
ret1 = ddsub1(coord_ext, atype_ext, nlist2[:, :, :-1])
ret2 = ddsub2(coord_ext, atype_ext, nlist1[:, :, [0, 1, 2, self.sel[0]]])
assert paddle.allclose(
ret[0],
paddle.concat([ret0[0], ret1[0], ret2[0]], axis=2),
np.testing.assert_allclose(
ret[0].numpy(),
paddle.concat([ret0[0], ret1[0], ret2[0]], axis=2).numpy(),
)
1 change: 0 additions & 1 deletion source/tests/pd/model/test_embedding_net.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ def setUp(self):
self.axis_neuron = model_config["descriptor"]["axis_neuron"]
self.np_batch, self.paddle_batch = get_single_batch(ds)

@unittest.skip("Wait for https://github.com/PaddlePaddle/Paddle/pull/68961")
def test_consistency(self):
dp_d = DescrptSeA_tf(
rcut=self.rcut,
Expand Down
39 changes: 22 additions & 17 deletions source/tests/pd/model/test_ener_spin_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,12 @@ def test_input_output_process(self):
force_real, force_mag, _ = self.model.process_spin_output(
self.atype, force_all
)
assert paddle.allclose(
force_real, force_all[:, :nloc] + force_all[:, nloc:]
np.testing.assert_allclose(
force_real.numpy(), (force_all[:, :nloc] + force_all[:, nloc:]).numpy()
)
assert paddle.allclose(
force_mag, force_all[:, nloc:] * virtual_scale.unsqueeze(-1)
np.testing.assert_allclose(
force_mag.numpy(),
(force_all[:, nloc:] * virtual_scale.unsqueeze(-1)).numpy(),
)

# 3. test forward_lower input process
Expand Down Expand Up @@ -204,18 +205,20 @@ def test_input_output_process(self):
# compare coords of real and virtual atoms
virtual_coord = extended_coord + extended_spin * virtual_scale.unsqueeze(-1)
assert np.allclose(extended_coord_updated.shape, [nframes, nall * 2, 3])
assert paddle.allclose(
extended_coord_updated[:, :nloc], extended_coord[:, :nloc]
np.testing.assert_allclose(
extended_coord_updated[:, :nloc].numpy(), extended_coord[:, :nloc].numpy()
)
assert paddle.allclose(
extended_coord_updated[:, nloc : nloc + nloc], virtual_coord[:, :nloc]
np.testing.assert_allclose(
extended_coord_updated[:, nloc : nloc + nloc].numpy(),
virtual_coord[:, :nloc].numpy(),
)
assert paddle.allclose(
extended_coord_updated[:, nloc + nloc : nloc + nall],
extended_coord[:, nloc:nall],
np.testing.assert_allclose(
extended_coord_updated[:, nloc + nloc : nloc + nall].numpy(),
extended_coord[:, nloc:nall].numpy(),
)
assert paddle.allclose(
extended_coord_updated[:, nloc + nall :], virtual_coord[:, nloc:nall]
np.testing.assert_allclose(
extended_coord_updated[:, nloc + nall :].numpy(),
virtual_coord[:, nloc:nall].numpy(),
)

# compare mapping
Expand Down Expand Up @@ -276,11 +279,13 @@ def test_input_output_process(self):
force_all_switched[:, nloc:nall] = force_all[:, nloc + nloc : nloc + nall]
force_all_switched[:, nall : nall + nloc] = force_all[:, nloc : nloc + nloc]
force_all_switched[:, nall + nloc :] = force_all[:, nloc + nall :]
assert paddle.allclose(
force_real, force_all_switched[:, :nall] + force_all_switched[:, nall:]
np.testing.assert_allclose(
force_real.numpy(),
(force_all_switched[:, :nall] + force_all_switched[:, nall:]).numpy(),
)
assert paddle.allclose(
force_mag, force_all_switched[:, nall:] * virtual_scale.unsqueeze(-1)
np.testing.assert_allclose(
force_mag.numpy(),
(force_all_switched[:, nall:] * virtual_scale.unsqueeze(-1)).numpy(),
)

def test_jit(self):
Expand Down
21 changes: 14 additions & 7 deletions source/tests/pd/model/test_forward_lower.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import copy
import unittest

import numpy as np
import paddle

from deepmd.pd.infer.deep_eval import (
Expand Down Expand Up @@ -110,21 +111,27 @@ def test(
result_forward_lower = self.model.forward_lower(**input_dict)
for key in test_keys:
if key in ["energy"]:
assert paddle.allclose(
result_forward_lower[key], result_forward[key], rtol=prec, atol=prec
np.testing.assert_allclose(
result_forward_lower[key].numpy(),
result_forward[key].numpy(),
rtol=prec,
atol=prec,
)
elif key in ["force", "force_mag"]:
reduced_vv = reduce_tensor(
result_forward_lower[f"extended_{key}"], mapping, natoms
)
assert paddle.allclose(
reduced_vv, result_forward[key], rtol=prec, atol=prec
np.testing.assert_allclose(
reduced_vv.numpy(),
result_forward[key].numpy(),
rtol=prec,
atol=prec,
)
elif key == "virial":
if not hasattr(self, "test_virial") or self.test_virial:
assert paddle.allclose(
result_forward_lower[key],
result_forward[key],
np.testing.assert_allclose(
result_forward_lower[key].numpy(),
result_forward[key].numpy(),
rtol=prec,
atol=prec,
)
Expand Down
4 changes: 3 additions & 1 deletion source/tests/pd/model/test_linear_atomic_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ def test_pairwise(self, mock_loadtxt):
dtype=paddle.float64,
place=env.DEVICE,
)
assert paddle.allclose(results, excepted_res, rtol=0.0001, atol=0.0001)
np.testing.assert_allclose(
results.numpy(), excepted_res.numpy(), rtol=0.0001, atol=0.0001
)


class TestIntegration(unittest.TestCase, TestCaseSingleFrameWithNlist):
Expand Down
4 changes: 3 additions & 1 deletion source/tests/pd/model/test_make_hessian_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ def test(
coord, atype, box=cell, fparam=fparam, aparam=aparam
)
# compare hess and value models
assert paddle.allclose(ret_dict0["energy"], ret_dict1["energy"])
np.testing.assert_allclose(
ret_dict0["energy"].numpy(), ret_dict1["energy"].numpy()
)
ana_hess = ret_dict0["energy_derv_r_derv_r"]

# compute finite difference
Expand Down
Loading

0 comments on commit 2b34756

Please sign in to comment.