'''simple docstring'''






import gc
import random
import unittest

import numpy as np
import torch
from transformers import (
    CLIPImageProcessor,
    CLIPTextConfig,
    CLIPTextModel,
    CLIPTokenizer,
    CLIPVisionConfig,
    CLIPVisionModelWithProjection,
)

from diffusers import AutoencoderKL, DDIMScheduler, DDPMScheduler, StableUnCLIPImgaImgPipeline, UNetaDConditionModel
from diffusers.pipelines.pipeline_utils import DiffusionPipeline
from diffusers.pipelines.stable_diffusion.stable_unclip_image_normalizer import StableUnCLIPImageNormalizer
from diffusers.utils.import_utils import is_xformers_available
from diffusers.utils.testing_utils import (
    enable_full_determinism,
    floats_tensor,
    load_image,
    load_numpy,
    require_torch_gpu,
    skip_mps,
    slow,
    torch_device,
)

from ..pipeline_params import TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_PARAMS
from ..test_pipelines_common import (
    PipelineKarrasSchedulerTesterMixin,
    PipelineLatentTesterMixin,
    PipelineTesterMixin,
    assert_mean_pixel_difference,
)


enable_full_determinism()







class lowercase_       (		_UpperCamelCase    ,    _UpperCamelCase    ,    _UpperCamelCase    ,    unittest.TestCase			):







   """simple docstring"""

   __lowerCAmelCase         =   StableUnCLIPImgaImgPipeline
   __lowerCAmelCase         =   TEXT_GUIDED_IMAGE_VARIATION_PARAMS
   __lowerCAmelCase         =   TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS
   __lowerCAmelCase         =   frozenset(
       []			)  # TO-DO: update image_params once pipeline is refactored with VaeImageProcessor.preprocess
   __lowerCAmelCase         =   frozenset([]			)
   def 	__UpperCAmelCase   (      self      :			Optional[int]     )  ->			Optional[Any]:
        _A    		=  32
        _A    		=  embedder_hidden_size

        # image encoding components

        _A    		=  CLIPImageProcessor(crop_size=32, size=32     )

        torch.manual_seed(0     )
        _A    		=  CLIPVisionModelWithProjection(
            CLIPVisionConfig(
                hidden_size=UpperCamelCase__, projection_dim=UpperCamelCase__, num_hidden_layers=5, num_attention_heads=4, image_size=32, intermediate_size=37, patch_size=1, )     )

        # regular denoising components

        torch.manual_seed(0     )
        _A    		=  StableUnCLIPImageNormalizer(embedding_dim=UpperCamelCase__     )
        _A    		=  DDPMScheduler(beta_schedule='squaredcos_cap_v2'     )

        torch.manual_seed(0     )
        _A    		=  CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip'     )

        torch.manual_seed(0     )
        _A    		=  CLIPTextModel(
            CLIPTextConfig(
                bos_token_id=0, eos_token_id=2, hidden_size=UpperCamelCase__, projection_dim=32, intermediate_size=37, layer_norm_eps=1e-05, num_attention_heads=4, num_hidden_layers=5, pad_token_id=1, vocab_size=10_00, )     )

        torch.manual_seed(0     )
        _A    		=  UNetaDConditionModel(
            sample_size=32, in_channels=4, out_channels=4, down_block_types=('CrossAttnDownBlock2D', 'DownBlock2D'), up_block_types=('UpBlock2D', 'CrossAttnUpBlock2D'), block_out_channels=(32, 64), attention_head_dim=(2, 4), class_embed_type='projection', projection_class_embeddings_input_dim=embedder_projection_dim * 2, cross_attention_dim=UpperCamelCase__, layers_per_block=1, upcast_attention=UpperCamelCase__, use_linear_projection=UpperCamelCase__, )

        torch.manual_seed(0     )
        _A    		=  DDIMScheduler(
            beta_schedule='scaled_linear', beta_start=0.00_085, beta_end=0.012, prediction_type='v_prediction', set_alpha_to_one=UpperCamelCase__, steps_offset=1, )

        torch.manual_seed(0     )
        _A    		=  AutoencoderKL()

        _A    		=  {
            # image encoding components
            'feature_extractor': feature_extractor,
            'image_encoder': image_encoder.eval(),
            # image noising components
            'image_normalizer': image_normalizer.eval(),
            'image_noising_scheduler': image_noising_scheduler,
            # regular denoising components
            'tokenizer': tokenizer,
            'text_encoder': text_encoder.eval(),
            'unet': unet.eval(),
            'scheduler': scheduler,
            'vae': vae.eval(),
        }

        return components
   def 	__UpperCAmelCase   (      self      :			List[str], UpperCamelCase__      :			str, UpperCamelCase__      :			List[Any]=0, UpperCamelCase__      :			Optional[int]=True     )  ->			Tuple:
        if str(UpperCamelCase__     ).startswith('mps'     ):
             _A    		=  torch.manual_seed(UpperCamelCase__     )
        else:
             _A    		=  torch.Generator(device=UpperCamelCase__     ).manual_seed(UpperCamelCase__     )

        _A    		=  floats_tensor((1, 3, 32, 32), rng=random.Random(UpperCamelCase__     )     ).to(UpperCamelCase__     )

        if pil_image:
             _A    		=  input_image * 0.5 + 0.5
             _A    		=  input_image.clamp(0, 1     )
             _A    		=  input_image.cpu().permute(0, 2, 3, 1     ).float().numpy()
             _A    		=  DiffusionPipeline.numpy_to_pil(UpperCamelCase__     )[0]

        return {
            "prompt": "An anime racoon running a marathon",
            "image": input_image,
            "generator": generator,
            "num_inference_steps": 2,
            "output_type": "np",
        }
   @skip_mps
   def 	__UpperCAmelCase   (      self      :			str     )  ->			Any:
        _A    		=  'cpu'  # ensure determinism for the device-dependent torch.Generator
        _A    		=  self.get_dummy_components()
        _A    		=  StableUnCLIPImgaImgPipeline(**UpperCamelCase__     )
        _A    		=  sd_pipe.to(UpperCamelCase__     )
        sd_pipe.set_progress_bar_config(disable=UpperCamelCase__     )

        _A    		=  self.get_dummy_inputs(UpperCamelCase__     )
        inputs.update({'image_embeds': None}     )
        _A    		=  sd_pipe(**UpperCamelCase__     ).images
        _A    		=  image[0, -3:, -3:, -1]

        assert image.shape == (1, 32, 32, 3)
        _A    		=  np.array([0.3_872, 0.7_224, 0.5_601, 0.4_741, 0.6_872, 0.5_814, 0.4_636, 0.3_867, 0.5_078]     )

        assert np.abs(image_slice.flatten() - expected_slice     ).max() < 1e-3
   def 	__UpperCAmelCase   (      self      :			int     )  ->			int:
        _A    		=  torch_device in ['cpu', 'mps']

        self._test_attention_slicing_forward_pass(test_max_difference=UpperCamelCase__     )
   def 	__UpperCAmelCase   (      self      :			int     )  ->			List[Any]:
        _A    		=  torch_device in ['cpu', 'mps']

        self._test_inference_batch_single_identical(test_max_difference=UpperCamelCase__     )

   @unittest.skipIf(
       torch_device != 'cuda' or not is_xformers_available(), reason='XFormers attention is only available with CUDA and `xformers` installed', )
   def 	__UpperCAmelCase   (      self      :			Any     )  ->			Any:
        self._test_xformers_attention_forwardGenerator_pass(test_max_difference=UpperCamelCase__     )







