def repeated_batch(batch_size, num_steps, vocab, Lmin, Lmax, device=None):
"""Random patterns of length Lmin..Lmax, tiled to num_steps tokens."""
L = torch.randint(Lmin, Lmax + 1, (batch_size,), device=device)
s = torch.randint(0, vocab, (batch_size, num_steps), device=device)
pos = torch.arange(num_steps, device=device)
return torch.gather(s, 1, pos[None, :] % L[:, None]), L
x, L = repeated_batch(batch_size=2, num_steps=16, vocab=10, Lmin=5, Lmax=8)
print(f'pattern lengths: {L.tolist()}')
print(x)