


import math
from collections import OrderedDict

import torch
from packaging import version
from torch import Tensor, nn

from .utils import logging


SCREAMING_SNAKE_CASE : Optional[int]          =    logging.get_logger(__name__)





class 					snake_case__  ( nn.Module       ):





	def __init__(  self							)		->			Tuple:
								"""simple docstring"""






								super().__init__()
								if version.parse(torch.__version__							) < version.parse("""1.12.0"""							):
															raise ImportError(
															    f"""You are using torch=={torch.__version__}, but torch>=1.12.0 is required to use """
															    """PytorchGELUTanh. Please upgrade torch."""							)





	def     A				(  self				,    UpperCamelCase_							)		->			Tensor:
								"""simple docstring"""






								return nn.functional.gelu(UpperCamelCase_				,    approximate="""tanh"""							)





class 					snake_case__  ( nn.Module       ):





	def     A				(  self				,    UpperCamelCase_							)		->			Tensor:
								"""simple docstring"""






								return 0.5 * input * (1.0 + torch.tanh(math.sqrt(2.0 / math.pi							) * (input + 0.04_4715 * torch.pow(UpperCamelCase_				,    3.0							))							))





class 					snake_case__  ( nn.Module       ):





	def __init__(  self				,    UpperCamelCase_ = False							)		->			int:
								"""simple docstring"""






								super().__init__()
								if use_gelu_python:
															a_       :   str			=	self._gelu_python
								else:
															a_       :   Any			=	nn.functional.gelu





	def     A				(  self				,    UpperCamelCase_							)		->			Tensor:
								"""simple docstring"""






								return input * 0.5 * (1.0 + torch.erf(input / math.sqrt(2.0							)							))





	def     A				(  self				,    UpperCamelCase_							)		->			Tensor:
								"""simple docstring"""






								return self.act(UpperCamelCase_							)





class 					snake_case__  ( nn.Module       ):





	def     A				(  self				,    UpperCamelCase_							)		->			Tensor:
								"""simple docstring"""






								return 0.5 * input * (1.0 + torch.tanh(input * 0.79_7884_5608 * (1.0 + 0.04_4715 * input * input)							))





class 					snake_case__  ( nn.Module       ):





	def     A				(  self				,    UpperCamelCase_							)		->			Tensor:
								"""simple docstring"""






								return input * torch.sigmoid(1.702 * input							)





class 					snake_case__  ( nn.Module       ):





	def __init__(  self				,    UpperCamelCase_				,    UpperCamelCase_							)		->			Tuple:
								"""simple docstring"""






								if min > max:
															raise ValueError(f"""min should be < max (got min: {min}, max: {max})"""							)

								super().__init__()
								a_       :   Any			=	min
								a_       :   Dict			=	max





	def     A				(  self				,    UpperCamelCase_							)		->			Tensor:
								"""simple docstring"""






								return torch.clip(gelu(UpperCamelCase_							)				,    self.min				,    self.max							)





class 					snake_case__  ( nn.Module       ):





	def __init__(  self							)		->			Optional[Any]:
								"""simple docstring"""






								super().__init__()
								a_       :   Union[str, Any]			=	math.sqrt(2 / math.pi							)





	def     A				(  self				,    UpperCamelCase_							)		->			Tensor:
								"""simple docstring"""






								return 0.5 * input * (1 + torch.tanh(self.precomputed_constant * (input + 0.04_4715 * torch.pow(UpperCamelCase_				,    3							))							))





class 					snake_case__  ( nn.Module       ):





	def     A				(  self				,    UpperCamelCase_							)		->			Tensor:
								"""simple docstring"""






								return nn.functional.silu(UpperCamelCase_							)





class 					snake_case__  ( nn.Module       ):





	def __init__(  self							)		->			str:
								"""simple docstring"""






								super().__init__()
								if version.parse(torch.__version__							) < version.parse("""1.9.0"""							):
															a_       :   Tuple			=	self._mish_python
								else:
															a_       :   List[str]			=	nn.functional.mish





	def     A				(  self				,    UpperCamelCase_							)		->			Tensor:
								"""simple docstring"""






								return input * torch.tanh(nn.functional.softplus(UpperCamelCase_							)							)





	def     A				(  self				,    UpperCamelCase_							)		->			Tensor:
								"""simple docstring"""






								return self.act(UpperCamelCase_							)





