




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







   def __init__(      self		:						Dict					,						_snake_case		:						List[Any]					,						_snake_case		:						Union[str, Any]=3					,						_snake_case		:						Any=32					,						_snake_case		:						List[str]=3					,						_snake_case		:						Optional[int]=10					,						_snake_case		:						Any=[10, 20, 30, 40]					,						_snake_case		:						List[str]=[1, 1, 2, 1]					,						_snake_case		:						Tuple=True					,						_snake_case		:						List[str]=True					,						_snake_case		:						Optional[Any]="relu"					,						_snake_case		:						List[str]=3					,						_snake_case		:						Union[str, Any]=None					,						):

        """simple docstring"""


        UpperCAmelCase_          =							parent
        UpperCAmelCase_          =							batch_size
        UpperCAmelCase_          =							image_size
        UpperCAmelCase_          =							num_channels
        UpperCAmelCase_          =							embeddings_size
        UpperCAmelCase_          =							hidden_sizes
        UpperCAmelCase_          =							depths
        UpperCAmelCase_          =							is_training
        UpperCAmelCase_          =							use_labels
        UpperCAmelCase_          =							hidden_act
        UpperCAmelCase_          =							num_labels
        UpperCAmelCase_          =							scope
        UpperCAmelCase_          =							len(_snake_case)







   def lowerCamelCase						(      self		:						str):

        """simple docstring"""


        UpperCAmelCase_          =							floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

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

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







   def lowerCamelCase						(      self		:						List[str]):

        """simple docstring"""


        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 lowerCamelCase						(      self		:						Any					,						_snake_case		:						Tuple					,						_snake_case		:						Optional[Any]					,						_snake_case		:						Optional[Any]):

        """simple docstring"""


        UpperCAmelCase_          =							TFRegNetModel(config=_snake_case)
        UpperCAmelCase_          =							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 lowerCamelCase						(      self		:						Optional[int]					,						_snake_case		:						str					,						_snake_case		:						int					,						_snake_case		:						List[str]):

        """simple docstring"""


        UpperCAmelCase_          =							self.num_labels
        UpperCAmelCase_          =							TFRegNetForImageClassification(_snake_case)
        UpperCAmelCase_          =							model(_snake_case					,						labels=_snake_case					,						training=_snake_case)
        self.parent.assertEqual(result.logits.shape					,						(self.batch_size, self.num_labels))







   def lowerCamelCase						(      self		:						int):

        """simple docstring"""


        UpperCAmelCase_          =							self.prepare_config_and_inputs()
        UpperCAmelCase_     ,       UpperCAmelCase_     ,       UpperCAmelCase_          =							config_and_inputs
        UpperCAmelCase_          =							{'''pixel_values''': pixel_values}
        return config, inputs_dict







