


import inspect
import unittest

from huggingface_hub import hf_hub_download

from transformers import ConvNextConfig, UperNetConfig
from transformers.testing_utils import require_torch, require_torch_multi_gpu, require_vision, slow, torch_device
from transformers.utils import is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
				import torch

				from transformers import UperNetForSemanticSegmentation
				from transformers.models.upernet.modeling_upernet import UPERNET_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
				from PIL import Image

				from transformers import AutoImageProcessor




class        lowerCamelCase      :


	def __init__(						self							,    lowercase__							,    lowercase__=1_3							,    lowercase__=3_2							,    lowercase__=3							,    lowercase__=4							,    lowercase__=[1_0, 2_0, 3_0, 4_0]							,    lowercase__=[2, 2, 3, 2]							,    lowercase__=True							,    lowercase__=True							,    lowercase__=3_7							,    lowercase__="gelu"							,    lowercase__=1_0							,    lowercase__=0.0_2							,    lowercase__=["stage2", "stage3", "stage4"]							,    lowercase__=3							,    lowercase__=None							,    ):
							__UpperCAmelCase :       List[Any]  						=  parent
							__UpperCAmelCase :       Dict  						=  batch_size
							__UpperCAmelCase :       Dict  						=  image_size
							__UpperCAmelCase :       str  						=  num_channels
							__UpperCAmelCase :       Optional[Any]  						=  num_stages
							__UpperCAmelCase :       Union[str, Any]  						=  hidden_sizes
							__UpperCAmelCase :       Optional[int]  						=  depths
							__UpperCAmelCase :       Union[str, Any]  						=  is_training
							__UpperCAmelCase :       List[str]  						=  use_labels
							__UpperCAmelCase :       Optional[int]  						=  intermediate_size
							__UpperCAmelCase :       Union[str, Any]  						=  hidden_act
							__UpperCAmelCase :       Union[str, Any]  						=  type_sequence_label_size
							__UpperCAmelCase :       Optional[Any]  						=  initializer_range
							__UpperCAmelCase :       Union[str, Any]  						=  out_features
							__UpperCAmelCase :       Dict  						=  num_labels
							__UpperCAmelCase :       Union[str, Any]  						=  scope
							__UpperCAmelCase :       Optional[Any]  						=  num_stages


	def    A(						self):
							__UpperCAmelCase :       List[str]  						=  floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

							__UpperCAmelCase :       Union[str, Any]  						=  None
							if self.use_labels:
													__UpperCAmelCase :       Dict  						=  ids_tensor([self.batch_size]							,    self.type_sequence_label_size)

							__UpperCAmelCase :       Tuple  						=  self.get_config()

							return config, pixel_values, labels


	def    A(						self):
							return ConvNextConfig(
							    num_channels=self.num_channels							,    num_stages=self.num_stages							,    hidden_sizes=self.hidden_sizes							,    depths=self.depths							,    is_training=self.is_training							,    intermediate_size=self.intermediate_size							,    hidden_act=self.hidden_act							,    out_features=self.out_features							,    )


	def    A(						self):
							return UperNetConfig(
							    backbone_config=self.get_backbone_config()							,    hidden_size=5_1_2							,    pool_scales=[1, 2, 3, 6]							,    use_auxiliary_head=lowercase__							,    auxiliary_loss_weight=0.4							,    auxiliary_in_channels=4_0							,    auxiliary_channels=2_5_6							,    auxiliary_num_convs=1							,    auxiliary_concat_input=lowercase__							,    loss_ignore_index=2_5_5							,    num_labels=self.num_labels							,    )


	def    A(						self							,    lowercase__							,    lowercase__							,    lowercase__):
							__UpperCAmelCase :       List[Any]  						=  UperNetForSemanticSegmentation(config=lowercase__)
							model.to(lowercase__)
							model.eval()
							__UpperCAmelCase :       str  						=  model(lowercase__)
							self.parent.assertEqual(
							    result.logits.shape							,    (self.batch_size, self.num_labels, self.image_size, self.image_size))


	def    A(						self):
							__UpperCAmelCase :       Tuple  						=  self.prepare_config_and_inputs()
							(
							    (
							    __UpperCAmelCase
							)						,			(
							    __UpperCAmelCase
							)						,			(
							    __UpperCAmelCase
							)						,			
							) :       Optional[Any]  						=  config_and_inputs
							__UpperCAmelCase :       List[Any]  						=  {'''pixel_values''': pixel_values}
							return config, inputs_dict






