





"""simple docstring"""




from dataclasses import dataclass
from typing import Optional

import numpy as np
import torch
import torch.nn as nn

from ..utils import BaseOutput, is_torch_version, randn_tensor
from .attention_processor import SpatialNorm
from .unet_ad_blocks import UNetMidBlockaD, get_down_block, get_up_block


@dataclass
class  __UpperCamelCase       (		a__		):
							lowerCamelCase     :      torch.FloatTensor


class  __UpperCamelCase       (		nn.Module		):






							def __init__(     self   ,      lowerCAmelCase__=3   ,      lowerCAmelCase__=3   ,      lowerCAmelCase__=("DownEncoderBlock2D",)   ,      lowerCAmelCase__=(64,)   ,      lowerCAmelCase__=2   ,      lowerCAmelCase__=32   ,      lowerCAmelCase__="silu"   ,      lowerCAmelCase__=True   ,      )			->				Dict:
								super().__init__()
								a   :	Tuple			=       layers_per_block

								a   :	List[str]			=       torch.nn.Convad(
								    lowerCAmelCase__   ,      block_out_channels[0]   ,      kernel_size=3   ,      stride=1   ,      padding=1   ,      )

								a   :	str			=       None
								a   :	Any			=       nn.ModuleList([]   )

								# down
								a   :	Tuple			=       block_out_channels[0]
								for i, down_block_type in enumerate(lowerCAmelCase__   ):
									a   :	Dict			=       output_channel
									a   :	str			=       block_out_channels[i]
									a   :	int			=       i == len(lowerCAmelCase__   ) - 1

									a   :	List[Any]			=       get_down_block(
									    lowerCAmelCase__   ,      num_layers=self.layers_per_block   ,      in_channels=lowerCAmelCase__   ,      out_channels=lowerCAmelCase__   ,      add_downsample=not is_final_block   ,      resnet_eps=1E-6   ,      downsample_padding=0   ,      resnet_act_fn=lowerCAmelCase__   ,      resnet_groups=lowerCAmelCase__   ,      attention_head_dim=lowerCAmelCase__   ,      temb_channels=lowerCAmelCase__   ,      )
									self.down_blocks.append(lowerCAmelCase__   )

								# mid
								a   :	str			=       UNetMidBlockaD(
								    in_channels=block_out_channels[-1]   ,      resnet_eps=1E-6   ,      resnet_act_fn=lowerCAmelCase__   ,      output_scale_factor=1   ,      resnet_time_scale_shift="default"   ,      attention_head_dim=block_out_channels[-1]   ,      resnet_groups=lowerCAmelCase__   ,      temb_channels=lowerCAmelCase__   ,      )

								# out
								a   :	Tuple			=       nn.GroupNorm(num_channels=block_out_channels[-1]   ,      num_groups=lowerCAmelCase__   ,      eps=1E-6   )
								a   :	Dict			=       nn.SiLU()

								a   :	Union[str, Any]			=       2 * out_channels if double_z else out_channels
								a   :	List[Any]			=       nn.Convad(block_out_channels[-1]   ,      lowerCAmelCase__   ,      3   ,      padding=1   )

								a   :	List[str]			=       False






							def 					__a     (     self   ,      lowerCAmelCase__   )			->				Union[str, Any]:
								a   :	Optional[Any]			=       x
								a   :	Dict			=       self.conv_in(lowerCAmelCase__   )

								if self.training and self.gradient_checkpointing:

									def create_custom_forward(lowerCAmelCase__   ):
										def custom_forward(*lowerCAmelCase__   ):
											return module(*lowerCAmelCase__   )

										return custom_forward

									# down
									if is_torch_version(">="   ,      "1.11.0"   ):
										for down_block in self.down_blocks:
											a   :	List[Any]			=       torch.utils.checkpoint.checkpoint(
											    create_custom_forward(lowerCAmelCase__   )   ,      lowerCAmelCase__   ,      use_reentrant=lowerCAmelCase__   )
										# middle
										a   :	List[str]			=       torch.utils.checkpoint.checkpoint(
										    create_custom_forward(self.mid_block   )   ,      lowerCAmelCase__   ,      use_reentrant=lowerCAmelCase__   )
									else:
										for down_block in self.down_blocks:
											a   :	List[Any]			=       torch.utils.checkpoint.checkpoint(create_custom_forward(lowerCAmelCase__   )   ,      lowerCAmelCase__   )
										# middle
										a   :	Optional[int]			=       torch.utils.checkpoint.checkpoint(create_custom_forward(self.mid_block   )   ,      lowerCAmelCase__   )

								else:
									# down
									for down_block in self.down_blocks:
										a   :	Optional[int]			=       down_block(lowerCAmelCase__   )

									# middle
									a   :	int			=       self.mid_block(lowerCAmelCase__   )

								# post-process
								a   :	Tuple			=       self.conv_norm_out(lowerCAmelCase__   )
								a   :	int			=       self.conv_act(lowerCAmelCase__   )
								a   :	str			=       self.conv_out(lowerCAmelCase__   )

								return sample


