





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        _a(    nn.Module    ):







		def __init__(    self ,						__snake_case ,						__snake_case ,						__snake_case ,						__snake_case=0.0 ,						__snake_case = None ,						__snake_case = "geglu" ,						__snake_case = None ,						__snake_case = False ,						__snake_case = False ,						__snake_case = False ,						__snake_case = False ,						__snake_case = True ,						__snake_case = "layer_norm" ,						__snake_case = False ,						)      ->      Union[str, Any]:


							'''simple docstring'''

							super().__init__()
							_snake_case       :					Tuple				       =   only_cross_attention

							_snake_case       :					List[Any]				       =   (num_embeds_ada_norm is not None) and norm_type == "ada_norm_zero"
							_snake_case       :					Optional[int]				       =   (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:
												_snake_case       :					Any				       =   AdaLayerNorm(__snake_case ,						__snake_case				)
							elif self.use_ada_layer_norm_zero:
												_snake_case       :					str				       =   AdaLayerNormZero(__snake_case ,						__snake_case				)
							else:
												_snake_case       :					Dict				       =   nn.LayerNorm(__snake_case ,						elementwise_affine=__snake_case				)
							_snake_case       :					Optional[Any]				       =   Attention(
							    query_dim=__snake_case ,						heads=__snake_case ,						dim_head=__snake_case ,						dropout=__snake_case ,						bias=__snake_case ,						cross_attention_dim=cross_attention_dim if only_cross_attention else None ,						upcast_attention=__snake_case ,						)

							# 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.
												_snake_case       :					List[str]				       =   (
												    AdaLayerNorm(__snake_case ,						__snake_case				)
												    if self.use_ada_layer_norm
												    else nn.LayerNorm(__snake_case ,						elementwise_affine=__snake_case				)
												)
												_snake_case       :					List[Any]				       =   Attention(
												    query_dim=__snake_case ,						cross_attention_dim=cross_attention_dim if not double_self_attention else None ,						heads=__snake_case ,						dim_head=__snake_case ,						dropout=__snake_case ,						bias=__snake_case ,						upcast_attention=__snake_case ,						)  # is self-attn if encoder_hidden_states is none
							else:
												_snake_case       :					Optional[int]				       =   None
												_snake_case       :					List[Any]				       =   None

							# 3. Feed-forward
							_snake_case       :					Optional[Any]				       =   nn.LayerNorm(__snake_case ,						elementwise_affine=__snake_case				)
							_snake_case       :					Optional[Any]				       =   FeedForward(__snake_case ,						dropout=__snake_case ,						activation_fn=__snake_case ,						final_dropout=__snake_case				)

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







		def        lowercase       (    self ,						__snake_case ,						__snake_case				)      ->      Optional[Any]:


							'''simple docstring'''

							_snake_case       :					Any				       =   chunk_size
							_snake_case       :					List[Any]				       =   dim







		def        lowercase       (    self ,						__snake_case ,						__snake_case = None ,						__snake_case = None ,						__snake_case = None ,						__snake_case = None ,						__snake_case = None ,						__snake_case = None ,						)      ->      Any:


							'''simple docstring'''

							if self.use_ada_layer_norm:
												_snake_case       :					Any				       =   self.norma(__snake_case ,						__snake_case				)
							elif self.use_ada_layer_norm_zero:
												_snake_case       :					Dict				       =   self.norma(
												    __snake_case ,						__snake_case ,						__snake_case ,						hidden_dtype=hidden_states.dtype				)
							else:
												_snake_case       :					int				       =   self.norma(__snake_case				)

							_snake_case       :					Optional[Any]				       =   cross_attention_kwargs if cross_attention_kwargs is not None else {}

							_snake_case       :					Dict				       =   self.attna(
							    __snake_case ,						encoder_hidden_states=encoder_hidden_states if self.only_cross_attention else None ,						attention_mask=__snake_case ,						**__snake_case ,						)
							if self.use_ada_layer_norm_zero:
												_snake_case       :					List[str]				       =   gate_msa.unsqueeze(1				) * attn_output
							_snake_case       :					List[Any]				       =   attn_output + hidden_states

							# 2. Cross-Attention
							if self.attna is not None:
												_snake_case       :					Optional[int]				       =   (
												    self.norma(__snake_case ,						__snake_case				) if self.use_ada_layer_norm else self.norma(__snake_case				)
												)

												_snake_case       :					Union[str, Any]				       =   self.attna(
												    __snake_case ,						encoder_hidden_states=__snake_case ,						attention_mask=__snake_case ,						**__snake_case ,						)
												_snake_case       :					str				       =   attn_output + hidden_states

							# 3. Feed-forward
							_snake_case       :					Optional[Any]				       =   self.norma(__snake_case				)

							if self.use_ada_layer_norm_zero:
												_snake_case       :					List[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`."""				)

												_snake_case       :					Dict				       =   norm_hidden_states.shape[self._chunk_dim] // self._chunk_size
												_snake_case       :					Optional[Any]				       =   torch.cat(
												    [self.ff(__snake_case				) for hid_slice in norm_hidden_states.chunk(__snake_case ,						dim=self._chunk_dim				)] ,						dim=self._chunk_dim ,						)
							else:
												_snake_case       :					List[str]				       =   self.ff(__snake_case				)

							if self.use_ada_layer_norm_zero:
												_snake_case       :					List[Any]				       =   gate_mlp.unsqueeze(1				) * ff_output

							_snake_case       :					Optional[Any]				       =   ff_output + hidden_states

							return hidden_states






class        _a(    nn.Module    ):







		def __init__(    self ,						__snake_case ,						__snake_case = None ,						__snake_case = 4 ,						__snake_case = 0.0 ,						__snake_case = "geglu" ,						__snake_case = False ,						)      ->      Union[str, Any]:


							'''simple docstring'''

							super().__init__()
							_snake_case       :					str				       =   int(dim * mult				)
							_snake_case       :					Dict				       =   dim_out if dim_out is not None else dim

							if activation_fn == "gelu":
												_snake_case       :					Any				       =   GELU(__snake_case ,						__snake_case				)
							if activation_fn == "gelu-approximate":
												_snake_case       :					List[str]				       =   GELU(__snake_case ,						__snake_case ,						approximate="tanh"				)
							elif activation_fn == "geglu":
												_snake_case       :					Union[str, Any]				       =   GEGLU(__snake_case ,						__snake_case				)
							elif activation_fn == "geglu-approximate":
												_snake_case       :					Any				       =   ApproximateGELU(__snake_case ,						__snake_case				)

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







		def        lowercase       (    self ,						__snake_case				)      ->      Any:


							'''simple docstring'''

							for module in self.net:
												_snake_case       :					int				       =   module(__snake_case				)
							return hidden_states






class        _a(    nn.Module    ):







		def __init__(    self ,						__snake_case ,						__snake_case ,						__snake_case = "none"				)      ->      int:


							'''simple docstring'''

							super().__init__()
							_snake_case       :					Union[str, Any]				       =   nn.Linear(__snake_case ,						__snake_case				)
							_snake_case       :					List[Any]				       =   approximate







		def        lowercase       (    self ,						__snake_case				)      ->      Optional[int]:


							'''simple docstring'''

							if gate.device.type != "mps":
												return F.gelu(__snake_case ,						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        lowercase       (    self ,						__snake_case				)      ->      Optional[int]:


							'''simple docstring'''

							_snake_case       :					List[Any]				       =   self.proj(__snake_case				)
							_snake_case       :					Optional[int]				       =   self.gelu(__snake_case				)
							return hidden_states






class        _a(    nn.Module    ):







		def __init__(    self ,						__snake_case ,						__snake_case				)      ->      List[str]:


							'''simple docstring'''

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







		def        lowercase       (    self ,						__snake_case				)      ->      Optional[int]:


							'''simple docstring'''

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







		def        lowercase       (    self ,						__snake_case				)      ->      Tuple:


							'''simple docstring'''

							_snake_case       :					Optional[int]				       =   self.proj(__snake_case				).chunk(2 ,						dim=-1				)
							return hidden_states * self.gelu(__snake_case				)






class        _a(    nn.Module    ):







		def __init__(    self ,						__snake_case ,						__snake_case				)      ->      Dict:


							'''simple docstring'''

							super().__init__()
							_snake_case       :					Union[str, Any]				       =   nn.Linear(__snake_case ,						__snake_case				)







		def        lowercase       (    self ,						__snake_case				)      ->      str:


							'''simple docstring'''

							_snake_case       :					Union[str, Any]				       =   self.proj(__snake_case				)
							return x * torch.sigmoid(1.7_02 * x				)






class        _a(    nn.Module    ):







		def __init__(    self ,						__snake_case ,						__snake_case				)      ->      Optional[Any]:


							'''simple docstring'''

							super().__init__()
							_snake_case       :					Optional[Any]				       =   nn.Embedding(__snake_case ,						__snake_case				)
							_snake_case       :					str				       =   nn.SiLU()
							_snake_case       :					Dict				       =   nn.Linear(__snake_case ,						embedding_dim * 2				)
							_snake_case       :					Optional[Any]				       =   nn.LayerNorm(__snake_case ,						elementwise_affine=__snake_case				)







		def        lowercase       (    self ,						__snake_case ,						__snake_case				)      ->      Optional[Any]:


							'''simple docstring'''

							_snake_case       :					List[Any]				       =   self.linear(self.silu(self.emb(__snake_case				)				)				)
							_snake_case       :					Any				       =   torch.chunk(__snake_case ,						2				)
							_snake_case       :					List[Any]				       =   self.norm(__snake_case				) * (1 + scale) + shift
							return x






class        _a(    nn.Module    ):







		def __init__(    self ,						__snake_case ,						__snake_case				)      ->      Optional[Any]:


							'''simple docstring'''

							super().__init__()

							_snake_case       :					Union[str, Any]				       =   CombinedTimestepLabelEmbeddings(__snake_case ,						__snake_case				)

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







		def        lowercase       (    self ,						__snake_case ,						__snake_case ,						__snake_case ,						__snake_case=None				)      ->      Dict:


							'''simple docstring'''

							_snake_case       :					Any				       =   self.linear(self.silu(self.emb(__snake_case ,						__snake_case ,						hidden_dtype=__snake_case				)				)				)
							_snake_case       :					Optional[Any]				       =   emb.chunk(6 ,						dim=1				)
							_snake_case       :					str				       =   self.norm(__snake_case				) * (1 + scale_msa[:, None]) + shift_msa[:, None]
							return x, gate_msa, shift_mlp, scale_mlp, gate_mlp






class        _a(    nn.Module    ):







		def __init__(    self ,						__snake_case ,						__snake_case ,						__snake_case ,						__snake_case = None ,						__snake_case = 1E-5				)      ->      Tuple:


							'''simple docstring'''

							super().__init__()
							_snake_case       :					Dict				       =   num_groups
							_snake_case       :					List[Any]				       =   eps

							if act_fn is None:
												_snake_case       :					Union[str, Any]				       =   None
							else:
												_snake_case       :					List[str]				       =   get_activation(__snake_case				)

							_snake_case       :					Any				       =   nn.Linear(__snake_case ,						out_dim * 2				)







		def        lowercase       (    self ,						__snake_case ,						__snake_case				)      ->      Union[str, Any]:


							'''simple docstring'''

							if self.act:
												_snake_case       :					Any				       =   self.act(__snake_case				)
							_snake_case       :					Optional[int]				       =   self.linear(__snake_case				)
							_snake_case       :					Optional[int]				       =   emb[:, :, None, None]
							_snake_case       :					Union[str, Any]				       =   emb.chunk(2 ,						dim=1				)

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






import re







def    A				(							UpperCAmelCase				):
					_snake_case       :					Any				       =   re.compile(R"^(\+91[\-\s]?)?[0]?(91)?[789]\d{9}$"				)
					if match := re.search(UpperCAmelCase  ,  UpperCAmelCase				):
										return match.string == phone
					return False


if __name__ == "__main__":
							print(indian_phone_validator('+918827897895'))