


'''simple docstring'''
from dataclasses import dataclass
from typing import Dict, Optional, Tuple, Union

import torch
import torch.nn as nn

from ..configuration_utils import ConfigMixin, register_to_config
from ..utils import BaseOutput, apply_forward_hook
from .attention_processor import AttentionProcessor, AttnProcessor
from .modeling_utils import ModelMixin
from .vae import Decoder, DecoderOutput, DiagonalGaussianDistribution, Encoder
@dataclass
class        lowercase     (							A__ ):

  """simple docstring"""

  _a   			=    42



class        lowercase     (							A__     ,		A__ ):

  """simple docstring"""

  _a   			=    True




  @register_to_config
  def __init__(		self   ,       UpperCamelCase_ = 3   ,       UpperCamelCase_ = 3   ,       UpperCamelCase_ = ("DownEncoderBlock2D",)   ,       UpperCamelCase_ = ("UpDecoderBlock2D",)   ,       UpperCamelCase_ = (64,)   ,       UpperCamelCase_ = 1   ,       UpperCamelCase_ = "silu"   ,       UpperCamelCase_ = 4   ,       UpperCamelCase_ = 32   ,       UpperCamelCase_ = 32   ,       UpperCamelCase_ = 0.18215   ,       ):

       '''simple docstring'''


       super().__init__()

       # pass init params to Encoder
       UpperCamelCase__       :Union[str, Any]							 =			Encoder(
           in_channels=UpperCamelCase_   ,       out_channels=UpperCamelCase_   ,       down_block_types=UpperCamelCase_   ,       block_out_channels=UpperCamelCase_   ,       layers_per_block=UpperCamelCase_   ,       act_fn=UpperCamelCase_   ,       norm_num_groups=UpperCamelCase_   ,       double_z=UpperCamelCase_   ,       )

       # pass init params to Decoder
       UpperCamelCase__       :Tuple							 =			Decoder(
           in_channels=UpperCamelCase_   ,       out_channels=UpperCamelCase_   ,       up_block_types=UpperCamelCase_   ,       block_out_channels=UpperCamelCase_   ,       layers_per_block=UpperCamelCase_   ,       norm_num_groups=UpperCamelCase_   ,       act_fn=UpperCamelCase_   ,       )

       UpperCamelCase__       :Union[str, Any]							 =			nn.Convad(2 * latent_channels   ,       2 * latent_channels   ,       1							)
       UpperCamelCase__       :List[Any]							 =			nn.Convad(UpperCamelCase_   ,       UpperCamelCase_   ,       1							)

       UpperCamelCase__       :Optional[int]							 =			False
       UpperCamelCase__       :List[str]							 =			False

       # only relevant if vae tiling is enabled
       UpperCamelCase__       :Dict							 =			self.config.sample_size
       UpperCamelCase__       :Optional[int]							 =			(
           self.config.sample_size[0]
           if isinstance(self.config.sample_size   ,       (list, tuple)							)
           else self.config.sample_size
       )
       UpperCamelCase__       :Optional[Any]							 =			int(sample_size / (2 ** (len(self.config.block_out_channels							) - 1))							)
       UpperCamelCase__       :int							 =			0.25




  def    lowerCAmelCase__    (		self   ,       UpperCamelCase_   ,       UpperCamelCase_=False							):

       '''simple docstring'''


       if isinstance(UpperCamelCase_   ,       (Encoder, Decoder)							):
            UpperCamelCase__       :Any							 =			value




  def    lowerCAmelCase__    (		self   ,       UpperCamelCase_ = True							):

       '''simple docstring'''


       UpperCamelCase__       :str							 =			use_tiling




  def    lowerCAmelCase__    (		self							):

       '''simple docstring'''


       self.enable_tiling(UpperCamelCase_							)




  def    lowerCAmelCase__    (		self							):

       '''simple docstring'''


       UpperCamelCase__       :int							 =			True




  def    lowerCAmelCase__    (		self							):

       '''simple docstring'''


       UpperCamelCase__       :Any							 =			False




  @property
  # Copied from diffusers.models.unet_2d_condition.UNet2DConditionModel.attn_processors
  def    lowerCAmelCase__    (		self							):

       '''simple docstring'''


       UpperCamelCase__       :Union[str, Any]							 =			{}

       def fn_recursive_add_processors(UpperCamelCase_   ,       UpperCamelCase_   ,       UpperCamelCase_							):
            if hasattr(UpperCamelCase_   ,       '''set_processor'''							):
                 UpperCamelCase__       :Any							 =			module.processor

            for sub_name, child in module.named_children():
                 fn_recursive_add_processors(F'''{name}.{sub_name}'''   ,       UpperCamelCase_   ,       UpperCamelCase_							)

            return processors

       for name, module in self.named_children():
            fn_recursive_add_processors(UpperCamelCase_   ,       UpperCamelCase_   ,       UpperCamelCase_							)

       return processors




  def    lowerCAmelCase__    (		self   ,       UpperCamelCase_							):

       '''simple docstring'''


       UpperCamelCase__       :List[str]							 =			len(self.attn_processors.keys()							)

       if isinstance(UpperCamelCase_   ,       UpperCamelCase_							) and len(UpperCamelCase_							) != count:
            raise ValueError(
                F'''A dict of processors was passed, but the number of processors {len(UpperCamelCase_							)} does not match the'''
                F''' number of attention layers: {count}. Please make sure to pass {count} processor classes.'''							)

       def fn_recursive_attn_processor(UpperCamelCase_   ,       UpperCamelCase_   ,       UpperCamelCase_							):
            if hasattr(UpperCamelCase_   ,       '''set_processor'''							):
                 if not isinstance(UpperCamelCase_   ,       UpperCamelCase_							):
                      module.set_processor(UpperCamelCase_							)
                 else:
                      module.set_processor(processor.pop(F'''{name}.processor'''							)							)

            for sub_name, child in module.named_children():
                 fn_recursive_attn_processor(F'''{name}.{sub_name}'''   ,       UpperCamelCase_   ,       UpperCamelCase_							)

       for name, module in self.named_children():
            fn_recursive_attn_processor(UpperCamelCase_   ,       UpperCamelCase_   ,       UpperCamelCase_							)




  def    lowerCAmelCase__    (		self							):

       '''simple docstring'''


       self.set_attn_processor(AttnProcessor()							)




  @apply_forward_hook
  def    lowerCAmelCase__    (		self   ,       UpperCamelCase_   ,       UpperCamelCase_ = True							):

       '''simple docstring'''


       if self.use_tiling and (x.shape[-1] > self.tile_sample_min_size or x.shape[-2] > self.tile_sample_min_size):
            return self.tiled_encode(UpperCamelCase_   ,       return_dict=UpperCamelCase_							)

       if self.use_slicing and x.shape[0] > 1:
            UpperCamelCase__       :List[str]							 =			[self.encoder(UpperCamelCase_							) for x_slice in x.split(1							)]
            UpperCamelCase__       :Dict							 =			torch.cat(UpperCamelCase_							)
       else:
            UpperCamelCase__       :Optional[int]							 =			self.encoder(UpperCamelCase_							)

       UpperCamelCase__       :Any							 =			self.quant_conv(UpperCamelCase_							)
       UpperCamelCase__       :str							 =			DiagonalGaussianDistribution(UpperCamelCase_							)

       if not return_dict:
            return (posterior,)

       return AutoencoderKLOutput(latent_dist=UpperCamelCase_							)




  def    lowerCAmelCase__    (		self   ,       UpperCamelCase_   ,       UpperCamelCase_ = True							):

       '''simple docstring'''


       if self.use_tiling and (z.shape[-1] > self.tile_latent_min_size or z.shape[-2] > self.tile_latent_min_size):
            return self.tiled_decode(UpperCamelCase_   ,       return_dict=UpperCamelCase_							)

       UpperCamelCase__       :str							 =			self.post_quant_conv(UpperCamelCase_							)
       UpperCamelCase__       :Any							 =			self.decoder(UpperCamelCase_							)

       if not return_dict:
            return (dec,)

       return DecoderOutput(sample=UpperCamelCase_							)




  @apply_forward_hook
  def    lowerCAmelCase__    (		self   ,       UpperCamelCase_   ,       UpperCamelCase_ = True							):

       '''simple docstring'''


       if self.use_slicing and z.shape[0] > 1:
            UpperCamelCase__       :int							 =			[self._decode(UpperCamelCase_							).sample for z_slice in z.split(1							)]
            UpperCamelCase__       :Any							 =			torch.cat(UpperCamelCase_							)
       else:
            UpperCamelCase__       :Tuple							 =			self._decode(UpperCamelCase_							).sample

       if not return_dict:
            return (decoded,)

       return DecoderOutput(sample=UpperCamelCase_							)




  def    lowerCAmelCase__    (		self   ,       UpperCamelCase_   ,       UpperCamelCase_   ,       UpperCamelCase_							):

       '''simple docstring'''


       UpperCamelCase__       :Dict							 =			min(a.shape[2]   ,       b.shape[2]   ,       UpperCamelCase_							)
       for y in range(UpperCamelCase_							):
            UpperCamelCase__       :Optional[Any]							 =			a[:, :, -blend_extent + y, :] * (1 - y / blend_extent) + b[:, :, y, :] * (y / blend_extent)
       return b




  def    lowerCAmelCase__    (		self   ,       UpperCamelCase_   ,       UpperCamelCase_   ,       UpperCamelCase_							):

       '''simple docstring'''


       UpperCamelCase__       :List[str]							 =			min(a.shape[3]   ,       b.shape[3]   ,       UpperCamelCase_							)
       for x in range(UpperCamelCase_							):
            UpperCamelCase__       :Optional[int]							 =			a[:, :, :, -blend_extent + x] * (1 - x / blend_extent) + b[:, :, :, x] * (x / blend_extent)
       return b




  def    lowerCAmelCase__    (		self   ,       UpperCamelCase_   ,       UpperCamelCase_ = True							):

       '''simple docstring'''


       UpperCamelCase__       :Union[str, Any]							 =			int(self.tile_sample_min_size * (1 - self.tile_overlap_factor)							)
       UpperCamelCase__       :Tuple							 =			int(self.tile_latent_min_size * self.tile_overlap_factor							)
       UpperCamelCase__       :List[str]							 =			self.tile_latent_min_size - blend_extent

       # Split the image into 512x512 tiles and encode them separately.
       UpperCamelCase__       :Union[str, Any]							 =			[]
       for i in range(0   ,       x.shape[2]   ,       UpperCamelCase_							):
            UpperCamelCase__       :List[Any]							 =			[]
            for j in range(0   ,       x.shape[3]   ,       UpperCamelCase_							):
                 UpperCamelCase__       :Dict							 =			x[:, :, i : i + self.tile_sample_min_size, j : j + self.tile_sample_min_size]
                 UpperCamelCase__       :str							 =			self.encoder(UpperCamelCase_							)
                 UpperCamelCase__       :Optional[int]							 =			self.quant_conv(UpperCamelCase_							)
                 row.append(UpperCamelCase_							)
            rows.append(UpperCamelCase_							)
       UpperCamelCase__       :int							 =			[]
       for i, row in enumerate(UpperCamelCase_							):
            UpperCamelCase__       :List[str]							 =			[]
            for j, tile in enumerate(UpperCamelCase_							):
                 # blend the above tile and the left tile
                 # to the current tile and add the current tile to the result row
                 if i > 0:
                      UpperCamelCase__       :str							 =			self.blend_v(rows[i - 1][j]   ,       UpperCamelCase_   ,       UpperCamelCase_							)
                 if j > 0:
                      UpperCamelCase__       :Union[str, Any]							 =			self.blend_h(row[j - 1]   ,       UpperCamelCase_   ,       UpperCamelCase_							)
                 result_row.append(tile[:, :, :row_limit, :row_limit]							)
            result_rows.append(torch.cat(UpperCamelCase_   ,       dim=3							)							)

       UpperCamelCase__       :Dict							 =			torch.cat(UpperCamelCase_   ,       dim=2							)
       UpperCamelCase__       :Union[str, Any]							 =			DiagonalGaussianDistribution(UpperCamelCase_							)

       if not return_dict:
            return (posterior,)

       return AutoencoderKLOutput(latent_dist=UpperCamelCase_							)




  def    lowerCAmelCase__    (		self   ,       UpperCamelCase_   ,       UpperCamelCase_ = True							):

       '''simple docstring'''


       UpperCamelCase__       :List[str]							 =			int(self.tile_latent_min_size * (1 - self.tile_overlap_factor)							)
       UpperCamelCase__       :Optional[int]							 =			int(self.tile_sample_min_size * self.tile_overlap_factor							)
       UpperCamelCase__       :str							 =			self.tile_sample_min_size - blend_extent

       # Split z into overlapping 64x64 tiles and decode them separately.
       # The tiles have an overlap to avoid seams between tiles.
       UpperCamelCase__       :str							 =			[]
       for i in range(0   ,       z.shape[2]   ,       UpperCamelCase_							):
            UpperCamelCase__       :Union[str, Any]							 =			[]
            for j in range(0   ,       z.shape[3]   ,       UpperCamelCase_							):
                 UpperCamelCase__       :List[str]							 =			z[:, :, i : i + self.tile_latent_min_size, j : j + self.tile_latent_min_size]
                 UpperCamelCase__       :Tuple							 =			self.post_quant_conv(UpperCamelCase_							)
                 UpperCamelCase__       :Union[str, Any]							 =			self.decoder(UpperCamelCase_							)
                 row.append(UpperCamelCase_							)
            rows.append(UpperCamelCase_							)
       UpperCamelCase__       :Dict							 =			[]
       for i, row in enumerate(UpperCamelCase_							):
            UpperCamelCase__       :Optional[Any]							 =			[]
            for j, tile in enumerate(UpperCamelCase_							):
                 # blend the above tile and the left tile
                 # to the current tile and add the current tile to the result row
                 if i > 0:
                      UpperCamelCase__       :List[Any]							 =			self.blend_v(rows[i - 1][j]   ,       UpperCamelCase_   ,       UpperCamelCase_							)
                 if j > 0:
                      UpperCamelCase__       :List[str]							 =			self.blend_h(row[j - 1]   ,       UpperCamelCase_   ,       UpperCamelCase_							)
                 result_row.append(tile[:, :, :row_limit, :row_limit]							)
            result_rows.append(torch.cat(UpperCamelCase_   ,       dim=3							)							)

       UpperCamelCase__       :Any							 =			torch.cat(UpperCamelCase_   ,       dim=2							)
       if not return_dict:
            return (dec,)

       return DecoderOutput(sample=UpperCamelCase_							)




  def    lowerCAmelCase__    (		self   ,       UpperCamelCase_   ,       UpperCamelCase_ = False   ,       UpperCamelCase_ = True   ,       UpperCamelCase_ = None   ,       ):

       '''simple docstring'''


       UpperCamelCase__       :Union[str, Any]							 =			sample
       UpperCamelCase__       :Tuple							 =			self.encode(UpperCamelCase_							).latent_dist
       if sample_posterior:
            UpperCamelCase__       :List[str]							 =			posterior.sample(generator=UpperCamelCase_							)
       else:
            UpperCamelCase__       :Dict							 =			posterior.mode()
       UpperCamelCase__       :List[str]							 =			self.decode(UpperCamelCase_							).sample

       if not return_dict:
            return (dec,)

       return DecoderOutput(sample=UpperCamelCase_							)



'''simple docstring'''
import socket


def       a				(      )  ->     Dict:



					'''simple docstring'''






					UpperCamelCase__       :int							 =			socket.socket(socket.AF_INET			, socket.SOCK_STREAM       )
					UpperCamelCase__       :List[Any]							 =			socket.gethostname()
					UpperCamelCase__       :List[str]							 =			12312

					sock.connect((host, port)       )
					sock.send(B'''Hello server!'''       )

					with open('''Received_file'''			, '''wb'''       ) as out_file:
										print('''File opened'''       )
										print('''Receiving data...'''       )
										while True:
															UpperCamelCase__       :str							 =			sock.recv(1024       )
															if not data:
																				break
															out_file.write(__a       )

					print('''Successfully received the file'''       )
					sock.close()
					print('''Connection closed'''       )


if __name__ == "__main__":
		main()