"""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 							a					(		lowerCAmelCase_ ):


    def   lowerCAmelCase_	(       self						:	List[Any]       ):
      _UpperCAmelCase    			=   self.config_class(**self.inputs_dict       )
      self.parent.assertTrue(hasattr(__lowerCAmelCase ,					"""embed_dim"""       )       )
      self.parent.assertTrue(hasattr(__lowerCAmelCase ,					"""num_heads"""       )       )



class 							a					:


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


    def   lowerCAmelCase_	(       self						:	Optional[int]       ):
      _UpperCAmelCase    			=   floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]       )

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

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


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


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



@require_tf
class 							a					(		lowerCAmelCase_						,				lowerCAmelCase_						,				unittest.TestCase ):
    _snake_case  :	str  				=				(TFCvtModel, TFCvtForImageClassification) if is_tf_available() else ()
    _snake_case  :	List[Any]  				=				(
        {'feature-extraction': TFCvtModel, 'image-classification': TFCvtForImageClassification}
        if is_tf_available()
        else {}
    )
    _snake_case  :	Union[str, Any]  				=				False
    _snake_case  :	int  				=				False
    _snake_case  :	str  				=				False
    _snake_case  :	List[str]  				=				False
    _snake_case  :	Optional[int]  				=				False


    def   lowerCAmelCase_	(       self						:	Union[str, Any]       ):
      _UpperCAmelCase    			=   TFCvtModelTester(self       )
      _UpperCAmelCase    			=   TFCvtConfigTester(self ,					config_class=__lowerCAmelCase ,					has_text_modality=__lowerCAmelCase ,					hidden_size=37       )


    def   lowerCAmelCase_	(       self						:	Union[str, Any]       ):
      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						:	Any       ):
      pass


    @unittest.skip(reason="""Cvt does not use inputs_embeds"""       )
    def   lowerCAmelCase_	(       self						:	Any       ):
      pass


    @unittest.skip(reason="""Cvt does not support input and output embeddings"""       )
    def   lowerCAmelCase_	(       self						:	Union[str, 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.""" ,					)
    def   lowerCAmelCase_	(       self						:	Union[str, Any]       ):
      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						:	Optional[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						:	Optional[int]       ):
      _UpperCAmelCase    			=   tf.keras.mixed_precision.Policy("""mixed_float16"""       )
      tf.keras.mixed_precision.set_global_policy(__lowerCAmelCase       )
      super().test_keras_fit()
      tf.keras.mixed_precision.set_global_policy("""float32"""       )


    def   lowerCAmelCase_	(       self						:	Optional[int]       ):
      _UpperCAmelCase			,					_UpperCAmelCase    			=   self.model_tester.prepare_config_and_inputs_for_common()

      for model_class in self.all_model_classes:
        _UpperCAmelCase    			=   model_class(__lowerCAmelCase       )
        _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] ,					__lowerCAmelCase       )


    def   lowerCAmelCase_	(       self						:	str       ):
      def check_hidden_states_output(__lowerCAmelCase						:	Optional[Any] ,					__lowerCAmelCase						:	List[Any] ,					__lowerCAmelCase						:	int       ):
        _UpperCAmelCase    			=   model_class(__lowerCAmelCase       )

        _UpperCAmelCase    			=   model(**self._prepare_for_class(__lowerCAmelCase ,					__lowerCAmelCase       )       )
        _UpperCAmelCase    			=   outputs.hidden_states

        _UpperCAmelCase    			=   len(self.model_tester.depth       )
        self.assertEqual(len(__lowerCAmelCase       ) ,					__lowerCAmelCase       )

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

      _UpperCAmelCase			,					_UpperCAmelCase    			=   self.model_tester.prepare_config_and_inputs_for_common()

      for model_class in self.all_model_classes:
        _UpperCAmelCase    			=   True
        check_hidden_states_output(__lowerCAmelCase ,					__lowerCAmelCase ,					__lowerCAmelCase       )

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

        check_hidden_states_output(__lowerCAmelCase ,					__lowerCAmelCase ,					__lowerCAmelCase       )


    def   lowerCAmelCase_	(       self						:	Dict       ):
      _UpperCAmelCase    			=   self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*__lowerCAmelCase       )


    def   lowerCAmelCase_	(       self						:	Tuple       ):
      _UpperCAmelCase    			=   self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_image_classification(*__lowerCAmelCase       )


    @slow
    def   lowerCAmelCase_	(       self						:	Any       ):
      for model_name in TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        _UpperCAmelCase    			=   TFCvtModel.from_pretrained(__lowerCAmelCase       )
        self.assertIsNotNone(__lowerCAmelCase       )






