






import torch
from torch import autograd



class  __UpperCAmelCase						(autograd.Function       ):







			@staticmethod
			def 				UpperCamelCase (      UpperCAmelCase_:    Tuple ,      UpperCAmelCase_:    torch.tensor ,      UpperCAmelCase_:    float ,      UpperCAmelCase_:    bool	):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE       =    inputs.numel()
									_SCREAMING_SNAKE_CASE       =    int(0.0_05 * nb_elems	) + 1
									if sigmoid:
															_SCREAMING_SNAKE_CASE       =    (torch.sigmoid(lowerCamelCase_	) > threshold).type(inputs.type()	)
									else:
															_SCREAMING_SNAKE_CASE       =    (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
															_SCREAMING_SNAKE_CASE       =    inputs.flatten().kthvalue(max(nb_elems - nb_min ,      1	)	).values
															_SCREAMING_SNAKE_CASE       =    (inputs > k_threshold).type(inputs.type()	)
									return mask







			@staticmethod
			def 				UpperCamelCase (      UpperCAmelCase_:    str ,      UpperCAmelCase_:    Tuple	):







									'''simple docstring'''




									return gradOutput, None, None



class  __UpperCAmelCase						(autograd.Function       ):







			@staticmethod
			def 				UpperCamelCase (      UpperCAmelCase_:    str ,      UpperCAmelCase_:    torch.tensor ,      UpperCAmelCase_:    float	):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE       =    inputs.clone()
									_SCREAMING_SNAKE_CASE ,	_SCREAMING_SNAKE_CASE       =    inputs.flatten().sort(descending=lowerCamelCase_	)
									_SCREAMING_SNAKE_CASE       =    int(threshold * inputs.numel()	)

									# flat_out and mask access the same memory.
									_SCREAMING_SNAKE_CASE       =    mask.flatten()
									_SCREAMING_SNAKE_CASE       =    0
									_SCREAMING_SNAKE_CASE       =    1
									return mask







			@staticmethod
			def 				UpperCamelCase (      UpperCAmelCase_:    Tuple ,      UpperCAmelCase_:    Optional[int]	):







									'''simple docstring'''




									return gradOutput, None



class  __UpperCAmelCase						(__SCREAMING_SNAKE_CASE       ):







			@staticmethod
			def 				UpperCamelCase (      UpperCAmelCase_:    torch.tensor ,      UpperCAmelCase_:    float	):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE       =    inputs.clone()
									_SCREAMING_SNAKE_CASE ,	_SCREAMING_SNAKE_CASE       =    inputs.abs().flatten().sort(descending=lowerCamelCase_	)
									_SCREAMING_SNAKE_CASE       =    int(threshold * inputs.numel()	)

									# flat_out and mask access the same memory.
									_SCREAMING_SNAKE_CASE       =    mask.flatten()
									_SCREAMING_SNAKE_CASE       =    0
									_SCREAMING_SNAKE_CASE       =    1
									return mask





"""simple docstring"""







from __future__ import annotations






def        a__			(							_UpperCamelCase			):





						"""simple docstring"""

						UpperCAmelCase__												=     sorted(_UpperCamelCase			)
						UpperCAmelCase__												=     len(_UpperCamelCase			)
						UpperCAmelCase__												=     length >> 1
						return (
						    (sorted_list[mid_index] + sorted_list[mid_index - 1]) / 2
						    if length % 2 == 0
						    else sorted_list[mid_index]
						)






def        a__			(							):





						"""simple docstring"""

						import doctest

						doctest.testmod()


if __name__ == "__main__":
						main()