'''simple docstring'''

from __future__ import annotations

import inspect
import unittest
from math import floor

import numpy as np

from transformers import CvtConfig
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 TFCvtForImageClassification, TFCvtModel
  from transformers.models.cvt.modeling_tf_cvt import TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
  from PIL import Image

  from transformers import AutoImageProcessor



class 			UpperCAmelCase  (						a__					):





       '''simple docstring'''



       def _lowerCAmelCase(						self )  ->  Any:
        lowercase__				:    Optional[Any]  =					self.config_class(**self.inputs_dict )
        self.parent.assertTrue(hasattr(SCREAMING_SNAKE_CASE_	,   '''embed_dim''' ) )
        self.parent.assertTrue(hasattr(SCREAMING_SNAKE_CASE_	,   '''num_heads''' ) )



class 			UpperCAmelCase  :





       '''simple docstring'''



       def __init__(						self	,   __lowerCAmelCase	,   __lowerCAmelCase=13	,   __lowerCAmelCase=64	,   __lowerCAmelCase=3	,   __lowerCAmelCase=[16, 48, 96]	,   __lowerCAmelCase=[1, 3, 6]	,   __lowerCAmelCase=[1, 2, 10]	,   __lowerCAmelCase=[7, 3, 3]	,   __lowerCAmelCase=[4, 2, 2]	,   __lowerCAmelCase=[2, 1, 1]	,   __lowerCAmelCase=[2, 2, 2]	,   __lowerCAmelCase=[False, False, True]	,   __lowerCAmelCase=[0.0, 0.0, 0.0]	,   __lowerCAmelCase=0.0_2	,   __lowerCAmelCase=1E-12	,   __lowerCAmelCase=True	,   __lowerCAmelCase=True	,   __lowerCAmelCase=2	,   )  ->  Optional[Any]:
        lowercase__				:    List[Any]  =					parent
        lowercase__				:    str  =					batch_size
        lowercase__				:    int  =					image_size
        lowercase__				:    Any  =					patch_sizes
        lowercase__				:    Optional[int]  =					patch_stride
        lowercase__				:    int  =					patch_padding
        lowercase__				:    int  =					is_training
        lowercase__				:    str  =					use_labels
        lowercase__				:    Union[str, Any]  =					num_labels
        lowercase__				:    Optional[int]  =					num_channels
        lowercase__				:    List[Any]  =					embed_dim
        lowercase__				:    Optional[int]  =					num_heads
        lowercase__				:    List[str]  =					stride_kv
        lowercase__				:    Optional[int]  =					depth
        lowercase__				:    Tuple  =					cls_token
        lowercase__				:    Optional[int]  =					attention_drop_rate
        lowercase__				:    Any  =					initializer_range
        lowercase__				:    Union[str, Any]  =					layer_norm_eps

       def _lowerCAmelCase(						self )  ->  str:
        lowercase__				:    Optional[Any]  =					floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] )

        lowercase__				:    Optional[int]  =					None
        if self.use_labels:
         # create a random int32 tensor of given shape
         lowercase__				:    List[Any]  =					ids_tensor([self.batch_size]	,   self.num_labels )

        lowercase__				:    str  =					self.get_config()
        return config, pixel_values, labels

       def _lowerCAmelCase(						self )  ->  Dict:
        return CvtConfig(
            image_size=self.image_size	,   num_labels=self.num_labels	,   num_channels=self.num_channels	,   embed_dim=self.embed_dim	,   num_heads=self.num_heads	,   patch_sizes=self.patch_sizes	,   patch_padding=self.patch_padding	,   patch_stride=self.patch_stride	,   stride_kv=self.stride_kv	,   depth=self.depth	,   cls_token=self.cls_token	,   attention_drop_rate=self.attention_drop_rate	,   initializer_range=self.initializer_range	,   )

       def _lowerCAmelCase(						self	,   __lowerCAmelCase	,   __lowerCAmelCase	,   __lowerCAmelCase )  ->  Optional[Any]:
        lowercase__				:    Union[str, Any]  =					TFCvtModel(config=SCREAMING_SNAKE_CASE_ )
        lowercase__				:    Any  =					model(SCREAMING_SNAKE_CASE_	,   training=SCREAMING_SNAKE_CASE_ )
        lowercase__				:    int  =					(self.image_size, self.image_size)
        lowercase__				:    Optional[int]  =					image_size[0], image_size[1]
        for i in range(len(self.depth ) ):
         lowercase__				:    List[str]  =					floor(((height + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1 )
         lowercase__				:    str  =					floor(((width + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1 )
        self.parent.assertEqual(result.last_hidden_state.shape	,   (self.batch_size, self.embed_dim[-1], height, width) )

       def _lowerCAmelCase(						self	,   __lowerCAmelCase	,   __lowerCAmelCase	,   __lowerCAmelCase )  ->  Optional[Any]:
        lowercase__				:    str  =					self.num_labels
        lowercase__				:    Any  =					TFCvtForImageClassification(SCREAMING_SNAKE_CASE_ )
        lowercase__				:    Optional[Any]  =					model(SCREAMING_SNAKE_CASE_	,   labels=SCREAMING_SNAKE_CASE_	,   training=SCREAMING_SNAKE_CASE_ )
        self.parent.assertEqual(result.logits.shape	,   (self.batch_size, self.num_labels) )


       def _lowerCAmelCase(						self )  ->  Optional[Any]:
        lowercase__				:    Any  =					self.prepare_config_and_inputs()
        lowercase__				:    int  =					config_and_inputs
        lowercase__				:    Dict  =					{'pixel_values': pixel_values}
        return config, inputs_dict



@require_tf
class 			UpperCAmelCase  (						a__    ,						a__    ,						unittest.TestCase					):





       '''simple docstring'''



       SCREAMING_SNAKE_CASE							=    (TFCvtModel, TFCvtForImageClassification) if is_tf_available() else ()
       SCREAMING_SNAKE_CASE							=    (
           {"feature-extraction": TFCvtModel, "image-classification": TFCvtForImageClassification}
           if is_tf_available()
           else {}
       )
       SCREAMING_SNAKE_CASE							=    False
       SCREAMING_SNAKE_CASE							=    False
       SCREAMING_SNAKE_CASE							=    False
       SCREAMING_SNAKE_CASE							=    False
       SCREAMING_SNAKE_CASE							=    False

       def _lowerCAmelCase(						self )  ->  Union[str, Any]:
        lowercase__				:    Tuple  =					TFCvtModelTester(self )
        lowercase__				:    List[str]  =					TFCvtConfigTester(self	,   config_class=SCREAMING_SNAKE_CASE_	,   has_text_modality=SCREAMING_SNAKE_CASE_	,   hidden_size=37 )

       def _lowerCAmelCase(						self )  ->  List[str]:
        self.config_tester.create_and_test_config_common_properties()
        self.config_tester.create_and_test_config_to_json_string()
        self.config_tester.create_and_test_config_to_json_file()
        self.config_tester.create_and_test_config_from_and_save_pretrained()
        self.config_tester.create_and_test_config_with_num_labels()
        self.config_tester.check_config_can_be_init_without_params()
        self.config_tester.check_config_arguments_init()

       @unittest.skip(reason='''Cvt does not output attentions''' )
       def _lowerCAmelCase(						self )  ->  Tuple:
        pass

       @unittest.skip(reason='''Cvt does not use inputs_embeds''' )
       def _lowerCAmelCase(						self )  ->  int:
        pass

       @unittest.skip(reason='''Cvt does not support input and output embeddings''' )
       def _lowerCAmelCase(						self )  ->  List[str]:
        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.'''	,   )
       def _lowerCAmelCase(						self )  ->  List[str]:
        super().test_dataset_conversion()

       @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 )  ->  Any:
        super().test_keras_fit()

       @unittest.skip(reason='''Get `Failed to determine best cudnn convolution algo.` error after using TF 2.12+cuda 11.8''' )
       def _lowerCAmelCase(						self )  ->  Any:
        lowercase__				:    Dict  =					tf.keras.mixed_precision.Policy('''mixed_float16''' )
        tf.keras.mixed_precision.set_global_policy(SCREAMING_SNAKE_CASE_ )
        super().test_keras_fit()
        tf.keras.mixed_precision.set_global_policy('''float32''' )

       def _lowerCAmelCase(						self )  ->  Optional[int]:
        lowercase__				:    Any  =					self.model_tester.prepare_config_and_inputs_for_common()

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

         lowercase__				:    Tuple  =					['pixel_values']
         self.assertListEqual(arg_names[:1]	,   SCREAMING_SNAKE_CASE_ )

       def _lowerCAmelCase(						self )  ->  List[str]:
        def check_hidden_states_output(__lowerCAmelCase	,   __lowerCAmelCase	,   __lowerCAmelCase ):
         lowercase__				:    Union[str, Any]  =					model_class(SCREAMING_SNAKE_CASE_ )

         lowercase__				:    Tuple  =					model(**self._prepare_for_class(SCREAMING_SNAKE_CASE_	,   SCREAMING_SNAKE_CASE_ ) )
         lowercase__				:    Optional[Any]  =					outputs.hidden_states

         lowercase__				:    List[Any]  =					len(self.model_tester.depth )
         self.assertEqual(len(SCREAMING_SNAKE_CASE_ )	,   SCREAMING_SNAKE_CASE_ )

         # verify the first hidden states (first block)
         self.assertListEqual(
             list(hidden_states[0].shape[-3:] )	,   [
                 self.model_tester.embed_dim[0],
                 self.model_tester.image_size // 4,
                 self.model_tester.image_size // 4,
             ]	,   )

        lowercase__				:    Any  =					self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
         lowercase__				:    Tuple  =					True
         check_hidden_states_output(SCREAMING_SNAKE_CASE_	,   SCREAMING_SNAKE_CASE_	,   SCREAMING_SNAKE_CASE_ )

         # check that output_hidden_states also work using config
         del inputs_dict["output_hidden_states"]
         lowercase__				:    Union[str, Any]  =					True

         check_hidden_states_output(SCREAMING_SNAKE_CASE_	,   SCREAMING_SNAKE_CASE_	,   SCREAMING_SNAKE_CASE_ )

       def _lowerCAmelCase(						self )  ->  Any:
        lowercase__				:    Dict  =					self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE_ )

       def _lowerCAmelCase(						self )  ->  Any:
        lowercase__				:    Optional[int]  =					self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_image_classification(*SCREAMING_SNAKE_CASE_ )


       @slow
       def _lowerCAmelCase(						self )  ->  Optional[int]:
        for model_name in TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         lowercase__				:    Dict  =					TFCvtModel.from_pretrained(SCREAMING_SNAKE_CASE_ )
         self.assertIsNotNone(SCREAMING_SNAKE_CASE_ )


def 			__UpperCamelCase       (   ):
 lowercase__				:    List[str]  =					Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''     )
 return image





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





       '''simple docstring'''



       @cached_property
       def _lowerCAmelCase(						self )  ->  Any:
        return AutoImageProcessor.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0] )


       @slow
       def _lowerCAmelCase(						self )  ->  str:
        lowercase__				:    Optional[int]  =					TFCvtForImageClassification.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0] )

        lowercase__				:    str  =					self.default_image_processor
        lowercase__				:    Tuple  =					prepare_img()
        lowercase__				:    List[Any]  =					image_processor(images=SCREAMING_SNAKE_CASE_	,   return_tensors='''tf''' )

        # forward pass
        lowercase__				:    Tuple  =					model(**SCREAMING_SNAKE_CASE_ )

        # verify the logits
        lowercase__				:    Any  =					tf.TensorShape((1, 1000) )
        self.assertEqual(outputs.logits.shape	,   SCREAMING_SNAKE_CASE_ )

        lowercase__				:    List[str]  =					tf.constant([0.9_2_8_5, 0.9_0_1_5, -0.3_1_5_0] )
        self.assertTrue(np.allclose(outputs.logits[0, :3].numpy()	,   SCREAMING_SNAKE_CASE_	,   atol=1E-4 ) )






from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available


__UpperCAmelCase          =       {
    '''configuration_transfo_xl''': ['''TRANSFO_XL_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''TransfoXLConfig'''],
    '''tokenization_transfo_xl''': ['''TransfoXLCorpus''', '''TransfoXLTokenizer'''],
}

try:
    if not is_torch_available():
        raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
    pass
else:
    __UpperCAmelCase          =       [
        '''TRANSFO_XL_PRETRAINED_MODEL_ARCHIVE_LIST''',
        '''AdaptiveEmbedding''',
        '''TransfoXLForSequenceClassification''',
        '''TransfoXLLMHeadModel''',
        '''TransfoXLModel''',
        '''TransfoXLPreTrainedModel''',
        '''load_tf_weights_in_transfo_xl''',
    ]

try:
    if not is_tf_available():
        raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
    pass
else:
    __UpperCAmelCase          =       [
        '''TF_TRANSFO_XL_PRETRAINED_MODEL_ARCHIVE_LIST''',
        '''TFAdaptiveEmbedding''',
        '''TFTransfoXLForSequenceClassification''',
        '''TFTransfoXLLMHeadModel''',
        '''TFTransfoXLMainLayer''',
        '''TFTransfoXLModel''',
        '''TFTransfoXLPreTrainedModel''',
    ]


if TYPE_CHECKING:
    from .configuration_transfo_xl import TRANSFO_XL_PRETRAINED_CONFIG_ARCHIVE_MAP, TransfoXLConfig
    from .tokenization_transfo_xl import TransfoXLCorpus, TransfoXLTokenizer

    try:
        if not is_torch_available():
            raise OptionalDependencyNotAvailable()
    except OptionalDependencyNotAvailable:
        pass
    else:
        from .modeling_transfo_xl import (
            TRANSFO_XL_PRETRAINED_MODEL_ARCHIVE_LIST,
            AdaptiveEmbedding,
            TransfoXLForSequenceClassification,
            TransfoXLLMHeadModel,
            TransfoXLModel,
            TransfoXLPreTrainedModel,
            load_tf_weights_in_transfo_xl,
        )

    try:
        if not is_tf_available():
            raise OptionalDependencyNotAvailable()
    except OptionalDependencyNotAvailable:
        pass
    else:
        from .modeling_tf_transfo_xl import (
            TF_TRANSFO_XL_PRETRAINED_MODEL_ARCHIVE_LIST,
            TFAdaptiveEmbedding,
            TFTransfoXLForSequenceClassification,
            TFTransfoXLLMHeadModel,
            TFTransfoXLMainLayer,
            TFTransfoXLModel,
            TFTransfoXLPreTrainedModel,
        )

else:
    import sys

    __UpperCAmelCase          =       _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)



