


import json
import os
import tempfile

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


if is_torch_available():
   import numpy as np
   import torch

if is_vision_available():
   from PIL import Image



def      __lowerCAmelCase    (				snake_case						:     int    ,   snake_case						:     Optional[int]=False    ,   snake_case						:     str=False    ,   snake_case						:     Union[str, Any]=False			)							->				str:
   assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

   __lowerCamelCase:					Any	 =						[]
   for i in range(image_processor_tester.batch_size			):
      if equal_resolution:
         __lowerCamelCase:					str	 =						image_processor_tester.max_resolution
      else:
         # To avoid getting image width/height 0
         __lowerCamelCase:					int	 =						image_processor_tester.min_resolution
         if getattr(snake_case    ,   """size_divisor"""    ,   snake_case			):
            # If `size_divisor` is defined, the image needs to have width/size >= `size_divisor`
            __lowerCamelCase:					List[str]	 =						max(image_processor_tester.size_divisor    ,   snake_case			)
         __lowerCamelCase ,  __lowerCamelCase:					Any	 =						np.random.choice(np.arange(snake_case    ,   image_processor_tester.max_resolution			)    ,   2			)
      image_inputs.append(
          np.random.randint(255    ,   size=(image_processor_tester.num_channels, width, height)    ,   dtype=np.uinta			)			)

   if not numpify and not torchify:
      # PIL expects the channel dimension as last dimension
      __lowerCamelCase:					Dict	 =						[Image.fromarray(np.moveaxis(snake_case    ,   0    ,   -1			)			) for image in image_inputs]

   if torchify:
      __lowerCamelCase:					str	 =						[torch.from_numpy(snake_case			) for image in image_inputs]

   return image_inputs



def      __lowerCAmelCase    (				snake_case						:     int    ,   snake_case						:     List[Any]=10    ,   snake_case						:     List[Any]=10    ,   snake_case						:     str=False    ,   snake_case						:     Any=False			)							->				str:
   __lowerCamelCase:					Union[str, Any]	 =						[]
   for i in range(image_processor_tester.num_frames			):
      video.append(np.random.randint(255    ,   size=(image_processor_tester.num_channels, width, height)    ,   dtype=np.uinta			)			)

   if not numpify and not torchify:
      # PIL expects the channel dimension as last dimension
      __lowerCamelCase:					Dict	 =						[Image.fromarray(np.moveaxis(snake_case    ,   0    ,   -1			)			) for frame in video]

   if torchify:
      __lowerCamelCase:					Optional[int]	 =						[torch.from_numpy(snake_case			) for frame in video]

   return video



def      __lowerCAmelCase    (				snake_case						:     List[Any]    ,   snake_case						:     Any=False    ,   snake_case						:     int=False    ,   snake_case						:     Tuple=False			)							->				Optional[int]:
   assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

   __lowerCamelCase:					str	 =						[]
   for i in range(image_processor_tester.batch_size			):
      if equal_resolution:
         __lowerCamelCase:					Tuple	 =						image_processor_tester.max_resolution
      else:
         __lowerCamelCase ,  __lowerCamelCase:					Any	 =						np.random.choice(
             np.arange(image_processor_tester.min_resolution    ,   image_processor_tester.max_resolution			)    ,   2			)
         __lowerCamelCase:					int	 =						prepare_video(
             image_processor_tester=snake_case    ,   width=snake_case    ,   height=snake_case    ,   numpify=snake_case    ,   torchify=snake_case    ,   )
      video_inputs.append(snake_case			)

   return video_inputs







