

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  __lowerCamelCase				:







      def __init__(     self							,  lowerCamelCase							,  lowerCamelCase=13							,  lowerCamelCase=32							,  lowerCamelCase=3							,  lowerCamelCase=4							,  lowerCamelCase=[10, 20, 30, 40]							,  lowerCamelCase=[2, 2, 3, 2]							,  lowerCamelCase=True							,  lowerCamelCase=True							,  lowerCamelCase=37							,  lowerCamelCase="gelu"							,  lowerCamelCase=10							,  lowerCamelCase=0.02							,  lowerCamelCase=3							,  lowerCamelCase=None							,  )						->       List[Any]:
        snake_case_       						=      parent
        snake_case_       						=      batch_size
        snake_case_       						=      image_size
        snake_case_       						=      num_channels
        snake_case_       						=      num_stages
        snake_case_       						=      hidden_sizes
        snake_case_       						=      depths
        snake_case_       						=      is_training
        snake_case_       						=      use_labels
        snake_case_       						=      intermediate_size
        snake_case_       						=      hidden_act
        snake_case_       						=      type_sequence_label_size
        snake_case_       						=      initializer_range
        snake_case_       						=      scope







      def 	lowerCAmelCase_    (     self   )						->       Any:
        snake_case_       						=      floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]   )

        snake_case_       						=      None
        if self.use_labels:
          snake_case_       						=      ids_tensor([self.batch_size]							,  self.type_sequence_label_size   )

        snake_case_       						=      self.get_config()

        return config, pixel_values, labels







      def 	lowerCAmelCase_    (     self   )						->       Optional[int]:
        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=lowerCamelCase							,  initializer_range=self.initializer_range							,  )







      def 	lowerCAmelCase_    (     self							,  lowerCamelCase							,  lowerCamelCase							,  lowerCamelCase   )						->       str:
        snake_case_       						=      TFConvNextModel(config=lowerCamelCase   )
        snake_case_       						=      model(lowerCamelCase							,  training=lowerCamelCase   )
        # 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 	lowerCAmelCase_    (     self							,  lowerCamelCase							,  lowerCamelCase							,  lowerCamelCase   )						->       str:
        snake_case_       						=      self.type_sequence_label_size
        snake_case_       						=      TFConvNextForImageClassification(lowerCamelCase   )
        snake_case_       						=      model(lowerCamelCase							,  labels=lowerCamelCase							,  training=lowerCamelCase   )
        self.parent.assertEqual(result.logits.shape							,  (self.batch_size, self.type_sequence_label_size)   )







      def 	lowerCAmelCase_    (     self   )						->       List[str]:
        snake_case_       						=      self.prepare_config_and_inputs()
        snake_case_						,				snake_case_						,				snake_case_       						=      config_and_inputs
        snake_case_       						=      {"""pixel_values""": pixel_values}
        return config, inputs_dict