@slow
@require_torch_gpu
class lowercase_       (		unittest.TestCase			):







   """simple docstring"""

   def 	__UpperCAmelCase   (      self      :			List[str]     )  ->			Optional[int]:
        # clean up the VRAM after each test
        super().tearDown()
        gc.collect()
        torch.cuda.empty_cache()
   def 	__UpperCAmelCase   (      self      :			str     )  ->			int:
        _A    		=  load_image(
            'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/stable_unclip/turtle.png'     )

        _A    		=  load_numpy(
            'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/stable_unclip/stable_unclip_2_1_l_img2img_anime_turtle_fp16.npy'     )

        _A    		=  StableUnCLIPImgaImgPipeline.from_pretrained(
            'fusing/stable-unclip-2-1-l-img2img', torch_dtype=torch.floataa     )
        pipe.to(UpperCamelCase__     )
        pipe.set_progress_bar_config(disable=UpperCamelCase__     )
        # stable unclip will oom when integration tests are run on a V100,
        # so turn on memory savings
        pipe.enable_attention_slicing()
        pipe.enable_sequential_cpu_offload()

        _A    		=  torch.Generator(device='cpu'     ).manual_seed(0     )
        _A    		=  pipe(UpperCamelCase__, 'anime turle', generator=UpperCamelCase__, output_type='np'     )

        _A    		=  output.images[0]

        assert image.shape == (7_68, 7_68, 3)

        assert_mean_pixel_difference(UpperCamelCase__, UpperCamelCase__     )
   def 	__UpperCAmelCase   (      self      :			List[str]     )  ->			Tuple:
        _A    		=  load_image(
            'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/stable_unclip/turtle.png'     )

        _A    		=  load_numpy(
            'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/stable_unclip/stable_unclip_2_1_h_img2img_anime_turtle_fp16.npy'     )

        _A    		=  StableUnCLIPImgaImgPipeline.from_pretrained(
            'fusing/stable-unclip-2-1-h-img2img', torch_dtype=torch.floataa     )
        pipe.to(UpperCamelCase__     )
        pipe.set_progress_bar_config(disable=UpperCamelCase__     )
        # stable unclip will oom when integration tests are run on a V100,
        # so turn on memory savings
        pipe.enable_attention_slicing()
        pipe.enable_sequential_cpu_offload()

        _A    		=  torch.Generator(device='cpu'     ).manual_seed(0     )
        _A    		=  pipe(UpperCamelCase__, 'anime turle', generator=UpperCamelCase__, output_type='np'     )

        _A    		=  output.images[0]

        assert image.shape == (7_68, 7_68, 3)

        assert_mean_pixel_difference(UpperCamelCase__, UpperCamelCase__     )

   def 	__UpperCAmelCase   (      self      :			Optional[Any]     )  ->			Optional[Any]:
        _A    		=  load_image(
            'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/stable_unclip/turtle.png'     )

        torch.cuda.empty_cache()
        torch.cuda.reset_max_memory_allocated()
        torch.cuda.reset_peak_memory_stats()

        _A    		=  StableUnCLIPImgaImgPipeline.from_pretrained(
            'fusing/stable-unclip-2-1-h-img2img', torch_dtype=torch.floataa     )
        _A    		=  pipe.to(UpperCamelCase__     )
        pipe.set_progress_bar_config(disable=UpperCamelCase__     )
        pipe.enable_attention_slicing()
        pipe.enable_sequential_cpu_offload()

        _A    		=  pipe(
            UpperCamelCase__, 'anime turtle', num_inference_steps=2, output_type='np', )

        _A    		=  torch.cuda.max_memory_allocated()
        # make sure that less than 7 GB is allocated
        assert mem_bytes < 7 * 10**9




