


"""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
lowerCAmelCase	=							'src/transformers'
lowerCAmelCase	=							'docs/source/en'
lowerCAmelCase	=							'.'
def       _a			(				SCREAMING_SNAKE_CASE						,    SCREAMING_SNAKE_CASE						,    SCREAMING_SNAKE_CASE					):


		"""simple docstring"""




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

		lowercase__						=				start_index
		while not lines[end_index].startswith(SCREAMING_SNAKE_CASE					):
				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 |
lowerCAmelCase	=							'Model|Encoder|Decoder|ForConditionalGeneration'
# Regexes that match TF/Flax/PT model names.
lowerCAmelCase	=							re.compile(R'TF(.*)(?:Model|Encoder|Decoder|ForConditionalGeneration)')
lowerCAmelCase	=							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.
lowerCAmelCase	=							re.compile(R'(.*)(?:Model|Encoder|Decoder|ForConditionalGeneration)')


# This is to make sure the transformers module imported is the one in the repo.
lowerCAmelCase	=							direct_transformers_import(TRANSFORMERS_PATH)
def       _a			(				SCREAMING_SNAKE_CASE					):


		"""simple docstring"""




		lowercase__						=				re.finditer('''.+?(?:(?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])|$)'''						,    SCREAMING_SNAKE_CASE					)
		return [m.group(0					) for m in matches]
def       _a			(				SCREAMING_SNAKE_CASE						,    SCREAMING_SNAKE_CASE					):


		"""simple docstring"""




		lowercase__						=				2 if text == '''✅''' or text == '''❌''' else len(SCREAMING_SNAKE_CASE					)
		lowercase__						=				(width - text_length) // 2
		lowercase__						=				width - text_length - left_indent
		return " " * left_indent + text + " " * right_indent
def       _a			(				):


		"""simple docstring"""




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

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

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

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

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

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


		"""simple docstring"""




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

		if current_table != new_table:
				if overwrite:
						with open(os.path.join(SCREAMING_SNAKE_CASE						,    '''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__":
			lowerCAmelCase	=							argparse.ArgumentParser()
			parser.add_argument('--fix_and_overwrite', action='store_true', help='Whether to fix inconsistencies.')
			lowerCAmelCase	=							parser.parse_args()

			check_model_table(args.fix_and_overwrite)






import math_equivalence  # From: git+https://github.com/hendrycks/math.git

import datasets


lowerCAmelCase	=							'\\n@article{hendrycksmath2021,\n  title={Measuring Mathematical Problem Solving With the MATH Dataset},\n  author={Dan Hendrycks\n    and Collin Burns\n    and Saurav Kadavath\n    and Akul Arora\n    and Steven Basart\n    and Eric Tang\n    and Dawn Song\n    and Jacob Steinhardt},\n  journal={arXiv preprint arXiv:2103.03874},\n  year={2021}\n}\n'


lowerCAmelCase	=							'\\nThis metric is used to assess performance on the Mathematics Aptitude Test of Heuristics (MATH) dataset.\nIt first canonicalizes the inputs (e.g., converting "1/2" to "\\frac{1}{2}") and then computes accuracy.\n'


lowerCAmelCase	=							R'\nCalculates accuracy after canonicalizing inputs.\n\nArgs:\n    predictions: list of predictions to score. Each prediction\n        is a string that contains natural language and LaTex.\n    references: list of reference for each prediction. Each\n        reference is a string that contains natural language\n        and LaTex.\nReturns:\n    accuracy: accuracy after canonicalizing inputs\n        (e.g., converting "1/2" to "\\frac{1}{2}")\n\nExamples:\n    >>> metric = datasets.load_metric("competition_math")\n    >>> results = metric.compute(references=["\\frac{1}{2}"], predictions=["1/2"])\n    >>> print(results)\n    {\'accuracy\': 1.0}\n'


@datasets.utils.file_utils.add_end_docstrings(_DESCRIPTION					,     _KWARGS_DESCRIPTION      )
class 	_a							(      datasets.Metric      ):

							def 				lowerCamelCase_			(							self:     List[str]							)      ->       Tuple:





									"""simple docstring"""







									return datasets.MetricInfo(
									    description=_DESCRIPTION   ,						citation=_CITATION   ,						inputs_description=_KWARGS_DESCRIPTION   ,						features=datasets.Features(
									        {
									            '''predictions''': datasets.Value('''string'''							),
									            '''references''': datasets.Value('''string'''							),
									        }							)   ,						homepage='''https://github.com/hendrycks/math'''   ,						codebase_urls=['''https://github.com/hendrycks/math''']   ,						)


							def 				lowerCamelCase_			(							self:     Union[str, Any]   ,						UpperCamelCase_:     Any   ,						UpperCamelCase_:     int							)      ->       List[str]:





									"""simple docstring"""







									lowercase__						=				0.0
									for i, j in zip(UpperCamelCase_   ,						UpperCamelCase_							):
											n_correct += 1.0 if math_equivalence.is_equiv(UpperCamelCase_   ,						UpperCamelCase_							) else 0.0
									lowercase__						=				n_correct / len(UpperCamelCase_							)
									return {
									    "accuracy": accuracy,
									}


