
'''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 						__UpperCAmelCase      (					_lowerCamelCase						):
							__lowercase      		=       42


class 						__UpperCAmelCase      (					_lowerCamelCase						,   _lowerCamelCase						):


							@register_to_config
							def __init__(       self    ,   lowerCAmelCase_ = 6_55_36    ,   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__()
								_snake_case   						=		sample_size

								# time
								if time_embedding_type == "fourier":
									_snake_case   						=		GaussianFourierProjection(
									    embedding_size=8    ,   set_W_to_weight=lowerCAmelCase_    ,   log=lowerCAmelCase_    ,   flip_sin_to_cos=lowerCAmelCase_  )
									_snake_case   						=		2 * block_out_channels[0]
								elif time_embedding_type == "positional":
									_snake_case   						=		Timesteps(
									    block_out_channels[0]    ,   flip_sin_to_cos=lowerCAmelCase_    ,   downscale_freq_shift=lowerCAmelCase_  )
									_snake_case   						=		block_out_channels[0]

								if use_timestep_embedding:
									_snake_case   						=		block_out_channels[0] * 4
									_snake_case   						=		TimestepEmbedding(
									    in_channels=lowerCAmelCase_    ,   time_embed_dim=lowerCAmelCase_    ,   act_fn=lowerCAmelCase_    ,   out_dim=block_out_channels[0]    ,   )

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

								# down
								_snake_case   						=		in_channels
								for i, down_block_type in enumerate(lowerCAmelCase_  ):
									_snake_case   						=		output_channel
									_snake_case   						=		block_out_channels[i]

									if i == 0:
										input_channel += extra_in_channels

									_snake_case   						=		i == len(lowerCAmelCase_  ) - 1

									_snake_case   						=		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
								_snake_case   						=		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
								_snake_case   						=		list(reversed(lowerCAmelCase_  )  )
								_snake_case   						=		reversed_block_out_channels[0]
								if out_block_type is None:
									_snake_case   						=		out_channels
								else:
									_snake_case   						=		block_out_channels[0]

								for i, up_block_type in enumerate(lowerCAmelCase_  ):
									_snake_case   						=		output_channel
									_snake_case   						=		(
									    reversed_block_out_channels[i + 1] if i < len(lowerCAmelCase_  ) - 1 else final_upsample_channels
									)

									_snake_case   						=		i == len(lowerCAmelCase_  ) - 1

									_snake_case   						=		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_  )
									_snake_case   						=		output_channel

								# out
								_snake_case   						=		norm_num_groups if norm_num_groups is not None else min(block_out_channels[0] // 4    ,   32  )
								_snake_case   						=		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  lowerCamelCase				(       self    ,   lowerCAmelCase_    ,   lowerCAmelCase_    ,   lowerCAmelCase_ = True    ,   ):





								"""simple docstring"""






								_snake_case   						=		timestep
								if not torch.is_tensor(lowerCAmelCase_  ):
									_snake_case   						=		torch.tensor([timesteps]    ,   dtype=torch.long    ,   device=sample.device  )
								elif torch.is_tensor(lowerCAmelCase_  ) and len(timesteps.shape  ) == 0:
									_snake_case   						=		timesteps[None].to(sample.device  )

								_snake_case   						=		self.time_proj(lowerCAmelCase_  )
								if self.config.use_timestep_embedding:
									_snake_case   						=		self.time_mlp(lowerCAmelCase_  )
								else:
									_snake_case   						=		timestep_embed[..., None]
									_snake_case   						=		timestep_embed.repeat([1, 1, sample.shape[2]]  ).to(sample.dtype  )
									_snake_case   						=		timestep_embed.broadcast_to((sample.shape[:1] + timestep_embed.shape[1:])  )

								# 2. down
								_snake_case   						=		()
								for downsample_block in self.down_blocks:
									_snake_case						, _snake_case   						=		downsample_block(hidden_states=lowerCAmelCase_    ,   temb=lowerCAmelCase_  )
									down_block_res_samples += res_samples

								# 3. mid
								if self.mid_block:
									_snake_case   						=		self.mid_block(lowerCAmelCase_    ,   lowerCAmelCase_  )

								# 4. up
								for i, upsample_block in enumerate(self.up_blocks  ):
									_snake_case   						=		down_block_res_samples[-1:]
									_snake_case   						=		down_block_res_samples[:-1]
									_snake_case   						=		upsample_block(lowerCAmelCase_    ,   res_hidden_states_tuple=lowerCAmelCase_    ,   temb=lowerCAmelCase_  )

								# 5. post-process
								if self.out_block:
									_snake_case   						=		self.out_block(lowerCAmelCase_    ,   lowerCAmelCase_  )

								if not return_dict:
									return (sample,)

								return UNetaDOutput(sample=lowerCAmelCase_  )



'''simple docstring'''







from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available


lowercase    :							Tuple      =					{
    "configuration_xlm": ["XLM_PRETRAINED_CONFIG_ARCHIVE_MAP", "XLMConfig", "XLMOnnxConfig"],
    "tokenization_xlm": ["XLMTokenizer"],
}

try:
					if not is_torch_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					lowercase    :							int      =					[
					    "XLM_PRETRAINED_MODEL_ARCHIVE_LIST",
					    "XLMForMultipleChoice",
					    "XLMForQuestionAnswering",
					    "XLMForQuestionAnsweringSimple",
					    "XLMForSequenceClassification",
					    "XLMForTokenClassification",
					    "XLMModel",
					    "XLMPreTrainedModel",
					    "XLMWithLMHeadModel",
					]

try:
					if not is_tf_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					lowercase    :							Tuple      =					[
					    "TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST",
					    "TFXLMForMultipleChoice",
					    "TFXLMForQuestionAnsweringSimple",
					    "TFXLMForSequenceClassification",
					    "TFXLMForTokenClassification",
					    "TFXLMMainLayer",
					    "TFXLMModel",
					    "TFXLMPreTrainedModel",
					    "TFXLMWithLMHeadModel",
					]


if TYPE_CHECKING:
					from .configuration_xlm import XLM_PRETRAINED_CONFIG_ARCHIVE_MAP, XLMConfig, XLMOnnxConfig
					from .tokenization_xlm import XLMTokenizer

					try:
										if not is_torch_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .modeling_xlm import (
										    XLM_PRETRAINED_MODEL_ARCHIVE_LIST,
										    XLMForMultipleChoice,
										    XLMForQuestionAnswering,
										    XLMForQuestionAnsweringSimple,
										    XLMForSequenceClassification,
										    XLMForTokenClassification,
										    XLMModel,
										    XLMPreTrainedModel,
										    XLMWithLMHeadModel,
										)

					try:
										if not is_tf_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .modeling_tf_xlm import (
										    TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST,
										    TFXLMForMultipleChoice,
										    TFXLMForQuestionAnsweringSimple,
										    TFXLMForSequenceClassification,
										    TFXLMForTokenClassification,
										    TFXLMMainLayer,
										    TFXLMModel,
										    TFXLMPreTrainedModel,
										    TFXLMWithLMHeadModel,
										)

else:
					import sys

					lowercase    :							Union[str, Any]      =					_LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)

