



"""simple docstring"""




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


if is_torch_available():
  import torch

if is_vision_available():
  from PIL import Image

  from transformers import TvltImageProcessor



def  lowercase    (				SCREAMING_SNAKE_CASE__    :		Optional[Any]			,    SCREAMING_SNAKE_CASE__    :		Dict=10			,    SCREAMING_SNAKE_CASE__    :		Union[str, Any]=10			,    SCREAMING_SNAKE_CASE__    :		List[str]=False			,    SCREAMING_SNAKE_CASE__    :		Union[str, Any]=False			)					->       Union[str, Any]:
 _snake_case      :					List[str]					=   []
 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
  _snake_case      :					List[Any]					=   [Image.fromarray(np.moveaxis(lowerCamelCase_			,    0			,    -1			)			) for frame in video]

 if torchify:
  _snake_case      :					str					=   [torch.from_numpy(lowerCamelCase_			) for frame in video]

 return video



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

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

 return video_inputs







class 						snake_case  (		unittest.TestCase					):



   '''simple docstring'''






   def __init__(   self							:     Tuple			,						lowerCAmelCase							:     Union[str, Any]			,						lowerCAmelCase							:     Optional[int]=7			,						lowerCAmelCase							:     Union[str, Any]=3			,						lowerCAmelCase							:     List[Any]=4			,						lowerCAmelCase							:     Any=18			,						lowerCAmelCase							:     List[Any]=30			,						lowerCAmelCase							:     int=400			,						lowerCAmelCase							:     Any=True			,						lowerCAmelCase							:     Union[str, Any]=None			,						lowerCAmelCase							:     Tuple=True			,						lowerCAmelCase							:     List[Any]=[0.5, 0.5, 0.5]			,						lowerCAmelCase							:     List[Any]=[0.5, 0.5, 0.5]			,						lowerCAmelCase							:     Union[str, Any]=True			,						lowerCAmelCase							:     List[Any]=None			,						)			->			Dict:

    """simple docstring"""







    _snake_case      :					List[Any]					=   size if size is not None else {"""shortest_edge""": 18}
    _snake_case      :					int					=   crop_size if crop_size is not None else {"""height""": 18, """width""": 18}

    _snake_case      :					Optional[Any]					=   parent
    _snake_case      :					Dict					=   batch_size
    _snake_case      :					int					=   num_channels
    _snake_case      :					Tuple					=   num_frames
    _snake_case      :					Optional[int]					=   image_size
    _snake_case      :					int					=   min_resolution
    _snake_case      :					Any					=   max_resolution
    _snake_case      :					List[Any]					=   do_resize
    _snake_case      :					Any					=   size
    _snake_case      :					int					=   do_normalize
    _snake_case      :					Optional[int]					=   image_mean
    _snake_case      :					Optional[Any]					=   image_std
    _snake_case      :					Any					=   do_center_crop
    _snake_case      :					str					=   crop_size


   def        UpperCamelCase_					(   self							:     Union[str, Any])			->			List[Any]:

    """simple docstring"""







    return {
        "image_mean": self.image_mean,
        "image_std": self.image_std,
        "do_normalize": self.do_normalize,
        "do_resize": self.do_resize,
        "size": self.size,
        "do_center_crop": self.do_center_crop,
        "crop_size": self.crop_size,
    }







