



import math
from dataclasses import dataclass
from typing import List, Optional, Tuple, Union

import numpy as np
import torch

from ..configuration_utils import ConfigMixin, register_to_config
from ..utils import BaseOutput, logging, randn_tensor
from .scheduling_utils import KarrasDiffusionSchedulers, SchedulerMixin


UpperCAmelCase :				int							=logging.get_logger(__name__)  # pylint: disable=invalid-name


@dataclass
# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput with DDPM->EulerDiscrete
class    _lowercase (lowerCamelCase_   ):
				'''simple docstring'''







				lowercase__     =    42
				lowercase__     =    None




def  _lowerCAmelCase     (_lowerCAmelCase ,						_lowerCAmelCase=0.999 ,						_lowerCAmelCase="cosine" ,						):


					if alpha_transform_type == "cosine":

										def alpha_bar_fn(_lowerCAmelCase):
															return math.cos((t + 0.008) / 1.008 * math.pi / 2) ** 2

					elif alpha_transform_type == "exp":

										def alpha_bar_fn(_lowerCAmelCase):
															return math.exp(t * -12.0)

					else:
										raise ValueError(f"""Unsupported alpha_tranform_type: {alpha_transform_type}""")

					UpperCamelCase_			= []
					for i in range(__snake_case):
										UpperCamelCase_			= i / num_diffusion_timesteps
										UpperCamelCase_			= (i + 1) / num_diffusion_timesteps
										betas.append(min(1 - alpha_bar_fn(__snake_case) / alpha_bar_fn(__snake_case) ,						__snake_case))
					return torch.tensor(__snake_case ,						dtype=torch.floataa)






