Solved by alepack in post #3 
              
              
                Hi 
If it can help, I think a simple implementation of the median pooling using fold/unfold can be 
import torch as th
import torch.nn as thNn
import torch.nn.functional as thFn

import math

def unpack_param_2d(param):

  try:
    p_H, p_W = param[0], param[1]
  except:
    p_H, p_W = param, param
…