Skip to content

Commit d7fc378

Browse files
fix contiguous (#56863)
* fix contiguous
1 parent 83b942f commit d7fc378

File tree

2 files changed

+4
-32
lines changed

2 files changed

+4
-32
lines changed

paddle/fluid/pybind/eager_method.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2819,9 +2819,10 @@ static PyObject* tensor_contiguous(TensorObject* self,
28192819
return reinterpret_cast<PyObject*>(self);
28202820
} else {
28212821
eager_gil_scoped_release guard;
2822-
return ToPyObject(
2823-
paddle::Tensor(std::make_shared<phi::DenseTensor>(std::move(
2824-
paddle::experimental::Trans2Contiguous(*(dense_tensor.get()))))));
2822+
self->tensor.set_impl(std::make_shared<phi::DenseTensor>(std::move(
2823+
paddle::experimental::Trans2Contiguous(*(dense_tensor.get())))));
2824+
Py_INCREF(self);
2825+
return reinterpret_cast<PyObject*>(self);
28252826
}
28262827

28272828
} else {

test/legacy_test/test_stride.py

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,11 @@ def call_transpose(self):
4242

4343
x_c = x_transposed1.contiguous()
4444
self.assertTrue(np.allclose(x_c.numpy(), x_np_transposed1))
45-
self.assertFalse(x_c._is_shared_buffer_with(x_transposed1))
4645

4746
x_transposed2 = paddle.transpose(x_transposed1, perm=[2, 0, 1])
4847
x_np_transposed2 = x_np_transposed1.transpose(2, 0, 1)
4948
self.assertTrue(np.allclose(x_transposed2.numpy(), x_np_transposed2))
5049
self.assertFalse(x_transposed2.is_contiguous())
51-
self.assertTrue(x._is_shared_buffer_with(x_transposed2))
5250

5351
y = x_transposed2 + 2
5452
y_np = x_np_transposed2 + 2
@@ -96,11 +94,6 @@ def call_diagonal(self):
9694
self.assertTrue(np.allclose(out3_c.numpy(), np_out3))
9795
self.assertTrue(np.allclose(out4_c.numpy(), np_out4))
9896

99-
self.assertFalse(out_c._is_shared_buffer_with(out))
100-
self.assertFalse(out2_c._is_shared_buffer_with(out2))
101-
self.assertFalse(out3_c._is_shared_buffer_with(out3))
102-
self.assertFalse(out4_c._is_shared_buffer_with(out4))
103-
10497
def call_slice(self):
10598
x_np = np.random.random(size=[10, 10, 10, 20]).astype('float32')
10699
x = paddle.to_tensor(x_np)
@@ -141,8 +134,6 @@ def call_strided_slice(self):
141134

142135
self.assertTrue(np.allclose(out_c.numpy(), np_out))
143136

144-
self.assertFalse(out_c._is_shared_buffer_with(out))
145-
146137
def call_index_select(self):
147138
x_np = np.random.random(size=[10, 10, 10, 20]).astype('float32')
148139
x = paddle.to_tensor(x_np)
@@ -161,8 +152,6 @@ def call_index_select(self):
161152

162153
self.assertTrue(np.allclose(out_c.numpy(), np_out))
163154

164-
self.assertFalse(out_c._is_shared_buffer_with(out))
165-
166155
def call_reshape(self):
167156
x_np = np.random.random(size=[10, 10, 10, 20]).astype('float32')
168157
x = paddle.to_tensor(x_np)
@@ -201,8 +190,6 @@ def call_real(self):
201190

202191
self.assertTrue(np.allclose(out_c.numpy(), np_out))
203192

204-
self.assertFalse(out_c._is_shared_buffer_with(out))
205-
206193
def call_imag(self):
207194
x_np = np.random.random(size=[10, 10, 10, 20]).astype('complex128')
208195
x = paddle.to_tensor(x_np)
@@ -221,8 +208,6 @@ def call_imag(self):
221208

222209
self.assertTrue(np.allclose(out_c.numpy(), np_out))
223210

224-
self.assertFalse(out_c._is_shared_buffer_with(out))
225-
226211
def call_as_real(self):
227212
x_np = np.random.random(size=[10, 10, 10, 20]).astype('complex128')
228213
x = paddle.to_tensor(x_np)
@@ -351,10 +336,6 @@ def call_split(self):
351336
self.assertTrue(np.allclose(out1_c.numpy(), np_out1))
352337
self.assertTrue(np.allclose(out2_c.numpy(), np_out2))
353338

354-
self.assertFalse(out0_c._is_shared_buffer_with(out0))
355-
self.assertFalse(out1_c._is_shared_buffer_with(out1))
356-
self.assertFalse(out2_c._is_shared_buffer_with(out2))
357-
358339
def call_split2(self):
359340
x_np = np.random.random(size=[3, 9, 5]).astype('float32')
360341
x = paddle.to_tensor(x_np)
@@ -386,10 +367,6 @@ def call_split2(self):
386367
self.assertTrue(np.allclose(out1_c.numpy(), np_out1))
387368
self.assertTrue(np.allclose(out2_c.numpy(), np_out2))
388369

389-
self.assertFalse(out0_c._is_shared_buffer_with(out0))
390-
self.assertFalse(out1_c._is_shared_buffer_with(out1))
391-
self.assertFalse(out2_c._is_shared_buffer_with(out2))
392-
393370
def call_split3(self):
394371
x_np = np.random.random(size=[9, 3, 5]).astype('float32')
395372
x = paddle.to_tensor(x_np)
@@ -485,10 +462,6 @@ def call_chunk(self):
485462
self.assertTrue(np.allclose(out1_c.numpy(), np_out1))
486463
self.assertTrue(np.allclose(out2_c.numpy(), np_out2))
487464

488-
self.assertFalse(out0_c._is_shared_buffer_with(out0))
489-
self.assertFalse(out1_c._is_shared_buffer_with(out1))
490-
self.assertFalse(out2_c._is_shared_buffer_with(out2))
491-
492465
def call_unbind(self):
493466
x_np = np.random.random(size=[3, 9, 5]).astype('float32')
494467
x = paddle.to_tensor(x_np)
@@ -622,8 +595,6 @@ def call_unfold(self):
622595

623596
self.assertTrue(np.allclose(out_c.numpy(), np_out))
624597

625-
self.assertFalse(out_c._is_shared_buffer_with(out))
626-
627598
def call_stride(self):
628599
self.call_transpose()
629600
self.call_diagonal()

0 commit comments

Comments
 (0)