


"""simple docstring"""


import argparse
import collections
import os
import re

from transformers.utils import direct_transformers_import


# All paths are set with the intent you should run this script from the root of the repo with the command
# python utils/check_table.py
__snake_case    					=					'src/transformers'
__snake_case    					=					'docs/source/en'
__snake_case    					=					'.'


def    _lowerCamelCase   (     lowerCamelCase__	:							Union[str, Any]       ,		lowerCamelCase__	:							Optional[Any]       ,		lowerCamelCase__	:							Any  ):
 with open(lowerCamelCase__       ,		"""r"""       ,		encoding="""utf-8"""       ,		newline="""\n"""  ) as f:
  lowercase__       :       List[str]				=						f.readlines()
 # Find the start prompt.
 lowercase__       :       str				=						0
 while not lines[start_index].startswith(lowerCamelCase__  ):
  start_index += 1
 start_index += 1

 lowercase__       :       Optional[Any]				=						start_index
 while not lines[end_index].startswith(lowerCamelCase__  ):
  end_index += 1
 end_index -= 1

 while len(lines[start_index]  ) <= 1:
  start_index += 1
 while len(lines[end_index]  ) <= 1:
  end_index -= 1
 end_index += 1
 return "".join(lines[start_index:end_index]  ), start_index, end_index, lines


# Add here suffixes that are used to identify models, separated by |
__snake_case    					=					'Model|Encoder|Decoder|ForConditionalGeneration'
# Regexes that match TF/Flax/PT model names.
__snake_case    					=					re.compile(R'TF(.*)(?:Model|Encoder|Decoder|ForConditionalGeneration)')
__snake_case    					=					re.compile(R'Flax(.*)(?:Model|Encoder|Decoder|ForConditionalGeneration)')
# Will match any TF or Flax model too so need to be in an else branch afterthe two previous regexes.
__snake_case    					=					re.compile(R'(.*)(?:Model|Encoder|Decoder|ForConditionalGeneration)')


# This is to make sure the transformers module imported is the one in the repo.
__snake_case    					=					direct_transformers_import(TRANSFORMERS_PATH)


def    _lowerCamelCase   (     lowerCamelCase__	:							List[Any]  ):
 lowercase__       :       Union[str, Any]				=						re.finditer(""".+?(?:(?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])|$)"""       ,		lowerCamelCase__  )
 return [m.group(0  ) for m in matches]


def    _lowerCamelCase   (     lowerCamelCase__	:							Optional[int]       ,		lowerCamelCase__	:							int  ):
 lowercase__       :       Any				=						2 if text == """✅""" or text == """❌""" else len(lowerCamelCase__  )
 lowercase__       :       int				=						(width - text_length) // 2
 lowercase__       :       List[str]				=						width - text_length - left_indent
 return " " * left_indent + text + " " * right_indent


