
"""simple docstring"""





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




			"""simple docstring"""







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







								"""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   lowerCamelCase__  (					self    :       Optional[Any]						)   ->			Any:







								"""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   lowerCamelCase__  (					self    :       List[str]							, _snake_case    :       Optional[int]							, _snake_case    :       Optional[Any]							, _snake_case    :       Optional[Any]							, _snake_case    :       Union[str, Any]							, _snake_case    :       Union[str, Any]							, _snake_case    :       List[str]							, _snake_case    :       int							, )   ->			str:







								"""simple docstring"""
								A_								=    TFFunnelModel(config=_snake_case						)
								A_								=    {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
								A_								=    model(_snake_case						)

								A_								=    [input_ids, input_mask]
								A_								=    model(_snake_case						)

								A_								=    model(_snake_case						)
								self.parent.assertEqual(result.last_hidden_state.shape							, (self.batch_size, self.seq_length, self.d_model)						)

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

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




			def   lowerCamelCase__  (					self    :       int							, _snake_case    :       Optional[Any]							, _snake_case    :       Optional[int]							, _snake_case    :       Any							, _snake_case    :       List[str]							, _snake_case    :       List[Any]							, _snake_case    :       str							, _snake_case    :       Any							, )   ->			int:







								"""simple docstring"""
								A_								=    TFFunnelBaseModel(config=_snake_case						)
								A_								=    {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
								A_								=    model(_snake_case						)

								A_								=    [input_ids, input_mask]
								A_								=    model(_snake_case						)

								A_								=    model(_snake_case						)
								self.parent.assertEqual(result.last_hidden_state.shape							, (self.batch_size, 2, self.d_model)						)

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

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




			def   lowerCamelCase__  (					self    :       Dict							, _snake_case    :       List[str]							, _snake_case    :       List[Any]							, _snake_case    :       List[Any]							, _snake_case    :       int							, _snake_case    :       List[str]							, _snake_case    :       int							, _snake_case    :       str							, )   ->			Union[str, Any]:







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




			def   lowerCamelCase__  (					self    :       Union[str, Any]							, _snake_case    :       List[Any]							, _snake_case    :       List[Any]							, _snake_case    :       List[Any]							, _snake_case    :       Tuple							, _snake_case    :       Any							, _snake_case    :       List[Any]							, _snake_case    :       str							, )   ->			Optional[Any]:







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




			def   lowerCamelCase__  (					self    :       str							, _snake_case    :       Optional[Any]							, _snake_case    :       Dict							, _snake_case    :       str							, _snake_case    :       str							, _snake_case    :       Optional[int]							, _snake_case    :       Dict							, _snake_case    :       Optional[int]							, )   ->			str:







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




			def   lowerCamelCase__  (					self    :       Union[str, Any]							, _snake_case    :       Optional[int]							, _snake_case    :       str							, _snake_case    :       Optional[Any]							, _snake_case    :       Optional[int]							, _snake_case    :       int							, _snake_case    :       Optional[Any]							, _snake_case    :       Optional[Any]							, )   ->			Any:







								"""simple docstring"""
								A_								=    self.num_choices
								A_								=    TFFunnelForMultipleChoice(config=_snake_case						)
								A_								=    tf.tile(tf.expand_dims(_snake_case							, 1						)							, (1, self.num_choices, 1)						)
								A_								=    tf.tile(tf.expand_dims(_snake_case							, 1						)							, (1, self.num_choices, 1)						)
								A_								=    tf.tile(tf.expand_dims(_snake_case							, 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(_snake_case						)
								self.parent.assertEqual(result.logits.shape							, (self.batch_size, self.num_choices)						)




			def   lowerCamelCase__  (					self    :       int							, _snake_case    :       int							, _snake_case    :       Tuple							, _snake_case    :       Dict							, _snake_case    :       Any							, _snake_case    :       Any							, _snake_case    :       Union[str, Any]							, _snake_case    :       int							, )   ->			Optional[int]:







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




			def   lowerCamelCase__  (					self    :       Tuple							, _snake_case    :       Dict							, _snake_case    :       Union[str, Any]							, _snake_case    :       List[str]							, _snake_case    :       List[str]							, _snake_case    :       List[Any]							, _snake_case    :       Optional[int]							, _snake_case    :       Tuple							, )   ->			Tuple:







								"""simple docstring"""
								A_								=    TFFunnelForQuestionAnswering(config=_snake_case						)
								A_								=    {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
								A_								=    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   lowerCamelCase__  (					self    :       List[Any]						)   ->			int:







								"""simple docstring"""
								A_								=    self.prepare_config_and_inputs()
								(
								    (
								    A_
								)			,     (
								    A_
								)			,     (
								    A_
								)			,     (
								    A_
								)			,     (
								    A_
								)			,     (
								    A_
								)			,     (
								    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 __lowerCAmelCase					(       _lowercase  ,					_lowercase  ,					unittest.TestCase   ):




			"""simple docstring"""







			snake_case						   =					(
			    (
			        TFFunnelModel,
			        TFFunnelForMaskedLM,
			        TFFunnelForPreTraining,
			        TFFunnelForQuestionAnswering,
			        TFFunnelForTokenClassification,
			    )
			    if is_tf_available()
			    else ()
			)
			snake_case						   =					(
			    {
			        "feature-extraction": (TFFunnelBaseModel, TFFunnelModel),
			        "fill-mask": TFFunnelForMaskedLM,
			        "question-answering": TFFunnelForQuestionAnswering,
			        "text-classification": TFFunnelForSequenceClassification,
			        "token-classification": TFFunnelForTokenClassification,
			        "zero-shot": TFFunnelForSequenceClassification,
			    }
			    if is_tf_available()
			    else {}
			)
			snake_case						   =					False
			snake_case						   =					False




			def   lowerCamelCase__  (					self    :       str						)   ->			Dict:







								"""simple docstring"""
								A_								=    TFFunnelModelTester(self						)
								A_								=    ConfigTester(self							, config_class=_snake_case						)




			def   lowerCamelCase__  (					self    :       Dict						)   ->			Optional[int]:







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




			def   lowerCamelCase__  (					self    :       Tuple						)   ->			List[Any]:







								"""simple docstring"""
								A_								=    self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*_snake_case						)




			def   lowerCamelCase__  (					self    :       Union[str, Any]						)   ->			Dict:







								"""simple docstring"""
								A_								=    self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_pretraining(*_snake_case						)




			def   lowerCamelCase__  (					self    :       int						)   ->			Optional[int]:







								"""simple docstring"""
								A_								=    self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_masked_lm(*_snake_case						)




			def   lowerCamelCase__  (					self    :       Tuple						)   ->			Union[str, Any]:







								"""simple docstring"""
								A_								=    self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_token_classification(*_snake_case						)






			def   lowerCamelCase__  (					self    :       Union[str, Any]						)   ->			Optional[int]:







								"""simple docstring"""
								A_								=    self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_question_answering(*_snake_case						)





@require_tf
class __lowerCAmelCase					(       _lowercase  ,					unittest.TestCase   ):




			"""simple docstring"""







			snake_case						   =					(
			    (TFFunnelBaseModel, TFFunnelForMultipleChoice, TFFunnelForSequenceClassification) if is_tf_available() else ()
			)
			snake_case						   =					False
			snake_case						   =					False




			def   lowerCamelCase__  (					self    :       int						)   ->			int:







								"""simple docstring"""
								A_								=    TFFunnelModelTester(self							, base=_snake_case						)
								A_								=    ConfigTester(self							, config_class=_snake_case						)




			def   lowerCamelCase__  (					self    :       Dict						)   ->			List[str]:







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




			def   lowerCamelCase__  (					self    :       List[str]						)   ->			Union[str, Any]:







								"""simple docstring"""
								A_								=    self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_base_model(*_snake_case						)




			def   lowerCamelCase__  (					self    :       Any						)   ->			int:







								"""simple docstring"""
								A_								=    self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_sequence_classification(*_snake_case						)






			def   lowerCamelCase__  (					self    :       List[Any]						)   ->			str:







								"""simple docstring"""
								A_								=    self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_multiple_choice(*_snake_case						)






"""simple docstring"""





from __future__ import annotations
def 				A_  (__a					, __a = None					, __a = None					):

					'''simple docstring'''





					if start is None:
										A_								=    0

					if end is None:
										A_								=    len(__a					) - 1

					if start >= end:
										return

					A_								=    (start + end) // 2

					slowsort(__a					, __a					, __a					)
					slowsort(__a					, mid + 1					, __a					)

					if sequence[end] < sequence[mid]:
										A_			,     A_								=    sequence[mid], sequence[end]

					slowsort(__a					, __a					, end - 1					)


if __name__ == "__main__":
		from doctest import testmod

		testmod()




