


import gc
import random
import unittest

import numpy as np
import torch
from transformers import CLIPTextConfig, CLIPTextModel, CLIPTextModelWithProjection, CLIPTokenizer

from diffusers import (
    AutoencoderKL,
    DiffusionPipeline,
    EulerDiscreteScheduler,
    StableDiffusionXLImgaImgPipeline,
    UNetaDConditionModel,
)
from diffusers.utils import floats_tensor, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

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


enable_full_determinism()






class      UpperCamelCase					(		lowercase__						,      lowercase__						,      unittest.TestCase     ):


   '''simple docstring'''







   lowercase					:	Optional[int]									=StableDiffusionXLImgaImgPipeline
   lowercase					:	List[str]									=TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {"""height""", """width"""}
   lowercase					:	Any									=PipelineTesterMixin.required_optional_params - {"""latents"""}
   lowercase					:	Any									=TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS
   lowercase					:	Any									=IMAGE_TO_IMAGE_IMAGE_PARAMS
   lowercase					:	Tuple									=IMAGE_TO_IMAGE_IMAGE_PARAMS
   def 		UpperCamelCase  (     self		):
        torch.manual_seed(0		)
        lowercase_       :Optional[Any]     =       UNetaDConditionModel(
            block_out_channels=(32, 64)   ,							layers_per_block=2   ,							sample_size=32   ,							in_channels=4   ,							out_channels=4   ,							down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''')   ,							up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D''')   ,							attention_head_dim=(2, 4)   ,							use_linear_projection=UpperCamelCase_   ,							addition_embed_type='''text_time'''   ,							addition_time_embed_dim=8   ,							transformer_layers_per_block=(1, 2)   ,							projection_class_embeddings_input_dim=80   ,							cross_attention_dim=64   ,							)
        lowercase_       :str     =       EulerDiscreteScheduler(
            beta_start=0.0_0085   ,							beta_end=0.012   ,							steps_offset=1   ,							beta_schedule='''scaled_linear'''   ,							timestep_spacing='''leading'''   ,							)
        torch.manual_seed(0		)
        lowercase_       :List[str]     =       AutoencoderKL(
            block_out_channels=[32, 64]   ,							in_channels=3   ,							out_channels=3   ,							down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D''']   ,							up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D''']   ,							latent_channels=4   ,							sample_size=128   ,							)
        torch.manual_seed(0		)
        lowercase_       :Union[str, Any]     =       CLIPTextConfig(
            bos_token_id=0   ,							eos_token_id=2   ,							hidden_size=32   ,							intermediate_size=37   ,							layer_norm_eps=1E-05   ,							num_attention_heads=4   ,							num_hidden_layers=5   ,							pad_token_id=1   ,							vocab_size=1000   ,							hidden_act='''gelu'''   ,							projection_dim=32   ,							)
        lowercase_       :int     =       CLIPTextModel(UpperCamelCase_		)
        lowercase_       :Optional[Any]     =       CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip'''   ,							local_files_only=UpperCamelCase_		)

        lowercase_       :Any     =       CLIPTextModelWithProjection(UpperCamelCase_		)
        lowercase_       :Tuple     =       CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip'''   ,							local_files_only=UpperCamelCase_		)

        lowercase_       :int     =       {
            '''unet''': unet,
            '''scheduler''': scheduler,
            '''vae''': vae,
            '''text_encoder''': text_encoder,
            '''tokenizer''': tokenizer,
            '''text_encoder_2''': text_encoder_a,
            '''tokenizer_2''': tokenizer_a,
            # "safety_checker": None,
            # "feature_extractor": None,
        }
        return components
   def 		UpperCamelCase  (     self   ,							UpperCamelCase_   ,							UpperCamelCase_=0		):
        lowercase_       :int     =       floats_tensor((1, 3, 32, 32)   ,							rng=random.Random(UpperCamelCase_		)		).to(UpperCamelCase_		)
        lowercase_       :Tuple     =       image / 2 + 0.5
        if str(UpperCamelCase_		).startswith('''mps'''		):
             lowercase_       :Union[str, Any]     =       torch.manual_seed(UpperCamelCase_		)
        else:
             lowercase_       :int     =       torch.Generator(device=UpperCamelCase_		).manual_seed(UpperCamelCase_		)
        lowercase_       :Optional[Any]     =       {
            '''prompt''': '''A painting of a squirrel eating a burger''',
            '''image''': image,
            '''generator''': generator,
            '''num_inference_steps''': 2,
            '''guidance_scale''': 5.0,
            '''output_type''': '''numpy''',
            '''strength''': 0.75,
        }
        return inputs
   def 		UpperCamelCase  (     self		):
        lowercase_       :Any     =       '''cpu'''  # ensure determinism for the device-dependent torch.Generator
        lowercase_       :Union[str, Any]     =       self.get_dummy_components()
        lowercase_       :Optional[Any]     =       StableDiffusionXLImgaImgPipeline(**UpperCamelCase_		)
        lowercase_       :List[str]     =       sd_pipe.to(UpperCamelCase_		)
        sd_pipe.set_progress_bar_config(disable=UpperCamelCase_		)

        lowercase_       :Tuple     =       self.get_dummy_inputs(UpperCamelCase_		)
        lowercase_       :Dict     =       sd_pipe(**UpperCamelCase_		).images
        lowercase_       :List[Any]     =       image[0, -3:, -3:, -1]

        assert image.shape == (1, 32, 32, 3)

        lowercase_       :str     =       np.array([0.4656, 0.4840, 0.4439, 0.6698, 0.5574, 0.4524, 0.5799, 0.5943, 0.5165]		)

        assert np.abs(image_slice.flatten() - expected_slice		).max() < 1E-2
   def 		UpperCamelCase  (     self		):
        super().test_attention_slicing_forward_pass(expected_max_diff=3E-3		)
   def 		UpperCamelCase  (     self		):
        super().test_inference_batch_single_identical(expected_max_diff=3E-3		)
   def 		UpperCamelCase  (     self		):
        pass




   def 		UpperCamelCase  (     self		):
        lowercase_       :List[str]     =       self.get_dummy_components()
        lowercase_       :Optional[Any]     =       StableDiffusionXLImgaImgPipeline(**UpperCamelCase_		)
        lowercase_       :Union[str, Any]     =       sd_pipe.to(UpperCamelCase_		)
        lowercase_       :int     =       sd_pipe.to(UpperCamelCase_		)
        sd_pipe.set_progress_bar_config(disable=UpperCamelCase_		)

        # forward without prompt embeds
        lowercase_       :Any     =       self.get_dummy_inputs(UpperCamelCase_		)
        lowercase_       :Optional[int]     =       3 * ['''this is a negative prompt''']
        lowercase_       :List[Any]     =       negative_prompt
        lowercase_       :Optional[int]     =       3 * [inputs['''prompt''']]

        lowercase_       :int     =       sd_pipe(**UpperCamelCase_		)
        lowercase_       :int     =       output.images[0, -3:, -3:, -1]

        # forward with prompt embeds
        lowercase_       :Optional[Any]     =       self.get_dummy_inputs(UpperCamelCase_		)
        lowercase_       :Optional[int]     =       3 * ['''this is a negative prompt''']
        lowercase_       :List[Any]     =       3 * [inputs.pop('''prompt'''		)]

        (
            (
            lowercase_
        )    ,      (
            lowercase_
        )    ,      (
            lowercase_
        )    ,      (
            lowercase_
        )    ,      
        )       :str     =       sd_pipe.encode_prompt(UpperCamelCase_   ,							negative_prompt=UpperCamelCase_		)

        lowercase_       :List[Any]     =       sd_pipe(
            **UpperCamelCase_   ,							prompt_embeds=UpperCamelCase_   ,							negative_prompt_embeds=UpperCamelCase_   ,							pooled_prompt_embeds=UpperCamelCase_   ,							negative_pooled_prompt_embeds=UpperCamelCase_   ,							)
        lowercase_       :Any     =       output.images[0, -3:, -3:, -1]

        # make sure that it's equal
        assert np.abs(image_slice_a.flatten() - image_slice_a.flatten()		).max() < 1E-4






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


   '''simple docstring'''







   def 		UpperCamelCase  (     self		):
        super().tearDown()
        gc.collect()
        torch.cuda.empty_cache()
   def 		UpperCamelCase  (     self   ,							UpperCamelCase_   ,							UpperCamelCase_="cpu"   ,							UpperCamelCase_=torch.floataa   ,							UpperCamelCase_=0		):
        lowercase_       :List[str]     =       torch.Generator(device=UpperCamelCase_		).manual_seed(UpperCamelCase_		)
        lowercase_       :List[Any]     =       np.random.RandomState(UpperCamelCase_		).standard_normal((1, 4, 64, 64)		)
        lowercase_       :str     =       torch.from_numpy(UpperCamelCase_		).to(device=UpperCamelCase_   ,							dtype=UpperCamelCase_		)
        lowercase_       :List[str]     =       {
            '''prompt''': '''a photograph of an astronaut riding a horse''',
            '''latents''': latents,
            '''generator''': generator,
            '''num_inference_steps''': 3,
            '''guidance_scale''': 7.5,
            '''output_type''': '''numpy''',
        }
        return inputs




   def 		UpperCamelCase  (     self		):
        lowercase_       :Union[str, Any]     =       DiffusionPipeline.from_pretrained('''stabilityai/stable-diffusion-2-base'''		)
        pipe.to(UpperCamelCase_		)
        pipe.set_progress_bar_config(disable=UpperCamelCase_		)

        lowercase_       :Optional[int]     =       self.get_inputs(UpperCamelCase_		)
        lowercase_       :Any     =       pipe(**UpperCamelCase_		).images
        lowercase_       :Tuple     =       image[0, -3:, -3:, -1].flatten()

        assert image.shape == (1, 512, 512, 3)
        lowercase_       :List[str]     =       np.array([0.4_9493, 0.4_7896, 0.4_0798, 0.5_4214, 0.5_3212, 0.4_8202, 0.4_7656, 0.4_6329, 0.4_8506]		)
        assert np.abs(image_slice - expected_slice		).max() < 7E-3





