You are looking for nn.Unfold (otherwise commonly known as im2col). See https://pytorch.org/docs/master/nn.html#torch.nn.Unfold 27.
The functional form also exists and is at torch.nn.functional.unfold.