


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






 """simple docstring"""




 __lowerCAmelCase				      =						42






class    __A       ( _SCREAMING_SNAKE_CASE,					_SCREAMING_SNAKE_CASE    ):






 """simple docstring"""




 @register_to_config
 def __init__(	self				,					__A = None				,					__A = 3				,					__A = 3				,					__A = False				,					__A = "positional"				,					__A = 0				,					__A = True				,					__A = ("DownBlock2D", "AttnDownBlock2D", "AttnDownBlock2D", "AttnDownBlock2D")				,					__A = ("AttnUpBlock2D", "AttnUpBlock2D", "AttnUpBlock2D", "UpBlock2D")				,					__A = (224, 448, 672, 896)				,					__A = 2				,					__A = 1				,					__A = 1				,					__A = "conv"				,					__A = "conv"				,					__A = "silu"				,					__A = 8				,					__A = 32				,					__A = 1E-5				,					__A = "default"				,					__A = True				,					__A = None				,					__A = None				,					)      ->  str:
     super().__init__()

     a					=sample_size
     a					=block_out_channels[0] * 4

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

     # time
     if time_embedding_type == "fourier":
         a					=GaussianFourierProjection(embedding_size=block_out_channels[0]				,					scale=16		)
         a					=2 * block_out_channels[0]
     elif time_embedding_type == "positional":
         a					=Timesteps(block_out_channels[0]				,					__A				,					__A		)
         a					=block_out_channels[0]

     a					=TimestepEmbedding(__A				,					__A		)

     # class embedding
     if class_embed_type is None and num_class_embeds is not None:
         a					=nn.Embedding(__A				,					__A		)
     elif class_embed_type == "timestep":
         a					=TimestepEmbedding(__A				,					__A		)
     elif class_embed_type == "identity":
         a					=nn.Identity(__A				,					__A		)
     else:
         a					=None

     a					=nn.ModuleList([]		)
     a					=None
     a					=nn.ModuleList([]		)

     # down
     a					=block_out_channels[0]
     for i, down_block_type in enumerate(__A		):
         a					=output_channel
         a					=block_out_channels[i]
         a					=i == len(__A		) - 1

         a					=get_down_block(
             __A				,					num_layers=__A				,					in_channels=__A				,					out_channels=__A				,					temb_channels=__A				,					add_downsample=not is_final_block				,					resnet_eps=__A				,					resnet_act_fn=__A				,					resnet_groups=__A				,					attention_head_dim=attention_head_dim if attention_head_dim is not None else output_channel				,					downsample_padding=__A				,					resnet_time_scale_shift=__A				,					downsample_type=__A				,					)
         self.down_blocks.append(__A		)

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

     # up
     a					=list(reversed(__A		)		)
     a					=reversed_block_out_channels[0]
     for i, up_block_type in enumerate(__A		):
         a					=output_channel
         a					=reversed_block_out_channels[i]
         a					=reversed_block_out_channels[min(i + 1				,					len(__A		) - 1		)]

         a					=i == len(__A		) - 1

         a					=get_up_block(
             __A				,					num_layers=layers_per_block + 1				,					in_channels=__A				,					out_channels=__A				,					prev_output_channel=__A				,					temb_channels=__A				,					add_upsample=not is_final_block				,					resnet_eps=__A				,					resnet_act_fn=__A				,					resnet_groups=__A				,					attention_head_dim=attention_head_dim if attention_head_dim is not None else output_channel				,					resnet_time_scale_shift=__A				,					upsample_type=__A				,					)
         self.up_blocks.append(__A		)
         a					=output_channel

     # out
     a					=norm_num_groups if norm_num_groups is not None else min(block_out_channels[0] // 4				,					32		)
     a					=nn.GroupNorm(num_channels=block_out_channels[0]				,					num_groups=__A				,					eps=__A		)
     a					=nn.SiLU()
     a					=nn.Convad(block_out_channels[0]				,					__A				,					kernel_size=3				,					padding=1		)





 def    SCREAMING_SNAKE_CASE							(	self				,					__A				,					__A				,					__A = None				,					__A = True				,					)      ->  Union[UNetaDOutput, Tuple]:
     # 0. center input if necessary
     if self.config.center_input_sample:
         a					=2 * sample - 1.0

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

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

     a					=self.time_proj(__A		)

     # 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.
     a					=t_emb.to(dtype=self.dtype		)
     a					=self.time_embedding(__A		)

     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":
             a					=self.time_proj(__A		)

         a					=self.class_embedding(__A		).to(dtype=self.dtype		)
         a					=emb + class_emb

     # 2. pre-process
     a					=sample
     a					=self.conv_in(__A		)

     # 3. down
     a					=(sample,)
     for downsample_block in self.down_blocks:
         if hasattr(__A				,					'''skip_conv'''		):
             a	,   a	,   a					=downsample_block(
                 hidden_states=__A				,					temb=__A				,					skip_sample=__A		)
         else:
             a	,   a					=downsample_block(hidden_states=__A				,					temb=__A		)

         down_block_res_samples += res_samples

     # 4. mid
     a					=self.mid_block(__A				,					__A		)

     # 5. up
     a					=None
     for upsample_block in self.up_blocks:
         a					=down_block_res_samples[-len(upsample_block.resnets		) :]
         a					=down_block_res_samples[: -len(upsample_block.resnets		)]

         if hasattr(__A				,					'''skip_conv'''		):
             a	,   a					=upsample_block(__A				,					__A				,					__A				,					__A		)
         else:
             a					=upsample_block(__A				,					__A				,					__A		)

        # 6. post-process
     a					=self.conv_norm_out(__A		)
     a					=self.conv_act(__A		)
     a					=self.conv_out(__A		)

     if skip_sample is not None:
         sample += skip_sample

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

     if not return_dict:
         return (sample,)

     return UNetaDOutput(sample=__A		)



"""simple docstring"""







from collections import OrderedDict
from typing import Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


lowerCamelCase_     :     Any    		=						logging.get_logger(__name__)

lowerCamelCase_     :     Tuple    		=						{
    """apple/mobilevitv2-1.0""": """https://huggingface.co/apple/mobilevitv2-1.0/resolve/main/config.json""",
}






class    __A       ( _SCREAMING_SNAKE_CASE    ):






 """simple docstring"""




 __lowerCAmelCase				      =						"mobilevitv2"


 def __init__(	self				,					__A=3				,					__A=256				,					__A=2				,					__A=2.0				,					__A="swish"				,					__A=3				,					__A=32				,					__A=0.1				,					__A=0.02				,					__A=1E-5				,					__A=512				,					__A=[6, 12, 18]				,					__A=0.1				,					__A=255				,					__A=[2, 4, 3]				,					__A=[128, 192, 256]				,					__A=1.0				,					__A=2				,					__A=0.0				,					__A=0.0				,					**__A				,					)      ->  int:
     super().__init__(**__A		)

     a					=num_channels
     a					=image_size
     a					=patch_size
     a					=expand_ratio
     a					=hidden_act
     a					=conv_kernel_size
     a					=output_stride
     a					=initializer_range
     a					=layer_norm_eps
     a					=n_attn_blocks
     a					=base_attn_unit_dims
     a					=width_multiplier
     a					=ffn_multiplier
     a					=ffn_dropout
     a					=attn_dropout
     a					=classifier_dropout_prob

     # decode head attributes for semantic segmentation
     a					=aspp_out_channels
     a					=atrous_rates
     a					=aspp_dropout_prob
     a					=semantic_loss_ignore_index






class    __A       ( _SCREAMING_SNAKE_CASE    ):






 """simple docstring"""




 __lowerCAmelCase				      =						version.parse("1.11"    )


 @property
 def    SCREAMING_SNAKE_CASE							(	self		)      ->  Mapping[str, Mapping[int, str]]:
     return OrderedDict([('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''})]		)


 @property
 def    SCREAMING_SNAKE_CASE							(	self		)      ->  Mapping[str, Mapping[int, str]]:
     if self.task == "image-classification":
         return OrderedDict([('''logits''', {0: '''batch'''})]		)
     else:
         return OrderedDict([('''last_hidden_state''', {0: '''batch'''}), ('''pooler_output''', {0: '''batch'''})]		)





 @property
 def    SCREAMING_SNAKE_CASE							(	self		)      ->  float:
     return 1E-4