from typing import Dict

from transformers import EvalPrediction, HfArgumentParser, TrainingArguments, is_torch_available
from transformers.testing_utils import (
    TestCasePlus,
    execute_subprocess_async,
    get_torch_dist_unique_port,
    require_torch_multi_gpu,
    require_torch_neuroncore,
)
from transformers.training_args import ParallelMode
from transformers.utils import logging


SCREAMING_SNAKE_CASE       :		List[str]         =				logging.get_logger(__name__)


if is_torch_available():
   import torch
   from torch import nn
   from torch.utils.data import Dataset

   from transformers import Trainer






   class      UpperCamelCase					(		lowercase__     ):


      '''simple docstring'''







      def __init__(     self   ,							UpperCamelCase_ = 101		):
           lowercase_       :Tuple     =       length
      def __len__(     self		):
           return self.length




      def __getitem__(     self   ,							UpperCamelCase_		):
           return i






   class      UpperCamelCase					:


      '''simple docstring'''







      def __call__(     self   ,							UpperCamelCase_		):
           return {"input_ids": torch.tensor(UpperCamelCase_		), "labels": torch.tensor(UpperCamelCase_		)}






   class      UpperCamelCase					(		nn.Module     ):


      '''simple docstring'''







      def __init__(     self		):
           super().__init__()
           # Add some (unused) params otherwise DDP will complain.
           lowercase_       :List[Any]     =       nn.Linear(120   ,							80		)




      def 		UpperCamelCase  (     self   ,							UpperCamelCase_   ,							UpperCamelCase_=None		):
           if labels is not None:
                return torch.tensor(0.0   ,							device=input_ids.device		), input_ids
           else:
                return input_ids






