





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__					(     lowerCamelCase__				):






		'''simple docstring'''






		__a       :       torch.FloatTensor




class     UpperCamelCase__					(     nn.Module				):






		'''simple docstring'''






		def __init__(							self,   snake_case__=3,   snake_case__=3,   snake_case__=("DownEncoderBlock2D",),   snake_case__=(64,),   snake_case__=2,   snake_case__=32,   snake_case__="silu",   snake_case__=True,   )       -> str:






									"""simple docstring"""
									super().__init__()
									lowercase_	:   List[Any]  				=     layers_per_block

									lowercase_	:   List[Any]  				=     torch.nn.Convad(
									    snake_case__,   block_out_channels[0],   kernel_size=3,   stride=1,   padding=1,   )

									lowercase_	:   int  				=     None
									lowercase_	:   str  				=     nn.ModuleList([]  )

									# down
									lowercase_	:   int  				=     block_out_channels[0]
									for i, down_block_type in enumerate(snake_case__  ):
																lowercase_	:   Optional[int]  				=     output_channel
																lowercase_	:   List[str]  				=     block_out_channels[i]
																lowercase_	:   int  				=     i == len(snake_case__  ) - 1

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

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

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

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

									lowercase_	:   List[Any]  				=     False






		def 						snake_case__				(							self,   snake_case__  )       -> int:






									"""simple docstring"""
									lowercase_	:   List[Any]  				=     x
									lowercase_	:   Any  				=     self.conv_in(snake_case__  )

									if self.training and self.gradient_checkpointing:

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

																							return custom_forward

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

									else:
																# down
																for down_block in self.down_blocks:
																							lowercase_	:   List[str]  				=     down_block(snake_case__  )

																# middle
																lowercase_	:   Tuple  				=     self.mid_block(snake_case__  )

									# post-process
									lowercase_	:   int  				=     self.conv_norm_out(snake_case__  )
									lowercase_	:   Optional[int]  				=     self.conv_act(snake_case__  )
									lowercase_	:   Tuple  				=     self.conv_out(snake_case__  )

									return sample




class     UpperCamelCase__					(     nn.Module				):






		'''simple docstring'''






		def __init__(							self,   snake_case__=3,   snake_case__=3,   snake_case__=("UpDecoderBlock2D",),   snake_case__=(64,),   snake_case__=2,   snake_case__=32,   snake_case__="silu",   snake_case__="group",   )       -> Dict:






									"""simple docstring"""
									super().__init__()
									lowercase_	:   int  				=     layers_per_block

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

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

									lowercase_	:   Union[str, Any]  				=     in_channels if norm_type == """spatial""" else None

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

									# up
									lowercase_	:   Any  				=     list(reversed(snake_case__  )  )
									lowercase_	:   List[Any]  				=     reversed_block_out_channels[0]
									for i, up_block_type in enumerate(snake_case__  ):
																lowercase_	:   Any  				=     output_channel
																lowercase_	:   int  				=     reversed_block_out_channels[i]

																lowercase_	:   Dict  				=     i == len(snake_case__  ) - 1

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

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

									lowercase_	:   Dict  				=     False






		def 						snake_case__				(							self,   snake_case__,   snake_case__=None  )       -> Optional[Any]:






									"""simple docstring"""
									lowercase_	:   Tuple  				=     z
									lowercase_	:   str  				=     self.conv_in(snake_case__  )

									lowercase_	:   Optional[int]  				=     next(iter(self.up_blocks.parameters()  )  ).dtype
									if self.training and self.gradient_checkpointing:

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

																							return custom_forward

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

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

																							# up
																							for up_block in self.up_blocks:
																														lowercase_	:   str  				=     torch.utils.checkpoint.checkpoint(create_custom_forward(snake_case__  ),   snake_case__,   snake_case__  )
									else:
																# middle
																lowercase_	:   Tuple  				=     self.mid_block(snake_case__,   snake_case__  )
																lowercase_	:   Union[str, Any]  				=     sample.to(snake_case__  )

																# up
																for up_block in self.up_blocks:
																							lowercase_	:   Any  				=     up_block(snake_case__,   snake_case__  )

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

									return sample




