From d7fc3781ec66c6cfbed1ee2fad5c5ed9367f3148 Mon Sep 17 00:00:00 2001 From: wanghuancoder Date: Mon, 4 Sep 2023 10:27:56 +0800 Subject: [PATCH] fix contiguous (#56863) * fix contiguous --- paddle/fluid/pybind/eager_method.cc | 7 ++++--- test/legacy_test/test_stride.py | 29 ----------------------------- 2 files changed, 4 insertions(+), 32 deletions(-) diff --git a/paddle/fluid/pybind/eager_method.cc b/paddle/fluid/pybind/eager_method.cc index 7d3037a076d..2a4699f9454 100644 --- a/paddle/fluid/pybind/eager_method.cc +++ b/paddle/fluid/pybind/eager_method.cc @@ -2819,9 +2819,10 @@ static PyObject* tensor_contiguous(TensorObject* self, return reinterpret_cast(self); } else { eager_gil_scoped_release guard; - return ToPyObject( - paddle::Tensor(std::make_shared(std::move( - paddle::experimental::Trans2Contiguous(*(dense_tensor.get())))))); + self->tensor.set_impl(std::make_shared(std::move( + paddle::experimental::Trans2Contiguous(*(dense_tensor.get()))))); + Py_INCREF(self); + return reinterpret_cast(self); } } else { diff --git a/test/legacy_test/test_stride.py b/test/legacy_test/test_stride.py index ec78392bbc6..2886e46febd 100644 --- a/test/legacy_test/test_stride.py +++ b/test/legacy_test/test_stride.py @@ -42,13 +42,11 @@ class TestStride(unittest.TestCase): x_c = x_transposed1.contiguous() self.assertTrue(np.allclose(x_c.numpy(), x_np_transposed1)) - self.assertFalse(x_c._is_shared_buffer_with(x_transposed1)) x_transposed2 = paddle.transpose(x_transposed1, perm=[2, 0, 1]) x_np_transposed2 = x_np_transposed1.transpose(2, 0, 1) self.assertTrue(np.allclose(x_transposed2.numpy(), x_np_transposed2)) self.assertFalse(x_transposed2.is_contiguous()) - self.assertTrue(x._is_shared_buffer_with(x_transposed2)) y = x_transposed2 + 2 y_np = x_np_transposed2 + 2 @@ -96,11 +94,6 @@ class TestStride(unittest.TestCase): self.assertTrue(np.allclose(out3_c.numpy(), np_out3)) self.assertTrue(np.allclose(out4_c.numpy(), np_out4)) - self.assertFalse(out_c._is_shared_buffer_with(out)) - self.assertFalse(out2_c._is_shared_buffer_with(out2)) - self.assertFalse(out3_c._is_shared_buffer_with(out3)) - self.assertFalse(out4_c._is_shared_buffer_with(out4)) - def call_slice(self): x_np = np.random.random(size=[10, 10, 10, 20]).astype('float32') x = paddle.to_tensor(x_np) @@ -141,8 +134,6 @@ class TestStride(unittest.TestCase): self.assertTrue(np.allclose(out_c.numpy(), np_out)) - self.assertFalse(out_c._is_shared_buffer_with(out)) - def call_index_select(self): x_np = np.random.random(size=[10, 10, 10, 20]).astype('float32') x = paddle.to_tensor(x_np) @@ -161,8 +152,6 @@ class TestStride(unittest.TestCase): self.assertTrue(np.allclose(out_c.numpy(), np_out)) - self.assertFalse(out_c._is_shared_buffer_with(out)) - def call_reshape(self): x_np = np.random.random(size=[10, 10, 10, 20]).astype('float32') x = paddle.to_tensor(x_np) @@ -201,8 +190,6 @@ class TestStride(unittest.TestCase): self.assertTrue(np.allclose(out_c.numpy(), np_out)) - self.assertFalse(out_c._is_shared_buffer_with(out)) - def call_imag(self): x_np = np.random.random(size=[10, 10, 10, 20]).astype('complex128') x = paddle.to_tensor(x_np) @@ -221,8 +208,6 @@ class TestStride(unittest.TestCase): self.assertTrue(np.allclose(out_c.numpy(), np_out)) - self.assertFalse(out_c._is_shared_buffer_with(out)) - def call_as_real(self): x_np = np.random.random(size=[10, 10, 10, 20]).astype('complex128') x = paddle.to_tensor(x_np) @@ -351,10 +336,6 @@ class TestStride(unittest.TestCase): self.assertTrue(np.allclose(out1_c.numpy(), np_out1)) self.assertTrue(np.allclose(out2_c.numpy(), np_out2)) - self.assertFalse(out0_c._is_shared_buffer_with(out0)) - self.assertFalse(out1_c._is_shared_buffer_with(out1)) - self.assertFalse(out2_c._is_shared_buffer_with(out2)) - def call_split2(self): x_np = np.random.random(size=[3, 9, 5]).astype('float32') x = paddle.to_tensor(x_np) @@ -386,10 +367,6 @@ class TestStride(unittest.TestCase): self.assertTrue(np.allclose(out1_c.numpy(), np_out1)) self.assertTrue(np.allclose(out2_c.numpy(), np_out2)) - self.assertFalse(out0_c._is_shared_buffer_with(out0)) - self.assertFalse(out1_c._is_shared_buffer_with(out1)) - self.assertFalse(out2_c._is_shared_buffer_with(out2)) - def call_split3(self): x_np = np.random.random(size=[9, 3, 5]).astype('float32') x = paddle.to_tensor(x_np) @@ -485,10 +462,6 @@ class TestStride(unittest.TestCase): self.assertTrue(np.allclose(out1_c.numpy(), np_out1)) self.assertTrue(np.allclose(out2_c.numpy(), np_out2)) - self.assertFalse(out0_c._is_shared_buffer_with(out0)) - self.assertFalse(out1_c._is_shared_buffer_with(out1)) - self.assertFalse(out2_c._is_shared_buffer_with(out2)) - def call_unbind(self): x_np = np.random.random(size=[3, 9, 5]).astype('float32') x = paddle.to_tensor(x_np) @@ -622,8 +595,6 @@ class TestStride(unittest.TestCase): self.assertTrue(np.allclose(out_c.numpy(), np_out)) - self.assertFalse(out_c._is_shared_buffer_with(out)) - def call_stride(self): self.call_transpose() self.call_diagonal() -- GitLab