You have to code positional encoding yourself, pytorch hasn’t have this.
I work with Transformers in NLP section and i usually use only Transformer Encoder layer for forecasting so i add simple example
import torch
import math
import torch
import torch.nn as nn
import torch.nn.functional as F

class Example(nn.Module):
    def __init__(self, vocab_size, feature_size):
        super(Example, self).__init__()
        self.embedding = nn.Embedding(vocab_size, feature_size)
        self.layers = nn.TransformerEncoderLayer(d_model=feature_size, nhead=8)
        self.transformer = nn.TransformerEncoder(self.layers, num_layers=6)
        self.decoder = nn.Linear(feature_size, vocab_size)
        
    def forward(self, x):
        # X shape: [seq_len, batch_size]
        print(x.shape)
        x = self.embedding(x)
        # X shape: [seq_len, batch_size, feature_size]
        print(x.shape)
        x = self.transformer(x)
        # X shape: [seq_len, batch_size, feature_size]
        print(x.shape)
        x = self.decoder(x)
        # X shape: [seq_len, batc_size, vocab_size]
        print(x.shape)
        
        return x
    
model = Example(10, 512)
src = torch.LongTensor([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]).view(10, 1)
model(src)