class 					snake_case__  ( nn.Module       ):





	def     A				(  self				,    UpperCamelCase_							)		->			Tensor:
								"""simple docstring"""






								return input





class 					snake_case__  ( nn.Module       ):





	def     A				(  self				,    UpperCamelCase_				,    UpperCamelCase_=0.70_7107				,    UpperCamelCase_=0.28_2095							)		->			List[Any]:
								"""simple docstring"""






								a_       :   List[str]			=	(input - mu).div(sigma * math.sqrt(2.0							)							)
								return 0.5 * (1.0 + torch.erf(UpperCamelCase_							))





class 					snake_case__  ( nn.Module       ):





	def     A				(  self				,    UpperCamelCase_							)		->			Union[str, Any]:
								"""simple docstring"""






								a_       :   List[str]			=	nn.functional.relu(UpperCamelCase_							)
								a_       :   Optional[Any]			=	torch.square(UpperCamelCase_							)
								return squared





class 					snake_case__  ( __A       ):





	def __getitem__(  self				,    UpperCamelCase_							)		->			List[Any]:
								"""simple docstring"""






								a_       :   List[str]			=	super().__getitem__(UpperCamelCase_							)
								a_		,				a_       :   Any			=	content if isinstance(UpperCamelCase_				,    UpperCamelCase_							) else (content, {})
								return cls(**UpperCamelCase_							)





SCREAMING_SNAKE_CASE : List[Any]          =    {
    "gelu": GELUActivation,
    "gelu_10": (ClippedGELUActivation, {"min": -10, "max": 10}),
    "gelu_fast": FastGELUActivation,
    "gelu_new": NewGELUActivation,
    "gelu_python": (GELUActivation, {"use_gelu_python": True}),
    "gelu_pytorch_tanh": PytorchGELUTanh,
    "gelu_accurate": AccurateGELUActivation,
    "laplace": LaplaceActivation,
    "linear": LinearActivation,
    "mish": MishActivation,
    "quick_gelu": QuickGELUActivation,
    "relu": nn.ReLU,
    "relu2": ReLUSquaredActivation,
    "relu6": nn.ReLUa,
    "sigmoid": nn.Sigmoid,
    "silu": SiLUActivation,
    "swish": SiLUActivation,
    "tanh": nn.Tanh,
}
SCREAMING_SNAKE_CASE : Dict          =    ClassInstantier(ACTaCLS)





def 						_lowerCamelCase		(							SCREAMING_SNAKE_CASE_				: List[str]      ):






							"""simple docstring"""



							if activation_string in ACTaFN:
														return ACTaFN[activation_string]
							else:
														raise KeyError(F"""function {activation_string} not found in ACT2FN mapping {list(ACTaFN.keys()      )}"""      )


# For backwards compatibility with: from activations import gelu_python
SCREAMING_SNAKE_CASE : Any          =    get_activation("gelu_python")
SCREAMING_SNAKE_CASE : List[Any]          =    get_activation("gelu_new")
SCREAMING_SNAKE_CASE : List[str]          =    get_activation("gelu")
SCREAMING_SNAKE_CASE : Optional[Any]          =    get_activation("gelu_fast")
SCREAMING_SNAKE_CASE : Dict          =    get_activation("quick_gelu")
SCREAMING_SNAKE_CASE : Union[str, Any]          =    get_activation("silu")
SCREAMING_SNAKE_CASE : Union[str, Any]          =    get_activation("mish")

SCREAMING_SNAKE_CASE : int          =    get_activation("linear")






from collections import OrderedDict
from typing import Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


SCREAMING_SNAKE_CASE : Dict          =    logging.get_logger(__name__)

