






'''simple docstring'''







import os
import tempfile
import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_torch


if is_torch_available():
		import torch
		from torch import nn

		from transformers import (
		    Adafactor,
		    AdamW,
		    get_constant_schedule,
		    get_constant_schedule_with_warmup,
		    get_cosine_schedule_with_warmup,
		    get_cosine_with_hard_restarts_schedule_with_warmup,
		    get_inverse_sqrt_schedule,
		    get_linear_schedule_with_warmup,
		    get_polynomial_decay_schedule_with_warmup,
		)





def 		_UpperCAmelCase   (							__A							:   List[Any]						,		__A							:   Tuple=10      ):
				a_		:  List[str]											=      []
				for _ in range(__A      ):
								lrs.append(scheduler.get_lr()[0]      )
								scheduler.step()
				return lrs





def 		_UpperCAmelCase   (							__A							:   Dict						,		__A							:   Tuple=10      ):
				a_		:  Dict											=      []
				for step in range(__A      ):
								lrs.append(scheduler.get_lr()[0]      )
								scheduler.step()
								if step == num_steps // 2:
												with tempfile.TemporaryDirectory() as tmpdirname:
																a_		:  Tuple											=      os.path.join(__A						,		'''schedule.bin'''      )
																torch.save(scheduler.state_dict()						,		__A      )

																a_		:  List[Any]											=      torch.load(__A      )
																scheduler.load_state_dict(__A      )
				return lrs





@require_torch
class 	SCREAMING_SNAKE_CASE					(					unittest.TestCase       ):






			def 			SCREAMING_SNAKE_CASE      ( self      :			Tuple  ,						__SCREAMING_SNAKE_CASE      :			Optional[Any]  ,						__SCREAMING_SNAKE_CASE      :			str  ,						__SCREAMING_SNAKE_CASE      :			Any					)			->			Tuple:
							self.assertEqual(len(__SCREAMING_SNAKE_CASE					)  ,						len(__SCREAMING_SNAKE_CASE					)					)
							for a, b in zip(__SCREAMING_SNAKE_CASE  ,						__SCREAMING_SNAKE_CASE					):
											self.assertAlmostEqual(__SCREAMING_SNAKE_CASE  ,						__SCREAMING_SNAKE_CASE  ,						delta=__SCREAMING_SNAKE_CASE					)






			def 			SCREAMING_SNAKE_CASE      ( self      :			Optional[Any]					)			->			Tuple:
							a_		:  Any											=      torch.tensor([0.1, -0.2, -0.1]  ,						requires_grad=__SCREAMING_SNAKE_CASE					)
							a_		:  Union[str, Any]											=      torch.tensor([0.4, 0.2, -0.5]					)
							a_		:  Dict											=      nn.MSELoss()
							# No warmup, constant schedule, no gradient clipping
							a_		:  Any											=      AdamW(params=[w]  ,						lr=2e-1  ,						weight_decay=0.0					)
							for _ in range(100					):
											a_		:  int											=      criterion(__SCREAMING_SNAKE_CASE  ,						__SCREAMING_SNAKE_CASE					)
											loss.backward()
											optimizer.step()
											w.grad.detach_()  # No zero_grad() function on simple tensors. we do it ourselves.
											w.grad.zero_()
							self.assertListAlmostEqual(w.tolist()  ,						[0.4, 0.2, -0.5]  ,						tol=1e-2					)







			def 			SCREAMING_SNAKE_CASE      ( self      :			Dict					)			->			Dict:
							a_		:  Tuple											=      torch.tensor([0.1, -0.2, -0.1]  ,						requires_grad=__SCREAMING_SNAKE_CASE					)
							a_		:  Dict											=      torch.tensor([0.4, 0.2, -0.5]					)
							a_		:  int											=      nn.MSELoss()
							# No warmup, constant schedule, no gradient clipping
							a_		:  Union[str, Any]											=      Adafactor(
							    params=[w]  ,						lr=1e-2  ,						eps=(1e-30, 1e-3)  ,						clip_threshold=1.0  ,						decay_rate=-0.8  ,						betaa=__SCREAMING_SNAKE_CASE  ,						weight_decay=0.0  ,						relative_step=__SCREAMING_SNAKE_CASE  ,						scale_parameter=__SCREAMING_SNAKE_CASE  ,						warmup_init=__SCREAMING_SNAKE_CASE  ,						)
							for _ in range(1000					):
											a_		:  Tuple											=      criterion(__SCREAMING_SNAKE_CASE  ,						__SCREAMING_SNAKE_CASE					)
											loss.backward()
											optimizer.step()
											w.grad.detach_()  # No zero_grad() function on simple tensors. we do it ourselves.
											w.grad.zero_()
							self.assertListAlmostEqual(w.tolist()  ,						[0.4, 0.2, -0.5]  ,						tol=1e-2					)





