





"""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 get_down_block, get_mid_block, get_out_block, get_up_block






@dataclass
class 		__snake_case (		SCREAMING_SNAKE_CASE__					):





 """simple docstring"""


 _lowerCamelCase				=					42






class 		__snake_case (		SCREAMING_SNAKE_CASE__							,							SCREAMING_SNAKE_CASE__					):





 """simple docstring"""


 @register_to_config
 def __init__(   self ,						__lowerCamelCase = 6_5536 ,						__lowerCamelCase = None ,						__lowerCamelCase = 2 ,						__lowerCamelCase = 2 ,						__lowerCamelCase = 0 ,						__lowerCamelCase = "fourier" ,						__lowerCamelCase = True ,						__lowerCamelCase = False ,						__lowerCamelCase = 0.0 ,						__lowerCamelCase = ("DownBlock1DNoSkip", "DownBlock1D", "AttnDownBlock1D") ,						__lowerCamelCase = ("AttnUpBlock1D", "UpBlock1D", "UpBlock1DNoSkip") ,						__lowerCamelCase = "UNetMidBlock1D" ,						__lowerCamelCase = None ,						__lowerCamelCase = (32, 32, 64) ,						__lowerCamelCase = None ,						__lowerCamelCase = 8 ,						__lowerCamelCase = 1 ,						__lowerCamelCase = False ,						):



     '''simple docstring'''




     super().__init__()
     __A   :						Optional[Any]									=					sample_size

     # time
     if time_embedding_type == "fourier":
         __A   :						Dict									=					GaussianFourierProjection(
             embedding_size=8 ,						set_W_to_weight=__lowerCamelCase ,						log=__lowerCamelCase ,						flip_sin_to_cos=__lowerCamelCase  )
         __A   :						Optional[int]									=					2 * block_out_channels[0]
     elif time_embedding_type == "positional":
         __A   :						Tuple									=					Timesteps(
             block_out_channels[0] ,						flip_sin_to_cos=__lowerCamelCase ,						downscale_freq_shift=__lowerCamelCase  )
         __A   :						Dict									=					block_out_channels[0]

     if use_timestep_embedding:
         __A   :						Dict									=					block_out_channels[0] * 4
         __A   :						Union[str, Any]									=					TimestepEmbedding(
             in_channels=__lowerCamelCase ,						time_embed_dim=__lowerCamelCase ,						act_fn=__lowerCamelCase ,						out_dim=block_out_channels[0] ,						)

     __A   :						str									=					nn.ModuleList([]  )
     __A   :						Any									=					None
     __A   :						Dict									=					nn.ModuleList([]  )
     __A   :						int									=					None

     # down
     __A   :						Tuple									=					in_channels
     for i, down_block_type in enumerate(__lowerCamelCase  ):
         __A   :						List[str]									=					output_channel
         __A   :						Tuple									=					block_out_channels[i]

         if i == 0:
             input_channel += extra_in_channels

         __A   :						Tuple									=					i == len(__lowerCamelCase  ) - 1

         __A   :						str									=					get_down_block(
             __lowerCamelCase ,						num_layers=__lowerCamelCase ,						in_channels=__lowerCamelCase ,						out_channels=__lowerCamelCase ,						temb_channels=block_out_channels[0] ,						add_downsample=not is_final_block or downsample_each_block ,						)
         self.down_blocks.append(__lowerCamelCase  )

     # mid
     __A   :						Union[str, Any]									=					get_mid_block(
         __lowerCamelCase ,						in_channels=block_out_channels[-1] ,						mid_channels=block_out_channels[-1] ,						out_channels=block_out_channels[-1] ,						embed_dim=block_out_channels[0] ,						num_layers=__lowerCamelCase ,						add_downsample=__lowerCamelCase ,						)

     # up
     __A   :						Tuple									=					list(reversed(__lowerCamelCase  )  )
     __A   :						Optional[Any]									=					reversed_block_out_channels[0]
     if out_block_type is None:
         __A   :						Dict									=					out_channels
     else:
         __A   :						List[Any]									=					block_out_channels[0]

     for i, up_block_type in enumerate(__lowerCamelCase  ):
         __A   :						Optional[Any]									=					output_channel
         __A   :						str									=					(
             reversed_block_out_channels[i + 1] if i < len(__lowerCamelCase  ) - 1 else final_upsample_channels
         )

         __A   :						int									=					i == len(__lowerCamelCase  ) - 1

         __A   :						Union[str, Any]									=					get_up_block(
             __lowerCamelCase ,						num_layers=__lowerCamelCase ,						in_channels=__lowerCamelCase ,						out_channels=__lowerCamelCase ,						temb_channels=block_out_channels[0] ,						add_upsample=not is_final_block ,						)
         self.up_blocks.append(__lowerCamelCase  )
         __A   :						str									=					output_channel

     # out
     __A   :						Tuple									=					norm_num_groups if norm_num_groups is not None else min(block_out_channels[0] // 4 ,						32  )
     __A   :						int									=					get_out_block(
         out_block_type=__lowerCamelCase ,						num_groups_out=__lowerCamelCase ,						embed_dim=block_out_channels[0] ,						out_channels=__lowerCamelCase ,						act_fn=__lowerCamelCase ,						fc_dim=block_out_channels[-1] // 4 ,						)







 def  UpperCamelCase__(   self ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase = True ,						):



     '''simple docstring'''




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

     __A   :						Tuple									=					self.time_proj(__lowerCamelCase  )
     if self.config.use_timestep_embedding:
         __A   :						str									=					self.time_mlp(__lowerCamelCase  )
     else:
         __A   :						Optional[int]									=					timestep_embed[..., None]
         __A   :						Optional[int]									=					timestep_embed.repeat([1, 1, sample.shape[2]]  ).to(sample.dtype  )
         __A   :						str									=					timestep_embed.broadcast_to((sample.shape[:1] + timestep_embed.shape[1:])  )

     # 2. down
     __A   :						Dict									=					()
     for downsample_block in self.down_blocks:
         __A			,	__A   :						List[str]									=					downsample_block(hidden_states=__lowerCamelCase ,						temb=__lowerCamelCase  )
         down_block_res_samples += res_samples

     # 3. mid
     if self.mid_block:
         __A   :						Union[str, Any]									=					self.mid_block(__lowerCamelCase ,						__lowerCamelCase  )

     # 4. up
     for i, upsample_block in enumerate(self.up_blocks  ):
         __A   :						Any									=					down_block_res_samples[-1:]
         __A   :						int									=					down_block_res_samples[:-1]
         __A   :						str									=					upsample_block(__lowerCamelCase ,						res_hidden_states_tuple=__lowerCamelCase ,						temb=__lowerCamelCase  )

     # 5. post-process
     if self.out_block:
         __A   :						Any									=					self.out_block(__lowerCamelCase ,						__lowerCamelCase  )

     if not return_dict:
         return (sample,)

     return UNetaDOutput(sample=__lowerCamelCase  )











"""simple docstring"""



from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices


a_			=      logging.get_logger(__name__)

a_			=      {
    """shi-labs/dinat-mini-in1k-224""": """https://huggingface.co/shi-labs/dinat-mini-in1k-224/resolve/main/config.json""",
    # See all Dinat models at https://huggingface.co/models?filter=dinat
}






