


'''simple docstring'''

import math
from collections import OrderedDict

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

from .utils import logging


lowercase__					  =		logging.get_logger(__name__)





class     __SCREAMING_SNAKE_CASE       (   nn.Module			):






     def __init__(      self		)							->     str:
          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:
          return nn.functional.gelu(__UpperCamelCase  ,				approximate="tanh"		)







class     __SCREAMING_SNAKE_CASE       (   nn.Module			):






     def 			a_       (      self  ,				__UpperCamelCase		)							->     Tensor:
          return 0.5 * input * (1.0 + torch.tanh(math.sqrt(2.0 / math.pi		) * (input + 0.04_47_15 * torch.pow(__UpperCamelCase  ,				3.0		))		))







class     __SCREAMING_SNAKE_CASE       (   nn.Module			):






     def __init__(      self  ,				__UpperCamelCase = False		)							->     Tuple:
          super().__init__()
          if use_gelu_python:
               _a            =		self._gelu_python
          else:
               _a            =		nn.functional.gelu






     def 			a_       (      self  ,				__UpperCamelCase		)							->     Tensor:
          return input * 0.5 * (1.0 + torch.erf(input / math.sqrt(2.0		)		))






     def 			a_       (      self  ,				__UpperCamelCase		)							->     Tensor:
          return self.act(__UpperCamelCase		)







class     __SCREAMING_SNAKE_CASE       (   nn.Module			):






     def 			a_       (      self  ,				__UpperCamelCase		)							->     Tensor:
          return 0.5 * input * (1.0 + torch.tanh(input * 0.79_78_84_56_08 * (1.0 + 0.04_47_15 * input * input)		))







class     __SCREAMING_SNAKE_CASE       (   nn.Module			):






     def 			a_       (      self  ,				__UpperCamelCase		)							->     Tensor:
          return input * torch.sigmoid(1.7_02 * input		)







class     __SCREAMING_SNAKE_CASE       (   nn.Module			):






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

          super().__init__()
          _a            =		min
          _a            =		max






     def 			a_       (      self  ,				__UpperCamelCase		)							->     Tensor:
          return torch.clip(gelu(__UpperCamelCase		)  ,				self.min  ,				self.max		)







class     __SCREAMING_SNAKE_CASE       (   nn.Module			):






     def __init__(      self		)							->     Optional[Any]:
          super().__init__()
          _a            =		math.sqrt(2 / math.pi		)






     def 			a_       (      self  ,				__UpperCamelCase		)							->     Tensor:
          return 0.5 * input * (1 + torch.tanh(self.precomputed_constant * (input + 0.04_47_15 * torch.pow(__UpperCamelCase  ,				3		))		))







class     __SCREAMING_SNAKE_CASE       (   nn.Module			):






     def 			a_       (      self  ,				__UpperCamelCase		)							->     Tensor:
          return nn.functional.silu(__UpperCamelCase		)







class     __SCREAMING_SNAKE_CASE       (   nn.Module			):






     def __init__(      self		)							->     Optional[Any]:
          super().__init__()
          if version.parse(torch.__version__		) < version.parse("1.9.0"		):
               _a            =		self._mish_python
          else:
               _a            =		nn.functional.mish






     def 			a_       (      self  ,				__UpperCamelCase		)							->     Tensor:
          return input * torch.tanh(nn.functional.softplus(__UpperCamelCase		)		)






     def 			a_       (      self  ,				__UpperCamelCase		)							->     Tensor:
          return self.act(__UpperCamelCase		)







class     __SCREAMING_SNAKE_CASE       (   nn.Module			):






     def 			a_       (      self  ,				__UpperCamelCase		)							->     Tensor:
          return input







class     __SCREAMING_SNAKE_CASE       (   nn.Module			):






     def 			a_       (      self  ,				__UpperCamelCase  ,				__UpperCamelCase=0.70_71_07  ,				__UpperCamelCase=0.28_20_95		)							->     Union[str, Any]:
          _a            =		(input - mu).div(sigma * math.sqrt(2.0		)		)
          return 0.5 * (1.0 + torch.erf(__UpperCamelCase		))







