

'''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 				_snake_case			:
   def __init__(			self      :   Optional[int]  ,SCREAMING_SNAKE_CASE__      :   Tuple  ,SCREAMING_SNAKE_CASE__      :   List[str]=13  ,SCREAMING_SNAKE_CASE__      :   Optional[Any]=7  ,SCREAMING_SNAKE_CASE__      :   List[str]=True  ,SCREAMING_SNAKE_CASE__      :   List[str]=True  ,SCREAMING_SNAKE_CASE__      :   List[Any]=True  ,SCREAMING_SNAKE_CASE__      :   List[str]=True  ,SCREAMING_SNAKE_CASE__      :   List[str]=99  ,SCREAMING_SNAKE_CASE__      :   List[Any]=32  ,SCREAMING_SNAKE_CASE__      :   int=2  ,SCREAMING_SNAKE_CASE__      :   Union[str, Any]=4  ,SCREAMING_SNAKE_CASE__      :   Optional[Any]=37  ,SCREAMING_SNAKE_CASE__      :   Tuple="gelu"  ,SCREAMING_SNAKE_CASE__      :   Tuple=0.1  ,SCREAMING_SNAKE_CASE__      :   List[str]=0.1  ,SCREAMING_SNAKE_CASE__      :   str=512  ,SCREAMING_SNAKE_CASE__      :   Optional[Any]=16  ,SCREAMING_SNAKE_CASE__      :   Any=2  ,SCREAMING_SNAKE_CASE__      :   List[Any]=0.02  ,SCREAMING_SNAKE_CASE__      :   str=3  ,SCREAMING_SNAKE_CASE__      :   Any=4  ,SCREAMING_SNAKE_CASE__      :   Any=None  ,):
     SCREAMING_SNAKE_CASE:str       =       parent
     SCREAMING_SNAKE_CASE:Union[str, Any]       =       13
     SCREAMING_SNAKE_CASE:List[str]       =       7
     SCREAMING_SNAKE_CASE:Any       =       True
     SCREAMING_SNAKE_CASE:Any       =       True
     SCREAMING_SNAKE_CASE:int       =       True
     SCREAMING_SNAKE_CASE:List[str]       =       True
     SCREAMING_SNAKE_CASE:Dict       =       99
     SCREAMING_SNAKE_CASE:Any       =       384
     SCREAMING_SNAKE_CASE:Union[str, Any]       =       2
     SCREAMING_SNAKE_CASE:Optional[Any]       =       4
     SCREAMING_SNAKE_CASE:Dict       =       37
     SCREAMING_SNAKE_CASE:Optional[Any]       =       "gelu"
     SCREAMING_SNAKE_CASE:List[Any]       =       0.1
     SCREAMING_SNAKE_CASE:str       =       0.1
     SCREAMING_SNAKE_CASE:List[str]       =       512
     SCREAMING_SNAKE_CASE:Dict       =       16
     SCREAMING_SNAKE_CASE:Optional[int]       =       2
     SCREAMING_SNAKE_CASE:int       =       0.02
     SCREAMING_SNAKE_CASE:Any       =       3
     SCREAMING_SNAKE_CASE:int       =       4
     SCREAMING_SNAKE_CASE:List[Any]       =       128
     SCREAMING_SNAKE_CASE:Optional[int]       =       2
     SCREAMING_SNAKE_CASE:List[Any]       =       9
     SCREAMING_SNAKE_CASE:List[Any]       =       1
     SCREAMING_SNAKE_CASE:Any       =       None
   def      __UpperCamelCase    (			self      :   List[str]				):
     SCREAMING_SNAKE_CASE:Tuple       =       ids_tensor([self.batch_size, self.seq_length]  ,self.vocab_size				)

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

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

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

     SCREAMING_SNAKE_CASE: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=lowercase__  ,)

     return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
   def      __UpperCamelCase    (			self      :   Tuple  ,SCREAMING_SNAKE_CASE__      :   Any  ,SCREAMING_SNAKE_CASE__      :   Optional[Any]  ,SCREAMING_SNAKE_CASE__      :   List[str]  ,SCREAMING_SNAKE_CASE__      :   Any  ,SCREAMING_SNAKE_CASE__      :   Any  ,SCREAMING_SNAKE_CASE__      :   Any  ,SCREAMING_SNAKE_CASE__      :   Optional[Any]				):
     SCREAMING_SNAKE_CASE:str       =       TFConvBertModel(config=lowercase__				)
     SCREAMING_SNAKE_CASE:List[str]       =       {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}

     SCREAMING_SNAKE_CASE:List[str]       =       [input_ids, input_mask]
     SCREAMING_SNAKE_CASE:int       =       model(lowercase__				)

     SCREAMING_SNAKE_CASE:List[str]       =       model(lowercase__				)

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

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

     SCREAMING_SNAKE_CASE:int       =       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      __UpperCamelCase    (			self      :   Optional[int]				):
     SCREAMING_SNAKE_CASE:Tuple       =       self.prepare_config_and_inputs()
     (
         (
         SCREAMING_SNAKE_CASE		
     )   ,     (
         SCREAMING_SNAKE_CASE		
     )   ,     (
         SCREAMING_SNAKE_CASE		
     )   ,     (
         SCREAMING_SNAKE_CASE		
     )   ,     (
         SCREAMING_SNAKE_CASE		
     )   ,     (
         SCREAMING_SNAKE_CASE		
     )   ,     (
         SCREAMING_SNAKE_CASE		
     )   ,     		
     ):Optional[int]       =       config_and_inputs
     SCREAMING_SNAKE_CASE: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			(							_a						,	_a						,	unittest.TestCase    ):
   _A							:  Tuple      =       (
       (
           TFConvBertModel,
           TFConvBertForMaskedLM,
           TFConvBertForQuestionAnswering,
           TFConvBertForSequenceClassification,
           TFConvBertForTokenClassification,
           TFConvBertForMultipleChoice,
       )
       if is_tf_available()
       else ()
   )
   _A							:  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 {}
   )
   _A							:  int      =       False
   _A							:  Any      =       False
   _A							:  Optional[int]      =       False
   def      __UpperCamelCase    (			self      :   int				):
     SCREAMING_SNAKE_CASE:List[Any]       =       TFConvBertModelTester(self				)
     SCREAMING_SNAKE_CASE:Optional[Any]       =       ConfigTester(self  ,config_class=lowercase__  ,hidden_size=37				)
   def      __UpperCamelCase    (			self      :   int				):
     self.config_tester.run_common_tests()
   def      __UpperCamelCase    (			self      :   int				):
     SCREAMING_SNAKE_CASE:Any       =       self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*lowercase__				)
   def      __UpperCamelCase    (			self      :   Tuple				):
     SCREAMING_SNAKE_CASE:str       =       self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_masked_lm(*lowercase__				)
   def      __UpperCamelCase    (			self      :   Dict				):
     SCREAMING_SNAKE_CASE:Any       =       self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_multiple_choice(*lowercase__				)
   def      __UpperCamelCase    (			self      :   Any				):
     SCREAMING_SNAKE_CASE:Union[str, Any]       =       self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_question_answering(*lowercase__				)
   def      __UpperCamelCase    (			self      :   List[Any]				):
     SCREAMING_SNAKE_CASE:Dict       =       self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_sequence_classification(*lowercase__				)
   def      __UpperCamelCase    (			self      :   Tuple				):
     SCREAMING_SNAKE_CASE:List[str]       =       self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_token_classification(*lowercase__				)
   @slow
   def      __UpperCamelCase    (			self      :   Optional[int]				):
     SCREAMING_SNAKE_CASE   ,     SCREAMING_SNAKE_CASE:List[str]       =       self.model_tester.prepare_config_and_inputs_for_common()
     SCREAMING_SNAKE_CASE:Tuple       =       True
     SCREAMING_SNAKE_CASE:Optional[Any]       =       True

     if hasattr(lowercase__  ,"use_cache"				):
       SCREAMING_SNAKE_CASE:List[str]       =       True

     SCREAMING_SNAKE_CASE:Tuple       =       getattr(self.model_tester  ,"encoder_seq_length"  ,self.model_tester.seq_length				)
     SCREAMING_SNAKE_CASE:int       =       getattr(self.model_tester  ,"key_length"  ,lowercase__				)

     for model_class in self.all_model_classes:
       SCREAMING_SNAKE_CASE:str       =       self._prepare_for_class(lowercase__  ,lowercase__				)
       SCREAMING_SNAKE_CASE:Optional[int]       =       model_class(lowercase__				)
       SCREAMING_SNAKE_CASE:Optional[Any]       =       len(model(lowercase__				)				)

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

         if self.is_encoder_decoder:
           SCREAMING_SNAKE_CASE:Optional[Any]       =       outputs["encoder_hidden_states"]
           SCREAMING_SNAKE_CASE:Optional[Any]       =       outputs["encoder_attentions"]
         else:
           SCREAMING_SNAKE_CASE:Optional[Any]       =       outputs["hidden_states"]
           SCREAMING_SNAKE_CASE:int       =       outputs["attentions"]

         self.assertEqual(len(lowercase__				)  ,lowercase__				)

         SCREAMING_SNAKE_CASE:Tuple       =       getattr(
             self.model_tester  ,"expected_num_hidden_layers"  ,self.model_tester.num_hidden_layers + 1				)

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

         self.assertEqual(len(lowercase__				)  ,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      :   List[str]				):
     SCREAMING_SNAKE_CASE:Dict       =       TFConvBertModel.from_pretrained("YituTech/conv-bert-base"				)
     self.assertIsNotNone(lowercase__				)







   def      __UpperCamelCase    (			self      :   Any				):
     SCREAMING_SNAKE_CASE   ,     SCREAMING_SNAKE_CASE:Dict       =       self.model_tester.prepare_config_and_inputs_for_common()
     SCREAMING_SNAKE_CASE:Optional[int]       =       True
     SCREAMING_SNAKE_CASE:List[Any]       =       getattr(self.model_tester  ,"decoder_seq_length"  ,self.model_tester.seq_length				)
     SCREAMING_SNAKE_CASE:int       =       getattr(self.model_tester  ,"encoder_seq_length"  ,self.model_tester.seq_length				)
     SCREAMING_SNAKE_CASE:int       =       getattr(self.model_tester  ,"key_length"  ,lowercase__				)
     SCREAMING_SNAKE_CASE:Tuple       =       getattr(self.model_tester  ,"key_length"  ,lowercase__				)

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

       if self.is_encoder_decoder:
         SCREAMING_SNAKE_CASE:Union[str, Any]       =       model_class(lowercase__				)
         SCREAMING_SNAKE_CASE:Union[str, Any]       =       model(self._prepare_for_class(lowercase__  ,lowercase__				)				)
         self.assertEqual(config.output_hidden_states  ,lowercase__				)
         check_decoder_attentions_output(lowercase__				)

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

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

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





@require_tf
class 				_snake_case			(							unittest.TestCase    ):
   @slow
   def      __UpperCamelCase    (			self      :   Union[str, Any]				):
     SCREAMING_SNAKE_CASE:List[str]       =       TFConvBertModel.from_pretrained("YituTech/conv-bert-base"				)
     SCREAMING_SNAKE_CASE:List[str]       =       tf.constant([[0, 1, 2, 3, 4, 5]]				)
     SCREAMING_SNAKE_CASE:Any       =       model(lowercase__				)[0]

     SCREAMING_SNAKE_CASE:Union[str, Any]       =       [1, 6, 768]
     self.assertEqual(output.shape  ,lowercase__				)

     SCREAMING_SNAKE_CASE:int       =       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]  ,lowercase__  ,atol=1e-4				)







import argparse
import os
import re


_lowercase:	Optional[Any]       			=		'''src/transformers/models/auto'''


# re pattern that matches mapping introductions:
#    SUPER_MODEL_MAPPING_NAMES = OrderedDict or SUPER_MODEL_MAPPING = OrderedDict
_lowercase:	Union[str, Any]       			=		re.compile(R'''[A-Z_]+_MAPPING(\s+|_[A-Z_]+\s+)=\s+OrderedDict''')
# re pattern that matches identifiers in mappings
_lowercase:	Optional[Any]       			=		re.compile(R'''\s*\(\s*"(\S[^"]+)"''')






