






"""simple docstring"""
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_       (UpperCAmelCase_				,						UpperCAmelCase_				,						unittest.TestCase     ):
	__magic_name__	      =				StableDiffusionXLImgaImgPipeline
	__magic_name__	      =				TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {'height', 'width'}
	__magic_name__	      =				PipelineTesterMixin.required_optional_params - {'latents'}
	__magic_name__	      =				TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS
	__magic_name__	      =				IMAGE_TO_IMAGE_IMAGE_PARAMS
	__magic_name__	      =				IMAGE_TO_IMAGE_IMAGE_PARAMS





	def      _SCREAMING_SNAKE_CASE      (      self					: Tuple						)						->			List[Any]:
				torch.manual_seed(0						)
				UpperCAmelCase_     : List[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	,	)
				UpperCAmelCase_     : Any			     =					EulerDiscreteScheduler(
				    beta_start=0.0_0_0_8_5	,	beta_end=0.0_1_2	,	steps_offset=1	,	beta_schedule="scaled_linear"	,	timestep_spacing="leading"	,	)
				torch.manual_seed(0						)
				UpperCAmelCase_     : 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	,	sample_size=128	,	)
				torch.manual_seed(0						)
				UpperCAmelCase_     : List[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=1_000	,	hidden_act="gelu"	,	projection_dim=32	,	)
				UpperCAmelCase_     : Tuple			     =					CLIPTextModel(__UpperCAmelCase						)
				UpperCAmelCase_     : Union[str, Any]			     =					CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip"	,	local_files_only=__UpperCAmelCase						)

				UpperCAmelCase_     : List[Any]			     =					CLIPTextModelWithProjection(__UpperCAmelCase						)
				UpperCAmelCase_     : Tuple			     =					CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip"	,	local_files_only=__UpperCAmelCase						)

				UpperCAmelCase_     : 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					: List[Any]	,	lowerCAmelCase_					: Tuple	,	lowerCAmelCase_					: Optional[int]=0						)						->			List[Any]:
				UpperCAmelCase_     : str			     =					floats_tensor((1, 3, 32, 32)	,	rng=random.Random(__UpperCAmelCase						)						).to(__UpperCAmelCase						)
				UpperCAmelCase_     : Tuple			     =					image / 2 + 0.5
				if str(__UpperCAmelCase						).startswith("mps"						):
							UpperCAmelCase_     : Optional[int]			     =					torch.manual_seed(__UpperCAmelCase						)
				else:
							UpperCAmelCase_     : Tuple			     =					torch.Generator(device=__UpperCAmelCase						).manual_seed(__UpperCAmelCase						)
				UpperCAmelCase_     : List[str]			     =					{
				    "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.7_5,
				}
				return inputs





	def      _SCREAMING_SNAKE_CASE      (      self					: Any						)						->			Union[str, Any]:
				UpperCAmelCase_     : List[Any]			     =					"cpu"  # ensure determinism for the device-dependent torch.Generator
				UpperCAmelCase_     : Dict			     =					self.get_dummy_components()
				UpperCAmelCase_     : Any			     =					StableDiffusionXLImgaImgPipeline(**__UpperCAmelCase						)
				UpperCAmelCase_     : Optional[Any]			     =					sd_pipe.to(__UpperCAmelCase						)
				sd_pipe.set_progress_bar_config(disable=__UpperCAmelCase						)

				UpperCAmelCase_     : Optional[Any]			     =					self.get_dummy_inputs(__UpperCAmelCase						)
				UpperCAmelCase_     : Optional[int]			     =					sd_pipe(**__UpperCAmelCase						).images
				UpperCAmelCase_     : Dict			     =					image[0, -3:, -3:, -1]

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

				UpperCAmelCase_     : str			     =					np.array([0.4_6_5_6, 0.4_8_4_0, 0.4_4_3_9, 0.6_6_9_8, 0.5_5_7_4, 0.4_5_2_4, 0.5_7_9_9, 0.5_9_4_3, 0.5_1_6_5]						)

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





	def      _SCREAMING_SNAKE_CASE      (      self					: Tuple						)						->			Optional[Any]:
				super().test_attention_slicing_forward_pass(expected_max_diff=3e-3						)





	def      _SCREAMING_SNAKE_CASE      (      self					: int						)						->			List[Any]:
				super().test_inference_batch_single_identical(expected_max_diff=3e-3						)





	def      _SCREAMING_SNAKE_CASE      (      self					: Tuple						)						->			Union[str, Any]:
				pass





	def      _SCREAMING_SNAKE_CASE      (      self					: int						)						->			str:
				UpperCAmelCase_     : Dict			     =					self.get_dummy_components()
				UpperCAmelCase_     : Dict			     =					StableDiffusionXLImgaImgPipeline(**__UpperCAmelCase						)
				UpperCAmelCase_     : Any			     =					sd_pipe.to(__UpperCAmelCase						)
				UpperCAmelCase_     : Optional[Any]			     =					sd_pipe.to(__UpperCAmelCase						)
				sd_pipe.set_progress_bar_config(disable=__UpperCAmelCase						)

				# forward without prompt embeds
				UpperCAmelCase_     : Optional[int]			     =					self.get_dummy_inputs(__UpperCAmelCase						)
				UpperCAmelCase_     : Dict			     =					3 * ["this is a negative prompt"]
				UpperCAmelCase_     : List[Any]			     =					negative_prompt
				UpperCAmelCase_     : List[str]			     =					3 * [inputs["prompt"]]

				UpperCAmelCase_     : List[Any]			     =					sd_pipe(**__UpperCAmelCase						)
				UpperCAmelCase_     : List[Any]			     =					output.images[0, -3:, -3:, -1]

				# forward with prompt embeds
				UpperCAmelCase_     : Any			     =					self.get_dummy_inputs(__UpperCAmelCase						)
				UpperCAmelCase_     : Optional[int]			     =					3 * ["this is a negative prompt"]
				UpperCAmelCase_     : List[str]			     =					3 * [inputs.pop("prompt"						)]

				(
				    (
				    UpperCAmelCase_      
				)     ,	(
				    UpperCAmelCase_      
				)     ,	(
				    UpperCAmelCase_      
				)     ,	(
				    UpperCAmelCase_      
				)     ,	      
				)     : Optional[Any]			     =					sd_pipe.encode_prompt(__UpperCAmelCase	,	negative_prompt=__UpperCAmelCase						)

				UpperCAmelCase_     : Optional[Any]			     =					sd_pipe(
				    **__UpperCAmelCase	,	prompt_embeds=__UpperCAmelCase	,	negative_prompt_embeds=__UpperCAmelCase	,	pooled_prompt_embeds=__UpperCAmelCase	,	negative_pooled_prompt_embeds=__UpperCAmelCase	,	)
				UpperCAmelCase_     : str			     =					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     ):





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





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

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

				assert image.shape == (1, 512, 512, 3)
				UpperCAmelCase_     : Any			     =					np.array([0.4_9_4_9_3, 0.4_7_8_9_6, 0.4_0_7_9_8, 0.5_4_2_1_4, 0.5_3_2_1_2, 0.4_8_2_0_2, 0.4_7_6_5_6, 0.4_6_3_2_9, 0.4_8_5_0_6]						)
				assert np.abs(image_slice - expected_slice						).max() < 7e-3




