Repository: pytorch
Issue: [MPS] Indexing Returns 0 if OOB
Labels: module: error checking,triaged,module: mps

### 🐛 Describe the bug

Using PyTorch to train a model on MacOS worked fine, so I switched to using CUDA, where it would crash. The issue is that CUDA will crash if you index out of bounds, along with CPU, MPS will return a 0. This causes an inconsistency in models, and will result in undefined behavior on MPS. This was tested on PyTorch version `2.1.1+cu121` and the latest version on GitHub main at the time of the issue creation

The following code was tested on each platform

```python
import torch
import torch.nn as nn
```
```python
# CPU
embed = nn.Embedding(10, 2).to('cpu')
t = torch.tensor(10).to('cpu')
print(embed(t))

"""
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
Cell In[2], line 3
      1 embed = nn.Embedding(10, 2).to('cpu')
      2 t = torch.tensor(10).to('cpu')
----> 3 print(embed(t))

...

IndexError: index out of range in self
"""
```
```python
# CUDA
embed = nn.Embedding(10, 2).to('cuda')
t = torch.tensor(10).to('cuda')
print(embed(t))

"""
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
Cell In[2], line 3
      1 embed = nn.Embedding(10, 2).to('cuda')
      2 t = torch.tensor(10).to('cuda')
----> 3 print(embed(t))

...

RuntimeError: CUDA error: device-side assert triggered
CUDA kernel errors might be asynchronously reported at some other API call, so the stacktrace below might be incorrect.
For debugging consider passing CUDA_LAUNCH_BLOCKING=1.
Compile with `TORCH_USE_CUDA_DSA` to enable device-side assertions.
"""
```
```python
# MPS
embed = nn.Embedding(10, 2).to('mps')
t = torch.tensor(10).to('mps')
print(embed(t))

"""
tensor([0., 0.], device='mps:0', grad_fn=<EmbeddingBackward0>)
"""
```

### Versions

Collecting environment information...
PyTorch version: N/A
Is debug build: N/A
CUDA used to build PyTorch: N/A
ROCM used to build PyTorch: N/A

OS: macOS 15.1.1 (arm64)
GCC version: Could not collect
Clang version: 16.0.0 (clang-1600.0.26.6)
CMake version: version 3.30.1
Libc version: N/A

Python version: 3.13.1 | packaged by Anaconda, Inc. | (main, Dec 11 2024, 10:38:40) [Clang 14.0.6 ] (64-bit runtime)
Python platform: macOS-15.1.1-arm64-arm-64bit-Mach-O
Is CUDA available: N/A
CUDA runtime version: Could not collect
CUDA_MODULE_LOADING set to: N/A
GPU models and configuration: Could not collect
Nvidia driver version: Could not collect
cuDNN version: Could not collect
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: N/A

CPU:
Apple M3 Max

Versions of relevant libraries:
[pip3] numpy==2.2.1
[pip3] optree==0.13.1
[pip3] torch==2.6.0a0+git0431d47
[conda] numpy                     2.2.1                    pypi_0    pypi
[conda] optree                    0.13.1                   pypi_0    pypi
[conda] torch                     2.6.0a0+git0431d47          pypi_0    pypi

cc @kulinseth @albanD @malfet @DenisVieriu97 @jhavukainen