<h2 dir="auto">Issue description</h2>
<p dir="auto"><code class="notranslate">torch.min</code> and <code class="notranslate">torch.max</code> ignores <code class="notranslate">nan</code> in cuda case and crash with pure <code class="notranslate">nan</code> tensor.</p>
<h2 dir="auto">Code example</h2>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="&gt;&gt;&gt; import torch
&gt;&gt;&gt; a = torch.Tensor(4, 3).fill_(1)
&gt;&gt;&gt; a = a.cuda()
&gt;&gt;&gt; a[1, 2] = float('nan')
&gt;&gt;&gt; a
tensor([[  1.,   1.,   1.],
        [  1.,   1., nan.],
        [  1.,   1.,   1.],
        [  1.,   1.,   1.]], device='cuda:0')
&gt;&gt;&gt; torch.max(a)
tensor(1., device='cuda:0')
&gt;&gt;&gt; torch.min(a)
tensor(1., device='cuda:0')
&gt;&gt;&gt; a = torch.Tensor(4, 3).fill_(float('nan'))
&gt;&gt;&gt; a = a.cuda()
&gt;&gt;&gt; torch.max(a)
Traceback (most recent call last):
  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;
  File &quot;anaconda3/lib/python3.6/site-packages/torch/tensor.py&quot;, line 57, in __repr__
    return torch._tensor_str._str(self)
  File &quot;anaconda3/lib/python3.6/site-packages/torch/_tensor_str.py&quot;, line 218, in _str
    fmt, scale, sz = _number_format(self)
  File &quot;anaconda3/lib/python3.6/site-packages/torch/_tensor_str.py&quot;, line 96, in _number_format
    if value != math.ceil(value.item()):
RuntimeError: Overflow when unpacking long
&gt;&gt;&gt; torch.min(a)
Traceback (most recent call last):
  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;
  File &quot;anaconda3/lib/python3.6/site-packages/torch/tensor.py&quot;, line 57, in __repr__
    return torch._tensor_str._str(self)
  File &quot;anaconda3/lib/python3.6/site-packages/torch/_tensor_str.py&quot;, line 218, in _str
    fmt, scale, sz = _number_format(self)
  File &quot;anaconda3/lib/python3.6/site-packages/torch/_tensor_str.py&quot;, line 96, in _number_format
    if value != math.ceil(value.item()):
