




from __future__ import annotations

import unittest

from transformers import DistilBertConfig, 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.models.distilbert.modeling_tf_distilbert import (
		    TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
		    TFDistilBertForMaskedLM,
		    TFDistilBertForMultipleChoice,
		    TFDistilBertForQuestionAnswering,
		    TFDistilBertForSequenceClassification,
		    TFDistilBertForTokenClassification,
		    TFDistilBertModel,
		)




class 					snake_case__							:
				"""simple docstring"""



				def __init__(   self       :  List[Any]      ,   __lowerCamelCase       :  Optional[Any]      ,   )      ->  Optional[Any]:
							a					=    parent
							a					=    13
							a					=    7
							a					=    True
							a					=    True
							a					=    False
							a					=    True
							a					=    99
							a					=    32
							a					=    2
							a					=    4
							a					=    37
							a					=    "gelu"
							a					=    0.1
							a					=    0.1
							a					=    5_12
							a					=    16
							a					=    2
							a					=    0.02
							a					=    3
							a					=    4
							a					=    None





				def 							__UpperCAmelCase							(   self       :  int							)      ->  Any:
							a					=    ids_tensor([self.batch_size, self.seq_length]      ,   self.vocab_size							)

							a					=    None
							if self.use_input_mask:
										a					=    random_attention_mask([self.batch_size, self.seq_length]							)

							a					=    None
							a					=    None
							a					=    None
							if self.use_labels:
										a					=    ids_tensor([self.batch_size]      ,   self.type_sequence_label_size							)
										a					=    ids_tensor([self.batch_size, self.seq_length]      ,   self.num_labels							)
										a					=    ids_tensor([self.batch_size]      ,   self.num_choices							)

							a					=    DistilBertConfig(
							    vocab_size=self.vocab_size      ,   dim=self.hidden_size      ,   n_layers=self.num_hidden_layers      ,   n_heads=self.num_attention_heads      ,   hidden_dim=self.intermediate_size      ,   hidden_act=self.hidden_act      ,   dropout=self.hidden_dropout_prob      ,   attention_dropout=self.attention_probs_dropout_prob      ,   max_position_embeddings=self.max_position_embeddings      ,   initializer_range=self.initializer_range      ,   )

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





				def 							__UpperCAmelCase							(   self       :  Union[str, Any]      ,   __lowerCamelCase       :  int      ,   __lowerCamelCase       :  List[Any]      ,   __lowerCamelCase       :  Optional[int]      ,   __lowerCamelCase       :  List[Any]      ,   __lowerCamelCase       :  Dict      ,   __lowerCamelCase       :  int							)      ->  Any:
							a					=    TFDistilBertModel(config=__lowerCamelCase							)
							a					=    {"input_ids": input_ids, "attention_mask": input_mask}

							a					=    model(__lowerCamelCase							)

							a					=    [input_ids, input_mask]

							a					=    model(__lowerCamelCase							)

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





				def 							__UpperCAmelCase							(   self       :  Dict      ,   __lowerCamelCase       :  int      ,   __lowerCamelCase       :  Optional[int]      ,   __lowerCamelCase       :  Optional[int]      ,   __lowerCamelCase       :  str      ,   __lowerCamelCase       :  Tuple      ,   __lowerCamelCase       :  Dict							)      ->  Any:
							a					=    TFDistilBertForMaskedLM(config=__lowerCamelCase							)
							a					=    {"input_ids": input_ids, "attention_mask": input_mask}
							a					=    model(__lowerCamelCase							)
							self.parent.assertEqual(result.logits.shape      ,   (self.batch_size, self.seq_length, self.vocab_size)							)





				def 							__UpperCAmelCase							(   self       :  List[Any]      ,   __lowerCamelCase       :  Dict      ,   __lowerCamelCase       :  Union[str, Any]      ,   __lowerCamelCase       :  Union[str, Any]      ,   __lowerCamelCase       :  Optional[int]      ,   __lowerCamelCase       :  int      ,   __lowerCamelCase       :  Tuple							)      ->  Optional[Any]:
							a					=    TFDistilBertForQuestionAnswering(config=__lowerCamelCase							)
							a					=    {
							    "input_ids": input_ids,
							    "attention_mask": input_mask,
							}
							a					=    model(__lowerCamelCase							)
							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       :  int      ,   __lowerCamelCase       :  Dict      ,   __lowerCamelCase       :  Any      ,   __lowerCamelCase       :  Dict      ,   __lowerCamelCase       :  Tuple      ,   __lowerCamelCase       :  Any      ,   __lowerCamelCase       :  List[str]							)      ->  Tuple:
							a					=    self.num_labels
							a					=    TFDistilBertForSequenceClassification(__lowerCamelCase							)
							a					=    {"input_ids": input_ids, "attention_mask": input_mask}
							a					=    model(__lowerCamelCase							)
							self.parent.assertEqual(result.logits.shape      ,   (self.batch_size, self.num_labels)							)





				def 							__UpperCAmelCase							(   self       :  Dict      ,   __lowerCamelCase       :  List[str]      ,   __lowerCamelCase       :  Optional[Any]      ,   __lowerCamelCase       :  List[str]      ,   __lowerCamelCase       :  Union[str, Any]      ,   __lowerCamelCase       :  Any      ,   __lowerCamelCase       :  Dict							)      ->  Dict:
							a					=    self.num_choices
							a					=    TFDistilBertForMultipleChoice(__lowerCamelCase							)
							a					=    tf.tile(tf.expand_dims(__lowerCamelCase      ,   1							)      ,   (1, self.num_choices, 1)							)
							a					=    tf.tile(tf.expand_dims(__lowerCamelCase      ,   1							)      ,   (1, self.num_choices, 1)							)
							a					=    {
							    "input_ids": multiple_choice_inputs_ids,
							    "attention_mask": multiple_choice_input_mask,
							}
							a					=    model(__lowerCamelCase							)
							self.parent.assertEqual(result.logits.shape      ,   (self.batch_size, self.num_choices)							)





				def 							__UpperCAmelCase							(   self       :  int      ,   __lowerCamelCase       :  Tuple      ,   __lowerCamelCase       :  Optional[int]      ,   __lowerCamelCase       :  Tuple      ,   __lowerCamelCase       :  Tuple      ,   __lowerCamelCase       :  Optional[Any]      ,   __lowerCamelCase       :  Any							)      ->  Dict:
							a					=    self.num_labels
							a					=    TFDistilBertForTokenClassification(__lowerCamelCase							)
							a					=    {"input_ids": input_ids, "attention_mask": input_mask}
							a					=    model(__lowerCamelCase							)
							self.parent.assertEqual(result.logits.shape      ,   (self.batch_size, self.seq_length, self.num_labels)							)





				def 							__UpperCAmelCase							(   self       :  Tuple							)      ->  Optional[Any]:
							a					=    self.prepare_config_and_inputs()
							((a)					,	(a)					,	(a)					,	(a)					,	(a)					,	(a))					=    config_and_inputs
							a					=    {"input_ids": input_ids, "attention_mask": input_mask}
							return config, inputs_dict




