

"""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      __UpperCamelCase					:


		def __init__(self					:						Dict	,	__SCREAMING_SNAKE_CASE					:						List[str]	,	__SCREAMING_SNAKE_CASE					:						Optional[int]=1_3	,	__SCREAMING_SNAKE_CASE					:						List[str]=7	,	__SCREAMING_SNAKE_CASE					:						Optional[Any]=True	,	__SCREAMING_SNAKE_CASE					:						Optional[int]=True	,	__SCREAMING_SNAKE_CASE					:						List[Any]=True	,	__SCREAMING_SNAKE_CASE					:						Any=True	,	__SCREAMING_SNAKE_CASE					:						Dict=9_9	,	__SCREAMING_SNAKE_CASE					:						int=[1, 1, 2]	,	__SCREAMING_SNAKE_CASE					:						List[str]=1	,	__SCREAMING_SNAKE_CASE					:						List[str]=3_2	,	__SCREAMING_SNAKE_CASE					:						Union[str, Any]=4	,	__SCREAMING_SNAKE_CASE					:						Tuple=8	,	__SCREAMING_SNAKE_CASE					:						List[str]=3_7	,	__SCREAMING_SNAKE_CASE					:						List[str]="gelu_new"	,	__SCREAMING_SNAKE_CASE					:						int=0.1	,	__SCREAMING_SNAKE_CASE					:						List[str]=0.1	,	__SCREAMING_SNAKE_CASE					:						Union[str, Any]=0.0	,	__SCREAMING_SNAKE_CASE					:						Optional[Any]=5_1_2	,	__SCREAMING_SNAKE_CASE					:						str=3	,	__SCREAMING_SNAKE_CASE					:						int=0.0_2	,	__SCREAMING_SNAKE_CASE					:						str=3	,	__SCREAMING_SNAKE_CASE					:						Optional[int]=4	,	__SCREAMING_SNAKE_CASE					:						Optional[Any]=None	,	__SCREAMING_SNAKE_CASE					:						List[Any]=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    SCREAMING_SNAKE_CASE__  (self					:						Optional[int]):
							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					:						Any):
							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					:						int	,	__SCREAMING_SNAKE_CASE					:						str	,	__SCREAMING_SNAKE_CASE					:						List[Any]	,	__SCREAMING_SNAKE_CASE					:						Any	,	__SCREAMING_SNAKE_CASE					:						Tuple	,	__SCREAMING_SNAKE_CASE					:						int	,	__SCREAMING_SNAKE_CASE					:						List[Any]	,	__SCREAMING_SNAKE_CASE					:						Tuple	,	__SCREAMING_SNAKE_CASE					:						List[str]	,	):
							A     =   FunnelModel(config=_lowerCAmelCase)
							model.to(_lowerCAmelCase)
							model.eval()
							A     =   model(_lowerCAmelCase	,	attention_mask=_lowerCAmelCase	,	token_type_ids=_lowerCAmelCase)
							A     =   model(_lowerCAmelCase	,	token_type_ids=_lowerCAmelCase)
							A     =   model(_lowerCAmelCase)
							self.parent.assertEqual(result.last_hidden_state.shape	,	(self.batch_size, self.seq_length, self.d_model))

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

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


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

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

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


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


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


		def    SCREAMING_SNAKE_CASE__  (self					:						int	,	__SCREAMING_SNAKE_CASE					:						Any	,	__SCREAMING_SNAKE_CASE					:						int	,	__SCREAMING_SNAKE_CASE					:						int	,	__SCREAMING_SNAKE_CASE					:						str	,	__SCREAMING_SNAKE_CASE					:						Dict	,	__SCREAMING_SNAKE_CASE					:						List[Any]	,	__SCREAMING_SNAKE_CASE					:						str	,	__SCREAMING_SNAKE_CASE					:						str	,	):
							A     =   self.num_labels
							A     =   FunnelForSequenceClassification(_lowerCAmelCase)
							model.to(_lowerCAmelCase)
							model.eval()
							A     =   model(_lowerCAmelCase	,	attention_mask=_lowerCAmelCase	,	token_type_ids=_lowerCAmelCase	,	labels=_lowerCAmelCase)
							self.parent.assertEqual(result.logits.shape	,	(self.batch_size, self.num_labels))


		def    SCREAMING_SNAKE_CASE__  (self					:						List[Any]	,	__SCREAMING_SNAKE_CASE					:						List[str]	,	__SCREAMING_SNAKE_CASE					:						Dict	,	__SCREAMING_SNAKE_CASE					:						Tuple	,	__SCREAMING_SNAKE_CASE					:						Dict	,	__SCREAMING_SNAKE_CASE					:						Any	,	__SCREAMING_SNAKE_CASE					:						Dict	,	__SCREAMING_SNAKE_CASE					:						List[str]	,	__SCREAMING_SNAKE_CASE					:						str	,	):
							A     =   self.num_choices
							A     =   FunnelForMultipleChoice(config=_lowerCAmelCase)
							model.to(_lowerCAmelCase)
							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(
							    _lowerCAmelCase	,	attention_mask=_lowerCAmelCase	,	token_type_ids=_lowerCAmelCase	,	labels=_lowerCAmelCase	,	)
							self.parent.assertEqual(result.logits.shape	,	(self.batch_size, self.num_choices))


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


		def    SCREAMING_SNAKE_CASE__  (self					:						Any	,	__SCREAMING_SNAKE_CASE					:						Tuple	,	__SCREAMING_SNAKE_CASE					:						Any	,	__SCREAMING_SNAKE_CASE					:						Any	,	__SCREAMING_SNAKE_CASE					:						Optional[int]	,	__SCREAMING_SNAKE_CASE					:						int	,	__SCREAMING_SNAKE_CASE					:						List[str]	,	__SCREAMING_SNAKE_CASE					:						Any	,	__SCREAMING_SNAKE_CASE					:						Optional[int]	,	):
							A     =   FunnelForQuestionAnswering(config=_lowerCAmelCase)
							model.to(_lowerCAmelCase)
							model.eval()
							A     =   model(
							    _lowerCAmelCase	,	attention_mask=_lowerCAmelCase	,	token_type_ids=_lowerCAmelCase	,	start_positions=_lowerCAmelCase	,	end_positions=_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    SCREAMING_SNAKE_CASE__  (self					:						List[Any]):
							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      __UpperCamelCase					(  A__	,						A__	,						unittest.TestCase		):
		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    SCREAMING_SNAKE_CASE__  (self					:						Optional[Any]	,	__SCREAMING_SNAKE_CASE					:						List[str]	,	__SCREAMING_SNAKE_CASE					:						Optional[int]	,	__SCREAMING_SNAKE_CASE					:						List[Any]=False):
							A     =   super()._prepare_for_class(_lowerCAmelCase	,	_lowerCAmelCase	,	return_labels=_lowerCAmelCase)

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


		def    SCREAMING_SNAKE_CASE__  (self					:						Dict):
							A     =   FunnelModelTester(self)
							A     =   ConfigTester(self	,	config_class=_lowerCAmelCase)


		def    SCREAMING_SNAKE_CASE__  (self					:						Dict):
							self.config_tester.run_common_tests()


		def    SCREAMING_SNAKE_CASE__  (self					:						Any):
							A     =   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*_lowerCAmelCase)


		def    SCREAMING_SNAKE_CASE__  (self					:						List[str]):
							A     =   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_pretraining(*_lowerCAmelCase)


		def    SCREAMING_SNAKE_CASE__  (self					:						Dict):
							A     =   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_masked_lm(*_lowerCAmelCase)


		def    SCREAMING_SNAKE_CASE__  (self					:						Any):
							A     =   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_token_classification(*_lowerCAmelCase)


		def    SCREAMING_SNAKE_CASE__  (self					:						str):
							A     =   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_question_answering(*_lowerCAmelCase)


		def    SCREAMING_SNAKE_CASE__  (self					:						str	,	__SCREAMING_SNAKE_CASE					:						Optional[int]):
							if hasattr(_lowerCAmelCase	,	"weight") and module.weight is not None:
												module.weight.data.fill_(3)
							if hasattr(_lowerCAmelCase	,	"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(_lowerCAmelCase	,	_lowerCAmelCase) and getattr(_lowerCAmelCase	,	_lowerCAmelCase) is not None:
																	A     =   getattr(_lowerCAmelCase	,	_lowerCAmelCase)
																	weight.data.fill_(3)





@require_torch
class      __UpperCamelCase					(  A__	,						unittest.TestCase		):
		SCREAMING_SNAKE_CASE														=       False
		SCREAMING_SNAKE_CASE														=       False
		SCREAMING_SNAKE_CASE														=       (
		    (FunnelBaseModel, FunnelForMultipleChoice, FunnelForSequenceClassification) if is_torch_available() else ()
		)


		def    SCREAMING_SNAKE_CASE__  (self					:						List[str]):
							A     =   FunnelModelTester(self	,	base=_lowerCAmelCase)
							A     =   ConfigTester(self	,	config_class=_lowerCAmelCase)


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


		def    SCREAMING_SNAKE_CASE__  (self					:						List[Any]):
							A     =   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_base_model(*_lowerCAmelCase)


		def    SCREAMING_SNAKE_CASE__  (self					:						Tuple):
							A     =   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_sequence_classification(*_lowerCAmelCase)


		def    SCREAMING_SNAKE_CASE__  (self					:						Dict):
							A     =   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_multiple_choice(*_lowerCAmelCase)


		def    SCREAMING_SNAKE_CASE__  (self					:						List[str]):
							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(_lowerCAmelCase)
												model.to(_lowerCAmelCase)
												model.train()
												A     =   self._prepare_for_class(_lowerCAmelCase	,	_lowerCAmelCase	,	return_labels=_lowerCAmelCase)
												A     =   model(**_lowerCAmelCase).loss
												loss.backward()


		def    SCREAMING_SNAKE_CASE__  (self					:						Optional[int]	,	__SCREAMING_SNAKE_CASE					:						Any):
							if hasattr(_lowerCAmelCase	,	"weight") and module.weight is not None:
												module.weight.data.fill_(3)
							if hasattr(_lowerCAmelCase	,	"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(_lowerCAmelCase	,	_lowerCAmelCase) and getattr(_lowerCAmelCase	,	_lowerCAmelCase) is not None:
																	A     =   getattr(_lowerCAmelCase	,	_lowerCAmelCase)
																	weight.data.fill_(3)





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


		def    SCREAMING_SNAKE_CASE__  (self					:						Union[str, Any]):
							A     =   1_3
							A     =   7
							A     =   torch.arange(0	,	batch_size * sequence_length).long().reshape(_lowerCAmelCase	,	_lowerCAmelCase)
							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(_lowerCAmelCase	,	token_type_ids=_lowerCAmelCase)[0].abs()

							A     =   torch.tensor(2_3_4_4.8_3_5_2)
							A     =   torch.tensor(0.8_0_5_2)
							self.assertTrue(torch.allclose(output.sum()	,	_lowerCAmelCase	,	atol=1E-4))
							self.assertTrue(torch.allclose(output.mean()	,	_lowerCAmelCase	,	atol=1E-4))

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

							A     =   torch.tensor(2_3_4_3.8_4_2_5)
							A     =   torch.tensor(0.8_0_4_9)
							self.assertTrue(torch.allclose(output.sum()	,	_lowerCAmelCase	,	atol=1E-4))
							self.assertTrue(torch.allclose(output.mean()	,	_lowerCAmelCase	,	atol=1E-4))


		@slow
		def    SCREAMING_SNAKE_CASE__  (self					:						Union[str, Any]):
							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(**_lowerCAmelCase)[0]

							A     =   torch.tensor(2_3_5.7_2_4_6)
							A     =   torch.tensor(0.0_2_5_6)
							self.assertTrue(torch.allclose(output.sum()	,	_lowerCAmelCase	,	atol=1E-4))
							self.assertTrue(torch.allclose(output.mean()	,	_lowerCAmelCase	,	atol=1E-4))












'''simple docstring'''


from __future__ import annotations

import unittest
import warnings

from transformers import AutoTokenizer, MarianConfig, MarianTokenizer, TranslationPipeline, is_tf_available
from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, slow
from transformers.utils import cached_property

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
				import tensorflow as tf

				from transformers import TFAutoModelForSeqaSeqLM, TFMarianModel, TFMarianMTModel



@require_tf
class 				__UpperCAmelCase	:







		'''simple docstring'''







		__lowerCAmelCase								=     MarianConfig
		__lowerCAmelCase								=     {}
		__lowerCAmelCase								=     '''gelu'''







		def __init__(self    :   Tuple   ,   _lowerCAmelCase    :   Union[str, Any]   ,   _lowerCAmelCase    :   Dict=13   ,   _lowerCAmelCase    :   Optional[int]=7   ,   _lowerCAmelCase    :   str=True   ,   _lowerCAmelCase    :   Optional[int]=False   ,   _lowerCAmelCase    :   Tuple=99   ,   _lowerCAmelCase    :   str=32   ,   _lowerCAmelCase    :   str=2   ,   _lowerCAmelCase    :   Union[str, Any]=4   ,   _lowerCAmelCase    :   Dict=37   ,   _lowerCAmelCase    :   int=0.1   ,   _lowerCAmelCase    :   List[Any]=0.1   ,   _lowerCAmelCase    :   Tuple=20   ,   _lowerCAmelCase    :   List[str]=2   ,   _lowerCAmelCase    :   Any=1   ,   _lowerCAmelCase    :   int=0   ,   ):
			A					=   parent
			A					=   batch_size
			A					=   seq_length
			A					=   is_training
			A					=   use_labels
			A					=   vocab_size
			A					=   hidden_size
			A					=   num_hidden_layers
			A					=   num_attention_heads
			A					=   intermediate_size

			A					=   hidden_dropout_prob
			A					=   attention_probs_dropout_prob
			A					=   max_position_embeddings
			A					=   eos_token_id
			A					=   pad_token_id
			A					=   bos_token_id







		def 							A  (self    :   int   ):
			A					=   ids_tensor([self.batch_size, self.seq_length - 1]   ,   self.vocab_size   )
			A					=   tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size   )   ,   1   )
			A					=   tf.concat([input_ids, eos_tensor]   ,   axis=1   )

			A					=   ids_tensor([self.batch_size, self.seq_length]   ,   self.vocab_size   )

			A					=   self.config_cls(
			    vocab_size=self.vocab_size   ,   d_model=self.hidden_size   ,   encoder_layers=self.num_hidden_layers   ,   decoder_layers=self.num_hidden_layers   ,   encoder_attention_heads=self.num_attention_heads   ,   decoder_attention_heads=self.num_attention_heads   ,   encoder_ffn_dim=self.intermediate_size   ,   decoder_ffn_dim=self.intermediate_size   ,   dropout=self.hidden_dropout_prob   ,   attention_dropout=self.attention_probs_dropout_prob   ,   max_position_embeddings=self.max_position_embeddings   ,   eos_token_ids=[2]   ,   bos_token_id=self.bos_token_id   ,   pad_token_id=self.pad_token_id   ,   decoder_start_token_id=self.pad_token_id   ,   **self.config_updates   ,   )
			A					=   prepare_marian_inputs_dict(_lowerCAmelCase   ,   _lowerCAmelCase   ,   _lowerCAmelCase   )
			return config, inputs_dict







		def 							A  (self    :   List[Any]   ,   _lowerCAmelCase    :   int   ,   _lowerCAmelCase    :   Any   ):
			A					=   TFMarianModel(config=_lowerCAmelCase   ).get_decoder()
			A					=   inputs_dict["""input_ids"""]

			A					=   input_ids[:1, :]
			A					=   inputs_dict["""attention_mask"""][:1, :]
			A					=   inputs_dict["""head_mask"""]
			A					=   1

			# first forward pass
			A					=   model(_lowerCAmelCase   ,   attention_mask=_lowerCAmelCase   ,   head_mask=_lowerCAmelCase   ,   use_cache=_lowerCAmelCase   )

			A	,     A					=   outputs.to_tuple()

			# create hypothetical next token and extent to next_input_ids
			A					=   ids_tensor((self.batch_size, 3)   ,   config.vocab_size   )
			A					=   tf.cast(ids_tensor((self.batch_size, 3)   ,   2   )   ,   tf.inta   )

			# append to next input_ids and
			A					=   tf.concat([input_ids, next_tokens]   ,   axis=-1   )
			A					=   tf.concat([attention_mask, next_attn_mask]   ,   axis=-1   )

			A					=   model(_lowerCAmelCase   ,   attention_mask=_lowerCAmelCase   )[0]
			A					=   model(_lowerCAmelCase   ,   attention_mask=_lowerCAmelCase   ,   past_key_values=_lowerCAmelCase   )[0]

			self.parent.assertEqual(next_tokens.shape[1]   ,   output_from_past.shape[1]   )

			# select random slice
			A					=   int(ids_tensor((1,)   ,   output_from_past.shape[-1]   )   )
			A					=   output_from_no_past[:, -3:, random_slice_idx]
			A					=   output_from_past[:, :, random_slice_idx]

			# test that outputs are equal for slice
			tf.debugging.assert_near(_lowerCAmelCase   ,   _lowerCAmelCase   ,   rtol=1e-3   )






def  __a			(  UpperCAmelCase							,      UpperCAmelCase							,      UpperCAmelCase							,      UpperCAmelCase=None							,      UpperCAmelCase=None							,      UpperCAmelCase=None							,      UpperCAmelCase=None							,      UpperCAmelCase=None							,      )   ->List[str]:



	"""simple docstring"""







	if attention_mask is None:
		A					=   tf.cast(tf.math.not_equal(UpperCAmelCase							,      config.pad_token_id     )							,      tf.inta     )
	if decoder_attention_mask is None:
		A					=   tf.concat(
		    [
		        tf.ones(decoder_input_ids[:, :1].shape							,      dtype=tf.inta     ),
		        tf.cast(tf.math.not_equal(decoder_input_ids[:, 1:]							,      config.pad_token_id     )							,      tf.inta     ),
		    ]							,      axis=-1							,      )
	if head_mask is None:
		A					=   tf.ones((config.encoder_layers, config.encoder_attention_heads)     )
	if decoder_head_mask is None:
		A					=   tf.ones((config.decoder_layers, config.decoder_attention_heads)     )
	if cross_attn_head_mask is None:
		A					=   tf.ones((config.decoder_layers, config.decoder_attention_heads)     )
	return {
	    "input_ids": input_ids,
	    "decoder_input_ids": decoder_input_ids,
	    "attention_mask": attention_mask,
	    "decoder_attention_mask": decoder_attention_mask,
	    "head_mask": head_mask,
	    "decoder_head_mask": decoder_head_mask,
	    "cross_attn_head_mask": cross_attn_head_mask,
	}



@require_tf
class 				__UpperCAmelCase	(							A__		,       A__		,       unittest.TestCase    ):







		'''simple docstring'''







		__lowerCAmelCase								=     (TFMarianMTModel, TFMarianModel) if is_tf_available() else ()
		__lowerCAmelCase								=     (TFMarianMTModel,) if is_tf_available() else ()
		__lowerCAmelCase								=     (
		    {
		        '''conversational''': TFMarianMTModel,
		        '''feature-extraction''': TFMarianModel,
		        '''summarization''': TFMarianMTModel,
		        '''text2text-generation''': TFMarianMTModel,
		        '''translation''': TFMarianMTModel,
		    }
		    if is_tf_available()
		    else {}
		)
		__lowerCAmelCase								=     True
		__lowerCAmelCase								=     False
		__lowerCAmelCase								=     False







		def 							A  (self    :   Tuple   ):
			A					=   TFMarianModelTester(self   )
			A					=   ConfigTester(self   ,   config_class=_lowerCAmelCase   )







		def 							A  (self    :   List[str]   ):
			self.config_tester.run_common_tests()







		def 							A  (self    :   Any   ):
			A					=   self.model_tester.prepare_config_and_inputs_for_common()
			self.model_tester.check_decoder_model_past_large_inputs(*_lowerCAmelCase   )






@require_tf
class 				__UpperCAmelCase	(							unittest.TestCase    ):







		'''simple docstring'''







		__lowerCAmelCase								=     10_00  # show more chars for failing integration tests







		@classmethod
		def 							A  (cls    :   Union[str, Any]   ):
			A					=   F"""Helsinki-NLP/opus-mt-{cls.src}-{cls.tgt}"""
			return cls







		@cached_property
		def 							A  (self    :   str   ):
			return AutoTokenizer.from_pretrained(self.model_name   )







		@property
		def 							A  (self    :   Optional[int]   ):
			return self.tokenizer.eos_token_id







		@cached_property
		def 							A  (self    :   Optional[Any]   ):
			warnings.simplefilter("""error"""   )
			A					=   TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name   )
			assert isinstance(_lowerCAmelCase   ,   _lowerCAmelCase   )
			A					=   model.config
			self.assertListEqual(c.bad_words_ids   ,   [[c.pad_token_id]]   )
			self.assertEqual(c.max_length   ,   512   )
			self.assertEqual(c.decoder_start_token_id   ,   c.pad_token_id   )
			return model







		def 							A  (self    :   Optional[int]   ,   **_lowerCAmelCase    :   Any   ):
			A					=   self.translate_src_text(**_lowerCAmelCase   )
			self.assertListEqual(self.expected_text   ,   _lowerCAmelCase   )







		def 							A  (self    :   Union[str, Any]   ,   **_lowerCAmelCase    :   Union[str, Any]   ):
			A					=   self.tokenizer(self.src_text   ,   **_lowerCAmelCase   ,   padding=_lowerCAmelCase   ,   return_tensors="""tf"""   )
			A					=   self.model.generate(
			    model_inputs.input_ids   ,   attention_mask=model_inputs.attention_mask   ,   num_beams=2   ,   max_length=128   )
			A					=   self.tokenizer.batch_decode(generated_ids.numpy()   ,   skip_special_tokens=_lowerCAmelCase   )
			return generated_words






@require_sentencepiece
@require_tokenizers
@require_tf
class 				__UpperCAmelCase	(							A__    ):







		'''simple docstring'''







		__lowerCAmelCase								=     '''mt'''
		__lowerCAmelCase								=     '''en'''
		__lowerCAmelCase								=     ['''Billi messu b\'mod ġentili, Ġesù fejjaq raġel li kien milqut bil - marda kerha tal - ġdiem.''']
		__lowerCAmelCase								=     ['''Touching gently, Jesus healed a man who was affected by the sad disease of leprosy.''']







		@unittest.skip("""Skipping until #12647 is resolved."""   )
		@slow
		def 							A  (self    :   str   ):
			self._assert_generated_batch_equal_expected()






