





import gc
import random
import unittest

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

from diffusers import AutoencoderKL, PNDMScheduler, StableDiffusionInpaintPipeline, UNetaDConditionModel
from diffusers.utils import floats_tensor, load_image, load_numpy, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, slow

from ..pipeline_params import TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, TEXT_GUIDED_IMAGE_INPAINTING_PARAMS
from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin


enable_full_determinism()

class 	A_       (a_     ,							a_     ,							a_     ,							unittest.TestCase			):
	UpperCAmelCase__					=			StableDiffusionInpaintPipeline
	UpperCAmelCase__					=			TEXT_GUIDED_IMAGE_INPAINTING_PARAMS
	UpperCAmelCase__					=			TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS
	UpperCAmelCase__					=			frozenset(
	    []			)  # TO-DO: update image_params once pipeline is refactored with VaeImageProcessor.preprocess
	UpperCAmelCase__					=			frozenset([]			)
	def      _lowercase				(    self		):






				'''simple docstring'''







				torch.manual_seed(0		)
				UpperCAmelCase   		=				UNetaDConditionModel(
				    block_out_channels=(3_2, 6_4)							,					layers_per_block=2							,					sample_size=3_2							,					in_channels=9							,					out_channels=4							,					down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''')							,					up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D''')							,					cross_attention_dim=3_2							,					attention_head_dim=(2, 4)							,					use_linear_projection=_A							,					)
				UpperCAmelCase   		=				PNDMScheduler(skip_prk_steps=_A		)
				torch.manual_seed(0		)
				UpperCAmelCase   		=				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		)
				UpperCAmelCase   		=				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=5_1_2							,					)
				UpperCAmelCase   		=				CLIPTextModel(_A		)
				UpperCAmelCase   		=				CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip'''		)

				UpperCAmelCase   		=				{
				    '''unet''': unet,
				    '''scheduler''': scheduler,
				    '''vae''': vae,
				    '''text_encoder''': text_encoder,
				    '''tokenizer''': tokenizer,
				    '''safety_checker''': None,
				    '''feature_extractor''': None,
				}
				return components
	def      _lowercase				(    self							,					_A							,					_A=0		):






				'''simple docstring'''







				UpperCAmelCase   		=				floats_tensor((1, 3, 3_2, 3_2)							,					rng=random.Random(_A		)		).to(_A		)
				UpperCAmelCase   		=				image.cpu().permute(0							,					2							,					3							,					1		)[0]
				UpperCAmelCase   		=				Image.fromarray(np.uinta(_A		)		).convert('''RGB'''		).resize((6_4, 6_4)		)
				UpperCAmelCase   		=				Image.fromarray(np.uinta(image + 4		)		).convert('''RGB'''		).resize((6_4, 6_4)		)
				if str(_A		).startswith('''mps'''		):
							UpperCAmelCase   		=				torch.manual_seed(_A		)
				else:
							UpperCAmelCase   		=				torch.Generator(device=_A		).manual_seed(_A		)
				UpperCAmelCase   		=				{
				    '''prompt''': '''A painting of a squirrel eating a burger''',
				    '''image''': init_image,
				    '''mask_image''': mask_image,
				    '''generator''': generator,
				    '''num_inference_steps''': 2,
				    '''guidance_scale''': 6.0,
				    '''output_type''': '''numpy''',
				}
				return inputs
	def      _lowercase				(    self		):






				'''simple docstring'''







				UpperCAmelCase   		=				'''cpu'''  # ensure determinism for the device-dependent torch.Generator
				UpperCAmelCase   		=				self.get_dummy_components()
				UpperCAmelCase   		=				StableDiffusionInpaintPipeline(**_A		)
				UpperCAmelCase   		=				sd_pipe.to(_A		)
				sd_pipe.set_progress_bar_config(disable=_A		)

				UpperCAmelCase   		=				self.get_dummy_inputs(_A		)
				UpperCAmelCase   		=				sd_pipe(**_A		).images
				UpperCAmelCase   		=				image[0, -3:, -3:, -1]

				assert image.shape == (1, 6_4, 6_4, 3)
				UpperCAmelCase   		=				np.array([0.47_27, 0.57_35, 0.39_41, 0.54_46, 0.59_26, 0.43_94, 0.50_62, 0.46_54, 0.44_76]		)

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







	def      _lowercase				(    self		):






				'''simple docstring'''







				super().test_inference_batch_single_identical(expected_max_diff=3E-3		)

@slow
@require_torch_gpu
class 	A_       (unittest.TestCase			):
	def      _lowercase				(    self		):






				'''simple docstring'''







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






				'''simple docstring'''







				UpperCAmelCase   		=				load_image(
				    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'''
				    '''/sd2-inpaint/init_image.png'''		)
				UpperCAmelCase   		=				load_image(
				    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png'''		)
				UpperCAmelCase   		=				load_numpy(
				    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint'''
				    '''/yellow_cat_sitting_on_a_park_bench.npy'''		)

				UpperCAmelCase   		=				'''stabilityai/stable-diffusion-2-inpainting'''
				UpperCAmelCase   		=				StableDiffusionInpaintPipeline.from_pretrained(_A							,					safety_checker=_A		)
				pipe.to(_A		)
				pipe.set_progress_bar_config(disable=_A		)
				pipe.enable_attention_slicing()

				UpperCAmelCase   		=				'''Face of a yellow cat, high resolution, sitting on a park bench'''

				UpperCAmelCase   		=				torch.manual_seed(0		)
				UpperCAmelCase   		=				pipe(
				    prompt=_A							,					image=_A							,					mask_image=_A							,					generator=_A							,					output_type='''np'''							,					)
				UpperCAmelCase   		=				output.images[0]

				assert image.shape == (5_1_2, 5_1_2, 3)
				assert np.abs(expected_image - image		).max() < 9E-3
	def      _lowercase				(    self		):






				'''simple docstring'''







				UpperCAmelCase   		=				load_image(
				    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'''
				    '''/sd2-inpaint/init_image.png'''		)
				UpperCAmelCase   		=				load_image(
				    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png'''		)
				UpperCAmelCase   		=				load_numpy(
				    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint'''
				    '''/yellow_cat_sitting_on_a_park_bench_fp16.npy'''		)

				UpperCAmelCase   		=				'''stabilityai/stable-diffusion-2-inpainting'''
				UpperCAmelCase   		=				StableDiffusionInpaintPipeline.from_pretrained(
				    _A							,					torch_dtype=torch.floataa							,					safety_checker=_A							,					)
				pipe.to(_A		)
				pipe.set_progress_bar_config(disable=_A		)
				pipe.enable_attention_slicing()

				UpperCAmelCase   		=				'''Face of a yellow cat, high resolution, sitting on a park bench'''

				UpperCAmelCase   		=				torch.manual_seed(0		)
				UpperCAmelCase   		=				pipe(
				    prompt=_A							,					image=_A							,					mask_image=_A							,					generator=_A							,					output_type='''np'''							,					)
				UpperCAmelCase   		=				output.images[0]

				assert image.shape == (5_1_2, 5_1_2, 3)
				assert np.abs(expected_image - image		).max() < 5E-1







	def      _lowercase				(    self		):






				'''simple docstring'''







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

				UpperCAmelCase   		=				load_image(
				    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'''
				    '''/sd2-inpaint/init_image.png'''		)
				UpperCAmelCase   		=				load_image(
				    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png'''		)

				UpperCAmelCase   		=				'''stabilityai/stable-diffusion-2-inpainting'''
				UpperCAmelCase   		=				PNDMScheduler.from_pretrained(_A							,					subfolder='''scheduler'''		)
				UpperCAmelCase   		=				StableDiffusionInpaintPipeline.from_pretrained(
				    _A							,					safety_checker=_A							,					scheduler=_A							,					torch_dtype=torch.floataa							,					)
				pipe.to(_A		)
				pipe.set_progress_bar_config(disable=_A		)
				pipe.enable_attention_slicing(1		)
				pipe.enable_sequential_cpu_offload()

				UpperCAmelCase   		=				'''Face of a yellow cat, high resolution, sitting on a park bench'''

				UpperCAmelCase   		=				torch.manual_seed(0		)
				UpperCAmelCase   		=				pipe(
				    prompt=_A							,					image=_A							,					mask_image=_A							,					generator=_A							,					num_inference_steps=2							,					output_type='''np'''							,					)

				UpperCAmelCase   		=				torch.cuda.max_memory_allocated()
				# make sure that less than 2.65 GB is allocated
				assert mem_bytes < 2.65 * 1_0**9










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_5_0, '''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_0_0, '''eval_accuracy''': 0.3, '''eval_loss''': 0.9},
        },
    ]			)
class 	A_       (unittest.TestCase			):
	def      _lowercase				(    self		):






				'''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=_A							,					)
				assert hasattr(self							,					'''env'''		)
	def      _lowercase				(    self							,					_A=1		):






				'''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=_A							,					instance_type=self.instance_type							,					debugger_hook_config=_A							,					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							,					_A		):






				'''simple docstring'''







				TrainingJobAnalytics(_A		).export_csv(F"""{self.env.test_path}/{job_name}_metrics.csv"""		)







	def      _lowercase				(    self		):






				'''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}							,					_A		)



