'''simple docstring'''







import importlib.util
import json
import os
import warnings
from dataclasses import dataclass, field

import torch

from ..training_args import TrainingArguments
from ..utils import cached_property, is_sagemaker_dp_enabled, logging


UpperCamelCase_      	=	logging.get_logger(__name__)


def    lowerCamelCase       (			)			->	Tuple:


							'''simple docstring'''






							SCREAMING_SNAKE_CASE__     :Any						    = os.getenv('SM_HP_MP_PARAMETERS'				,						'{}'		)
							try:
														# Parse it and check the field "partitions" is included, it is required for model parallel.
														SCREAMING_SNAKE_CASE__     :Tuple						    = json.loads(UpperCAmelCase__		)
														if "partitions" not in smp_options:
																					return False
							except json.JSONDecodeError:
														return False

							# Get the sagemaker specific framework parameters from mpi_options variable.
							SCREAMING_SNAKE_CASE__     :List[Any]						    = os.getenv('SM_FRAMEWORK_PARAMS'				,						'{}'		)
							try:
														# Parse it and check the field "sagemaker_distributed_dataparallel_enabled".
														SCREAMING_SNAKE_CASE__     :List[Any]						    = json.loads(UpperCAmelCase__		)
														if not mpi_options.get('sagemaker_mpi_enabled'				,						UpperCAmelCase__		):
																					return False
							except json.JSONDecodeError:
														return False
							# Lastly, check if the `smdistributed` module is present.
							return importlib.util.find_spec('smdistributed'		) is not None


if is_sagemaker_model_parallel_available():
		import smdistributed.modelparallel.torch as smp

		smp.init()





@dataclass
class _SCREAMING_SNAKE_CASE(							_SCREAMING_SNAKE_CASE					):
						A_			:							str     		= field(
						    default=''						,						metadata={'help': 'Used by the SageMaker launcher to send mp-specific args. Ignored in SageMakerTrainer'}						,						)




						def      __lowerCamelCase       (    self :       Union[str, Any]      )     ->				Any:
													super().__post_init__()
													warnings.warn(
													    '`SageMakerTrainingArguments` is deprecated and will be removed in v5 of Transformers. You can use '
													    '`TrainingArguments` instead.'							,							UpperCamelCase_							,							)




						@cached_property
						def      __lowerCamelCase       (    self :       List[str]      )     ->				"torch.device":
													logger.info('PyTorch: setting up devices'      )
													if torch.distributed.is_available() and torch.distributed.is_initialized() and self.local_rank == -1:
																				logger.warning(
																				    'torch.distributed process group is initialized, but local_rank == -1. '
																				    'In order to use Torch DDP, launch your script with `python -m torch.distributed.launch'      )
													if self.no_cuda:
																				SCREAMING_SNAKE_CASE__     :List[Any]						    = torch.device('cpu'      )
																				SCREAMING_SNAKE_CASE__     :List[str]						    = 0
													elif is_sagemaker_model_parallel_available():
																				SCREAMING_SNAKE_CASE__     :Tuple						    = smp.local_rank()
																				SCREAMING_SNAKE_CASE__     :Optional[int]						    = torch.device('cuda'							,							UpperCamelCase_      )
																				SCREAMING_SNAKE_CASE__     :str						    = 1
													elif is_sagemaker_dp_enabled():
																				import smdistributed.dataparallel.torch.torch_smddp  # noqa: F401

																				torch.distributed.init_process_group(backend='smddp'							,							timeout=self.ddp_timeout_delta      )
																				SCREAMING_SNAKE_CASE__     :List[Any]						    = int(os.getenv('SMDATAPARALLEL_LOCAL_RANK'      )      )
																				SCREAMING_SNAKE_CASE__     :Any						    = torch.device('cuda'							,							self.local_rank      )
																				SCREAMING_SNAKE_CASE__     :Union[str, Any]						    = 1
													elif self.local_rank == -1:
																				# if n_gpu is > 1 we'll use nn.DataParallel.
																				# If you only want to use a specific subset of GPUs use `CUDA_VISIBLE_DEVICES=0`
																				# Explicitly set CUDA to the first (index 0) CUDA device, otherwise `set_device` will
																				# trigger an error that a device index is missing. Index 0 takes into account the
																				# GPUs available in the environment, so `CUDA_VISIBLE_DEVICES=1,2` with `cuda:0`
																				# will use the first GPU in that env, i.e. GPU#1
																				SCREAMING_SNAKE_CASE__     :List[Any]						    = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu'      )
																				# Sometimes the line in the postinit has not been run before we end up here, so just checking we're not at
																				# the default value.
																				SCREAMING_SNAKE_CASE__     :Union[str, Any]						    = torch.cuda.device_count()
													else:
																				# Here, we'll use torch.distributed.
																				# Initializes the distributed backend which will take care of synchronizing nodes/GPUs
																				if not torch.distributed.is_initialized():
																											torch.distributed.init_process_group(backend='nccl'							,							timeout=self.ddp_timeout_delta      )
																				SCREAMING_SNAKE_CASE__     :Any						    = torch.device('cuda'							,							self.local_rank      )
																				SCREAMING_SNAKE_CASE__     :str						    = 1

													if device.type == "cuda":
																				torch.cuda.set_device(UpperCamelCase_      )

													return device




						@property
						def      __lowerCamelCase       (    self :       Union[str, Any]      )     ->				List[str]:
													if is_sagemaker_model_parallel_available():
																				return smp.dp_size()

													return super().world_size




						@property
						def      __lowerCamelCase       (    self :       Optional[int]      )     ->				Union[str, Any]:
													return not is_sagemaker_model_parallel_available()




						@property
						def      __lowerCamelCase       (    self :       Optional[Any]      )     ->				Optional[int]:
													return False







