you could try with torch.nn.functional.unfold method.

  
      pytorch.org
  
  
    

Unfold — PyTorch 1.7.0 documentation 7



  
  
    
    
  
  


import torch, torch.nn as nn, torch.nn.functional as F
x = torch.randn(1, 128, 96, 96).cuda() 
o = F.unfold(x, kernel_size=16, stride=16) # 1, 32768, 36 
o = o.view(1, 128, 16, 16, 36)  # 1, 128, 16, 16, 36
o = o.permute(0, 4, 1, 2, 3)  # 1, 36, 128, 16, 16