






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 							SCREAMING_SNAKE_CASE__		(       UpperCamelCase_		):





					"""simple docstring"""






					a_			=	42



class 							SCREAMING_SNAKE_CASE__		(       nn.Module		):





					"""simple docstring"""






					def __init__(      self  :						str							,	__A  :						Union[str, Any]=3							,	__A  :						List[Any]=3							,	__A  :						Union[str, Any]=("DownEncoderBlock2D",)							,	__A  :						str=(6_4,)							,	__A  :						Optional[int]=2							,	__A  :						Union[str, Any]=3_2							,	__A  :						Any="silu"							,	__A  :						Optional[Any]=True							,	):
						super().__init__()
						snake_case__				:   Optional[Any]     =						layers_per_block

						snake_case__				:   Optional[Any]     =						torch.nn.Convad(
						    __A							,	block_out_channels[0]							,	kernel_size=3							,	stride=1							,	padding=1							,	)

						snake_case__				:   int     =						None
						snake_case__				:   Tuple     =						nn.ModuleList([] )

						# down
						snake_case__				:   str     =						block_out_channels[0]
						for i, down_block_type in enumerate(__A ):
							snake_case__				:   List[Any]     =						output_channel
							snake_case__				:   Union[str, Any]     =						block_out_channels[i]
							snake_case__				:   int     =						i == len(__A ) - 1

							snake_case__				:   str     =						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__				:   List[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__				:   Tuple     =						nn.GroupNorm(num_channels=block_out_channels[-1]							,	num_groups=__A							,	eps=1e-6 )
						snake_case__				:   List[str]     =						nn.SiLU()

						snake_case__				:   Tuple     =						2 * out_channels if double_z else out_channels
						snake_case__				:   Tuple     =						nn.Convad(block_out_channels[-1]							,	__A							,	3							,	padding=1 )

						snake_case__				:   Any     =						False






					def  _lowercase  (      self  :						Dict							,	__A  :						str ):
						snake_case__				:   int     =						x
						snake_case__				:   List[Any]     =						self.conv_in(__A )

						if self.training and self.gradient_checkpointing:

							def create_custom_forward(__A  :						Optional[int] ):
								def custom_forward(*__A  :						Tuple ):
									return module(*__A )

								return custom_forward

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

						else:
							# down
							for down_block in self.down_blocks:
								snake_case__				:   int     =						down_block(__A )

							# middle
							snake_case__				:   Optional[Any]     =						self.mid_block(__A )

						# post-process
						snake_case__				:   Any     =						self.conv_norm_out(__A )
						snake_case__				:   Union[str, Any]     =						self.conv_act(__A )
						snake_case__				:   Union[str, Any]     =						self.conv_out(__A )

						return sample



class 							SCREAMING_SNAKE_CASE__		(       nn.Module		):





					"""simple docstring"""






					def __init__(      self  :						str							,	__A  :						Optional[int]=3							,	__A  :						List[str]=3							,	__A  :						str=("UpDecoderBlock2D",)							,	__A  :						List[str]=(6_4,)							,	__A  :						List[str]=2							,	__A  :						str=3_2							,	__A  :						int="silu"							,	__A  :						Tuple="group"							,	):
						super().__init__()
						snake_case__				:   List[str]     =						layers_per_block

						snake_case__				:   str     =						nn.Convad(
						    __A							,	block_out_channels[-1]							,	kernel_size=3							,	stride=1							,	padding=1							,	)

						snake_case__				:   str     =						None
						snake_case__				:   int     =						nn.ModuleList([] )

						snake_case__				:   str     =						in_channels if norm_type == "spatial" else None

						# mid
						snake_case__				:   Tuple     =						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__				:   Union[str, Any]     =						list(reversed(__A ) )
						snake_case__				:   Dict     =						reversed_block_out_channels[0]
						for i, up_block_type in enumerate(__A ):
							snake_case__				:   str     =						output_channel
							snake_case__				:   int     =						reversed_block_out_channels[i]

							snake_case__				:   List[Any]     =						i == len(__A ) - 1

							snake_case__				:   Tuple     =						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__				:   Optional[Any]     =						output_channel

						# out
						if norm_type == "spatial":
							snake_case__				:   str     =						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__				:   List[Any]     =						nn.SiLU()
						snake_case__				:   List[Any]     =						nn.Convad(block_out_channels[0]							,	__A							,	3							,	padding=1 )

						snake_case__				:   Optional[int]     =						False






					def  _lowercase  (      self  :						str							,	__A  :						Union[str, Any]							,	__A  :						int=None ):
						snake_case__				:   List[str]     =						z
						snake_case__				:   str     =						self.conv_in(__A )

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

							def create_custom_forward(__A  :						Optional[int] ):
								def custom_forward(*__A  :						Tuple ):
									return module(*__A )

								return custom_forward

							if is_torch_version(">="							,	"1.11.0" ):
								# middle
								snake_case__				:   Union[str, Any]     =						torch.utils.checkpoint.checkpoint(
								    create_custom_forward(self.mid_block )							,	__A							,	__A							,	use_reentrant=__A )
								snake_case__				:   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							,	use_reentrant=__A )
							else:
								# middle
								snake_case__				:   str     =						torch.utils.checkpoint.checkpoint(
								    create_custom_forward(self.mid_block )							,	__A							,	__A )
								snake_case__				:   List[Any]     =						sample.to(__A )

								# up
								for up_block in self.up_blocks:
									snake_case__				:   Dict     =						torch.utils.checkpoint.checkpoint(create_custom_forward(__A )							,	__A							,	__A )
						else:
							# middle
							snake_case__				:   Tuple     =						self.mid_block(__A							,	__A )
							snake_case__				:   Optional[int]     =						sample.to(__A )

							# up
							for up_block in self.up_blocks:
								snake_case__				:   Optional[int]     =						up_block(__A							,	__A )

        # post-process
						if latent_embeds is None:
							snake_case__				:   Tuple     =						self.conv_norm_out(__A )
						else:
							snake_case__				:   Optional[Any]     =						self.conv_norm_out(__A							,	__A )
						snake_case__				:   Union[str, Any]     =						self.conv_act(__A )
						snake_case__				:   Any     =						self.conv_out(__A )

						return sample



