You are right! Thanks for pointing this out.
Here is a (hopefully) fixed version:
kh, kw = 2, 2
dh, dw = 2, 2
input = torch.randint(10, (1,2,6,6))
input_windows = input.unfold(2, kh, dh).unfold(3, kw, dw)
input_windows = input_windows.contiguous().view(*input_windows.size()[:-2], -1)
input_windows_sorted = input_windows.sort(descending=True)[0]
input_windows_sorted = input_windows_sorted.permute(0, 1, 4, 2, 3)
print(input_windows_sorted.size())