



"""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 			_a     (   SCREAMING_SNAKE_CASE__):
			__magic_name__    				=    42



class 			_a     (   SCREAMING_SNAKE_CASE__    ,  SCREAMING_SNAKE_CASE__):



			@register_to_config
			def __init__( self       :							str      ,  _lowercase       :							Optional[Union[int, Tuple[int, int]]] = None      ,  _lowercase       :							int = 3      ,  _lowercase       :							int = 3      ,  _lowercase       :							bool = False      ,  _lowercase       :							str = "positional"      ,  _lowercase       :							int = 0      ,  _lowercase       :							bool = True      ,  _lowercase       :							Tuple[str] = ("DownBlock2D", "AttnDownBlock2D", "AttnDownBlock2D", "AttnDownBlock2D")      ,  _lowercase       :							Tuple[str] = ("AttnUpBlock2D", "AttnUpBlock2D", "AttnUpBlock2D", "UpBlock2D")      ,  _lowercase       :							Tuple[int] = (224, 448, 672, 896)      ,  _lowercase       :							int = 2      ,  _lowercase       :							float = 1      ,  _lowercase       :							int = 1      ,  _lowercase       :							str = "conv"      ,  _lowercase       :							str = "conv"      ,  _lowercase       :							str = "silu"      ,  _lowercase       :							Optional[int] = 8      ,  _lowercase       :							int = 32      ,  _lowercase       :							float = 1E-5      ,  _lowercase       :							str = "default"      ,  _lowercase       :							bool = True      ,  _lowercase       :							Optional[str] = None      ,  _lowercase       :							Optional[int] = None      ,  )       ->				int:
									super().__init__()

									snake_case      : Optional[int]							=   sample_size
									snake_case      : List[Any]							=   block_out_channels[0] * 4

									# Check inputs
									if len(_lowercase		) != len(_lowercase		):
															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(_lowercase		) != len(_lowercase		):
															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
									snake_case      : Optional[Any]							=   nn.Convad(_lowercase      ,  block_out_channels[0]      ,  kernel_size=3      ,  padding=(1, 1)		)

									# time
									if time_embedding_type == "fourier":
															snake_case      : Union[str, Any]							=   GaussianFourierProjection(embedding_size=block_out_channels[0]      ,  scale=16		)
															snake_case      : List[str]							=   2 * block_out_channels[0]
									elif time_embedding_type == "positional":
															snake_case      : str							=   Timesteps(block_out_channels[0]      ,  _lowercase      ,  _lowercase		)
															snake_case      : Union[str, Any]							=   block_out_channels[0]

									snake_case      : str							=   TimestepEmbedding(_lowercase      ,  _lowercase		)

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

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

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

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

									# mid
									snake_case      : Any							=   UNetMidBlockaD(
									    in_channels=block_out_channels[-1]      ,  temb_channels=_lowercase      ,  resnet_eps=_lowercase      ,  resnet_act_fn=_lowercase      ,  output_scale_factor=_lowercase      ,  resnet_time_scale_shift=_lowercase      ,  attention_head_dim=attention_head_dim if attention_head_dim is not None else block_out_channels[-1]      ,  resnet_groups=_lowercase      ,  add_attention=_lowercase      ,  )

									# up
									snake_case      : Union[str, Any]							=   list(reversed(_lowercase		)		)
									snake_case      : Optional[int]							=   reversed_block_out_channels[0]
									for i, up_block_type in enumerate(_lowercase		):
															snake_case      : List[str]							=   output_channel
															snake_case      : Union[str, Any]							=   reversed_block_out_channels[i]
															snake_case      : Any							=   reversed_block_out_channels[min(i + 1      ,  len(_lowercase		) - 1		)]

															snake_case      : Dict							=   i == len(_lowercase		) - 1

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

									# out
									snake_case      : int							=   norm_num_groups if norm_num_groups is not None else min(block_out_channels[0] // 4      ,  32		)
									snake_case      : str							=   nn.GroupNorm(num_channels=block_out_channels[0]      ,  num_groups=_lowercase      ,  eps=_lowercase		)
									snake_case      : Tuple							=   nn.SiLU()
									snake_case      : List[Any]							=   nn.Convad(block_out_channels[0]      ,  _lowercase      ,  kernel_size=3      ,  padding=1		)



			def 							__lowercase				( self       :							Dict      ,  _lowercase       :							torch.FloatTensor      ,  _lowercase       :							Union[torch.Tensor, float, int]      ,  _lowercase       :							Optional[torch.Tensor] = None      ,  _lowercase       :							bool = True      ,  )       ->				Union[UNetaDOutput, Tuple]:
									# 0. center input if necessary
									if self.config.center_input_sample:
															snake_case      : Any							=   2 * sample - 1.0

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

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

									snake_case      : List[Any]							=   self.time_proj(_lowercase		)

									# 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.
									snake_case      : Dict							=   t_emb.to(dtype=self.dtype		)
									snake_case      : Any							=   self.time_embedding(_lowercase		)

									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":
																					snake_case      : str							=   self.time_proj(_lowercase		)

															snake_case      : Optional[Any]							=   self.class_embedding(_lowercase		).to(dtype=self.dtype		)
															snake_case      : Tuple							=   emb + class_emb

									# 2. pre-process
									snake_case      : int							=   sample
									snake_case      : List[Any]							=   self.conv_in(_lowercase		)

									# 3. down
									snake_case      : str							=   (sample,)
									for downsample_block in self.down_blocks:
															if hasattr(_lowercase      ,  "skip_conv"		):
																					snake_case						, snake_case						, snake_case      : Tuple							=   downsample_block(
																					    hidden_states=_lowercase      ,  temb=_lowercase      ,  skip_sample=_lowercase		)
															else:
																					snake_case						, snake_case      : Optional[int]							=   downsample_block(hidden_states=_lowercase      ,  temb=_lowercase		)

															down_block_res_samples += res_samples

									# 4. mid
									snake_case      : Optional[Any]							=   self.mid_block(_lowercase      ,  _lowercase		)

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

															if hasattr(_lowercase      ,  "skip_conv"		):
																					snake_case						, snake_case      : Tuple							=   upsample_block(_lowercase      ,  _lowercase      ,  _lowercase      ,  _lowercase		)
															else:
																					snake_case      : List[Any]							=   upsample_block(_lowercase      ,  _lowercase      ,  _lowercase		)

        # 6. post-process
									snake_case      : Dict							=   self.conv_norm_out(_lowercase		)
									snake_case      : List[Any]							=   self.conv_act(_lowercase		)
									snake_case      : int							=   self.conv_out(_lowercase		)

									if skip_sample is not None:
															sample += skip_sample

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

									if not return_dict:
															return (sample,)

									return UNetaDOutput(sample=_lowercase		)








"""simple docstring"""







from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_torch_available,
    is_vision_available,
)


A    =	{
    'configuration_mobilevitv2': [
        'MOBILEVITV2_PRETRAINED_CONFIG_ARCHIVE_MAP',
        'MobileViTV2Config',
        'MobileViTV2OnnxConfig',
    ],
}


try:
			if not is_torch_available():
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			pass
else:
			A    =	[
			    'MOBILEVITV2_PRETRAINED_MODEL_ARCHIVE_LIST',
			    'MobileViTV2ForImageClassification',
			    'MobileViTV2ForSemanticSegmentation',
			    'MobileViTV2Model',
			    'MobileViTV2PreTrainedModel',
			]

if TYPE_CHECKING:
			from .configuration_mobilevitva import (
			    MOBILEVITV2_PRETRAINED_CONFIG_ARCHIVE_MAP,
			    MobileViTVaConfig,
			    MobileViTVaOnnxConfig,
			)

			try:
						if not is_torch_available():
									raise OptionalDependencyNotAvailable()
			except OptionalDependencyNotAvailable:
						pass
			else:
						from .modeling_mobilevitva import (
						    MOBILEVITV2_PRETRAINED_MODEL_ARCHIVE_LIST,
						    MobileViTVaForImageClassification,
						    MobileViTVaForSemanticSegmentation,
						    MobileViTVaModel,
						    MobileViTVaPreTrainedModel,
						)

else:
			import sys

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



