

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




      """simple docstring"""

      _lowerCamelCase       :			torch.FloatTensor




class   _a					(			nn.Module				):




      """simple docstring"""

      def __init__(   self							:				Optional[Any]						,			UpperCAmelCase							:				str=3						,			UpperCAmelCase							:				Dict=3						,			UpperCAmelCase							:				int=("DownEncoderBlock2D",)						,			UpperCAmelCase							:				Tuple=(64,)						,			UpperCAmelCase							:				List[Any]=2						,			UpperCAmelCase							:				Optional[int]=32						,			UpperCAmelCase							:				Optional[int]="silu"						,			UpperCAmelCase							:				List[Any]=True						,			):
            super().__init__()
            A_						  =				layers_per_block

            A_						  =				torch.nn.Convad(
                UpperCAmelCase						,			block_out_channels[0]						,			kernel_size=3						,			stride=1						,			padding=1						,			)

            A_						  =				None
            A_						  =				nn.ModuleList([]	)

            # down
            A_						  =				block_out_channels[0]
            for i, down_block_type in enumerate(UpperCAmelCase	):
                  A_						  =				output_channel
                  A_						  =				block_out_channels[i]
                  A_						  =				i == len(UpperCAmelCase	) - 1

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

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

            # out
            A_						  =				nn.GroupNorm(num_channels=block_out_channels[-1]						,			num_groups=UpperCAmelCase						,			eps=1E-6	)
            A_						  =				nn.SiLU()

            A_						  =				2 * out_channels if double_z else out_channels
            A_						  =				nn.Convad(block_out_channels[-1]						,			UpperCAmelCase						,			3						,			padding=1	)

            A_						  =				False







      def 		__A	(   self							:				Optional[Any]						,			UpperCAmelCase							:				Optional[int]	):
            A_						  =				x
            A_						  =				self.conv_in(UpperCAmelCase	)

            if self.training and self.gradient_checkpointing:

                  def create_custom_forward(UpperCAmelCase							:				int	):
                        def custom_forward(*UpperCAmelCase							:				str	):
                              return module(*UpperCAmelCase	)

                        return custom_forward

                  # down
                  if is_torch_version(">="						,			"1.11.0"	):
                        for down_block in self.down_blocks:
                              A_						  =				torch.utils.checkpoint.checkpoint(
                                  create_custom_forward(UpperCAmelCase	)						,			UpperCAmelCase						,			use_reentrant=UpperCAmelCase	)
                        # middle
                        A_						  =				torch.utils.checkpoint.checkpoint(
                            create_custom_forward(self.mid_block	)						,			UpperCAmelCase						,			use_reentrant=UpperCAmelCase	)
                  else:
                        for down_block in self.down_blocks:
                              A_						  =				torch.utils.checkpoint.checkpoint(create_custom_forward(UpperCAmelCase	)						,			UpperCAmelCase	)
                        # middle
                        A_						  =				torch.utils.checkpoint.checkpoint(create_custom_forward(self.mid_block	)						,			UpperCAmelCase	)

            else:
                  # down
                  for down_block in self.down_blocks:
                        A_						  =				down_block(UpperCAmelCase	)

                  # middle
                  A_						  =				self.mid_block(UpperCAmelCase	)

            # post-process
            A_						  =				self.conv_norm_out(UpperCAmelCase	)
            A_						  =				self.conv_act(UpperCAmelCase	)
            A_						  =				self.conv_out(UpperCAmelCase	)

            return sample




