From 6b24ea1404921574ac2b27faefb817cd64f35c73 Mon Sep 17 00:00:00 2001 From: risemeup1 <515586620@qq.com> Date: Fri, 30 Aug 2024 07:13:59 +0000 Subject: [PATCH 1/3] fix --- paddle/fluid/pybind/tensor.cc | 12 ++++++++++-- python/paddle/base/dygraph/tensor_patch_methods.py | 6 +++++- python/requirements.txt | 2 +- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/paddle/fluid/pybind/tensor.cc b/paddle/fluid/pybind/tensor.cc index 44983e3e13df7..19e12ce5b0a5b 100644 --- a/paddle/fluid/pybind/tensor.cc +++ b/paddle/fluid/pybind/tensor.cc @@ -210,8 +210,16 @@ void BindTensor(pybind11::module &m) { // NOLINT g_framework_tensor_pytype = reinterpret_cast(framework_tensor.ptr()); framework_tensor - .def("__array__", - [](phi::DenseTensor &self) { return TensorToPyArray(self); }) + .def( + // TODO(risemeup): Modify the logic of + // TensorToPyArray() according to the dtype and copy + // parameters. + "__array__", + [](phi::DenseTensor &self, py::object dtype, py::object copy) { + return TensorToPyArray(self); + }, + py::arg("dtype") = py::none(), + py::arg("copy") = py::none()) .def("_ptr", [](const phi::DenseTensor &self) { return reinterpret_cast(self.data()); diff --git a/python/paddle/base/dygraph/tensor_patch_methods.py b/python/paddle/base/dygraph/tensor_patch_methods.py index 7b4c81cfa323d..857589274f891 100644 --- a/python/paddle/base/dygraph/tensor_patch_methods.py +++ b/python/paddle/base/dygraph/tensor_patch_methods.py @@ -848,7 +848,11 @@ def __nonzero__(self): def __bool__(self): return self.__nonzero__() - def __array__(self, dtype=None): + def __array__( + self, + dtype=None, + copy=None, + ): """ Returns a numpy array shows the value of current Tensor. diff --git a/python/requirements.txt b/python/requirements.txt index 70609cf7c3776..10bc22bc432b4 100644 --- a/python/requirements.txt +++ b/python/requirements.txt @@ -1,5 +1,5 @@ httpx -numpy>=1.13 +numpy>=1.21 protobuf>=3.20.2 ; platform_system != "Windows" protobuf>=3.1.0, <=3.20.2 ; platform_system == "Windows" Pillow From 2726fd6f6d6f34ba88f757527470416eb86c7e3c Mon Sep 17 00:00:00 2001 From: risemeup1 <515586620@qq.com> Date: Mon, 2 Sep 2024 08:42:11 +0000 Subject: [PATCH 2/3] fix --- paddle/fluid/pybind/tensor_py.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/paddle/fluid/pybind/tensor_py.h b/paddle/fluid/pybind/tensor_py.h index b49584b50962a..4bf3e4abd1cfb 100644 --- a/paddle/fluid/pybind/tensor_py.h +++ b/paddle/fluid/pybind/tensor_py.h @@ -411,7 +411,7 @@ void SetTensorFromPyArrayT( static_cast(dst), platform::CPUPlace(), static_cast(array.data()), - array.nbytes()); + array.size()*sizeof(T)); #else PADDLE_THROW(platform::errors::PermissionDenied( "Cannot use XPUPlace in CPU/GPU version, " @@ -447,7 +447,7 @@ void SetTensorFromPyArrayT( phi::DeviceManager::GetDeviceWithPlace(tmp_place)->MemoryCopyH2D( reinterpret_cast(dst), const_cast(reinterpret_cast(array.data())), - array.nbytes()); + array.size()*sizeof(T)); platform::DeviceContextPool &pool = platform::DeviceContextPool::Instance(); auto &ctx = *pool.Get(place); ctx.Wait(); @@ -465,10 +465,10 @@ void SetTensorFromPyArrayT( auto dst = self->mutable_data(place); #ifdef PADDLE_WITH_HIP paddle::platform::GpuMemcpySync( - dst, array.data(), array.nbytes(), hipMemcpyHostToDevice); + dst, array.data(),array.size()*sizeof(T), hipMemcpyHostToDevice); //NOLINT #else paddle::platform::GpuMemcpySync( - dst, array.data(), array.nbytes(), cudaMemcpyHostToDevice); + dst, array.data(), array.size()*sizeof(T), cudaMemcpyHostToDevice); #endif } else if (paddle::platform::is_cuda_pinned_place(place)) { @@ -617,7 +617,7 @@ void SetUVATensorFromPyArrayImpl( cudaHostAlloc(reinterpret_cast(&data_ptr), need_allocate_size, cudaHostAllocWriteCombined | cudaHostAllocMapped); - std::memcpy(data_ptr, array.data(), array.nbytes()); + std::memcpy(data_ptr, array.data(), array.size()*sizeof(T)); void *cuda_device_pointer = nullptr; cudaHostGetDevicePointer(reinterpret_cast(&cuda_device_pointer), From db8b8838bd21a94790af2bdcc40f0dec6f1aba1d Mon Sep 17 00:00:00 2001 From: risemeup1 <62429225+risemeup1@users.noreply.github.com> Date: Mon, 2 Sep 2024 16:50:49 +0800 Subject: [PATCH 3/3] Update requirements.txt --- python/requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/requirements.txt b/python/requirements.txt index 10bc22bc432b4..70609cf7c3776 100644 --- a/python/requirements.txt +++ b/python/requirements.txt @@ -1,5 +1,5 @@ httpx -numpy>=1.21 +numpy>=1.13 protobuf>=3.20.2 ; platform_system != "Windows" protobuf>=3.1.0, <=3.20.2 ; platform_system == "Windows" Pillow