





"""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 		lowercase					(					_UpperCAmelCase					):
      _SCREAMING_SNAKE_CASE					=      42







class 		lowercase					(					nn.Module					):





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

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

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

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

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

            # mid
            lowerCAmelCase  						=       UNetMidBlockaD(
                in_channels=block_out_channels[-1]      ,						resnet_eps=1e-6      ,						resnet_act_fn=lowercase      ,						output_scale_factor=1      ,						resnet_time_scale_shift="""default"""      ,						attention_head_dim=block_out_channels[-1]      ,						resnet_groups=lowercase      ,						temb_channels=lowercase      ,						)

            # out
            lowerCAmelCase  						=       nn.GroupNorm(num_channels=block_out_channels[-1]      ,						num_groups=lowercase      ,						eps=1e-6 )
            lowerCAmelCase  						=       nn.SiLU()

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

            lowerCAmelCase  						=       False





      def     _snake_case			(       self      ,						lowercase )		->     int:
            lowerCAmelCase  						=       x
            lowerCAmelCase  						=       self.conv_in(lowercase )

            if self.training and self.gradient_checkpointing:

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

                        return custom_forward

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

            else:
                  # down
                  for down_block in self.down_blocks:
                        lowerCAmelCase  						=       down_block(lowercase )

                  # middle
                  lowerCAmelCase  						=       self.mid_block(lowercase )

            # post-process
            lowerCAmelCase  						=       self.conv_norm_out(lowercase )
            lowerCAmelCase  						=       self.conv_act(lowercase )
            lowerCAmelCase  						=       self.conv_out(lowercase )

            return sample







class 		lowercase					(					nn.Module					):





      def __init__(       self      ,						lowercase=3      ,						lowercase=3      ,						lowercase=("UpDecoderBlock2D",)      ,						lowercase=(64,)      ,						lowercase=2      ,						lowercase=32      ,						lowercase="silu"      ,						lowercase="group"      ,						)		->     Dict:
            super().__init__()
            lowerCAmelCase  						=       layers_per_block

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

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

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

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

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

                  lowerCAmelCase  						=       i == len(lowercase ) - 1

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

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

            lowerCAmelCase  						=       False





      def     _snake_case			(       self      ,						lowercase      ,						lowercase=None )		->     str:
            lowerCAmelCase  						=       z
            lowerCAmelCase  						=       self.conv_in(lowercase )

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

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

                        return custom_forward

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

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

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

                  # up
                  for up_block in self.up_blocks:
                        lowerCAmelCase  						=       up_block(lowercase      ,						lowercase )

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

            return sample







class 		lowercase					(					nn.Module					):





      def __init__(       self      ,						lowercase      ,						lowercase      ,						lowercase      ,						lowercase=None      ,						lowercase="random"      ,						lowercase=False      ,						lowercase=True )		->     Dict:
            super().__init__()
            lowerCAmelCase  						=       n_e
            lowerCAmelCase  						=       vq_embed_dim
            lowerCAmelCase  						=       beta
            lowerCAmelCase  						=       legacy

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

            lowerCAmelCase  						=       sane_index_shape





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





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





      def     _snake_case			(       self      ,						lowercase )		->     Any:
            # reshape z -> (batch, height, width, channel) and flatten
            lowerCAmelCase  						=       z.permute(0      ,						2      ,						3      ,						1 ).contiguous()
            lowerCAmelCase  						=       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  						=       torch.argmin(torch.cdist(lowercase      ,						self.embedding.weight )      ,						dim=1 )

            lowerCAmelCase  						=       self.embedding(lowercase ).view(z.shape )
            lowerCAmelCase  						=       None
            lowerCAmelCase  						=       None

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

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

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

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

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

            # get quantized latent vectors
            lowerCAmelCase  						=       self.embedding(lowercase )

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

            return z_q







class 		lowercase					(					_UpperCAmelCase					):





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





      def     _snake_case			(       self      ,						lowercase = None )		->     torch.FloatTensor:
            # make sure sample is on the same device as the parameters and has same dtype
            lowerCAmelCase  						=       randn_tensor(
                self.mean.shape      ,						generator=lowercase      ,						device=self.parameters.device      ,						dtype=self.parameters.dtype )
            lowerCAmelCase  						=       self.mean + self.std * sample
            return x





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





      def     _snake_case			(       self )		->     Optional[int]:
            return self.mean













"""simple docstring"""





from dataclasses import dataclass, field
from typing import ClassVar, Dict

from ..features import Features, Value
from .base import TaskTemplate

@dataclass(frozen=_UpperCAmelCase					)
class 		lowercase					(					_UpperCAmelCase					):
      _SCREAMING_SNAKE_CASE					=      field(default='language-modeling'     ,  metadata={'include_in_asdict_even_if_is_default': True}					)

      _SCREAMING_SNAKE_CASE					=      Features({'text': Value('string'					)}					)
      _SCREAMING_SNAKE_CASE					=      Features({}					)
      _SCREAMING_SNAKE_CASE					=      "text"





      @property
      def     _snake_case			(       self )		->     Dict[str, str]:
            return {self.text_column: "text"}






