






from typing import List, Optional, Union

import torch

from ...models import UNetaDConditionModel, VQModel
from ...pipelines import DiffusionPipeline
from ...pipelines.pipeline_utils import ImagePipelineOutput
from ...schedulers import DDPMScheduler
from ...utils import (
    is_accelerate_available,
    is_accelerate_version,
    logging,
    randn_tensor,
    replace_example_docstring,
)


__UpperCAmelCase								=						logging.get_logger(__name__)  # pylint: disable=invalid-name

__UpperCAmelCase								=						'\n    Examples:\n        ```py\n        >>> from diffusers import KandinskyV22Pipeline, KandinskyV22PriorPipeline\n        >>> import torch\n\n        >>> pipe_prior = KandinskyV22PriorPipeline.from_pretrained("kandinsky-community/kandinsky-2-2-prior")\n        >>> pipe_prior.to("cuda")\n        >>> prompt = "red cat, 4k photo"\n        >>> out = pipe_prior(prompt)\n        >>> image_emb = out.image_embeds\n        >>> zero_image_emb = out.negative_image_embeds\n        >>> pipe = KandinskyV22Pipeline.from_pretrained("kandinsky-community/kandinsky-2-2-decoder")\n        >>> pipe.to("cuda")\n        >>> image = pipe(\n        ...     image_embeds=image_emb,\n        ...     negative_image_embeds=zero_image_emb,\n        ...     height=768,\n        ...     width=768,\n        ...     num_inference_steps=50,\n        ... ).images\n        >>> image[0].save("cat.png")\n        ```\n'




def      lowercase__      (     __snake_case    :							int     ,       __snake_case    :							List[str]     ,       __snake_case    :							str=8  ):


					'''simple docstring'''


					UpperCAmelCase_    :      List[Any]            =						height // scale_factor**2
					if height % scale_factor**2 != 0:
										new_height += 1
					UpperCAmelCase_    :      List[Any]            =						width // scale_factor**2
					if width % scale_factor**2 != 0:
										new_width += 1
					return new_height * scale_factor, new_width * scale_factor






