



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 SCREAMING_SNAKE_CASE_  (   snake_case_   ):
						__magic_name__:     torch.FloatTensor
class SCREAMING_SNAKE_CASE_  (   nn.Module   ):






						def __init__( self : List[Any]						,				_A : str=3						,				_A : List[str]=3						,				_A : str=("DownEncoderBlock2D",)						,				_A : int=(64,)						,				_A : Dict=2						,				_A : str=32						,				_A : List[str]="silu"						,				_A : Any=True						,				)							->      Optional[int]:







							"""simple docstring"""


							super().__init__()
							snake_case_						:     List[str]     =		layers_per_block

							snake_case_						:     Optional[int]     =		torch.nn.Convad(
							    _A						,				block_out_channels[0]						,				kernel_size=3						,				stride=1						,				padding=1						,				)

							snake_case_						:     str     =		None
							snake_case_						:     List[str]     =		nn.ModuleList([]		)

							# down
							snake_case_						:     List[str]     =		block_out_channels[0]
							for i, down_block_type in enumerate(_A		):
								snake_case_						:     List[str]     =		output_channel
								snake_case_						:     Any     =		block_out_channels[i]
								snake_case_						:     Any     =		i == len(_A		) - 1

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

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

							# out
							snake_case_						:     Optional[int]     =		nn.GroupNorm(num_channels=block_out_channels[-1]						,				num_groups=_A						,				eps=1E-6		)
							snake_case_						:     int     =		nn.SiLU()

							snake_case_						:     Optional[int]     =		2 * out_channels if double_z else out_channels
							snake_case_						:     Any     =		nn.Convad(block_out_channels[-1]						,				_A						,				3						,				padding=1		)

							snake_case_						:     List[str]     =		False






						def UpperCAmelCase_			( self : int						,				_A : Optional[int]		)							->      Tuple:







							"""simple docstring"""


							snake_case_						:     Any     =		x
							snake_case_						:     List[str]     =		self.conv_in(_A		)

							if self.training and self.gradient_checkpointing:

								def create_custom_forward(_A : Any		):
									def custom_forward(*_A : Optional[Any]		):
										return module(*_A		)

									return custom_forward

								# down
								if is_torch_version('>='						,				'1.11.0'		):
									for down_block in self.down_blocks:
										snake_case_						:     List[str]     =		torch.utils.checkpoint.checkpoint(
										    create_custom_forward(_A		)						,				_A						,				use_reentrant=_A		)
									# middle
									snake_case_						:     Union[str, Any]     =		torch.utils.checkpoint.checkpoint(
									    create_custom_forward(self.mid_block		)						,				_A						,				use_reentrant=_A		)
								else:
									for down_block in self.down_blocks:
										snake_case_						:     Tuple     =		torch.utils.checkpoint.checkpoint(create_custom_forward(_A		)						,				_A		)
									# middle
									snake_case_						:     Any     =		torch.utils.checkpoint.checkpoint(create_custom_forward(self.mid_block		)						,				_A		)

							else:
								# down
								for down_block in self.down_blocks:
									snake_case_						:     Tuple     =		down_block(_A		)

								# middle
								snake_case_						:     Optional[int]     =		self.mid_block(_A		)

							# post-process
							snake_case_						:     str     =		self.conv_norm_out(_A		)
							snake_case_						:     Dict     =		self.conv_act(_A		)
							snake_case_						:     List[Any]     =		self.conv_out(_A		)

							return sample