class    _lowercase (lowerCamelCase_   , lowerCamelCase_   ):
				'''simple docstring'''







				lowercase__     =    [e.name for e in KarrasDiffusionSchedulers]
				lowercase__     =    1

				@register_to_config
				def __init__(			self  ,			snake_case__ = 1000  ,			snake_case__ = 0.0_001  ,			snake_case__ = 0.02  ,			snake_case__ = "linear"  ,			snake_case__ = None  ,			snake_case__ = "epsilon"  ,			snake_case__ = "linear"  ,			snake_case__ = False  ,			snake_case__ = "linspace"  ,			snake_case__ = 0  ,			):

									'''simple docstring'''


									if trained_betas is not None:
														UpperCamelCase_			= torch.tensor(__UpperCAmelCase  ,			dtype=torch.floataa     )
									elif beta_schedule == "linear":
														UpperCamelCase_			= torch.linspace(__UpperCAmelCase  ,			__UpperCAmelCase  ,			__UpperCAmelCase  ,			dtype=torch.floataa     )
									elif beta_schedule == "scaled_linear":
														# this schedule is very specific to the latent diffusion model.
														UpperCamelCase_			= (
														    torch.linspace(beta_start**0.5  ,			beta_end**0.5  ,			__UpperCAmelCase  ,			dtype=torch.floataa     ) ** 2
														)
									elif beta_schedule == "squaredcos_cap_v2":
														# Glide cosine schedule
														UpperCamelCase_			= betas_for_alpha_bar(__UpperCAmelCase     )
									else:
														raise NotImplementedError(F"""{beta_schedule} does is not implemented for {self.__class__}"""     )

									UpperCamelCase_			= 1.0 - self.betas
									UpperCamelCase_			= torch.cumprod(self.alphas  ,			dim=0     )

									UpperCamelCase_			= np.array(((1 - self.alphas_cumprod) / self.alphas_cumprod) ** 0.5     )
									UpperCamelCase_			= np.concatenate([sigmas[::-1], [0.0]]     ).astype(np.floataa     )
									UpperCamelCase_			= torch.from_numpy(__UpperCAmelCase     )

									# setable values
									UpperCamelCase_			= None
									UpperCamelCase_			= np.linspace(0  ,			num_train_timesteps - 1  ,			__UpperCAmelCase  ,			dtype=__UpperCAmelCase     )[::-1].copy()
									UpperCamelCase_			= torch.from_numpy(__UpperCAmelCase     )
									UpperCamelCase_			= False
									UpperCamelCase_			= use_karras_sigmas

				@property
				def    _lowerCamelCase       (			self     ):

									'''simple docstring'''


									if self.config.timestep_spacing in ["linspace", "trailing"]:
														return self.sigmas.max()

									return (self.sigmas.max() ** 2 + 1) ** 0.5

				def    _lowerCamelCase       (			self  ,			snake_case__  ,			snake_case__     ):

									'''simple docstring'''


									if isinstance(__UpperCAmelCase  ,			torch.Tensor     ):
														UpperCamelCase_			= timestep.to(self.timesteps.device     )
									UpperCamelCase_			= (self.timesteps == timestep).nonzero().item()
									UpperCamelCase_			= self.sigmas[step_index]

									UpperCamelCase_			= sample / ((sigma**2 + 1) ** 0.5)

									UpperCamelCase_			= True
									return sample

				def    _lowerCamelCase       (			self  ,			snake_case__  ,			snake_case__ = None     ):

									'''simple docstring'''


									UpperCamelCase_			= num_inference_steps

									# "linspace", "leading", "trailing" corresponds to annotation of Table 2. of https://arxiv.org/abs/2305.08891
									if self.config.timestep_spacing == "linspace":
														UpperCamelCase_			= np.linspace(0  ,			self.config.num_train_timesteps - 1  ,			__UpperCAmelCase  ,			dtype=__UpperCAmelCase     )[
														    ::-1
														].copy()
									elif self.config.timestep_spacing == "leading":
														UpperCamelCase_			= self.config.num_train_timesteps // self.num_inference_steps
														# creates integer timesteps by multiplying by ratio
														# casting to int to avoid issues when num_inference_step is power of 3
														UpperCamelCase_			= (np.arange(0  ,			__UpperCAmelCase     ) * step_ratio).round()[::-1].copy().astype(__UpperCAmelCase     )
														timesteps += self.config.steps_offset
									elif self.config.timestep_spacing == "trailing":
														UpperCamelCase_			= self.config.num_train_timesteps / self.num_inference_steps
														# creates integer timesteps by multiplying by ratio
														# casting to int to avoid issues when num_inference_step is power of 3
														UpperCamelCase_			= (np.arange(self.config.num_train_timesteps  ,			0  ,			-step_ratio     )).round().copy().astype(__UpperCAmelCase     )
														timesteps -= 1
									else:
														raise ValueError(
														    F"""{self.config.timestep_spacing} is not supported. Please make sure to choose one of \'linspace\', \'leading\' or \'trailing\'."""     )

									UpperCamelCase_			= np.array(((1 - self.alphas_cumprod) / self.alphas_cumprod) ** 0.5     )
									UpperCamelCase_			= np.log(__UpperCAmelCase     )

									if self.config.interpolation_type == "linear":
														UpperCamelCase_			= np.interp(__UpperCAmelCase  ,			np.arange(0  ,			len(__UpperCAmelCase     )     )  ,			__UpperCAmelCase     )
									elif self.config.interpolation_type == "log_linear":
														UpperCamelCase_			= torch.linspace(np.log(sigmas[-1]     )  ,			np.log(sigmas[0]     )  ,			num_inference_steps + 1     ).exp()
									else:
														raise ValueError(
														    F"""{self.config.interpolation_type} is not implemented. Please specify interpolation_type to either"""
														    " \'linear\' or \'log_linear\'"     )

									if self.use_karras_sigmas:
														UpperCamelCase_			= self._convert_to_karras(in_sigmas=__UpperCAmelCase  ,			num_inference_steps=self.num_inference_steps     )
														UpperCamelCase_			= np.array([self._sigma_to_t(__UpperCAmelCase  ,			__UpperCAmelCase     ) for sigma in sigmas]     )

									UpperCamelCase_			= np.concatenate([sigmas, [0.0]]     ).astype(np.floataa     )
									UpperCamelCase_			= torch.from_numpy(__UpperCAmelCase     ).to(device=__UpperCAmelCase     )
									if str(__UpperCAmelCase     ).startswith("mps"     ):
														# mps does not support float64
														UpperCamelCase_			= torch.from_numpy(__UpperCAmelCase     ).to(__UpperCAmelCase  ,			dtype=torch.floataa     )
									else:
														UpperCamelCase_			= torch.from_numpy(__UpperCAmelCase     ).to(device=__UpperCAmelCase     )

				def    _lowerCamelCase       (			self  ,			snake_case__  ,			snake_case__     ):

									'''simple docstring'''


									UpperCamelCase_			= np.log(__UpperCAmelCase     )

									# get distribution
									UpperCamelCase_			= log_sigma - log_sigmas[:, np.newaxis]

									# get sigmas range
									UpperCamelCase_			= np.cumsum((dists >= 0)  ,			axis=0     ).argmax(axis=0     ).clip(max=log_sigmas.shape[0] - 2     )
									UpperCamelCase_			= low_idx + 1

									UpperCamelCase_			= log_sigmas[low_idx]
									UpperCamelCase_			= log_sigmas[high_idx]

									# interpolate sigmas
									UpperCamelCase_			= (low - log_sigma) / (low - high)
									UpperCamelCase_			= np.clip(__UpperCAmelCase  ,			0  ,			1     )

									# transform interpolation to time range
									UpperCamelCase_			= (1 - w) * low_idx + w * high_idx
									UpperCamelCase_			= t.reshape(sigma.shape     )
									return t

				def    _lowerCamelCase       (			self  ,			snake_case__  ,			snake_case__     ):

									'''simple docstring'''


									UpperCamelCase_			= in_sigmas[-1].item()
									UpperCamelCase_			= in_sigmas[0].item()

									UpperCamelCase_			= 7.0  # 7.0 is the value used in the paper
									UpperCamelCase_			= np.linspace(0  ,			1  ,			__UpperCAmelCase     )
									UpperCamelCase_			= sigma_min ** (1 / rho)
									UpperCamelCase_			= sigma_max ** (1 / rho)
									UpperCamelCase_			= (max_inv_rho + ramp * (min_inv_rho - max_inv_rho)) ** rho
									return sigmas

				def    _lowerCamelCase       (			self  ,			snake_case__  ,			snake_case__  ,			snake_case__  ,			snake_case__ = 0.0  ,			snake_case__ = 0.0  ,			snake_case__ = float("inf"     )  ,			snake_case__ = 1.0  ,			snake_case__ = None  ,			snake_case__ = True  ,			):

									'''simple docstring'''


									if (
									    isinstance(__UpperCAmelCase  ,			__UpperCAmelCase     )
									    or isinstance(__UpperCAmelCase  ,			torch.IntTensor     )
									    or isinstance(__UpperCAmelCase  ,			torch.LongTensor     )
									):
														raise ValueError(
														    (
														        "Passing integer indices (e.g. from `enumerate(timesteps)`) as timesteps to"
														        " `EulerDiscreteScheduler.step()` is not supported. Make sure to pass"
														        " one of the `scheduler.timesteps` as a timestep."
														    )  ,			)

									if not self.is_scale_input_called:
														logger.warning(
														    "The `scale_model_input` function should be called before `step` to ensure correct denoising. "
														    "See `StableDiffusionPipeline` for a usage example."     )

									if isinstance(__UpperCAmelCase  ,			torch.Tensor     ):
														UpperCamelCase_			= timestep.to(self.timesteps.device     )

									UpperCamelCase_			= (self.timesteps == timestep).nonzero().item()
									UpperCamelCase_			= self.sigmas[step_index]

									UpperCamelCase_			= min(s_churn / (len(self.sigmas     ) - 1)  ,			2**0.5 - 1     ) if s_tmin <= sigma <= s_tmax else 0.0

									UpperCamelCase_			= randn_tensor(
									    model_output.shape  ,			dtype=model_output.dtype  ,			device=model_output.device  ,			generator=__UpperCAmelCase     )

									UpperCamelCase_			= noise * s_noise
									UpperCamelCase_			= sigma * (gamma + 1)

									if gamma > 0:
														UpperCamelCase_			= sample + eps * (sigma_hat**2 - sigma**2) ** 0.5

									# 1. compute predicted original sample (x_0) from sigma-scaled predicted noise
									# NOTE: "original_sample" should not be an expected prediction_type but is left in for
									# backwards compatibility
									if self.config.prediction_type == "original_sample" or self.config.prediction_type == "sample":
														UpperCamelCase_			= model_output
									elif self.config.prediction_type == "epsilon":
														UpperCamelCase_			= sample - sigma_hat * model_output
									elif self.config.prediction_type == "v_prediction":
														# * c_out + input * c_skip
														UpperCamelCase_			= model_output * (-sigma / (sigma**2 + 1) ** 0.5) + (sample / (sigma**2 + 1))
									else:
														raise ValueError(
														    F"""prediction_type given as {self.config.prediction_type} must be one of `epsilon`, or `v_prediction`"""     )

									# 2. Convert to an ODE derivative
									UpperCamelCase_			= (sample - pred_original_sample) / sigma_hat

									UpperCamelCase_			= self.sigmas[step_index + 1] - sigma_hat

									UpperCamelCase_			= sample + derivative * dt

									if not return_dict:
														return (prev_sample,)

									return EulerDiscreteSchedulerOutput(prev_sample=__UpperCAmelCase  ,			pred_original_sample=__UpperCAmelCase     )

				def    _lowerCamelCase       (			self  ,			snake_case__  ,			snake_case__  ,			snake_case__  ,			):

									'''simple docstring'''


									UpperCamelCase_			= self.sigmas.to(device=original_samples.device  ,			dtype=original_samples.dtype     )
									if original_samples.device.type == "mps" and torch.is_floating_point(__UpperCAmelCase     ):
														# mps does not support float64
														UpperCamelCase_			= self.timesteps.to(original_samples.device  ,			dtype=torch.floataa     )
														UpperCamelCase_			= timesteps.to(original_samples.device  ,			dtype=torch.floataa     )
									else:
														UpperCamelCase_			= self.timesteps.to(original_samples.device     )
														UpperCamelCase_			= timesteps.to(original_samples.device     )

									UpperCamelCase_			= [(schedule_timesteps == t).nonzero().item() for t in timesteps]

									UpperCamelCase_			= sigmas[step_indices].flatten()
									while len(sigma.shape     ) < len(original_samples.shape     ):
														UpperCamelCase_			= sigma.unsqueeze(-1     )

									UpperCamelCase_			= original_samples + noise * sigma
									return noisy_samples

				def __len__(			self     ):

									'''simple docstring'''


									return self.config.num_train_timesteps






