diff --git a/source/api_cc/tests/test_deeppot_a_fparam_aparam_nframes_ptexpt.cc b/source/api_cc/tests/test_deeppot_a_fparam_aparam_nframes_ptexpt.cc index b6da8a37a7..c72b210f7c 100644 --- a/source/api_cc/tests/test_deeppot_a_fparam_aparam_nframes_ptexpt.cc +++ b/source/api_cc/tests/test_deeppot_a_fparam_aparam_nframes_ptexpt.cc @@ -8,6 +8,7 @@ #include #include "DeepPot.h" +#include "DeepPotPTExpt.h" #include "test_utils.h" template @@ -113,13 +114,18 @@ class TestInferDeepPotAFparamAparamNFramesPtExpt : public ::testing::Test { std::vector expected_tot_e; std::vector expected_tot_v; - deepmd::DeepPot dp; + static deepmd::DeepPot dp; + + static void SetUpTestSuite() { +#if defined(BUILD_PYTORCH) && BUILD_PT_EXPT + dp.init("../../tests/infer/fparam_aparam.pt2"); +#endif + } void SetUp() override { -#ifndef BUILD_PYTORCH +#if !defined(BUILD_PYTORCH) || !BUILD_PT_EXPT GTEST_SKIP() << "Skip because PyTorch support is not enabled."; #endif - dp.init("../../tests/infer/fparam_aparam.pt2"); natoms = expected_e.size() / nframes; EXPECT_EQ(nframes * natoms * 3, expected_f.size()); @@ -142,8 +148,13 @@ class TestInferDeepPotAFparamAparamNFramesPtExpt : public ::testing::Test { }; void TearDown() override {}; + + static void TearDownTestSuite() { dp = deepmd::DeepPot(); } }; +template +deepmd::DeepPot TestInferDeepPotAFparamAparamNFramesPtExpt::dp; + TYPED_TEST_SUITE(TestInferDeepPotAFparamAparamNFramesPtExpt, ValueTypes); TYPED_TEST(TestInferDeepPotAFparamAparamNFramesPtExpt, cpu_build_nlist) { diff --git a/source/api_cc/tests/test_deeppot_a_fparam_aparam_ptexpt.cc b/source/api_cc/tests/test_deeppot_a_fparam_aparam_ptexpt.cc index 0ef30df29e..fe1ec116b8 100644 --- a/source/api_cc/tests/test_deeppot_a_fparam_aparam_ptexpt.cc +++ b/source/api_cc/tests/test_deeppot_a_fparam_aparam_ptexpt.cc @@ -9,6 +9,7 @@ #include #include "DeepPot.h" +#include "DeepPotPTExpt.h" #include "neighbor_list.h" #include "test_utils.h" @@ -73,13 +74,18 @@ class TestInferDeepPotAFParamAParamPtExpt : public ::testing::Test { double expected_tot_e; std::vector expected_tot_v; - deepmd::DeepPot dp; + static deepmd::DeepPot dp; + + static void SetUpTestSuite() { +#if defined(BUILD_PYTORCH) && BUILD_PT_EXPT + dp.init("../../tests/infer/fparam_aparam.pt2"); +#endif + } void SetUp() override { -#ifndef BUILD_PYTORCH +#if !defined(BUILD_PYTORCH) || !BUILD_PT_EXPT GTEST_SKIP() << "Skip because PyTorch support is not enabled."; #endif - dp.init("../../tests/infer/fparam_aparam.pt2"); natoms = expected_e.size(); EXPECT_EQ(natoms * 3, expected_f.size()); @@ -98,8 +104,13 @@ class TestInferDeepPotAFParamAParamPtExpt : public ::testing::Test { }; void TearDown() override {}; + + static void TearDownTestSuite() { dp = deepmd::DeepPot(); } }; +template +deepmd::DeepPot TestInferDeepPotAFParamAParamPtExpt::dp; + TYPED_TEST_SUITE(TestInferDeepPotAFParamAParamPtExpt, ValueTypes); TYPED_TEST(TestInferDeepPotAFParamAParamPtExpt, cpu_build_nlist) { @@ -325,18 +336,28 @@ TYPED_TEST(TestInferDeepPotAFParamAParamPtExpt, cpu_lmp_nlist_atomic) { template class TestInferDeepPotNoDefaultFParamPtExpt : public ::testing::Test { protected: - deepmd::DeepPot dp; + static deepmd::DeepPot dp; + + static void SetUpTestSuite() { +#if defined(BUILD_PYTORCH) && BUILD_PT_EXPT + dp.init("../../tests/infer/fparam_aparam.pt2"); +#endif + } void SetUp() override { -#ifndef BUILD_PYTORCH +#if !defined(BUILD_PYTORCH) || !BUILD_PT_EXPT GTEST_SKIP() << "Skip because PyTorch support is not enabled."; #endif - dp.init("../../tests/infer/fparam_aparam.pt2"); }; void TearDown() override {}; + + static void TearDownTestSuite() { dp = deepmd::DeepPot(); } }; +template +deepmd::DeepPot TestInferDeepPotNoDefaultFParamPtExpt::dp; + TYPED_TEST_SUITE(TestInferDeepPotNoDefaultFParamPtExpt, ValueTypes); TYPED_TEST(TestInferDeepPotNoDefaultFParamPtExpt, no_default_fparam) { diff --git a/source/api_cc/tests/test_deeppot_default_fparam_ptexpt.cc b/source/api_cc/tests/test_deeppot_default_fparam_ptexpt.cc index 88db060521..996dcb1b50 100644 --- a/source/api_cc/tests/test_deeppot_default_fparam_ptexpt.cc +++ b/source/api_cc/tests/test_deeppot_default_fparam_ptexpt.cc @@ -10,6 +10,7 @@ #include #include "DeepPot.h" +#include "DeepPotPTExpt.h" #include "neighbor_list.h" #include "test_utils.h" @@ -78,13 +79,18 @@ class TestInferDeepPotDefaultFParamPtExpt : public ::testing::Test { double expected_tot_e; std::vector expected_tot_v; - deepmd::DeepPot dp; + static deepmd::DeepPot dp; + + static void SetUpTestSuite() { +#if defined(BUILD_PYTORCH) && BUILD_PT_EXPT + dp.init("../../tests/infer/fparam_aparam_default.pt2"); +#endif + } void SetUp() override { -#ifndef BUILD_PYTORCH +#if !defined(BUILD_PYTORCH) || !BUILD_PT_EXPT GTEST_SKIP() << "Skip because PyTorch support is not enabled."; #endif - dp.init("../../tests/infer/fparam_aparam_default.pt2"); natoms = expected_e.size(); EXPECT_EQ(natoms * 3, expected_f.size()); @@ -103,8 +109,13 @@ class TestInferDeepPotDefaultFParamPtExpt : public ::testing::Test { }; void TearDown() override {}; + + static void TearDownTestSuite() { dp = deepmd::DeepPot(); } }; +template +deepmd::DeepPot TestInferDeepPotDefaultFParamPtExpt::dp; + TYPED_TEST_SUITE(TestInferDeepPotDefaultFParamPtExpt, ValueTypes); TYPED_TEST(TestInferDeepPotDefaultFParamPtExpt, attrs) { diff --git a/source/api_cc/tests/test_deeppot_dpa1_ptexpt.cc b/source/api_cc/tests/test_deeppot_dpa1_ptexpt.cc index 53a631f6f8..c5d3b90303 100644 --- a/source/api_cc/tests/test_deeppot_dpa1_ptexpt.cc +++ b/source/api_cc/tests/test_deeppot_dpa1_ptexpt.cc @@ -10,6 +10,7 @@ #include #include "DeepPot.h" +#include "DeepPotPTExpt.h" #include "neighbor_list.h" #include "test_utils.h" @@ -72,13 +73,18 @@ class TestInferDeepPotDpa1PtExpt : public ::testing::Test { double expected_tot_e; std::vector expected_tot_v; - deepmd::DeepPot dp; + static deepmd::DeepPot dp; + + static void SetUpTestSuite() { +#if defined(BUILD_PYTORCH) && BUILD_PT_EXPT + dp.init("../../tests/infer/deeppot_dpa1.pt2"); +#endif + } void SetUp() override { -#ifndef BUILD_PYTORCH +#if !defined(BUILD_PYTORCH) || !BUILD_PT_EXPT GTEST_SKIP() << "Skip because PyTorch support is not enabled."; #endif - dp.init("../../tests/infer/deeppot_dpa1.pt2"); natoms = expected_e.size(); EXPECT_EQ(natoms * 3, expected_f.size()); @@ -97,8 +103,13 @@ class TestInferDeepPotDpa1PtExpt : public ::testing::Test { }; void TearDown() override {}; + + static void TearDownTestSuite() { dp = deepmd::DeepPot(); } }; +template +deepmd::DeepPot TestInferDeepPotDpa1PtExpt::dp; + TYPED_TEST_SUITE(TestInferDeepPotDpa1PtExpt, ValueTypes); TYPED_TEST(TestInferDeepPotDpa1PtExpt, cpu_build_nlist) { @@ -218,13 +229,18 @@ class TestInferDeepPotDpa1PtExptNoPbc : public ::testing::Test { double expected_tot_e; std::vector expected_tot_v; - deepmd::DeepPot dp; + static deepmd::DeepPot dp; + + static void SetUpTestSuite() { +#if defined(BUILD_PYTORCH) && BUILD_PT_EXPT + dp.init("../../tests/infer/deeppot_dpa1.pt2"); +#endif + } void SetUp() override { -#ifndef BUILD_PYTORCH +#if !defined(BUILD_PYTORCH) || !BUILD_PT_EXPT GTEST_SKIP() << "Skip because PyTorch support is not enabled."; #endif - dp.init("../../tests/infer/deeppot_dpa1.pt2"); natoms = expected_e.size(); EXPECT_EQ(natoms * 3, expected_f.size()); @@ -243,8 +259,13 @@ class TestInferDeepPotDpa1PtExptNoPbc : public ::testing::Test { }; void TearDown() override {}; + + static void TearDownTestSuite() { dp = deepmd::DeepPot(); } }; +template +deepmd::DeepPot TestInferDeepPotDpa1PtExptNoPbc::dp; + TYPED_TEST_SUITE(TestInferDeepPotDpa1PtExptNoPbc, ValueTypes); TYPED_TEST(TestInferDeepPotDpa1PtExptNoPbc, cpu_build_nlist) { diff --git a/source/api_cc/tests/test_deeppot_dpa2_ptexpt.cc b/source/api_cc/tests/test_deeppot_dpa2_ptexpt.cc index 4bbf1292b0..67bb8f129f 100644 --- a/source/api_cc/tests/test_deeppot_dpa2_ptexpt.cc +++ b/source/api_cc/tests/test_deeppot_dpa2_ptexpt.cc @@ -9,6 +9,7 @@ #include #include "DeepPot.h" +#include "DeepPotPTExpt.h" #include "neighbor_list.h" #include "test_utils.h" @@ -71,13 +72,18 @@ class TestInferDeepPotDpa2PtExpt : public ::testing::Test { double expected_tot_e; std::vector expected_tot_v; - deepmd::DeepPot dp; + static deepmd::DeepPot dp; + + static void SetUpTestSuite() { +#if defined(BUILD_PYTORCH) && BUILD_PT_EXPT + dp.init("../../tests/infer/deeppot_dpa2.pt2"); +#endif + } void SetUp() override { -#ifndef BUILD_PYTORCH +#if !defined(BUILD_PYTORCH) || !BUILD_PT_EXPT GTEST_SKIP() << "Skip because PyTorch support is not enabled."; #endif - dp.init("../../tests/infer/deeppot_dpa2.pt2"); natoms = expected_e.size(); EXPECT_EQ(natoms * 3, expected_f.size()); @@ -96,8 +102,13 @@ class TestInferDeepPotDpa2PtExpt : public ::testing::Test { }; void TearDown() override {}; + + static void TearDownTestSuite() { dp = deepmd::DeepPot(); } }; +template +deepmd::DeepPot TestInferDeepPotDpa2PtExpt::dp; + TYPED_TEST_SUITE(TestInferDeepPotDpa2PtExpt, ValueTypes); TYPED_TEST(TestInferDeepPotDpa2PtExpt, cpu_build_nlist) { @@ -492,13 +503,18 @@ class TestInferDeepPotDpa2PtExptNoPbc : public ::testing::Test { double expected_tot_e; std::vector expected_tot_v; - deepmd::DeepPot dp; + static deepmd::DeepPot dp; + + static void SetUpTestSuite() { +#if defined(BUILD_PYTORCH) && BUILD_PT_EXPT + dp.init("../../tests/infer/deeppot_dpa2.pt2"); +#endif + } void SetUp() override { -#ifndef BUILD_PYTORCH +#if !defined(BUILD_PYTORCH) || !BUILD_PT_EXPT GTEST_SKIP() << "Skip because PyTorch support is not enabled."; #endif - dp.init("../../tests/infer/deeppot_dpa2.pt2"); natoms = expected_e.size(); EXPECT_EQ(natoms * 3, expected_f.size()); @@ -517,8 +533,13 @@ class TestInferDeepPotDpa2PtExptNoPbc : public ::testing::Test { }; void TearDown() override {}; + + static void TearDownTestSuite() { dp = deepmd::DeepPot(); } }; +template +deepmd::DeepPot TestInferDeepPotDpa2PtExptNoPbc::dp; + TYPED_TEST_SUITE(TestInferDeepPotDpa2PtExptNoPbc, ValueTypes); TYPED_TEST(TestInferDeepPotDpa2PtExptNoPbc, cpu_build_nlist) { diff --git a/source/api_cc/tests/test_deeppot_dpa3_ptexpt.cc b/source/api_cc/tests/test_deeppot_dpa3_ptexpt.cc index 2767f5c641..a76a11e94f 100644 --- a/source/api_cc/tests/test_deeppot_dpa3_ptexpt.cc +++ b/source/api_cc/tests/test_deeppot_dpa3_ptexpt.cc @@ -9,6 +9,7 @@ #include #include "DeepPot.h" +#include "DeepPotPTExpt.h" #include "neighbor_list.h" #include "test_utils.h" @@ -71,13 +72,18 @@ class TestInferDeepPotDpa3PtExpt : public ::testing::Test { double expected_tot_e; std::vector expected_tot_v; - deepmd::DeepPot dp; + static deepmd::DeepPot dp; + + static void SetUpTestSuite() { +#if defined(BUILD_PYTORCH) && BUILD_PT_EXPT + dp.init("../../tests/infer/deeppot_dpa3.pt2"); +#endif + } void SetUp() override { -#ifndef BUILD_PYTORCH +#if !defined(BUILD_PYTORCH) || !BUILD_PT_EXPT GTEST_SKIP() << "Skip because PyTorch support is not enabled."; #endif - dp.init("../../tests/infer/deeppot_dpa3.pt2"); natoms = expected_e.size(); EXPECT_EQ(natoms * 3, expected_f.size()); @@ -96,8 +102,13 @@ class TestInferDeepPotDpa3PtExpt : public ::testing::Test { }; void TearDown() override {}; + + static void TearDownTestSuite() { dp = deepmd::DeepPot(); } }; +template +deepmd::DeepPot TestInferDeepPotDpa3PtExpt::dp; + TYPED_TEST_SUITE(TestInferDeepPotDpa3PtExpt, ValueTypes); TYPED_TEST(TestInferDeepPotDpa3PtExpt, cpu_build_nlist) { @@ -492,13 +503,18 @@ class TestInferDeepPotDpa3PtExptNoPbc : public ::testing::Test { double expected_tot_e; std::vector expected_tot_v; - deepmd::DeepPot dp; + static deepmd::DeepPot dp; + + static void SetUpTestSuite() { +#if defined(BUILD_PYTORCH) && BUILD_PT_EXPT + dp.init("../../tests/infer/deeppot_dpa3.pt2"); +#endif + } void SetUp() override { -#ifndef BUILD_PYTORCH +#if !defined(BUILD_PYTORCH) || !BUILD_PT_EXPT GTEST_SKIP() << "Skip because PyTorch support is not enabled."; #endif - dp.init("../../tests/infer/deeppot_dpa3.pt2"); natoms = expected_e.size(); EXPECT_EQ(natoms * 3, expected_f.size()); @@ -517,8 +533,13 @@ class TestInferDeepPotDpa3PtExptNoPbc : public ::testing::Test { }; void TearDown() override {}; + + static void TearDownTestSuite() { dp = deepmd::DeepPot(); } }; +template +deepmd::DeepPot TestInferDeepPotDpa3PtExptNoPbc::dp; + TYPED_TEST_SUITE(TestInferDeepPotDpa3PtExptNoPbc, ValueTypes); TYPED_TEST(TestInferDeepPotDpa3PtExptNoPbc, cpu_build_nlist) { diff --git a/source/api_cc/tests/test_deeppot_dpa_ptexpt.cc b/source/api_cc/tests/test_deeppot_dpa_ptexpt.cc index 769c166d12..faa51a7fde 100644 --- a/source/api_cc/tests/test_deeppot_dpa_ptexpt.cc +++ b/source/api_cc/tests/test_deeppot_dpa_ptexpt.cc @@ -8,6 +8,7 @@ #include #include "DeepPot.h" +#include "DeepPotPTExpt.h" #include "neighbor_list.h" #include "test_utils.h" @@ -70,13 +71,18 @@ class TestInferDeepPotDpaPtExpt : public ::testing::Test { double expected_tot_e; std::vector expected_tot_v; - deepmd::DeepPot dp; + static deepmd::DeepPot dp; + + static void SetUpTestSuite() { +#if defined(BUILD_PYTORCH) && BUILD_PT_EXPT + dp.init("../../tests/infer/deeppot_dpa1.pt2"); +#endif + } void SetUp() override { -#ifndef BUILD_PYTORCH +#if !defined(BUILD_PYTORCH) || !BUILD_PT_EXPT GTEST_SKIP() << "Skip because PyTorch support is not enabled."; #endif - dp.init("../../tests/infer/deeppot_dpa1.pt2"); natoms = expected_e.size(); EXPECT_EQ(natoms * 3, expected_f.size()); @@ -95,8 +101,13 @@ class TestInferDeepPotDpaPtExpt : public ::testing::Test { }; void TearDown() override {}; + + static void TearDownTestSuite() { dp = deepmd::DeepPot(); } }; +template +deepmd::DeepPot TestInferDeepPotDpaPtExpt::dp; + TYPED_TEST_SUITE(TestInferDeepPotDpaPtExpt, ValueTypes); TYPED_TEST(TestInferDeepPotDpaPtExpt, cpu_build_nlist) { @@ -482,13 +493,18 @@ class TestInferDeepPotDpaPtExptNoPbc : public ::testing::Test { double expected_tot_e; std::vector expected_tot_v; - deepmd::DeepPot dp; + static deepmd::DeepPot dp; + + static void SetUpTestSuite() { +#if defined(BUILD_PYTORCH) && BUILD_PT_EXPT + dp.init("../../tests/infer/deeppot_dpa1.pt2"); +#endif + } void SetUp() override { -#ifndef BUILD_PYTORCH +#if !defined(BUILD_PYTORCH) || !BUILD_PT_EXPT GTEST_SKIP() << "Skip because PyTorch support is not enabled."; #endif - dp.init("../../tests/infer/deeppot_dpa1.pt2"); natoms = expected_e.size(); EXPECT_EQ(natoms * 3, expected_f.size()); @@ -507,8 +523,13 @@ class TestInferDeepPotDpaPtExptNoPbc : public ::testing::Test { }; void TearDown() override {}; + + static void TearDownTestSuite() { dp = deepmd::DeepPot(); } }; +template +deepmd::DeepPot TestInferDeepPotDpaPtExptNoPbc::dp; + TYPED_TEST_SUITE(TestInferDeepPotDpaPtExptNoPbc, ValueTypes); TYPED_TEST(TestInferDeepPotDpaPtExptNoPbc, cpu_build_nlist) { diff --git a/source/api_cc/tests/test_deeppot_model_devi_ptexpt.cc b/source/api_cc/tests/test_deeppot_model_devi_ptexpt.cc index 69f5082295..6da4185adf 100644 --- a/source/api_cc/tests/test_deeppot_model_devi_ptexpt.cc +++ b/source/api_cc/tests/test_deeppot_model_devi_ptexpt.cc @@ -9,6 +9,7 @@ #include #include "DeepPot.h" +#include "DeepPotPTExpt.h" #include "neighbor_list.h" #include "test_utils.h" @@ -32,25 +33,43 @@ class TestInferDeepPotModeDeviPtExpt : public ::testing::Test { 0.25852028, 0.25852028, 0.25852028}; int natoms; - deepmd::DeepPot dp0; - deepmd::DeepPot dp1; - deepmd::DeepPotModelDevi dp_md; + static deepmd::DeepPot dp0; + static deepmd::DeepPot dp1; + static deepmd::DeepPotModelDevi dp_md; - void SetUp() override { -#ifndef BUILD_PYTORCH - GTEST_SKIP() << "Skip because PyTorch support is not enabled."; -#endif + static void SetUpTestSuite() { +#if defined(BUILD_PYTORCH) && BUILD_PT_EXPT dp0.init("../../tests/infer/model_devi_md0.pt2"); dp1.init("../../tests/infer/model_devi_md1.pt2"); dp_md.init( std::vector({"../../tests/infer/model_devi_md0.pt2", "../../tests/infer/model_devi_md1.pt2"})); +#endif + } + + void SetUp() override { +#if !defined(BUILD_PYTORCH) || !BUILD_PT_EXPT + GTEST_SKIP() << "Skip because PyTorch support is not enabled."; +#endif natoms = coord.size() / 3; }; void TearDown() override {}; + + static void TearDownTestSuite() { + dp0 = deepmd::DeepPot(); + dp1 = deepmd::DeepPot(); + dp_md = deepmd::DeepPotModelDevi(); + } }; +template +deepmd::DeepPot TestInferDeepPotModeDeviPtExpt::dp0; +template +deepmd::DeepPot TestInferDeepPotModeDeviPtExpt::dp1; +template +deepmd::DeepPotModelDevi TestInferDeepPotModeDeviPtExpt::dp_md; + TYPED_TEST_SUITE(TestInferDeepPotModeDeviPtExpt, ValueTypes); TYPED_TEST(TestInferDeepPotModeDeviPtExpt, attrs) { @@ -414,25 +433,44 @@ class TestInferDeepPotModeDeviPtExptPrecomputed : public ::testing::Test { 1.735782978938303146e-04, 2.441022592033971239e-05, 7.333090508662540210e-05}; // max min mystd - deepmd::DeepPot dp0; - deepmd::DeepPot dp1; - deepmd::DeepPotModelDevi dp_md; + static deepmd::DeepPot dp0; + static deepmd::DeepPot dp1; + static deepmd::DeepPotModelDevi dp_md; - void SetUp() override { -#ifndef BUILD_PYTORCH - GTEST_SKIP() << "Skip because PyTorch support is not enabled."; -#endif + static void SetUpTestSuite() { +#if defined(BUILD_PYTORCH) && BUILD_PT_EXPT dp0.init("../../tests/infer/model_devi_md0.pt2"); dp1.init("../../tests/infer/model_devi_md1.pt2"); dp_md.init( std::vector({"../../tests/infer/model_devi_md0.pt2", "../../tests/infer/model_devi_md1.pt2"})); +#endif + } + + void SetUp() override { +#if !defined(BUILD_PYTORCH) || !BUILD_PT_EXPT + GTEST_SKIP() << "Skip because PyTorch support is not enabled."; +#endif natoms = coord.size() / 3; }; void TearDown() override {}; + + static void TearDownTestSuite() { + dp0 = deepmd::DeepPot(); + dp1 = deepmd::DeepPot(); + dp_md = deepmd::DeepPotModelDevi(); + } }; +template +deepmd::DeepPot TestInferDeepPotModeDeviPtExptPrecomputed::dp0; +template +deepmd::DeepPot TestInferDeepPotModeDeviPtExptPrecomputed::dp1; +template +deepmd::DeepPotModelDevi + TestInferDeepPotModeDeviPtExptPrecomputed::dp_md; + TYPED_TEST_SUITE(TestInferDeepPotModeDeviPtExptPrecomputed, ValueTypes); template diff --git a/source/api_cc/tests/test_deeppot_ptexpt.cc b/source/api_cc/tests/test_deeppot_ptexpt.cc index 2cda44bb19..cfde173a82 100644 --- a/source/api_cc/tests/test_deeppot_ptexpt.cc +++ b/source/api_cc/tests/test_deeppot_ptexpt.cc @@ -10,6 +10,7 @@ #include #include "DeepPot.h" +#include "DeepPotPTExpt.h" #include "neighbor_list.h" #include "test_utils.h" @@ -54,15 +55,18 @@ class TestInferDeepPotAPtExpt : public ::testing::Test { double expected_tot_e; std::vector expected_tot_v; - deepmd::DeepPot dp; + static deepmd::DeepPot dp; + + static void SetUpTestSuite() { +#if defined(BUILD_PYTORCH) && BUILD_PT_EXPT + dp.init("../../tests/infer/deeppot_sea.pt2"); +#endif + } void SetUp() override { -#ifndef BUILD_PYTORCH +#if !defined(BUILD_PYTORCH) || !BUILD_PT_EXPT GTEST_SKIP() << "Skip because PyTorch support is not enabled."; #endif - std::string file_name = "../../tests/infer/deeppot_sea.pt2"; - - dp.init(file_name); natoms = expected_e.size(); EXPECT_EQ(natoms * 3, expected_f.size()); @@ -81,8 +85,13 @@ class TestInferDeepPotAPtExpt : public ::testing::Test { }; void TearDown() override {}; + + static void TearDownTestSuite() { dp = deepmd::DeepPot(); } }; +template +deepmd::DeepPot TestInferDeepPotAPtExpt::dp; + TYPED_TEST_SUITE(TestInferDeepPotAPtExpt, ValueTypes); TYPED_TEST(TestInferDeepPotAPtExpt, cpu_build_nlist) { @@ -452,14 +461,18 @@ class TestInferDeepPotAPtExptNoPbc : public ::testing::Test { double expected_tot_e; std::vector expected_tot_v; - deepmd::DeepPot dp; + static deepmd::DeepPot dp; + + static void SetUpTestSuite() { +#if defined(BUILD_PYTORCH) && BUILD_PT_EXPT + dp.init("../../tests/infer/deeppot_sea.pt2"); +#endif + } void SetUp() override { -#ifndef BUILD_PYTORCH +#if !defined(BUILD_PYTORCH) || !BUILD_PT_EXPT GTEST_SKIP() << "Skip because PyTorch support is not enabled."; #endif - std::string file_name = "../../tests/infer/deeppot_sea.pt2"; - dp.init(file_name); natoms = expected_e.size(); EXPECT_EQ(natoms * 3, expected_f.size()); @@ -478,8 +491,13 @@ class TestInferDeepPotAPtExptNoPbc : public ::testing::Test { }; void TearDown() override {}; + + static void TearDownTestSuite() { dp = deepmd::DeepPot(); } }; +template +deepmd::DeepPot TestInferDeepPotAPtExptNoPbc::dp; + TYPED_TEST_SUITE(TestInferDeepPotAPtExptNoPbc, ValueTypes); TYPED_TEST(TestInferDeepPotAPtExptNoPbc, cpu_build_nlist) { @@ -623,7 +641,7 @@ TYPED_TEST(TestInferDeepPotAPtExptNoPbc, cpu_build_nlist_nframes) { // ========== Parser / metadata coverage tests ========== TEST(TestDeepPotPTExptParser, load_nonexistent_file) { -#ifndef BUILD_PYTORCH +#if !defined(BUILD_PYTORCH) || !BUILD_PT_EXPT GTEST_SKIP() << "Skip because PyTorch support is not enabled."; #endif deepmd::DeepPot dp; @@ -631,7 +649,7 @@ TEST(TestDeepPotPTExptParser, load_nonexistent_file) { } TEST(TestDeepPotPTExptParser, load_invalid_zip) { -#ifndef BUILD_PYTORCH +#if !defined(BUILD_PYTORCH) || !BUILD_PT_EXPT GTEST_SKIP() << "Skip because PyTorch support is not enabled."; #endif std::string tmpfile = "test_invalid.pt2"; @@ -646,7 +664,7 @@ TEST(TestDeepPotPTExptParser, load_invalid_zip) { } TEST(TestDeepPotPTExptParser, load_tiny_file) { -#ifndef BUILD_PYTORCH +#if !defined(BUILD_PYTORCH) || !BUILD_PT_EXPT GTEST_SKIP() << "Skip because PyTorch support is not enabled."; #endif std::string tmpfile = "test_tiny.pt2"; @@ -664,16 +682,25 @@ TEST(TestDeepPotPTExptParser, load_tiny_file) { template class TestDeepPotPTExptMetadata : public ::testing::Test { protected: - deepmd::DeepPot dp; + static deepmd::DeepPot dp; + static void SetUpTestSuite() { +#if defined(BUILD_PYTORCH) && BUILD_PT_EXPT + dp.init("../../tests/infer/deeppot_sea.pt2"); +#endif + } void SetUp() override { -#ifndef BUILD_PYTORCH +#if !defined(BUILD_PYTORCH) || !BUILD_PT_EXPT GTEST_SKIP() << "Skip because PyTorch support is not enabled."; #endif - dp.init("../../tests/infer/deeppot_sea.pt2"); }; void TearDown() override {}; + + static void TearDownTestSuite() { dp = deepmd::DeepPot(); } }; +template +deepmd::DeepPot TestDeepPotPTExptMetadata::dp; + TYPED_TEST_SUITE(TestDeepPotPTExptMetadata, ValueTypes); TYPED_TEST(TestDeepPotPTExptMetadata, type_map) { @@ -707,16 +734,25 @@ TYPED_TEST(TestDeepPotPTExptMetadata, no_default_fparam) { template class TestDeepPotPTExptJsonTypes : public ::testing::Test { protected: - deepmd::DeepPot dp; + static deepmd::DeepPot dp; + static void SetUpTestSuite() { +#if defined(BUILD_PYTORCH) && BUILD_PT_EXPT + dp.init("../../tests/infer/fparam_aparam.pt2"); +#endif + } void SetUp() override { -#ifndef BUILD_PYTORCH +#if !defined(BUILD_PYTORCH) || !BUILD_PT_EXPT GTEST_SKIP() << "Skip because PyTorch support is not enabled."; #endif - dp.init("../../tests/infer/fparam_aparam.pt2"); }; void TearDown() override {}; + + static void TearDownTestSuite() { dp = deepmd::DeepPot(); } }; +template +deepmd::DeepPot TestDeepPotPTExptJsonTypes::dp; + TYPED_TEST_SUITE(TestDeepPotPTExptJsonTypes, ValueTypes); TYPED_TEST(TestDeepPotPTExptJsonTypes, integer_fields) { @@ -743,16 +779,25 @@ TYPED_TEST(TestDeepPotPTExptJsonTypes, float_field) { template class TestDeepPotPTExptJsonDefaults : public ::testing::Test { protected: - deepmd::DeepPot dp; + static deepmd::DeepPot dp; + static void SetUpTestSuite() { +#if defined(BUILD_PYTORCH) && BUILD_PT_EXPT + dp.init("../../tests/infer/fparam_aparam_default.pt2"); +#endif + } void SetUp() override { -#ifndef BUILD_PYTORCH +#if !defined(BUILD_PYTORCH) || !BUILD_PT_EXPT GTEST_SKIP() << "Skip because PyTorch support is not enabled."; #endif - dp.init("../../tests/infer/fparam_aparam_default.pt2"); }; void TearDown() override {}; + + static void TearDownTestSuite() { dp = deepmd::DeepPot(); } }; +template +deepmd::DeepPot TestDeepPotPTExptJsonDefaults::dp; + TYPED_TEST_SUITE(TestDeepPotPTExptJsonDefaults, ValueTypes); TYPED_TEST(TestDeepPotPTExptJsonDefaults, boolean_true) { diff --git a/source/install/test_cc_local.sh b/source/install/test_cc_local.sh index 0866eb45b8..b49858daff 100755 --- a/source/install/test_cc_local.sh +++ b/source/install/test_cc_local.sh @@ -59,6 +59,9 @@ else: # runtime to be preloaded (otherwise dlopen fails). We disable leak detection # in the gen scripts to avoid false reports from torch/paddle internals. INFER_SCRIPT_PATH=${SCRIPT_PATH}/../tests/infer + # Remove stale generated model files so they can't be accidentally reused + # if gen scripts change format or the code version changes. + rm -f ${INFER_SCRIPT_PATH}/*.pt2 ${INFER_SCRIPT_PATH}/*.pte _GEN_ENV="" if echo "${CXXFLAGS:-}" | grep -q fsanitize=leak; then _LSAN_LIB=$(gcc -print-file-name=liblsan.so 2>/dev/null || true) @@ -66,12 +69,27 @@ else: _GEN_ENV="LD_PRELOAD=${_LSAN_LIB} LSAN_OPTIONS=detect_leaks=0" fi fi - env ${_GEN_ENV} python ${INFER_SCRIPT_PATH}/gen_sea.py - env ${_GEN_ENV} python ${INFER_SCRIPT_PATH}/gen_dpa1.py - env ${_GEN_ENV} python ${INFER_SCRIPT_PATH}/gen_dpa2.py - env ${_GEN_ENV} python ${INFER_SCRIPT_PATH}/gen_dpa3.py - env ${_GEN_ENV} python ${INFER_SCRIPT_PATH}/gen_fparam_aparam.py - env ${_GEN_ENV} python ${INFER_SCRIPT_PATH}/gen_model_devi.py + # Run gen scripts in parallel (2 groups of 3) for faster model generation. + # Wait on each PID separately so any failure is caught by set -e. + env ${_GEN_ENV} python ${INFER_SCRIPT_PATH}/gen_sea.py & + PID1=$! + env ${_GEN_ENV} python ${INFER_SCRIPT_PATH}/gen_dpa1.py & + PID2=$! + env ${_GEN_ENV} python ${INFER_SCRIPT_PATH}/gen_dpa2.py & + PID3=$! + wait $PID1 + wait $PID2 + wait $PID3 + + env ${_GEN_ENV} python ${INFER_SCRIPT_PATH}/gen_dpa3.py & + PID4=$! + env ${_GEN_ENV} python ${INFER_SCRIPT_PATH}/gen_fparam_aparam.py & + PID5=$! + env ${_GEN_ENV} python ${INFER_SCRIPT_PATH}/gen_model_devi.py & + PID6=$! + wait $PID4 + wait $PID5 + wait $PID6 fi if [ "${ENABLE_PADDLE:-TRUE}" == "TRUE" ]; then PADDLE_INFERENCE_DIR=${BUILD_TMP_DIR}/paddle_inference_install_dir diff --git a/source/tests/pt_expt/model/test_model_compression.py b/source/tests/pt_expt/model/test_model_compression.py index fb7681a8bb..95a66b528d 100644 --- a/source/tests/pt_expt/model/test_model_compression.py +++ b/source/tests/pt_expt/model/test_model_compression.py @@ -352,20 +352,10 @@ def test_freeze_compress_eval_pt2(self) -> None: if os.path.exists(compressed_pt2_path): os.unlink(compressed_pt2_path) - def test_min_nbor_dist_roundtrip_pt2(self) -> None: - """Test that min_nbor_dist survives freeze → load round-trip via .pt2.""" - md = self._make_model() - md.min_nbor_dist = 0.5 - - model_data = {"model": md.serialize(), "min_nbor_dist": 0.5} - with tempfile.NamedTemporaryFile(suffix=".pt2", delete=False) as f: - pt2_path = f.name - try: - deserialize_to_file(pt2_path, model_data) - loaded_data = serialize_from_file(pt2_path) - self.assertAlmostEqual(loaded_data["min_nbor_dist"], 0.5) - finally: - os.unlink(pt2_path) + # test_min_nbor_dist_roundtrip_pt2 removed: .pte and .pt2 share identical + # metadata storage (metadata.json in ZIP), so the .pte round-trip test + # (test_min_nbor_dist_roundtrip) already covers this path. Removing saves + # one redundant AOTInductor compilation (~82s). if __name__ == "__main__": diff --git a/source/tests/pt_expt/test_change_bias.py b/source/tests/pt_expt/test_change_bias.py index 9dcd1811e3..03329642e9 100644 --- a/source/tests/pt_expt/test_change_bias.py +++ b/source/tests/pt_expt/test_change_bias.py @@ -147,6 +147,24 @@ def setUpClass(cls) -> None: # Record original bias cls.original_bias = to_numpy(trainer.wrapper.model.get_out_bias()) + # Pre-freeze shared .pte and .pt2 files so individual tests don't + # each pay the AOTInductor compilation cost (~82s per .pt2). + from deepmd.pt_expt.entrypoints.main import ( + freeze, + ) + + cls.shared_pte = os.path.join(cls.tmpdir, "shared.pte") + freeze(model=cls.model_path, output=cls.shared_pte) + cls.shared_pt2 = os.path.join(cls.tmpdir, "shared.pt2") + # Clear default device: tests/pt/__init__.py may set a fake device + # for CPU fallback, which poisons AOTInductor compilation. + saved_device = torch.get_default_device() + torch.set_default_device(None) + try: + freeze(model=cls.model_path, output=cls.shared_pt2) + finally: + torch.set_default_device(saved_device) + @classmethod def tearDownClass(cls) -> None: os.chdir(cls.old_cwd) @@ -213,9 +231,6 @@ def test_change_bias_with_user_defined(self) -> None: np.testing.assert_allclose(updated_bias, expected_bias) def test_change_bias_frozen_pte(self) -> None: - from deepmd.pt_expt.entrypoints.main import ( - freeze, - ) from deepmd.pt_expt.model.model import ( BaseModel, ) @@ -223,19 +238,15 @@ def test_change_bias_frozen_pte(self) -> None: serialize_from_file, ) - # Freeze the checkpoint - pte_path = os.path.join(self.tmpdir, "frozen.pte") - freeze(model=self.model_path, output=pte_path) - - # Get original bias - original_data = serialize_from_file(pte_path) + # Get original bias from shared frozen file + original_data = serialize_from_file(self.shared_pte) original_model = BaseModel.deserialize(original_data["model"]) original_bias = to_numpy(original_model.get_out_bias()) # Run change-bias on the frozen model output_pte = os.path.join(self.tmpdir, "frozen_updated.pte") run_dp( - f"dp --pt-expt change-bias {pte_path} " + f"dp --pt-expt change-bias {self.shared_pte} " f"-s {self.data_file[0]} -o {output_pte}" ) @@ -252,9 +263,6 @@ def test_change_bias_frozen_pte(self) -> None: def test_change_bias_frozen_pt2(self) -> None: """Change-bias on a .pt2 frozen model.""" - from deepmd.pt_expt.entrypoints.main import ( - freeze, - ) from deepmd.pt_expt.model.model import ( BaseModel, ) @@ -262,16 +270,13 @@ def test_change_bias_frozen_pt2(self) -> None: serialize_from_file, ) - pt2_path = os.path.join(self.tmpdir, "frozen.pt2") - freeze(model=self.model_path, output=pt2_path) - - original_data = serialize_from_file(pt2_path) + original_data = serialize_from_file(self.shared_pt2) original_model = BaseModel.deserialize(original_data["model"]) original_bias = to_numpy(original_model.get_out_bias()) output_pt2 = os.path.join(self.tmpdir, "frozen_updated.pt2") run_dp( - f"dp --pt-expt change-bias {pt2_path} " + f"dp --pt-expt change-bias {self.shared_pt2} " f"-s {self.data_file[0]} -o {output_pt2}" ) @@ -286,9 +291,6 @@ def test_change_bias_frozen_pt2(self) -> None: def test_change_bias_frozen_pt2_user_defined(self) -> None: """Change-bias with user-defined values on a .pt2 model.""" - from deepmd.pt_expt.entrypoints.main import ( - freeze, - ) from deepmd.pt_expt.model.model import ( BaseModel, ) @@ -296,11 +298,8 @@ def test_change_bias_frozen_pt2_user_defined(self) -> None: serialize_from_file, ) - pt2_path = os.path.join(self.tmpdir, "frozen_ud.pt2") - freeze(model=self.model_path, output=pt2_path) - output_pt2 = os.path.join(self.tmpdir, "frozen_ud_updated.pt2") - run_dp(f"dp --pt-expt change-bias {pt2_path} -b 1.0 2.0 -o {output_pt2}") + run_dp(f"dp --pt-expt change-bias {self.shared_pt2} -b 1.0 2.0 -o {output_pt2}") updated_data = serialize_from_file(output_pt2) updated_model = BaseModel.deserialize(updated_data["model"]) @@ -311,9 +310,6 @@ def test_change_bias_frozen_pt2_user_defined(self) -> None: def test_change_bias_pt2_pte_consistency(self) -> None: """Change-bias on .pte and .pt2 should produce same bias values.""" - from deepmd.pt_expt.entrypoints.main import ( - freeze, - ) from deepmd.pt_expt.model.model import ( BaseModel, ) @@ -321,19 +317,14 @@ def test_change_bias_pt2_pte_consistency(self) -> None: serialize_from_file, ) - pte_path = os.path.join(self.tmpdir, "cons.pte") - pt2_path = os.path.join(self.tmpdir, "cons.pt2") - freeze(model=self.model_path, output=pte_path) - freeze(model=self.model_path, output=pt2_path) - output_pte = os.path.join(self.tmpdir, "cons_updated.pte") output_pt2 = os.path.join(self.tmpdir, "cons_updated.pt2") run_dp( - f"dp --pt-expt change-bias {pte_path} " + f"dp --pt-expt change-bias {self.shared_pte} " f"-s {self.data_file[0]} -o {output_pte}" ) run_dp( - f"dp --pt-expt change-bias {pt2_path} " + f"dp --pt-expt change-bias {self.shared_pt2} " f"-s {self.data_file[0]} -o {output_pt2}" ) @@ -346,26 +337,19 @@ def test_change_bias_pt2_pte_consistency(self) -> None: def test_change_bias_pte_preserves_model_def_script(self) -> None: """change_bias on .pte should preserve model_def_script (training config).""" - from deepmd.pt_expt.entrypoints.main import ( - freeze, - ) from deepmd.pt_expt.utils.serialization import ( serialize_from_file, ) - # Freeze the checkpoint (embeds training config as model_def_script) - pte_path = os.path.join(self.tmpdir, "frozen_mds.pte") - freeze(model=self.model_path, output=pte_path) - - # Verify training config is present - original_data = serialize_from_file(pte_path) + # Verify training config is present in shared frozen file + original_data = serialize_from_file(self.shared_pte) self.assertIn("model_def_script", original_data) original_mds = original_data["model_def_script"] self.assertIn("type_map", original_mds) # training config has model params # Run change-bias with user-defined values output_pte = os.path.join(self.tmpdir, "frozen_mds_updated.pte") - run_dp(f"dp --pt-expt change-bias {pte_path} -b 0.1 3.2 -o {output_pte}") + run_dp(f"dp --pt-expt change-bias {self.shared_pte} -b 0.1 3.2 -o {output_pte}") # Verify model_def_script is preserved in the output updated_data = serialize_from_file(output_pte) @@ -374,36 +358,19 @@ def test_change_bias_pte_preserves_model_def_script(self) -> None: def test_change_bias_pt2_preserves_model_def_script(self) -> None: """change_bias on .pt2 should preserve model_def_script (training config).""" - from deepmd.pt_expt.entrypoints.main import ( - freeze, - ) from deepmd.pt_expt.utils.serialization import ( serialize_from_file, ) - # Freeze to .pt2 - # Clear default device: tests/pt/__init__.py sets it to "cuda:9999999" - # for CPU fallback, which poisons AOTInductor compilation. - pt2_path = os.path.join(self.tmpdir, "frozen_mds.pt2") - torch.set_default_device(None) - try: - freeze(model=self.model_path, output=pt2_path) - finally: - torch.set_default_device("cuda:9999999") - - # Verify training config is present - original_data = serialize_from_file(pt2_path) + # Verify training config is present in shared frozen file + original_data = serialize_from_file(self.shared_pt2) self.assertIn("model_def_script", original_data) original_mds = original_data["model_def_script"] self.assertIn("type_map", original_mds) # training config has model params - # Run change-bias with user-defined values (same device workaround) + # Run change-bias with user-defined values output_pt2 = os.path.join(self.tmpdir, "frozen_mds_updated.pt2") - torch.set_default_device(None) - try: - run_dp(f"dp --pt-expt change-bias {pt2_path} -b 0.1 3.2 -o {output_pt2}") - finally: - torch.set_default_device("cuda:9999999") + run_dp(f"dp --pt-expt change-bias {self.shared_pt2} -b 0.1 3.2 -o {output_pt2}") # Verify model_def_script is preserved in the output updated_data = serialize_from_file(output_pt2) diff --git a/source/tests/pt_expt/test_dp_freeze.py b/source/tests/pt_expt/test_dp_freeze.py index 60eab4f50a..add8313752 100644 --- a/source/tests/pt_expt/test_dp_freeze.py +++ b/source/tests/pt_expt/test_dp_freeze.py @@ -57,6 +57,13 @@ def setUpClass(cls) -> None: cls.ckpt_file = os.path.join(cls.tmpdir, "model.pt") torch.save({"model": state_dict}, cls.ckpt_file) + # Pre-freeze shared .pte and .pt2 files so individual tests don't + # each pay the AOTInductor compilation cost (~82s per .pt2). + cls.shared_pte = os.path.join(cls.tmpdir, "shared.pte") + freeze(model=cls.ckpt_file, output=cls.shared_pte) + cls.shared_pt2 = os.path.join(cls.tmpdir, "shared.pt2") + freeze(model=cls.ckpt_file, output=cls.shared_pt2) + @classmethod def tearDownClass(cls) -> None: shutil.rmtree(cls.tmpdir) @@ -98,9 +105,7 @@ def test_freeze_default_suffix(self) -> None: def test_freeze_pt2(self) -> None: """Freeze to .pt2 (AOTInductor) and verify the file is loadable.""" - output = os.path.join(self.tmpdir, "frozen_model.pt2") - freeze(model=self.ckpt_file, output=output) - self.assertTrue(os.path.exists(output)) + self.assertTrue(os.path.exists(self.shared_pt2)) # Verify the .pt2 can be loaded and evaluated via DeepPot import numpy as np @@ -109,7 +114,7 @@ def test_freeze_pt2(self) -> None: DeepPot, ) - dp = DeepPot(output) + dp = DeepPot(self.shared_pt2) self.assertEqual(dp.get_type_map(), ["O", "H", "B"]) rcut = dp.get_rcut() self.assertGreater(rcut, 0.0) @@ -134,11 +139,6 @@ def test_freeze_pt2_eval_consistency(self) -> None: DeepPot, ) - pte_path = os.path.join(self.tmpdir, "consistency.pte") - pt2_path = os.path.join(self.tmpdir, "consistency.pt2") - freeze(model=self.ckpt_file, output=pte_path) - freeze(model=self.ckpt_file, output=pt2_path) - coord = np.array( [0.0, 0.0, 0.1, 1.0, 0.3, 0.2, 0.1, 1.9, 3.4], dtype=np.float64, @@ -146,8 +146,8 @@ def test_freeze_pt2_eval_consistency(self) -> None: box = np.array([5.0, 0.0, 0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 5.0], dtype=np.float64) atype = [0, 1, 2] - dp_pte = DeepPot(pte_path) - dp_pt2 = DeepPot(pt2_path) + dp_pte = DeepPot(self.shared_pte) + dp_pt2 = DeepPot(self.shared_pt2) e_pte, f_pte, v_pte = dp_pte.eval(coord, box, atype) e_pt2, f_pt2, v_pt2 = dp_pt2.eval(coord, box, atype) @@ -171,11 +171,6 @@ def test_freeze_pt2_nopbc_negative_coords(self) -> None: DeepPot, ) - pte_path = os.path.join(self.tmpdir, "nopbc_neg.pte") - pt2_path = os.path.join(self.tmpdir, "nopbc_neg.pt2") - freeze(model=self.ckpt_file, output=pte_path) - freeze(model=self.ckpt_file, output=pt2_path) - # Coordinates with negative values — no periodic box coord = np.array( [-1.0, -2.0, 0.5, 1.0, 0.3, -0.2, 0.1, -1.9, 3.4], @@ -183,8 +178,8 @@ def test_freeze_pt2_nopbc_negative_coords(self) -> None: ) atype = [0, 1, 2] - dp_pte = DeepPot(pte_path) - dp_pt2 = DeepPot(pt2_path) + dp_pte = DeepPot(self.shared_pte) + dp_pt2 = DeepPot(self.shared_pt2) e_pte, f_pte, v_pte = dp_pte.eval(coord, None, atype) e_pt2, f_pt2, v_pt2 = dp_pt2.eval(coord, None, atype)