@require_torch
@require_vision
class 						snake_case  (		A__   ,unittest.TestCase					):



   '''simple docstring'''






   snake_case_   :	Optional[Any]       =						TvltImageProcessor if is_vision_available() else None

   def        UpperCamelCase_					(   self							:     Optional[int])			->			Optional[Any]:

    """simple docstring"""







    _snake_case      :					Tuple					=   TvltImageProcessorTester(self)

   @property
   def        UpperCamelCase_					(   self							:     Tuple)			->			Tuple:

    """simple docstring"""







    return self.image_processor_tester.prepare_image_processor_dict()

   def        UpperCamelCase_					(   self							:     Any)			->			Any:

    """simple docstring"""







    _snake_case      :					str					=   self.image_processing_class(**self.image_processor_dict)
    self.assertTrue(hasattr(lowerCAmelCase			,						"""image_mean"""))
    self.assertTrue(hasattr(lowerCAmelCase			,						"""image_std"""))
    self.assertTrue(hasattr(lowerCAmelCase			,						"""do_normalize"""))
    self.assertTrue(hasattr(lowerCAmelCase			,						"""do_resize"""))
    self.assertTrue(hasattr(lowerCAmelCase			,						"""do_center_crop"""))
    self.assertTrue(hasattr(lowerCAmelCase			,						"""size"""))

   def        UpperCamelCase_					(   self							:     int)			->			Union[str, Any]:

    """simple docstring"""







    _snake_case      :					Optional[Any]					=   self.image_processing_class(**self.image_processor_dict)
    # create random PIL videos
    _snake_case      :					List[str]					=   prepare_video_inputs(self.image_processor_tester			,						equal_resolution=lowerCAmelCase)
    for video in video_inputs:
     self.assertIsInstance(lowerCAmelCase			,						lowerCAmelCase)
     self.assertIsInstance(video[0]			,						Image.Image)

    # Test not batched input
    _snake_case      :					List[Any]					=   image_processor(video_inputs[0]			,						return_tensors="""pt""").pixel_values
    self.assertEqual(
        encoded_videos.shape			,						(
            1,
            self.image_processor_tester.num_frames,
            self.image_processor_tester.num_channels,
            self.image_processor_tester.crop_size["""height"""],
            self.image_processor_tester.crop_size["""width"""],
        )			,						)

    # Test batched
    _snake_case      :					Union[str, Any]					=   image_processor(lowerCAmelCase			,						return_tensors="""pt""").pixel_values
    self.assertEqual(
        encoded_videos.shape			,						(
            self.image_processor_tester.batch_size,
            self.image_processor_tester.num_frames,
            self.image_processor_tester.num_channels,
            self.image_processor_tester.crop_size["""height"""],
            self.image_processor_tester.crop_size["""width"""],
        )			,						)

   def        UpperCamelCase_					(   self							:     Tuple)			->			Union[str, Any]:

    """simple docstring"""







    _snake_case      :					str					=   self.image_processing_class(**self.image_processor_dict)
    # create random numpy tensors
    _snake_case      :					Union[str, Any]					=   prepare_video_inputs(self.image_processor_tester			,						equal_resolution=lowerCAmelCase			,						numpify=lowerCAmelCase)
    for video in video_inputs:
     self.assertIsInstance(lowerCAmelCase			,						lowerCAmelCase)
     self.assertIsInstance(video[0]			,						np.ndarray)

    # Test not batched input
    _snake_case      :					List[Any]					=   image_processor(video_inputs[0]			,						return_tensors="""pt""").pixel_values
    self.assertEqual(
        encoded_videos.shape			,						(
            1,
            self.image_processor_tester.num_frames,
            self.image_processor_tester.num_channels,
            self.image_processor_tester.crop_size["""height"""],
            self.image_processor_tester.crop_size["""width"""],
        )			,						)

    # Test batched
    _snake_case      :					List[str]					=   image_processor(lowerCAmelCase			,						return_tensors="""pt""").pixel_values
    self.assertEqual(
        encoded_videos.shape			,						(
            self.image_processor_tester.batch_size,
            self.image_processor_tester.num_frames,
            self.image_processor_tester.num_channels,
            self.image_processor_tester.crop_size["""height"""],
            self.image_processor_tester.crop_size["""width"""],
        )			,						)


   def        UpperCamelCase_					(   self							:     str)			->			Any:

    """simple docstring"""







    _snake_case      :					List[Any]					=   self.image_processing_class(**self.image_processor_dict)
    # create random PyTorch tensors
    _snake_case      :					int					=   prepare_video_inputs(self.image_processor_tester			,						equal_resolution=lowerCAmelCase			,						torchify=lowerCAmelCase)
    for video in video_inputs:
     self.assertIsInstance(lowerCAmelCase			,						lowerCAmelCase)
     self.assertIsInstance(video[0]			,						torch.Tensor)

    # Test not batched input
    _snake_case      :					Dict					=   image_processor(video_inputs[0]			,						return_tensors="""pt""").pixel_values
    self.assertEqual(
        encoded_videos.shape			,						(
            1,
            self.image_processor_tester.num_frames,
            self.image_processor_tester.num_channels,
            self.image_processor_tester.crop_size["""height"""],
            self.image_processor_tester.crop_size["""width"""],
        )			,						)

    # Test batched
    _snake_case      :					Union[str, Any]					=   image_processor(lowerCAmelCase			,						return_tensors="""pt""").pixel_values
    self.assertEqual(
        encoded_videos.shape			,						(
            self.image_processor_tester.batch_size,
            self.image_processor_tester.num_frames,
            self.image_processor_tester.num_channels,
            self.image_processor_tester.crop_size["""height"""],
            self.image_processor_tester.crop_size["""width"""],
        )			,						)





import math
from collections import OrderedDict

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

from .utils import logging


a__      =		logging.get_logger(__name__)







