

from __future__ import annotations

import inspect
import unittest
from typing import List, Tuple

from transformers import RegNetConfig
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 TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST, TFRegNetForImageClassification, TFRegNetModel


if is_vision_available():
       from PIL import Image

       from transformers import AutoImageProcessor






class 	UpperCAmelCase__		:






  """simple docstring"""


  def __init__(							self						:		Optional[Any]     ,       __lowerCamelCase						:		Optional[Any]     ,       __lowerCamelCase						:		List[str]=3     ,       __lowerCamelCase						:		int=32     ,       __lowerCamelCase						:		str=3     ,       __lowerCamelCase						:		str=10     ,       __lowerCamelCase						:		Optional[int]=[10, 20, 30, 40]     ,       __lowerCamelCase						:		int=[1, 1, 2, 1]     ,       __lowerCamelCase						:		str=True     ,       __lowerCamelCase						:		Dict=True     ,       __lowerCamelCase						:		Tuple="relu"     ,       __lowerCamelCase						:		Union[str, Any]=3     ,       __lowerCamelCase						:		Dict=None     ,       )		->							Optional[Any]:
       SCREAMING_SNAKE_CASE__   =   parent
       SCREAMING_SNAKE_CASE__   =   batch_size
       SCREAMING_SNAKE_CASE__   =   image_size
       SCREAMING_SNAKE_CASE__   =   num_channels
       SCREAMING_SNAKE_CASE__   =   embeddings_size
       SCREAMING_SNAKE_CASE__   =   hidden_sizes
       SCREAMING_SNAKE_CASE__   =   depths
       SCREAMING_SNAKE_CASE__   =   is_training
       SCREAMING_SNAKE_CASE__   =   use_labels
       SCREAMING_SNAKE_CASE__   =   hidden_act
       SCREAMING_SNAKE_CASE__   =   num_labels
       SCREAMING_SNAKE_CASE__   =   scope
       SCREAMING_SNAKE_CASE__   =   len(__lowerCamelCase )






  def 				lowercase_		(							self						:		Union[str, Any] )		->							Dict:
       SCREAMING_SNAKE_CASE__   =   floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] )

       SCREAMING_SNAKE_CASE__   =   None
       if self.use_labels:
            SCREAMING_SNAKE_CASE__   =   ids_tensor([self.batch_size]     ,       self.num_labels )

       SCREAMING_SNAKE_CASE__   =   self.get_config()
       return config, pixel_values, labels






  def 				lowercase_		(							self						:		List[Any] )		->							str:
       return RegNetConfig(
           num_channels=self.num_channels     ,       embeddings_size=self.embeddings_size     ,       hidden_sizes=self.hidden_sizes     ,       depths=self.depths     ,       hidden_act=self.hidden_act     ,       num_labels=self.num_labels     ,       )






  def 				lowercase_		(							self						:		Optional[Any]     ,       __lowerCamelCase						:		Any     ,       __lowerCamelCase						:		Any     ,       __lowerCamelCase						:		Optional[int] )		->							int:
       SCREAMING_SNAKE_CASE__   =   TFRegNetModel(config=__lowerCamelCase )
       SCREAMING_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 				lowercase_		(							self						:		Union[str, Any]     ,       __lowerCamelCase						:		int     ,       __lowerCamelCase						:		Optional[Any]     ,       __lowerCamelCase						:		Union[str, Any] )		->							Optional[int]:
       SCREAMING_SNAKE_CASE__   =   self.num_labels
       SCREAMING_SNAKE_CASE__   =   TFRegNetForImageClassification(__lowerCamelCase )
       SCREAMING_SNAKE_CASE__   =   model(__lowerCamelCase     ,       labels=__lowerCamelCase     ,       training=__lowerCamelCase )
       self.parent.assertEqual(result.logits.shape     ,       (self.batch_size, self.num_labels) )







  def 				lowercase_		(							self						:		List[Any] )		->							str:
       SCREAMING_SNAKE_CASE__   =   self.prepare_config_and_inputs()
       SCREAMING_SNAKE_CASE__,SCREAMING_SNAKE_CASE__,SCREAMING_SNAKE_CASE__   =   config_and_inputs
       SCREAMING_SNAKE_CASE__   =   {'''pixel_values''': pixel_values}
       return config, inputs_dict