def    _lowerCamelCase   (     ):
 lowercase__       :       List[str]				=						transformers_module.models.auto.configuration_auto.CONFIG_MAPPING_NAMES
 lowercase__       :       Optional[int]				=						{
     name: config_maping_names[code]
     for code, name in transformers_module.MODEL_NAMES_MAPPING.items()
     if code in config_maping_names
 }
 lowercase__       :       str				=						{name: config.replace("""Config"""       ,		""""""  ) for name, config in model_name_to_config.items()}

 # Dictionaries flagging if each model prefix has a slow/fast tokenizer, backend in PT/TF/Flax.
 lowercase__       :       Tuple				=						collections.defaultdict(lowerCamelCase__  )
 lowercase__       :       Optional[Any]				=						collections.defaultdict(lowerCamelCase__  )
 lowercase__       :       Optional[int]				=						collections.defaultdict(lowerCamelCase__  )
 lowercase__       :       Dict				=						collections.defaultdict(lowerCamelCase__  )
 lowercase__       :       Union[str, Any]				=						collections.defaultdict(lowerCamelCase__  )

 # Let's lookup through all transformers object (once).
 for attr_name in dir(lowerCamelCase__  ):
  lowercase__       :       Optional[Any]				=						None
  if attr_name.endswith("""Tokenizer"""  ):
   lowercase__       :       List[Any]				=						slow_tokenizers
   lowercase__       :       List[str]				=						attr_name[:-9]
  elif attr_name.endswith("""TokenizerFast"""  ):
   lowercase__       :       Tuple				=						fast_tokenizers
   lowercase__       :       str				=						attr_name[:-13]
  elif _re_tf_models.match(lowerCamelCase__  ) is not None:
   lowercase__       :       Tuple				=						tf_models
   lowercase__       :       List[Any]				=						_re_tf_models.match(lowerCamelCase__  ).groups()[0]
  elif _re_flax_models.match(lowerCamelCase__  ) is not None:
   lowercase__       :       List[Any]				=						flax_models
   lowercase__       :       List[Any]				=						_re_flax_models.match(lowerCamelCase__  ).groups()[0]
  elif _re_pt_models.match(lowerCamelCase__  ) is not None:
   lowercase__       :       Union[str, Any]				=						pt_models
   lowercase__       :       Optional[int]				=						_re_pt_models.match(lowerCamelCase__  ).groups()[0]

  if lookup_dict is not None:
   while len(lowerCamelCase__  ) > 0:
    if attr_name in model_name_to_prefix.values():
     lowercase__       :       Tuple				=						True
     break
    # Try again after removing the last word in the name
    lowercase__       :       Union[str, Any]				=						"""""".join(camel_case_split(lowerCamelCase__  )[:-1]  )

    # Let's build that table!
 lowercase__       :       Union[str, Any]				=						list(model_name_to_config.keys()  )
 model_names.sort(key=str.lower  )
 lowercase__       :       List[Any]				=						["""Model""", """Tokenizer slow""", """Tokenizer fast""", """PyTorch support""", """TensorFlow support""", """Flax Support"""]
 # We'll need widths to properly display everything in the center (+2 is to leave one extra space on each side).
 lowercase__       :       str				=						[len(lowerCamelCase__  ) + 2 for c in columns]
 lowercase__       :       Tuple				=						max([len(lowerCamelCase__  ) for name in model_names]  ) + 2

 # Build the table per se
 lowercase__       :       List[Any]				=						"""|""" + """|""".join([_center_text(lowerCamelCase__       ,		lowerCamelCase__  ) for c, w in zip(lowerCamelCase__       ,		lowerCamelCase__  )]  ) + """|\n"""
 # Use ":-----:" format to center-aligned table cell texts
 table += "|" + "|".join([""":""" + """-""" * (w - 2) + """:""" for w in widths]  ) + "|\n"

 lowercase__       :       List[Any]				=						{True: """✅""", False: """❌"""}
 for name in model_names:
  lowercase__       :       int				=						model_name_to_prefix[name]
  lowercase__       :       List[str]				=						[
      name,
      check[slow_tokenizers[prefix]],
      check[fast_tokenizers[prefix]],
      check[pt_models[prefix]],
      check[tf_models[prefix]],
      check[flax_models[prefix]],
  ]
  table += "|" + "|".join([_center_text(lowerCamelCase__       ,		lowerCamelCase__  ) for l, w in zip(lowerCamelCase__       ,		lowerCamelCase__  )]  ) + "|\n"
 return table


def    _lowerCamelCase   (     lowerCamelCase__	:							Union[str, Any]=False  ):
 lowercase__	,       lowercase__	,       lowercase__	,       lowercase__       :       str				=						_find_text_in_file(
     filename=os.path.join(lowerCamelCase__       ,		"""index.md"""  )       ,		start_prompt="""<!--This table is updated automatically from the auto modules"""       ,		end_prompt="""<!-- End table-->"""       ,		)
 lowercase__       :       Union[str, Any]				=						get_model_table_from_auto_modules()

 if current_table != new_table:
  if overwrite:
   with open(os.path.join(lowerCamelCase__       ,		"""index.md"""  )       ,		"""w"""       ,		encoding="""utf-8"""       ,		newline="""\n"""  ) as f:
    f.writelines(lines[:start_index] + [new_table] + lines[end_index:]  )
  else:
   raise ValueError(
       """The model table in the `index.md` has not been updated. Run `make fix-copies` to fix this."""  )