@require_tf
class 					snake_case__							(_UpperCamelCase					, _UpperCamelCase					, unittest.TestCase     ):
				"""simple docstring"""



				SCREAMING_SNAKE_CASE_ :							Optional[int]											=      (
				    (
				        TFDistilBertModel,
				        TFDistilBertForMaskedLM,
				        TFDistilBertForQuestionAnswering,
				        TFDistilBertForSequenceClassification,
				        TFDistilBertForTokenClassification,
				        TFDistilBertForMultipleChoice,
				    )
				    if is_tf_available()
				    else None
				)
				SCREAMING_SNAKE_CASE_ :							Tuple											=      (
				    {
				        """feature-extraction""": TFDistilBertModel,
				        """fill-mask""": TFDistilBertForMaskedLM,
				        """question-answering""": TFDistilBertForQuestionAnswering,
				        """text-classification""": TFDistilBertForSequenceClassification,
				        """token-classification""": TFDistilBertForTokenClassification,
				        """zero-shot""": TFDistilBertForSequenceClassification,
				    }
				    if is_tf_available()
				    else {}
				)
				SCREAMING_SNAKE_CASE_ :							Optional[int]											=      False
				SCREAMING_SNAKE_CASE_ :							List[str]											=      False





				def 							__UpperCAmelCase							(   self       :  Optional[int]							)      ->  Any:
							a					=    TFDistilBertModelTester(self							)
							a					=    ConfigTester(self      ,   config_class=__lowerCamelCase      ,   dim=37							)





				def 							__UpperCAmelCase							(   self       :  List[Any]							)      ->  Union[str, Any]:
							self.config_tester.run_common_tests()





				def 							__UpperCAmelCase							(   self       :  Any							)      ->  Any:
							a					=    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_distilbert_model(*__lowerCamelCase							)





				def 							__UpperCAmelCase							(   self       :  Dict							)      ->  Union[str, Any]:
							a					=    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_distilbert_for_masked_lm(*__lowerCamelCase							)





				def 							__UpperCAmelCase							(   self       :  Tuple							)      ->  Dict:
							a					=    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_distilbert_for_question_answering(*__lowerCamelCase							)





				def 							__UpperCAmelCase							(   self       :  Tuple							)      ->  Optional[int]:
							a					=    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_distilbert_for_sequence_classification(*__lowerCamelCase							)





				def 							__UpperCAmelCase							(   self       :  List[str]							)      ->  List[str]:
							a					=    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_distilbert_for_multiple_choice(*__lowerCamelCase							)





				def 							__UpperCAmelCase							(   self       :  List[Any]							)      ->  int:
							a					=    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_distilbert_for_token_classification(*__lowerCamelCase							)





				@slow
				def 							__UpperCAmelCase							(   self       :  List[Any]							)      ->  Union[str, Any]:
							for model_name in list(TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]							):
										a					=    TFDistilBertModel.from_pretrained(__lowerCamelCase							)
										self.assertIsNotNone(__lowerCamelCase							)




