
'''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 (     _lowerCAmelCase			):
			lowerCamelCase	:						torch.FloatTensor

class 		__lowercase (     nn.Module			):

			def __init__(self	,      A=3	,      A=3	,      A=("DownEncoderBlock2D",)	,      A=(6_4,)	,      A=2	,      A=3_2	,      A="silu"	,      A=True	,      ):
								super().__init__()
								lowerCamelCase_    :					Any   =						layers_per_block

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

								lowerCamelCase_    :					Union[str, Any]   =						None
								lowerCamelCase_    :					Dict   =						nn.ModuleList([]					)

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

													lowerCamelCase_    :					Union[str, Any]   =						get_down_block(
													    _lowerCamelCase	,      num_layers=self.layers_per_block	,      in_channels=_lowerCamelCase	,      out_channels=_lowerCamelCase	,      add_downsample=not is_final_block	,      resnet_eps=1E-6	,      downsample_padding=0	,      resnet_act_fn=_lowerCamelCase	,      resnet_groups=_lowerCamelCase	,      attention_head_dim=_lowerCamelCase	,      temb_channels=_lowerCamelCase	,      )
													self.down_blocks.append(_lowerCamelCase					)

								# mid
								lowerCamelCase_    :					List[Any]   =						UNetMidBlockaD(
								    in_channels=block_out_channels[-1]	,      resnet_eps=1E-6	,      resnet_act_fn=_lowerCamelCase	,      output_scale_factor=1	,      resnet_time_scale_shift='''default'''	,      attention_head_dim=block_out_channels[-1]	,      resnet_groups=_lowerCamelCase	,      temb_channels=_lowerCamelCase	,      )

								# out
								lowerCamelCase_    :					Optional[Any]   =						nn.GroupNorm(num_channels=block_out_channels[-1]	,      num_groups=_lowerCamelCase	,      eps=1E-6					)
								lowerCamelCase_    :					str   =						nn.SiLU()

								lowerCamelCase_    :					Optional[int]   =						2 * out_channels if double_z else out_channels
								lowerCamelCase_    :					Tuple   =						nn.Convad(block_out_channels[-1]	,      _lowerCamelCase	,      3	,      padding=1					)

								lowerCamelCase_    :					Optional[int]   =						False






			def 				UpperCAmelCase__       (self	,      A					):
								lowerCamelCase_    :					Optional[Any]   =						x
								lowerCamelCase_    :					Optional[int]   =						self.conv_in(_lowerCamelCase					)

								if self.training and self.gradient_checkpointing:

													def create_custom_forward(A					):
																		def custom_forward(*A					):
																							return module(*_lowerCamelCase					)

																		return custom_forward

													# down
													if is_torch_version('''>='''	,      '''1.11.0'''					):
																		for down_block in self.down_blocks:
																							lowerCamelCase_    :					Union[str, Any]   =						torch.utils.checkpoint.checkpoint(
																							    create_custom_forward(_lowerCamelCase					)	,      _lowerCamelCase	,      use_reentrant=_lowerCamelCase					)
																		# middle
																		lowerCamelCase_    :					List[Any]   =						torch.utils.checkpoint.checkpoint(
																		    create_custom_forward(self.mid_block					)	,      _lowerCamelCase	,      use_reentrant=_lowerCamelCase					)
													else:
																		for down_block in self.down_blocks:
																							lowerCamelCase_    :					List[Any]   =						torch.utils.checkpoint.checkpoint(create_custom_forward(_lowerCamelCase					)	,      _lowerCamelCase					)
																		# middle
																		lowerCamelCase_    :					int   =						torch.utils.checkpoint.checkpoint(create_custom_forward(self.mid_block					)	,      _lowerCamelCase					)

								else:
													# down
													for down_block in self.down_blocks:
																		lowerCamelCase_    :					int   =						down_block(_lowerCamelCase					)

													# middle
													lowerCamelCase_    :					Tuple   =						self.mid_block(_lowerCamelCase					)

								# post-process
								lowerCamelCase_    :					Optional[Any]   =						self.conv_norm_out(_lowerCamelCase					)
								lowerCamelCase_    :					Union[str, Any]   =						self.conv_act(_lowerCamelCase					)
								lowerCamelCase_    :					Tuple   =						self.conv_out(_lowerCamelCase					)

								return sample

class 		__lowercase (     nn.Module			):

			def __init__(self	,      A=3	,      A=3	,      A=("UpDecoderBlock2D",)	,      A=(6_4,)	,      A=2	,      A=3_2	,      A="silu"	,      A="group"	,      ):
								super().__init__()
								lowerCamelCase_    :					Tuple   =						layers_per_block

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

								lowerCamelCase_    :					int   =						None
								lowerCamelCase_    :					List[Any]   =						nn.ModuleList([]					)

								lowerCamelCase_    :					Tuple   =						in_channels if norm_type == '''spatial''' else None

								# mid
								lowerCamelCase_    :					Union[str, Any]   =						UNetMidBlockaD(
								    in_channels=block_out_channels[-1]	,      resnet_eps=1E-6	,      resnet_act_fn=_lowerCamelCase	,      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=_lowerCamelCase	,      temb_channels=_lowerCamelCase	,      )

								# up
								lowerCamelCase_    :					Union[str, Any]   =						list(reversed(_lowerCamelCase					)					)
								lowerCamelCase_    :					Dict   =						reversed_block_out_channels[0]
								for i, up_block_type in enumerate(_lowerCamelCase					):
													lowerCamelCase_    :					Tuple   =						output_channel
													lowerCamelCase_    :					str   =						reversed_block_out_channels[i]

													lowerCamelCase_    :					Optional[int]   =						i == len(_lowerCamelCase					) - 1

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

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

								lowerCamelCase_    :					Optional[Any]   =						False






			def 				UpperCAmelCase__       (self	,      A	,      A=None					):
								lowerCamelCase_    :					Optional[int]   =						z
								lowerCamelCase_    :					Dict   =						self.conv_in(_lowerCamelCase					)

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

													def create_custom_forward(A					):
																		def custom_forward(*A					):
																							return module(*_lowerCamelCase					)

																		return custom_forward

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

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

																		# up
																		for up_block in self.up_blocks:
																							lowerCamelCase_    :					Optional[Any]   =						torch.utils.checkpoint.checkpoint(create_custom_forward(_lowerCamelCase					)	,      _lowerCamelCase	,      _lowerCamelCase					)
								else:
													# middle
													lowerCamelCase_    :					List[str]   =						self.mid_block(_lowerCamelCase	,      _lowerCamelCase					)
													lowerCamelCase_    :					List[Any]   =						sample.to(_lowerCamelCase					)

													# up
													for up_block in self.up_blocks:
																		lowerCamelCase_    :					Tuple   =						up_block(_lowerCamelCase	,      _lowerCamelCase					)

													 # post-process
								if latent_embeds is None:
													lowerCamelCase_    :					str   =						self.conv_norm_out(_lowerCamelCase					)
								else:
													lowerCamelCase_    :					Dict   =						self.conv_norm_out(_lowerCamelCase	,      _lowerCamelCase					)
								lowerCamelCase_    :					str   =						self.conv_act(_lowerCamelCase					)
								lowerCamelCase_    :					List[str]   =						self.conv_out(_lowerCamelCase					)

								return sample

class 		__lowercase (     nn.Module			):

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

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

								lowerCamelCase_    :					List[str]   =						sane_index_shape

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

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

			def 				UpperCAmelCase__       (self	,      A					):
								lowerCamelCase_    :					Any   =						z.permute(0	,      2	,      3	,      1					).contiguous()
								lowerCamelCase_    :					Optional[int]   =						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_    :					List[str]   =						torch.argmin(torch.cdist(_lowerCamelCase	,      self.embedding.weight					)	,      dim=1					)

								lowerCamelCase_    :					Optional[int]   =						self.embedding(_lowerCamelCase					).view(z.shape					)
								lowerCamelCase_    :					Union[str, Any]   =						None
								lowerCamelCase_    :					List[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_    :					List[Any]   =						torch.mean((z_q.detach() - z) ** 2					) + self.beta * torch.mean((z_q - z.detach()) ** 2					)

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

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

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

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

								# get quantized latent vectors
								lowerCamelCase_    :					Dict   =						self.embedding(_lowerCamelCase					)

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

								return z_q

class 		__lowercase (     _lowerCAmelCase			):

			def __init__(self	,      A	,      A=False					):
								lowerCamelCase_    :					Dict   =						parameters
								lowerCamelCase_,					lowerCamelCase_    :					List[Any]   =						torch.chunk(_lowerCamelCase	,      2	,      dim=1					)
								lowerCamelCase_    :					Optional[int]   =						torch.clamp(self.logvar	,      -30.0	,      20.0					)
								lowerCamelCase_    :					Optional[Any]   =						deterministic
								lowerCamelCase_    :					int   =						torch.exp(0.5 * self.logvar					)
								lowerCamelCase_    :					int   =						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	,      A = None					):
								lowerCamelCase_    :					List[str]   =						randn_tensor(
								    self.mean.shape	,      generator=_lowerCamelCase	,      device=self.parameters.device	,      dtype=self.parameters.dtype					)
								lowerCamelCase_    :					Tuple   =						self.mean + self.std * sample
								return x

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






			def 				UpperCAmelCase__       (self					):
								return self.mean










"""simple docstring"""


from collections import defaultdict
from math import ceil, sqrt



def 					lowerCAmelCase				(    __UpperCamelCase = 1_0_0_0_0_0_0		,       __UpperCamelCase = 1_0 ):




  """simple docstring"""







  __A									= defaultdict(__UpperCamelCase )

  for outer_width in range(3		,       (t_limit // 4) + 2 ):
    if outer_width * outer_width > t_limit:
      __A									= max(
          ceil(sqrt(outer_width * outer_width - t_limit ) )		,       1 )
    else:
      __A									= 1

    hole_width_lower_bound += (outer_width - hole_width_lower_bound) % 2

    for hole_width in range(__UpperCamelCase		,       outer_width - 1		,       2 ):
      count[outer_width * outer_width - hole_width * hole_width] += 1

  return sum(1 for n in count.values() if 1 <= n <= 1_0 )


if __name__ == "__main__":
       print(F'''{solution() = }''')




