



'''simple docstring'''




import tempfile

import torch

from diffusers import PNDMScheduler

from .test_schedulers import SchedulerCommonTest







class 	_lowerCAmelCase							(			__UpperCAmelCase ):
	__SCREAMING_SNAKE_CASE   :		Optional[int]					=					(PNDMScheduler,)
	__SCREAMING_SNAKE_CASE   :		List[str]					=					(('num_inference_steps', 50),)


	def     _a	(self       ,      **lowercase							):
				A_	:						Tuple	=     {
				    """num_train_timesteps""": 1000,
				    """beta_start""": 0.00_01,
				    """beta_end""": 0.02,
				    """beta_schedule""": """linear""",
				}

				config.update(**lowercase							)
				return config


	def     _a	(self       ,      lowercase=0       ,      **lowercase							):
				A_	:						Any	=     dict(self.forward_default_kwargs							)
				A_	:						Optional[Any]	=     kwargs.pop("""num_inference_steps"""       ,      lowercase							)
				A_	:						int	=     self.dummy_sample
				A_	:						Union[str, Any]	=     0.1 * sample
				A_	:						int	=     [residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05]

				for scheduler_class in self.scheduler_classes:
							A_	:						Optional[int]	=     self.get_scheduler_config(**lowercase							)
							A_	:						List[Any]	=     scheduler_class(**lowercase							)
							scheduler.set_timesteps(lowercase							)
							# copy over dummy past residuals
							A_	:						Dict	=     dummy_past_residuals[:]

							with tempfile.TemporaryDirectory() as tmpdirname:
										scheduler.save_config(lowercase							)
										A_	:						int	=     scheduler_class.from_pretrained(lowercase							)
										new_scheduler.set_timesteps(lowercase							)
										# copy over dummy past residuals
										A_	:						str	=     dummy_past_residuals[:]

							A_	:						Dict	=     scheduler.step_prk(lowercase       ,      lowercase       ,      lowercase       ,      **lowercase							).prev_sample
							A_	:						Union[str, Any]	=     new_scheduler.step_prk(lowercase       ,      lowercase       ,      lowercase       ,      **lowercase							).prev_sample

							assert torch.sum(torch.abs(output - new_output							)							) < 1E-5, "Scheduler outputs are not identical"

							A_	:						Optional[Any]	=     scheduler.step_plms(lowercase       ,      lowercase       ,      lowercase       ,      **lowercase							).prev_sample
							A_	:						Any	=     new_scheduler.step_plms(lowercase       ,      lowercase       ,      lowercase       ,      **lowercase							).prev_sample

							assert torch.sum(torch.abs(output - new_output							)							) < 1E-5, "Scheduler outputs are not identical"


	def     _a	(self							):
				pass


	def     _a	(self       ,      lowercase=0       ,      **lowercase							):
				A_	:						Optional[int]	=     dict(self.forward_default_kwargs							)
				A_	:						int	=     kwargs.pop("""num_inference_steps"""       ,      lowercase							)
				A_	:						Optional[int]	=     self.dummy_sample
				A_	:						Optional[int]	=     0.1 * sample
				A_	:						Dict	=     [residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05]

				for scheduler_class in self.scheduler_classes:
							A_	:						Tuple	=     self.get_scheduler_config()
							A_	:						int	=     scheduler_class(**lowercase							)
							scheduler.set_timesteps(lowercase							)

							# copy over dummy past residuals (must be after setting timesteps)
							A_	:						str	=     dummy_past_residuals[:]

							with tempfile.TemporaryDirectory() as tmpdirname:
										scheduler.save_config(lowercase							)
										A_	:						Optional[int]	=     scheduler_class.from_pretrained(lowercase							)
										# copy over dummy past residuals
										new_scheduler.set_timesteps(lowercase							)

										# copy over dummy past residual (must be after setting timesteps)
										A_	:						Any	=     dummy_past_residuals[:]

							A_	:						Union[str, Any]	=     scheduler.step_prk(lowercase       ,      lowercase       ,      lowercase       ,      **lowercase							).prev_sample
							A_	:						List[str]	=     new_scheduler.step_prk(lowercase       ,      lowercase       ,      lowercase       ,      **lowercase							).prev_sample

							assert torch.sum(torch.abs(output - new_output							)							) < 1E-5, "Scheduler outputs are not identical"

							A_	:						Union[str, Any]	=     scheduler.step_plms(lowercase       ,      lowercase       ,      lowercase       ,      **lowercase							).prev_sample
							A_	:						Union[str, Any]	=     new_scheduler.step_plms(lowercase       ,      lowercase       ,      lowercase       ,      **lowercase							).prev_sample

							assert torch.sum(torch.abs(output - new_output							)							) < 1E-5, "Scheduler outputs are not identical"


	def     _a	(self       ,      **lowercase							):
				A_	:						Union[str, Any]	=     self.scheduler_classes[0]
				A_	:						Any	=     self.get_scheduler_config(**lowercase							)
				A_	:						Union[str, Any]	=     scheduler_class(**lowercase							)

				A_	:						Union[str, Any]	=     10
				A_	:						Any	=     self.dummy_model()
				A_	:						str	=     self.dummy_sample_deter
				scheduler.set_timesteps(lowercase							)

				for i, t in enumerate(scheduler.prk_timesteps							):
							A_	:						str	=     model(lowercase       ,      lowercase							)
							A_	:						Any	=     scheduler.step_prk(lowercase       ,      lowercase       ,      lowercase							).prev_sample

				for i, t in enumerate(scheduler.plms_timesteps							):
							A_	:						Optional[int]	=     model(lowercase       ,      lowercase							)
							A_	:						Optional[int]	=     scheduler.step_plms(lowercase       ,      lowercase       ,      lowercase							).prev_sample

				return sample


	def     _a	(self							):
				A_	:						Tuple	=     dict(self.forward_default_kwargs							)

				A_	:						int	=     kwargs.pop("""num_inference_steps"""       ,      lowercase							)

				for scheduler_class in self.scheduler_classes:
							A_	:						int	=     self.get_scheduler_config()
							A_	:						str	=     scheduler_class(**lowercase							)

							A_	:						Optional[int]	=     self.dummy_sample
							A_	:						str	=     0.1 * sample

							if num_inference_steps is not None and hasattr(lowercase       ,      """set_timesteps"""							):
										scheduler.set_timesteps(lowercase							)
							elif num_inference_steps is not None and not hasattr(lowercase       ,      """set_timesteps"""							):
										A_	:						List[Any]	=     num_inference_steps

							# copy over dummy past residuals (must be done after set_timesteps)
							A_	:						int	=     [residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05]
							A_	:						Optional[Any]	=     dummy_past_residuals[:]

							A_	:						List[str]	=     scheduler.step_prk(lowercase       ,      0       ,      lowercase       ,      **lowercase							).prev_sample
							A_	:						Tuple	=     scheduler.step_prk(lowercase       ,      1       ,      lowercase       ,      **lowercase							).prev_sample

							self.assertEqual(output_a.shape       ,      sample.shape							)
							self.assertEqual(output_a.shape       ,      output_a.shape							)

							A_	:						int	=     scheduler.step_plms(lowercase       ,      0       ,      lowercase       ,      **lowercase							).prev_sample
							A_	:						Dict	=     scheduler.step_plms(lowercase       ,      1       ,      lowercase       ,      **lowercase							).prev_sample

							self.assertEqual(output_a.shape       ,      sample.shape							)
							self.assertEqual(output_a.shape       ,      output_a.shape							)


	def     _a	(self							):
				for timesteps in [100, 1000]:
							self.check_over_configs(num_train_timesteps=lowercase							)


	def     _a	(self							):
				for steps_offset in [0, 1]:
							self.check_over_configs(steps_offset=lowercase							)

				A_	:						Optional[Any]	=     self.scheduler_classes[0]
				A_	:						List[str]	=     self.get_scheduler_config(steps_offset=1							)
				A_	:						List[str]	=     scheduler_class(**lowercase							)
				scheduler.set_timesteps(10							)
				assert torch.equal(
				    scheduler.timesteps       ,      torch.LongTensor(
				        [901, 851, 851, 801, 801, 751, 751, 701, 701, 651, 651, 601, 601, 501, 401, 301, 201, 101, 1]							)       ,      )


	def     _a	(self							):
				for beta_start, beta_end in zip([0.00_01, 0.0_01]       ,      [0.0_02, 0.02]							):
							self.check_over_configs(beta_start=lowercase       ,      beta_end=lowercase							)


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


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


	def     _a	(self							):
				for t in [1, 5, 10]:
							self.check_over_forward(time_step=lowercase							)


	def     _a	(self							):
				for t, num_inference_steps in zip([1, 5, 10]       ,      [10, 50, 100]							):
							self.check_over_forward(num_inference_steps=lowercase							)


	def     _a	(self							):
				# earlier version of set_timesteps() caused an error indexing alpha's with inference steps as power of 3
				A_	:						List[str]	=     27

				for scheduler_class in self.scheduler_classes:
							A_	:						Tuple	=     self.dummy_sample
							A_	:						Optional[int]	=     0.1 * sample

							A_	:						List[Any]	=     self.get_scheduler_config()
							A_	:						Dict	=     scheduler_class(**lowercase							)

							scheduler.set_timesteps(lowercase							)

							# before power of 3 fix, would error on first step, so we only need to do two
							for i, t in enumerate(scheduler.prk_timesteps[:2]							):
										A_	:						int	=     scheduler.step_prk(lowercase       ,      lowercase       ,      lowercase							).prev_sample


	def     _a	(self							):
				with self.assertRaises(lowercase							):
							A_	:						List[Any]	=     self.scheduler_classes[0]
							A_	:						Dict	=     self.get_scheduler_config()
							A_	:						List[Any]	=     scheduler_class(**lowercase							)

							scheduler.step_plms(self.dummy_sample       ,      1       ,      self.dummy_sample							).prev_sample


	def     _a	(self							):
				A_	:						Dict	=     self.full_loop()
				A_	:						Optional[int]	=     torch.sum(torch.abs(lowercase							)							)
				A_	:						List[Any]	=     torch.mean(torch.abs(lowercase							)							)

				assert abs(result_sum.item() - 1_98.13_18							) < 1E-2
				assert abs(result_mean.item() - 0.25_80							) < 1E-3


	def     _a	(self							):
				A_	:						str	=     self.full_loop(prediction_type="""v_prediction"""							)
				A_	:						Union[str, Any]	=     torch.sum(torch.abs(lowercase							)							)
				A_	:						Union[str, Any]	=     torch.mean(torch.abs(lowercase							)							)

				assert abs(result_sum.item() - 67.39_86							) < 1E-2
				assert abs(result_mean.item() - 0.08_78							) < 1E-3


	def     _a	(self							):
				# We specify different beta, so that the first alpha is 0.99
				A_	:						Any	=     self.full_loop(set_alpha_to_one=lowercase       ,      beta_start=0.01							)
				A_	:						Dict	=     torch.sum(torch.abs(lowercase							)							)
				A_	:						str	=     torch.mean(torch.abs(lowercase							)							)

				assert abs(result_sum.item() - 2_30.03_99							) < 1E-2
				assert abs(result_mean.item() - 0.29_95							) < 1E-3


	def     _a	(self							):
				# We specify different beta, so that the first alpha is 0.99
				A_	:						Union[str, Any]	=     self.full_loop(set_alpha_to_one=lowercase       ,      beta_start=0.01							)
				A_	:						Any	=     torch.sum(torch.abs(lowercase							)							)
				A_	:						str	=     torch.mean(torch.abs(lowercase							)							)

				assert abs(result_sum.item() - 1_86.94_82							) < 1E-2
				assert abs(result_mean.item() - 0.24_34							) < 1E-3