@require_tf
class 					snake_case__							(unittest.TestCase     ):
				"""simple docstring"""



				@slow
				def 							__UpperCAmelCase							(   self       :  Optional[int]							)      ->  int:
							a					=    TFDistilBertModel.from_pretrained("distilbert-base-uncased"							)
							a					=    tf.constant([[0, 1, 2, 3, 4, 5]]							)
							a					=    model(__lowerCamelCase							)[0]

							a					=    [1, 6, 7_68]
							self.assertEqual(output.shape      ,   __lowerCamelCase							)

							a					=    tf.constant(
							    [
							        [
							            [0.19_261_885, -0.13_732_955, 0.4_119_799],
							            [0.22_150_156, -0.07_422_661, 0.39_037_204],
							            [0.22_756_018, -0.0_896_414, 0.3_701_467],
							        ]
							    ]							)
							tf.debugging.assert_near(output[:, :3, :3]      ,   __lowerCamelCase      ,   atol=1e-4							)












import argparse
import os
import re


__lowerCAmelCase	:       Union[str, Any]     =	'src/transformers/models/auto'


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




def     __magic_name__       (				A	:      int,						A	:      bool = False     ):






			'''simple docstring'''







			with open(A,						"r",						encoding="utf-8"     ) as f:
						a					=    f.read()

			a					=    content.split("\n"     )
			a					=    []
			a					=    0
			while line_idx < len(A     ):
						if _re_intro_mapping.search(lines[line_idx]     ) is not None:
									a					=    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

									a					=    []
									while lines[line_idx].strip() != "]":
												# Blocks either fit in one line or not
												if lines[line_idx].strip() == "(":
															a					=    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
									a					=    sorted(A,						key=lambda A     : _re_identifier.search(A     ).groups()[0]     )
									new_lines += blocks
						else:
									new_lines.append(lines[line_idx]     )
									line_idx += 1

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




def     __magic_name__       (				A	:      bool = False     ):






			'''simple docstring'''







			a					=    [os.path.join(A,						A     ) for f in os.listdir(A     ) if f.endswith(".py"     )]
			a					=    [sort_auto_mapping(A,						overwrite=A     ) for fname in fnames]

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


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

		sort_all_auto_mappings(not args.check_only)