'''simple docstring'''






from __future__ import annotations

import unittest

from transformers import is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
   import numpy
   import tensorflow as tf

   from transformers import (
       TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
       TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
       TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST,
       BertConfig,
       DPRConfig,
       TFDPRContextEncoder,
       TFDPRQuestionEncoder,
       TFDPRReader,
   )







class lowercase_       :







   """simple docstring"""

   def __init__(      self      :			Optional[int], UpperCamelCase__      :			Dict, UpperCamelCase__      :			Dict=13, UpperCamelCase__      :			Optional[Any]=7, UpperCamelCase__      :			List[str]=True, UpperCamelCase__      :			Union[str, Any]=True, UpperCamelCase__      :			Optional[int]=True, UpperCamelCase__      :			Optional[Any]=True, UpperCamelCase__      :			Dict=99, UpperCamelCase__      :			Dict=32, UpperCamelCase__      :			Any=2, UpperCamelCase__      :			Optional[int]=4, UpperCamelCase__      :			Tuple=37, UpperCamelCase__      :			Union[str, Any]="gelu", UpperCamelCase__      :			Optional[Any]=0.1, UpperCamelCase__      :			Any=0.1, UpperCamelCase__      :			Union[str, Any]=5_12, UpperCamelCase__      :			Optional[Any]=16, UpperCamelCase__      :			List[str]=2, UpperCamelCase__      :			List[Any]=0.02, UpperCamelCase__      :			List[str]=3, UpperCamelCase__      :			Optional[Any]=4, UpperCamelCase__      :			Optional[Any]=None, UpperCamelCase__      :			Union[str, Any]=0, )  ->			str:
        _A    		=  parent
        _A    		=  batch_size
        _A    		=  seq_length
        _A    		=  is_training
        _A    		=  use_input_mask
        _A    		=  use_token_type_ids
        _A    		=  use_labels
        _A    		=  vocab_size
        _A    		=  hidden_size
        _A    		=  num_hidden_layers
        _A    		=  num_attention_heads
        _A    		=  intermediate_size
        _A    		=  hidden_act
        _A    		=  hidden_dropout_prob
        _A    		=  attention_probs_dropout_prob
        _A    		=  max_position_embeddings
        _A    		=  type_vocab_size
        _A    		=  type_sequence_label_size
        _A    		=  initializer_range
        _A    		=  num_labels
        _A    		=  num_choices
        _A    		=  scope
        _A    		=  projection_dim
   def 	__UpperCAmelCase   (      self      :			Any     )  ->			Optional[Any]:
        _A    		=  ids_tensor([self.batch_size, self.seq_length], self.vocab_size     )

        _A    		=  None
        if self.use_input_mask:
             # follow test_modeling_tf_ctrl.py
             _A    		=  random_attention_mask([self.batch_size, self.seq_length]     )

        _A    		=  None
        if self.use_token_type_ids:
             _A    		=  ids_tensor([self.batch_size, self.seq_length], self.type_vocab_size     )

        _A    		=  None
        _A    		=  None
        _A    		=  None
        if self.use_labels:
             _A    		=  ids_tensor([self.batch_size], self.type_sequence_label_size     )
             _A    		=  ids_tensor([self.batch_size, self.seq_length], self.num_labels     )
             _A    		=  ids_tensor([self.batch_size], self.num_choices     )

        _A    		=  BertConfig(
            vocab_size=self.vocab_size, hidden_size=self.hidden_size, num_hidden_layers=self.num_hidden_layers, num_attention_heads=self.num_attention_heads, intermediate_size=self.intermediate_size, hidden_act=self.hidden_act, hidden_dropout_prob=self.hidden_dropout_prob, attention_probs_dropout_prob=self.attention_probs_dropout_prob, max_position_embeddings=self.max_position_embeddings, type_vocab_size=self.type_vocab_size, is_decoder=UpperCamelCase__, initializer_range=self.initializer_range, )
        _A    		=  DPRConfig(projection_dim=self.projection_dim, **config.to_dict()     )

        return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
   def 	__UpperCAmelCase   (      self      :			Tuple, UpperCamelCase__      :			Any, UpperCamelCase__      :			Any, UpperCamelCase__      :			Tuple, UpperCamelCase__      :			List[Any], UpperCamelCase__      :			Any, UpperCamelCase__      :			List[Any], UpperCamelCase__      :			Union[str, Any]     )  ->			int:
        _A    		=  TFDPRContextEncoder(config=UpperCamelCase__     )
        _A    		=  model(UpperCamelCase__, attention_mask=UpperCamelCase__, token_type_ids=UpperCamelCase__     )
        _A    		=  model(UpperCamelCase__, token_type_ids=UpperCamelCase__     )
        _A    		=  model(UpperCamelCase__     )
        self.parent.assertEqual(result.pooler_output.shape, (self.batch_size, self.projection_dim or self.hidden_size)     )
   def 	__UpperCAmelCase   (      self      :			Dict, UpperCamelCase__      :			int, UpperCamelCase__      :			List[Any], UpperCamelCase__      :			List[str], UpperCamelCase__      :			List[Any], UpperCamelCase__      :			Tuple, UpperCamelCase__      :			str, UpperCamelCase__      :			str     )  ->			int:
        _A    		=  TFDPRQuestionEncoder(config=UpperCamelCase__     )
        _A    		=  model(UpperCamelCase__, attention_mask=UpperCamelCase__, token_type_ids=UpperCamelCase__     )
        _A    		=  model(UpperCamelCase__, token_type_ids=UpperCamelCase__     )
        _A    		=  model(UpperCamelCase__     )
        self.parent.assertEqual(result.pooler_output.shape, (self.batch_size, self.projection_dim or self.hidden_size)     )
   def 	__UpperCAmelCase   (      self      :			int, UpperCamelCase__      :			Union[str, Any], UpperCamelCase__      :			List[str], UpperCamelCase__      :			Tuple, UpperCamelCase__      :			List[str], UpperCamelCase__      :			Tuple, UpperCamelCase__      :			Optional[int], UpperCamelCase__      :			Optional[int]     )  ->			Any:
        _A    		=  TFDPRReader(config=UpperCamelCase__     )
        _A    		=  model(UpperCamelCase__, attention_mask=UpperCamelCase__     )

        self.parent.assertEqual(result.start_logits.shape, (self.batch_size, self.seq_length)     )
        self.parent.assertEqual(result.end_logits.shape, (self.batch_size, self.seq_length)     )
        self.parent.assertEqual(result.relevance_logits.shape, (self.batch_size,)     )

   def 	__UpperCAmelCase   (      self      :			Dict     )  ->			Dict:
        _A    		=  self.prepare_config_and_inputs()
        (
            (
            _A
        )		,       (
            _A
        )		,       (
            _A
        )		,       (
            _A
        )		,       (
            _A
        )		,       (
            _A
        )		,       (
            _A
        )		,       
        )    		=  config_and_inputs
        _A    		=  {'input_ids': input_ids}
        return config, inputs_dict