RuntimeError: Overflow when unpacking long"><pre class="notranslate"><span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-k">import</span> <span class="pl-s1">torch</span>
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">a</span> <span class="pl-c1">=</span> <span class="pl-s1">torch</span>.<span class="pl-v">Tensor</span>(<span class="pl-c1">4</span>, <span class="pl-c1">3</span>).<span class="pl-en">fill_</span>(<span class="pl-c1">1</span>)
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">a</span> <span class="pl-c1">=</span> <span class="pl-s1">a</span>.<span class="pl-en">cuda</span>()
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">a</span>[<span class="pl-c1">1</span>, <span class="pl-c1">2</span>] <span class="pl-c1">=</span> <span class="pl-en">float</span>(<span class="pl-s">'nan'</span>)
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">a</span>
<span class="pl-en">tensor</span>([[  <span class="pl-c1">1.</span>,   <span class="pl-c1">1.</span>,   <span class="pl-c1">1.</span>],
        [  <span class="pl-c1">1.</span>,   <span class="pl-c1">1.</span>, <span class="pl-s1">nan</span>.<span class="pl-s1"></span>],
        [  <span class="pl-c1">1.</span>,   <span class="pl-c1">1.</span>,   <span class="pl-c1">1.</span>],
        [  <span class="pl-c1">1.</span>,   <span class="pl-c1">1.</span>,   <span class="pl-c1">1.</span>]], <span class="pl-s1">device</span><span class="pl-c1">=</span><span class="pl-s">'cuda:0'</span>)
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">torch</span>.<span class="pl-en">max</span>(<span class="pl-s1">a</span>)
<span class="pl-en">tensor</span>(<span class="pl-c1">1.</span>, <span class="pl-s1">device</span><span class="pl-c1">=</span><span class="pl-s">'cuda:0'</span>)
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">torch</span>.<span class="pl-en">min</span>(<span class="pl-s1">a</span>)
<span class="pl-en">tensor</span>(<span class="pl-c1">1.</span>, <span class="pl-s1">device</span><span class="pl-c1">=</span><span class="pl-s">'cuda:0'</span>)
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">a</span> <span class="pl-c1">=</span> <span class="pl-s1">torch</span>.<span class="pl-v">Tensor</span>(<span class="pl-c1">4</span>, <span class="pl-c1">3</span>).<span class="pl-en">fill_</span>(<span class="pl-en">float</span>(<span class="pl-s">'nan'</span>))
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">a</span> <span class="pl-c1">=</span> <span class="pl-s1">a</span>.<span class="pl-en">cuda</span>()
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">torch</span>.<span class="pl-en">max</span>(<span class="pl-s1">a</span>)
<span class="pl-v">Traceback</span> (<span class="pl-s1">most</span> <span class="pl-s1">recent</span> <span class="pl-s1">call</span> <span class="pl-s1">last</span>):
  <span class="pl-v">File</span> <span class="pl-s">"&lt;stdin&gt;"</span>, <span class="pl-s1">line</span> <span class="pl-c1">1</span>, <span class="pl-s1">in</span> <span class="pl-c1">&lt;</span><span class="pl-s1">module</span><span class="pl-c1">&gt;</span>
  <span class="pl-v">File</span> <span class="pl-s">"anaconda3/lib/python3.6/site-packages/torch/tensor.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">57</span>, <span class="pl-s1">in</span> <span class="pl-s1">__repr__</span>
    <span class="pl-k">return</span> <span class="pl-s1">torch</span>.<span class="pl-s1">_tensor_str</span>.<span class="pl-en">_str</span>(<span class="pl-s1">self</span>)
  <span class="pl-v">File</span> <span class="pl-s">"anaconda3/lib/python3.6/site-packages/torch/_tensor_str.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">218</span>, <span class="pl-s1">in</span> <span class="pl-s1">_str</span>
    <span class="pl-s1">fmt</span>, <span class="pl-s1">scale</span>, <span class="pl-s1">sz</span> <span class="pl-c1">=</span> <span class="pl-en">_number_format</span>(<span class="pl-s1">self</span>)
  <span class="pl-v">File</span> <span class="pl-s">"anaconda3/lib/python3.6/site-packages/torch/_tensor_str.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">96</span>, <span class="pl-s1">in</span> <span class="pl-s1">_number_format</span>
    <span class="pl-k">if</span> <span class="pl-s1">value</span> <span class="pl-c1">!=</span> <span class="pl-s1">math</span>.<span class="pl-en">ceil</span>(<span class="pl-s1">value</span>.<span class="pl-en">item</span>()):
<span class="pl-v">RuntimeError</span>: <span class="pl-v">Overflow</span> <span class="pl-s1">when</span> <span class="pl-s1">unpacking</span> <span class="pl-s1">long</span>
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">torch</span>.<span class="pl-en">min</span>(<span class="pl-s1">a</span>)
<span class="pl-v">Traceback</span> (<span class="pl-s1">most</span> <span class="pl-s1">recent</span> <span class="pl-s1">call</span> <span class="pl-s1">last</span>):
  <span class="pl-v">File</span> <span class="pl-s">"&lt;stdin&gt;"</span>, <span class="pl-s1">line</span> <span class="pl-c1">1</span>, <span class="pl-s1">in</span> <span class="pl-c1">&lt;</span><span class="pl-s1">module</span><span class="pl-c1">&gt;</span>
  <span class="pl-v">File</span> <span class="pl-s">"anaconda3/lib/python3.6/site-packages/torch/tensor.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">57</span>, <span class="pl-s1">in</span> <span class="pl-s1">__repr__</span>
    <span class="pl-k">return</span> <span class="pl-s1">torch</span>.<span class="pl-s1">_tensor_str</span>.<span class="pl-en">_str</span>(<span class="pl-s1">self</span>)
  <span class="pl-v">File</span> <span class="pl-s">"anaconda3/lib/python3.6/site-packages/torch/_tensor_str.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">218</span>, <span class="pl-s1">in</span> <span class="pl-s1">_str</span>
    <span class="pl-s1">fmt</span>, <span class="pl-s1">scale</span>, <span class="pl-s1">sz</span> <span class="pl-c1">=</span> <span class="pl-en">_number_format</span>(<span class="pl-s1">self</span>)
  <span class="pl-v">File</span> <span class="pl-s">"anaconda3/lib/python3.6/site-packages/torch/_tensor_str.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">96</span>, <span class="pl-s1">in</span> <span class="pl-s1">_number_format</span>
    <span class="pl-k">if</span> <span class="pl-s1">value</span> <span class="pl-c1">!=</span> <span class="pl-s1">math</span>.<span class="pl-en">ceil</span>(<span class="pl-s1">value</span>.<span class="pl-en">item</span>()):