if __name__ == "__main__":
   __snake_case    					=					argparse.ArgumentParser()
   parser.add_argument('--fix_and_overwrite', action='store_true', help='Whether to fix inconsistencies.')
   __snake_case    					=					parser.parse_args()

   check_model_table(args.fix_and_overwrite)



"""simple docstring"""


import inspect
import unittest

from transformers import MobileNetVaConfig
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
   import torch

   from transformers import MobileNetVaForImageClassification, MobileNetVaForSemanticSegmentation, MobileNetVaModel
   from transformers.models.mobilenet_va.modeling_mobilenet_va import MOBILENET_V2_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
   from PIL import Image

   from transformers import MobileNetVaImageProcessor

class 				_SCREAMING_SNAKE_CASE				(							__UpperCAmelCase  ):
   """simple docstring"""



   def 					UpperCAmelCase__(						self						)      ->							Union[str, Any]:
    lowercase__       :       Optional[Any]				=						self.config_class(**self.inputs_dict						)
    self.parent.assertTrue(hasattr(lowerCamelCase__ ,     """tf_padding"""						)						)
    self.parent.assertTrue(hasattr(lowerCamelCase__ ,     """depth_multiplier"""						)						)

class 				_SCREAMING_SNAKE_CASE				:
   """simple docstring"""



   def __init__(						self ,     lowerCamelCase__ ,     lowerCamelCase__=13 ,     lowerCamelCase__=3 ,     lowerCamelCase__=32 ,     lowerCamelCase__=0.25 ,     lowerCamelCase__=8 ,     lowerCamelCase__=8 ,     lowerCamelCase__=6 ,     lowerCamelCase__=32 ,     lowerCamelCase__=True ,     lowerCamelCase__=True ,     lowerCamelCase__=True ,     lowerCamelCase__="relu6" ,     lowerCamelCase__=1280 ,     lowerCamelCase__=0.1 ,     lowerCamelCase__=0.02 ,     lowerCamelCase__=True ,     lowerCamelCase__=True ,     lowerCamelCase__=10 ,     lowerCamelCase__=None ,     )      ->							List[Any]:
    lowercase__       :       int				=						parent
    lowercase__       :       Any				=						batch_size
    lowercase__       :       Optional[int]				=						num_channels
    lowercase__       :       int				=						image_size
    lowercase__       :       Tuple				=						depth_multiplier
    lowercase__       :       Tuple				=						depth_divisible_by
    lowercase__       :       str				=						min_depth
    lowercase__       :       int				=						expand_ratio
    lowercase__       :       List[str]				=						tf_padding
    lowercase__       :       Union[str, Any]				=						output_stride
    lowercase__       :       Optional[int]				=						first_layer_is_expansion
    lowercase__       :       Optional[Any]				=						finegrained_output
    lowercase__       :       Union[str, Any]				=						hidden_act
    lowercase__       :       List[Any]				=						last_hidden_size if finegrained_output else int(last_hidden_size * depth_multiplier						)
    lowercase__       :       Union[str, Any]				=						classifier_dropout_prob
    lowercase__       :       str				=						use_labels
    lowercase__       :       Optional[Any]				=						is_training
    lowercase__       :       List[Any]				=						num_labels
    lowercase__       :       List[str]				=						initializer_range
    lowercase__       :       Optional[int]				=						scope


   def 					UpperCAmelCase__(						self						)      ->							Any:
    lowercase__       :       Dict				=						floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

    lowercase__       :       List[str]				=						None
    lowercase__       :       Tuple				=						None
    if self.use_labels:
     lowercase__       :       Optional[Any]				=						ids_tensor([self.batch_size] ,     self.num_labels						)
     lowercase__       :       Dict				=						ids_tensor([self.batch_size, self.image_size, self.image_size] ,     self.num_labels						)

    lowercase__       :       Dict				=						self.get_config()

    return config, pixel_values, labels, pixel_labels


   def 					UpperCAmelCase__(						self						)      ->							int:
    return MobileNetVaConfig(
        num_channels=self.num_channels ,     image_size=self.image_size ,     depth_multiplier=self.depth_multiplier ,     depth_divisible_by=self.depth_divisible_by ,     min_depth=self.min_depth ,     expand_ratio=self.expand_ratio ,     output_stride=self.output_stride ,     first_layer_is_expansion=self.first_layer_is_expansion ,     finegrained_output=self.finegrained_output ,     hidden_act=self.hidden_act ,     tf_padding=self.tf_padding ,     classifier_dropout_prob=self.classifier_dropout_prob ,     initializer_range=self.initializer_range ,     )


   def 					UpperCAmelCase__(						self ,     lowerCamelCase__ ,     lowerCamelCase__ ,     lowerCamelCase__ ,     lowerCamelCase__						)      ->							Dict:
    lowercase__       :       str				=						MobileNetVaModel(config=lowerCamelCase__						)
    model.to(lowerCamelCase__						)
    model.eval()
    lowercase__       :       List[Any]				=						model(lowerCamelCase__						)
    self.parent.assertEqual(
        result.last_hidden_state.shape ,     (
            self.batch_size,
            self.last_hidden_size,
            self.image_size // self.output_stride,
            self.image_size // self.output_stride,
        ) ,     )
    self.parent.assertEqual(
        result.pooler_output.shape ,     (self.batch_size, self.last_hidden_size) ,     )


   def 					UpperCAmelCase__(						self ,     lowerCamelCase__ ,     lowerCamelCase__ ,     lowerCamelCase__ ,     lowerCamelCase__						)      ->							List[Any]:
    lowercase__       :       Optional[int]				=						self.num_labels
    lowercase__       :       List[str]				=						MobileNetVaForImageClassification(lowerCamelCase__						)
    model.to(lowerCamelCase__						)
    model.eval()
    lowercase__       :       Dict				=						model(lowerCamelCase__ ,     labels=lowerCamelCase__						)
    self.parent.assertEqual(result.logits.shape ,     (self.batch_size, self.num_labels)						)


   def 					UpperCAmelCase__(						self ,     lowerCamelCase__ ,     lowerCamelCase__ ,     lowerCamelCase__ ,     lowerCamelCase__						)      ->							str:
    lowercase__       :       Dict				=						self.num_labels
    lowercase__       :       List[Any]				=						MobileNetVaForSemanticSegmentation(lowerCamelCase__						)
    model.to(lowerCamelCase__						)
    model.eval()
    lowercase__       :       List[Any]				=						model(lowerCamelCase__						)
    self.parent.assertEqual(
        result.logits.shape ,     (
            self.batch_size,
            self.num_labels,
            self.image_size // self.output_stride,
            self.image_size // self.output_stride,
        ) ,     )
    lowercase__       :       Tuple				=						model(lowerCamelCase__ ,     labels=lowerCamelCase__						)
    self.parent.assertEqual(
        result.logits.shape ,     (
            self.batch_size,
            self.num_labels,
            self.image_size // self.output_stride,
            self.image_size // self.output_stride,
        ) ,     )







   def 					UpperCAmelCase__(						self						)      ->							int:
    lowercase__       :       Optional[int]				=						self.prepare_config_and_inputs()
    lowercase__	,       lowercase__	,       lowercase__	,       lowercase__       :       Tuple				=						config_and_inputs
    lowercase__       :       List[Any]				=						{"""pixel_values""": pixel_values}
    return config, inputs_dict