class 							a       :
     UpperCAmelCase__			:	Optional[Any]       =						None





     def 			SCREAMING_SNAKE_CASE__							(     self       :					Union[str, Any]				):
        __lowerCamelCase:					Any	 =						self.image_processing_class(**self.image_processor_dict				)
        __lowerCamelCase:					Optional[Any]	 =						json.loads(image_processor.to_json_string()				)
        for key, value in self.image_processor_dict.items():
           self.assertEqual(obj[key]     ,						SCREAMING_SNAKE_CASE_				)





     def 			SCREAMING_SNAKE_CASE__							(     self       :					Optional[int]				):
        __lowerCamelCase:					List[str]	 =						self.image_processing_class(**self.image_processor_dict				)

        with tempfile.TemporaryDirectory() as tmpdirname:
           __lowerCamelCase:					Tuple	 =						os.path.join(SCREAMING_SNAKE_CASE_     ,						"""image_processor.json"""				)
           image_processor_first.to_json_file(SCREAMING_SNAKE_CASE_				)
           __lowerCamelCase:					Union[str, Any]	 =						self.image_processing_class.from_json_file(SCREAMING_SNAKE_CASE_				)

        self.assertEqual(image_processor_second.to_dict()     ,						image_processor_first.to_dict()				)





     def 			SCREAMING_SNAKE_CASE__							(     self       :					Any				):
        __lowerCamelCase:					Tuple	 =						self.image_processing_class(**self.image_processor_dict				)

        with tempfile.TemporaryDirectory() as tmpdirname:
           __lowerCamelCase:					Tuple	 =						image_processor_first.save_pretrained(SCREAMING_SNAKE_CASE_				)[0]
           check_json_file_has_correct_format(SCREAMING_SNAKE_CASE_				)
           __lowerCamelCase:					List[Any]	 =						self.image_processing_class.from_pretrained(SCREAMING_SNAKE_CASE_				)

        self.assertEqual(image_processor_second.to_dict()     ,						image_processor_first.to_dict()				)





     def 			SCREAMING_SNAKE_CASE__							(     self       :					Union[str, Any]				):
        __lowerCamelCase:					Union[str, Any]	 =						self.image_processing_class()
        self.assertIsNotNone(SCREAMING_SNAKE_CASE_				)







     @require_torch
     @require_vision
     def 			SCREAMING_SNAKE_CASE__							(     self       :					Optional[int]				):
        if self.test_cast_dtype is not None:
           # Initialize image_processor
           __lowerCamelCase:					Optional[Any]	 =						self.image_processing_class(**self.image_processor_dict				)

           # create random PyTorch tensors
           __lowerCamelCase:					Tuple	 =						prepare_image_inputs(self.image_processor_tester     ,						equal_resolution=SCREAMING_SNAKE_CASE_     ,						torchify=SCREAMING_SNAKE_CASE_				)

           __lowerCamelCase:					int	 =						image_processor(SCREAMING_SNAKE_CASE_     ,						return_tensors="""pt"""				)
           # for layoutLM compatiblity
           self.assertEqual(encoding.pixel_values.device     ,						torch.device("""cpu"""				)				)
           self.assertEqual(encoding.pixel_values.dtype     ,						torch.floataa				)

           __lowerCamelCase:					str	 =						image_processor(SCREAMING_SNAKE_CASE_     ,						return_tensors="""pt"""				).to(torch.floataa				)
           self.assertEqual(encoding.pixel_values.device     ,						torch.device("""cpu"""				)				)
           self.assertEqual(encoding.pixel_values.dtype     ,						torch.floataa				)

           __lowerCamelCase:					Any	 =						image_processor(SCREAMING_SNAKE_CASE_     ,						return_tensors="""pt"""				).to("""cpu"""     ,						torch.bfloataa				)
           self.assertEqual(encoding.pixel_values.device     ,						torch.device("""cpu"""				)				)
           self.assertEqual(encoding.pixel_values.dtype     ,						torch.bfloataa				)

           with self.assertRaises(SCREAMING_SNAKE_CASE_				):
              __lowerCamelCase:					Tuple	 =						image_processor(SCREAMING_SNAKE_CASE_     ,						return_tensors="""pt"""				).to(torch.bfloataa     ,						"""cpu"""				)

           # Try with text + image feature
           __lowerCamelCase:					List[str]	 =						image_processor(SCREAMING_SNAKE_CASE_     ,						return_tensors="""pt"""				)
           encoding.update({"""input_ids""": torch.LongTensor([[1, 2, 3], [4, 5, 6]]				)}				)
           __lowerCamelCase:					Optional[int]	 =						encoding.to(torch.floataa				)

           self.assertEqual(encoding.pixel_values.device     ,						torch.device("""cpu"""				)				)
           self.assertEqual(encoding.pixel_values.dtype     ,						torch.floataa				)
           self.assertEqual(encoding.input_ids.dtype     ,						torch.long				)






import math
from collections import OrderedDict

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

from .utils import logging


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



class 							a       (      nn.Module ):





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



class 							a       (      nn.Module ):





     def 			SCREAMING_SNAKE_CASE__							(     self       :					Any     ,						SCREAMING_SNAKE_CASE_       :					Tensor				):
        return 0.5 * input * (1.0 + torch.tanh(math.sqrt(2.0 / math.pi				) * (input + 0.04_4715 * torch.pow(SCREAMING_SNAKE_CASE_     ,						3.0				))				))