class      UpperCamelCase					(		lowercase__     ):


   '''simple docstring'''







   @require_torch_neuroncore
   def 		UpperCamelCase  (     self		):
        lowercase_       :int     =       f"--nproc_per_node=2\n            --master_port={get_torch_dist_unique_port()}\n            {self.test_file_dir}/test_trainer_distributed.py\n        ".split()
        lowercase_       :List[str]     =       self.get_auto_remove_tmp_dir()
        lowercase_       :List[str]     =       f"--output_dir {output_dir}".split()
        lowercase_       :Any     =       ['''torchrun'''] + distributed_args + args
        execute_subprocess_async(UpperCamelCase_   ,							env=self.get_env()		)
        # successful return here == success - any errors would have caused an error in the sub-call






class      UpperCamelCase					(		lowercase__     ):


   '''simple docstring'''







   @require_torch_multi_gpu
   def 		UpperCamelCase  (     self		):
        lowercase_       :Any     =       f"--nproc_per_node={torch.cuda.device_count()}\n            --master_port={get_torch_dist_unique_port()}\n            {self.test_file_dir}/test_trainer_distributed.py\n        ".split()
        lowercase_       :str     =       self.get_auto_remove_tmp_dir()
        lowercase_       :List[str]     =       f"--output_dir {output_dir}".split()
        lowercase_       :Dict     =       ['''torchrun'''] + distributed_args + args
        execute_subprocess_async(UpperCamelCase_   ,							env=self.get_env()		)
        # successful return here == success - any errors would have caused an error in the sub-call


