OpenReg: Fix issue when creating empty tensor (#140496)

On the exeuctor side, when it is found that meta.data_ptr is not in the allocated memory, tensor creation will fail, but there is no need to allocate memory when creating an empty tensor.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/140496
Approved by: https://github.com/ezyang
This commit is contained in:
Zhenbin Lin
2024-11-14 11:10:37 +00:00
committed by PyTorch MergeBot
parent aaefa48441
commit 9a051f6ee0
2 changed files with 8 additions and 0 deletions

View File

@ -59,6 +59,10 @@ class Allocator:
storage_offset=0,
)
# Might be an empty tensor
if found_base is None and meta.nelem_in_bytes == 0:
found_base = torch.tensor((), dtype=torch.uint8)
# This pointer is not allocated here, segfault !
if found_base is None:
log.info("Currently allocated blocks:\n %s", safe_str(self.allocated))

View File

@ -125,6 +125,10 @@ class TestOpenReg(TestCase):
self.assertEqual(y.to(device="cpu"), torch.tensor([[1, 1], [2, 2], [3, 3]]))
self.assertEqual(x.data_ptr(), y.data_ptr())
def test_empty_tensor(self):
empty_tensor = torch.tensor((), device="openreg")
self.assertEqual(empty_tensor.to(device="cpu"), torch.tensor(()))
if __name__ == "__main__":
run_tests()