From 8e9694d008f064bb2f09a667d63a39320971465c Mon Sep 17 00:00:00 2001 From: Lysithea Date: Tue, 22 Oct 2024 21:18:02 +0800 Subject: [PATCH 1/9] fix error in nopbc (nghost = 0) --- source/api_cc/src/DeepPotPT.cc | 10 +---- source/lmp/tests/test_lammps_dpa_pt_nopbc.py | 8 ---- source/op/pt/comm.cc | 43 ++++++++++++-------- 3 files changed, 29 insertions(+), 32 deletions(-) diff --git a/source/api_cc/src/DeepPotPT.cc b/source/api_cc/src/DeepPotPT.cc index 84629042f4..8ed9ae3cf1 100644 --- a/source/api_cc/src/DeepPotPT.cc +++ b/source/api_cc/src/DeepPotPT.cc @@ -168,7 +168,7 @@ void DeepPotPT::compute(ENERGYVTYPE& ener, nlist_data.copy_from_nlist(lmp_list); nlist_data.shuffle_exclude_empty(fwd_map); nlist_data.padding(); - if (do_message_passing == 1 && nghost > 0) { + if (do_message_passing == 1) { int nswap = lmp_list.nswap; torch::Tensor sendproc_tensor = torch::from_blob(lmp_list.sendproc, {nswap}, int32_option); @@ -196,12 +196,6 @@ void DeepPotPT::compute(ENERGYVTYPE& ener, comm_dict.insert("recv_num", recvnum_tensor); comm_dict.insert("communicator", communicator_tensor); } - if (do_message_passing == 1 && nghost == 0) { - // for the situation that no ghost atoms (e.g. serial nopbc) - // set the mapping arange(nloc) is enough - auto option = torch::TensorOptions().device(device).dtype(torch::kInt64); - mapping_tensor = at::arange(nloc_real, option).unsqueeze(0); - } } at::Tensor firstneigh = createNlistTensor(nlist_data.jlist); firstneigh_tensor = firstneigh.to(torch::kInt64).to(device); @@ -224,7 +218,7 @@ void DeepPotPT::compute(ENERGYVTYPE& ener, .to(device); } c10::Dict outputs = - (do_message_passing == 1 && nghost > 0) + (do_message_passing == 1 ) ? module .run_method("forward_lower", coord_wrapped_Tensor, atype_Tensor, firstneigh_tensor, mapping_tensor, fparam_tensor, diff --git a/source/lmp/tests/test_lammps_dpa_pt_nopbc.py b/source/lmp/tests/test_lammps_dpa_pt_nopbc.py index 15fe2c0bc2..b0909bfc03 100644 --- a/source/lmp/tests/test_lammps_dpa_pt_nopbc.py +++ b/source/lmp/tests/test_lammps_dpa_pt_nopbc.py @@ -681,14 +681,6 @@ def test_pair_deepmd_si(lammps_si): [(["--balance"],), ([],)], ) def test_pair_deepmd_mpi(balance_args: list): - if balance_args == []: - # python:5331 terminated with signal 11 at PC=7f3e940e3806 SP=7ffd5787edc0. Backtrace: - # /home/runner/work/deepmd-kit/deepmd-kit/dp_test/lib/libdeepmd_op_pt.so(+0x95806)[0x7f3e940e3806] - # /home/runner/work/deepmd-kit/deepmd-kit/dp_test/lib/libdeepmd_op_pt.so(+0x8f76e)[0x7f3e940dd76e] - # /home/runner/work/deepmd-kit/deepmd-kit/dp_test/lib/libdeepmd_op_pt.so(+0x9a38a)[0x7f3e940e838a] - # /home/runner/work/deepmd-kit/deepmd-kit/dp_test/lib/libdeepmd_op_pt.so(_Z9border_opRKN2at6TensorES2_S2_S2_S2_S2_S2_S2_S2_+0x8e)[0x7f3e940dda63] - # /home/runner/work/deepmd-kit/deepmd-kit/dp_test/lib/libdeepmd_op_pt.so(+0xaeac3)[0x7f3e940fcac3] - pytest.skip(reason="Known segfault, see comments for details") with tempfile.NamedTemporaryFile() as f: sp.check_call( [ diff --git a/source/op/pt/comm.cc b/source/op/pt/comm.cc index a25dfbd542..aa5c6f51c7 100644 --- a/source/op/pt/comm.cc +++ b/source/op/pt/comm.cc @@ -120,11 +120,17 @@ class Border : public torch::autograd::Function { for (int iswap = 0; iswap < nswap; ++iswap) { int nrecv = recvnum[iswap]; int nsend = sendnum[iswap]; - torch::Tensor isendlist = - torch::from_blob(sendlist[iswap], {nsend}, int32_options) - .to(recv_g1_tensor.device()); - torch::Tensor send_g1_tensor = recv_g1_tensor.index_select(0, isendlist); - FPTYPE* send_g1 = send_g1_tensor.data_ptr(); + torch::Tensor isendlist; + torch::Tensor send_g1_tensor; + FPTYPE* send_g1; + if(nsend != 0) + { + isendlist = + torch::from_blob(sendlist[iswap], {nsend}, int32_options) + .to(recv_g1_tensor.device()); + send_g1_tensor = recv_g1_tensor.index_select(0, isendlist); + send_g1 = send_g1_tensor.data_ptr(); + } #ifdef USE_MPI if (sendproc[iswap] != me) { if (nrecv) { @@ -248,17 +254,22 @@ class Border : public torch::autograd::Function { int nlocal = nlocal_tensor.item(); int nghost = nghost_tensor.item(); int ntotal = nlocal + nghost; - - torch::Tensor send_g1_tensor = d_local_g1_tensor; - - int max_recvnum = sendnum_tensor.max().item(); - auto options = torch::TensorOptions() - .dtype(d_local_g1_tensor.dtype()) - .device(d_local_g1_tensor.device()); - torch::Tensor recv_g1_tensor = - torch::empty({max_recvnum, tensor_size}, options); - FPTYPE* recv_g1 = recv_g1_tensor.data_ptr(); - FPTYPE* send_g1 = send_g1_tensor.data_ptr() + ntotal * tensor_size; + torch::Tensor send_g1_tensor; + torch::Tensor recv_g1_tensor; + FPTYPE* recv_g1; + FPTYPE* send_g1; + if(nswap != 0) + { + send_g1_tensor = d_local_g1_tensor; + int max_recvnum = sendnum_tensor.max().item(); + auto options = torch::TensorOptions() + .dtype(d_local_g1_tensor.dtype()) + .device(d_local_g1_tensor.device()); + recv_g1_tensor = + torch::empty({max_recvnum, tensor_size}, options); + recv_g1 = recv_g1_tensor.data_ptr(); + send_g1 = send_g1_tensor.data_ptr() + ntotal * tensor_size; + } int end = ntotal; auto int32_options = torch::TensorOptions().dtype(torch::kInt32); From 90d06486dd1cc084c89d323bd56eec6bf80f8d96 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 22 Oct 2024 13:24:06 +0000 Subject: [PATCH 2/9] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- source/api_cc/src/DeepPotPT.cc | 2 +- source/op/pt/comm.cc | 18 +++++++----------- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/source/api_cc/src/DeepPotPT.cc b/source/api_cc/src/DeepPotPT.cc index 8ed9ae3cf1..bd6f9e8b5e 100644 --- a/source/api_cc/src/DeepPotPT.cc +++ b/source/api_cc/src/DeepPotPT.cc @@ -218,7 +218,7 @@ void DeepPotPT::compute(ENERGYVTYPE& ener, .to(device); } c10::Dict outputs = - (do_message_passing == 1 ) + (do_message_passing == 1) ? module .run_method("forward_lower", coord_wrapped_Tensor, atype_Tensor, firstneigh_tensor, mapping_tensor, fparam_tensor, diff --git a/source/op/pt/comm.cc b/source/op/pt/comm.cc index aa5c6f51c7..e7f26365a7 100644 --- a/source/op/pt/comm.cc +++ b/source/op/pt/comm.cc @@ -123,11 +123,9 @@ class Border : public torch::autograd::Function { torch::Tensor isendlist; torch::Tensor send_g1_tensor; FPTYPE* send_g1; - if(nsend != 0) - { - isendlist = - torch::from_blob(sendlist[iswap], {nsend}, int32_options) - .to(recv_g1_tensor.device()); + if (nsend != 0) { + isendlist = torch::from_blob(sendlist[iswap], {nsend}, int32_options) + .to(recv_g1_tensor.device()); send_g1_tensor = recv_g1_tensor.index_select(0, isendlist); send_g1 = send_g1_tensor.data_ptr(); } @@ -258,15 +256,13 @@ class Border : public torch::autograd::Function { torch::Tensor recv_g1_tensor; FPTYPE* recv_g1; FPTYPE* send_g1; - if(nswap != 0) - { + if (nswap != 0) { send_g1_tensor = d_local_g1_tensor; int max_recvnum = sendnum_tensor.max().item(); auto options = torch::TensorOptions() - .dtype(d_local_g1_tensor.dtype()) - .device(d_local_g1_tensor.device()); - recv_g1_tensor = - torch::empty({max_recvnum, tensor_size}, options); + .dtype(d_local_g1_tensor.dtype()) + .device(d_local_g1_tensor.device()); + recv_g1_tensor = torch::empty({max_recvnum, tensor_size}, options); recv_g1 = recv_g1_tensor.data_ptr(); send_g1 = send_g1_tensor.data_ptr() + ntotal * tensor_size; } From 550f18da19cb04b7cdfd831a0a1860008011d98b Mon Sep 17 00:00:00 2001 From: Lysithea Date: Tue, 22 Oct 2024 23:43:50 +0800 Subject: [PATCH 3/9] fix dpa2 c++ when no lammps comm data pass in --- source/api_cc/src/DeepPotPT.cc | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/source/api_cc/src/DeepPotPT.cc b/source/api_cc/src/DeepPotPT.cc index bd6f9e8b5e..8bc03dc444 100644 --- a/source/api_cc/src/DeepPotPT.cc +++ b/source/api_cc/src/DeepPotPT.cc @@ -180,10 +180,16 @@ void DeepPotPT::compute(ENERGYVTYPE& ener, torch::from_blob(lmp_list.recvnum, {nswap}, int32_option); torch::Tensor sendnum_tensor = torch::from_blob(lmp_list.sendnum, {nswap}, int32_option); - torch::Tensor communicator_tensor = torch::from_blob( + torch::Tensor communicator_tensor; + if(lmp_list.world == 0) + { + communicator_tensor = torch::empty({1}, torch::kInt64); + } + else{ + communicator_tensor = torch::from_blob( const_cast(lmp_list.world), {1}, torch::kInt64); - // torch::Tensor communicator_tensor = - // torch::tensor(lmp_list.world, int32_option); + } + torch::Tensor nswap_tensor = torch::tensor(nswap, int32_option); int total_send = std::accumulate(lmp_list.sendnum, lmp_list.sendnum + nswap, 0); From d8541dace9dd7e68d7fee518d9dec0713cb3f6a5 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 22 Oct 2024 15:44:27 +0000 Subject: [PATCH 4/9] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- source/api_cc/src/DeepPotPT.cc | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/source/api_cc/src/DeepPotPT.cc b/source/api_cc/src/DeepPotPT.cc index 8bc03dc444..4c7aac19b8 100644 --- a/source/api_cc/src/DeepPotPT.cc +++ b/source/api_cc/src/DeepPotPT.cc @@ -181,13 +181,11 @@ void DeepPotPT::compute(ENERGYVTYPE& ener, torch::Tensor sendnum_tensor = torch::from_blob(lmp_list.sendnum, {nswap}, int32_option); torch::Tensor communicator_tensor; - if(lmp_list.world == 0) - { + if (lmp_list.world == 0) { communicator_tensor = torch::empty({1}, torch::kInt64); - } - else{ + } else { communicator_tensor = torch::from_blob( - const_cast(lmp_list.world), {1}, torch::kInt64); + const_cast(lmp_list.world), {1}, torch::kInt64); } torch::Tensor nswap_tensor = torch::tensor(nswap, int32_option); From 50621bd8f16f80508e337517868f4ba2e0607f59 Mon Sep 17 00:00:00 2001 From: Lysithea Date: Wed, 23 Oct 2024 00:18:25 +0800 Subject: [PATCH 5/9] remove c++ lmp nlist ut due to no comm data --- source/api_cc/src/DeepPotPT.cc | 6 +- source/api_cc/tests/test_deeppot_dpa_pt.cc | 80 ---------------------- 2 files changed, 1 insertion(+), 85 deletions(-) diff --git a/source/api_cc/src/DeepPotPT.cc b/source/api_cc/src/DeepPotPT.cc index 4c7aac19b8..859a610adc 100644 --- a/source/api_cc/src/DeepPotPT.cc +++ b/source/api_cc/src/DeepPotPT.cc @@ -181,12 +181,8 @@ void DeepPotPT::compute(ENERGYVTYPE& ener, torch::Tensor sendnum_tensor = torch::from_blob(lmp_list.sendnum, {nswap}, int32_option); torch::Tensor communicator_tensor; - if (lmp_list.world == 0) { - communicator_tensor = torch::empty({1}, torch::kInt64); - } else { - communicator_tensor = torch::from_blob( + communicator_tensor = torch::from_blob( const_cast(lmp_list.world), {1}, torch::kInt64); - } torch::Tensor nswap_tensor = torch::tensor(nswap, int32_option); int total_send = diff --git a/source/api_cc/tests/test_deeppot_dpa_pt.cc b/source/api_cc/tests/test_deeppot_dpa_pt.cc index 7bf4cbf376..d0645fa517 100644 --- a/source/api_cc/tests/test_deeppot_dpa_pt.cc +++ b/source/api_cc/tests/test_deeppot_dpa_pt.cc @@ -311,83 +311,3 @@ TYPED_TEST(TestInferDeepPotDpaPtNopbc, cpu_build_nlist_atomic) { } } -TYPED_TEST(TestInferDeepPotDpaPtNopbc, cpu_lmp_nlist) { - using VALUETYPE = TypeParam; - std::vector& coord = this->coord; - std::vector& atype = this->atype; - std::vector& box = this->box; - std::vector& expected_e = this->expected_e; - std::vector& expected_f = this->expected_f; - std::vector& expected_v = this->expected_v; - int& natoms = this->natoms; - double& expected_tot_e = this->expected_tot_e; - std::vector& expected_tot_v = this->expected_tot_v; - deepmd::DeepPot& dp = this->dp; - double ener; - std::vector force, virial; - - std::vector > nlist_data = { - {1, 2, 3, 4, 5}, {0, 2, 3, 4, 5}, {0, 1, 3, 4, 5}, - {0, 1, 2, 4, 5}, {0, 1, 2, 3, 5}, {0, 1, 2, 3, 4}}; - std::vector ilist(natoms), numneigh(natoms); - std::vector firstneigh(natoms); - deepmd::InputNlist inlist(natoms, &ilist[0], &numneigh[0], &firstneigh[0]); - convert_nlist(inlist, nlist_data); - dp.compute(ener, force, virial, coord, atype, box, 0, inlist, 0); - - EXPECT_EQ(force.size(), natoms * 3); - EXPECT_EQ(virial.size(), 9); - - EXPECT_LT(fabs(ener - expected_tot_e), EPSILON); - for (int ii = 0; ii < natoms * 3; ++ii) { - EXPECT_LT(fabs(force[ii] - expected_f[ii]), EPSILON); - } - for (int ii = 0; ii < 3 * 3; ++ii) { - EXPECT_LT(fabs(virial[ii] - expected_tot_v[ii]), EPSILON); - } -} - -TYPED_TEST(TestInferDeepPotDpaPtNopbc, cpu_lmp_nlist_atomic) { - using VALUETYPE = TypeParam; - std::vector& coord = this->coord; - std::vector& atype = this->atype; - std::vector& box = this->box; - std::vector& expected_e = this->expected_e; - std::vector& expected_f = this->expected_f; - std::vector& expected_v = this->expected_v; - int& natoms = this->natoms; - double& expected_tot_e = this->expected_tot_e; - std::vector& expected_tot_v = this->expected_tot_v; - deepmd::DeepPot& dp = this->dp; - double ener; - std::vector force, virial, atom_ener, atom_vir; - - std::vector > nlist_data = { - {1, 2, 3, 4, 5}, {0, 2, 3, 4, 5}, {0, 1, 3, 4, 5}, - {0, 1, 2, 4, 5}, {0, 1, 2, 3, 5}, {0, 1, 2, 3, 4}}; - std::vector ilist(natoms), numneigh(natoms); - std::vector firstneigh(natoms); - deepmd::InputNlist inlist(natoms, &ilist[0], &numneigh[0], &firstneigh[0]); - convert_nlist(inlist, nlist_data); - dp.compute(ener, force, virial, atom_ener, atom_vir, coord, atype, box, 0, - inlist, 0); - - EXPECT_EQ(force.size(), natoms * 3); - EXPECT_EQ(virial.size(), 9); - EXPECT_EQ(atom_ener.size(), natoms); - EXPECT_EQ(atom_vir.size(), natoms * 9); - - EXPECT_LT(fabs(ener - expected_tot_e), EPSILON); - for (int ii = 0; ii < natoms * 3; ++ii) { - EXPECT_LT(fabs(force[ii] - expected_f[ii]), EPSILON); - } - for (int ii = 0; ii < 3 * 3; ++ii) { - EXPECT_LT(fabs(virial[ii] - expected_tot_v[ii]), EPSILON); - } - for (int ii = 0; ii < natoms; ++ii) { - EXPECT_LT(fabs(atom_ener[ii] - expected_e[ii]), EPSILON); - } - for (int ii = 0; ii < natoms * 9; ++ii) { - EXPECT_LT(fabs(atom_vir[ii] - expected_v[ii]), EPSILON); - } -} From 389b914b97ae916f1f5245d34df3d5e72a536020 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 22 Oct 2024 16:19:09 +0000 Subject: [PATCH 6/9] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- source/api_cc/src/DeepPotPT.cc | 4 ++-- source/api_cc/tests/test_deeppot_dpa_pt.cc | 1 - 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/source/api_cc/src/DeepPotPT.cc b/source/api_cc/src/DeepPotPT.cc index 859a610adc..5eb6acbbca 100644 --- a/source/api_cc/src/DeepPotPT.cc +++ b/source/api_cc/src/DeepPotPT.cc @@ -181,8 +181,8 @@ void DeepPotPT::compute(ENERGYVTYPE& ener, torch::Tensor sendnum_tensor = torch::from_blob(lmp_list.sendnum, {nswap}, int32_option); torch::Tensor communicator_tensor; - communicator_tensor = torch::from_blob( - const_cast(lmp_list.world), {1}, torch::kInt64); + communicator_tensor = torch::from_blob(const_cast(lmp_list.world), + {1}, torch::kInt64); torch::Tensor nswap_tensor = torch::tensor(nswap, int32_option); int total_send = diff --git a/source/api_cc/tests/test_deeppot_dpa_pt.cc b/source/api_cc/tests/test_deeppot_dpa_pt.cc index d0645fa517..016521ebf5 100644 --- a/source/api_cc/tests/test_deeppot_dpa_pt.cc +++ b/source/api_cc/tests/test_deeppot_dpa_pt.cc @@ -310,4 +310,3 @@ TYPED_TEST(TestInferDeepPotDpaPtNopbc, cpu_build_nlist_atomic) { EXPECT_LT(fabs(atom_vir[ii] - expected_v[ii]), EPSILON); } } - From 8e82b9f10543757618ecc4c51b6c2f06b0d371d3 Mon Sep 17 00:00:00 2001 From: Lysithea Date: Wed, 23 Oct 2024 11:22:52 +0800 Subject: [PATCH 7/9] support old nlist api --- source/api_cc/src/DeepPotPT.cc | 10 ++++++++-- source/op/pt/comm.cc | 26 ++++++++++++++++---------- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/source/api_cc/src/DeepPotPT.cc b/source/api_cc/src/DeepPotPT.cc index 5eb6acbbca..8bc03dc444 100644 --- a/source/api_cc/src/DeepPotPT.cc +++ b/source/api_cc/src/DeepPotPT.cc @@ -181,8 +181,14 @@ void DeepPotPT::compute(ENERGYVTYPE& ener, torch::Tensor sendnum_tensor = torch::from_blob(lmp_list.sendnum, {nswap}, int32_option); torch::Tensor communicator_tensor; - communicator_tensor = torch::from_blob(const_cast(lmp_list.world), - {1}, torch::kInt64); + if(lmp_list.world == 0) + { + communicator_tensor = torch::empty({1}, torch::kInt64); + } + else{ + communicator_tensor = torch::from_blob( + const_cast(lmp_list.world), {1}, torch::kInt64); + } torch::Tensor nswap_tensor = torch::tensor(nswap, int32_option); int total_send = diff --git a/source/op/pt/comm.cc b/source/op/pt/comm.cc index e7f26365a7..2b2b3cb0bd 100644 --- a/source/op/pt/comm.cc +++ b/source/op/pt/comm.cc @@ -87,16 +87,19 @@ class Border : public torch::autograd::Function { int mpi_init = 0; MPI_Initialized(&mpi_init); int cuda_aware = 1; - int me; + int me = 0; MPI_Comm world; int world_size = 0; - unpack_communicator(communicator_tensor, world); - MPI_Comm_rank(world, &me); - MPI_Comm_size(world, &world_size); + if(mpi_init) + { + unpack_communicator(communicator_tensor, world); + MPI_Comm_rank(world, &me); + MPI_Comm_size(world, &world_size); + } MPI_Datatype mpi_type = get_mpi_type(); MPI_Request request; #if defined(GOOGLE_CUDA) || defined(TENSORFLOW_USE_ROCM) - if (world_size != 1) { + if (world_size >= 1) { int version, subversion; MPI_Get_version(&version, &subversion); if (version >= 4) { @@ -211,15 +214,18 @@ class Border : public torch::autograd::Function { MPI_Initialized(&mpi_init); int world_size = 0; int cuda_aware = 1; + int me = 0; MPI_Comm world; - unpack_communicator(communicator_tensor, world); - int me; - MPI_Comm_rank(world, &me); - MPI_Comm_size(world, &world_size); + if(mpi_init) + { + unpack_communicator(communicator_tensor, world); + MPI_Comm_rank(world, &me); + MPI_Comm_size(world, &world_size); + } MPI_Datatype mpi_type = get_mpi_type(); MPI_Request request; #if defined(GOOGLE_CUDA) || defined(TENSORFLOW_USE_ROCM) - if (world_size != 1) { + if (world_size >= 1) { int version, subversion; MPI_Get_version(&version, &subversion); if (version >= 4) { From 410da82a870a38bb8dfdcdbe8bb166d80e46d1b4 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 23 Oct 2024 03:23:27 +0000 Subject: [PATCH 8/9] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- source/api_cc/src/DeepPotPT.cc | 8 +++----- source/op/pt/comm.cc | 6 ++---- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/source/api_cc/src/DeepPotPT.cc b/source/api_cc/src/DeepPotPT.cc index 8bc03dc444..4c7aac19b8 100644 --- a/source/api_cc/src/DeepPotPT.cc +++ b/source/api_cc/src/DeepPotPT.cc @@ -181,13 +181,11 @@ void DeepPotPT::compute(ENERGYVTYPE& ener, torch::Tensor sendnum_tensor = torch::from_blob(lmp_list.sendnum, {nswap}, int32_option); torch::Tensor communicator_tensor; - if(lmp_list.world == 0) - { + if (lmp_list.world == 0) { communicator_tensor = torch::empty({1}, torch::kInt64); - } - else{ + } else { communicator_tensor = torch::from_blob( - const_cast(lmp_list.world), {1}, torch::kInt64); + const_cast(lmp_list.world), {1}, torch::kInt64); } torch::Tensor nswap_tensor = torch::tensor(nswap, int32_option); diff --git a/source/op/pt/comm.cc b/source/op/pt/comm.cc index 2b2b3cb0bd..71a2b0e118 100644 --- a/source/op/pt/comm.cc +++ b/source/op/pt/comm.cc @@ -90,8 +90,7 @@ class Border : public torch::autograd::Function { int me = 0; MPI_Comm world; int world_size = 0; - if(mpi_init) - { + if (mpi_init) { unpack_communicator(communicator_tensor, world); MPI_Comm_rank(world, &me); MPI_Comm_size(world, &world_size); @@ -216,8 +215,7 @@ class Border : public torch::autograd::Function { int cuda_aware = 1; int me = 0; MPI_Comm world; - if(mpi_init) - { + if (mpi_init) { unpack_communicator(communicator_tensor, world); MPI_Comm_rank(world, &me); MPI_Comm_size(world, &world_size); From e3f94338adc8f26d8c2c27bb719bb87ab406d00d Mon Sep 17 00:00:00 2001 From: Lysithea Date: Wed, 23 Oct 2024 11:33:04 +0800 Subject: [PATCH 9/9] add tests --- source/api_cc/tests/test_deeppot_dpa_pt.cc | 81 ++++++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/source/api_cc/tests/test_deeppot_dpa_pt.cc b/source/api_cc/tests/test_deeppot_dpa_pt.cc index 016521ebf5..7bf4cbf376 100644 --- a/source/api_cc/tests/test_deeppot_dpa_pt.cc +++ b/source/api_cc/tests/test_deeppot_dpa_pt.cc @@ -310,3 +310,84 @@ TYPED_TEST(TestInferDeepPotDpaPtNopbc, cpu_build_nlist_atomic) { EXPECT_LT(fabs(atom_vir[ii] - expected_v[ii]), EPSILON); } } + +TYPED_TEST(TestInferDeepPotDpaPtNopbc, cpu_lmp_nlist) { + using VALUETYPE = TypeParam; + std::vector& coord = this->coord; + std::vector& atype = this->atype; + std::vector& box = this->box; + std::vector& expected_e = this->expected_e; + std::vector& expected_f = this->expected_f; + std::vector& expected_v = this->expected_v; + int& natoms = this->natoms; + double& expected_tot_e = this->expected_tot_e; + std::vector& expected_tot_v = this->expected_tot_v; + deepmd::DeepPot& dp = this->dp; + double ener; + std::vector force, virial; + + std::vector > nlist_data = { + {1, 2, 3, 4, 5}, {0, 2, 3, 4, 5}, {0, 1, 3, 4, 5}, + {0, 1, 2, 4, 5}, {0, 1, 2, 3, 5}, {0, 1, 2, 3, 4}}; + std::vector ilist(natoms), numneigh(natoms); + std::vector firstneigh(natoms); + deepmd::InputNlist inlist(natoms, &ilist[0], &numneigh[0], &firstneigh[0]); + convert_nlist(inlist, nlist_data); + dp.compute(ener, force, virial, coord, atype, box, 0, inlist, 0); + + EXPECT_EQ(force.size(), natoms * 3); + EXPECT_EQ(virial.size(), 9); + + EXPECT_LT(fabs(ener - expected_tot_e), EPSILON); + for (int ii = 0; ii < natoms * 3; ++ii) { + EXPECT_LT(fabs(force[ii] - expected_f[ii]), EPSILON); + } + for (int ii = 0; ii < 3 * 3; ++ii) { + EXPECT_LT(fabs(virial[ii] - expected_tot_v[ii]), EPSILON); + } +} + +TYPED_TEST(TestInferDeepPotDpaPtNopbc, cpu_lmp_nlist_atomic) { + using VALUETYPE = TypeParam; + std::vector& coord = this->coord; + std::vector& atype = this->atype; + std::vector& box = this->box; + std::vector& expected_e = this->expected_e; + std::vector& expected_f = this->expected_f; + std::vector& expected_v = this->expected_v; + int& natoms = this->natoms; + double& expected_tot_e = this->expected_tot_e; + std::vector& expected_tot_v = this->expected_tot_v; + deepmd::DeepPot& dp = this->dp; + double ener; + std::vector force, virial, atom_ener, atom_vir; + + std::vector > nlist_data = { + {1, 2, 3, 4, 5}, {0, 2, 3, 4, 5}, {0, 1, 3, 4, 5}, + {0, 1, 2, 4, 5}, {0, 1, 2, 3, 5}, {0, 1, 2, 3, 4}}; + std::vector ilist(natoms), numneigh(natoms); + std::vector firstneigh(natoms); + deepmd::InputNlist inlist(natoms, &ilist[0], &numneigh[0], &firstneigh[0]); + convert_nlist(inlist, nlist_data); + dp.compute(ener, force, virial, atom_ener, atom_vir, coord, atype, box, 0, + inlist, 0); + + EXPECT_EQ(force.size(), natoms * 3); + EXPECT_EQ(virial.size(), 9); + EXPECT_EQ(atom_ener.size(), natoms); + EXPECT_EQ(atom_vir.size(), natoms * 9); + + EXPECT_LT(fabs(ener - expected_tot_e), EPSILON); + for (int ii = 0; ii < natoms * 3; ++ii) { + EXPECT_LT(fabs(force[ii] - expected_f[ii]), EPSILON); + } + for (int ii = 0; ii < 3 * 3; ++ii) { + EXPECT_LT(fabs(virial[ii] - expected_tot_v[ii]), EPSILON); + } + for (int ii = 0; ii < natoms; ++ii) { + EXPECT_LT(fabs(atom_ener[ii] - expected_e[ii]), EPSILON); + } + for (int ii = 0; ii < natoms * 9; ++ii) { + EXPECT_LT(fabs(atom_vir[ii] - expected_v[ii]), EPSILON); + } +}