class  __UpperCamelCase       (		nn.Module		):






							def __init__(     self   ,      lowerCAmelCase__=3   ,      lowerCAmelCase__=3   ,      lowerCAmelCase__=("UpDecoderBlock2D",)   ,      lowerCAmelCase__=(64,)   ,      lowerCAmelCase__=2   ,      lowerCAmelCase__=32   ,      lowerCAmelCase__="silu"   ,      lowerCAmelCase__="group"   ,      )			->				Dict:
								super().__init__()
								a   :	List[str]			=       layers_per_block

								a   :	Optional[int]			=       nn.Convad(
								    lowerCAmelCase__   ,      block_out_channels[-1]   ,      kernel_size=3   ,      stride=1   ,      padding=1   ,      )

								a   :	int			=       None
								a   :	Union[str, Any]			=       nn.ModuleList([]   )

								a   :	Optional[int]			=       in_channels if norm_type == "spatial" else None

								# mid
								a   :	Any			=       UNetMidBlockaD(
								    in_channels=block_out_channels[-1]   ,      resnet_eps=1E-6   ,      resnet_act_fn=lowerCAmelCase__   ,      output_scale_factor=1   ,      resnet_time_scale_shift="default" if norm_type == "group" else norm_type   ,      attention_head_dim=block_out_channels[-1]   ,      resnet_groups=lowerCAmelCase__   ,      temb_channels=lowerCAmelCase__   ,      )

								# up
								a   :	List[str]			=       list(reversed(lowerCAmelCase__   )   )
								a   :	List[Any]			=       reversed_block_out_channels[0]
								for i, up_block_type in enumerate(lowerCAmelCase__   ):
									a   :	str			=       output_channel
									a   :	Tuple			=       reversed_block_out_channels[i]

									a   :	Any			=       i == len(lowerCAmelCase__   ) - 1

									a   :	str			=       get_up_block(
									    lowerCAmelCase__   ,      num_layers=self.layers_per_block + 1   ,      in_channels=lowerCAmelCase__   ,      out_channels=lowerCAmelCase__   ,      prev_output_channel=lowerCAmelCase__   ,      add_upsample=not is_final_block   ,      resnet_eps=1E-6   ,      resnet_act_fn=lowerCAmelCase__   ,      resnet_groups=lowerCAmelCase__   ,      attention_head_dim=lowerCAmelCase__   ,      temb_channels=lowerCAmelCase__   ,      resnet_time_scale_shift=lowerCAmelCase__   ,      )
									self.up_blocks.append(lowerCAmelCase__   )
									a   :	List[str]			=       output_channel

								# out
								if norm_type == "spatial":
									a   :	Optional[Any]			=       SpatialNorm(block_out_channels[0]   ,      lowerCAmelCase__   )
								else:
									a   :	Optional[Any]			=       nn.GroupNorm(num_channels=block_out_channels[0]   ,      num_groups=lowerCAmelCase__   ,      eps=1E-6   )
								a   :	int			=       nn.SiLU()
								a   :	Union[str, Any]			=       nn.Convad(block_out_channels[0]   ,      lowerCAmelCase__   ,      3   ,      padding=1   )

								a   :	int			=       False






							def 					__a     (     self   ,      lowerCAmelCase__   ,      lowerCAmelCase__=None   )			->				Any:
								a   :	Any			=       z
								a   :	Union[str, Any]			=       self.conv_in(lowerCAmelCase__   )

								a   :	Any			=       next(iter(self.up_blocks.parameters()   )   ).dtype
								if self.training and self.gradient_checkpointing:

									def create_custom_forward(lowerCAmelCase__   ):
										def custom_forward(*lowerCAmelCase__   ):
											return module(*lowerCAmelCase__   )

										return custom_forward

									if is_torch_version(">="   ,      "1.11.0"   ):
										# middle
										a   :	Optional[Any]			=       torch.utils.checkpoint.checkpoint(
										    create_custom_forward(self.mid_block   )   ,      lowerCAmelCase__   ,      lowerCAmelCase__   ,      use_reentrant=lowerCAmelCase__   )
										a   :	List[Any]			=       sample.to(lowerCAmelCase__   )

										# up
										for up_block in self.up_blocks:
											a   :	str			=       torch.utils.checkpoint.checkpoint(
											    create_custom_forward(lowerCAmelCase__   )   ,      lowerCAmelCase__   ,      lowerCAmelCase__   ,      use_reentrant=lowerCAmelCase__   )
									else:
										# middle
										a   :	Optional[Any]			=       torch.utils.checkpoint.checkpoint(
										    create_custom_forward(self.mid_block   )   ,      lowerCAmelCase__   ,      lowerCAmelCase__   )
										a   :	int			=       sample.to(lowerCAmelCase__   )

										# up
										for up_block in self.up_blocks:
											a   :	Dict			=       torch.utils.checkpoint.checkpoint(create_custom_forward(lowerCAmelCase__   )   ,      lowerCAmelCase__   ,      lowerCAmelCase__   )
								else:
									# middle
									a   :	List[Any]			=       self.mid_block(lowerCAmelCase__   ,      lowerCAmelCase__   )
									a   :	int			=       sample.to(lowerCAmelCase__   )

									# up
									for up_block in self.up_blocks:
										a   :	Optional[Any]			=       up_block(lowerCAmelCase__   ,      lowerCAmelCase__   )

        # post-process
								if latent_embeds is None:
									a   :	str			=       self.conv_norm_out(lowerCAmelCase__   )
								else:
									a   :	Optional[int]			=       self.conv_norm_out(lowerCAmelCase__   ,      lowerCAmelCase__   )
								a   :	Any			=       self.conv_act(lowerCAmelCase__   )
								a   :	Tuple			=       self.conv_out(lowerCAmelCase__   )

								return sample


