We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
PyTorch throws an exception on import when denormals are flushed to zero
This happens, for example, when you import DyNet before importing PyTorch. The code from #3113 tries to take the log(0) which throws an exception.

My guess is that DyNet is setting FTZ or DAZ (or both). See:
https://software.intel.com/en-us/node/523328

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