'''simple docstring'''




from typing import List, Union

import numpy as np

from ..tokenization_utils import TruncationStrategy
from ..utils import add_end_docstrings, logging
from .base import PIPELINE_INIT_ARGS, ArgumentHandler, ChunkPipeline


lowerCamelCase							:Tuple				    =					logging.get_logger(__name__)







class 	_lowerCAmelCase							(			__UpperCAmelCase ):


	def     _a	(self       ,      lowercase							):
				if isinstance(lowercase       ,      lowercase							):
							A_	:						Tuple	=     [label.strip() for label in labels.split(""","""							) if label.strip()]
				return labels


	def __call__(self       ,      lowercase       ,      lowercase       ,      lowercase							):
				if len(lowercase							) == 0 or len(lowercase							) == 0:
							raise ValueError("""You must include at least one label and at least one sequence."""							)
				if hypothesis_template.format(labels[0]							) == hypothesis_template:
							raise ValueError(
							    (
							        """The provided hypothesis_template \"{}\" was not able to be formatted with the target labels. """
							        """Make sure the passed template includes formatting syntax such as {{}} where the label should go."""
							    ).format(lowercase							)							)

				if isinstance(lowercase       ,      lowercase							):
							A_	:						Optional[Any]	=     [sequences]

				A_	:						List[Any]	=     []
				for sequence in sequences:
							sequence_pairs.extend([[sequence, hypothesis_template.format(lowercase							)] for label in labels]							)

				return sequence_pairs, sequences







