






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

import datasets


__UpperCAmelCase							:   Optional[int]							=  "\\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"


__UpperCAmelCase							:   List[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"


__UpperCAmelCase							:   int							=  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 					_snake_case						(			datasets.Metric ):






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







import torch
import torch.nn as nn
from transformers.modeling_utils import ModuleUtilsMixin
from transformers.models.ta.modeling_ta import TaBlock, TaConfig, TaLayerNorm

from ...configuration_utils import ConfigMixin, register_to_config
from ...models import ModelMixin


class 					_snake_case						(			_A       ,    _A       ,    _A ):






	@register_to_config
	def __init__(		self					,UpperCamelCase					,UpperCamelCase					,UpperCamelCase					,UpperCamelCase					,UpperCamelCase					,UpperCamelCase					,UpperCamelCase					,UpperCamelCase					,UpperCamelCase					,UpperCamelCase = False					,)						->    int:
					super().__init__()

					snake_case__      :Union[str, Any]         =   nn.Embedding(UpperCamelCase					,UpperCamelCase       )

					snake_case__      :int         =   nn.Embedding(UpperCamelCase					,UpperCamelCase       )
					snake_case__      :Any         =   False

					snake_case__      :List[Any]         =   nn.Dropout(p=UpperCamelCase       )

					snake_case__      :Tuple         =   TaConfig(
					    vocab_size=UpperCamelCase					,d_model=UpperCamelCase					,num_heads=UpperCamelCase					,d_kv=UpperCamelCase					,d_ff=UpperCamelCase					,dropout_rate=UpperCamelCase					,feed_forward_proj=UpperCamelCase					,is_decoder=UpperCamelCase					,is_encoder_decoder=UpperCamelCase					,)

					snake_case__      :List[str]         =   nn.ModuleList()
					for lyr_num in range(UpperCamelCase       ):
									snake_case__      :List[Any]         =   TaBlock(UpperCamelCase       )
									self.encoders.append(UpperCamelCase       )

					snake_case__      :Optional[Any]         =   TaLayerNorm(UpperCamelCase       )
					snake_case__      :Any         =   nn.Dropout(p=UpperCamelCase       )






	def    lowerCAmelCase_							(		self					,UpperCamelCase					,UpperCamelCase       )						->    int:
					snake_case__      :str         =   self.token_embedder(UpperCamelCase       )

					snake_case__      :int         =   encoder_input_tokens.shape[1]
					snake_case__      :List[Any]         =   torch.arange(UpperCamelCase					,device=encoder_input_tokens.device       )
					x += self.position_encoding(UpperCamelCase       )

					snake_case__      :Optional[int]         =   self.dropout_pre(UpperCamelCase       )

					# inverted the attention mask
					snake_case__      :Optional[Any]         =   encoder_input_tokens.size()
					snake_case__      :Dict         =   self.get_extended_attention_mask(UpperCamelCase					,UpperCamelCase       )

					for lyr in self.encoders:
									snake_case__      :str         =   lyr(UpperCamelCase					,UpperCamelCase       )[0]
					snake_case__      :List[Any]         =   self.layer_norm(UpperCamelCase       )

					return self.dropout_post(UpperCamelCase       ), encoder_inputs_mask