Repository: pytorch
Issue: [Torchscript] torch.min returns wrong gradient when inputs are equal
Labels: oncall: jit

### 🐛 Describe the bug

The same issue applies to torch.max()

Steps To Reproduce: 
```py
import torch

# input
x = torch.ones([10]).requires_grad_()
y = torch.ones([10]).requires_grad_()
grad_output = torch.ones_like(x)

def minimum(x, y):
    return torch.minimum(x, y) * x

def min(x, y):
    return torch.min(x, y) * x

def test(func, func_script):
    # we need a few iterations to trigger the fused kernel
    for i in range(5):
        # forward
        result = func(x, y)
        result_script = func_script(x, y)
        # derivative
        (result_grad,) = torch.autograd.grad(result, x, grad_output, create_graph=True)
        (result_script_grad,) = torch.autograd.grad(result_script, x, grad_output, create_graph=True)
        # check result
        assert torch.allclose(result, result_script), f"results do not match:\n a: {result}\n b: {result_script}"
        assert torch.allclose(result_grad, result_script_grad), f"grads do not match:\n a: {result_grad}\n b: {result_script_grad}"

minimum_script = torch.jit.script(minimum)
min_script = torch.jit.script(min)

test(minimum, minimum_script)
print("minimum pass")
test(min, min_script)
print("min pass")
```

output: 
```
output:
minimum pass
Traceback (most recent call last):
  File "torch_min_torchscript_grad_bug.py", line 36, in <module>
    test(min, min_script)
  File "torch_min_torchscript_grad_bug.py", line 28, in test
    assert torch.allclose(result_grad, result_script_grad), f"grads do not match:\n a: {result_grad}\n b: {result_script_grad}"
AssertionError: grads do not match:
 a: tensor([1.5000, 1.5000, 1.5000, 1.5000, 1.5000, 1.5000, 1.5000, 1.5000, 1.5000,
        1.5000], grad_fn=<AddBackward0>)
 b: tensor([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.], grad_fn=<AddBackward0>)
```

### Versions

```
python collect_env.py
Collecting environment information...
PyTorch version: 1.13.0a0+340c412
Is debug build: False
CUDA used to build PyTorch: 11.7
ROCM used to build PyTorch: N/A

OS: Ubuntu 20.04.4 LTS (x86_64)
GCC version: (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0
Clang version: Could not collect
CMake version: version 3.23.2
Libc version: glibc-2.31

Python version: 3.8.13 | packaged by conda-forge | (default, Mar 25 2022, 06:04:10)  [GCC 10.3.0] (64-bit runtime)
Python platform: Linux-5.4.0-91-generic-x86_64-with-glibc2.10
Is CUDA available: True
CUDA runtime version: 11.7.99
GPU models and configuration: GPU 0: NVIDIA A100 80GB PCIe
Nvidia driver version: 510.73.08
cuDNN version: Probably one of the following:
/usr/lib/x86_64-linux-gnu/libcudnn.so.8.4.1
/usr/lib/x86_64-linux-gnu/libcudnn_adv_infer.so.8.4.1
/usr/lib/x86_64-linux-gnu/libcudnn_adv_train.so.8.4.1
/usr/lib/x86_64-linux-gnu/libcudnn_cnn_infer.so.8.4.1
/usr/lib/x86_64-linux-gnu/libcudnn_cnn_train.so.8.4.1
/usr/lib/x86_64-linux-gnu/libcudnn_ops_infer.so.8.4.1
/usr/lib/x86_64-linux-gnu/libcudnn_ops_train.so.8.4.1
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

Versions of relevant libraries:
[pip3] numpy==1.22.4
[pip3] pytorch-quantization==2.1.2
[pip3] torch==1.13.0a0+340c412
[pip3] torch-tensorrt==1.1.0a0
[pip3] torchtext==0.13.0a0
[pip3] torchvision==0.13.0a0
[conda] mkl                       2020.4             h726a3e6_304    conda-forge
[conda] mkl-include               2020.4             h726a3e6_304    conda-forge
[conda] numpy                     1.22.4           py38h99721a1_0    conda-forge
[conda] pytorch-quantization      2.1.2                    pypi_0    pypi
[conda] torch                     1.13.0a0+340c412          pypi_0    pypi
[conda] torch-tensorrt            1.1.0a0                  pypi_0    pypi
[conda] torchtext                 0.13.0a0                 pypi_0    pypi
[conda] torchvision               0.13.0a0                 pypi_0    pypi
```