class 						snake_case  (		nn.Module					):



   '''simple docstring'''






   def __init__(   self							:     Tuple)			->			Optional[int]:

    """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        UpperCamelCase_					(   self							:     Tuple			,						lowerCAmelCase							:     Tensor)			->			Tensor:

    """simple docstring"""







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







class 						snake_case  (		nn.Module					):



   '''simple docstring'''






   def        UpperCamelCase_					(   self							:     Optional[int]			,						lowerCAmelCase							:     Tensor)			->			Tensor:

    """simple docstring"""







    return 0.5 * input * (1.0 + torch.tanh(math.sqrt(2.0 / math.pi) * (input + 0.044_715 * torch.pow(lowerCAmelCase			,						3.0))))







class 						snake_case  (		nn.Module					):



   '''simple docstring'''






   def __init__(   self							:     str			,						lowerCAmelCase							:     bool = False)			->			Optional[Any]:

    """simple docstring"""







    super().__init__()
    if use_gelu_python:
     _snake_case      :					Optional[int]					=   self._gelu_python
    else:
     _snake_case      :					Any					=   nn.functional.gelu

   def        UpperCamelCase_					(   self							:     Union[str, Any]			,						lowerCAmelCase							:     Tensor)			->			Tensor:

    """simple docstring"""







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


   def        UpperCamelCase_					(   self							:     str			,						lowerCAmelCase							:     Tensor)			->			Tensor:

    """simple docstring"""







    return self.act(lowerCAmelCase)







class 						snake_case  (		nn.Module					):



   '''simple docstring'''






   def        UpperCamelCase_					(   self							:     int			,						lowerCAmelCase							:     Tensor)			->			Tensor:

    """simple docstring"""







    return 0.5 * input * (1.0 + torch.tanh(input * 0.7_978_845_608 * (1.0 + 0.044_715 * input * input)))







class 						snake_case  (		nn.Module					):



   '''simple docstring'''






   def        UpperCamelCase_					(   self							:     int			,						lowerCAmelCase							:     Tensor)			->			Tensor:

    """simple docstring"""







    return input * torch.sigmoid(1.702 * input)







class 						snake_case  (		nn.Module					):



   '''simple docstring'''






   def __init__(   self							:     int			,						lowerCAmelCase							:     float			,						lowerCAmelCase							:     float)			->			Optional[int]:

    """simple docstring"""







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

    super().__init__()
    _snake_case      :					Dict					=   min
    _snake_case      :					Optional[int]					=   max


   def        UpperCamelCase_					(   self							:     Union[str, Any]			,						lowerCAmelCase							:     Tensor)			->			Tensor:

    """simple docstring"""







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







class 						snake_case  (		nn.Module					):



   '''simple docstring'''






   def __init__(   self							:     Tuple)			->			List[str]:

    """simple docstring"""







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


   def        UpperCamelCase_					(   self							:     Dict			,						lowerCAmelCase							:     Tensor)			->			Tensor:

    """simple docstring"""







    return 0.5 * input * (1 + torch.tanh(self.precomputed_constant * (input + 0.044_715 * torch.pow(lowerCAmelCase			,						3))))







class 						snake_case  (		nn.Module					):



   '''simple docstring'''






   def        UpperCamelCase_					(   self							:     Dict			,						lowerCAmelCase							:     Tensor)			->			Tensor:

    """simple docstring"""







    return nn.functional.silu(lowerCAmelCase)







class 						snake_case  (		nn.Module					):



   '''simple docstring'''






   def __init__(   self							:     List[Any])			->			str:

    """simple docstring"""







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

   def        UpperCamelCase_					(   self							:     str			,						lowerCAmelCase							:     Tensor)			->			Tensor:

    """simple docstring"""







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


   def        UpperCamelCase_					(   self							:     str			,						lowerCAmelCase							:     Tensor)			->			Tensor:

    """simple docstring"""







    return self.act(lowerCAmelCase)







class 						snake_case  (		nn.Module					):



   '''simple docstring'''






   def        UpperCamelCase_					(   self							:     Optional[Any]			,						lowerCAmelCase							:     Tensor)			->			Tensor:

    """simple docstring"""







    return input







class 						snake_case  (		nn.Module					):



   '''simple docstring'''






   def        UpperCamelCase_					(   self							:     List[Any]			,						lowerCAmelCase							:     int			,						lowerCAmelCase							:     List[Any]=0.707_107			,						lowerCAmelCase							:     Optional[Any]=0.282_095)			->			List[Any]:

    """simple docstring"""







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







class 						snake_case  (		nn.Module					):



   '''simple docstring'''






   def        UpperCamelCase_					(   self							:     Any			,						lowerCAmelCase							:     Dict)			->			Union[str, Any]:

    """simple docstring"""







    _snake_case      :					Dict					=   nn.functional.relu(lowerCAmelCase)
    _snake_case      :					Tuple					=   torch.square(lowerCAmelCase)
    return squared







class 						snake_case  (		SCREAMING_SNAKE_CASE_					):



   '''simple docstring'''






   def __getitem__(   self							:     Dict			,						lowerCAmelCase							:     Tuple)			->			str:

    """simple docstring"""







    _snake_case      :					Optional[Any]					=   super().__getitem__(lowerCAmelCase)
    _snake_case		,      _snake_case      :					List[str]					=   content if isinstance(lowerCAmelCase			,						lowerCAmelCase) else (content, {})
    return cls(**lowerCAmelCase)
a__      =		{
    """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__      =		ClassInstantier(ACTaCLS)



def  lowercase    (				SCREAMING_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__      =		get_activation("""gelu_python""")
a__      =		get_activation("""gelu_new""")
a__      =		get_activation("""gelu""")
a__      =		get_activation("""gelu_fast""")
a__      =		get_activation("""quick_gelu""")
a__      =		get_activation("""silu""")
a__      =		get_activation("""mish""")

a__      =		get_activation("""linear""")
