'''simple docstring'''
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


a=	get_logger(__name__)







def 			_UpperCamelCase			(  _a						:						str					,  _a						:						List[dict]					,  _a						:						list					,  _a						:						list					,  _a						:						int					,  _a						:						GradScaler = None					,  ):




    """simple docstring"""



    for i, state in enumerate(_a      ):
        __UpperCamelCase						:						List[Any]		     =	f"""{MODEL_NAME}.bin""" if i == 0 else f"""{MODEL_NAME}_{i}.bin"""
        __UpperCamelCase						:						Union[str, Any]		     =	os.path.join(_a					,  _a      )
        save(_a					,  _a      )
        logger.info(f"""Model weights saved in {output_model_file}"""      )
    # Optimizer states
    for i, opt in enumerate(_a      ):
        __UpperCamelCase						:						List[str]		     =	opt.state_dict()
        __UpperCamelCase						:						List[Any]		     =	f"""{OPTIMIZER_NAME}.bin""" if i == 0 else f"""{OPTIMIZER_NAME}_{i}.bin"""
        __UpperCamelCase						:						str		     =	os.path.join(_a					,  _a      )
        save(_a					,  _a      )
        logger.info(f"""Optimizer state saved in {output_optimizer_file}"""      )
    # Scheduler states
    for i, scheduler in enumerate(_a      ):
        __UpperCamelCase						:						str		     =	scheduler.state_dict()
        __UpperCamelCase						:						Tuple		     =	f"""{SCHEDULER_NAME}.bin""" if i == 0 else f"""{SCHEDULER_NAME}_{i}.bin"""
        __UpperCamelCase						:						Any		     =	os.path.join(_a					,  _a      )
        save(_a					,  _a      )
        logger.info(f"""Scheduler state saved in {output_scheduler_file}"""      )
    # GradScaler state
    if scaler is not None:
        __UpperCamelCase						:						str		     =	scaler.state_dict()
        __UpperCamelCase						:						List[Any]		     =	os.path.join(_a					,  _a      )
        torch.save(_a					,  _a      )
        logger.info(f"""Gradient scaler state saved in {output_scaler_file}"""      )
    # Random number generator states
    __UpperCamelCase						:						Optional[int]		     =	{}
    __UpperCamelCase						:						Union[str, Any]		     =	f"""{RNG_STATE_NAME}_{process_index}.pkl"""
    __UpperCamelCase						:						List[str]		     =	random.getstate()
    __UpperCamelCase						:						Union[str, Any]		     =	np.random.get_state()
    __UpperCamelCase						:						Optional[int]		     =	torch.get_rng_state()
    if is_xpu_available():
        __UpperCamelCase						:						Optional[int]		     =	torch.xpu.get_rng_state_all()
    else:
        __UpperCamelCase						:						str		     =	torch.cuda.get_rng_state_all()
    if is_tpu_available():
        __UpperCamelCase						:						List[str]		     =	xm.get_rng_state()
    __UpperCamelCase						:						str		     =	os.path.join(_a					,  _a      )
    torch.save(_a					,  _a      )
    logger.info(f"""Random states saved in {output_states_file}"""      )
    return output_dir