class SCREAMING_SNAKE_CASE_  (   nn.Module   ):






						def __init__( self : Union[str, Any]						,				_A : List[str]=3						,				_A : Optional[int]=3						,				_A : Union[str, Any]=("UpDecoderBlock2D",)						,				_A : Dict=(64,)						,				_A : Tuple=2						,				_A : str=32						,				_A : Optional[int]="silu"						,				_A : Optional[int]="group"						,				)							->      List[str]:







							"""simple docstring"""


							super().__init__()
							snake_case_						:     Any     =		layers_per_block

							snake_case_						:     Any     =		nn.Convad(
							    _A						,				block_out_channels[-1]						,				kernel_size=3						,				stride=1						,				padding=1						,				)

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

							snake_case_						:     Dict     =		in_channels if norm_type == 'spatial' else None

							# mid
							snake_case_						:     int     =		UNetMidBlockaD(
							    in_channels=block_out_channels[-1]						,				resnet_eps=1E-6						,				resnet_act_fn=_A						,				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=_A						,				temb_channels=_A						,				)

							# up
							snake_case_						:     Union[str, Any]     =		list(reversed(_A		)		)
							snake_case_						:     int     =		reversed_block_out_channels[0]
							for i, up_block_type in enumerate(_A		):
								snake_case_						:     List[Any]     =		output_channel
								snake_case_						:     Optional[int]     =		reversed_block_out_channels[i]

								snake_case_						:     Dict     =		i == len(_A		) - 1

								snake_case_						:     Optional[Any]     =		get_up_block(
								    _A						,				num_layers=self.layers_per_block + 1						,				in_channels=_A						,				out_channels=_A						,				prev_output_channel=_A						,				add_upsample=not is_final_block						,				resnet_eps=1E-6						,				resnet_act_fn=_A						,				resnet_groups=_A						,				attention_head_dim=_A						,				temb_channels=_A						,				resnet_time_scale_shift=_A						,				)
								self.up_blocks.append(_A		)
								snake_case_						:     int     =		output_channel

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

							snake_case_						:     Optional[int]     =		False






						def UpperCAmelCase_			( self : str						,				_A : Tuple						,				_A : Optional[int]=None		)							->      List[Any]:







							"""simple docstring"""


							snake_case_						:     Any     =		z
							snake_case_						:     List[str]     =		self.conv_in(_A		)

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

								def create_custom_forward(_A : Any		):
									def custom_forward(*_A : Tuple		):
										return module(*_A		)

									return custom_forward

								if is_torch_version('>='						,				'1.11.0'		):
									# middle
									snake_case_						:     Dict     =		torch.utils.checkpoint.checkpoint(
									    create_custom_forward(self.mid_block		)						,				_A						,				_A						,				use_reentrant=_A		)
									snake_case_						:     Optional[Any]     =		sample.to(_A		)

									# up
									for up_block in self.up_blocks:
										snake_case_						:     int     =		torch.utils.checkpoint.checkpoint(
										    create_custom_forward(_A		)						,				_A						,				_A						,				use_reentrant=_A		)
								else:
									# middle
									snake_case_						:     int     =		torch.utils.checkpoint.checkpoint(
									    create_custom_forward(self.mid_block		)						,				_A						,				_A		)
									snake_case_						:     str     =		sample.to(_A		)

									# up
									for up_block in self.up_blocks:
										snake_case_						:     str     =		torch.utils.checkpoint.checkpoint(create_custom_forward(_A		)						,				_A						,				_A		)
							else:
								# middle
								snake_case_						:     str     =		self.mid_block(_A						,				_A		)
								snake_case_						:     Any     =		sample.to(_A		)

								# up
								for up_block in self.up_blocks:
									snake_case_						:     int     =		up_block(_A						,				_A		)

        # post-process
							if latent_embeds is None:
								snake_case_						:     List[Any]     =		self.conv_norm_out(_A		)
							else:
								snake_case_						:     Optional[int]     =		self.conv_norm_out(_A						,				_A		)
							snake_case_						:     Any     =		self.conv_act(_A		)
							snake_case_						:     Dict     =		self.conv_out(_A		)

							return sample
class SCREAMING_SNAKE_CASE_  (   nn.Module   ):






						def __init__( self : List[Any]						,				_A : int						,				_A : Tuple						,				_A : Optional[int]						,				_A : List[Any]=None						,				_A : Optional[Any]="random"						,				_A : Optional[int]=False						,				_A : Union[str, Any]=True		)							->      Union[str, Any]:







							"""simple docstring"""


							super().__init__()
							snake_case_						:     Optional[Any]     =		n_e
							snake_case_						:     List[Any]     =		vq_embed_dim
							snake_case_						:     Dict     =		beta
							snake_case_						:     List[Any]     =		legacy

							snake_case_						:     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		)

							snake_case_						:     str     =		remap
							if self.remap is not None:
								self.register_buffer('used'						,				torch.tensor(np.load(self.remap		)		)		)
								snake_case_						:     List[Any]     =		self.used.shape[0]
								snake_case_						:     Dict     =		unknown_index  # "random" or "extra" or integer
								if self.unknown_index == "extra":
									snake_case_						:     Union[str, Any]     =		self.re_embed
									snake_case_						:     List[Any]     =		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:
								snake_case_						:     Optional[Any]     =		n_e

							snake_case_						:     Tuple     =		sane_index_shape






						def UpperCAmelCase_			( self : str						,				_A : Any		)							->      List[Any]:







							"""simple docstring"""


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






						def UpperCAmelCase_			( self : Any						,				_A : Dict		)							->      Dict:







							"""simple docstring"""


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






						def UpperCAmelCase_			( self : List[Any]						,				_A : Union[str, Any]		)							->      List[str]:







							"""simple docstring"""


							snake_case_						:     str     =		z.permute(0						,				2						,				3						,				1		).contiguous()
							snake_case_						:     Optional[Any]     =		z.view(-1						,				self.vq_embed_dim		)

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

							snake_case_						:     Optional[int]     =		self.embedding(_A		).view(z.shape		)
							snake_case_						:     int     =		None
							snake_case_						:     List[Any]     =		None

							# compute loss for embedding
							if not self.legacy:
								snake_case_						:     int     =		self.beta * torch.mean((z_q.detach() - z) ** 2		) + torch.mean((z_q - z.detach()) ** 2		)
							else:
								snake_case_						:     int     =		torch.mean((z_q.detach() - z) ** 2		) + self.beta * torch.mean((z_q - z.detach()) ** 2		)

							# preserve gradients
							snake_case_						:     Tuple     =		z + (z_q - z).detach()

							# reshape back to match original input shape
							snake_case_						:     Union[str, Any]     =		z_q.permute(0						,				3						,				1						,				2		).contiguous()

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

							if self.sane_index_shape:
								snake_case_						:     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 UpperCAmelCase_			( self : Optional[int]						,				_A : int						,				_A : Dict		)							->      Optional[Any]:







							"""simple docstring"""


							if self.remap is not None:
								snake_case_						:     Optional[int]     =		indices.reshape(shape[0]						,				-1		)  # add batch axis
								snake_case_						:     Optional[int]     =		self.unmap_to_all(_A		)
								snake_case_						:     Optional[Any]     =		indices.reshape(-1		)  # flatten again

							# get quantized latent vectors
							snake_case_						:     List[Any]     =		self.embedding(_A		)

							if shape is not None:
								snake_case_						:     Union[str, Any]     =		z_q.view(_A		)
								# reshape back to match original input shape
								snake_case_						:     Tuple     =		z_q.permute(0						,				3						,				1						,				2		).contiguous()

							return z_q