class 							a       (      nn.Module ):





     def __init__(     self       :					List[Any]     ,						SCREAMING_SNAKE_CASE_       :					bool = False				):
        super().__init__()
        if use_gelu_python:
           __lowerCamelCase:					Union[str, Any]	 =						self._gelu_python
        else:
           __lowerCamelCase:					Tuple	 =						nn.functional.gelu





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







     def 			SCREAMING_SNAKE_CASE__							(     self       :					Union[str, Any]     ,						SCREAMING_SNAKE_CASE_       :					Tensor				):
        return self.act(SCREAMING_SNAKE_CASE_				)



class 							a       (      nn.Module ):





     def 			SCREAMING_SNAKE_CASE__							(     self       :					List[str]     ,						SCREAMING_SNAKE_CASE_       :					Tensor				):
        return 0.5 * input * (1.0 + torch.tanh(input * 0.79_7884_5608 * (1.0 + 0.04_4715 * input * input)				))



class 							a       (      nn.Module ):





     def 			SCREAMING_SNAKE_CASE__							(     self       :					List[Any]     ,						SCREAMING_SNAKE_CASE_       :					Tensor				):
        return input * torch.sigmoid(1.702 * input				)



class 							a       (      nn.Module ):





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

        super().__init__()
        __lowerCamelCase:					Optional[int]	 =						min
        __lowerCamelCase:					str	 =						max







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



class 							a       (      nn.Module ):





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







     def 			SCREAMING_SNAKE_CASE__							(     self       :					Tuple     ,						SCREAMING_SNAKE_CASE_       :					Tensor				):
        return 0.5 * input * (1 + torch.tanh(self.precomputed_constant * (input + 0.04_4715 * torch.pow(SCREAMING_SNAKE_CASE_     ,						3				))				))



class 							a       (      nn.Module ):





     def 			SCREAMING_SNAKE_CASE__							(     self       :					Any     ,						SCREAMING_SNAKE_CASE_       :					Tensor				):
        return nn.functional.silu(SCREAMING_SNAKE_CASE_				)



class 							a       (      nn.Module ):





     def __init__(     self       :					Any				):
        super().__init__()
        if version.parse(torch.__version__				) < version.parse("""1.9.0"""				):
           __lowerCamelCase:					str	 =						self._mish_python
        else:
           __lowerCamelCase:					Any	 =						nn.functional.mish





     def 			SCREAMING_SNAKE_CASE__							(     self       :					int     ,						SCREAMING_SNAKE_CASE_       :					Tensor				):
        return input * torch.tanh(nn.functional.softplus(SCREAMING_SNAKE_CASE_				)				)







     def 			SCREAMING_SNAKE_CASE__							(     self       :					List[str]     ,						SCREAMING_SNAKE_CASE_       :					Tensor				):
        return self.act(SCREAMING_SNAKE_CASE_				)



class 							a       (      nn.Module ):





     def 			SCREAMING_SNAKE_CASE__							(     self       :					List[str]     ,						SCREAMING_SNAKE_CASE_       :					Tensor				):
        return input



class 							a       (      nn.Module ):





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



class 							a       (      nn.Module ):





     def 			SCREAMING_SNAKE_CASE__							(     self       :					int     ,						SCREAMING_SNAKE_CASE_       :					str				):
        __lowerCamelCase:					Any	 =						nn.functional.relu(SCREAMING_SNAKE_CASE_				)
        __lowerCamelCase:					List[str]	 =						torch.square(SCREAMING_SNAKE_CASE_				)
        return squared



class 							a       (      _UpperCAmelCase ):





     def __getitem__(     self       :					str     ,						SCREAMING_SNAKE_CASE_       :					Tuple				):
        __lowerCamelCase:					Optional[Any]	 =						super().__getitem__(SCREAMING_SNAKE_CASE_				)
        __lowerCamelCase ,  __lowerCamelCase:					Optional[Any]	 =						content if isinstance(SCREAMING_SNAKE_CASE_     ,						SCREAMING_SNAKE_CASE_				) else (content, {})
        return cls(**SCREAMING_SNAKE_CASE_				)



_A		:					Optional[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,
}
_A		:					Optional[int]          =							ClassInstantier(ACTaCLS)



def      __lowerCAmelCase    (				snake_case						:     Any			)							->				int:
   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		:					Dict          =							get_activation('''gelu_new''')
_A		:					List[str]          =							get_activation('''gelu''')
_A		:					Tuple          =							get_activation('''gelu_fast''')
_A		:					int          =							get_activation('''quick_gelu''')
_A		:					Tuple          =							get_activation('''silu''')
_A		:					Optional[Any]          =							get_activation('''mish''')







_A		:					Dict          =							get_activation('''linear''')


