






import unittest

from transformers import AutoTokenizer, FalconConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
		import torch

		from transformers import (
		    FalconForCausalLM,
		    FalconForQuestionAnswering,
		    FalconForSequenceClassification,
		    FalconForTokenClassification,
		    FalconModel,
		)



class   lowerCamelCase				:







		'''simple docstring'''



		def __init__(							self    ,		_UpperCamelCase    ,		_UpperCamelCase=3    ,		_UpperCamelCase=7    ,		_UpperCamelCase=True    ,		_UpperCamelCase=True    ,		_UpperCamelCase=False    ,		_UpperCamelCase=True    ,		_UpperCamelCase=9_9    ,		_UpperCamelCase=3_2    ,		_UpperCamelCase=5    ,		_UpperCamelCase=4    ,		_UpperCamelCase=3_7    ,		_UpperCamelCase="gelu"    ,		_UpperCamelCase=0.1    ,		_UpperCamelCase=0.1    ,		_UpperCamelCase=5_1_2    ,		_UpperCamelCase=1_6    ,		_UpperCamelCase=2    ,		_UpperCamelCase=0.02    ,		_UpperCamelCase=3    ,		_UpperCamelCase=4    ,		_UpperCamelCase=None    ,		)     ->							int:
							UpperCAmelCase_   :				Optional[Any]						=						parent
							UpperCAmelCase_   :				Dict						=						batch_size
							UpperCAmelCase_   :				Tuple						=						seq_length
							UpperCAmelCase_   :				List[Any]						=						is_training
							UpperCAmelCase_   :				Tuple						=						use_input_mask
							UpperCAmelCase_   :				Tuple						=						use_token_type_ids
							UpperCAmelCase_   :				List[Any]						=						use_labels
							UpperCAmelCase_   :				Any						=						vocab_size
							UpperCAmelCase_   :				Optional[Any]						=						hidden_size
							UpperCAmelCase_   :				Tuple						=						num_hidden_layers
							UpperCAmelCase_   :				Optional[int]						=						num_attention_heads
							UpperCAmelCase_   :				Union[str, Any]						=						intermediate_size
							UpperCAmelCase_   :				int						=						hidden_act
							UpperCAmelCase_   :				str						=						hidden_dropout_prob
							UpperCAmelCase_   :				Tuple						=						attention_probs_dropout_prob
							UpperCAmelCase_   :				str						=						max_position_embeddings
							UpperCAmelCase_   :				Optional[int]						=						type_vocab_size
							UpperCAmelCase_   :				Union[str, Any]						=						type_sequence_label_size
							UpperCAmelCase_   :				Optional[Any]						=						initializer_range
							UpperCAmelCase_   :				Tuple						=						num_labels
							UpperCAmelCase_   :				Optional[int]						=						num_choices
							UpperCAmelCase_   :				List[Any]						=						scope



		def  __UpperCAmelCase      (							self       )     ->							Any:
							UpperCAmelCase_   :				Dict						=						ids_tensor([self.batch_size, self.seq_length]    ,		self.vocab_size       )

							UpperCAmelCase_   :				int						=						None
							if self.use_input_mask:
												UpperCAmelCase_   :				Dict						=						random_attention_mask([self.batch_size, self.seq_length]       )

							UpperCAmelCase_   :				Any						=						None

							UpperCAmelCase_   :				str						=						None
							UpperCAmelCase_   :				Optional[Any]						=						None
							UpperCAmelCase_   :				Dict						=						None
							if self.use_labels:
												UpperCAmelCase_   :				Union[str, Any]						=						ids_tensor([self.batch_size]    ,		self.type_sequence_label_size       )
												UpperCAmelCase_   :				List[Any]						=						ids_tensor([self.batch_size, self.seq_length]    ,		self.num_labels       )
												UpperCAmelCase_   :				Dict						=						ids_tensor([self.batch_size]    ,		self.num_choices       )

							UpperCAmelCase_   :				Union[str, Any]						=						self.get_config()

							return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels



		def  __UpperCAmelCase      (							self       )     ->							Optional[int]:
							return FalconConfig(
							    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    ,		pad_token_id=1    ,		new_decoder_architecture=_UpperCamelCase    ,		)



		def  __UpperCAmelCase      (							self    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase       )     ->							List[Any]:
							UpperCAmelCase_   :				List[Any]						=						FalconModel(config=_UpperCamelCase       )
							model.to(_UpperCamelCase       )
							model.eval()
							UpperCAmelCase_   :				List[str]						=						model(_UpperCamelCase    ,		attention_mask=_UpperCamelCase       )
							UpperCAmelCase_   :				int						=						model(_UpperCamelCase       )
							self.parent.assertEqual(result.last_hidden_state.shape    ,		(self.batch_size, self.seq_length, self.hidden_size)       )



		def  __UpperCAmelCase      (							self    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		)     ->							str:
							UpperCAmelCase_   :				Dict						=						True
							UpperCAmelCase_   :				Dict						=						FalconModel(_UpperCamelCase       )
							model.to(_UpperCamelCase       )
							model.eval()
							UpperCAmelCase_   :				Dict						=						model(
							    _UpperCamelCase    ,		attention_mask=_UpperCamelCase    ,		encoder_hidden_states=_UpperCamelCase    ,		encoder_attention_mask=_UpperCamelCase    ,		)
							UpperCAmelCase_   :				Optional[int]						=						model(
							    _UpperCamelCase    ,		attention_mask=_UpperCamelCase    ,		encoder_hidden_states=_UpperCamelCase    ,		)
							UpperCAmelCase_   :				Tuple						=						model(_UpperCamelCase    ,		attention_mask=_UpperCamelCase       )
							self.parent.assertEqual(result.last_hidden_state.shape    ,		(self.batch_size, self.seq_length, self.hidden_size)       )



		def  __UpperCAmelCase      (							self    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		)     ->							int:
							UpperCAmelCase_   :				Tuple						=						FalconForCausalLM(config=_UpperCamelCase       )
							model.to(_UpperCamelCase       )
							model.eval()
							UpperCAmelCase_   :				Optional[int]						=						model(_UpperCamelCase    ,		attention_mask=_UpperCamelCase    ,		labels=_UpperCamelCase       )
							self.parent.assertEqual(result.logits.shape    ,		(self.batch_size, self.seq_length, self.vocab_size)       )



		def  __UpperCAmelCase      (							self    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		)     ->							str:
							UpperCAmelCase_   :				str						=						True
							UpperCAmelCase_   :				Any						=						True
							UpperCAmelCase_   :				Dict						=						FalconForCausalLM(config=_UpperCamelCase       )
							model.to(_UpperCamelCase       )
							model.eval()

							# first forward pass
							UpperCAmelCase_   :				List[Any]						=						model(
							    _UpperCamelCase    ,		attention_mask=_UpperCamelCase    ,		encoder_hidden_states=_UpperCamelCase    ,		encoder_attention_mask=_UpperCamelCase    ,		use_cache=_UpperCamelCase    ,		)
							UpperCAmelCase_   :				int						=						outputs.past_key_values

							# create hypothetical multiple next token and extent to next_input_ids
							UpperCAmelCase_   :				Optional[Any]						=						ids_tensor((self.batch_size, 3)    ,		config.vocab_size       )
							UpperCAmelCase_   :				str						=						ids_tensor((self.batch_size, 3)    ,		vocab_size=2       )

							# append to next input_ids and
							UpperCAmelCase_   :				Optional[Any]						=						torch.cat([input_ids, next_tokens]    ,		dim=-1       )
							UpperCAmelCase_   :				Tuple						=						torch.cat([input_mask, next_mask]    ,		dim=-1       )

							UpperCAmelCase_   :				Union[str, Any]						=						model(
							    _UpperCamelCase    ,		attention_mask=_UpperCamelCase    ,		encoder_hidden_states=_UpperCamelCase    ,		encoder_attention_mask=_UpperCamelCase    ,		output_hidden_states=_UpperCamelCase    ,		)['hidden_states'][0]
							UpperCAmelCase_   :				str						=						model(
							    _UpperCamelCase    ,		attention_mask=_UpperCamelCase    ,		encoder_hidden_states=_UpperCamelCase    ,		encoder_attention_mask=_UpperCamelCase    ,		past_key_values=_UpperCamelCase    ,		output_hidden_states=_UpperCamelCase    ,		)['hidden_states'][0]

							# select random slice
							UpperCAmelCase_   :				str						=						ids_tensor((1,)    ,		output_from_past.shape[-1]       ).item()
							UpperCAmelCase_   :				Dict						=						output_from_no_past[:, -3:, random_slice_idx].detach()
							UpperCAmelCase_   :				Union[str, Any]						=						output_from_past[:, :, random_slice_idx].detach()

							self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1]       )

							# test that outputs are equal for slice
							self.parent.assertTrue(torch.allclose(_UpperCamelCase    ,		_UpperCamelCase    ,		atol=1E-3       )       )



		def  __UpperCAmelCase      (							self       )     ->							Optional[int]:
							UpperCAmelCase_   :				Tuple						=						self.prepare_config_and_inputs()
							(
							    UpperCAmelCase_  
							)   :				Dict						=						config_and_inputs
							UpperCAmelCase_   :				List[Any]						=						{'input_ids': input_ids, 'attention_mask': input_mask}
							return config, inputs_dict