class  __UpperCamelCase       (		nn.Module		):






							def __init__(     self   ,      lowerCAmelCase__   ,      lowerCAmelCase__   ,      lowerCAmelCase__   ,      lowerCAmelCase__=None   ,      lowerCAmelCase__="random"   ,      lowerCAmelCase__=False   ,      lowerCAmelCase__=True   )			->				List[Any]:
								super().__init__()
								a   :	Union[str, Any]			=       n_e
								a   :	Optional[Any]			=       vq_embed_dim
								a   :	int			=       beta
								a   :	Any			=       legacy

								a   :	str			=       nn.Embedding(self.n_e   ,      self.vq_embed_dim   )
								self.embedding.weight.data.uniform_(-1.0 / self.n_e   ,      1.0 / self.n_e   )

								a   :	str			=       remap
								if self.remap is not None:
									self.register_buffer("used"   ,      torch.tensor(np.load(self.remap   )   )   )
									a   :	List[Any]			=       self.used.shape[0]
									a   :	Optional[int]			=       unknown_index  # "random" or "extra" or integer
									if self.unknown_index == "extra":
										a   :	List[str]			=       self.re_embed
										a   :	List[str]			=       self.re_embed + 1
									print(
									    f"""Remapping {self.n_e} indices to {self.re_embed} indices. """
									    f"""Using {self.unknown_index} for unknown indices."""   )
								else:
									a   :	str			=       n_e

								a   :	Union[str, Any]			=       sane_index_shape






							def 					__a     (     self   ,      lowerCAmelCase__   )			->				str:
								a   :	List[Any]			=       inds.shape
								assert len(lowerCAmelCase__   ) > 1
								a   :	Union[str, Any]			=       inds.reshape(ishape[0]   ,      -1   )
								a   :	Optional[Any]			=       self.used.to(lowerCAmelCase__   )
								a   :	List[str]			=       (inds[:, :, None] == used[None, None, ...]).long()
								a   :	str			=       match.argmax(-1   )
								a   :	int			=       match.sum(2   ) < 1
								if self.unknown_index == "random":
									a   :	Optional[int]			=       torch.randint(0   ,      self.re_embed   ,      size=new[unknown].shape   ).to(device=new.device   )
								else:
									a   :	Optional[Any]			=       self.unknown_index
								return new.reshape(lowerCAmelCase__   )






							def 					__a     (     self   ,      lowerCAmelCase__   )			->				int:
								a   :	str			=       inds.shape
								assert len(lowerCAmelCase__   ) > 1
								a   :	Tuple			=       inds.reshape(ishape[0]   ,      -1   )
								a   :	Dict			=       self.used.to(lowerCAmelCase__   )
								if self.re_embed > self.used.shape[0]:  # extra token
									a   :	Optional[Any]			=       0  # simply set to zero
								a   :	Optional[Any]			=       torch.gather(used[None, :][inds.shape[0] * [0], :]   ,      1   ,      lowerCAmelCase__   )
								return back.reshape(lowerCAmelCase__   )






							def 					__a     (     self   ,      lowerCAmelCase__   )			->				int:
								# reshape z -> (batch, height, width, channel) and flatten
								a   :	Tuple			=       z.permute(0   ,      2   ,      3   ,      1   ).contiguous()
								a   :	Optional[int]			=       z.view(-1   ,      self.vq_embed_dim   )

								# distances from z to embeddings e_j (z - e)^2 = z^2 + e^2 - 2 e * z
								a   :	Tuple			=       torch.argmin(torch.cdist(lowerCAmelCase__   ,      self.embedding.weight   )   ,      dim=1   )

								a   :	Dict			=       self.embedding(lowerCAmelCase__   ).view(z.shape   )
								a   :	Dict			=       None
								a   :	Tuple			=       None

								# compute loss for embedding
								if not self.legacy:
									a   :	Optional[int]			=       self.beta * torch.mean((z_q.detach() - z) ** 2   ) + torch.mean((z_q - z.detach()) ** 2   )
								else:
									a   :	Union[str, Any]			=       torch.mean((z_q.detach() - z) ** 2   ) + self.beta * torch.mean((z_q - z.detach()) ** 2   )

								# preserve gradients
								a   :	Optional[int]			=       z + (z_q - z).detach()

								# reshape back to match original input shape
								a   :	Dict			=       z_q.permute(0   ,      3   ,      1   ,      2   ).contiguous()

								if self.remap is not None:
									a   :	Optional[Any]			=       min_encoding_indices.reshape(z.shape[0]   ,      -1   )  # add batch axis
									a   :	Optional[Any]			=       self.remap_to_used(lowerCAmelCase__   )
									a   :	List[Any]			=       min_encoding_indices.reshape(-1   ,      1   )  # flatten

								if self.sane_index_shape:
									a   :	List[Any]			=       min_encoding_indices.reshape(z_q.shape[0]   ,      z_q.shape[2]   ,      z_q.shape[3]   )

								return z_q, loss, (perplexity, min_encodings, min_encoding_indices)






							def 					__a     (     self   ,      lowerCAmelCase__   ,      lowerCAmelCase__   )			->				str:
								# shape specifying (batch, height, width, channel)
								if self.remap is not None:
									a   :	List[str]			=       indices.reshape(shape[0]   ,      -1   )  # add batch axis
									a   :	Optional[Any]			=       self.unmap_to_all(lowerCAmelCase__   )
									a   :	Any			=       indices.reshape(-1   )  # flatten again

								# get quantized latent vectors
								a   :	str			=       self.embedding(lowerCAmelCase__   )

								if shape is not None:
									a   :	List[str]			=       z_q.view(lowerCAmelCase__   )
									# reshape back to match original input shape
									a   :	Optional[int]			=       z_q.permute(0   ,      3   ,      1   ,      2   ).contiguous()

								return z_q


