Update:
I have no provided a standalone example of the problem that shows that the transformer doesn’t learn. I have simplified the model even more:
import torch
import torch.nn as nn
import random
import math

d_model = 512

class PositionalEncoding(nn.Module):
    def __init__(self, d_model, dropout=0.1, max_len=5000):
        super(PositionalEncoding, self).__init__()
        self.dropout = nn.Dropout(p=dropout)

        pe = torch.zeros(max_len, d_model)
        position = torch.arange(0, max_len, dtype=torch.float).unsqueeze(1)
        div_term = torch.exp(torch.arange(0, d_model, 2).float() * (-math.log(10000.0) / d_model))
        pe[:, 0::2] = torch.sin(position * div_term)
        pe[:, 1::2] = torch.cos(position * div_term)
        pe = pe.unsqueeze(0).transpose(0, 1)
        self.register_buffer('pe', pe)

    def forward(self, x):
        x = x + self.pe[:x.size(0), :]
        return self.dropout(x)

class TransformerModel(nn.Module):
    def __init__(self, nhead=8, dim_feedforward=1024, num_layers=6, dropout=0.1):
        super(TransformerModel, self).__init__()
        self.d_model = d_model
        self.output_embedding = nn.Linear(self.d_model, self.d_model)
        self.pos_encoder = PositionalEncoding(self.d_model, dropout)
        self.transformer = nn.Transformer(d_model=self.d_model, nhead=nhead, dim_feedforward=dim_feedforward,
                                          num_encoder_layers=num_layers, num_decoder_layers=num_layers, dropout=dropout)
        self.output_linear = nn.Linear(self.d_model, self.d_model)
        self.soft_max = nn.Softmax(dim=2)

    def forward(self, src, tgt):
        # src is a tensor of shape (S, N, d_model), where N = batch size,
        # S = sequence length of input, d_model = channel size
        # tgt is a tensor of shape (T, N, d_model), where N = batch size,
        # T = sequence length of output, d_model = channel size
        tgt_len = tgt.shape[0]

        tgt_mask = self.transformer.generate_square_subsequent_mask(tgt_len)

        src = self.pos_encoder(src)

        tgt = self.output_embedding(tgt)
        tgt = self.pos_encoder(tgt)

        output = self.transformer(src, tgt, tgt_mask=tgt_mask)
        output = self.output_linear(output)
        output = self.soft_max(output)
        return output

src_len = 30
tgt_len = 20
batch_size = 1

src = torch.randn((src_len, batch_size, d_model))
tgt = torch.zeros((tgt_len, batch_size, d_model))

for i in range(tgt_len):
    for j in range(batch_size):
        k = random.randrange(0, d_model)
        tgt[i, j, k] = 1.0

model = TransformerModel(dropout=0.0)

optimizer = torch.optim.Adam(model.parameters(), lr=0.01)
criterion = nn.CrossEntropyLoss()

# Feed the same example to the transformer 1000 times to test if it's learning anything
for _ in range(1000):
    optimizer.zero_grad()
    output = model(src, tgt)
    loss = criterion(output.view(-1, d_model), torch.argmax(tgt.view(-1, d_model), dim=1))
    difference = torch.sum(torch.abs(tgt - output))
    print("Loss", loss)
    print("Difference", difference)
    loss.backward()
    optimizer.step()

Output:
Loss tensor(6.2385, grad_fn=<NllLossBackward>)
Difference tensor(39.9306, grad_fn=<SumBackward0>)
Loss tensor(6.2116, grad_fn=<NllLossBackward>)
Difference tensor(38.8502, grad_fn=<SumBackward0>)
Loss tensor(6.1904, grad_fn=<NllLossBackward>)
Difference tensor(38.0000, grad_fn=<SumBackward0>)
Loss tensor(6.1903, grad_fn=<NllLossBackward>)
Difference tensor(38.0000, grad_fn=<SumBackward0>)
Loss tensor(6.1903, grad_fn=<NllLossBackward>)
Difference tensor(38., grad_fn=<SumBackward0>)
Loss tensor(6.1903, grad_fn=<NllLossBackward>)
Difference tensor(38., grad_fn=<SumBackward0>)
Loss tensor(6.1903, grad_fn=<NllLossBackward>)
Difference tensor(38., grad_fn=<SumBackward0>)
Loss tensor(6.1903, grad_fn=<NllLossBackward>)
Difference tensor(38., grad_fn=<SumBackward0>)
Loss tensor(6.1903, grad_fn=<NllLossBackward>)
Difference tensor(38., grad_fn=<SumBackward0>)
Loss tensor(6.1903, grad_fn=<NllLossBackward>)
Difference tensor(38., grad_fn=<SumBackward0>)

As you can see, the loss never decreases past 6.1903, and the absolute difference sum does not decrease past 38.