We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
print(torch.DoubleTensor(1)) # ZeroDivisionError: float division by zero
on print(torch.DoubleTensor(1)) I got ZeroDivisionError: float division by zero
but torch.DoubleTensor(0)) or torch.DoubleTensor(2)) work just fine.
I work in Jupyter notebook pytorch 0.1.12

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `torch/_tensor_str.py`
Content:
```
1 import math
2 import torch
3 from functools import reduce
4 from ._utils import _range
5 
6 
7 class __PrinterOptions(object):
8     precision = 4
9     threshold = 1000
10     edgeitems = 3
11     linewidth = 80
12 
13 
14 PRINT_OPTS = __PrinterOptions()
15 SCALE_FORMAT = '{:.5e} *\n'
16 
17 
18 # We could use **kwargs, but this will give better docs
19 def set_printoptions(
20         precision=None,
21         threshold=None,
22         edgeitems=None,
23         linewidth=None,
24         profile=None,
25 ):
26     """Set options for printing. Items shamelessly taken from Numpy
27 
28     Args:
29         precision: Number of digits of precision for floating point output
30             (default 8).
31         threshold: Total number of array elements which trigger summarization
32             rather than full repr (default 1000).
33         edgeitems: Number of array items in summary at beginning and end of
34             each dimension (default 3).
35         linewidth: The number of characters per line for the purpose of
36             inserting line breaks (default 80). Thresholded matricies will
37             ignore this parameter.
38         profile: Sane defaults for pretty printing. Can override with any of
39             the above options. (default, short, full)
40     """
41     if profile is not None:
42         if profile == "default":
43             PRINT_OPTS.precision = 4
44             PRINT_OPTS.threshold = 1000
45             PRINT_OPTS.edgeitems = 3
46             PRINT_OPTS.linewidth = 80
47         elif profile == "short":
48             PRINT_OPTS.precision = 2
49             PRINT_OPTS.threshold = 1000
50             PRINT_OPTS.edgeitems = 2
51             PRINT_OPTS.linewidth = 80
52         elif profile == "full":
53             PRINT_OPTS.precision = 4
54             PRINT_OPTS.threshold = float('inf')
55             PRINT_OPTS.edgeitems = 3
56             PRINT_OPTS.linewidth = 80
57 
58     if precision is not None:
59         PRINT_OPTS.precision = precision
60     if threshold is not None:
61         PRINT_OPTS.threshold = threshold
62     if edgeitems is not None:
63         PRINT_OPTS.edgeitems = edgeitems
64     if linewidth is not None:
65         PRINT_OPTS.linewidth = linewidth
66 
67 
68 def _number_format(tensor, min_sz=-1):
69     min_sz = max(min_sz, 2)
70     tensor = torch.DoubleTensor(tensor.size()).copy_(tensor).abs_().view(tensor.nelement())
71 
72     pos_inf_mask = tensor.eq(float('inf'))
73     neg_inf_mask = tensor.eq(float('-inf'))
74     nan_mask = tensor.ne(tensor)
75     invalid_value_mask = pos_inf_mask + neg_inf_mask + nan_mask
76     if invalid_value_mask.all():
77         example_value = 0
78     else:
79         example_value = tensor[invalid_value_mask.eq(0)][0]
80     tensor[invalid_value_mask] = example_value
81     if invalid_value_mask.any():
82         min_sz = max(min_sz, 3)
83 
84     int_mode = True
85     # TODO: use fmod?
86     for value in tensor:
87         if value != math.ceil(value):
88             int_mode = False
89             break
90 
91     exp_min = tensor.min()
92     if exp_min != 0:
93         exp_min = math.floor(math.log10(exp_min)) + 1
94     else:
95         exp_min = 1
96     exp_max = tensor.max()
97     if exp_max != 0:
98         exp_max = math.floor(math.log10(exp_max)) + 1
99     else:
100         exp_max = 1
101 
102     scale = 1
103     exp_max = int(exp_max)
104     prec = PRINT_OPTS.precision
105     if int_mode:
106         if exp_max > prec + 1:
107             format = '{{:11.{}e}}'.format(prec)
108             sz = max(min_sz, 7 + prec)
109         else:
110             sz = max(min_sz, exp_max + 1)
111             format = '{:' + str(sz) + '.0f}'
112     else:
113         if exp_max - exp_min > prec:
114             sz = 7 + prec
115             if abs(exp_max) > 99 or abs(exp_min) > 99:
116                 sz = sz + 1
117             sz = max(min_sz, sz)
118             format = '{{:{}.{}e}}'.format(sz, prec)
119         else:
120             if exp_max > prec + 1 or exp_max < 0:
121                 sz = max(min_sz, 7)
122                 scale = math.pow(10, exp_max - 1)
123             else:
124                 if exp_max == 0:
125                     sz = 7
126                 else:
127                     sz = exp_max + 6
128                 sz = max(min_sz, sz)
129             format = '{{:{}.{}f}}'.format(sz, prec)
130     return format, scale, sz
131 
132 
133 def _tensor_str(self):
134     n = PRINT_OPTS.edgeitems
135     has_hdots = self.size()[-1] > 2 * n
136     has_vdots = self.size()[-2] > 2 * n
137     print_full_mat = not has_hdots and not has_vdots
138     formatter = _number_format(self, min_sz=3 if not print_full_mat else 0)
139     print_dots = self.numel() >= PRINT_OPTS.threshold
140 
141     dim_sz = max(2, max(len(str(x)) for x in self.size()))
142     dim_fmt = "{:^" + str(dim_sz) + "}"
143     dot_fmt = u"{:^" + str(dim_sz + 1) + "}"
144 
145     counter_dim = self.ndimension() - 2
146     counter = torch.LongStorage(counter_dim).fill_(0)
147     counter[counter.size() - 1] = -1
148     finished = False
149     strt = ''
150     while True:
151         nrestarted = [False for i in counter]
152         nskipped = [False for i in counter]
153         for i in _range(counter_dim - 1, -1, -1):
154             counter[i] += 1
155             if print_dots and counter[i] == n and self.size(i) > 2 * n:
156                 counter[i] = self.size(i) - n
157                 nskipped[i] = True
158             if counter[i] == self.size(i):
159                 if i == 0:
160                     finished = True
161                 counter[i] = 0
162                 nrestarted[i] = True
163             else:
164                 break
165         if finished:
166             break
167         elif print_dots:
168             if any(nskipped):
169                 for hdot in nskipped:
170                     strt += dot_fmt.format('...') if hdot \
171                         else dot_fmt.format('')
172                 strt += '\n'
173             if any(nrestarted):
174                 strt += ' '
175                 for vdot in nrestarted:
176                     strt += dot_fmt.format(u'\u22EE' if vdot else '')
177                 strt += '\n'
178         if strt != '':
179             strt += '\n'
180         strt += '({},.,.) = \n'.format(
181             ','.join(dim_fmt.format(i) for i in counter))
182         submatrix = reduce(lambda t, i: t.select(0, i), counter, self)
183         strt += _matrix_str(submatrix, ' ', formatter, print_dots)
184     return strt
185 
186 
187 def __repr_row(row, indent, fmt, scale, sz, truncate=None):
188     if truncate is not None:
189         dotfmt = " {:^5} "
190         return (indent +
191                 ' '.join(fmt.format(val / scale) for val in row[:truncate]) +
192                 dotfmt.format('...') +
193                 ' '.join(fmt.format(val / scale) for val in row[-truncate:]) +
194                 '\n')
195     else:
196         return indent + ' '.join(fmt.format(val / scale) for val in row) + '\n'
197 
198 
199 def _matrix_str(self, indent='', formatter=None, force_truncate=False):
200     n = PRINT_OPTS.edgeitems
201     has_hdots = self.size(1) > 2 * n
202     has_vdots = self.size(0) > 2 * n
203     print_full_mat = not has_hdots and not has_vdots
204 
205     if formatter is None:
206         fmt, scale, sz = _number_format(self,
207                                         min_sz=5 if not print_full_mat else 0)
208     else:
209         fmt, scale, sz = formatter
210     nColumnPerLine = int(math.floor((PRINT_OPTS.linewidth - len(indent)) / (sz + 1)))
211     strt = ''
212     firstColumn = 0
213 
214     if not force_truncate and \
215        (self.numel() < PRINT_OPTS.threshold or print_full_mat):
216         while firstColumn < self.size(1):
217             lastColumn = min(firstColumn + nColumnPerLine - 1, self.size(1) - 1)
218             if nColumnPerLine < self.size(1):
219                 strt += '\n' if firstColumn != 1 else ''
220                 strt += 'Columns {} to {} \n{}'.format(
221                     firstColumn, lastColumn, indent)
222             if scale != 1:
223                 strt += SCALE_FORMAT.format(scale)
224             for l in _range(self.size(0)):
225                 strt += indent + (' ' if scale != 1 else '')
226                 row_slice = self[l, firstColumn:lastColumn + 1]
227                 strt += ' '.join(fmt.format(val / scale) for val in row_slice)
228                 strt += '\n'
229             firstColumn = lastColumn + 1
230     else:
231         if scale != 1:
232             strt += SCALE_FORMAT.format(scale)
233         if has_vdots and has_hdots:
234             vdotfmt = "{:^" + str((sz + 1) * n - 1) + "}"
235             ddotfmt = u"{:^5}"
236             for row in self[:n]:
237                 strt += __repr_row(row, indent, fmt, scale, sz, n)
238             strt += indent + ' '.join([vdotfmt.format('...'),
239                                        ddotfmt.format(u'\u22F1'),
240                                        vdotfmt.format('...')]) + "\n"
241             for row in self[-n:]:
242                 strt += __repr_row(row, indent, fmt, scale, sz, n)
243         elif not has_vdots and has_hdots:
244             for row in self:
245                 strt += __repr_row(row, indent, fmt, scale, sz, n)
246         elif has_vdots and not has_hdots:
247             vdotfmt = u"{:^" + \
248                 str(len(__repr_row(self[0], '', fmt, scale, sz))) + \
249                 "}\n"
250             for row in self[:n]:
251                 strt += __repr_row(row, indent, fmt, scale, sz)
252             strt += vdotfmt.format(u'\u22EE')
253             for row in self[-n:]:
254                 strt += __repr_row(row, indent, fmt, scale, sz)
255         else:
256             for row in self:
257                 strt += __repr_row(row, indent, fmt, scale, sz)
258     return strt
259 
260 
261 def _vector_str(self):
262     fmt, scale, sz = _number_format(self)
263     strt = ''
264     ident = ''
265     n = PRINT_OPTS.edgeitems
266     dotfmt = u"{:^" + str(sz) + "}\n"
267     if scale != 1:
268         strt += SCALE_FORMAT.format(scale)
269         ident = ' '
270     if self.numel() < PRINT_OPTS.threshold:
271         return (strt +
272                 '\n'.join(ident + fmt.format(val / scale) for val in self) +
273                 '\n')
274     else:
275         return (strt +
276                 '\n'.join(ident + fmt.format(val / scale) for val in self[:n]) +
277                 '\n' + (ident + dotfmt.format(u"\u22EE")) +
278                 '\n'.join(ident + fmt.format(val / scale) for val in self[-n:]) +
279                 '\n')
280 
281 
282 def _str(self):
283     if self.ndimension() == 0:
284         return '[{} with no dimension]\n'.format(torch.typename(self))
285     elif self.ndimension() == 1:
286         strt = _vector_str(self)
287     elif self.ndimension() == 2:
288         strt = _matrix_str(self)
289     else:
290         strt = _tensor_str(self)
291 
292     size_str = 'x'.join(str(size) for size in self.size())
293     device_str = '' if not self.is_cuda else \
294         ' (GPU {})'.format(self.get_device())
295     strt += '[{} of size {}{}]\n'.format(torch.typename(self),
296                                          size_str, device_str)
297     return '\n' + strt
298
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/torch/_tensor_str.py b/torch/_tensor_str.py
--- a/torch/_tensor_str.py
+++ b/torch/_tensor_str.py
@@ -2,6 +2,10 @@
 import torch
 from functools import reduce
 from ._utils import _range
+from sys import float_info
+
+
+__MIN_LOG_SCALE = math.ceil(math.log(float_info.min * float_info.epsilon, 10))
 
 
 class __PrinterOptions(object):
@@ -119,7 +123,7 @@
         else:
             if exp_max > prec + 1 or exp_max < 0:
                 sz = max(min_sz, 7)
-                scale = math.pow(10, exp_max - 1)
+                scale = math.pow(10, max(exp_max - 1, __MIN_LOG_SCALE))
             else:
                 if exp_max == 0:
                     sz = 7

{"golden_diff": "diff --git a/torch/_tensor_str.py b/torch/_tensor_str.py\n--- a/torch/_tensor_str.py\n+++ b/torch/_tensor_str.py\n@@ -2,6 +2,10 @@\n import torch\n from functools import reduce\n from ._utils import _range\n+from sys import float_info\n+\n+\n+__MIN_LOG_SCALE = math.ceil(math.log(float_info.min * float_info.epsilon, 10))\n \n \n class __PrinterOptions(object):\n@@ -119,7 +123,7 @@\n         else:\n             if exp_max > prec + 1 or exp_max < 0:\n                 sz = max(min_sz, 7)\n-                scale = math.pow(10, exp_max - 1)\n+                scale = math.pow(10, max(exp_max - 1, __MIN_LOG_SCALE))\n             else:\n                 if exp_max == 0:\n                     sz = 7\n", "issue": "print(torch.DoubleTensor(1)) # ZeroDivisionError: float division by zero\non print(torch.DoubleTensor(1)) I got ZeroDivisionError: float division by zero\r\nbut torch.DoubleTensor(0)) or torch.DoubleTensor(2)) work just fine.\r\nI work in Jupyter notebook pytorch 0.1.12\n", "before_files": [{"content": "import math\nimport torch\nfrom functools import reduce\nfrom ._utils import _range\n\n\nclass __PrinterOptions(object):\n    precision = 4\n    threshold = 1000\n    edgeitems = 3\n    linewidth = 80\n\n\nPRINT_OPTS = __PrinterOptions()\nSCALE_FORMAT = '{:.5e} *\\n'\n\n\n# We could use **kwargs, but this will give better docs\ndef set_printoptions(\n        precision=None,\n        threshold=None,\n        edgeitems=None,\n        linewidth=None,\n        profile=None,\n):\n    \"\"\"Set options for printing. Items shamelessly taken from Numpy\n\n    Args:\n        precision: Number of digits of precision for floating point output\n            (default 8).\n        threshold: Total number of array elements which trigger summarization\n            rather than full repr (default 1000).\n        edgeitems: Number of array items in summary at beginning and end of\n            each dimension (default 3).\n        linewidth: The number of characters per line for the purpose of\n            inserting line breaks (default 80). Thresholded matricies will\n            ignore this parameter.\n        profile: Sane defaults for pretty printing. Can override with any of\n            the above options. (default, short, full)\n    \"\"\"\n    if profile is not None:\n        if profile == \"default\":\n            PRINT_OPTS.precision = 4\n            PRINT_OPTS.threshold = 1000\n            PRINT_OPTS.edgeitems = 3\n            PRINT_OPTS.linewidth = 80\n        elif profile == \"short\":\n            PRINT_OPTS.precision = 2\n            PRINT_OPTS.threshold = 1000\n            PRINT_OPTS.edgeitems = 2\n            PRINT_OPTS.linewidth = 80\n        elif profile == \"full\":\n            PRINT_OPTS.precision = 4\n            PRINT_OPTS.threshold = float('inf')\n            PRINT_OPTS.edgeitems = 3\n            PRINT_OPTS.linewidth = 80\n\n    if precision is not None:\n        PRINT_OPTS.precision = precision\n    if threshold is not None:\n        PRINT_OPTS.threshold = threshold\n    if edgeitems is not None:\n        PRINT_OPTS.edgeitems = edgeitems\n    if linewidth is not None:\n        PRINT_OPTS.linewidth = linewidth\n\n\ndef _number_format(tensor, min_sz=-1):\n    min_sz = max(min_sz, 2)\n    tensor = torch.DoubleTensor(tensor.size()).copy_(tensor).abs_().view(tensor.nelement())\n\n    pos_inf_mask = tensor.eq(float('inf'))\n    neg_inf_mask = tensor.eq(float('-inf'))\n    nan_mask = tensor.ne(tensor)\n    invalid_value_mask = pos_inf_mask + neg_inf_mask + nan_mask\n    if invalid_value_mask.all():\n        example_value = 0\n    else:\n        example_value = tensor[invalid_value_mask.eq(0)][0]\n    tensor[invalid_value_mask] = example_value\n    if invalid_value_mask.any():\n        min_sz = max(min_sz, 3)\n\n    int_mode = True\n    # TODO: use fmod?\n    for value in tensor:\n        if value != math.ceil(value):\n            int_mode = False\n            break\n\n    exp_min = tensor.min()\n    if exp_min != 0:\n        exp_min = math.floor(math.log10(exp_min)) + 1\n    else:\n        exp_min = 1\n    exp_max = tensor.max()\n    if exp_max != 0:\n        exp_max = math.floor(math.log10(exp_max)) + 1\n    else:\n        exp_max = 1\n\n    scale = 1\n    exp_max = int(exp_max)\n    prec = PRINT_OPTS.precision\n    if int_mode:\n        if exp_max > prec + 1:\n            format = '{{:11.{}e}}'.format(prec)\n            sz = max(min_sz, 7 + prec)\n        else:\n            sz = max(min_sz, exp_max + 1)\n            format = '{:' + str(sz) + '.0f}'\n    else:\n        if exp_max - exp_min > prec:\n            sz = 7 + prec\n            if abs(exp_max) > 99 or abs(exp_min) > 99:\n                sz = sz + 1\n            sz = max(min_sz, sz)\n            format = '{{:{}.{}e}}'.format(sz, prec)\n        else:\n            if exp_max > prec + 1 or exp_max < 0:\n                sz = max(min_sz, 7)\n                scale = math.pow(10, exp_max - 1)\n            else:\n                if exp_max == 0:\n                    sz = 7\n                else:\n                    sz = exp_max + 6\n                sz = max(min_sz, sz)\n            format = '{{:{}.{}f}}'.format(sz, prec)\n    return format, scale, sz\n\n\ndef _tensor_str(self):\n    n = PRINT_OPTS.edgeitems\n    has_hdots = self.size()[-1] > 2 * n\n    has_vdots = self.size()[-2] > 2 * n\n    print_full_mat = not has_hdots and not has_vdots\n    formatter = _number_format(self, min_sz=3 if not print_full_mat else 0)\n    print_dots = self.numel() >= PRINT_OPTS.threshold\n\n    dim_sz = max(2, max(len(str(x)) for x in self.size()))\n    dim_fmt = \"{:^\" + str(dim_sz) + \"}\"\n    dot_fmt = u\"{:^\" + str(dim_sz + 1) + \"}\"\n\n    counter_dim = self.ndimension() - 2\n    counter = torch.LongStorage(counter_dim).fill_(0)\n    counter[counter.size() - 1] = -1\n    finished = False\n    strt = ''\n    while True:\n        nrestarted = [False for i in counter]\n        nskipped = [False for i in counter]\n        for i in _range(counter_dim - 1, -1, -1):\n            counter[i] += 1\n            if print_dots and counter[i] == n and self.size(i) > 2 * n:\n                counter[i] = self.size(i) - n\n                nskipped[i] = True\n            if counter[i] == self.size(i):\n                if i == 0:\n                    finished = True\n                counter[i] = 0\n                nrestarted[i] = True\n            else:\n                break\n        if finished:\n            break\n        elif print_dots:\n            if any(nskipped):\n                for hdot in nskipped:\n                    strt += dot_fmt.format('...') if hdot \\\n                        else dot_fmt.format('')\n                strt += '\\n'\n            if any(nrestarted):\n                strt += ' '\n                for vdot in nrestarted:\n                    strt += dot_fmt.format(u'\\u22EE' if vdot else '')\n                strt += '\\n'\n        if strt != '':\n            strt += '\\n'\n        strt += '({},.,.) = \\n'.format(\n            ','.join(dim_fmt.format(i) for i in counter))\n        submatrix = reduce(lambda t, i: t.select(0, i), counter, self)\n        strt += _matrix_str(submatrix, ' ', formatter, print_dots)\n    return strt\n\n\ndef __repr_row(row, indent, fmt, scale, sz, truncate=None):\n    if truncate is not None:\n        dotfmt = \" {:^5} \"\n        return (indent +\n                ' '.join(fmt.format(val / scale) for val in row[:truncate]) +\n                dotfmt.format('...') +\n                ' '.join(fmt.format(val / scale) for val in row[-truncate:]) +\n                '\\n')\n    else:\n        return indent + ' '.join(fmt.format(val / scale) for val in row) + '\\n'\n\n\ndef _matrix_str(self, indent='', formatter=None, force_truncate=False):\n    n = PRINT_OPTS.edgeitems\n    has_hdots = self.size(1) > 2 * n\n    has_vdots = self.size(0) > 2 * n\n    print_full_mat = not has_hdots and not has_vdots\n\n    if formatter is None:\n        fmt, scale, sz = _number_format(self,\n                                        min_sz=5 if not print_full_mat else 0)\n    else:\n        fmt, scale, sz = formatter\n    nColumnPerLine = int(math.floor((PRINT_OPTS.linewidth - len(indent)) / (sz + 1)))\n    strt = ''\n    firstColumn = 0\n\n    if not force_truncate and \\\n       (self.numel() < PRINT_OPTS.threshold or print_full_mat):\n        while firstColumn < self.size(1):\n            lastColumn = min(firstColumn + nColumnPerLine - 1, self.size(1) - 1)\n            if nColumnPerLine < self.size(1):\n                strt += '\\n' if firstColumn != 1 else ''\n                strt += 'Columns {} to {} \\n{}'.format(\n                    firstColumn, lastColumn, indent)\n            if scale != 1:\n                strt += SCALE_FORMAT.format(scale)\n            for l in _range(self.size(0)):\n                strt += indent + (' ' if scale != 1 else '')\n                row_slice = self[l, firstColumn:lastColumn + 1]\n                strt += ' '.join(fmt.format(val / scale) for val in row_slice)\n                strt += '\\n'\n            firstColumn = lastColumn + 1\n    else:\n        if scale != 1:\n            strt += SCALE_FORMAT.format(scale)\n        if has_vdots and has_hdots:\n            vdotfmt = \"{:^\" + str((sz + 1) * n - 1) + \"}\"\n            ddotfmt = u\"{:^5}\"\n            for row in self[:n]:\n                strt += __repr_row(row, indent, fmt, scale, sz, n)\n            strt += indent + ' '.join([vdotfmt.format('...'),\n                                       ddotfmt.format(u'\\u22F1'),\n                                       vdotfmt.format('...')]) + \"\\n\"\n            for row in self[-n:]:\n                strt += __repr_row(row, indent, fmt, scale, sz, n)\n        elif not has_vdots and has_hdots:\n            for row in self:\n                strt += __repr_row(row, indent, fmt, scale, sz, n)\n        elif has_vdots and not has_hdots:\n            vdotfmt = u\"{:^\" + \\\n                str(len(__repr_row(self[0], '', fmt, scale, sz))) + \\\n                \"}\\n\"\n            for row in self[:n]:\n                strt += __repr_row(row, indent, fmt, scale, sz)\n            strt += vdotfmt.format(u'\\u22EE')\n            for row in self[-n:]:\n                strt += __repr_row(row, indent, fmt, scale, sz)\n        else:\n            for row in self:\n                strt += __repr_row(row, indent, fmt, scale, sz)\n    return strt\n\n\ndef _vector_str(self):\n    fmt, scale, sz = _number_format(self)\n    strt = ''\n    ident = ''\n    n = PRINT_OPTS.edgeitems\n    dotfmt = u\"{:^\" + str(sz) + \"}\\n\"\n    if scale != 1:\n        strt += SCALE_FORMAT.format(scale)\n        ident = ' '\n    if self.numel() < PRINT_OPTS.threshold:\n        return (strt +\n                '\\n'.join(ident + fmt.format(val / scale) for val in self) +\n                '\\n')\n    else:\n        return (strt +\n                '\\n'.join(ident + fmt.format(val / scale) for val in self[:n]) +\n                '\\n' + (ident + dotfmt.format(u\"\\u22EE\")) +\n                '\\n'.join(ident + fmt.format(val / scale) for val in self[-n:]) +\n                '\\n')\n\n\ndef _str(self):\n    if self.ndimension() == 0:\n        return '[{} with no dimension]\\n'.format(torch.typename(self))\n    elif self.ndimension() == 1:\n        strt = _vector_str(self)\n    elif self.ndimension() == 2:\n        strt = _matrix_str(self)\n    else:\n        strt = _tensor_str(self)\n\n    size_str = 'x'.join(str(size) for size in self.size())\n    device_str = '' if not self.is_cuda else \\\n        ' (GPU {})'.format(self.get_device())\n    strt += '[{} of size {}{}]\\n'.format(torch.typename(self),\n                                         size_str, device_str)\n    return '\\n' + strt\n", "path": "torch/_tensor_str.py"}], "after_files": [{"content": "import math\nimport torch\nfrom functools import reduce\nfrom ._utils import _range\nfrom sys import float_info\n\n\n__MIN_LOG_SCALE = math.ceil(math.log(float_info.min * float_info.epsilon, 10))\n\n\nclass __PrinterOptions(object):\n    precision = 4\n    threshold = 1000\n    edgeitems = 3\n    linewidth = 80\n\n\nPRINT_OPTS = __PrinterOptions()\nSCALE_FORMAT = '{:.5e} *\\n'\n\n\n# We could use **kwargs, but this will give better docs\ndef set_printoptions(\n        precision=None,\n        threshold=None,\n        edgeitems=None,\n        linewidth=None,\n        profile=None,\n):\n    \"\"\"Set options for printing. Items shamelessly taken from Numpy\n\n    Args:\n        precision: Number of digits of precision for floating point output\n            (default 8).\n        threshold: Total number of array elements which trigger summarization\n            rather than full repr (default 1000).\n        edgeitems: Number of array items in summary at beginning and end of\n            each dimension (default 3).\n        linewidth: The number of characters per line for the purpose of\n            inserting line breaks (default 80). Thresholded matricies will\n            ignore this parameter.\n        profile: Sane defaults for pretty printing. Can override with any of\n            the above options. (default, short, full)\n    \"\"\"\n    if profile is not None:\n        if profile == \"default\":\n            PRINT_OPTS.precision = 4\n            PRINT_OPTS.threshold = 1000\n            PRINT_OPTS.edgeitems = 3\n            PRINT_OPTS.linewidth = 80\n        elif profile == \"short\":\n            PRINT_OPTS.precision = 2\n            PRINT_OPTS.threshold = 1000\n            PRINT_OPTS.edgeitems = 2\n            PRINT_OPTS.linewidth = 80\n        elif profile == \"full\":\n            PRINT_OPTS.precision = 4\n            PRINT_OPTS.threshold = float('inf')\n            PRINT_OPTS.edgeitems = 3\n            PRINT_OPTS.linewidth = 80\n\n    if precision is not None:\n        PRINT_OPTS.precision = precision\n    if threshold is not None:\n        PRINT_OPTS.threshold = threshold\n    if edgeitems is not None:\n        PRINT_OPTS.edgeitems = edgeitems\n    if linewidth is not None:\n        PRINT_OPTS.linewidth = linewidth\n\n\ndef _number_format(tensor, min_sz=-1):\n    min_sz = max(min_sz, 2)\n    tensor = torch.DoubleTensor(tensor.size()).copy_(tensor).abs_().view(tensor.nelement())\n\n    pos_inf_mask = tensor.eq(float('inf'))\n    neg_inf_mask = tensor.eq(float('-inf'))\n    nan_mask = tensor.ne(tensor)\n    invalid_value_mask = pos_inf_mask + neg_inf_mask + nan_mask\n    if invalid_value_mask.all():\n        example_value = 0\n    else:\n        example_value = tensor[invalid_value_mask.eq(0)][0]\n    tensor[invalid_value_mask] = example_value\n    if invalid_value_mask.any():\n        min_sz = max(min_sz, 3)\n\n    int_mode = True\n    # TODO: use fmod?\n    for value in tensor:\n        if value != math.ceil(value):\n            int_mode = False\n            break\n\n    exp_min = tensor.min()\n    if exp_min != 0:\n        exp_min = math.floor(math.log10(exp_min)) + 1\n    else:\n        exp_min = 1\n    exp_max = tensor.max()\n    if exp_max != 0:\n        exp_max = math.floor(math.log10(exp_max)) + 1\n    else:\n        exp_max = 1\n\n    scale = 1\n    exp_max = int(exp_max)\n    prec = PRINT_OPTS.precision\n    if int_mode:\n        if exp_max > prec + 1:\n            format = '{{:11.{}e}}'.format(prec)\n            sz = max(min_sz, 7 + prec)\n        else:\n            sz = max(min_sz, exp_max + 1)\n            format = '{:' + str(sz) + '.0f}'\n    else:\n        if exp_max - exp_min > prec:\n            sz = 7 + prec\n            if abs(exp_max) > 99 or abs(exp_min) > 99:\n                sz = sz + 1\n            sz = max(min_sz, sz)\n            format = '{{:{}.{}e}}'.format(sz, prec)\n        else:\n            if exp_max > prec + 1 or exp_max < 0:\n                sz = max(min_sz, 7)\n                scale = math.pow(10, max(exp_max - 1, __MIN_LOG_SCALE))\n            else:\n                if exp_max == 0:\n                    sz = 7\n                else:\n                    sz = exp_max + 6\n                sz = max(min_sz, sz)\n            format = '{{:{}.{}f}}'.format(sz, prec)\n    return format, scale, sz\n\n\ndef _tensor_str(self):\n    n = PRINT_OPTS.edgeitems\n    has_hdots = self.size()[-1] > 2 * n\n    has_vdots = self.size()[-2] > 2 * n\n    print_full_mat = not has_hdots and not has_vdots\n    formatter = _number_format(self, min_sz=3 if not print_full_mat else 0)\n    print_dots = self.numel() >= PRINT_OPTS.threshold\n\n    dim_sz = max(2, max(len(str(x)) for x in self.size()))\n    dim_fmt = \"{:^\" + str(dim_sz) + \"}\"\n    dot_fmt = u\"{:^\" + str(dim_sz + 1) + \"}\"\n\n    counter_dim = self.ndimension() - 2\n    counter = torch.LongStorage(counter_dim).fill_(0)\n    counter[counter.size() - 1] = -1\n    finished = False\n    strt = ''\n    while True:\n        nrestarted = [False for i in counter]\n        nskipped = [False for i in counter]\n        for i in _range(counter_dim - 1, -1, -1):\n            counter[i] += 1\n            if print_dots and counter[i] == n and self.size(i) > 2 * n:\n                counter[i] = self.size(i) - n\n                nskipped[i] = True\n            if counter[i] == self.size(i):\n                if i == 0:\n                    finished = True\n                counter[i] = 0\n                nrestarted[i] = True\n            else:\n                break\n        if finished:\n            break\n        elif print_dots:\n            if any(nskipped):\n                for hdot in nskipped:\n                    strt += dot_fmt.format('...') if hdot \\\n                        else dot_fmt.format('')\n                strt += '\\n'\n            if any(nrestarted):\n                strt += ' '\n                for vdot in nrestarted:\n                    strt += dot_fmt.format(u'\\u22EE' if vdot else '')\n                strt += '\\n'\n        if strt != '':\n            strt += '\\n'\n        strt += '({},.,.) = \\n'.format(\n            ','.join(dim_fmt.format(i) for i in counter))\n        submatrix = reduce(lambda t, i: t.select(0, i), counter, self)\n        strt += _matrix_str(submatrix, ' ', formatter, print_dots)\n    return strt\n\n\ndef __repr_row(row, indent, fmt, scale, sz, truncate=None):\n    if truncate is not None:\n        dotfmt = \" {:^5} \"\n        return (indent +\n                ' '.join(fmt.format(val / scale) for val in row[:truncate]) +\n                dotfmt.format('...') +\n                ' '.join(fmt.format(val / scale) for val in row[-truncate:]) +\n                '\\n')\n    else:\n        return indent + ' '.join(fmt.format(val / scale) for val in row) + '\\n'\n\n\ndef _matrix_str(self, indent='', formatter=None, force_truncate=False):\n    n = PRINT_OPTS.edgeitems\n    has_hdots = self.size(1) > 2 * n\n    has_vdots = self.size(0) > 2 * n\n    print_full_mat = not has_hdots and not has_vdots\n\n    if formatter is None:\n        fmt, scale, sz = _number_format(self,\n                                        min_sz=5 if not print_full_mat else 0)\n    else:\n        fmt, scale, sz = formatter\n    nColumnPerLine = int(math.floor((PRINT_OPTS.linewidth - len(indent)) / (sz + 1)))\n    strt = ''\n    firstColumn = 0\n\n    if not force_truncate and \\\n       (self.numel() < PRINT_OPTS.threshold or print_full_mat):\n        while firstColumn < self.size(1):\n            lastColumn = min(firstColumn + nColumnPerLine - 1, self.size(1) - 1)\n            if nColumnPerLine < self.size(1):\n                strt += '\\n' if firstColumn != 1 else ''\n                strt += 'Columns {} to {} \\n{}'.format(\n                    firstColumn, lastColumn, indent)\n            if scale != 1:\n                strt += SCALE_FORMAT.format(scale)\n            for l in _range(self.size(0)):\n                strt += indent + (' ' if scale != 1 else '')\n                row_slice = self[l, firstColumn:lastColumn + 1]\n                strt += ' '.join(fmt.format(val / scale) for val in row_slice)\n                strt += '\\n'\n            firstColumn = lastColumn + 1\n    else:\n        if scale != 1:\n            strt += SCALE_FORMAT.format(scale)\n        if has_vdots and has_hdots:\n            vdotfmt = \"{:^\" + str((sz + 1) * n - 1) + \"}\"\n            ddotfmt = u\"{:^5}\"\n            for row in self[:n]:\n                strt += __repr_row(row, indent, fmt, scale, sz, n)\n            strt += indent + ' '.join([vdotfmt.format('...'),\n                                       ddotfmt.format(u'\\u22F1'),\n                                       vdotfmt.format('...')]) + \"\\n\"\n            for row in self[-n:]:\n                strt += __repr_row(row, indent, fmt, scale, sz, n)\n        elif not has_vdots and has_hdots:\n            for row in self:\n                strt += __repr_row(row, indent, fmt, scale, sz, n)\n        elif has_vdots and not has_hdots:\n            vdotfmt = u\"{:^\" + \\\n                str(len(__repr_row(self[0], '', fmt, scale, sz))) + \\\n                \"}\\n\"\n            for row in self[:n]:\n                strt += __repr_row(row, indent, fmt, scale, sz)\n            strt += vdotfmt.format(u'\\u22EE')\n            for row in self[-n:]:\n                strt += __repr_row(row, indent, fmt, scale, sz)\n        else:\n            for row in self:\n                strt += __repr_row(row, indent, fmt, scale, sz)\n    return strt\n\n\ndef _vector_str(self):\n    fmt, scale, sz = _number_format(self)\n    strt = ''\n    ident = ''\n    n = PRINT_OPTS.edgeitems\n    dotfmt = u\"{:^\" + str(sz) + \"}\\n\"\n    if scale != 1:\n        strt += SCALE_FORMAT.format(scale)\n        ident = ' '\n    if self.numel() < PRINT_OPTS.threshold:\n        return (strt +\n                '\\n'.join(ident + fmt.format(val / scale) for val in self) +\n                '\\n')\n    else:\n        return (strt +\n                '\\n'.join(ident + fmt.format(val / scale) for val in self[:n]) +\n                '\\n' + (ident + dotfmt.format(u\"\\u22EE\")) +\n                '\\n'.join(ident + fmt.format(val / scale) for val in self[-n:]) +\n                '\\n')\n\n\ndef _str(self):\n    if self.ndimension() == 0:\n        return '[{} with no dimension]\\n'.format(torch.typename(self))\n    elif self.ndimension() == 1:\n        strt = _vector_str(self)\n    elif self.ndimension() == 2:\n        strt = _matrix_str(self)\n    else:\n        strt = _tensor_str(self)\n\n    size_str = 'x'.join(str(size) for size in self.size())\n    device_str = '' if not self.is_cuda else \\\n        ' (GPU {})'.format(self.get_device())\n    strt += '[{} of size {}{}]\\n'.format(torch.typename(self),\n                                         size_str, device_str)\n    return '\\n' + strt\n", "path": "torch/_tensor_str.py"}]}