<span class="pl-v">RuntimeError</span>: <span class="pl-v">Overflow</span> <span class="pl-s1">when</span> <span class="pl-s1">unpacking</span> <span class="pl-s1">long</span></pre></div>
<h2 dir="auto">System Info</h2>
<ul dir="auto">
<li>PyTorch or Caffe2: PyTorch</li>
<li>How you installed PyTorch (conda, pip, source): conda</li>
<li>Build command you used (if compiling from source):</li>
<li>OS: Ubuntu 16.04</li>
<li>PyTorch version: 0.4.0</li>
<li>Python version: 3.6.3</li>
<li>CUDA/cuDNN version: 8 / 5</li>
<li>GPU models and configuration: GTX 1050 Ti</li>
<li>GCC version (if compiling from source):</li>
<li>CMake version:</li>
<li>Versions of any other relevant libraries:</li>
</ul>
<p dir="auto">On master:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="&gt;&gt;&gt; x.max()
Traceback (most recent call last):
  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;
  File &quot;/home/ssnl/sftp/pytorch/torch/tensor.py&quot;, line 53, in __repr__
    return torch._tensor_str._str(self)
  File &quot;/home/ssnl/sftp/pytorch/torch/_tensor_str.py&quot;, line 302, in _str
    strt = _vector_str(self.unsqueeze(0))
  File &quot;/home/ssnl/sftp/pytorch/torch/_tensor_str.py&quot;, line 270, in _vector_str
    fmt, scale, sz = _number_format(self)
  File &quot;/home/ssnl/sftp/pytorch/torch/_tensor_str.py&quot;, line 95, in _number_format
    if value != math.ceil(value.item()):
RuntimeError: Overflow when unpacking long
&gt;&gt;&gt; x
Traceback (most recent call last):
  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;
  File &quot;/home/ssnl/sftp/pytorch/torch/tensor.py&quot;, line 53, in __repr__
    return torch._tensor_str._str(self)
  File &quot;/home/ssnl/sftp/pytorch/torch/_tensor_str.py&quot;, line 306, in _str
    strt = _matrix_str(self)
  File &quot;/home/ssnl/sftp/pytorch/torch/_tensor_str.py&quot;, line 215, in _matrix_str
    min_sz=5 if not print_full_mat else 0)
  File &quot;/home/ssnl/sftp/pytorch/torch/_tensor_str.py&quot;, line 95, in _number_format
    if value != math.ceil(value.item()):
RuntimeError: Overflow when unpacking long
&gt;&gt;&gt; x.max().item()
1.8446744073709552e+19
&gt;&gt;&gt; x.dtype
torch.float32"><pre class="notranslate"><span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">x</span>.<span class="pl-en">max</span>()
<span class="pl-v">Traceback</span> (<span class="pl-s1">most</span> <span class="pl-s1">recent</span> <span class="pl-s1">call</span> <span class="pl-s1">last</span>):
  <span class="pl-v">File</span> <span class="pl-s">"&lt;stdin&gt;"</span>, <span class="pl-s1">line</span> <span class="pl-c1">1</span>, <span class="pl-s1">in</span> <span class="pl-c1">&lt;</span><span class="pl-s1">module</span><span class="pl-c1">&gt;</span>
  <span class="pl-v">File</span> <span class="pl-s">"/home/ssnl/sftp/pytorch/torch/tensor.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">53</span>, <span class="pl-s1">in</span> <span class="pl-s1">__repr__</span>
    <span class="pl-k">return</span> <span class="pl-s1">torch</span>.<span class="pl-s1">_tensor_str</span>.<span class="pl-en">_str</span>(<span class="pl-s1">self</span>)
  <span class="pl-v">File</span> <span class="pl-s">"/home/ssnl/sftp/pytorch/torch/_tensor_str.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">302</span>, <span class="pl-s1">in</span> <span class="pl-s1">_str</span>
    <span class="pl-s1">strt</span> <span class="pl-c1">=</span> <span class="pl-en">_vector_str</span>(<span class="pl-s1">self</span>.<span class="pl-en">unsqueeze</span>(<span class="pl-c1">0</span>))
  <span class="pl-v">File</span> <span class="pl-s">"/home/ssnl/sftp/pytorch/torch/_tensor_str.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">270</span>, <span class="pl-s1">in</span> <span class="pl-s1">_vector_str</span>
    <span class="pl-s1">fmt</span>, <span class="pl-s1">scale</span>, <span class="pl-s1">sz</span> <span class="pl-c1">=</span> <span class="pl-en">_number_format</span>(<span class="pl-s1">self</span>)
  <span class="pl-v">File</span> <span class="pl-s">"/home/ssnl/sftp/pytorch/torch/_tensor_str.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">95</span>, <span class="pl-s1">in</span> <span class="pl-s1">_number_format</span>
    <span class="pl-k">if</span> <span class="pl-s1">value</span> <span class="pl-c1">!=</span> <span class="pl-s1">math</span>.<span class="pl-en">ceil</span>(<span class="pl-s1">value</span>.<span class="pl-en">item</span>()):