class   lowerCamelCase      (_snake_case			):







		'''simple docstring'''



		def __init__(							self				,		_UpperCamelCase				,		_UpperCamelCase				,		_UpperCamelCase				,		)	->							Any:
							super().__init__()

							self.register_modules(
							    unet=_UpperCamelCase				,		scheduler=_UpperCamelCase				,		movq=_UpperCamelCase				,		)
							UpperCAmelCase_    :      List[str]            =						2 ** (len(self.movq.config.block_out_channels   ) - 1)



		def 					__UpperCAmelCase			(							self				,		_UpperCamelCase				,		_UpperCamelCase				,		_UpperCamelCase				,		_UpperCamelCase				,		_UpperCamelCase				,		_UpperCamelCase   )	->							int:
							if latents is None:
												UpperCAmelCase_    :      List[Any]            =						randn_tensor(_UpperCamelCase				,		generator=_UpperCamelCase				,		device=_UpperCamelCase				,		dtype=_UpperCamelCase   )
							else:
												if latents.shape != shape:
																	raise ValueError(f"Unexpected latents shape, got {latents.shape}, expected {shape}"   )
												UpperCAmelCase_    :      Optional[int]            =						latents.to(_UpperCamelCase   )

							UpperCAmelCase_    :      str            =						latents * scheduler.init_noise_sigma
							return latents



		def 					__UpperCAmelCase			(							self				,		_UpperCamelCase=0   )	->							int:
							if is_accelerate_available():
												from accelerate import cpu_offload
							else:
												raise ImportError('Please install accelerate via `pip install accelerate`'   )

							UpperCAmelCase_    :      Optional[int]            =						torch.device(f"cuda:{gpu_id}"   )

							UpperCAmelCase_    :      List[str]            =						[
							    self.unet,
							    self.movq,
							]
							for cpu_offloaded_model in models:
												if cpu_offloaded_model is not None:
																	cpu_offload(_UpperCamelCase				,		_UpperCamelCase   )



		def 					__UpperCAmelCase			(							self				,		_UpperCamelCase=0   )	->							List[str]:
							if is_accelerate_available() and is_accelerate_version('>='				,		'0.17.0.dev0'   ):
												from accelerate import cpu_offload_with_hook
							else:
												raise ImportError('`enable_model_cpu_offload` requires `accelerate v0.17.0` or higher.'   )

							UpperCAmelCase_    :      Any            =						torch.device(f"cuda:{gpu_id}"   )

							if self.device.type != "cpu":
												self.to('cpu'				,		silence_dtype_warnings=_UpperCamelCase   )
												torch.cuda.empty_cache()  # otherwise we don't see the memory savings (but they probably exist)

							UpperCAmelCase_    :      Dict            =						None
							for cpu_offloaded_model in [self.unet, self.movq]:
												UpperCAmelCase_       ,  UpperCAmelCase_    :      List[str]            =						cpu_offload_with_hook(_UpperCamelCase				,		_UpperCamelCase				,		prev_module_hook=_UpperCamelCase   )

							# We'll offload the last model manually.
							UpperCAmelCase_    :      Dict            =						hook



		@property
		# Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline._execution_device
		def 					__UpperCAmelCase			(							self   )	->							int:
							if not hasattr(self.unet				,		'_hf_hook'   ):
												return self.device
							for module in self.unet.modules():
												if (
												    hasattr(_UpperCamelCase				,		'_hf_hook'   )
												    and hasattr(module._hf_hook				,		'execution_device'   )
												    and module._hf_hook.execution_device is not None
												):
																	return torch.device(module._hf_hook.execution_device   )
							return self.device



		@torch.no_grad()
		@replace_example_docstring(_UpperCamelCase   )
		def __call__(							self				,		_UpperCamelCase				,		_UpperCamelCase				,		_UpperCamelCase = 5_1_2				,		_UpperCamelCase = 5_1_2				,		_UpperCamelCase = 1_0_0				,		_UpperCamelCase = 4.0				,		_UpperCamelCase = 1				,		_UpperCamelCase = None				,		_UpperCamelCase = None				,		_UpperCamelCase = "pil"				,		_UpperCamelCase = True				,		)	->							Optional[int]:
							UpperCAmelCase_    :      str            =						self._execution_device

							UpperCAmelCase_    :      Union[str, Any]            =						guidance_scale > 1.0

							if isinstance(_UpperCamelCase				,		_UpperCamelCase   ):
												UpperCAmelCase_    :      Optional[Any]            =						torch.cat(_UpperCamelCase				,		dim=0   )
							UpperCAmelCase_    :      Optional[int]            =						image_embeds.shape[0] * num_images_per_prompt
							if isinstance(_UpperCamelCase				,		_UpperCamelCase   ):
												UpperCAmelCase_    :      Tuple            =						torch.cat(_UpperCamelCase				,		dim=0   )

							if do_classifier_free_guidance:
												UpperCAmelCase_    :      Optional[Any]            =						image_embeds.repeat_interleave(_UpperCamelCase				,		dim=0   )
												UpperCAmelCase_    :      Dict            =						negative_image_embeds.repeat_interleave(_UpperCamelCase				,		dim=0   )

							UpperCAmelCase_    :      Union[str, Any]            =						torch.cat([negative_image_embeds, image_embeds]				,		dim=0   ).to(dtype=self.unet.dtype				,		device=_UpperCamelCase   )

							self.scheduler.set_timesteps(_UpperCamelCase				,		device=_UpperCamelCase   )
							UpperCAmelCase_    :      List[Any]            =						self.scheduler.timesteps

							UpperCAmelCase_    :      Any            =						self.unet.config.in_channels

							UpperCAmelCase_       ,  UpperCAmelCase_    :      Dict            =						downscale_height_and_width(_UpperCamelCase				,		_UpperCamelCase				,		self.movq_scale_factor   )

							# create initial latent
							UpperCAmelCase_    :      List[str]            =						self.prepare_latents(
							    (batch_size, num_channels_latents, height, width)				,		image_embeds.dtype				,		_UpperCamelCase				,		_UpperCamelCase				,		_UpperCamelCase				,		self.scheduler				,		)

							for i, t in enumerate(self.progress_bar(_UpperCamelCase   )   ):
												# expand the latents if we are doing classifier free guidance
												UpperCAmelCase_    :      Optional[int]            =						torch.cat([latents] * 2   ) if do_classifier_free_guidance else latents

												UpperCAmelCase_    :      Optional[int]            =						{'image_embeds': image_embeds}
												UpperCAmelCase_    :      Union[str, Any]            =						self.unet(
												    sample=_UpperCamelCase				,		timestep=_UpperCamelCase				,		encoder_hidden_states=_UpperCamelCase				,		added_cond_kwargs=_UpperCamelCase				,		return_dict=_UpperCamelCase				,		)[0]

												if do_classifier_free_guidance:
																	UpperCAmelCase_       ,  UpperCAmelCase_    :      List[str]            =						noise_pred.split(latents.shape[1]				,		dim=1   )
																	UpperCAmelCase_       ,  UpperCAmelCase_    :      List[str]            =						noise_pred.chunk(2   )
																	UpperCAmelCase_       ,  UpperCAmelCase_    :      List[Any]            =						variance_pred.chunk(2   )
																	UpperCAmelCase_    :      Dict            =						noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond)
																	UpperCAmelCase_    :      Tuple            =						torch.cat([noise_pred, variance_pred_text]				,		dim=1   )

												if not (
												    hasattr(self.scheduler.config				,		'variance_type'   )
												    and self.scheduler.config.variance_type in ["learned", "learned_range"]
												):
																	UpperCAmelCase_       ,  UpperCAmelCase_    :      str            =						noise_pred.split(latents.shape[1]				,		dim=1   )

												# compute the previous noisy sample x_t -> x_t-1
												UpperCAmelCase_    :      Dict            =						self.scheduler.step(
												    _UpperCamelCase				,		_UpperCamelCase				,		_UpperCamelCase				,		generator=_UpperCamelCase				,		)[0]
							# post-processing
							UpperCAmelCase_    :      Dict            =						self.movq.decode(_UpperCamelCase				,		force_not_quantize=_UpperCamelCase   )['sample']

							if output_type not in ["pt", "np", "pil"]:
												raise ValueError(f"Only the output types `pt`, `pil` and `np` are supported not output_type={output_type}"   )

							if output_type in ["np", "pil"]:
												UpperCAmelCase_    :      Any            =						image * 0.5 + 0.5
												UpperCAmelCase_    :      Optional[Any]            =						image.clamp(0				,		1   )
												UpperCAmelCase_    :      Tuple            =						image.cpu().permute(0				,		2				,		3				,		1   ).float().numpy()

							if output_type == "pil":
												UpperCAmelCase_    :      Tuple            =						self.numpy_to_pil(_UpperCamelCase   )

							if not return_dict:
												return (image,)

							return ImagePipelineOutput(images=_UpperCamelCase   )














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   lowerCamelCase      (_snake_case			):







		'''simple docstring'''



		_snake_case    :    "DiagonalGaussianDistribution"





