




"""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 	a__      (							lowerCamelCase__	):
						snake_case__							   =			42




class 	a__      (							nn.Module	):





						def __init__(				self   :						Tuple  ,a__   :						str=3  ,a__   :						Tuple=3  ,a__   :						List[Any]=("DownEncoderBlock2D",)  ,a__   :						Any=(64,)  ,a__   :						Any=2  ,a__   :						Any=32  ,a__   :						Dict="silu"  ,a__   :						int=True  ,)    ->							str:




											"""simple docstring"""







											super().__init__()
											_lowerCAmelCase:List[Any]						=	layers_per_block

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

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

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

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

											# mid
											_lowerCAmelCase:int						=	UNetMidBlockaD(
											    in_channels=block_out_channels[-1]  ,resnet_eps=1E-6  ,resnet_act_fn=snake_case__  ,output_scale_factor=1  ,resnet_time_scale_shift='''default'''  ,attention_head_dim=block_out_channels[-1]  ,resnet_groups=snake_case__  ,temb_channels=snake_case__  ,)

											# out
											_lowerCAmelCase:Optional[int]						=	nn.GroupNorm(num_channels=block_out_channels[-1]  ,num_groups=snake_case__  ,eps=1E-6)
											_lowerCAmelCase:int						=	nn.SiLU()

											_lowerCAmelCase:List[Any]						=	2 * out_channels if double_z else out_channels
											_lowerCAmelCase:Union[str, Any]						=	nn.Convad(block_out_channels[-1]  ,snake_case__  ,3  ,padding=1)

											_lowerCAmelCase:List[Any]						=	False







						def    __UpperCamelCase  (				self   :						Optional[int]  ,a__   :						Any)    ->							int:




											"""simple docstring"""







											_lowerCAmelCase:List[Any]						=	x
											_lowerCAmelCase:Any						=	self.conv_in(snake_case__)

											if self.training and self.gradient_checkpointing:

																def create_custom_forward(a__   :						Dict):
																					def custom_forward(*a__   :						Optional[int]):
																										return module(*snake_case__)

																					return custom_forward

																# down
																if is_torch_version('''>='''  ,'''1.11.0'''):
																					for down_block in self.down_blocks:
																										_lowerCAmelCase:Tuple						=	torch.utils.checkpoint.checkpoint(
																										    create_custom_forward(snake_case__)  ,snake_case__  ,use_reentrant=snake_case__)
																					# middle
																					_lowerCAmelCase:Optional[int]						=	torch.utils.checkpoint.checkpoint(
																					    create_custom_forward(self.mid_block)  ,snake_case__  ,use_reentrant=snake_case__)
																else:
																					for down_block in self.down_blocks:
																										_lowerCAmelCase:List[str]						=	torch.utils.checkpoint.checkpoint(create_custom_forward(snake_case__)  ,snake_case__)
																					# middle
																					_lowerCAmelCase:Optional[int]						=	torch.utils.checkpoint.checkpoint(create_custom_forward(self.mid_block)  ,snake_case__)

											else:
																# down
																for down_block in self.down_blocks:
																					_lowerCAmelCase:List[str]						=	down_block(snake_case__)

																# middle
																_lowerCAmelCase:Tuple						=	self.mid_block(snake_case__)

											# post-process
											_lowerCAmelCase:int						=	self.conv_norm_out(snake_case__)
											_lowerCAmelCase:Optional[int]						=	self.conv_act(snake_case__)
											_lowerCAmelCase:Tuple						=	self.conv_out(snake_case__)

											return sample




class 	a__      (							nn.Module	):





						def __init__(				self   :						Optional[int]  ,a__   :						Optional[int]=3  ,a__   :						Tuple=3  ,a__   :						Union[str, Any]=("UpDecoderBlock2D",)  ,a__   :						Optional[Any]=(64,)  ,a__   :						List[Any]=2  ,a__   :						Optional[Any]=32  ,a__   :						List[Any]="silu"  ,a__   :						Any="group"  ,)    ->							Dict:




											"""simple docstring"""







											super().__init__()
											_lowerCAmelCase:int						=	layers_per_block

											_lowerCAmelCase:Optional[int]						=	nn.Convad(
											    snake_case__  ,block_out_channels[-1]  ,kernel_size=3  ,stride=1  ,padding=1  ,)

											_lowerCAmelCase:List[str]						=	None
											_lowerCAmelCase:List[str]						=	nn.ModuleList([])

											_lowerCAmelCase:Union[str, Any]						=	in_channels if norm_type == """spatial""" else None

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

											# up
											_lowerCAmelCase:Any						=	list(reversed(snake_case__))
											_lowerCAmelCase:List[Any]						=	reversed_block_out_channels[0]
											for i, up_block_type in enumerate(snake_case__):
																_lowerCAmelCase:Any						=	output_channel
																_lowerCAmelCase:int						=	reversed_block_out_channels[i]

																_lowerCAmelCase:Dict						=	i == len(snake_case__) - 1

																_lowerCAmelCase:Optional[Any]						=	get_up_block(
																    snake_case__  ,num_layers=self.layers_per_block + 1  ,in_channels=snake_case__  ,out_channels=snake_case__  ,prev_output_channel=snake_case__  ,add_upsample=not is_final_block  ,resnet_eps=1E-6  ,resnet_act_fn=snake_case__  ,resnet_groups=snake_case__  ,attention_head_dim=snake_case__  ,temb_channels=snake_case__  ,resnet_time_scale_shift=snake_case__  ,)
																self.up_blocks.append(snake_case__)
																_lowerCAmelCase:Optional[Any]						=	output_channel

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

											_lowerCAmelCase:Dict						=	False







						def    __UpperCamelCase  (				self   :						Optional[Any]  ,a__   :						Optional[Any]  ,a__   :						Tuple=None)    ->							Optional[Any]:




											"""simple docstring"""







											_lowerCAmelCase:Tuple						=	z
											_lowerCAmelCase:str						=	self.conv_in(snake_case__)

											_lowerCAmelCase:Optional[int]						=	next(iter(self.up_blocks.parameters())).dtype
											if self.training and self.gradient_checkpointing:

																def create_custom_forward(a__   :						List[Any]):
																					def custom_forward(*a__   :						List[str]):
																										return module(*snake_case__)

																					return custom_forward

																if is_torch_version('''>='''  ,'''1.11.0'''):
																					# middle
																					_lowerCAmelCase:str						=	torch.utils.checkpoint.checkpoint(
																					    create_custom_forward(self.mid_block)  ,snake_case__  ,snake_case__  ,use_reentrant=snake_case__)
																					_lowerCAmelCase:Optional[Any]						=	sample.to(snake_case__)

																					# up
																					for up_block in self.up_blocks:
																										_lowerCAmelCase:List[str]						=	torch.utils.checkpoint.checkpoint(
																										    create_custom_forward(snake_case__)  ,snake_case__  ,snake_case__  ,use_reentrant=snake_case__)
																else:
																					# middle
																					_lowerCAmelCase:List[Any]						=	torch.utils.checkpoint.checkpoint(
																					    create_custom_forward(self.mid_block)  ,snake_case__  ,snake_case__)
																					_lowerCAmelCase:int						=	sample.to(snake_case__)

																					# up
																					for up_block in self.up_blocks:
																										_lowerCAmelCase:str						=	torch.utils.checkpoint.checkpoint(create_custom_forward(snake_case__)  ,snake_case__  ,snake_case__)
											else:
																# middle
																_lowerCAmelCase:Tuple						=	self.mid_block(snake_case__  ,snake_case__)
																_lowerCAmelCase:Union[str, Any]						=	sample.to(snake_case__)

																# up
																for up_block in self.up_blocks:
																					_lowerCAmelCase:Any						=	up_block(snake_case__  ,snake_case__)

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

											return sample




class 	a__      (							nn.Module	):





						def __init__(				self   :						List[Any]  ,a__   :						Tuple  ,a__   :						Any  ,a__   :						Optional[int]  ,a__   :						List[str]=None  ,a__   :						Tuple="random"  ,a__   :						Tuple=False  ,a__   :						Optional[int]=True)    ->							Dict:




											"""simple docstring"""







											super().__init__()
											_lowerCAmelCase:Optional[int]						=	n_e
											_lowerCAmelCase:int						=	vq_embed_dim
											_lowerCAmelCase:List[Any]						=	beta
											_lowerCAmelCase:Dict						=	legacy

											_lowerCAmelCase: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)

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

											_lowerCAmelCase:List[Any]						=	sane_index_shape





						def    __UpperCamelCase  (				self   :						Optional[int]  ,a__   :						Tuple)    ->							Optional[Any]:




											"""simple docstring"""







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





						def    __UpperCamelCase  (				self   :						Union[str, Any]  ,a__   :						Optional[Any])    ->							Optional[int]:




											"""simple docstring"""







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





						def    __UpperCamelCase  (				self   :						Any  ,a__   :						Tuple)    ->							Optional[Any]:




											"""simple docstring"""







											_lowerCAmelCase:Dict						=	z.permute(0  ,2  ,3  ,1).contiguous()
											_lowerCAmelCase:Union[str, Any]						=	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:Tuple						=	torch.argmin(torch.cdist(snake_case__  ,self.embedding.weight)  ,dim=1)

											_lowerCAmelCase:List[Any]						=	self.embedding(snake_case__).view(z.shape)
											_lowerCAmelCase:Dict						=	None
											_lowerCAmelCase:str						=	None

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

											# preserve gradients
											_lowerCAmelCase:List[Any]						=	z + (z_q - z).detach()

											# reshape back to match original input shape
											_lowerCAmelCase:Optional[Any]						=	z_q.permute(0  ,3  ,1  ,2).contiguous()

											if self.remap is not None:
																_lowerCAmelCase:List[Any]						=	min_encoding_indices.reshape(z.shape[0]  ,-1)  # add batch axis
																_lowerCAmelCase:Dict						=	self.remap_to_used(snake_case__)
																_lowerCAmelCase:Dict						=	min_encoding_indices.reshape(-1  ,1)  # flatten

											if self.sane_index_shape:
																_lowerCAmelCase: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    __UpperCamelCase  (				self   :						int  ,a__   :						List[Any]  ,a__   :						Optional[int])    ->							List[str]:




											"""simple docstring"""







											if self.remap is not None:
																_lowerCAmelCase:Any						=	indices.reshape(shape[0]  ,-1)  # add batch axis
																_lowerCAmelCase:Any						=	self.unmap_to_all(snake_case__)
																_lowerCAmelCase:Optional[int]						=	indices.reshape(-1)  # flatten again

											# get quantized latent vectors
											_lowerCAmelCase:str						=	self.embedding(snake_case__)

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

											return z_q




class 	a__      (							lowerCamelCase__	):





						def __init__(				self   :						Tuple  ,a__   :						Union[str, Any]  ,a__   :						Union[str, Any]=False)    ->							Optional[Any]:




											"""simple docstring"""







											_lowerCAmelCase:Tuple						=	parameters
											_lowerCAmelCase:Union[str, Any]						=	torch.chunk(snake_case__  ,2  ,dim=1)
											_lowerCAmelCase:List[str]						=	torch.clamp(self.logvar  ,-30.0  ,20.0)
											_lowerCAmelCase:int						=	deterministic
											_lowerCAmelCase:Union[str, Any]						=	torch.exp(0.5 * self.logvar)
											_lowerCAmelCase:Dict						=	torch.exp(self.logvar)
											if self.deterministic:
																_lowerCAmelCase:Tuple						=	torch.zeros_like(
																    self.mean  ,device=self.parameters.device  ,dtype=self.parameters.dtype)





						def    __UpperCamelCase  (				self   :						Optional[int]  ,a__   :						Optional[int] = None)    ->							torch.FloatTensor:




											"""simple docstring"""







											_lowerCAmelCase:Tuple						=	randn_tensor(
											    self.mean.shape  ,generator=snake_case__  ,device=self.parameters.device  ,dtype=self.parameters.dtype)
											_lowerCAmelCase:Union[str, Any]						=	self.mean + self.std * sample
											return x





						def    __UpperCamelCase  (				self   :						Union[str, Any]  ,a__   :						int=None)    ->							int:




											"""simple docstring"""







											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    __UpperCamelCase  (				self   :						List[str]  ,a__   :						List[Any]  ,a__   :						List[str]=[1, 2, 3])    ->							str:




											"""simple docstring"""







											if self.deterministic:
																return torch.Tensor([0.0])
											_lowerCAmelCase:List[str]						=	np.log(2.0 * np.pi)
											return 0.5 * torch.sum(logtwopi + self.logvar + torch.pow(sample - self.mean  ,2) / self.var  ,dim=snake_case__)







						def    __UpperCamelCase  (				self   :						List[Any])    ->							Tuple:




											"""simple docstring"""







											return self.mean













def  __magic_name__  (				lowercase				)				-> list[list]:

							"""simple docstring"""



							lowercase_	:   int  				=     current_set.copy()
							for row_index, row in enumerate(lowercase				):
														lowercase_	:   Tuple  				=     row[0]
														for column_index, column in enumerate(lowercase				):
																					if magnitude == 0:
																												lowercase_	:   Optional[int]  				=     column
																												continue
																					lowercase_	:   List[str]  				=     column / magnitude
    # Subtract to cancel term
							lowercase_	:   List[str]  				=     current_set[0]
							lowercase_	:   Optional[int]  				=     [first_row]
							lowercase_	:   Optional[Any]  				=     current_set[1::]
							for row in current_set:
														lowercase_	:   Union[str, Any]  				=     []
														# If first term is 0, it is already in form we want, so we preserve it
														if row[0] == 0:
																					final_set.append(lowercase				)
																					continue
														for column_index in range(len(lowercase				)				):
																					temp_row.append(first_row[column_index] - row[column_index]				)
														final_set.append(lowercase				)
							# Create next recursion iteration set
							if len(final_set[0]				) != 3:
														lowercase_	:   Tuple  				=     final_set[0]
														lowercase_	:   Dict  				=     []
														lowercase_	:   Any  				=     []
														for row in final_set[1::]:
																					current_first_column.append(row[0]				)
																					next_iteration.append(row[1::]				)
														lowercase_	:   Optional[Any]  				=     simplify(lowercase				)
														for i in range(len(lowercase				)				):
																					resultant[i].insert(0				,       current_first_column[i]				)
														resultant.insert(0				,       lowercase				)
														lowercase_	:   Tuple  				=     resultant
							return final_set





def  __magic_name__  (				lowercase				)				-> list:

							"""simple docstring"""



							if len(lowercase				) == 0:
														raise IndexError("""solve_simultaneous() requires n lists of length n+1"""				)
							lowercase_	:   Tuple  				=     len(lowercase				) + 1
							if any(len(lowercase				) != _length for item in equations				):
														raise IndexError("""solve_simultaneous() requires n lists of length n+1"""				)
							for row in equations:
														if any(not isinstance(lowercase				,       (int, float)				) for column in row				):
																					raise ValueError("""solve_simultaneous() requires lists of integers"""				)
							if len(lowercase				) == 1:
														return [equations[0][-1] / equations[0][0]]
							lowercase_	:   List[str]  				=     equations.copy()
							if any(0 in row for row in data_set				):
														lowercase_	:   int  				=     data_set.copy()
														lowercase_	:   Dict  				=     []
														for row_index, row in enumerate(lowercase				):
																					if 0 not in row:
																												lowercase_	:   Dict  				=     data_set.pop(lowercase				)
																												break
														if not full_row:
																					raise ValueError("""solve_simultaneous() requires at least 1 full equation"""				)
														data_set.insert(0				,       lowercase				)
							lowercase_	:   Tuple  				=     data_set.copy()
							lowercase_	:   List[Any]  				=     simplify(lowercase				)
							lowercase_	:   Dict  				=     simplified[::-1]
							lowercase_	:   list  				=     []
							for row in simplified:
														lowercase_	:   Union[str, Any]  				=     row[-1]
														if not solutions:
																					if row[-2] == 0:
																												solutions.append(0				)
																												continue
																					solutions.append(current_solution / row[-2]				)
																					continue
														lowercase_	:   Any  				=     row.copy()[: len(lowercase				) - 1 :]
														while temp_row[0] == 0:
																					temp_row.pop(0				)
														if len(lowercase				) == 0:
																					solutions.append(0				)
																					continue
														lowercase_	:   Any  				=     temp_row[1::]
														lowercase_	:   Union[str, Any]  				=     temp_row[::-1]
														for column_index, column in enumerate(lowercase				):
																					current_solution -= column * solutions[column_index]
														solutions.append(lowercase				)
							lowercase_	:   Any  				=     []
							for item in solutions:
														final.append(float(round(lowercase				,       5				)				)				)
							return final[::-1]


if __name__ == "__main__":
							import doctest

							doctest.testmod()
							UpperCAmelCase_							=				[
							    [2, 1, 1, 1, 1, 4],
							    [1, 2, 1, 1, 1, 5],
							    [1, 1, 2, 1, 1, 6],
							    [1, 1, 1, 2, 1, 7],
							    [1, 1, 1, 1, 2, 8],
							]
							print(solve_simultaneous(eq))
							print(solve_simultaneous([[4, 2]]))