if __name__ == "__main__":
   # The script below is meant to be run under torch.distributed, on a machine with multiple GPUs:
   #
   # PYTHONPATH="src" python -m torch.distributed.run --nproc_per_node 2 --output_dir output_dir ./tests/test_trainer_distributed.py

   SCREAMING_SNAKE_CASE       :		Optional[int]         =				HfArgumentParser((TrainingArguments,))
   SCREAMING_SNAKE_CASE       :		Optional[int]         =				parser.parse_args_into_dataclasses()[0]

   logger.warning(
       f"Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}, "
       f"distributed training: {training_args.parallel_mode != ParallelMode.NOT_DISTRIBUTED}"
   )

   # Essentially, what we want to verify in the distributed case is that we get all samples back,
   # in the right order. (this is crucial for prediction for instance)
   for dataset_length in [101, 40, 7]:
      SCREAMING_SNAKE_CASE       :		Dict         =				DummyDataset(dataset_length)




      def     UpperCamelCase    (      _a		)					->							Dict:
           '''simple docstring'''





           lowercase_       :Optional[Any]     =       list(range(len(_a		)		)		)
           lowercase_       :Optional[Any]     =       p.predictions.tolist() == sequential and p.label_ids.tolist() == sequential
           if not success and training_args.local_rank == 0:
                logger.warning(
                    '''Predictions and/or labels do not match expected results:\n  - predictions: '''
                    f"{p.predictions.tolist()}\n  - labels: {p.label_ids.tolist()}\n  - expected: {sequential}"		)
           return {"success": success}

      SCREAMING_SNAKE_CASE       :		Optional[Any]         =				Trainer(
          model=DummyModel(),
          args=training_args,
          data_collator=DummyDataCollator(),
          eval_dataset=dataset,
          compute_metrics=compute_metrics,
      )
      SCREAMING_SNAKE_CASE       :		Any         =				trainer.evaluate()
      logger.info(metrics)
      if metrics["eval_success"] is not True:
         logger.error(metrics)
         exit(1)

      SCREAMING_SNAKE_CASE       :		Optional[int]         =				trainer.predict(dataset)
      logger.info(p.metrics)
      if p.metrics["test_success"] is not True:
         logger.error(p.metrics)
         exit(1)

      SCREAMING_SNAKE_CASE       :		Union[str, Any]         =				2

      SCREAMING_SNAKE_CASE       :		Any         =				trainer.evaluate()
      logger.info(metrics)
      if metrics["eval_success"] is not True:
         logger.error(metrics)
         exit(1)

      SCREAMING_SNAKE_CASE       :		List[str]         =				trainer.predict(dataset)
      logger.info(p.metrics)
      if p.metrics["test_success"] is not True:
         logger.error(p.metrics)
         exit(1)
      SCREAMING_SNAKE_CASE       :		Optional[int]         =				None