class  __UpperCamelCase       (		a__		):






							def __init__(     self   ,      lowerCAmelCase__   ,      lowerCAmelCase__=False   )			->				int:
								a   :	Any			=       parameters
								a, a   :	Tuple			=       torch.chunk(lowerCAmelCase__   ,      2   ,      dim=1   )
								a   :	Optional[Any]			=       torch.clamp(self.logvar   ,      -30.0   ,      20.0   )
								a   :	int			=       deterministic
								a   :	Optional[Any]			=       torch.exp(0.5 * self.logvar   )
								a   :	Optional[int]			=       torch.exp(self.logvar   )
								if self.deterministic:
									a   :	int			=       torch.zeros_like(
									    self.mean   ,      device=self.parameters.device   ,      dtype=self.parameters.dtype   )






							def 					__a     (     self   ,      lowerCAmelCase__ = None   )			->				torch.FloatTensor:
								# make sure sample is on the same device as the parameters and has same dtype
								a   :	List[str]			=       randn_tensor(
								    self.mean.shape   ,      generator=lowerCAmelCase__   ,      device=self.parameters.device   ,      dtype=self.parameters.dtype   )
								a   :	List[str]			=       self.mean + self.std * sample
								return x






							def 					__a     (     self   ,      lowerCAmelCase__=None   )			->				Tuple:
								if self.deterministic:
									return torch.Tensor([0.0]   )
								else:
									if other is None:
										return 0.5 * torch.sum(torch.pow(self.mean   ,      2   ) + self.var - 1.0 - self.logvar   ,      dim=[1, 2, 3]   )
									else:
										return 0.5 * torch.sum(
										    torch.pow(self.mean - other.mean   ,      2   ) / other.var
										    + self.var / other.var
										    - 1.0
										    - self.logvar
										    + other.logvar   ,      dim=[1, 2, 3]   ,      )






							def 					__a     (     self   ,      lowerCAmelCase__   ,      lowerCAmelCase__=[1, 2, 3]   )			->				int:
								if self.deterministic:
									return torch.Tensor([0.0]   )
								a   :	List[str]			=       np.log(2.0 * np.pi   )
								return 0.5 * torch.sum(logtwopi + self.logvar + torch.pow(sample - self.mean   ,      2   ) / self.var   ,      dim=lowerCAmelCase__   )






							def 					__a     (     self   )			->				Any:
								return self.mean









