



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

def  __SCREAMING_SNAKE_CASE		(			lowerCamelCase_:							str     ,		lowerCamelCase_:							Any     ,		lowerCamelCase_:							int ):
						"""simple docstring"""






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

						snake_case   :			Tuple						    = 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  __SCREAMING_SNAKE_CASE		(			lowerCamelCase_:							str ):
						"""simple docstring"""






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

def  __SCREAMING_SNAKE_CASE		(			lowerCamelCase_:							Tuple     ,		lowerCamelCase_:							Optional[int] ):
						"""simple docstring"""






						snake_case   :			Optional[Any]						    = 2 if text == "✅" or text == "❌" else len(lowerCamelCase_ )
						snake_case   :			Tuple						    = (width - text_length) // 2
						snake_case   :			int						    = width - text_length - left_indent
						return " " * left_indent + text + " " * right_indent

def  __SCREAMING_SNAKE_CASE		(			):
						"""simple docstring"""






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

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

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

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

						snake_case   :			Dict						    = {True: "✅", False: "❌"}
						for name in model_names:
												snake_case   :			int						    = model_name_to_prefix[name]
												snake_case   :			Tuple						    = [
												    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  __SCREAMING_SNAKE_CASE		(			lowerCamelCase_:							str=False ):
						"""simple docstring"""






						snake_case    ,			snake_case    ,			snake_case    ,			snake_case   :			Optional[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-->"     ,		)
						snake_case   :			Optional[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)








"""simple docstring"""







from collections import OrderedDict
from typing import TYPE_CHECKING, Any, List, Mapping, Optional

from packaging import version


if TYPE_CHECKING:
			from ... import PreTrainedTokenizer, TensorType

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfigWithPast, PatchingSpec
from ...utils import is_torch_available, logging


A		=						logging.get_logger(__name__)

A		=						{
    'bigscience/bloom': 'https://huggingface.co/bigscience/bloom/resolve/main/config.json',
    'bigscience/bloom-560m': 'https://huggingface.co/bigscience/bloom-560m/blob/main/config.json',
    'bigscience/bloom-1b1': 'https://huggingface.co/bigscience/bloom-1b1/blob/main/config.json',
    'bigscience/bloom-1b7': 'https://huggingface.co/bigscience/bloom-1b7/blob/main/config.json',
    'bigscience/bloom-3b': 'https://huggingface.co/bigscience/bloom-3b/blob/main/config.json',
    'bigscience/bloom-7b1': 'https://huggingface.co/bigscience/bloom-7b1/blob/main/config.json',
}

class       _a  ( SCREAMING_SNAKE_CASE__):
			__magic_name__     					=     """bloom"""
			__magic_name__     					=     ["""past_key_values"""]
			__magic_name__     					=     {
			    """num_hidden_layers""": """n_layer""",
			    """num_attention_heads""": """n_head""",
			}



			def __init__(			self							:				Tuple   ,	_lowercase							:				List[Any]=250880   ,	_lowercase							:				int=64   ,	_lowercase							:				Optional[Any]=2   ,	_lowercase							:				Dict=8   ,	_lowercase							:				List[str]=1E-5   ,	_lowercase							:				List[Any]=0.02   ,	_lowercase							:				Optional[Any]=True   ,	_lowercase							:				Dict=1   ,	_lowercase							:				Union[str, Any]=2   ,	_lowercase							:				str=False   ,	_lowercase							:				List[Any]=0.0   ,	_lowercase							:				Tuple=0.0   ,	_lowercase							:				Dict=1   ,	_lowercase							:				int=False   ,	**_lowercase							:				int   ,	)							->					List[str]:
									snake_case   :			Any						    = vocab_size
									# Backward compatibility with n_embed kwarg
									snake_case   :			Any						    = kwargs.pop("n_embed"   ,	_lowercase		)
									snake_case   :			Tuple						    = hidden_size if n_embed is None else n_embed
									snake_case   :			Optional[Any]						    = n_layer
									snake_case   :			Optional[Any]						    = n_head
									snake_case   :			Union[str, Any]						    = layer_norm_epsilon
									snake_case   :			int						    = initializer_range
									snake_case   :			int						    = use_cache
									snake_case   :			int						    = pretraining_tp
									snake_case   :			Tuple						    = apply_residual_connection_post_layernorm
									snake_case   :			Union[str, Any]						    = hidden_dropout
									snake_case   :			Optional[Any]						    = attention_dropout

									snake_case   :			List[Any]						    = bos_token_id
									snake_case   :			Any						    = eos_token_id
									snake_case   :			Optional[Any]						    = slow_but_exact

									super().__init__(bos_token_id=_lowercase   ,	eos_token_id=_lowercase   ,	**_lowercase		)



class       _a  ( SCREAMING_SNAKE_CASE__):
			__magic_name__     					=     version.parse("""1.12""")



			def __init__(			self							:				int   ,	_lowercase							:				PretrainedConfig   ,	_lowercase							:				str = "default"   ,	_lowercase							:				List[PatchingSpec] = None   ,	_lowercase							:				bool = False   ,	)							->					Dict:
									super().__init__(_lowercase   ,	task=_lowercase   ,	patching_specs=_lowercase   ,	use_past=_lowercase		)
									if not getattr(self._config   ,	"pad_token_id"   ,	_lowercase		):
															# TODO: how to do that better?
															snake_case   :			int						    = 0



			@property
			def 				__lowercase					(			self							:				str		)							->					Mapping[str, Mapping[int, str]]:
									snake_case   :			int						    = OrderedDict({"input_ids": {0: "batch", 1: "sequence"}}		)
									if self.use_past:
															# BLOOM stores values on dynamic axis 2. For more details see: https://github.com/huggingface/transformers/pull/18344
															self.fill_with_past_key_values_(_lowercase   ,	direction="inputs"   ,	inverted_values_shape=_lowercase		)
															snake_case   :			Optional[int]						    = {0: "batch", 1: "past_sequence + sequence"}
									else:
															snake_case   :			Dict						    = {0: "batch", 1: "sequence"}

									return common_inputs



			@property
			def 				__lowercase					(			self							:				int		)							->					int:
									return self._config.n_layer



			@property
			def 				__lowercase					(			self							:				Dict		)							->					int:
									return self._config.n_head



			@property
			def 				__lowercase					(			self							:				Union[str, Any]		)							->					float:
									return 1E-3



			def 				__lowercase					(			self							:				List[str]   ,	_lowercase							:				"PreTrainedTokenizer"   ,	_lowercase							:				int = -1   ,	_lowercase							:				int = -1   ,	_lowercase							:				bool = False   ,	_lowercase							:				Optional["TensorType"] = None   ,	)							->					Mapping[str, Any]:
									snake_case   :			int						    = super(_lowercase   ,	self		).generate_dummy_inputs(
									    _lowercase   ,	batch_size=_lowercase   ,	seq_length=_lowercase   ,	is_pair=_lowercase   ,	framework=_lowercase		)

									# We need to order the input in the way they appears in the forward()
									snake_case   :			Any						    = OrderedDict({"input_ids": common_inputs["input_ids"]}		)

									# Need to add the past_keys
									if self.use_past:
															if not is_torch_available():
																					raise ValueError("Cannot generate dummy past_keys inputs without PyTorch installed."		)
															else:
																					import torch

																					snake_case    ,			snake_case   :			List[str]						    = common_inputs["input_ids"].shape
																					# Not using the same length for past_key_values
																					snake_case   :			Optional[Any]						    = seqlen + 2
																					snake_case   :			Optional[int]						    = self._config.hidden_size // self.num_attention_heads
																					snake_case   :			List[str]						    = (
																					    batch * self.num_attention_heads,
																					    head_dim,
																					    past_key_values_length,
																					)
																					snake_case   :			Any						    = (
																					    batch * self.num_attention_heads,
																					    past_key_values_length,
																					    head_dim,
																					)
																					snake_case   :			Dict						    = [
																					    (torch.zeros(_lowercase		), torch.zeros(_lowercase		)) for _ in range(self.num_layers		)
																					]

									snake_case   :			Tuple						    = common_inputs["attention_mask"]
									if self.use_past:
															snake_case   :			Any						    = ordered_inputs["attention_mask"].dtype
															snake_case   :			List[str]						    = torch.cat(
															    [ordered_inputs["attention_mask"], torch.ones(_lowercase   ,	_lowercase   ,	dtype=_lowercase		)]   ,	dim=1		)

									return ordered_inputs



			@property
			def 				__lowercase					(			self							:				str		)							->					int:
									return 13



