
"""simple docstring"""







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

import datasets


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


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


lowerCamelCase__   					=		R"""
Calculates accuracy after canonicalizing inputs.

Args:
    predictions: list of predictions to score. Each prediction
        is a string that contains natural language and LaTex.
    references: list of reference for each prediction. Each
        reference is a string that contains natural language
        and LaTex.
Returns:
    accuracy: accuracy after canonicalizing inputs
        (e.g., converting \"1/2\" to \"\\frac{1}{2}\")

Examples:
    >>> metric = datasets.load_metric(\"competition_math\")
    >>> results = metric.compute(references=[\"\\frac{1}{2}\"], predictions=[\"1/2\"])
    >>> print(results)
    {'accuracy': 1.0}
"""




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

		def 							__lowerCamelCase							(      self ):
			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       ,		_SCREAMING_SNAKE_CASE       ,		_SCREAMING_SNAKE_CASE ):
			__lowerCAmelCase			:						List[Any]									=      0.0
			for i, j in zip(_SCREAMING_SNAKE_CASE       ,		_SCREAMING_SNAKE_CASE ):
				n_correct += 1.0 if math_equivalence.is_equiv(_SCREAMING_SNAKE_CASE       ,		_SCREAMING_SNAKE_CASE ) else 0.0
			__lowerCAmelCase			:						Any									=      n_correct / len(_SCREAMING_SNAKE_CASE )
			return {
			    "accuracy": accuracy,
			}
import os
import tempfile
import unittest
from pathlib import Path

from transformers import AutoConfig, is_tf_available
from transformers.testing_utils import require_tf


if is_tf_available():
					import tensorflow as tf

					from transformers import TensorFlowBenchmark, TensorFlowBenchmarkArguments