'''simple docstring'''







import gc
import unittest

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

from diffusers import AutoencoderKL, DDIMScheduler, LDMTextToImagePipeline, UNetaDConditionModel
from diffusers.utils.testing_utils import (
    enable_full_determinism,
    load_numpy,
    nightly,
    require_torch_gpu,
    slow,
    torch_device,
)

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


enable_full_determinism()




class _SCREAMING_SNAKE_CASE(							_SCREAMING_SNAKE_CASE						,						unittest.TestCase					):
						A_			:							Any     		= LDMTextToImagePipeline
						A_			:							Dict     		= TEXT_TO_IMAGE_PARAMS - {
						    'negative_prompt',
						    'negative_prompt_embeds',
						    'cross_attention_kwargs',
						    'prompt_embeds',
						}
						A_			:							Optional[Any]     		= PipelineTesterMixin.required_optional_params - {
						    'num_images_per_prompt',
						    'callback',
						    'callback_steps',
						}
						A_			:							Any     		= TEXT_TO_IMAGE_BATCH_PARAMS
						A_			:							Optional[int]     		= False




						def      __lowerCamelCase       (    self :       str      )     ->				List[str]:
													torch.manual_seed(0      )
													SCREAMING_SNAKE_CASE__     :List[str]						    = 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')							,							cross_attention_dim=32							,							)
													SCREAMING_SNAKE_CASE__     :Optional[Any]						    = DDIMScheduler(
													    beta_start=0.0_0085							,							beta_end=0.012							,							beta_schedule='scaled_linear'							,							clip_sample=UpperCamelCase_							,							set_alpha_to_one=UpperCamelCase_							,							)
													torch.manual_seed(0      )
													SCREAMING_SNAKE_CASE__     :Tuple						    = 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							,							)
													torch.manual_seed(0      )
													SCREAMING_SNAKE_CASE__     :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=10_00							,							)
													SCREAMING_SNAKE_CASE__     :List[str]						    = CLIPTextModel(UpperCamelCase_      )
													SCREAMING_SNAKE_CASE__     :List[str]						    = CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip'      )

													SCREAMING_SNAKE_CASE__     :List[Any]						    = {
													    'unet': unet,
													    'scheduler': scheduler,
													    'vqvae': vae,
													    'bert': text_encoder,
													    'tokenizer': tokenizer,
													}
													return components




						def      __lowerCamelCase       (    self :       Dict							,							UpperCamelCase_ :       str							,							UpperCamelCase_ :       str=0      )     ->				List[Any]:
													if str(UpperCamelCase_      ).startswith('mps'      ):
																				SCREAMING_SNAKE_CASE__     :int						    = torch.manual_seed(UpperCamelCase_      )
													else:
																				SCREAMING_SNAKE_CASE__     :Dict						    = torch.Generator(device=UpperCamelCase_      ).manual_seed(UpperCamelCase_      )
													SCREAMING_SNAKE_CASE__     :Any						    = {
													    'prompt': 'A painting of a squirrel eating a burger',
													    'generator': generator,
													    'num_inference_steps': 2,
													    'guidance_scale': 6.0,
													    'output_type': 'numpy',
													}
													return inputs




						def      __lowerCamelCase       (    self :       List[Any]      )     ->				Optional[int]:
													SCREAMING_SNAKE_CASE__     :Optional[int]						    = 'cpu'  # ensure determinism for the device-dependent torch.Generator

													SCREAMING_SNAKE_CASE__     :Union[str, Any]						    = self.get_dummy_components()
													SCREAMING_SNAKE_CASE__     :Tuple						    = LDMTextToImagePipeline(**UpperCamelCase_      )
													pipe.to(UpperCamelCase_      )
													pipe.set_progress_bar_config(disable=UpperCamelCase_      )

													SCREAMING_SNAKE_CASE__     :Any						    = self.get_dummy_inputs(UpperCamelCase_      )
													SCREAMING_SNAKE_CASE__     :Dict						    = pipe(**UpperCamelCase_      ).images
													SCREAMING_SNAKE_CASE__     :int						    = image[0, -3:, -3:, -1]

													assert image.shape == (1, 16, 16, 3)
													SCREAMING_SNAKE_CASE__     :Optional[Any]						    = np.array([0.6101, 0.6156, 0.5622, 0.4895, 0.6661, 0.3804, 0.5748, 0.6136, 0.5014]      )

													assert np.abs(image_slice.flatten() - expected_slice      ).max() < 1e-3




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




						def      __lowerCamelCase       (    self :       str      )     ->				Union[str, Any]:
													super().tearDown()
													gc.collect()
													torch.cuda.empty_cache()




						def      __lowerCamelCase       (    self :       Optional[Any]							,							UpperCamelCase_ :       Union[str, Any]							,							UpperCamelCase_ :       Optional[Any]=torch.floataa							,							UpperCamelCase_ :       int=0      )     ->				str:
													SCREAMING_SNAKE_CASE__     :str						    = torch.manual_seed(UpperCamelCase_      )
													SCREAMING_SNAKE_CASE__     :Optional[int]						    = np.random.RandomState(UpperCamelCase_      ).standard_normal((1, 4, 32, 32)      )
													SCREAMING_SNAKE_CASE__     :Optional[Any]						    = torch.from_numpy(UpperCamelCase_      ).to(device=UpperCamelCase_							,							dtype=UpperCamelCase_      )
													SCREAMING_SNAKE_CASE__     :Union[str, Any]						    = {
													    'prompt': 'A painting of a squirrel eating a burger',
													    'latents': latents,
													    'generator': generator,
													    'num_inference_steps': 3,
													    'guidance_scale': 6.0,
													    'output_type': 'numpy',
													}
													return inputs




						def      __lowerCamelCase       (    self :       Tuple      )     ->				Any:
													SCREAMING_SNAKE_CASE__     :Optional[int]						    = LDMTextToImagePipeline.from_pretrained('CompVis/ldm-text2im-large-256'      ).to(UpperCamelCase_      )
													pipe.set_progress_bar_config(disable=UpperCamelCase_      )

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

													assert image.shape == (1, 2_56, 2_56, 3)
													SCREAMING_SNAKE_CASE__     :str						    = np.array([0.5_1825, 0.5_2850, 0.5_2543, 0.5_4258, 0.5_2304, 0.5_2569, 0.5_4363, 0.5_5276, 0.5_6878]      )
													SCREAMING_SNAKE_CASE__     :Union[str, Any]						    = np.abs(expected_slice - image_slice      ).max()
													assert max_diff < 1e-3




