






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        a       :



      """simple docstring"""


      def __init__( self   :     Any						, __lowercase   :     Dict						, __lowercase   :     Optional[int]=13						, __lowercase   :     Optional[int]=7						, __lowercase   :     str=True						, __lowercase   :     int=True						, __lowercase   :     Any=True						, __lowercase   :     int=True						, __lowercase   :     Tuple=99						, __lowercase   :     int=32						, __lowercase   :     Any=2						, __lowercase   :     Optional[int]=4						, __lowercase   :     Union[str, Any]=37						, __lowercase   :     Optional[int]="gelu"						, __lowercase   :     Optional[int]=0.1						, __lowercase   :     Optional[Any]=0.1						, __lowercase   :     str=512						, __lowercase   :     Tuple=16						, __lowercase   :     Optional[int]=2						, __lowercase   :     Optional[int]=0.02						, __lowercase   :     str=3						, __lowercase   :     int=4						, __lowercase   :     Union[str, Any]=None						, )				-> List[Any]:
        __UpperCAmelCase				:      List[str]	 =    parent
        __UpperCAmelCase				:      int	 =    13
        __UpperCAmelCase				:      List[str]	 =    7
        __UpperCAmelCase				:      Dict	 =    True
        __UpperCAmelCase				:      List[Any]	 =    True
        __UpperCAmelCase				:      List[str]	 =    True
        __UpperCAmelCase				:      List[str]	 =    True
        __UpperCAmelCase				:      str	 =    99
        __UpperCAmelCase				:      Optional[int]	 =    384
        __UpperCAmelCase				:      List[str]	 =    2
        __UpperCAmelCase				:      int	 =    4
        __UpperCAmelCase				:      List[Any]	 =    37
        __UpperCAmelCase				:      Any	 =    """gelu"""
        __UpperCAmelCase				:      List[Any]	 =    0.1
        __UpperCAmelCase				:      Dict	 =    0.1
        __UpperCAmelCase				:      Optional[int]	 =    512
        __UpperCAmelCase				:      List[str]	 =    16
        __UpperCAmelCase				:      Optional[int]	 =    2
        __UpperCAmelCase				:      Optional[int]	 =    0.02
        __UpperCAmelCase				:      Union[str, Any]	 =    3
        __UpperCAmelCase				:      List[str]	 =    4
        __UpperCAmelCase				:      Any	 =    128
        __UpperCAmelCase				:      List[str]	 =    2
        __UpperCAmelCase				:      Tuple	 =    9
        __UpperCAmelCase				:      List[str]	 =    1
        __UpperCAmelCase				:      List[Any]	 =    None
      def       UpperCAmelCase ( self   :     Union[str, Any]   )				-> Tuple:
        __UpperCAmelCase				:      List[Any]	 =    ids_tensor([self.batch_size, self.seq_length]						, self.vocab_size   )

        __UpperCAmelCase				:      List[Any]	 =    None
        if self.use_input_mask:
          __UpperCAmelCase				:      Union[str, Any]	 =    random_attention_mask([self.batch_size, self.seq_length]   )

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

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

        __UpperCAmelCase				:      str	 =    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=a__						, )

        return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
      def       UpperCAmelCase ( self   :     Dict						, __lowercase   :     Union[str, Any]						, __lowercase   :     int						, __lowercase   :     Tuple						, __lowercase   :     str						, __lowercase   :     Optional[int]						, __lowercase   :     Union[str, Any]						, __lowercase   :     Dict   )				-> Optional[int]:
        __UpperCAmelCase				:      Optional[int]	 =    TFConvBertModel(config=a__   )
        __UpperCAmelCase				:      List[str]	 =    {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}

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

        __UpperCAmelCase				:      List[str]	 =    model(a__   )

        self.parent.assertEqual(result.last_hidden_state.shape						, (self.batch_size, self.seq_length, self.hidden_size)   )
      def       UpperCAmelCase ( self   :     str						, __lowercase   :     Any						, __lowercase   :     Optional[int]						, __lowercase   :     Dict						, __lowercase   :     List[Any]						, __lowercase   :     str						, __lowercase   :     Union[str, Any]						, __lowercase   :     List[Any]   )				-> Optional[Any]:
        __UpperCAmelCase				:      List[Any]	 =    TFConvBertForMaskedLM(config=a__   )
        __UpperCAmelCase				:      str	 =    {
            """input_ids""": input_ids,
            """attention_mask""": input_mask,
            """token_type_ids""": token_type_ids,
        }
        __UpperCAmelCase				:      Any	 =    model(a__   )
        self.parent.assertEqual(result.logits.shape						, (self.batch_size, self.seq_length, self.vocab_size)   )
      def       UpperCAmelCase ( self   :     Tuple						, __lowercase   :     int						, __lowercase   :     int						, __lowercase   :     Dict						, __lowercase   :     List[Any]						, __lowercase   :     Optional[Any]						, __lowercase   :     List[Any]						, __lowercase   :     List[Any]   )				-> List[str]:
        __UpperCAmelCase				:      Tuple	 =    self.num_labels
        __UpperCAmelCase				:      Optional[int]	 =    TFConvBertForSequenceClassification(config=a__   )
        __UpperCAmelCase				:      Optional[Any]	 =    {
            """input_ids""": input_ids,
            """attention_mask""": input_mask,
            """token_type_ids""": token_type_ids,
        }

        __UpperCAmelCase				:      Optional[Any]	 =    model(a__   )
        self.parent.assertEqual(result.logits.shape						, (self.batch_size, self.num_labels)   )
      def       UpperCAmelCase ( self   :     str						, __lowercase   :     Any						, __lowercase   :     Optional[int]						, __lowercase   :     str						, __lowercase   :     List[Any]						, __lowercase   :     Optional[Any]						, __lowercase   :     Tuple						, __lowercase   :     Tuple   )				-> List[str]:
        __UpperCAmelCase				:      Optional[int]	 =    self.num_choices
        __UpperCAmelCase				:      Tuple	 =    TFConvBertForMultipleChoice(config=a__   )
        __UpperCAmelCase				:      int	 =    tf.tile(tf.expand_dims(a__						, 1   )						, (1, self.num_choices, 1)   )
        __UpperCAmelCase				:      Any	 =    tf.tile(tf.expand_dims(a__						, 1   )						, (1, self.num_choices, 1)   )
        __UpperCAmelCase				:      Dict	 =    tf.tile(tf.expand_dims(a__						, 1   )						, (1, self.num_choices, 1)   )
        __UpperCAmelCase				:      Optional[int]	 =    {
            """input_ids""": multiple_choice_inputs_ids,
            """attention_mask""": multiple_choice_input_mask,
            """token_type_ids""": multiple_choice_token_type_ids,
        }
        __UpperCAmelCase				:      List[str]	 =    model(a__   )
        self.parent.assertEqual(result.logits.shape						, (self.batch_size, self.num_choices)   )
      def       UpperCAmelCase ( self   :     Optional[int]						, __lowercase   :     List[str]						, __lowercase   :     str						, __lowercase   :     int						, __lowercase   :     Optional[Any]						, __lowercase   :     Optional[int]						, __lowercase   :     Dict						, __lowercase   :     Any   )				-> Tuple:
        __UpperCAmelCase				:      Optional[int]	 =    self.num_labels
        __UpperCAmelCase				:      Dict	 =    TFConvBertForTokenClassification(config=a__   )
        __UpperCAmelCase				:      Any	 =    {
            """input_ids""": input_ids,
            """attention_mask""": input_mask,
            """token_type_ids""": token_type_ids,
        }
        __UpperCAmelCase				:      Union[str, Any]	 =    model(a__   )
        self.parent.assertEqual(result.logits.shape						, (self.batch_size, self.seq_length, self.num_labels)   )
      def       UpperCAmelCase ( self   :     List[str]						, __lowercase   :     str						, __lowercase   :     int						, __lowercase   :     int						, __lowercase   :     Union[str, Any]						, __lowercase   :     List[Any]						, __lowercase   :     str						, __lowercase   :     Dict   )				-> Dict:
        __UpperCAmelCase				:      Dict	 =    TFConvBertForQuestionAnswering(config=a__   )
        __UpperCAmelCase				:      List[str]	 =    {
            """input_ids""": input_ids,
            """attention_mask""": input_mask,
            """token_type_ids""": token_type_ids,
        }

        __UpperCAmelCase				:      Tuple	 =    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       UpperCAmelCase ( self   :     List[str]   )				-> List[Any]:
        __UpperCAmelCase				:      int	 =    self.prepare_config_and_inputs()
        (
            (
            __UpperCAmelCase      
        ) ,					(
            __UpperCAmelCase      
        ) ,					(
            __UpperCAmelCase      
        ) ,					(
            __UpperCAmelCase      
        ) ,					(
            __UpperCAmelCase      
        ) ,					(
            __UpperCAmelCase      
        ) ,					(
            __UpperCAmelCase      
        ) ,					      
        )				:      int	 =    config_and_inputs
        __UpperCAmelCase				:      Union[str, Any]	 =    {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
        return config, inputs_dict


@require_tf
class        a       (					__snake_case				,			__snake_case				,			unittest.TestCase  ):



      """simple docstring"""


      a    :					Optional[Any]							=      (
          (
              TFConvBertModel,
              TFConvBertForMaskedLM,
              TFConvBertForQuestionAnswering,
              TFConvBertForSequenceClassification,
              TFConvBertForTokenClassification,
              TFConvBertForMultipleChoice,
          )
          if is_tf_available()
          else ()
      )
      a    :					Optional[int]							=      (
          {
              'feature-extraction': TFConvBertModel,
              'fill-mask': TFConvBertForMaskedLM,
              'question-answering': TFConvBertForQuestionAnswering,
              'text-classification': TFConvBertForSequenceClassification,
              'token-classification': TFConvBertForTokenClassification,
              'zero-shot': TFConvBertForSequenceClassification,
          }
          if is_tf_available()
          else {}
      )
      a    :					List[Any]							=      False
      a    :					str							=      False
      a    :					Union[str, Any]							=      False
      def       UpperCAmelCase ( self   :     Optional[Any]   )				-> Optional[Any]:
        __UpperCAmelCase				:      Optional[int]	 =    TFConvBertModelTester(self   )
        __UpperCAmelCase				:      Optional[int]	 =    ConfigTester(self						, config_class=a__						, hidden_size=37   )
      def       UpperCAmelCase ( self   :     Any   )				-> List[str]:
        self.config_tester.run_common_tests()
      def       UpperCAmelCase ( self   :     Any   )				-> str:
        __UpperCAmelCase				:      List[Any]	 =    self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*a__   )
      def       UpperCAmelCase ( self   :     Any   )				-> List[str]:
        __UpperCAmelCase				:      List[str]	 =    self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_masked_lm(*a__   )
      def       UpperCAmelCase ( self   :     List[Any]   )				-> Union[str, Any]:
        __UpperCAmelCase				:      List[str]	 =    self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_multiple_choice(*a__   )
      def       UpperCAmelCase ( self   :     Union[str, Any]   )				-> Optional[Any]:
        __UpperCAmelCase				:      Optional[int]	 =    self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_question_answering(*a__   )
      def       UpperCAmelCase ( self   :     Dict   )				-> Tuple:
        __UpperCAmelCase				:      Tuple	 =    self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_sequence_classification(*a__   )
      def       UpperCAmelCase ( self   :     Union[str, Any]   )				-> Optional[Any]:
        __UpperCAmelCase				:      str	 =    self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_token_classification(*a__   )
      @slow
      def       UpperCAmelCase ( self   :     Union[str, Any]   )				-> Dict:
        __UpperCAmelCase ,					__UpperCAmelCase				:      Any	 =    self.model_tester.prepare_config_and_inputs_for_common()
        __UpperCAmelCase				:      Any	 =    True
        __UpperCAmelCase				:      List[str]	 =    True

        if hasattr(a__						, """use_cache"""   ):
          __UpperCAmelCase				:      Any	 =    True

        __UpperCAmelCase				:      Optional[int]	 =    getattr(self.model_tester						, """encoder_seq_length"""						, self.model_tester.seq_length   )
        __UpperCAmelCase				:      Tuple	 =    getattr(self.model_tester						, """key_length"""						, a__   )

        for model_class in self.all_model_classes:
          __UpperCAmelCase				:      Optional[Any]	 =    self._prepare_for_class(a__						, a__   )
          __UpperCAmelCase				:      Union[str, Any]	 =    model_class(a__   )
          __UpperCAmelCase				:      str	 =    len(model(a__   )   )

          with tempfile.TemporaryDirectory() as tmpdirname:
            model.save_pretrained(a__						, saved_model=a__   )
            __UpperCAmelCase				:      Optional[int]	 =    os.path.join(a__						, """saved_model"""						, """1"""   )
            __UpperCAmelCase				:      List[Any]	 =    tf.keras.models.load_model(a__   )
            __UpperCAmelCase				:      Dict	 =    model(a__   )

            if self.is_encoder_decoder:
              __UpperCAmelCase				:      Optional[Any]	 =    outputs["""encoder_hidden_states"""]
              __UpperCAmelCase				:      Optional[Any]	 =    outputs["""encoder_attentions"""]
            else:
              __UpperCAmelCase				:      Dict	 =    outputs["""hidden_states"""]
              __UpperCAmelCase				:      Union[str, Any]	 =    outputs["""attentions"""]

            self.assertEqual(len(a__   )						, a__   )

            __UpperCAmelCase				:      Dict	 =    getattr(
                self.model_tester						, """expected_num_hidden_layers"""						, self.model_tester.num_hidden_layers + 1   )

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

            self.assertEqual(len(a__   )						, 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       UpperCAmelCase ( self   :     Optional[Any]   )				-> List[Any]:
        __UpperCAmelCase				:      List[Any]	 =    TFConvBertModel.from_pretrained("""YituTech/conv-bert-base"""   )
        self.assertIsNotNone(a__   )




      def       UpperCAmelCase ( self   :     int   )				-> List[Any]:
        __UpperCAmelCase ,					__UpperCAmelCase				:      Optional[int]	 =    self.model_tester.prepare_config_and_inputs_for_common()
        __UpperCAmelCase				:      List[Any]	 =    True
        __UpperCAmelCase				:      int	 =    getattr(self.model_tester						, """decoder_seq_length"""						, self.model_tester.seq_length   )
        __UpperCAmelCase				:      Dict	 =    getattr(self.model_tester						, """encoder_seq_length"""						, self.model_tester.seq_length   )
        __UpperCAmelCase				:      int	 =    getattr(self.model_tester						, """key_length"""						, a__   )
        __UpperCAmelCase				:      str	 =    getattr(self.model_tester						, """key_length"""						, a__   )

        def check_decoder_attentions_output(__lowercase   :     Any   ):
          __UpperCAmelCase				:      List[Any]	 =    len(a__   )
          self.assertEqual(out_len % 2						, 0   )
          __UpperCAmelCase				:      Union[str, Any]	 =    outputs.decoder_attentions
          self.assertEqual(len(a__   )						, 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(__lowercase   :     int   ):
          __UpperCAmelCase				:      int	 =    [
              t.numpy() for t in (outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions)
          ]
          self.assertEqual(len(a__   )						, 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:
          __UpperCAmelCase				:      Any	 =    True
          __UpperCAmelCase				:      Tuple	 =    False
          __UpperCAmelCase				:      Union[str, Any]	 =    model_class(a__   )
          __UpperCAmelCase				:      List[str]	 =    model(self._prepare_for_class(a__						, a__   )   )
          __UpperCAmelCase				:      Tuple	 =    len(a__   )
          self.assertEqual(config.output_hidden_states						, a__   )
          check_encoder_attentions_output(a__   )

          if self.is_encoder_decoder:
            __UpperCAmelCase				:      Optional[int]	 =    model_class(a__   )
            __UpperCAmelCase				:      int	 =    model(self._prepare_for_class(a__						, a__   )   )
            self.assertEqual(config.output_hidden_states						, a__   )
            check_decoder_attentions_output(a__   )

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

          # Check attention is always last and order is fine
          __UpperCAmelCase				:      str	 =    True
          __UpperCAmelCase				:      List[Any]	 =    True
          __UpperCAmelCase				:      Tuple	 =    model_class(a__   )
          __UpperCAmelCase				:      List[str]	 =    model(self._prepare_for_class(a__						, a__   )   )

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


@require_tf
class        a       (					unittest.TestCase  ):



      """simple docstring"""


      @slow
      def       UpperCAmelCase ( self   :     Tuple   )				-> Any:
        __UpperCAmelCase				:      Dict	 =    TFConvBertModel.from_pretrained("""YituTech/conv-bert-base"""   )
        __UpperCAmelCase				:      Tuple	 =    tf.constant([[0, 1, 2, 3, 4, 5]]   )
        __UpperCAmelCase				:      Union[str, Any]	 =    model(a__   )[0]

        __UpperCAmelCase				:      Optional[Any]	 =    [1, 6, 768]
        self.assertEqual(output.shape						, a__   )

        __UpperCAmelCase				:      str	 =    tf.constant(
            [
                [
                    [-0.03_475_493, -0.4_686_034, -0.30_638_832],
                    [0.22_637_248, -0.26_988_646, -0.7_423_424],
                    [0.10_324_868, -0.45_013_508, -0.58_280_784],
                ]
            ]   )
        tf.debugging.assert_near(output[:, :3, :3]						, a__						, atol=1e-4   )






_lowercase							:   Dict						=						"0.21.0"

from .accelerator import Accelerator
from .big_modeling import (
    cpu_offload,
    cpu_offload_with_hook,
    disk_offload,
    dispatch_model,
    init_empty_weights,
    init_on_device,
    load_checkpoint_and_dispatch,
)
from .data_loader import skip_first_batches
from .launchers import debug_launcher, notebook_launcher
from .state import PartialState
from .utils import (
    DeepSpeedPlugin,
    DistributedDataParallelKwargs,
    DistributedType,
    FullyShardedDataParallelPlugin,
    GradScalerKwargs,
    InitProcessGroupKwargs,
    find_executable_batch_size,
    infer_auto_device_map,
    is_rich_available,
    load_checkpoint_in_model,
    synchronize_rng_states,
)


if is_rich_available():
				from .utils import rich