class   lowerCamelCase      (_snake_case ,  _snake_case			):







		'''simple docstring'''



		_snake_case    :    Optional[int]       						=  True



		@register_to_config
		def __init__(							self				,		_UpperCamelCase = 3				,		_UpperCamelCase = 3				,		_UpperCamelCase = ("DownEncoderBlock2D",)				,		_UpperCamelCase = ("UpDecoderBlock2D",)				,		_UpperCamelCase = (6_4,)				,		_UpperCamelCase = 1				,		_UpperCamelCase = "silu"				,		_UpperCamelCase = 4				,		_UpperCamelCase = 3_2				,		_UpperCamelCase = 3_2				,		_UpperCamelCase = 0.1_82_15				,		)	->							List[Any]:
							super().__init__()

							# pass init params to Encoder
							UpperCAmelCase_    :      List[str]            =						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_    :      Dict            =						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_    :      Any            =						nn.Convad(2 * latent_channels				,		2 * latent_channels				,		1   )
							UpperCAmelCase_    :      List[Any]            =						nn.Convad(_UpperCamelCase				,		_UpperCamelCase				,		1   )

							UpperCAmelCase_    :      Any            =						False
							UpperCAmelCase_    :      int            =						False

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



		def 					__UpperCAmelCase			(							self				,		_UpperCamelCase				,		_UpperCamelCase=False   )	->							List[str]:
							if isinstance(_UpperCamelCase				,		(Encoder, Decoder)   ):
												UpperCAmelCase_    :      Union[str, Any]            =						value



		def 					__UpperCAmelCase			(							self				,		_UpperCamelCase = True   )	->							int:
							UpperCAmelCase_    :      Tuple            =						use_tiling



		def 					__UpperCAmelCase			(							self   )	->							Dict:
							self.enable_tiling(_UpperCamelCase   )



		def 					__UpperCAmelCase			(							self   )	->							Optional[Any]:
							UpperCAmelCase_    :      str            =						True



		def 					__UpperCAmelCase			(							self   )	->							List[Any]:
							UpperCAmelCase_    :      Optional[int]            =						False



		@property
		# Copied from diffusers.models.unet_2d_condition.UNet2DConditionModel.attn_processors
		def 					__UpperCAmelCase			(							self   )	->							Dict[str, AttentionProcessor]:
							UpperCAmelCase_    :      Optional[int]            =						{}

							def fn_recursive_add_processors(_UpperCamelCase				,		_UpperCamelCase				,		_UpperCamelCase   ):
												if hasattr(_UpperCamelCase				,		'set_processor'   ):
																	UpperCAmelCase_    :      Optional[int]            =						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 					__UpperCAmelCase			(							self				,		_UpperCamelCase   )	->							List[Any]:
							UpperCAmelCase_    :      Union[str, Any]            =						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 					__UpperCAmelCase			(							self   )	->							Union[str, Any]:
							self.set_attn_processor(AttnProcessor()   )



		@apply_forward_hook
		def 					__UpperCAmelCase			(							self				,		_UpperCamelCase				,		_UpperCamelCase = True   )	->							AutoencoderKLOutput:
							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_    :      Union[str, Any]            =						[self.encoder(_UpperCamelCase   ) for x_slice in x.split(1   )]
												UpperCAmelCase_    :      Tuple            =						torch.cat(_UpperCamelCase   )
							else:
												UpperCAmelCase_    :      List[Any]            =						self.encoder(_UpperCamelCase   )

							UpperCAmelCase_    :      Optional[Any]            =						self.quant_conv(_UpperCamelCase   )
							UpperCAmelCase_    :      Tuple            =						DiagonalGaussianDistribution(_UpperCamelCase   )

							if not return_dict:
												return (posterior,)

							return AutoencoderKLOutput(latent_dist=_UpperCamelCase   )



		def 					__UpperCAmelCase			(							self				,		_UpperCamelCase				,		_UpperCamelCase = True   )	->							Union[DecoderOutput, torch.FloatTensor]:
							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_    :      List[str]            =						self.decoder(_UpperCamelCase   )

							if not return_dict:
												return (dec,)

							return DecoderOutput(sample=_UpperCamelCase   )



		@apply_forward_hook
		def 					__UpperCAmelCase			(							self				,		_UpperCamelCase				,		_UpperCamelCase = True   )	->							Union[DecoderOutput, torch.FloatTensor]:
							if self.use_slicing and z.shape[0] > 1:
												UpperCAmelCase_    :      List[str]            =						[self._decode(_UpperCamelCase   ).sample for z_slice in z.split(1   )]
												UpperCAmelCase_    :      Dict            =						torch.cat(_UpperCamelCase   )
							else:
												UpperCAmelCase_    :      Any            =						self._decode(_UpperCamelCase   ).sample

							if not return_dict:
												return (decoded,)

							return DecoderOutput(sample=_UpperCamelCase   )



		def 					__UpperCAmelCase			(							self				,		_UpperCamelCase				,		_UpperCamelCase				,		_UpperCamelCase   )	->							Any:
							UpperCAmelCase_    :      Tuple            =						min(a.shape[2]				,		b.shape[2]				,		_UpperCamelCase   )
							for y in range(_UpperCamelCase   ):
												UpperCAmelCase_    :      str            =						a[:, :, -blend_extent + y, :] * (1 - y / blend_extent) + b[:, :, y, :] * (y / blend_extent)
							return b



		def 					__UpperCAmelCase			(							self				,		_UpperCamelCase				,		_UpperCamelCase				,		_UpperCamelCase   )	->							Dict:
							UpperCAmelCase_    :      Tuple            =						min(a.shape[3]				,		b.shape[3]				,		_UpperCamelCase   )
							for x in range(_UpperCamelCase   ):
												UpperCAmelCase_    :      int            =						a[:, :, :, -blend_extent + x] * (1 - x / blend_extent) + b[:, :, :, x] * (x / blend_extent)
							return b



		def 					__UpperCAmelCase			(							self				,		_UpperCamelCase				,		_UpperCamelCase = True   )	->							AutoencoderKLOutput:
							UpperCAmelCase_    :      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_    :      Optional[int]            =						self.tile_latent_min_size - blend_extent

							# Split the image into 512x512 tiles and encode them separately.
							UpperCAmelCase_    :      List[str]            =						[]
							for i in range(0				,		x.shape[2]				,		_UpperCamelCase   ):
												UpperCAmelCase_    :      Any            =						[]
												for j in range(0				,		x.shape[3]				,		_UpperCamelCase   ):
																	UpperCAmelCase_    :      Any            =						x[:, :, i : i + self.tile_sample_min_size, j : j + self.tile_sample_min_size]
																	UpperCAmelCase_    :      Dict            =						self.encoder(_UpperCamelCase   )
																	UpperCAmelCase_    :      List[str]            =						self.quant_conv(_UpperCamelCase   )
																	row.append(_UpperCamelCase   )
												rows.append(_UpperCamelCase   )
							UpperCAmelCase_    :      str            =						[]
							for i, row in enumerate(_UpperCamelCase   ):
												UpperCAmelCase_    :      List[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_    :      Dict            =						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_    :      Union[str, Any]            =						torch.cat(_UpperCamelCase				,		dim=2   )
							UpperCAmelCase_    :      List[Any]            =						DiagonalGaussianDistribution(_UpperCamelCase   )

							if not return_dict:
												return (posterior,)

							return AutoencoderKLOutput(latent_dist=_UpperCamelCase   )



		def 					__UpperCAmelCase			(							self				,		_UpperCamelCase				,		_UpperCamelCase = True   )	->							Union[DecoderOutput, torch.FloatTensor]:
							UpperCAmelCase_    :      str            =						int(self.tile_latent_min_size * (1 - self.tile_overlap_factor)   )
							UpperCAmelCase_    :      Dict            =						int(self.tile_sample_min_size * self.tile_overlap_factor   )
							UpperCAmelCase_    :      Dict            =						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_    :      Union[str, Any]            =						[]
							for i in range(0				,		z.shape[2]				,		_UpperCamelCase   ):
												UpperCAmelCase_    :      List[str]            =						[]
												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_    :      Optional[Any]            =						self.post_quant_conv(_UpperCamelCase   )
																	UpperCAmelCase_    :      Tuple            =						self.decoder(_UpperCamelCase   )
																	row.append(_UpperCamelCase   )
												rows.append(_UpperCamelCase   )
							UpperCAmelCase_    :      Optional[Any]            =						[]
							for i, row in enumerate(_UpperCamelCase   ):
												UpperCAmelCase_    :      List[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_    :      Union[str, Any]            =						self.blend_v(rows[i - 1][j]				,		_UpperCamelCase				,		_UpperCamelCase   )
																	if j > 0:
																						UpperCAmelCase_    :      Optional[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   )
							if not return_dict:
												return (dec,)

							return DecoderOutput(sample=_UpperCamelCase   )



		def 					__UpperCAmelCase			(							self				,		_UpperCamelCase				,		_UpperCamelCase = False				,		_UpperCamelCase = True				,		_UpperCamelCase = None				,		)	->							Union[DecoderOutput, torch.FloatTensor]:
							UpperCAmelCase_    :      Optional[Any]            =						sample
							UpperCAmelCase_    :      Union[str, Any]            =						self.encode(_UpperCamelCase   ).latent_dist
							if sample_posterior:
												UpperCAmelCase_    :      str            =						posterior.sample(generator=_UpperCamelCase   )
							else:
												UpperCAmelCase_    :      int            =						posterior.mode()
							UpperCAmelCase_    :      Dict            =						self.decode(_UpperCamelCase   ).sample

							if not return_dict:
												return (dec,)

							return DecoderOutput(sample=_UpperCamelCase   )






