[PyTorch] Add tests for empty tensors w/storage null data_ptr (#101426)

Further investigation seems to show that changing this behavior (making empty tensors sometimes have non-null data_ptr) was the real problem with #98090 . Adding tests to lock down this behavior so we don't change it by accident again.

Differential Revision: [D45873002](https://our.internmc.facebook.com/intern/diff/D45873002/)
Pull Request resolved: https://github.com/pytorch/pytorch/pull/101426
Approved by: https://github.com/zou3519
This commit is contained in:
Scott Wolchok 2023-07-26 09:31:44 -07:00 committed by PyTorch MergeBot
parent 5d8596292b
commit b435bff53a

View File

@ -9108,6 +9108,18 @@ tensor([[[1.+1.j, 1.+1.j, 1.+1.j, ..., 1.+1.j, 1.+1.j, 1.+1.j],
self.assertEqual(b, c)
def test_data_ptr_of_empty_tensor_with_storage(self):
t = torch.empty((2, 2))
self.assertNotEqual(t.data_ptr(), 0)
t.resize_((0, 2))
self.assertEqual(t.data_ptr(), 0)
def test_data_ptr_of_empty_view_with_storage(self):
t = torch.empty((2, 2))
self.assertNotEqual(t.data_ptr(), 0)
t2 = t[0:0].view(0, 1)
self.assertEqual(t2.data_ptr(), 0)
# The following block extends TestTorch with negative dim wrapping tests
# FIXME: replace these with OpInfo sample inputs or systemic OpInfo tests
# Functions to test negative dimension wrapping