Interesting observation! The reason it’s happening is:
        pe[:, 0::2] = torch.sin(position * div_term)
        pe[:, 1::2] = torch.cos(position * div_term)

Here, torch.sin is used for the even indexes and torch.cos is used for the odd indexes of the input dimension (size = d_model). So, if d_model is odd, there will be a dimension size mismatch (division by 2 leaves a remainder).