

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




			"""simple docstring"""







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

									# Used in the tests to check the size of the first attention layer
									SCREAMING_SNAKE_CASE_: Union[str, Any]  		=       n_head
									# Used in the tests to check the size of the first hidden state
									SCREAMING_SNAKE_CASE_: int  		=       self.d_model
									# Used in the tests to check the number of output hidden states/attentions
									SCREAMING_SNAKE_CASE_: str  		=       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:
															SCREAMING_SNAKE_CASE_: Tuple  		=       self.num_hidden_layers + 2



			def        _SCREAMING_SNAKE_CASE			( self  :     Optional[Any]):
									SCREAMING_SNAKE_CASE_: str  		=       ids_tensor([self.batch_size, self.seq_length]   ,						self.vocab_size)

									SCREAMING_SNAKE_CASE_: List[Any]  		=       None
									if self.use_input_mask:
															SCREAMING_SNAKE_CASE_: str  		=       random_attention_mask([self.batch_size, self.seq_length])

									SCREAMING_SNAKE_CASE_: int  		=       None
									if self.use_token_type_ids:
															SCREAMING_SNAKE_CASE_: Optional[Any]  		=       ids_tensor([self.batch_size, self.seq_length]   ,						self.type_vocab_size)

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

									SCREAMING_SNAKE_CASE_: Any  		=       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  :     Any   ,						lowerCAmelCase__  :     Any   ,						lowerCAmelCase__  :     Optional[int]   ,						lowerCAmelCase__  :     Tuple   ,						lowerCAmelCase__  :     int   ,						lowerCAmelCase__  :     Optional[int]   ,						lowerCAmelCase__  :     List[Any]   ,						lowerCAmelCase__  :     Any   ,						):
									SCREAMING_SNAKE_CASE_: Optional[int]  		=       TFFunnelModel(config=_a)
									SCREAMING_SNAKE_CASE_: List[str]  		=       {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
									SCREAMING_SNAKE_CASE_: Optional[Any]  		=       model(_a)

									SCREAMING_SNAKE_CASE_: List[Any]  		=       [input_ids, input_mask]
									SCREAMING_SNAKE_CASE_: Any  		=       model(_a)

									SCREAMING_SNAKE_CASE_: List[Any]  		=       model(_a)
									self.parent.assertEqual(result.last_hidden_state.shape   ,						(self.batch_size, self.seq_length, self.d_model))

									SCREAMING_SNAKE_CASE_: str  		=       False
									SCREAMING_SNAKE_CASE_: Optional[int]  		=       TFFunnelModel(config=_a)
									SCREAMING_SNAKE_CASE_: List[Any]  		=       model(_a)
									self.parent.assertEqual(result.last_hidden_state.shape   ,						(self.batch_size, self.seq_length, self.d_model))

									SCREAMING_SNAKE_CASE_: List[str]  		=       False
									SCREAMING_SNAKE_CASE_: Any  		=       TFFunnelModel(config=_a)
									SCREAMING_SNAKE_CASE_: List[Any]  		=       model(_a)
									self.parent.assertEqual(result.last_hidden_state.shape   ,						(self.batch_size, self.seq_length, self.d_model))



			def        _SCREAMING_SNAKE_CASE			( self  :     List[str]   ,						lowerCAmelCase__  :     Tuple   ,						lowerCAmelCase__  :     List[Any]   ,						lowerCAmelCase__  :     Optional[int]   ,						lowerCAmelCase__  :     Tuple   ,						lowerCAmelCase__  :     Dict   ,						lowerCAmelCase__  :     Any   ,						lowerCAmelCase__  :     int   ,						):
									SCREAMING_SNAKE_CASE_: int  		=       TFFunnelBaseModel(config=_a)
									SCREAMING_SNAKE_CASE_: List[str]  		=       {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
									SCREAMING_SNAKE_CASE_: List[Any]  		=       model(_a)

									SCREAMING_SNAKE_CASE_: str  		=       [input_ids, input_mask]
									SCREAMING_SNAKE_CASE_: List[Any]  		=       model(_a)

									SCREAMING_SNAKE_CASE_: Union[str, Any]  		=       model(_a)
									self.parent.assertEqual(result.last_hidden_state.shape   ,						(self.batch_size, 2, self.d_model))

									SCREAMING_SNAKE_CASE_: Union[str, Any]  		=       False
									SCREAMING_SNAKE_CASE_: Tuple  		=       TFFunnelBaseModel(config=_a)
									SCREAMING_SNAKE_CASE_: List[str]  		=       model(_a)
									self.parent.assertEqual(result.last_hidden_state.shape   ,						(self.batch_size, 3, self.d_model))

									SCREAMING_SNAKE_CASE_: Tuple  		=       False
									SCREAMING_SNAKE_CASE_: Tuple  		=       TFFunnelBaseModel(config=_a)
									SCREAMING_SNAKE_CASE_: Optional[Any]  		=       model(_a)
									self.parent.assertEqual(result.last_hidden_state.shape   ,						(self.batch_size, 2, self.d_model))



			def        _SCREAMING_SNAKE_CASE			( self  :     Any   ,						lowerCAmelCase__  :     Optional[Any]   ,						lowerCAmelCase__  :     str   ,						lowerCAmelCase__  :     str   ,						lowerCAmelCase__  :     Optional[int]   ,						lowerCAmelCase__  :     Dict   ,						lowerCAmelCase__  :     Union[str, Any]   ,						lowerCAmelCase__  :     str   ,						):
									SCREAMING_SNAKE_CASE_: int  		=       TFFunnelForPreTraining(config=_a)
									SCREAMING_SNAKE_CASE_: Union[str, Any]  		=       {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
									SCREAMING_SNAKE_CASE_: Optional[Any]  		=       model(_a)
									self.parent.assertEqual(result.logits.shape   ,						(self.batch_size, self.seq_length))



			def        _SCREAMING_SNAKE_CASE			( self  :     Union[str, Any]   ,						lowerCAmelCase__  :     Optional[Any]   ,						lowerCAmelCase__  :     Optional[Any]   ,						lowerCAmelCase__  :     Dict   ,						lowerCAmelCase__  :     int   ,						lowerCAmelCase__  :     List[str]   ,						lowerCAmelCase__  :     Dict   ,						lowerCAmelCase__  :     Dict   ,						):
									SCREAMING_SNAKE_CASE_: Union[str, Any]  		=       TFFunnelForMaskedLM(config=_a)
									SCREAMING_SNAKE_CASE_: List[str]  		=       {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
									SCREAMING_SNAKE_CASE_: int  		=       model(_a)
									self.parent.assertEqual(result.logits.shape   ,						(self.batch_size, self.seq_length, self.vocab_size))



			def        _SCREAMING_SNAKE_CASE			( self  :     str   ,						lowerCAmelCase__  :     Optional[Any]   ,						lowerCAmelCase__  :     str   ,						lowerCAmelCase__  :     List[str]   ,						lowerCAmelCase__  :     Tuple   ,						lowerCAmelCase__  :     str   ,						lowerCAmelCase__  :     List[str]   ,						lowerCAmelCase__  :     Optional[Any]   ,						):
									SCREAMING_SNAKE_CASE_: List[str]  		=       self.num_labels
									SCREAMING_SNAKE_CASE_: Tuple  		=       TFFunnelForSequenceClassification(config=_a)
									SCREAMING_SNAKE_CASE_: Any  		=       {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
									SCREAMING_SNAKE_CASE_: Tuple  		=       model(_a)
									self.parent.assertEqual(result.logits.shape   ,						(self.batch_size, self.num_labels))



			def        _SCREAMING_SNAKE_CASE			( self  :     Tuple   ,						lowerCAmelCase__  :     Any   ,						lowerCAmelCase__  :     Union[str, Any]   ,						lowerCAmelCase__  :     List[str]   ,						lowerCAmelCase__  :     Optional[int]   ,						lowerCAmelCase__  :     List[Any]   ,						lowerCAmelCase__  :     Optional[int]   ,						lowerCAmelCase__  :     int   ,						):
									SCREAMING_SNAKE_CASE_: Any  		=       self.num_choices
									SCREAMING_SNAKE_CASE_: Any  		=       TFFunnelForMultipleChoice(config=_a)
									SCREAMING_SNAKE_CASE_: List[Any]  		=       tf.tile(tf.expand_dims(_a   ,						1)   ,						(1, self.num_choices, 1))
									SCREAMING_SNAKE_CASE_: Any  		=       tf.tile(tf.expand_dims(_a   ,						1)   ,						(1, self.num_choices, 1))
									SCREAMING_SNAKE_CASE_: Any  		=       tf.tile(tf.expand_dims(_a   ,						1)   ,						(1, self.num_choices, 1))
									SCREAMING_SNAKE_CASE_: Any  		=       {
									    "input_ids": multiple_choice_inputs_ids,
									    "attention_mask": multiple_choice_input_mask,
									    "token_type_ids": multiple_choice_token_type_ids,
									}
									SCREAMING_SNAKE_CASE_: List[Any]  		=       model(_a)
									self.parent.assertEqual(result.logits.shape   ,						(self.batch_size, self.num_choices))



			def        _SCREAMING_SNAKE_CASE			( self  :     str   ,						lowerCAmelCase__  :     int   ,						lowerCAmelCase__  :     List[Any]   ,						lowerCAmelCase__  :     int   ,						lowerCAmelCase__  :     Tuple   ,						lowerCAmelCase__  :     str   ,						lowerCAmelCase__  :     List[Any]   ,						lowerCAmelCase__  :     Optional[Any]   ,						):
									SCREAMING_SNAKE_CASE_: int  		=       self.num_labels
									SCREAMING_SNAKE_CASE_: int  		=       TFFunnelForTokenClassification(config=_a)
									SCREAMING_SNAKE_CASE_: Optional[Any]  		=       {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
									SCREAMING_SNAKE_CASE_: int  		=       model(_a)
									self.parent.assertEqual(result.logits.shape   ,						(self.batch_size, self.seq_length, self.num_labels))



			def        _SCREAMING_SNAKE_CASE			( self  :     Optional[Any]   ,						lowerCAmelCase__  :     Dict   ,						lowerCAmelCase__  :     Union[str, Any]   ,						lowerCAmelCase__  :     Tuple   ,						lowerCAmelCase__  :     Optional[Any]   ,						lowerCAmelCase__  :     Tuple   ,						lowerCAmelCase__  :     int   ,						lowerCAmelCase__  :     List[Any]   ,						):
									SCREAMING_SNAKE_CASE_: Any  		=       TFFunnelForQuestionAnswering(config=_a)
									SCREAMING_SNAKE_CASE_: Any  		=       {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
									SCREAMING_SNAKE_CASE_: Union[str, Any]  		=       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        _SCREAMING_SNAKE_CASE			( self  :     int):
									SCREAMING_SNAKE_CASE_: List[str]  		=       self.prepare_config_and_inputs()
									(
									    (
									    SCREAMING_SNAKE_CASE_						
									)		,    (
									    SCREAMING_SNAKE_CASE_						
									)		,    (
									    SCREAMING_SNAKE_CASE_						
									)		,    (
									    SCREAMING_SNAKE_CASE_						
									)		,    (
									    SCREAMING_SNAKE_CASE_						
									)		,    (
									    SCREAMING_SNAKE_CASE_						
									)		,    (
									    SCREAMING_SNAKE_CASE_						
									)		,    						
									): int  		=       config_and_inputs
									SCREAMING_SNAKE_CASE_: Optional[Any]  		=       {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
									return config, inputs_dict







@require_tf
class   __lowercase				( A__		,       A__		,       unittest.TestCase	):




			"""simple docstring"""







			_UpperCAmelCase   :							List[str]										=     (
			    (
			        TFFunnelModel,
			        TFFunnelForMaskedLM,
			        TFFunnelForPreTraining,
			        TFFunnelForQuestionAnswering,
			        TFFunnelForTokenClassification,
			    )
			    if is_tf_available()
			    else ()
			)
			_UpperCAmelCase   :							List[Any]										=     (
			    {
			        '''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   :							str										=     False
			_UpperCAmelCase   :							Dict										=     False



			def        _SCREAMING_SNAKE_CASE			( self  :     Dict):
									SCREAMING_SNAKE_CASE_: Dict  		=       TFFunnelModelTester(self)
									SCREAMING_SNAKE_CASE_: Optional[int]  		=       ConfigTester(self   ,						config_class=_a)



			def        _SCREAMING_SNAKE_CASE			( self  :     Any):
									self.config_tester.run_common_tests()



			def        _SCREAMING_SNAKE_CASE			( self  :     Tuple):
									SCREAMING_SNAKE_CASE_: Optional[int]  		=       self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*_a)



			def        _SCREAMING_SNAKE_CASE			( self  :     str):
									SCREAMING_SNAKE_CASE_: Tuple  		=       self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_pretraining(*_a)



			def        _SCREAMING_SNAKE_CASE			( self  :     Optional[Any]):
									SCREAMING_SNAKE_CASE_: str  		=       self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_masked_lm(*_a)



			def        _SCREAMING_SNAKE_CASE			( self  :     Tuple):
									SCREAMING_SNAKE_CASE_: str  		=       self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_token_classification(*_a)




			def        _SCREAMING_SNAKE_CASE			( self  :     int):
									SCREAMING_SNAKE_CASE_: List[str]  		=       self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_question_answering(*_a)







@require_tf
class   __lowercase				( A__		,       unittest.TestCase	):




			"""simple docstring"""







			_UpperCAmelCase   :							Dict										=     (
			    (TFFunnelBaseModel, TFFunnelForMultipleChoice, TFFunnelForSequenceClassification) if is_tf_available() else ()
			)
			_UpperCAmelCase   :							Optional[int]										=     False
			_UpperCAmelCase   :							Union[str, Any]										=     False



			def        _SCREAMING_SNAKE_CASE			( self  :     List[Any]):
									SCREAMING_SNAKE_CASE_: Any  		=       TFFunnelModelTester(self   ,						base=_a)
									SCREAMING_SNAKE_CASE_: List[str]  		=       ConfigTester(self   ,						config_class=_a)



			def        _SCREAMING_SNAKE_CASE			( self  :     Tuple):
									self.config_tester.run_common_tests()



			def        _SCREAMING_SNAKE_CASE			( self  :     Dict):
									SCREAMING_SNAKE_CASE_: str  		=       self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_base_model(*_a)



			def        _SCREAMING_SNAKE_CASE			( self  :     Any):
									SCREAMING_SNAKE_CASE_: str  		=       self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_sequence_classification(*_a)




			def        _SCREAMING_SNAKE_CASE			( self  :     int):
									SCREAMING_SNAKE_CASE_: List[str]  		=       self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_multiple_choice(*_a)









'''simple docstring'''
class 							A__    :





  def __init__(		self      :	Union[str, Any]							,					_a      :	int )	->    None:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =size
       _SCREAMING_SNAKE_CASE							    =[0] * size
       _SCREAMING_SNAKE_CASE							    =[0] * size





  @staticmethod
  def 						A  (		_a      :	int )	->    int:





       '''simple docstring'''






       return index | (index + 1)





  @staticmethod
  def 						A  (		_a      :	int )	->    int:





       '''simple docstring'''






       return (index & (index + 1)) - 1





  def 						A  (		self      :	Tuple							,					_a      :	int							,					_a      :	int )	->    None:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =value
       while index < self.size:
            _SCREAMING_SNAKE_CASE							    =self.get_prev(_a ) + 1
            if current_left_border == index:
                 _SCREAMING_SNAKE_CASE							    =value
            else:
                 _SCREAMING_SNAKE_CASE							    =max(_a							,					_a							,					_a )
            _SCREAMING_SNAKE_CASE							    =self.get_next(_a )





  def 						A  (		self      :	int							,					_a      :	int							,					_a      :	int )	->    int:





       '''simple docstring'''






       right -= 1  # Because of right is exclusive
       _SCREAMING_SNAKE_CASE							    =0
       while left <= right:
            _SCREAMING_SNAKE_CASE							    =self.get_prev(_a )
            if left <= current_left:
                 _SCREAMING_SNAKE_CASE							    =max(_a							,					self.tree[right] )
                 _SCREAMING_SNAKE_CASE							    =current_left
            else:
                 _SCREAMING_SNAKE_CASE							    =max(_a							,					self.arr[right] )
                 right -= 1
       return result


if __name__ == "__main__":
     import doctest

     doctest.testmod()
