




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





     '''simple docstring'''



     lowerCAmelCase_  :     torch.FloatTensor







class 					_a (nn.Module	):





     '''simple docstring'''



     def __init__(				self							,__a=3							,__a=3							,__a=("DownEncoderBlock2D",)							,__a=(64,)							,__a=2							,__a=32							,__a="silu"							,__a=True							,)      ->				List[str]:
          super().__init__()
          snake_case			:      List[Any]        =			layers_per_block

          snake_case			:      Union[str, Any]        =			torch.nn.Convad(
              __a							,block_out_channels[0]							,kernel_size=3							,stride=1							,padding=1							,)

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

          # down
          snake_case			:      int        =			block_out_channels[0]
          for i, down_block_type in enumerate(__a							):
               snake_case			:      int        =			output_channel
               snake_case			:      Optional[Any]        =			block_out_channels[i]
               snake_case			:      int        =			i == len(__a							) - 1

               snake_case			:      List[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			:      Any        =			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			:      Any        =			nn.SiLU()

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

          snake_case			:      List[Any]        =			False







     def 			snake_case_      (				self							,__a							)      ->				Tuple:
          snake_case			:      Any        =			x
          snake_case			:      Dict        =			self.conv_in(__a							)

          if self.training and self.gradient_checkpointing:

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

                    return custom_forward

               # down
               if is_torch_version(""">="""							,"""1.11.0"""							):
                    for down_block in self.down_blocks:
                         snake_case			:      int        =			torch.utils.checkpoint.checkpoint(
                             create_custom_forward(__a							)							,__a							,use_reentrant=__a							)
                    # middle
                    snake_case			:      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			:      Any        =			torch.utils.checkpoint.checkpoint(create_custom_forward(__a							)							,__a							)
                    # middle
                    snake_case			:      Union[str, Any]        =			torch.utils.checkpoint.checkpoint(create_custom_forward(self.mid_block							)							,__a							)

          else:
               # down
               for down_block in self.down_blocks:
                    snake_case			:      Dict        =			down_block(__a							)

               # middle
               snake_case			:      int        =			self.mid_block(__a							)

          # post-process
          snake_case			:      Dict        =			self.conv_norm_out(__a							)
          snake_case			:      Dict        =			self.conv_act(__a							)
          snake_case			:      Any        =			self.conv_out(__a							)

          return sample







class 					_a (nn.Module	):





     '''simple docstring'''



     def __init__(				self							,__a=3							,__a=3							,__a=("UpDecoderBlock2D",)							,__a=(64,)							,__a=2							,__a=32							,__a="silu"							,__a="group"							,)      ->				Any:
          super().__init__()
          snake_case			:      List[Any]        =			layers_per_block

          snake_case			:      Optional[Any]        =			nn.Convad(
              __a							,block_out_channels[-1]							,kernel_size=3							,stride=1							,padding=1							,)

          snake_case			:      List[Any]        =			None
          snake_case			:      Optional[Any]        =			nn.ModuleList([]							)

          snake_case			:      List[str]        =			in_channels if norm_type == """spatial""" else None

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

               snake_case			:      Optional[Any]        =			i == len(__a							) - 1

               snake_case			:      List[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			:      Any        =			output_channel

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

          snake_case			:      List[str]        =			False







     def 			snake_case_      (				self							,__a							,__a=None							)      ->				Any:
          snake_case			:      Union[str, Any]        =			z
          snake_case			:      int        =			self.conv_in(__a							)

          snake_case			:      str        =			next(iter(self.up_blocks.parameters()							)							).dtype
          if self.training and self.gradient_checkpointing:

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

                    return custom_forward

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

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

                    # up
                    for up_block in self.up_blocks:
                         snake_case			:      int        =			torch.utils.checkpoint.checkpoint(create_custom_forward(__a							)							,__a							,__a							)
          else:
               # middle
               snake_case			:      int        =			self.mid_block(__a							,__a							)
               snake_case			:      Dict        =			sample.to(__a							)

               # up
               for up_block in self.up_blocks:
                    snake_case			:      Tuple        =			up_block(__a							,__a							)

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

          return sample







class 					_a (nn.Module	):





     '''simple docstring'''



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

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

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

          snake_case			:      Union[str, Any]        =			sane_index_shape







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







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







     def 			snake_case_      (				self							,__a							)      ->				Any:
          # reshape z -> (batch, height, width, channel) and flatten
          snake_case			:      Optional[int]        =			z.permute(0							,2							,3							,1							).contiguous()
          snake_case			:      List[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			:      Dict        =			self.embedding(__a							).view(z.shape							)
          snake_case			:      str        =			None
          snake_case			:      Optional[int]        =			None

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

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

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

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

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

          # get quantized latent vectors
          snake_case			:      Tuple        =			self.embedding(__a							)

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

          return z_q







class 					_a (a__	):





     '''simple docstring'''



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









'''simple docstring'''







import numpy as np
from cva import destroyAllWindows, imread, imshow, waitKey







class 					_a :





     '''simple docstring'''



     def __init__(				self							,__a							,__a							,__a							)      ->				Tuple:
          if dst_width < 0 or dst_height < 0:
               raise ValueError("""Destination width/height should be > 0"""							)

          snake_case			:      str        =			img
          snake_case			:      str        =			img.shape[1]
          snake_case			:      Union[str, Any]        =			img.shape[0]
          snake_case			:      Optional[int]        =			dst_width
          snake_case			:      List[str]        =			dst_height

          snake_case			:      List[str]        =			self.src_w / self.dst_w
          snake_case			:      int        =			self.src_h / self.dst_h

          snake_case			:      List[Any]        =			(
              np.ones((self.dst_h, self.dst_w, 3)							,np.uinta							) * 255
          )







     def 			snake_case_      (				self							)      ->				int:
          for i in range(self.dst_h							):
               for j in range(self.dst_w							):
                    snake_case			:      Optional[Any]        =			self.img[self.get_y(__a							)][self.get_x(__a							)]







     def 			snake_case_      (				self							,__a							)      ->				int:
          return int(self.ratio_x * x							)







     def 			snake_case_      (				self							,__a							)      ->				int:
          return int(self.ratio_y * y							)


if __name__ == "__main__":
      lowercase,	lowercase      :   Optional[int]      =       800, 600
      lowercase      :   Union[str, Any]      =       imread("""image_data/lena.jpg""", 1)
      lowercase      :   int      =       NearestNeighbour(im, dst_w, dst_h)
      n.process()

      imshow(
          F"""Image resized from: {im.shape[1]}x{im.shape[0]} to {dst_w}x{dst_h}""", n.output
      )
      waitKey(0)
      destroyAllWindows()