@require_torch
class        lowerCamelCase      (     _UpperCamelCase       ,	_UpperCamelCase       ,	unittest.TestCase   ):
	_lowerCAmelCase    :		Any        =	(UperNetForSemanticSegmentation,) if is_torch_available() else ()
	_lowerCAmelCase    :		Dict        =	{'''image-segmentation''': UperNetForSemanticSegmentation} if is_torch_available() else {}
	_lowerCAmelCase    :		int        =	False
	_lowerCAmelCase    :		Tuple        =	False
	_lowerCAmelCase    :		Tuple        =	False
	_lowerCAmelCase    :		int        =	False
	_lowerCAmelCase    :		Optional[int]        =	False
	_lowerCAmelCase    :		Tuple        =	False


	def    A(						self):
							__UpperCAmelCase :       Dict  						=  UperNetModelTester(self)
							__UpperCAmelCase :       List[Any]  						=  ConfigTester(self							,    config_class=lowercase__							,    has_text_modality=lowercase__							,    hidden_size=3_7)


	def    A(						self):
							self.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()


	def    A(						self):
							return


	def    A(						self):
							__UpperCAmelCase						,			__UpperCAmelCase :       str  						=  self.model_tester.prepare_config_and_inputs_for_common()

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

													__UpperCAmelCase :       int  						=  ['''pixel_values''']
													self.assertListEqual(arg_names[:1]							,    lowercase__)


	def    A(						self):
							__UpperCAmelCase :       int  						=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_semantic_segmentation(*lowercase__)


	@unittest.skip(reason='''UperNet does not use inputs_embeds''')
	def    A(						self):
							pass


	@unittest.skip(reason='''UperNet does not support input and output embeddings''')
	def    A(						self):
							pass


	@unittest.skip(reason='''UperNet does not have a base model''')
	def    A(						self):
							pass


	@unittest.skip(reason='''UperNet does not have a base model''')
	def    A(						self):
							pass


	@require_torch_multi_gpu
	@unittest.skip(reason='''UperNet has some layers using `add_module` which doesn\'t work well with `nn.DataParallel`''')
	def    A(						self):
							pass


	@unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.''')
	def    A(						self):
							pass


	def    A(						self):
							def check_hidden_states_output(lowercase__							,    lowercase__							,    lowercase__):
													__UpperCAmelCase :       Union[str, Any]  						=  model_class(lowercase__)
													model.to(lowercase__)
													model.eval()

													with torch.no_grad():
																			__UpperCAmelCase :       List[str]  						=  model(**self._prepare_for_class(lowercase__							,    lowercase__))

													__UpperCAmelCase :       Dict  						=  outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

													__UpperCAmelCase :       Union[str, Any]  						=  self.model_tester.num_stages
													self.assertEqual(len(lowercase__)							,    expected_num_stages + 1)

													# ConvNext'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 // 4, self.model_tester.image_size // 4]							,    )

							__UpperCAmelCase						,			__UpperCAmelCase :       Tuple  						=  self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
													__UpperCAmelCase :       Dict  						=  True
													check_hidden_states_output(lowercase__							,    lowercase__							,    lowercase__)

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

													check_hidden_states_output(lowercase__							,    lowercase__							,    lowercase__)


	def    A(						self):
							__UpperCAmelCase						,			__UpperCAmelCase :       List[Any]  						=  self.model_tester.prepare_config_and_inputs_for_common()

							__UpperCAmelCase :       Optional[int]  						=  _config_zero_init(lowercase__)
							__UpperCAmelCase :       Any  						=  _config_zero_init(configs_no_init.backbone_config)
							for model_class in self.all_model_classes:
													__UpperCAmelCase :       List[str]  						=  model_class(config=lowercase__)
													for name, param in model.named_parameters():
																			if param.requires_grad:
																									self.assertIn(
																									    ((param.data.mean() * 1e9).round() / 1e9).item()							,    [0.0, 1.0]							,    msg=F"Parameter {name} of model {model_class} seems not properly initialized"							,    )


	@unittest.skip(reason='''UperNet does not have tied weights''')
	def    A(						self):
							pass


	@slow
	def    A(						self):
							for model_name in UPERNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													__UpperCAmelCase :       Tuple  						=  UperNetForSemanticSegmentation.from_pretrained(lowercase__)
													self.assertIsNotNone(lowercase__)






def 						__SCREAMING_SNAKE_CASE       (					)		->    Optional[Any]:




						'''simple docstring'''







						__UpperCAmelCase :       List[str]  						=  hf_hub_download(
						    repo_id='''hf-internal-testing/fixtures_ade20k'''			,					repo_type='''dataset'''			,					filename='''ADE_val_00000001.jpg'''			)
						__UpperCAmelCase :       Tuple  						=  Image.open(lowercase_			).convert('''RGB'''			)
						return image





@require_torch
@require_vision
@slow
class        lowerCamelCase      (     unittest.TestCase   ):


	def    A(						self):
							__UpperCAmelCase :       Any  						=  AutoImageProcessor.from_pretrained('''openmmlab/upernet-swin-tiny''')
							__UpperCAmelCase :       Optional[Any]  						=  UperNetForSemanticSegmentation.from_pretrained('''openmmlab/upernet-swin-tiny''').to(lowercase__)

							__UpperCAmelCase :       Optional[int]  						=  prepare_img()
							__UpperCAmelCase :       Optional[Any]  						=  processor(images=lowercase__							,    return_tensors='''pt''').to(lowercase__)

							with torch.no_grad():
													__UpperCAmelCase :       Tuple  						=  model(**lowercase__)

							__UpperCAmelCase :       str  						=  torch.Size((1, model.config.num_labels, 5_1_2, 5_1_2))
							self.assertEqual(outputs.logits.shape							,    lowercase__)

							__UpperCAmelCase :       List[str]  						=  torch.tensor(
							    [[-7.5_9_5_8, -7.5_9_5_8, -7.4_3_0_2], [-7.5_9_5_8, -7.5_9_5_8, -7.4_3_0_2], [-7.4_7_9_7, -7.4_7_9_7, -7.3_0_6_8]]).to(lowercase__)
							self.assertTrue(torch.allclose(outputs.logits[0, 0, :3, :3]							,    lowercase__							,    atol=1e-4))


	def    A(						self):
							__UpperCAmelCase :       List[Any]  						=  AutoImageProcessor.from_pretrained('''openmmlab/upernet-convnext-tiny''')
							__UpperCAmelCase :       Tuple  						=  UperNetForSemanticSegmentation.from_pretrained('''openmmlab/upernet-convnext-tiny''').to(lowercase__)

							__UpperCAmelCase :       Tuple  						=  prepare_img()
							__UpperCAmelCase :       List[str]  						=  processor(images=lowercase__							,    return_tensors='''pt''').to(lowercase__)

							with torch.no_grad():
													__UpperCAmelCase :       str  						=  model(**lowercase__)

							__UpperCAmelCase :       int  						=  torch.Size((1, model.config.num_labels, 5_1_2, 5_1_2))
							self.assertEqual(outputs.logits.shape							,    lowercase__)

							__UpperCAmelCase :       Dict  						=  torch.tensor(
							    [[-8.8_1_1_0, -8.8_1_1_0, -8.6_5_2_1], [-8.8_1_1_0, -8.8_1_1_0, -8.6_5_2_1], [-8.7_7_4_6, -8.7_7_4_6, -8.6_1_3_0]]).to(lowercase__)
							self.assertTrue(torch.allclose(outputs.logits[0, 0, :3, :3]							,    lowercase__							,    atol=1e-4))





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


lowerCAmelCase							=						logging.get_logger(__name__)

lowerCAmelCase							=						{
    """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        lowerCamelCase      (     _UpperCamelCase   ):
	_lowerCAmelCase    :		Tuple        =	'''realm'''


	def __init__(						self							,    lowercase__=3_0_5_2_2							,    lowercase__=7_6_8							,    lowercase__=1_2_8							,    lowercase__=1_2							,    lowercase__=1_2							,    lowercase__=8							,    lowercase__=3_0_7_2							,    lowercase__="gelu_new"							,    lowercase__=0.1							,    lowercase__=0.1							,    lowercase__=5_1_2							,    lowercase__=2							,    lowercase__=0.0_2							,    lowercase__=1e-12							,    lowercase__=2_5_6							,    lowercase__=1_0							,    lowercase__=1e-3							,    lowercase__=5							,    lowercase__=3_2_0							,    lowercase__=1_3_3_5_3_7_1_8							,    lowercase__=5_0_0_0							,    lowercase__=1							,    lowercase__=0							,    lowercase__=2							,    **lowercase__							,    ):
							super().__init__(pad_token_id=lowercase__							,    bos_token_id=lowercase__							,    eos_token_id=lowercase__							,    **lowercase__)

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

							# Reader config
							__UpperCAmelCase :       Optional[int]  						=  span_hidden_size
							__UpperCAmelCase :       Dict  						=  max_span_width
							__UpperCAmelCase :       int  						=  reader_layer_norm_eps
							__UpperCAmelCase :       int  						=  reader_beam_size
							__UpperCAmelCase :       Optional[int]  						=  reader_seq_len

							# Retrieval config
							__UpperCAmelCase :       Optional[int]  						=  num_block_records
							__UpperCAmelCase :       Optional[Any]  						=  searcher_beam_size

