from typing import Any, Dict, Optional

import torch
import torch.nn.functional as F
from torch import nn

from ..utils import maybe_allow_in_graph
from .activations import get_activation
from .attention_processor import Attention
from .embeddings import CombinedTimestepLabelEmbeddings





@maybe_allow_in_graph
class 					lowercase__						(   nn.Module):


						def __init__(       self	:				Tuple    ,		UpperCamelCase__	:				int    ,		UpperCamelCase__	:				int    ,		UpperCamelCase__	:				int    ,		UpperCamelCase__	:				Any=0.0    ,		UpperCamelCase__	:				Optional[int] = None    ,		UpperCamelCase__	:				str = "geglu"    ,		UpperCamelCase__	:				Optional[int] = None    ,		UpperCamelCase__	:				bool = False    ,		UpperCamelCase__	:				bool = False    ,		UpperCamelCase__	:				bool = False    ,		UpperCamelCase__	:				bool = False    ,		UpperCamelCase__	:				bool = True    ,		UpperCamelCase__	:				str = "layer_norm"    ,		UpperCamelCase__	:				bool = False    ,		):





												'''simple docstring'''





												super().__init__()
												SCREAMING_SNAKE_CASE							:    Optional[Any]							=							only_cross_attention

												SCREAMING_SNAKE_CASE							:    Optional[Any]							=							(num_embeds_ada_norm is not None) and norm_type == '''ada_norm_zero'''
												SCREAMING_SNAKE_CASE							:    Optional[Any]							=							(num_embeds_ada_norm is not None) and norm_type == '''ada_norm'''

												if norm_type in ("ada_norm", "ada_norm_zero") and num_embeds_ada_norm is None:
																		raise ValueError(
																		    f"""`norm_type` is set to {norm_type}, but `num_embeds_ada_norm` is not defined. Please make sure to"""
																		    f""" define `num_embeds_ada_norm` if setting `norm_type` to {norm_type}."""						)

												# Define 3 blocks. Each block has its own normalization layer.
												# 1. Self-Attn
												if self.use_ada_layer_norm:
																		SCREAMING_SNAKE_CASE							:    Optional[Any]							=							AdaLayerNorm(UpperCamelCase__    ,		UpperCamelCase__						)
												elif self.use_ada_layer_norm_zero:
																		SCREAMING_SNAKE_CASE							:    Optional[Any]							=							AdaLayerNormZero(UpperCamelCase__    ,		UpperCamelCase__						)
												else:
																		SCREAMING_SNAKE_CASE							:    Union[str, Any]							=							nn.LayerNorm(UpperCamelCase__    ,		elementwise_affine=UpperCamelCase__						)
												SCREAMING_SNAKE_CASE							:    Union[str, Any]							=							Attention(
												    query_dim=UpperCamelCase__    ,		heads=UpperCamelCase__    ,		dim_head=UpperCamelCase__    ,		dropout=UpperCamelCase__    ,		bias=UpperCamelCase__    ,		cross_attention_dim=cross_attention_dim if only_cross_attention else None    ,		upcast_attention=UpperCamelCase__    ,		)

												# 2. Cross-Attn
												if cross_attention_dim is not None or double_self_attention:
																		# We currently only use AdaLayerNormZero for self attention where there will only be one attention block.
																		# I.e. the number of returned modulation chunks from AdaLayerZero would not make sense if returned during
																		# the second cross attention block.
																		SCREAMING_SNAKE_CASE							:    Optional[Any]							=							(
																		    AdaLayerNorm(UpperCamelCase__    ,		UpperCamelCase__						)
																		    if self.use_ada_layer_norm
																		    else nn.LayerNorm(UpperCamelCase__    ,		elementwise_affine=UpperCamelCase__						)
																		)
																		SCREAMING_SNAKE_CASE							:    str							=							Attention(
																		    query_dim=UpperCamelCase__    ,		cross_attention_dim=cross_attention_dim if not double_self_attention else None    ,		heads=UpperCamelCase__    ,		dim_head=UpperCamelCase__    ,		dropout=UpperCamelCase__    ,		bias=UpperCamelCase__    ,		upcast_attention=UpperCamelCase__    ,		)  # is self-attn if encoder_hidden_states is none
												else:
																		SCREAMING_SNAKE_CASE							:    Dict							=							None
																		SCREAMING_SNAKE_CASE							:    Optional[int]							=							None

												# 3. Feed-forward
												SCREAMING_SNAKE_CASE							:    List[str]							=							nn.LayerNorm(UpperCamelCase__    ,		elementwise_affine=UpperCamelCase__						)
												SCREAMING_SNAKE_CASE							:    Any							=							FeedForward(UpperCamelCase__    ,		dropout=UpperCamelCase__    ,		activation_fn=UpperCamelCase__    ,		final_dropout=UpperCamelCase__						)

												# let chunk size default to None
												SCREAMING_SNAKE_CASE							:    Any							=							None
												SCREAMING_SNAKE_CASE							:    str							=							0


						def        __A       (       self	:				Dict    ,		UpperCamelCase__	:				Optional[int]    ,		UpperCamelCase__	:				int						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    Tuple							=							chunk_size
												SCREAMING_SNAKE_CASE							:    str							=							dim



						def        __A       (       self	:				Optional[int]    ,		UpperCamelCase__	:				torch.FloatTensor    ,		UpperCamelCase__	:				Optional[torch.FloatTensor] = None    ,		UpperCamelCase__	:				Optional[torch.FloatTensor] = None    ,		UpperCamelCase__	:				Optional[torch.FloatTensor] = None    ,		UpperCamelCase__	:				Optional[torch.LongTensor] = None    ,		UpperCamelCase__	:				Dict[str, Any] = None    ,		UpperCamelCase__	:				Optional[torch.LongTensor] = None    ,		):





												'''simple docstring'''





												if self.use_ada_layer_norm:
																		SCREAMING_SNAKE_CASE							:    List[str]							=							self.norma(UpperCamelCase__    ,		UpperCamelCase__						)
												elif self.use_ada_layer_norm_zero:
																		SCREAMING_SNAKE_CASE				,  SCREAMING_SNAKE_CASE				,  SCREAMING_SNAKE_CASE				,  SCREAMING_SNAKE_CASE				,  SCREAMING_SNAKE_CASE							:    List[Any]							=							self.norma(
																		    UpperCamelCase__    ,		UpperCamelCase__    ,		UpperCamelCase__    ,		hidden_dtype=hidden_states.dtype						)
												else:
																		SCREAMING_SNAKE_CASE							:    Optional[int]							=							self.norma(UpperCamelCase__						)

												SCREAMING_SNAKE_CASE							:    Any							=							cross_attention_kwargs if cross_attention_kwargs is not None else {}

												SCREAMING_SNAKE_CASE							:    Union[str, Any]							=							self.attna(
												    UpperCamelCase__    ,		encoder_hidden_states=encoder_hidden_states if self.only_cross_attention else None    ,		attention_mask=UpperCamelCase__    ,		**UpperCamelCase__    ,		)
												if self.use_ada_layer_norm_zero:
																		SCREAMING_SNAKE_CASE							:    Union[str, Any]							=							gate_msa.unsqueeze(1						) * attn_output
												SCREAMING_SNAKE_CASE							:    Optional[Any]							=							attn_output + hidden_states

												# 2. Cross-Attention
												if self.attna is not None:
																		SCREAMING_SNAKE_CASE							:    List[Any]							=							(
																		    self.norma(UpperCamelCase__    ,		UpperCamelCase__						) if self.use_ada_layer_norm else self.norma(UpperCamelCase__						)
																		)

																		SCREAMING_SNAKE_CASE							:    Optional[Any]							=							self.attna(
																		    UpperCamelCase__    ,		encoder_hidden_states=UpperCamelCase__    ,		attention_mask=UpperCamelCase__    ,		**UpperCamelCase__    ,		)
																		SCREAMING_SNAKE_CASE							:    List[Any]							=							attn_output + hidden_states

												# 3. Feed-forward
												SCREAMING_SNAKE_CASE							:    Optional[int]							=							self.norma(UpperCamelCase__						)

												if self.use_ada_layer_norm_zero:
																		SCREAMING_SNAKE_CASE							:    Any							=							norm_hidden_states * (1 + scale_mlp[:, None]) + shift_mlp[:, None]

												if self._chunk_size is not None:
																		# "feed_forward_chunk_size" can be used to save memory
																		if norm_hidden_states.shape[self._chunk_dim] % self._chunk_size != 0:
																								raise ValueError(
																								    f"""`hidden_states` dimension to be chunked: {norm_hidden_states.shape[self._chunk_dim]} has to be divisible by chunk size: {self._chunk_size}. Make sure to set an appropriate `chunk_size` when calling `unet.enable_forward_chunking`."""						)

																		SCREAMING_SNAKE_CASE							:    Dict							=							norm_hidden_states.shape[self._chunk_dim] // self._chunk_size
																		SCREAMING_SNAKE_CASE							:    Dict							=							torch.cat(
																		    [self.ff(UpperCamelCase__						) for hid_slice in norm_hidden_states.chunk(UpperCamelCase__    ,		dim=self._chunk_dim						)]    ,		dim=self._chunk_dim    ,		)
												else:
																		SCREAMING_SNAKE_CASE							:    Any							=							self.ff(UpperCamelCase__						)

												if self.use_ada_layer_norm_zero:
																		SCREAMING_SNAKE_CASE							:    Dict							=							gate_mlp.unsqueeze(1						) * ff_output

												SCREAMING_SNAKE_CASE							:    Optional[int]							=							ff_output + hidden_states

												return hidden_states






class 					lowercase__						(   nn.Module):


						def __init__(       self	:				Tuple    ,		UpperCamelCase__	:				int    ,		UpperCamelCase__	:				Optional[int] = None    ,		UpperCamelCase__	:				int = 4    ,		UpperCamelCase__	:				float = 0.0    ,		UpperCamelCase__	:				str = "geglu"    ,		UpperCamelCase__	:				bool = False    ,		):





												'''simple docstring'''





												super().__init__()
												SCREAMING_SNAKE_CASE							:    Optional[Any]							=							int(dim * mult						)
												SCREAMING_SNAKE_CASE							:    Dict							=							dim_out if dim_out is not None else dim

												if activation_fn == "gelu":
																		SCREAMING_SNAKE_CASE							:    Tuple							=							GELU(UpperCamelCase__    ,		UpperCamelCase__						)
												if activation_fn == "gelu-approximate":
																		SCREAMING_SNAKE_CASE							:    Optional[int]							=							GELU(UpperCamelCase__    ,		UpperCamelCase__    ,		approximate='''tanh'''						)
												elif activation_fn == "geglu":
																		SCREAMING_SNAKE_CASE							:    Optional[int]							=							GEGLU(UpperCamelCase__    ,		UpperCamelCase__						)
												elif activation_fn == "geglu-approximate":
																		SCREAMING_SNAKE_CASE							:    Optional[Any]							=							ApproximateGELU(UpperCamelCase__    ,		UpperCamelCase__						)

												SCREAMING_SNAKE_CASE							:    str							=							nn.ModuleList([]						)
												# project in
												self.net.append(UpperCamelCase__						)
												# project dropout
												self.net.append(nn.Dropout(UpperCamelCase__						)						)
												# project out
												self.net.append(nn.Linear(UpperCamelCase__    ,		UpperCamelCase__						)						)
												# FF as used in Vision Transformer, MLP-Mixer, etc. have a final dropout
												if final_dropout:
																		self.net.append(nn.Dropout(UpperCamelCase__						)						)



						def        __A       (       self	:				Tuple    ,		UpperCamelCase__	:				Optional[int]						):





												'''simple docstring'''





												for module in self.net:
																		SCREAMING_SNAKE_CASE							:    Union[str, Any]							=							module(UpperCamelCase__						)
												return hidden_states






class 					lowercase__						(   nn.Module):


						def __init__(       self	:				List[Any]    ,		UpperCamelCase__	:				int    ,		UpperCamelCase__	:				int    ,		UpperCamelCase__	:				str = "none"						):





												'''simple docstring'''





												super().__init__()
												SCREAMING_SNAKE_CASE							:    Dict							=							nn.Linear(UpperCamelCase__    ,		UpperCamelCase__						)
												SCREAMING_SNAKE_CASE							:    Tuple							=							approximate


						def        __A       (       self	:				Dict    ,		UpperCamelCase__	:				Any						):





												'''simple docstring'''





												if gate.device.type != "mps":
																		return F.gelu(UpperCamelCase__    ,		approximate=self.approximate						)
												# mps: gelu is not implemented for float16
												return F.gelu(gate.to(dtype=torch.floataa						)    ,		approximate=self.approximate						).to(dtype=gate.dtype						)



						def        __A       (       self	:				Any    ,		UpperCamelCase__	:				Any						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    Optional[int]							=							self.proj(UpperCamelCase__						)
												SCREAMING_SNAKE_CASE							:    Union[str, Any]							=							self.gelu(UpperCamelCase__						)
												return hidden_states






class 					lowercase__						(   nn.Module):


						def __init__(       self	:				Optional[Any]    ,		UpperCamelCase__	:				int    ,		UpperCamelCase__	:				int						):





												'''simple docstring'''





												super().__init__()
												SCREAMING_SNAKE_CASE							:    Optional[Any]							=							nn.Linear(UpperCamelCase__    ,		dim_out * 2						)


						def        __A       (       self	:				Any    ,		UpperCamelCase__	:				List[Any]						):





												'''simple docstring'''





												if gate.device.type != "mps":
																		return F.gelu(UpperCamelCase__						)
												# mps: gelu is not implemented for float16
												return F.gelu(gate.to(dtype=torch.floataa						)						).to(dtype=gate.dtype						)



						def        __A       (       self	:				Optional[int]    ,		UpperCamelCase__	:				Dict						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE				,  SCREAMING_SNAKE_CASE							:    int							=							self.proj(UpperCamelCase__						).chunk(2    ,		dim=-1						)
												return hidden_states * self.gelu(UpperCamelCase__						)






class 					lowercase__						(   nn.Module):


						def __init__(       self	:				Union[str, Any]    ,		UpperCamelCase__	:				int    ,		UpperCamelCase__	:				int						):





												'''simple docstring'''





												super().__init__()
												SCREAMING_SNAKE_CASE							:    Union[str, Any]							=							nn.Linear(UpperCamelCase__    ,		UpperCamelCase__						)



						def        __A       (       self	:				Tuple    ,		UpperCamelCase__	:				Union[str, Any]						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    List[str]							=							self.proj(UpperCamelCase__						)
												return x * torch.sigmoid(1.702 * x						)






class 					lowercase__						(   nn.Module):


						def __init__(       self	:				str    ,		UpperCamelCase__	:				str    ,		UpperCamelCase__	:				List[str]						):





												'''simple docstring'''





												super().__init__()
												SCREAMING_SNAKE_CASE							:    List[Any]							=							nn.Embedding(UpperCamelCase__    ,		UpperCamelCase__						)
												SCREAMING_SNAKE_CASE							:    int							=							nn.SiLU()
												SCREAMING_SNAKE_CASE							:    Dict							=							nn.Linear(UpperCamelCase__    ,		embedding_dim * 2						)
												SCREAMING_SNAKE_CASE							:    List[str]							=							nn.LayerNorm(UpperCamelCase__    ,		elementwise_affine=UpperCamelCase__						)



						def        __A       (       self	:				Dict    ,		UpperCamelCase__	:				Optional[Any]    ,		UpperCamelCase__	:				Optional[int]						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    Tuple							=							self.linear(self.silu(self.emb(UpperCamelCase__						)						)						)
												SCREAMING_SNAKE_CASE				,  SCREAMING_SNAKE_CASE							:    Dict							=							torch.chunk(UpperCamelCase__    ,		2						)
												SCREAMING_SNAKE_CASE							:    Optional[Any]							=							self.norm(UpperCamelCase__						) * (1 + scale) + shift
												return x






class 					lowercase__						(   nn.Module):


						def __init__(       self	:				Optional[int]    ,		UpperCamelCase__	:				Optional[Any]    ,		UpperCamelCase__	:				int						):





												'''simple docstring'''





												super().__init__()

												SCREAMING_SNAKE_CASE							:    int							=							CombinedTimestepLabelEmbeddings(UpperCamelCase__    ,		UpperCamelCase__						)

												SCREAMING_SNAKE_CASE							:    Optional[Any]							=							nn.SiLU()
												SCREAMING_SNAKE_CASE							:    Optional[int]							=							nn.Linear(UpperCamelCase__    ,		6 * embedding_dim    ,		bias=UpperCamelCase__						)
												SCREAMING_SNAKE_CASE							:    List[Any]							=							nn.LayerNorm(UpperCamelCase__    ,		elementwise_affine=UpperCamelCase__    ,		eps=1E-6						)



						def        __A       (       self	:				Union[str, Any]    ,		UpperCamelCase__	:				str    ,		UpperCamelCase__	:				Dict    ,		UpperCamelCase__	:				List[Any]    ,		UpperCamelCase__	:				List[Any]=None						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    Union[str, Any]							=							self.linear(self.silu(self.emb(UpperCamelCase__    ,		UpperCamelCase__    ,		hidden_dtype=UpperCamelCase__						)						)						)
												SCREAMING_SNAKE_CASE				,  SCREAMING_SNAKE_CASE				,  SCREAMING_SNAKE_CASE				,  SCREAMING_SNAKE_CASE				,  SCREAMING_SNAKE_CASE				,  SCREAMING_SNAKE_CASE							:    Dict							=							emb.chunk(6    ,		dim=1						)
												SCREAMING_SNAKE_CASE							:    Tuple							=							self.norm(UpperCamelCase__						) * (1 + scale_msa[:, None]) + shift_msa[:, None]
												return x, gate_msa, shift_mlp, scale_mlp, gate_mlp






class 					lowercase__						(   nn.Module):


						def __init__(       self	:				Dict    ,		UpperCamelCase__	:				int    ,		UpperCamelCase__	:				int    ,		UpperCamelCase__	:				int    ,		UpperCamelCase__	:				Optional[str] = None    ,		UpperCamelCase__	:				float = 1E-5						):





												'''simple docstring'''





												super().__init__()
												SCREAMING_SNAKE_CASE							:    List[Any]							=							num_groups
												SCREAMING_SNAKE_CASE							:    Optional[int]							=							eps

												if act_fn is None:
																		SCREAMING_SNAKE_CASE							:    List[Any]							=							None
												else:
																		SCREAMING_SNAKE_CASE							:    List[Any]							=							get_activation(UpperCamelCase__						)

												SCREAMING_SNAKE_CASE							:    Optional[int]							=							nn.Linear(UpperCamelCase__    ,		out_dim * 2						)



						def        __A       (       self	:				Union[str, Any]    ,		UpperCamelCase__	:				Optional[int]    ,		UpperCamelCase__	:				Union[str, Any]						):





												'''simple docstring'''





												if self.act:
																		SCREAMING_SNAKE_CASE							:    Dict							=							self.act(UpperCamelCase__						)
												SCREAMING_SNAKE_CASE							:    int							=							self.linear(UpperCamelCase__						)
												SCREAMING_SNAKE_CASE							:    Optional[Any]							=							emb[:, :, None, None]
												SCREAMING_SNAKE_CASE				,  SCREAMING_SNAKE_CASE							:    List[Any]							=							emb.chunk(2    ,		dim=1						)

												SCREAMING_SNAKE_CASE							:    List[Any]							=							F.group_norm(UpperCamelCase__    ,		self.num_groups    ,		eps=self.eps						)
												SCREAMING_SNAKE_CASE							:    List[Any]							=							x * (1 + scale) + shift
												return x






from typing import TYPE_CHECKING

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


__UpperCamelCase		:       List[Any]									=     {'configuration_glpn': ['GLPN_PRETRAINED_CONFIG_ARCHIVE_MAP', 'GLPNConfig']}

try:
					if not is_vision_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					__UpperCamelCase		:       Optional[Any]									=     ['GLPNFeatureExtractor']
					__UpperCamelCase		:       List[str]									=     ['GLPNImageProcessor']

try:
					if not is_torch_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					__UpperCamelCase		:       str									=     [
					    'GLPN_PRETRAINED_MODEL_ARCHIVE_LIST',
					    'GLPNForDepthEstimation',
					    'GLPNLayer',
					    'GLPNModel',
					    'GLPNPreTrainedModel',
					]


if TYPE_CHECKING:
					from .configuration_glpn import GLPN_PRETRAINED_CONFIG_ARCHIVE_MAP, GLPNConfig

					try:
										if not is_vision_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .feature_extraction_glpn import GLPNFeatureExtractor
										from .image_processing_glpn import GLPNImageProcessor

					try:
										if not is_torch_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .modeling_glpn import (
										    GLPN_PRETRAINED_MODEL_ARCHIVE_LIST,
										    GLPNForDepthEstimation,
										    GLPNLayer,
										    GLPNModel,
										    GLPNPreTrainedModel,
										)


else:
					import sys

					__UpperCamelCase		:       Optional[Any]									=     _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)