def 			_UpperCamelCase			(  _a						:						List[str]					,  _a						:						Any					,  _a						:						Optional[int]					,  _a						:						int					,  _a						:						List[Any]					,  _a						:						List[str]=None					,  _a						:						Dict=None					,  **_a						:						Any					,  ):




    """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:
        __UpperCamelCase						:						List[str]		     =	'cpu'
    elif map_location == "on_device":
        __UpperCamelCase						:						Optional[int]		     =	PartialState().device
    # Model states
    for i, model in enumerate(_a      ):
        __UpperCamelCase						:						List[Any]		     =	f"""{MODEL_NAME}.bin""" if i == 0 else f"""{MODEL_NAME}_{i}.bin"""
        __UpperCamelCase						:						Optional[Any]		     =	os.path.join(_a					,  _a      )
        models[i].load_state_dict(torch.load(_a					,  map_location=_a      )					,  **_a      )
    logger.info('All model weights loaded successfully'      )

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

    # Scheduler states
    for i, scheduler in enumerate(_a      ):
        __UpperCamelCase						:						Tuple		     =	f"""{SCHEDULER_NAME}.bin""" if i == 0 else f"""{SCHEDULER_NAME}_{i}.bin"""
        __UpperCamelCase						:						List[str]		     =	os.path.join(_a					,  _a      )
        scheduler.load_state_dict(torch.load(_a      )      )
    logger.info('All scheduler states loaded successfully'      )

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

    # Random states
    try:
        __UpperCamelCase						:						Union[str, Any]		     =	torch.load(os.path.join(_a					,  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			(  _a						:						Optional[Any]					,  _a						:						Any					,  _a						:						int = 0      ):




    """simple docstring"""



    __UpperCamelCase						:						int		     =	Path(_a      ) / f"""custom_checkpoint_{index}.pkl"""
    logger.info(f"""Saving the state of {get_pretty_name(_a      )} to {save_location}"""      )
    torch.save(obj.state_dict()					,  _a      )







def 			_UpperCamelCase			(  _a						:						List[Any]					,  _a						:						List[str]					,  _a						:						int = 0      ):




    """simple docstring"""



    __UpperCamelCase						:						Optional[int]		     =	f"""{path}/custom_checkpoint_{index}.pkl"""
    logger.info(f"""Loading the state of {get_pretty_name(_a      )} from {load_location}"""      )
    obj.load_state_dict(torch.load(_a					,  map_location='cpu'      )      )




'''simple docstring'''
import random
import unittest

import numpy as np
import torch

from diffusers import DDIMScheduler, LDMSuperResolutionPipeline, UNetaDModel, VQModel
from diffusers.utils import PIL_INTERPOLATION, floats_tensor, load_image, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch


enable_full_determinism()






class       __lowercase		(    unittest.TestCase    ):







     """simple docstring"""






     @property
     def lowerCAmelCase  (		self     ):
         __UpperCamelCase						:						Dict		     =	1
         __UpperCamelCase						:						Union[str, Any]		     =	3
         __UpperCamelCase						:						Tuple		     =	(3_2, 3_2)

         __UpperCamelCase						:						List[Any]		     =	floats_tensor((batch_size, num_channels) + sizes  ,      rng=random.Random(0     )     ).to(_lowerCamelCase     )
         return image







     @property
     def lowerCAmelCase  (		self     ):
         torch.manual_seed(0     )
         __UpperCamelCase						:						Dict		     =	UNetaDModel(
             block_out_channels=(3_2, 6_4)  ,      layers_per_block=2  ,      sample_size=3_2  ,      in_channels=6  ,      out_channels=3  ,      down_block_types=('DownBlock2D', 'AttnDownBlock2D')  ,      up_block_types=('AttnUpBlock2D', 'UpBlock2D')  ,      )
         return model







     @property
     def lowerCAmelCase  (		self     ):
         torch.manual_seed(0     )
         __UpperCamelCase						:						List[str]		     =	VQModel(
             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=3  ,      )
         return model







     def lowerCAmelCase  (		self     ):
         __UpperCamelCase						:						Tuple		     =	'cpu'
         __UpperCamelCase						:						Union[str, Any]		     =	self.dummy_uncond_unet
         __UpperCamelCase						:						List[str]		     =	DDIMScheduler()
         __UpperCamelCase						:						Any		     =	self.dummy_vq_model

         __UpperCamelCase						:						Tuple		     =	LDMSuperResolutionPipeline(unet=_lowerCamelCase  ,      vqvae=_lowerCamelCase  ,      scheduler=_lowerCamelCase     )
         ldm.to(_lowerCamelCase     )
         ldm.set_progress_bar_config(disable=_lowerCamelCase     )

         __UpperCamelCase						:						int		     =	self.dummy_image.to(_lowerCamelCase     )

         __UpperCamelCase						:						Union[str, Any]		     =	torch.Generator(device=_lowerCamelCase     ).manual_seed(0     )
         __UpperCamelCase						:						Union[str, Any]		     =	ldm(image=_lowerCamelCase  ,      generator=_lowerCamelCase  ,      num_inference_steps=2  ,      output_type='numpy'     ).images

         __UpperCamelCase						:						str		     =	image[0, -3:, -3:, -1]

         assert image.shape == (1, 6_4, 6_4, 3)
         __UpperCamelCase						:						str		     =	np.array([0.8_6_7_8, 0.8_2_4_5, 0.6_3_8_1, 0.6_8_3_0, 0.4_3_8_5, 0.5_5_9_9, 0.4_6_4_1, 0.6_2_0_1, 0.5_1_5_0]     )

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







     @unittest.skipIf(torch_device != 'cuda'  ,      'This test requires a GPU'     )
     def lowerCAmelCase  (		self     ):
         __UpperCamelCase						:						Optional[Any]		     =	self.dummy_uncond_unet
         __UpperCamelCase						:						Optional[int]		     =	DDIMScheduler()
         __UpperCamelCase						:						Any		     =	self.dummy_vq_model

         # put models in fp16
         __UpperCamelCase						:						str		     =	unet.half()
         __UpperCamelCase						:						Optional[Any]		     =	vqvae.half()

         __UpperCamelCase						:						Optional[int]		     =	LDMSuperResolutionPipeline(unet=_lowerCamelCase  ,      vqvae=_lowerCamelCase  ,      scheduler=_lowerCamelCase     )
         ldm.to(_lowerCamelCase     )
         ldm.set_progress_bar_config(disable=_lowerCamelCase     )

         __UpperCamelCase						:						Dict		     =	self.dummy_image.to(_lowerCamelCase     )

         __UpperCamelCase						:						Optional[Any]		     =	ldm(_lowerCamelCase  ,      num_inference_steps=2  ,      output_type='numpy'     ).images

         assert image.shape == (1, 6_4, 6_4, 3)






@slow
@require_torch
class       __lowercase		(    unittest.TestCase    ):







     """simple docstring"""






     def lowerCAmelCase  (		self     ):
         __UpperCamelCase						:						List[Any]		     =	load_image(
             'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'
             '/vq_diffusion/teddy_bear_pool.png'     )
         __UpperCamelCase						:						List[Any]		     =	init_image.resize((6_4, 6_4)  ,      resample=PIL_INTERPOLATION['lanczos']     )

         __UpperCamelCase						:						List[str]		     =	LDMSuperResolutionPipeline.from_pretrained('duongna/ldm-super-resolution'  ,      device_map='auto'     )
         ldm.set_progress_bar_config(disable=_lowerCamelCase     )

         __UpperCamelCase						:						Union[str, Any]		     =	torch.manual_seed(0     )
         __UpperCamelCase						:						Dict		     =	ldm(image=_lowerCamelCase  ,      generator=_lowerCamelCase  ,      num_inference_steps=2_0  ,      output_type='numpy'     ).images

         __UpperCamelCase						:						List[Any]		     =	image[0, -3:, -3:, -1]

         assert image.shape == (1, 2_5_6, 2_5_6, 3)
         __UpperCamelCase						:						str		     =	np.array([0.7_6_4_4, 0.7_6_7_9, 0.7_6_4_2, 0.7_6_3_3, 0.7_6_6_6, 0.7_5_6_0, 0.7_4_2_5, 0.7_2_5_7, 0.6_9_0_7]     )

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



