'''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 (       _SCREAMING_SNAKE_CASE ):




    '''simple docstring'''







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






class 				UpperCAmelCase :




    '''simple docstring'''







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






    def       _UpperCamelCase    (       self )							-> str:
         SCREAMING_SNAKE_CASE					:			List[str]    	=       floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] )

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

         SCREAMING_SNAKE_CASE					:			Dict    	=       self.get_config()
         return config, pixel_values, labels






    def       _UpperCamelCase    (       self )							-> int:
         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       _UpperCamelCase    (       self    ,							lowercase__    ,							lowercase__    ,							lowercase__ )							-> Optional[Any]:
         SCREAMING_SNAKE_CASE					:			Dict    	=       TFCvtModel(config=lowercase__ )
         SCREAMING_SNAKE_CASE					:			List[str]    	=       model(lowercase__    ,							training=lowercase__ )
         SCREAMING_SNAKE_CASE					:			Tuple    	=       (self.image_size, self.image_size)
         SCREAMING_SNAKE_CASE					:			List[str]    	=       image_size[0], image_size[1]
         for i in range(len(self.depth ) ):
              SCREAMING_SNAKE_CASE					:			Optional[int]    	=       floor(((height + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1 )
              SCREAMING_SNAKE_CASE					:			Dict    	=       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       _UpperCamelCase    (       self    ,							lowercase__    ,							lowercase__    ,							lowercase__ )							-> List[Any]:
         SCREAMING_SNAKE_CASE					:			Union[str, Any]    	=       self.num_labels
         SCREAMING_SNAKE_CASE					:			List[Any]    	=       TFCvtForImageClassification(lowercase__ )
         SCREAMING_SNAKE_CASE					:			int    	=       model(lowercase__    ,							labels=lowercase__    ,							training=lowercase__ )
         self.parent.assertEqual(result.logits.shape    ,							(self.batch_size, self.num_labels) )






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






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




    '''simple docstring'''







    snake_case__							:   str							=						(TFCvtModel, TFCvtForImageClassification) if is_tf_available() else ()
    snake_case__							:   str							=						(
        {"feature-extraction": TFCvtModel, "image-classification": TFCvtForImageClassification}
        if is_tf_available()
        else {}
    )
    snake_case__							:   Dict							=						False
    snake_case__							:   str							=						False
    snake_case__							:   Optional[int]							=						False
    snake_case__							:   str							=						False
    snake_case__							:   Optional[Any]							=						False






    def       _UpperCamelCase    (       self )							-> int:
         SCREAMING_SNAKE_CASE					:			int    	=       TFCvtModelTester(self )
         SCREAMING_SNAKE_CASE					:			Optional[Any]    	=       TFCvtConfigTester(self    ,							config_class=lowercase__    ,							has_text_modality=lowercase__    ,							hidden_size=37 )






    def       _UpperCamelCase    (       self )							-> Tuple:
         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       _UpperCamelCase    (       self )							-> List[str]:
         pass






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






    @unittest.skip(reason='Cvt does not support input and output embeddings' )
    def       _UpperCamelCase    (       self )							-> int:
         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       _UpperCamelCase    (       self )							-> Dict:
         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       _UpperCamelCase    (       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       _UpperCamelCase    (       self )							-> Any:
         SCREAMING_SNAKE_CASE					:			List[str]    	=       tf.keras.mixed_precision.Policy('mixed_float16' )
         tf.keras.mixed_precision.set_global_policy(lowercase__ )
         super().test_keras_fit()
         tf.keras.mixed_precision.set_global_policy('float32' )






    def       _UpperCamelCase    (       self )							-> int:
         SCREAMING_SNAKE_CASE					:			Tuple    	=       self.model_tester.prepare_config_and_inputs_for_common()

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

              SCREAMING_SNAKE_CASE					:			Optional[Any]    	=       ['pixel_values']
              self.assertListEqual(arg_names[:1]    ,							lowercase__ )






    def       _UpperCamelCase    (       self )							-> str:
         def check_hidden_states_output(lowercase__    ,							lowercase__    ,							lowercase__ ):
              SCREAMING_SNAKE_CASE					:			Optional[Any]    	=       model_class(lowercase__ )

              SCREAMING_SNAKE_CASE					:			Any    	=       model(**self._prepare_for_class(lowercase__    ,							lowercase__ ) )
              SCREAMING_SNAKE_CASE					:			str    	=       outputs.hidden_states

              SCREAMING_SNAKE_CASE					:			int    	=       len(self.model_tester.depth )
              self.assertEqual(len(lowercase__ )    ,							lowercase__ )

              # 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,
                  ]    ,							)

         SCREAMING_SNAKE_CASE					:			Tuple    	=       self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
              SCREAMING_SNAKE_CASE					:			Optional[Any]    	=       True
              check_hidden_states_output(lowercase__    ,							lowercase__    ,							lowercase__ )

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

              check_hidden_states_output(lowercase__    ,							lowercase__    ,							lowercase__ )






    def       _UpperCamelCase    (       self )							-> Tuple:
         SCREAMING_SNAKE_CASE					:			Optional[Any]    	=       self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*lowercase__ )






    def       _UpperCamelCase    (       self )							-> Dict:
         SCREAMING_SNAKE_CASE					:			List[Any]    	=       self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_image_classification(*lowercase__ )






    @slow
    def       _UpperCamelCase    (       self )							-> Any:
         for model_name in TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              SCREAMING_SNAKE_CASE					:			Any    	=       TFCvtModel.from_pretrained(lowercase__ )
              self.assertIsNotNone(lowercase__ )






def     __lowerCAmelCase						(       )      ->	Tuple:





     '''simple docstring'''





     SCREAMING_SNAKE_CASE					:			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       _UpperCamelCase    (       self )							-> List[Any]:
         return AutoImageProcessor.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0] )






    @slow
    def       _UpperCamelCase    (       self )							-> List[str]:
         SCREAMING_SNAKE_CASE					:			List[str]    	=       TFCvtForImageClassification.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0] )

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

         # forward pass
         SCREAMING_SNAKE_CASE					:			Union[str, Any]    	=       model(**lowercase__ )

         # verify the logits
         SCREAMING_SNAKE_CASE					:			Union[str, Any]    	=       tf.TensorShape((1, 1_000) )
         self.assertEqual(outputs.logits.shape    ,							lowercase__ )

         SCREAMING_SNAKE_CASE					:			Dict    	=       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()    ,							lowercase__    ,							atol=1E-4 ) )




'''simple docstring'''





from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_sentencepiece_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


_lowerCAmelCase  :int  				=    {
    """configuration_albert""": ["""ALBERT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """AlbertConfig""", """AlbertOnnxConfig"""],
}

try:
 if not is_sentencepiece_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 _lowerCAmelCase  :str  				=    ["""AlbertTokenizer"""]

try:
 if not is_tokenizers_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 _lowerCAmelCase  :Optional[int]  				=    ["""AlbertTokenizerFast"""]

try:
 if not is_torch_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 _lowerCAmelCase  :List[str]  				=    [
     """ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST""",
     """AlbertForMaskedLM""",
     """AlbertForMultipleChoice""",
     """AlbertForPreTraining""",
     """AlbertForQuestionAnswering""",
     """AlbertForSequenceClassification""",
     """AlbertForTokenClassification""",
     """AlbertModel""",
     """AlbertPreTrainedModel""",
     """load_tf_weights_in_albert""",
 ]

try:
 if not is_tf_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 _lowerCAmelCase  :Any  				=    [
     """TF_ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST""",
     """TFAlbertForMaskedLM""",
     """TFAlbertForMultipleChoice""",
     """TFAlbertForPreTraining""",
     """TFAlbertForQuestionAnswering""",
     """TFAlbertForSequenceClassification""",
     """TFAlbertForTokenClassification""",
     """TFAlbertMainLayer""",
     """TFAlbertModel""",
     """TFAlbertPreTrainedModel""",
 ]

try:
 if not is_flax_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 _lowerCAmelCase  :Optional[int]  				=    [
     """FlaxAlbertForMaskedLM""",
     """FlaxAlbertForMultipleChoice""",
     """FlaxAlbertForPreTraining""",
     """FlaxAlbertForQuestionAnswering""",
     """FlaxAlbertForSequenceClassification""",
     """FlaxAlbertForTokenClassification""",
     """FlaxAlbertModel""",
     """FlaxAlbertPreTrainedModel""",
 ]

if TYPE_CHECKING:
 from .configuration_albert import ALBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, AlbertConfig, AlbertOnnxConfig

 try:
  if not is_sentencepiece_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .tokenization_albert import AlbertTokenizer

 try:
  if not is_tokenizers_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .tokenization_albert_fast import AlbertTokenizerFast

 try:
  if not is_torch_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_albert import (
      ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
      AlbertForMaskedLM,
      AlbertForMultipleChoice,
      AlbertForPreTraining,
      AlbertForQuestionAnswering,
      AlbertForSequenceClassification,
      AlbertForTokenClassification,
      AlbertModel,
      AlbertPreTrainedModel,
      load_tf_weights_in_albert,
  )

 try:
  if not is_tf_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_tf_albert import (
      TF_ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
      TFAlbertForMaskedLM,
      TFAlbertForMultipleChoice,
      TFAlbertForPreTraining,
      TFAlbertForQuestionAnswering,
      TFAlbertForSequenceClassification,
      TFAlbertForTokenClassification,
      TFAlbertMainLayer,
      TFAlbertModel,
      TFAlbertPreTrainedModel,
  )

 try:
  if not is_flax_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_flax_albert import (
      FlaxAlbertForMaskedLM,
      FlaxAlbertForMultipleChoice,
      FlaxAlbertForPreTraining,
      FlaxAlbertForQuestionAnswering,
      FlaxAlbertForSequenceClassification,
      FlaxAlbertForTokenClassification,
      FlaxAlbertModel,
      FlaxAlbertPreTrainedModel,
  )
else:
 import sys

 _lowerCAmelCase  :Union[str, Any]  				=    _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)



