



"""simple docstring"""







import gc
import unittest

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

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

from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS
from ..test_pipelines_common import PipelineLatentTesterMixin, PipelineTesterMixin


enable_full_determinism()







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




      """simple docstring"""




      _SCREAMING_SNAKE_CASE    							=	StableDiffusionXLPipeline
      _SCREAMING_SNAKE_CASE    							=	TEXT_TO_IMAGE_PARAMS
      _SCREAMING_SNAKE_CASE    							=	TEXT_TO_IMAGE_BATCH_PARAMS
      _SCREAMING_SNAKE_CASE    							=	TEXT_TO_IMAGE_IMAGE_PARAMS
      _SCREAMING_SNAKE_CASE    							=	TEXT_TO_IMAGE_IMAGE_PARAMS



      def 	SCREAMING_SNAKE_CASE__					( self							: str			):
        torch.manual_seed(0			)
        lowerCAmelCase_  :							Union[str, Any]								=					UNetaDConditionModel(
            block_out_channels=(3_2, 6_4)       ,     layers_per_block=2       ,     sample_size=3_2       ,     in_channels=4       ,     out_channels=4       ,     down_block_types=('DownBlock2D', 'CrossAttnDownBlock2D')       ,     up_block_types=('CrossAttnUpBlock2D', 'UpBlock2D')       ,     attention_head_dim=(2, 4)       ,     use_linear_projection=_SCREAMING_SNAKE_CASE       ,     addition_embed_type='text_time'       ,     addition_time_embed_dim=8       ,     transformer_layers_per_block=(1, 2)       ,     projection_class_embeddings_input_dim=8_0       ,     cross_attention_dim=6_4       ,     )
        lowerCAmelCase_  :							Dict								=					EulerDiscreteScheduler(
            beta_start=0.0_00_85       ,     beta_end=0.0_12       ,     steps_offset=1       ,     beta_schedule='scaled_linear'       ,     timestep_spacing='leading'       ,     )
        torch.manual_seed(0			)
        lowerCAmelCase_  :							Tuple								=					AutoencoderKL(
            block_out_channels=[3_2, 6_4]       ,     in_channels=3       ,     out_channels=3       ,     down_block_types=['DownEncoderBlock2D', 'DownEncoderBlock2D']       ,     up_block_types=['UpDecoderBlock2D', 'UpDecoderBlock2D']       ,     latent_channels=4       ,     sample_size=1_2_8       ,     )
        torch.manual_seed(0			)
        lowerCAmelCase_  :							Dict								=					CLIPTextConfig(
            bos_token_id=0       ,     eos_token_id=2       ,     hidden_size=3_2       ,     intermediate_size=3_7       ,     layer_norm_eps=1E-05       ,     num_attention_heads=4       ,     num_hidden_layers=5       ,     pad_token_id=1       ,     vocab_size=1_0_0_0       ,     hidden_act='gelu'       ,     projection_dim=3_2       ,     )
        lowerCAmelCase_  :							Optional[int]								=					CLIPTextModel(_SCREAMING_SNAKE_CASE			)
        lowerCAmelCase_  :							Optional[Any]								=					CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip'       ,     local_files_only=_SCREAMING_SNAKE_CASE			)

        lowerCAmelCase_  :							Tuple								=					CLIPTextModelWithProjection(_SCREAMING_SNAKE_CASE			)
        lowerCAmelCase_  :							Optional[Any]								=					CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip'       ,     local_files_only=_SCREAMING_SNAKE_CASE			)

        lowerCAmelCase_  :							List[Any]								=					{
            '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 	SCREAMING_SNAKE_CASE__					( self							: str       ,     SCREAMING_SNAKE_CASE_							: Tuple       ,     SCREAMING_SNAKE_CASE_							: Dict=0			):





        if str(_SCREAMING_SNAKE_CASE			).startswith('mps'			):
          lowerCAmelCase_  :							str								=					torch.manual_seed(_SCREAMING_SNAKE_CASE			)
        else:
          lowerCAmelCase_  :							Tuple								=					torch.Generator(device=_SCREAMING_SNAKE_CASE			).manual_seed(_SCREAMING_SNAKE_CASE			)
        lowerCAmelCase_  :							List[str]								=					{
            'prompt': 'A painting of a squirrel eating a burger',
            'generator': generator,
            'num_inference_steps': 2,
            'guidance_scale': 5.0,
            'output_type': 'numpy',
        }
        return inputs



      def 	SCREAMING_SNAKE_CASE__					( self							: List[str]			):
        lowerCAmelCase_  :							int								=					'cpu'  # ensure determinism for the device-dependent torch.Generator
        lowerCAmelCase_  :							Optional[int]								=					self.get_dummy_components()
        lowerCAmelCase_  :							Union[str, Any]								=					StableDiffusionXLPipeline(**_SCREAMING_SNAKE_CASE			)
        lowerCAmelCase_  :							Any								=					sd_pipe.to(_SCREAMING_SNAKE_CASE			)
        sd_pipe.set_progress_bar_config(disable=_SCREAMING_SNAKE_CASE			)

        lowerCAmelCase_  :							str								=					self.get_dummy_inputs(_SCREAMING_SNAKE_CASE			)
        lowerCAmelCase_  :							Optional[Any]								=					sd_pipe(**_SCREAMING_SNAKE_CASE			).images
        lowerCAmelCase_  :							Dict								=					image[0, -3:, -3:, -1]

        assert image.shape == (1, 6_4, 6_4, 3)
        lowerCAmelCase_  :							List[str]								=					np.array([0.58_73, 0.61_28, 0.47_97, 0.51_22, 0.56_74, 0.46_39, 0.52_27, 0.51_49, 0.47_47]			)

        assert np.abs(image_slice.flatten() - expected_slice			).max() < 1E-2



      def 	SCREAMING_SNAKE_CASE__					( self							: int			):
        lowerCAmelCase_  :							List[Any]								=					self.get_dummy_components()
        lowerCAmelCase_  :							Optional[Any]								=					StableDiffusionXLPipeline(**_SCREAMING_SNAKE_CASE			)
        lowerCAmelCase_  :							Optional[Any]								=					sd_pipe.to(_SCREAMING_SNAKE_CASE			)
        lowerCAmelCase_  :							Optional[Any]								=					sd_pipe.to(_SCREAMING_SNAKE_CASE			)
        sd_pipe.set_progress_bar_config(disable=_SCREAMING_SNAKE_CASE			)

        # forward without prompt embeds
        lowerCAmelCase_  :							List[str]								=					self.get_dummy_inputs(_SCREAMING_SNAKE_CASE			)
        lowerCAmelCase_  :							str								=					3 * ['this is a negative prompt']
        lowerCAmelCase_  :							List[Any]								=					negative_prompt
        lowerCAmelCase_  :							List[str]								=					3 * [inputs['prompt']]

        lowerCAmelCase_  :							Tuple								=					sd_pipe(**_SCREAMING_SNAKE_CASE			)
        lowerCAmelCase_  :							List[Any]								=					output.images[0, -3:, -3:, -1]

        # forward with prompt embeds
        lowerCAmelCase_  :							Dict								=					self.get_dummy_inputs(_SCREAMING_SNAKE_CASE			)
        lowerCAmelCase_  :							List[str]								=					3 * ['this is a negative prompt']
        lowerCAmelCase_  :							List[str]								=					3 * [inputs.pop('prompt'			)]

        (
            (
            lowerCAmelCase_  
        )		,(
            lowerCAmelCase_  
        )		,(
            lowerCAmelCase_  
        )		,(
            lowerCAmelCase_  
        )		,  
        )  :							Any								=					sd_pipe.encode_prompt(_SCREAMING_SNAKE_CASE       ,     negative_prompt=_SCREAMING_SNAKE_CASE			)

        lowerCAmelCase_  :							Any								=					sd_pipe(
            **_SCREAMING_SNAKE_CASE       ,     prompt_embeds=_SCREAMING_SNAKE_CASE       ,     negative_prompt_embeds=_SCREAMING_SNAKE_CASE       ,     pooled_prompt_embeds=_SCREAMING_SNAKE_CASE       ,     negative_pooled_prompt_embeds=_SCREAMING_SNAKE_CASE       ,     )
        lowerCAmelCase_  :							Dict								=					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



      def 	SCREAMING_SNAKE_CASE__					( self							: Dict			):
        super().test_attention_slicing_forward_pass(expected_max_diff=3E-3			)





      def 	SCREAMING_SNAKE_CASE__					( self							: Optional[Any]			):
        super().test_inference_batch_single_identical(expected_max_diff=3E-3			)







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




      """simple docstring"""




      def 	SCREAMING_SNAKE_CASE__					( self							: int			):
        super().tearDown()
        gc.collect()
        torch.cuda.empty_cache()



      def 	SCREAMING_SNAKE_CASE__					( self							: int       ,     SCREAMING_SNAKE_CASE_							: Any       ,     SCREAMING_SNAKE_CASE_							: int="cpu"       ,     SCREAMING_SNAKE_CASE_							: Optional[int]=torch.floataa       ,     SCREAMING_SNAKE_CASE_							: int=0			):
        lowerCAmelCase_  :							Optional[Any]								=					torch.Generator(device=_SCREAMING_SNAKE_CASE			).manual_seed(_SCREAMING_SNAKE_CASE			)
        lowerCAmelCase_  :							Union[str, Any]								=					np.random.RandomState(_SCREAMING_SNAKE_CASE			).standard_normal((1, 4, 6_4, 6_4)			)
        lowerCAmelCase_  :							Dict								=					torch.from_numpy(_SCREAMING_SNAKE_CASE			).to(device=_SCREAMING_SNAKE_CASE       ,     dtype=_SCREAMING_SNAKE_CASE			)
        lowerCAmelCase_  :							Any								=					{
            '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 	SCREAMING_SNAKE_CASE__					( self							: int			):
        lowerCAmelCase_  :							Optional[Any]								=					DiffusionPipeline.from_pretrained('stabilityai/stable-diffusion-2-base'			)
        pipe.to(_SCREAMING_SNAKE_CASE			)
        pipe.set_progress_bar_config(disable=_SCREAMING_SNAKE_CASE			)

        lowerCAmelCase_  :							Any								=					self.get_inputs(_SCREAMING_SNAKE_CASE			)
        lowerCAmelCase_  :							List[Any]								=					pipe(**_SCREAMING_SNAKE_CASE			).images
        lowerCAmelCase_  :							str								=					image[0, -3:, -3:, -1].flatten()

        assert image.shape == (1, 5_1_2, 5_1_2, 3)
        lowerCAmelCase_  :							int								=					np.array([0.4_94_93, 0.4_78_96, 0.4_07_98, 0.5_42_14, 0.5_32_12, 0.4_82_02, 0.4_76_56, 0.4_63_29, 0.4_85_06]			)
        assert np.abs(image_slice - expected_slice			).max() < 7E-3













"""simple docstring"""



import timeit
from typing import Callable, Optional

from ..configuration_utils import PretrainedConfig
from ..models.auto.modeling_auto import MODEL_MAPPING, MODEL_WITH_LM_HEAD_MAPPING
from ..utils import is_pyanvml_available, is_torch_available, logging
from .benchmark_utils import (
    Benchmark,
    Memory,
    MemorySummary,
    measure_peak_memory_cpu,
    start_memory_tracing,
    stop_memory_tracing,
)


if is_torch_available():
						import torch

						from .benchmark_args import PyTorchBenchmarkArguments


if is_pyanvml_available():
						import pyanvml.pyanvml as nvml


_snake_case							:   str	  =       logging.get_logger(__name__)




class 							__lowercase(							_UpperCamelCase	):
						lowerCAmelCase_		:				PyTorchBenchmarkArguments
						lowerCAmelCase_		:				PretrainedConfig
						lowerCAmelCase_		:				str=							"PyTorch"






						@property
						def 		lowerCAmelCase (       self)      ->	List[Any]:




											"""simple docstring"""





											return torch.__version__






						def 		lowerCAmelCase (       self     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE)      ->	float:




											"""simple docstring"""





											lowerCamelCase__					=self._prepare_inference_func(_SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE)
											return self._measure_speed(_inference)






						def 		lowerCAmelCase (       self     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE)      ->	[Memory, Optional[MemorySummary]]:




											"""simple docstring"""





											lowerCamelCase__					=self._prepare_inference_func(_SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE)
											return self._measure_memory(_inference)






						def 		lowerCAmelCase (       self     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE)      ->	float:




											"""simple docstring"""





											lowerCamelCase__					=self._prepare_train_func(_SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE)
											return self._measure_speed(_train)






						def 		lowerCAmelCase (       self     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE)      ->	[Memory, Optional[MemorySummary]]:




											"""simple docstring"""





											lowerCamelCase__					=self._prepare_train_func(_SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE)
											return self._measure_memory(_train)






						def 		lowerCAmelCase (       self     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE)      ->	Callable[[], None]:




											"""simple docstring"""





											lowerCamelCase__					=self.config_dict[model_name]

											if self.args.torchscript:
																lowerCamelCase__					=True

											lowerCamelCase__					=(
											    hasattr(_SCREAMING_SNAKE_CASE     ,    'architectures')
											    and isinstance(config.architectures     ,    _SCREAMING_SNAKE_CASE)
											    and len(config.architectures) > 0
											)
											if not self.args.only_pretrain_model and has_model_class_in_config:
																try:
																					lowerCamelCase__					=config.architectures[0]
																					lowerCamelCase__					=__import__('transformers'     ,    fromlist=[model_class])
																					lowerCamelCase__					=getattr(_SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE)
																					lowerCamelCase__					=model_cls(_SCREAMING_SNAKE_CASE)
																except ImportError:
																					raise ImportError(
																					    F"{model_class} does not exist. If you just want to test the pretrained model, you might want to"
																					    ' set `--only_pretrain_model` or `args.only_pretrain_model=True`.')
											else:
																lowerCamelCase__					=MODEL_MAPPING[config.__class__](_SCREAMING_SNAKE_CASE)

											model.eval()
											model.to(self.args.device)

											# encoder-decoder has vocab size saved differently
											lowerCamelCase__					=config.vocab_size if hasattr(_SCREAMING_SNAKE_CASE     ,    'vocab_size') else config.encoder.vocab_size
											lowerCamelCase__					=torch.randint(_SCREAMING_SNAKE_CASE     ,    (batch_size, sequence_length)     ,    dtype=torch.long     ,    device=self.args.device)

											if self.args.fpaa:
																logger.info('Running training in Mixed Precision...')
																if not self.args.is_gpu:
																					raise ValueError('Mixed precision is possible only for GPU.')
																# amp seems to have memory leaks so that memory usage
																# is measured using .half() for now https://github.com/NVIDIA/apex/issues/439
																model.half()

											if self.args.torchscript:
																with torch.no_grad():
																					lowerCamelCase__					=torch.jit.trace(_SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE)
											else:
																lowerCamelCase__					=model

											def encoder_decoder_forward():
																with torch.no_grad():
																					lowerCamelCase__					=inference_model(_SCREAMING_SNAKE_CASE     ,    decoder_input_ids=_SCREAMING_SNAKE_CASE)
																return outputs

											def encoder_forward():
																with torch.no_grad():
																					lowerCamelCase__					=inference_model(_SCREAMING_SNAKE_CASE)
																return outputs

											lowerCamelCase__					=encoder_decoder_forward if config.is_encoder_decoder else encoder_forward
											return _forward






						def 		lowerCAmelCase (       self     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE)      ->	Callable[[], None]:




											"""simple docstring"""





											lowerCamelCase__					=self.config_dict[model_name]

											lowerCamelCase__					=(
											    hasattr(_SCREAMING_SNAKE_CASE     ,    'architectures')
											    and isinstance(config.architectures     ,    _SCREAMING_SNAKE_CASE)
											    and len(config.architectures) > 0
											)
											if not self.args.only_pretrain_model and has_model_class_in_config:
																try:
																					lowerCamelCase__					=config.architectures[0]
																					lowerCamelCase__					=__import__('transformers'     ,    fromlist=[model_class])
																					lowerCamelCase__					=getattr(_SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE)
																					lowerCamelCase__					=model_cls(_SCREAMING_SNAKE_CASE)
																except ImportError:
																					raise ImportError(
																					    F"{model_class} does not exist. If you just want to test the pretrained model, you might want to"
																					    ' set `--only_pretrain_model` or `args.only_pretrain_model=True`.')
											else:
																lowerCamelCase__					=MODEL_WITH_LM_HEAD_MAPPING[config.__class__](_SCREAMING_SNAKE_CASE)

											if self.args.torchscript:
																raise NotImplementedError('Training for torchscript is currently not implemented')
											else:
																lowerCamelCase__					=model

											model.train()
											model.to(self.args.device)

											# encoder-decoder has vocab size saved differently
											lowerCamelCase__					=config.vocab_size if hasattr(_SCREAMING_SNAKE_CASE     ,    'vocab_size') else config.encoder.vocab_size
											lowerCamelCase__					=torch.randint(_SCREAMING_SNAKE_CASE     ,    (batch_size, sequence_length)     ,    dtype=torch.long     ,    device=self.args.device)

											if self.args.fpaa:
																logger.info('Running training in Mixed Precision...')
																if not self.args.is_gpu:
																					raise ValueError('Mixed precision is possible only for GPU.')

																# amp seems to have memory leaks so that memory usage
																# is measured using .half() for now https://github.com/NVIDIA/apex/issues/439
																model.half()

											def compute_loss_and_backprob_encoder():
																lowerCamelCase__					=train_model(_SCREAMING_SNAKE_CASE     ,    labels=_SCREAMING_SNAKE_CASE)[0]
																loss.backward()
																return loss

											def compute_loss_and_backprob_encoder_decoder():
																lowerCamelCase__					=train_model(_SCREAMING_SNAKE_CASE     ,    decoder_input_ids=_SCREAMING_SNAKE_CASE     ,    labels=_SCREAMING_SNAKE_CASE)[0]
																loss.backward()
																return loss

											lowerCamelCase__					=(
											    compute_loss_and_backprob_encoder_decoder
											    if config.is_encoder_decoder
											    else compute_loss_and_backprob_encoder
											)
											return _train






						def 		lowerCAmelCase (       self     ,    _SCREAMING_SNAKE_CASE)      ->	float:




											"""simple docstring"""





											try:
																if self.args.is_tpu or self.args.torchscript:
																					# run additional 10 times to stabilize compilation for tpu and torchscript
																					logger.info('Do inference on TPU or torchscript. Running model 5 times to stabilize compilation')
																					timeit.repeat(
																					    _SCREAMING_SNAKE_CASE     ,    repeat=1     ,    number=5     ,    )

																# as written in https://docs.python.org/2/library/timeit.html#timeit.Timer.repeat, min should be taken rather than the average
																lowerCamelCase__					=timeit.repeat(
																    _SCREAMING_SNAKE_CASE     ,    repeat=self.args.repeat     ,    number=10     ,    )

																if self.args.is_tpu and self.args.torch_xla_tpu_print_metrics:
																					import torch_xla.debug.metrics as met

																					self.print_fn(met.metrics_report())

																return min(_SCREAMING_SNAKE_CASE) / 10.0
											except RuntimeError as e:
																self.print_fn(F"Doesn't fit on GPU. {e}")
																return "N/A"






						def 		lowerCAmelCase (       self     ,    _SCREAMING_SNAKE_CASE)      ->	[Memory, MemorySummary]:




											"""simple docstring"""





											try:
																if self.args.trace_memory_line_by_line:
																					lowerCamelCase__					=start_memory_tracing('transformers')

																if self.args.is_tpu:
																					# tpu
																					raise NotImplementedError(
																					    'Memory Benchmarking is currently not implemented for TPU. Please disable memory benchmarking with'
																					    ' `--no-memory` or `args.memory=False`')
																elif self.args.is_gpu:
																					if not is_pyanvml_available():
																										logger.warning(
																										    'py3nvml not installed, we won\'t log GPU memory usage. '
																										    'Install py3nvml (pip install py3nvml) to log information about GPU.')
																										lowerCamelCase__					='N/A'
																					else:
																										logger.info(
																										    'Measuring total GPU usage on GPU device. Make sure to not have additional processes running'
																										    ' on the same GPU.')
																										# init nvml
																										nvml.nvmlInit()
																										func()
																										lowerCamelCase__					=nvml.nvmlDeviceGetHandleByIndex(self.args.device_idx)
																										lowerCamelCase__					=nvml.nvmlDeviceGetMemoryInfo(_SCREAMING_SNAKE_CASE)
																										lowerCamelCase__					=meminfo.used
																										lowerCamelCase__					=Memory(_SCREAMING_SNAKE_CASE)
																										# shutdown nvml
																										nvml.nvmlShutdown()
																else:
																					# cpu
																					lowerCamelCase__					=measure_peak_memory_cpu(_SCREAMING_SNAKE_CASE)
																					lowerCamelCase__					=Memory(_SCREAMING_SNAKE_CASE) if isinstance(_SCREAMING_SNAKE_CASE     ,    _SCREAMING_SNAKE_CASE) else memory_bytes

																if self.args.trace_memory_line_by_line:
																					lowerCamelCase__					=stop_memory_tracing(_SCREAMING_SNAKE_CASE)
																else:
																					lowerCamelCase__					=None

																return memory, summary
											except RuntimeError as e:
																self.print_fn(F"Doesn't fit on GPU. {e}")
																return "N/A", None