





def       lowerCAmelCase__(	lowercase       :    List[str]				) ->    Optional[Any]:







  if not isinstance(lowercase_   ,     lowercase_				) or number < 0:
    raise ValueError("Input must be a non-negative integer"				)

  __snake_case   :				Any        =       number & ~(number - 1)
  __snake_case   :				Optional[Any]        =       0
  while intermediate:
    intermediate >>= 1
    index += 1
  return index - 1


if __name__ == "__main__":
     import doctest

     doctest.testmod(verbose=True)








import torch
from torch import autograd




class 				_lowerCamelCase (    autograd.Function    ):



      """simple docstring"""






      @staticmethod
      def 						UpperCAmelCase		( UpperCAmelCase				,						UpperCAmelCase				,						UpperCAmelCase				,						UpperCAmelCase   )       ->   str:






        '''simple docstring'''






        __snake_case   :				Dict        =       inputs.numel()
        __snake_case   :				Optional[int]        =       int(0.005 * nb_elems   ) + 1
        if sigmoid:
          __snake_case   :				Any        =       (torch.sigmoid(UpperCAmelCase   ) > threshold).type(inputs.type()   )
        else:
          __snake_case   :				Optional[int]        =       (inputs > threshold).type(inputs.type()   )
        if mask.sum() < nb_min:
          # We limit the pruning so that at least 0.5% (half a percent) of the weights are remaining
          __snake_case   :				List[str]        =       inputs.flatten().kthvalue(max(nb_elems - nb_min				,						1   )   ).values
          __snake_case   :				Union[str, Any]        =       (inputs > k_threshold).type(inputs.type()   )
        return mask







      @staticmethod
      def 						UpperCAmelCase		( UpperCAmelCase				,						UpperCAmelCase   )       ->   Optional[int]:






        '''simple docstring'''






        return gradOutput, None, None




class 				_lowerCamelCase (    autograd.Function    ):



      """simple docstring"""






      @staticmethod
      def 						UpperCAmelCase		( UpperCAmelCase				,						UpperCAmelCase				,						UpperCAmelCase   )       ->   Optional[int]:






        '''simple docstring'''






        __snake_case   :				Optional[int]        =       inputs.clone()
        __snake_case    ,   __snake_case   :				int        =       inputs.flatten().sort(descending=UpperCAmelCase   )
        __snake_case   :				Tuple        =       int(threshold * inputs.numel()   )

        # flat_out and mask access the same memory.
        __snake_case   :				Optional[int]        =       mask.flatten()
        __snake_case   :				Union[str, Any]        =       0
        __snake_case   :				Any        =       1
        return mask







      @staticmethod
      def 						UpperCAmelCase		( UpperCAmelCase				,						UpperCAmelCase   )       ->   List[str]:






        '''simple docstring'''






        return gradOutput, None




class 				_lowerCamelCase (    a    ):



      """simple docstring"""






      @staticmethod
      def 						UpperCAmelCase		( UpperCAmelCase				,						UpperCAmelCase   )       ->   Any:






        '''simple docstring'''






        __snake_case   :				Union[str, Any]        =       inputs.clone()
        __snake_case    ,   __snake_case   :				Optional[Any]        =       inputs.abs().flatten().sort(descending=UpperCAmelCase   )
        __snake_case   :				List[Any]        =       int(threshold * inputs.numel()   )

        # flat_out and mask access the same memory.
        __snake_case   :				Optional[Any]        =       mask.flatten()
        __snake_case   :				Tuple        =       0
        __snake_case   :				Any        =       1
        return mask

