Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions test/test_torch.py
Original file line number Diff line number Diff line change
Expand Up @@ -4349,6 +4349,19 @@ def test_print(self):
x.__repr__()
str(x),

def test_sizeof(self):
sizeof_empty = torch.randn(0).storage().__sizeof__()
sizeof_10 = torch.randn(10).storage().__sizeof__()
sizeof_100 = torch.randn(100).storage().__sizeof__()
self.assertEqual((sizeof_100 - sizeof_empty) // (sizeof_10 - sizeof_empty), 10)
self.assertEqual((sizeof_100 - sizeof_empty) % (sizeof_10 - sizeof_empty), 0)

sizeof_empty = torch.randn(0).type(torch.ByteTensor).storage().__sizeof__()
sizeof_10 = torch.randn(10).type(torch.ByteTensor).storage().__sizeof__()
sizeof_100 = torch.randn(100).type(torch.ByteTensor).storage().__sizeof__()
self.assertEqual((sizeof_100 - sizeof_empty) // (sizeof_10 - sizeof_empty), 10)
self.assertEqual((sizeof_100 - sizeof_empty) % (sizeof_10 - sizeof_empty), 0)

def test_unsqueeze(self):
x = torch.randn(2, 3, 4)
y = x.unsqueeze(1)
Expand Down
3 changes: 3 additions & 0 deletions torch/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ def __deepcopy__(self, memo):
def __reduce__(self):
return type(self), (self.tolist(),)

def __sizeof__(self):
return super(_StorageBase, self).__sizeof__() + self.element_size() * self.size()

def clone(self):
"""Returns a copy of this storage"""
return type(self)(self.size()).copy_(self)
Expand Down