


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      :    Tuple		=					"src/transformers"
A      :    List[str]		=					"docs/source/en"
A      :    Any		=					"."

def       lowercase_		(		_A			:    Dict    ,					_A			:    List[str]    ,					_A			:    Optional[int]						):

      """simple docstring"""




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

      lowerCamelCase__		:				Optional[int]    =  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      :    Optional[int]		=					"Model|Encoder|Decoder|ForConditionalGeneration"
# Regexes that match TF/Flax/PT model names.
A      :    List[str]		=					re.compile(r"TF(.*)(?:Model|Encoder|Decoder|ForConditionalGeneration)")
A      :    Optional[int]		=					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      :    Any		=					re.compile(r"(.*)(?:Model|Encoder|Decoder|ForConditionalGeneration)")


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

def       lowercase_		(		_A			:    Optional[Any]						):

      """simple docstring"""




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

def       lowercase_		(		_A			:    List[str]    ,					_A			:    Optional[int]						):

      """simple docstring"""




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

def       lowercase_		(		):

      """simple docstring"""




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

      # Let's lookup through all transformers object (once).
      for attr_name in dir(__lowerCAmelCase						):
            lowerCamelCase__		:				List[str]    =  None
            if attr_name.endswith("Tokenizer"						):
                  lowerCamelCase__		:				Dict    =  slow_tokenizers
                  lowerCamelCase__		:				Optional[Any]    =  attr_name[:-9]
            elif attr_name.endswith("TokenizerFast"						):
                  lowerCamelCase__		:				int    =  fast_tokenizers
                  lowerCamelCase__		:				List[str]    =  attr_name[:-13]
            elif _re_tf_models.match(__lowerCAmelCase						) is not None:
                  lowerCamelCase__		:				Any    =  tf_models
                  lowerCamelCase__		:				List[Any]    =  _re_tf_models.match(__lowerCAmelCase						).groups()[0]
            elif _re_flax_models.match(__lowerCAmelCase						) is not None:
                  lowerCamelCase__		:				int    =  flax_models
                  lowerCamelCase__		:				Optional[int]    =  _re_flax_models.match(__lowerCAmelCase						).groups()[0]
            elif _re_pt_models.match(__lowerCAmelCase						) is not None:
                  lowerCamelCase__		:				Dict    =  pt_models
                  lowerCamelCase__		:				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():
                              lowerCamelCase__		:				Any    =  True
                              break
                        # Try again after removing the last word in the name
                        lowerCamelCase__		:				List[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__		:				Optional[Any]    =  [len(__lowerCAmelCase						) + 2 for c in columns]
      lowerCamelCase__		:				str    =  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__		:				Optional[Any]    =  {True: "✅", False: "❌"}
      for name in model_names:
            lowerCamelCase__		:				Optional[int]    =  model_name_to_prefix[name]
            lowerCamelCase__		:				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       lowercase_		(		_A			:    Tuple=False						):

      """simple docstring"""




      lowerCamelCase__    ,   lowerCamelCase__    ,   lowerCamelCase__    ,   lowerCamelCase__		:				Union[str, Any]    =  _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__		:				str    =  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      :    Dict		=					argparse.ArgumentParser()
    parser.add_argument("--fix_and_overwrite", action="store_true", help="Whether to fix inconsistencies.")
    A      :    str		=					parser.parse_args()

    check_model_table(args.fix_and_overwrite)




import inspect
import unittest

from transformers import ConvNextVaConfig
from transformers.models.auto import get_values
from transformers.models.auto.modeling_auto import MODEL_FOR_BACKBONE_MAPPING_NAMES, MODEL_MAPPING_NAMES
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 ConvNextVaBackbone, ConvNextVaForImageClassification, ConvNextVaModel
			from transformers.models.convnextva.modeling_convnextva import CONVNEXTV2_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
			from PIL import Image

			from transformers import AutoImageProcessor





class 	_lowerCAmelCase					:




				def __init__(					self				,				_UpperCamelCase				,				_UpperCamelCase=13				,				_UpperCamelCase=32				,				_UpperCamelCase=3				,				_UpperCamelCase=4				,				_UpperCamelCase=[10, 20, 30, 40]				,				_UpperCamelCase=[2, 2, 3, 2]				,				_UpperCamelCase=True				,				_UpperCamelCase=True				,				_UpperCamelCase=37				,				_UpperCamelCase="gelu"				,				_UpperCamelCase=10				,				_UpperCamelCase=0.02				,				_UpperCamelCase=["stage2", "stage3", "stage4"]				,				_UpperCamelCase=[2, 3, 4]				,				_UpperCamelCase=None				,				)				->      Union[str, Any]:
							lowerCAmelCase_						=	parent
							lowerCAmelCase_						=	batch_size
							lowerCAmelCase_						=	image_size
							lowerCAmelCase_						=	num_channels
							lowerCAmelCase_						=	num_stages
							lowerCAmelCase_						=	hidden_sizes
							lowerCAmelCase_						=	depths
							lowerCAmelCase_						=	is_training
							lowerCAmelCase_						=	use_labels
							lowerCAmelCase_						=	intermediate_size
							lowerCAmelCase_						=	hidden_act
							lowerCAmelCase_						=	num_labels
							lowerCAmelCase_						=	initializer_range
							lowerCAmelCase_						=	out_features
							lowerCAmelCase_						=	out_indices
							lowerCAmelCase_						=	scope




				def      __a						(					self     )				->      Optional[int]:
							lowerCAmelCase_						=	floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]     )

							lowerCAmelCase_						=	None
							if self.use_labels:
										lowerCAmelCase_						=	ids_tensor([self.batch_size]				,				self.num_labels     )

							lowerCAmelCase_						=	self.get_config()

							return config, pixel_values, labels




				def      __a						(					self     )				->      List[str]:
							return ConvNextVaConfig(
							    num_channels=self.num_channels				,				hidden_sizes=self.hidden_sizes				,				depths=self.depths				,				num_stages=self.num_stages				,				hidden_act=self.hidden_act				,				is_decoder=_UpperCamelCase				,				initializer_range=self.initializer_range				,				out_features=self.out_features				,				out_indices=self.out_indices				,				num_labels=self.num_labels				,				)




				def      __a						(					self				,				_UpperCamelCase				,				_UpperCamelCase				,				_UpperCamelCase     )				->      str:
							lowerCAmelCase_						=	ConvNextVaModel(config=_UpperCamelCase     )
							model.to(_UpperCamelCase     )
							model.eval()
							lowerCAmelCase_						=	model(_UpperCamelCase     )
							# expected last hidden states: B, C, H // 32, W // 32
							self.parent.assertEqual(
							    result.last_hidden_state.shape				,				(self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32)				,				)




				def      __a						(					self				,				_UpperCamelCase				,				_UpperCamelCase				,				_UpperCamelCase     )				->      Optional[int]:
							lowerCAmelCase_						=	ConvNextVaForImageClassification(_UpperCamelCase     )
							model.to(_UpperCamelCase     )
							model.eval()
							lowerCAmelCase_						=	model(_UpperCamelCase				,				labels=_UpperCamelCase     )
							self.parent.assertEqual(result.logits.shape				,				(self.batch_size, self.num_labels)     )




				def      __a						(					self				,				_UpperCamelCase				,				_UpperCamelCase				,				_UpperCamelCase     )				->      Tuple:
							lowerCAmelCase_						=	ConvNextVaBackbone(config=_UpperCamelCase     )
							model.to(_UpperCamelCase     )
							model.eval()
							lowerCAmelCase_						=	model(_UpperCamelCase     )

							# verify hidden states
							self.parent.assertEqual(len(result.feature_maps     )				,				len(config.out_features     )     )
							self.parent.assertListEqual(list(result.feature_maps[0].shape     )				,				[self.batch_size, self.hidden_sizes[1], 4, 4]     )

							# verify channels
							self.parent.assertEqual(len(model.channels     )				,				len(config.out_features     )     )
							self.parent.assertListEqual(model.channels				,				config.hidden_sizes[1:]     )

							# verify backbone works with out_features=None
							lowerCAmelCase_						=	None
							lowerCAmelCase_						=	ConvNextVaBackbone(config=_UpperCamelCase     )
							model.to(_UpperCamelCase     )
							model.eval()
							lowerCAmelCase_						=	model(_UpperCamelCase     )

							# verify feature maps
							self.parent.assertEqual(len(result.feature_maps     )				,				1     )
							self.parent.assertListEqual(list(result.feature_maps[0].shape     )				,				[self.batch_size, self.hidden_sizes[-1], 1, 1]     )

							# verify channels
							self.parent.assertEqual(len(model.channels     )				,				1     )
							self.parent.assertListEqual(model.channels				,				[config.hidden_sizes[-1]]     )




				def      __a						(					self     )				->      Any:
							lowerCAmelCase_						=	self.prepare_config_and_inputs()
							lowerCAmelCase_	,  lowerCAmelCase_	,  lowerCAmelCase_						=	config_and_inputs
							lowerCAmelCase_						=	{"pixel_values": pixel_values}
							return config, inputs_dict




				def      __a						(					self     )				->      str:
							lowerCAmelCase_						=	self.prepare_config_and_inputs()
							lowerCAmelCase_	,  lowerCAmelCase_	,  lowerCAmelCase_						=	config_and_inputs
							lowerCAmelCase_						=	{"pixel_values": pixel_values, "labels": labels}
							return config, inputs_dict