@require_tf
class  __lowerCamelCase				(	__snake_case  ,							__snake_case  ,							unittest.TestCase	):
      lowerCamelCase_	:      Union[str, Any]          =					(TFConvNextModel, TFConvNextForImageClassification) if is_tf_available() else ()
      lowerCamelCase_	:      Any          =					(
          {'feature-extraction': TFConvNextModel, 'image-classification': TFConvNextForImageClassification}
          if is_tf_available()
          else {}
      )

      lowerCamelCase_	:      Any          =					False
      lowerCamelCase_	:      List[str]          =					False
      lowerCamelCase_	:      Any          =					False
      lowerCamelCase_	:      Dict          =					False
      lowerCamelCase_	:      Optional[Any]          =					False







      def 	lowerCAmelCase_    (     self   )						->       str:
        snake_case_       						=      TFConvNextModelTester(self   )
        snake_case_       						=      ConfigTester(
            self							,  config_class=lowerCamelCase							,  has_text_modality=lowerCamelCase							,  hidden_size=37							,  )







      @unittest.skip(reason="""ConvNext does not use inputs_embeds"""   )
      def 	lowerCAmelCase_    (     self   )						->       Any:
        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 	lowerCAmelCase_    (     self   )						->       Tuple:
        super().test_keras_fit()







      @unittest.skip(reason="""ConvNext does not support input and output embeddings"""   )
      def 	lowerCAmelCase_    (     self   )						->       List[str]:
        pass







      def 	lowerCAmelCase_    (     self   )						->       List[str]:
        snake_case_						,				snake_case_       						=      self.model_tester.prepare_config_and_inputs_for_common()

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

          snake_case_       						=      ["""pixel_values"""]
          self.assertListEqual(arg_names[:1]							,  lowerCamelCase   )







      def 	lowerCAmelCase_    (     self   )						->       List[Any]:
        snake_case_       						=      self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*lowerCamelCase   )







      @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 	lowerCAmelCase_    (     self   )						->       Optional[int]:
        super().test_dataset_conversion()







      def 	lowerCAmelCase_    (     self   )						->       str:
        def check_hidden_states_output(lowerCamelCase							,  lowerCamelCase							,  lowerCamelCase   ):
          snake_case_       						=      model_class(lowerCamelCase   )

          snake_case_       						=      model(**self._prepare_for_class(lowerCamelCase							,  lowerCamelCase   )   )
          snake_case_       						=      outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

          snake_case_       						=      self.model_tester.num_stages
          self.assertEqual(len(lowerCamelCase   )							,  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_       						=      self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
          snake_case_       						=      True
          check_hidden_states_output(lowerCamelCase							,  lowerCamelCase							,  lowerCamelCase   )

          # check that output_hidden_states also work using config
          del inputs_dict["output_hidden_states"]
          snake_case_       						=      True

          check_hidden_states_output(lowerCamelCase							,  lowerCamelCase							,  lowerCamelCase   )







      def 	lowerCAmelCase_    (     self   )						->       Union[str, Any]:
        snake_case_						,				snake_case_       						=      self.model_tester.prepare_config_and_inputs_for_common()

        def check_equivalence(lowerCamelCase							,  lowerCamelCase							,  lowerCamelCase							,  lowerCamelCase={}   ):
          snake_case_       						=      model(lowerCamelCase							,  return_dict=lowerCamelCase							,  **lowerCamelCase   )
          snake_case_       						=      model(lowerCamelCase							,  return_dict=lowerCamelCase							,  **lowerCamelCase   ).to_tuple()

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

            recursive_check(lowerCamelCase							,  lowerCamelCase   )

        for model_class in self.all_model_classes:
          snake_case_       						=      model_class(lowerCamelCase   )

          snake_case_       						=      self._prepare_for_class(lowerCamelCase							,  lowerCamelCase   )
          snake_case_       						=      self._prepare_for_class(lowerCamelCase							,  lowerCamelCase   )
          check_equivalence(lowerCamelCase							,  lowerCamelCase							,  lowerCamelCase   )

          snake_case_       						=      self._prepare_for_class(lowerCamelCase							,  lowerCamelCase							,  return_labels=lowerCamelCase   )
          snake_case_       						=      self._prepare_for_class(lowerCamelCase							,  lowerCamelCase							,  return_labels=lowerCamelCase   )
          check_equivalence(lowerCamelCase							,  lowerCamelCase							,  lowerCamelCase   )

          snake_case_       						=      self._prepare_for_class(lowerCamelCase							,  lowerCamelCase   )
          snake_case_       						=      self._prepare_for_class(lowerCamelCase							,  lowerCamelCase   )
          check_equivalence(lowerCamelCase							,  lowerCamelCase							,  lowerCamelCase							,  {"""output_hidden_states""": True}   )

          snake_case_       						=      self._prepare_for_class(lowerCamelCase							,  lowerCamelCase							,  return_labels=lowerCamelCase   )
          snake_case_       						=      self._prepare_for_class(lowerCamelCase							,  lowerCamelCase							,  return_labels=lowerCamelCase   )
          check_equivalence(lowerCamelCase							,  lowerCamelCase							,  lowerCamelCase							,  {"""output_hidden_states""": True}   )







      def 	lowerCAmelCase_    (     self   )						->       List[Any]:
        snake_case_       						=      self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_image_classification(*lowerCamelCase   )







      @slow
      def 	lowerCAmelCase_    (     self   )						->       Tuple:
        snake_case_       						=      TFConvNextModel.from_pretrained("""facebook/convnext-tiny-224"""   )
        self.assertIsNotNone(lowerCamelCase   )



def    UpperCamelCase(					)	->				Tuple:





  '''simple docstring'''





  snake_case_       						=      Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""							)
  return image







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







      @cached_property
      def 	lowerCAmelCase_    (     self   )						->       Union[str, Any]:
        return ConvNextImageProcessor.from_pretrained("""facebook/convnext-tiny-224"""   ) if is_vision_available() else None







      @slow
      def 	lowerCAmelCase_    (     self   )						->       Dict:
        snake_case_       						=      TFConvNextForImageClassification.from_pretrained("""facebook/convnext-tiny-224"""   )

        snake_case_       						=      self.default_image_processor
        snake_case_       						=      prepare_img()
        snake_case_       						=      image_processor(images=lowerCamelCase							,  return_tensors="""tf"""   )

        # forward pass
        snake_case_       						=      model(**lowerCamelCase   )

        # verify the logits
        snake_case_       						=      tf.TensorShape((1, 1000)   )
        self.assertEqual(outputs.logits.shape							,  lowerCamelCase   )

        snake_case_       						=      tf.constant([-0.0260, -0.4739, 0.1911]   )

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