@require_tf
class __snake_case			(			a ,  a ,  unittest.TestCase		):
   UpperCAmelCase__  :    Optional[int]          =  (TFRegNetModel, TFRegNetForImageClassification) if is_tf_available() else ()
   UpperCAmelCase__  :    str          =  (
       {'''feature-extraction''': TFRegNetModel, '''image-classification''': TFRegNetForImageClassification}
       if is_tf_available()
       else {}
   )

   UpperCAmelCase__  :    Tuple          =  False
   UpperCAmelCase__  :    Optional[Any]          =  False
   UpperCAmelCase__  :    Tuple          =  False
   UpperCAmelCase__  :    Union[str, Any]          =  False
   UpperCAmelCase__  :    Optional[int]          =  False







   def lowerCamelCase						(      self		:						Optional[Any]):

        """simple docstring"""


        UpperCAmelCase_          =							TFRegNetModelTester(self)
        UpperCAmelCase_          =							ConfigTester(self					,						config_class=_snake_case					,						has_text_modality=_snake_case)







   def lowerCamelCase						(      self		:						int):

        """simple docstring"""


        return







   @unittest.skip(reason='''RegNet does not use inputs_embeds''')
   def lowerCamelCase						(      self		:						List[str]):

        """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 lowerCamelCase						(      self		:						Optional[int]):

        """simple docstring"""


        super().test_keras_fit()







   @unittest.skip(reason='''RegNet does not support input and output embeddings''')
   def lowerCamelCase						(      self		:						List[str]):

        """simple docstring"""


        pass







   def lowerCamelCase						(      self		:						int):

        """simple docstring"""


        UpperCAmelCase_     ,       UpperCAmelCase_          =							self.model_tester.prepare_config_and_inputs_for_common()

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

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







   def lowerCamelCase						(      self		:						Tuple):

        """simple docstring"""


        UpperCAmelCase_          =							self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*_snake_case)







   def lowerCamelCase						(      self		:						str):

        """simple docstring"""


        def check_hidden_states_output(_snake_case		:						int					,						_snake_case		:						List[Any]					,						_snake_case		:						List[Any]):
             UpperCAmelCase_          =							model_class(_snake_case)
             UpperCAmelCase_          =							model(**self._prepare_for_class(_snake_case					,						_snake_case)					,						training=_snake_case)

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

             UpperCAmelCase_          =							self.model_tester.num_stages
             self.assertEqual(len(_snake_case)					,						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]					,						)

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

                  check_hidden_states_output(_snake_case					,						_snake_case					,						_snake_case)







   def lowerCamelCase						(      self		:						List[Any]):

        """simple docstring"""


        UpperCAmelCase_     ,       UpperCAmelCase_          =							self.model_tester.prepare_config_and_inputs_for_common()

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

             def recursive_check(_snake_case		:						Tuple					,						_snake_case		:						List[Any]):
                  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:
             UpperCAmelCase_          =							model_class(_snake_case)

             UpperCAmelCase_          =							self._prepare_for_class(_snake_case					,						_snake_case)
             UpperCAmelCase_          =							self._prepare_for_class(_snake_case					,						_snake_case)
             check_equivalence(_snake_case					,						_snake_case					,						_snake_case)

             UpperCAmelCase_          =							self._prepare_for_class(_snake_case					,						_snake_case					,						return_labels=_snake_case)
             UpperCAmelCase_          =							self._prepare_for_class(_snake_case					,						_snake_case					,						return_labels=_snake_case)
             check_equivalence(_snake_case					,						_snake_case					,						_snake_case)

             UpperCAmelCase_          =							self._prepare_for_class(_snake_case					,						_snake_case)
             UpperCAmelCase_          =							self._prepare_for_class(_snake_case					,						_snake_case)
             check_equivalence(_snake_case					,						_snake_case					,						_snake_case					,						{'''output_hidden_states''': True})

             UpperCAmelCase_          =							self._prepare_for_class(_snake_case					,						_snake_case					,						return_labels=_snake_case)
             UpperCAmelCase_          =							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 lowerCamelCase						(      self		:						int):

        """simple docstring"""


        UpperCAmelCase_          =							self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_image_classification(*_snake_case)







   @slow
   def lowerCamelCase						(      self		:						Union[str, Any]):

        """simple docstring"""


        for model_name in TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             UpperCAmelCase_          =							TFRegNetModel.from_pretrained(_snake_case)
             self.assertIsNotNone(_snake_case)







def    A						()   ->      List[str]:







     """simple docstring"""


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







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







   @cached_property
   def lowerCamelCase						(      self		:						Optional[Any]):

        """simple docstring"""


        return (
            AutoImageProcessor.from_pretrained(TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[0])
            if is_vision_available()
            else None
        )







   @slow
   def lowerCamelCase						(      self		:						List[str]):

        """simple docstring"""


        UpperCAmelCase_          =							TFRegNetForImageClassification.from_pretrained(TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[0])

        UpperCAmelCase_          =							self.default_image_processor
        UpperCAmelCase_          =							prepare_img()
        UpperCAmelCase_          =							image_processor(images=_snake_case					,						return_tensors='''tf''')

        # forward pass
        UpperCAmelCase_          =							model(**_snake_case					,						training=_snake_case)

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

        UpperCAmelCase_          =							tf.constant([-0.4_1_8_0, -1.5_0_5_1, -3.4_8_3_6])

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












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


snake_case_ : Any    		=       logging.get_logger(__name__)

snake_case_ : Optional[Any]    		=       {
    "tiiuae/falcon-40b": "https://huggingface.co/tiiuae/falcon-40b/resolve/main/config.json",
    "tiiuae/falcon-7b": "https://huggingface.co/tiiuae/falcon-7b/resolve/main/config.json",
}







class __snake_case			(			a		):
   UpperCAmelCase__  :    Optional[Any]          =  '''falcon'''
   UpperCAmelCase__  :    List[Any]          =  ['''past_key_values''']







   def __init__(      self		:						Union[str, Any]					,						_snake_case		:						List[str]=65024					,						_snake_case		:						int=4544					,						_snake_case		:						int=32					,						_snake_case		:						Any=71					,						_snake_case		:						int=1e-5					,						_snake_case		:						Dict=0.0_2					,						_snake_case		:						int=True					,						_snake_case		:						List[Any]=0.0					,						_snake_case		:						Tuple=0.0					,						_snake_case		:						int=None					,						_snake_case		:						Tuple=False					,						_snake_case		:						Any=False					,						_snake_case		:						str=True					,						_snake_case		:						Any=True					,						_snake_case		:						List[str]=False					,						_snake_case		:						Tuple=11					,						_snake_case		:						Dict=11					,						**_snake_case		:						Optional[int]					,						):

        """simple docstring"""


        UpperCAmelCase_          =							vocab_size
        # Backward compatibility with n_embed kwarg
        UpperCAmelCase_          =							kwargs.pop('''n_embed'''					,						_snake_case)
        UpperCAmelCase_          =							hidden_size if n_embed is None else n_embed
        UpperCAmelCase_          =							num_hidden_layers
        UpperCAmelCase_          =							num_attention_heads
        UpperCAmelCase_          =							layer_norm_epsilon
        UpperCAmelCase_          =							initializer_range
        UpperCAmelCase_          =							use_cache
        UpperCAmelCase_          =							hidden_dropout
        UpperCAmelCase_          =							attention_dropout

        UpperCAmelCase_          =							bos_token_id
        UpperCAmelCase_          =							eos_token_id
        UpperCAmelCase_          =							num_attention_heads if num_kv_heads is None else num_kv_heads
        UpperCAmelCase_          =							alibi
        UpperCAmelCase_          =							new_decoder_architecture
        UpperCAmelCase_          =							multi_query  # Ignored when new_decoder_architecture is True
        UpperCAmelCase_          =							parallel_attn
        UpperCAmelCase_          =							bias

        super().__init__(bos_token_id=_snake_case					,						eos_token_id=_snake_case					,						**_snake_case)







   @property
   def lowerCamelCase						(      self		:						List[Any]):

        """simple docstring"""


        return self.hidden_size // self.num_attention_heads







   @property
   def lowerCamelCase						(      self		:						Union[str, Any]):

        """simple docstring"""


        return not self.alibi