"""simple docstring"""




def    _SCREAMING_SNAKE_CASE	(			_lowercase		:					int = 10					, _lowercase		:					int = 1000					, _lowercase		:					bool = True      )				->int:






	'''simple docstring'''






	assert (
	    isinstance(_lowercase					, _lowercase      )
	    and isinstance(_lowercase					, _lowercase      )
	    and isinstance(_lowercase					, _lowercase      )
	), "Invalid type of value(s) specified to function!"

	if min_val > max_val:
		raise ValueError("Invalid value for min_val or max_val (min_value < max_value)"      )
	return min_val if option else max_val






def    _SCREAMING_SNAKE_CASE	(			_lowercase		:					int					, _lowercase		:					int      )				->int:






	'''simple docstring'''






	return int((number_a + number_a) / 2      )






def    _SCREAMING_SNAKE_CASE	(			_lowercase		:					int					, _lowercase		:					int					, _lowercase		:					int      )				->None:






	'''simple docstring'''






	assert (
	    isinstance(_lowercase					, _lowercase      ) and isinstance(_lowercase					, _lowercase      ) and isinstance(_lowercase					, _lowercase      )
	), 'argument values must be type of "int"'

	if lower > higher:
		raise ValueError("argument value for lower and higher must be(lower > higher)"      )

	if not lower < to_guess < higher:
		raise ValueError(
		    "guess value must be within the range of lower and higher value"      )

	def answer(_lowercase		:					int      ) -> str:
		if number > to_guess:
			return "high"
		elif number < to_guess:
			return "low"
		else:
			return "same"

	print("started..."      )

	a   :	Optional[Any]			=       lower
	a   :	List[Any]			=       higher

	a   :	Tuple			=       []

	while True:
		a   :	List[Any]			=       get_avg(_lowercase					, _lowercase      )
		last_numbers.append(_lowercase      )

		if answer(_lowercase      ) == "low":
			a   :	Optional[int]			=       number
		elif answer(_lowercase      ) == "high":
			a   :	Tuple			=       number
		else:
			break

	print(F"""guess the number : {last_numbers[-1]}"""      )
	print(F"""details : {last_numbers!s}"""      )






def    _SCREAMING_SNAKE_CASE	(			)				->None:






	'''simple docstring'''






	a   :	Tuple			=       int(input("Enter lower value : "      ).strip()      )
	a   :	Dict			=       int(input("Enter high value : "      ).strip()      )
	a   :	Optional[int]			=       int(input("Enter value to guess : "      ).strip()      )
	guess_the_number(_lowercase					, _lowercase					, _lowercase      )


if __name__ == "__main__":
		main()


