
import gc
import unittest

import numpy as np
import torch

from diffusers import RePaintPipeline, RePaintScheduler, UNetaDModel
from diffusers.utils.testing_utils import (
    enable_full_determinism,
    load_image,
    load_numpy,
    nightly,
    require_torch_gpu,
    skip_mps,
    torch_device,
)

from ..pipeline_params import IMAGE_INPAINTING_BATCH_PARAMS, IMAGE_INPAINTING_PARAMS
from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()







class 							lowercase						(   SCREAMING_SNAKE_CASE__			,    unittest.TestCase						):
							lowercase_			:			Union[str, Any]			   =RePaintPipeline
							lowercase_			:			Union[str, Any]			   =IMAGE_INPAINTING_PARAMS - {'''width''', '''height''', '''guidance_scale'''}
							lowercase_			:			List[Any]			   =PipelineTesterMixin.required_optional_params - {
							    '''latents''',
							    '''num_images_per_prompt''',
							    '''callback''',
							    '''callback_steps''',
							}
							lowercase_			:			Optional[int]			   =IMAGE_INPAINTING_BATCH_PARAMS
							lowercase_			:			Tuple			   =False




							def 					A__ (							self):
												torch.manual_seed(0)
												torch.manual_seed(0)
												lowercase		      =		UNetaDModel(
												    block_out_channels=(3_2, 6_4)  ,layers_per_block=2  ,sample_size=3_2  ,in_channels=3  ,out_channels=3  ,down_block_types=('''DownBlock2D''', '''AttnDownBlock2D''')  ,up_block_types=('''AttnUpBlock2D''', '''UpBlock2D''')  ,)
												lowercase		      =		RePaintScheduler()
												lowercase		      =		{'''unet''': unet, '''scheduler''': scheduler}
												return components




							def 					A__ (							self  ,A__  ,A__=0):
												if str(A__).startswith('''mps'''):
																	lowercase		      =		torch.manual_seed(A__)
												else:
																	lowercase		      =		torch.Generator(device=A__).manual_seed(A__)
												lowercase		      =		np.random.RandomState(A__).standard_normal((1, 3, 3_2, 3_2))
												lowercase		      =		torch.from_numpy(A__).to(device=A__  ,dtype=torch.floataa)
												lowercase		      =		(image > 0).to(device=A__  ,dtype=torch.floataa)
												lowercase		      =		{
												    '''image''': image,
												    '''mask_image''': mask,
												    '''generator''': generator,
												    '''num_inference_steps''': 5,
												    '''eta''': 0.0,
												    '''jump_length''': 2,
												    '''jump_n_sample''': 2,
												    '''output_type''': '''numpy''',
												}
												return inputs




							def 					A__ (							self):
												lowercase		      =		'''cpu'''  # ensure determinism for the device-dependent torch.Generator
												lowercase		      =		self.get_dummy_components()
												lowercase		      =		RePaintPipeline(**A__)
												lowercase		      =		sd_pipe.to(A__)
												sd_pipe.set_progress_bar_config(disable=A__)

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

												assert image.shape == (1, 3_2, 3_2, 3)
												lowercase		      =		np.array([1.0000, 0.5426, 0.5497, 0.2200, 1.0000, 1.0000, 0.5623, 1.0000, 0.6274])

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




							@skip_mps
							def 					A__ (							self):
												return super().test_save_load_local()




							@unittest.skip('''non-deterministic pipeline''')
							def 					A__ (							self):
												return super().test_inference_batch_single_identical()




							@skip_mps
							def 					A__ (							self):
												return super().test_dict_tuple_outputs_equivalent()




							@skip_mps
							def 					A__ (							self):
												return super().test_save_load_optional_components()





							@skip_mps
							def 					A__ (							self):
												return super().test_attention_slicing_forward_pass()







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




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





							def 					A__ (							self):
												lowercase		      =		load_image(
												    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/'''
												    '''repaint/celeba_hq_256.png''')
												lowercase		      =		load_image(
												    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/repaint/mask_256.png''')
												lowercase		      =		load_numpy(
												    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/'''
												    '''repaint/celeba_hq_256_result.npy''')

												lowercase		      =		'''google/ddpm-ema-celebahq-256'''
												lowercase		      =		UNetaDModel.from_pretrained(A__)
												lowercase		      =		RePaintScheduler.from_pretrained(A__)

												lowercase		      =		RePaintPipeline(unet=A__  ,scheduler=A__).to(A__)
												repaint.set_progress_bar_config(disable=A__)
												repaint.enable_attention_slicing()

												lowercase		      =		torch.manual_seed(0)
												lowercase		      =		repaint(
												    A__  ,A__  ,num_inference_steps=2_5_0  ,eta=0.0  ,jump_length=1_0  ,jump_n_sample=1_0  ,generator=A__  ,output_type='''np'''  ,)
												lowercase		      =		output.images[0]

												assert image.shape == (2_5_6, 2_5_6, 3)
												assert np.abs(expected_image - image).mean() < 1E-2






import os
import random
from pathlib import Path
from typing import List

import numpy as np
import torch
from torch.cuda.amp import GradScaler

from .utils import (
    MODEL_NAME,
    OPTIMIZER_NAME,
    RNG_STATE_NAME,
    SCALER_NAME,
    SCHEDULER_NAME,
    get_pretty_name,
    is_tpu_available,
    is_xpu_available,
    save,
)


if is_tpu_available(check_device=False):
							import torch_xla.core.xla_model as xm

from .logging import get_logger
from .state import PartialState


lowercase__						:Optional[Any]					    =       get_logger(__name__)



def 			UpperCamelCase       (				lowerCAmelCase__					,     lowerCAmelCase__					,     lowerCAmelCase__					,     lowerCAmelCase__					,     lowerCAmelCase__					,     lowerCAmelCase__ = None					,     ):
					'''simple docstring'''
					# Model states
					for i, state in enumerate(lowerCAmelCase__						):
										lowercase		      =		f'{MODEL_NAME}.bin' if i == 0 else f'{MODEL_NAME}_{i}.bin'
										lowercase		      =		os.path.join(lowerCAmelCase__					,     lowerCAmelCase__						)
										save(lowerCAmelCase__					,     lowerCAmelCase__						)
										logger.info(f'Model weights saved in {output_model_file}'						)
					# Optimizer states
					for i, opt in enumerate(lowerCAmelCase__						):
										lowercase		      =		opt.state_dict()
										lowercase		      =		f'{OPTIMIZER_NAME}.bin' if i == 0 else f'{OPTIMIZER_NAME}_{i}.bin'
										lowercase		      =		os.path.join(lowerCAmelCase__					,     lowerCAmelCase__						)
										save(lowerCAmelCase__					,     lowerCAmelCase__						)
										logger.info(f'Optimizer state saved in {output_optimizer_file}'						)
					# Scheduler states
					for i, scheduler in enumerate(lowerCAmelCase__						):
										lowercase		      =		scheduler.state_dict()
										lowercase		      =		f'{SCHEDULER_NAME}.bin' if i == 0 else f'{SCHEDULER_NAME}_{i}.bin'
										lowercase		      =		os.path.join(lowerCAmelCase__					,     lowerCAmelCase__						)
										save(lowerCAmelCase__					,     lowerCAmelCase__						)
										logger.info(f'Scheduler state saved in {output_scheduler_file}'						)
					# GradScaler state
					if scaler is not None:
										lowercase		      =		scaler.state_dict()
										lowercase		      =		os.path.join(lowerCAmelCase__					,     lowerCAmelCase__						)
										torch.save(lowerCAmelCase__					,     lowerCAmelCase__						)
										logger.info(f'Gradient scaler state saved in {output_scaler_file}'						)
					# Random number generator states
					lowercase		      =		{}
					lowercase		      =		f'{RNG_STATE_NAME}_{process_index}.pkl'
					lowercase		      =		random.getstate()
					lowercase		      =		np.random.get_state()
					lowercase		      =		torch.get_rng_state()
					if is_xpu_available():
										lowercase		      =		torch.xpu.get_rng_state_all()
					else:
										lowercase		      =		torch.cuda.get_rng_state_all()
					if is_tpu_available():
										lowercase		      =		xm.get_rng_state()
					lowercase		      =		os.path.join(lowerCAmelCase__					,     lowerCAmelCase__						)
					torch.save(lowerCAmelCase__					,     lowerCAmelCase__						)
					logger.info(f'Random states saved in {output_states_file}'						)
					return output_dir



def 			UpperCamelCase       (				lowerCAmelCase__					,     lowerCAmelCase__					,     lowerCAmelCase__					,     lowerCAmelCase__					,     lowerCAmelCase__					,     lowerCAmelCase__=None					,     lowerCAmelCase__=None					,     **lowerCAmelCase__					,     ):
					'''simple docstring'''
					if map_location not in [None, "cpu", "on_device"]:
										raise TypeError(
										    '''Unsupported optimizer map location passed, please choose one of `None`, `\'cpu\'`, or `\'on_device\'`'''						)
					if map_location is None:
										lowercase		      =		'''cpu'''
					elif map_location == "on_device":
										lowercase		      =		PartialState().device
					# Model states
					for i, model in enumerate(lowerCAmelCase__						):
										lowercase		      =		f'{MODEL_NAME}.bin' if i == 0 else f'{MODEL_NAME}_{i}.bin'
										lowercase		      =		os.path.join(lowerCAmelCase__					,     lowerCAmelCase__						)
										models[i].load_state_dict(torch.load(lowerCAmelCase__					,     map_location=lowerCAmelCase__						)					,     **lowerCAmelCase__						)
					logger.info('''All model weights loaded successfully'''						)

					# Optimizer states
					for i, opt in enumerate(lowerCAmelCase__						):
										lowercase		      =		f'{OPTIMIZER_NAME}.bin' if i == 0 else f'{OPTIMIZER_NAME}_{i}.bin'
										lowercase		      =		os.path.join(lowerCAmelCase__					,     lowerCAmelCase__						)
										lowercase		      =		torch.load(lowerCAmelCase__					,     map_location=lowerCAmelCase__						)
										optimizers[i].load_state_dict(lowerCAmelCase__						)
					logger.info('''All optimizer states loaded successfully'''						)

					# Scheduler states
					for i, scheduler in enumerate(lowerCAmelCase__						):
										lowercase		      =		f'{SCHEDULER_NAME}.bin' if i == 0 else f'{SCHEDULER_NAME}_{i}.bin'
										lowercase		      =		os.path.join(lowerCAmelCase__					,     lowerCAmelCase__						)
										scheduler.load_state_dict(torch.load(lowerCAmelCase__						)						)
					logger.info('''All scheduler states loaded successfully'''						)

					# GradScaler state
					if scaler is not None:
										lowercase		      =		os.path.join(lowerCAmelCase__					,     lowerCAmelCase__						)
										scaler.load_state_dict(torch.load(lowerCAmelCase__						)						)
										logger.info('''GradScaler state loaded successfully'''						)

					# Random states
					try:
										lowercase		      =		torch.load(os.path.join(lowerCAmelCase__					,     f'{RNG_STATE_NAME}_{process_index}.pkl'						)						)
										random.setstate(states['''random_state''']						)
										np.random.set_state(states['''numpy_random_seed''']						)
										torch.set_rng_state(states['''torch_manual_seed''']						)
										if is_xpu_available():
															torch.xpu.set_rng_state_all(states['''torch_xpu_manual_seed''']						)
										else:
															torch.cuda.set_rng_state_all(states['''torch_cuda_manual_seed''']						)
										if is_tpu_available():
															xm.set_rng_state(states['''xm_seed''']						)
										logger.info('''All random states loaded successfully'''						)
					except Exception:
										logger.info('''Could not load random states'''						)



def 			UpperCamelCase       (				lowerCAmelCase__					,     lowerCAmelCase__					,     lowerCAmelCase__ = 0						):
					'''simple docstring'''
					lowercase		      =		Path(lowerCAmelCase__						) / f'custom_checkpoint_{index}.pkl'
					logger.info(f'Saving the state of {get_pretty_name(lowerCAmelCase__						)} to {save_location}'						)
					torch.save(obj.state_dict()					,     lowerCAmelCase__						)



def 			UpperCamelCase       (				lowerCAmelCase__					,     lowerCAmelCase__					,     lowerCAmelCase__ = 0						):
					'''simple docstring'''
					lowercase		      =		f'{path}/custom_checkpoint_{index}.pkl'
					logger.info(f'Loading the state of {get_pretty_name(lowerCAmelCase__						)} from {load_location}'						)
					obj.load_state_dict(torch.load(lowerCAmelCase__					,     map_location='''cpu'''						)						)




