From a7cbd3ef6312c095d344a3483be82a36dd8f9ee7 Mon Sep 17 00:00:00 2001 From: Jiabin Yang <360788950@qq.com> Date: Fri, 18 Feb 2022 09:44:49 +0800 Subject: [PATCH] [Bug Fix]Fix gradient accumulator (#39577) * merge legacy to fluid * Remove legacy code * Remove legacy code * Remove DataType test * Using Tensor directly instead of using EagerTensor * support gradient_accumulation * make test_imperative_lod_tensor_to_selected_rows longer * make test_imperative_lod_tensor_to_selected_rows longer * refine code * Rename all EagerTensor to Tensor * Rename some EagerTensor to Tensor * rename EagerTensor to EagerVariable * add more test * fix different device gradient_accmulator bug * merge develop * remove useless tests --- .../fluid/imperative/gradient_accumulator.cc | 3 --- .../tests/test_gradient_accmulator.cc | 20 +++++++++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/paddle/fluid/imperative/gradient_accumulator.cc b/paddle/fluid/imperative/gradient_accumulator.cc index 17ab1f1f7c..2020e2900c 100644 --- a/paddle/fluid/imperative/gradient_accumulator.cc +++ b/paddle/fluid/imperative/gradient_accumulator.cc @@ -300,13 +300,10 @@ void TensorAdd(const VarType& src, VarType* dst) { "should be equal, Otherwise, the calculation results " "will be incorrect.")); -#ifdef PADDLE_WITH_XPU // if src and dst are in different place, copy dst to src's place if (dst_tensor->place() != place) { paddle::framework::TensorCopySync(*dst_tensor, place, dst_tensor); } -#endif - #define PADDLE_TENSOR_ADD(cpp_type) \ if (data_type == framework::DataTypeTrait::DataType()) { \ TensorAddFunctor func( \ diff --git a/paddle/fluid/imperative/tests/test_gradient_accmulator.cc b/paddle/fluid/imperative/tests/test_gradient_accmulator.cc index 4dfc819806..60b238e5cf 100644 --- a/paddle/fluid/imperative/tests/test_gradient_accmulator.cc +++ b/paddle/fluid/imperative/tests/test_gradient_accmulator.cc @@ -163,10 +163,30 @@ TEST(test_add_functor, add_functor) { gpu_res = TensorddTest(gpu_place, gpu_place, static_cast(1.0), static_cast(2.0)); EXPECT_EQ(gpu_res, 0); + + // normal + gpu_res = TensorddTest(gpu_place, gpu_place, static_cast(1.0), + static_cast(2.0)); + EXPECT_EQ(gpu_res, 0); gpu_res = TensorddTest(gpu_place, gpu_place, static_cast(1.0), static_cast(2.0)); EXPECT_EQ(gpu_res, 0); + // different places + gpu_res = TensorddTest(cpu_place, gpu_place, static_cast(1.0), + static_cast(2.0)); + EXPECT_EQ(gpu_res, 0); + gpu_res = TensorddTest(gpu_place, cpu_place, static_cast(1.0), + static_cast(2.0)); + EXPECT_EQ(gpu_res, 0); + gpu_res = + TensorddTest(cpu_place, gpu_place, static_cast(1.0), + static_cast(2.0)); + EXPECT_EQ(gpu_res, 0); + gpu_res = + TensorddTest(gpu_place, cpu_place, static_cast(1.0), + static_cast(2.0)); + EXPECT_EQ(gpu_res, 0); #endif #ifdef PADDLE_WITH_XPU -- GitLab