class SCREAMING_SNAKE_CASE_  (   snake_case_   ):






						def __init__( self : List[str]						,				_A : Union[str, Any]						,				_A : List[Any]=False		)							->      Any:







							"""simple docstring"""


							snake_case_						:     int     =		parameters
							snake_case_						:     Optional[int]     =		torch.chunk(_A						,				2						,				dim=1		)
							snake_case_						:     str     =		torch.clamp(self.logvar						,				-30.0						,				20.0		)
							snake_case_						:     Any     =		deterministic
							snake_case_						:     int     =		torch.exp(0.5 * self.logvar		)
							snake_case_						:     Optional[int]     =		torch.exp(self.logvar		)
							if self.deterministic:
								snake_case_						:     Union[str, Any]     =		torch.zeros_like(
								    self.mean						,				device=self.parameters.device						,				dtype=self.parameters.dtype		)






						def UpperCAmelCase_			( self : List[Any]						,				_A : Optional[torch.Generator] = None		)							->      torch.FloatTensor:







							"""simple docstring"""


							snake_case_						:     Union[str, Any]     =		randn_tensor(
							    self.mean.shape						,				generator=_A						,				device=self.parameters.device						,				dtype=self.parameters.dtype		)
							snake_case_						:     Optional[int]     =		self.mean + self.std * sample
							return x






						def UpperCAmelCase_			( self : Union[str, Any]						,				_A : Dict=None		)							->      Union[str, Any]:







							"""simple docstring"""


							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 UpperCAmelCase_			( self : Optional[int]						,				_A : Dict						,				_A : List[str]=[1, 2, 3]		)							->      Optional[int]:







							"""simple docstring"""


							if self.deterministic:
								return torch.Tensor([0.0]		)
							snake_case_						:     int     =		np.log(2.0 * np.pi		)
							return 0.5 * torch.sum(logtwopi + self.logvar + torch.pow(sample - self.mean						,				2		) / self.var						,				dim=_A		)






						def UpperCAmelCase_			( self : Optional[int]		)							->      Optional[Any]:







							"""simple docstring"""


							return self.mean








from decimal import Decimal, getcontext
from math import ceil, factorial


def 							SCREAMING_SNAKE_CASE__  (      __a				):

	if not isinstance(__a				,	__a				):
		raise TypeError('Undefined for non-integers'				)
	elif precision < 1:
		raise ValueError('Undefined for non-natural numbers'				)

	snake_case_						:     Dict     =		precision
	snake_case_						:     str     =		ceil(precision / 14				)
	snake_case_						:     str     =		42_68_80 * Decimal(1_00_05				).sqrt()
	snake_case_						:     Tuple     =		1
	snake_case_						:     int     =		13_59_14_09
	snake_case_						:     Tuple     =		Decimal(__a				)
	for k in range(1				,	__a				):
		snake_case_						:     List[Any]     =		factorial(6 * k				) // (factorial(3 * k				) * factorial(__a				) ** 3)
		linear_term += 5_45_14_01_34
		exponential_term *= -26_25_37_41_26_40_76_80_00
		partial_sum += Decimal(multinomial_term * linear_term				) / exponential_term
	return str(constant_term / partial_sum				)[:-1]


if __name__ == "__main__":
						_SCREAMING_SNAKE_CASE 		=						50
						print(F'''The first {n} digits of pi is: {pi(n)}''')

