

'''simple docstring'''



import math
from collections import OrderedDict

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

from .utils import logging


SCREAMING_SNAKE_CASE_    =   logging.get_logger(__name__)

class 			SCREAMING_SNAKE_CASE					(							nn.Module					):






      '''simple docstring'''






      def __init__(					self :		Optional[Any]   ):



          """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 :		Any     ,					snake_case :		Tensor   ):



          """simple docstring"""





          return nn.functional.gelu(snake_case     ,					approximate='tanh'   )



class 			SCREAMING_SNAKE_CASE					(							nn.Module					):






      '''simple docstring'''






      def 	__UpperCAmelCase			(					self :		List[str]     ,					snake_case :		Tensor   ):



          """simple docstring"""





          return 0.5 * input * (1.0 + torch.tanh(math.sqrt(2.0 / math.pi   ) * (input + 0.04_4715 * torch.pow(snake_case     ,					3.0   ))   ))



class 			SCREAMING_SNAKE_CASE					(							nn.Module					):






      '''simple docstring'''






      def __init__(					self :		List[Any]     ,					snake_case :		bool = False   ):



          """simple docstring"""





          super().__init__()
          if use_gelu_python:
              _snake_case : Dict												=		self._gelu_python
          else:
              _snake_case : Union[str, Any]												=		nn.functional.gelu




      def 	__UpperCAmelCase			(					self :		Optional[Any]     ,					snake_case :		Tensor   ):



          """simple docstring"""





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




      def 	__UpperCAmelCase			(					self :		Optional[int]     ,					snake_case :		Tensor   ):



          """simple docstring"""





          return self.act(snake_case   )



class 			SCREAMING_SNAKE_CASE					(							nn.Module					):






      '''simple docstring'''






      def 	__UpperCAmelCase			(					self :		Optional[Any]     ,					snake_case :		Tensor   ):



          """simple docstring"""





          return 0.5 * input * (1.0 + torch.tanh(input * 0.79_7884_5608 * (1.0 + 0.04_4715 * input * input)   ))



class 			SCREAMING_SNAKE_CASE					(							nn.Module					):






      '''simple docstring'''






      def 	__UpperCAmelCase			(					self :		Dict     ,					snake_case :		Tensor   ):



          """simple docstring"""





          return input * torch.sigmoid(1.702 * input   )



class 			SCREAMING_SNAKE_CASE					(							nn.Module					):






      '''simple docstring'''






      def __init__(					self :		Tuple     ,					snake_case :		float     ,					snake_case :		float   ):



          """simple docstring"""





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

          super().__init__()
          _snake_case : Any												=		min
          _snake_case : Tuple												=		max




      def 	__UpperCAmelCase			(					self :		Tuple     ,					snake_case :		Tensor   ):



          """simple docstring"""





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



class 			SCREAMING_SNAKE_CASE					(							nn.Module					):






      '''simple docstring'''






      def __init__(					self :		Optional[Any]   ):



          """simple docstring"""





          super().__init__()
          _snake_case : int												=		math.sqrt(2 / math.pi   )




      def 	__UpperCAmelCase			(					self :		Dict     ,					snake_case :		Tensor   ):



          """simple docstring"""





          return 0.5 * input * (1 + torch.tanh(self.precomputed_constant * (input + 0.04_4715 * torch.pow(snake_case     ,					3   ))   ))



class 			SCREAMING_SNAKE_CASE					(							nn.Module					):






      '''simple docstring'''






      def 	__UpperCAmelCase			(					self :		Any     ,					snake_case :		Tensor   ):



          """simple docstring"""





          return nn.functional.silu(snake_case   )



class 			SCREAMING_SNAKE_CASE					(							nn.Module					):






      '''simple docstring'''






      def __init__(					self :		Optional[int]   ):



          """simple docstring"""





          super().__init__()
          if version.parse(torch.__version__   ) < version.parse('1.9.0'   ):
              _snake_case : Union[str, Any]												=		self._mish_python
          else:
              _snake_case : Dict												=		nn.functional.mish




      def 	__UpperCAmelCase			(					self :		int     ,					snake_case :		Tensor   ):



          """simple docstring"""





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




      def 	__UpperCAmelCase			(					self :		Optional[Any]     ,					snake_case :		Tensor   ):



          """simple docstring"""





          return self.act(snake_case   )



class 			SCREAMING_SNAKE_CASE					(							nn.Module					):






      '''simple docstring'''






      def 	__UpperCAmelCase			(					self :		int     ,					snake_case :		Tensor   ):



          """simple docstring"""





          return input



class 			SCREAMING_SNAKE_CASE					(							nn.Module					):






      '''simple docstring'''






      def 	__UpperCAmelCase			(					self :		Union[str, Any]     ,					snake_case :		Any     ,					snake_case :		int=0.70_7107     ,					snake_case :		Union[str, Any]=0.28_2095   ):



          """simple docstring"""





          _snake_case : Tuple												=		(input - mu).div(sigma * math.sqrt(2.0   )   )
          return 0.5 * (1.0 + torch.erf(snake_case   ))



class 			SCREAMING_SNAKE_CASE					(							nn.Module					):






      '''simple docstring'''






      def 	__UpperCAmelCase			(					self :		str     ,					snake_case :		int   ):



          """simple docstring"""





          _snake_case : Optional[int]												=		nn.functional.relu(snake_case   )
          _snake_case : int												=		torch.square(snake_case   )
          return squared



class 			SCREAMING_SNAKE_CASE					(							lowercase_					):






      '''simple docstring'''






      def __getitem__(					self :		Dict     ,					snake_case :		Tuple   ):



          """simple docstring"""





          _snake_case : Tuple												=		super().__getitem__(snake_case   )
          _snake_case : int												=		content if isinstance(snake_case     ,					snake_case   ) else (content, {})
          return cls(**snake_case   )



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

def   lowerCamelCase__       (      a__)		->    List[Any]:



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




SCREAMING_SNAKE_CASE_    =   get_activation("linear")






'''simple docstring'''



from __future__ import annotations

import inspect
import unittest
from typing import List, Tuple

from transformers import ConvNextConfig
from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import cached_property, is_tf_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
   import tensorflow as tf

   from transformers import TFConvNextForImageClassification, TFConvNextModel


if is_vision_available():
   from PIL import Image

   from transformers import ConvNextImageProcessor

class 			SCREAMING_SNAKE_CASE					:






      '''simple docstring'''






      def __init__(					self :		Optional[int]     ,					snake_case :		Union[str, Any]     ,					snake_case :		Optional[int]=13     ,					snake_case :		Union[str, Any]=32     ,					snake_case :		List[Any]=3     ,					snake_case :		int=4     ,					snake_case :		List[Any]=[10, 20, 30, 40]     ,					snake_case :		List[str]=[2, 2, 3, 2]     ,					snake_case :		List[str]=True     ,					snake_case :		Optional[int]=True     ,					snake_case :		Union[str, Any]=37     ,					snake_case :		Any="gelu"     ,					snake_case :		Any=10     ,					snake_case :		Optional[Any]=0.02     ,					snake_case :		List[str]=3     ,					snake_case :		Any=None     ,					):



          """simple docstring"""





          _snake_case : Optional[Any]												=		parent
          _snake_case : Any												=		batch_size
          _snake_case : int												=		image_size
          _snake_case : int												=		num_channels
          _snake_case : int												=		num_stages
          _snake_case : Optional[int]												=		hidden_sizes
          _snake_case : Tuple												=		depths
          _snake_case : Optional[int]												=		is_training
          _snake_case : Tuple												=		use_labels
          _snake_case : str												=		intermediate_size
          _snake_case : Any												=		hidden_act
          _snake_case : Union[str, Any]												=		type_sequence_label_size
          _snake_case : Tuple												=		initializer_range
          _snake_case : List[Any]												=		scope




      def 	__UpperCAmelCase			(					self :		List[str]   ):



          """simple docstring"""





          _snake_case : str												=		floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]   )

          _snake_case : Any												=		None
          if self.use_labels:
              _snake_case : str												=		ids_tensor([self.batch_size]     ,					self.type_sequence_label_size   )

          _snake_case : Any												=		self.get_config()

          return config, pixel_values, labels




      def 	__UpperCAmelCase			(					self :		Union[str, Any]   ):



          """simple docstring"""





          return ConvNextConfig(
              num_channels=self.num_channels     ,					hidden_sizes=self.hidden_sizes     ,					depths=self.depths     ,					num_stages=self.num_stages     ,					hidden_act=self.hidden_act     ,					is_decoder=snake_case     ,					initializer_range=self.initializer_range     ,					)




      def 	__UpperCAmelCase			(					self :		int     ,					snake_case :		Optional[Any]     ,					snake_case :		str     ,					snake_case :		Optional[int]   ):



          """simple docstring"""





          _snake_case : Optional[int]												=		TFConvNextModel(config=snake_case   )
          _snake_case : Tuple												=		model(snake_case     ,					training=snake_case   )
          # expected last hidden states: B, C, H // 32, W // 32
          self.parent.assertEqual(
              result.last_hidden_state.shape     ,					(self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32)     ,					)




      def 	__UpperCAmelCase			(					self :		Any     ,					snake_case :		int     ,					snake_case :		List[str]     ,					snake_case :		List[Any]   ):



          """simple docstring"""





          _snake_case : int												=		self.type_sequence_label_size
          _snake_case : str												=		TFConvNextForImageClassification(snake_case   )
          _snake_case : Any												=		model(snake_case     ,					labels=snake_case     ,					training=snake_case   )
          self.parent.assertEqual(result.logits.shape     ,					(self.batch_size, self.type_sequence_label_size)   )




      def 	__UpperCAmelCase			(					self :		Any   ):



          """simple docstring"""





          _snake_case : Union[str, Any]												=		self.prepare_config_and_inputs()
          _snake_case					,							_snake_case					,							_snake_case : Optional[int]												=		config_and_inputs
          _snake_case : Tuple												=		{'pixel_values': pixel_values}
          return config, inputs_dict



@require_tf
class 			SCREAMING_SNAKE_CASE					(							lowercase_      ,lowercase_      ,unittest.TestCase					):






      '''simple docstring'''






      SCREAMING_SNAKE_CASE__ :				List[Any]    =				(TFConvNextModel, TFConvNextForImageClassification) if is_tf_available() else ()
      SCREAMING_SNAKE_CASE__ :				Tuple    =				(
          {'''feature-extraction''': TFConvNextModel, '''image-classification''': TFConvNextForImageClassification}
          if is_tf_available()
          else {}
      )

      SCREAMING_SNAKE_CASE__ :				Optional[int]    =				False
      SCREAMING_SNAKE_CASE__ :				str    =				False
      SCREAMING_SNAKE_CASE__ :				Optional[Any]    =				False
      SCREAMING_SNAKE_CASE__ :				str    =				False
      SCREAMING_SNAKE_CASE__ :				Any    =				False




      def 	__UpperCAmelCase			(					self :		List[str]   ):



          """simple docstring"""





          _snake_case : Optional[int]												=		TFConvNextModelTester(self   )
          _snake_case : Any												=		ConfigTester(
              self     ,					config_class=snake_case     ,					has_text_modality=snake_case     ,					hidden_size=37     ,					)




      @unittest.skip(reason='ConvNext does not use inputs_embeds'   )
      def 	__UpperCAmelCase			(					self :		Any   ):



          """simple docstring"""





          pass




      @unittest.skipIf(
          not is_tf_available() or len(tf.config.list_physical_devices('GPU'   )   ) == 0     ,					reason='TF does not support backprop for grouped convolutions on CPU.'     ,					)
      @slow
      def 	__UpperCAmelCase			(					self :		Optional[Any]   ):



          """simple docstring"""





          super().test_keras_fit()




      @unittest.skip(reason='ConvNext does not support input and output embeddings'   )
      def 	__UpperCAmelCase			(					self :		Tuple   ):



          """simple docstring"""





          pass




      def 	__UpperCAmelCase			(					self :		Optional[int]   ):



          """simple docstring"""





          _snake_case					,							_snake_case : Optional[Any]												=		self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes:
              _snake_case : str												=		model_class(snake_case   )
              _snake_case : int												=		inspect.signature(model.call   )
              # signature.parameters is an OrderedDict => so arg_names order is deterministic
              _snake_case : Dict												=		[*signature.parameters.keys()]

              _snake_case : str												=		['pixel_values']
              self.assertListEqual(arg_names[:1]     ,					snake_case   )




      def 	__UpperCAmelCase			(					self :		Optional[int]   ):



          """simple docstring"""





          _snake_case : Optional[Any]												=		self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*snake_case   )




      @unittest.skipIf(
          not is_tf_available() or len(tf.config.list_physical_devices('GPU'   )   ) == 0     ,					reason='TF does not support backprop for grouped convolutions on CPU.'     ,					)
      def 	__UpperCAmelCase			(					self :		Dict   ):



          """simple docstring"""





          super().test_dataset_conversion()




      def 	__UpperCAmelCase			(					self :		List[Any]   ):



          """simple docstring"""





          def check_hidden_states_output(snake_case :		Union[str, Any]     ,					snake_case :		str     ,					snake_case :		Dict   ):
              _snake_case : Any												=		model_class(snake_case   )

              _snake_case : Tuple												=		model(**self._prepare_for_class(snake_case     ,					snake_case   )   )
              _snake_case : Tuple												=		outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

              _snake_case : Optional[Any]												=		self.model_tester.num_stages
              self.assertEqual(len(snake_case   )     ,					expected_num_stages + 1   )

              # ConvNext's feature maps are of shape (batch_size, num_channels, height, width)
              self.assertListEqual(
                  list(hidden_states[0].shape[-2:]   )     ,					[self.model_tester.image_size // 4, self.model_tester.image_size // 4]     ,					)

          _snake_case					,							_snake_case : Optional[int]												=		self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes:
              _snake_case : int												=		True
              check_hidden_states_output(snake_case     ,					snake_case     ,					snake_case   )

              # check that output_hidden_states also work using config
              del inputs_dict["output_hidden_states"]
              _snake_case : Optional[Any]												=		True

              check_hidden_states_output(snake_case     ,					snake_case     ,					snake_case   )




      def 	__UpperCAmelCase			(					self :		Optional[int]   ):



          """simple docstring"""





          _snake_case					,							_snake_case : Any												=		self.model_tester.prepare_config_and_inputs_for_common()

          def check_equivalence(snake_case :		Any     ,					snake_case :		Tuple     ,					snake_case :		List[str]     ,					snake_case :		Dict={}   ):
              _snake_case : Union[str, Any]												=		model(snake_case     ,					return_dict=snake_case     ,					**snake_case   )
              _snake_case : Dict												=		model(snake_case     ,					return_dict=snake_case     ,					**snake_case   ).to_tuple()

              def recursive_check(snake_case :		Dict     ,					snake_case :		Tuple   ):
                  if isinstance(snake_case     ,					(List, Tuple)   ):
                      for tuple_iterable_value, dict_iterable_value in zip(snake_case     ,					snake_case   ):
                          recursive_check(snake_case     ,					snake_case   )
                  elif tuple_object is None:
                      return
                  else:
                      self.assertTrue(
                          all(tf.equal(snake_case     ,					snake_case   )   )     ,					msg=(
                              'Tuple and dict output are not equal. Difference:'
                              F""" {tf.math.reduce_max(tf.abs(tuple_object - dict_object   )   )}"""
                          )     ,					)

                  recursive_check(snake_case     ,					snake_case   )

          for model_class in self.all_model_classes:
              _snake_case : Any												=		model_class(snake_case   )

              _snake_case : List[str]												=		self._prepare_for_class(snake_case     ,					snake_case   )
              _snake_case : Optional[int]												=		self._prepare_for_class(snake_case     ,					snake_case   )
              check_equivalence(snake_case     ,					snake_case     ,					snake_case   )

              _snake_case : Optional[int]												=		self._prepare_for_class(snake_case     ,					snake_case     ,					return_labels=snake_case   )
              _snake_case : Union[str, Any]												=		self._prepare_for_class(snake_case     ,					snake_case     ,					return_labels=snake_case   )
              check_equivalence(snake_case     ,					snake_case     ,					snake_case   )

              _snake_case : Any												=		self._prepare_for_class(snake_case     ,					snake_case   )
              _snake_case : Optional[Any]												=		self._prepare_for_class(snake_case     ,					snake_case   )
              check_equivalence(snake_case     ,					snake_case     ,					snake_case     ,					{'output_hidden_states': True}   )

              _snake_case : str												=		self._prepare_for_class(snake_case     ,					snake_case     ,					return_labels=snake_case   )
              _snake_case : Any												=		self._prepare_for_class(snake_case     ,					snake_case     ,					return_labels=snake_case   )
              check_equivalence(snake_case     ,					snake_case     ,					snake_case     ,					{'output_hidden_states': True}   )




      def 	__UpperCAmelCase			(					self :		str   ):



          """simple docstring"""





          _snake_case : List[str]												=		self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_image_classification(*snake_case   )




      @slow
      def 	__UpperCAmelCase			(					self :		str   ):



          """simple docstring"""





          _snake_case : Optional[Any]												=		TFConvNextModel.from_pretrained('facebook/convnext-tiny-224'   )
          self.assertIsNotNone(snake_case   )



def   lowerCamelCase__       (      )		->    int:



    """simple docstring"""







    _snake_case : Any												=		Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png')
    return image




@require_tf
@require_vision
class 			SCREAMING_SNAKE_CASE					(							unittest.TestCase					):






      '''simple docstring'''






      @cached_property
      def 	__UpperCAmelCase			(					self :		Optional[int]   ):



          """simple docstring"""





          return ConvNextImageProcessor.from_pretrained('facebook/convnext-tiny-224'   ) if is_vision_available() else None




      @slow
      def 	__UpperCAmelCase			(					self :		int   ):



          """simple docstring"""





          _snake_case : List[Any]												=		TFConvNextForImageClassification.from_pretrained('facebook/convnext-tiny-224'   )

          _snake_case : Optional[int]												=		self.default_image_processor
          _snake_case : Dict												=		prepare_img()
          _snake_case : str												=		image_processor(images=snake_case     ,					return_tensors='tf'   )

          # forward pass
          _snake_case : Optional[int]												=		model(**snake_case   )

          # verify the logits
          _snake_case : List[Any]												=		tf.TensorShape((1, 1000)   )
          self.assertEqual(outputs.logits.shape     ,					snake_case   )

          _snake_case : Union[str, Any]												=		tf.constant([-0.0260, -0.4739, 0.1911]   )

          tf.debugging.assert_near(outputs.logits[0, :3]     ,					snake_case     ,					atol=1e-4   )



