





import logging
import os
from dataclasses import dataclass
from typing import List, Optional, Union

import tqdm
from filelock import FileLock

from transformers import (
    BartTokenizer,
    BartTokenizerFast,
    DataProcessor,
    PreTrainedTokenizer,
    RobertaTokenizer,
    RobertaTokenizerFast,
    XLMRobertaTokenizer,
    is_tf_available,
    is_torch_available,
)


__lowerCamelCase :  Optional[Any]										=  logging.getLogger(__name__)



@dataclass(frozen=__snake_case    )
class       A__     :
      _UpperCAmelCase			:str
      _UpperCAmelCase			:str
      _UpperCAmelCase			:Optional[str]							=     None
      _UpperCAmelCase			:Optional[str]							=     None
      _UpperCAmelCase			:Optional[str]							=     None



@dataclass(frozen=__snake_case    )
class       A__     :
      _UpperCAmelCase			:List[int]
      _UpperCAmelCase			:Optional[List[int]]							=     None
      _UpperCAmelCase			:Optional[List[int]]							=     None
      _UpperCAmelCase			:Optional[Union[int, float]]							=     None
      _UpperCAmelCase			:Optional[int]							=     None


if is_torch_available():
    import torch
    from torch.utils.data import Dataset



    class       A__     ( __snake_case    ):
          _UpperCAmelCase			:List[InputFeatures]





          def __init__(				self ,							A_ ,							A_ ,							A_ ,							A_ = None ,							A_=False ,							A_ = False ,							):
           '''simple docstring'''




           UpperCamelCase			:      Optional[Any] 			=     hans_processors[task]()

           UpperCamelCase			:      Optional[Any] 			=     os.path.join(
               A_ ,							"cached_{}_{}_{}_{}".format(
                   "dev" if evaluate else "train" ,							tokenizer.__class__.__name__ ,							str(A_     ) ,							A_ ,							) ,							)
           UpperCamelCase			:      List[str] 			=     processor.get_labels()
           if tokenizer.__class__ in (
               RobertaTokenizer,
               RobertaTokenizerFast,
               XLMRobertaTokenizer,
               BartTokenizer,
               BartTokenizerFast,
           ):
            # HACK(label indices are swapped in RoBERTa pretrained model)
            UpperCamelCase			,					UpperCamelCase			:      int 			=     label_list[2], label_list[1]
           UpperCamelCase			:      Dict 			=     label_list

           # Make sure only the first process in distributed training processes the dataset,
           # and the others will use the cache.
           UpperCamelCase			:      int 			=     cached_features_file + ".lock"
           with FileLock(A_     ):
            if os.path.exists(A_     ) and not overwrite_cache:
             logger.info(F"""Loading features from cached file {cached_features_file}"""     )
             UpperCamelCase			:      List[str] 			=     torch.load(A_     )
            else:
             logger.info(F"""Creating features from dataset file at {data_dir}"""     )

             UpperCamelCase			:      List[Any] 			=     (
                 processor.get_dev_examples(A_     ) if evaluate else processor.get_train_examples(A_     )
             )

             logger.info("Training examples: %s" ,							len(A_     )     )
             UpperCamelCase			:      Tuple 			=     hans_convert_examples_to_features(A_ ,							A_ ,							A_ ,							A_     )
             logger.info("Saving features into cached file %s" ,							A_     )
             torch.save(self.features ,							A_     )





          def __len__(				self     ):
           '''simple docstring'''




           return len(self.features     )





          def __getitem__(				self ,							A_     ):
           '''simple docstring'''




           return self.features[i]





          def  __UpperCamelCase(				self     ):
           '''simple docstring'''




           return self.label_list