class   _a					(			nn.Module				):




      """simple docstring"""

      def __init__(   self							:				List[Any]						,			UpperCAmelCase							:				str=3						,			UpperCAmelCase							:				Any=3						,			UpperCAmelCase							:				int=("UpDecoderBlock2D",)						,			UpperCAmelCase							:				str=(64,)						,			UpperCAmelCase							:				List[Any]=2						,			UpperCAmelCase							:				int=32						,			UpperCAmelCase							:				Optional[Any]="silu"						,			UpperCAmelCase							:				Any="group"						,			):
            super().__init__()
            A_						  =				layers_per_block

            A_						  =				nn.Convad(
                UpperCAmelCase						,			block_out_channels[-1]						,			kernel_size=3						,			stride=1						,			padding=1						,			)

            A_						  =				None
            A_						  =				nn.ModuleList([]	)

            A_						  =				in_channels if norm_type == "spatial" else None

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

            # up
            A_						  =				list(reversed(UpperCAmelCase	)	)
            A_						  =				reversed_block_out_channels[0]
            for i, up_block_type in enumerate(UpperCAmelCase	):
                  A_						  =				output_channel
                  A_						  =				reversed_block_out_channels[i]

                  A_						  =				i == len(UpperCAmelCase	) - 1

                  A_						  =				get_up_block(
                      UpperCAmelCase						,			num_layers=self.layers_per_block + 1						,			in_channels=UpperCAmelCase						,			out_channels=UpperCAmelCase						,			prev_output_channel=UpperCAmelCase						,			add_upsample=not is_final_block						,			resnet_eps=1E-6						,			resnet_act_fn=UpperCAmelCase						,			resnet_groups=UpperCAmelCase						,			attention_head_dim=UpperCAmelCase						,			temb_channels=UpperCAmelCase						,			resnet_time_scale_shift=UpperCAmelCase						,			)
                  self.up_blocks.append(UpperCAmelCase	)
                  A_						  =				output_channel

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

            A_						  =				False







      def 		__A	(   self							:				Union[str, Any]						,			UpperCAmelCase							:				Tuple						,			UpperCAmelCase							:				Tuple=None	):
            A_						  =				z
            A_						  =				self.conv_in(UpperCAmelCase	)

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

                  def create_custom_forward(UpperCAmelCase							:				Union[str, Any]	):
                        def custom_forward(*UpperCAmelCase							:				int	):
                              return module(*UpperCAmelCase	)

                        return custom_forward

                  if is_torch_version(">="						,			"1.11.0"	):
                        # middle
                        A_						  =				torch.utils.checkpoint.checkpoint(
                            create_custom_forward(self.mid_block	)						,			UpperCAmelCase						,			UpperCAmelCase						,			use_reentrant=UpperCAmelCase	)
                        A_						  =				sample.to(UpperCAmelCase	)

                        # up
                        for up_block in self.up_blocks:
                              A_						  =				torch.utils.checkpoint.checkpoint(
                                  create_custom_forward(UpperCAmelCase	)						,			UpperCAmelCase						,			UpperCAmelCase						,			use_reentrant=UpperCAmelCase	)
                  else:
                        # middle
                        A_						  =				torch.utils.checkpoint.checkpoint(
                            create_custom_forward(self.mid_block	)						,			UpperCAmelCase						,			UpperCAmelCase	)
                        A_						  =				sample.to(UpperCAmelCase	)

                        # up
                        for up_block in self.up_blocks:
                              A_						  =				torch.utils.checkpoint.checkpoint(create_custom_forward(UpperCAmelCase	)						,			UpperCAmelCase						,			UpperCAmelCase	)
            else:
                  # middle
                  A_						  =				self.mid_block(UpperCAmelCase						,			UpperCAmelCase	)
                  A_						  =				sample.to(UpperCAmelCase	)

                  # up
                  for up_block in self.up_blocks:
                        A_						  =				up_block(UpperCAmelCase						,			UpperCAmelCase	)

        # post-process
            if latent_embeds is None:
                  A_						  =				self.conv_norm_out(UpperCAmelCase	)
            else:
                  A_						  =				self.conv_norm_out(UpperCAmelCase						,			UpperCAmelCase	)
            A_						  =				self.conv_act(UpperCAmelCase	)
            A_						  =				self.conv_out(UpperCAmelCase	)

            return sample




