Repository: pytorch
Issue: RuntimeError: Expected a proper Tensor but got None (or an undefined Tensor in C++) for argument #0 'grad_y'
Labels: module: autograd,triaged,actionable,module: mps

### 🐛 Describe the bug

##Code 

Encoder:

```
import torch
import torch.nn as nn
import torch.optim as optim


class Encoder(nn.Module):
    def __init__(self, input_dim, emb_dim, hid_dim, n_layers, dropout):
        super().__init__()
        
        self.hid_dim = hid_dim
        self.n_layers = n_layers
        
        self.embedding = nn.Embedding(input_dim, emb_dim)
        
        self.rnn = nn.LSTM(emb_dim, hid_dim, n_layers, batch_first=True,dropout = dropout,)
        
        self.dropout = nn.Dropout(dropout)
        
    def forward(self, src):
        
        #src = [src len, batch size]
        
        embedded = self.dropout(self.embedding(src))
        
        outputs, (hidden, cell) = self.rnn(embedded)
        
        return hidden, cell 
```

Decoder:

```
import torch
import torch.nn as nn
import torch.optim as optim

'''The Decoder class does a single step of decoding, i.e. it ouputs single token per time-step'''
class Decoder(nn.Module):
    def __init__(self, output_dim, emb_dim, hid_dim, n_layers, dropout):
        super().__init__()
        
        self.output_dim = output_dim
        self.hid_dim = hid_dim
        self.n_layers = n_layers
        
        self.embedding = nn.Embedding(output_dim, emb_dim)
        
        self.rnn = nn.LSTM(emb_dim, hid_dim, n_layers, batch_first=True,dropout = dropout)
        
        self.fc_out = nn.Linear(hid_dim, output_dim)
        
        self.dropout = nn.Dropout(dropout)

    def forward(self, input, hidden, cell):
        
        input = input.unsqueeze(1)
        
        embedded = self.dropout(self.embedding(input))
                
        output, (hidden, cell) = self.rnn(embedded, (hidden, cell))
        
        prediction = self.fc_out(output.squeeze(1))
        
        return prediction, hidden, cell 
```

Seq2Seq

```
import torch
import torch.nn as nn
import torch.optim as optim
import random
from utils import PrintDebug
from loadData2 import VOCAB_TRANSFORM,SRC_LANGUAGE,TGT_LANGUAGE

class Seq2Seq(nn.Module):
    def __init__(self, encoder, decoder, device):
        super().__init__()
        
        self.encoder = encoder
        self.decoder = decoder
        self.device = device
        
        assert encoder.hid_dim == decoder.hid_dim, \
            "Hidden dimensions of encoder and decoder must be equal!"
        assert encoder.n_layers == decoder.n_layers, \
            "Encoder and decoder must have equal number of layers!"
        
    def forward(self, src, trg, teacher_forcing_ratio = 0.5):

        batch_size = trg.shape[0]
        trg_len = trg.shape[1]
        trg_vocab_size = self.decoder.output_dim
        
        #tensor to store decoder outputs
        outputs = torch.zeros(batch_size,trg_len, trg_vocab_size).to(self.device)
        
        #last hidden state of the encoder is used as the initial hidden state of the decoder
        hidden, cell = self.encoder(src)
        
        #first input to the decoder is the <sos> tokens
        input = trg[:,0]

        for t in range(1, trg_len):
            
            output, hidden, cell = self.decoder(input, hidden, cell)
            
            outputs[:,t] = output
            teacher_force = random.random() < teacher_forcing_ratio

            top1 = torch.argmax(output,axis=1) 
            input = trg[:,t] if teacher_force else top1
        
        return outputs
```

The issue only happens when running the code on MPS on Mac M2. On CPU the code runs fine and no errors pop up. Only on MPS and the DEVICE this issue happens on ```loss.backward()```. I found another issue with the same title ```https://github.com/pytorch/pytorch/issues/103430``` and I see that a PR was made to fix this though i belive a fix is still needed.  

```
  File "/Users/deepakrishi/Documents/Repo/pytorch/SequentialData/run.py", line 105, in <module>
    train_loss = train(MODEL, TRAIN_DATALOADER, OPTIMIZER, CRITERION, CLIP)
  File "/Users/deepakrishi/Documents/Repo/pytorch/SequentialData/run.py", line 52, in train
    loss.backward()
  File "/Users/deepakrishi/miniconda3/envs/pytorch/lib/python3.10/site-packages/torch/_tensor.py", line 487, in backward
    torch.autograd.backward(
  File "/Users/deepakrishi/miniconda3/envs/pytorch/lib/python3.10/site-packages/torch/autograd/__init__.py", line 200, in backward
    Variable._execution_engine.run_backward(  # Calls into the C++ engine to run the backward pass
RuntimeError: Expected a proper Tensor but got None (or an undefined Tensor in C++) for argument #0 'grad_y' 
```

### Versions

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.1 (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 (main, Apr 20 2023, 13:58:42) [Clang 14.0.6 ] (64-bit runtime)
Python platform: macOS-13.4.1-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 Pro

Versions of relevant libraries:
[pip3] mypy-extensions==1.0.0
[pip3] numpy==1.21.4
[pip3] torch==2.0.1
[pip3] torcharrow==0.1.0
[pip3] torchaudio==2.0.2
[pip3] torchdata==0.6.1
[pip3] torchtext==0.15.2
[pip3] torchvision==0.15.2
[conda] numpy                     1.21.4                   pypi_0    pypi
[conda] pytorch                   2.0.1                  py3.10_0    pytorch
[conda] torcharrow                0.1.0                    pypi_0    pypi
[conda] torchaudio                2.0.2                 py310_cpu    pytorch
[conda] torchdata                 0.6.1                     py310    pytorch
[conda] torchtext                 0.15.2                   pypi_0    pypi
[conda] torchvision               0.15.2                py310_cpu    pytorch


cc @ezyang @albanD @zou3519 @gqchen @pearu @nikitaved @soulitzer @Lezcano @Varal7 @kulinseth @malfet @DenisVieriu97 @razarmehr @abhudev