@require_tf
class lowercase_       (		_UpperCamelCase    ,    _UpperCamelCase    ,    unittest.TestCase			):







   """simple docstring"""

   __lowerCAmelCase         =   (
       (
           TFDPRContextEncoder,
           TFDPRQuestionEncoder,
           TFDPRReader,
       )
       if is_tf_available()
       else ()
   )
   __lowerCAmelCase         =   {"feature-extraction": TFDPRQuestionEncoder} if is_tf_available() else {}

   __lowerCAmelCase         =   False
   __lowerCAmelCase         =   False
   __lowerCAmelCase         =   False
   __lowerCAmelCase         =   False
   __lowerCAmelCase         =   False
   def 	__UpperCAmelCase   (      self      :			Optional[int]     )  ->			Union[str, Any]:
        _A    		=  TFDPRModelTester(self     )
        _A    		=  ConfigTester(self, config_class=UpperCamelCase__, hidden_size=37     )
   def 	__UpperCAmelCase   (      self      :			List[str]     )  ->			Union[str, Any]:
        self.config_tester.run_common_tests()
   def 	__UpperCAmelCase   (      self      :			List[str]     )  ->			List[str]:
        _A    		=  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_dpr_context_encoder(*UpperCamelCase__     )
   def 	__UpperCAmelCase   (      self      :			int     )  ->			List[str]:
        _A    		=  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_dpr_question_encoder(*UpperCamelCase__     )
   def 	__UpperCAmelCase   (      self      :			int     )  ->			Tuple:
        _A    		=  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_dpr_reader(*UpperCamelCase__     )

   @slow
   def 	__UpperCAmelCase   (      self      :			List[Any]     )  ->			Union[str, Any]:
        for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             _A    		=  TFDPRContextEncoder.from_pretrained(UpperCamelCase__     )
             self.assertIsNotNone(UpperCamelCase__     )

        for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             _A    		=  TFDPRContextEncoder.from_pretrained(UpperCamelCase__     )
             self.assertIsNotNone(UpperCamelCase__     )

        for model_name in TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             _A    		=  TFDPRQuestionEncoder.from_pretrained(UpperCamelCase__     )
             self.assertIsNotNone(UpperCamelCase__     )

        for model_name in TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             _A    		=  TFDPRReader.from_pretrained(UpperCamelCase__     )
             self.assertIsNotNone(UpperCamelCase__     )







@require_tf
class lowercase_       (		unittest.TestCase			):







   """simple docstring"""

   @slow
   def 	__UpperCAmelCase   (      self      :			Union[str, Any]     )  ->			Optional[Any]:
        _A    		=  TFDPRQuestionEncoder.from_pretrained('facebook/dpr-question_encoder-single-nq-base'     )

        _A    		=  tf.constant(
            [[1_01, 75_92, 10_10, 20_03, 20_26, 38_99, 1_01_40, 10_29, 1_02]]     )  # [CLS] hello, is my dog cute? [SEP]
        _A    		=  model(UpperCamelCase__     )[0]  # embedding shape = (1, 768)
        # compare the actual values for a slice.
        _A    		=  tf.constant(
            [
                [
                    0.03_236_253,
                    0.12_753_335,
                    0.16_818_509,
                    0.00_279_786,
                    0.3_896_933,
                    0.24_264_945,
                    0.2_178_971,
                    -0.02_335_227,
                    -0.08_481_959,
                    -0.14_324_117,
                ]
            ]     )
        self.assertTrue(numpy.allclose(output[:, :10].numpy(), expected_slice.numpy(), atol=1e-4     )     )



