Skip to content

Commit

Permalink
fix contiguous (PaddlePaddle#56863)
Browse files Browse the repository at this point in the history
* fix contiguous
  • Loading branch information
wanghuancoder authored and BeingGod committed Sep 9, 2023
1 parent d7d6dc2 commit 53fc698
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 32 deletions.
7 changes: 4 additions & 3 deletions paddle/fluid/pybind/eager_method.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2819,9 +2819,10 @@ static PyObject* tensor_contiguous(TensorObject* self,
return reinterpret_cast<PyObject*>(self);
} else {
eager_gil_scoped_release guard;
return ToPyObject(
paddle::Tensor(std::make_shared<phi::DenseTensor>(std::move(
paddle::experimental::Trans2Contiguous(*(dense_tensor.get()))))));
self->tensor.set_impl(std::make_shared<phi::DenseTensor>(std::move(
paddle::experimental::Trans2Contiguous(*(dense_tensor.get())))));
Py_INCREF(self);
return reinterpret_cast<PyObject*>(self);
}

} else {
Expand Down
29 changes: 0 additions & 29 deletions test/legacy_test/test_stride.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,11 @@ def call_transpose(self):

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
Expand Down Expand Up @@ -96,11 +94,6 @@ def call_diagonal(self):
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)
Expand Down Expand Up @@ -141,8 +134,6 @@ def call_strided_slice(self):

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)
Expand All @@ -161,8 +152,6 @@ def call_index_select(self):

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)
Expand Down Expand Up @@ -201,8 +190,6 @@ def call_real(self):

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)
Expand All @@ -221,8 +208,6 @@ def call_imag(self):

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)
Expand Down Expand Up @@ -351,10 +336,6 @@ def call_split(self):
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)
Expand Down Expand Up @@ -386,10 +367,6 @@ def call_split2(self):
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)
Expand Down Expand Up @@ -485,10 +462,6 @@ def call_chunk(self):
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)
Expand Down Expand Up @@ -622,8 +595,6 @@ def call_unfold(self):

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()
Expand Down

0 comments on commit 53fc698

Please sign in to comment.