

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

			"""simple docstring"""

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

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






			def    _lowerCAmelCase			(							self     :					Tuple						):
								UpperCamelCase__		:    str         =ids_tensor([self.batch_size, self.seq_length]						,  self.vocab_size						)

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

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

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

								UpperCamelCase__		:    str         =self.get_config()

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






			def    _lowerCAmelCase			(							self     :					int						):
								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    _lowerCAmelCase			(							self     :					Tuple						,  lowercase_     :					List[str]						,  lowercase_     :					Dict						,  lowercase_     :					int						,  lowercase_     :					Dict						,  lowercase_     :					Union[str, Any]						,  lowercase_     :					Optional[Any]						,  lowercase_     :					List[Any]						,  lowercase_     :					str						,  ):
								UpperCamelCase__		:    Dict         =FunnelModel(config=__SCREAMING_SNAKE_CASE						)
								model.to(__SCREAMING_SNAKE_CASE						)
								model.eval()
								UpperCamelCase__		:    Any         =model(__SCREAMING_SNAKE_CASE						,  attention_mask=__SCREAMING_SNAKE_CASE						,  token_type_ids=__SCREAMING_SNAKE_CASE						)
								UpperCamelCase__		:    Tuple         =model(__SCREAMING_SNAKE_CASE						,  token_type_ids=__SCREAMING_SNAKE_CASE						)
								UpperCamelCase__		:    List[Any]         =model(__SCREAMING_SNAKE_CASE						)
								self.parent.assertEqual(result.last_hidden_state.shape						,  (self.batch_size, self.seq_length, self.d_model)						)

								UpperCamelCase__		:    Dict         =False
								UpperCamelCase__		:    str         =model(__SCREAMING_SNAKE_CASE						)
								self.parent.assertEqual(result.last_hidden_state.shape						,  (self.batch_size, self.seq_length, self.d_model)						)

								UpperCamelCase__		:    List[str]         =False
								UpperCamelCase__		:    Union[str, Any]         =model(__SCREAMING_SNAKE_CASE						)
								self.parent.assertEqual(result.last_hidden_state.shape						,  (self.batch_size, self.seq_length, self.d_model)						)






			def    _lowerCAmelCase			(							self     :					Tuple						,  lowercase_     :					int						,  lowercase_     :					List[Any]						,  lowercase_     :					Optional[int]						,  lowercase_     :					Tuple						,  lowercase_     :					List[Any]						,  lowercase_     :					List[Any]						,  lowercase_     :					List[str]						,  lowercase_     :					Optional[int]						,  ):
								UpperCamelCase__		:    Union[str, Any]         =FunnelBaseModel(config=__SCREAMING_SNAKE_CASE						)
								model.to(__SCREAMING_SNAKE_CASE						)
								model.eval()
								UpperCamelCase__		:    Union[str, Any]         =model(__SCREAMING_SNAKE_CASE						,  attention_mask=__SCREAMING_SNAKE_CASE						,  token_type_ids=__SCREAMING_SNAKE_CASE						)
								UpperCamelCase__		:    int         =model(__SCREAMING_SNAKE_CASE						,  token_type_ids=__SCREAMING_SNAKE_CASE						)
								UpperCamelCase__		:    List[str]         =model(__SCREAMING_SNAKE_CASE						)
								self.parent.assertEqual(result.last_hidden_state.shape						,  (self.batch_size, 2, self.d_model)						)

								UpperCamelCase__		:    str         =False
								UpperCamelCase__		:    Optional[Any]         =model(__SCREAMING_SNAKE_CASE						)
								self.parent.assertEqual(result.last_hidden_state.shape						,  (self.batch_size, 3, self.d_model)						)

								UpperCamelCase__		:    Tuple         =False
								UpperCamelCase__		:    List[Any]         =model(__SCREAMING_SNAKE_CASE						)
								self.parent.assertEqual(result.last_hidden_state.shape						,  (self.batch_size, 2, self.d_model)						)






			def    _lowerCAmelCase			(							self     :					int						,  lowercase_     :					int						,  lowercase_     :					Any						,  lowercase_     :					int						,  lowercase_     :					Optional[int]						,  lowercase_     :					List[str]						,  lowercase_     :					str						,  lowercase_     :					str						,  lowercase_     :					Dict						,  ):
								UpperCamelCase__		:    Optional[Any]         =self.num_labels
								UpperCamelCase__		:    Optional[Any]         =FunnelForPreTraining(config=__SCREAMING_SNAKE_CASE						)
								model.to(__SCREAMING_SNAKE_CASE						)
								model.eval()
								UpperCamelCase__		:    Dict         =model(__SCREAMING_SNAKE_CASE						,  attention_mask=__SCREAMING_SNAKE_CASE						,  token_type_ids=__SCREAMING_SNAKE_CASE						,  labels=__SCREAMING_SNAKE_CASE						)
								self.parent.assertEqual(result.logits.shape						,  (self.batch_size, self.seq_length)						)






			def    _lowerCAmelCase			(							self     :					Dict						,  lowercase_     :					Optional[int]						,  lowercase_     :					Any						,  lowercase_     :					Optional[int]						,  lowercase_     :					int						,  lowercase_     :					List[Any]						,  lowercase_     :					Tuple						,  lowercase_     :					str						,  lowercase_     :					List[str]						,  ):
								UpperCamelCase__		:    Tuple         =FunnelForMaskedLM(config=__SCREAMING_SNAKE_CASE						)
								model.to(__SCREAMING_SNAKE_CASE						)
								model.eval()
								UpperCamelCase__		:    Any         =model(__SCREAMING_SNAKE_CASE						,  attention_mask=__SCREAMING_SNAKE_CASE						,  token_type_ids=__SCREAMING_SNAKE_CASE						,  labels=__SCREAMING_SNAKE_CASE						)
								self.parent.assertEqual(result.logits.shape						,  (self.batch_size, self.seq_length, self.vocab_size)						)






			def    _lowerCAmelCase			(							self     :					Any						,  lowercase_     :					Tuple						,  lowercase_     :					Any						,  lowercase_     :					Optional[Any]						,  lowercase_     :					str						,  lowercase_     :					Any						,  lowercase_     :					List[Any]						,  lowercase_     :					Tuple						,  lowercase_     :					Tuple						,  ):
								UpperCamelCase__		:    Union[str, Any]         =self.num_labels
								UpperCamelCase__		:    Union[str, Any]         =FunnelForSequenceClassification(__SCREAMING_SNAKE_CASE						)
								model.to(__SCREAMING_SNAKE_CASE						)
								model.eval()
								UpperCamelCase__		:    Optional[Any]         =model(__SCREAMING_SNAKE_CASE						,  attention_mask=__SCREAMING_SNAKE_CASE						,  token_type_ids=__SCREAMING_SNAKE_CASE						,  labels=__SCREAMING_SNAKE_CASE						)
								self.parent.assertEqual(result.logits.shape						,  (self.batch_size, self.num_labels)						)






			def    _lowerCAmelCase			(							self     :					Union[str, Any]						,  lowercase_     :					Optional[Any]						,  lowercase_     :					List[Any]						,  lowercase_     :					Dict						,  lowercase_     :					List[Any]						,  lowercase_     :					str						,  lowercase_     :					Union[str, Any]						,  lowercase_     :					int						,  lowercase_     :					int						,  ):
								UpperCamelCase__		:    Optional[Any]         =self.num_choices
								UpperCamelCase__		:    Tuple         =FunnelForMultipleChoice(config=__SCREAMING_SNAKE_CASE						)
								model.to(__SCREAMING_SNAKE_CASE						)
								model.eval()
								UpperCamelCase__		:    List[Any]         =input_ids.unsqueeze(1						).expand(-1						,  self.num_choices						,  -1						).contiguous()
								UpperCamelCase__		:    str         =token_type_ids.unsqueeze(1						).expand(-1						,  self.num_choices						,  -1						).contiguous()
								UpperCamelCase__		:    str         =input_mask.unsqueeze(1						).expand(-1						,  self.num_choices						,  -1						).contiguous()
								UpperCamelCase__		:    Tuple         =model(
								    __SCREAMING_SNAKE_CASE						,  attention_mask=__SCREAMING_SNAKE_CASE						,  token_type_ids=__SCREAMING_SNAKE_CASE						,  labels=__SCREAMING_SNAKE_CASE						,  )
								self.parent.assertEqual(result.logits.shape						,  (self.batch_size, self.num_choices)						)






			def    _lowerCAmelCase			(							self     :					Any						,  lowercase_     :					Any						,  lowercase_     :					Any						,  lowercase_     :					Dict						,  lowercase_     :					Optional[Any]						,  lowercase_     :					Any						,  lowercase_     :					Tuple						,  lowercase_     :					Optional[int]						,  lowercase_     :					List[str]						,  ):
								UpperCamelCase__		:    Union[str, Any]         =self.num_labels
								UpperCamelCase__		:    int         =FunnelForTokenClassification(config=__SCREAMING_SNAKE_CASE						)
								model.to(__SCREAMING_SNAKE_CASE						)
								model.eval()
								UpperCamelCase__		:    Any         =model(__SCREAMING_SNAKE_CASE						,  attention_mask=__SCREAMING_SNAKE_CASE						,  token_type_ids=__SCREAMING_SNAKE_CASE						,  labels=__SCREAMING_SNAKE_CASE						)
								self.parent.assertEqual(result.logits.shape						,  (self.batch_size, self.seq_length, self.num_labels)						)






			def    _lowerCAmelCase			(							self     :					Optional[int]						,  lowercase_     :					Any						,  lowercase_     :					str						,  lowercase_     :					Any						,  lowercase_     :					Dict						,  lowercase_     :					Optional[int]						,  lowercase_     :					Tuple						,  lowercase_     :					List[Any]						,  lowercase_     :					Union[str, Any]						,  ):
								UpperCamelCase__		:    List[str]         =FunnelForQuestionAnswering(config=__SCREAMING_SNAKE_CASE						)
								model.to(__SCREAMING_SNAKE_CASE						)
								model.eval()
								UpperCamelCase__		:    Tuple         =model(
								    __SCREAMING_SNAKE_CASE						,  attention_mask=__SCREAMING_SNAKE_CASE						,  token_type_ids=__SCREAMING_SNAKE_CASE						,  start_positions=__SCREAMING_SNAKE_CASE						,  end_positions=__SCREAMING_SNAKE_CASE						,  )
								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    _lowerCAmelCase			(							self     :					Dict						):
								UpperCamelCase__		:    Union[str, Any]         =self.prepare_config_and_inputs()
								(
								    (
								    UpperCamelCase__ 
								)  ,			(
								    UpperCamelCase__ 
								)  ,			(
								    UpperCamelCase__ 
								)  ,			(
								    UpperCamelCase__ 
								)  ,			(
								    UpperCamelCase__ 
								)  ,			(
								    UpperCamelCase__ 
								)  ,			(
								    UpperCamelCase__ 
								)  ,			(
								    UpperCamelCase__ 
								)  ,			 
								)		:    Any         =config_and_inputs
								UpperCamelCase__		:    Dict         ={'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
								return config, inputs_dict



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

			"""simple docstring"""

			SCREAMING_SNAKE_CASE_						     =						False
			SCREAMING_SNAKE_CASE_						     =						False
			SCREAMING_SNAKE_CASE_						     =						(
			    (
			        FunnelModel,
			        FunnelForMaskedLM,
			        FunnelForPreTraining,
			        FunnelForQuestionAnswering,
			        FunnelForTokenClassification,
			    )
			    if is_torch_available()
			    else ()
			)
			SCREAMING_SNAKE_CASE_						     =						(
			    {
			        """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    _lowerCAmelCase			(							self     :					Union[str, Any]						,  lowercase_     :					Tuple						,  lowercase_     :					Any						,  lowercase_     :					Optional[Any]=False						):
								UpperCamelCase__		:    int         =super()._prepare_for_class(__SCREAMING_SNAKE_CASE						,  __SCREAMING_SNAKE_CASE						,  return_labels=__SCREAMING_SNAKE_CASE						)

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






			def    _lowerCAmelCase			(							self     :					List[Any]						):
								UpperCamelCase__		:    str         =FunnelModelTester(self						)
								UpperCamelCase__		:    List[Any]         =ConfigTester(self						,  config_class=__SCREAMING_SNAKE_CASE						)






			def    _lowerCAmelCase			(							self     :					str						):
								self.config_tester.run_common_tests()






			def    _lowerCAmelCase			(							self     :					str						):
								UpperCamelCase__		:    Union[str, Any]         =self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*__SCREAMING_SNAKE_CASE						)






			def    _lowerCAmelCase			(							self     :					Dict						):
								UpperCamelCase__		:    List[Any]         =self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_pretraining(*__SCREAMING_SNAKE_CASE						)






			def    _lowerCAmelCase			(							self     :					Optional[int]						):
								UpperCamelCase__		:    Union[str, Any]         =self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_masked_lm(*__SCREAMING_SNAKE_CASE						)






			def    _lowerCAmelCase			(							self     :					Any						):
								UpperCamelCase__		:    List[Any]         =self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_token_classification(*__SCREAMING_SNAKE_CASE						)






			def    _lowerCAmelCase			(							self     :					str						):
								UpperCamelCase__		:    Optional[Any]         =self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_question_answering(*__SCREAMING_SNAKE_CASE						)






			def    _lowerCAmelCase			(							self     :					Optional[int]						,  lowercase_     :					List[Any]						):

								if hasattr(__SCREAMING_SNAKE_CASE						,  '''weight'''						) and module.weight is not None:
													module.weight.data.fill_(3						)
								if hasattr(__SCREAMING_SNAKE_CASE						,  '''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(__SCREAMING_SNAKE_CASE						,  __SCREAMING_SNAKE_CASE						) and getattr(__SCREAMING_SNAKE_CASE						,  __SCREAMING_SNAKE_CASE						) is not None:
																		UpperCamelCase__		:    str         =getattr(__SCREAMING_SNAKE_CASE						,  __SCREAMING_SNAKE_CASE						)
																		weight.data.fill_(3						)



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

			"""simple docstring"""

			SCREAMING_SNAKE_CASE_						     =						False
			SCREAMING_SNAKE_CASE_						     =						False
			SCREAMING_SNAKE_CASE_						     =						(
			    (FunnelBaseModel, FunnelForMultipleChoice, FunnelForSequenceClassification) if is_torch_available() else ()
			)






			def    _lowerCAmelCase			(							self     :					Dict						):
								UpperCamelCase__		:    Dict         =FunnelModelTester(self						,  base=__SCREAMING_SNAKE_CASE						)
								UpperCamelCase__		:    Optional[int]         =ConfigTester(self						,  config_class=__SCREAMING_SNAKE_CASE						)






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






			def    _lowerCAmelCase			(							self     :					int						):
								UpperCamelCase__		:    Dict         =self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_base_model(*__SCREAMING_SNAKE_CASE						)






			def    _lowerCAmelCase			(							self     :					Optional[int]						):
								UpperCamelCase__		:    Tuple         =self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_sequence_classification(*__SCREAMING_SNAKE_CASE						)






			def    _lowerCAmelCase			(							self     :					str						):
								UpperCamelCase__		:    Union[str, Any]         =self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_multiple_choice(*__SCREAMING_SNAKE_CASE						)






			def    _lowerCAmelCase			(							self     :					Tuple						):
								UpperCamelCase__  ,			UpperCamelCase__		:    Optional[int]         =self.model_tester.prepare_config_and_inputs_for_common()
								UpperCamelCase__		:    Optional[int]         =True

								for model_class in self.all_model_classes:
													if model_class.__name__ == "FunnelBaseModel":
																		continue
													UpperCamelCase__		:    List[Any]         =model_class(__SCREAMING_SNAKE_CASE						)
													model.to(__SCREAMING_SNAKE_CASE						)
													model.train()
													UpperCamelCase__		:    Dict         =self._prepare_for_class(__SCREAMING_SNAKE_CASE						,  __SCREAMING_SNAKE_CASE						,  return_labels=__SCREAMING_SNAKE_CASE						)
													UpperCamelCase__		:    Union[str, Any]         =model(**__SCREAMING_SNAKE_CASE						).loss
													loss.backward()






			def    _lowerCAmelCase			(							self     :					Dict						,  lowercase_     :					List[str]						):

								if hasattr(__SCREAMING_SNAKE_CASE						,  '''weight'''						) and module.weight is not None:
													module.weight.data.fill_(3						)
								if hasattr(__SCREAMING_SNAKE_CASE						,  '''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(__SCREAMING_SNAKE_CASE						,  __SCREAMING_SNAKE_CASE						) and getattr(__SCREAMING_SNAKE_CASE						,  __SCREAMING_SNAKE_CASE						) is not None:
																		UpperCamelCase__		:    Any         =getattr(__SCREAMING_SNAKE_CASE						,  __SCREAMING_SNAKE_CASE						)
																		weight.data.fill_(3						)



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

			"""simple docstring"""

			def    _lowerCAmelCase			(							self     :					str						):
								UpperCamelCase__		:    int         =13
								UpperCamelCase__		:    Union[str, Any]         =7
								UpperCamelCase__		:    str         =torch.arange(0						,  batch_size * sequence_length						).long().reshape(__SCREAMING_SNAKE_CASE						,  __SCREAMING_SNAKE_CASE						)
								UpperCamelCase__		:    Dict         =[0, 1, 2, 3, 4, 5, 6, 4, 1, 3, 5, 0, 1]
								UpperCamelCase__		:    List[Any]         =torch.tensor([[2] + [0] * a + [1] * (sequence_length - a - 1) for a in lengths]						)

								UpperCamelCase__		:    List[str]         =FunnelModel.from_pretrained('''sgugger/funnel-random-tiny'''						)
								UpperCamelCase__		:    Dict         =model(__SCREAMING_SNAKE_CASE						,  token_type_ids=__SCREAMING_SNAKE_CASE						)[0].abs()

								UpperCamelCase__		:    Optional[Any]         =torch.tensor(2_3_4_4.8_3_5_2						)
								UpperCamelCase__		:    Tuple         =torch.tensor(0.8_0_5_2						)
								self.assertTrue(torch.allclose(output.sum()						,  __SCREAMING_SNAKE_CASE						,  atol=1e-4						)						)
								self.assertTrue(torch.allclose(output.mean()						,  __SCREAMING_SNAKE_CASE						,  atol=1e-4						)						)

								UpperCamelCase__		:    List[str]         =torch.tensor([[1] * 7, [1] * 4 + [0] * 3] * 6 + [[0, 1, 1, 0, 0, 1, 1]]						)
								UpperCamelCase__		:    Optional[Any]         =model(__SCREAMING_SNAKE_CASE						,  attention_mask=__SCREAMING_SNAKE_CASE						,  token_type_ids=__SCREAMING_SNAKE_CASE						)[0].abs()

								UpperCamelCase__		:    Dict         =torch.tensor(2_3_4_3.8_4_2_5						)
								UpperCamelCase__		:    Optional[Any]         =torch.tensor(0.8_0_4_9						)
								self.assertTrue(torch.allclose(output.sum()						,  __SCREAMING_SNAKE_CASE						,  atol=1e-4						)						)
								self.assertTrue(torch.allclose(output.mean()						,  __SCREAMING_SNAKE_CASE						,  atol=1e-4						)						)






			@slow
			def    _lowerCAmelCase			(							self     :					Union[str, Any]						):
								UpperCamelCase__		:    str         =FunnelTokenizer.from_pretrained('''huggingface/funnel-small'''						)
								UpperCamelCase__		:    int         =FunnelModel.from_pretrained('''huggingface/funnel-small'''						)
								UpperCamelCase__		:    Optional[int]         =tokenizer('''Hello! I am the Funnel Transformer model.'''						,  return_tensors='''pt'''						)
								UpperCamelCase__		:    List[str]         =model(**__SCREAMING_SNAKE_CASE						)[0]

								UpperCamelCase__		:    Any         =torch.tensor(2_3_5.7_2_4_6						)
								UpperCamelCase__		:    Optional[int]         =torch.tensor(0.0_2_5_6						)
								self.assertTrue(torch.allclose(output.sum()						,  __SCREAMING_SNAKE_CASE						,  atol=1e-4						)						)
								self.assertTrue(torch.allclose(output.mean()						,  __SCREAMING_SNAKE_CASE						,  atol=1e-4						)						)





"""simple docstring"""





import io
import json
import subprocess


_SCREAMING_SNAKE_CASE       :  List[Any]        = [
    ["""en""", """ru"""],
    ["""ru""", """en"""],
    ["""en""", """de"""],
    ["""de""", """en"""],
]

_SCREAMING_SNAKE_CASE       :  str        = 8



def        _lowerCAmelCase      (						UpperCAmelCase     :							Tuple      ,							UpperCAmelCase     :							List[Any]				):


					'''simple docstring'''






					UpperCamelCase__		:    Any         ={}
					for src, tgt in pairs:
										UpperCamelCase__		:    Optional[int]         =F'''{src}-{tgt}'''
										UpperCamelCase__		:    int         =F'''sacrebleu -t wmt19 -l {pair} --echo src'''.split()
										UpperCamelCase__		:    str         =subprocess.run(UpperCAmelCase      ,							stdout=subprocess.PIPE				).stdout.decode('''utf-8'''				).splitlines()
										UpperCamelCase__		:    List[Any]         =F'''sacrebleu -t wmt19 -l {pair} --echo ref'''.split()
										UpperCamelCase__		:    str         =subprocess.run(UpperCAmelCase      ,							stdout=subprocess.PIPE				).stdout.decode('''utf-8'''				).splitlines()
										UpperCamelCase__		:    Any         ={'''src''': src_lines[:n_objs], '''tgt''': tgt_lines[:n_objs]}
					return text


_SCREAMING_SNAKE_CASE       :  List[str]        = get_all_data(pairs, n_objs)
_SCREAMING_SNAKE_CASE       :  Optional[Any]        = """./fsmt_val_data.json"""
with io.open(filename, """w""", encoding="""utf-8""") as f:
				_SCREAMING_SNAKE_CASE       :  Any        = json.dump(text, f, indent=2, ensure_ascii=False)


