





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    _UpperCAmelCase							:
      def __init__(		self      ,       a__      ,       a__=13      ,       a__=7      ,       a__=True      ,       a__=True      ,       a__=True      ,       a__=True      ,       a__=99      ,       a__=[1, 1, 2]      ,       a__=1      ,       a__=32      ,       a__=4      ,       a__=8      ,       a__=37      ,       a__="gelu_new"      ,       a__=0.1      ,       a__=0.1      ,       a__=0.0      ,       a__=512      ,       a__=3      ,       a__=0.02      ,       a__=3      ,       a__=4      ,       a__=None      ,       a__=False      ,       ):
         A_     :				Dict			      =	parent
         A_     :				List[str]			      =	batch_size
         A_     :				Any			      =	seq_length
         A_     :				Optional[int]			      =	is_training
         A_     :				Optional[Any]			      =	use_input_mask
         A_     :				int			      =	use_token_type_ids
         A_     :				Any			      =	use_labels
         A_     :				Dict			      =	vocab_size
         A_     :				Any			      =	block_sizes
         A_     :				str			      =	num_decoder_layers
         A_     :				List[Any]			      =	d_model
         A_     :				Optional[Any]			      =	n_head
         A_     :				Optional[Any]			      =	d_head
         A_     :				int			      =	d_inner
         A_     :				Dict			      =	hidden_act
         A_     :				Tuple			      =	hidden_dropout
         A_     :				Tuple			      =	attention_dropout
         A_     :				Optional[Any]			      =	activation_dropout
         A_     :				Any			      =	max_position_embeddings
         A_     :				Tuple			      =	type_vocab_size
         A_     :				str			      =	2
         A_     :				List[Any]			      =	num_labels
         A_     :				str			      =	num_choices
         A_     :				Dict			      =	scope
         A_     :				Tuple			      =	initializer_std

         # Used in the tests to check the size of the first attention layer
         A_     :				int			      =	n_head
         # Used in the tests to check the size of the first hidden state
         A_     :				List[Any]			      =	self.d_model
         # Used in the tests to check the number of output hidden states/attentions
         A_     :				Any			      =	sum(self.block_sizes       ) + (0 if base else self.num_decoder_layers)
         # FunnelModel adds two hidden layers: input embeddings and the sum of the upsampled encoder hidden state with
         # the last hidden state of the first block (which is the first hidden state of the decoder).
         if not base:
            A_     :				List[str]			      =	self.num_hidden_layers + 2
      def 		_lowerCamelCase      (		self       ):
         A_     :				Dict			      =	ids_tensor([self.batch_size, self.seq_length]      ,       self.vocab_size       )

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

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

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

         A_     :				Tuple			      =	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 		_lowerCamelCase      (		self      ,       a__      ,       a__      ,       a__      ,       a__      ,       a__      ,       a__      ,       a__      ,       ):
         A_     :				Dict			      =	TFFunnelModel(config=a__       )
         A_     :				str			      =	{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
         A_     :				str			      =	model(a__       )

         A_     :				List[str]			      =	[input_ids, input_mask]
         A_     :				str			      =	model(a__       )

         A_     :				List[Any]			      =	model(a__       )
         self.parent.assertEqual(result.last_hidden_state.shape      ,       (self.batch_size, self.seq_length, self.d_model)       )

         A_     :				Optional[int]			      =	False
         A_     :				Optional[int]			      =	TFFunnelModel(config=a__       )
         A_     :				Optional[Any]			      =	model(a__       )
         self.parent.assertEqual(result.last_hidden_state.shape      ,       (self.batch_size, self.seq_length, self.d_model)       )

         A_     :				Dict			      =	False
         A_     :				Optional[Any]			      =	TFFunnelModel(config=a__       )
         A_     :				Any			      =	model(a__       )
         self.parent.assertEqual(result.last_hidden_state.shape      ,       (self.batch_size, self.seq_length, self.d_model)       )
      def 		_lowerCamelCase      (		self      ,       a__      ,       a__      ,       a__      ,       a__      ,       a__      ,       a__      ,       a__      ,       ):
         A_     :				Optional[Any]			      =	TFFunnelBaseModel(config=a__       )
         A_     :				int			      =	{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
         A_     :				int			      =	model(a__       )

         A_     :				Tuple			      =	[input_ids, input_mask]
         A_     :				Optional[int]			      =	model(a__       )

         A_     :				List[Any]			      =	model(a__       )
         self.parent.assertEqual(result.last_hidden_state.shape      ,       (self.batch_size, 2, self.d_model)       )

         A_     :				str			      =	False
         A_     :				List[Any]			      =	TFFunnelBaseModel(config=a__       )
         A_     :				Optional[int]			      =	model(a__       )
         self.parent.assertEqual(result.last_hidden_state.shape      ,       (self.batch_size, 3, self.d_model)       )

         A_     :				Union[str, Any]			      =	False
         A_     :				List[Any]			      =	TFFunnelBaseModel(config=a__       )
         A_     :				str			      =	model(a__       )
         self.parent.assertEqual(result.last_hidden_state.shape      ,       (self.batch_size, 2, self.d_model)       )
      def 		_lowerCamelCase      (		self      ,       a__      ,       a__      ,       a__      ,       a__      ,       a__      ,       a__      ,       a__      ,       ):
         A_     :				str			      =	TFFunnelForPreTraining(config=a__       )
         A_     :				List[str]			      =	{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
         A_     :				str			      =	model(a__       )
         self.parent.assertEqual(result.logits.shape      ,       (self.batch_size, self.seq_length)       )
      def 		_lowerCamelCase      (		self      ,       a__      ,       a__      ,       a__      ,       a__      ,       a__      ,       a__      ,       a__      ,       ):
         A_     :				Optional[int]			      =	TFFunnelForMaskedLM(config=a__       )
         A_     :				Optional[Any]			      =	{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
         A_     :				Optional[Any]			      =	model(a__       )
         self.parent.assertEqual(result.logits.shape      ,       (self.batch_size, self.seq_length, self.vocab_size)       )
      def 		_lowerCamelCase      (		self      ,       a__      ,       a__      ,       a__      ,       a__      ,       a__      ,       a__      ,       a__      ,       ):
         A_     :				Optional[int]			      =	self.num_labels
         A_     :				str			      =	TFFunnelForSequenceClassification(config=a__       )
         A_     :				Optional[int]			      =	{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
         A_     :				Union[str, Any]			      =	model(a__       )
         self.parent.assertEqual(result.logits.shape      ,       (self.batch_size, self.num_labels)       )
      def 		_lowerCamelCase      (		self      ,       a__      ,       a__      ,       a__      ,       a__      ,       a__      ,       a__      ,       a__      ,       ):
         A_     :				Tuple			      =	self.num_choices
         A_     :				Any			      =	TFFunnelForMultipleChoice(config=a__       )
         A_     :				Any			      =	tf.tile(tf.expand_dims(a__      ,       1       )      ,       (1, self.num_choices, 1)       )
         A_     :				Dict			      =	tf.tile(tf.expand_dims(a__      ,       1       )      ,       (1, self.num_choices, 1)       )
         A_     :				Tuple			      =	tf.tile(tf.expand_dims(a__      ,       1       )      ,       (1, self.num_choices, 1)       )
         A_     :				Union[str, Any]			      =	{
             """input_ids""": multiple_choice_inputs_ids,
             """attention_mask""": multiple_choice_input_mask,
             """token_type_ids""": multiple_choice_token_type_ids,
         }
         A_     :				int			      =	model(a__       )
         self.parent.assertEqual(result.logits.shape      ,       (self.batch_size, self.num_choices)       )
      def 		_lowerCamelCase      (		self      ,       a__      ,       a__      ,       a__      ,       a__      ,       a__      ,       a__      ,       a__      ,       ):
         A_     :				Any			      =	self.num_labels
         A_     :				Dict			      =	TFFunnelForTokenClassification(config=a__       )
         A_     :				Any			      =	{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
         A_     :				Union[str, Any]			      =	model(a__       )
         self.parent.assertEqual(result.logits.shape      ,       (self.batch_size, self.seq_length, self.num_labels)       )
      def 		_lowerCamelCase      (		self      ,       a__      ,       a__      ,       a__      ,       a__      ,       a__      ,       a__      ,       a__      ,       ):
         A_     :				str			      =	TFFunnelForQuestionAnswering(config=a__       )
         A_     :				Optional[Any]			      =	{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
         A_     :				List[Any]			      =	model(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 		_lowerCamelCase      (		self       ):
         A_     :				List[Any]			      =	self.prepare_config_and_inputs()
         (
             (
             A_
         )      ,				(
             A_
         )      ,				(
             A_
         )      ,				(
             A_
         )      ,				(
             A_
         )      ,				(
             A_
         )      ,				(
             A_
         )      ,				
         )     :				Union[str, Any]			      =	config_and_inputs
         A_     :				Dict			      =	{"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
         return config, inputs_dict






@require_tf
class    _UpperCAmelCase							(		_lowerCamelCase			,       _lowerCamelCase			,       unittest.TestCase     ):
      a						       =							(
          (
              TFFunnelModel,
              TFFunnelForMaskedLM,
              TFFunnelForPreTraining,
              TFFunnelForQuestionAnswering,
              TFFunnelForTokenClassification,
          )
          if is_tf_available()
          else ()
      )
      a						       =							(
          {
              '''feature-extraction''': (TFFunnelBaseModel, TFFunnelModel),
              '''fill-mask''': TFFunnelForMaskedLM,
              '''question-answering''': TFFunnelForQuestionAnswering,
              '''text-classification''': TFFunnelForSequenceClassification,
              '''token-classification''': TFFunnelForTokenClassification,
              '''zero-shot''': TFFunnelForSequenceClassification,
          }
          if is_tf_available()
          else {}
      )
      a						       =							False
      a						       =							False
      def 		_lowerCamelCase      (		self       ):
         A_     :				Optional[int]			      =	TFFunnelModelTester(self       )
         A_     :				int			      =	ConfigTester(self      ,       config_class=a__       )
      def 		_lowerCamelCase      (		self       ):
         self.config_tester.run_common_tests()
      def 		_lowerCamelCase      (		self       ):
         A_     :				int			      =	self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*a__       )
      def 		_lowerCamelCase      (		self       ):
         A_     :				str			      =	self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_pretraining(*a__       )
      def 		_lowerCamelCase      (		self       ):
         A_     :				List[str]			      =	self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_masked_lm(*a__       )
      def 		_lowerCamelCase      (		self       ):
         A_     :				Union[str, Any]			      =	self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_token_classification(*a__       )






      def 		_lowerCamelCase      (		self       ):
         A_     :				str			      =	self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_question_answering(*a__       )






@require_tf
class    _UpperCAmelCase							(		_lowerCamelCase			,       unittest.TestCase     ):
      a						       =							(
          (TFFunnelBaseModel, TFFunnelForMultipleChoice, TFFunnelForSequenceClassification) if is_tf_available() else ()
      )
      a						       =							False
      a						       =							False
      def 		_lowerCamelCase      (		self       ):
         A_     :				Optional[Any]			      =	TFFunnelModelTester(self      ,       base=a__       )
         A_     :				str			      =	ConfigTester(self      ,       config_class=a__       )
      def 		_lowerCamelCase      (		self       ):
         self.config_tester.run_common_tests()
      def 		_lowerCamelCase      (		self       ):
         A_     :				Optional[Any]			      =	self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_base_model(*a__       )
      def 		_lowerCamelCase      (		self       ):
         A_     :				Dict			      =	self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_sequence_classification(*a__       )






      def 		_lowerCamelCase      (		self       ):
         A_     :				List[Any]			      =	self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_multiple_choice(*a__       )













from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


_lowerCAmelCase 				=     {
    """configuration_nllb_moe""": [
        """NLLB_MOE_PRETRAINED_CONFIG_ARCHIVE_MAP""",
        """NllbMoeConfig""",
    ]
}

try:
      if not is_torch_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      _lowerCAmelCase 				=     [
          """NLLB_MOE_PRETRAINED_MODEL_ARCHIVE_LIST""",
          """NllbMoeForConditionalGeneration""",
          """NllbMoeModel""",
          """NllbMoePreTrainedModel""",
          """NllbMoeTop2Router""",
          """NllbMoeSparseMLP""",
      ]


if TYPE_CHECKING:
      from .configuration_nllb_moe import (
          NLLB_MOE_PRETRAINED_CONFIG_ARCHIVE_MAP,
          NllbMoeConfig,
      )

      try:
            if not is_torch_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_nllb_moe import (
                NLLB_MOE_PRETRAINED_MODEL_ARCHIVE_LIST,
                NllbMoeForConditionalGeneration,
                NllbMoeModel,
                NllbMoePreTrainedModel,
                NllbMoeSparseMLP,
                NllbMoeTopaRouter,
            )


else:
      import sys

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