'''simple docstring'''



from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxSeqaSeqConfigWithPast
from ...utils import logging


UpperCamelCase__					:						Any											=     logging.get_logger(__name__)

class       _a (_lowerCamelCase):





       """simple docstring"""




       SCREAMING_SNAKE_CASE					=     'mt5'
       SCREAMING_SNAKE_CASE					=     ['past_key_values']






       def __init__(							self				,							A__=25_01_12				,							A__=5_12				,							A__=64				,							A__=10_24				,							A__=8				,							A__=None				,							A__=6				,							A__=32				,							A__=1_28				,							A__=0.1				,							A__=1E-6				,							A__=1.0				,							A__="gated-gelu"				,							A__=True				,							A__=True				,							A__="T5Tokenizer"				,							A__=False				,							A__=0				,							A__=1				,							A__=0				,							**A__				,							)			->	Union[str, Any]:
          super().__init__(
              is_encoder_decoder=A__				,							tokenizer_class=A__				,							tie_word_embeddings=A__				,							pad_token_id=A__				,							eos_token_id=A__				,							decoder_start_token_id=A__				,							**A__				,							)
          _SCREAMING_SNAKE_CASE          = vocab_size
          _SCREAMING_SNAKE_CASE          = d_model
          _SCREAMING_SNAKE_CASE          = d_kv
          _SCREAMING_SNAKE_CASE          = d_ff
          _SCREAMING_SNAKE_CASE          = num_layers
          _SCREAMING_SNAKE_CASE          = (
              num_decoder_layers if num_decoder_layers is not None else self.num_layers
          )  # default = symmetry
          _SCREAMING_SNAKE_CASE          = num_heads
          _SCREAMING_SNAKE_CASE          = relative_attention_num_buckets
          _SCREAMING_SNAKE_CASE          = relative_attention_max_distance
          _SCREAMING_SNAKE_CASE          = dropout_rate
          _SCREAMING_SNAKE_CASE          = layer_norm_epsilon
          _SCREAMING_SNAKE_CASE          = initializer_factor
          _SCREAMING_SNAKE_CASE          = feed_forward_proj
          _SCREAMING_SNAKE_CASE          = use_cache

          _SCREAMING_SNAKE_CASE          = self.feed_forward_proj.split("""-"""					)
          _SCREAMING_SNAKE_CASE          = act_info[-1]
          _SCREAMING_SNAKE_CASE          = act_info[0] == """gated"""

          if len(A__					) > 1 and act_info[0] != "gated" or len(A__					) > 2:
             raise ValueError(
                 F"`feed_forward_proj`: {feed_forward_proj} is not a valid activation function of the dense layer."
                 """Please make sure `feed_forward_proj` is of the format `gated-{ACT_FN}` or `{ACT_FN}`, e.g. """
                 """'gated-gelu' or 'relu'"""					)

          # for backwards compatibility
          if feed_forward_proj == "gated-gelu":
             _SCREAMING_SNAKE_CASE          = """gelu_new"""






       @property
       def 				UpperCamelCase (							self					)			->	int:
          return self.d_model






       @property
       def 				UpperCamelCase (							self					)			->	Optional[Any]:
          return self.num_heads






       @property
       def 				UpperCamelCase (							self					)			->	Dict:
          return self.num_layers






class       _a (_lowerCamelCase):





       """simple docstring"""




       @property
       # Copied from transformers.models.t5.configuration_t5.T5OnnxConfig.inputs
       def 				UpperCamelCase (							self					)			->	Mapping[str, Mapping[int, str]]:
          _SCREAMING_SNAKE_CASE          = {
              """input_ids""": {0: """batch""", 1: """encoder_sequence"""},
              """attention_mask""": {0: """batch""", 1: """encoder_sequence"""},
          }
          if self.use_past:
             _SCREAMING_SNAKE_CASE          = """past_encoder_sequence + sequence"""
             _SCREAMING_SNAKE_CASE          = {0: """batch"""}
             _SCREAMING_SNAKE_CASE          = {0: """batch""", 1: """past_decoder_sequence + sequence"""}
          else:
             _SCREAMING_SNAKE_CASE          = {0: """batch""", 1: """decoder_sequence"""}
             _SCREAMING_SNAKE_CASE          = {0: """batch""", 1: """decoder_sequence"""}

          if self.use_past:
             self.fill_with_past_key_values_(A__				,							direction="""inputs"""					)

          return common_inputs






       @property
       # Copied from transformers.models.t5.configuration_t5.T5OnnxConfig.default_onnx_opset
       def 				UpperCamelCase (							self					)			->	int:
          return 13






       @property
       def 				UpperCamelCase (							self					)			->	float:
          return 5E-4






