'''simple docstring'''





import torch
from torch import autograd




class      lowercase_						(						autograd.Function		):



	"""simple docstring"""





	@staticmethod
	def     SCREAMING_SNAKE_CASE_       (							__SCREAMING_SNAKE_CASE	,    __SCREAMING_SNAKE_CASE	,    __SCREAMING_SNAKE_CASE	,    __SCREAMING_SNAKE_CASE       )		->Tuple:
								lowerCAmelCase				=	inputs.numel()
								lowerCAmelCase				=	int(0.0_0_5 * nb_elems       ) + 1
								if sigmoid:
															lowerCAmelCase				=	(torch.sigmoid(__SCREAMING_SNAKE_CASE       ) > threshold).type(inputs.type()       )
								else:
															lowerCAmelCase				=	(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
															lowerCAmelCase				=	inputs.flatten().kthvalue(max(nb_elems - nb_min	,    1       )       ).values
															lowerCAmelCase				=	(inputs > k_threshold).type(inputs.type()       )
								return mask




	@staticmethod
	def     SCREAMING_SNAKE_CASE_       (							__SCREAMING_SNAKE_CASE	,    __SCREAMING_SNAKE_CASE       )		->List[Any]:
								return gradOutput, None, None




class      lowercase_						(						autograd.Function		):



	"""simple docstring"""





	@staticmethod
	def     SCREAMING_SNAKE_CASE_       (							__SCREAMING_SNAKE_CASE	,    __SCREAMING_SNAKE_CASE	,    __SCREAMING_SNAKE_CASE       )		->Any:
								lowerCAmelCase				=	inputs.clone()
								lowerCAmelCase	,   lowerCAmelCase				=	inputs.flatten().sort(descending=__SCREAMING_SNAKE_CASE       )
								lowerCAmelCase				=	int(threshold * inputs.numel()       )

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




	@staticmethod
	def     SCREAMING_SNAKE_CASE_       (							__SCREAMING_SNAKE_CASE	,    __SCREAMING_SNAKE_CASE       )		->Optional[int]:
								return gradOutput, None




class      lowercase_						(						A_		):



	"""simple docstring"""





	@staticmethod
	def     SCREAMING_SNAKE_CASE_       (							__SCREAMING_SNAKE_CASE	,    __SCREAMING_SNAKE_CASE       )		->Dict:
								lowerCAmelCase				=	inputs.clone()
								lowerCAmelCase	,   lowerCAmelCase				=	inputs.abs().flatten().sort(descending=__SCREAMING_SNAKE_CASE       )
								lowerCAmelCase				=	int(threshold * inputs.numel()       )

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




def      SCREAMING_SNAKE_CASE_  (		snake_case__     ,						snake_case__							)  ->			int:
							return (input_a, input_a).count(0							) % 2

def      SCREAMING_SNAKE_CASE_  (		)  ->			None:
							assert xor_gate(0     ,						0							) == 0
							assert xor_gate(0     ,						1							) == 1
							assert xor_gate(1     ,						0							) == 1
							assert xor_gate(1     ,						1							) == 0


if __name__ == "__main__":
	print(xor_gate(0, 0))
	print(xor_gate(0, 1))