import json
import os
from collections.abc import Mapping
from typing import Dict, List, Optional, Union

import numpy as np
import torch

from .imports import is_safetensors_available




def  a_		(							__snake_case     ,		__snake_case     ,		__snake_case     ,		__snake_case=None					)	->			str:

			'''simple docstring'''


			UpperCamelCase_     				= None
			# Check the string instead of the dtype to be compatible with versions of PyTorch that don't have bfloat16.
			if str(weight.dtype					) == "torch.bfloat16":
						# Need to reinterpret the underlined data as int16 since NumPy does not handle bfloat16s.
						UpperCamelCase_     				= weight.view(torch.intaa					)
						UpperCamelCase_     				= 'bfloat16'
			UpperCamelCase_     				= weight.cpu().numpy()
			UpperCamelCase_     				= os.path.join(__snake_case     ,		F'''{weight_name}.dat'''					)
			if index is not None:
						if dtype is None:
									UpperCamelCase_     				= str(array.dtype					)
						UpperCamelCase_     				= {'dtype': dtype, 'shape': list(array.shape					)}
			if array.ndim == 0:
						UpperCamelCase_     				= array[None]
			UpperCamelCase_     				= np.memmap(__snake_case     ,		dtype=array.dtype     ,		mode='w+'     ,		shape=array.shape					)
			UpperCamelCase_     				= array[:]
			file_array.flush()
			return index




