



import math
from collections import OrderedDict

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

from .utils import logging


A__						:						int          =      logging.get_logger(__name__)



class        __snake_case		(  nn.Module		):




							def __init__( self  : int):
									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      UpperCAmelCase__    ( self  : Dict       ,       A_  : Tensor):
									return nn.functional.gelu(A_       ,       approximate='''tanh''')





class        __snake_case		(  nn.Module		):




							def      UpperCAmelCase__    ( self  : Optional[Any]       ,       A_  : Tensor):
									return 0.5 * input * (1.0 + torch.tanh(math.sqrt(2.0 / math.pi) * (input + 0.04_4715 * torch.pow(A_       ,       3.0))))





class        __snake_case		(  nn.Module		):




							def __init__( self  : Union[str, Any]       ,       A_  : bool = False):
									super().__init__()
									if use_gelu_python:
											lowerCAmelCase_    :  Tuple             =				self._gelu_python
									else:
											lowerCAmelCase_    :  Optional[int]             =				nn.functional.gelu




							def      UpperCAmelCase__    ( self  : str       ,       A_  : Tensor):
									return input * 0.5 * (1.0 + torch.erf(input / math.sqrt(2.0)))







							def      UpperCAmelCase__    ( self  : Union[str, Any]       ,       A_  : Tensor):
									return self.act(A_)





class        __snake_case		(  nn.Module		):




							def      UpperCAmelCase__    ( self  : Union[str, Any]       ,       A_  : Tensor):
									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      UpperCAmelCase__    ( self  : int       ,       A_  : Tensor):
									return input * torch.sigmoid(1.702 * input)





class        __snake_case		(  nn.Module		):




							def __init__( self  : List[Any]       ,       A_  : float       ,       A_  : float):
									if min > max:
											raise ValueError(F"""min should be < max (got min: {min}, max: {max})""")

									super().__init__()
									lowerCAmelCase_    :  str             =				min
									lowerCAmelCase_    :  List[str]             =				max







							def      UpperCAmelCase__    ( self  : List[str]       ,       A_  : Tensor):
									return torch.clip(gelu(A_)       ,       self.min       ,       self.max)





class        __snake_case		(  nn.Module		):




							def __init__( self  : Dict):
									super().__init__()
									lowerCAmelCase_    :  List[str]             =				math.sqrt(2 / math.pi)







							def      UpperCAmelCase__    ( self  : str       ,       A_  : Tensor):
									return 0.5 * input * (1 + torch.tanh(self.precomputed_constant * (input + 0.04_4715 * torch.pow(A_       ,       3))))





class        __snake_case		(  nn.Module		):




							def      UpperCAmelCase__    ( self  : List[str]       ,       A_  : Tensor):
									return nn.functional.silu(A_)





class        __snake_case		(  nn.Module		):




							def __init__( self  : int):
									super().__init__()
									if version.parse(torch.__version__) < version.parse('''1.9.0'''):
											lowerCAmelCase_    :  List[Any]             =				self._mish_python
									else:
											lowerCAmelCase_    :  List[Any]             =				nn.functional.mish




							def      UpperCAmelCase__    ( self  : Any       ,       A_  : Tensor):
									return input * torch.tanh(nn.functional.softplus(A_))







							def      UpperCAmelCase__    ( self  : Tuple       ,       A_  : Tensor):
									return self.act(A_)





class        __snake_case		(  nn.Module		):




							def      UpperCAmelCase__    ( self  : int       ,       A_  : Tensor):
									return input





class        __snake_case		(  nn.Module		):




							def      UpperCAmelCase__    ( self  : List[str]       ,       A_  : Tuple       ,       A_  : Optional[Any]=0.70_7107       ,       A_  : Tuple=0.28_2095):
									lowerCAmelCase_    :  int             =				(input - mu).div(sigma * math.sqrt(2.0))
									return 0.5 * (1.0 + torch.erf(A_))





class        __snake_case		(  nn.Module		):




							def      UpperCAmelCase__    ( self  : Tuple       ,       A_  : List[Any]):
									lowerCAmelCase_    :  Optional[Any]             =				nn.functional.relu(A_)
									lowerCAmelCase_    :  Optional[int]             =				torch.square(A_)
									return squared





class        __snake_case		(  UpperCamelCase_		):




							def __getitem__( self  : List[Any]       ,       A_  : str):
									lowerCAmelCase_    :  Union[str, Any]             =				super().__getitem__(A_)
									lowerCAmelCase_      ,						lowerCAmelCase_    :  Optional[int]             =				content if isinstance(A_       ,       A_) else (content, {})
									return cls(**A_)