@require_torch
class 				_SCREAMING_SNAKE_CASE				(							__UpperCAmelCase     ,   __UpperCAmelCase     ,   unittest.TestCase  ):
   """simple docstring"""



   _a   :       Optional[int]									=    (
       (MobileNetVaModel, MobileNetVaForImageClassification, MobileNetVaForSemanticSegmentation)
       if is_torch_available()
       else ()
   )
   _a   :       Tuple									=    (
       {
           '''feature-extraction''': MobileNetVaModel,
           '''image-classification''': MobileNetVaForImageClassification,
           '''image-segmentation''': MobileNetVaForSemanticSegmentation,
       }
       if is_torch_available()
       else {}
   )

   _a   :       List[str]									=    False
   _a   :       int									=    False
   _a   :       Optional[Any]									=    False
   _a   :       List[str]									=    False


   def 					UpperCAmelCase__(						self						)      ->							Optional[int]:
    lowercase__       :       List[Any]				=						MobileNetVaModelTester(self						)
    lowercase__       :       Any				=						MobileNetVaConfigTester(self ,     config_class=lowerCamelCase__ ,     has_text_modality=lowerCamelCase__						)


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


   @unittest.skip(reason="""MobileNetV2 does not use inputs_embeds"""						)
   def 					UpperCAmelCase__(						self						)      ->							str:
    pass


   @unittest.skip(reason="""MobileNetV2 does not support input and output embeddings"""						)
   def 					UpperCAmelCase__(						self						)      ->							List[Any]:
    pass


   @unittest.skip(reason="""MobileNetV2 does not output attentions"""						)
   def 					UpperCAmelCase__(						self						)      ->							List[Any]:
    pass


   def 					UpperCAmelCase__(						self						)      ->							str:
    lowercase__	,       lowercase__       :       Optional[int]				=						self.model_tester.prepare_config_and_inputs_for_common()

    for model_class in self.all_model_classes:
     lowercase__       :       Union[str, Any]				=						model_class(lowerCamelCase__						)
     lowercase__       :       List[Any]				=						inspect.signature(model.forward						)
     # signature.parameters is an OrderedDict => so arg_names order is deterministic
     lowercase__       :       Union[str, Any]				=						[*signature.parameters.keys()]

     lowercase__       :       Union[str, Any]				=						["""pixel_values"""]
     self.assertListEqual(arg_names[:1] ,     lowerCamelCase__						)


   def 					UpperCAmelCase__(						self						)      ->							Union[str, Any]:
    lowercase__       :       Any				=						self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_model(*lowerCamelCase__						)


   def 					UpperCAmelCase__(						self						)      ->							Any:
    def check_hidden_states_output(lowerCamelCase__ ,     lowerCamelCase__ ,     lowerCamelCase__						):
     lowercase__       :       Optional[int]				=						model_class(lowerCamelCase__						)
     model.to(lowerCamelCase__						)
     model.eval()

     with torch.no_grad():
      lowercase__       :       str				=						model(**self._prepare_for_class(lowerCamelCase__ ,     lowerCamelCase__						)						)

     lowercase__       :       int				=						outputs.hidden_states

     lowercase__       :       Optional[int]				=						16
     self.assertEqual(len(lowerCamelCase__						) ,     lowerCamelCase__						)

    lowercase__	,       lowercase__       :       List[str]				=						self.model_tester.prepare_config_and_inputs_for_common()

    for model_class in self.all_model_classes:
     lowercase__       :       Tuple				=						True
     check_hidden_states_output(lowerCamelCase__ ,     lowerCamelCase__ ,     lowerCamelCase__						)

     # check that output_hidden_states also work using config
     del inputs_dict["output_hidden_states"]
     lowercase__       :       Union[str, Any]				=						True

     check_hidden_states_output(lowerCamelCase__ ,     lowerCamelCase__ ,     lowerCamelCase__						)


   def 					UpperCAmelCase__(						self						)      ->							str:
    lowercase__       :       Union[str, Any]				=						self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_image_classification(*lowerCamelCase__						)


   def 					UpperCAmelCase__(						self						)      ->							str:
    lowercase__       :       str				=						self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_semantic_segmentation(*lowerCamelCase__						)







   @slow
   def 					UpperCAmelCase__(						self						)      ->							Optional[int]:
    for model_name in MOBILENET_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
     lowercase__       :       List[str]				=						MobileNetVaModel.from_pretrained(lowerCamelCase__						)
     self.assertIsNotNone(lowerCamelCase__						)