@nightly
@require_torch_gpu
class _SCREAMING_SNAKE_CASE(							unittest.TestCase					):




						def      __lowerCamelCase       (    self :       Optional[Any]      )     ->				Optional[Any]:
													super().tearDown()
													gc.collect()
													torch.cuda.empty_cache()




						def      __lowerCamelCase       (    self :       Optional[int]							,							UpperCamelCase_ :       Any							,							UpperCamelCase_ :       Any=torch.floataa							,							UpperCamelCase_ :       List[str]=0      )     ->				str:
													SCREAMING_SNAKE_CASE__     :Union[str, Any]						    = torch.manual_seed(UpperCamelCase_      )
													SCREAMING_SNAKE_CASE__     :Optional[Any]						    = np.random.RandomState(UpperCamelCase_      ).standard_normal((1, 4, 32, 32)      )
													SCREAMING_SNAKE_CASE__     :Optional[Any]						    = torch.from_numpy(UpperCamelCase_      ).to(device=UpperCamelCase_							,							dtype=UpperCamelCase_      )
													SCREAMING_SNAKE_CASE__     :Optional[Any]						    = {
													    'prompt': 'A painting of a squirrel eating a burger',
													    'latents': latents,
													    'generator': generator,
													    'num_inference_steps': 50,
													    'guidance_scale': 6.0,
													    'output_type': 'numpy',
													}
													return inputs




						def      __lowerCamelCase       (    self :       int      )     ->				Dict:
													SCREAMING_SNAKE_CASE__     :str						    = LDMTextToImagePipeline.from_pretrained('CompVis/ldm-text2im-large-256'      ).to(UpperCamelCase_      )
													pipe.set_progress_bar_config(disable=UpperCamelCase_      )

													SCREAMING_SNAKE_CASE__     :List[Any]						    = self.get_inputs(UpperCamelCase_      )
													SCREAMING_SNAKE_CASE__     :Any						    = pipe(**UpperCamelCase_      ).images[0]

													SCREAMING_SNAKE_CASE__     :List[Any]						    = load_numpy(
													    'https://huggingface.co/datasets/diffusers/test-arrays/resolve/main/ldm_text2img/ldm_large_256_ddim.npy'      )
													SCREAMING_SNAKE_CASE__     :str						    = np.abs(expected_image - image      ).max()
													assert max_diff < 1e-3






