From 64c69370083e0adfb830a509aea31c6bc86ec6e2 Mon Sep 17 00:00:00 2001 From: risemeup1 <62429225+risemeup1@users.noreply.github.com> Date: Mon, 2 Sep 2024 18:59:44 +0800 Subject: [PATCH] [cherry-pick] Fix numpy2.1 bug in py3.10 (#67866) * fix * fix * Update requirements.txt --- paddle/fluid/pybind/tensor.cc | 12 ++++++++++-- paddle/fluid/pybind/tensor_py.h | 10 +++++----- python/paddle/base/dygraph/tensor_patch_methods.py | 6 +++++- 3 files changed, 20 insertions(+), 8 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/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), 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.