


"""simple docstring"""





from dataclasses import dataclass
from typing import Optional, Tuple, Union

import torch
import torch.nn as nn

from ..configuration_utils import ConfigMixin, register_to_config
from ..utils import BaseOutput
from .embeddings import GaussianFourierProjection, TimestepEmbedding, Timesteps
from .modeling_utils import ModelMixin
from .unet_ad_blocks import UNetMidBlockaD, get_down_block, get_up_block



@dataclass
class 		snake_case_(							_lowercase      ):
     __UpperCamelCase	=       42



class 		snake_case_(							_lowercase				,       _lowercase      ):






     @register_to_config
     def __init__(						self	:       Optional[int]       ,					UpperCamelCase_	:       Optional[Union[int, Tuple[int, int]]] = None       ,					UpperCamelCase_	:       int = 3       ,					UpperCamelCase_	:       int = 3       ,					UpperCamelCase_	:       bool = False       ,					UpperCamelCase_	:       str = "positional"       ,					UpperCamelCase_	:       int = 0       ,					UpperCamelCase_	:       bool = True       ,					UpperCamelCase_	:       Tuple[str] = ("DownBlock2D", "AttnDownBlock2D", "AttnDownBlock2D", "AttnDownBlock2D")       ,					UpperCamelCase_	:       Tuple[str] = ("AttnUpBlock2D", "AttnUpBlock2D", "AttnUpBlock2D", "UpBlock2D")       ,					UpperCamelCase_	:       Tuple[int] = (2_2_4, 4_4_8, 6_7_2, 8_9_6)       ,					UpperCamelCase_	:       int = 2       ,					UpperCamelCase_	:       float = 1       ,					UpperCamelCase_	:       int = 1       ,					UpperCamelCase_	:       str = "conv"       ,					UpperCamelCase_	:       str = "conv"       ,					UpperCamelCase_	:       str = "silu"       ,					UpperCamelCase_	:       Optional[int] = 8       ,					UpperCamelCase_	:       int = 3_2       ,					UpperCamelCase_	:       float = 1E-5       ,					UpperCamelCase_	:       str = "default"       ,					UpperCamelCase_	:       bool = True       ,					UpperCamelCase_	:       Optional[str] = None       ,					UpperCamelCase_	:       Optional[int] = None       ,					):
           super().__init__()

           lowerCAmelCase       :						List[Any]							= sample_size
           lowerCAmelCase       :						List[str]							= block_out_channels[0] * 4

           # Check inputs
           if len(_SCREAMING_SNAKE_CASE					) != len(_SCREAMING_SNAKE_CASE					):
                 raise ValueError(
                     F'''Must provide the same number of `down_block_types` as `up_block_types`. `down_block_types`: {down_block_types}. `up_block_types`: {up_block_types}.'''					)

           if len(_SCREAMING_SNAKE_CASE					) != len(_SCREAMING_SNAKE_CASE					):
                 raise ValueError(
                     F'''Must provide the same number of `block_out_channels` as `down_block_types`. `block_out_channels`: {block_out_channels}. `down_block_types`: {down_block_types}.'''					)

           # input
           lowerCAmelCase       :						str							= nn.Convad(_SCREAMING_SNAKE_CASE       ,					block_out_channels[0]       ,					kernel_size=3       ,					padding=(1, 1)					)

           # time
           if time_embedding_type == "fourier":
                 lowerCAmelCase       :						Optional[int]							= GaussianFourierProjection(embedding_size=block_out_channels[0]       ,					scale=1_6					)
                 lowerCAmelCase       :						List[str]							= 2 * block_out_channels[0]
           elif time_embedding_type == "positional":
                 lowerCAmelCase       :						Optional[int]							= Timesteps(block_out_channels[0]       ,					_SCREAMING_SNAKE_CASE       ,					_SCREAMING_SNAKE_CASE					)
                 lowerCAmelCase       :						Tuple							= block_out_channels[0]

           lowerCAmelCase       :						int							= TimestepEmbedding(_SCREAMING_SNAKE_CASE       ,					_SCREAMING_SNAKE_CASE					)

           # class embedding
           if class_embed_type is None and num_class_embeds is not None:
                 lowerCAmelCase       :						Any							= nn.Embedding(_SCREAMING_SNAKE_CASE       ,					_SCREAMING_SNAKE_CASE					)
           elif class_embed_type == "timestep":
                 lowerCAmelCase       :						str							= TimestepEmbedding(_SCREAMING_SNAKE_CASE       ,					_SCREAMING_SNAKE_CASE					)
           elif class_embed_type == "identity":
                 lowerCAmelCase       :						List[Any]							= nn.Identity(_SCREAMING_SNAKE_CASE       ,					_SCREAMING_SNAKE_CASE					)
           else:
                 lowerCAmelCase       :						Dict							= None

           lowerCAmelCase       :						List[Any]							= nn.ModuleList([]					)
           lowerCAmelCase       :						List[Any]							= None
           lowerCAmelCase       :						Optional[Any]							= nn.ModuleList([]					)

           # down
           lowerCAmelCase       :						Tuple							= block_out_channels[0]
           for i, down_block_type in enumerate(_SCREAMING_SNAKE_CASE					):
                 lowerCAmelCase       :						Optional[int]							= output_channel
                 lowerCAmelCase       :						Optional[Any]							= block_out_channels[i]
                 lowerCAmelCase       :						Optional[Any]							= i == len(_SCREAMING_SNAKE_CASE					) - 1

                 lowerCAmelCase       :						Optional[Any]							= get_down_block(
                     _SCREAMING_SNAKE_CASE       ,					num_layers=_SCREAMING_SNAKE_CASE       ,					in_channels=_SCREAMING_SNAKE_CASE       ,					out_channels=_SCREAMING_SNAKE_CASE       ,					temb_channels=_SCREAMING_SNAKE_CASE       ,					add_downsample=not is_final_block       ,					resnet_eps=_SCREAMING_SNAKE_CASE       ,					resnet_act_fn=_SCREAMING_SNAKE_CASE       ,					resnet_groups=_SCREAMING_SNAKE_CASE       ,					attention_head_dim=attention_head_dim if attention_head_dim is not None else output_channel       ,					downsample_padding=_SCREAMING_SNAKE_CASE       ,					resnet_time_scale_shift=_SCREAMING_SNAKE_CASE       ,					downsample_type=_SCREAMING_SNAKE_CASE       ,					)
                 self.down_blocks.append(_SCREAMING_SNAKE_CASE					)

           # mid
           lowerCAmelCase       :						List[str]							= UNetMidBlockaD(
               in_channels=block_out_channels[-1]       ,					temb_channels=_SCREAMING_SNAKE_CASE       ,					resnet_eps=_SCREAMING_SNAKE_CASE       ,					resnet_act_fn=_SCREAMING_SNAKE_CASE       ,					output_scale_factor=_SCREAMING_SNAKE_CASE       ,					resnet_time_scale_shift=_SCREAMING_SNAKE_CASE       ,					attention_head_dim=attention_head_dim if attention_head_dim is not None else block_out_channels[-1]       ,					resnet_groups=_SCREAMING_SNAKE_CASE       ,					add_attention=_SCREAMING_SNAKE_CASE       ,					)

           # up
           lowerCAmelCase       :						Tuple							= list(reversed(_SCREAMING_SNAKE_CASE					)					)
           lowerCAmelCase       :						Any							= reversed_block_out_channels[0]
           for i, up_block_type in enumerate(_SCREAMING_SNAKE_CASE					):
                 lowerCAmelCase       :						Tuple							= output_channel
                 lowerCAmelCase       :						Union[str, Any]							= reversed_block_out_channels[i]
                 lowerCAmelCase       :						Union[str, Any]							= reversed_block_out_channels[min(i + 1       ,					len(_SCREAMING_SNAKE_CASE					) - 1					)]

                 lowerCAmelCase       :						Any							= i == len(_SCREAMING_SNAKE_CASE					) - 1

                 lowerCAmelCase       :						Dict							= get_up_block(
                     _SCREAMING_SNAKE_CASE       ,					num_layers=layers_per_block + 1       ,					in_channels=_SCREAMING_SNAKE_CASE       ,					out_channels=_SCREAMING_SNAKE_CASE       ,					prev_output_channel=_SCREAMING_SNAKE_CASE       ,					temb_channels=_SCREAMING_SNAKE_CASE       ,					add_upsample=not is_final_block       ,					resnet_eps=_SCREAMING_SNAKE_CASE       ,					resnet_act_fn=_SCREAMING_SNAKE_CASE       ,					resnet_groups=_SCREAMING_SNAKE_CASE       ,					attention_head_dim=attention_head_dim if attention_head_dim is not None else output_channel       ,					resnet_time_scale_shift=_SCREAMING_SNAKE_CASE       ,					upsample_type=_SCREAMING_SNAKE_CASE       ,					)
                 self.up_blocks.append(_SCREAMING_SNAKE_CASE					)
                 lowerCAmelCase       :						Union[str, Any]							= output_channel

           # out
           lowerCAmelCase       :						Any							= norm_num_groups if norm_num_groups is not None else min(block_out_channels[0] // 4       ,					3_2					)
           lowerCAmelCase       :						Any							= nn.GroupNorm(num_channels=block_out_channels[0]       ,					num_groups=_SCREAMING_SNAKE_CASE       ,					eps=_SCREAMING_SNAKE_CASE					)
           lowerCAmelCase       :						List[str]							= nn.SiLU()
           lowerCAmelCase       :						Dict							= nn.Convad(block_out_channels[0]       ,					_SCREAMING_SNAKE_CASE       ,					kernel_size=3       ,					padding=1					)






     def   lowerCamelCase__  (						self	:       Tuple       ,					UpperCamelCase_	:       torch.FloatTensor       ,					UpperCamelCase_	:       Union[torch.Tensor, float, int]       ,					UpperCamelCase_	:       Optional[torch.Tensor] = None       ,					UpperCamelCase_	:       bool = True       ,					):
           # 0. center input if necessary
           if self.config.center_input_sample:
                 lowerCAmelCase       :						Tuple							= 2 * sample - 1.0

           # 1. time
           lowerCAmelCase       :						List[str]							= timestep
           if not torch.is_tensor(_SCREAMING_SNAKE_CASE					):
                 lowerCAmelCase       :						List[Any]							= torch.tensor([timesteps]       ,					dtype=torch.long       ,					device=sample.device					)
           elif torch.is_tensor(_SCREAMING_SNAKE_CASE					) and len(timesteps.shape					) == 0:
                 lowerCAmelCase       :						List[str]							= timesteps[None].to(sample.device					)

           # broadcast to batch dimension in a way that's compatible with ONNX/Core ML
           lowerCAmelCase       :						Union[str, Any]							= timesteps * torch.ones(sample.shape[0]       ,					dtype=timesteps.dtype       ,					device=timesteps.device					)

           lowerCAmelCase       :						str							= self.time_proj(_SCREAMING_SNAKE_CASE					)

           # timesteps does not contain any weights and will always return f32 tensors
           # but time_embedding might actually be running in fp16. so we need to cast here.
           # there might be better ways to encapsulate this.
           lowerCAmelCase       :						List[str]							= t_emb.to(dtype=self.dtype					)
           lowerCAmelCase       :						Union[str, Any]							= self.time_embedding(_SCREAMING_SNAKE_CASE					)

           if self.class_embedding is not None:
                 if class_labels is None:
                       raise ValueError('''class_labels should be provided when doing class conditioning'''					)

                 if self.config.class_embed_type == "timestep":
                       lowerCAmelCase       :						Dict							= self.time_proj(_SCREAMING_SNAKE_CASE					)

                 lowerCAmelCase       :						int							= self.class_embedding(_SCREAMING_SNAKE_CASE					).to(dtype=self.dtype					)
                 lowerCAmelCase       :						Optional[Any]							= emb + class_emb

           # 2. pre-process
           lowerCAmelCase       :						List[str]							= sample
           lowerCAmelCase       :						List[str]							= self.conv_in(_SCREAMING_SNAKE_CASE					)

           # 3. down
           lowerCAmelCase       :						Any							= (sample,)
           for downsample_block in self.down_blocks:
                 if hasattr(_SCREAMING_SNAKE_CASE       ,					'''skip_conv'''					):
                       lowerCAmelCase       :						List[str]							= downsample_block(
                           hidden_states=_SCREAMING_SNAKE_CASE       ,					temb=_SCREAMING_SNAKE_CASE       ,					skip_sample=_SCREAMING_SNAKE_CASE					)
                 else:
                       lowerCAmelCase       :						List[Any]							= downsample_block(hidden_states=_SCREAMING_SNAKE_CASE       ,					temb=_SCREAMING_SNAKE_CASE					)

                 down_block_res_samples += res_samples

           # 4. mid
           lowerCAmelCase       :						Union[str, Any]							= self.mid_block(_SCREAMING_SNAKE_CASE       ,					_SCREAMING_SNAKE_CASE					)

           # 5. up
           lowerCAmelCase       :						Optional[int]							= None
           for upsample_block in self.up_blocks:
                 lowerCAmelCase       :						Dict							= down_block_res_samples[-len(upsample_block.resnets					) :]
                 lowerCAmelCase       :						List[Any]							= down_block_res_samples[: -len(upsample_block.resnets					)]

                 if hasattr(_SCREAMING_SNAKE_CASE       ,					'''skip_conv'''					):
                       lowerCAmelCase       :						str							= upsample_block(_SCREAMING_SNAKE_CASE       ,					_SCREAMING_SNAKE_CASE       ,					_SCREAMING_SNAKE_CASE       ,					_SCREAMING_SNAKE_CASE					)
                 else:
                       lowerCAmelCase       :						Dict							= upsample_block(_SCREAMING_SNAKE_CASE       ,					_SCREAMING_SNAKE_CASE       ,					_SCREAMING_SNAKE_CASE					)

        # 6. post-process
           lowerCAmelCase       :						List[str]							= self.conv_norm_out(_SCREAMING_SNAKE_CASE					)
           lowerCAmelCase       :						str							= self.conv_act(_SCREAMING_SNAKE_CASE					)
           lowerCAmelCase       :						Optional[Any]							= self.conv_out(_SCREAMING_SNAKE_CASE					)

           if skip_sample is not None:
                 sample += skip_sample

           if self.config.time_embedding_type == "fourier":
                 lowerCAmelCase       :						int							= timesteps.reshape((sample.shape[0], *([1] * len(sample.shape[1:]					)))					)
                 lowerCAmelCase       :						Optional[Any]							= sample / timesteps

           if not return_dict:
                 return (sample,)

           return UNetaDOutput(sample=_SCREAMING_SNAKE_CASE					)






from typing import TYPE_CHECKING

from ...utils import _LazyModule


lowerCamelCase		=						{'''tokenization_phobert''': ['''PhobertTokenizer''']}


if TYPE_CHECKING:
		from .tokenization_phobert import PhobertTokenizer

else:
		import sys

		lowerCamelCase		=						_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)