@require_torch
class   lowerCamelCase				(_snake_case			,  _snake_case			,  _snake_case			,  unittest.TestCase      ):







		'''simple docstring'''



		_snake_case   :   List[Any]							=  (
		    (
		        FalconModel,
		        FalconForCausalLM,
		        FalconForSequenceClassification,
		        FalconForTokenClassification,
		        FalconForQuestionAnswering,
		    )
		    if is_torch_available()
		    else ()
		)
		_snake_case   :   Tuple							=  (FalconForCausalLM,) if is_torch_available() else ()
		_snake_case   :   Union[str, Any]							=  (
		    {
		        '''feature-extraction''': FalconModel,
		        '''text-classification''': FalconForSequenceClassification,
		        '''text-generation''': FalconForCausalLM,
		        '''question-answering''': FalconForQuestionAnswering,
		        '''token-classification''': FalconForTokenClassification,
		        '''zero-shot''': FalconForSequenceClassification,
		    }
		    if is_torch_available()
		    else {}
		)
		_snake_case   :   str							=  False
		_snake_case   :   str							=  False



		def  __UpperCAmelCase      (							self       )     ->							Tuple:
							UpperCAmelCase_   :				Optional[Any]						=						FalconModelTester(self       )
							UpperCAmelCase_   :				Union[str, Any]						=						ConfigTester(self    ,		config_class=_UpperCamelCase    ,		hidden_size=3_7       )



		def  __UpperCAmelCase      (							self       )     ->							List[str]:
							self.config_tester.run_common_tests()



		def  __UpperCAmelCase      (							self       )     ->							Dict:
							UpperCAmelCase_   :				int						=						self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*_UpperCamelCase       )



		def  __UpperCAmelCase      (							self       )     ->							Optional[int]:
							UpperCAmelCase_   :				List[str]						=						self.model_tester.prepare_config_and_inputs()
							for alibi in [True, False]:
												UpperCAmelCase_   :				Optional[Any]						=						alibi
												self.model_tester.create_and_check_model(_UpperCamelCase    ,		*_UpperCamelCase       )



		def  __UpperCAmelCase      (							self       )     ->							Optional[Any]:
							UpperCAmelCase_   :				str						=						self.model_tester.prepare_config_and_inputs_for_common()
							UpperCAmelCase_   :				str						=						3
							UpperCAmelCase_   :				Dict						=						input_dict['input_ids']
							UpperCAmelCase_   :				Optional[Any]						=						input_ids.ne(1       ).to(_UpperCamelCase       )
							UpperCAmelCase_   :				Optional[Any]						=						ids_tensor([self.model_tester.batch_size]    ,		self.model_tester.type_sequence_label_size       )
							UpperCAmelCase_   :				Optional[Any]						=						FalconForSequenceClassification(_UpperCamelCase       )
							model.to(_UpperCamelCase       )
							model.eval()
							UpperCAmelCase_   :				Any						=						model(_UpperCamelCase    ,		attention_mask=_UpperCamelCase    ,		labels=_UpperCamelCase       )
							self.assertEqual(result.logits.shape    ,		(self.model_tester.batch_size, self.model_tester.num_labels)       )



		def  __UpperCAmelCase      (							self       )     ->							Dict:
							UpperCAmelCase_   :				Dict						=						self.model_tester.prepare_config_and_inputs_for_common()
							UpperCAmelCase_   :				Dict						=						3
							UpperCAmelCase_   :				List[Any]						=						'single_label_classification'
							UpperCAmelCase_   :				Optional[Any]						=						input_dict['input_ids']
							UpperCAmelCase_   :				Dict						=						input_ids.ne(1       ).to(_UpperCamelCase       )
							UpperCAmelCase_   :				str						=						ids_tensor([self.model_tester.batch_size]    ,		self.model_tester.type_sequence_label_size       )
							UpperCAmelCase_   :				int						=						FalconForSequenceClassification(_UpperCamelCase       )
							model.to(_UpperCamelCase       )
							model.eval()
							UpperCAmelCase_   :				Optional[int]						=						model(_UpperCamelCase    ,		attention_mask=_UpperCamelCase    ,		labels=_UpperCamelCase       )
							self.assertEqual(result.logits.shape    ,		(self.model_tester.batch_size, self.model_tester.num_labels)       )



		def  __UpperCAmelCase      (							self       )     ->							Dict:
							UpperCAmelCase_   :				Optional[int]						=						self.model_tester.prepare_config_and_inputs_for_common()
							UpperCAmelCase_   :				Union[str, Any]						=						input_dict['input_ids']
							UpperCAmelCase_   :				List[str]						=						FalconForCausalLM(_UpperCamelCase       )
							model.to(_UpperCamelCase       )
							model.eval()
							UpperCAmelCase_   :				List[Any]						=						model(_UpperCamelCase    ,		use_cache=_UpperCamelCase       )
							UpperCAmelCase_   :				Tuple						=						input_ids.shape[0]
							UpperCAmelCase_   :				Tuple						=						model._convert_to_rw_cache(result.past_key_values       )
							UpperCAmelCase_   :				int						=						model._convert_cache_to_standard_format(_UpperCamelCase    ,		_UpperCamelCase       )
							for layer in range(len(_UpperCamelCase       )       ):
												for tensor_idx in range(2       ):
																	self.assertTrue(rw_cache[layer][tensor_idx].ndim == 3       )
																	self.assertTrue(result.past_key_values[layer][tensor_idx].ndim == 4       )
																	self.assertTrue(
																	    torch.all(result.past_key_values[layer][tensor_idx] == standard_cache[layer][tensor_idx]       )       )



		def  __UpperCAmelCase      (							self       )     ->							int:
							UpperCAmelCase_   :				Union[str, Any]						=						self.model_tester.prepare_config_and_inputs_for_common()
							UpperCAmelCase_   :				Tuple						=						3
							UpperCAmelCase_   :				int						=						'multi_label_classification'
							UpperCAmelCase_   :				Union[str, Any]						=						input_dict['input_ids']
							UpperCAmelCase_   :				Optional[int]						=						input_ids.ne(1       ).to(_UpperCamelCase       )
							UpperCAmelCase_   :				Tuple						=						ids_tensor(
							    [self.model_tester.batch_size, config.num_labels]    ,		self.model_tester.type_sequence_label_size       ).to(torch.float       )
							UpperCAmelCase_   :				str						=						FalconForSequenceClassification(_UpperCamelCase       )
							model.to(_UpperCamelCase       )
							model.eval()
							UpperCAmelCase_   :				Tuple						=						model(_UpperCamelCase    ,		attention_mask=_UpperCamelCase    ,		labels=_UpperCamelCase       )
							self.assertEqual(result.logits.shape    ,		(self.model_tester.batch_size, self.model_tester.num_labels)       )



		def  __UpperCAmelCase      (							self       )     ->							List[Any]:
							# Falcon can have different numbers of KV-heads than the number of query heads, so we need
							# to override this test to use the right head counts.
							for model_class in self.all_generative_model_classes:
												UpperCAmelCase_   :				int						=						self.model_tester.prepare_config_and_inputs_for_common()

												# If it doesn't support cache, pass the test
												if not hasattr(_UpperCamelCase    ,		'use_cache'       ):
																	return

												UpperCAmelCase_   :				Optional[Any]						=						model_class(_UpperCamelCase       ).to(_UpperCamelCase       )
												if "use_cache" not in inputs:
																	UpperCAmelCase_   :				Dict						=						True
												UpperCAmelCase_   :				List[str]						=						model(**_UpperCamelCase       )

												# If "past_key_values" is not returned, pass the test (e.g. RWKV uses a different cache name and format)
												if "past_key_values" not in outputs:
																	return

												UpperCAmelCase_   :				Any						=						(
												    getattr(_UpperCamelCase    ,		'decoder_layers'    ,		_UpperCamelCase       )
												    or getattr(_UpperCamelCase    ,		'num_decoder_layers'    ,		_UpperCamelCase       )
												    or config.num_hidden_layers
												)
												UpperCAmelCase_   :				Optional[int]						=						getattr(_UpperCamelCase    ,		'num_kv_heads'    ,		config.num_attention_heads       )
												UpperCAmelCase_   :				List[str]						=						getattr(_UpperCamelCase    ,		'd_model'    ,		config.hidden_size       )
												UpperCAmelCase_   :				Optional[int]						=						embed_dim // num_attention_heads

												UpperCAmelCase_   :				List[str]						=						outputs['past_key_values']
												self.assertEqual(len(_UpperCamelCase       )    ,		_UpperCamelCase       )

												UpperCAmelCase_   :				Optional[int]						=						inputs['input_ids'].shape
												for i in range(_UpperCamelCase       ):
																	if config.new_decoder_architecture:
																						UpperCAmelCase_   :				Optional[Any]						=						config.num_attention_heads
																	elif config.multi_query:
																						UpperCAmelCase_   :				List[Any]						=						1
																	self.assertEqual(len(past_kv[0]       )    ,		2       )  # K V for the decoder = 2
																	self.assertEqual(
																	    past_kv[i][0].shape    ,		(batch_size, num_attention_heads, seq_length, per_head_embed_dim)       )
																	self.assertEqual(
																	    past_kv[i][1].shape    ,		(batch_size, num_attention_heads, seq_length, per_head_embed_dim)       )





