






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

	def __init__(      self   :						Any  ,					UpperCAmelCase_   :						int  ,					UpperCAmelCase_   :						int  ,					UpperCAmelCase_   :						int  ,					UpperCAmelCase_   :						int=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  ,					):
			super().__init__()
			UpperCamelCase__				:					Tuple					=					only_cross_attention

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

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

			# 3. Feed-forward
			UpperCamelCase__				:					Dict					=					nn.LayerNorm(__lowerCamelCase  ,					elementwise_affine=__lowerCamelCase)
			UpperCamelCase__				:					int					=					FeedForward(__lowerCamelCase  ,					dropout=__lowerCamelCase  ,					activation_fn=__lowerCamelCase  ,					final_dropout=__lowerCamelCase)

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

	def      __UpperCamelCase							(      self   :						int  ,					UpperCAmelCase_   :						Optional[int]  ,					UpperCAmelCase_   :						int):
			# Sets chunk feed-forward
			UpperCamelCase__				:					Tuple					=					chunk_size
			UpperCamelCase__				:					Union[str, Any]					=					dim





	def      __UpperCamelCase							(      self   :						Any  ,					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  ,					):
			# Notice that normalization is always applied before the real computation in the following blocks.
			# 1. Self-Attention
			if self.use_ada_layer_norm:
					UpperCamelCase__				:					Tuple					=					self.norma(__lowerCamelCase  ,					__lowerCamelCase)
			elif self.use_ada_layer_norm_zero:
					UpperCamelCase__				:					Dict					=					self.norma(
					    __lowerCamelCase  ,					__lowerCamelCase  ,					__lowerCamelCase  ,					hidden_dtype=hidden_states.dtype)
			else:
					UpperCamelCase__				:					Union[str, Any]					=					self.norma(__lowerCamelCase)

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

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

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

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

			# 3. Feed-forward
			UpperCamelCase__				:					Union[str, Any]					=					self.norma(__lowerCamelCase)

			if self.use_ada_layer_norm_zero:
					UpperCamelCase__				:					Dict					=					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__				:					str					=					norm_hidden_states.shape[self._chunk_dim] // self._chunk_size
					UpperCamelCase__				:					Dict					=					torch.cat(
					    [self.ff(__lowerCamelCase) for hid_slice in norm_hidden_states.chunk(__lowerCamelCase  ,					dim=self._chunk_dim)]  ,					dim=self._chunk_dim  ,					)
			else:
					UpperCamelCase__				:					List[str]					=					self.ff(__lowerCamelCase)

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

			UpperCamelCase__				:					Union[str, Any]					=					ff_output + hidden_states

			return hidden_states







class       __lowercase					(nn.Module    ):

	def __init__(      self   :						int  ,					UpperCAmelCase_   :						int  ,					UpperCAmelCase_   :						Optional[int] = None  ,					UpperCAmelCase_   :						int = 4  ,					UpperCAmelCase_   :						float = 0.0  ,					UpperCAmelCase_   :						str = "geglu"  ,					UpperCAmelCase_   :						bool = False  ,					):
			super().__init__()
			UpperCamelCase__				:					List[str]					=					int(dim * mult)
			UpperCamelCase__				:					Tuple					=					dim_out if dim_out is not None else dim

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

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





	def      __UpperCamelCase							(      self   :						Union[str, Any]  ,					UpperCAmelCase_   :						Optional[int]):
			for module in self.net:
					UpperCamelCase__				:					Optional[int]					=					module(__lowerCamelCase)
			return hidden_states







class       __lowercase					(nn.Module    ):

	def __init__(      self   :						Optional[int]  ,					UpperCAmelCase_   :						int  ,					UpperCAmelCase_   :						int  ,					UpperCAmelCase_   :						str = "none"):
			super().__init__()
			UpperCamelCase__				:					List[Any]					=					nn.Linear(__lowerCamelCase  ,					__lowerCamelCase)
			UpperCamelCase__				:					int					=					approximate

	def      __UpperCamelCase							(      self   :						Optional[Any]  ,					UpperCAmelCase_   :						str):
			if gate.device.type != "mps":
					return F.gelu(__lowerCamelCase  ,					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   :						List[Any]  ,					UpperCAmelCase_   :						str):
			UpperCamelCase__				:					List[Any]					=					self.proj(__lowerCamelCase)
			UpperCamelCase__				:					Optional[int]					=					self.gelu(__lowerCamelCase)
			return hidden_states







class       __lowercase					(nn.Module    ):

	def __init__(      self   :						List[str]  ,					UpperCAmelCase_   :						int  ,					UpperCAmelCase_   :						int):
			super().__init__()
			UpperCamelCase__				:					str					=					nn.Linear(__lowerCamelCase  ,					dim_out * 2)

	def      __UpperCamelCase							(      self   :						List[str]  ,					UpperCAmelCase_   :						Union[str, Any]):
			if gate.device.type != "mps":
					return F.gelu(__lowerCamelCase)
			# mps: gelu is not implemented for float16
			return F.gelu(gate.to(dtype=torch.floataa)).to(dtype=gate.dtype)





	def      __UpperCamelCase							(      self   :						Optional[int]  ,					UpperCAmelCase_   :						Tuple):
			UpperCamelCase__				:					Union[str, Any]					=					self.proj(__lowerCamelCase).chunk(2  ,					dim=-1)
			return hidden_states * self.gelu(__lowerCamelCase)