@require_torch
class 	SCREAMING_SNAKE_CASE					(					unittest.TestCase       ):
			snake_case__  =				nn.Linear(50	,   50       ) if is_torch_available() else None
			snake_case__  =				AdamW(m.parameters()	,   lr=10.0       ) if is_torch_available() else None
			snake_case__  =				10






			def 			SCREAMING_SNAKE_CASE      ( self      :			Any  ,						__SCREAMING_SNAKE_CASE      :			Optional[int]  ,						__SCREAMING_SNAKE_CASE      :			Optional[Any]  ,						__SCREAMING_SNAKE_CASE      :			str  ,						__SCREAMING_SNAKE_CASE      :			List[Any]=None					)			->			Dict:
							self.assertEqual(len(__SCREAMING_SNAKE_CASE					)  ,						len(__SCREAMING_SNAKE_CASE					)					)
							for a, b in zip(__SCREAMING_SNAKE_CASE  ,						__SCREAMING_SNAKE_CASE					):
											self.assertAlmostEqual(__SCREAMING_SNAKE_CASE  ,						__SCREAMING_SNAKE_CASE  ,						delta=__SCREAMING_SNAKE_CASE  ,						msg=__SCREAMING_SNAKE_CASE					)







			def 			SCREAMING_SNAKE_CASE      ( self      :			str					)			->			Optional[int]:
							a_		:  List[str]											=      {'''num_warmup_steps''': 2, '''num_training_steps''': 10}
							# schedulers doct format
							# function: (sched_args_dict, expected_learning_rates)
							a_		:  Union[str, Any]											=      {
							    get_constant_schedule: ({}, [10.0] * self.num_steps),
							    get_constant_schedule_with_warmup: (
							        {'''num_warmup_steps''': 4},
							        [0.0, 2.5, 5.0, 7.5, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0],
							    ),
							    get_linear_schedule_with_warmup: (
							        {**common_kwargs},
							        [0.0, 5.0, 10.0, 8.75, 7.5, 6.25, 5.0, 3.75, 2.5, 1.25],
							    ),
							    get_cosine_schedule_with_warmup: (
							        {**common_kwargs},
							        [0.0, 5.0, 10.0, 9.61, 8.53, 6.91, 5.0, 3.08, 1.46, 0.38],
							    ),
							    get_cosine_with_hard_restarts_schedule_with_warmup: (
							        {**common_kwargs, '''num_cycles''': 2},
							        [0.0, 5.0, 10.0, 8.53, 5.0, 1.46, 10.0, 8.53, 5.0, 1.46],
							    ),
							    get_polynomial_decay_schedule_with_warmup: (
							        {**common_kwargs, '''power''': 2.0, '''lr_end''': 1e-7},
							        [0.0, 5.0, 10.0, 7.656, 5.625, 3.906, 2.5, 1.406, 0.625, 0.156],
							    ),
							    get_inverse_sqrt_schedule: (
							        {'''num_warmup_steps''': 2},
							        [0.0, 5.0, 10.0, 8.165, 7.071, 6.325, 5.774, 5.345, 5.0, 4.714],
							    ),
							}

							for scheduler_func, data in scheds.items():
											a_				,			a_		:  str											=      data

											a_		:  Dict											=      scheduler_func(self.optimizer  ,						**__SCREAMING_SNAKE_CASE					)
											self.assertEqual(len([scheduler.get_lr()[0]]					)  ,						1					)
											a_		:  Optional[Any]											=      unwrap_schedule(__SCREAMING_SNAKE_CASE  ,						self.num_steps					)
											self.assertListAlmostEqual(
											    __SCREAMING_SNAKE_CASE  ,						__SCREAMING_SNAKE_CASE  ,						tol=1e-2  ,						msg=f'failed for {scheduler_func} in normal scheduler'  ,						)

											a_		:  Optional[int]											=      scheduler_func(self.optimizer  ,						**__SCREAMING_SNAKE_CASE					)
											if scheduler_func.__name__ != "get_constant_schedule":
															LambdaScheduleWrapper.wrap_scheduler(__SCREAMING_SNAKE_CASE					)  # wrap to test picklability of the schedule
											a_		:  Optional[int]											=      unwrap_and_save_reload_schedule(__SCREAMING_SNAKE_CASE  ,						self.num_steps					)
											self.assertListEqual(__SCREAMING_SNAKE_CASE  ,						__SCREAMING_SNAKE_CASE  ,						msg=f'failed for {scheduler_func} in save and reload'					)





