





"""simple docstring"""


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


     """simple docstring"""

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

     def 		a__						(	self:						Any	)->    List[str]:
           lowerCamelCase					:					int        =	floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]	)

           lowerCamelCase					:					str        =	None
           if self.use_labels:
                 lowerCamelCase					:					Any        =	ids_tensor([self.batch_size]     ,		self.num_labels	)

           lowerCamelCase					:					Optional[Any]        =	self.get_config()
           return config, pixel_values, labels

     def 		a__						(	self:						str	)->    int:
           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 		a__						(	self:						Optional[int]     ,		__a:						int     ,		__a:						Tuple     ,		__a:						Union[str, Any]	)->    Any:
           lowerCamelCase					:					Any        =	TFRegNetModel(config=__UpperCamelCase	)
           lowerCamelCase					:					int        =	model(__UpperCamelCase     ,		training=__UpperCamelCase	)
           # 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 		a__						(	self:						Union[str, Any]     ,		__a:						int     ,		__a:						List[Any]     ,		__a:						List[str]	)->    Any:
           lowerCamelCase					:					List[Any]        =	self.num_labels
           lowerCamelCase					:					Any        =	TFRegNetForImageClassification(__UpperCamelCase	)
           lowerCamelCase					:					Optional[int]        =	model(__UpperCamelCase     ,		labels=__UpperCamelCase     ,		training=__UpperCamelCase	)
           self.parent.assertEqual(result.logits.shape     ,		(self.batch_size, self.num_labels)	)







     def 		a__						(	self:						int	)->    str:
           lowerCamelCase					:					List[Any]        =	self.prepare_config_and_inputs()
           lowerCamelCase     ,			lowerCamelCase     ,			lowerCamelCase					:					List[Any]        =	config_and_inputs
           lowerCamelCase					:					Tuple        =	{"""pixel_values""": pixel_values}
           return config, inputs_dict


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


     """simple docstring"""
     snake_case__	:					str													=(TFRegNetModel, TFRegNetForImageClassification) if is_tf_available() else ()
     snake_case__	:					List[str]													=(
         {'''feature-extraction''': TFRegNetModel, '''image-classification''': TFRegNetForImageClassification}
         if is_tf_available()
         else {}
     )

     snake_case__	:					List[str]													=False
     snake_case__	:					Tuple													=False
     snake_case__	:					Tuple													=False
     snake_case__	:					Dict													=False
     snake_case__	:					str													=False

     def 		a__						(	self:						Tuple	)->    List[Any]:
           lowerCamelCase					:					Any        =	TFRegNetModelTester(self	)
           lowerCamelCase					:					Optional[int]        =	ConfigTester(self     ,		config_class=__UpperCamelCase     ,		has_text_modality=__UpperCamelCase	)

     def 		a__						(	self:						Union[str, Any]	)->    int:
           return

     @unittest.skip(reason="""RegNet does not use inputs_embeds"""	)
     def 		a__						(	self:						Optional[int]	)->    Optional[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."""     ,		)
     @slow
     def 		a__						(	self:						Tuple	)->    Union[str, Any]:
           super().test_keras_fit()

     @unittest.skip(reason="""RegNet does not support input and output embeddings"""	)
     def 		a__						(	self:						Optional[Any]	)->    str:
           pass

     def 		a__						(	self:						Dict	)->    Optional[int]:
           lowerCamelCase     ,			lowerCamelCase					:					Any        =	self.model_tester.prepare_config_and_inputs_for_common()

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

                 lowerCamelCase					:					List[str]        =	["""pixel_values"""]
                 self.assertListEqual(arg_names[:1]     ,		__UpperCamelCase	)

     def 		a__						(	self:						Any	)->    int:
           lowerCamelCase					:					Tuple        =	self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*__UpperCamelCase	)

     def 		a__						(	self:						int	)->    List[Any]:
           def check_hidden_states_output(__a:						Union[str, Any]     ,		__a:						str     ,		__a:						Any	):
                 lowerCamelCase					:					Any        =	model_class(__UpperCamelCase	)
                 lowerCamelCase					:					List[str]        =	model(**self._prepare_for_class(__UpperCamelCase     ,		__UpperCamelCase	)     ,		training=__UpperCamelCase	)

                 lowerCamelCase					:					str        =	outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

                 lowerCamelCase					:					List[str]        =	self.model_tester.num_stages
                 self.assertEqual(len(__UpperCamelCase	)     ,		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]     ,		)

           lowerCamelCase     ,			lowerCamelCase					:					Union[str, Any]        =	self.model_tester.prepare_config_and_inputs_for_common()
           lowerCamelCase					:					Optional[int]        =	["""basic""", """bottleneck"""]
           for model_class in self.all_model_classes:
                 for layer_type in layers_type:
                       lowerCamelCase					:					Union[str, Any]        =	layer_type
                       lowerCamelCase					:					Optional[int]        =	True
                       check_hidden_states_output(__UpperCamelCase     ,		__UpperCamelCase     ,		__UpperCamelCase	)

                       # check that output_hidden_states also work using config
                       del inputs_dict["output_hidden_states"]
                       lowerCamelCase					:					Tuple        =	True

                       check_hidden_states_output(__UpperCamelCase     ,		__UpperCamelCase     ,		__UpperCamelCase	)

     def 		a__						(	self:						Optional[Any]	)->    Tuple:
           lowerCamelCase     ,			lowerCamelCase					:					str        =	self.model_tester.prepare_config_and_inputs_for_common()

           def check_equivalence(__a:						List[str]     ,		__a:						Optional[Any]     ,		__a:						Tuple     ,		__a:						List[Any]={}	):
                 lowerCamelCase					:					Optional[int]        =	model(__UpperCamelCase     ,		return_dict=__UpperCamelCase     ,		**__UpperCamelCase	)
                 lowerCamelCase					:					Union[str, Any]        =	model(__UpperCamelCase     ,		return_dict=__UpperCamelCase     ,		**__UpperCamelCase	).to_tuple()

                 def recursive_check(__a:						Union[str, Any]     ,		__a:						Dict	):
                       if isinstance(__UpperCamelCase     ,		(List, Tuple)	):
                             for tuple_iterable_value, dict_iterable_value in zip(__UpperCamelCase     ,		__UpperCamelCase	):
                                   recursive_check(__UpperCamelCase     ,		__UpperCamelCase	)
                       elif tuple_object is None:
                             return
                       else:
                             self.assertTrue(
                                 all(tf.equal(__UpperCamelCase     ,		__UpperCamelCase	)	)     ,		msg=(
                                     """Tuple and dict output are not equal. Difference:"""
                                     f' {tf.math.reduce_max(tf.abs(tuple_object - dict_object	)	)}'
                                 )     ,		)

                       recursive_check(__UpperCamelCase     ,		__UpperCamelCase	)

           for model_class in self.all_model_classes:
                 lowerCamelCase					:					List[str]        =	model_class(__UpperCamelCase	)

                 lowerCamelCase					:					Any        =	self._prepare_for_class(__UpperCamelCase     ,		__UpperCamelCase	)
                 lowerCamelCase					:					List[Any]        =	self._prepare_for_class(__UpperCamelCase     ,		__UpperCamelCase	)
                 check_equivalence(__UpperCamelCase     ,		__UpperCamelCase     ,		__UpperCamelCase	)

                 lowerCamelCase					:					Optional[Any]        =	self._prepare_for_class(__UpperCamelCase     ,		__UpperCamelCase     ,		return_labels=__UpperCamelCase	)
                 lowerCamelCase					:					List[Any]        =	self._prepare_for_class(__UpperCamelCase     ,		__UpperCamelCase     ,		return_labels=__UpperCamelCase	)
                 check_equivalence(__UpperCamelCase     ,		__UpperCamelCase     ,		__UpperCamelCase	)

                 lowerCamelCase					:					Optional[int]        =	self._prepare_for_class(__UpperCamelCase     ,		__UpperCamelCase	)
                 lowerCamelCase					:					Optional[Any]        =	self._prepare_for_class(__UpperCamelCase     ,		__UpperCamelCase	)
                 check_equivalence(__UpperCamelCase     ,		__UpperCamelCase     ,		__UpperCamelCase     ,		{"""output_hidden_states""": True}	)

                 lowerCamelCase					:					int        =	self._prepare_for_class(__UpperCamelCase     ,		__UpperCamelCase     ,		return_labels=__UpperCamelCase	)
                 lowerCamelCase					:					Tuple        =	self._prepare_for_class(__UpperCamelCase     ,		__UpperCamelCase     ,		return_labels=__UpperCamelCase	)
                 check_equivalence(__UpperCamelCase     ,		__UpperCamelCase     ,		__UpperCamelCase     ,		{"""output_hidden_states""": True}	)

     def 		a__						(	self:						Union[str, Any]	)->    Any:
           lowerCamelCase					:					str        =	self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_image_classification(*__UpperCamelCase	)







     @slow
     def 		a__						(	self:						List[Any]	)->    Tuple:
           for model_name in TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                 lowerCamelCase					:					Dict        =	TFRegNetModel.from_pretrained(__UpperCamelCase	)
                 self.assertIsNotNone(__UpperCamelCase	)




def   snake_case		(     )   ->      Optional[int]:
      lowerCamelCase					:					List[Any]        =	Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""    )
      return image






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


     """simple docstring"""

     @cached_property
     def 		a__						(	self:						Union[str, Any]	)->    Optional[Any]:
           return (
               AutoImageProcessor.from_pretrained(TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]	)
               if is_vision_available()
               else None
           )







     @slow
     def 		a__						(	self:						List[str]	)->    int:
           lowerCamelCase					:					Any        =	TFRegNetForImageClassification.from_pretrained(TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]	)

           lowerCamelCase					:					str        =	self.default_image_processor
           lowerCamelCase					:					str        =	prepare_img()
           lowerCamelCase					:					Any        =	image_processor(images=__UpperCamelCase     ,		return_tensors="""tf"""	)

           # forward pass
           lowerCamelCase					:					Tuple        =	model(**__UpperCamelCase     ,		training=__UpperCamelCase	)

           # verify the logits
           lowerCamelCase					:					Optional[int]        =	tf.TensorShape((1, 1_000)	)
           self.assertEqual(outputs.logits.shape     ,		__UpperCamelCase	)

           lowerCamelCase					:					Union[str, Any]        =	tf.constant([-0.41_80, -1.50_51, -3.48_36]	)

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








"""simple docstring"""


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


__lowerCamelCase      :Optional[int]	    =		logging.get_logger(__name__)

__lowerCamelCase      :List[str]	    =		{
    '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__	(						__lowercase):


     """simple docstring"""
     snake_case__	:					Optional[Any]													='''realm'''

     def __init__(	self:						Union[str, Any]     ,		__a:						List[Any]=30_522     ,		__a:						List[Any]=768     ,		__a:						List[Any]=128     ,		__a:						Union[str, Any]=12     ,		__a:						Union[str, Any]=12     ,		__a:						Optional[Any]=8     ,		__a:						Dict=3_072     ,		__a:						List[Any]="gelu_new"     ,		__a:						List[Any]=0.1     ,		__a:						Tuple=0.1     ,		__a:						Optional[Any]=512     ,		__a:						Optional[int]=2     ,		__a:						str=0.02     ,		__a:						int=1e-1_2     ,		__a:						Optional[Any]=256     ,		__a:						Any=10     ,		__a:						Dict=1e-3     ,		__a:						Optional[Any]=5     ,		__a:						Dict=320     ,		__a:						Tuple=13_353_718     ,		__a:						List[Any]=5_000     ,		__a:						Dict=1     ,		__a:						int=0     ,		__a:						Dict=2     ,		**__a:						List[str]     ,		)->    Any:
           super().__init__(pad_token_id=__a     ,		bos_token_id=__a     ,		eos_token_id=__a     ,		**__a	)

           # Common config
           lowerCamelCase					:					Optional[Any]        =	vocab_size
           lowerCamelCase					:					str        =	max_position_embeddings
           lowerCamelCase					:					Dict        =	hidden_size
           lowerCamelCase					:					Dict        =	retriever_proj_size
           lowerCamelCase					:					Optional[Any]        =	num_hidden_layers
           lowerCamelCase					:					List[str]        =	num_attention_heads
           lowerCamelCase					:					Tuple        =	num_candidates
           lowerCamelCase					:					int        =	intermediate_size
           lowerCamelCase					:					Dict        =	hidden_act
           lowerCamelCase					:					List[str]        =	hidden_dropout_prob
           lowerCamelCase					:					Dict        =	attention_probs_dropout_prob
           lowerCamelCase					:					Optional[int]        =	initializer_range
           lowerCamelCase					:					Dict        =	type_vocab_size
           lowerCamelCase					:					Optional[Any]        =	layer_norm_eps

           # Reader config
           lowerCamelCase					:					List[str]        =	span_hidden_size
           lowerCamelCase					:					Dict        =	max_span_width
           lowerCamelCase					:					Optional[Any]        =	reader_layer_norm_eps
           lowerCamelCase					:					Optional[int]        =	reader_beam_size
           lowerCamelCase					:					List[Any]        =	reader_seq_len

           # Retrieval config
           lowerCamelCase					:					int        =	num_block_records
           lowerCamelCase					:					Dict        =	searcher_beam_size




