

"""simple docstring"""


import unittest

from transformers import FunnelConfig, FunnelTokenizer, is_torch_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
		import torch

		from transformers import (
		    MODEL_FOR_PRETRAINING_MAPPING,
		    FunnelBaseModel,
		    FunnelForMaskedLM,
		    FunnelForMultipleChoice,
		    FunnelForPreTraining,
		    FunnelForQuestionAnswering,
		    FunnelForSequenceClassification,
		    FunnelForTokenClassification,
		    FunnelModel,
		)







class 			lowerCAmelCase_			:



						'''simple docstring'''


						def __init__(	self       : Optional[int]					,A_       : int					,A_       : Optional[int]=13					,A_       : List[Any]=7					,A_       : Optional[Any]=True					,A_       : List[Any]=True					,A_       : List[Any]=True					,A_       : Any=True					,A_       : Optional[Any]=99					,A_       : str=[1, 1, 2]					,A_       : str=1					,A_       : Tuple=32					,A_       : str=4					,A_       : str=8					,A_       : List[Any]=37					,A_       : Tuple="gelu_new"					,A_       : Tuple=0.1					,A_       : Optional[int]=0.1					,A_       : Any=0.0					,A_       : Any=512					,A_       : str=3					,A_       : Dict=0.02					,A_       : Tuple=3					,A_       : str=4					,A_       : int=None					,A_       : Optional[int]=False					,)       ->				Union[str, Any]:
							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   _SCREAMING_SNAKE_CASE (	self       : Optional[Any] )       ->				List[str]:
							A  				=				ids_tensor([self.batch_size, self.seq_length]					,self.vocab_size )

							A  				=				None
							if self.use_input_mask:
								A  				=				ids_tensor([self.batch_size, self.seq_length]					,vocab_size=2 )

							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  				=				ids_tensor([self.batch_size, self.seq_length]					,1 )

							A  				=				self.get_config()

							return (
							    config,
							    input_ids,
							    token_type_ids,
							    input_mask,
							    sequence_labels,
							    token_labels,
							    choice_labels,
							    fake_token_labels,
							)


						def   _SCREAMING_SNAKE_CASE (	self       : str )       ->				Dict:
							return 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					,)


						def   _SCREAMING_SNAKE_CASE (	self       : Any					,A_       : str					,A_       : Dict					,A_       : List[str]					,A_       : List[Any]					,A_       : Dict					,A_       : List[Any]					,A_       : Any					,A_       : Any					,)       ->				Tuple:
							A  				=				FunnelModel(config=A_ )
							model.to(A_ )
							model.eval()
							A  				=				model(A_					,attention_mask=A_					,token_type_ids=A_ )
							A  				=				model(A_					,token_type_ids=A_ )
							A  				=				model(A_ )
							self.parent.assertEqual(result.last_hidden_state.shape					,(self.batch_size, self.seq_length, self.d_model) )

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

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


						def   _SCREAMING_SNAKE_CASE (	self       : int					,A_       : Optional[int]					,A_       : List[Any]					,A_       : List[Any]					,A_       : Any					,A_       : Optional[int]					,A_       : int					,A_       : str					,A_       : Optional[Any]					,)       ->				List[Any]:
							A  				=				FunnelBaseModel(config=A_ )
							model.to(A_ )
							model.eval()
							A  				=				model(A_					,attention_mask=A_					,token_type_ids=A_ )
							A  				=				model(A_					,token_type_ids=A_ )
							A  				=				model(A_ )
							self.parent.assertEqual(result.last_hidden_state.shape					,(self.batch_size, 2, self.d_model) )

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

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


						def   _SCREAMING_SNAKE_CASE (	self       : Optional[Any]					,A_       : int					,A_       : Optional[Any]					,A_       : Union[str, Any]					,A_       : Any					,A_       : Union[str, Any]					,A_       : List[Any]					,A_       : str					,A_       : Dict					,)       ->				Tuple:
							A  				=				self.num_labels
							A  				=				FunnelForPreTraining(config=A_ )
							model.to(A_ )
							model.eval()
							A  				=				model(A_					,attention_mask=A_					,token_type_ids=A_					,labels=A_ )
							self.parent.assertEqual(result.logits.shape					,(self.batch_size, self.seq_length) )


						def   _SCREAMING_SNAKE_CASE (	self       : Dict					,A_       : Tuple					,A_       : List[str]					,A_       : str					,A_       : Dict					,A_       : str					,A_       : List[Any]					,A_       : Union[str, Any]					,A_       : int					,)       ->				Tuple:
							A  				=				FunnelForMaskedLM(config=A_ )
							model.to(A_ )
							model.eval()
							A  				=				model(A_					,attention_mask=A_					,token_type_ids=A_					,labels=A_ )
							self.parent.assertEqual(result.logits.shape					,(self.batch_size, self.seq_length, self.vocab_size) )


						def   _SCREAMING_SNAKE_CASE (	self       : Dict					,A_       : Union[str, Any]					,A_       : str					,A_       : Optional[int]					,A_       : List[Any]					,A_       : Any					,A_       : Dict					,A_       : Tuple					,A_       : str					,)       ->				Optional[int]:
							A  				=				self.num_labels
							A  				=				FunnelForSequenceClassification(A_ )
							model.to(A_ )
							model.eval()
							A  				=				model(A_					,attention_mask=A_					,token_type_ids=A_					,labels=A_ )
							self.parent.assertEqual(result.logits.shape					,(self.batch_size, self.num_labels) )


						def   _SCREAMING_SNAKE_CASE (	self       : List[Any]					,A_       : str					,A_       : int					,A_       : Optional[int]					,A_       : Any					,A_       : Optional[Any]					,A_       : Any					,A_       : List[str]					,A_       : Optional[Any]					,)       ->				int:
							A  				=				self.num_choices
							A  				=				FunnelForMultipleChoice(config=A_ )
							model.to(A_ )
							model.eval()
							A  				=				input_ids.unsqueeze(1 ).expand(-1					,self.num_choices					,-1 ).contiguous()
							A  				=				token_type_ids.unsqueeze(1 ).expand(-1					,self.num_choices					,-1 ).contiguous()
							A  				=				input_mask.unsqueeze(1 ).expand(-1					,self.num_choices					,-1 ).contiguous()
							A  				=				model(
							    A_					,attention_mask=A_					,token_type_ids=A_					,labels=A_					,)
							self.parent.assertEqual(result.logits.shape					,(self.batch_size, self.num_choices) )


						def   _SCREAMING_SNAKE_CASE (	self       : Optional[int]					,A_       : str					,A_       : int					,A_       : int					,A_       : Tuple					,A_       : Dict					,A_       : Tuple					,A_       : List[Any]					,A_       : List[Any]					,)       ->				Dict:
							A  				=				self.num_labels
							A  				=				FunnelForTokenClassification(config=A_ )
							model.to(A_ )
							model.eval()
							A  				=				model(A_					,attention_mask=A_					,token_type_ids=A_					,labels=A_ )
							self.parent.assertEqual(result.logits.shape					,(self.batch_size, self.seq_length, self.num_labels) )


						def   _SCREAMING_SNAKE_CASE (	self       : Dict					,A_       : int					,A_       : Tuple					,A_       : str					,A_       : Any					,A_       : Optional[int]					,A_       : Dict					,A_       : Union[str, Any]					,A_       : Tuple					,)       ->				Optional[Any]:
							A  				=				FunnelForQuestionAnswering(config=A_ )
							model.to(A_ )
							model.eval()
							A  				=				model(
							    A_					,attention_mask=A_					,token_type_ids=A_					,start_positions=A_					,end_positions=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       : Dict )       ->				Dict:
							A  				=				self.prepare_config_and_inputs()
							(
							    (
							    A						
							)				, (
							    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_torch
class 			lowerCAmelCase_			( A__						,   A__						,   unittest.TestCase      ):



						'''simple docstring'''


						_lowerCamelCase:      str =      False
						_lowerCamelCase:      Tuple =      False
						_lowerCamelCase:      Any =      (
						    (
						        FunnelModel,
						        FunnelForMaskedLM,
						        FunnelForPreTraining,
						        FunnelForQuestionAnswering,
						        FunnelForTokenClassification,
						    )
						    if is_torch_available()
						    else ()
						)
						_lowerCamelCase:      str =      (
						    {
						        '''feature-extraction''': (FunnelBaseModel, FunnelModel),
						        '''fill-mask''': FunnelForMaskedLM,
						        '''question-answering''': FunnelForQuestionAnswering,
						        '''text-classification''': FunnelForSequenceClassification,
						        '''token-classification''': FunnelForTokenClassification,
						        '''zero-shot''': FunnelForSequenceClassification,
						    }
						    if is_torch_available()
						    else {}
						)


						def   _SCREAMING_SNAKE_CASE (	self       : Optional[int]					,A_       : Dict					,A_       : Optional[Any]					,A_       : int=False )       ->				str:
							A  				=				super()._prepare_for_class(A_					,A_					,return_labels=A_ )

							if return_labels:
								if model_class in get_values(A_ ):
									A  				=				torch.zeros(
									    (self.model_tester.batch_size, self.model_tester.seq_length)					,dtype=torch.long					,device=A_ )
							return inputs_dict


						def   _SCREAMING_SNAKE_CASE (	self       : List[str] )       ->				List[Any]:
							A  				=				FunnelModelTester(self )
							A  				=				ConfigTester(self					,config_class=A_ )


						def   _SCREAMING_SNAKE_CASE (	self       : Union[str, Any] )       ->				Any:
							self.config_tester.run_common_tests()


						def   _SCREAMING_SNAKE_CASE (	self       : Union[str, Any] )       ->				Union[str, Any]:
							A  				=				self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*A_ )


						def   _SCREAMING_SNAKE_CASE (	self       : Optional[Any] )       ->				str:
							A  				=				self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_pretraining(*A_ )


						def   _SCREAMING_SNAKE_CASE (	self       : Union[str, Any] )       ->				List[Any]:
							A  				=				self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_masked_lm(*A_ )


						def   _SCREAMING_SNAKE_CASE (	self       : Optional[int] )       ->				Union[str, Any]:
							A  				=				self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_token_classification(*A_ )


						def   _SCREAMING_SNAKE_CASE (	self       : List[str] )       ->				Dict:
							A  				=				self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_question_answering(*A_ )




						def   _SCREAMING_SNAKE_CASE (	self       : str					,A_       : List[Any] )       ->				List[Any]:
							if hasattr(A_					,'weight' ) and module.weight is not None:
								module.weight.data.fill_(3 )
							if hasattr(A_					,'bias' ) and module.bias is not None:
								module.bias.data.fill_(3 )

							for param in ["r_w_bias", "r_r_bias", "r_kernel", "r_s_bias", "seg_embed"]:
								if hasattr(A_					,A_ ) and getattr(A_					,A_ ) is not None:
									A  				=				getattr(A_					,A_ )
									weight.data.fill_(3 )







@require_torch
class 			lowerCAmelCase_			( A__						,   unittest.TestCase      ):



						'''simple docstring'''


						_lowerCamelCase:      List[str] =      False
						_lowerCamelCase:      str =      False
						_lowerCamelCase:      Optional[int] =      (
						    (FunnelBaseModel, FunnelForMultipleChoice, FunnelForSequenceClassification) if is_torch_available() else ()
						)


						def   _SCREAMING_SNAKE_CASE (	self       : Any )       ->				int:
							A  				=				FunnelModelTester(self					,base=A_ )
							A  				=				ConfigTester(self					,config_class=A_ )


						def   _SCREAMING_SNAKE_CASE (	self       : Optional[int] )       ->				Any:
							self.config_tester.run_common_tests()


						def   _SCREAMING_SNAKE_CASE (	self       : int )       ->				List[Any]:
							A  				=				self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_base_model(*A_ )


						def   _SCREAMING_SNAKE_CASE (	self       : List[Any] )       ->				Union[str, Any]:
							A  				=				self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_sequence_classification(*A_ )


						def   _SCREAMING_SNAKE_CASE (	self       : Optional[Any] )       ->				str:
							A  				=				self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_multiple_choice(*A_ )


						def   _SCREAMING_SNAKE_CASE (	self       : Optional[Any] )       ->				Dict:
							A				, A  				=				self.model_tester.prepare_config_and_inputs_for_common()
							A  				=				True

							for model_class in self.all_model_classes:
								if model_class.__name__ == "FunnelBaseModel":
									continue
								A  				=				model_class(A_ )
								model.to(A_ )
								model.train()
								A  				=				self._prepare_for_class(A_					,A_					,return_labels=A_ )
								A  				=				model(**A_ ).loss
								loss.backward()




						def   _SCREAMING_SNAKE_CASE (	self       : Tuple					,A_       : int )       ->				Optional[Any]:
							if hasattr(A_					,'weight' ) and module.weight is not None:
								module.weight.data.fill_(3 )
							if hasattr(A_					,'bias' ) and module.bias is not None:
								module.bias.data.fill_(3 )

							for param in ["r_w_bias", "r_r_bias", "r_kernel", "r_s_bias", "seg_embed"]:
								if hasattr(A_					,A_ ) and getattr(A_					,A_ ) is not None:
									A  				=				getattr(A_					,A_ )
									weight.data.fill_(3 )







@require_torch
@require_sentencepiece
@require_tokenizers
class 			lowerCAmelCase_			( unittest.TestCase      ):



						'''simple docstring'''


						def   _SCREAMING_SNAKE_CASE (	self       : Dict )       ->				Optional[Any]:
							A  				=				13
							A  				=				7
							A  				=				torch.arange(0					,batch_size * sequence_length ).long().reshape(A_					,A_ )
							A  				=				[0, 1, 2, 3, 4, 5, 6, 4, 1, 3, 5, 0, 1]
							A  				=				torch.tensor([[2] + [0] * a + [1] * (sequence_length - a - 1) for a in lengths] )

							A  				=				FunnelModel.from_pretrained('sgugger/funnel-random-tiny' )
							A  				=				model(A_					,token_type_ids=A_ )[0].abs()

							A  				=				torch.tensor(23_44.83_52 )
							A  				=				torch.tensor(0.80_52 )
							self.assertTrue(torch.allclose(output.sum()					,A_					,atol=1e-4 ) )
							self.assertTrue(torch.allclose(output.mean()					,A_					,atol=1e-4 ) )

							A  				=				torch.tensor([[1] * 7, [1] * 4 + [0] * 3] * 6 + [[0, 1, 1, 0, 0, 1, 1]] )
							A  				=				model(A_					,attention_mask=A_					,token_type_ids=A_ )[0].abs()

							A  				=				torch.tensor(23_43.84_25 )
							A  				=				torch.tensor(0.80_49 )
							self.assertTrue(torch.allclose(output.sum()					,A_					,atol=1e-4 ) )
							self.assertTrue(torch.allclose(output.mean()					,A_					,atol=1e-4 ) )




						@slow
						def   _SCREAMING_SNAKE_CASE (	self       : Union[str, Any] )       ->				Tuple:
							A  				=				FunnelTokenizer.from_pretrained('huggingface/funnel-small' )
							A  				=				FunnelModel.from_pretrained('huggingface/funnel-small' )
							A  				=				tokenizer('Hello! I am the Funnel Transformer model.'					,return_tensors='pt' )
							A  				=				model(**A_ )[0]

							A  				=				torch.tensor(2_35.72_46 )
							A  				=				torch.tensor(0.02_56 )
							self.assertTrue(torch.allclose(output.sum()					,A_					,atol=1e-4 ) )
							self.assertTrue(torch.allclose(output.mean()					,A_					,atol=1e-4 ) )

"""simple docstring"""


from dataclasses import dataclass
from typing import List, Optional, Union

import numpy as np
import PIL
from PIL import Image

from ...utils import is_torch_available, is_transformers_available


if is_transformers_available() and is_torch_available():
		from .image_encoder import PaintByExampleImageEncoder
		from .pipeline_paint_by_example import PaintByExamplePipeline