def  a_		(							__snake_case     ,		__snake_case					)	->			Dict:

			'''simple docstring'''


			UpperCamelCase_     				= tuple(weight_info['shape']					)
			if shape == ():
						# NumPy memory-mapped arrays can't have 0 dims so it was saved as 1d tensor
						UpperCamelCase_     				= (1,)

			UpperCamelCase_     				= weight_info['dtype']
			if dtype == "bfloat16":
						# NumPy does not support bfloat16 so this was saved as a int16
						UpperCamelCase_     				= 'int16'

			UpperCamelCase_     				= np.memmap(__snake_case     ,		dtype=__snake_case     ,		shape=__snake_case     ,		mode='r'					)

			if len(weight_info['shape']					) == 0:
						UpperCamelCase_     				= weight[0]
			UpperCamelCase_     				= torch.tensor(__snake_case					)
			if weight_info["dtype"] == "bfloat16":
						UpperCamelCase_     				= weight.view(torch.bfloataa					)

			return weight




def  a_		(							__snake_case     ,		__snake_case					)	->			Optional[int]:

			'''simple docstring'''


			if index is None or len(__snake_case					) == 0:
						# Nothing to save
						return

			UpperCamelCase_     				= os.path.join(__snake_case     ,		'index.json'					)
			if os.path.isfile(__snake_case					):
						with open(__snake_case     ,		'r'     ,		encoding='utf-8'					) as f:
									UpperCamelCase_     				= json.load(__snake_case					)
			else:
						UpperCamelCase_     				= {}
			current_index.update(__snake_case					)

			with open(__snake_case     ,		'w'     ,		encoding='utf-8'					) as f:
						json.dump(__snake_case     ,		__snake_case     ,		indent=2					)




