



"""simple docstring"""






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      (			_A,     _A,     _A,     _A=None							):




					"""simple docstring"""
					snake_case_						:Union[str, Any]								=						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.
										snake_case_						:Optional[int]								=						weight.view(torch.intaa							)
										snake_case_						:Any								=						"bfloat16"
					snake_case_						:Any								=						weight.cpu().numpy()
					snake_case_						:List[str]								=						os.path.join(_A,     F'''{weight_name}.dat'''							)
					if index is not None:
										if dtype is None:
															snake_case_						:Tuple								=						str(array.dtype							)
										snake_case_						:Any								=						{"dtype": dtype, "shape": list(array.shape							)}
					if array.ndim == 0:
										snake_case_						:int								=						array[None]
					snake_case_						:Tuple								=						np.memmap(_A,     dtype=array.dtype,     mode="w+",     shape=array.shape							)
					snake_case_						:str								=						array[:]
					file_array.flush()
					return index
def      A      (			_A,     _A							):




					"""simple docstring"""
					snake_case_						:Tuple								=						tuple(weight_info["shape"]							)
					if shape == ():
										# NumPy memory-mapped arrays can't have 0 dims so it was saved as 1d tensor
										snake_case_						:Union[str, Any]								=						(1,)

					snake_case_						:Optional[Any]								=						weight_info["dtype"]
					if dtype == "bfloat16":
										# NumPy does not support bfloat16 so this was saved as a int16
										snake_case_						:str								=						"int16"

					snake_case_						:str								=						np.memmap(_A,     dtype=_A,     shape=_A,     mode="r"							)

					if len(weight_info["shape"]							) == 0:
										snake_case_						:List[str]								=						weight[0]
					snake_case_						:Tuple								=						torch.tensor(_A							)
					if weight_info["dtype"] == "bfloat16":
										snake_case_						:List[Any]								=						weight.view(torch.bfloataa							)

					return weight
def      A      (			_A,     _A							):




					"""simple docstring"""
					if index is None or len(_A							) == 0:
										# Nothing to save
										return

					snake_case_						:Union[str, Any]								=						os.path.join(_A,     "index.json"							)
					if os.path.isfile(_A							):
										with open(_A,     "r",     encoding="utf-8"							) as f:
															snake_case_						:Union[str, Any]								=						json.load(_A							)
					else:
										snake_case_						:Any								=						{}
					current_index.update(_A							)

					with open(_A,     "w",     encoding="utf-8"							) as f:
										json.dump(_A,     _A,     indent=2							)
def      A      (			_A,     _A							):




					"""simple docstring"""
					os.makedirs(_A,     exist_ok=_A							)
					snake_case_						:Optional[Any]								=						{}
					for name, parameter in state_dict.items():
										snake_case_						:Dict								=						offload_weight(_A,     _A,     _A,     index=_A							)

					# Update index
					save_offload_index(_A,     _A							)

class 	__lowerCAmelCase							(__UpperCamelCase  ):


					'''simple docstring'''


					def __init__(      self    ,       a    ,       a   ):




										"""simple docstring"""





										snake_case_						:Optional[Any]								=						dataset
										snake_case_						:Tuple								=						prefix







					def __getitem__(      self    ,       a   ):




										"""simple docstring"""





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







					def __iter__(      self   ):




										"""simple docstring"""





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







					def __len__(      self   ):




										"""simple docstring"""





										return len(self.dataset   )



