





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       :   List[str]							,     __lowerCAmelCase       :   str							,     __lowerCAmelCase       :   List[Any]=13							,     __lowerCAmelCase       :   List[Any]=7							,     __lowerCAmelCase       :   Optional[int]=True							,     __lowerCAmelCase       :   List[Any]=True							,     __lowerCAmelCase       :   Union[str, Any]=True							,     __lowerCAmelCase       :   Tuple=True							,     __lowerCAmelCase       :   Optional[int]=99							,     __lowerCAmelCase       :   List[str]=[1, 1, 2]							,     __lowerCAmelCase       :   List[Any]=1							,     __lowerCAmelCase       :   Tuple=32							,     __lowerCAmelCase       :   str=4							,     __lowerCAmelCase       :   str=8							,     __lowerCAmelCase       :   Optional[int]=37							,     __lowerCAmelCase       :   Optional[int]="gelu_new"							,     __lowerCAmelCase       :   Union[str, Any]=0.1							,     __lowerCAmelCase       :   List[Any]=0.1							,     __lowerCAmelCase       :   int=0.0							,     __lowerCAmelCase       :   str=5_12							,     __lowerCAmelCase       :   Optional[Any]=3							,     __lowerCAmelCase       :   Optional[Any]=0.0_2							,     __lowerCAmelCase       :   List[str]=3							,     __lowerCAmelCase       :   List[Any]=4							,     __lowerCAmelCase       :   Optional[Any]=None							,     __lowerCAmelCase       :   Optional[Any]=False							,     )     ->     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        a_	(      self       :   Tuple       )     ->     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        a_	(      self       :   Optional[int]							,     __lowerCAmelCase       :   Dict							,     __lowerCAmelCase       :   List[str]							,     __lowerCAmelCase       :   Tuple							,     __lowerCAmelCase       :   List[Any]							,     __lowerCAmelCase       :   Any							,     __lowerCAmelCase       :   int							,     __lowerCAmelCase       :   Any							,     )     ->     List[str]:







					"""simple docstring"""






					A__  				=       TFFunnelModel(config=__lowerCAmelCase       )
					A__  				=       {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
					A__  				=       model(__lowerCAmelCase       )

					A__  				=       [input_ids, input_mask]
					A__  				=       model(__lowerCAmelCase       )

					A__  				=       model(__lowerCAmelCase       )
					self.parent.assertEqual(result.last_hidden_state.shape							,     (self.batch_size, self.seq_length, self.d_model)       )

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

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






			def        a_	(      self       :   Optional[Any]							,     __lowerCAmelCase       :   Optional[Any]							,     __lowerCAmelCase       :   Optional[int]							,     __lowerCAmelCase       :   int							,     __lowerCAmelCase       :   Tuple							,     __lowerCAmelCase       :   str							,     __lowerCAmelCase       :   int							,     __lowerCAmelCase       :   List[Any]							,     )     ->     int:







					"""simple docstring"""






					A__  				=       TFFunnelBaseModel(config=__lowerCAmelCase       )
					A__  				=       {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
					A__  				=       model(__lowerCAmelCase       )

					A__  				=       [input_ids, input_mask]
					A__  				=       model(__lowerCAmelCase       )

					A__  				=       model(__lowerCAmelCase       )
					self.parent.assertEqual(result.last_hidden_state.shape							,     (self.batch_size, 2, self.d_model)       )

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

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






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







					"""simple docstring"""






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






			def        a_	(      self       :   List[Any]							,     __lowerCAmelCase       :   Optional[Any]							,     __lowerCAmelCase       :   int							,     __lowerCAmelCase       :   Any							,     __lowerCAmelCase       :   Optional[int]							,     __lowerCAmelCase       :   Optional[int]							,     __lowerCAmelCase       :   int							,     __lowerCAmelCase       :   Tuple							,     )     ->     str:







					"""simple docstring"""






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






			def        a_	(      self       :   Optional[Any]							,     __lowerCAmelCase       :   List[str]							,     __lowerCAmelCase       :   int							,     __lowerCAmelCase       :   Dict							,     __lowerCAmelCase       :   Optional[int]							,     __lowerCAmelCase       :   Optional[Any]							,     __lowerCAmelCase       :   Any							,     __lowerCAmelCase       :   Optional[Any]							,     )     ->     Any:







					"""simple docstring"""






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






			def        a_	(      self       :   Any							,     __lowerCAmelCase       :   Optional[int]							,     __lowerCAmelCase       :   str							,     __lowerCAmelCase       :   int							,     __lowerCAmelCase       :   List[Any]							,     __lowerCAmelCase       :   str							,     __lowerCAmelCase       :   Tuple							,     __lowerCAmelCase       :   List[Any]							,     )     ->     Any:







					"""simple docstring"""






					A__  				=       self.num_choices
					A__  				=       TFFunnelForMultipleChoice(config=__lowerCAmelCase       )
					A__  				=       tf.tile(tf.expand_dims(__lowerCAmelCase							,     1       )							,     (1, self.num_choices, 1)       )
					A__  				=       tf.tile(tf.expand_dims(__lowerCAmelCase							,     1       )							,     (1, self.num_choices, 1)       )
					A__  				=       tf.tile(tf.expand_dims(__lowerCAmelCase							,     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(__lowerCAmelCase       )
					self.parent.assertEqual(result.logits.shape							,     (self.batch_size, self.num_choices)       )






			def        a_	(      self       :   Optional[int]							,     __lowerCAmelCase       :   Union[str, Any]							,     __lowerCAmelCase       :   Optional[Any]							,     __lowerCAmelCase       :   int							,     __lowerCAmelCase       :   int							,     __lowerCAmelCase       :   str							,     __lowerCAmelCase       :   Optional[int]							,     __lowerCAmelCase       :   Union[str, Any]							,     )     ->     Tuple:







					"""simple docstring"""






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






			def        a_	(      self       :   Optional[int]							,     __lowerCAmelCase       :   Any							,     __lowerCAmelCase       :   Optional[Any]							,     __lowerCAmelCase       :   Optional[Any]							,     __lowerCAmelCase       :   Union[str, Any]							,     __lowerCAmelCase       :   str							,     __lowerCAmelCase       :   Tuple							,     __lowerCAmelCase       :   Union[str, Any]							,     )     ->     Optional[Any]:







					"""simple docstring"""






					A__  				=       TFFunnelForQuestionAnswering(config=__lowerCAmelCase       )
					A__  				=       {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
					A__  				=       model(__lowerCAmelCase       )
					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        a_	(      self       :   Dict       )     ->     List[Any]:







					"""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   A      (SCREAMING_SNAKE_CASE       ,					SCREAMING_SNAKE_CASE       ,					unittest.TestCase   ):
			'''simple docstring'''




			__lowerCamelCase     :	int          =   (
			    (
			        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     :	Union[str, Any]          =   False
			__lowerCamelCase     :	List[Any]          =   False






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







					"""simple docstring"""






					A__  				=       TFFunnelModelTester(self       )
					A__  				=       ConfigTester(self							,     config_class=__lowerCAmelCase       )






			def        a_	(      self       :   Tuple       )     ->     Dict:







					"""simple docstring"""






					self.config_tester.run_common_tests()






			def        a_	(      self       :   Any       )     ->     Optional[Any]:







					"""simple docstring"""






					A__  				=       self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(*__lowerCAmelCase       )






			def        a_	(      self       :   int       )     ->     List[Any]:







					"""simple docstring"""






					A__  				=       self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_pretraining(*__lowerCAmelCase       )






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







					"""simple docstring"""






					A__  				=       self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_masked_lm(*__lowerCAmelCase       )






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







					"""simple docstring"""






					A__  				=       self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_token_classification(*__lowerCAmelCase       )







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







					"""simple docstring"""






					A__  				=       self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_question_answering(*__lowerCAmelCase       )


@require_tf
class   A      (SCREAMING_SNAKE_CASE       ,					unittest.TestCase   ):
			'''simple docstring'''




			__lowerCamelCase     :	str          =   (
			    (TFFunnelBaseModel, TFFunnelForMultipleChoice, TFFunnelForSequenceClassification) if is_tf_available() else ()
			)
			__lowerCamelCase     :	Tuple          =   False
			__lowerCamelCase     :	Tuple          =   False






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







					"""simple docstring"""






					A__  				=       TFFunnelModelTester(self							,     base=__lowerCAmelCase       )
					A__  				=       ConfigTester(self							,     config_class=__lowerCAmelCase       )






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







					"""simple docstring"""






					self.config_tester.run_common_tests()






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







					"""simple docstring"""






					A__  				=       self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_base_model(*__lowerCAmelCase       )






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







					"""simple docstring"""






					A__  				=       self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_sequence_classification(*__lowerCAmelCase       )







			def        a_	(      self       :   Any       )     ->     Optional[Any]:







					"""simple docstring"""






					A__  				=       self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_multiple_choice(*__lowerCAmelCase       )










from ...utils import is_note_seq_available, is_transformers_available, is_torch_available
from ...utils import OptionalDependencyNotAvailable


try:
					if not (is_transformers_available() and is_torch_available()):
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					from ...utils.dummy_torch_and_transformers_objects import *  # noqa F403
else:
					from .notes_encoder import SpectrogramNotesEncoder
					from .continous_encoder import SpectrogramContEncoder
					from .pipeline_spectrogram_diffusion import (
					    SpectrogramContEncoder,
					    SpectrogramDiffusionPipeline,
					    TaFilmDecoder,
					)

try:
					if not (is_transformers_available() and is_torch_available() and is_note_seq_available()):
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					from ...utils.dummy_transformers_and_torch_and_note_seq_objects import *  # noqa F403
else:
					from .midi_utils import MidiProcessor



