




"""simple docstring"""







from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available


__A   :  List[str]					=      {
    'configuration_mask2former': [
        'MASK2FORMER_PRETRAINED_CONFIG_ARCHIVE_MAP',
        'Mask2FormerConfig',
    ],
}

try:
						if not is_vision_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						__A   :  Any					=      ['Mask2FormerImageProcessor']

try:
						if not is_torch_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						__A   :  List[str]					=      [
						    'MASK2FORMER_PRETRAINED_MODEL_ARCHIVE_LIST',
						    'Mask2FormerForUniversalSegmentation',
						    'Mask2FormerModel',
						    'Mask2FormerPreTrainedModel',
						]

if TYPE_CHECKING:
						from .configuration_maskaformer import MASK2FORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, MaskaFormerConfig

						try:
												if not is_vision_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .image_processing_maskaformer import MaskaFormerImageProcessor

						try:
												if not is_torch_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .modeling_maskaformer import (
												    MASK2FORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
												    MaskaFormerForUniversalSegmentation,
												    MaskaFormerModel,
												    MaskaFormerPreTrainedModel,
												)


else:
						import sys

						__A   :  Any					=      _LazyModule(__name__, globals()['__file__'], _import_structure)






"""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					:	Optional[Any]    ,   lowercase__					:	List[Any]    ,   lowercase__					:	int=1_3    ,   lowercase__					:	Optional[int]=7    ,   lowercase__					:	Any=True    ,   lowercase__					:	int=True    ,   lowercase__					:	List[Any]=True    ,   lowercase__					:	Union[str, Any]=True    ,   lowercase__					:	Any=9_9    ,   lowercase__					:	Tuple=[1, 1, 2]    ,   lowercase__					:	str=1    ,   lowercase__					:	Union[str, Any]=3_2    ,   lowercase__					:	int=4    ,   lowercase__					:	Dict=8    ,   lowercase__					:	Tuple=3_7    ,   lowercase__					:	int="gelu_new"    ,   lowercase__					:	Tuple=0.1    ,   lowercase__					:	int=0.1    ,   lowercase__					:	Dict=0.0    ,   lowercase__					:	int=5_1_2    ,   lowercase__					:	str=3    ,   lowercase__					:	List[Any]=0.0_2    ,   lowercase__					:	Any=3    ,   lowercase__					:	Union[str, Any]=4    ,   lowercase__					:	Tuple=None    ,   lowercase__					:	List[Any]=False    ,   ):
						__lowercase				:		Any												=				parent
						__lowercase				:		Tuple												=				batch_size
						__lowercase				:		Union[str, Any]												=				seq_length
						__lowercase				:		List[Any]												=				is_training
						__lowercase				:		Tuple												=				use_input_mask
						__lowercase				:		Optional[int]												=				use_token_type_ids
						__lowercase				:		List[Any]												=				use_labels
						__lowercase				:		Any												=				vocab_size
						__lowercase				:		Union[str, Any]												=				block_sizes
						__lowercase				:		Optional[Any]												=				num_decoder_layers
						__lowercase				:		str												=				d_model
						__lowercase				:		Tuple												=				n_head
						__lowercase				:		Any												=				d_head
						__lowercase				:		Dict												=				d_inner
						__lowercase				:		Optional[Any]												=				hidden_act
						__lowercase				:		int												=				hidden_dropout
						__lowercase				:		int												=				attention_dropout
						__lowercase				:		Tuple												=				activation_dropout
						__lowercase				:		int												=				max_position_embeddings
						__lowercase				:		Optional[Any]												=				type_vocab_size
						__lowercase				:		Union[str, Any]												=				2
						__lowercase				:		Optional[int]												=				num_labels
						__lowercase				:		List[str]												=				num_choices
						__lowercase				:		List[Any]												=				scope
						__lowercase				:		List[str]												=				initializer_std

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



				def       snake_case    (     self					:	List[Any]						):
						__lowercase				:		Any												=				ids_tensor([self.batch_size, self.seq_length]    ,   self.vocab_size						)

						__lowercase				:		Optional[int]												=				None
						if self.use_input_mask:
								__lowercase				:		Optional[int]												=				random_attention_mask([self.batch_size, self.seq_length]						)

						__lowercase				:		List[str]												=				None
						if self.use_token_type_ids:
								__lowercase				:		List[Any]												=				ids_tensor([self.batch_size, self.seq_length]    ,   self.type_vocab_size						)

						__lowercase				:		Any												=				None
						__lowercase				:		str												=				None
						__lowercase				:		str												=				None
						if self.use_labels:
								__lowercase				:		List[Any]												=				ids_tensor([self.batch_size]    ,   self.type_sequence_label_size						)
								__lowercase				:		Optional[Any]												=				ids_tensor([self.batch_size, self.seq_length]    ,   self.num_labels						)
								__lowercase				:		Optional[Any]												=				ids_tensor([self.batch_size]    ,   self.num_choices						)

						__lowercase				:		Union[str, 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       snake_case    (     self					:	Dict    ,   lowercase__					:	str    ,   lowercase__					:	List[str]    ,   lowercase__					:	Dict    ,   lowercase__					:	int    ,   lowercase__					:	Union[str, Any]    ,   lowercase__					:	Any    ,   lowercase__					:	Optional[int]    ,   ):
						__lowercase				:		int												=				TFFunnelModel(config=lowercase__						)
						__lowercase				:		Optional[Any]												=				{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
						__lowercase				:		List[Any]												=				model(lowercase__						)

						__lowercase				:		Optional[Any]												=				[input_ids, input_mask]
						__lowercase				:		int												=				model(lowercase__						)

						__lowercase				:		Tuple												=				model(lowercase__						)
						self.parent.assertEqual(result.last_hidden_state.shape    ,   (self.batch_size, self.seq_length, self.d_model)						)

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

						__lowercase				:		str												=				False
						__lowercase				:		int												=				TFFunnelModel(config=lowercase__						)
						__lowercase				:		Dict												=				model(lowercase__						)
						self.parent.assertEqual(result.last_hidden_state.shape    ,   (self.batch_size, self.seq_length, self.d_model)						)



				def       snake_case    (     self					:	Union[str, Any]    ,   lowercase__					:	List[Any]    ,   lowercase__					:	Union[str, Any]    ,   lowercase__					:	Optional[int]    ,   lowercase__					:	int    ,   lowercase__					:	str    ,   lowercase__					:	Dict    ,   lowercase__					:	Union[str, Any]    ,   ):
						__lowercase				:		List[str]												=				TFFunnelBaseModel(config=lowercase__						)
						__lowercase				:		int												=				{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
						__lowercase				:		List[Any]												=				model(lowercase__						)

						__lowercase				:		List[Any]												=				[input_ids, input_mask]
						__lowercase				:		Optional[int]												=				model(lowercase__						)

						__lowercase				:		Optional[Any]												=				model(lowercase__						)
						self.parent.assertEqual(result.last_hidden_state.shape    ,   (self.batch_size, 2, self.d_model)						)

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

						__lowercase				:		List[Any]												=				False
						__lowercase				:		Optional[int]												=				TFFunnelBaseModel(config=lowercase__						)
						__lowercase				:		Optional[int]												=				model(lowercase__						)
						self.parent.assertEqual(result.last_hidden_state.shape    ,   (self.batch_size, 2, self.d_model)						)



				def       snake_case    (     self					:	Optional[int]    ,   lowercase__					:	str    ,   lowercase__					:	Tuple    ,   lowercase__					:	Any    ,   lowercase__					:	List[Any]    ,   lowercase__					:	Dict    ,   lowercase__					:	List[str]    ,   lowercase__					:	Optional[Any]    ,   ):
						__lowercase				:		Tuple												=				TFFunnelForPreTraining(config=lowercase__						)
						__lowercase				:		Optional[int]												=				{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
						__lowercase				:		Union[str, Any]												=				model(lowercase__						)
						self.parent.assertEqual(result.logits.shape    ,   (self.batch_size, self.seq_length)						)



				def       snake_case    (     self					:	int    ,   lowercase__					:	List[Any]    ,   lowercase__					:	List[Any]    ,   lowercase__					:	Any    ,   lowercase__					:	int    ,   lowercase__					:	Any    ,   lowercase__					:	str    ,   lowercase__					:	Optional[Any]    ,   ):
						__lowercase				:		Optional[int]												=				TFFunnelForMaskedLM(config=lowercase__						)
						__lowercase				:		List[str]												=				{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
						__lowercase				:		Optional[int]												=				model(lowercase__						)
						self.parent.assertEqual(result.logits.shape    ,   (self.batch_size, self.seq_length, self.vocab_size)						)



				def       snake_case    (     self					:	Union[str, Any]    ,   lowercase__					:	str    ,   lowercase__					:	Any    ,   lowercase__					:	Any    ,   lowercase__					:	Union[str, Any]    ,   lowercase__					:	Dict    ,   lowercase__					:	int    ,   lowercase__					:	int    ,   ):
						__lowercase				:		str												=				self.num_labels
						__lowercase				:		List[Any]												=				TFFunnelForSequenceClassification(config=lowercase__						)
						__lowercase				:		Union[str, Any]												=				{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
						__lowercase				:		int												=				model(lowercase__						)
						self.parent.assertEqual(result.logits.shape    ,   (self.batch_size, self.num_labels)						)



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



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



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





@require_tf
class       lowerCAmelCase__       (			lowerCAmelCase_  ,			lowerCAmelCase_  ,			unittest.TestCase    ):


				"""simple docstring"""





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



				def       snake_case    (     self					:	Dict						):
						__lowercase				:		List[Any]												=				TFFunnelModelTester(self						)
						__lowercase				:		Tuple												=				ConfigTester(self    ,   config_class=lowercase__						)



				def       snake_case    (     self					:	List[Any]						):
						self.config_tester.run_common_tests()



				def       snake_case    (     self					:	Any						):
						__lowercase				:		Optional[int]												=				self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*lowercase__						)



				def       snake_case    (     self					:	List[Any]						):
						__lowercase				:		Optional[int]												=				self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_pretraining(*lowercase__						)



				def       snake_case    (     self					:	str						):
						__lowercase				:		List[str]												=				self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_masked_lm(*lowercase__						)



				def       snake_case    (     self					:	Any						):
						__lowercase				:		Any												=				self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_token_classification(*lowercase__						)



				def       snake_case    (     self					:	str						):
						__lowercase				:		Union[str, Any]												=				self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_question_answering(*lowercase__						)





@require_tf
class       lowerCAmelCase__       (			lowerCAmelCase_  ,			unittest.TestCase    ):


				"""simple docstring"""





				__UpperCAmelCase :     List[str]       =      (
				    (TFFunnelBaseModel, TFFunnelForMultipleChoice, TFFunnelForSequenceClassification) if is_tf_available() else ()
				)
				__UpperCAmelCase :     str       =      False
				__UpperCAmelCase :     Optional[Any]       =      False



				def       snake_case    (     self					:	List[Any]						):
						__lowercase				:		Optional[int]												=				TFFunnelModelTester(self    ,   base=lowercase__						)
						__lowercase				:		List[Any]												=				ConfigTester(self    ,   config_class=lowercase__						)



				def       snake_case    (     self					:	Union[str, Any]						):
						self.config_tester.run_common_tests()



				def       snake_case    (     self					:	List[str]						):
						__lowercase				:		Any												=				self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_base_model(*lowercase__						)



				def       snake_case    (     self					:	Union[str, Any]						):
						__lowercase				:		Tuple												=				self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_sequence_classification(*lowercase__						)



				def       snake_case    (     self					:	List[Any]						):
						__lowercase				:		Optional[Any]												=				self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_multiple_choice(*lowercase__						)