class 	SCREAMING_SNAKE_CASE					:






			def __init__( self      :			Dict  ,						__SCREAMING_SNAKE_CASE      :			int					)			->			Any:
							a_		:  List[str]											=      fn






			def __call__( self      :			Union[str, Any]  ,						*__SCREAMING_SNAKE_CASE      :			Optional[Any]  ,						**__SCREAMING_SNAKE_CASE      :			Optional[Any]					)			->			Optional[Any]:
							return self.fn(*__SCREAMING_SNAKE_CASE  ,						**__SCREAMING_SNAKE_CASE					)







			@classmethod
			def 			SCREAMING_SNAKE_CASE      ( self      :			Optional[int]  ,						__SCREAMING_SNAKE_CASE      :			int					)			->			Dict:
							a_		:  str											=      list(map(self  ,						scheduler.lr_lambdas					)					)












'''simple docstring'''







import re
import string

import numpy as np

import datasets


__lowerCAmelCase       		=      '\nReturns the rate at which the input predicted strings exactly match their references, ignoring any strings input as part of the regexes_to_ignore list.\n'

__lowerCAmelCase       		=      '\nArgs:\n    predictions: List of predicted texts.\n    references: List of reference texts.\n    regexes_to_ignore: List, defaults to None. Regex expressions of characters to\n        ignore when calculating the exact matches. Note: these regexes are removed\n        from the input data before the changes based on the options below (e.g. ignore_case,\n        ignore_punctuation, ignore_numbers) are applied.\n    ignore_case: Boolean, defaults to False. If true, turns everything\n        to lowercase so that capitalization differences are ignored.\n    ignore_punctuation: Boolean, defaults to False. If true, removes all punctuation before\n        comparing predictions and references.\n    ignore_numbers: Boolean, defaults to False. If true, removes all punctuation before\n        comparing predictions and references.\nReturns:\n    exact_match: Dictionary containing exact_match rate. Possible values are between 0.0 and 100.0, inclusive.\nExamples:\n    >>> exact_match = datasets.load_metric("exact_match")\n    >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n    >>> preds = ["cat?", "theater", "yelling", "agent"]\n    >>> results = exact_match.compute(references=refs, predictions=preds)\n    >>> print(round(results["exact_match"], 1))\n    25.0\n\n    >>> exact_match = datasets.load_metric("exact_match")\n    >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n    >>> preds = ["cat?", "theater", "yelling", "agent"]\n    >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=["the ", "yell"], ignore_case=True, ignore_punctuation=True)\n    >>> print(round(results["exact_match"], 1))\n    50.0\n\n\n    >>> exact_match = datasets.load_metric("exact_match")\n    >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n    >>> preds = ["cat?", "theater", "yelling", "agent"]\n    >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=["the ", "yell", "YELL"], ignore_case=True, ignore_punctuation=True)\n    >>> print(round(results["exact_match"], 1))\n    75.0\n\n    >>> exact_match = datasets.load_metric("exact_match")\n    >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n    >>> preds = ["cat?", "theater", "yelling", "agent"]\n    >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=["the ", "yell", "YELL"], ignore_case=True, ignore_punctuation=True, ignore_numbers=True)\n    >>> print(round(results["exact_match"], 1))\n    100.0\n\n    >>> exact_match = datasets.load_metric("exact_match")\n    >>> refs = ["The cat sat on the mat.", "Theaters are great.", "It\'s like comparing oranges and apples."]\n    >>> preds = ["The cat sat on the mat?", "Theaters are great.", "It\'s like comparing apples and oranges."]\n    >>> results = exact_match.compute(references=refs, predictions=preds)\n    >>> print(round(results["exact_match"], 1))\n    33.3\n\n'