@require_sentencepiece
@require_tokenizers
@require_tf
class 				__UpperCAmelCase	(							A__    ):







		'''simple docstring'''







		__lowerCAmelCase								=     '''en'''
		__lowerCAmelCase								=     '''zh'''
		__lowerCAmelCase								=     ['''My name is Wolfgang and I live in Berlin''']
		__lowerCAmelCase								=     ['''我叫沃尔夫冈 我住在柏林''']







		@unittest.skip("""Skipping until #12647 is resolved."""   )
		@slow
		def 							A  (self    :   Any   ):
			self._assert_generated_batch_equal_expected()






@require_sentencepiece
@require_tokenizers
@require_tf
class 				__UpperCAmelCase	(							A__    ):







		'''simple docstring'''







		__lowerCAmelCase								=     '''en'''
		__lowerCAmelCase								=     '''ROMANCE'''
		__lowerCAmelCase								=     [
		    '''>>fr<< Don\'t spend so much time watching TV.''',
		    '''>>pt<< Your message has been sent.''',
		    '''>>es<< He\'s two years older than me.''',
		]
		__lowerCAmelCase								=     [
		    '''Ne passez pas autant de temps à regarder la télé.''',
		    '''A sua mensagem foi enviada.''',
		    '''Es dos años más viejo que yo.''',
		]







		@unittest.skip("""Skipping until #12647 is resolved."""   )
		@slow
		def 							A  (self    :   Union[str, Any]   ):
			self._assert_generated_batch_equal_expected()







		@unittest.skip("""Skipping until #12647 is resolved."""   )
		@slow
		def 							A  (self    :   Union[str, Any]   ):
			A					=   TranslationPipeline(self.model   ,   self.tokenizer   ,   framework="""tf"""   )
			A					=   pipeline(self.src_text   )
			self.assertEqual(self.expected_text   ,   [x["""translation_text"""] for x in output]   )

