






import unittest

import numpy as np

from transformers import AlbertConfig, is_flax_available
from transformers.testing_utils import require_flax, slow

from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask


if is_flax_available():
	import jax.numpy as jnp

	from transformers.models.albert.modeling_flax_albert import (
	    FlaxAlbertForMaskedLM,
	    FlaxAlbertForMultipleChoice,
	    FlaxAlbertForPreTraining,
	    FlaxAlbertForQuestionAnswering,
	    FlaxAlbertForSequenceClassification,
	    FlaxAlbertForTokenClassification,
	    FlaxAlbertModel,
	)


class 					_snake_case						(			unittest.TestCase ):






	def __init__(		self					,UpperCamelCase					,UpperCamelCase=13					,UpperCamelCase=7					,UpperCamelCase=True					,UpperCamelCase=True					,UpperCamelCase=True					,UpperCamelCase=True					,UpperCamelCase=99					,UpperCamelCase=32					,UpperCamelCase=5					,UpperCamelCase=4					,UpperCamelCase=37					,UpperCamelCase="gelu"					,UpperCamelCase=0.1					,UpperCamelCase=0.1					,UpperCamelCase=512					,UpperCamelCase=16					,UpperCamelCase=2					,UpperCamelCase=0.02					,UpperCamelCase=4					,)						->    Optional[int]:
					snake_case__      :Optional[Any]         =   parent
					snake_case__      :Tuple         =   batch_size
					snake_case__      :Tuple         =   seq_length
					snake_case__      :Optional[int]         =   is_training
					snake_case__      :str         =   use_attention_mask
					snake_case__      :Dict         =   use_token_type_ids
					snake_case__      :int         =   use_labels
					snake_case__      :Optional[Any]         =   vocab_size
					snake_case__      :Dict         =   hidden_size
					snake_case__      :List[str]         =   num_hidden_layers
					snake_case__      :Tuple         =   num_attention_heads
					snake_case__      :Optional[int]         =   intermediate_size
					snake_case__      :Optional[int]         =   hidden_act
					snake_case__      :Optional[int]         =   hidden_dropout_prob
					snake_case__      :List[str]         =   attention_probs_dropout_prob
					snake_case__      :Any         =   max_position_embeddings
					snake_case__      :List[Any]         =   type_vocab_size
					snake_case__      :List[Any]         =   type_sequence_label_size
					snake_case__      :Optional[int]         =   initializer_range
					snake_case__      :List[Any]         =   num_choices






	def    lowerCAmelCase_							(		self       )						->    Tuple:
					snake_case__      :Optional[Any]         =   ids_tensor([self.batch_size, self.seq_length]					,self.vocab_size       )

					snake_case__      :List[Any]         =   None
					if self.use_attention_mask:
									snake_case__      :Tuple         =   random_attention_mask([self.batch_size, self.seq_length]       )

					snake_case__      :List[str]         =   None
					if self.use_token_type_ids:
									snake_case__      :Optional[int]         =   ids_tensor([self.batch_size, self.seq_length]					,self.type_vocab_size       )

					snake_case__      :Tuple         =   AlbertConfig(
					    vocab_size=self.vocab_size					,hidden_size=self.hidden_size					,num_hidden_layers=self.num_hidden_layers					,num_attention_heads=self.num_attention_heads					,intermediate_size=self.intermediate_size					,hidden_act=self.hidden_act					,hidden_dropout_prob=self.hidden_dropout_prob					,attention_probs_dropout_prob=self.attention_probs_dropout_prob					,max_position_embeddings=self.max_position_embeddings					,type_vocab_size=self.type_vocab_size					,is_decoder=UpperCamelCase					,initializer_range=self.initializer_range					,)

					return config, input_ids, token_type_ids, attention_mask






	def    lowerCAmelCase_							(		self       )						->    Optional[int]:
					snake_case__      :Union[str, Any]         =   self.prepare_config_and_inputs()
					snake_case__    ,						snake_case__    ,						snake_case__    ,						snake_case__      :Any         =   config_and_inputs
					snake_case__      :Optional[Any]         =   {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": attention_mask}
					return config, inputs_dict




@require_flax
class 					_snake_case						(			_A       ,    unittest.TestCase ):
	_A          =   (
	    (
	        FlaxAlbertModel,
	        FlaxAlbertForPreTraining,
	        FlaxAlbertForMaskedLM,
	        FlaxAlbertForMultipleChoice,
	        FlaxAlbertForQuestionAnswering,
	        FlaxAlbertForSequenceClassification,
	        FlaxAlbertForTokenClassification,
	        FlaxAlbertForQuestionAnswering,
	    )
	    if is_flax_available()
	    else ()
	)






	def    lowerCAmelCase_							(		self       )						->    Any:
					snake_case__      :Union[str, Any]         =   FlaxAlbertModelTester(self       )






	@slow
	def    lowerCAmelCase_							(		self       )						->    str:
					for model_class_name in self.all_model_classes:
									snake_case__      :Union[str, Any]         =   model_class_name.from_pretrained("albert-base-v2"       )
									snake_case__      :Any         =   model(np.ones((1, 1)       )       )
									self.assertIsNotNone(UpperCamelCase       )




@require_flax
class 					_snake_case						(			unittest.TestCase ):






	@slow
	def    lowerCAmelCase_							(		self       )						->    int:
					snake_case__      :Optional[int]         =   FlaxAlbertModel.from_pretrained("albert-base-v2"       )
					snake_case__      :Tuple         =   np.array([[0, 345, 232, 328, 740, 140, 1_695, 69, 6_078, 1_588, 2]]       )
					snake_case__      :List[Any]         =   np.array([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]       )
					snake_case__      :Tuple         =   model(UpperCamelCase					,attention_mask=UpperCamelCase       )[0]
					snake_case__      :Optional[int]         =   (1, 11, 768)
					self.assertEqual(output.shape					,UpperCamelCase       )
					snake_case__      :Tuple         =   np.array(
					    [[[-0.6513, 1.5035, -0.2766], [-0.6515, 1.5046, -0.2780], [-0.6512, 1.5049, -0.2784]]]       )

					self.assertTrue(jnp.allclose(output[:, 1:4, 1:4]					,UpperCamelCase					,atol=1E-4       )       )







def 					lowercase_ (    __snake_case     :							Tuple	,	__snake_case     :							Optional[int]      )     -> List[Any]:





				'''simple docstring'''


				snake_case__      :Dict         =   ""
				for i in table:
								res += inp[i - 1]
				return res





def 					lowercase_ (    __snake_case     :							List[str]      )     -> int:





				'''simple docstring'''


				return data[1:] + data[0]





def 					lowercase_ (    __snake_case     :							int	,	__snake_case     :							Union[str, Any]      )     -> Union[str, Any]:





				'''simple docstring'''


				snake_case__      :Union[str, Any]         =   ""
				for i in range(len(__snake_case      )      ):
								if a[i] == b[i]:
												res += "0"
								else:
												res += "1"
				return res





def 					lowercase_ (    __snake_case     :							Optional[int]	,	__snake_case     :							Dict      )     -> Union[str, Any]:





				'''simple docstring'''


				snake_case__      :int         =   int("0b" + data[0] + data[-1]	,	2      )
				snake_case__      :Union[str, Any]         =   int("0b" + data[1:3]	,	2      )
				return bin(s[row][col]      )[2:]





def 					lowercase_ (    __snake_case     :							Dict	,	__snake_case     :							Optional[Any]	,	__snake_case     :							Dict	,	__snake_case     :							List[Any]	,	__snake_case     :							Optional[int]      )     -> List[str]:





				'''simple docstring'''


				snake_case__      :Tuple         =   message[:4]
				snake_case__      :int         =   message[4:]
				snake_case__      :int         =   apply_table(__snake_case	,	__snake_case      )
				snake_case__      :Union[str, Any]         =   xor(__snake_case	,	__snake_case      )
				snake_case__      :Tuple         =   apply_sbox(__snake_case	,	temp[:4]      )  # noqa: E741
				snake_case__      :List[str]         =   apply_sbox(__snake_case	,	temp[4:]      )
				snake_case__      :int         =   "0" * (2 - len(__snake_case      )) + l  # noqa: E741
				snake_case__      :int         =   "0" * (2 - len(__snake_case      )) + r
				snake_case__      :Optional[Any]         =   apply_table(l + r	,	__snake_case      )
				snake_case__      :Tuple         =   xor(__snake_case	,	__snake_case      )
				return temp + right


if __name__ == "__main__":
	__UpperCAmelCase							:   Dict							=  input("Enter 10 bit key: ")
	__UpperCAmelCase							:   Tuple							=  input("Enter 8 bit message: ")

	__UpperCAmelCase							:   Any							=  [6, 3, 7, 4, 8, 5, 1_0, 9]
	__UpperCAmelCase							:   List[str]							=  [3, 5, 2, 7, 4, 1_0, 1, 9, 8, 6]
	__UpperCAmelCase							:   Tuple							=  [2, 4, 3, 1]
	__UpperCAmelCase							:   List[Any]							=  [2, 6, 3, 1, 4, 8, 5, 7]
	__UpperCAmelCase							:   Optional[Any]							=  [4, 1, 3, 5, 7, 2, 8, 6]
	__UpperCAmelCase							:   Optional[int]							=  [4, 1, 2, 3, 2, 3, 4, 1]
	__UpperCAmelCase							:   List[Any]							=  [[1, 0, 3, 2], [3, 2, 1, 0], [0, 2, 1, 3], [3, 1, 3, 2]]
	__UpperCAmelCase							:   Union[str, Any]							=  [[0, 1, 2, 3], [2, 0, 1, 3], [3, 0, 1, 0], [2, 1, 0, 3]]

	# key generation
	__UpperCAmelCase							:   int							=  apply_table(key, paa_table)
	__UpperCAmelCase							:   Dict							=  temp[:5]
	__UpperCAmelCase							:   Optional[int]							=  temp[5:]
	__UpperCAmelCase							:   Optional[int]							=  left_shift(left)
	__UpperCAmelCase							:   Union[str, Any]							=  left_shift(right)
	__UpperCAmelCase							:   int							=  apply_table(left + right, pa_table)
	__UpperCAmelCase							:   Tuple							=  left_shift(left)
	__UpperCAmelCase							:   Union[str, Any]							=  left_shift(right)
	__UpperCAmelCase							:   Dict							=  left_shift(left)
	__UpperCAmelCase							:   Optional[Any]							=  left_shift(right)
	__UpperCAmelCase							:   Optional[int]							=  apply_table(left + right, pa_table)

	# encryption
	__UpperCAmelCase							:   Tuple							=  apply_table(message, IP)
	__UpperCAmelCase							:   Tuple							=  function(expansion, sa, sa, keya, temp)
	__UpperCAmelCase							:   List[Any]							=  temp[4:] + temp[:4]
	__UpperCAmelCase							:   int							=  function(expansion, sa, sa, keya, temp)
	__UpperCAmelCase							:   Union[str, Any]							=  apply_table(temp, IP_inv)
	print("Cipher text is:", CT)

	# decryption
	__UpperCAmelCase							:   List[Any]							=  apply_table(CT, IP)
	__UpperCAmelCase							:   List[Any]							=  function(expansion, sa, sa, keya, temp)
	__UpperCAmelCase							:   int							=  temp[4:] + temp[:4]
	__UpperCAmelCase							:   Union[str, Any]							=  function(expansion, sa, sa, keya, temp)
	__UpperCAmelCase							:   Union[str, Any]							=  apply_table(temp, IP_inv)
	print("Plain text after decypting is:", PT)