





'''simple docstring'''







from sklearn.metrics import precision_score

import datasets


snake_case_       :    Tuple          =					"\nPrecision is the fraction of correctly labeled positive examples out of all of the examples that were labeled as positive. It is computed via the equation:\nPrecision = TP / (TP + FP)\nwhere TP is the True positives (i.e. the examples correctly labeled as positive) and FP is the False positive examples (i.e. the examples incorrectly labeled as positive).\n"


snake_case_       :    Union[str, Any]          =					"\nArgs:\n    predictions (`list` of `int`): Predicted class labels.\n    references (`list` of `int`): Actual class labels.\n    labels (`list` of `int`): The set of labels to include when `average` is not set to `'binary'`. If `average` is `None`, it should be the label order. Labels present in the data can be excluded, for example to calculate a multiclass average ignoring a majority negative class. Labels not present in the data will result in 0 components in a macro average. For multilabel targets, labels are column indices. By default, all labels in `predictions` and `references` are used in sorted order. Defaults to None.\n    pos_label (`int`): The class to be considered the positive class, in the case where `average` is set to `binary`. Defaults to 1.\n    average (`string`): This parameter is required for multiclass/multilabel targets. If set to `None`, the scores for each class are returned. Otherwise, this determines the type of averaging performed on the data. Defaults to `'binary'`.\n\n        - 'binary': Only report results for the class specified by `pos_label`. This is applicable only if the classes found in `predictions` and `references` are binary.\n        - 'micro': Calculate metrics globally by counting the total true positives, false negatives and false positives.\n        - 'macro': Calculate metrics for each label, and find their unweighted mean. This does not take label imbalance into account.\n        - 'weighted': Calculate metrics for each label, and find their average weighted by support (the number of true instances for each label). This alters `'macro'` to account for label imbalance. This option can result in an F-score that is not between precision and recall.\n        - 'samples': Calculate metrics for each instance, and find their average (only meaningful for multilabel classification).\n    sample_weight (`list` of `float`): Sample weights Defaults to None.\n    zero_division (`int` or `string`): Sets the value to return when there is a zero division. Defaults to 'warn'.\n\n        - 0: Returns 0 when there is a zero division.\n        - 1: Returns 1 when there is a zero division.\n        - 'warn': Raises warnings and then returns 0 when there is a zero division.\n\nReturns:\n    precision (`float` or `array` of `float`): Precision score or list of precision scores, depending on the value passed to `average`. Minimum possible value is 0. Maximum possible value is 1. Higher values indicate that fewer negative examples were incorrectly labeled as positive, which means that, generally, higher scores are better.\n\nExamples:\n\n    Example 1-A simple binary example\n        >>> precision_metric = datasets.load_metric(\"precision\")\n        >>> results = precision_metric.compute(references=[0, 1, 0, 1, 0], predictions=[0, 0, 1, 1, 0])\n        >>> print(results)\n        {'precision': 0.5}\n\n    Example 2-The same simple binary example as in Example 1, but with `pos_label` set to `0`.\n        >>> precision_metric = datasets.load_metric(\"precision\")\n        >>> results = precision_metric.compute(references=[0, 1, 0, 1, 0], predictions=[0, 0, 1, 1, 0], pos_label=0)\n        >>> print(round(results['precision'], 2))\n        0.67\n\n    Example 3-The same simple binary example as in Example 1, but with `sample_weight` included.\n        >>> precision_metric = datasets.load_metric(\"precision\")\n        >>> results = precision_metric.compute(references=[0, 1, 0, 1, 0], predictions=[0, 0, 1, 1, 0], sample_weight=[0.9, 0.5, 3.9, 1.2, 0.3])\n        >>> print(results)\n        {'precision': 0.23529411764705882}\n\n    Example 4-A multiclass example, with different values for the `average` input.\n        >>> predictions = [0, 2, 1, 0, 0, 1]\n        >>> references = [0, 1, 2, 0, 1, 2]\n        >>> results = precision_metric.compute(predictions=predictions, references=references, average='macro')\n        >>> print(results)\n        {'precision': 0.2222222222222222}\n        >>> results = precision_metric.compute(predictions=predictions, references=references, average='micro')\n        >>> print(results)\n        {'precision': 0.3333333333333333}\n        >>> results = precision_metric.compute(predictions=predictions, references=references, average='weighted')\n        >>> print(results)\n        {'precision': 0.2222222222222222}\n        >>> results = precision_metric.compute(predictions=predictions, references=references, average=None)\n        >>> print([round(res, 2) for res in results['precision']])\n        [0.67, 0.0, 0.0]\n"