def    _lowerCamelCase   (     ):
 lowercase__       :       List[Any]				=						Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""  )
 return image






@require_torch
@require_vision
class 				_SCREAMING_SNAKE_CASE				(							unittest.TestCase  ):
   """simple docstring"""



   @cached_property
   def 					UpperCAmelCase__(						self						)      ->							Optional[Any]:
    return (
        MobileNetVaImageProcessor.from_pretrained("""google/mobilenet_v2_1.0_224"""						) if is_vision_available() else None
    )


   @slow
   def 					UpperCAmelCase__(						self						)      ->							Optional[int]:
    lowercase__       :       Union[str, Any]				=						MobileNetVaForImageClassification.from_pretrained("""google/mobilenet_v2_1.0_224"""						).to(lowerCamelCase__						)

    lowercase__       :       int				=						self.default_image_processor
    lowercase__       :       str				=						prepare_img()
    lowercase__       :       Optional[int]				=						image_processor(images=lowerCamelCase__ ,     return_tensors="""pt"""						).to(lowerCamelCase__						)

    # forward pass
    with torch.no_grad():
     lowercase__       :       Optional[Any]				=						model(**lowerCamelCase__						)

    # verify the logits
    lowercase__       :       List[Any]				=						torch.Size((1, 1001)						)
    self.assertEqual(outputs.logits.shape ,     lowerCamelCase__						)

    lowercase__       :       List[Any]				=						torch.tensor([0.2445, -1.1993, 0.1905]						).to(lowerCamelCase__						)

    self.assertTrue(torch.allclose(outputs.logits[0, :3] ,     lowerCamelCase__ ,     atol=1E-4						)						)







   @slow
   def 					UpperCAmelCase__(						self						)      ->							List[str]:
    lowercase__       :       Any				=						MobileNetVaForSemanticSegmentation.from_pretrained("""google/deeplabv3_mobilenet_v2_1.0_513"""						)
    lowercase__       :       Optional[int]				=						model.to(lowerCamelCase__						)

    lowercase__       :       int				=						MobileNetVaImageProcessor.from_pretrained("""google/deeplabv3_mobilenet_v2_1.0_513"""						)

    lowercase__       :       Tuple				=						prepare_img()
    lowercase__       :       int				=						image_processor(images=lowerCamelCase__ ,     return_tensors="""pt"""						).to(lowerCamelCase__						)

    # forward pass
    with torch.no_grad():
     lowercase__       :       List[Any]				=						model(**lowerCamelCase__						)
    lowercase__       :       int				=						outputs.logits

    # verify the logits
    lowercase__       :       Any				=						torch.Size((1, 21, 65, 65)						)
    self.assertEqual(logits.shape ,     lowerCamelCase__						)

    lowercase__       :       Union[str, Any]				=						torch.tensor(
        [
            [[17.5790, 17.7581, 18.3355], [18.3257, 18.4230, 18.8973], [18.6169, 18.8650, 19.2187]],
            [[-2.1595, -2.0977, -2.3741], [-2.4226, -2.3028, -2.6835], [-2.7819, -2.5991, -2.7706]],
            [[4.2058, 4.8317, 4.7638], [4.4136, 5.0361, 4.9383], [4.5028, 4.9644, 4.8734]],
        ] ,     device=lowerCamelCase__ ,     )

    self.assertTrue(torch.allclose(logits[0, :3, :3, :3] ,     lowerCamelCase__ ,     atol=1E-4						)						)