


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    UpperCAmelCase__	( A_       ,  A_       ,  unittest.TestCase		):



    """simple docstring"""






    UpperCAmelCase__						:		Dict              =  IFImgaImgSuperResolutionPipeline
    UpperCAmelCase__						:		Optional[Any]              =  TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {"width", "height"}
    UpperCAmelCase__						:		Optional[int]              =  TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS.union({"original_image"}		)
    UpperCAmelCase__						:		Union[str, Any]              =  PipelineTesterMixin.required_optional_params - {"latents"}






    def _a (      self   )  ->	Optional[Any]:
         return self._get_superresolution_dummy_components()






    def _a (      self ,       A_ ,       A_=0   )  ->	Union[str, Any]:
         if str(A_   ).startswith('mps'   ):
              __UpperCamelCase   							=torch.manual_seed(A_   )
         else:
              __UpperCamelCase   							=torch.Generator(device=A_   ).manual_seed(A_   )

         __UpperCamelCase   							=floats_tensor((1, 3, 32, 32) ,       rng=random.Random(A_   )   ).to(A_   )
         __UpperCamelCase   							=floats_tensor((1, 3, 16, 16) ,       rng=random.Random(A_   )   ).to(A_   )

         __UpperCamelCase   							={
             '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 _a (      self   )  ->	Dict:
         self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1E-3   )






    def _a (      self   )  ->	str:
         self._test_save_load_optional_components()






    @unittest.skipIf(torch_device != 'cuda' ,       reason='float16 requires CUDA'   )
    def _a (      self   )  ->	Optional[int]:
         # Due to non-determinism in save load of the hf-internal-testing/tiny-random-t5 text encoder
         super().test_save_load_floataa(expected_max_diff=1E-1   )






    def _a (      self   )  ->	List[Any]:
         self._test_attention_slicing_forward_pass(expected_max_diff=1E-2   )






    def _a (      self   )  ->	Tuple:
         self._test_save_load_local()






    def _a (      self   )  ->	Tuple:
         self._test_inference_batch_single_identical(
             expected_max_diff=1E-2 ,       )









import warnings
from contextlib import contextmanager

from ....processing_utils import ProcessorMixin






class    UpperCAmelCase__	( A_		):



    """simple docstring"""






    UpperCAmelCase__						:		Optional[int]              =  "MCTCTFeatureExtractor"
    UpperCAmelCase__						:		str              =  "AutoTokenizer"






    def __init__(      self ,       A_ ,       A_   )  ->	Dict:
         super().__init__(A_ ,       A_   )
         __UpperCamelCase   							=self.feature_extractor
         __UpperCamelCase   							=False






    def __call__(      self ,       *A_ ,       **A_   )  ->	Optional[Any]:
         # For backward compatibility
         if self._in_target_context_manager:
              return self.current_processor(*A_ ,       **A_   )

         if "raw_speech" in kwargs:
              warnings.warn('Using `raw_speech` as a keyword argument is deprecated. Use `audio` instead.'   )
              __UpperCamelCase   							=kwargs.pop('raw_speech'   )
         else:
              __UpperCamelCase   							=kwargs.pop('audio' ,       A_   )
         __UpperCamelCase   							=kwargs.pop('sampling_rate' ,       A_   )
         __UpperCamelCase   							=kwargs.pop('text' ,       A_   )
         if len(A_   ) > 0:
              __UpperCamelCase   							=args[0]
              __UpperCamelCase   							=args[1:]

         if audio is None and text is None:
              raise ValueError('You need to specify either an `audio` or `text` input to process.'   )

         if audio is not None:
              __UpperCamelCase   							=self.feature_extractor(A_ ,       *A_ ,       sampling_rate=A_ ,       **A_   )
         if text is not None:
              __UpperCamelCase   							=self.tokenizer(A_ ,       **A_   )

         if text is None:
              return inputs
         elif audio is None:
              return encodings
         else:
              __UpperCamelCase   							=encodings['input_ids']
              return inputs






    def _a (      self ,       *A_ ,       **A_   )  ->	str:
         return self.tokenizer.batch_decode(*A_ ,       **A_   )






    def _a (      self ,       *A_ ,       **A_   )  ->	List[Any]:
         # For backward compatibility
         if self._in_target_context_manager:
              return self.current_processor.pad(*A_ ,       **A_   )

         __UpperCamelCase   							=kwargs.pop('input_features' ,       A_   )
         __UpperCamelCase   							=kwargs.pop('labels' ,       A_   )
         if len(A_   ) > 0:
              __UpperCamelCase   							=args[0]
              __UpperCamelCase   							=args[1:]

         if input_features is not None:
              __UpperCamelCase   							=self.feature_extractor.pad(A_ ,       *A_ ,       **A_   )
         if labels is not None:
              __UpperCamelCase   							=self.tokenizer.pad(A_ ,       **A_   )

         if labels is None:
              return input_features
         elif input_features is None:
              return labels
         else:
              __UpperCamelCase   							=labels['input_ids']
              return input_features






    def _a (      self ,       *A_ ,       **A_   )  ->	Optional[int]:
         return self.tokenizer.decode(*A_ ,       **A_   )






    @contextmanager
    def _a (      self   )  ->	str:
         warnings.warn(
             '`as_target_processor` is deprecated and will be removed in v5 of Transformers. You can process your '
             'labels by using the argument `text` of the regular `__call__` method (either in the same call as '
             'your audio inputs, or in a separate call.'   )
         __UpperCamelCase   							=True
         __UpperCamelCase   							=self.tokenizer
         yield
         __UpperCamelCase   							=self.feature_extractor
         __UpperCamelCase   							=False





