I forgot add training loop, here it is:
# Hyperparameters
src_vocab_size = len(vocab)
d_model = 512
nhead = 8
dim_forward = 1024
num_layers = 12
max_len = 10
pad_idx = 0
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
 
load_model = False
save_model = True
 
learning_rate = 1e-5
src_pad_idx = 0
 
model = Kemal(src_vocab_size, d_model, nhead, dim_forward, num_layers, max_len, pad_idx, 0.3, device).to(device)
 
optimizer = optim.Adam(model.parameters(), lr=learning_rate)
criterion = nn.CrossEntropyLoss()
#scheduler = optim.lr_scheduler.StepLR(optimizer, 1.0, gamma=0.95)
 
loss_list = []
 
model.train() 
for epoch in range(500):
    for sequence in range(int(len(encoded_text[0:30])/10)):
        x, y = get_data(encoded_text[0:30], sequence)
        x = masking(x)
        x = torch.LongTensor(x).to(device)
        y = torch.LongTensor(y).to(device)
        x = x.reshape(1, -1)

        out = model(x)
        out = out.view(-1, len(vocab))
 
        optimizer.zero_grad()
        loss = criterion(out, y)
        loss.backward()

        torch.nn.utils.clip_grad_norm_(model.parameters(), 0.5)
            
        if sequence % 1000 == 0 and sequence != 0:
          print("Loss: ", loss, "S: ", sequence)
 
        optimizer.step()

    #scheduler.step()

    if epoch % 1 == 0:
      if len(loss_list) > 0:
        print(f'Epoch: {epoch}, Step: {sequence}, Loss: {sum(loss_list) / len(loss_list)}')
      loss_list.append(loss)