Repository: pytorch
Issue: `amax` fails on `masked_tensor` with multiple negative dims
Labels: triaged,module: masked operators

### 🐛 Describe the bug

`amax` fails on `masked_tensor` with multiple negative dims, error message: "data.size() must equal mask.size()".

Minimal reproducable code:
```python
import torch

data = torch.ones((10, 20, 30))
mask = torch.ones((10, 20, 30), dtype=torch.bool)
x = torch.masked.masked_tensor(data, mask)
print(x.amax(dim=(-2, -1)))
```

Error messages:
```
/home/ubuntu/miniconda3/envs/pytorch/lib/python3.11/site-packages/torch/masked/maskedtensor/core.py:156: UserWarning: The PyTorch API of MaskedTensors is in prototype stage and will change in the near future. Please open a Github issue for features requests and see our documentation on the torch.masked module for further information about the project.
  warnings.warn(("The PyTorch API of MaskedTensors is in prototype stage "
Traceback (most recent call last):
  File "/home/ubuntu/shichen/test/test.py", line 6, in <module>
    print(x.amax(dim=(-2, -1)))
          ^^^^^^^^^^^^^^^^^^^^
  File "/home/ubuntu/miniconda3/envs/pytorch/lib/python3.11/site-packages/torch/masked/maskedtensor/core.py", line 269, in __torch_function__
    return _MASKEDTENSOR_FUNCTION_TABLE[func](*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/ubuntu/miniconda3/envs/pytorch/lib/python3.11/site-packages/torch/masked/maskedtensor/_ops_refs.py", line 184, in _general_function_reductions
    return _apply_reduction(func, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/ubuntu/miniconda3/envs/pytorch/lib/python3.11/site-packages/torch/masked/maskedtensor/reductions.py", line 172, in _apply_reduction
    return TENSOR_REDUCE_MAP[fn](*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/ubuntu/miniconda3/envs/pytorch/lib/python3.11/site-packages/torch/masked/maskedtensor/reductions.py", line 129, in grad_reduce
    return _torch_reduce_dim(fn)(input, dim, keepdim, dtype)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/ubuntu/miniconda3/envs/pytorch/lib/python3.11/site-packages/torch/masked/maskedtensor/reductions.py", line 105, in reduce_dim
    return as_masked_tensor(result_data, _multidim_any(mask, dim, keepdim))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/ubuntu/miniconda3/envs/pytorch/lib/python3.11/site-packages/torch/masked/maskedtensor/creation.py", line 21, in as_masked_tensor
    return MaskedTensor._from_values(data, mask)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/ubuntu/miniconda3/envs/pytorch/lib/python3.11/site-packages/torch/masked/maskedtensor/core.py", line 232, in _from_values
    result = Constructor.apply(data, mask)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/ubuntu/miniconda3/envs/pytorch/lib/python3.11/site-packages/torch/autograd/function.py", line 539, in apply
    return super().apply(*args, **kwargs)  # type: ignore[misc]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/ubuntu/miniconda3/envs/pytorch/lib/python3.11/site-packages/torch/masked/maskedtensor/core.py", line 226, in forward
    return MaskedTensor(data, mask)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/ubuntu/miniconda3/envs/pytorch/lib/python3.11/site-packages/torch/masked/maskedtensor/core.py", line 218, in __init__
    self._validate_members()
  File "/home/ubuntu/miniconda3/envs/pytorch/lib/python3.11/site-packages/torch/masked/maskedtensor/core.py", line 214, in _validate_members
    raise ValueError("data.size() must equal mask.size()")
ValueError: data.size() must equal mask.size()
```

### Versions

