<h2 dir="auto">🐛 Order dependent min and max</h2>
<p dir="auto">The functions torch.min and torch.max depend on the order of their arguments when one of the arguments is nan. This is unexpected behaviour.</p>
<h2 dir="auto">To Reproduce</h2>
<p dir="auto">Here is a minimal working example</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import torch
import numpy as np
a, b = torch.tensor(0.), torch.tensor(np.nan)
print('min(0,nan): ', torch.min(a, b) )
print('min(nan, 0): ', torch.min(b, a))"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">torch</span>
<span class="pl-k">import</span> <span class="pl-s1">numpy</span> <span class="pl-k">as</span> <span class="pl-s1">np</span>
<span class="pl-s1">a</span>, <span class="pl-s1">b</span> <span class="pl-c1">=</span> <span class="pl-s1">torch</span>.<span class="pl-en">tensor</span>(<span class="pl-c1">0.</span>), <span class="pl-s1">torch</span>.<span class="pl-en">tensor</span>(<span class="pl-s1">np</span>.<span class="pl-s1">nan</span>)
<span class="pl-en">print</span>(<span class="pl-s">'min(0,nan): '</span>, <span class="pl-s1">torch</span>.<span class="pl-en">min</span>(<span class="pl-s1">a</span>, <span class="pl-s1">b</span>) )
<span class="pl-en">print</span>(<span class="pl-s">'min(nan, 0): '</span>, <span class="pl-s1">torch</span>.<span class="pl-en">min</span>(<span class="pl-s1">b</span>, <span class="pl-s1">a</span>))</pre></div>
<h2 dir="auto">Expected behavior</h2>
<p dir="auto">These function should not depend on order.</p>
<h2 dir="auto">Environment</h2>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Collecting environment information...
PyTorch version: 1.2.0
Is debug build: No
CUDA used to build PyTorch: 10.0.130

OS: Ubuntu 18.04.3 LTS
GCC version: (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0
CMake version: version 3.10.2

Python version: 3.6
Is CUDA available: Yes
CUDA runtime version: Could not collect
GPU models and configuration: 
GPU 0: GeForce RTX 2080 Ti
GPU 1: GeForce RTX 2080 Ti
GPU 2: GeForce RTX 2080 Ti
GPU 3: GeForce RTX 2080 Ti

Nvidia driver version: 440.33.01
cuDNN version: /usr/lib/x86_64-linux-gnu/libcudnn.so.7.6.5

Versions of relevant libraries:
[pip3] numpy==1.14.3
[pip3] numpydoc==0.8.0
[pip3] torch==1.2.0
[pip3] torchfile==0.1.0
[pip3] torchnet==0.0.4
[pip3] torchvision==0.4.0a0+6b959ee
[conda] blas                      1.0                         mkl  
[conda] mkl                       2018.0.2                      1  
[conda] mkl-service               1.1.2            py36h17a0993_4  
[conda] mkl_fft                   1.0.1            py36h3010b51_0  
[conda] mkl_random                1.0.1            py36h629b387_0  
[conda] pytorch                   1.2.0           py3.6_cuda10.0.130_cudnn7.6.2_0    pytorch
[conda] torchfile                 0.1.0                    pypi_0    pypi
[conda] torchnet                  0.0.4                    pypi_0    pypi
[conda] torchvision               0.4.0                py36_cu100    pytorch"><pre class="notranslate"><code class="notranslate">Collecting environment information...
PyTorch version: 1.2.0
Is debug build: No
CUDA used to build PyTorch: 10.0.130

OS: Ubuntu 18.04.3 LTS
GCC version: (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0
CMake version: version 3.10.2

Python version: 3.6
Is CUDA available: Yes
CUDA runtime version: Could not collect
GPU models and configuration: 
GPU 0: GeForce RTX 2080 Ti
GPU 1: GeForce RTX 2080 Ti
GPU 2: GeForce RTX 2080 Ti
GPU 3: GeForce RTX 2080 Ti

Nvidia driver version: 440.33.01
cuDNN version: /usr/lib/x86_64-linux-gnu/libcudnn.so.7.6.5

Versions of relevant libraries:
[pip3] numpy==1.14.3
[pip3] numpydoc==0.8.0
[pip3] torch==1.2.0
[pip3] torchfile==0.1.0
[pip3] torchnet==0.0.4
[pip3] torchvision==0.4.0a0+6b959ee
[conda] blas                      1.0                         mkl  
[conda] mkl                       2018.0.2                      1  
[conda] mkl-service               1.1.2            py36h17a0993_4  
[conda] mkl_fft                   1.0.1            py36h3010b51_0  
[conda] mkl_random                1.0.1            py36h629b387_0  
[conda] pytorch                   1.2.0           py3.6_cuda10.0.130_cudnn7.6.2_0    pytorch
[conda] torchfile                 0.1.0                    pypi_0    pypi
[conda] torchnet                  0.0.4                    pypi_0    pypi
[conda] torchvision               0.4.0                py36_cu100    pytorch
</code></pre></div>
<h2 dir="auto">🐛 Bug</h2>
<p dir="auto">When using <code class="notranslate">torch.max()</code> between two tensors where one tensor contains nans, the result varies depending upon the order of input into <code class="notranslate">torch.max()</code>.</p>
<h2 dir="auto">To Reproduce</h2>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="import torch
x = torch.ones(3)
y = torch.ones(3) * torch.tensor(float('nan'))

print(f'torch.max(x, y) : {torch.max(x, y)}')
print(f'torch.max(y, x) : {torch.max(y, x)}')"><pre class="notranslate"><code class="notranslate">import torch
x = torch.ones(3)
y = torch.ones(3) * torch.tensor(float('nan'))

print(f'torch.max(x, y) : {torch.max(x, y)}')
print(f'torch.max(y, x) : {torch.max(y, x)}')
</code></pre></div>
<p dir="auto">Returns:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="torch.max(x, y) : tensor([nan, nan, nan])
torch.max(y, x) : tensor([1., 1., 1.])"><pre class="notranslate"><code class="notranslate">torch.max(x, y) : tensor([nan, nan, nan])
torch.max(y, x) : tensor([1., 1., 1.])
</code></pre></div>

<h2 dir="auto">Expected behavior</h2>
<p dir="auto">I'd expect the output to be consistent in both cases.<br>
i.e. <code class="notranslate">torch.max(x, y) = torch.max(y, x)</code> in all cases.</p>
<p dir="auto">My personal preference would be as in the above case of <code class="notranslate">torch.max(y,x) = [1,1,1]</code>, but the most important part is that it should be consistent.</p>