



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
UpperCAmelCase_							:      Dict      =  "src/transformers"
UpperCAmelCase_							:      Dict      =  "docs/source/en"
UpperCAmelCase_							:      int      =  "."
def  UpperCamelCase						(       _A			:				Any			,							_A			:				Union[str, Any]			,							_A			:				Optional[Any]    ):





						"""simple docstring"""





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

						A__							=       start_index
						while not lines[end_index].startswith(_A    ):
												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 |
UpperCAmelCase_							:      str      =  "Model|Encoder|Decoder|ForConditionalGeneration"
# Regexes that match TF/Flax/PT model names.
UpperCAmelCase_							:      Union[str, Any]      =  re.compile(R"TF(.*)(?:Model|Encoder|Decoder|ForConditionalGeneration)")
UpperCAmelCase_							:      str      =  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.
UpperCAmelCase_							:      Dict      =  re.compile(R"(.*)(?:Model|Encoder|Decoder|ForConditionalGeneration)")


# This is to make sure the transformers module imported is the one in the repo.
UpperCAmelCase_							:      Dict      =  direct_transformers_import(TRANSFORMERS_PATH)
def  UpperCamelCase						(       _A			:				str    ):





						"""simple docstring"""





						A__							=       re.finditer(".+?(?:(?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])|$)"			,							_A    )
						return [m.group(0    ) for m in matches]
def  UpperCamelCase						(       _A			:				Any			,							_A			:				Any    ):





						"""simple docstring"""





						A__							=       2 if text == "✅" or text == "❌" else len(_A    )
						A__							=       (width - text_length) // 2
						A__							=       width - text_length - left_indent
						return " " * left_indent + text + " " * right_indent
def  UpperCamelCase						(       ):





						"""simple docstring"""





						A__							=       transformers_module.models.auto.configuration_auto.CONFIG_MAPPING_NAMES
						A__							=       {
						    name: config_maping_names[code]
						    for code, name in transformers_module.MODEL_NAMES_MAPPING.items()
						    if code in config_maping_names
						}
						A__							=       {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.
						A__							=       collections.defaultdict(_A    )
						A__							=       collections.defaultdict(_A    )
						A__							=       collections.defaultdict(_A    )
						A__							=       collections.defaultdict(_A    )
						A__							=       collections.defaultdict(_A    )

						# Let's lookup through all transformers object (once).
						for attr_name in dir(_A    ):
												A__							=       None
												if attr_name.endswith("Tokenizer"    ):
																		A__							=       slow_tokenizers
																		A__							=       attr_name[:-9]
												elif attr_name.endswith("TokenizerFast"    ):
																		A__							=       fast_tokenizers
																		A__							=       attr_name[:-13]
												elif _re_tf_models.match(_A    ) is not None:
																		A__							=       tf_models
																		A__							=       _re_tf_models.match(_A    ).groups()[0]
												elif _re_flax_models.match(_A    ) is not None:
																		A__							=       flax_models
																		A__							=       _re_flax_models.match(_A    ).groups()[0]
												elif _re_pt_models.match(_A    ) is not None:
																		A__							=       pt_models
																		A__							=       _re_pt_models.match(_A    ).groups()[0]

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

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

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

						A__							=       {True: "✅", False: "❌"}
						for name in model_names:
												A__							=       model_name_to_prefix[name]
												A__							=       [
												    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(_A			,							_A    ) for l, w in zip(_A			,							_A    )]    ) + "|\n"
						return table
