



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


UpperCamelCase__ =						logging.get_logger(__name__)
class 			_lowerCAmelCase   ( _UpperCAmelCase			):






   """simple docstring"""





   def 		snake_case__			(				self	:       Union[str, Any]			,     lowercase	:       int   )						->					str:
         """simple docstring"""







         if isinstance(lowercase			,     lowercase   ):
               __lowercase					=				[label.strip() for label in labels.split(""","""   ) if label.strip()]
         return labels







   def __call__(				self	:       List[Any]			,     lowercase	:       str			,     lowercase	:       Any			,     lowercase	:       Optional[int]   )						->					Union[str, Any]:
         """simple docstring"""







         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   ):
               __lowercase					=				[sequences]

         __lowercase					=				[]
         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			):






   """simple docstring"""





   def __init__(				self	:       List[Any]			,     lowercase	:       Tuple=ZeroShotClassificationArgumentHandler()			,     *lowercase	:       List[str]			,     **lowercase	:       Any   )						->					Optional[int]:
         """simple docstring"""







         __lowercase					=				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 		snake_case__			(				self	:       List[str]   )						->					List[Any]:
         """simple docstring"""







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





   def 		snake_case__			(				self	:       int			,     lowercase	:       Optional[Any]			,     lowercase	:       Any=True			,     lowercase	:       Tuple=True			,     lowercase	:       Tuple=TruncationStrategy.ONLY_FIRST			,     **lowercase	:       str   )						->					str:
         """simple docstring"""







         __lowercase					=				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`"""   )
               __lowercase					=				self.tokenizer.eos_token
         try:
               __lowercase					=				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.

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

         return inputs





   def 		snake_case__			(				self	:       Tuple			,     **lowercase	:       Dict   )						->					Dict:
         """simple docstring"""







         if kwargs.get("""multi_class"""			,     lowercase   ) is not None:
               __lowercase					=				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."""   )
         __lowercase					=				{}
         if "candidate_labels" in kwargs:
               __lowercase					=				self._args_parser._parse_labels(kwargs["""candidate_labels"""]   )
         if "hypothesis_template" in kwargs:
               __lowercase					=				kwargs["""hypothesis_template"""]

         __lowercase					=				{}
         if "multi_label" in kwargs:
               __lowercase					=				kwargs["""multi_label"""]
         return preprocess_params, {}, postprocess_params





   def __call__(				self	:       Dict			,     lowercase	:       Union[str, List[str]]			,     *lowercase	:       List[Any]			,     **lowercase	:       Dict			,     )						->					Union[str, Any]:
         """simple docstring"""







         if len(lowercase   ) == 0:
               pass
         elif len(lowercase   ) == 1 and "candidate_labels" not in kwargs:
               __lowercase					=				args[0]
         else:
               raise ValueError(F"Unable to understand extra arguments {args}"   )

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





   def 		snake_case__			(				self	:       List[str]			,     lowercase	:       int			,     lowercase	:       Tuple=None			,     lowercase	:       Tuple="This example is {}."   )						->					Union[str, Any]:
         """simple docstring"""







         __lowercase	,			__lowercase					=				self._args_parser(lowercase			,     lowercase			,     lowercase   )

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

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





   def 		snake_case__			(				self	:       Tuple			,     lowercase	:       Optional[int]   )						->					Optional[Any]:
         """simple docstring"""







         __lowercase					=				inputs["""candidate_label"""]
         __lowercase					=				inputs["""sequence"""]
         __lowercase					=				{k: inputs[k] for k in self.tokenizer.model_input_names}
         __lowercase					=				self.model(**lowercase   )

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







   def 		snake_case__			(				self	:       List[str]			,     lowercase	:       Union[str, Any]			,     lowercase	:       List[str]=False   )						->					Tuple:
         """simple docstring"""







         __lowercase					=				[outputs["""candidate_label"""] for outputs in model_outputs]
         __lowercase					=				[outputs["""sequence"""] for outputs in model_outputs]
         __lowercase					=				np.concatenate([output["""logits"""].numpy() for output in model_outputs]   )
         __lowercase					=				logits.shape[0]
         __lowercase					=				len(lowercase   )
         __lowercase					=				N // n
         __lowercase					=				logits.reshape((num_sequences, n, -1)   )

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

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










import random
import unittest

import torch

from diffusers import IFImgaImgSuperResolutionPipeline
from diffusers.utils import floats_tensor
from diffusers.utils.import_utils import is_xformers_available
from diffusers.utils.testing_utils import skip_mps, torch_device

from ..pipeline_params import TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_PARAMS
from ..test_pipelines_common import PipelineTesterMixin
from . import IFPipelineTesterMixin
@skip_mps
class 			_lowerCAmelCase   ( _UpperCAmelCase    ,					_UpperCAmelCase    ,					unittest.TestCase			):






			"""simple docstring"""





			lowercase__      :  int      =			IFImgaImgSuperResolutionPipeline
			lowercase__      :  Optional[int]      =			TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {"""width""", """height"""}
			lowercase__      :  Optional[int]      =			TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS.union({"""original_image"""}			)
			lowercase__      :  Tuple      =			PipelineTesterMixin.required_optional_params - {"""latents"""}





			def 		snake_case__			(				self	:       Tuple   )						->					Any:
									"""simple docstring"""







									return self._get_superresolution_dummy_components()





			def 		snake_case__			(				self	:       List[str]			,     lowercase	:       Optional[int]			,     lowercase	:       Optional[Any]=0   )						->					Union[str, Any]:
									"""simple docstring"""







									if str(lowercase   ).startswith("""mps"""   ):
															__lowercase					=				torch.manual_seed(lowercase   )
									else:
															__lowercase					=				torch.Generator(device=lowercase   ).manual_seed(lowercase   )

									__lowercase					=				floats_tensor((1, 3, 32, 32)			,     rng=random.Random(lowercase   )   ).to(lowercase   )
									__lowercase					=				floats_tensor((1, 3, 16, 16)			,     rng=random.Random(lowercase   )   ).to(lowercase   )

									__lowercase					=				{
									    """prompt""": """A painting of a squirrel eating a burger""",
									    """image""": image,
									    """original_image""": original_image,
									    """generator""": generator,
									    """num_inference_steps""": 2,
									    """output_type""": """numpy""",
									}

									return inputs





			@unittest.skipIf(
			    torch_device != """cuda""" or not is_xformers_available()			,     reason="""XFormers attention is only available with CUDA and `xformers` installed"""			,     )
			def 		snake_case__			(				self	:       Optional[Any]   )						->					Tuple:
									"""simple docstring"""







									self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1E-3   )





			def 		snake_case__			(				self	:       List[Any]   )						->					Union[str, Any]:
									"""simple docstring"""







									self._test_save_load_optional_components()





			@unittest.skipIf(torch_device != """cuda"""			,     reason="""float16 requires CUDA"""   )
			def 		snake_case__			(				self	:       List[Any]   )						->					Dict:
									"""simple docstring"""







									super().test_save_load_floataa(expected_max_diff=1E-1   )





			def 		snake_case__			(				self	:       Dict   )						->					int:
									"""simple docstring"""







									self._test_attention_slicing_forward_pass(expected_max_diff=1E-2   )





			def 		snake_case__			(				self	:       Optional[Any]   )						->					int:
									"""simple docstring"""







									self._test_save_load_local()







			def 		snake_case__			(				self	:       Optional[int]   )						->					Optional[int]:
									"""simple docstring"""







									self._test_inference_batch_single_identical(
									    expected_max_diff=1E-2			,     )