@add_end_docstrings(__UpperCAmelCase )
class 	_lowerCAmelCase							(			__UpperCAmelCase ):


	def __init__(self       ,      lowercase=ZeroShotClassificationArgumentHandler()       ,      *lowercase       ,      **lowercase							):
				A_	:						str	=     args_parser
				super().__init__(*lowercase       ,      **lowercase							)
				if self.entailment_id == -1:
							logger.warning(
							    """Failed to determine 'entailment' label id from the label2id mapping in the model config. Setting to """
							    """-1. Define a descriptive label2id mapping in the model config to ensure correct outputs."""							)


	@property
	def     _a	(self							):
				for label, ind in self.model.config.labelaid.items():
							if label.lower().startswith("""entail"""							):
										return ind
				return -1


	def     _a	(self       ,      lowercase       ,      lowercase=True       ,      lowercase=True       ,      lowercase=TruncationStrategy.ONLY_FIRST       ,      **lowercase							):
				A_	:						Optional[int]	=     self.framework
				if self.tokenizer.pad_token is None:
							# Override for tokenizers not supporting padding
							logger.error(
							    """Tokenizer was not supporting padding necessary for zero-shot, attempting to use """
							    """ `pad_token=eos_token`"""							)
							A_	:						Any	=     self.tokenizer.eos_token
				try:
							A_	:						Optional[Any]	=     self.tokenizer(
							    lowercase       ,      add_special_tokens=lowercase       ,      return_tensors=lowercase       ,      padding=lowercase       ,      truncation=lowercase       ,      )
				except Exception as e:
							if "too short" in str(lowercase							):
										# tokenizers might yell that we want to truncate
										# to a value that is not even reached by the input.
										# In that case we don't want to truncate.
										# It seems there's not a really better way to catch that
										# exception.

										A_	:						str	=     self.tokenizer(
										    lowercase       ,      add_special_tokens=lowercase       ,      return_tensors=lowercase       ,      padding=lowercase       ,      truncation=TruncationStrategy.DO_NOT_TRUNCATE       ,      )
							else:
										raise e

				return inputs


	def     _a	(self       ,      **lowercase							):
				if kwargs.get("""multi_class"""       ,      lowercase							) is not None:
							A_	:						Union[str, Any]	=     kwargs["""multi_class"""]
							logger.warning(
							    """The `multi_class` argument has been deprecated and renamed to `multi_label`. """
							    """`multi_class` will be removed in a future version of Transformers."""							)
				A_	:						Tuple	=     {}
				if "candidate_labels" in kwargs:
							A_	:						Optional[int]	=     self._args_parser._parse_labels(kwargs["""candidate_labels"""]							)
				if "hypothesis_template" in kwargs:
							A_	:						List[str]	=     kwargs["""hypothesis_template"""]

				A_	:						Union[str, Any]	=     {}
				if "multi_label" in kwargs:
							A_	:						Optional[int]	=     kwargs["""multi_label"""]
				return preprocess_params, {}, postprocess_params


	def __call__(self       ,      lowercase       ,      *lowercase       ,      **lowercase       ,      ):
				if len(lowercase							) == 0:
							pass
				elif len(lowercase							) == 1 and "candidate_labels" not in kwargs:
							A_	:						Any	=     args[0]
				else:
							raise ValueError(F'Unable to understand extra arguments {args}'							)

				return super().__call__(lowercase       ,      **lowercase							)


	def     _a	(self       ,      lowercase       ,      lowercase=None       ,      lowercase="This example is {}."							):
				A_,    A_	:						Any	=     self._args_parser(lowercase       ,      lowercase       ,      lowercase							)

				for i, (candidate_label, sequence_pair) in enumerate(zip(lowercase       ,      lowercase							)							):
							A_	:						List[Any]	=     self._parse_and_tokenize([sequence_pair]							)

							yield {
							    "candidate_label": candidate_label,
							    "sequence": sequences[0],
							    "is_last": i == len(lowercase							) - 1,
							    **model_input,
							}


	def     _a	(self       ,      lowercase							):
				A_	:						Dict	=     inputs["""candidate_label"""]
				A_	:						Any	=     inputs["""sequence"""]
				A_	:						List[Any]	=     {k: inputs[k] for k in self.tokenizer.model_input_names}
				A_	:						Optional[int]	=     self.model(**lowercase							)

				A_	:						Optional[Any]	=     {
				    """candidate_label""": candidate_label,
				    """sequence""": sequence,
				    """is_last""": inputs["""is_last"""],
				    **outputs,
				}
				return model_outputs


	def     _a	(self       ,      lowercase       ,      lowercase=False							):
				A_	:						Tuple	=     [outputs["""candidate_label"""] for outputs in model_outputs]
				A_	:						Optional[int]	=     [outputs["""sequence"""] for outputs in model_outputs]
				A_	:						Union[str, Any]	=     np.concatenate([output["""logits"""].numpy() for output in model_outputs]							)
				A_	:						List[str]	=     logits.shape[0]
				A_	:						Optional[int]	=     len(lowercase							)
				A_	:						int	=     N // n
				A_	:						int	=     logits.reshape((num_sequences, n, -1)							)

				if multi_label or len(lowercase							) == 1:
							# softmax over the entailment vs. contradiction dim for each label independently
							A_	:						Dict	=     self.entailment_id
							A_	:						Dict	=     -1 if entailment_id == 0 else 0
							A_	:						str	=     reshaped_outputs[..., [contradiction_id, entailment_id]]
							A_	:						Optional[Any]	=     np.exp(lowercase							) / np.exp(lowercase							).sum(-1       ,      keepdims=lowercase							)
							A_	:						Tuple	=     scores[..., 1]
				else:
							# softmax the "entailment" logits over all candidate labels
							A_	:						Optional[int]	=     reshaped_outputs[..., self.entailment_id]
							A_	:						Any	=     np.exp(lowercase							) / np.exp(lowercase							).sum(-1       ,      keepdims=lowercase							)

				A_	:						List[str]	=     list(reversed(scores[0].argsort()							)							)
				return {
				    "sequence": sequences[0],
				    "labels": [candidate_labels[i] for i in top_inds],
				    "scores": scores[0, top_inds].tolist(),
				}