class     UpperCamelCase__					(     nn.Module				):






		'''simple docstring'''






		def __init__(							self,   snake_case__,   snake_case__,   snake_case__,   snake_case__=None,   snake_case__="random",   snake_case__=False,   snake_case__=True  )       -> Dict:






									"""simple docstring"""
									super().__init__()
									lowercase_	:   Optional[int]  				=     n_e
									lowercase_	:   int  				=     vq_embed_dim
									lowercase_	:   List[Any]  				=     beta
									lowercase_	:   Dict  				=     legacy

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

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

									lowercase_	:   List[Any]  				=     sane_index_shape






		def 						snake_case__				(							self,   snake_case__  )       -> Optional[Any]:






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






		def 						snake_case__				(							self,   snake_case__  )       -> Optional[int]:






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






		def 						snake_case__				(							self,   snake_case__  )       -> Optional[Any]:






									"""simple docstring"""
									# reshape z -> (batch, height, width, channel) and flatten
									lowercase_	:   Dict  				=     z.permute(0,   2,   3,   1  ).contiguous()
									lowercase_	:   Union[str, 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
									lowercase_	:   Tuple  				=     torch.argmin(torch.cdist(snake_case__,   self.embedding.weight  ),   dim=1  )

									lowercase_	:   List[Any]  				=     self.embedding(snake_case__  ).view(z.shape  )
									lowercase_	:   Dict  				=     None
									lowercase_	:   str  				=     None

									# compute loss for embedding
									if not self.legacy:
																lowercase_	:   List[str]  				=     self.beta * torch.mean((z_q.detach() - z) ** 2  ) + torch.mean((z_q - z.detach()) ** 2  )
									else:
																lowercase_	:   Tuple  				=     torch.mean((z_q.detach() - z) ** 2  ) + self.beta * torch.mean((z_q - z.detach()) ** 2  )

									# preserve gradients
									lowercase_	:   List[Any]  				=     z + (z_q - z).detach()

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

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

									if self.sane_index_shape:
																lowercase_	:   List[str]  				=     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 						snake_case__				(							self,   snake_case__,   snake_case__  )       -> List[str]:






									"""simple docstring"""
									# shape specifying (batch, height, width, channel)
									if self.remap is not None:
																lowercase_	:   Any  				=     indices.reshape(shape[0],   -1  )  # add batch axis
																lowercase_	:   Any  				=     self.unmap_to_all(snake_case__  )
																lowercase_	:   Optional[int]  				=     indices.reshape(-1  )  # flatten again

									# get quantized latent vectors
									lowercase_	:   str  				=     self.embedding(snake_case__  )

									if shape is not None:
																lowercase_	:   Any  				=     z_q.view(snake_case__  )
																# reshape back to match original input shape
																lowercase_	:   str  				=     z_q.permute(0,   3,   1,   2  ).contiguous()

									return z_q




class     UpperCamelCase__					(     lowerCamelCase__				):






		'''simple docstring'''






		def __init__(							self,   snake_case__,   snake_case__=False  )       -> Optional[Any]:






									"""simple docstring"""
									lowercase_	:   Tuple  				=     parameters
									lowercase_				,		lowercase_	:   Union[str, Any]  				=     torch.chunk(snake_case__,   2,   dim=1  )
									lowercase_	:   List[str]  				=     torch.clamp(self.logvar,   -30.0,   20.0  )
									lowercase_	:   int  				=     deterministic
									lowercase_	:   Union[str, Any]  				=     torch.exp(0.5 * self.logvar  )
									lowercase_	:   Dict  				=     torch.exp(self.logvar  )
									if self.deterministic:
																lowercase_	:   Tuple  				=     torch.zeros_like(
																    self.mean,   device=self.parameters.device,   dtype=self.parameters.dtype  )






		def 						snake_case__				(							self,   snake_case__ = None  )       -> torch.FloatTensor:






									"""simple docstring"""
									# make sure sample is on the same device as the parameters and has same dtype
									lowercase_	:   Tuple  				=     randn_tensor(
									    self.mean.shape,   generator=snake_case__,   device=self.parameters.device,   dtype=self.parameters.dtype  )
									lowercase_	:   Union[str, Any]  				=     self.mean + self.std * sample
									return x






		def 						snake_case__				(							self,   snake_case__=None  )       -> int:






									"""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 						snake_case__				(							self,   snake_case__,   snake_case__=[1, 2, 3]  )       -> str:






									"""simple docstring"""
									if self.deterministic:
																return torch.Tensor([0.0]  )
									lowercase_	:   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=snake_case__  )






		def 						snake_case__				(							self  )       -> Tuple:






									"""simple docstring"""
									return self.mean






def  __magic_name__  (				lowercase				)				-> list:

							"""simple docstring"""



							if n_term == "":
														return []
							lowercase_	:   list  				=     []
							for temp in range(int(lowercase				)				):
														series.append(f"""1/{temp + 1}""" if series else """1"""				)
							return series


if __name__ == "__main__":
							UpperCAmelCase_							=				input("""Enter the last number (nth term) of the Harmonic Series""")
							print("""Formula of Harmonic Series => 1+1/2+1/3 ..... 1/n""")
							print(harmonic_series(nth_term))