snake_case_       :    Optional[Any]          =					"\n@article{scikit-learn,\n    title={Scikit-learn: Machine Learning in {P}ython},\n    author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V.\n    and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P.\n    and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and\n    Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.},\n    journal={Journal of Machine Learning Research},\n    volume={12},\n    pages={2825--2830},\n    year={2011}\n}\n"


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

							def       UpperCAmelCase__		(							self							:       List[Any]				)   ->    str:







													"""simple docstring"""
													return datasets.MetricInfo(
													    description=_DESCRIPTION    ,       citation=_CITATION    ,       inputs_description=_KWARGS_DESCRIPTION    ,       features=datasets.Features(
													        {
													            '''predictions''': datasets.Sequence(datasets.Value('''int32'''				)				),
													            '''references''': datasets.Sequence(datasets.Value('''int32'''				)				),
													        }
													        if self.config_name == '''multilabel'''
													        else {
													            '''predictions''': datasets.Value('''int32'''				),
													            '''references''': datasets.Value('''int32'''				),
													        }				)    ,       reference_urls=['''https://scikit-learn.org/stable/modules/generated/sklearn.metrics.precision_score.html''']    ,       )



							def       UpperCAmelCase__		(							self							:       List[str]    ,       __magic_name__							:       Any    ,       __magic_name__							:       str    ,       __magic_name__							:       Union[str, Any]=None    ,       __magic_name__							:       List[str]=1    ,       __magic_name__							:       Optional[int]="binary"    ,       __magic_name__							:       Tuple=None    ,       __magic_name__							:       int="warn"    ,       )   ->    Union[str, Any]:







													"""simple docstring"""
													UpperCAmelCase_     : List[str]         =					precision_score(
													    __magic_name__    ,       __magic_name__    ,       labels=__magic_name__    ,       pos_label=__magic_name__    ,       average=__magic_name__    ,       sample_weight=__magic_name__    ,       zero_division=__magic_name__    ,       )
													return {"precision": float(__magic_name__				) if score.size == 1 else score}












'''simple docstring'''







import math
from dataclasses import dataclass
from typing import List, Optional, Tuple, Union

import numpy as np
import torch

from ..configuration_utils import ConfigMixin, register_to_config
from ..utils import BaseOutput, logging, randn_tensor
from .scheduling_utils import KarrasDiffusionSchedulers, SchedulerMixin


snake_case_       :    Union[str, Any]          =					logging.get_logger(__name__)  # pylint: disable=invalid-name


@dataclass
# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput with DDPM->EulerDiscrete
class    __a (lowerCamelCase       ):
							__a   :				torch.FloatTensor
							__a   :				Optional[torch.FloatTensor]       =    None





def     lowerCamelCase_ (					SCREAMING_SNAKE_CASE__ :     str,     SCREAMING_SNAKE_CASE__ :     Optional[Any]=0.9_99,     SCREAMING_SNAKE_CASE__ :     Tuple="cosine",     )	->		Optional[Any]:
						if alpha_transform_type == "cosine":

												def alpha_bar_fn(SCREAMING_SNAKE_CASE__ :     Union[str, Any]				):
																		return math.cos((t + 0.0_08) / 1.0_08 * math.pi / 2				) ** 2

						elif alpha_transform_type == "exp":

												def alpha_bar_fn(SCREAMING_SNAKE_CASE__ :     Dict				):
																		return math.exp(t * -12.0				)

						else:
												raise ValueError(F"""Unsupported alpha_tranform_type: {alpha_transform_type}"""				)

						UpperCAmelCase_     : Dict         =					[]
						for i in range(SCREAMING_SNAKE_CASE__				):
												UpperCAmelCase_     : str         =					i / num_diffusion_timesteps
												UpperCAmelCase_     : str         =					(i + 1) / num_diffusion_timesteps
												betas.append(min(1 - alpha_bar_fn(SCREAMING_SNAKE_CASE__				) / alpha_bar_fn(SCREAMING_SNAKE_CASE__				),     SCREAMING_SNAKE_CASE__				)				)
						return torch.tensor(SCREAMING_SNAKE_CASE__,     dtype=torch.floataa				)


