





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
__A     =    "src/transformers"
__A     =    "docs/source/en"
__A     =    "."





def 					lowerCAmelCase_			(	__a     ,   __a     ,   __a		)					->		List[str]:







  """simple docstring"""





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

  lowerCamelCase__:				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 |
__A     =    "Model|Encoder|Decoder|ForConditionalGeneration"
# Regexes that match TF/Flax/PT model names.
__A     =    re.compile(R"TF(.*)(?:Model|Encoder|Decoder|ForConditionalGeneration)")
__A     =    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.
__A     =    re.compile(R"(.*)(?:Model|Encoder|Decoder|ForConditionalGeneration)")


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





def 					lowerCAmelCase_			(	__a		)					->		Any:







  """simple docstring"""





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





def 					lowerCAmelCase_			(	__a     ,   __a		)					->		Optional[int]:







  """simple docstring"""





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





def 					lowerCAmelCase_			(	)					->		Tuple:







  """simple docstring"""





  lowerCamelCase__:				int        =transformers_module.models.auto.configuration_auto.CONFIG_MAPPING_NAMES
  lowerCamelCase__:				List[str]        ={
      name: config_maping_names[code]
      for code, name in transformers_module.MODEL_NAMES_MAPPING.items()
      if code in config_maping_names
  }
  lowerCamelCase__:				Dict        ={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.
  lowerCamelCase__:				Tuple        =collections.defaultdict(__lowerCamelCase		)
  lowerCamelCase__:				int        =collections.defaultdict(__lowerCamelCase		)
  lowerCamelCase__:				Optional[int]        =collections.defaultdict(__lowerCamelCase		)
  lowerCamelCase__:				Tuple        =collections.defaultdict(__lowerCamelCase		)
  lowerCamelCase__:				List[Any]        =collections.defaultdict(__lowerCamelCase		)

  # Let's lookup through all transformers object (once).
  for attr_name in dir(__lowerCamelCase		):
    lowerCamelCase__:				Dict        =None
    if attr_name.endswith("Tokenizer"		):
      lowerCamelCase__:				Optional[int]        =slow_tokenizers
      lowerCamelCase__:				int        =attr_name[:-9]
    elif attr_name.endswith("TokenizerFast"		):
      lowerCamelCase__:				List[str]        =fast_tokenizers
      lowerCamelCase__:				Any        =attr_name[:-13]
    elif _re_tf_models.match(__lowerCamelCase		) is not None:
      lowerCamelCase__:				List[str]        =tf_models
      lowerCamelCase__:				Optional[Any]        =_re_tf_models.match(__lowerCamelCase		).groups()[0]
    elif _re_flax_models.match(__lowerCamelCase		) is not None:
      lowerCamelCase__:				List[str]        =flax_models
      lowerCamelCase__:				str        =_re_flax_models.match(__lowerCamelCase		).groups()[0]
    elif _re_pt_models.match(__lowerCamelCase		) is not None:
      lowerCamelCase__:				Any        =pt_models
      lowerCamelCase__:				Any        =_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():
          lowerCamelCase__:				Dict        =True
          break
        # Try again after removing the last word in the name
        lowerCamelCase__:				Any        ="".join(camel_case_split(__lowerCamelCase		)[:-1]		)

    # Let's build that table!
  lowerCamelCase__:				Dict        =list(model_name_to_config.keys()		)
  model_names.sort(key=str.lower		)
  lowerCamelCase__:				Tuple        =["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).
  lowerCamelCase__:				str        =[len(__lowerCamelCase		) + 2 for c in columns]
  lowerCamelCase__:				Optional[int]        =max([len(__lowerCamelCase		) for name in model_names]		) + 2

  # Build the table per se
  lowerCamelCase__:				int        ="|" + "|".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"

  lowerCamelCase__:				str        ={True: "✅", False: "❌"}
  for name in model_names:
    lowerCamelCase__:				Dict        =model_name_to_prefix[name]
    lowerCamelCase__:				Union[str, Any]        =[
        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_			(	__a=False		)					->		Any:







  """simple docstring"""





  lowerCamelCase__  , lowerCamelCase__  , lowerCamelCase__  , lowerCamelCase__:				int        =_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-->"     ,   )
  lowerCamelCase__:				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__":
     __A     =    argparse.ArgumentParser()
     parser.add_argument("--fix_and_overwrite", action="store_true", help="Whether to fix inconsistencies.")
     __A     =    parser.parse_args()

     check_model_table(args.fix_and_overwrite)








from __future__ import annotations

import inspect
import unittest

from transformers import ViTConfig
from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import cached_property, is_tf_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
     import tensorflow as tf

     from transformers import TFViTForImageClassification, TFViTModel


if is_vision_available():
     from PIL import Image

     from transformers import ViTImageProcessor



class        _SCREAMING_SNAKE_CASE						:







 '''simple docstring'''

 def __init__(self    :       Optional[Any]	,			UpperCAmelCase_    :       Optional[int]	,			UpperCAmelCase_    :       List[Any]=13	,			UpperCAmelCase_    :       Dict=30	,			UpperCAmelCase_    :       str=2	,			UpperCAmelCase_    :       Union[str, Any]=3	,			UpperCAmelCase_    :       Union[str, Any]=True	,			UpperCAmelCase_    :       List[str]=True	,			UpperCAmelCase_    :       Dict=32	,			UpperCAmelCase_    :       List[Any]=2	,			UpperCAmelCase_    :       List[str]=4	,			UpperCAmelCase_    :       List[str]=37	,			UpperCAmelCase_    :       List[str]="gelu"	,			UpperCAmelCase_    :       List[Any]=0.1	,			UpperCAmelCase_    :       Optional[Any]=0.1	,			UpperCAmelCase_    :       str=10	,			UpperCAmelCase_    :       str=0.02	,			UpperCAmelCase_    :       Optional[int]=3	,			UpperCAmelCase_    :       Dict=None	,			)	->Union[str, Any]:



   '''simple docstring'''



   lowerCamelCase__:				int        =parent
   lowerCamelCase__:				Any        =batch_size
   lowerCamelCase__:				List[str]        =image_size
   lowerCamelCase__:				Tuple        =patch_size
   lowerCamelCase__:				Tuple        =num_channels
   lowerCamelCase__:				Tuple        =is_training
   lowerCamelCase__:				Optional[int]        =use_labels
   lowerCamelCase__:				Dict        =hidden_size
   lowerCamelCase__:				Dict        =num_hidden_layers
   lowerCamelCase__:				Optional[Any]        =num_attention_heads
   lowerCamelCase__:				Union[str, Any]        =intermediate_size
   lowerCamelCase__:				List[str]        =hidden_act
   lowerCamelCase__:				Optional[Any]        =hidden_dropout_prob
   lowerCamelCase__:				str        =attention_probs_dropout_prob
   lowerCamelCase__:				str        =type_sequence_label_size
   lowerCamelCase__:				Optional[Any]        =initializer_range
   lowerCamelCase__:				Optional[int]        =scope

   # in ViT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
   lowerCamelCase__:				List[str]        =(image_size // patch_size) ** 2
   lowerCamelCase__:				Optional[Any]        =num_patches + 1

 def 							SCREAMING_SNAKE_CASE_ (self    :       Optional[Any])	->List[str]:



   '''simple docstring'''



   lowerCamelCase__:				Tuple        =floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

   lowerCamelCase__:				List[str]        =None
   if self.use_labels:
     lowerCamelCase__:				Any        =ids_tensor([self.batch_size]	,			self.type_sequence_label_size)

   lowerCamelCase__:				List[str]        =self.get_config()

   return config, pixel_values, labels

 def 							SCREAMING_SNAKE_CASE_ (self    :       Optional[int])	->List[str]:



   '''simple docstring'''



   return ViTConfig(
       image_size=self.image_size	,			patch_size=self.patch_size	,			num_channels=self.num_channels	,			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	,			is_decoder=UpperCAmelCase_	,			initializer_range=self.initializer_range	,			)

 def 							SCREAMING_SNAKE_CASE_ (self    :       int	,			UpperCAmelCase_    :       int	,			UpperCAmelCase_    :       Optional[Any]	,			UpperCAmelCase_    :       Tuple)	->Tuple:



   '''simple docstring'''



   lowerCamelCase__:				List[str]        =TFViTModel(config=UpperCAmelCase_)
   lowerCamelCase__:				Tuple        =model(UpperCAmelCase_	,			training=UpperCAmelCase_)
   self.parent.assertEqual(result.last_hidden_state.shape	,			(self.batch_size, self.seq_length, self.hidden_size))

   # Test with an image with different size than the one specified in config.
   lowerCamelCase__:				Dict        =self.image_size // 2
   lowerCamelCase__:				List[str]        =pixel_values[:, :, :image_size, :image_size]
   lowerCamelCase__:				List[str]        =model(UpperCAmelCase_	,			interpolate_pos_encoding=UpperCAmelCase_	,			training=UpperCAmelCase_)
   lowerCamelCase__:				List[str]        =(image_size // self.patch_size) ** 2 + 1
   self.parent.assertEqual(result.last_hidden_state.shape	,			(self.batch_size, seq_length, self.hidden_size))

 def 							SCREAMING_SNAKE_CASE_ (self    :       Optional[Any]	,			UpperCAmelCase_    :       Tuple	,			UpperCAmelCase_    :       int	,			UpperCAmelCase_    :       Any)	->str:



   '''simple docstring'''



   lowerCamelCase__:				Any        =self.type_sequence_label_size
   lowerCamelCase__:				str        =TFViTForImageClassification(UpperCAmelCase_)
   lowerCamelCase__:				List[str]        =model(UpperCAmelCase_	,			labels=UpperCAmelCase_	,			training=UpperCAmelCase_)
   self.parent.assertEqual(result.logits.shape	,			(self.batch_size, self.type_sequence_label_size))

   # Test with an image with different size than the one specified in config.
   lowerCamelCase__:				Union[str, Any]        =self.image_size // 2
   lowerCamelCase__:				Dict        =pixel_values[:, :, :image_size, :image_size]
   lowerCamelCase__:				Tuple        =model(UpperCAmelCase_	,			interpolate_pos_encoding=UpperCAmelCase_	,			training=UpperCAmelCase_)
   self.parent.assertEqual(result.logits.shape	,			(self.batch_size, self.type_sequence_label_size))

   # test greyscale images
   lowerCamelCase__:				int        =1
   lowerCamelCase__:				List[str]        =TFViTForImageClassification(UpperCAmelCase_)
   lowerCamelCase__:				List[str]        =floats_tensor([self.batch_size, 1, self.image_size, self.image_size])
   lowerCamelCase__:				Any        =model(UpperCAmelCase_)
   self.parent.assertEqual(result.logits.shape	,			(self.batch_size, self.type_sequence_label_size))







 def 							SCREAMING_SNAKE_CASE_ (self    :       str)	->Union[str, Any]:



   '''simple docstring'''



   lowerCamelCase__:				Optional[Any]        =self.prepare_config_and_inputs()
   lowerCamelCase__  , lowerCamelCase__  , lowerCamelCase__:				Union[str, Any]        =config_and_inputs
   lowerCamelCase__:				Optional[Any]        ={"pixel_values": pixel_values}
   return config, inputs_dict



@require_tf
class        _SCREAMING_SNAKE_CASE						(     __SCREAMING_SNAKE_CASE   ,							__SCREAMING_SNAKE_CASE   ,							unittest.TestCase   ):







 '''simple docstring'''
 lowercase_		=      (TFViTModel, TFViTForImageClassification) if is_tf_available() else ()
 lowercase_		=      (
     {"feature-extraction": TFViTModel, "image-classification": TFViTForImageClassification}
     if is_tf_available()
     else {}
 )

 lowercase_		=      False
 lowercase_		=      False
 lowercase_		=      False

 def 							SCREAMING_SNAKE_CASE_ (self    :       List[str])	->Dict:



   '''simple docstring'''



   lowerCamelCase__:				int        =TFViTModelTester(self)
   lowerCamelCase__:				List[str]        =ConfigTester(self	,			config_class=UpperCAmelCase_	,			has_text_modality=UpperCAmelCase_	,			hidden_size=37)

 def 							SCREAMING_SNAKE_CASE_ (self    :       str)	->Tuple:



   '''simple docstring'''



   self.config_tester.run_common_tests()

 @unittest.skip(reason="ViT does not use inputs_embeds")
 def 							SCREAMING_SNAKE_CASE_ (self    :       List[str])	->Any:



   '''simple docstring'''



   pass

 @unittest.skip(reason="ViT does not use inputs_embeds")
 def 							SCREAMING_SNAKE_CASE_ (self    :       int)	->str:



   '''simple docstring'''



   pass

 def 							SCREAMING_SNAKE_CASE_ (self    :       Tuple)	->Optional[int]:



   '''simple docstring'''



   lowerCamelCase__  , lowerCamelCase__:				int        =self.model_tester.prepare_config_and_inputs_for_common()

   for model_class in self.all_model_classes:
     lowerCamelCase__:				int        =model_class(UpperCAmelCase_)
     self.assertIsInstance(model.get_input_embeddings()	,			(tf.keras.layers.Layer))
     lowerCamelCase__:				Tuple        =model.get_output_embeddings()
     self.assertTrue(x is None or isinstance(UpperCAmelCase_	,			tf.keras.layers.Layer))

 def 							SCREAMING_SNAKE_CASE_ (self    :       Union[str, Any])	->Dict:



   '''simple docstring'''



   lowerCamelCase__  , lowerCamelCase__:				Optional[Any]        =self.model_tester.prepare_config_and_inputs_for_common()

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

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

 def 							SCREAMING_SNAKE_CASE_ (self    :       int)	->Union[str, Any]:



   '''simple docstring'''



   lowerCamelCase__:				Optional[int]        =self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_model(*UpperCAmelCase_)

 def 							SCREAMING_SNAKE_CASE_ (self    :       Optional[Any])	->Any:



   '''simple docstring'''



   lowerCamelCase__:				List[str]        =self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_for_image_classification(*UpperCAmelCase_)







 @slow
 def 							SCREAMING_SNAKE_CASE_ (self    :       Dict)	->List[Any]:



   '''simple docstring'''



   lowerCamelCase__:				Any        =TFViTModel.from_pretrained("google/vit-base-patch16-224")
   self.assertIsNotNone(UpperCAmelCase_)





def 					lowerCAmelCase_			(	)					->		Tuple:







  """simple docstring"""





  lowerCamelCase__:				Any        =Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"		)
  return image






@require_tf
@require_vision
class        _SCREAMING_SNAKE_CASE						(     unittest.TestCase   ):







 '''simple docstring'''

 @cached_property
 def 							SCREAMING_SNAKE_CASE_ (self    :       str)	->List[Any]:



   '''simple docstring'''



   return ViTImageProcessor.from_pretrained("google/vit-base-patch16-224") if is_vision_available() else None







 @slow
 def 							SCREAMING_SNAKE_CASE_ (self    :       Optional[int])	->Tuple:



   '''simple docstring'''



   lowerCamelCase__:				List[str]        =TFViTForImageClassification.from_pretrained("google/vit-base-patch16-224")

   lowerCamelCase__:				List[str]        =self.default_image_processor
   lowerCamelCase__:				Union[str, Any]        =prepare_img()
   lowerCamelCase__:				List[Any]        =image_processor(images=UpperCAmelCase_	,			return_tensors="tf")

   # forward pass
   lowerCamelCase__:				Dict        =model(**UpperCAmelCase_)

   # verify the logits
   lowerCamelCase__:				List[Any]        =tf.TensorShape((1, 1_000))
   self.assertEqual(outputs.logits.shape	,			UpperCAmelCase_)

   lowerCamelCase__:				List[Any]        =tf.constant([-0.2744, 0.8215, -0.0836])

   tf.debugging.assert_near(outputs.logits[0, :3]	,			UpperCAmelCase_	,			atol=1E-4)