```
Collecting environment information...
PyTorch version: 2.1.1
Is debug build: False
CUDA used to build PyTorch: 11.8
ROCM used to build PyTorch: N/A

OS: Ubuntu 20.04.5 LTS (x86_64)
GCC version: Could not collect
Clang version: Could not collect
CMake version: Could not collect
Libc version: glibc-2.31

Python version: 3.11.5 (main, Sep 11 2023, 13:54:46) [GCC 11.2.0] (64-bit runtime)
Python platform: Linux-5.4.0-126-generic-x86_64-with-glibc2.31
Is CUDA available: True
CUDA runtime version: Could not collect
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration:
GPU 0: NVIDIA GeForce RTX 3080
GPU 1: NVIDIA GeForce RTX 3080

Nvidia driver version: 515.65.01
cuDNN version: Could not collect
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

CPU:
Architecture:                    x86_64
CPU op-mode(s):                  32-bit, 64-bit
Byte Order:                      Little Endian
Address sizes:                   46 bits physical, 48 bits virtual
CPU(s):                          48
On-line CPU(s) list:             0-47
Thread(s) per core:              2
Core(s) per socket:              12
Socket(s):                       2
NUMA node(s):                    2
Vendor ID:                       GenuineIntel
CPU family:                      6
Model:                           63
Model name:                      Intel(R) Xeon(R) CPU E5-2678 v3 @ 2.50GHz
Stepping:                        2
CPU MHz:                         1198.338
CPU max MHz:                     3300.0000
CPU min MHz:                     1200.0000
BogoMIPS:                        4988.95
Virtualization:                  VT-x
L1d cache:                       768 KiB
L1i cache:                       768 KiB
L2 cache:                        6 MiB
L3 cache:                        60 MiB
NUMA node0 CPU(s):               0-11,24-35
NUMA node1 CPU(s):               12-23,36-47
Vulnerability Itlb multihit:     KVM: Mitigation: Split huge pages
Vulnerability L1tf:              Mitigation; PTE Inversion; VMX conditional cache flushes, SMT vulnerable
Vulnerability Mds:               Mitigation; Clear CPU buffers; SMT vulnerable
Vulnerability Meltdown:          Mitigation; PTI
Vulnerability Mmio stale data:   Mitigation; Clear CPU buffers; SMT vulnerable
Vulnerability Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl and seccomp
Vulnerability Spectre v1:        Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2:        Mitigation; Retpolines, IBPB conditional, IBRS_FW, STIBP conditional, RSB filling
Vulnerability Srbds:             Not affected
Vulnerability Tsx async abort:   Not affected
Flags:                           fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm cpuid_fault epb invpcid_single pti intel_ppin ssbd ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid cqm xsaveopt cqm_llc cqm_occup_llc dtherm ida arat pln pts md_clear flush_l1d

Versions of relevant libraries:
[pip3] numpy==1.26.2
[pip3] torch==2.1.1
[pip3] torchaudio==2.1.1
[pip3] torchvision==0.16.1
[pip3] triton==2.1.0
[conda] blas                      1.0                         mkl    defaults
[conda] ffmpeg                    4.3                  hf484d3e_0    pytorch
[conda] libjpeg-turbo             2.0.0                h9bf148f_0    pytorch
[conda] mkl                       2023.1.0         h213fc3f_46344    defaults
[conda] mkl-service               2.4.0           py311h5eee18b_1    defaults
[conda] mkl_fft                   1.3.8           py311h5eee18b_0    defaults
[conda] mkl_random                1.2.4           py311hdb19cb5_0    defaults
[conda] numpy                     1.26.2          py311h08b1b3b_0    defaults
[conda] numpy-base                1.26.2          py311hf175353_0    defaults
[conda] pytorch                   2.1.1           py3.11_cuda11.8_cudnn8.7.0_0    pytorch
[conda] pytorch-cuda              11.8                 h7e8668a_5    pytorch
[conda] pytorch-mutex             1.0                        cuda    pytorch
[conda] torchaudio                2.1.1               py311_cu118    pytorch
[conda] torchtriton               2.1.0                     py311    pytorch
[conda] torchvision               0.16.1              py311_cu118    pytorch
```