From ae35f502178d711aea4db9a7012256d72a9dc9db Mon Sep 17 00:00:00 2001 From: JYChen Date: Mon, 22 May 2023 12:33:25 +0800 Subject: [PATCH] fix device changed in setitem-numpy case (#53987) --- paddle/fluid/pybind/eager_method.cc | 2 +- .../fluid/tests/unittests/test_set_value_op.py | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/paddle/fluid/pybind/eager_method.cc b/paddle/fluid/pybind/eager_method.cc index 7f32c14c493..cc2f21c418f 100644 --- a/paddle/fluid/pybind/eager_method.cc +++ b/paddle/fluid/pybind/eager_method.cc @@ -1313,7 +1313,7 @@ static PyObject* tensor_method__setitem_eager_tensor(TensorObject* self, } } } else { - auto self_numpy = TensorToPyArray(*self_tensor); + auto self_numpy = TensorToPyArray(*self_tensor, true); VLOG(4) << "parse_index is false"; if (PyCheckTensor(_index)) { VLOG(4) << "index is tensor"; diff --git a/python/paddle/fluid/tests/unittests/test_set_value_op.py b/python/paddle/fluid/tests/unittests/test_set_value_op.py index c2dde3a258d..b4d5c1d02ef 100644 --- a/python/paddle/fluid/tests/unittests/test_set_value_op.py +++ b/python/paddle/fluid/tests/unittests/test_set_value_op.py @@ -1635,6 +1635,20 @@ class TestSetValueInplaceLeafVar(unittest.TestCase): paddle.enable_static() +class TestSetValueIsSamePlace(unittest.TestCase): + def test_is_same_place(self): + paddle.disable_static() + paddle.seed(100) + paddle.set_device('cpu') + a = paddle.rand(shape=[2, 3, 4]) + origin_place = a.place + a[[0, 1], 1] = 10 + self.assertEqual(origin_place._type(), a.place._type()) + if paddle.is_compiled_with_cuda(): + paddle.set_device('gpu') + paddle.enable_static() + + @unittest.skipIf( not core.is_compiled_with_cuda() or not core.is_bfloat16_supported(core.CUDAPlace(0)), -- GitLab