if is_tf_available():
    import tensorflow as tf



    class       A__     :
          _UpperCAmelCase			:List[InputFeatures]





          def __init__(				self ,							A_ ,							A_ ,							A_ ,							A_ = 128 ,							A_=False ,							A_ = False ,							):
           '''simple docstring'''




           UpperCamelCase			:      Optional[Any] 			=     hans_processors[task]()
           UpperCamelCase			:      Optional[int] 			=     processor.get_labels()
           if tokenizer.__class__ in (
               RobertaTokenizer,
               RobertaTokenizerFast,
               XLMRobertaTokenizer,
               BartTokenizer,
               BartTokenizerFast,
           ):
            # HACK(label indices are swapped in RoBERTa pretrained model)
            UpperCamelCase			,					UpperCamelCase			:      Any 			=     label_list[2], label_list[1]
           UpperCamelCase			:      Tuple 			=     label_list

           UpperCamelCase			:      Optional[Any] 			=     processor.get_dev_examples(A_     ) if evaluate else processor.get_train_examples(A_     )
           UpperCamelCase			:      str 			=     hans_convert_examples_to_features(A_ ,							A_ ,							A_ ,							A_     )

           def gen():
            for ex_index, ex in tqdm.tqdm(enumerate(self.features     ) ,							desc="convert examples to features"     ):
             if ex_index % 1_0000 == 0:
              logger.info("Writing example %d of %d" % (ex_index, len(A_     ))     )

             yield (
                 {
                     "example_id": 0,
                     "input_ids": ex.input_ids,
                     "attention_mask": ex.attention_mask,
                     "token_type_ids": ex.token_type_ids,
                 },
                 ex.label,
             )

           UpperCamelCase			:      str 			=     tf.data.Dataset.from_generator(
               A_ ,							(
                   {
                       "example_id": tf.intaa,
                       "input_ids": tf.intaa,
                       "attention_mask": tf.intaa,
                       "token_type_ids": tf.intaa,
                   },
                   tf.intaa,
               ) ,							(
                   {
                       "example_id": tf.TensorShape([]     ),
                       "input_ids": tf.TensorShape([None, None]     ),
                       "attention_mask": tf.TensorShape([None, None]     ),
                       "token_type_ids": tf.TensorShape([None, None]     ),
                   },
                   tf.TensorShape([]     ),
               ) ,							)





          def  __UpperCamelCase(				self     ):
           '''simple docstring'''




           return self.dataset





          def __len__(				self     ):
           '''simple docstring'''




           return len(self.features     )





          def __getitem__(				self ,							A_     ):
           '''simple docstring'''




           return self.features[i]





          def  __UpperCamelCase(				self     ):
           '''simple docstring'''




           return self.label_list



class       A__     ( __snake_case    ):





      def  __UpperCamelCase(				self ,							A_     ):
       '''simple docstring'''




       return self._create_examples(self._read_tsv(os.path.join(A_ ,							"heuristics_train_set.txt"     )     ) ,							"train"     )





      def  __UpperCamelCase(				self ,							A_     ):
       '''simple docstring'''




       return self._create_examples(self._read_tsv(os.path.join(A_ ,							"heuristics_evaluation_set.txt"     )     ) ,							"dev"     )





      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




       return ["contradiction", "entailment", "neutral"]





      def  __UpperCamelCase(				self ,							A_ ,							A_     ):
       '''simple docstring'''




       UpperCamelCase			:      Optional[Any] 			=     []
       for i, line in enumerate(A_     ):
        if i == 0:
         continue
        UpperCamelCase			:      Union[str, Any] 			=     "%s-%s" % (set_type, line[0])
        UpperCamelCase			:      Optional[int] 			=     line[5]
        UpperCamelCase			:      Tuple 			=     line[6]
        UpperCamelCase			:      int 			=     line[7][2:] if line[7].startswith("ex"     ) else line[7]
        UpperCamelCase			:      Optional[Any] 			=     line[0]
        examples.append(InputExample(guid=A_ ,							text_a=A_ ,							text_b=A_ ,							label=A_ ,							pairID=A_     )     )
       return examples



def 	A_       (  _lowerCAmelCase							,     _lowerCAmelCase							,     _lowerCAmelCase							,     _lowerCAmelCase							,     )		->				int:
 UpperCamelCase			:      Optional[int] 			=     {label: i for i, label in enumerate(_lowerCAmelCase  )}

 UpperCamelCase			:      List[str] 			=     []
 for ex_index, example in tqdm.tqdm(enumerate(_lowerCAmelCase  )							,     desc="convert examples to features"  ):
  if ex_index % 1_0000 == 0:
   logger.info("Writing example %d" % (ex_index)  )

  UpperCamelCase			:      List[str] 			=     tokenizer(
      example.text_a							,     example.text_b							,     add_special_tokens=_lowerCAmelCase							,     max_length=_lowerCAmelCase							,     padding="max_length"							,     truncation=_lowerCAmelCase							,     return_overflowing_tokens=_lowerCAmelCase							,     )

  UpperCamelCase			:      str 			=     label_map[example.label] if example.label in label_map else 0

  UpperCamelCase			:      Tuple 			=     int(example.pairID  )

  features.append(InputFeatures(**_lowerCAmelCase							,     label=_lowerCAmelCase							,     pairID=_lowerCAmelCase  )  )

 for i, example in enumerate(examples[:5]  ):
  logger.info("*** Example ***"  )
  logger.info(F"""guid: {example}"""  )
  logger.info(F"""features: {features[i]}"""  )

 return features


