


import torch

from diffusers import DDIMParallelScheduler

from .test_schedulers import SchedulerCommonTest







class 							lowerCamelCase__						(				UpperCAmelCase   ):






			"""simple docstring"""






			_UpperCamelCase		:       int 			=		(DDIMParallelScheduler,)
			_UpperCamelCase		:       List[Any] 			=		(('eta', 0.0), ('num_inference_steps', 50))




			def 					snake_case__					(				self   ,    **snake_case  ):

						'''simple docstring'''


						UpperCamelCase__       =							{
						    "num_train_timesteps": 1000,
						    "beta_start": 0.0001,
						    "beta_end": 0.02,
						    "beta_schedule": "linear",
						    "clip_sample": True,
						}

						config.update(**snake_case  )
						return config




			def 					snake_case__					(				self   ,    **snake_case  ):

						'''simple docstring'''


						UpperCamelCase__       =							self.scheduler_classes[0]
						UpperCamelCase__       =							self.get_scheduler_config(**snake_case  )
						UpperCamelCase__       =							scheduler_class(**snake_case  )

						UpperCamelCase__,   UpperCamelCase__       =							10, 0.0

						UpperCamelCase__       =							self.dummy_model()
						UpperCamelCase__       =							self.dummy_sample_deter

						scheduler.set_timesteps(snake_case  )

						for t in scheduler.timesteps:
									UpperCamelCase__       =							model(snake_case   ,    snake_case  )
									UpperCamelCase__       =							scheduler.step(snake_case   ,    snake_case   ,    snake_case   ,    snake_case  ).prev_sample

						return sample




			def 					snake_case__					(				self  ):

						'''simple docstring'''


						for timesteps in [100, 500, 1000]:
									self.check_over_configs(num_train_timesteps=snake_case  )




			def 					snake_case__					(				self  ):

						'''simple docstring'''


						for steps_offset in [0, 1]:
									self.check_over_configs(steps_offset=snake_case  )

						UpperCamelCase__       =							self.scheduler_classes[0]
						UpperCamelCase__       =							self.get_scheduler_config(steps_offset=1  )
						UpperCamelCase__       =							scheduler_class(**snake_case  )
						scheduler.set_timesteps(5  )
						assert torch.equal(scheduler.timesteps   ,    torch.LongTensor([801, 601, 401, 201, 1]  )  )




			def 					snake_case__					(				self  ):

						'''simple docstring'''


						for beta_start, beta_end in zip([0.0001, 0.001, 0.01, 0.1]   ,    [0.002, 0.02, 0.2, 2]  ):
									self.check_over_configs(beta_start=snake_case   ,    beta_end=snake_case  )




			def 					snake_case__					(				self  ):

						'''simple docstring'''


						for schedule in ["linear", "squaredcos_cap_v2"]:
									self.check_over_configs(beta_schedule=snake_case  )




			def 					snake_case__					(				self  ):

						'''simple docstring'''


						for prediction_type in ["epsilon", "v_prediction"]:
									self.check_over_configs(prediction_type=snake_case  )




			def 					snake_case__					(				self  ):

						'''simple docstring'''


						for clip_sample in [True, False]:
									self.check_over_configs(clip_sample=snake_case  )




			def 					snake_case__					(				self  ):

						'''simple docstring'''


						for timestep_spacing in ["trailing", "leading"]:
									self.check_over_configs(timestep_spacing=snake_case  )




			def 					snake_case__					(				self  ):

						'''simple docstring'''


						for rescale_betas_zero_snr in [True, False]:
									self.check_over_configs(rescale_betas_zero_snr=snake_case  )




			def 					snake_case__					(				self  ):

						'''simple docstring'''


						self.check_over_configs(thresholding=snake_case  )
						for threshold in [0.5, 1.0, 2.0]:
									for prediction_type in ["epsilon", "v_prediction"]:
												self.check_over_configs(
												    thresholding=snake_case   ,    prediction_type=snake_case   ,    sample_max_value=snake_case   ,    )




			def 					snake_case__					(				self  ):

						'''simple docstring'''


						for t in [1, 10, 49]:
									self.check_over_forward(time_step=snake_case  )




			def 					snake_case__					(				self  ):

						'''simple docstring'''


						for t, num_inference_steps in zip([1, 10, 50]   ,    [10, 50, 500]  ):
									self.check_over_forward(time_step=snake_case   ,    num_inference_steps=snake_case  )




			def 					snake_case__					(				self  ):

						'''simple docstring'''


						for t, eta in zip([1, 10, 49]   ,    [0.0, 0.5, 1.0]  ):
									self.check_over_forward(time_step=snake_case   ,    eta=snake_case  )




			def 					snake_case__					(				self  ):

						'''simple docstring'''


						UpperCamelCase__       =							self.scheduler_classes[0]
						UpperCamelCase__       =							self.get_scheduler_config()
						UpperCamelCase__       =							scheduler_class(**snake_case  )

						assert torch.sum(torch.abs(scheduler._get_variance(0   ,    0  ) - 0.0  )  ) < 1E-5
						assert torch.sum(torch.abs(scheduler._get_variance(420   ,    400  ) - 0.14771  )  ) < 1E-5
						assert torch.sum(torch.abs(scheduler._get_variance(980   ,    960  ) - 0.32460  )  ) < 1E-5
						assert torch.sum(torch.abs(scheduler._get_variance(0   ,    0  ) - 0.0  )  ) < 1E-5
						assert torch.sum(torch.abs(scheduler._get_variance(487   ,    486  ) - 0.00979  )  ) < 1E-5
						assert torch.sum(torch.abs(scheduler._get_variance(999   ,    998  ) - 0.02  )  ) < 1E-5




			def 					snake_case__					(				self  ):

						'''simple docstring'''


						UpperCamelCase__       =							self.scheduler_classes[0]
						UpperCamelCase__       =							self.get_scheduler_config()
						UpperCamelCase__       =							scheduler_class(**snake_case  )

						UpperCamelCase__,   UpperCamelCase__       =							10, 0.0
						scheduler.set_timesteps(snake_case  )

						UpperCamelCase__       =							self.dummy_model()
						UpperCamelCase__       =							self.dummy_sample_deter
						UpperCamelCase__       =							self.dummy_sample_deter + 0.1
						UpperCamelCase__       =							self.dummy_sample_deter - 0.1

						UpperCamelCase__       =							samplea.shape[0]
						UpperCamelCase__       =							torch.stack([samplea, samplea, samplea]   ,    dim=0  )
						UpperCamelCase__       =							torch.arange(snake_case  )[0:3, None].repeat(1   ,    snake_case  )

						UpperCamelCase__       =							model(samples.flatten(0   ,    1  )   ,    timesteps.flatten(0   ,    1  )  )
						UpperCamelCase__       =							scheduler.batch_step_no_noise(snake_case   ,    timesteps.flatten(0   ,    1  )   ,    samples.flatten(0   ,    1  )   ,    snake_case  )

						UpperCamelCase__       =							torch.sum(torch.abs(snake_case  )  )
						UpperCamelCase__       =							torch.mean(torch.abs(snake_case  )  )

						assert abs(result_sum.item() - 1147.7904  ) < 1E-2
						assert abs(result_mean.item() - 0.4982  ) < 1E-3




			def 					snake_case__					(				self  ):

						'''simple docstring'''


						UpperCamelCase__       =							self.full_loop()

						UpperCamelCase__       =							torch.sum(torch.abs(snake_case  )  )
						UpperCamelCase__       =							torch.mean(torch.abs(snake_case  )  )

						assert abs(result_sum.item() - 172.0067  ) < 1E-2
						assert abs(result_mean.item() - 0.223967  ) < 1E-3




			def 					snake_case__					(				self  ):

						'''simple docstring'''


						UpperCamelCase__       =							self.full_loop(prediction_type="v_prediction"  )

						UpperCamelCase__       =							torch.sum(torch.abs(snake_case  )  )
						UpperCamelCase__       =							torch.mean(torch.abs(snake_case  )  )

						assert abs(result_sum.item() - 52.5302  ) < 1E-2
						assert abs(result_mean.item() - 0.0684  ) < 1E-3




			def 					snake_case__					(				self  ):

						'''simple docstring'''


						UpperCamelCase__       =							self.full_loop(set_alpha_to_one=snake_case   ,    beta_start=0.01  )
						UpperCamelCase__       =							torch.sum(torch.abs(snake_case  )  )
						UpperCamelCase__       =							torch.mean(torch.abs(snake_case  )  )

						assert abs(result_sum.item() - 149.8295  ) < 1E-2
						assert abs(result_mean.item() - 0.1951  ) < 1E-3







			def 					snake_case__					(				self  ):

						'''simple docstring'''


						UpperCamelCase__       =							self.full_loop(set_alpha_to_one=snake_case   ,    beta_start=0.01  )
						UpperCamelCase__       =							torch.sum(torch.abs(snake_case  )  )
						UpperCamelCase__       =							torch.mean(torch.abs(snake_case  )  )

						assert abs(result_sum.item() - 149.0784  ) < 1E-2
						assert abs(result_mean.item() - 0.1941  ) < 1E-3