def      __UpperCAmelCase       (      ):







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


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


    @cached_property
    def   lowerCAmelCase_	(       self						:	Dict       ):
      return AutoImageProcessor.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]       )


    @slow
    def   lowerCAmelCase_	(       self						:	Any       ):
      _UpperCAmelCase    			=   TFCvtForImageClassification.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]       )

      _UpperCAmelCase    			=   self.default_image_processor
      _UpperCAmelCase    			=   prepare_img()
      _UpperCAmelCase    			=   image_processor(images=__lowerCAmelCase ,					return_tensors="""tf"""       )

      # forward pass
      _UpperCAmelCase    			=   model(**__lowerCAmelCase       )

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

      _UpperCAmelCase    			=   tf.constant([0.9_285, 0.9_015, -0.3_150]       )
      self.assertTrue(np.allclose(outputs.logits[0, :3].numpy() ,					__lowerCAmelCase ,					atol=1e-4       )       )

"""simple docstring"""


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


UpperCAmelCase__			=    logging.get_logger(__name__)

UpperCAmelCase__			=    {
    """google/realm-cc-news-pretrained-embedder""": (
        """https://huggingface.co/google/realm-cc-news-pretrained-embedder/resolve/main/config.json"""
    ),
    """google/realm-cc-news-pretrained-encoder""": (
        """https://huggingface.co/google/realm-cc-news-pretrained-encoder/resolve/main/config.json"""
    ),
    """google/realm-cc-news-pretrained-scorer""": (
        """https://huggingface.co/google/realm-cc-news-pretrained-scorer/resolve/main/config.json"""
    ),
    """google/realm-cc-news-pretrained-openqa""": (
        """https://huggingface.co/google/realm-cc-news-pretrained-openqa/aresolve/main/config.json"""
    ),
    """google/realm-orqa-nq-openqa""": """https://huggingface.co/google/realm-orqa-nq-openqa/resolve/main/config.json""",
    """google/realm-orqa-nq-reader""": """https://huggingface.co/google/realm-orqa-nq-reader/resolve/main/config.json""",
    """google/realm-orqa-wq-openqa""": """https://huggingface.co/google/realm-orqa-wq-openqa/resolve/main/config.json""",
    """google/realm-orqa-wq-reader""": """https://huggingface.co/google/realm-orqa-wq-reader/resolve/main/config.json""",
    # See all REALM models at https://huggingface.co/models?filter=realm
}


class 							a					(		lowerCAmelCase_ ):
    _snake_case  :	List[Any]  				=				'realm'


    def __init__(       self						:	List[Any] ,					__lowerCAmelCase						:	List[str]=3_0522 ,					__lowerCAmelCase						:	Tuple=768 ,					__lowerCAmelCase						:	List[Any]=128 ,					__lowerCAmelCase						:	Tuple=12 ,					__lowerCAmelCase						:	List[str]=12 ,					__lowerCAmelCase						:	Union[str, Any]=8 ,					__lowerCAmelCase						:	List[str]=3072 ,					__lowerCAmelCase						:	Union[str, Any]="gelu_new" ,					__lowerCAmelCase						:	str=0.1 ,					__lowerCAmelCase						:	Tuple=0.1 ,					__lowerCAmelCase						:	List[str]=512 ,					__lowerCAmelCase						:	Dict=2 ,					__lowerCAmelCase						:	List[Any]=0.02 ,					__lowerCAmelCase						:	Optional[int]=1e-1_2 ,					__lowerCAmelCase						:	Any=256 ,					__lowerCAmelCase						:	Union[str, Any]=10 ,					__lowerCAmelCase						:	Dict=1e-3 ,					__lowerCAmelCase						:	str=5 ,					__lowerCAmelCase						:	str=320 ,					__lowerCAmelCase						:	Tuple=1335_3718 ,					__lowerCAmelCase						:	Optional[Any]=5000 ,					__lowerCAmelCase						:	int=1 ,					__lowerCAmelCase						:	Dict=0 ,					__lowerCAmelCase						:	Any=2 ,					**__lowerCAmelCase						:	Optional[int] ,					):
      super().__init__(pad_token_id=__lowerCAmelCase ,					bos_token_id=__lowerCAmelCase ,					eos_token_id=__lowerCAmelCase ,					**__lowerCAmelCase       )

      # Common config
      _UpperCAmelCase    			=   vocab_size
      _UpperCAmelCase    			=   max_position_embeddings
      _UpperCAmelCase    			=   hidden_size
      _UpperCAmelCase    			=   retriever_proj_size
      _UpperCAmelCase    			=   num_hidden_layers
      _UpperCAmelCase    			=   num_attention_heads
      _UpperCAmelCase    			=   num_candidates
      _UpperCAmelCase    			=   intermediate_size
      _UpperCAmelCase    			=   hidden_act
      _UpperCAmelCase    			=   hidden_dropout_prob
      _UpperCAmelCase    			=   attention_probs_dropout_prob
      _UpperCAmelCase    			=   initializer_range
      _UpperCAmelCase    			=   type_vocab_size
      _UpperCAmelCase    			=   layer_norm_eps

      # Reader config
      _UpperCAmelCase    			=   span_hidden_size
      _UpperCAmelCase    			=   max_span_width
      _UpperCAmelCase    			=   reader_layer_norm_eps
      _UpperCAmelCase    			=   reader_beam_size
      _UpperCAmelCase    			=   reader_seq_len

      # Retrieval config
      _UpperCAmelCase    			=   num_block_records
      _UpperCAmelCase    			=   searcher_beam_size