@require_tf
class 	UpperCAmelCase__		(	A__						,					A__						,					unittest.TestCase	):






  """simple docstring"""


  a									= (TFRegNetModel, TFRegNetForImageClassification) if is_tf_available() else ()
  a									= (
      {"feature-extraction": TFRegNetModel, "image-classification": TFRegNetForImageClassification}
      if is_tf_available()
      else {}
  )

  a									= False
  a									= False
  a									= False
  a									= False
  a									= False






  def 				lowercase_		(							self						:		int )		->							Optional[int]:
       SCREAMING_SNAKE_CASE__   =   TFRegNetModelTester(self )
       SCREAMING_SNAKE_CASE__   =   ConfigTester(self     ,       config_class=__lowerCamelCase     ,       has_text_modality=__lowerCamelCase )






  def 				lowercase_		(							self						:		str )		->							Any:
       return






  @unittest.skip(reason='''RegNet does not use inputs_embeds''' )
  def 				lowercase_		(							self						:		List[str] )		->							Optional[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 				lowercase_		(							self						:		Any )		->							List[str]:
       super().test_keras_fit()






  @unittest.skip(reason='''RegNet does not support input and output embeddings''' )
  def 				lowercase_		(							self						:		Dict )		->							List[Any]:
       pass






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

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

            SCREAMING_SNAKE_CASE__   =   ['''pixel_values''']
            self.assertListEqual(arg_names[:1]     ,       __lowerCamelCase )






  def 				lowercase_		(							self						:		Any )		->							Dict:
       SCREAMING_SNAKE_CASE__   =   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*__lowerCamelCase )






  def 				lowercase_		(							self						:		List[str] )		->							Tuple:
       def check_hidden_states_output(__lowerCamelCase						:		Tuple     ,       __lowerCamelCase						:		Union[str, Any]     ,       __lowerCamelCase						:		Optional[Any] ):
            SCREAMING_SNAKE_CASE__   =   model_class(__lowerCamelCase )
            SCREAMING_SNAKE_CASE__   =   model(**self._prepare_for_class(__lowerCamelCase     ,       __lowerCamelCase )     ,       training=__lowerCamelCase )

            SCREAMING_SNAKE_CASE__   =   outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

            SCREAMING_SNAKE_CASE__   =   self.model_tester.num_stages
            self.assertEqual(len(__lowerCamelCase )     ,       expected_num_stages + 1 )

            # RegNet'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 // 2, self.model_tester.image_size // 2]     ,       )

       SCREAMING_SNAKE_CASE__,SCREAMING_SNAKE_CASE__   =   self.model_tester.prepare_config_and_inputs_for_common()
       SCREAMING_SNAKE_CASE__   =   ['''basic''', '''bottleneck''']
       for model_class in self.all_model_classes:
            for layer_type in layers_type:
                 SCREAMING_SNAKE_CASE__   =   layer_type
                 SCREAMING_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"]
                 SCREAMING_SNAKE_CASE__   =   True

                 check_hidden_states_output(__lowerCamelCase     ,       __lowerCamelCase     ,       __lowerCamelCase )






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

       def check_equivalence(__lowerCamelCase						:		Optional[int]     ,       __lowerCamelCase						:		List[str]     ,       __lowerCamelCase						:		int     ,       __lowerCamelCase						:		Union[str, Any]={} ):
            SCREAMING_SNAKE_CASE__   =   model(__lowerCamelCase     ,       return_dict=__lowerCamelCase     ,       **__lowerCamelCase )
            SCREAMING_SNAKE_CASE__   =   model(__lowerCamelCase     ,       return_dict=__lowerCamelCase     ,       **__lowerCamelCase ).to_tuple()

            def recursive_check(__lowerCamelCase						:		Union[str, Any]     ,       __lowerCamelCase						:		List[str] ):
                 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:
            SCREAMING_SNAKE_CASE__   =   model_class(__lowerCamelCase )

            SCREAMING_SNAKE_CASE__   =   self._prepare_for_class(__lowerCamelCase     ,       __lowerCamelCase )
            SCREAMING_SNAKE_CASE__   =   self._prepare_for_class(__lowerCamelCase     ,       __lowerCamelCase )
            check_equivalence(__lowerCamelCase     ,       __lowerCamelCase     ,       __lowerCamelCase )

            SCREAMING_SNAKE_CASE__   =   self._prepare_for_class(__lowerCamelCase     ,       __lowerCamelCase     ,       return_labels=__lowerCamelCase )
            SCREAMING_SNAKE_CASE__   =   self._prepare_for_class(__lowerCamelCase     ,       __lowerCamelCase     ,       return_labels=__lowerCamelCase )
            check_equivalence(__lowerCamelCase     ,       __lowerCamelCase     ,       __lowerCamelCase )

            SCREAMING_SNAKE_CASE__   =   self._prepare_for_class(__lowerCamelCase     ,       __lowerCamelCase )
            SCREAMING_SNAKE_CASE__   =   self._prepare_for_class(__lowerCamelCase     ,       __lowerCamelCase )
            check_equivalence(__lowerCamelCase     ,       __lowerCamelCase     ,       __lowerCamelCase     ,       {'''output_hidden_states''': True} )

            SCREAMING_SNAKE_CASE__   =   self._prepare_for_class(__lowerCamelCase     ,       __lowerCamelCase     ,       return_labels=__lowerCamelCase )
            SCREAMING_SNAKE_CASE__   =   self._prepare_for_class(__lowerCamelCase     ,       __lowerCamelCase     ,       return_labels=__lowerCamelCase )
            check_equivalence(__lowerCamelCase     ,       __lowerCamelCase     ,       __lowerCamelCase     ,       {'''output_hidden_states''': True} )






  def 				lowercase_		(							self						:		int )		->							str:
       SCREAMING_SNAKE_CASE__   =   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_image_classification(*__lowerCamelCase )







  @slow
  def 				lowercase_		(							self						:		Dict )		->							Any:
       for model_name in TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
            SCREAMING_SNAKE_CASE__   =   TFRegNetModel.from_pretrained(__lowerCamelCase )
            self.assertIsNotNone(__lowerCamelCase )