import re
import string

import numpy as np

import datasets


__UpperCamelCase     		=							'\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'

__UpperCamelCase     		=							'\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'

__UpperCamelCase     		=							'\n'







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






			"""simple docstring"""






			def 					snake_case__					(				self  ):

						'''simple docstring'''


						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 					snake_case__					(				self   ,    snake_case   ,    snake_case   ,    snake_case=None   ,    snake_case=False   ,    snake_case=False   ,    snake_case=False   ,    ):

						'''simple docstring'''


						if regexes_to_ignore is not None:
									for s in regexes_to_ignore:
												UpperCamelCase__       =							np.array([re.sub(snake_case   ,    ""   ,    snake_case  ) for x in predictions]  )
												UpperCamelCase__       =							np.array([re.sub(snake_case   ,    ""   ,    snake_case  ) for x in references]  )
						else:
									UpperCamelCase__       =							np.asarray(snake_case  )
									UpperCamelCase__       =							np.asarray(snake_case  )

						if ignore_case:
									UpperCamelCase__       =							np.char.lower(snake_case  )
									UpperCamelCase__       =							np.char.lower(snake_case  )

						if ignore_punctuation:
									UpperCamelCase__       =							string.punctuation.maketrans(""   ,    ""   ,    string.punctuation  )
									UpperCamelCase__       =							np.char.translate(snake_case   ,    table=snake_case  )
									UpperCamelCase__       =							np.char.translate(snake_case   ,    table=snake_case  )

						if ignore_numbers:
									UpperCamelCase__       =							string.digits.maketrans(""   ,    ""   ,    string.digits  )
									UpperCamelCase__       =							np.char.translate(snake_case   ,    table=snake_case  )
									UpperCamelCase__       =							np.char.translate(snake_case   ,    table=snake_case  )

						UpperCamelCase__       =							predictions == references

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


