Thanks for your reply.

How to automatically use padding in unfold? Or just padding before unfolding operation?
In your posted example, why do we need to permute patches with parameters (0,1,3,2)? What is the difference between

patches = patches.contiguous().view(B, C, -1, kernel_size*kernel_size)
print(patches.shape) # [B, C, nb_patches_all, kernel_size*kernel_size]
patches = patches.permute(0, 1, 3, 2) 
print(patches.shape) # [B, C, kernel_size*kernel_size, nb_patches_all]
patches = patches.contiguous().view(B, C*kernel_size*kernel_size, -1)
print(patches.shape) # [B, C*prod(kernel_size), L] as expected by Fold

and
patches2=patches.contiguous().view(B,C,-1,kernel_size*kernel_size)
print(patches2.shape)
patches2=patches2.view(B,C*kernel_size*kernel_size,-1)
print(patches2.shape)

Thanks a lot!