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




	'''simple docstring'''





	def __init__(			self   :					int				,A   :					List[str]				,A   :					List[str]=13				,A   :					Optional[Any]=7				,A   :					int=True				,A   :					List[Any]=True				,A   :					Tuple=True				,A   :					Union[str, Any]=True				,A   :					List[Any]=99				,A   :					Tuple=[1, 1, 2]				,A   :					str=1				,A   :					Dict=32				,A   :					List[Any]=4				,A   :					List[str]=8				,A   :					Optional[int]=37				,A   :					Tuple="gelu_new"				,A   :					int=0.1				,A   :					Tuple=0.1				,A   :					Tuple=0.0				,A   :					Tuple=5_12				,A   :					Optional[Any]=3				,A   :					Dict=0.02				,A   :					List[str]=3				,A   :					Union[str, Any]=4				,A   :					Optional[Any]=None				,A   :					str=False				,):
					__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[Any]					):
					__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   :					Tuple				,A   :					Union[str, Any]				,A   :					int				,A   :					str				,A   :					Union[str, Any]				,A   :					Union[str, Any]				,A   :					Tuple				,A   :					Optional[Any]				,):
					__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[Any]				,A   :					List[Any]				,A   :					List[str]				,A   :					Any				,A   :					int				,A   :					Tuple				,A   :					Optional[Any]				,A   :					List[str]				,):
					__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   :					Union[str, Any]				,A   :					Optional[Any]				,A   :					int				,A   :					Optional[Any]				,A   :					Any				,A   :					Dict				,A   :					Optional[Any]				,A   :					Optional[int]				,):
					__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   :					Optional[Any]				,A   :					int				,A   :					Any				,A   :					str				,A   :					Optional[int]				,A   :					Union[str, Any]				,A   :					Dict				,A   :					List[Any]				,):
					__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   :					Dict				,A   :					Optional[Any]				,A   :					Tuple				,A   :					Dict				,A   :					List[Any]				,A   :					Optional[int]				,A   :					int				,A   :					Dict				,):
					__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   :					List[Any]				,A   :					List[str]				,A   :					Dict				,A   :					int				,A   :					Dict				,A   :					List[Any]				,A   :					str				,A   :					Dict				,):
					__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   :					Tuple				,A   :					Optional[Any]				,A   :					Any				,A   :					str				,A   :					Optional[Any]				,A   :					List[Any]				,A   :					Union[str, Any]				,A   :					Tuple				,):
					__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]				,A   :					Optional[Any]				,A   :					Optional[Any]				,A   :					str				,A   :					Any				,A   :					Dict				,A   :					Union[str, Any]				,A   :					Union[str, Any]				,):
					__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   :					Union[str, Any]					):
					__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 				UpperCAmelCase (    _a     ,     _a     ,     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 							UpperCamelCase_    (			self   :					Tuple					):
					__A       =					TFFunnelModelTester(self					)
					__A       =					ConfigTester(self				,config_class=A					)






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






	def 							UpperCamelCase_    (			self   :					List[str]					):
					__A       =					self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(*A					)






	def 							UpperCamelCase_    (			self   :					Optional[int]					):
					__A       =					self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_pretraining(*A					)






	def 							UpperCamelCase_    (			self   :					Optional[Any]					):
					__A       =					self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_masked_lm(*A					)






	def 							UpperCamelCase_    (			self   :					Tuple					):
					__A       =					self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_token_classification(*A					)







	def 							UpperCamelCase_    (			self   :					Dict					):
					__A       =					self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_question_answering(*A					)





@require_tf
class 				UpperCAmelCase (    _a     ,     unittest.TestCase    ):




	'''simple docstring'''





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






	def 							UpperCamelCase_    (			self   :					List[str]					):
					__A       =					TFFunnelModelTester(self				,base=A					)
					__A       =					ConfigTester(self				,config_class=A					)






	def 							UpperCamelCase_    (			self   :					Optional[int]					):
					self.config_tester.run_common_tests()






	def 							UpperCamelCase_    (			self   :					Dict					):
					__A       =					self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_base_model(*A					)






	def 							UpperCamelCase_    (			self   :					List[str]					):
					__A       =					self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_sequence_classification(*A					)







	def 							UpperCamelCase_    (			self   :					List[str]					):
					__A       =					self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_multiple_choice(*A					)




from argparse import ArgumentParser

from .add_new_model import AddNewModelCommand
from .add_new_model_like import AddNewModelLikeCommand
from .convert import ConvertCommand
from .download import DownloadCommand
from .env import EnvironmentCommand
from .lfs import LfsCommands
from .pt_to_tf import PTtoTFCommand
from .run import RunCommand
from .serving import ServeCommand
from .user import UserCommands



def 							UpperCamelCase_( )       ->		List[Any]:
    _lowercase			:				int 						=						ArgumentParser('Transformers CLI tool'				,					usage='transformers-cli <command> [<args>]'					)
    _lowercase			:				Optional[Any] 						=						parser.add_subparsers(help='transformers-cli command helpers'					)

    # Register commands
    ConvertCommand.register_subcommand(lowerCamelCase_					)
    DownloadCommand.register_subcommand(lowerCamelCase_					)
    EnvironmentCommand.register_subcommand(lowerCamelCase_					)
    RunCommand.register_subcommand(lowerCamelCase_					)
    ServeCommand.register_subcommand(lowerCamelCase_					)
    UserCommands.register_subcommand(lowerCamelCase_					)
    AddNewModelCommand.register_subcommand(lowerCamelCase_					)
    AddNewModelLikeCommand.register_subcommand(lowerCamelCase_					)
    LfsCommands.register_subcommand(lowerCamelCase_					)
    PTtoTFCommand.register_subcommand(lowerCamelCase_					)

    # Let's go
    _lowercase			:				Any 						=						parser.parse_args()

    if not hasattr(lowerCamelCase_				,					'func'					):
        parser.print_help()
        exit(1					)

    # Run
    _lowercase			:				Optional[int] 						=						args.func(lowerCamelCase_					)
    service.run()


if __name__ == "__main__":
 main()


