



'''simple docstring'''



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



					def __init__(    self			,				UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase=0.0			,				UpperCamelCase = None			,				UpperCamelCase = "geglu"			,				UpperCamelCase = None			,				UpperCamelCase = False			,				UpperCamelCase = False			,				UpperCamelCase = False			,				UpperCamelCase = False			,				UpperCamelCase = True			,				UpperCamelCase = "layer_norm"			,				UpperCamelCase = False			,				)     ->			int:
						super().__init__()
						__lowerCAmelCase												=  only_cross_attention

						__lowerCAmelCase												=  (num_embeds_ada_norm is not None) and norm_type == "ada_norm_zero"
						__lowerCAmelCase												=  (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:
							__lowerCAmelCase												=  AdaLayerNorm(UpperCamelCase			,				UpperCamelCase     )
						elif self.use_ada_layer_norm_zero:
							__lowerCAmelCase												=  AdaLayerNormZero(UpperCamelCase			,				UpperCamelCase     )
						else:
							__lowerCAmelCase												=  nn.LayerNorm(UpperCamelCase			,				elementwise_affine=UpperCamelCase     )
						__lowerCAmelCase												=  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.
							__lowerCAmelCase												=  (
							    AdaLayerNorm(UpperCamelCase			,				UpperCamelCase     )
							    if self.use_ada_layer_norm
							    else nn.LayerNorm(UpperCamelCase			,				elementwise_affine=UpperCamelCase     )
							)
							__lowerCAmelCase												=  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:
							__lowerCAmelCase												=  None
							__lowerCAmelCase												=  None

						# 3. Feed-forward
						__lowerCAmelCase												=  nn.LayerNorm(UpperCamelCase			,				elementwise_affine=UpperCamelCase     )
						__lowerCAmelCase												=  FeedForward(UpperCamelCase			,				dropout=UpperCamelCase			,				activation_fn=UpperCamelCase			,				final_dropout=UpperCamelCase     )

						# let chunk size default to None
						__lowerCAmelCase												=  None
						__lowerCAmelCase												=  0



					def        UpperCAmelCase_ (    self			,				UpperCamelCase			,				UpperCamelCase     )     ->			Tuple:
						# Sets chunk feed-forward
						__lowerCAmelCase												=  chunk_size
						__lowerCAmelCase												=  dim



					def        UpperCAmelCase_ (    self			,				UpperCamelCase			,				UpperCamelCase = None			,				UpperCamelCase = None			,				UpperCamelCase = None			,				UpperCamelCase = None			,				UpperCamelCase = None			,				UpperCamelCase = None			,				)     ->			Any:
						# Notice that normalization is always applied before the real computation in the following blocks.
						# 1. Self-Attention
						if self.use_ada_layer_norm:
							__lowerCAmelCase												=  self.norma(UpperCamelCase			,				UpperCamelCase     )
						elif self.use_ada_layer_norm_zero:
							__lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase												=  self.norma(
							    UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase			,				hidden_dtype=hidden_states.dtype     )
						else:
							__lowerCAmelCase												=  self.norma(UpperCamelCase     )

						__lowerCAmelCase												=  cross_attention_kwargs if cross_attention_kwargs is not None else {}

						__lowerCAmelCase												=  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:
							__lowerCAmelCase												=  gate_msa.unsqueeze(1     ) * attn_output
						__lowerCAmelCase												=  attn_output + hidden_states

						# 2. Cross-Attention
						if self.attna is not None:
							__lowerCAmelCase												=  (
							    self.norma(UpperCamelCase			,				UpperCamelCase     ) if self.use_ada_layer_norm else self.norma(UpperCamelCase     )
							)

							__lowerCAmelCase												=  self.attna(
							    UpperCamelCase			,				encoder_hidden_states=UpperCamelCase			,				attention_mask=UpperCamelCase			,				**UpperCamelCase			,				)
							__lowerCAmelCase												=  attn_output + hidden_states

						# 3. Feed-forward
						__lowerCAmelCase												=  self.norma(UpperCamelCase     )

						if self.use_ada_layer_norm_zero:
							__lowerCAmelCase												=  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`.'''     )

							__lowerCAmelCase												=  norm_hidden_states.shape[self._chunk_dim] // self._chunk_size
							__lowerCAmelCase												=  torch.cat(
							    [self.ff(UpperCamelCase     ) for hid_slice in norm_hidden_states.chunk(UpperCamelCase			,				dim=self._chunk_dim     )]			,				dim=self._chunk_dim			,				)
						else:
							__lowerCAmelCase												=  self.ff(UpperCamelCase     )

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

						__lowerCAmelCase												=  ff_output + hidden_states

						return hidden_states






class 					UpperCAmelCase__	(		nn.Module ):



					def __init__(    self			,				UpperCamelCase			,				UpperCamelCase = None			,				UpperCamelCase = 4			,				UpperCamelCase = 0.0			,				UpperCamelCase = "geglu"			,				UpperCamelCase = False			,				)     ->			Union[str, Any]:
						super().__init__()
						__lowerCAmelCase												=  int(dim * mult     )
						__lowerCAmelCase												=  dim_out if dim_out is not None else dim

						if activation_fn == "gelu":
							__lowerCAmelCase												=  GELU(UpperCamelCase			,				UpperCamelCase     )
						if activation_fn == "gelu-approximate":
							__lowerCAmelCase												=  GELU(UpperCamelCase			,				UpperCamelCase			,				approximate="tanh"     )
						elif activation_fn == "geglu":
							__lowerCAmelCase												=  GEGLU(UpperCamelCase			,				UpperCamelCase     )
						elif activation_fn == "geglu-approximate":
							__lowerCAmelCase												=  ApproximateGELU(UpperCamelCase			,				UpperCamelCase     )

						__lowerCAmelCase												=  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        UpperCAmelCase_ (    self			,				UpperCamelCase     )     ->			str:
						for module in self.net:
							__lowerCAmelCase												=  module(UpperCamelCase     )
						return hidden_states






class 					UpperCAmelCase__	(		nn.Module ):



					def __init__(    self			,				UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase = "none"     )     ->			Dict:
						super().__init__()
						__lowerCAmelCase												=  nn.Linear(UpperCamelCase			,				UpperCamelCase     )
						__lowerCAmelCase												=  approximate



					def        UpperCAmelCase_ (    self			,				UpperCamelCase     )     ->			Dict:
						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        UpperCAmelCase_ (    self			,				UpperCamelCase     )     ->			Union[str, Any]:
						__lowerCAmelCase												=  self.proj(UpperCamelCase     )
						__lowerCAmelCase												=  self.gelu(UpperCamelCase     )
						return hidden_states






class 					UpperCAmelCase__	(		nn.Module ):



					def __init__(    self			,				UpperCamelCase			,				UpperCamelCase     )     ->			int:
						super().__init__()
						__lowerCAmelCase												=  nn.Linear(UpperCamelCase			,				dim_out * 2     )



					def        UpperCAmelCase_ (    self			,				UpperCamelCase     )     ->			List[str]:
						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        UpperCAmelCase_ (    self			,				UpperCamelCase     )     ->			Dict:
						__lowerCAmelCase							,       __lowerCAmelCase												=  self.proj(UpperCamelCase     ).chunk(2			,				dim=-1     )
						return hidden_states * self.gelu(UpperCamelCase     )






class 					UpperCAmelCase__	(		nn.Module ):



					def __init__(    self			,				UpperCamelCase			,				UpperCamelCase     )     ->			Dict:
						super().__init__()
						__lowerCAmelCase												=  nn.Linear(UpperCamelCase			,				UpperCamelCase     )



					def        UpperCAmelCase_ (    self			,				UpperCamelCase     )     ->			int:
						__lowerCAmelCase												=  self.proj(UpperCamelCase     )
						return x * torch.sigmoid(1.7_02 * x     )






class 					UpperCAmelCase__	(		nn.Module ):



					def __init__(    self			,				UpperCamelCase			,				UpperCamelCase     )     ->			Tuple:
						super().__init__()
						__lowerCAmelCase												=  nn.Embedding(UpperCamelCase			,				UpperCamelCase     )
						__lowerCAmelCase												=  nn.SiLU()
						__lowerCAmelCase												=  nn.Linear(UpperCamelCase			,				embedding_dim * 2     )
						__lowerCAmelCase												=  nn.LayerNorm(UpperCamelCase			,				elementwise_affine=UpperCamelCase     )



					def        UpperCAmelCase_ (    self			,				UpperCamelCase			,				UpperCamelCase     )     ->			Any:
						__lowerCAmelCase												=  self.linear(self.silu(self.emb(UpperCamelCase     )     )     )
						__lowerCAmelCase							,       __lowerCAmelCase												=  torch.chunk(UpperCamelCase			,				2     )
						__lowerCAmelCase												=  self.norm(UpperCamelCase     ) * (1 + scale) + shift
						return x






class 					UpperCAmelCase__	(		nn.Module ):



					def __init__(    self			,				UpperCamelCase			,				UpperCamelCase     )     ->			List[str]:
						super().__init__()

						__lowerCAmelCase												=  CombinedTimestepLabelEmbeddings(UpperCamelCase			,				UpperCamelCase     )

						__lowerCAmelCase												=  nn.SiLU()
						__lowerCAmelCase												=  nn.Linear(UpperCamelCase			,				6 * embedding_dim			,				bias=UpperCamelCase     )
						__lowerCAmelCase												=  nn.LayerNorm(UpperCamelCase			,				elementwise_affine=UpperCamelCase			,				eps=1E-6     )



					def        UpperCAmelCase_ (    self			,				UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase=None     )     ->			List[Any]:
						__lowerCAmelCase												=  self.linear(self.silu(self.emb(UpperCamelCase			,				UpperCamelCase			,				hidden_dtype=UpperCamelCase     )     )     )
						__lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase							,       __lowerCAmelCase												=  emb.chunk(6			,				dim=1     )
						__lowerCAmelCase												=  self.norm(UpperCamelCase     ) * (1 + scale_msa[:, None]) + shift_msa[:, None]
						return x, gate_msa, shift_mlp, scale_mlp, gate_mlp






class 					UpperCAmelCase__	(		nn.Module ):



					def __init__(    self			,				UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase			,				UpperCamelCase = None			,				UpperCamelCase = 1E-5     )     ->			List[str]:
						super().__init__()
						__lowerCAmelCase												=  num_groups
						__lowerCAmelCase												=  eps

						if act_fn is None:
							__lowerCAmelCase												=  None
						else:
							__lowerCAmelCase												=  get_activation(UpperCamelCase     )

						__lowerCAmelCase												=  nn.Linear(UpperCamelCase			,				out_dim * 2     )



					def        UpperCAmelCase_ (    self			,				UpperCamelCase			,				UpperCamelCase     )     ->			int:
						if self.act:
							__lowerCAmelCase												=  self.act(UpperCamelCase     )
						__lowerCAmelCase												=  self.linear(UpperCamelCase     )
						__lowerCAmelCase												=  emb[:, :, None, None]
						__lowerCAmelCase							,       __lowerCAmelCase												=  emb.chunk(2			,				dim=1     )

						__lowerCAmelCase												=  F.group_norm(UpperCamelCase			,				self.num_groups			,				eps=self.eps     )
						__lowerCAmelCase												=  x * (1 + scale) + shift
						return x




'''simple docstring'''



import re



def   __lowerCAmelCase					(							lowerCamelCase   :						str					):






	'''simple docstring'''




	__lowerCAmelCase												=  re.compile(
	    r"^(?:0|94|\+94|0{2}94)" r"7(0|1|2|4|5|6|7|8)" r"(-| |)" r"\d{7}$"					)

	return bool(re.search(lowerCamelCase				,		lowerCamelCase					)					)


if __name__ == "__main__":
	lowerCAmelCase	:		Optional[Any]				=   '''0094702343221'''

	print(is_sri_lankan_phone_number(phone))