@require_torch
class 	_lowerCAmelCase					(	__a					,      __a					,      unittest.TestCase       ):
				_lowercase					=(
				    (
				        ConvNextVaModel,
				        ConvNextVaForImageClassification,
				        ConvNextVaBackbone,
				    )
				    if is_torch_available()
				    else ()
				)
				_lowercase					=(
				    {'''feature-extraction''': ConvNextVaModel, '''image-classification''': ConvNextVaForImageClassification}
				    if is_torch_available()
				    else {}
				)

				_lowercase					=False
				_lowercase					=False
				_lowercase					=False
				_lowercase					=False
				_lowercase					=False




				def      __a						(					self     )				->      Tuple:
							lowerCAmelCase_						=	ConvNextVaModelTester(self     )
							lowerCAmelCase_						=	ConfigTester(self				,				config_class=_UpperCamelCase				,				has_text_modality=_UpperCamelCase				,				hidden_size=37     )




				def      __a						(					self     )				->      List[Any]:
							self.create_and_test_config_common_properties()
							self.config_tester.create_and_test_config_to_json_string()
							self.config_tester.create_and_test_config_to_json_file()
							self.config_tester.create_and_test_config_from_and_save_pretrained()
							self.config_tester.create_and_test_config_with_num_labels()
							self.config_tester.check_config_can_be_init_without_params()
							self.config_tester.check_config_arguments_init()




				def      __a						(					self     )				->      Any:
							return




				@unittest.skip(reason="ConvNextV2 does not use inputs_embeds"     )
				def      __a						(					self     )				->      List[Any]:
							pass




				@unittest.skip(reason="ConvNextV2 does not support input and output embeddings"     )
				def      __a						(					self     )				->      int:
							pass




				@unittest.skip(reason="ConvNextV2 does not use feedforward chunking"     )
				def      __a						(					self     )				->      str:
							pass




				def      __a						(					self     )				->      Union[str, Any]:
							if not self.model_tester.is_training:
										return

							for model_class in self.all_model_classes:
										lowerCAmelCase_	,  lowerCAmelCase_						=	self.model_tester.prepare_config_and_inputs_with_labels()
										lowerCAmelCase_						=	True

										if model_class.__name__ in [
										    *get_values(_UpperCamelCase     ),
										    *get_values(_UpperCamelCase     ),
										]:
													continue

										lowerCAmelCase_						=	model_class(_UpperCamelCase     )
										model.to(_UpperCamelCase     )
										model.train()
										lowerCAmelCase_						=	self._prepare_for_class(_UpperCamelCase				,				_UpperCamelCase				,				return_labels=_UpperCamelCase     )
										lowerCAmelCase_						=	model(**_UpperCamelCase     ).loss
										loss.backward()




				def      __a						(					self     )				->      Optional[int]:
							if not self.model_tester.is_training:
										return

							for model_class in self.all_model_classes:
										lowerCAmelCase_	,  lowerCAmelCase_						=	self.model_tester.prepare_config_and_inputs_with_labels()
										lowerCAmelCase_						=	False
										lowerCAmelCase_						=	True

										if (
										    model_class.__name__
										    in [*get_values(_UpperCamelCase     ), *get_values(_UpperCamelCase     )]
										    or not model_class.supports_gradient_checkpointing
										):
													continue

										lowerCAmelCase_						=	model_class(_UpperCamelCase     )
										model.to(_UpperCamelCase     )
										model.gradient_checkpointing_enable()
										model.train()
										lowerCAmelCase_						=	self._prepare_for_class(_UpperCamelCase				,				_UpperCamelCase				,				return_labels=_UpperCamelCase     )
										lowerCAmelCase_						=	model(**_UpperCamelCase     ).loss
										loss.backward()




				def      __a						(					self     )				->      Any:
							lowerCAmelCase_	,  lowerCAmelCase_						=	self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
										lowerCAmelCase_						=	model_class(_UpperCamelCase     )
										lowerCAmelCase_						=	inspect.signature(model.forward     )
										# signature.parameters is an OrderedDict => so arg_names order is deterministic
										lowerCAmelCase_						=	[*signature.parameters.keys()]

										lowerCAmelCase_						=	["pixel_values"]
										self.assertListEqual(arg_names[:1]				,				_UpperCamelCase     )




				def      __a						(					self     )				->      Tuple:
							lowerCAmelCase_						=	self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*_UpperCamelCase     )




				def      __a						(					self     )				->      List[Any]:
							def check_hidden_states_output(_UpperCamelCase				,				_UpperCamelCase				,				_UpperCamelCase     ):
										lowerCAmelCase_						=	model_class(_UpperCamelCase     )
										model.to(_UpperCamelCase     )
										model.eval()

										with torch.no_grad():
													lowerCAmelCase_						=	model(**self._prepare_for_class(_UpperCamelCase				,				_UpperCamelCase     )     )

										lowerCAmelCase_						=	outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

										lowerCAmelCase_						=	self.model_tester.num_stages
										self.assertEqual(len(_UpperCamelCase     )				,				expected_num_stages + 1     )

										# ConvNextV2's feature maps are of shape (batch_size, num_channels, height, width)
										self.assertListEqual(
										    list(hidden_states[0].shape[-2:]     )				,				[self.model_tester.image_size // 4, self.model_tester.image_size // 4]				,				)

							lowerCAmelCase_	,  lowerCAmelCase_						=	self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
										lowerCAmelCase_						=	True
										check_hidden_states_output(_UpperCamelCase				,				_UpperCamelCase				,				_UpperCamelCase     )

										# check that output_hidden_states also work using config
										del inputs_dict["output_hidden_states"]
										lowerCAmelCase_						=	True

										check_hidden_states_output(_UpperCamelCase				,				_UpperCamelCase				,				_UpperCamelCase     )




				def      __a						(					self     )				->      Optional[int]:
							lowerCAmelCase_						=	self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_image_classification(*_UpperCamelCase     )




				@slow
				def      __a						(					self     )				->      Optional[Any]:
							for model_name in CONVNEXTV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
										lowerCAmelCase_						=	ConvNextVaModel.from_pretrained(_UpperCamelCase     )
										self.assertIsNotNone(_UpperCamelCase     )






def    lowerCamelCase__				(				):




			"""simple docstring"""


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





@require_torch
@require_vision
class 	_lowerCAmelCase					(	unittest.TestCase       ):




				@cached_property
				def      __a						(					self     )				->      int:
							return AutoImageProcessor.from_pretrained("facebook/convnextv2-tiny-1k-224"     ) if is_vision_available() else None




				@slow
				def      __a						(					self     )				->      List[str]:
							lowerCAmelCase_						=	ConvNextVaForImageClassification.from_pretrained("facebook/convnextv2-tiny-1k-224"     ).to(_UpperCamelCase     )

							lowerCAmelCase_						=	self.default_image_processor
							lowerCAmelCase_						=	prepare_img()
							lowerCAmelCase_						=	preprocessor(images=_UpperCamelCase				,				return_tensors="pt"     ).to(_UpperCamelCase     )

							# forward pass
							with torch.no_grad():
										lowerCAmelCase_						=	model(**_UpperCamelCase     )

							# verify the logits
							lowerCAmelCase_						=	torch.Size((1, 1_000)     )
							self.assertEqual(outputs.logits.shape				,				_UpperCamelCase     )

							lowerCAmelCase_						=	torch.tensor([0.9996, 0.1966, -0.4386]     ).to(_UpperCamelCase     )
							self.assertTrue(torch.allclose(outputs.logits[0, :3]				,				_UpperCamelCase				,				atol=1e-4     )     )