def     UpperCAmelCase_       (						):





     '''simple docstring'''




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







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






  """simple docstring"""


  @cached_property
  def 				lowercase_		(							self						:		Any )		->							Optional[int]:
       return (
           AutoImageProcessor.from_pretrained(TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[0] )
           if is_vision_available()
           else None
       )







  @slow
  def 				lowercase_		(							self						:		Optional[Any] )		->							str:
       SCREAMING_SNAKE_CASE__   =   TFRegNetForImageClassification.from_pretrained(TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[0] )

       SCREAMING_SNAKE_CASE__   =   self.default_image_processor
       SCREAMING_SNAKE_CASE__   =   prepare_img()
       SCREAMING_SNAKE_CASE__   =   image_processor(images=__lowerCamelCase     ,       return_tensors='''tf''' )

       # forward pass
       SCREAMING_SNAKE_CASE__   =   model(**__lowerCamelCase     ,       training=__lowerCamelCase )

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

       SCREAMING_SNAKE_CASE__   =   tf.constant([-0.4180, -1.5051, -3.4836] )

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





from ...configuration_utils import PretrainedConfig
from ...utils import logging


_SCREAMING_SNAKE_CASE					:  List[str]             =      logging.get_logger(__name__)

_SCREAMING_SNAKE_CASE					:  Tuple             =      {
    '''transfo-xl-wt103''': '''https://huggingface.co/transfo-xl-wt103/resolve/main/config.json''',
}






