




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						,		_SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE=0.0						,		_SCREAMING_SNAKE_CASE = None						,		_SCREAMING_SNAKE_CASE = "geglu"						,		_SCREAMING_SNAKE_CASE = None						,		_SCREAMING_SNAKE_CASE = False						,		_SCREAMING_SNAKE_CASE = False						,		_SCREAMING_SNAKE_CASE = False						,		_SCREAMING_SNAKE_CASE = False						,		_SCREAMING_SNAKE_CASE = True						,		_SCREAMING_SNAKE_CASE = "layer_norm"						,		_SCREAMING_SNAKE_CASE = False						,		):
								super().__init__()
								_UpperCAmelCase      =			only_cross_attention

								_UpperCAmelCase      =			(num_embeds_ada_norm is not None) and norm_type == """ada_norm_zero"""
								_UpperCAmelCase      =			(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:
														_UpperCAmelCase      =			AdaLayerNorm(_SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE			)
								elif self.use_ada_layer_norm_zero:
														_UpperCAmelCase      =			AdaLayerNormZero(_SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE			)
								else:
														_UpperCAmelCase      =			nn.LayerNorm(_SCREAMING_SNAKE_CASE						,		elementwise_affine=_SCREAMING_SNAKE_CASE			)
								_UpperCAmelCase      =			Attention(
								    query_dim=_SCREAMING_SNAKE_CASE						,		heads=_SCREAMING_SNAKE_CASE						,		dim_head=_SCREAMING_SNAKE_CASE						,		dropout=_SCREAMING_SNAKE_CASE						,		bias=_SCREAMING_SNAKE_CASE						,		cross_attention_dim=cross_attention_dim if only_cross_attention else None						,		upcast_attention=_SCREAMING_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.
														_UpperCAmelCase      =			(
														    AdaLayerNorm(_SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE			)
														    if self.use_ada_layer_norm
														    else nn.LayerNorm(_SCREAMING_SNAKE_CASE						,		elementwise_affine=_SCREAMING_SNAKE_CASE			)
														)
														_UpperCAmelCase      =			Attention(
														    query_dim=_SCREAMING_SNAKE_CASE						,		cross_attention_dim=cross_attention_dim if not double_self_attention else None						,		heads=_SCREAMING_SNAKE_CASE						,		dim_head=_SCREAMING_SNAKE_CASE						,		dropout=_SCREAMING_SNAKE_CASE						,		bias=_SCREAMING_SNAKE_CASE						,		upcast_attention=_SCREAMING_SNAKE_CASE						,		)  # is self-attn if encoder_hidden_states is none
								else:
														_UpperCAmelCase      =			None
														_UpperCAmelCase      =			None

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

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






		def 				UpperCAmelCase (    self						,		_SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE			):
								# Sets chunk feed-forward
								_UpperCAmelCase      =			chunk_size
								_UpperCAmelCase      =			dim






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

								_UpperCAmelCase      =			cross_attention_kwargs if cross_attention_kwargs is not None else {}

								_UpperCAmelCase      =			self.attna(
								    _SCREAMING_SNAKE_CASE						,		encoder_hidden_states=encoder_hidden_states if self.only_cross_attention else None						,		attention_mask=_SCREAMING_SNAKE_CASE						,		**_SCREAMING_SNAKE_CASE						,		)
								if self.use_ada_layer_norm_zero:
														_UpperCAmelCase      =			gate_msa.unsqueeze(1			) * attn_output
								_UpperCAmelCase      =			attn_output + hidden_states

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

														_UpperCAmelCase      =			self.attna(
														    _SCREAMING_SNAKE_CASE						,		encoder_hidden_states=_SCREAMING_SNAKE_CASE						,		attention_mask=_SCREAMING_SNAKE_CASE						,		**_SCREAMING_SNAKE_CASE						,		)
														_UpperCAmelCase      =			attn_output + hidden_states

								# 3. Feed-forward
								_UpperCAmelCase      =			self.norma(_SCREAMING_SNAKE_CASE			)

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

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

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

								_UpperCAmelCase      =			ff_output + hidden_states

								return hidden_states




class       _A				(		nn.Module     ):






		def __init__(    self						,		_SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE = None						,		_SCREAMING_SNAKE_CASE = 4						,		_SCREAMING_SNAKE_CASE = 0.0						,		_SCREAMING_SNAKE_CASE = "geglu"						,		_SCREAMING_SNAKE_CASE = False						,		):
								super().__init__()
								_UpperCAmelCase      =			int(dim * mult			)
								_UpperCAmelCase      =			dim_out if dim_out is not None else dim

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

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






		def 				UpperCAmelCase (    self						,		_SCREAMING_SNAKE_CASE			):
								for module in self.net:
														_UpperCAmelCase      =			module(_SCREAMING_SNAKE_CASE			)
								return hidden_states




class       _A				(		nn.Module     ):






		def __init__(    self						,		_SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE = "none"			):
								super().__init__()
								_UpperCAmelCase      =			nn.Linear(_SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE			)
								_UpperCAmelCase      =			approximate






		def 				UpperCAmelCase (    self						,		_SCREAMING_SNAKE_CASE			):
								if gate.device.type != "mps":
														return F.gelu(_SCREAMING_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 				UpperCAmelCase (    self						,		_SCREAMING_SNAKE_CASE			):
								_UpperCAmelCase      =			self.proj(_SCREAMING_SNAKE_CASE			)
								_UpperCAmelCase      =			self.gelu(_SCREAMING_SNAKE_CASE			)
								return hidden_states




class       _A				(		nn.Module     ):






		def __init__(    self						,		_SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE			):
								super().__init__()
								_UpperCAmelCase      =			nn.Linear(_SCREAMING_SNAKE_CASE						,		dim_out * 2			)






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






		def 				UpperCAmelCase (    self						,		_SCREAMING_SNAKE_CASE			):
								_UpperCAmelCase    ,					_UpperCAmelCase      =			self.proj(_SCREAMING_SNAKE_CASE			).chunk(2						,		dim=-1			)
								return hidden_states * self.gelu(_SCREAMING_SNAKE_CASE			)




class       _A				(		nn.Module     ):






		def __init__(    self						,		_SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE			):
								super().__init__()
								_UpperCAmelCase      =			nn.Linear(_SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE			)






		def 				UpperCAmelCase (    self						,		_SCREAMING_SNAKE_CASE			):
								_UpperCAmelCase      =			self.proj(_SCREAMING_SNAKE_CASE			)
								return x * torch.sigmoid(1.702 * x			)




class       _A				(		nn.Module     ):






		def __init__(    self						,		_SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE			):
								super().__init__()
								_UpperCAmelCase      =			nn.Embedding(_SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE			)
								_UpperCAmelCase      =			nn.SiLU()
								_UpperCAmelCase      =			nn.Linear(_SCREAMING_SNAKE_CASE						,		embedding_dim * 2			)
								_UpperCAmelCase      =			nn.LayerNorm(_SCREAMING_SNAKE_CASE						,		elementwise_affine=_SCREAMING_SNAKE_CASE			)






		def 				UpperCAmelCase (    self						,		_SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE			):
								_UpperCAmelCase      =			self.linear(self.silu(self.emb(_SCREAMING_SNAKE_CASE			)			)			)
								_UpperCAmelCase    ,					_UpperCAmelCase      =			torch.chunk(_SCREAMING_SNAKE_CASE						,		2			)
								_UpperCAmelCase      =			self.norm(_SCREAMING_SNAKE_CASE			) * (1 + scale) + shift
								return x




class       _A				(		nn.Module     ):






		def __init__(    self						,		_SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE			):
								super().__init__()

								_UpperCAmelCase      =			CombinedTimestepLabelEmbeddings(_SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE			)

								_UpperCAmelCase      =			nn.SiLU()
								_UpperCAmelCase      =			nn.Linear(_SCREAMING_SNAKE_CASE						,		6 * embedding_dim						,		bias=_SCREAMING_SNAKE_CASE			)
								_UpperCAmelCase      =			nn.LayerNorm(_SCREAMING_SNAKE_CASE						,		elementwise_affine=_SCREAMING_SNAKE_CASE						,		eps=1e-6			)






		def 				UpperCAmelCase (    self						,		_SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE=None			):
								_UpperCAmelCase      =			self.linear(self.silu(self.emb(_SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE						,		hidden_dtype=_SCREAMING_SNAKE_CASE			)			)			)
								_UpperCAmelCase    ,					_UpperCAmelCase    ,					_UpperCAmelCase    ,					_UpperCAmelCase    ,					_UpperCAmelCase    ,					_UpperCAmelCase      =			emb.chunk(6						,		dim=1			)
								_UpperCAmelCase      =			self.norm(_SCREAMING_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						,		_SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE = None						,		_SCREAMING_SNAKE_CASE = 1e-5			):
								super().__init__()
								_UpperCAmelCase      =			num_groups
								_UpperCAmelCase      =			eps

								if act_fn is None:
														_UpperCAmelCase      =			None
								else:
														_UpperCAmelCase      =			get_activation(_SCREAMING_SNAKE_CASE			)

								_UpperCAmelCase      =			nn.Linear(_SCREAMING_SNAKE_CASE						,		out_dim * 2			)






		def 				UpperCAmelCase (    self						,		_SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE			):
								if self.act:
														_UpperCAmelCase      =			self.act(_SCREAMING_SNAKE_CASE			)
								_UpperCAmelCase      =			self.linear(_SCREAMING_SNAKE_CASE			)
								_UpperCAmelCase      =			emb[:, :, None, None]
								_UpperCAmelCase    ,					_UpperCAmelCase      =			emb.chunk(2						,		dim=1			)

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





def        _SCREAMING_SNAKE_CASE      (		snake_case			,     snake_case       )						->				bool:
						_UpperCAmelCase      =			len(snake_case       ) + 1
						_UpperCAmelCase      =			len(snake_case       ) + 1

						# dp is a 2d matrix where dp[i][j] denotes whether prefix string of
						# length i of input_string matches with prefix string of length j of
						# given pattern.
						# "dp" stands for dynamic programming.
						_UpperCAmelCase      =			[[0 for i in range(snake_case       )] for j in range(snake_case       )]

						# since string of zero length match pattern of zero length
						_UpperCAmelCase      =			1

						# since pattern of zero length will never match with string of non-zero length
						for i in range(1			,     snake_case       ):
												_UpperCAmelCase      =			0

						# since string of zero length will match with pattern where there
						# is at least one * alternatively
						for j in range(1			,     snake_case       ):
												_UpperCAmelCase      =			dp[0][j - 2] if pattern[j - 1] == """*""" else 0

						# now using bottom-up approach to find for all remaining lengths
						for i in range(1			,     snake_case       ):
												for j in range(1			,     snake_case       ):
																		if input_string[i - 1] == pattern[j - 1] or pattern[j - 1] == ".":
																								_UpperCAmelCase      =			dp[i - 1][j - 1]

																		elif pattern[j - 1] == "*":
																								if dp[i][j - 2] == 1:
																														_UpperCAmelCase      =			1
																								elif pattern[j - 2] in (input_string[i - 1], "."):
																														_UpperCAmelCase      =			dp[i - 1][j]
																								else:
																														_UpperCAmelCase      =			0
																		else:
																								_UpperCAmelCase      =			0

						return bool(dp[-1][-1]       )


if __name__ == "__main__":
				import doctest

				doctest.testmod()
				# inputing the strings
				# input_string = input("input a string :")
				# pattern = input("input a pattern :")

				a													=			"aab"
				a													=			"c*a*b"

				# using function to check whether given string matches the given pattern
				if match_pattern(input_string, pattern):
								print(F'{input_string} matches the given pattern {pattern}')
				else:
								print(F'{input_string} does not match with the given pattern {pattern}')