def _lowerCamelCase  (		snake_case     , snake_case = False  ):
							with open(snake_case     , 'r'     , encoding='utf-8'  ) as f:
														_lowerCAmelCase							=							f.read()

							_lowerCAmelCase							=							content.split('\n'  )
							_lowerCAmelCase							=							[]
							_lowerCAmelCase							=							0
							while line_idx < len(snake_case  ):
														if _re_intro_mapping.search(lines[line_idx]  ) is not None:
																					_lowerCAmelCase							=							len(re.search(R'^(\s*)\S'     , lines[line_idx]  ).groups()[0]  ) + 8
																					# Start of a new mapping!
																					while not lines[line_idx].startswith(' ' * indent + '('  ):
																												new_lines.append(lines[line_idx]  )
																												line_idx += 1

																					_lowerCAmelCase							=							[]
																					while lines[line_idx].strip() != "]":
																												# Blocks either fit in one line or not
																												if lines[line_idx].strip() == "(":
																																			_lowerCAmelCase							=							line_idx
																																			while not lines[line_idx].startswith(' ' * indent + ')'  ):
																																										line_idx += 1
																																			blocks.append('\n'.join(lines[start_idx : line_idx + 1]  )  )
																												else:
																																			blocks.append(lines[line_idx]  )
																												line_idx += 1

																					# Sort blocks by their identifiers
																					_lowerCAmelCase							=							sorted(snake_case     , key=lambda snake_case  : _re_identifier.search(snake_case  ).groups()[0]  )
																					new_lines += blocks
														else:
																					new_lines.append(lines[line_idx]  )
																					line_idx += 1

							if overwrite:
														with open(snake_case     , 'w'     , encoding='utf-8'  ) as f:
																					f.write('\n'.join(snake_case  )  )
							elif "\n".join(snake_case  ) != content:
														return True






def _lowerCamelCase  (		snake_case = False  ):
							_lowerCAmelCase							=							[os.path.join(snake_case     , snake_case  ) for f in os.listdir(snake_case  ) if f.endswith('.py'  )]
							_lowerCAmelCase							=							[sort_auto_mapping(snake_case     , overwrite=snake_case  ) for fname in fnames]

							if not overwrite and any(snake_case  ):
														_lowerCAmelCase							=							[f for f, d in zip(snake_case     , snake_case  ) if d]
														raise ValueError(
														    F'The following files have auto mappings that need sorting: {", ".join(snake_case  )}. Run `make style` to fix'
														    ' this.'  )


if __name__ == "__main__":
				_lowercase:	Any       			=		argparse.ArgumentParser()
				parser.add_argument('''--check_only''', action='''store_true''', help='''Whether to only check or fix style.''')
				_lowercase:	Tuple       			=		parser.parse_args()

				sort_all_auto_mappings(not args.check_only)