A__						:						int          =      {
    '''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,
}
A__						:						int          =      ClassInstantier(ACTaCLS)


def     UpperCamelCase(						__UpperCamelCase							:	Union[str, Any]				):
		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
A__						:						Optional[int]          =      get_activation('''gelu_python''')
A__						:						Tuple          =      get_activation('''gelu_new''')
A__						:						List[str]          =      get_activation('''gelu''')
A__						:						Any          =      get_activation('''gelu_fast''')
A__						:						Dict          =      get_activation('''quick_gelu''')
A__						:						List[str]          =      get_activation('''silu''')
A__						:						Any          =      get_activation('''mish''')





A__						:						List[str]          =      get_activation('''linear''')










import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
						import torch

if is_vision_available():
						from PIL import Image

						from transformers import DeiTImageProcessor



class        __snake_case		(  unittest.TestCase		):




							def __init__( self  : Union[str, Any]       ,       A_  : Optional[Any]       ,       A_  : Optional[Any]=7       ,       A_  : str=3       ,       A_  : Optional[Any]=1_8       ,       A_  : Union[str, Any]=3_0       ,       A_  : str=4_0_0       ,       A_  : List[str]=True       ,       A_  : str=None       ,       A_  : str=True       ,       A_  : List[Any]=None       ,       A_  : List[Any]=True       ,       A_  : Optional[Any]=[0.5, 0.5, 0.5]       ,       A_  : Optional[int]=[0.5, 0.5, 0.5]       ,       ):
									lowerCAmelCase_    :  List[str]             =				size if size is not None else {'''height''': 2_0, '''width''': 2_0}
									lowerCAmelCase_    :  List[Any]             =				crop_size if crop_size is not None else {'''height''': 1_8, '''width''': 1_8}

									lowerCAmelCase_    :  str             =				parent
									lowerCAmelCase_    :  Union[str, Any]             =				batch_size
									lowerCAmelCase_    :  Optional[Any]             =				num_channels
									lowerCAmelCase_    :  List[str]             =				image_size
									lowerCAmelCase_    :  List[str]             =				min_resolution
									lowerCAmelCase_    :  Union[str, Any]             =				max_resolution
									lowerCAmelCase_    :  Optional[int]             =				do_resize
									lowerCAmelCase_    :  Dict             =				size
									lowerCAmelCase_    :  Dict             =				do_center_crop
									lowerCAmelCase_    :  str             =				crop_size
									lowerCAmelCase_    :  List[Any]             =				do_normalize
									lowerCAmelCase_    :  str             =				image_mean
									lowerCAmelCase_    :  Dict             =				image_std







							def      UpperCAmelCase__    ( self  : int):
									return {
									    "do_resize": self.do_resize,
									    "size": self.size,
									    "do_center_crop": self.do_center_crop,
									    "crop_size": self.crop_size,
									    "do_normalize": self.do_normalize,
									    "image_mean": self.image_mean,
									    "image_std": self.image_std,
									}





@require_torch
@require_vision
class        __snake_case		(  UpperCamelCase_		,unittest.TestCase		):
							_a       =				DeiTImageProcessor if is_vision_available() else None
							_a       =				True




							def      UpperCAmelCase__    ( self  : Tuple):
									lowerCAmelCase_    :  int             =				DeiTImageProcessingTester(self)




							@property
							def      UpperCAmelCase__    ( self  : Optional[Any]):
									return self.image_processor_tester.prepare_image_processor_dict()




							def      UpperCAmelCase__    ( self  : str):
									lowerCAmelCase_    :  List[Any]             =				self.image_processing_class(**self.image_processor_dict)
									self.assertTrue(hasattr(A_       ,       '''do_resize'''))
									self.assertTrue(hasattr(A_       ,       '''size'''))
									self.assertTrue(hasattr(A_       ,       '''do_center_crop'''))
									self.assertTrue(hasattr(A_       ,       '''center_crop'''))
									self.assertTrue(hasattr(A_       ,       '''do_normalize'''))
									self.assertTrue(hasattr(A_       ,       '''image_mean'''))
									self.assertTrue(hasattr(A_       ,       '''image_std'''))




							def      UpperCAmelCase__    ( self  : Union[str, Any]):
									lowerCAmelCase_    :  Tuple             =				self.image_processing_class.from_dict(self.image_processor_dict)
									self.assertEqual(image_processor.size       ,       {'''height''': 2_0, '''width''': 2_0})
									self.assertEqual(image_processor.crop_size       ,       {'''height''': 1_8, '''width''': 1_8})

									lowerCAmelCase_    :  Any             =				self.image_processing_class.from_dict(self.image_processor_dict       ,       size=4_2       ,       crop_size=8_4)
									self.assertEqual(image_processor.size       ,       {'''height''': 4_2, '''width''': 4_2})
									self.assertEqual(image_processor.crop_size       ,       {'''height''': 8_4, '''width''': 8_4})




							def      UpperCAmelCase__    ( self  : Union[str, Any]):
									pass




							def      UpperCAmelCase__    ( self  : Any):
									# Initialize image_processing
									lowerCAmelCase_    :  List[str]             =				self.image_processing_class(**self.image_processor_dict)
									# create random PIL images
									lowerCAmelCase_    :  Any             =				prepare_image_inputs(self.image_processor_tester       ,       equal_resolution=A_)
									for image in image_inputs:
											self.assertIsInstance(A_       ,       Image.Image)

									# Test not batched input
									lowerCAmelCase_    :  Union[str, Any]             =				image_processing(image_inputs[0]       ,       return_tensors='''pt''').pixel_values
									self.assertEqual(
									    encoded_images.shape       ,       (
									        1,
									        self.image_processor_tester.num_channels,
									        self.image_processor_tester.crop_size['''height'''],
									        self.image_processor_tester.crop_size['''width'''],
									    )       ,       )

									# Test batched
									lowerCAmelCase_    :  Dict             =				image_processing(A_       ,       return_tensors='''pt''').pixel_values
									self.assertEqual(
									    encoded_images.shape       ,       (
									        self.image_processor_tester.batch_size,
									        self.image_processor_tester.num_channels,
									        self.image_processor_tester.crop_size['''height'''],
									        self.image_processor_tester.crop_size['''width'''],
									    )       ,       )




							def      UpperCAmelCase__    ( self  : List[str]):
									# Initialize image_processing
									lowerCAmelCase_    :  Union[str, Any]             =				self.image_processing_class(**self.image_processor_dict)
									# create random numpy tensors
									lowerCAmelCase_    :  Union[str, Any]             =				prepare_image_inputs(self.image_processor_tester       ,       equal_resolution=A_       ,       numpify=A_)
									for image in image_inputs:
											self.assertIsInstance(A_       ,       np.ndarray)

									# Test not batched input
									lowerCAmelCase_    :  Optional[Any]             =				image_processing(image_inputs[0]       ,       return_tensors='''pt''').pixel_values
									self.assertEqual(
									    encoded_images.shape       ,       (
									        1,
									        self.image_processor_tester.num_channels,
									        self.image_processor_tester.crop_size['''height'''],
									        self.image_processor_tester.crop_size['''width'''],
									    )       ,       )

									# Test batched
									lowerCAmelCase_    :  str             =				image_processing(A_       ,       return_tensors='''pt''').pixel_values
									self.assertEqual(
									    encoded_images.shape       ,       (
									        self.image_processor_tester.batch_size,
									        self.image_processor_tester.num_channels,
									        self.image_processor_tester.crop_size['''height'''],
									        self.image_processor_tester.crop_size['''width'''],
									    )       ,       )







							def      UpperCAmelCase__    ( self  : Any):
									# Initialize image_processing
									lowerCAmelCase_    :  str             =				self.image_processing_class(**self.image_processor_dict)
									# create random PyTorch tensors
									lowerCAmelCase_    :  Union[str, Any]             =				prepare_image_inputs(self.image_processor_tester       ,       equal_resolution=A_       ,       torchify=A_)
									for image in image_inputs:
											self.assertIsInstance(A_       ,       torch.Tensor)

									# Test not batched input
									lowerCAmelCase_    :  Dict             =				image_processing(image_inputs[0]       ,       return_tensors='''pt''').pixel_values
									self.assertEqual(
									    encoded_images.shape       ,       (
									        1,
									        self.image_processor_tester.num_channels,
									        self.image_processor_tester.crop_size['''height'''],
									        self.image_processor_tester.crop_size['''width'''],
									    )       ,       )

									# Test batched
									lowerCAmelCase_    :  Tuple             =				image_processing(A_       ,       return_tensors='''pt''').pixel_values
									self.assertEqual(
									    encoded_images.shape       ,       (
									        self.image_processor_tester.batch_size,
									        self.image_processor_tester.num_channels,
									        self.image_processor_tester.crop_size['''height'''],
									        self.image_processor_tester.crop_size['''width'''],
									    )       ,       )





