






'''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      A     (   SCREAMING_SNAKE_CASE__    ):






 """simple docstring"""





 __a      :    torch.FloatTensor






class      A     (   nn.Module    ):






 """simple docstring"""





 def __init__(				self      ,  __lowerCAmelCase=3      ,  __lowerCAmelCase=3      ,  __lowerCAmelCase=("DownEncoderBlock2D",)      ,  __lowerCAmelCase=(64,)      ,  __lowerCAmelCase=2      ,  __lowerCAmelCase=32      ,  __lowerCAmelCase="silu"      ,  __lowerCAmelCase=True      ,  ):
       super().__init__()
       UpperCamelCase_					: List[Any]            =				layers_per_block

       UpperCamelCase_					: Any            =				torch.nn.Convad(
           __lowerCAmelCase      ,  block_out_channels[0]      ,  kernel_size=3      ,  stride=1      ,  padding=1      ,  )

       UpperCamelCase_					: str            =				None
       UpperCamelCase_					: str            =				nn.ModuleList([]			)

       # down
       UpperCamelCase_					: List[Any]            =				block_out_channels[0]
       for i, down_block_type in enumerate(__lowerCAmelCase			):
             UpperCamelCase_					: List[Any]            =				output_channel
             UpperCamelCase_					: List[Any]            =				block_out_channels[i]
             UpperCamelCase_					: Dict            =				i == len(__lowerCAmelCase			) - 1

             UpperCamelCase_					: Optional[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
       UpperCamelCase_					: int            =				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
       UpperCamelCase_					: Union[str, Any]            =				nn.GroupNorm(num_channels=block_out_channels[-1]      ,  num_groups=__lowerCAmelCase      ,  eps=1E-6			)
       UpperCamelCase_					: Any            =				nn.SiLU()

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

       UpperCamelCase_					: Dict            =				False





 def 	_UpperCAmelCase							(				self      ,  __lowerCAmelCase			):
       UpperCamelCase_					: Any            =				x
       UpperCamelCase_					: List[str]            =				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:
                         UpperCamelCase_					: Dict            =				torch.utils.checkpoint.checkpoint(
                             create_custom_forward(__lowerCAmelCase			)      ,  __lowerCAmelCase      ,  use_reentrant=__lowerCAmelCase			)
                   # middle
                   UpperCamelCase_					: Dict            =				torch.utils.checkpoint.checkpoint(
                       create_custom_forward(self.mid_block			)      ,  __lowerCAmelCase      ,  use_reentrant=__lowerCAmelCase			)
             else:
                   for down_block in self.down_blocks:
                         UpperCamelCase_					: Tuple            =				torch.utils.checkpoint.checkpoint(create_custom_forward(__lowerCAmelCase			)      ,  __lowerCAmelCase			)
                   # middle
                   UpperCamelCase_					: Tuple            =				torch.utils.checkpoint.checkpoint(create_custom_forward(self.mid_block			)      ,  __lowerCAmelCase			)

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

             # middle
             UpperCamelCase_					: Union[str, Any]            =				self.mid_block(__lowerCAmelCase			)

       # post-process
       UpperCamelCase_					: List[str]            =				self.conv_norm_out(__lowerCAmelCase			)
       UpperCamelCase_					: Optional[Any]            =				self.conv_act(__lowerCAmelCase			)
       UpperCamelCase_					: Any            =				self.conv_out(__lowerCAmelCase			)

       return sample






class      A     (   nn.Module    ):






 """simple docstring"""





 def __init__(				self      ,  __lowerCAmelCase=3      ,  __lowerCAmelCase=3      ,  __lowerCAmelCase=("UpDecoderBlock2D",)      ,  __lowerCAmelCase=(64,)      ,  __lowerCAmelCase=2      ,  __lowerCAmelCase=32      ,  __lowerCAmelCase="silu"      ,  __lowerCAmelCase="group"      ,  ):
       super().__init__()
       UpperCamelCase_					: int            =				layers_per_block

       UpperCamelCase_					: int            =				nn.Convad(
           __lowerCAmelCase      ,  block_out_channels[-1]      ,  kernel_size=3      ,  stride=1      ,  padding=1      ,  )

       UpperCamelCase_					: Tuple            =				None
       UpperCamelCase_					: Optional[Any]            =				nn.ModuleList([]			)

       UpperCamelCase_					: int            =				in_channels if norm_type == """spatial""" else None

       # mid
       UpperCamelCase_					: Optional[int]            =				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
       UpperCamelCase_					: str            =				list(reversed(__lowerCAmelCase			)			)
       UpperCamelCase_					: Optional[Any]            =				reversed_block_out_channels[0]
       for i, up_block_type in enumerate(__lowerCAmelCase			):
             UpperCamelCase_					: Dict            =				output_channel
             UpperCamelCase_					: Tuple            =				reversed_block_out_channels[i]

             UpperCamelCase_					: Optional[Any]            =				i == len(__lowerCAmelCase			) - 1

             UpperCamelCase_					: Optional[int]            =				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			)
             UpperCamelCase_					: str            =				output_channel

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

       UpperCamelCase_					: Tuple            =				False





 def 	_UpperCAmelCase							(				self      ,  __lowerCAmelCase      ,  __lowerCAmelCase=None			):
       UpperCamelCase_					: List[Any]            =				z
       UpperCamelCase_					: Union[str, Any]            =				self.conv_in(__lowerCAmelCase			)

       UpperCamelCase_					: 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
                   UpperCamelCase_					: Any            =				torch.utils.checkpoint.checkpoint(
                       create_custom_forward(self.mid_block			)      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  use_reentrant=__lowerCAmelCase			)
                   UpperCamelCase_					: List[Any]            =				sample.to(__lowerCAmelCase			)

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

                   # up
                   for up_block in self.up_blocks:
                         UpperCamelCase_					: Optional[Any]            =				torch.utils.checkpoint.checkpoint(create_custom_forward(__lowerCAmelCase			)      ,  __lowerCAmelCase      ,  __lowerCAmelCase			)
       else:
             # middle
             UpperCamelCase_					: Tuple            =				self.mid_block(__lowerCAmelCase      ,  __lowerCAmelCase			)
             UpperCamelCase_					: List[str]            =				sample.to(__lowerCAmelCase			)

             # up
             for up_block in self.up_blocks:
                   UpperCamelCase_					: str            =				up_block(__lowerCAmelCase      ,  __lowerCAmelCase			)

        # post-process
       if latent_embeds is None:
             UpperCamelCase_					: Tuple            =				self.conv_norm_out(__lowerCAmelCase			)
       else:
             UpperCamelCase_					: Dict            =				self.conv_norm_out(__lowerCAmelCase      ,  __lowerCAmelCase			)
       UpperCamelCase_					: List[Any]            =				self.conv_act(__lowerCAmelCase			)
       UpperCamelCase_					: List[Any]            =				self.conv_out(__lowerCAmelCase			)

       return sample






class      A     (   nn.Module    ):






 """simple docstring"""





 def __init__(				self      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase=None      ,  __lowerCAmelCase="random"      ,  __lowerCAmelCase=False      ,  __lowerCAmelCase=True			):
       super().__init__()
       UpperCamelCase_					: Optional[Any]            =				n_e
       UpperCamelCase_					: Optional[int]            =				vq_embed_dim
       UpperCamelCase_					: List[Any]            =				beta
       UpperCamelCase_					: Any            =				legacy

       UpperCamelCase_					: Optional[int]            =				nn.Embedding(self.n_e      ,  self.vq_embed_dim			)
       self.embedding.weight.data.uniform_(-1.0 / self.n_e      ,  1.0 / self.n_e			)

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

       UpperCamelCase_					: Tuple            =				sane_index_shape
 def 	_UpperCAmelCase							(				self      ,  __lowerCAmelCase			):
       UpperCamelCase_					: int            =				inds.shape
       assert len(__lowerCAmelCase			) > 1
       UpperCamelCase_					: Optional[Any]            =				inds.reshape(ishape[0]      ,  -1			)
       UpperCamelCase_					: List[str]            =				self.used.to(__lowerCAmelCase			)
       UpperCamelCase_					: List[Any]            =				(inds[:, :, None] == used[None, None, ...]).long()
       UpperCamelCase_					: int            =				match.argmax(-1			)
       UpperCamelCase_					: List[Any]            =				match.sum(2			) < 1
       if self.unknown_index == "random":
             UpperCamelCase_					: str            =				torch.randint(0      ,  self.re_embed      ,  size=new[unknown].shape			).to(device=new.device			)
       else:
             UpperCamelCase_					: Optional[int]            =				self.unknown_index
       return new.reshape(__lowerCAmelCase			)
 def 	_UpperCAmelCase							(				self      ,  __lowerCAmelCase			):
       UpperCamelCase_					: Optional[int]            =				inds.shape
       assert len(__lowerCAmelCase			) > 1
       UpperCamelCase_					: Dict            =				inds.reshape(ishape[0]      ,  -1			)
       UpperCamelCase_					: List[Any]            =				self.used.to(__lowerCAmelCase			)
       if self.re_embed > self.used.shape[0]:  # extra token
             UpperCamelCase_					: Optional[int]            =				0  # simply set to zero
       UpperCamelCase_					: Any            =				torch.gather(used[None, :][inds.shape[0] * [0], :]      ,  1      ,  __lowerCAmelCase			)
       return back.reshape(__lowerCAmelCase			)
 def 	_UpperCAmelCase							(				self      ,  __lowerCAmelCase			):
       # reshape z -> (batch, height, width, channel) and flatten
       UpperCamelCase_					: List[str]            =				z.permute(0      ,  2      ,  3      ,  1			).contiguous()
       UpperCamelCase_					: Dict            =				z.view(-1      ,  self.vq_embed_dim			)

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

       UpperCamelCase_					: Union[str, Any]            =				self.embedding(__lowerCAmelCase			).view(z.shape			)
       UpperCamelCase_					: int            =				None
       UpperCamelCase_					: Tuple            =				None

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

       # preserve gradients
       UpperCamelCase_					: Dict            =				z + (z_q - z).detach()

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

       if self.remap is not None:
             UpperCamelCase_					: Union[str, Any]            =				min_encoding_indices.reshape(z.shape[0]      ,  -1			)  # add batch axis
             UpperCamelCase_					: str            =				self.remap_to_used(__lowerCAmelCase			)
             UpperCamelCase_					: Tuple            =				min_encoding_indices.reshape(-1      ,  1			)  # flatten

       if self.sane_index_shape:
             UpperCamelCase_					: Optional[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      ,  __lowerCAmelCase      ,  __lowerCAmelCase			):
       # shape specifying (batch, height, width, channel)
       if self.remap is not None:
             UpperCamelCase_					: int            =				indices.reshape(shape[0]      ,  -1			)  # add batch axis
             UpperCamelCase_					: Optional[Any]            =				self.unmap_to_all(__lowerCAmelCase			)
             UpperCamelCase_					: List[Any]            =				indices.reshape(-1			)  # flatten again

       # get quantized latent vectors
       UpperCamelCase_					: Tuple            =				self.embedding(__lowerCAmelCase			)

       if shape is not None:
             UpperCamelCase_					: Tuple            =				z_q.view(__lowerCAmelCase			)
             # reshape back to match original input shape
             UpperCamelCase_					: Tuple            =				z_q.permute(0      ,  3      ,  1      ,  2			).contiguous()

       return z_q






class      A     (   SCREAMING_SNAKE_CASE__    ):






 """simple docstring"""





 def __init__(				self      ,  __lowerCAmelCase      ,  __lowerCAmelCase=False			):
       UpperCamelCase_					: Dict            =				parameters
       UpperCamelCase_     ,      UpperCamelCase_					: Optional[Any]            =				torch.chunk(__lowerCAmelCase      ,  2      ,  dim=1			)
       UpperCamelCase_					: str            =				torch.clamp(self.logvar      ,  -30.0      ,  20.0			)
       UpperCamelCase_					: Optional[Any]            =				deterministic
       UpperCamelCase_					: List[Any]            =				torch.exp(0.5 * self.logvar			)
       UpperCamelCase_					: int            =				torch.exp(self.logvar			)
       if self.deterministic:
             UpperCamelCase_					: Any            =				torch.zeros_like(
                 self.mean      ,  device=self.parameters.device      ,  dtype=self.parameters.dtype			)
 def 	_UpperCAmelCase							(				self      ,  __lowerCAmelCase = None			):
       # make sure sample is on the same device as the parameters and has same dtype
       UpperCamelCase_					: Tuple            =				randn_tensor(
           self.mean.shape      ,  generator=__lowerCAmelCase      ,  device=self.parameters.device      ,  dtype=self.parameters.dtype			)
       UpperCamelCase_					: Tuple            =				self.mean + self.std * sample
       return x
 def 	_UpperCAmelCase							(				self      ,  __lowerCAmelCase=None			):
       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      ,  __lowerCAmelCase      ,  __lowerCAmelCase=[1, 2, 3]			):
       if self.deterministic:
             return torch.Tensor([0.0]			)
       UpperCamelCase_					: List[Any]            =				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 	_UpperCAmelCase							(				self			):
       return self.mean














'''simple docstring'''

from __future__ import annotations

from math import pi, sqrt



def 				snake_case       (  a_				:			float     , a_				:			float						)							->					tuple:





      """simple docstring"""

      if inductance <= 0:
            raise ValueError("""Inductance cannot be 0 or negative"""						)

      elif capacitance <= 0:
            raise ValueError("""Capacitance cannot be 0 or negative"""						)

      else:
            return (
                "Resonant frequency",
                float(1 / (2 * pi * (sqrt(inductance * capacitance						)))						),
            )


if __name__ == "__main__":
      import doctest

      doctest.testmod()