@require_tf
class 					lowercase__						(   unittest.TestCase):


						def        __A       (       self	:				str    ,		UpperCamelCase__	:				int						):





												'''simple docstring'''





												for model_result in results.values():
																		for batch_size, sequence_length in zip(model_result['''bs''']    ,		model_result['''ss''']						):
																								SCREAMING_SNAKE_CASE							:    Union[str, Any]							=							model_result['''result'''][batch_size][sequence_length]
																								self.assertIsNotNone(UpperCamelCase__						)


						def        __A       (       self	:				Optional[Any]						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    List[Any]							=							'''sshleifer/tiny-gpt2'''
												SCREAMING_SNAKE_CASE							:    List[Any]							=							TensorFlowBenchmarkArguments(
												    models=[MODEL_ID]    ,		training=UpperCamelCase__    ,		inference=UpperCamelCase__    ,		sequence_lengths=[8]    ,		batch_sizes=[1]    ,		eager_mode=UpperCamelCase__    ,		multi_process=UpperCamelCase__    ,		)
												SCREAMING_SNAKE_CASE							:    str							=							TensorFlowBenchmark(UpperCamelCase__						)
												SCREAMING_SNAKE_CASE							:    List[Any]							=							benchmark.run()
												self.check_results_dict_not_empty(results.time_inference_result						)
												self.check_results_dict_not_empty(results.memory_inference_result						)


						def        __A       (       self	:				List[str]						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    str							=							'''sgugger/tiny-distilbert-classification'''
												SCREAMING_SNAKE_CASE							:    Union[str, Any]							=							TensorFlowBenchmarkArguments(
												    models=[MODEL_ID]    ,		training=UpperCamelCase__    ,		inference=UpperCamelCase__    ,		sequence_lengths=[8]    ,		batch_sizes=[1]    ,		multi_process=UpperCamelCase__    ,		only_pretrain_model=UpperCamelCase__    ,		)
												SCREAMING_SNAKE_CASE							:    List[str]							=							TensorFlowBenchmark(UpperCamelCase__						)
												SCREAMING_SNAKE_CASE							:    Optional[Any]							=							benchmark.run()
												self.check_results_dict_not_empty(results.time_inference_result						)
												self.check_results_dict_not_empty(results.memory_inference_result						)


						def        __A       (       self	:				Any						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    Union[str, Any]							=							'''sshleifer/tiny-gpt2'''
												SCREAMING_SNAKE_CASE							:    int							=							TensorFlowBenchmarkArguments(
												    models=[MODEL_ID]    ,		training=UpperCamelCase__    ,		inference=UpperCamelCase__    ,		sequence_lengths=[8]    ,		batch_sizes=[1]    ,		multi_process=UpperCamelCase__    ,		)
												SCREAMING_SNAKE_CASE							:    Union[str, Any]							=							TensorFlowBenchmark(UpperCamelCase__						)
												SCREAMING_SNAKE_CASE							:    int							=							benchmark.run()
												self.check_results_dict_not_empty(results.time_inference_result						)
												self.check_results_dict_not_empty(results.memory_inference_result						)


						def        __A       (       self	:				Optional[Any]						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    Optional[int]							=							'''sshleifer/tiny-gpt2'''
												SCREAMING_SNAKE_CASE							:    int							=							AutoConfig.from_pretrained(UpperCamelCase__						)
												SCREAMING_SNAKE_CASE							:    Optional[int]							=							TensorFlowBenchmarkArguments(
												    models=[MODEL_ID]    ,		training=UpperCamelCase__    ,		inference=UpperCamelCase__    ,		sequence_lengths=[8]    ,		batch_sizes=[1]    ,		eager_mode=UpperCamelCase__    ,		multi_process=UpperCamelCase__    ,		)
												SCREAMING_SNAKE_CASE							:    str							=							TensorFlowBenchmark(UpperCamelCase__    ,		[config]						)
												SCREAMING_SNAKE_CASE							:    Optional[int]							=							benchmark.run()
												self.check_results_dict_not_empty(results.time_inference_result						)
												self.check_results_dict_not_empty(results.memory_inference_result						)


						def        __A       (       self	:				int						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    Dict							=							'''sshleifer/tiny-gpt2'''
												SCREAMING_SNAKE_CASE							:    Optional[int]							=							AutoConfig.from_pretrained(UpperCamelCase__						)
												SCREAMING_SNAKE_CASE							:    Optional[Any]							=							TensorFlowBenchmarkArguments(
												    models=[MODEL_ID]    ,		training=UpperCamelCase__    ,		inference=UpperCamelCase__    ,		sequence_lengths=[8]    ,		batch_sizes=[1]    ,		multi_process=UpperCamelCase__    ,		)
												SCREAMING_SNAKE_CASE							:    List[str]							=							TensorFlowBenchmark(UpperCamelCase__    ,		[config]						)
												SCREAMING_SNAKE_CASE							:    int							=							benchmark.run()
												self.check_results_dict_not_empty(results.time_inference_result						)
												self.check_results_dict_not_empty(results.memory_inference_result						)


						def        __A       (       self	:				List[Any]						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    str							=							'''sshleifer/tiny-gpt2'''
												SCREAMING_SNAKE_CASE							:    int							=							TensorFlowBenchmarkArguments(
												    models=[MODEL_ID]    ,		training=UpperCamelCase__    ,		inference=UpperCamelCase__    ,		sequence_lengths=[8]    ,		batch_sizes=[1]    ,		multi_process=UpperCamelCase__    ,		)
												SCREAMING_SNAKE_CASE							:    Optional[int]							=							TensorFlowBenchmark(UpperCamelCase__						)
												SCREAMING_SNAKE_CASE							:    Tuple							=							benchmark.run()
												self.check_results_dict_not_empty(results.time_train_result						)
												self.check_results_dict_not_empty(results.memory_train_result						)


						def        __A       (       self	:				Optional[Any]						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    List[str]							=							'''sshleifer/tiny-gpt2'''
												SCREAMING_SNAKE_CASE							:    int							=							AutoConfig.from_pretrained(UpperCamelCase__						)
												SCREAMING_SNAKE_CASE							:    Tuple							=							TensorFlowBenchmarkArguments(
												    models=[MODEL_ID]    ,		training=UpperCamelCase__    ,		inference=UpperCamelCase__    ,		sequence_lengths=[8]    ,		batch_sizes=[1]    ,		multi_process=UpperCamelCase__    ,		)
												SCREAMING_SNAKE_CASE							:    Dict							=							TensorFlowBenchmark(UpperCamelCase__    ,		[config]						)
												SCREAMING_SNAKE_CASE							:    str							=							benchmark.run()
												self.check_results_dict_not_empty(results.time_train_result						)
												self.check_results_dict_not_empty(results.memory_train_result						)


						def        __A       (       self	:				Optional[int]						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    List[str]							=							'''patrickvonplaten/t5-tiny-random'''
												SCREAMING_SNAKE_CASE							:    Union[str, Any]							=							AutoConfig.from_pretrained(UpperCamelCase__						)
												SCREAMING_SNAKE_CASE							:    Union[str, Any]							=							TensorFlowBenchmarkArguments(
												    models=[MODEL_ID]    ,		training=UpperCamelCase__    ,		inference=UpperCamelCase__    ,		sequence_lengths=[8]    ,		batch_sizes=[1]    ,		multi_process=UpperCamelCase__    ,		)
												SCREAMING_SNAKE_CASE							:    Dict							=							TensorFlowBenchmark(UpperCamelCase__    ,		configs=[config]						)
												SCREAMING_SNAKE_CASE							:    Dict							=							benchmark.run()
												self.check_results_dict_not_empty(results.time_inference_result						)
												self.check_results_dict_not_empty(results.memory_inference_result						)


						@unittest.skipIf(is_tf_available() and len(tf.config.list_physical_devices('''GPU'''						)						) == 0    ,		'''Cannot do xla on CPU.'''						)
						def        __A       (       self	:				Optional[int]						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    Optional[Any]							=							'''sshleifer/tiny-gpt2'''
												SCREAMING_SNAKE_CASE							:    Tuple							=							TensorFlowBenchmarkArguments(
												    models=[MODEL_ID]    ,		training=UpperCamelCase__    ,		inference=UpperCamelCase__    ,		sequence_lengths=[8]    ,		batch_sizes=[1]    ,		use_xla=UpperCamelCase__    ,		multi_process=UpperCamelCase__    ,		)
												SCREAMING_SNAKE_CASE							:    Any							=							TensorFlowBenchmark(UpperCamelCase__						)
												SCREAMING_SNAKE_CASE							:    List[Any]							=							benchmark.run()
												self.check_results_dict_not_empty(results.time_inference_result						)
												self.check_results_dict_not_empty(results.memory_inference_result						)


						def        __A       (       self	:				List[str]						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    Optional[int]							=							'''sshleifer/tiny-gpt2'''
												with tempfile.TemporaryDirectory() as tmp_dir:
																		SCREAMING_SNAKE_CASE							:    Tuple							=							TensorFlowBenchmarkArguments(
																		    models=[MODEL_ID]    ,		inference=UpperCamelCase__    ,		save_to_csv=UpperCamelCase__    ,		sequence_lengths=[8]    ,		batch_sizes=[1]    ,		inference_time_csv_file=os.path.join(UpperCamelCase__    ,		'''inf_time.csv'''						)    ,		inference_memory_csv_file=os.path.join(UpperCamelCase__    ,		'''inf_mem.csv'''						)    ,		env_info_csv_file=os.path.join(UpperCamelCase__    ,		'''env.csv'''						)    ,		multi_process=UpperCamelCase__    ,		)
																		SCREAMING_SNAKE_CASE							:    Optional[Any]							=							TensorFlowBenchmark(UpperCamelCase__						)
																		benchmark.run()
																		self.assertTrue(Path(os.path.join(UpperCamelCase__    ,		'''inf_time.csv'''						)						).exists()						)
																		self.assertTrue(Path(os.path.join(UpperCamelCase__    ,		'''inf_mem.csv'''						)						).exists()						)
																		self.assertTrue(Path(os.path.join(UpperCamelCase__    ,		'''env.csv'''						)						).exists()						)



						def        __A       (       self	:				int						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    Any							=							'''sshleifer/tiny-gpt2'''

												def _check_summary_is_not_empty(UpperCamelCase__	:				Dict						):
																		self.assertTrue(hasattr(UpperCamelCase__    ,		'''sequential'''						)						)
																		self.assertTrue(hasattr(UpperCamelCase__    ,		'''cumulative'''						)						)
																		self.assertTrue(hasattr(UpperCamelCase__    ,		'''current'''						)						)
																		self.assertTrue(hasattr(UpperCamelCase__    ,		'''total'''						)						)

												with tempfile.TemporaryDirectory() as tmp_dir:
																		SCREAMING_SNAKE_CASE							:    List[str]							=							TensorFlowBenchmarkArguments(
																		    models=[MODEL_ID]    ,		inference=UpperCamelCase__    ,		sequence_lengths=[8]    ,		batch_sizes=[1]    ,		log_filename=os.path.join(UpperCamelCase__    ,		'''log.txt'''						)    ,		log_print=UpperCamelCase__    ,		trace_memory_line_by_line=UpperCamelCase__    ,		eager_mode=UpperCamelCase__    ,		multi_process=UpperCamelCase__    ,		)
																		SCREAMING_SNAKE_CASE							:    List[Any]							=							TensorFlowBenchmark(UpperCamelCase__						)
																		SCREAMING_SNAKE_CASE							:    Dict							=							benchmark.run()
																		_check_summary_is_not_empty(result.inference_summary						)
																		self.assertTrue(Path(os.path.join(UpperCamelCase__    ,		'''log.txt'''						)						).exists()						)





