


from typing import List, Optional, Tuple, Union

from . import logging
from .import_utils import is_torch_available, is_torch_version


if is_torch_available():
  import torch

UpperCAmelCase__     			=							logging.get_logger(__name__)  # pylint: disable=invalid-name

try:
  from torch._dynamo import allow_in_graph as maybe_allow_in_graph
except (ImportError, ModuleNotFoundError):
  def        UpperCAmelCase_      (							cls		)  ->    List[str]:







   """simple docstring"""


   return cls
def        UpperCAmelCase_      (							__snake_case  ,		__snake_case = None  ,		__snake_case = None  ,		__snake_case = None  ,		__snake_case = None  ,		)  ->    int:







 """simple docstring"""


 _lowercase							   =device
 _lowercase							   =shape[0]

 _lowercase							   =layout or torch.strided
 _lowercase							   =device or torch.device('''cpu'''		)

 if generator is not None:
  _lowercase							   =generator.device.type if not isinstance(__snake_case  ,		__snake_case		) else generator[0].device.type
  if gen_device_type != device.type and gen_device_type == "cpu":
   _lowercase							   ='''cpu'''
   if device != "mps":
    logger.info(
        F"The passed generator was created on 'cpu' even though a tensor on {device} was expected."
        F" Tensors will be created on 'cpu' and then moved to {device}. Note that one can probably"
        F" slighly speed up this function by passing a generator that was created on the {device} device."		)
  elif gen_device_type != device.type and gen_device_type == "cuda":
   raise ValueError(F"Cannot generate a {device} tensor from a generator of type {gen_device_type}."		)

 if isinstance(__snake_case  ,		__snake_case		):
  _lowercase							   =(1,) + shape[1:]
  _lowercase							   =[
      torch.randn(__snake_case  ,		generator=generator[i]  ,		device=__snake_case  ,		dtype=__snake_case  ,		layout=__snake_case		)
      for i in range(__snake_case		)
  ]
  _lowercase							   =torch.cat(__snake_case  ,		dim=0		).to(__snake_case		)
 else:
  _lowercase							   =torch.randn(__snake_case  ,		generator=__snake_case  ,		device=__snake_case  ,		dtype=__snake_case  ,		layout=__snake_case		).to(__snake_case		)

 return latents



def        UpperCAmelCase_      (							__snake_case		)  ->    Dict:







 """simple docstring"""


 if is_torch_version('''<'''  ,		'''2.0.0'''		) or not hasattr(__snake_case  ,		'''_dynamo'''		):
  return False
 return isinstance(__snake_case  ,		torch._dynamo.eval_frame.OptimizedModule		)





import unittest

import numpy as np

from transformers import is_tf_available
from transformers.testing_utils import require_tf


if is_tf_available():
  import tensorflow as tf

  from transformers.activations_tf import get_tf_activation







@require_tf
class    lowerCamelCase__     ( unittest.TestCase):





     def   __A      (self )  ->     Optional[Any]:
      _lowercase							   =tf.constant([-1_0_0, -1.0, -0.1, 0, 0.1, 1.0, 100.0] )
      _lowercase							   =get_tf_activation('''gelu''' )
      _lowercase							   =get_tf_activation('''gelu_10''' )

      _lowercase							   =gelu(UpperCAmelCase )
      _lowercase							   =geluaa(UpperCAmelCase )

      _lowercase							   =tf.where(y_gelu_aa < 10.0	,  1.0	,  0.0 )

      self.assertEqual(tf.math.reduce_max(UpperCAmelCase ).numpy().item()	,  10.0 )
      self.assertTrue(np.allclose(y_gelu * clipped_mask	,  y_gelu_aa * clipped_mask ) )





     def   __A      (self )  ->     Optional[Any]:
      get_tf_activation('''gelu''' )
      get_tf_activation('''gelu_10''' )
      get_tf_activation('''gelu_fast''' )
      get_tf_activation('''gelu_new''' )
      get_tf_activation('''glu''' )
      get_tf_activation('''mish''' )
      get_tf_activation('''quick_gelu''' )
      get_tf_activation('''relu''' )
      get_tf_activation('''sigmoid''' )
      get_tf_activation('''silu''' )
      get_tf_activation('''swish''' )
      get_tf_activation('''tanh''' )
      with self.assertRaises(UpperCAmelCase ):
       get_tf_activation('''bogus''' )
      with self.assertRaises(UpperCAmelCase ):
       get_tf_activation(UpperCAmelCase )

