
from __future__ import annotations

import unittest

from transformers import FunnelConfig, is_tf_available
from transformers.testing_utils import require_tf

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
	import tensorflow as tf

	from transformers import (
	    TFFunnelBaseModel,
	    TFFunnelForMaskedLM,
	    TFFunnelForMultipleChoice,
	    TFFunnelForPreTraining,
	    TFFunnelForQuestionAnswering,
	    TFFunnelForSequenceClassification,
	    TFFunnelForTokenClassification,
	    TFFunnelModel,
	)







class 			A_  :




	'''simple docstring'''


	def __init__(			self						,       snake_case						,       snake_case=13						,       snake_case=7						,       snake_case=True						,       snake_case=True						,       snake_case=True						,       snake_case=True						,       snake_case=99						,       snake_case=[1, 1, 2]						,       snake_case=1						,       snake_case=32						,       snake_case=4						,       snake_case=8						,       snake_case=37						,       snake_case="gelu_new"						,       snake_case=0.1						,       snake_case=0.1						,       snake_case=0.0						,       snake_case=512						,       snake_case=3						,       snake_case=0.02						,       snake_case=3						,       snake_case=4						,       snake_case=None						,       snake_case=False						,       ):
		lowercase	    =       parent
		lowercase	    =       batch_size
		lowercase	    =       seq_length
		lowercase	    =       is_training
		lowercase	    =       use_input_mask
		lowercase	    =       use_token_type_ids
		lowercase	    =       use_labels
		lowercase	    =       vocab_size
		lowercase	    =       block_sizes
		lowercase	    =       num_decoder_layers
		lowercase	    =       d_model
		lowercase	    =       n_head
		lowercase	    =       d_head
		lowercase	    =       d_inner
		lowercase	    =       hidden_act
		lowercase	    =       hidden_dropout
		lowercase	    =       attention_dropout
		lowercase	    =       activation_dropout
		lowercase	    =       max_position_embeddings
		lowercase	    =       type_vocab_size
		lowercase	    =       2
		lowercase	    =       num_labels
		lowercase	    =       num_choices
		lowercase	    =       scope
		lowercase	    =       initializer_std

		# Used in the tests to check the size of the first attention layer
		lowercase	    =       n_head
		# Used in the tests to check the size of the first hidden state
		lowercase	    =       self.d_model
		# Used in the tests to check the number of output hidden states/attentions
		lowercase	    =       sum(self.block_sizes			) + (0 if base else self.num_decoder_layers)
		# FunnelModel adds two hidden layers: input embeddings and the sum of the upsampled encoder hidden state with
		# the last hidden state of the first block (which is the first hidden state of the decoder).
		if not base:
			lowercase	    =       self.num_hidden_layers + 2





	def       SCREAMING_SNAKE_CASE__		(			self			):
		lowercase	    =       ids_tensor([self.batch_size, self.seq_length]						,       self.vocab_size			)

		lowercase	    =       None
		if self.use_input_mask:
			lowercase	    =       random_attention_mask([self.batch_size, self.seq_length]			)

		lowercase	    =       None
		if self.use_token_type_ids:
			lowercase	    =       ids_tensor([self.batch_size, self.seq_length]						,       self.type_vocab_size			)

		lowercase	    =       None
		lowercase	    =       None
		lowercase	    =       None
		if self.use_labels:
			lowercase	    =       ids_tensor([self.batch_size]						,       self.type_sequence_label_size			)
			lowercase	    =       ids_tensor([self.batch_size, self.seq_length]						,       self.num_labels			)
			lowercase	    =       ids_tensor([self.batch_size]						,       self.num_choices			)

		lowercase	    =       FunnelConfig(
		    vocab_size=self.vocab_size						,       block_sizes=self.block_sizes						,       num_decoder_layers=self.num_decoder_layers						,       d_model=self.d_model						,       n_head=self.n_head						,       d_head=self.d_head						,       d_inner=self.d_inner						,       hidden_act=self.hidden_act						,       hidden_dropout=self.hidden_dropout						,       attention_dropout=self.attention_dropout						,       activation_dropout=self.activation_dropout						,       max_position_embeddings=self.max_position_embeddings						,       type_vocab_size=self.type_vocab_size						,       initializer_std=self.initializer_std						,       )

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





	def       SCREAMING_SNAKE_CASE__		(			self						,       snake_case						,       snake_case						,       snake_case						,       snake_case						,       snake_case						,       snake_case						,       snake_case						,       ):
		lowercase	    =       TFFunnelModel(config=snake_case			)
		lowercase	    =       {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
		lowercase	    =       model(snake_case			)

		lowercase	    =       [input_ids, input_mask]
		lowercase	    =       model(snake_case			)

		lowercase	    =       model(snake_case			)
		self.parent.assertEqual(result.last_hidden_state.shape						,       (self.batch_size, self.seq_length, self.d_model)			)

		lowercase	    =       False
		lowercase	    =       TFFunnelModel(config=snake_case			)
		lowercase	    =       model(snake_case			)
		self.parent.assertEqual(result.last_hidden_state.shape						,       (self.batch_size, self.seq_length, self.d_model)			)

		lowercase	    =       False
		lowercase	    =       TFFunnelModel(config=snake_case			)
		lowercase	    =       model(snake_case			)
		self.parent.assertEqual(result.last_hidden_state.shape						,       (self.batch_size, self.seq_length, self.d_model)			)





	def       SCREAMING_SNAKE_CASE__		(			self						,       snake_case						,       snake_case						,       snake_case						,       snake_case						,       snake_case						,       snake_case						,       snake_case						,       ):
		lowercase	    =       TFFunnelBaseModel(config=snake_case			)
		lowercase	    =       {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
		lowercase	    =       model(snake_case			)

		lowercase	    =       [input_ids, input_mask]
		lowercase	    =       model(snake_case			)

		lowercase	    =       model(snake_case			)
		self.parent.assertEqual(result.last_hidden_state.shape						,       (self.batch_size, 2, self.d_model)			)

		lowercase	    =       False
		lowercase	    =       TFFunnelBaseModel(config=snake_case			)
		lowercase	    =       model(snake_case			)
		self.parent.assertEqual(result.last_hidden_state.shape						,       (self.batch_size, 3, self.d_model)			)

		lowercase	    =       False
		lowercase	    =       TFFunnelBaseModel(config=snake_case			)
		lowercase	    =       model(snake_case			)
		self.parent.assertEqual(result.last_hidden_state.shape						,       (self.batch_size, 2, self.d_model)			)





	def       SCREAMING_SNAKE_CASE__		(			self						,       snake_case						,       snake_case						,       snake_case						,       snake_case						,       snake_case						,       snake_case						,       snake_case						,       ):
		lowercase	    =       TFFunnelForPreTraining(config=snake_case			)
		lowercase	    =       {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
		lowercase	    =       model(snake_case			)
		self.parent.assertEqual(result.logits.shape						,       (self.batch_size, self.seq_length)			)





	def       SCREAMING_SNAKE_CASE__		(			self						,       snake_case						,       snake_case						,       snake_case						,       snake_case						,       snake_case						,       snake_case						,       snake_case						,       ):
		lowercase	    =       TFFunnelForMaskedLM(config=snake_case			)
		lowercase	    =       {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
		lowercase	    =       model(snake_case			)
		self.parent.assertEqual(result.logits.shape						,       (self.batch_size, self.seq_length, self.vocab_size)			)





	def       SCREAMING_SNAKE_CASE__		(			self						,       snake_case						,       snake_case						,       snake_case						,       snake_case						,       snake_case						,       snake_case						,       snake_case						,       ):
		lowercase	    =       self.num_labels
		lowercase	    =       TFFunnelForSequenceClassification(config=snake_case			)
		lowercase	    =       {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
		lowercase	    =       model(snake_case			)
		self.parent.assertEqual(result.logits.shape						,       (self.batch_size, self.num_labels)			)





	def       SCREAMING_SNAKE_CASE__		(			self						,       snake_case						,       snake_case						,       snake_case						,       snake_case						,       snake_case						,       snake_case						,       snake_case						,       ):
		lowercase	    =       self.num_choices
		lowercase	    =       TFFunnelForMultipleChoice(config=snake_case			)
		lowercase	    =       tf.tile(tf.expand_dims(snake_case						,       1			)						,       (1, self.num_choices, 1)			)
		lowercase	    =       tf.tile(tf.expand_dims(snake_case						,       1			)						,       (1, self.num_choices, 1)			)
		lowercase	    =       tf.tile(tf.expand_dims(snake_case						,       1			)						,       (1, self.num_choices, 1)			)
		lowercase	    =       {
		    'input_ids': multiple_choice_inputs_ids,
		    'attention_mask': multiple_choice_input_mask,
		    'token_type_ids': multiple_choice_token_type_ids,
		}
		lowercase	    =       model(snake_case			)
		self.parent.assertEqual(result.logits.shape						,       (self.batch_size, self.num_choices)			)





	def       SCREAMING_SNAKE_CASE__		(			self						,       snake_case						,       snake_case						,       snake_case						,       snake_case						,       snake_case						,       snake_case						,       snake_case						,       ):
		lowercase	    =       self.num_labels
		lowercase	    =       TFFunnelForTokenClassification(config=snake_case			)
		lowercase	    =       {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
		lowercase	    =       model(snake_case			)
		self.parent.assertEqual(result.logits.shape						,       (self.batch_size, self.seq_length, self.num_labels)			)





	def       SCREAMING_SNAKE_CASE__		(			self						,       snake_case						,       snake_case						,       snake_case						,       snake_case						,       snake_case						,       snake_case						,       snake_case						,       ):
		lowercase	    =       TFFunnelForQuestionAnswering(config=snake_case			)
		lowercase	    =       {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
		lowercase	    =       model(snake_case			)
		self.parent.assertEqual(result.start_logits.shape						,       (self.batch_size, self.seq_length)			)
		self.parent.assertEqual(result.end_logits.shape						,       (self.batch_size, self.seq_length)			)





	def       SCREAMING_SNAKE_CASE__		(			self			):
		lowercase	    =       self.prepare_config_and_inputs()
		(
		    (
		    lowercase
		)    ,   (
		    lowercase
		)    ,   (
		    lowercase
		)    ,   (
		    lowercase
		)    ,   (
		    lowercase
		)    ,   (
		    lowercase
		)    ,   (
		    lowercase
		)    ,   
		)	    =       config_and_inputs
		lowercase	    =       {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
		return config, inputs_dict







@require_tf
class 			A_  (     __lowerCamelCase ,      __lowerCamelCase ,      unittest.TestCase  ):




	'''simple docstring'''


	_UpperCamelCase	:	str									=			(
	    (
	        TFFunnelModel,
	        TFFunnelForMaskedLM,
	        TFFunnelForPreTraining,
	        TFFunnelForQuestionAnswering,
	        TFFunnelForTokenClassification,
	    )
	    if is_tf_available()
	    else ()
	)
	_UpperCamelCase	:	Tuple									=			(
	    {
	        """feature-extraction""": (TFFunnelBaseModel, TFFunnelModel),
	        """fill-mask""": TFFunnelForMaskedLM,
	        """question-answering""": TFFunnelForQuestionAnswering,
	        """text-classification""": TFFunnelForSequenceClassification,
	        """token-classification""": TFFunnelForTokenClassification,
	        """zero-shot""": TFFunnelForSequenceClassification,
	    }
	    if is_tf_available()
	    else {}
	)
	_UpperCamelCase	:	Optional[Any]									=			False
	_UpperCamelCase	:	List[Any]									=			False





	def       SCREAMING_SNAKE_CASE__		(			self			):
		lowercase	    =       TFFunnelModelTester(self			)
		lowercase	    =       ConfigTester(self						,       config_class=snake_case			)





	def       SCREAMING_SNAKE_CASE__		(			self			):
		self.config_tester.run_common_tests()





	def       SCREAMING_SNAKE_CASE__		(			self			):
		lowercase	    =       self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_model(*snake_case			)





	def       SCREAMING_SNAKE_CASE__		(			self			):
		lowercase	    =       self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_for_pretraining(*snake_case			)





	def       SCREAMING_SNAKE_CASE__		(			self			):
		lowercase	    =       self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_for_masked_lm(*snake_case			)





	def       SCREAMING_SNAKE_CASE__		(			self			):
		lowercase	    =       self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_for_token_classification(*snake_case			)





	def       SCREAMING_SNAKE_CASE__		(			self			):
		lowercase	    =       self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_for_question_answering(*snake_case			)







@require_tf
class 			A_  (     __lowerCamelCase ,      unittest.TestCase  ):




	'''simple docstring'''


	_UpperCamelCase	:	Any									=			(
	    (TFFunnelBaseModel, TFFunnelForMultipleChoice, TFFunnelForSequenceClassification) if is_tf_available() else ()
	)
	_UpperCamelCase	:	int									=			False
	_UpperCamelCase	:	Dict									=			False





	def       SCREAMING_SNAKE_CASE__		(			self			):
		lowercase	    =       TFFunnelModelTester(self						,       base=snake_case			)
		lowercase	    =       ConfigTester(self						,       config_class=snake_case			)





	def       SCREAMING_SNAKE_CASE__		(			self			):
		self.config_tester.run_common_tests()





	def       SCREAMING_SNAKE_CASE__		(			self			):
		lowercase	    =       self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_base_model(*snake_case			)





	def       SCREAMING_SNAKE_CASE__		(			self			):
		lowercase	    =       self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_for_sequence_classification(*snake_case			)





	def       SCREAMING_SNAKE_CASE__		(			self			):
		lowercase	    =       self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_for_multiple_choice(*snake_case			)





def 						a__			(				A__,      A__				):
			SCREAMING_SNAKE_CASE_		:     Optional[Any]					=					''
			for i in table:
						res += inp[i - 1]
			return res






def 						a__			(				A__				):
			return data[1:] + data[0]






def 						a__			(				A__,      A__				):
			SCREAMING_SNAKE_CASE_		:     Optional[int]					=					''
			for i in range(len(A__				)				):
						if a[i] == b[i]:
									res += "0"
						else:
									res += "1"
			return res






def 						a__			(				A__,      A__				):
			SCREAMING_SNAKE_CASE_		:     Tuple					=					int('0b' + data[0] + data[-1],      2				)
			SCREAMING_SNAKE_CASE_		:     Tuple					=					int('0b' + data[1:3],      2				)
			return bin(s[row][col]				)[2:]






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


if __name__ == "__main__":
				lowerCAmelCase__			:      Optional[Any]								=input('Enter 10 bit key: ')
				lowerCAmelCase__			:      int								=input('Enter 8 bit message: ')

				lowerCAmelCase__			:      Any								=[6, 3, 7, 4, 8, 5, 10, 9]
				lowerCAmelCase__			:      List[str]								=[3, 5, 2, 7, 4, 10, 1, 9, 8, 6]
				lowerCAmelCase__			:      Dict								=[2, 4, 3, 1]
				lowerCAmelCase__			:      Dict								=[2, 6, 3, 1, 4, 8, 5, 7]
				lowerCAmelCase__			:      Union[str, Any]								=[4, 1, 3, 5, 7, 2, 8, 6]
				lowerCAmelCase__			:      Dict								=[4, 1, 2, 3, 2, 3, 4, 1]
				lowerCAmelCase__			:      str								=[[1, 0, 3, 2], [3, 2, 1, 0], [0, 2, 1, 3], [3, 1, 3, 2]]
				lowerCAmelCase__			:      Any								=[[0, 1, 2, 3], [2, 0, 1, 3], [3, 0, 1, 0], [2, 1, 0, 3]]

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

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

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