__lowerCAmelCase       		=      '\n'





@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION	,   _KWARGS_DESCRIPTION       )
class 	SCREAMING_SNAKE_CASE					(					datasets.Metric       ):






			def 			SCREAMING_SNAKE_CASE      ( self      :			Optional[Any]					)			->			int:
							return datasets.MetricInfo(
							    description=_DESCRIPTION  ,						citation=_CITATION  ,						inputs_description=_KWARGS_DESCRIPTION  ,						features=datasets.Features(
							        {
							            '''predictions''': datasets.Value('''string'''  ,						id='''sequence'''					),
							            '''references''': datasets.Value('''string'''  ,						id='''sequence'''					),
							        }					)  ,						reference_urls=[]  ,						)







			def 			SCREAMING_SNAKE_CASE      ( self      :			str  ,						__SCREAMING_SNAKE_CASE      :			List[Any]  ,						__SCREAMING_SNAKE_CASE      :			Optional[int]  ,						__SCREAMING_SNAKE_CASE      :			Dict=None  ,						__SCREAMING_SNAKE_CASE      :			int=False  ,						__SCREAMING_SNAKE_CASE      :			Optional[int]=False  ,						__SCREAMING_SNAKE_CASE      :			Dict=False  ,						)			->			str:
							if regexes_to_ignore is not None:
											for s in regexes_to_ignore:
															a_		:  Optional[Any]											=      np.array([re.sub(__SCREAMING_SNAKE_CASE  ,						''''''  ,						__SCREAMING_SNAKE_CASE					) for x in predictions]					)
															a_		:  int											=      np.array([re.sub(__SCREAMING_SNAKE_CASE  ,						''''''  ,						__SCREAMING_SNAKE_CASE					) for x in references]					)
							else:
											a_		:  List[str]											=      np.asarray(__SCREAMING_SNAKE_CASE					)
											a_		:  Any											=      np.asarray(__SCREAMING_SNAKE_CASE					)

							if ignore_case:
											a_		:  List[str]											=      np.char.lower(__SCREAMING_SNAKE_CASE					)
											a_		:  List[Any]											=      np.char.lower(__SCREAMING_SNAKE_CASE					)

							if ignore_punctuation:
											a_		:  Any											=      string.punctuation.maketrans(''''''  ,						''''''  ,						string.punctuation					)
											a_		:  Union[str, Any]											=      np.char.translate(__SCREAMING_SNAKE_CASE  ,						table=__SCREAMING_SNAKE_CASE					)
											a_		:  int											=      np.char.translate(__SCREAMING_SNAKE_CASE  ,						table=__SCREAMING_SNAKE_CASE					)

							if ignore_numbers:
											a_		:  int											=      string.digits.maketrans(''''''  ,						''''''  ,						string.digits					)
											a_		:  Optional[int]											=      np.char.translate(__SCREAMING_SNAKE_CASE  ,						table=__SCREAMING_SNAKE_CASE					)
											a_		:  Dict											=      np.char.translate(__SCREAMING_SNAKE_CASE  ,						table=__SCREAMING_SNAKE_CASE					)

							a_		:  Optional[Any]											=      predictions == references

							return {"exact_match": np.mean(__SCREAMING_SNAKE_CASE					) * 100}