class 	__lowerCAmelCase							(__UpperCamelCase  ):


					'''simple docstring'''


					def __init__(      self    ,       a = None    ,       a = None    ,       a = None    ,       a=None    ,       ):




										"""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."   )

										snake_case_						:Optional[Any]								=						{} if state_dict is None else state_dict
										snake_case_						:Tuple								=						save_folder
										if index is None and save_folder is not None:
															with open(os.path.join(a    ,       "index.json"   )   ) as f:
																				snake_case_						:Optional[int]								=						json.load(a   )
										snake_case_						:Optional[Any]								=						{} if index is None else index
										snake_case_						:Union[str, Any]								=						list(self.state_dict.keys()   )
										self.all_keys.extend([key for key in self.index if key not in self.all_keys]   )
										snake_case_						:Optional[Any]								=						device







					def __getitem__(      self    ,       a   ):




										"""simple docstring"""





										if key in self.state_dict:
															return self.state_dict[key]
										snake_case_						:str								=						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

															snake_case_						:Union[str, Any]								=						"cpu" if self.device is None else self.device
															with safe_open(weight_info["safetensors_file"]    ,       framework="pt"    ,       device=a   ) as f:
																				snake_case_						:List[str]								=						f.get_tensor(weight_info.get("weight_name"    ,       a   )   )

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

										snake_case_						:List[Any]								=						os.path.join(self.save_folder    ,       F'''{key}.dat'''   )
										return load_offloaded_weight(a    ,       a   )







					def __iter__(      self   ):




										"""simple docstring"""





										return iter(self.all_keys   )







					def __len__(      self   ):




										"""simple docstring"""





										return len(self.all_keys   )



def      A      (			_A,     _A							):




					"""simple docstring"""
					snake_case_						:Optional[int]								=						{}
					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






"""simple docstring"""






import warnings
from typing import List, Optional, Union

import numpy as np
import PIL
import torch
from PIL import Image

from .configuration_utils import ConfigMixin, register_to_config
from .utils import CONFIG_NAME, PIL_INTERPOLATION, deprecate

class 	__lowerCAmelCase							(__UpperCamelCase  ):


					'''simple docstring'''


					a__        =     CONFIG_NAME







					@register_to_config
					def __init__(      self    ,       a = True    ,       a = 8    ,       a = "lanczos"    ,       a = True    ,       a = False    ,       ):




										"""simple docstring"""





										super().__init__()







					@staticmethod
					def 		_a (      a   ):




										"""simple docstring"""





										if images.ndim == 3:
															snake_case_						:Union[str, Any]								=						images[None, ...]
										snake_case_						:Optional[int]								=						(images * 2_55).round().astype("uint8"   )
										if images.shape[-1] == 1:
															# special case for grayscale (single channel) images
															snake_case_						:List[str]								=						[Image.fromarray(image.squeeze()    ,       mode="L"   ) for image in images]
										else:
															snake_case_						:str								=						[Image.fromarray(a   ) for image in images]

										return pil_images







					@staticmethod
					def 		_a (      a   ):




										"""simple docstring"""





										if not isinstance(a    ,       a   ):
															snake_case_						:Dict								=						[images]
										snake_case_						:Optional[int]								=						[np.array(a   ).astype(np.floataa   ) / 255.0 for image in images]
										snake_case_						:Optional[int]								=						np.stack(a    ,       axis=0   )

										return images







					@staticmethod
					def 		_a (      a   ):




										"""simple docstring"""





										if images.ndim == 3:
															snake_case_						:int								=						images[..., None]

										snake_case_						:str								=						torch.from_numpy(images.transpose(0    ,       3    ,       1    ,       2   )   )
										return images







					@staticmethod
					def 		_a (      a   ):




										"""simple docstring"""





										snake_case_						:str								=						images.cpu().permute(0    ,       2    ,       3    ,       1   ).float().numpy()
										return images







					@staticmethod
					def 		_a (      a   ):




										"""simple docstring"""





										return 2.0 * images - 1.0







					@staticmethod
					def 		_a (      a   ):




										"""simple docstring"""





										return (images / 2 + 0.5).clamp(0    ,       1   )







					@staticmethod
					def 		_a (      a   ):




										"""simple docstring"""





										snake_case_						:List[str]								=						image.convert("RGB"   )
										return image







					def 		_a (      self    ,       a    ,       a = None    ,       a = None    ,       ):




										"""simple docstring"""





										if height is None:
															snake_case_						:Optional[Any]								=						image.height
										if width is None:
															snake_case_						:Dict								=						image.width

										snake_case_	,  snake_case_						:Optional[Any]								=						(
										    x - x % self.config.vae_scale_factor for x in (width, height)
										)  # resize to integer multiple of vae_scale_factor
										snake_case_						:Any								=						image.resize((width, height)    ,       resample=PIL_INTERPOLATION[self.config.resample]   )
										return image







					def 		_a (      self    ,       a    ,       a = None    ,       a = None    ,       ):




										"""simple docstring"""





										snake_case_						:Optional[Any]								=						(PIL.Image.Image, np.ndarray, torch.Tensor)
										if isinstance(a    ,       a   ):
															snake_case_						:str								=						[image]
										elif not (isinstance(a    ,       a   ) and all(isinstance(a    ,       a   ) for i in image   )):
															raise ValueError(
															    F'''Input is in incorrect format: {[type(a   ) for i in image]}. Currently, we only support {', '.join(a   )}'''   )

										if isinstance(image[0]    ,       PIL.Image.Image   ):
															if self.config.do_convert_rgb:
																				snake_case_						:int								=						[self.convert_to_rgb(a   ) for i in image]
															if self.config.do_resize:
																				snake_case_						:Tuple								=						[self.resize(a    ,       a    ,       a   ) for i in image]
															snake_case_						:Dict								=						self.pil_to_numpy(a   )  # to np
															snake_case_						:int								=						self.numpy_to_pt(a   )  # to pt

										elif isinstance(image[0]    ,       np.ndarray   ):
															snake_case_						:Optional[Any]								=						np.concatenate(a    ,       axis=0   ) if image[0].ndim == 4 else np.stack(a    ,       axis=0   )
															snake_case_						:List[Any]								=						self.numpy_to_pt(a   )
															snake_case_	,  snake_case_	,  snake_case_	,  snake_case_						:Optional[int]								=						image.shape
															if self.config.do_resize and (
															    height % self.config.vae_scale_factor != 0 or width % self.config.vae_scale_factor != 0
															):
																				raise ValueError(
																				    F'''Currently we only support resizing for PIL image - please resize your numpy array to be divisible by {self.config.vae_scale_factor}'''
																				    F'''currently the sizes are {height} and {width}. You can also pass a PIL image instead to use resize option in VAEImageProcessor'''   )

										elif isinstance(image[0]    ,       torch.Tensor   ):
															snake_case_						:Optional[Any]								=						torch.cat(a    ,       axis=0   ) if image[0].ndim == 4 else torch.stack(a    ,       axis=0   )
															snake_case_	,  snake_case_	,  snake_case_	,  snake_case_						:int								=						image.shape

															# don't need any preprocess if the image is latents
															if channel == 4:
																				return image

															if self.config.do_resize and (
															    height % self.config.vae_scale_factor != 0 or width % self.config.vae_scale_factor != 0
															):
																				raise ValueError(
																				    F'''Currently we only support resizing for PIL image - please resize your pytorch tensor to be divisible by {self.config.vae_scale_factor}'''
																				    F'''currently the sizes are {height} and {width}. You can also pass a PIL image instead to use resize option in VAEImageProcessor'''   )

        # expected range [0,1], normalize to [-1,1]
										snake_case_						:Union[str, Any]								=						self.config.do_normalize
										if image.min() < 0:
															warnings.warn(
															    "Passing `image` as torch tensor with value range in [-1,1] is deprecated. The expected value range for image tensor is [0,1] "
															    F'''when passing as pytorch tensor or numpy Array. You passed `image` with value range [{image.min()},{image.max()}]'''    ,       a    ,       )
															snake_case_						:Optional[int]								=						False

										if do_normalize:
															snake_case_						:Tuple								=						self.normalize(a   )

										return image







					def 		_a (      self    ,       a    ,       a = "pil"    ,       a = None    ,       ):




										"""simple docstring"""





										if not isinstance(a    ,       torch.Tensor   ):
															raise ValueError(
															    F'''Input for postprocessing is in incorrect format: {type(a   )}. We only support pytorch tensor'''   )
										if output_type not in ["latent", "pt", "np", "pil"]:
															snake_case_						:List[Any]								=						(
															    F'''the output_type {output_type} is outdated and has been set to `np`. Please make sure to set it to one of these instead: '''
															    "`pil`, `np`, `pt`, `latent`"
															)
															deprecate("Unsupported output_type"    ,       "1.0.0"    ,       a    ,       standard_warn=a   )
															snake_case_						:Dict								=						"np"

										if output_type == "latent":
															return image

										if do_denormalize is None:
															snake_case_						:List[Any]								=						[self.config.do_normalize] * image.shape[0]

										snake_case_						:Any								=						torch.stack(
										    [self.denormalize(image[i]   ) if do_denormalize[i] else image[i] for i in range(image.shape[0]   )]   )

										if output_type == "pt":
															return image

										snake_case_						:int								=						self.pt_to_numpy(a   )

										if output_type == "np":
															return image

										if output_type == "pil":
															return self.numpy_to_pil(a   )



class 	__lowerCAmelCase							(__UpperCamelCase  ):


					'''simple docstring'''


					a__        =     CONFIG_NAME







					@register_to_config
					def __init__(      self    ,       a = True    ,       a = 8    ,       a = "lanczos"    ,       a = True    ,       ):




										"""simple docstring"""





										super().__init__()







					@staticmethod
					def 		_a (      a   ):




										"""simple docstring"""





										if images.ndim == 3:
															snake_case_						:List[Any]								=						images[None, ...]
										snake_case_						:Optional[int]								=						(images * 2_55).round().astype("uint8"   )
										if images.shape[-1] == 1:
															# special case for grayscale (single channel) images
															snake_case_						:str								=						[Image.fromarray(image.squeeze()    ,       mode="L"   ) for image in images]
										else:
															snake_case_						:Any								=						[Image.fromarray(image[:, :, :3]   ) for image in images]

										return pil_images







					@staticmethod
					def 		_a (      a   ):




										"""simple docstring"""





										return image[:, :, 1] * 2**8 + image[:, :, 2]







					def 		_a (      self    ,       a   ):




										"""simple docstring"""





										if images.ndim == 3:
															snake_case_						:Optional[int]								=						images[None, ...]
										snake_case_						:Union[str, Any]								=						images[:, :, :, 3:]
										if images.shape[-1] == 6:
															snake_case_						:Tuple								=						(images_depth * 2_55).round().astype("uint8"   )
															snake_case_						:Optional[Any]								=						[
															    Image.fromarray(self.rgblike_to_depthmap(a   )    ,       mode="I;16"   ) for image_depth in images_depth
															]
										elif images.shape[-1] == 4:
															snake_case_						:Optional[Any]								=						(images_depth * 65535.0).astype(np.uintaa   )
															snake_case_						:List[Any]								=						[Image.fromarray(a    ,       mode="I;16"   ) for image_depth in images_depth]
										else:
															raise Exception("Not supported"   )

										return pil_images







					def 		_a (      self    ,       a    ,       a = "pil"    ,       a = None    ,       ):




										"""simple docstring"""





										if not isinstance(a    ,       torch.Tensor   ):
															raise ValueError(
															    F'''Input for postprocessing is in incorrect format: {type(a   )}. We only support pytorch tensor'''   )
										if output_type not in ["latent", "pt", "np", "pil"]:
															snake_case_						:Any								=						(
															    F'''the output_type {output_type} is outdated and has been set to `np`. Please make sure to set it to one of these instead: '''
															    "`pil`, `np`, `pt`, `latent`"
															)
															deprecate("Unsupported output_type"    ,       "1.0.0"    ,       a    ,       standard_warn=a   )
															snake_case_						:Optional[Any]								=						"np"

										if do_denormalize is None:
															snake_case_						:Any								=						[self.config.do_normalize] * image.shape[0]

										snake_case_						:Tuple								=						torch.stack(
										    [self.denormalize(image[i]   ) if do_denormalize[i] else image[i] for i in range(image.shape[0]   )]   )

										snake_case_						:List[str]								=						self.pt_to_numpy(a   )

										if output_type == "np":
															if image.shape[-1] == 6:
																				snake_case_						:Optional[int]								=						np.stack([self.rgblike_to_depthmap(im[:, :, 3:]   ) for im in image]    ,       axis=0   )
															else:
																				snake_case_						:Any								=						image[:, :, :, 3:]
															return image[:, :, :, :3], image_depth

										if output_type == "pil":
															return self.numpy_to_pil(a   ), self.numpy_to_depth(a   )
										else:
															raise Exception(F'''This type {output_type} is not supported'''   )