class 		__snake_case (		SCREAMING_SNAKE_CASE__							,							SCREAMING_SNAKE_CASE__					):





 """simple docstring"""


 _lowerCamelCase				=					"""dinat"""

 _lowerCamelCase				=					{
     """num_attention_heads""": """num_heads""",
     """num_hidden_layers""": """num_layers""",
 }

 def __init__(   self ,						__lowerCamelCase=4 ,						__lowerCamelCase=3 ,						__lowerCamelCase=64 ,						__lowerCamelCase=[3, 4, 6, 5] ,						__lowerCamelCase=[2, 4, 8, 16] ,						__lowerCamelCase=7 ,						__lowerCamelCase=[[1, 8, 1], [1, 4, 1, 4], [1, 2, 1, 2, 1, 2], [1, 1, 1, 1, 1]] ,						__lowerCamelCase=3.0 ,						__lowerCamelCase=True ,						__lowerCamelCase=0.0 ,						__lowerCamelCase=0.0 ,						__lowerCamelCase=0.1 ,						__lowerCamelCase="gelu" ,						__lowerCamelCase=0.0_2 ,						__lowerCamelCase=1e-5 ,						__lowerCamelCase=0.0 ,						__lowerCamelCase=None ,						__lowerCamelCase=None ,						**__lowerCamelCase ,						):



     '''simple docstring'''




     super().__init__(**__lowerCamelCase  )

     __A   :						Dict									=					patch_size
     __A   :						Union[str, Any]									=					num_channels
     __A   :						str									=					embed_dim
     __A   :						Optional[Any]									=					depths
     __A   :						int									=					len(__lowerCamelCase  )
     __A   :						Union[str, Any]									=					num_heads
     __A   :						Tuple									=					kernel_size
     __A   :						Optional[int]									=					dilations
     __A   :						Tuple									=					mlp_ratio
     __A   :						Optional[int]									=					qkv_bias
     __A   :						int									=					hidden_dropout_prob
     __A   :						Dict									=					attention_probs_dropout_prob
     __A   :						int									=					drop_path_rate
     __A   :						Dict									=					hidden_act
     __A   :						Any									=					layer_norm_eps
     __A   :						Tuple									=					initializer_range
     # we set the hidden_size attribute in order to make Dinat work with VisionEncoderDecoderModel
     # this indicates the channel dimension after the last stage of the model
     __A   :						str									=					int(embed_dim * 2 ** (len(__lowerCamelCase  ) - 1)  )
     __A   :						List[Any]									=					layer_scale_init_value
     __A   :						Any									=					['''stem'''] + [F"""stage{idx}""" for idx in range(1 ,						len(__lowerCamelCase  ) + 1  )]
     __A			,	__A   :						Union[str, Any]									=					get_aligned_output_features_output_indices(
         out_features=__lowerCamelCase ,						out_indices=__lowerCamelCase ,						stage_names=self.stage_names  )




