Repository: pytorch
Issue: PackedSequences on MPS accelerator yields `grad_y` missing or crashes the kernel.
Labels: module: crash,triaged,module: mps

### 🐛 Describe the bug

# PackedSequences on MPS accelerator yields `grad_y` missing or crashes the kernel.

This issue is related to [Issue 96416][loss error on m1] ([Loss.backward() error when using MPS on M1 #96416][loss error on m1]), [Issue 94691][gru nan] ([Nan is output by GRU on mps #94691][gru nan]), [Issue 97552][packed sequence failure] ([PackedSequence failure with MPS #97552][packed sequence failure]), and [PR 96601][grad_y missing fix] ([[MPS] LSTM grad_y missing fix #96601][grad_y missing fix]).

To faithfully reproduce the error I have provided a self contained [Github Gist][Github Gist].

In addition, I already pasted some [results][Results] showing that this does consistently yield `Expected a proper Tensor but got None (or an undefined Tensor in C++) for argument #0 'grad_y'` or crash a Jupyter Notebook kernel.

A `env.yml` file is provided in the [Github Gist][Github Gist] and using `collect_env.py`:

```sh
# For security purposes, please check the contents of collect_env.py before running it.
wget https://raw.githubusercontent.com/pytorch/pytorch/main/torch/utils/collect_env.py

$ python collect_env.py


Collecting environment information...
PyTorch version: 2.0.1
Is debug build: False
CUDA used to build PyTorch: None
ROCM used to build PyTorch: N/A

OS: macOS 13.4 (arm64)
GCC version: Could not collect
Clang version: 14.0.3 (clang-1403.0.22.14.1)
CMake version: Could not collect
Libc version: N/A

Python version: 3.10.11 | packaged by conda-forge | (main, May 10 2023, 19:01:19) [Clang 14.0.6 ] (64-bit runtime)
Python platform: macOS-13.4-arm64-arm-64bit
Is CUDA available: False
CUDA runtime version: No CUDA
CUDA_MODULE_LOADING set to: N/A
GPU models and configuration: No CUDA
Nvidia driver version: No CUDA
cuDNN version: No CUDA
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

CPU:
Apple M2 Max

Versions of relevant libraries:
[pip3] numpy==1.24.3
[pip3] pytorch-lightning==2.0.2
[pip3] torch==2.0.1
[pip3] torchmetrics==0.11.4
[conda] numpy                     1.24.3          py310haa1e00c_0    conda-forge
[conda] pytorch                   2.0.1                  py3.10_0    pytorch
[conda] pytorch-lightning         2.0.2              pyhd8ed1ab_0    conda-forge
[conda] torchmetrics              0.11.4             pyhd8ed1ab_0    conda-forge
```


Of note @alexdremov has started to fix the error ([[MPS] LSTM grad_y missing fix #96601][grad_y missing fix]), but it may not work with PackedSequences. Quote:
> What worries me is that the example uses packed sequences. Not sure, but this may be the problem as MPS LSTM does not support it.
> 
> Concerning GRU, it simply does not work on MPS ;). No need to even test it.



[packed sequence failure]: https://github.com/pytorch/pytorch/issues/97552
[loss error on m1]: https://github.com/pytorch/pytorch/issues/96416
[grad_y missing fix]: https://github.com/pytorch/pytorch/pull/96601
[gru nan]: https://github.com/pytorch/pytorch/issues/94691
[Github Gist]: https://gist.github.com/dsm-72/1cea0601145a8b92155d8d08c90bf998
[Results]: https://github.com/pytorch/pytorch/issues/94691#issuecomment-1574365231


cc @kulinseth @albanD @malfet @DenisVieriu97 @jhavukainen @razarmehr @abhudev