@require_torch
class   lowerCamelCase				(unittest.TestCase      ):







		'''simple docstring'''



		@slow
		def  __UpperCAmelCase      (							self       )     ->							str:
							UpperCAmelCase_   :				List[Any]						=						AutoTokenizer.from_pretrained('Rocketknight1/falcon-rw-1b'       )
							UpperCAmelCase_   :				List[Any]						=						FalconForCausalLM.from_pretrained('Rocketknight1/falcon-rw-1b'       )
							model.eval()
							model.to(_UpperCamelCase       )
							UpperCAmelCase_   :				str						=						tokenizer('My favorite food is'    ,		return_tensors='pt'       ).to(_UpperCamelCase       )

							UpperCAmelCase_   :				Dict						=						(
							    'My favorite food is pizza. I love it so much that I have a pizza party every year for my birthday.'
							)

							UpperCAmelCase_   :				Dict						=						model.generate(**_UpperCamelCase    ,		do_sample=_UpperCamelCase    ,		max_new_tokens=1_9       )
							UpperCAmelCase_   :				Optional[int]						=						tokenizer.batch_decode(_UpperCamelCase       )[0]

							self.assertEqual(_UpperCamelCase    ,		_UpperCamelCase       )



		@slow
		def  __UpperCAmelCase      (							self       )     ->							Optional[Any]:
							# The big models are way too big for the CI, so we use tiny random models that resemble their
							# architectures but with much smaller and fewer layers
							for repo in ["Rocketknight1/tiny-random-falcon-7b", "Rocketknight1/tiny-random-falcon-40b"]:
												UpperCAmelCase_   :				int						=						AutoTokenizer.from_pretrained(_UpperCamelCase       )
												UpperCAmelCase_   :				Dict						=						FalconForCausalLM.from_pretrained(_UpperCamelCase       )
												model.eval()
												model.to(_UpperCamelCase       )
												UpperCAmelCase_   :				List[Any]						=						tokenizer('My favorite food is'    ,		return_tensors='pt'       ).to(_UpperCamelCase       )

												# We just test that these run without errors - the models are randomly initialized
												# and so the actual text outputs will be garbage
												model.generate(**_UpperCamelCase    ,		do_sample=_UpperCamelCase    ,		max_new_tokens=4       )
												model.generate(**_UpperCamelCase    ,		do_sample=_UpperCamelCase    ,		max_new_tokens=4       )
												model.generate(**_UpperCamelCase    ,		num_beams=2    ,		max_new_tokens=4       )



		@slow
		def  __UpperCAmelCase      (							self       )     ->							Union[str, Any]:
							# The big models are way too big for the CI, so we use tiny random models that resemble their
							# architectures but with much smaller and fewer layers
							with torch.no_grad():
												for repo in [
												    "Rocketknight1/falcon-rw-1b",
												    "Rocketknight1/tiny-random-falcon-7b",
												    "Rocketknight1/tiny-random-falcon-40b",
												]:
																	UpperCAmelCase_   :				Any						=						AutoTokenizer.from_pretrained(_UpperCamelCase       )
																	UpperCAmelCase_   :				List[Any]						=						FalconForCausalLM.from_pretrained(_UpperCamelCase       )
																	model.eval()
																	model.to(device=_UpperCamelCase       )
																	UpperCAmelCase_   :				List[Any]						=						tokenizer('My favorite food is'    ,		return_tensors='pt'       ).to(_UpperCamelCase       )

																	# Test results are the same with and without cache
																	UpperCAmelCase_   :				Union[str, Any]						=						model.generate(**_UpperCamelCase    ,		do_sample=_UpperCamelCase    ,		max_new_tokens=2_0    ,		use_cache=_UpperCamelCase       )
																	UpperCAmelCase_   :				str						=						model.generate(**_UpperCamelCase    ,		do_sample=_UpperCamelCase    ,		max_new_tokens=2_0    ,		use_cache=_UpperCamelCase       )
																	self.assertTrue((outputs_cache - outputs_no_cache).sum().item() == 0       )














