<h2 dir="auto">Environment info</h2>
<ul dir="auto">
<li>OS: Linux 4.13.0-31-generic <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="177291339" data-permission-text="Title is private" data-url="https://github.com/pytorch/pytorch/issues/34" data-hovercard-type="pull_request" data-hovercard-url="/pytorch/pytorch/pull/34/hovercard" href="https://github.com/pytorch/pytorch/pull/34">#34</a>~16.04.1-Ubuntu</li>
<li>PyTorch version: 0.3</li>
<li>How you installed PyTorch (conda, pip, source): pip</li>
<li>Python version: 3.5</li>
<li>CUDA/cuDNN version: 8/7</li>
<li>GPU models and configuration: 1080Ti</li>
</ul>
<h2 dir="auto">Issue</h2>
<p dir="auto">Snippet:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="print(&quot;============================================================&quot;)
if DEBUG_LOG1 : print(&quot;type(decoder_output) = {}&quot;.format(type(decoder_output)))
if DEBUG_LOG1 : print(&quot;decoder_output.size() = {}&quot;.format(decoder_output.size()))
print(&quot;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++&quot;)
if DEBUG_LOG1 : print(&quot;decoder_output.data.topk(1) = &quot;.format(decoder_output.data.topk(1)))
if DEBUG_LOG1 : print(&quot;decoder_output.data.topk(1) = &quot;.format(decoder_output.data.topk(1)))
if DEBUG_LOG1 : print(&quot;decoder_output.data.topk(1) = &quot;.format(decoder_output.data.topk(1)))
if DEBUG_LOG1 : print(&quot;decoder_output.data.topk(1) = &quot;.format(decoder_output.data.topk(1)))
print(&quot;////////////////////////////////////////////////////////////&quot;)
topv, topi = decoder_output.data.topk(1)
if DEBUG_LOG1 : print(&quot;topi,topv = {}{}&quot;.format(topi,topv))
topv, topi = decoder_output.data.topk(1)
if DEBUG_LOG1 : print(&quot;topi,topv = {}{}&quot;.format(topi,topv))
topv, topi = decoder_output.data.topk(1)
if DEBUG_LOG1 : print(&quot;topi,topv = {}{}&quot;.format(topi,topv))
topv, topi = decoder_output.data.topk(1)
if DEBUG_LOG1 : print(&quot;topi,topv = {}{}&quot;.format(topi,topv))
print(&quot;------------------------------------------------------------&quot;)"><pre class="notranslate"><code class="notranslate">print("============================================================")
if DEBUG_LOG1 : print("type(decoder_output) = {}".format(type(decoder_output)))
if DEBUG_LOG1 : print("decoder_output.size() = {}".format(decoder_output.size()))
print("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
if DEBUG_LOG1 : print("decoder_output.data.topk(1) = ".format(decoder_output.data.topk(1)))
if DEBUG_LOG1 : print("decoder_output.data.topk(1) = ".format(decoder_output.data.topk(1)))
if DEBUG_LOG1 : print("decoder_output.data.topk(1) = ".format(decoder_output.data.topk(1)))
if DEBUG_LOG1 : print("decoder_output.data.topk(1) = ".format(decoder_output.data.topk(1)))
print("////////////////////////////////////////////////////////////")
topv, topi = decoder_output.data.topk(1)
if DEBUG_LOG1 : print("topi,topv = {}{}".format(topi,topv))
topv, topi = decoder_output.data.topk(1)
if DEBUG_LOG1 : print("topi,topv = {}{}".format(topi,topv))
topv, topi = decoder_output.data.topk(1)
if DEBUG_LOG1 : print("topi,topv = {}{}".format(topi,topv))
topv, topi = decoder_output.data.topk(1)
if DEBUG_LOG1 : print("topi,topv = {}{}".format(topi,topv))
print("------------------------------------------------------------")
</code></pre></div>
<p dir="auto">Out:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="============================================================
type(decoder_output) = &lt;class 'torch.autograd.variable.Variable'&gt;
decoder_output.size() = torch.Size([1, 29419])
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
decoder_output.data.topk(1) = (
nan
[torch.cuda.FloatTensor of size 1x1 (GPU 0)]
, 
 0
[torch.cuda.LongTensor of size 1x1 (GPU 0)]
)
decoder_output.data.topk(1) = (
nan
[torch.cuda.FloatTensor of size 1x1 (GPU 0)]
, 
 0
[torch.cuda.LongTensor of size 1x1 (GPU 0)]
)
decoder_output.data.topk(1) = (
nan
[torch.cuda.FloatTensor of size 1x1 (GPU 0)]
, 
 0
[torch.cuda.LongTensor of size 1x1 (GPU 0)]
)
decoder_output.data.topk(1) = (
nan
[torch.cuda.FloatTensor of size 1x1 (GPU 0)]
, 
 0
[torch.cuda.LongTensor of size 1x1 (GPU 0)]
)

////////////////////////////////////////////////////////////
topi,topv = 
 0
[torch.cuda.LongTensor of size 1x1 (GPU 0)]

nan
[torch.cuda.FloatTensor of size 1x1 (GPU 0)]

topi,topv = 
 9.2234e+18
[torch.cuda.LongTensor of size 1x1 (GPU 0)]

nan
[torch.cuda.FloatTensor of size 1x1 (GPU 0)]

topi,topv = 
 9.2234e+18
[torch.cuda.LongTensor of size 1x1 (GPU 0)]

nan
[torch.cuda.FloatTensor of size 1x1 (GPU 0)]

topi,topv = 
 2.1475e+09
[torch.cuda.LongTensor of size 1x1 (GPU 0)]

nan
[torch.cuda.FloatTensor of size 1x1 (GPU 0)]

------------------------------------------------------------
"><pre class="notranslate"><code class="notranslate">============================================================
type(decoder_output) = &lt;class 'torch.autograd.variable.Variable'&gt;
decoder_output.size() = torch.Size([1, 29419])
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
decoder_output.data.topk(1) = (
nan
[torch.cuda.FloatTensor of size 1x1 (GPU 0)]
, 
 0
[torch.cuda.LongTensor of size 1x1 (GPU 0)]
)
decoder_output.data.topk(1) = (
nan
[torch.cuda.FloatTensor of size 1x1 (GPU 0)]
, 
 0
[torch.cuda.LongTensor of size 1x1 (GPU 0)]
)
decoder_output.data.topk(1) = (
nan
[torch.cuda.FloatTensor of size 1x1 (GPU 0)]
, 
 0
[torch.cuda.LongTensor of size 1x1 (GPU 0)]
)
decoder_output.data.topk(1) = (
nan
[torch.cuda.FloatTensor of size 1x1 (GPU 0)]
, 
 0
[torch.cuda.LongTensor of size 1x1 (GPU 0)]
)

////////////////////////////////////////////////////////////
topi,topv = 
 0
[torch.cuda.LongTensor of size 1x1 (GPU 0)]

nan
[torch.cuda.FloatTensor of size 1x1 (GPU 0)]

topi,topv = 
 9.2234e+18
[torch.cuda.LongTensor of size 1x1 (GPU 0)]

nan
[torch.cuda.FloatTensor of size 1x1 (GPU 0)]

topi,topv = 
 9.2234e+18
[torch.cuda.LongTensor of size 1x1 (GPU 0)]

nan
[torch.cuda.FloatTensor of size 1x1 (GPU 0)]

topi,topv = 
 2.1475e+09
[torch.cuda.LongTensor of size 1x1 (GPU 0)]

nan
[torch.cuda.FloatTensor of size 1x1 (GPU 0)]

------------------------------------------------------------

</code></pre></div>
<p dir="auto">Should I report this to torch repository?</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="&gt;&gt;&gt; torch.topk(torch.FloatTensor([-1,float(&quot;nan&quot;),2, 1]), 1)
(
  2
 [torch.FloatTensor of size 1], 
  2
 [torch.LongTensor of size 1])

&gt;&gt;&gt; torch.topk(torch.cuda.FloatTensor([-1,float(&quot;nan&quot;),2]), 1)
(
 nan
 [torch.cuda.FloatTensor of size 1 (GPU 0)], 
  9.2054e+18
 [torch.cuda.LongTensor of size 1 (GPU 0)])"><pre class="notranslate"><span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">torch</span>.<span class="pl-en">topk</span>(<span class="pl-s1">torch</span>.<span class="pl-v">FloatTensor</span>([<span class="pl-c1">-</span><span class="pl-c1">1</span>,<span class="pl-en">float</span>(<span class="pl-s">"nan"</span>),<span class="pl-c1">2</span>, <span class="pl-c1">1</span>]), <span class="pl-c1">1</span>)
(
  <span class="pl-c1">2</span>
 [<span class="pl-s1">torch</span>.<span class="pl-v">FloatTensor</span> <span class="pl-s1">of</span> <span class="pl-s1">size</span> <span class="pl-c1">1</span>], 
  <span class="pl-c1">2</span>
 [<span class="pl-s1">torch</span>.<span class="pl-v">LongTensor</span> <span class="pl-s1">of</span> <span class="pl-s1">size</span> <span class="pl-c1">1</span>])

<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">torch</span>.<span class="pl-en">topk</span>(<span class="pl-s1">torch</span>.<span class="pl-s1">cuda</span>.<span class="pl-v">FloatTensor</span>([<span class="pl-c1">-</span><span class="pl-c1">1</span>,<span class="pl-en">float</span>(<span class="pl-s">"nan"</span>),<span class="pl-c1">2</span>]), <span class="pl-c1">1</span>)
(
 <span class="pl-s1">nan</span>
 [<span class="pl-s1">torch</span>.<span class="pl-s1">cuda</span>.<span class="pl-v">FloatTensor</span> <span class="pl-s1">of</span> <span class="pl-s1">size</span> <span class="pl-c1">1</span> (<span class="pl-v">GPU</span> <span class="pl-c1">0</span>)], 
  <span class="pl-c1">9.2054e+18</span>
 [<span class="pl-s1">torch</span>.<span class="pl-s1">cuda</span>.<span class="pl-v">LongTensor</span> <span class="pl-s1">of</span> <span class="pl-s1">size</span> <span class="pl-c1">1</span> (<span class="pl-v">GPU</span> <span class="pl-c1">0</span>)])</pre></div>
<p dir="auto">In CPU, <code class="notranslate">topk</code> seems to ignore "nan" and its indices from result.<br>
In GPU, <code class="notranslate">topk</code> with "nan" is kind of undefined behavior?</p>
<p dir="auto">and other examples</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="&gt;&gt;&gt; torch.max(torch.cuda.FloatTensor([float(&quot;nan&quot;), 1]))
1.0

&gt;&gt;&gt; torch.max(torch.FloatTensor([float(&quot;nan&quot;), 1]))
nan

&gt;&gt;&gt; torch.min(torch.cuda.FloatTensor([float(&quot;nan&quot;), 1]))
1.0

&gt;&gt;&gt; torch.min(torch.FloatTensor([float(&quot;nan&quot;), 1]))
nan"><pre class="notranslate"><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">torch</span>.<span class="pl-s1">cuda</span>.<span class="pl-v">FloatTensor</span>([<span class="pl-en">float</span>(<span class="pl-s">"nan"</span>), <span class="pl-c1">1</span>]))
<span class="pl-c1">1.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">torch</span>.<span class="pl-v">FloatTensor</span>([<span class="pl-en">float</span>(<span class="pl-s">"nan"</span>), <span class="pl-c1">1</span>]))
<span class="pl-s1">nan</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">torch</span>.<span class="pl-s1">cuda</span>.<span class="pl-v">FloatTensor</span>([<span class="pl-en">float</span>(<span class="pl-s">"nan"</span>), <span class="pl-c1">1</span>]))
<span class="pl-c1">1.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">torch</span>.<span class="pl-v">FloatTensor</span>([<span class="pl-en">float</span>(<span class="pl-s">"nan"</span>), <span class="pl-c1">1</span>]))
<span class="pl-s1">nan</span></pre></div>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="&gt;&gt;&gt; torch.sort(torch.FloatTensor([float(&quot;nan&quot;), 1]))
(
 nan
   1
 [torch.FloatTensor of size 2], 
  0
  1
 [torch.LongTensor of size 2])

&gt;&gt; torch.sort(torch.cuda.FloatTensor([float(&quot;nan&quot;), 1]))
(
   1
 nan
 [torch.cuda.FloatTensor of size 2 (GPU 0)], 
  1
  0
 [torch.cuda.LongTensor of size 2 (GPU 0)])"><pre class="notranslate"><span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">torch</span>.<span class="pl-en">sort</span>(<span class="pl-s1">torch</span>.<span class="pl-v">FloatTensor</span>([<span class="pl-en">float</span>(<span class="pl-s">"nan"</span>), <span class="pl-c1">1</span>]))
(
 <span class="pl-s1">nan</span>
   <span class="pl-c1">1</span>
 [<span class="pl-s1">torch</span>.<span class="pl-v">FloatTensor</span> <span class="pl-s1">of</span> <span class="pl-s1">size</span> <span class="pl-c1">2</span>], 
  <span class="pl-c1">0</span>
  <span class="pl-c1">1</span>
 [<span class="pl-s1">torch</span>.<span class="pl-v">LongTensor</span> <span class="pl-s1">of</span> <span class="pl-s1">size</span> <span class="pl-c1">2</span>])

<span class="pl-c1">&gt;&gt;</span> <span class="pl-s1">torch</span>.<span class="pl-en">sort</span>(<span class="pl-s1">torch</span>.<span class="pl-s1">cuda</span>.<span class="pl-v">FloatTensor</span>([<span class="pl-en">float</span>(<span class="pl-s">"nan"</span>), <span class="pl-c1">1</span>]))
(
   <span class="pl-c1">1</span>
 <span class="pl-s1">nan</span>
 [<span class="pl-s1">torch</span>.<span class="pl-s1">cuda</span>.<span class="pl-v">FloatTensor</span> <span class="pl-s1">of</span> <span class="pl-s1">size</span> <span class="pl-c1">2</span> (<span class="pl-v">GPU</span> <span class="pl-c1">0</span>)], 
  <span class="pl-c1">1</span>
  <span class="pl-c1">0</span>
 [<span class="pl-s1">torch</span>.<span class="pl-s1">cuda</span>.<span class="pl-v">LongTensor</span> <span class="pl-s1">of</span> <span class="pl-s1">size</span> <span class="pl-c1">2</span> (<span class="pl-v">GPU</span> <span class="pl-c1">0</span>)])</pre></div>
<p dir="auto">cc <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/VitalyFedyunin/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/VitalyFedyunin">@VitalyFedyunin</a> <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/ngimel/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/ngimel">@ngimel</a> <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/heitorschueroff/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/heitorschueroff">@heitorschueroff</a></p>