From c9da703d954052d864c91f4e8cd6c292d30f7811 Mon Sep 17 00:00:00 2001 From: dzhwinter Date: Sun, 24 Dec 2017 17:01:48 -0800 Subject: [PATCH 1/3] "fix CopyFrom parameters" --- paddle/framework/tensor_util.h | 41 ++++++++++++++++++++++++++++ paddle/framework/tensor_util_test.cc | 3 +- 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/paddle/framework/tensor_util.h b/paddle/framework/tensor_util.h index 4e34b90d57eed8..557e6936251b43 100644 --- a/paddle/framework/tensor_util.h +++ b/paddle/framework/tensor_util.h @@ -82,6 +82,28 @@ inline void CopyFrom(const Tensor& src, const platform::Place& dst_place, #endif } +/** + * @brief CopyFrom support CPU <-> CPU + */ + +inline void CopyFrom(const Tensor& src, const platform::Place& dst_place, + Tensor* dst) { + src.check_memory_size(); + dst->Resize(src.dims()); + + auto src_place = src.place(); + auto src_ptr = src.data(); + + auto dst_ptr = dst->mutable_data(dst_place, src.type()); + + auto size = src.numel() * SizeOfType(src.type()); + + if (platform::is_cpu_place(src_place) && platform::is_cpu_place(dst_place)) { + memory::Copy(boost::get(dst_place), dst_ptr, + boost::get(src_place), src_ptr, size); + } +} + /** * @brief Copy the content of an external vector to a tensor. * @@ -115,6 +137,25 @@ inline void CopyFromVector(const std::vector& src, #endif } +/** + * @brief CopyFromVector CPU vector -> CPU Tensor + */ + +inline void CopyFromVector(const std::vector& src, + const platform::DeviceContext& ctx, Tensor* dst) { + auto dst_place = ctx.GetPlace(); + auto src_ptr = static_cast(src.data()); + platform::CPUPlace src_place; + dst->Resize({static_cast(src.size())}); + auto dst_ptr = static_cast(dst->mutable_data(dst_place)); + auto size = src.size() * sizeof(T); + + if (platform::is_cpu_place(dst_place)) { + memory::Copy(boost::get(dst_place), dst_ptr, src_place, + src_ptr, size); + } +} + /** * @brief Copy the content of a tensor to a vector * diff --git a/paddle/framework/tensor_util_test.cc b/paddle/framework/tensor_util_test.cc index 03a70de182d0eb..281cb37b49096d 100644 --- a/paddle/framework/tensor_util_test.cc +++ b/paddle/framework/tensor_util_test.cc @@ -17,6 +17,7 @@ namespace paddle { namespace framework { + TEST(CopyFrom, Tensor) { Tensor src_tensor; Tensor dst_tensor; @@ -29,7 +30,7 @@ TEST(CopyFrom, Tensor) { memcpy(src_ptr, arr, 9 * sizeof(int)); auto cpu_place = new platform::CPUPlace(); - CopyFrom(src_tensor, *cpu_place, cpu_ctx, &dst_tensor); + CopyFrom(src_tensor, *cpu_place, &dst_tensor); const int* dst_ptr = dst_tensor.data(); ASSERT_NE(src_ptr, dst_ptr); From 83e427c95b313104f01a2e6dc404149165aa3957 Mon Sep 17 00:00:00 2001 From: dzhwinter Date: Sun, 24 Dec 2017 17:11:30 -0800 Subject: [PATCH 2/3] "fix unused Place argument" --- paddle/framework/tensor_util.h | 30 ++++++++++++++++++++-------- paddle/framework/tensor_util_test.cc | 8 +++----- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/paddle/framework/tensor_util.h b/paddle/framework/tensor_util.h index 557e6936251b43..87614f9847ae06 100644 --- a/paddle/framework/tensor_util.h +++ b/paddle/framework/tensor_util.h @@ -140,20 +140,16 @@ inline void CopyFromVector(const std::vector& src, /** * @brief CopyFromVector CPU vector -> CPU Tensor */ - -inline void CopyFromVector(const std::vector& src, - const platform::DeviceContext& ctx, Tensor* dst) { - auto dst_place = ctx.GetPlace(); +template +inline void CopyFromVector(const std::vector& src, Tensor* dst) { + platform::CPUPlace dst_place = platform::CPUPlace(); auto src_ptr = static_cast(src.data()); platform::CPUPlace src_place; dst->Resize({static_cast(src.size())}); auto dst_ptr = static_cast(dst->mutable_data(dst_place)); auto size = src.size() * sizeof(T); - if (platform::is_cpu_place(dst_place)) { - memory::Copy(boost::get(dst_place), dst_ptr, src_place, - src_ptr, size); - } + memory::Copy(dst_place, dst_ptr, src_place, src_ptr, size); } /** @@ -188,6 +184,24 @@ inline void CopyToVector(const Tensor& src, const platform::DeviceContext& ctx, } #endif } +/** + * @brief CopyToVector CPUTensor <-> CPU Vector + */ + +template +inline void CopyToVector(const Tensor& src, std::vector* dst) { + auto src_ptr = static_cast(src.data()); + auto size = src.numel() * sizeof(T); + + platform::CPUPlace dst_place; + dst->resize(src.numel()); + auto dst_ptr = static_cast(dst->data()); + + if (platform::is_cpu_place(src.place())) { + memory::Copy(dst_place, dst_ptr, + boost::get(src.place()), src_ptr, size); + } +} } // namespace framework } // namespace paddle diff --git a/paddle/framework/tensor_util_test.cc b/paddle/framework/tensor_util_test.cc index 281cb37b49096d..c0935ce8bb0cf2 100644 --- a/paddle/framework/tensor_util_test.cc +++ b/paddle/framework/tensor_util_test.cc @@ -105,8 +105,7 @@ TEST(CopyFromVector, Tensor) { // Copy to CPU Tensor cpu_tensor.Resize(make_ddim({3, 3})); auto cpu_place = new paddle::platform::CPUPlace(); - CPUDeviceContext cpu_ctx(*cpu_place); - CopyFromVector(src_vec, cpu_ctx, &cpu_tensor); + CopyFromVector(src_vec, &cpu_tensor); // Compare Tensors const int* cpu_ptr = cpu_tensor.data(); @@ -118,7 +117,7 @@ TEST(CopyFromVector, Tensor) { src_vec.erase(src_vec.begin(), src_vec.begin() + 5); cpu_tensor.Resize(make_ddim({2, 2})); - CopyFromVector(src_vec, cpu_ctx, &cpu_tensor); + CopyFromVector(src_vec, &cpu_tensor); cpu_ptr = cpu_tensor.data(); src_ptr = src_vec.data(); ASSERT_NE(src_ptr, cpu_ptr); @@ -199,9 +198,8 @@ TEST(CopyToVector, Tensor) { } CPUPlace place; - CPUDeviceContext cpu_ctx(place); std::vector dst; - CopyToVector(src, cpu_ctx, &dst); + CopyToVector(src, &dst); for (int i = 0; i < 3 * 3; ++i) { EXPECT_EQ(src_ptr[i], dst[i]); From 8b93efd42cb3a4c1ca772266ce5876e4e8ab63af Mon Sep 17 00:00:00 2001 From: dzhwinter Date: Sun, 24 Dec 2017 20:07:37 -0800 Subject: [PATCH 3/3] "fixed based on comment" --- paddle/framework/tensor_util.h | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/paddle/framework/tensor_util.h b/paddle/framework/tensor_util.h index 87614f9847ae06..9c9244f609caa3 100644 --- a/paddle/framework/tensor_util.h +++ b/paddle/framework/tensor_util.h @@ -85,7 +85,6 @@ inline void CopyFrom(const Tensor& src, const platform::Place& dst_place, /** * @brief CopyFrom support CPU <-> CPU */ - inline void CopyFrom(const Tensor& src, const platform::Place& dst_place, Tensor* dst) { src.check_memory_size(); @@ -98,10 +97,11 @@ inline void CopyFrom(const Tensor& src, const platform::Place& dst_place, auto size = src.numel() * SizeOfType(src.type()); - if (platform::is_cpu_place(src_place) && platform::is_cpu_place(dst_place)) { - memory::Copy(boost::get(dst_place), dst_ptr, - boost::get(src_place), src_ptr, size); - } + PADDLE_ENFORCE(platform::is_cpu_place(src_place) && + platform::is_cpu_place(dst_place)); + + memory::Copy(boost::get(dst_place), dst_ptr, + boost::get(src_place), src_ptr, size); } /** @@ -184,10 +184,10 @@ inline void CopyToVector(const Tensor& src, const platform::DeviceContext& ctx, } #endif } + /** * @brief CopyToVector CPUTensor <-> CPU Vector */ - template inline void CopyToVector(const Tensor& src, std::vector* dst) { auto src_ptr = static_cast(src.data()); @@ -197,10 +197,10 @@ inline void CopyToVector(const Tensor& src, std::vector* dst) { dst->resize(src.numel()); auto dst_ptr = static_cast(dst->data()); - if (platform::is_cpu_place(src.place())) { - memory::Copy(dst_place, dst_ptr, - boost::get(src.place()), src_ptr, size); - } + PADDLE_ENFORCE(platform::is_cpu_place(src.place())); + + memory::Copy(dst_place, dst_ptr, boost::get(src.place()), + src_ptr, size); } } // namespace framework