def 					lowercase__				(					__snake_case   :							Tuple					,	__snake_case   :							List[Any]  ):


					'''simple docstring'''


					UpperCAmelCase_   :				Any						=						''
					for i in table:
										res += inp[i - 1]
					return res




def 					lowercase__				(					__snake_case   :							Optional[int]  ):


					'''simple docstring'''


					return data[1:] + data[0]




def 					lowercase__				(					__snake_case   :							Union[str, Any]					,	__snake_case   :							Optional[int]  ):


					'''simple docstring'''


					UpperCAmelCase_   :				Any						=						''
					for i in range(len(__snake_case  )  ):
										if a[i] == b[i]:
															res += "0"
										else:
															res += "1"
					return res




def 					lowercase__				(					__snake_case   :							Dict					,	__snake_case   :							Optional[int]  ):


					'''simple docstring'''


					UpperCAmelCase_   :				Optional[Any]						=						int('0b' + data[0] + data[-1]					,	2  )
					UpperCAmelCase_   :				Any						=						int('0b' + data[1:3]					,	2  )
					return bin(s[row][col]  )[2:]




def 					lowercase__				(					__snake_case   :							Tuple					,	__snake_case   :							List[Any]					,	__snake_case   :							List[Any]					,	__snake_case   :							Optional[int]					,	__snake_case   :							str  ):


					'''simple docstring'''


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


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

		__UpperCAmelCase     							=						[6, 3, 7, 4, 8, 5, 10, 9]
		__UpperCAmelCase     							=						[3, 5, 2, 7, 4, 10, 1, 9, 8, 6]
		__UpperCAmelCase     							=						[2, 4, 3, 1]
		__UpperCAmelCase     							=						[2, 6, 3, 1, 4, 8, 5, 7]
		__UpperCAmelCase     							=						[4, 1, 3, 5, 7, 2, 8, 6]
		__UpperCAmelCase     							=						[4, 1, 2, 3, 2, 3, 4, 1]
		__UpperCAmelCase     							=						[[1, 0, 3, 2], [3, 2, 1, 0], [0, 2, 1, 3], [3, 1, 3, 2]]
		__UpperCAmelCase     							=						[[0, 1, 2, 3], [2, 0, 1, 3], [3, 0, 1, 0], [2, 1, 0, 3]]

		# key generation
		__UpperCAmelCase     							=						apply_table(key, paa_table)
		__UpperCAmelCase     							=						temp[:5]
		__UpperCAmelCase     							=						temp[5:]
		__UpperCAmelCase     							=						left_shift(left)
		__UpperCAmelCase     							=						left_shift(right)
		__UpperCAmelCase     							=						apply_table(left + right, pa_table)
		__UpperCAmelCase     							=						left_shift(left)
		__UpperCAmelCase     							=						left_shift(right)
		__UpperCAmelCase     							=						left_shift(left)
		__UpperCAmelCase     							=						left_shift(right)
		__UpperCAmelCase     							=						apply_table(left + right, pa_table)

		# encryption
		__UpperCAmelCase     							=						apply_table(message, IP)
		__UpperCAmelCase     							=						function(expansion, sa, sa, keya, temp)
		__UpperCAmelCase     							=						temp[4:] + temp[:4]
		__UpperCAmelCase     							=						function(expansion, sa, sa, keya, temp)
		__UpperCAmelCase     							=						apply_table(temp, IP_inv)
		print('Cipher text is:', CT)

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