class     __SCREAMING_SNAKE_CASE       (   nn.Module			):






     def 			a_       (      self  ,				__UpperCamelCase		)							->     Any:
          _a            =		nn.functional.relu(__UpperCamelCase		)
          _a            =		torch.square(__UpperCamelCase		)
          return squared







class     __SCREAMING_SNAKE_CASE       (   lowerCamelCase__			):






     def __getitem__(      self  ,				__UpperCamelCase		)							->     List[Any]:
          _a            =		super().__getitem__(__UpperCamelCase		)
          _a      ,      _a            =		content if isinstance(__UpperCamelCase  ,				__UpperCamelCase		) else (content, {})
          return cls(**__UpperCamelCase		)







lowercase__					  =		{
    "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,
}
lowercase__					  =		ClassInstantier(ACTaCLS)






def __UpperCamelCase (      __lowerCamelCase		:			str					)   ->  Optional[int]:







     '''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
lowercase__					  =		get_activation("gelu_python")
lowercase__					  =		get_activation("gelu_new")
lowercase__					  =		get_activation("gelu")
lowercase__					  =		get_activation("gelu_fast")
lowercase__					  =		get_activation("quick_gelu")
lowercase__					  =		get_activation("silu")
lowercase__					  =		get_activation("mish")






lowercase__					  =		get_activation("linear")





'''simple docstring'''

import random
import unittest

import torch

from diffusers import IFImgaImgPipeline
from diffusers.utils import floats_tensor
from diffusers.utils.import_utils import is_xformers_available
from diffusers.utils.testing_utils import skip_mps, torch_device

from ..pipeline_params import (
    TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS,
    TEXT_GUIDED_IMAGE_VARIATION_PARAMS,
)
from ..test_pipelines_common import PipelineTesterMixin
from . import IFPipelineTesterMixin





@skip_mps
class     __SCREAMING_SNAKE_CASE       (   lowerCamelCase__			,      lowerCamelCase__			,      unittest.TestCase			):
     UpperCAmelCase			       =					IFImgaImgPipeline
     UpperCAmelCase			       =					TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {'''width''', '''height'''}
     UpperCAmelCase			       =					TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS
     UpperCAmelCase			       =					PipelineTesterMixin.required_optional_params - {'''latents'''}






     def 			a_       (      self		)							->     Optional[Any]:
          return self._get_dummy_components()






     def 			a_       (      self  ,				__UpperCamelCase  ,				__UpperCamelCase=0		)							->     Any:
          if str(__UpperCamelCase		).startswith("mps"		):
               _a            =		torch.manual_seed(__UpperCamelCase		)
          else:
               _a            =		torch.Generator(device=__UpperCamelCase		).manual_seed(__UpperCamelCase		)

          _a            =		floats_tensor((1, 3, 32, 32)  ,				rng=random.Random(__UpperCamelCase		)		).to(__UpperCamelCase		)

          _a            =		{
              "prompt": "A painting of a squirrel eating a burger",
              "image": image,
              "generator": generator,
              "num_inference_steps": 2,
              "output_type": "numpy",
          }

          return inputs






     def 			a_       (      self		)							->     Any:
          self._test_save_load_optional_components()






     @unittest.skipIf(
         torch_device != "cuda" or not is_xformers_available()  ,				reason="XFormers attention is only available with CUDA and `xformers` installed"  ,				)
     def 			a_       (      self		)							->     Dict:
          self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1e-3		)






     @unittest.skipIf(torch_device != "cuda"  ,				reason="float16 requires CUDA"		)
     def 			a_       (      self		)							->     List[Any]:
          # Due to non-determinism in save load of the hf-internal-testing/tiny-random-t5 text encoder
          super().test_save_load_floataa(expected_max_diff=1e-1		)






     @unittest.skipIf(torch_device != "cuda"  ,				reason="float16 requires CUDA"		)
     def 			a_       (      self		)							->     int:
          super().test_floataa_inference(expected_max_diff=1e-1		)






     def 			a_       (      self		)							->     Dict:
          self._test_attention_slicing_forward_pass(expected_max_diff=1e-2		)






     def 			a_       (      self		)							->     Optional[Any]:
          self._test_save_load_local()






     def 			a_       (      self		)							->     Union[str, Any]:
          self._test_inference_batch_single_identical(
              expected_max_diff=1e-2  ,				)