class   _a					(			nn.Module				):




      """simple docstring"""

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

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

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

            A_						  =				sane_index_shape





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





      def 		__A	(   self							:				str						,			UpperCAmelCase							:				Union[str, Any]	):
            A_						  =				inds.shape
            assert len(UpperCAmelCase	) > 1
            A_						  =				inds.reshape(ishape[0]						,			-1	)
            A_						  =				self.used.to(UpperCAmelCase	)
            if self.re_embed > self.used.shape[0]:  # extra token
                  A_						  =				0  # simply set to zero
            A_						  =				torch.gather(used[None, :][inds.shape[0] * [0], :]						,			1						,			UpperCAmelCase	)
            return back.reshape(UpperCAmelCase	)





      def 		__A	(   self							:				Any						,			UpperCAmelCase							:				List[Any]	):
            # reshape z -> (batch, height, width, channel) and flatten
            A_						  =				z.permute(0						,			2						,			3						,			1	).contiguous()
            A_						  =				z.view(-1						,			self.vq_embed_dim	)

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

            A_						  =				self.embedding(UpperCAmelCase	).view(z.shape	)
            A_						  =				None
            A_						  =				None

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

            # preserve gradients
            A_						  =				z + (z_q - z).detach()

            # reshape back to match original input shape
            A_						  =				z_q.permute(0						,			3						,			1						,			2	).contiguous()

            if self.remap is not None:
                  A_						  =				min_encoding_indices.reshape(z.shape[0]						,			-1	)  # add batch axis
                  A_						  =				self.remap_to_used(UpperCAmelCase	)
                  A_						  =				min_encoding_indices.reshape(-1						,			1	)  # flatten

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

            # get quantized latent vectors
            A_						  =				self.embedding(UpperCAmelCase	)

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

            return z_q




class   _a					(			snake_case_				):




      """simple docstring"""

      def __init__(   self							:				Optional[int]						,			UpperCAmelCase							:				List[Any]						,			UpperCAmelCase							:				List[Any]=False	):
            A_						  =				parameters
            A_  ,     A_						  =				torch.chunk(UpperCAmelCase						,			2						,			dim=1	)
            A_						  =				torch.clamp(self.logvar						,			-30.0						,			20.0	)
            A_						  =				deterministic
            A_						  =				torch.exp(0.5 * self.logvar	)
            A_						  =				torch.exp(self.logvar	)
            if self.deterministic:
                  A_						  =				A_						  =				torch.zeros_like(
                      self.mean						,			device=self.parameters.device						,			dtype=self.parameters.dtype	)





      def 		__A	(   self							:				Optional[int]						,			UpperCAmelCase							:				Optional[torch.Generator] = None	):
            # make sure sample is on the same device as the parameters and has same dtype
            A_						  =				randn_tensor(
                self.mean.shape						,			generator=UpperCAmelCase						,			device=self.parameters.device						,			dtype=self.parameters.dtype	)
            A_						  =				self.mean + self.std * sample
            return x





      def 		__A	(   self							:				Dict						,			UpperCAmelCase							:				List[Any]=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 		__A	(   self							:				str						,			UpperCAmelCase							:				Tuple						,			UpperCAmelCase							:				Union[str, Any]=[1, 2, 3]	):
            if self.deterministic:
                  return torch.Tensor([0.0]	)
            A_						  =				np.log(2.0 * np.pi	)
            return 0.5 * torch.sum(logtwopi + self.logvar + torch.pow(sample - self.mean						,			2	) / self.var						,			dim=UpperCAmelCase	)







      def 		__A	(   self							:				Union[str, Any]	):
            return self.mean

"""simple docstring"""





from typing import TYPE_CHECKING

from ....utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available


UpperCamelCase_       :    Any						      =			{'''configuration_van''': ['''VAN_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''VanConfig''']}


try:
		if not is_torch_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		UpperCamelCase_       :    str						      =			[
		    '''VAN_PRETRAINED_MODEL_ARCHIVE_LIST''',
		    '''VanForImageClassification''',
		    '''VanModel''',
		    '''VanPreTrainedModel''',
		]

if TYPE_CHECKING:
		from .configuration_van import VAN_PRETRAINED_CONFIG_ARCHIVE_MAP, VanConfig

		try:
				if not is_torch_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_van import (
				    VAN_PRETRAINED_MODEL_ARCHIVE_LIST,
				    VanForImageClassification,
				    VanModel,
				    VanPreTrainedModel,
				)

else:
		import sys

		UpperCamelCase_       :    Optional[Any]						      =			_LazyModule(__name__, globals()['''__file__'''], _import_structure)




