I think if you write this as normal Module (instead of an autograd function) you can get the backwards for automatically:
class Thresher(nn.Module):
  def __init__(self, n):
   self.holds = n

 def forward(self, input):
   abs = torch.abs(input)
   th, _ = torch.sort(abs)
   threshold = th[0, output.size()[1]-self.holds]
   mask = (abs >= threshold).type_as(input)
   return input * mask

# or just as a normal function

def threshold(input, holds):
   abs = torch.abs(input)
   th, _ = torch.sort(abs)
   threshold = th[0, output.size()[1]-holds]
   mask = (abs >= threshold).type_as(input)
   return input * mask