



"""simple docstring"""


from dataclasses import dataclass, field
from typing import Tuple

from ..utils import cached_property, is_torch_available, is_torch_tpu_available, logging, requires_backends
from .benchmark_args_utils import BenchmarkArguments


if is_torch_available():
						import torch

if is_torch_tpu_available(check_device=False):
						import torch_xla.core.xla_model as xm


__A   :							Dict										=   logging.get_logger(__name__)
@dataclass
class 			_a (					snake_case__):
			"""simple docstring"""
			UpperCamelCase__			      =	[
			    """no_inference""",
			    """no_cuda""",
			    """no_tpu""",
			    """no_speed""",
			    """no_memory""",
			    """no_env_print""",
			    """no_multi_process""",
			]



			def __init__(		self     :    Optional[int]				,   **__UpperCamelCase     :    str   )->Optional[Any]:
							for deprecated_arg in self.deprecated_args:
											if deprecated_arg in kwargs:
															_UpperCAmelCase					=    deprecated_arg[3:]
															setattr(self				,   lowercase_				,   not kwargs.pop(lowercase_   )   )
															logger.warning(
															    F'{deprecated_arg} is depreciated. Please use --no_{positive_arg} or'
															    F' {positive_arg}={kwargs[positive_arg]}'   )

							_UpperCAmelCase					=    kwargs.pop('''torchscript'''				,   self.torchscript   )
							_UpperCAmelCase					=    kwargs.pop('''torch_xla_tpu_print_metrics'''				,   self.torch_xla_tpu_print_metrics   )
							_UpperCAmelCase					=    kwargs.pop('''fp16_opt_level'''				,   self.fpaa_opt_level   )
							super().__init__(**lowercase_   )

			UpperCamelCase__			      =	field(default=snake_case__			,							metadata={"""help""": """Trace the models using torchscript"""})
			UpperCamelCase__			      =	field(default=snake_case__			,							metadata={"""help""": """Print Xla/PyTorch tpu metrics"""})
			UpperCamelCase__			      =	field(
			    default="""O1"""			,							metadata={
			        """help""": (
			            """For fp16: Apex AMP optimization level selected in ['O0', 'O1', 'O2', and 'O3']. """
			            """See details at https://nvidia.github.io/apex/amp.html"""
			        )
			    }			,							)



			@cached_property
			def  lowercase__						(		self     :    Optional[int]   )->int:
							requires_backends(self				,   ['''torch''']   )
							logger.info('''PyTorch: setting up devices'''   )
							if not self.cuda:
											_UpperCAmelCase					=    torch.device('''cpu'''   )
											_UpperCAmelCase					=    0
							elif is_torch_tpu_available():
											_UpperCAmelCase					=    xm.xla_device()
											_UpperCAmelCase					=    0
							else:
											_UpperCAmelCase					=    torch.device('''cuda''' if torch.cuda.is_available() else '''cpu'''   )
											_UpperCAmelCase					=    torch.cuda.device_count()
							return device, n_gpu



			@property
			def  lowercase__						(		self     :    str   )->Any:
							return is_torch_tpu_available() and self.tpu



			@property
			def  lowercase__						(		self     :    List[str]   )->int:
							requires_backends(self				,   ['''torch''']   )
							# TODO(PVP): currently only single GPU is supported
							return torch.cuda.current_device()



			@property
			def  lowercase__						(		self     :    Optional[int]   )->str:
							requires_backends(self				,   ['''torch''']   )
							return self._setup_devices[0]



			@property
			def  lowercase__						(		self     :    Union[str, Any]   )->str:
							requires_backends(self				,   ['''torch''']   )
							return self._setup_devices[1]



			@property
			def  lowercase__						(		self     :    str   )->str:
							return self.n_gpu > 0








"""simple docstring"""


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

import datasets


__A   :							Any										=   "\\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"


__A   :							Optional[Any]										=   "\\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"


__A   :							Tuple										=   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):
			"""simple docstring"""



			def  lowercase__						(		self     :    List[Any]   )->Dict:
							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  lowercase__						(		self     :    int				,   __UpperCamelCase     :    List[str]				,   __UpperCamelCase     :    Union[str, Any]   )->Tuple:
							_UpperCAmelCase					=    0.0
							for i, j in zip(__UpperCamelCase				,   __UpperCamelCase   ):
											n_correct += 1.0 if math_equivalence.is_equiv(__UpperCamelCase				,   __UpperCamelCase   ) else 0.0
							_UpperCAmelCase					=    n_correct / len(__UpperCamelCase   )
							return {
							    "accuracy": accuracy,
							}