class    __a (lowerCamelCase    ,						lowerCamelCase       ):
							__a   :				str       =    [e.name for e in KarrasDiffusionSchedulers]
							__a   :				Tuple       =    1

							@register_to_config
							def __init__(							self							:       Union[str, Any]    ,       __magic_name__							:       int = 10_00    ,       __magic_name__							:       float = 0.0_0_0_1    ,       __magic_name__							:       float = 0.0_2    ,       __magic_name__							:       str = "linear"    ,       __magic_name__							:       Optional[Union[np.ndarray, List[float]]] = None    ,       __magic_name__							:       str = "epsilon"    ,       __magic_name__							:       str = "linear"    ,       __magic_name__							:       Optional[bool] = False    ,       __magic_name__							:       str = "linspace"    ,       __magic_name__							:       int = 0    ,       )   ->    Any:







													"""simple docstring"""
													if trained_betas is not None:
																			UpperCAmelCase_     : Optional[Any]         =					torch.tensor(__magic_name__    ,       dtype=torch.floataa				)
													elif beta_schedule == "linear":
																			UpperCAmelCase_     : Tuple         =					torch.linspace(__magic_name__    ,       __magic_name__    ,       __magic_name__    ,       dtype=torch.floataa				)
													elif beta_schedule == "scaled_linear":
																			# this schedule is very specific to the latent diffusion model.
																			UpperCAmelCase_     : Optional[Any]         =					(
																			    torch.linspace(beta_start**0.5    ,       beta_end**0.5    ,       __magic_name__    ,       dtype=torch.floataa				) ** 2
																			)
													elif beta_schedule == "squaredcos_cap_v2":
																			# Glide cosine schedule
																			UpperCAmelCase_     : Tuple         =					betas_for_alpha_bar(__magic_name__				)
													else:
																			raise NotImplementedError(F"""{beta_schedule} does is not implemented for {self.__class__}"""				)

													UpperCAmelCase_     : Dict         =					1.0 - self.betas
													UpperCAmelCase_     : Union[str, Any]         =					torch.cumprod(self.alphas    ,       dim=0				)

													UpperCAmelCase_     : Dict         =					np.array(((1 - self.alphas_cumprod) / self.alphas_cumprod) ** 0.5				)
													UpperCAmelCase_     : Optional[Any]         =					np.concatenate([sigmas[::-1], [0.0]]				).astype(np.floataa				)
													UpperCAmelCase_     : Optional[Any]         =					torch.from_numpy(__magic_name__				)

													# setable values
													UpperCAmelCase_     : Any         =					None
													UpperCAmelCase_     : Optional[int]         =					np.linspace(0    ,       num_train_timesteps - 1    ,       __magic_name__    ,       dtype=__magic_name__				)[::-1].copy()
													UpperCAmelCase_     : Union[str, Any]         =					torch.from_numpy(__magic_name__				)
													UpperCAmelCase_     : Dict         =					False
													UpperCAmelCase_     : Tuple         =					use_karras_sigmas

							@property
							def       UpperCAmelCase__		(							self							:       Dict				)   ->    List[Any]:







													"""simple docstring"""
													# standard deviation of the initial noise distribution
													if self.config.timestep_spacing in ["linspace", "trailing"]:
																			return self.sigmas.max()

													return (self.sigmas.max() ** 2 + 1) ** 0.5

							def       UpperCAmelCase__		(							self							:       Tuple    ,       __magic_name__							:       torch.FloatTensor    ,       __magic_name__							:       Union[float, torch.FloatTensor]				)   ->    torch.FloatTensor:







													"""simple docstring"""
													if isinstance(__magic_name__    ,       torch.Tensor				):
																			UpperCAmelCase_     : Tuple         =					timestep.to(self.timesteps.device				)
													UpperCAmelCase_     : Tuple         =					(self.timesteps == timestep).nonzero().item()
													UpperCAmelCase_     : List[str]         =					self.sigmas[step_index]

													UpperCAmelCase_     : List[str]         =					sample / ((sigma**2 + 1) ** 0.5)

													UpperCAmelCase_     : Tuple         =					True
													return sample

							def       UpperCAmelCase__		(							self							:       Optional[int]    ,       __magic_name__							:       int    ,       __magic_name__							:       Union[str, torch.device] = None				)   ->    Any:







													"""simple docstring"""
													UpperCAmelCase_     : Optional[Any]         =					num_inference_steps

													# "linspace", "leading", "trailing" corresponds to annotation of Table 2. of https://arxiv.org/abs/2305.08891
													if self.config.timestep_spacing == "linspace":
																			UpperCAmelCase_     : int         =					np.linspace(0    ,       self.config.num_train_timesteps - 1    ,       __magic_name__    ,       dtype=__magic_name__				)[
																			    ::-1
																			].copy()
													elif self.config.timestep_spacing == "leading":
																			UpperCAmelCase_     : List[str]         =					self.config.num_train_timesteps // self.num_inference_steps
																			# creates integer timesteps by multiplying by ratio
																			# casting to int to avoid issues when num_inference_step is power of 3
																			UpperCAmelCase_     : List[str]         =					(np.arange(0    ,       __magic_name__				) * step_ratio).round()[::-1].copy().astype(__magic_name__				)
																			timesteps += self.config.steps_offset
													elif self.config.timestep_spacing == "trailing":
																			UpperCAmelCase_     : Union[str, Any]         =					self.config.num_train_timesteps / self.num_inference_steps
																			# creates integer timesteps by multiplying by ratio
																			# casting to int to avoid issues when num_inference_step is power of 3
																			UpperCAmelCase_     : int         =					(np.arange(self.config.num_train_timesteps    ,       0    ,       -step_ratio				)).round().copy().astype(__magic_name__				)
																			timesteps -= 1
													else:
																			raise ValueError(
																			    F"""{self.config.timestep_spacing} is not supported. Please make sure to choose one of 'linspace', 'leading' or 'trailing'."""				)

													UpperCAmelCase_     : Optional[Any]         =					np.array(((1 - self.alphas_cumprod) / self.alphas_cumprod) ** 0.5				)
													UpperCAmelCase_     : Union[str, Any]         =					np.log(__magic_name__				)

													if self.config.interpolation_type == "linear":
																			UpperCAmelCase_     : List[Any]         =					np.interp(__magic_name__    ,       np.arange(0    ,       len(__magic_name__				)				)    ,       __magic_name__				)
													elif self.config.interpolation_type == "log_linear":
																			UpperCAmelCase_     : str         =					torch.linspace(np.log(sigmas[-1]				)    ,       np.log(sigmas[0]				)    ,       num_inference_steps + 1				).exp()
													else:
																			raise ValueError(
																			    F"""{self.config.interpolation_type} is not implemented. Please specify interpolation_type to either"""
																			    ''' \'linear\' or \'log_linear\''''				)

													if self.use_karras_sigmas:
																			UpperCAmelCase_     : List[Any]         =					self._convert_to_karras(in_sigmas=__magic_name__    ,       num_inference_steps=self.num_inference_steps				)
																			UpperCAmelCase_     : Dict         =					np.array([self._sigma_to_t(__magic_name__    ,       __magic_name__				) for sigma in sigmas]				)

													UpperCAmelCase_     : Tuple         =					np.concatenate([sigmas, [0.0]]				).astype(np.floataa				)
													UpperCAmelCase_     : Any         =					torch.from_numpy(__magic_name__				).to(device=__magic_name__				)
													if str(__magic_name__				).startswith('''mps'''				):
																			# mps does not support float64
																			UpperCAmelCase_     : List[str]         =					torch.from_numpy(__magic_name__				).to(__magic_name__    ,       dtype=torch.floataa				)
													else:
																			UpperCAmelCase_     : int         =					torch.from_numpy(__magic_name__				).to(device=__magic_name__				)

							def       UpperCAmelCase__		(							self							:       Union[str, Any]    ,       __magic_name__							:       Union[str, Any]    ,       __magic_name__							:       Optional[Any]				)   ->    Optional[Any]:







													"""simple docstring"""
													# get log sigma
													UpperCAmelCase_     : List[Any]         =					np.log(__magic_name__				)

													# get distribution
													UpperCAmelCase_     : Optional[int]         =					log_sigma - log_sigmas[:, np.newaxis]

													# get sigmas range
													UpperCAmelCase_     : List[Any]         =					np.cumsum((dists >= 0)    ,       axis=0				).argmax(axis=0				).clip(max=log_sigmas.shape[0] - 2				)
													UpperCAmelCase_     : List[str]         =					low_idx + 1

													UpperCAmelCase_     : Optional[int]         =					log_sigmas[low_idx]
													UpperCAmelCase_     : Union[str, Any]         =					log_sigmas[high_idx]

													# interpolate sigmas
													UpperCAmelCase_     : Union[str, Any]         =					(low - log_sigma) / (low - high)
													UpperCAmelCase_     : Dict         =					np.clip(__magic_name__    ,       0    ,       1				)

													# transform interpolation to time range
													UpperCAmelCase_     : Optional[Any]         =					(1 - w) * low_idx + w * high_idx
													UpperCAmelCase_     : List[Any]         =					t.reshape(sigma.shape				)
													return t

							def       UpperCAmelCase__		(							self							:       str    ,       __magic_name__							:       torch.FloatTensor    ,       __magic_name__							:       Union[str, Any]				)   ->    torch.FloatTensor:







													"""simple docstring"""
													UpperCAmelCase_     : float         =					in_sigmas[-1].item()
													UpperCAmelCase_     : float         =					in_sigmas[0].item()

													UpperCAmelCase_     : List[str]         =					7.0  # 7.0 is the value used in the paper
													UpperCAmelCase_     : int         =					np.linspace(0    ,       1    ,       __magic_name__				)
													UpperCAmelCase_     : int         =					sigma_min ** (1 / rho)
													UpperCAmelCase_     : str         =					sigma_max ** (1 / rho)
													UpperCAmelCase_     : Optional[Any]         =					(max_inv_rho + ramp * (min_inv_rho - max_inv_rho)) ** rho
													return sigmas

							def       UpperCAmelCase__		(							self							:       List[Any]    ,       __magic_name__							:       torch.FloatTensor    ,       __magic_name__							:       Union[float, torch.FloatTensor]    ,       __magic_name__							:       torch.FloatTensor    ,       __magic_name__							:       float = 0.0    ,       __magic_name__							:       float = 0.0    ,       __magic_name__							:       float = float('''inf'''				)    ,       __magic_name__							:       float = 1.0    ,       __magic_name__							:       Optional[torch.Generator] = None    ,       __magic_name__							:       bool = True    ,       )   ->    Union[EulerDiscreteSchedulerOutput, Tuple]:







													"""simple docstring"""

													if (
													    isinstance(__magic_name__    ,       __magic_name__				)
													    or isinstance(__magic_name__    ,       torch.IntTensor				)
													    or isinstance(__magic_name__    ,       torch.LongTensor				)
													):
																			raise ValueError(
																			    (
																			        '''Passing integer indices (e.g. from `enumerate(timesteps)`) as timesteps to'''
																			        ''' `EulerDiscreteScheduler.step()` is not supported. Make sure to pass'''
																			        ''' one of the `scheduler.timesteps` as a timestep.'''
																			    )    ,       )

													if not self.is_scale_input_called:
																			logger.warning(
																			    '''The `scale_model_input` function should be called before `step` to ensure correct denoising. '''
																			    '''See `StableDiffusionPipeline` for a usage example.'''				)

													if isinstance(__magic_name__    ,       torch.Tensor				):
																			UpperCAmelCase_     : Tuple         =					timestep.to(self.timesteps.device				)

													UpperCAmelCase_     : Optional[Any]         =					(self.timesteps == timestep).nonzero().item()
													UpperCAmelCase_     : int         =					self.sigmas[step_index]

													UpperCAmelCase_     : List[str]         =					min(s_churn / (len(self.sigmas				) - 1)    ,       2**0.5 - 1				) if s_tmin <= sigma <= s_tmax else 0.0

													UpperCAmelCase_     : Dict         =					randn_tensor(
													    model_output.shape    ,       dtype=model_output.dtype    ,       device=model_output.device    ,       generator=__magic_name__				)

													UpperCAmelCase_     : Tuple         =					noise * s_noise
													UpperCAmelCase_     : Tuple         =					sigma * (gamma + 1)

													if gamma > 0:
																			UpperCAmelCase_     : Any         =					sample + eps * (sigma_hat**2 - sigma**2) ** 0.5

													# 1. compute predicted original sample (x_0) from sigma-scaled predicted noise
													# NOTE: "original_sample" should not be an expected prediction_type but is left in for
													# backwards compatibility
													if self.config.prediction_type == "original_sample" or self.config.prediction_type == "sample":
																			UpperCAmelCase_     : str         =					model_output
													elif self.config.prediction_type == "epsilon":
																			UpperCAmelCase_     : Any         =					sample - sigma_hat * model_output
													elif self.config.prediction_type == "v_prediction":
																			# * c_out + input * c_skip
																			UpperCAmelCase_     : Optional[Any]         =					model_output * (-sigma / (sigma**2 + 1) ** 0.5) + (sample / (sigma**2 + 1))
													else:
																			raise ValueError(
																			    F"""prediction_type given as {self.config.prediction_type} must be one of `epsilon`, or `v_prediction`"""				)

													# 2. Convert to an ODE derivative
													UpperCAmelCase_     : int         =					(sample - pred_original_sample) / sigma_hat

													UpperCAmelCase_     : List[str]         =					self.sigmas[step_index + 1] - sigma_hat

													UpperCAmelCase_     : str         =					sample + derivative * dt

													if not return_dict:
																			return (prev_sample,)

													return EulerDiscreteSchedulerOutput(prev_sample=__magic_name__    ,       pred_original_sample=__magic_name__				)

							def       UpperCAmelCase__		(							self							:       Optional[int]    ,       __magic_name__							:       torch.FloatTensor    ,       __magic_name__							:       torch.FloatTensor    ,       __magic_name__							:       torch.FloatTensor    ,       )   ->    torch.FloatTensor:







													"""simple docstring"""
													# Make sure sigmas and timesteps have the same device and dtype as original_samples
													UpperCAmelCase_     : Union[str, Any]         =					self.sigmas.to(device=original_samples.device    ,       dtype=original_samples.dtype				)
													if original_samples.device.type == "mps" and torch.is_floating_point(__magic_name__				):
																			# mps does not support float64
																			UpperCAmelCase_     : str         =					self.timesteps.to(original_samples.device    ,       dtype=torch.floataa				)
																			UpperCAmelCase_     : Dict         =					timesteps.to(original_samples.device    ,       dtype=torch.floataa				)
													else:
																			UpperCAmelCase_     : Optional[int]         =					self.timesteps.to(original_samples.device				)
																			UpperCAmelCase_     : Dict         =					timesteps.to(original_samples.device				)

													UpperCAmelCase_     : List[str]         =					[(schedule_timesteps == t).nonzero().item() for t in timesteps]

													UpperCAmelCase_     : Union[str, Any]         =					sigmas[step_indices].flatten()
													while len(sigma.shape				) < len(original_samples.shape				):
																			UpperCAmelCase_     : List[str]         =					sigma.unsqueeze(-1				)

													UpperCAmelCase_     : str         =					original_samples + noise * sigma
													return noisy_samples



							def __len__(							self							:       Optional[Any]				)   ->    int:







													"""simple docstring"""
													return self.config.num_train_timesteps





