Two things:

Can you share the part where you are reading, preparing the data and vocab. If you’re using a text field maybe try:
text_field = data.Field(lower=True, fix_length=100)

I think this part can be moved inside model definition:




 keimoriyama:

        if data.shape[0] != bptt:
            src_mask = model.generate_square_subsequent_mask(data.shape[0]).to(device)
        output = model(data, src_mask)



Make sure the input sequence lengths are the same (for all batches). Then, align it with the target length.