



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    SCREAMING_SNAKE_CASE			:

							'''simple docstring'''





							def __init__(   self		:		Union[str, Any]      ,				__lowercase		:		int      ,				__lowercase		:		List[Any]=13      ,				__lowercase		:		str=7      ,				__lowercase		:		Union[str, Any]=True      ,				__lowercase		:		Tuple=True      ,				__lowercase		:		Tuple=True      ,				__lowercase		:		Optional[Any]=True      ,				__lowercase		:		List[Any]=99      ,				__lowercase		:		List[str]=[1, 1, 2]      ,				__lowercase		:		Optional[Any]=1      ,				__lowercase		:		int=32      ,				__lowercase		:		Optional[Any]=4      ,				__lowercase		:		Optional[Any]=8      ,				__lowercase		:		List[str]=37      ,				__lowercase		:		Optional[int]="gelu_new"      ,				__lowercase		:		str=0.1      ,				__lowercase		:		Union[str, Any]=0.1      ,				__lowercase		:		Union[str, Any]=0.0      ,				__lowercase		:		List[Any]=512      ,				__lowercase		:		List[Any]=3      ,				__lowercase		:		Optional[Any]=0.02      ,				__lowercase		:		Optional[int]=3      ,				__lowercase		:		List[str]=4      ,				__lowercase		:		int=None      ,				__lowercase		:		Tuple=False      ,				):

											'''simple docstring'''
											__a       =		parent
											__a       =		batch_size
											__a       =		seq_length
											__a       =		is_training
											__a       =		use_input_mask
											__a       =		use_token_type_ids
											__a       =		use_labels
											__a       =		vocab_size
											__a       =		block_sizes
											__a       =		num_decoder_layers
											__a       =		d_model
											__a       =		n_head
											__a       =		d_head
											__a       =		d_inner
											__a       =		hidden_act
											__a       =		hidden_dropout
											__a       =		attention_dropout
											__a       =		activation_dropout
											__a       =		max_position_embeddings
											__a       =		type_vocab_size
											__a       =		2
											__a       =		num_labels
											__a       =		num_choices
											__a       =		scope
											__a       =		initializer_std

											# Used in the tests to check the size of the first attention layer
											__a       =		n_head
											# Used in the tests to check the size of the first hidden state
											__a       =		self.d_model
											# Used in the tests to check the number of output hidden states/attentions
											__a       =		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:
															__a       =		self.num_hidden_layers + 2





							def 						UpperCamelCase_					(   self		:		List[str]	):

											'''simple docstring'''
											__a       =		ids_tensor([self.batch_size, self.seq_length]      ,				self.vocab_size	)

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

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

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

											__a       =		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 						UpperCamelCase_					(   self		:		Optional[Any]      ,				__lowercase		:		Optional[int]      ,				__lowercase		:		Any      ,				__lowercase		:		Tuple      ,				__lowercase		:		int      ,				__lowercase		:		Dict      ,				__lowercase		:		Union[str, Any]      ,				__lowercase		:		int      ,				):

											'''simple docstring'''
											__a       =		TFFunnelModel(config=__A	)
											__a       =		{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
											__a       =		model(__A	)

											__a       =		[input_ids, input_mask]
											__a       =		model(__A	)

											__a       =		model(__A	)
											self.parent.assertEqual(result.last_hidden_state.shape      ,				(self.batch_size, self.seq_length, self.d_model)	)

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

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





							def 						UpperCamelCase_					(   self		:		Optional[int]      ,				__lowercase		:		Dict      ,				__lowercase		:		List[str]      ,				__lowercase		:		List[Any]      ,				__lowercase		:		Optional[int]      ,				__lowercase		:		Union[str, Any]      ,				__lowercase		:		int      ,				__lowercase		:		List[Any]      ,				):

											'''simple docstring'''
											__a       =		TFFunnelBaseModel(config=__A	)
											__a       =		{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
											__a       =		model(__A	)

											__a       =		[input_ids, input_mask]
											__a       =		model(__A	)

											__a       =		model(__A	)
											self.parent.assertEqual(result.last_hidden_state.shape      ,				(self.batch_size, 2, self.d_model)	)

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

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





							def 						UpperCamelCase_					(   self		:		List[Any]      ,				__lowercase		:		List[str]      ,				__lowercase		:		int      ,				__lowercase		:		Optional[int]      ,				__lowercase		:		Dict      ,				__lowercase		:		Any      ,				__lowercase		:		Optional[Any]      ,				__lowercase		:		Dict      ,				):

											'''simple docstring'''
											__a       =		TFFunnelForPreTraining(config=__A	)
											__a       =		{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
											__a       =		model(__A	)
											self.parent.assertEqual(result.logits.shape      ,				(self.batch_size, self.seq_length)	)





							def 						UpperCamelCase_					(   self		:		str      ,				__lowercase		:		Optional[Any]      ,				__lowercase		:		Optional[int]      ,				__lowercase		:		Optional[Any]      ,				__lowercase		:		Optional[int]      ,				__lowercase		:		Optional[int]      ,				__lowercase		:		Dict      ,				__lowercase		:		Optional[Any]      ,				):

											'''simple docstring'''
											__a       =		TFFunnelForMaskedLM(config=__A	)
											__a       =		{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
											__a       =		model(__A	)
											self.parent.assertEqual(result.logits.shape      ,				(self.batch_size, self.seq_length, self.vocab_size)	)





							def 						UpperCamelCase_					(   self		:		List[str]      ,				__lowercase		:		List[str]      ,				__lowercase		:		Union[str, Any]      ,				__lowercase		:		Optional[Any]      ,				__lowercase		:		List[Any]      ,				__lowercase		:		Union[str, Any]      ,				__lowercase		:		str      ,				__lowercase		:		Tuple      ,				):

											'''simple docstring'''
											__a       =		self.num_labels
											__a       =		TFFunnelForSequenceClassification(config=__A	)
											__a       =		{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
											__a       =		model(__A	)
											self.parent.assertEqual(result.logits.shape      ,				(self.batch_size, self.num_labels)	)





							def 						UpperCamelCase_					(   self		:		Any      ,				__lowercase		:		Optional[Any]      ,				__lowercase		:		List[Any]      ,				__lowercase		:		Any      ,				__lowercase		:		Any      ,				__lowercase		:		Any      ,				__lowercase		:		Dict      ,				__lowercase		:		Optional[Any]      ,				):

											'''simple docstring'''
											__a       =		self.num_choices
											__a       =		TFFunnelForMultipleChoice(config=__A	)
											__a       =		tf.tile(tf.expand_dims(__A      ,				1	)      ,				(1, self.num_choices, 1)	)
											__a       =		tf.tile(tf.expand_dims(__A      ,				1	)      ,				(1, self.num_choices, 1)	)
											__a       =		tf.tile(tf.expand_dims(__A      ,				1	)      ,				(1, self.num_choices, 1)	)
											__a       =		{
											    "input_ids": multiple_choice_inputs_ids,
											    "attention_mask": multiple_choice_input_mask,
											    "token_type_ids": multiple_choice_token_type_ids,
											}
											__a       =		model(__A	)
											self.parent.assertEqual(result.logits.shape      ,				(self.batch_size, self.num_choices)	)





							def 						UpperCamelCase_					(   self		:		Optional[int]      ,				__lowercase		:		int      ,				__lowercase		:		Optional[int]      ,				__lowercase		:		Dict      ,				__lowercase		:		str      ,				__lowercase		:		List[Any]      ,				__lowercase		:		Optional[int]      ,				__lowercase		:		List[str]      ,				):

											'''simple docstring'''
											__a       =		self.num_labels
											__a       =		TFFunnelForTokenClassification(config=__A	)
											__a       =		{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
											__a       =		model(__A	)
											self.parent.assertEqual(result.logits.shape      ,				(self.batch_size, self.seq_length, self.num_labels)	)





							def 						UpperCamelCase_					(   self		:		Optional[int]      ,				__lowercase		:		Tuple      ,				__lowercase		:		List[Any]      ,				__lowercase		:		Union[str, Any]      ,				__lowercase		:		Dict      ,				__lowercase		:		Dict      ,				__lowercase		:		Dict      ,				__lowercase		:		Dict      ,				):

											'''simple docstring'''
											__a       =		TFFunnelForQuestionAnswering(config=__A	)
											__a       =		{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
											__a       =		model(__A	)
											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 						UpperCamelCase_					(   self		:		int	):

											'''simple docstring'''
											__a       =		self.prepare_config_and_inputs()
											(
											    __a		
											)       =		config_and_inputs
											__a       =		{"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
											return config, inputs_dict






@require_tf
class    SCREAMING_SNAKE_CASE			(							UpperCamelCase_			,      UpperCamelCase_			,      unittest.TestCase					):

							'''simple docstring'''





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





							def 						UpperCamelCase_					(   self		:		Dict	):

											'''simple docstring'''
											__a       =		TFFunnelModelTester(self	)
											__a       =		ConfigTester(self      ,				config_class=__A	)





							def 						UpperCamelCase_					(   self		:		Union[str, Any]	):

											'''simple docstring'''
											self.config_tester.run_common_tests()





							def 						UpperCamelCase_					(   self		:		List[str]	):

											'''simple docstring'''
											__a       =		self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*__A	)





							def 						UpperCamelCase_					(   self		:		Optional[Any]	):

											'''simple docstring'''
											__a       =		self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_pretraining(*__A	)





							def 						UpperCamelCase_					(   self		:		int	):

											'''simple docstring'''
											__a       =		self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_masked_lm(*__A	)





							def 						UpperCamelCase_					(   self		:		Tuple	):

											'''simple docstring'''
											__a       =		self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_token_classification(*__A	)







							def 						UpperCamelCase_					(   self		:		Optional[Any]	):

											'''simple docstring'''
											__a       =		self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_question_answering(*__A	)






@require_tf
class    SCREAMING_SNAKE_CASE			(							UpperCamelCase_			,      unittest.TestCase					):

							'''simple docstring'''





							__lowerCamelCase	:	Optional[Any]    =(
							    (TFFunnelBaseModel, TFFunnelForMultipleChoice, TFFunnelForSequenceClassification) if is_tf_available() else ()
							)
							__lowerCamelCase	:	Tuple    =False
							__lowerCamelCase	:	Union[str, Any]    =False





							def 						UpperCamelCase_					(   self		:		Any	):

											'''simple docstring'''
											__a       =		TFFunnelModelTester(self      ,				base=__A	)
											__a       =		ConfigTester(self      ,				config_class=__A	)





							def 						UpperCamelCase_					(   self		:		List[str]	):

											'''simple docstring'''
											self.config_tester.run_common_tests()





							def 						UpperCamelCase_					(   self		:		Optional[Any]	):

											'''simple docstring'''
											__a       =		self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_base_model(*__A	)





							def 						UpperCamelCase_					(   self		:		List[Any]	):

											'''simple docstring'''
											__a       =		self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_sequence_classification(*__A	)







							def 						UpperCamelCase_					(   self		:		Any	):

											'''simple docstring'''
											__a       =		self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_multiple_choice(*__A	)





import itertools
import string
from collections.abc import Generator, Iterable

def   lowerCAmelCase__						(							_SCREAMING_SNAKE_CASE       :			Iterable[str]				,     _SCREAMING_SNAKE_CASE       :			int    ):
				"""simple docstring"""







				__a       =		iter(_SCREAMING_SNAKE_CASE    )
				while True:
								__a       =		tuple(itertools.islice(_SCREAMING_SNAKE_CASE				,     _SCREAMING_SNAKE_CASE    )    )
								if not chunk:
												return
								yield chunk

def   lowerCAmelCase__						(							_SCREAMING_SNAKE_CASE       :			str    ):
				"""simple docstring"""







				__a       =		"""""".join([c.upper() for c in dirty if c in string.ascii_letters]    )
				__a       =		""""""

				if len(_SCREAMING_SNAKE_CASE    ) < 2:
								return dirty

				for i in range(len(_SCREAMING_SNAKE_CASE    ) - 1    ):
								clean += dirty[i]

								if dirty[i] == dirty[i + 1]:
												clean += "X"

				clean += dirty[-1]

				if len(_SCREAMING_SNAKE_CASE    ) & 1:
								clean += "X"

				return clean

def   lowerCAmelCase__						(							_SCREAMING_SNAKE_CASE       :			str    ):
				"""simple docstring"""







				__a       =		"""ABCDEFGHIKLMNOPQRSTUVWXYZ"""
				# we're using a list instead of a '2d' array because it makes the math
				# for setting up the table and doing the actual encoding/decoding simpler
				__a       =		[]

				# copy key chars into the table if they are in `alphabet` ignoring duplicates
				for char in key.upper():
								if char not in table and char in alphabet:
												table.append(_SCREAMING_SNAKE_CASE    )

    # fill the rest of the table in with the remaining alphabet chars
				for char in alphabet:
								if char not in table:
												table.append(_SCREAMING_SNAKE_CASE    )

				return table

def   lowerCAmelCase__						(							_SCREAMING_SNAKE_CASE       :			str				,     _SCREAMING_SNAKE_CASE       :			str    ):
				"""simple docstring"""







				__a       =		generate_table(_SCREAMING_SNAKE_CASE    )
				__a       =		prepare_input(_SCREAMING_SNAKE_CASE    )
				__a       =		""""""

				# https://en.wikipedia.org/wiki/Playfair_cipher#Description
				for chara, chara in chunker(_SCREAMING_SNAKE_CASE				,     2    ):
								__a     ,					__a       =		divmod(table.index(_SCREAMING_SNAKE_CASE    )				,     5    )
								__a     ,					__a       =		divmod(table.index(_SCREAMING_SNAKE_CASE    )				,     5    )

								if rowa == rowa:
												ciphertext += table[rowa * 5 + (cola + 1) % 5]
												ciphertext += table[rowa * 5 + (cola + 1) % 5]
								elif cola == cola:
												ciphertext += table[((rowa + 1) % 5) * 5 + cola]
												ciphertext += table[((rowa + 1) % 5) * 5 + cola]
								else:  # rectangle
												ciphertext += table[rowa * 5 + cola]
												ciphertext += table[rowa * 5 + cola]

				return ciphertext

def   lowerCAmelCase__						(							_SCREAMING_SNAKE_CASE       :			str				,     _SCREAMING_SNAKE_CASE       :			str    ):
				"""simple docstring"""







				__a       =		generate_table(_SCREAMING_SNAKE_CASE    )
				__a       =		""""""

				# https://en.wikipedia.org/wiki/Playfair_cipher#Description
				for chara, chara in chunker(_SCREAMING_SNAKE_CASE				,     2    ):
								__a     ,					__a       =		divmod(table.index(_SCREAMING_SNAKE_CASE    )				,     5    )
								__a     ,					__a       =		divmod(table.index(_SCREAMING_SNAKE_CASE    )				,     5    )

								if rowa == rowa:
												plaintext += table[rowa * 5 + (cola - 1) % 5]
												plaintext += table[rowa * 5 + (cola - 1) % 5]
								elif cola == cola:
												plaintext += table[((rowa - 1) % 5) * 5 + cola]
												plaintext += table[((rowa - 1) % 5) * 5 + cola]
								else:  # rectangle
												plaintext += table[rowa * 5 + cola]
												plaintext += table[rowa * 5 + cola]

				return plaintext
