class_docstr: Magnitude Binarizer.
  Computes a binary mask M from a real value matrix S such that `M_{i,j} = 1` if and only if `S_{i,j}`
  is among the k% highest values of |S| (absolute value).
  
  Implementation is inspired from https://github.com/NervanaSystems/distiller/blob/2291fdcc2ea642a98d4e20629acb5a9e2e04b4e6/distiller/pruning/automated_gradual_pruner.py#L24
class_docstr_tok: ["Magnitude","Binarizer",".","Computes","a","binary","mask","M","from","a","real","value","matrix","S","such","that","`","M_","{","i",",","j","}","=","1","`","if","and","only","if","`","S_","{","i",",","j","}","`","is","among","the","k","%","highest","values","of","|S|","(","absolute","value",")",".","Implementation","is","inspired","from","https",":","\/\/github.com\/NervanaSystems\/distiller\/blob\/2291fdcc2ea642a98d4e20629acb5a9e2e04b4e6\/distiller\/pruning\/automated_gradual_pruner.py","#","L24"]
docstr: Args:
      inputs (`torch.FloatTensor`)
          The input matrix from which the binarizer computes the binary mask.
          This input marix is typically the weight matrix.
      threshold (`float`)
          The percentage of weights to keep (the rest is pruned).
          `threshold` is a float between 0 and 1.
  Returns:
      mask (`torch.FloatTensor`)
          Binary matrix of the same size as `inputs` acting as a mask (1 - the associated weight is
          retained, 0 - the associated weight is pruned).
docstr_tok: ["Args",":","inputs","(","`","torch.FloatTensor","`",")","The","input","matrix","from","which","the","binarizer","computes","the","binary","mask",".","This","input","marix","is","typically","the","weight","matrix",".","threshold","(","`","float","`",")","The","percentage","of","weights","to","keep","(","the","rest","is","pruned",")",".","`","threshold","`","is","a","float","between","0","and","1",".","Returns",":","mask","(","`","torch.FloatTensor","`",")","Binary","matrix","of","the","same","size","as","`","inputs","`","acting","as","a","mask","(","1","-","the","associated","weight","is","retained",",","0","-","the","associated","weight","is","pruned",")","."]
code: def apply(inputs: torch.tensor, threshold: float):
      """
      Args:
          inputs (`torch.FloatTensor`)
              The input matrix from which the binarizer computes the binary mask.
              This input marix is typically the weight matrix.
          threshold (`float`)
              The percentage of weights to keep (the rest is pruned).
              `threshold` is a float between 0 and 1.
      Returns:
          mask (`torch.FloatTensor`)
              Binary matrix of the same size as `inputs` acting as a mask (1 - the associated weight is
              retained, 0 - the associated weight is pruned).
      """
      # Get the subnetwork by sorting the inputs and using the top threshold %
      mask = inputs.clone()
      _, idx = inputs.abs().flatten().sort(descending=True)
      j = int(threshold * inputs.numel())
  
      # flat_out and mask access the same memory.
      flat_out = mask.flatten()
      flat_out[idx[j:]] = 0
      flat_out[idx[:j]] = 1
      return mask
  
file_path: transformers/examples/research_projects/movement-pruning/emmental/modules/binarizer.py
imports: import torch
  from torch import autograd
label_desc: Use image node_id 1 for calling the MagnitudeBinarizer obj's underlying member method code with example usage: obj.apply(inputs, threshold) and returns: mask