import json
import os
import subprocess
import unittest
from ast import literal_eval

import pytest
from parameterized import parameterized_class

from . import is_sagemaker_available


if is_sagemaker_available():
      from sagemaker import Session, TrainingJobAnalytics
      from sagemaker.huggingface import HuggingFace





@pytest.mark.skipif(
    literal_eval(os.getenv('TEST_SAGEMAKER'		,				'False'     )     ) is not True		,				reason='Skipping test because should only be run when releasing minor transformers version'		,				)
@pytest.mark.usefixtures('sm_env'     )
@parameterized_class(
    [
        {
            'framework': 'pytorch',
            'script': 'run_glue.py',
            'model_name_or_path': 'distilbert-base-cased',
            'instance_type': 'ml.g4dn.xlarge',
            'results': {'train_runtime': 6_50, 'eval_accuracy': 0.6, 'eval_loss': 0.9},
        },
        {
            'framework': 'tensorflow',
            'script': 'run_tf.py',
            'model_name_or_path': 'distilbert-base-cased',
            'instance_type': 'ml.g4dn.xlarge',
            'results': {'train_runtime': 6_00, 'eval_accuracy': 0.3, 'eval_loss': 0.9},
        },
    ]     )
class      A							(				unittest.TestCase     ):


   def 			lowercase_      (self     :						int		)						->				Optional[Any]:
         """simple docstring"""



         if self.framework == "pytorch":
               subprocess.run(
                   f"""cp ./examples/pytorch/text-classification/run_glue.py {self.env.test_path}/run_glue.py""".split()       ,  encoding="utf-8"       ,  check=__UpperCAmelCase       ,  )
         assert hasattr(self       ,  "env"		)


   def 			lowercase_      (self     :						List[Any]       ,  __UpperCAmelCase     :						Optional[int]=1		)						->				Dict:
         """simple docstring"""



         return HuggingFace(
             entry_point=self.script       ,  source_dir=self.env.test_path       ,  role=self.env.role       ,  image_uri=self.env.image_uri       ,  base_job_name=f"""{self.env.base_job_name}-single"""       ,  instance_count=__UpperCAmelCase       ,  instance_type=self.instance_type       ,  debugger_hook_config=__UpperCAmelCase       ,  hyperparameters={**self.env.hyperparameters, "model_name_or_path": self.model_name_or_path}       ,  metric_definitions=self.env.metric_definitions       ,  py_version="py36"       ,  )


   def 			lowercase_      (self     :						Optional[Any]       ,  __UpperCAmelCase     :						Tuple		)						->				Optional[int]:
         """simple docstring"""



         TrainingJobAnalytics(__UpperCAmelCase		).export_csv(f"""{self.env.test_path}/{job_name}_metrics.csv"""		)





   def 			lowercase_      (self     :						Any		)						->				Optional[Any]:
         """simple docstring"""



         UpperCAmelCase__								=  self.create_estimator()

         # run training
         estimator.fit()

         # result dataframe
         UpperCAmelCase__								=  TrainingJobAnalytics(estimator.latest_training_job.name		).dataframe()

         # extract kpis
         UpperCAmelCase__								=  list(result_metrics_df[result_metrics_df.metric_name == "eval_accuracy"]["value"]		)
         UpperCAmelCase__								=  list(result_metrics_df[result_metrics_df.metric_name == "eval_loss"]["value"]		)
         # get train time from SageMaker job, this includes starting, preprocessing, stopping
         UpperCAmelCase__								=  (
             Session().describe_training_job(estimator.latest_training_job.name		).get("TrainingTimeInSeconds"       ,  9_9_9_9_9_9		)
         )

         # assert kpis
         assert train_runtime <= self.results["train_runtime"]
         assert all(t >= self.results["eval_accuracy"] for t in eval_accuracy		)
         assert all(t <= self.results["eval_loss"] for t in eval_loss		)

         # dump tests result into json file to share in PR
         with open(f"""{estimator.latest_training_job.name}.json"""       ,  "w"		) as outfile:
               json.dump({"train_time": train_runtime, "eval_accuracy": eval_accuracy, "eval_loss": eval_loss}       ,  __UpperCAmelCase		)



