I checked the code again.
The batch size and sequence length are both inferred from the input for each batch.
batch_size, sequence_length = y.shape

To be sure I also added  drop_last=True in the DataLoader, but the error persists.
When I intentionally reduce the batch size of either y, decoder_output or valid the following correct error message is raised:
IndexError: The shape of the mask [1] at index 0 does not match the shape of the indexed tensor [2, 7] at index 0

Maybe the following can be helpful too. I actually removed the following line of code from the example for simplicity. This line works perfectly fine and computes the expected outputs.
log_perplexity += torch.nn.functional.nll_loss(decoder_output[valid], y[valid, idx]).item()

May it be caused by the batch order being changed by the mask?