
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					:




				def __init__(					self				,				_UpperCamelCase				,				_UpperCamelCase=13				,				_UpperCamelCase=7				,				_UpperCamelCase=True				,				_UpperCamelCase=True				,				_UpperCamelCase=True				,				_UpperCamelCase=True				,				_UpperCamelCase=99				,				_UpperCamelCase=[1, 1, 2]				,				_UpperCamelCase=1				,				_UpperCamelCase=32				,				_UpperCamelCase=4				,				_UpperCamelCase=8				,				_UpperCamelCase=37				,				_UpperCamelCase="gelu_new"				,				_UpperCamelCase=0.1				,				_UpperCamelCase=0.1				,				_UpperCamelCase=0.0				,				_UpperCamelCase=512				,				_UpperCamelCase=3				,				_UpperCamelCase=0.02				,				_UpperCamelCase=3				,				_UpperCamelCase=4				,				_UpperCamelCase=None				,				_UpperCamelCase=False				,				)				->      Tuple:
							lowerCAmelCase_						=	parent
							lowerCAmelCase_						=	batch_size
							lowerCAmelCase_						=	seq_length
							lowerCAmelCase_						=	is_training
							lowerCAmelCase_						=	use_input_mask
							lowerCAmelCase_						=	use_token_type_ids
							lowerCAmelCase_						=	use_labels
							lowerCAmelCase_						=	vocab_size
							lowerCAmelCase_						=	block_sizes
							lowerCAmelCase_						=	num_decoder_layers
							lowerCAmelCase_						=	d_model
							lowerCAmelCase_						=	n_head
							lowerCAmelCase_						=	d_head
							lowerCAmelCase_						=	d_inner
							lowerCAmelCase_						=	hidden_act
							lowerCAmelCase_						=	hidden_dropout
							lowerCAmelCase_						=	attention_dropout
							lowerCAmelCase_						=	activation_dropout
							lowerCAmelCase_						=	max_position_embeddings
							lowerCAmelCase_						=	type_vocab_size
							lowerCAmelCase_						=	2
							lowerCAmelCase_						=	num_labels
							lowerCAmelCase_						=	num_choices
							lowerCAmelCase_						=	scope
							lowerCAmelCase_						=	initializer_std

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




				def      __a						(					self     )				->      List[str]:
							lowerCAmelCase_						=	ids_tensor([self.batch_size, self.seq_length]				,				self.vocab_size     )

							lowerCAmelCase_						=	None
							if self.use_input_mask:
										lowerCAmelCase_						=	random_attention_mask([self.batch_size, self.seq_length]     )

							lowerCAmelCase_						=	None
							if self.use_token_type_ids:
										lowerCAmelCase_						=	ids_tensor([self.batch_size, self.seq_length]				,				self.type_vocab_size     )

							lowerCAmelCase_						=	None
							lowerCAmelCase_						=	None
							lowerCAmelCase_						=	None
							if self.use_labels:
										lowerCAmelCase_						=	ids_tensor([self.batch_size]				,				self.type_sequence_label_size     )
										lowerCAmelCase_						=	ids_tensor([self.batch_size, self.seq_length]				,				self.num_labels     )
										lowerCAmelCase_						=	ids_tensor([self.batch_size]				,				self.num_choices     )

							lowerCAmelCase_						=	FunnelConfig(
							    vocab_size=self.vocab_size				,				block_sizes=self.block_sizes				,				num_decoder_layers=self.num_decoder_layers				,				d_model=self.d_model				,				n_head=self.n_head				,				d_head=self.d_head				,				d_inner=self.d_inner				,				hidden_act=self.hidden_act				,				hidden_dropout=self.hidden_dropout				,				attention_dropout=self.attention_dropout				,				activation_dropout=self.activation_dropout				,				max_position_embeddings=self.max_position_embeddings				,				type_vocab_size=self.type_vocab_size				,				initializer_std=self.initializer_std				,				)

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




				def      __a						(					self				,				_UpperCamelCase				,				_UpperCamelCase				,				_UpperCamelCase				,				_UpperCamelCase				,				_UpperCamelCase				,				_UpperCamelCase				,				_UpperCamelCase				,				)				->      Tuple:
							lowerCAmelCase_						=	TFFunnelModel(config=_UpperCamelCase     )
							lowerCAmelCase_						=	{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
							lowerCAmelCase_						=	model(_UpperCamelCase     )

							lowerCAmelCase_						=	[input_ids, input_mask]
							lowerCAmelCase_						=	model(_UpperCamelCase     )

							lowerCAmelCase_						=	model(_UpperCamelCase     )
							self.parent.assertEqual(result.last_hidden_state.shape				,				(self.batch_size, self.seq_length, self.d_model)     )

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

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




				def      __a						(					self				,				_UpperCamelCase				,				_UpperCamelCase				,				_UpperCamelCase				,				_UpperCamelCase				,				_UpperCamelCase				,				_UpperCamelCase				,				_UpperCamelCase				,				)				->      List[Any]:
							lowerCAmelCase_						=	TFFunnelBaseModel(config=_UpperCamelCase     )
							lowerCAmelCase_						=	{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
							lowerCAmelCase_						=	model(_UpperCamelCase     )

							lowerCAmelCase_						=	[input_ids, input_mask]
							lowerCAmelCase_						=	model(_UpperCamelCase     )

							lowerCAmelCase_						=	model(_UpperCamelCase     )
							self.parent.assertEqual(result.last_hidden_state.shape				,				(self.batch_size, 2, self.d_model)     )

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

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




				def      __a						(					self				,				_UpperCamelCase				,				_UpperCamelCase				,				_UpperCamelCase				,				_UpperCamelCase				,				_UpperCamelCase				,				_UpperCamelCase				,				_UpperCamelCase				,				)				->      Dict:
							lowerCAmelCase_						=	TFFunnelForPreTraining(config=_UpperCamelCase     )
							lowerCAmelCase_						=	{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
							lowerCAmelCase_						=	model(_UpperCamelCase     )
							self.parent.assertEqual(result.logits.shape				,				(self.batch_size, self.seq_length)     )




				def      __a						(					self				,				_UpperCamelCase				,				_UpperCamelCase				,				_UpperCamelCase				,				_UpperCamelCase				,				_UpperCamelCase				,				_UpperCamelCase				,				_UpperCamelCase				,				)				->      Tuple:
							lowerCAmelCase_						=	TFFunnelForMaskedLM(config=_UpperCamelCase     )
							lowerCAmelCase_						=	{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
							lowerCAmelCase_						=	model(_UpperCamelCase     )
							self.parent.assertEqual(result.logits.shape				,				(self.batch_size, self.seq_length, self.vocab_size)     )




				def      __a						(					self				,				_UpperCamelCase				,				_UpperCamelCase				,				_UpperCamelCase				,				_UpperCamelCase				,				_UpperCamelCase				,				_UpperCamelCase				,				_UpperCamelCase				,				)				->      int:
							lowerCAmelCase_						=	self.num_labels
							lowerCAmelCase_						=	TFFunnelForSequenceClassification(config=_UpperCamelCase     )
							lowerCAmelCase_						=	{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
							lowerCAmelCase_						=	model(_UpperCamelCase     )
							self.parent.assertEqual(result.logits.shape				,				(self.batch_size, self.num_labels)     )




				def      __a						(					self				,				_UpperCamelCase				,				_UpperCamelCase				,				_UpperCamelCase				,				_UpperCamelCase				,				_UpperCamelCase				,				_UpperCamelCase				,				_UpperCamelCase				,				)				->      str:
							lowerCAmelCase_						=	self.num_choices
							lowerCAmelCase_						=	TFFunnelForMultipleChoice(config=_UpperCamelCase     )
							lowerCAmelCase_						=	tf.tile(tf.expand_dims(_UpperCamelCase				,				1     )				,				(1, self.num_choices, 1)     )
							lowerCAmelCase_						=	tf.tile(tf.expand_dims(_UpperCamelCase				,				1     )				,				(1, self.num_choices, 1)     )
							lowerCAmelCase_						=	tf.tile(tf.expand_dims(_UpperCamelCase				,				1     )				,				(1, self.num_choices, 1)     )
							lowerCAmelCase_						=	{
							    "input_ids": multiple_choice_inputs_ids,
							    "attention_mask": multiple_choice_input_mask,
							    "token_type_ids": multiple_choice_token_type_ids,
							}
							lowerCAmelCase_						=	model(_UpperCamelCase     )
							self.parent.assertEqual(result.logits.shape				,				(self.batch_size, self.num_choices)     )




				def      __a						(					self				,				_UpperCamelCase				,				_UpperCamelCase				,				_UpperCamelCase				,				_UpperCamelCase				,				_UpperCamelCase				,				_UpperCamelCase				,				_UpperCamelCase				,				)				->      Optional[Any]:
							lowerCAmelCase_						=	self.num_labels
							lowerCAmelCase_						=	TFFunnelForTokenClassification(config=_UpperCamelCase     )
							lowerCAmelCase_						=	{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
							lowerCAmelCase_						=	model(_UpperCamelCase     )
							self.parent.assertEqual(result.logits.shape				,				(self.batch_size, self.seq_length, self.num_labels)     )




				def      __a						(					self				,				_UpperCamelCase				,				_UpperCamelCase				,				_UpperCamelCase				,				_UpperCamelCase				,				_UpperCamelCase				,				_UpperCamelCase				,				_UpperCamelCase				,				)				->      int:
							lowerCAmelCase_						=	TFFunnelForQuestionAnswering(config=_UpperCamelCase     )
							lowerCAmelCase_						=	{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
							lowerCAmelCase_						=	model(_UpperCamelCase     )
							self.parent.assertEqual(result.start_logits.shape				,				(self.batch_size, self.seq_length)     )
							self.parent.assertEqual(result.end_logits.shape				,				(self.batch_size, self.seq_length)     )




				def      __a						(					self     )				->      Tuple:
							lowerCAmelCase_						=	self.prepare_config_and_inputs()
							(
							    (
							    lowerCAmelCase_
							)	,  (
							    lowerCAmelCase_
							)	,  (
							    lowerCAmelCase_
							)	,  (
							    lowerCAmelCase_
							)	,  (
							    lowerCAmelCase_
							)	,  (
							    lowerCAmelCase_
							)	,  (
							    lowerCAmelCase_
							)	,  
							)						=	config_and_inputs
							lowerCAmelCase_						=	{"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
							return config, inputs_dict





@require_tf
class 	_lowerCAmelCase					(	__a					,      __a					,      unittest.TestCase       ):
				_lowercase					=(
				    (
				        TFFunnelModel,
				        TFFunnelForMaskedLM,
				        TFFunnelForPreTraining,
				        TFFunnelForQuestionAnswering,
				        TFFunnelForTokenClassification,
				    )
				    if is_tf_available()
				    else ()
				)
				_lowercase					=(
				    {
				        '''feature-extraction''': (TFFunnelBaseModel, TFFunnelModel),
				        '''fill-mask''': TFFunnelForMaskedLM,
				        '''question-answering''': TFFunnelForQuestionAnswering,
				        '''text-classification''': TFFunnelForSequenceClassification,
				        '''token-classification''': TFFunnelForTokenClassification,
				        '''zero-shot''': TFFunnelForSequenceClassification,
				    }
				    if is_tf_available()
				    else {}
				)
				_lowercase					=False
				_lowercase					=False




				def      __a						(					self     )				->      Any:
							lowerCAmelCase_						=	TFFunnelModelTester(self     )
							lowerCAmelCase_						=	ConfigTester(self				,				config_class=_UpperCamelCase     )




				def      __a						(					self     )				->      List[str]:
							self.config_tester.run_common_tests()




				def      __a						(					self     )				->      List[Any]:
							lowerCAmelCase_						=	self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*_UpperCamelCase     )




				def      __a						(					self     )				->      Tuple:
							lowerCAmelCase_						=	self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_pretraining(*_UpperCamelCase     )




				def      __a						(					self     )				->      Any:
							lowerCAmelCase_						=	self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_masked_lm(*_UpperCamelCase     )




				def      __a						(					self     )				->      Any:
							lowerCAmelCase_						=	self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_token_classification(*_UpperCamelCase     )




				def      __a						(					self     )				->      Any:
							lowerCAmelCase_						=	self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_question_answering(*_UpperCamelCase     )





@require_tf
class 	_lowerCAmelCase					(	__a					,      unittest.TestCase       ):
				_lowercase					=(
				    (TFFunnelBaseModel, TFFunnelForMultipleChoice, TFFunnelForSequenceClassification) if is_tf_available() else ()
				)
				_lowercase					=False
				_lowercase					=False




				def      __a						(					self     )				->      str:
							lowerCAmelCase_						=	TFFunnelModelTester(self				,				base=_UpperCamelCase     )
							lowerCAmelCase_						=	ConfigTester(self				,				config_class=_UpperCamelCase     )




				def      __a						(					self     )				->      List[Any]:
							self.config_tester.run_common_tests()




				def      __a						(					self     )				->      List[str]:
							lowerCAmelCase_						=	self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_base_model(*_UpperCamelCase     )




				def      __a						(					self     )				->      Optional[int]:
							lowerCAmelCase_						=	self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_sequence_classification(*_UpperCamelCase     )




				def      __a						(					self     )				->      List[str]:
							lowerCAmelCase_						=	self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_multiple_choice(*_UpperCamelCase     )





from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_sentencepiece_available,
    is_tokenizers_available,
    is_torch_available,
)


_A   =     {}

try:
			if not is_sentencepiece_available():
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			pass
else:
			_A   =     ["NllbTokenizer"]

try:
			if not is_tokenizers_available():
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			pass
else:
			_A   =     ["NllbTokenizerFast"]


if TYPE_CHECKING:
			try:
						if not is_sentencepiece_available():
									raise OptionalDependencyNotAvailable()
			except OptionalDependencyNotAvailable:
						pass
			else:
						from .tokenization_nllb import NllbTokenizer

			try:
						if not is_tokenizers_available():
									raise OptionalDependencyNotAvailable()
			except OptionalDependencyNotAvailable:
						pass
			else:
						from .tokenization_nllb_fast import NllbTokenizerFast

else:
			import sys

			_A   =     _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)



