





"""simple docstring"""




from __future__ import annotations

import os
import tempfile
import unittest

from transformers import ConvBertConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

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 (
       TFConvBertForMaskedLM,
       TFConvBertForMultipleChoice,
       TFConvBertForQuestionAnswering,
       TFConvBertForSequenceClassification,
       TFConvBertForTokenClassification,
       TFConvBertModel,
   )

class  _UpperCAmelCase  :



      '''simple docstring'''

      def __init__(     self					,      snake_case_					,      snake_case_=1_3					,      snake_case_=7					,      snake_case_=True					,      snake_case_=True					,      snake_case_=True					,      snake_case_=True					,      snake_case_=9_9					,      snake_case_=3_2					,      snake_case_=2					,      snake_case_=4					,      snake_case_=3_7					,      snake_case_="gelu"					,      snake_case_=0.1					,      snake_case_=0.1					,      snake_case_=5_1_2					,      snake_case_=1_6					,      snake_case_=2					,      snake_case_=0.02					,      snake_case_=3					,      snake_case_=4					,      snake_case_=None					,      ):

        """simple docstring"""

        A_  :       List[str]										=  parent
        A_  :       Optional[int]										=  1_3
        A_  :       List[str]										=  7
        A_  :       List[Any]										=  True
        A_  :       Optional[int]										=  True
        A_  :       Dict										=  True
        A_  :       List[str]										=  True
        A_  :       Dict										=  9_9
        A_  :       Optional[Any]										=  3_8_4
        A_  :       str										=  2
        A_  :       Union[str, Any]										=  4
        A_  :       List[str]										=  3_7
        A_  :       str										=  'gelu'
        A_  :       Optional[int]										=  0.1
        A_  :       int										=  0.1
        A_  :       List[str]										=  5_1_2
        A_  :       Dict										=  1_6
        A_  :       Union[str, Any]										=  2
        A_  :       Tuple										=  0.02
        A_  :       Any										=  3
        A_  :       List[Any]										=  4
        A_  :       str										=  1_2_8
        A_  :       List[str]										=  2
        A_  :       List[str]										=  9
        A_  :       Union[str, Any]										=  1
        A_  :       Dict										=  None

      def 					lowerCamelCase_			(     self  ):

        """simple docstring"""

        A_  :       int										=  ids_tensor([self.batch_size, self.seq_length]					,      self.vocab_size  )

        A_  :       List[str]										=  None
        if self.use_input_mask:
          A_  :       Dict										=  random_attention_mask([self.batch_size, self.seq_length]  )

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

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

        A_  :       Any										=  ConvBertConfig(
            vocab_size=self.vocab_size					,      hidden_size=self.hidden_size					,      num_hidden_layers=self.num_hidden_layers					,      num_attention_heads=self.num_attention_heads					,      intermediate_size=self.intermediate_size					,      hidden_act=self.hidden_act					,      hidden_dropout_prob=self.hidden_dropout_prob					,      attention_probs_dropout_prob=self.attention_probs_dropout_prob					,      max_position_embeddings=self.max_position_embeddings					,      type_vocab_size=self.type_vocab_size					,      initializer_range=self.initializer_range					,      return_dict=snake_case_					,      )

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

      def 					lowerCamelCase_			(     self					,      snake_case_					,      snake_case_					,      snake_case_					,      snake_case_					,      snake_case_					,      snake_case_					,      snake_case_  ):

        """simple docstring"""

        A_  :       Tuple										=  TFConvBertModel(config=snake_case_  )
        A_  :       str										=  {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}

        A_  :       int										=  [input_ids, input_mask]
        A_  :       str										=  model(snake_case_  )

        A_  :       Any										=  model(snake_case_  )

        self.parent.assertEqual(result.last_hidden_state.shape					,      (self.batch_size, self.seq_length, self.hidden_size)  )

      def 					lowerCamelCase_			(     self					,      snake_case_					,      snake_case_					,      snake_case_					,      snake_case_					,      snake_case_					,      snake_case_					,      snake_case_  ):

        """simple docstring"""

        A_  :       Tuple										=  TFConvBertForMaskedLM(config=snake_case_  )
        A_  :       Tuple										=  {
            'input_ids': input_ids,
            'attention_mask': input_mask,
            'token_type_ids': token_type_ids,
        }
        A_  :       str										=  model(snake_case_  )
        self.parent.assertEqual(result.logits.shape					,      (self.batch_size, self.seq_length, self.vocab_size)  )

      def 					lowerCamelCase_			(     self					,      snake_case_					,      snake_case_					,      snake_case_					,      snake_case_					,      snake_case_					,      snake_case_					,      snake_case_  ):

        """simple docstring"""

        A_  :       Dict										=  self.num_labels
        A_  :       int										=  TFConvBertForSequenceClassification(config=snake_case_  )
        A_  :       List[str]										=  {
            'input_ids': input_ids,
            'attention_mask': input_mask,
            'token_type_ids': token_type_ids,
        }

        A_  :       Any										=  model(snake_case_  )
        self.parent.assertEqual(result.logits.shape					,      (self.batch_size, self.num_labels)  )

      def 					lowerCamelCase_			(     self					,      snake_case_					,      snake_case_					,      snake_case_					,      snake_case_					,      snake_case_					,      snake_case_					,      snake_case_  ):

        """simple docstring"""

        A_  :       int										=  self.num_choices
        A_  :       Union[str, Any]										=  TFConvBertForMultipleChoice(config=snake_case_  )
        A_  :       str										=  tf.tile(tf.expand_dims(snake_case_					,      1  )					,      (1, self.num_choices, 1)  )
        A_  :       str										=  tf.tile(tf.expand_dims(snake_case_					,      1  )					,      (1, self.num_choices, 1)  )
        A_  :       Optional[int]										=  tf.tile(tf.expand_dims(snake_case_					,      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_  :       Any										=  model(snake_case_  )
        self.parent.assertEqual(result.logits.shape					,      (self.batch_size, self.num_choices)  )

      def 					lowerCamelCase_			(     self					,      snake_case_					,      snake_case_					,      snake_case_					,      snake_case_					,      snake_case_					,      snake_case_					,      snake_case_  ):

        """simple docstring"""

        A_  :       str										=  self.num_labels
        A_  :       Union[str, Any]										=  TFConvBertForTokenClassification(config=snake_case_  )
        A_  :       str										=  {
            'input_ids': input_ids,
            'attention_mask': input_mask,
            'token_type_ids': token_type_ids,
        }
        A_  :       Tuple										=  model(snake_case_  )
        self.parent.assertEqual(result.logits.shape					,      (self.batch_size, self.seq_length, self.num_labels)  )

      def 					lowerCamelCase_			(     self					,      snake_case_					,      snake_case_					,      snake_case_					,      snake_case_					,      snake_case_					,      snake_case_					,      snake_case_  ):

        """simple docstring"""

        A_  :       List[str]										=  TFConvBertForQuestionAnswering(config=snake_case_  )
        A_  :       Any										=  {
            'input_ids': input_ids,
            'attention_mask': input_mask,
            'token_type_ids': token_type_ids,
        }

        A_  :       List[str]										=  model(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  ):

        """simple docstring"""

        A_  :       Tuple										=  self.prepare_config_and_inputs()
        (
            (
            A_
        )						,       (
            A_
        )						,       (
            A_
        )						,       (
            A_
        )						,       (
            A_
        )						,       (
            A_
        )						,       (
            A_
        )						,       
        )  :       List[Any]										=  config_and_inputs
        A_  :       List[Any]										=  {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
        return config, inputs_dict


@require_tf
class  _UpperCAmelCase  (   UpperCAmelCase__			,   UpperCAmelCase__			,   unittest.TestCase		):



      '''simple docstring'''
      lowercase_      : List[Any]	    =   (
          (
              TFConvBertModel,
              TFConvBertForMaskedLM,
              TFConvBertForQuestionAnswering,
              TFConvBertForSequenceClassification,
              TFConvBertForTokenClassification,
              TFConvBertForMultipleChoice,
          )
          if is_tf_available()
          else ()
      )
      lowercase_      : Optional[Any]	    =   (
          {
              """feature-extraction""": TFConvBertModel,
              """fill-mask""": TFConvBertForMaskedLM,
              """question-answering""": TFConvBertForQuestionAnswering,
              """text-classification""": TFConvBertForSequenceClassification,
              """token-classification""": TFConvBertForTokenClassification,
              """zero-shot""": TFConvBertForSequenceClassification,
          }
          if is_tf_available()
          else {}
      )
      lowercase_      : List[Any]	    =   False
      lowercase_      : Optional[int]	    =   False
      lowercase_      : Optional[int]	    =   False

      def 					lowerCamelCase_			(     self  ):

        """simple docstring"""

        A_  :       int										=  TFConvBertModelTester(self  )
        A_  :       Optional[int]										=  ConfigTester(self					,      config_class=snake_case_					,      hidden_size=3_7  )

      def 					lowerCamelCase_			(     self  ):

        """simple docstring"""

        self.config_tester.run_common_tests()

      def 					lowerCamelCase_			(     self  ):

        """simple docstring"""

        A_  :       Optional[int]										=  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*snake_case_  )

      def 					lowerCamelCase_			(     self  ):

        """simple docstring"""

        A_  :       List[str]										=  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_masked_lm(*snake_case_  )

      def 					lowerCamelCase_			(     self  ):

        """simple docstring"""

        A_  :       List[Any]										=  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_multiple_choice(*snake_case_  )

      def 					lowerCamelCase_			(     self  ):

        """simple docstring"""

        A_  :       int										=  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_question_answering(*snake_case_  )

      def 					lowerCamelCase_			(     self  ):

        """simple docstring"""

        A_  :       List[str]										=  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_sequence_classification(*snake_case_  )

      def 					lowerCamelCase_			(     self  ):

        """simple docstring"""

        A_  :       Any										=  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_token_classification(*snake_case_  )

      @slow
      def 					lowerCamelCase_			(     self  ):

        """simple docstring"""

        A_						,       A_  :       int										=  self.model_tester.prepare_config_and_inputs_for_common()
        A_  :       Union[str, Any]										=  True
        A_  :       str										=  True

        if hasattr(snake_case_					,      'use_cache'  ):
          A_  :       Optional[int]										=  True

        A_  :       Optional[Any]										=  getattr(self.model_tester					,      'encoder_seq_length'					,      self.model_tester.seq_length  )
        A_  :       Tuple										=  getattr(self.model_tester					,      'key_length'					,      snake_case_  )

        for model_class in self.all_model_classes:
          A_  :       Union[str, Any]										=  self._prepare_for_class(snake_case_					,      snake_case_  )
          A_  :       Tuple										=  model_class(snake_case_  )
          A_  :       Union[str, Any]										=  len(model(snake_case_  )  )

          with tempfile.TemporaryDirectory() as tmpdirname:
            model.save_pretrained(snake_case_					,      saved_model=snake_case_  )
            A_  :       Any										=  os.path.join(snake_case_					,      'saved_model'					,      '1'  )
            A_  :       Tuple										=  tf.keras.models.load_model(snake_case_  )
            A_  :       Optional[int]										=  model(snake_case_  )

            if self.is_encoder_decoder:
              A_  :       str										=  outputs['encoder_hidden_states']
              A_  :       Tuple										=  outputs['encoder_attentions']
            else:
              A_  :       int										=  outputs['hidden_states']
              A_  :       Any										=  outputs['attentions']

            self.assertEqual(len(snake_case_  )					,      snake_case_  )

            A_  :       List[Any]										=  getattr(
                self.model_tester					,      'expected_num_hidden_layers'					,      self.model_tester.num_hidden_layers + 1  )

            self.assertEqual(len(snake_case_  )					,      snake_case_  )
            self.assertListEqual(
                list(output_hidden_states[0].shape[-2:]  )					,      [self.model_tester.seq_length, self.model_tester.hidden_size]					,      )

            self.assertEqual(len(snake_case_  )					,      self.model_tester.num_hidden_layers  )
            self.assertListEqual(
                list(output_attentions[0].shape[-3:]  )					,      [self.model_tester.num_attention_heads / 2, encoder_seq_length, encoder_key_length]					,      )

      @slow
      def 					lowerCamelCase_			(     self  ):

        """simple docstring"""

        A_  :       Tuple										=  TFConvBertModel.from_pretrained('YituTech/conv-bert-base'  )
        self.assertIsNotNone(snake_case_  )

      def 					lowerCamelCase_			(     self  ):

        """simple docstring"""

        A_						,       A_  :       List[str]										=  self.model_tester.prepare_config_and_inputs_for_common()
        A_  :       str										=  True
        A_  :       Optional[int]										=  getattr(self.model_tester					,      'decoder_seq_length'					,      self.model_tester.seq_length  )
        A_  :       Union[str, Any]										=  getattr(self.model_tester					,      'encoder_seq_length'					,      self.model_tester.seq_length  )
        A_  :       Union[str, Any]										=  getattr(self.model_tester					,      'key_length'					,      snake_case_  )
        A_  :       str										=  getattr(self.model_tester					,      'key_length'					,      snake_case_  )

        def check_decoder_attentions_output(snake_case_  ):
          A_  :       str										=  len(snake_case_  )
          self.assertEqual(out_len % 2					,      0  )
          A_  :       Optional[Any]										=  outputs.decoder_attentions
          self.assertEqual(len(snake_case_  )					,      self.model_tester.num_hidden_layers  )
          self.assertListEqual(
              list(decoder_attentions[0].shape[-3:]  )					,      [self.model_tester.num_attention_heads / 2, decoder_seq_length, decoder_key_length]					,      )

        def check_encoder_attentions_output(snake_case_  ):
          A_  :       Tuple										=  [
              t.numpy() for t in (outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions)
          ]
          self.assertEqual(len(snake_case_  )					,      self.model_tester.num_hidden_layers  )
          self.assertListEqual(
              list(attentions[0].shape[-3:]  )					,      [self.model_tester.num_attention_heads / 2, encoder_seq_length, encoder_key_length]					,      )

        for model_class in self.all_model_classes:
          A_  :       List[str]										=  True
          A_  :       Any										=  False
          A_  :       Dict										=  model_class(snake_case_  )
          A_  :       int										=  model(self._prepare_for_class(snake_case_					,      snake_case_  )  )
          A_  :       List[Any]										=  len(snake_case_  )
          self.assertEqual(config.output_hidden_states					,      snake_case_  )
          check_encoder_attentions_output(snake_case_  )

          if self.is_encoder_decoder:
            A_  :       Any										=  model_class(snake_case_  )
            A_  :       Optional[Any]										=  model(self._prepare_for_class(snake_case_					,      snake_case_  )  )
            self.assertEqual(config.output_hidden_states					,      snake_case_  )
            check_decoder_attentions_output(snake_case_  )

          # Check that output attentions can also be changed via the config
          del inputs_dict["output_attentions"]
          A_  :       List[Any]										=  True
          A_  :       Optional[Any]										=  model_class(snake_case_  )
          A_  :       Optional[int]										=  model(self._prepare_for_class(snake_case_					,      snake_case_  )  )
          self.assertEqual(config.output_hidden_states					,      snake_case_  )
          check_encoder_attentions_output(snake_case_  )

          # Check attention is always last and order is fine
          A_  :       Union[str, Any]										=  True
          A_  :       Dict										=  True
          A_  :       List[str]										=  model_class(snake_case_  )
          A_  :       Any										=  model(self._prepare_for_class(snake_case_					,      snake_case_  )  )

          self.assertEqual(out_len + (2 if self.is_encoder_decoder else 1)					,      len(snake_case_  )  )
          self.assertEqual(model.config.output_hidden_states					,      snake_case_  )
          check_encoder_attentions_output(snake_case_  )


@require_tf
class  _UpperCAmelCase  (   unittest.TestCase		):



      '''simple docstring'''

      @slow
      def 					lowerCamelCase_			(     self  ):

        """simple docstring"""

        A_  :       Dict										=  TFConvBertModel.from_pretrained('YituTech/conv-bert-base'  )
        A_  :       List[str]										=  tf.constant([[0, 1, 2, 3, 4, 5]]  )
        A_  :       List[Any]										=  model(snake_case_  )[0]

        A_  :       int										=  [1, 6, 7_6_8]
        self.assertEqual(output.shape					,      snake_case_  )

        A_  :       List[Any]										=  tf.constant(
            [
                [
                    [-0.03_47_54_93, -0.4_68_60_34, -0.30_63_88_32],
                    [0.22_63_72_48, -0.26_98_86_46, -0.7_42_34_24],
                    [0.10_32_48_68, -0.45_01_35_08, -0.58_28_07_84],
                ]
            ]  )
        tf.debugging.assert_near(output[:, :3, :3]					,      snake_case_					,      atol=1E-4  )






"""simple docstring"""




from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


lowerCamelCase_     :						str						=					logging.get_logger(__name__)

lowerCamelCase_     :						Any						=					{
    'facebook/xmod-base': 'https://huggingface.co/facebook/xmod-base/resolve/main/config.json',
    'facebook/xmod-large-prenorm': 'https://huggingface.co/facebook/xmod-large-prenorm/resolve/main/config.json',
    'facebook/xmod-base-13-125k': 'https://huggingface.co/facebook/xmod-base-13-125k/resolve/main/config.json',
    'facebook/xmod-base-30-125k': 'https://huggingface.co/facebook/xmod-base-30-125k/resolve/main/config.json',
    'facebook/xmod-base-30-195k': 'https://huggingface.co/facebook/xmod-base-30-195k/resolve/main/config.json',
    'facebook/xmod-base-60-125k': 'https://huggingface.co/facebook/xmod-base-60-125k/resolve/main/config.json',
    'facebook/xmod-base-60-265k': 'https://huggingface.co/facebook/xmod-base-60-265k/resolve/main/config.json',
    'facebook/xmod-base-75-125k': 'https://huggingface.co/facebook/xmod-base-75-125k/resolve/main/config.json',
    'facebook/xmod-base-75-269k': 'https://huggingface.co/facebook/xmod-base-75-269k/resolve/main/config.json',
}

class  _UpperCAmelCase  (   UpperCAmelCase__		):



      '''simple docstring'''
      lowercase_      : Tuple	    =   """xmod"""

      def __init__(     self					,      snake_case_=3_0_5_2_2					,      snake_case_=7_6_8					,      snake_case_=1_2					,      snake_case_=1_2					,      snake_case_=3_0_7_2					,      snake_case_="gelu"					,      snake_case_=0.1					,      snake_case_=0.1					,      snake_case_=5_1_2					,      snake_case_=2					,      snake_case_=0.02					,      snake_case_=1E-12					,      snake_case_=1					,      snake_case_=0					,      snake_case_=2					,      snake_case_="absolute"					,      snake_case_=True					,      snake_case_=None					,      snake_case_=False					,      snake_case_=2					,      snake_case_=False					,      snake_case_=True					,      snake_case_=True					,      snake_case_=("en_XX",)					,      snake_case_=None					,      **snake_case_					,      ):

        """simple docstring"""

        super().__init__(pad_token_id=snake_case_					,      bos_token_id=snake_case_					,      eos_token_id=snake_case_					,      **snake_case_  )

        A_  :       Union[str, Any]										=  vocab_size
        A_  :       Any										=  hidden_size
        A_  :       List[str]										=  num_hidden_layers
        A_  :       Tuple										=  num_attention_heads
        A_  :       int										=  hidden_act
        A_  :       Any										=  intermediate_size
        A_  :       Any										=  hidden_dropout_prob
        A_  :       Dict										=  attention_probs_dropout_prob
        A_  :       Union[str, Any]										=  max_position_embeddings
        A_  :       List[Any]										=  type_vocab_size
        A_  :       List[str]										=  initializer_range
        A_  :       Any										=  layer_norm_eps
        A_  :       Optional[Any]										=  position_embedding_type
        A_  :       int										=  use_cache
        A_  :       Dict										=  classifier_dropout
        A_  :       int										=  pre_norm
        A_  :       Optional[Any]										=  adapter_reduction_factor
        A_  :       List[Any]										=  adapter_layer_norm
        A_  :       int										=  adapter_reuse_layer_norm
        A_  :       Dict										=  ln_before_adapter
        A_  :       List[str]										=  list(snake_case_  )
        A_  :       Union[str, Any]										=  default_language


class  _UpperCAmelCase  (   UpperCAmelCase__		):



      '''simple docstring'''

      @property
      def 					lowerCamelCase_			(     self  ):

        """simple docstring"""

        if self.task == "multiple-choice":
          A_  :       Dict										=  {0: 'batch', 1: 'choice', 2: 'sequence'}
        else:
          A_  :       int										=  {0: 'batch', 1: 'sequence'}
        return OrderedDict(
            [
                ('input_ids', dynamic_axis),
                ('attention_mask', dynamic_axis),
            ]  )