SCREAMING_SNAKE_CASE : str          =    {
    "apple/mobilevit-small": "https://huggingface.co/apple/mobilevit-small/resolve/main/config.json",
    "apple/mobilevit-x-small": "https://huggingface.co/apple/mobilevit-x-small/resolve/main/config.json",
    "apple/mobilevit-xx-small": "https://huggingface.co/apple/mobilevit-xx-small/resolve/main/config.json",
    "apple/deeplabv3-mobilevit-small": (
        "https://huggingface.co/apple/deeplabv3-mobilevit-small/resolve/main/config.json"
    ),
    "apple/deeplabv3-mobilevit-x-small": (
        "https://huggingface.co/apple/deeplabv3-mobilevit-x-small/resolve/main/config.json"
    ),
    "apple/deeplabv3-mobilevit-xx-small": (
        "https://huggingface.co/apple/deeplabv3-mobilevit-xx-small/resolve/main/config.json"
    ),
    # See all MobileViT models at https://huggingface.co/models?filter=mobilevit
}





class 					snake_case__  ( __A       ):
	UpperCAmelCase   :							List[str]         = """mobilevit"""





	def __init__(  self				,    UpperCamelCase_=3				,    UpperCamelCase_=256				,    UpperCamelCase_=2				,    UpperCamelCase_=[144, 192, 240]				,    UpperCamelCase_=[16, 32, 64, 96, 128, 160, 640]				,    UpperCamelCase_=4				,    UpperCamelCase_=2.0				,    UpperCamelCase_=4.0				,    UpperCamelCase_="silu"				,    UpperCamelCase_=3				,    UpperCamelCase_=32				,    UpperCamelCase_=0.1				,    UpperCamelCase_=0.0				,    UpperCamelCase_=0.1				,    UpperCamelCase_=0.02				,    UpperCamelCase_=1e-5				,    UpperCamelCase_=True				,    UpperCamelCase_=256				,    UpperCamelCase_=[6, 12, 18]				,    UpperCamelCase_=0.1				,    UpperCamelCase_=255				,    **UpperCamelCase_				,    )		->			str:
								"""simple docstring"""






								super().__init__(**UpperCamelCase_							)

								a_       :   Optional[int]			=	num_channels
								a_       :   Optional[Any]			=	image_size
								a_       :   int			=	patch_size
								a_       :   List[str]			=	hidden_sizes
								a_       :   Optional[int]			=	neck_hidden_sizes
								a_       :   List[Any]			=	num_attention_heads
								a_       :   Union[str, Any]			=	mlp_ratio
								a_       :   List[str]			=	expand_ratio
								a_       :   str			=	hidden_act
								a_       :   Union[str, Any]			=	conv_kernel_size
								a_       :   Optional[Any]			=	output_stride
								a_       :   Union[str, Any]			=	hidden_dropout_prob
								a_       :   List[Any]			=	attention_probs_dropout_prob
								a_       :   List[Any]			=	classifier_dropout_prob
								a_       :   Union[str, Any]			=	initializer_range
								a_       :   List[Any]			=	layer_norm_eps
								a_       :   str			=	qkv_bias

								# decode head attributes for semantic segmentation
								a_       :   Optional[Any]			=	aspp_out_channels
								a_       :   Any			=	atrous_rates
								a_       :   Union[str, Any]			=	aspp_dropout_prob
								a_       :   Any			=	semantic_loss_ignore_index





class 					snake_case__  ( __A       ):
	UpperCAmelCase   :							Optional[int]         = version.parse("""1.11"""       )





	@property
	def     A				(  self							)		->			Mapping[str, Mapping[int, str]]:
								"""simple docstring"""






								return OrderedDict([("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""})]							)





	@property
	def     A				(  self							)		->			Mapping[str, Mapping[int, str]]:
								"""simple docstring"""






								if self.task == "image-classification":
															return OrderedDict([("""logits""", {0: """batch"""})]							)
								else:
															return OrderedDict([("""last_hidden_state""", {0: """batch"""}), ("""pooler_output""", {0: """batch"""})]							)





	@property
	def     A				(  self							)		->			float:
								"""simple docstring"""






								return 1e-4


