From 94c6682a4ba59081c2f01f760d84ca61574fbba5 Mon Sep 17 00:00:00 2001 From: risemeup1 <62429225+risemeup1@users.noreply.github.com> Date: Thu, 29 Aug 2024 11:48:15 +0800 Subject: [PATCH] fix to_tensor bug (#67795) --- paddle/fluid/pybind/tensor_py.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/paddle/fluid/pybind/tensor_py.h b/paddle/fluid/pybind/tensor_py.h index dd5bd7f1d91c4..b49584b50962a 100644 --- a/paddle/fluid/pybind/tensor_py.h +++ b/paddle/fluid/pybind/tensor_py.h @@ -398,7 +398,7 @@ void SetTensorFromPyArrayT( self->ResetHolderWithType(holder, framework::TransToPhiDataType(type)); } else { auto dst = self->mutable_data(place); - std::memcpy(dst, array.data(), array.nbytes()); + std::memcpy(dst, array.data(), array.size()*sizeof(T)); } } else if (paddle::platform::is_xpu_place(place)) { #ifdef PADDLE_WITH_XPU @@ -427,10 +427,10 @@ void SetTensorFromPyArrayT( // IPU does not store Tensor data, Tensor will be created on CPU if (!self->initialized()) { auto dst = self->mutable_data(place); - std::memcpy(dst, array.data(), array.nbytes()); + std::memcpy(dst, array.data(), array.size()*sizeof(T)); } else { auto dst = self->mutable_data(self->place()); - std::memcpy(dst, array.data(), array.nbytes()); + std::memcpy(dst, array.data(), array.size()*sizeof(T)); } } #else @@ -473,7 +473,7 @@ void SetTensorFromPyArrayT( } else if (paddle::platform::is_cuda_pinned_place(place)) { auto dst = self->mutable_data(place); - std::memcpy(dst, array.data(), array.nbytes()); + std::memcpy(dst, array.data(), array.size()*sizeof(T)); } else { PADDLE_THROW(platform::errors::InvalidArgument( "Incompatible place type: Tensor.set() supports "