






'''simple docstring'''





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




					def __init__(   self   :						List[str]							,      _A   :						int							,      _A   :						Optional[int]=13							,      _A   :						Optional[int]=32							,      _A   :						List[str]=3							,      _A   :						Tuple=4							,      _A   :						int=[10, 20, 30, 40]							,      _A   :						Any=[2, 2, 3, 2]							,      _A   :						Optional[Any]=True							,      _A   :						int=True							,      _A   :						List[Any]=37							,      _A   :						int="gelu"							,      _A   :						List[str]=10							,      _A   :						Dict=0.02							,      _A   :						Optional[int]=["stage2", "stage3", "stage4"]							,      _A   :						Any=3							,      _A   :						List[Any]=None							,      )  ->				Union[str, Any]:
								UpperCAmelCase_	:    List[str]	  =    parent
								UpperCAmelCase_	:    Any	  =    batch_size
								UpperCAmelCase_	:    Optional[int]	  =    image_size
								UpperCAmelCase_	:    str	  =    num_channels
								UpperCAmelCase_	:    int	  =    num_stages
								UpperCAmelCase_	:    Any	  =    hidden_sizes
								UpperCAmelCase_	:    Optional[int]	  =    depths
								UpperCAmelCase_	:    int	  =    is_training
								UpperCAmelCase_	:    Union[str, Any]	  =    use_labels
								UpperCAmelCase_	:    Tuple	  =    intermediate_size
								UpperCAmelCase_	:    Tuple	  =    hidden_act
								UpperCAmelCase_	:    Optional[Any]	  =    type_sequence_label_size
								UpperCAmelCase_	:    Any	  =    initializer_range
								UpperCAmelCase_	:    str	  =    out_features
								UpperCAmelCase_	:    Optional[Any]	  =    num_labels
								UpperCAmelCase_	:    Optional[int]	  =    scope
								UpperCAmelCase_	:    Tuple	  =    num_stages




					def 						A	(   self   :						List[str]	)  ->				Dict:
								UpperCAmelCase_	:    Optional[int]	  =    floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]	)

								UpperCAmelCase_	:    int	  =    None
								if self.use_labels:
											UpperCAmelCase_	:    Dict	  =    ids_tensor([self.batch_size]							,      self.type_sequence_label_size	)

								UpperCAmelCase_	:    int	  =    self.get_config()

								return config, pixel_values, labels




					def 						A	(   self   :						str	)  ->				List[str]:
								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   :						Optional[Any]	)  ->				List[Any]:
								return UperNetConfig(
								    backbone_config=self.get_backbone_config()							,      hidden_size=5_12							,      pool_scales=[1, 2, 3, 6]							,      use_auxiliary_head=_A							,      auxiliary_loss_weight=0.4							,      auxiliary_in_channels=40							,      auxiliary_channels=2_56							,      auxiliary_num_convs=1							,      auxiliary_concat_input=_A							,      loss_ignore_index=2_55							,      num_labels=self.num_labels							,      )




					def 						A	(   self   :						List[str]							,      _A   :						Dict							,      _A   :						int							,      _A   :						List[Any]	)  ->				int:
								UpperCAmelCase_	:    Tuple	  =    UperNetForSemanticSegmentation(config=_A	)
								model.to(_A	)
								model.eval()
								UpperCAmelCase_	:    List[Any]	  =    model(_A	)
								self.parent.assertEqual(
								    result.logits.shape							,      (self.batch_size, self.num_labels, self.image_size, self.image_size)	)





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