def  a_		(							__snake_case     ,		__snake_case					)	->			Tuple:

			'''simple docstring'''


			os.makedirs(__snake_case     ,		exist_ok=__snake_case					)
			UpperCamelCase_     				= {}
			for name, parameter in state_dict.items():
						UpperCamelCase_     				= offload_weight(__snake_case     ,		__snake_case     ,		__snake_case     ,		index=__snake_case					)

			# Update index
			save_offload_index(__snake_case     ,		__snake_case					)




class     A					(				lowerCamelCase_       ):







							def __init__(    self   :	int   ,     __UpperCAmelCase   :	Mapping   ,     __UpperCAmelCase   :	str				)		->					Tuple:

										"""simple docstring"""



										UpperCamelCase_     				= dataset
										UpperCamelCase_     				= prefix







							def __getitem__(    self   :	List[Any]   ,     __UpperCAmelCase   :	Dict				)		->					Tuple:

										"""simple docstring"""



										return self.dataset[f'''{self.prefix}{key}''']







							def __iter__(    self   :	int				)		->					List[str]:

										"""simple docstring"""



										return iter([key for key in self.dataset if key.startswith(self.prefix				)]				)







							def __len__(    self   :	Any				)		->					Optional[Any]:

										"""simple docstring"""



										return len(self.dataset				)







class     A					(				lowerCamelCase_       ):







							def __init__(    self   :	int   ,     __UpperCAmelCase   :	Dict[str, torch.Tensor] = None   ,     __UpperCAmelCase   :	Optional[Union[str, os.PathLike]] = None   ,     __UpperCAmelCase   :	Mapping = None   ,     __UpperCAmelCase   :	Optional[int]=None   ,     )		->					List[str]:

										"""simple docstring"""



										if state_dict is None and save_folder is None:
													raise ValueError('Need either a `state_dict` or a `save_folder` containing offloaded weights.'				)

										UpperCamelCase_     				= {} if state_dict is None else state_dict
										UpperCamelCase_     				= save_folder
										if index is None and save_folder is not None:
													with open(os.path.join(__UpperCAmelCase   ,     'index.json'				)				) as f:
																UpperCamelCase_     				= json.load(__UpperCAmelCase				)
										UpperCamelCase_     				= {} if index is None else index
										UpperCamelCase_     				= list(self.state_dict.keys()				)
										self.all_keys.extend([key for key in self.index if key not in self.all_keys]				)
										UpperCamelCase_     				= device







							def __getitem__(    self   :	Optional[Any]   ,     __UpperCAmelCase   :	str				)		->					Tuple:

										"""simple docstring"""



										if key in self.state_dict:
													return self.state_dict[key]
										UpperCamelCase_     				= self.index[key]
										if weight_info.get('safetensors_file'				) is not None:
													if not is_safetensors_available():
																raise ImportError('These offloaded weights require the use of safetensors: `pip install safetensors`.'				)

													from safetensors import safe_open

													UpperCamelCase_     				= 'cpu' if self.device is None else self.device
													with safe_open(weight_info['safetensors_file']   ,     framework='pt'   ,     device=__UpperCAmelCase				) as f:
																UpperCamelCase_     				= f.get_tensor(weight_info.get('weight_name'   ,     __UpperCAmelCase				)				)

													if "dtype" in weight_info:
																return tensor.to(getattr(__UpperCAmelCase   ,     weight_info['dtype']				)				)
													else:
																return tensor

										UpperCamelCase_     				= os.path.join(self.save_folder   ,     f'''{key}.dat'''				)
										return load_offloaded_weight(__UpperCAmelCase   ,     __UpperCAmelCase				)







							def __iter__(    self   :	Optional[int]				)		->					Union[str, Any]:

										"""simple docstring"""



										return iter(self.all_keys				)







							def __len__(    self   :	Any				)		->					Any:

										"""simple docstring"""



										return len(self.all_keys				)







def  a_		(							__snake_case     ,		__snake_case					)	->			Any:

			'''simple docstring'''


			UpperCamelCase_     				= {}
			for module_name in submodule_names:
						# We want to catch module_name parameter (module_name.xxx) or potentially module_name, but not any of the
						# submodules that could being like module_name (transformers.h.1 and transformers.h.10 for instance)
						result.update(
						    {
						        key: param
						        for key, param in state_dict.items()
						        if key == module_name or key.startswith(module_name + '.'					)
						    }					)
			return result