def  UpperCamelCase						(       _A			:				Any=False    ):





						"""simple docstring"""





						A__						,       A__						,       A__						,       A__							=       _find_text_in_file(
						    filename=os.path.join(_A			,							"index.md"    )			,							start_prompt="<!--This table is updated automatically from the auto modules"			,							end_prompt="<!-- End table-->"			,							)
						A__							=       get_model_table_from_auto_modules()

						if current_table != new_table:
												if overwrite:
																		with open(os.path.join(_A			,							"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__":
						UpperCAmelCase_							:      Optional[int]      =  argparse.ArgumentParser()
						parser.add_argument("--fix_and_overwrite", action="store_true", help="Whether to fix inconsistencies.")
						UpperCAmelCase_							:      List[str]      =  parser.parse_args()

						check_model_table(args.fix_and_overwrite)










import math
from enum import Enum
from typing import Optional, Union

from torch.optim import Optimizer
from torch.optim.lr_scheduler import LambdaLR

from .utils import logging


UpperCAmelCase_							:      Optional[Any]      =  logging.get_logger(__name__)






class      UpperCamelCase    (						_UpperCAmelCase     ):
	lowerCAmelCase	:   List[str]				=	"""linear"""
	lowerCAmelCase	:   int				=	"""cosine"""
	lowerCAmelCase	:   Dict				=	"""cosine_with_restarts"""
	lowerCAmelCase	:   Optional[Any]				=	"""polynomial"""
	lowerCAmelCase	:   Dict				=	"""constant"""
	lowerCAmelCase	:   Any				=	"""constant_with_warmup"""
	lowerCAmelCase	:   Union[str, Any]				=	"""piecewise_constant"""





def  UpperCamelCase						(       _A			:				Optimizer			,							_A			:				int = -1    )->       Dict:





						"""simple docstring"""





						return LambdaLR(_A			,							lambda _A    : 1			,							last_epoch=_A    )
def  UpperCamelCase						(       _A			:				Optimizer			,							_A			:				int			,							_A			:				int = -1    )->       Optional[Any]:





						"""simple docstring"""





						def lr_lambda(_A			:				int    ):
												if current_step < num_warmup_steps:
																		return float(_A    ) / float(max(1.0			,							_A    )    )
												return 1.0

						return LambdaLR(_A			,							_A			,							last_epoch=_A    )
def  UpperCamelCase						(       _A			:				Optimizer			,							_A			:				str			,							_A			:				int = -1    )->       Dict:





						"""simple docstring"""





						A__							=       {}
						A__							=       step_rules.split(","    )
						for rule_str in rule_list[:-1]:
												A__						,       A__							=       rule_str.split(":"    )
												A__							=       int(_A    )
												A__							=       float(_A    )
												A__							=       value
						A__							=       float(rule_list[-1]    )

						def create_rules_function(_A			:				Any			,							_A			:				Optional[int]    ):
												def rule_func(_A			:				int    ) -> float:
																		A__							=       sorted(rules_dict.keys()    )
																		for i, sorted_step in enumerate(_A    ):
																								if steps < sorted_step:
																														return rules_dict[sorted_steps[i]]
																		return last_lr_multiple

												return rule_func

						A__							=       create_rules_function(_A			,							_A    )

						return LambdaLR(_A			,							_A			,							last_epoch=_A    )
def  UpperCamelCase						(       _A			:				Any			,							_A			:				Union[str, Any]			,							_A			:				str			,							_A			:				str=-1    )->       Tuple:





						"""simple docstring"""





						def lr_lambda(_A			:				int    ):
												if current_step < num_warmup_steps:
																		return float(_A    ) / float(max(1			,							_A    )    )
												return max(
												    0.0			,							float(num_training_steps - current_step    ) / float(max(1			,							num_training_steps - num_warmup_steps    )    )    )

						return LambdaLR(_A			,							_A			,							_A    )
def  UpperCamelCase						(       _A			:				Optimizer			,							_A			:				int			,							_A			:				int			,							_A			:				float = 0.5			,							_A			:				int = -1    )->       Any:





						"""simple docstring"""





						def lr_lambda(_A			:				Tuple    ):
												if current_step < num_warmup_steps:
																		return float(_A    ) / float(max(1			,							_A    )    )
												A__							=       float(current_step - num_warmup_steps    ) / float(max(1			,							num_training_steps - num_warmup_steps    )    )
												return max(0.0			,							0.5 * (1.0 + math.cos(math.pi * float(_A    ) * 2.0 * progress    ))    )

						return LambdaLR(_A			,							_A			,							_A    )
def  UpperCamelCase						(       _A			:				Optimizer			,							_A			:				int			,							_A			:				int			,							_A			:				int = 1			,							_A			:				int = -1    )->       Any:





						"""simple docstring"""





						def lr_lambda(_A			:				Tuple    ):
												if current_step < num_warmup_steps:
																		return float(_A    ) / float(max(1			,							_A    )    )
												A__							=       float(current_step - num_warmup_steps    ) / float(max(1			,							num_training_steps - num_warmup_steps    )    )
												if progress >= 1.0:
																		return 0.0
												return max(0.0			,							0.5 * (1.0 + math.cos(math.pi * ((float(_A    ) * progress) % 1.0)    ))    )

						return LambdaLR(_A			,							_A			,							_A    )
def  UpperCamelCase						(       _A			:				Union[str, Any]			,							_A			:				Union[str, Any]			,							_A			:				List[str]			,							_A			:				Tuple=1E-7			,							_A			:				Dict=1.0			,							_A			:				Union[str, Any]=-1    )->       Any:





						"""simple docstring"""





						A__							=       optimizer.defaults["lr"]
						if not (lr_init > lr_end):
												raise ValueError(f"""lr_end ({lr_end}) must be be smaller than initial lr ({lr_init})"""    )

						def lr_lambda(_A			:				int    ):
												if current_step < num_warmup_steps:
																		return float(_A    ) / float(max(1			,							_A    )    )
												elif current_step > num_training_steps:
																		return lr_end / lr_init  # as LambdaLR multiplies by lr_init
												else:
																		A__							=       lr_init - lr_end
																		A__							=       num_training_steps - num_warmup_steps
																		A__							=       1 - (current_step - num_warmup_steps) / decay_steps
																		A__							=       lr_range * pct_remaining**power + lr_end
																		return decay / lr_init  # as LambdaLR multiplies by lr_init

						return LambdaLR(_A			,							_A			,							_A    )


UpperCAmelCase_							:      Any      =  {
    SchedulerType.LINEAR: get_linear_schedule_with_warmup,
    SchedulerType.COSINE: get_cosine_schedule_with_warmup,
    SchedulerType.COSINE_WITH_RESTARTS: get_cosine_with_hard_restarts_schedule_with_warmup,
    SchedulerType.POLYNOMIAL: get_polynomial_decay_schedule_with_warmup,
    SchedulerType.CONSTANT: get_constant_schedule,
    SchedulerType.CONSTANT_WITH_WARMUP: get_constant_schedule_with_warmup,
    SchedulerType.PIECEWISE_CONSTANT: get_piecewise_constant_schedule,
}
def  UpperCamelCase						(       _A			:				Union[str, SchedulerType]			,							_A			:				Optimizer			,							_A			:				Optional[str] = None			,							_A			:				Optional[int] = None			,							_A			:				Optional[int] = None			,							_A			:				int = 1			,							_A			:				float = 1.0			,							_A			:				int = -1			,							)->       Union[str, Any]:





						"""simple docstring"""





						A__							=       SchedulerType(_A    )
						A__							=       TYPE_TO_SCHEDULER_FUNCTION[name]
						if name == SchedulerType.CONSTANT:
												return schedule_func(_A			,							last_epoch=_A    )

						if name == SchedulerType.PIECEWISE_CONSTANT:
												return schedule_func(_A			,							step_rules=_A			,							last_epoch=_A    )

						# All other schedulers require `num_warmup_steps`
						if num_warmup_steps is None:
												raise ValueError(f"""{name} requires `num_warmup_steps`, please provide that argument."""    )

						if name == SchedulerType.CONSTANT_WITH_WARMUP:
												return schedule_func(_A			,							num_warmup_steps=_A			,							last_epoch=_A    )

						# All other schedulers require `num_training_steps`
						if num_training_steps is None:
												raise ValueError(f"""{name} requires `num_training_steps`, please provide that argument."""    )

						if name == SchedulerType.COSINE_WITH_RESTARTS:
												return schedule_func(
												    _A			,							num_warmup_steps=_A			,							num_training_steps=_A			,							num_cycles=_A			,							last_epoch=_A			,							)

						if name == SchedulerType.POLYNOMIAL:
												return schedule_func(
												    _A			,							num_warmup_steps=_A			,							num_training_steps=_A			,							power=_A			,							last_epoch=_A			,							)

						return schedule_func(
						    _A			,							num_warmup_steps=_A			,							num_training_steps=_A			,							last_epoch=_A    )