<span class="pl-v">RuntimeError</span>: <span class="pl-v">Overflow</span> <span class="pl-s1">when</span> <span class="pl-s1">unpacking</span> <span class="pl-s1">long</span>
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">x</span>
<span class="pl-v">Traceback</span> (<span class="pl-s1">most</span> <span class="pl-s1">recent</span> <span class="pl-s1">call</span> <span class="pl-s1">last</span>):
  <span class="pl-v">File</span> <span class="pl-s">"&lt;stdin&gt;"</span>, <span class="pl-s1">line</span> <span class="pl-c1">1</span>, <span class="pl-s1">in</span> <span class="pl-c1">&lt;</span><span class="pl-s1">module</span><span class="pl-c1">&gt;</span>
  <span class="pl-v">File</span> <span class="pl-s">"/home/ssnl/sftp/pytorch/torch/tensor.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">53</span>, <span class="pl-s1">in</span> <span class="pl-s1">__repr__</span>
    <span class="pl-k">return</span> <span class="pl-s1">torch</span>.<span class="pl-s1">_tensor_str</span>.<span class="pl-en">_str</span>(<span class="pl-s1">self</span>)
  <span class="pl-v">File</span> <span class="pl-s">"/home/ssnl/sftp/pytorch/torch/_tensor_str.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">306</span>, <span class="pl-s1">in</span> <span class="pl-s1">_str</span>
    <span class="pl-s1">strt</span> <span class="pl-c1">=</span> <span class="pl-en">_matrix_str</span>(<span class="pl-s1">self</span>)
  <span class="pl-v">File</span> <span class="pl-s">"/home/ssnl/sftp/pytorch/torch/_tensor_str.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">215</span>, <span class="pl-s1">in</span> <span class="pl-s1">_matrix_str</span>
    <span class="pl-s1">min_sz</span><span class="pl-c1">=</span><span class="pl-c1">5</span> <span class="pl-k">if</span> <span class="pl-c1">not</span> <span class="pl-s1">print_full_mat</span> <span class="pl-k">else</span> <span class="pl-c1">0</span>)
  <span class="pl-v">File</span> <span class="pl-s">"/home/ssnl/sftp/pytorch/torch/_tensor_str.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">95</span>, <span class="pl-s1">in</span> <span class="pl-s1">_number_format</span>
    <span class="pl-k">if</span> <span class="pl-s1">value</span> <span class="pl-c1">!=</span> <span class="pl-s1">math</span>.<span class="pl-en">ceil</span>(<span class="pl-s1">value</span>.<span class="pl-en">item</span>()):
<span class="pl-v">RuntimeError</span>: <span class="pl-v">Overflow</span> <span class="pl-s1">when</span> <span class="pl-s1">unpacking</span> <span class="pl-s1">long</span>
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">x</span>.<span class="pl-en">max</span>().<span class="pl-en">item</span>()
<span class="pl-c1">1.8446744073709552e+19</span>
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">x</span>.<span class="pl-s1">dtype</span>
<span class="pl-s1">torch</span>.<span class="pl-s1">float32</span></pre></div>
<p dir="auto">This is a regression as printing this tensor works in 0.3.1. Assigning to <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/li-roy/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/li-roy">@li-roy</a> as you are taking care of the printing issue. Reported in <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="310102253" data-permission-text="Title is private" data-url="https://github.com/pytorch/pytorch/issues/6136" data-hovercard-type="pull_request" data-hovercard-url="/pytorch/pytorch/pull/6136/hovercard" href="https://github.com/pytorch/pytorch/pull/6136">#6136</a></p>