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_ ):
       _UpperCAmelCase				:  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=(6_4,)		, A					:     Dict=2		, A					:     str=3_2		, A					:     List[str]="silu"		, A					:     Any=True		, )       ->Optional[int]:
          super().__init__()
          lowerCamelCase__       :						List[str]        =							layers_per_block

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

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

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

             lowerCamelCase__       :						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
          lowerCamelCase__       :						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
          lowerCamelCase__       :						Optional[int]        =							nn.GroupNorm(num_channels=block_out_channels[-1]		, num_groups=_A		, eps=1e-6      )
          lowerCamelCase__       :						int        =							nn.SiLU()

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

          lowerCamelCase__       :						List[str]        =							False







       def 						__lowerCamelCase   (				self					:     int		, A					:     Optional[int]      )       ->Tuple:
          lowerCamelCase__       :						Any        =							x
          lowerCamelCase__       :						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:
                   lowerCamelCase__       :						List[str]        =							torch.utils.checkpoint.checkpoint(
                       create_custom_forward(_A      )		, _A		, use_reentrant=_A      )
                # middle
                lowerCamelCase__       :						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:
                   lowerCamelCase__       :						Tuple        =							torch.utils.checkpoint.checkpoint(create_custom_forward(_A      )		, _A      )
                # middle
                lowerCamelCase__       :						Any        =							torch.utils.checkpoint.checkpoint(create_custom_forward(self.mid_block      )		, _A      )

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

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

          # post-process
          lowerCamelCase__       :						str        =							self.conv_norm_out(_A      )
          lowerCamelCase__       :						Dict        =							self.conv_act(_A      )
          lowerCamelCase__       :						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=(6_4,)		, A					:     Tuple=2		, A					:     str=3_2		, A					:     Optional[int]="silu"		, A					:     Optional[int]="group"		, )       ->List[str]:
          super().__init__()
          lowerCamelCase__       :						Any        =							layers_per_block

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

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

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

          # mid
          lowerCamelCase__       :						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
          lowerCamelCase__       :						Union[str, Any]        =							list(reversed(_A      )      )
          lowerCamelCase__       :						int        =							reversed_block_out_channels[0]
          for i, up_block_type in enumerate(_A      ):
             lowerCamelCase__       :						List[Any]        =							output_channel
             lowerCamelCase__       :						Optional[int]        =							reversed_block_out_channels[i]

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

             lowerCamelCase__       :						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      )
             lowerCamelCase__       :						int        =							output_channel

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

          lowerCamelCase__       :						Optional[int]        =							False







       def 						__lowerCamelCase   (				self					:     str		, A					:     Tuple		, A					:     Optional[int]=None      )       ->List[Any]:
          lowerCamelCase__       :						Any        =							z
          lowerCamelCase__       :						List[str]        =							self.conv_in(_A      )

          lowerCamelCase__       :						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
                lowerCamelCase__       :						Dict        =							torch.utils.checkpoint.checkpoint(
                    create_custom_forward(self.mid_block      )		, _A		, _A		, use_reentrant=_A      )
                lowerCamelCase__       :						Optional[Any]        =							sample.to(_A      )

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

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

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

        # post-process
          if latent_embeds is None:
             lowerCamelCase__       :						List[Any]        =							self.conv_norm_out(_A      )
          else:
             lowerCamelCase__       :						Optional[int]        =							self.conv_norm_out(_A		, _A      )
          lowerCamelCase__       :						Any        =							self.conv_act(_A      )
          lowerCamelCase__       :						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]:
          super().__init__()
          lowerCamelCase__       :						Optional[Any]        =							n_e
          lowerCamelCase__       :						List[Any]        =							vq_embed_dim
          lowerCamelCase__       :						Dict        =							beta
          lowerCamelCase__       :						List[Any]        =							legacy

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

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

          lowerCamelCase__       :						Tuple        =							sane_index_shape







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







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







       def 						__lowerCamelCase   (				self					:     List[Any]		, A					:     Union[str, Any]      )       ->List[str]:
          lowerCamelCase__       :						str        =							z.permute(0		, 2		, 3		, 1      ).contiguous()
          lowerCamelCase__       :						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
          lowerCamelCase__       :						int        =							torch.argmin(torch.cdist(_A		, self.embedding.weight      )		, dim=1      )

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

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

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

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

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

          if self.sane_index_shape:
             lowerCamelCase__       :						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 						__lowerCamelCase   (				self					:     Optional[int]		, A					:     int		, A					:     Dict      )       ->Optional[Any]:


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

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

          if shape is not None:
             lowerCamelCase__       :						Union[str, Any]        =							z_q.view(_A      )
             # reshape back to match original input shape
             lowerCamelCase__       :						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:
          lowerCamelCase__       :						int        =							parameters
          lowerCamelCase__       :						Optional[int]        =							torch.chunk(_A		, 2		, dim=1      )
          lowerCamelCase__       :						str        =							torch.clamp(self.logvar		, -3_0.0		, 2_0.0      )
          lowerCamelCase__       :						Any        =							deterministic
          lowerCamelCase__       :						int        =							torch.exp(0.5 * self.logvar      )
          lowerCamelCase__       :						Optional[int]        =							torch.exp(self.logvar      )
          if self.deterministic:
             lowerCamelCase__       :						Union[str, Any]        =							torch.zeros_like(
                 self.mean		, device=self.parameters.device		, dtype=self.parameters.dtype      )







       def 						__lowerCamelCase   (				self					:     List[Any]		, A					:     Optional[torch.Generator] = None      )       ->torch.FloatTensor:
          lowerCamelCase__       :						Union[str, Any]        =							randn_tensor(
              self.mean.shape		, generator=_A		, device=self.parameters.device		, dtype=self.parameters.dtype      )
          lowerCamelCase__       :						Optional[int]        =							self.mean + self.std * sample
          return x







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


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


          if self.deterministic:
             return torch.Tensor([0.0]      )
          lowerCamelCase__       :						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 						__lowerCamelCase   (				self					:     Optional[int]      )       ->Optional[Any]:
          return self.mean










import pprint

import requests

_A :   Any  			=  'https://zenquotes.io/api'



def 	_a						(   )    ->    list:







   """simple docstring"""
   return requests.get(API_ENDPOINT_URL + '''/today'''					).json()



def 	_a						(   )    ->    list:







   """simple docstring"""
   return requests.get(API_ENDPOINT_URL + '''/random'''					).json()


if __name__ == "__main__":
       _A :   Optional[Any]  			=  random_quotes()
       pprint.pprint(response)



