Thanks @anantguptadbl . That works much faster than the loop.
import torch
import time


def stack_sequence(sequence, window_size):
    thread_range=sequence.size(-1)-window_size+1
    out=torch.empty(0,window_size)
    for i in range(thread_range):
        out=torch.cat([out,sequence[i:i+window_size].unsqueeze(0)])

    return out

def stack_sequence2(sequence, window_size):
    return torch.stack([sequence[x:x+window_size] for x in range(sequence.size()[-1]-window_size+1)])



A=torch.arange(start=1, end=100001)
now=time.time()
stack_sequence(A, 10)
print(time.time()-now)

now=time.time()
stack_sequence2(A,10)
print(time.time()-now)

Results:
32.05299949645996
0.2589995861053467