@require_torch
class       snake_case__	( UpperCamelCase			,							UpperCamelCase			,							unittest.TestCase):
					a_								= (UperNetForSemanticSegmentation,) if is_torch_available() else ()
					a_								= {"image-segmentation": UperNetForSemanticSegmentation} if is_torch_available() else {}
					a_								= False
					a_								= False
					a_								= False
					a_								= False
					a_								= False
					a_								= False




					def 						A	(   self   :						str	)  ->				List[Any]:
								UpperCAmelCase_	:    int	  =    UperNetModelTester(self	)
								UpperCAmelCase_	:    int	  =    ConfigTester(self							,      config_class=_A							,      has_text_modality=_A							,      hidden_size=37	)




					def 						A	(   self   :						Optional[Any]	)  ->				Dict:
								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   :						Optional[int]	)  ->				Optional[Any]:
								return




					def 						A	(   self   :						Dict	)  ->				Tuple:
								UpperCAmelCase_  ,	UpperCAmelCase_	:    Optional[int]	  =    self.model_tester.prepare_config_and_inputs_for_common()

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

											UpperCAmelCase_	:    str	  =    ['''pixel_values''']
											self.assertListEqual(arg_names[:1]							,      _A	)




					def 						A	(   self   :						Optional[int]	)  ->				int:
								UpperCAmelCase_	:    Dict	  =    self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_semantic_segmentation(*_A	)




					@unittest.skip(reason='''UperNet does not use inputs_embeds'''	)
					def 						A	(   self   :						List[str]	)  ->				Union[str, Any]:
								pass




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




					@unittest.skip(reason='''UperNet does not have a base model'''	)
					def 						A	(   self   :						Optional[Any]	)  ->				Optional[int]:
								pass




					@unittest.skip(reason='''UperNet does not have a base model'''	)
					def 						A	(   self   :						int	)  ->				Union[str, Any]:
								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   :						List[str]	)  ->				Union[str, Any]:
								pass




					@unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.'''	)
					def 						A	(   self   :						int	)  ->				List[str]:
								pass




					def 						A	(   self   :						int	)  ->				List[str]:






								def check_hidden_states_output(_A   :						Any							,      _A   :						List[str]							,      _A   :						Optional[Any]	):
											UpperCAmelCase_	:    Dict	  =    model_class(_A	)
											model.to(_A	)
											model.eval()

											with torch.no_grad():
														UpperCAmelCase_	:    int	  =    model(**self._prepare_for_class(_A							,      _A	)	)

											UpperCAmelCase_	:    List[Any]	  =    outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

											UpperCAmelCase_	:    str	  =    self.model_tester.num_stages
											self.assertEqual(len(_A	)							,      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_	:    List[Any]	  =    self.model_tester.prepare_config_and_inputs_for_common()

								for model_class in self.all_model_classes:
											UpperCAmelCase_	:    int	  =    True
											check_hidden_states_output(_A							,      _A							,      _A	)

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

											check_hidden_states_output(_A							,      _A							,      _A	)




					def 						A	(   self   :						Union[str, Any]	)  ->				int:
								UpperCAmelCase_  ,	UpperCAmelCase_	:    List[str]	  =    self.model_tester.prepare_config_and_inputs_for_common()

								UpperCAmelCase_	:    Optional[Any]	  =    _config_zero_init(_A	)
								UpperCAmelCase_	:    Union[str, Any]	  =    _config_zero_init(configs_no_init.backbone_config	)
								for model_class in self.all_model_classes:
											UpperCAmelCase_	:    int	  =    model_class(config=_A	)
											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   :						Dict	)  ->				List[str]:
								pass





					@slow
					def 						A	(   self   :						Union[str, Any]	)  ->				str:






								for model_name in UPERNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											UpperCAmelCase_	:    Any	  =    UperNetForSemanticSegmentation.from_pretrained(_A	)
											self.assertIsNotNone(_A	)






def __UpperCAmelCase					( )  ->					Dict:
			UpperCAmelCase_	:    List[Any]	  =    hf_hub_download(
			    repo_id='''hf-internal-testing/fixtures_ade20k'''       ,    repo_type='''dataset'''       ,    filename='''ADE_val_00000001.jpg'''						)
			UpperCAmelCase_	:    List[Any]	  =    Image.open(A__						).convert('''RGB'''						)
			return image







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




					def 						A	(   self   :						Optional[Any]	)  ->				Union[str, Any]:
								UpperCAmelCase_	:    int	  =    AutoImageProcessor.from_pretrained('''openmmlab/upernet-swin-tiny'''	)
								UpperCAmelCase_	:    Any	  =    UperNetForSemanticSegmentation.from_pretrained('''openmmlab/upernet-swin-tiny'''	).to(_A	)

								UpperCAmelCase_	:    str	  =    prepare_img()
								UpperCAmelCase_	:    int	  =    processor(images=_A							,      return_tensors='''pt'''	).to(_A	)

								with torch.no_grad():
											UpperCAmelCase_	:    int	  =    model(**_A	)

								UpperCAmelCase_	:    Tuple	  =    torch.Size((1, model.config.num_labels, 5_12, 5_12)	)
								self.assertEqual(outputs.logits.shape							,      _A	)

								UpperCAmelCase_	:    Optional[Any]	  =    torch.tensor(
								    [[-7.5_958, -7.5_958, -7.4_302], [-7.5_958, -7.5_958, -7.4_302], [-7.4_797, -7.4_797, -7.3_068]]	).to(_A	)
								self.assertTrue(torch.allclose(outputs.logits[0, 0, :3, :3]							,      _A							,      atol=1e-4	)	)





					def 						A	(   self   :						Dict	)  ->				str:
								UpperCAmelCase_	:    List[Any]	  =    AutoImageProcessor.from_pretrained('''openmmlab/upernet-convnext-tiny'''	)
								UpperCAmelCase_	:    str	  =    UperNetForSemanticSegmentation.from_pretrained('''openmmlab/upernet-convnext-tiny'''	).to(_A	)

								UpperCAmelCase_	:    Optional[int]	  =    prepare_img()
								UpperCAmelCase_	:    Tuple	  =    processor(images=_A							,      return_tensors='''pt'''	).to(_A	)

								with torch.no_grad():
											UpperCAmelCase_	:    List[Any]	  =    model(**_A	)

								UpperCAmelCase_	:    List[str]	  =    torch.Size((1, model.config.num_labels, 5_12, 5_12)	)
								self.assertEqual(outputs.logits.shape							,      _A	)

								UpperCAmelCase_	:    Any	  =    torch.tensor(
								    [[-8.8_110, -8.8_110, -8.6_521], [-8.8_110, -8.8_110, -8.6_521], [-8.7_746, -8.7_746, -8.6_130]]	).to(_A	)
								self.assertTrue(torch.allclose(outputs.logits[0, 0, :3, :3]							,      _A							,      atol=1e-4	)	)





from typing import TYPE_CHECKING

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


lowercase_									=   {'configuration_xlnet': ['XLNET_PRETRAINED_CONFIG_ARCHIVE_MAP', 'XLNetConfig']}

try:
  if not is_sentencepiece_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  lowercase_									=   ['XLNetTokenizer']

try:
  if not is_tokenizers_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  lowercase_									=   ['XLNetTokenizerFast']

try:
  if not is_torch_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  lowercase_									=   [
      'XLNET_PRETRAINED_MODEL_ARCHIVE_LIST',
      'XLNetForMultipleChoice',
      'XLNetForQuestionAnswering',
      'XLNetForQuestionAnsweringSimple',
      'XLNetForSequenceClassification',
      'XLNetForTokenClassification',
      'XLNetLMHeadModel',
      'XLNetModel',
      'XLNetPreTrainedModel',
      'load_tf_weights_in_xlnet',
  ]

try:
  if not is_tf_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  lowercase_									=   [
      'TF_XLNET_PRETRAINED_MODEL_ARCHIVE_LIST',
      'TFXLNetForMultipleChoice',
      'TFXLNetForQuestionAnsweringSimple',
      'TFXLNetForSequenceClassification',
      'TFXLNetForTokenClassification',
      'TFXLNetLMHeadModel',
      'TFXLNetMainLayer',
      'TFXLNetModel',
      'TFXLNetPreTrainedModel',
  ]


if TYPE_CHECKING:
  from .configuration_xlnet import XLNET_PRETRAINED_CONFIG_ARCHIVE_MAP, XLNetConfig

  try:
    if not is_sentencepiece_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .tokenization_xlnet import XLNetTokenizer

  try:
    if not is_tokenizers_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .tokenization_xlnet_fast import XLNetTokenizerFast

  try:
    if not is_torch_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_xlnet import (
        XLNET_PRETRAINED_MODEL_ARCHIVE_LIST,
        XLNetForMultipleChoice,
        XLNetForQuestionAnswering,
        XLNetForQuestionAnsweringSimple,
        XLNetForSequenceClassification,
        XLNetForTokenClassification,
        XLNetLMHeadModel,
        XLNetModel,
        XLNetPreTrainedModel,
        load_tf_weights_in_xlnet,
    )

  try:
    if not is_tf_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_tf_xlnet import (
        TF_XLNET_PRETRAINED_MODEL_ARCHIVE_LIST,
        TFXLNetForMultipleChoice,
        TFXLNetForQuestionAnsweringSimple,
        TFXLNetForSequenceClassification,
        TFXLNetForTokenClassification,
        TFXLNetLMHeadModel,
        TFXLNetMainLayer,
        TFXLNetModel,
        TFXLNetPreTrainedModel,
    )

else:
  import sys

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


