Skip to content
Closed
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
11 changes: 11 additions & 0 deletions test/test_torch.py
Original file line number Diff line number Diff line change
Expand Up @@ -8299,6 +8299,10 @@ def test_print(self):
obj = t(100, 100).fill_(1)
obj.__repr__()
str(obj)
# test half tensor
obj = torch.rand(100, 100, device='cpu').half()
obj.__repr__()
str(obj)
for t in torch._storage_classes:
if t.is_cuda and not torch.cuda.is_available():
continue
Expand Down Expand Up @@ -8353,6 +8357,13 @@ def test_print(self):
self.assertEqual(x.__repr__(), str(x))
self.assertExpectedInline(str(x), '''tensor([0., 0., 0., ..., 0., 0., 0.])''')

# test internal summary function
x = torch.rand(1, 20, 5, 30)
summary = torch._tensor_str.get_summarized_data(x)
self.assertEqual(summary.shape, (1, 6, 5, 6))
first_and_last = [0, 1, 2, -3, -2, -1]
self.assertEqual(summary, x[:, first_and_last][..., first_and_last])

# test device
if torch.cuda.is_available():
x = torch.tensor([123], device='cuda:0')
Expand Down
10 changes: 6 additions & 4 deletions torch/_tensor_str.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ def _tensor_str(self, indent):
return '[]'

summarize = self.numel() > PRINT_OPTS.threshold
if self.dtype is torch.float16:
self = self.float()
formatter = _Formatter(get_summarized_data(self) if summarize else self)
return _tensor_str_with_formatter(self, indent, formatter, summarize)

Expand Down Expand Up @@ -220,12 +222,12 @@ def get_summarized_data(self):
else:
return self
if self.size(0) > 2 * PRINT_OPTS.edgeitems:
start = [get_summarized_data(self[i]).reshape(-1) for i in range(0, PRINT_OPTS.edgeitems)]
end = ([get_summarized_data(self[i]).reshape(-1)
start = [self[i] for i in range(0, PRINT_OPTS.edgeitems)]
end = ([self[i]
for i in range(len(self) - PRINT_OPTS.edgeitems, len(self))])
return torch.cat((start + end))
return torch.stack([get_summarized_data(x) for x in (start + end)])
else:
return self
return torch.stack([get_summarized_data(x) for x in self])


def _str(self):
Expand Down