class       __lowercase					(nn.Module    ):

	def __init__(      self   :						Tuple  ,					UpperCAmelCase_   :						int  ,					UpperCAmelCase_   :						int):
			super().__init__()
			UpperCamelCase__				:					int					=					nn.Linear(__lowerCamelCase  ,					__lowerCamelCase)





	def      __UpperCamelCase							(      self   :						Optional[Any]  ,					UpperCAmelCase_   :						Union[str, Any]):
			UpperCamelCase__				:					Tuple					=					self.proj(__lowerCamelCase)
			return x * torch.sigmoid(1.7_02 * x)







class       __lowercase					(nn.Module    ):

	def __init__(      self   :						List[str]  ,					UpperCAmelCase_   :						Union[str, Any]  ,					UpperCAmelCase_   :						Optional[int]):
			super().__init__()
			UpperCamelCase__				:					List[str]					=					nn.Embedding(__lowerCamelCase  ,					__lowerCamelCase)
			UpperCamelCase__				:					Dict					=					nn.SiLU()
			UpperCamelCase__				:					Any					=					nn.Linear(__lowerCamelCase  ,					embedding_dim * 2)
			UpperCamelCase__				:					Optional[Any]					=					nn.LayerNorm(__lowerCamelCase  ,					elementwise_affine=__lowerCamelCase)





	def      __UpperCamelCase							(      self   :						Optional[int]  ,					UpperCAmelCase_   :						Any  ,					UpperCAmelCase_   :						Any):
			UpperCamelCase__				:					Optional[int]					=					self.linear(self.silu(self.emb(__lowerCamelCase)))
			UpperCamelCase__				:					str					=					torch.chunk(__lowerCamelCase  ,					2)
			UpperCamelCase__				:					Optional[int]					=					self.norm(__lowerCamelCase) * (1 + scale) + shift
			return x







class       __lowercase					(nn.Module    ):

	def __init__(      self   :						Union[str, Any]  ,					UpperCAmelCase_   :						Optional[Any]  ,					UpperCAmelCase_   :						Union[str, Any]):
			super().__init__()

			UpperCamelCase__				:					Dict					=					CombinedTimestepLabelEmbeddings(__lowerCamelCase  ,					__lowerCamelCase)

			UpperCamelCase__				:					Dict					=					nn.SiLU()
			UpperCamelCase__				:					Any					=					nn.Linear(__lowerCamelCase  ,					6 * embedding_dim  ,					bias=__lowerCamelCase)
			UpperCamelCase__				:					Tuple					=					nn.LayerNorm(__lowerCamelCase  ,					elementwise_affine=__lowerCamelCase  ,					eps=1e-6)





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







class       __lowercase					(nn.Module    ):

	def __init__(      self   :						str  ,					UpperCAmelCase_   :						int  ,					UpperCAmelCase_   :						int  ,					UpperCAmelCase_   :						int  ,					UpperCAmelCase_   :						Optional[str] = None  ,					UpperCAmelCase_   :						float = 1e-5):
			super().__init__()
			UpperCamelCase__				:					Dict					=					num_groups
			UpperCamelCase__				:					List[str]					=					eps

			if act_fn is None:
					UpperCamelCase__				:					List[str]					=					None
			else:
					UpperCamelCase__				:					Optional[int]					=					get_activation(__lowerCamelCase)

			UpperCamelCase__				:					List[Any]					=					nn.Linear(__lowerCamelCase  ,					out_dim * 2)





	def      __UpperCamelCase							(      self   :						str  ,					UpperCAmelCase_   :						Union[str, Any]  ,					UpperCAmelCase_   :						int):
			if self.act:
					UpperCamelCase__				:					int					=					self.act(__lowerCamelCase)
			UpperCamelCase__				:					Optional[int]					=					self.linear(__lowerCamelCase)
			UpperCamelCase__				:					Any					=					emb[:, :, None, None]
			UpperCamelCase__				:					Union[str, Any]					=					emb.chunk(2  ,					dim=1)

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












'''simple docstring'''




import requests
from bsa import BeautifulSoup







def       __UpperCAmelCase						(  lowerCamelCase_ = "AAPL")							->     str:
		UpperCamelCase__				:					str					=					f'https://in.finance.yahoo.com/quote/{symbol}?s={symbol}'
		UpperCamelCase__				:					Optional[Any]					=					BeautifulSoup(requests.get(lowerCamelCase_).text     ,      'html.parser')
		UpperCamelCase__				:					Union[str, Any]					=					'My(6px) Pos(r) smartphone_Mt(6px)'
		return soup.find('div'     ,      class_=class_).find('span').text


if __name__ == "__main__":
		for symbol in "AAPL AMZN IBM GOOG MSFT ORCL".split():
				print(f'''Current {symbol:<4} stock price is {stock_price(symbol):>8}''')