class 							SCREAMING_SNAKE_CASE__		(       nn.Module		):





					"""simple docstring"""






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

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

						snake_case__				:   Tuple     =						sane_index_shape



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



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



					def  _lowercase  (      self  :						List[str]							,	__A  :						Union[str, Any] ):
						# reshape z -> (batch, height, width, channel) and flatten
						snake_case__				:   int     =						z.permute(0							,	2							,	3							,	1 ).contiguous()
						snake_case__				:   Tuple     =						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__				:   List[str]     =						torch.argmin(torch.cdist(__A							,	self.embedding.weight )							,	dim=1 )

						snake_case__				:   Optional[Any]     =						self.embedding(__A ).view(z.shape )
						snake_case__				:   List[str]     =						None
						snake_case__				:   Union[str, Any]     =						None

						# compute loss for embedding
						if not self.legacy:
							snake_case__				:   List[Any]     =						self.beta * torch.mean((z_q.detach() - z) ** 2 ) + torch.mean((z_q - z.detach()) ** 2 )
						else:
							snake_case__				:   Union[str, Any]     =						torch.mean((z_q.detach() - z) ** 2 ) + self.beta * torch.mean((z_q - z.detach()) ** 2 )

						# preserve gradients
						snake_case__				:   Optional[int]     =						z + (z_q - z).detach()

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

						if self.remap is not None:
							snake_case__				:   str     =						min_encoding_indices.reshape(z.shape[0]							,	-1 )  # add batch axis
							snake_case__				:   Tuple     =						self.remap_to_used(__A )
							snake_case__				:   Optional[int]     =						min_encoding_indices.reshape(-1							,	1 )  # flatten

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

						# get quantized latent vectors
						snake_case__				:   Dict     =						self.embedding(__A )

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

						return z_q



class 							SCREAMING_SNAKE_CASE__		(       UpperCamelCase_		):





					"""simple docstring"""






					def __init__(      self  :						Dict							,	__A  :						List[Any]							,	__A  :						Union[str, Any]=False ):
						snake_case__				:   Dict     =						parameters
						snake_case__,		snake_case__				:   Optional[Any]     =						torch.chunk(__A							,	2							,	dim=1 )
						snake_case__				:   Any     =						torch.clamp(self.logvar							,	-3_0.0							,	2_0.0 )
						snake_case__				:   Optional[int]     =						deterministic
						snake_case__				:   Optional[Any]     =						torch.exp(0.5 * self.logvar )
						snake_case__				:   Optional[Any]     =						torch.exp(self.logvar )
						if self.deterministic:
							snake_case__				:   List[Any]     =						torch.zeros_like(
							    self.mean							,	device=self.parameters.device							,	dtype=self.parameters.dtype )



					def  _lowercase  (      self  :						Tuple							,	__A  :						Optional[torch.Generator] = None ):
						# 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__				:   List[Any]     =						self.mean + self.std * sample
						return x



					def  _lowercase  (      self  :						Dict							,	__A  :						Optional[int]=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  _lowercase  (      self  :						Tuple							,	__A  :						str							,	__A  :						Any=[1, 2, 3] ):
						if self.deterministic:
							return torch.Tensor([0.0] )
						snake_case__				:   Tuple     =						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  _lowercase  (      self  :						List[str] ):
						return self.mean











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

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



@dataclass(frozen=UpperCamelCase_		)
class 							SCREAMING_SNAKE_CASE__		(       UpperCamelCase_		):





					"""simple docstring"""






					a_			=	field(default="text-classification"				,     metadata={"include_in_asdict_even_if_is_default": True}		)
					a_			=	Features({"text": Value("string"		)}		)
					a_			=	Features({"labels": ClassLabel}		)
					a_			=	"text"
					a_			=	"labels"



					def  _lowercase  (      self  :						Tuple							,	__A  :						List[Any] ):
						if self.label_column not in features:
							raise ValueError(f'''Column {self.label_column} is not present in features.''' )
						if not isinstance(features[self.label_column]							,	__A ):
							raise ValueError(f'''Column {self.label_column} is not a ClassLabel.''' )
						snake_case__				:   Any     =						copy.deepcopy(self )
						snake_case__				:   Optional[Any]     =						self.label_schema.copy()
						snake_case__				:   List[str]     =						features[self.label_column]
						snake_case__				:   Dict     =						label_schema
						return task_template






					@property
					def  _lowercase  (      self  :						Tuple ):
						return {
						    self.text_column: "text",
						    self.label_column: "labels",
						}



