"""simple docstring"""
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        __snake_case	:



      def __init__(						self   ,      lowercase   ,      lowercase=13   ,      lowercase=7   ,      lowercase=True   ,      lowercase=True   ,      lowercase=True   ,      lowercase=True   ,      lowercase=99   ,      lowercase=[1, 1, 2]   ,      lowercase=1   ,      lowercase=32   ,      lowercase=4   ,      lowercase=8   ,      lowercase=37   ,      lowercase="gelu_new"   ,      lowercase=0.1   ,      lowercase=0.1   ,      lowercase=0.0   ,      lowercase=5_12   ,      lowercase=3   ,      lowercase=0.02   ,      lowercase=3   ,      lowercase=4   ,      lowercase=None   ,      lowercase=False   ,      )     ->      List[str]:





        '''simple docstring'''





        a__:					int      							=     parent
        a__:					Dict      							=     batch_size
        a__:					Union[str, Any]      							=     seq_length
        a__:					Union[str, Any]      							=     is_training
        a__:					int      							=     use_input_mask
        a__:					Optional[Any]      							=     use_token_type_ids
        a__:					Optional[Any]      							=     use_labels
        a__:					Dict      							=     vocab_size
        a__:					Any      							=     block_sizes
        a__:					Union[str, Any]      							=     num_decoder_layers
        a__:					Dict      							=     d_model
        a__:					Any      							=     n_head
        a__:					Union[str, Any]      							=     d_head
        a__:					List[str]      							=     d_inner
        a__:					List[Any]      							=     hidden_act
        a__:					str      							=     hidden_dropout
        a__:					Dict      							=     attention_dropout
        a__:					Optional[int]      							=     activation_dropout
        a__:					int      							=     max_position_embeddings
        a__:					str      							=     type_vocab_size
        a__:					List[str]      							=     2
        a__:					List[str]      							=     num_labels
        a__:					Tuple      							=     num_choices
        a__:					List[Any]      							=     scope
        a__:					Tuple      							=     initializer_std

        # Used in the tests to check the size of the first attention layer
        a__:					Union[str, Any]      							=     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__:					Union[str, 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__:					Any      							=     self.num_hidden_layers + 2



      def   lowerCamelCase_       (						self)     ->      List[Any]:





        '''simple docstring'''





        a__:					List[Any]      							=     ids_tensor([self.batch_size, self.seq_length]   ,      self.vocab_size)

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

        a__:					Optional[Any]      							=     None
        if self.use_token_type_ids:
          a__:					Optional[int]      							=     ids_tensor([self.batch_size, self.seq_length]   ,      self.type_vocab_size)

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

        a__:					Optional[Any]      							=     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   ,      lowercase   ,      lowercase   ,      lowercase   ,      lowercase   ,      lowercase   ,      lowercase   ,      lowercase   ,      )     ->      Tuple:





        '''simple docstring'''





        a__:					Tuple      							=     TFFunnelModel(config=lowercase)
        a__:					Dict      							=     {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
        a__:					Tuple      							=     model(lowercase)

        a__:					Dict      							=     [input_ids, input_mask]
        a__:					List[Any]      							=     model(lowercase)

        a__:					Optional[int]      							=     model(lowercase)
        self.parent.assertEqual(result.last_hidden_state.shape   ,      (self.batch_size, self.seq_length, self.d_model))

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

        a__:					Tuple      							=     False
        a__:					Any      							=     TFFunnelModel(config=lowercase)
        a__:					int      							=     model(lowercase)
        self.parent.assertEqual(result.last_hidden_state.shape   ,      (self.batch_size, self.seq_length, self.d_model))



      def   lowerCamelCase_       (						self   ,      lowercase   ,      lowercase   ,      lowercase   ,      lowercase   ,      lowercase   ,      lowercase   ,      lowercase   ,      )     ->      List[Any]:





        '''simple docstring'''





        a__:					List[Any]      							=     TFFunnelBaseModel(config=lowercase)
        a__:					List[str]      							=     {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
        a__:					Tuple      							=     model(lowercase)

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

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

        a__:					Dict      							=     False
        a__:					Any      							=     TFFunnelBaseModel(config=lowercase)
        a__:					str      							=     model(lowercase)
        self.parent.assertEqual(result.last_hidden_state.shape   ,      (self.batch_size, 3, self.d_model))

        a__:					Union[str, Any]      							=     False
        a__:					Tuple      							=     TFFunnelBaseModel(config=lowercase)
        a__:					int      							=     model(lowercase)
        self.parent.assertEqual(result.last_hidden_state.shape   ,      (self.batch_size, 2, self.d_model))



      def   lowerCamelCase_       (						self   ,      lowercase   ,      lowercase   ,      lowercase   ,      lowercase   ,      lowercase   ,      lowercase   ,      lowercase   ,      )     ->      List[Any]:





        '''simple docstring'''





        a__:					int      							=     TFFunnelForPreTraining(config=lowercase)
        a__:					List[str]      							=     {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
        a__:					str      							=     model(lowercase)
        self.parent.assertEqual(result.logits.shape   ,      (self.batch_size, self.seq_length))



      def   lowerCamelCase_       (						self   ,      lowercase   ,      lowercase   ,      lowercase   ,      lowercase   ,      lowercase   ,      lowercase   ,      lowercase   ,      )     ->      Union[str, Any]:





        '''simple docstring'''





        a__:					List[str]      							=     TFFunnelForMaskedLM(config=lowercase)
        a__:					int      							=     {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
        a__:					Optional[int]      							=     model(lowercase)
        self.parent.assertEqual(result.logits.shape   ,      (self.batch_size, self.seq_length, self.vocab_size))



      def   lowerCamelCase_       (						self   ,      lowercase   ,      lowercase   ,      lowercase   ,      lowercase   ,      lowercase   ,      lowercase   ,      lowercase   ,      )     ->      Optional[Any]:





        '''simple docstring'''





        a__:					List[Any]      							=     self.num_labels
        a__:					Dict      							=     TFFunnelForSequenceClassification(config=lowercase)
        a__:					Optional[int]      							=     {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
        a__:					Dict      							=     model(lowercase)
        self.parent.assertEqual(result.logits.shape   ,      (self.batch_size, self.num_labels))



      def   lowerCamelCase_       (						self   ,      lowercase   ,      lowercase   ,      lowercase   ,      lowercase   ,      lowercase   ,      lowercase   ,      lowercase   ,      )     ->      List[Any]:





        '''simple docstring'''





        a__:					Dict      							=     self.num_choices
        a__:					Union[str, Any]      							=     TFFunnelForMultipleChoice(config=lowercase)
        a__:					Union[str, Any]      							=     tf.tile(tf.expand_dims(lowercase   ,      1)   ,      (1, self.num_choices, 1))
        a__:					Dict      							=     tf.tile(tf.expand_dims(lowercase   ,      1)   ,      (1, self.num_choices, 1))
        a__:					List[Any]      							=     tf.tile(tf.expand_dims(lowercase   ,      1)   ,      (1, self.num_choices, 1))
        a__:					int      							=     {
            'input_ids': multiple_choice_inputs_ids,
            'attention_mask': multiple_choice_input_mask,
            'token_type_ids': multiple_choice_token_type_ids,
        }
        a__:					List[str]      							=     model(lowercase)
        self.parent.assertEqual(result.logits.shape   ,      (self.batch_size, self.num_choices))



      def   lowerCamelCase_       (						self   ,      lowercase   ,      lowercase   ,      lowercase   ,      lowercase   ,      lowercase   ,      lowercase   ,      lowercase   ,      )     ->      str:





        '''simple docstring'''





        a__:					Optional[Any]      							=     self.num_labels
        a__:					Union[str, Any]      							=     TFFunnelForTokenClassification(config=lowercase)
        a__:					List[str]      							=     {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
        a__:					str      							=     model(lowercase)
        self.parent.assertEqual(result.logits.shape   ,      (self.batch_size, self.seq_length, self.num_labels))



      def   lowerCamelCase_       (						self   ,      lowercase   ,      lowercase   ,      lowercase   ,      lowercase   ,      lowercase   ,      lowercase   ,      lowercase   ,      )     ->      Optional[int]:





        '''simple docstring'''





        a__:					str      							=     TFFunnelForQuestionAnswering(config=lowercase)
        a__:					List[str]      							=     {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
        a__:					Dict      							=     model(lowercase)
        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:





        '''simple docstring'''





        a__:					Union[str, Any]      							=     self.prepare_config_and_inputs()
        (
            a__   
        ):					Optional[int]      							=     config_and_inputs
        a__:					Optional[Any]      							=     {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
        return config, inputs_dict







@require_tf
class        __snake_case	(					__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)     ->      Dict:





        '''simple docstring'''





        a__:					Dict      							=     TFFunnelModelTester(self)
        a__:					int      							=     ConfigTester(self   ,      config_class=lowercase)



      def   lowerCamelCase_       (						self)     ->      int:





        '''simple docstring'''





        self.config_tester.run_common_tests()



      def   lowerCamelCase_       (						self)     ->      Union[str, Any]:





        '''simple docstring'''





        a__:					List[Any]      							=     self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*lowercase)



      def   lowerCamelCase_       (						self)     ->      List[str]:





        '''simple docstring'''





        a__:					int      							=     self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_pretraining(*lowercase)



      def   lowerCamelCase_       (						self)     ->      Tuple:





        '''simple docstring'''





        a__:					str      							=     self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_masked_lm(*lowercase)



      def   lowerCamelCase_       (						self)     ->      Optional[int]:





        '''simple docstring'''





        a__:					Dict      							=     self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_token_classification(*lowercase)



      def   lowerCamelCase_       (						self)     ->      str:





        '''simple docstring'''





        a__:					int      							=     self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_question_answering(*lowercase)







@require_tf
class        __snake_case	(					__lowerCAmelCase	,				unittest.TestCase    ):
      a__				=		(
          (TFFunnelBaseModel, TFFunnelForMultipleChoice, TFFunnelForSequenceClassification) if is_tf_available() else ()
      )
      a__				=		False
      a__				=		False



      def   lowerCamelCase_       (						self)     ->      Union[str, Any]:





        '''simple docstring'''





        a__:					List[str]      							=     TFFunnelModelTester(self   ,      base=lowercase)
        a__:					Any      							=     ConfigTester(self   ,      config_class=lowercase)



      def   lowerCamelCase_       (						self)     ->      Optional[int]:





        '''simple docstring'''





        self.config_tester.run_common_tests()



      def   lowerCamelCase_       (						self)     ->      Optional[Any]:





        '''simple docstring'''





        a__:					Optional[int]      							=     self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_base_model(*lowercase)



      def   lowerCamelCase_       (						self)     ->      Optional[Any]:





        '''simple docstring'''





        a__:					Optional[int]      							=     self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_sequence_classification(*lowercase)



      def   lowerCamelCase_       (						self)     ->      Optional[int]:





        '''simple docstring'''





        a__:					List[Any]      							=     self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_multiple_choice(*lowercase)



"""simple docstring"""
import unittest

from transformers import AutoConfig, AutoTokenizer, BertConfig, TensorType, is_flax_available
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, require_flax, slow


if is_flax_available():
 import jax

 from transformers.models.auto.modeling_flax_auto import FlaxAutoModel
 from transformers.models.bert.modeling_flax_bert import FlaxBertModel
 from transformers.models.roberta.modeling_flax_roberta import FlaxRobertaModel




@require_flax
class        __snake_case	(					unittest.TestCase    ):



      @slow
      def   lowerCamelCase_       (						self)     ->      Tuple:





        '''simple docstring'''





        for model_name in ["bert-base-cased", "bert-large-uncased"]:
          with self.subTest(lowercase):
            a__:					Optional[Any]      							=     AutoConfig.from_pretrained(lowercase)
            self.assertIsNotNone(lowercase)
            self.assertIsInstance(lowercase   ,      lowercase)

            a__:					Any      							=     FlaxAutoModel.from_pretrained(lowercase)
            self.assertIsNotNone(lowercase)
            self.assertIsInstance(lowercase   ,      lowercase)



      @slow
      def   lowerCamelCase_       (						self)     ->      Tuple:





        '''simple docstring'''





        for model_name in ["roberta-base", "roberta-large"]:
          with self.subTest(lowercase):
            a__:					str      							=     AutoConfig.from_pretrained(lowercase)
            self.assertIsNotNone(lowercase)
            self.assertIsInstance(lowercase   ,      lowercase)

            a__:					Any      							=     FlaxAutoModel.from_pretrained(lowercase)
            self.assertIsNotNone(lowercase)
            self.assertIsInstance(lowercase   ,      lowercase)



      @slow
      def   lowerCamelCase_       (						self)     ->      str:





        '''simple docstring'''





        for model_name in ["bert-base-cased", "bert-large-uncased"]:
          a__:					Dict      							=     AutoTokenizer.from_pretrained(lowercase)
          a__:					Union[str, Any]      							=     FlaxBertModel.from_pretrained(lowercase)
          a__:					List[str]      							=     tokenizer('Do you support jax jitted function?'   ,      return_tensors=TensorType.JAX)

          @jax.jit
          def eval(**lowercase):
            return model(**lowercase)

          eval(**lowercase).block_until_ready()



      @slow
      def   lowerCamelCase_       (						self)     ->      str:





        '''simple docstring'''





        for model_name in ["roberta-base", "roberta-large"]:
          a__:					Optional[Any]      							=     AutoTokenizer.from_pretrained(lowercase)
          a__:					Any      							=     FlaxRobertaModel.from_pretrained(lowercase)
          a__:					int      							=     tokenizer('Do you support jax jitted function?'   ,      return_tensors=TensorType.JAX)

          @jax.jit
          def eval(**lowercase):
            return model(**lowercase)

          eval(**lowercase).block_until_ready()



      def   lowerCamelCase_       (						self)     ->      Any:





        '''simple docstring'''





        with self.assertRaisesRegex(
            lowercase   ,      'bert-base is not a local folder and is not a valid model identifier'):
          a__:					str      							=     FlaxAutoModel.from_pretrained('bert-base')



      def   lowerCamelCase_       (						self)     ->      Optional[int]:





        '''simple docstring'''





        with self.assertRaisesRegex(
            lowercase   ,      r'aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)'):
          a__:					List[str]      							=     FlaxAutoModel.from_pretrained(lowercase   ,      revision='aaaaaa')



      def   lowerCamelCase_       (						self)     ->      Tuple:





        '''simple docstring'''





        with self.assertRaisesRegex(
            lowercase   ,      'hf-internal-testing/config-no-model does not appear to have a file named flax_model.msgpack'   ,      ):
          a__:					List[str]      							=     FlaxAutoModel.from_pretrained('hf-internal-testing/config-no-model')



      def   lowerCamelCase_       (						self)     ->      List[str]:





        '''simple docstring'''





        with self.assertRaisesRegex(lowercase   ,      'Use `from_pt=True` to load this model'):
          a__:					List[str]      							=     FlaxAutoModel.from_pretrained('hf-internal-testing/tiny-bert-pt-only')


