diff --git a/paddle/fluid/distributed/collective/ProcessGroupNCCL.cc b/paddle/fluid/distributed/collective/ProcessGroupNCCL.cc index 7f21bcee87ab705097d3c2beaf799e5f2d93b833..70e111b0a9a5ef115494a0745482a48b7153adf8 100644 --- a/paddle/fluid/distributed/collective/ProcessGroupNCCL.cc +++ b/paddle/fluid/distributed/collective/ProcessGroupNCCL.cc @@ -417,7 +417,7 @@ void CheckTensorsInDifferentDevices(const std::vector& tensors, std::set used_devices; for (const auto& t : tensors) { - PADDLE_ENFORCE_EQ(t.is_cuda() && t.is_dense_tensor(), true, + PADDLE_ENFORCE_EQ(t.is_gpu() && t.is_dense_tensor(), true, platform::errors::InvalidArgument( "Tensors must be CUDA and dense tensor.")); diff --git a/paddle/fluid/eager/tests/data_structure_tests/eager_tensor_test.cc b/paddle/fluid/eager/tests/data_structure_tests/eager_tensor_test.cc index c8b2d22dcf95139db47704be86a6f64554f7c0ba..5fec38bf25a433542e09e0eec0cb52c61cff7167 100644 --- a/paddle/fluid/eager/tests/data_structure_tests/eager_tensor_test.cc +++ b/paddle/fluid/eager/tests/data_structure_tests/eager_tensor_test.cc @@ -90,7 +90,7 @@ TEST(Tensor, MemberFunction) { VLOG(6) << "Set impl"; CHECK_EQ(et3.initialized(), true); CHECK_EQ(et3.is_cpu(), true); - CHECK_EQ(et3.is_cuda(), false); + CHECK_EQ(et3.is_gpu(), false); CHECK_EQ(et3.numel(), 2); auto expected_dim = phi::make_ddim({1, 2}); CHECK_EQ(et3.dims(), expected_dim); diff --git a/paddle/fluid/pybind/eager_method.cc b/paddle/fluid/pybind/eager_method.cc index 49745e5679d9af3c8cf07ba0cac679217a691052..dd237b76728e2862eef2d89166d73293e93d9720 100644 --- a/paddle/fluid/pybind/eager_method.cc +++ b/paddle/fluid/pybind/eager_method.cc @@ -175,7 +175,7 @@ static PyObject* tensor_method_numpy(TensorObject* self, PyObject* args, pybind11::detail::npy_api::NPY_ARRAY_WRITEABLE_, nullptr); - if (self->tensor.is_cpu()) { + if (self->tensor.is_cpu() || self->tensor.is_gpu_pinned()) { auto dense_tensor = std::dynamic_pointer_cast(self->tensor.impl()); platform::CPUPlace place; @@ -184,7 +184,7 @@ static PyObject* tensor_method_numpy(TensorObject* self, PyObject* args, pybind11::detail::array_proxy(array)->data), place, dense_tensor->data(), sizeof_dtype * numel); #if defined(PADDLE_WITH_CUDA) - } else if (self->tensor.is_cuda()) { + } else if (self->tensor.is_gpu()) { auto dense_tensor = std::dynamic_pointer_cast(self->tensor.impl()); diff --git a/paddle/phi/api/include/tensor.h b/paddle/phi/api/include/tensor.h index c58ebe69523eb97a4357bea061de64e5e01ec181..649f4d11383844dabff3a0c1ecdf324ab6bd07f2 100644 --- a/paddle/phi/api/include/tensor.h +++ b/paddle/phi/api/include/tensor.h @@ -269,12 +269,20 @@ class PADDLE_API Tensor final { bool is_cpu() const; /** - * @brief Determine whether the tensor device is CUDA + * @brief Determine whether the tensor device is GPU * * @return true * @return false */ - bool is_cuda() const; + bool is_gpu() const; + + /** + * @brief Determine whether the tensor device is GPU_PINNED + * + * @return true + * @return false + */ + bool is_gpu_pinned() const; /* Part 4: Data Access methods */ diff --git a/paddle/phi/api/lib/tensor.cc b/paddle/phi/api/lib/tensor.cc index 066287d4244797e316a34d524eca171e94afcfdf..b9b6ca36f673b03ba0b548424711833f21e612a9 100644 --- a/paddle/phi/api/lib/tensor.cc +++ b/paddle/phi/api/lib/tensor.cc @@ -163,10 +163,14 @@ bool Tensor::is_cpu() const { return paddle::platform::is_cpu_place(inner_place()); } -bool Tensor::is_cuda() const { +bool Tensor::is_gpu() const { return paddle::platform::is_gpu_place(inner_place()); } +bool Tensor::is_gpu_pinned() const { + return paddle::platform::is_cuda_pinned_place(inner_place()); +} + /* Part 4: Data Access methods */ template diff --git a/paddle/phi/kernels/gpu/copy_kernel.cu b/paddle/phi/kernels/gpu/copy_kernel.cu index 4545f9ce436ea4028d43d3a91ae46a21cde41bb5..a16c8369cc9e5b71ab483b5c2eb9a3d2e61f900a 100644 --- a/paddle/phi/kernels/gpu/copy_kernel.cu +++ b/paddle/phi/kernels/gpu/copy_kernel.cu @@ -87,7 +87,8 @@ void Copy(const Context& dev_ctx, : reinterpret_cast(dev_ctx).stream(); paddle::memory::Copy( dst_cpu_place, dst_ptr, src_gpu_place, src_ptr, size, stream); - } else if (paddle::platform::is_cpu_place(src_place) && // NOLINT + } else if ((paddle::platform::is_cpu_place(src_place) || + paddle::platform::is_cuda_pinned_place(src_place)) && // NOLINT paddle::platform::is_gpu_place(dst_place)) { auto src_cpu_place = src_place; auto dst_gpu_place = dst_place; diff --git a/python/paddle/fluid/dygraph/layers.py b/python/paddle/fluid/dygraph/layers.py index 6957850d205794363183b4e6ca58a6daf3e11358..f4334085620f510e3d520f89332b754a93aa120a 100644 --- a/python/paddle/fluid/dygraph/layers.py +++ b/python/paddle/fluid/dygraph/layers.py @@ -1155,7 +1155,8 @@ class Layer(object): layers[name] = None else: _buffers = self.__dict__.get('_buffers', None) - if type(value) == core.VarBase: + if type(value) == core.VarBase or \ + type(value) == core.eager.Tensor: if _buffers is None: raise ValueError( "super(YourLayer, self).__init__() should be called first" diff --git a/python/paddle/fluid/tests/unittests/test_egr_python_api.py b/python/paddle/fluid/tests/unittests/test_egr_python_api.py index 98ef339e04535bb943add02b6cf6efe490f0354b..8166598677a3eb7ce7a4cb42b8a96b6b100aeb20 100644 --- a/python/paddle/fluid/tests/unittests/test_egr_python_api.py +++ b/python/paddle/fluid/tests/unittests/test_egr_python_api.py @@ -637,6 +637,10 @@ class EagerVariablePropertiesAndMethodsTestCase(unittest.TestCase): self.assertTrue(tensor3.persistable, True) self.assertTrue(tensor3.stop_gradient, True) self.assertTrue(tensor3.place.is_gpu_place()) + tensor4 = paddle.to_tensor([1, 2, 3], place='gpu_pinned') + tensor5 = tensor4._copy_to(core.CUDAPlace(0), True) + self.assertTrue( + np.array_equal(tensor4.numpy(), tensor5.numpy())) else: tensor3 = tensor2._copy_to(core.CPUPlace(), True) self.assertTrue(np.array_equal(tensor3.numpy(), arr2)) diff --git a/python/paddle/tensor/creation.py b/python/paddle/tensor/creation.py index bdb0eabe2bbb2968a00d5baf3f9ada14e05a635e..9cef336aa54ae4daae14a8bb99d70470e5dd7511 100644 --- a/python/paddle/tensor/creation.py +++ b/python/paddle/tensor/creation.py @@ -127,7 +127,7 @@ def to_tensor(data, dtype=None, place=None, stop_gradient=True): "\n\tFaild to convert input data to a regular ndarray :\n\t - Usually " "this means the input data contains nested lists with different lengths. " ) - elif isinstance(data, paddle.Tensor): + elif isinstance(data, (paddle.Tensor, core.eager.Tensor)): data = data._copy_to(place, False) data = _handle_dtype(data, dtype) data.stop_gradient = stop_gradient