__lowerCamelCase :  Tuple										=  {
    """hans""": 3,
}


__lowerCamelCase :  List[Any]										=  {
    """hans""": HansProcessor,
}







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





      def __init__(				self ,							A_ ,							A_=13 ,							A_=7 ,							A_=True ,							A_=True ,							A_=True ,							A_=True ,							A_=99 ,							A_=32 ,							A_=2 ,							A_=4 ,							A_=37 ,							A_="gelu" ,							A_=0.1 ,							A_=0.1 ,							A_=512 ,							A_=16 ,							A_=2 ,							A_=0.02 ,							A_=3 ,							A_=4 ,							A_=None ,							):
       '''simple docstring'''




       UpperCamelCase			:      Dict 			=     parent
       UpperCamelCase			:      str 			=     13
       UpperCamelCase			:      int 			=     7
       UpperCamelCase			:      str 			=     True
       UpperCamelCase			:      Dict 			=     True
       UpperCamelCase			:      str 			=     True
       UpperCamelCase			:      Tuple 			=     True
       UpperCamelCase			:      List[str] 			=     99
       UpperCamelCase			:      Optional[Any] 			=     384
       UpperCamelCase			:      Tuple 			=     2
       UpperCamelCase			:      Union[str, Any] 			=     4
       UpperCamelCase			:      Dict 			=     37
       UpperCamelCase			:      Any 			=     "gelu"
       UpperCamelCase			:      List[Any] 			=     0.1
       UpperCamelCase			:      int 			=     0.1
       UpperCamelCase			:      Tuple 			=     512
       UpperCamelCase			:      List[Any] 			=     16
       UpperCamelCase			:      int 			=     2
       UpperCamelCase			:      Dict 			=     0.02
       UpperCamelCase			:      Optional[Any] 			=     3
       UpperCamelCase			:      List[Any] 			=     4
       UpperCamelCase			:      Dict 			=     128
       UpperCamelCase			:      Optional[Any] 			=     2
       UpperCamelCase			:      Optional[int] 			=     9
       UpperCamelCase			:      Optional[int] 			=     1
       UpperCamelCase			:      Union[str, Any] 			=     None





      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




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

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

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

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

       UpperCamelCase			:      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=A_ ,							)

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





      def  __UpperCamelCase(				self ,							A_ ,							A_ ,							A_ ,							A_ ,							A_ ,							A_ ,							A_     ):
       '''simple docstring'''




       UpperCamelCase			:      str 			=     TFConvBertModel(config=A_     )
       UpperCamelCase			:      Optional[Any] 			=     {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}

       UpperCamelCase			:      Optional[int] 			=     [input_ids, input_mask]
       UpperCamelCase			:      Any 			=     model(A_     )

       UpperCamelCase			:      int 			=     model(A_     )

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





      def  __UpperCamelCase(				self ,							A_ ,							A_ ,							A_ ,							A_ ,							A_ ,							A_ ,							A_     ):
       '''simple docstring'''




       UpperCamelCase			:      Tuple 			=     TFConvBertForMaskedLM(config=A_     )
       UpperCamelCase			:      int 			=     {
           "input_ids": input_ids,
           "attention_mask": input_mask,
           "token_type_ids": token_type_ids,
       }
       UpperCamelCase			:      Dict 			=     model(A_     )
       self.parent.assertEqual(result.logits.shape ,							(self.batch_size, self.seq_length, self.vocab_size)     )





      def  __UpperCamelCase(				self ,							A_ ,							A_ ,							A_ ,							A_ ,							A_ ,							A_ ,							A_     ):
       '''simple docstring'''




       UpperCamelCase			:      Dict 			=     self.num_labels
       UpperCamelCase			:      int 			=     TFConvBertForSequenceClassification(config=A_     )
       UpperCamelCase			:      List[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 ,							A_ ,							A_ ,							A_ ,							A_ ,							A_ ,							A_ ,							A_     ):
       '''simple docstring'''




       UpperCamelCase			:      List[str] 			=     self.num_choices
       UpperCamelCase			:      str 			=     TFConvBertForMultipleChoice(config=A_     )
       UpperCamelCase			:      List[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			:      Any 			=     tf.tile(tf.expand_dims(A_ ,							1     ) ,							(1, self.num_choices, 1)     )
       UpperCamelCase			:      List[str] 			=     {
           "input_ids": multiple_choice_inputs_ids,
           "attention_mask": multiple_choice_input_mask,
           "token_type_ids": multiple_choice_token_type_ids,
       }
       UpperCamelCase			:      Optional[Any] 			=     model(A_     )
       self.parent.assertEqual(result.logits.shape ,							(self.batch_size, self.num_choices)     )





      def  __UpperCamelCase(				self ,							A_ ,							A_ ,							A_ ,							A_ ,							A_ ,							A_ ,							A_     ):
       '''simple docstring'''




       UpperCamelCase			:      Dict 			=     self.num_labels
       UpperCamelCase			:      str 			=     TFConvBertForTokenClassification(config=A_     )
       UpperCamelCase			:      List[Any] 			=     {
           "input_ids": input_ids,
           "attention_mask": input_mask,
           "token_type_ids": token_type_ids,
       }
       UpperCamelCase			:      str 			=     model(A_     )
       self.parent.assertEqual(result.logits.shape ,							(self.batch_size, self.seq_length, self.num_labels)     )





      def  __UpperCamelCase(				self ,							A_ ,							A_ ,							A_ ,							A_ ,							A_ ,							A_ ,							A_     ):
       '''simple docstring'''




       UpperCamelCase			:      List[str] 			=     TFConvBertForQuestionAnswering(config=A_     )
       UpperCamelCase			:      Union[str, 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.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     ):
       '''simple docstring'''




       UpperCamelCase			:      Optional[int] 			=     self.prepare_config_and_inputs()
       (
           (
           UpperCamelCase
       )			,					(
           UpperCamelCase
       )			,					(
           UpperCamelCase
       )			,					(
           UpperCamelCase
       )			,					(
           UpperCamelCase
       )			,					(
           UpperCamelCase
       )			,					(
           UpperCamelCase
       )			,					
       )			:      Optional[Any] 			=     config_and_inputs
       UpperCamelCase			:      int 			=     {"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    ):
      _UpperCAmelCase			:Dict							=     (
          (
              TFConvBertModel,
              TFConvBertForMaskedLM,
              TFConvBertForQuestionAnswering,
              TFConvBertForSequenceClassification,
              TFConvBertForTokenClassification,
              TFConvBertForMultipleChoice,
          )
          if is_tf_available()
          else ()
      )
      _UpperCAmelCase			: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 {}
      )
      _UpperCAmelCase			:Any							=     False
      _UpperCAmelCase			:int							=     False
      _UpperCAmelCase			:str							=     False





      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




       UpperCamelCase			:      Dict 			=     TFConvBertModelTester(self     )
       UpperCamelCase			:      Dict 			=     ConfigTester(self ,							config_class=A_ ,							hidden_size=37     )





      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




       self.config_tester.run_common_tests()





      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




       UpperCamelCase			:      Dict 			=     self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*A_     )





      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




       UpperCamelCase			:      Dict 			=     self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_masked_lm(*A_     )





      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




       UpperCamelCase			:      List[str] 			=     self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_multiple_choice(*A_     )





      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




       UpperCamelCase			:      List[str] 			=     self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_question_answering(*A_     )





      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




       UpperCamelCase			:      Tuple 			=     self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_sequence_classification(*A_     )





      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




       UpperCamelCase			:      Optional[Any] 			=     self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_token_classification(*A_     )





      @slow
      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




       UpperCamelCase			,					UpperCamelCase			:      Dict 			=     self.model_tester.prepare_config_and_inputs_for_common()
       UpperCamelCase			:      Optional[Any] 			=     True
       UpperCamelCase			:      Any 			=     True

       if hasattr(A_ ,							"use_cache"     ):
        UpperCamelCase			:      List[str] 			=     True

       UpperCamelCase			:      List[Any] 			=     getattr(self.model_tester ,							"encoder_seq_length" ,							self.model_tester.seq_length     )
       UpperCamelCase			:      Any 			=     getattr(self.model_tester ,							"key_length" ,							A_     )

       for model_class in self.all_model_classes:
        UpperCamelCase			:      List[Any] 			=     self._prepare_for_class(A_ ,							A_     )
        UpperCamelCase			:      Dict 			=     model_class(A_     )
        UpperCamelCase			:      Optional[int] 			=     len(model(A_     )     )

        with tempfile.TemporaryDirectory() as tmpdirname:
         model.save_pretrained(A_ ,							saved_model=A_     )
         UpperCamelCase			:      Union[str, Any] 			=     os.path.join(A_ ,							"saved_model" ,							"1"     )
         UpperCamelCase			:      Dict 			=     tf.keras.models.load_model(A_     )
         UpperCamelCase			:      str 			=     model(A_     )

         if self.is_encoder_decoder:
          UpperCamelCase			:      Union[str, Any] 			=     outputs["encoder_hidden_states"]
          UpperCamelCase			:      Any 			=     outputs["encoder_attentions"]
         else:
          UpperCamelCase			:      Any 			=     outputs["hidden_states"]
          UpperCamelCase			:      List[str] 			=     outputs["attentions"]

         self.assertEqual(len(A_     ) ,							A_     )

         UpperCamelCase			:      int 			=     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     ):
       '''simple docstring'''




       UpperCamelCase			:      Union[str, Any] 			=     TFConvBertModel.from_pretrained("YituTech/conv-bert-base"     )
       self.assertIsNotNone(A_     )





      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




       UpperCamelCase			,					UpperCamelCase			:      Tuple 			=     self.model_tester.prepare_config_and_inputs_for_common()
       UpperCamelCase			:      Dict 			=     True
       UpperCamelCase			:      int 			=     getattr(self.model_tester ,							"decoder_seq_length" ,							self.model_tester.seq_length     )
       UpperCamelCase			:      Optional[int] 			=     getattr(self.model_tester ,							"encoder_seq_length" ,							self.model_tester.seq_length     )
       UpperCamelCase			:      Optional[int] 			=     getattr(self.model_tester ,							"key_length" ,							A_     )
       UpperCamelCase			:      Optional[Any] 			=     getattr(self.model_tester ,							"key_length" ,							A_     )

       def check_decoder_attentions_output(A_     ):
        UpperCamelCase			:      Optional[Any] 			=     len(A_     )
        self.assertEqual(out_len % 2 ,							0     )
        UpperCamelCase			:      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(A_     ):
        UpperCamelCase			:      Dict 			=     [
            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			:      Union[str, Any] 			=     True
        UpperCamelCase			:      List[Any] 			=     False
        UpperCamelCase			:      Dict 			=     model_class(A_     )
        UpperCamelCase			:      Dict 			=     model(self._prepare_for_class(A_ ,							A_     )     )
        UpperCamelCase			:      List[str] 			=     len(A_     )
        self.assertEqual(config.output_hidden_states ,							A_     )
        check_encoder_attentions_output(A_     )

        if self.is_encoder_decoder:
         UpperCamelCase			:      int 			=     model_class(A_     )
         UpperCamelCase			:      Tuple 			=     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			:      Tuple 			=     True
        UpperCamelCase			:      int 			=     model_class(A_     )
        UpperCamelCase			:      Dict 			=     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			:      Optional[int] 			=     True
        UpperCamelCase			:      List[str] 			=     True
        UpperCamelCase			:      Optional[int] 			=     model_class(A_     )
        UpperCamelCase			:      Optional[Any] 			=     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    ):





      @slow
      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




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

       UpperCamelCase			:      int 			=     [1, 6, 768]
       self.assertEqual(output.shape ,							A_     )

       UpperCamelCase			:      List[str] 			=     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] ,							A_ ,							atol=1e-4     )