class 	UpperCAmelCase__		(	A__	):






  """simple docstring"""


  a									= "transfo-xl"
  a									= ["mems"]
  a									= {
      "n_token": "vocab_size",
      "hidden_size": "d_model",
      "num_attention_heads": "n_head",
      "num_hidden_layers": "n_layer",
  }






  def __init__(							self						:		Any     ,       __lowerCamelCase						:		int=26_7735     ,       __lowerCamelCase						:		Any=[2_0000, 4_0000, 20_0000]     ,       __lowerCamelCase						:		Dict=1024     ,       __lowerCamelCase						:		Optional[int]=1024     ,       __lowerCamelCase						:		List[Any]=16     ,       __lowerCamelCase						:		Union[str, Any]=64     ,       __lowerCamelCase						:		Dict=4096     ,       __lowerCamelCase						:		int=4     ,       __lowerCamelCase						:		Dict=False     ,       __lowerCamelCase						:		Tuple=18     ,       __lowerCamelCase						:		Optional[int]=1600     ,       __lowerCamelCase						:		str=1000     ,       __lowerCamelCase						:		Any=True     ,       __lowerCamelCase						:		Dict=True     ,       __lowerCamelCase						:		List[Any]=0     ,       __lowerCamelCase						:		int=-1     ,       __lowerCamelCase						:		Tuple=True     ,       __lowerCamelCase						:		List[Any]=0.1     ,       __lowerCamelCase						:		int=0.0     ,       __lowerCamelCase						:		int=True     ,       __lowerCamelCase						:		str="normal"     ,       __lowerCamelCase						:		List[str]=0.01     ,       __lowerCamelCase						:		Any=0.01     ,       __lowerCamelCase						:		List[str]=0.02     ,       __lowerCamelCase						:		List[str]=1e-5     ,       __lowerCamelCase						:		Union[str, Any]=0     ,       **__lowerCamelCase						:		int     ,       )		->							Optional[Any]:
       SCREAMING_SNAKE_CASE__   =   vocab_size
       SCREAMING_SNAKE_CASE__   =   []
       self.cutoffs.extend(__lowerCamelCase )
       if proj_share_all_but_first:
            SCREAMING_SNAKE_CASE__   =   [False] + [True] * len(self.cutoffs )
       else:
            SCREAMING_SNAKE_CASE__   =   [False] + [False] * len(self.cutoffs )
       SCREAMING_SNAKE_CASE__   =   d_model
       SCREAMING_SNAKE_CASE__   =   d_embed
       SCREAMING_SNAKE_CASE__   =   d_head
       SCREAMING_SNAKE_CASE__   =   d_inner
       SCREAMING_SNAKE_CASE__   =   div_val
       SCREAMING_SNAKE_CASE__   =   pre_lnorm
       SCREAMING_SNAKE_CASE__   =   n_layer
       SCREAMING_SNAKE_CASE__   =   n_head
       SCREAMING_SNAKE_CASE__   =   mem_len
       SCREAMING_SNAKE_CASE__   =   same_length
       SCREAMING_SNAKE_CASE__   =   attn_type
       SCREAMING_SNAKE_CASE__   =   clamp_len
       SCREAMING_SNAKE_CASE__   =   sample_softmax
       SCREAMING_SNAKE_CASE__   =   adaptive
       SCREAMING_SNAKE_CASE__   =   dropout
       SCREAMING_SNAKE_CASE__   =   dropatt
       SCREAMING_SNAKE_CASE__   =   untie_r
       SCREAMING_SNAKE_CASE__   =   init
       SCREAMING_SNAKE_CASE__   =   init_range
       SCREAMING_SNAKE_CASE__   =   proj_init_std
       SCREAMING_SNAKE_CASE__   =   init_std
       SCREAMING_SNAKE_CASE__   =   layer_norm_epsilon
       super().__init__(eos_token_id=__lowerCamelCase     ,       **__lowerCamelCase )






  @property
  def 				lowercase_		(							self						:		str )		->							Dict:
       # Message copied from Transformer-XL documentation
       logger.info(f'''The model {self.model_type} is one of the few models that has no sequence length limit.''' )
       return -1







  @max_position_embeddings.setter
  def 				lowercase_		(							self						:		List[str]     ,       __lowerCamelCase						:		Any )		->							List[Any]:
       # Message copied from Transformer-XL documentation
       raise NotImplementedError(
           f'''The model {self.model_type} is one of the few models that has no sequence length limit.''' )


