


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


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


      def 				__lowerCamelCase	(							self						):
            lowercase	:      Any          =						floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

            lowercase	:      List[Any]          =						None
            if self.use_labels:
                  lowercase	:      Dict          =						ids_tensor([self.batch_size]							,  self.type_sequence_label_size						)

            lowercase	:      Union[str, Any]          =						self.get_config()

            return config, pixel_values, labels


      def 				__lowerCamelCase	(							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 				__lowerCamelCase	(							self						):
            return UperNetConfig(
                backbone_config=self.get_backbone_config()							,  hidden_size=512							,  pool_scales=[1, 2, 3, 6]							,  use_auxiliary_head=lowercase_							,  auxiliary_loss_weight=0.4							,  auxiliary_in_channels=40							,  auxiliary_channels=256							,  auxiliary_num_convs=1							,  auxiliary_concat_input=lowercase_							,  loss_ignore_index=255							,  num_labels=self.num_labels							,  )


      def 				__lowerCamelCase	(							self							,  SCREAMING_SNAKE_CASE__							,  SCREAMING_SNAKE_CASE__							,  SCREAMING_SNAKE_CASE__						):
            lowercase	:      Tuple          =						UperNetForSemanticSegmentation(config=lowercase_						)
            model.to(lowercase_						)
            model.eval()
            lowercase	:      Dict          =						model(lowercase_						)
            self.parent.assertEqual(
                result.logits.shape							,  (self.batch_size, self.num_labels, self.image_size, self.image_size)						)






      def 				__lowerCamelCase	(							self						):
            lowercase	:      Optional[int]          =						self.prepare_config_and_inputs()
            (
                lowercase
            )	:      Any          =						config_and_inputs
            lowercase	:      Any          =						{'''pixel_values''': pixel_values}
            return config, inputs_dict







@require_torch
class  __SCREAMING_SNAKE_CASE      (	a_     ,  a_     ,  unittest.TestCase      ):
      A :							Dict										=   (UperNetForSemanticSegmentation,) if is_torch_available() else ()
      A :							List[str]										=   {'image-segmentation': UperNetForSemanticSegmentation} if is_torch_available() else {}
      A :							Any										=   False
      A :							Dict										=   False
      A :							List[Any]										=   False
      A :							str										=   False
      A :							Tuple										=   False
      A :							List[Any]										=   False


      def 				__lowerCamelCase	(							self						):
            lowercase	:      str          =						UperNetModelTester(self						)
            lowercase	:      Any          =						ConfigTester(self							,  config_class=lowercase_							,  has_text_modality=lowercase_							,  hidden_size=37						)


      def 				__lowerCamelCase	(							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 				__lowerCamelCase	(							self						):
            return


      def 				__lowerCamelCase	(							self						):
            lowercase	:      List[Any]          =						self.model_tester.prepare_config_and_inputs_for_common()

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

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


      def 				__lowerCamelCase	(							self						):
            lowercase	:      List[Any]          =						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 				__lowerCamelCase	(							self						):
            pass


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


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


      @unittest.skip(reason='''UperNet does not have a base model'''						)
      def 				__lowerCamelCase	(							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 				__lowerCamelCase	(							self						):
            pass


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


      def 				__lowerCamelCase	(							self						):
            def check_hidden_states_output(SCREAMING_SNAKE_CASE__							,  SCREAMING_SNAKE_CASE__							,  SCREAMING_SNAKE_CASE__						):
                  lowercase	:      Optional[Any]          =						model_class(lowercase_						)
                  model.to(lowercase_						)
                  model.eval()

                  with torch.no_grad():
                        lowercase	:      int          =						model(**self._prepare_for_class(lowercase_							,  lowercase_						)						)

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

                  lowercase	:      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]							,  )

            lowercase	:      List[Any]          =						self.model_tester.prepare_config_and_inputs_for_common()

            for model_class in self.all_model_classes:
                  lowercase	:      str          =						True
                  check_hidden_states_output(lowercase_							,  lowercase_							,  lowercase_						)

                  # check that output_hidden_states also work using config
                  del inputs_dict["output_hidden_states"]
                  lowercase	:      Union[str, Any]          =						True

                  check_hidden_states_output(lowercase_							,  lowercase_							,  lowercase_						)


      def 				__lowerCamelCase	(							self						):
            lowercase	:      str          =						self.model_tester.prepare_config_and_inputs_for_common()

            lowercase	:      Dict          =						_config_zero_init(lowercase_						)
            lowercase	:      str          =						_config_zero_init(configs_no_init.backbone_config						)
            for model_class in self.all_model_classes:
                  lowercase	:      Any          =						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 				__lowerCamelCase	(							self						):
            pass






      @slow
      def 				__lowerCamelCase	(							self						):
            for model_name in UPERNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                  lowercase	:      Union[str, Any]          =						UperNetForSemanticSegmentation.from_pretrained(lowercase_						)
                  self.assertIsNotNone(lowercase_						)





def 	__lowercase		(  )			->Any:







      """simple docstring"""

      lowercase	:      int          =						hf_hub_download(
          repo_id='''hf-internal-testing/fixtures_ade20k''',			repo_type='''dataset''',			filename='''ADE_val_00000001.jpg'''   )
      lowercase	:      Union[str, Any]          =						Image.open(A_   ).convert('''RGB'''   )
      return image







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


      def 				__lowerCamelCase	(							self						):
            lowercase	:      Any          =						AutoImageProcessor.from_pretrained('''openmmlab/upernet-swin-tiny'''						)
            lowercase	:      Tuple          =						UperNetForSemanticSegmentation.from_pretrained('''openmmlab/upernet-swin-tiny'''						).to(lowercase_						)

            lowercase	:      str          =						prepare_img()
            lowercase	:      Optional[int]          =						processor(images=lowercase_							,  return_tensors='''pt'''						).to(lowercase_						)

            with torch.no_grad():
                  lowercase	:      int          =						model(**lowercase_						)

            lowercase	:      Tuple          =						torch.Size((1, model.config.num_labels, 512, 512)						)
            self.assertEqual(outputs.logits.shape							,  lowercase_						)

            lowercase	:      Tuple          =						torch.tensor(
                [[-7.5958, -7.5958, -7.4302], [-7.5958, -7.5958, -7.4302], [-7.4797, -7.4797, -7.3068]]						).to(lowercase_						)
            self.assertTrue(torch.allclose(outputs.logits[0, 0, :3, :3]							,  lowercase_							,  atol=1E-4						)						)






      def 				__lowerCamelCase	(							self						):
            lowercase	:      Any          =						AutoImageProcessor.from_pretrained('''openmmlab/upernet-convnext-tiny'''						)
            lowercase	:      List[str]          =						UperNetForSemanticSegmentation.from_pretrained('''openmmlab/upernet-convnext-tiny'''						).to(lowercase_						)

            lowercase	:      Dict          =						prepare_img()
            lowercase	:      Tuple          =						processor(images=lowercase_							,  return_tensors='''pt'''						).to(lowercase_						)

            with torch.no_grad():
                  lowercase	:      List[Any]          =						model(**lowercase_						)

            lowercase	:      Union[str, Any]          =						torch.Size((1, model.config.num_labels, 512, 512)						)
            self.assertEqual(outputs.logits.shape							,  lowercase_						)

            lowercase	:      Optional[Any]          =						torch.tensor(
                [[-8.8110, -8.8110, -8.6521], [-8.8110, -8.8110, -8.6521], [-8.7746, -8.7746, -8.6130]]						).to(lowercase_						)
            self.assertTrue(torch.allclose(outputs.logits[0, 0, :3, :3]							,  lowercase_							,  atol=1E-4						)						)




"""simple docstring"""





from typing import TYPE_CHECKING

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


__UpperCamelCase  :      Dict							      =		{
    '''configuration_clip''': [
        '''CLIP_PRETRAINED_CONFIG_ARCHIVE_MAP''',
        '''CLIPConfig''',
        '''CLIPOnnxConfig''',
        '''CLIPTextConfig''',
        '''CLIPVisionConfig''',
    ],
    '''processing_clip''': ['''CLIPProcessor'''],
    '''tokenization_clip''': ['''CLIPTokenizer'''],
}

try:
							if not is_tokenizers_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							__UpperCamelCase  :      int							      =		['''CLIPTokenizerFast''']

try:
							if not is_vision_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							__UpperCamelCase  :      Optional[int]							      =		['''CLIPFeatureExtractor''']
							__UpperCamelCase  :      Optional[Any]							      =		['''CLIPImageProcessor''']

try:
							if not is_torch_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							__UpperCamelCase  :      Tuple							      =		[
							    '''CLIP_PRETRAINED_MODEL_ARCHIVE_LIST''',
							    '''CLIPModel''',
							    '''CLIPPreTrainedModel''',
							    '''CLIPTextModel''',
							    '''CLIPTextModelWithProjection''',
							    '''CLIPVisionModel''',
							    '''CLIPVisionModelWithProjection''',
							]

try:
							if not is_tf_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							__UpperCamelCase  :      Optional[int]							      =		[
							    '''TF_CLIP_PRETRAINED_MODEL_ARCHIVE_LIST''',
							    '''TFCLIPModel''',
							    '''TFCLIPPreTrainedModel''',
							    '''TFCLIPTextModel''',
							    '''TFCLIPVisionModel''',
							]

try:
							if not is_flax_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							__UpperCamelCase  :      Optional[Any]							      =		[
							    '''FlaxCLIPModel''',
							    '''FlaxCLIPPreTrainedModel''',
							    '''FlaxCLIPTextModel''',
							    '''FlaxCLIPTextPreTrainedModel''',
							    '''FlaxCLIPVisionModel''',
							    '''FlaxCLIPVisionPreTrainedModel''',
							]


if TYPE_CHECKING:
							from .configuration_clip import (
							    CLIP_PRETRAINED_CONFIG_ARCHIVE_MAP,
							    CLIPConfig,
							    CLIPOnnxConfig,
							    CLIPTextConfig,
							    CLIPVisionConfig,
							)
							from .processing_clip import CLIPProcessor
							from .tokenization_clip import CLIPTokenizer

							try:
														if not is_tokenizers_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .tokenization_clip_fast import CLIPTokenizerFast

							try:
														if not is_vision_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .feature_extraction_clip import CLIPFeatureExtractor
														from .image_processing_clip import CLIPImageProcessor

							try:
														if not is_torch_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .modeling_clip import (
														    CLIP_PRETRAINED_MODEL_ARCHIVE_LIST,
														    CLIPModel,
														    CLIPPreTrainedModel,
														    CLIPTextModel,
														    CLIPTextModelWithProjection,
														    CLIPVisionModel,
														    CLIPVisionModelWithProjection,
														)

							try:
														if not is_tf_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .modeling_tf_clip import (
														    TF_CLIP_PRETRAINED_MODEL_ARCHIVE_LIST,
														    TFCLIPModel,
														    TFCLIPPreTrainedModel,
														    TFCLIPTextModel,
														    TFCLIPVisionModel,
														)

							try:
														if not is_flax_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .modeling_flax_clip import (
														    FlaxCLIPModel,
														    FlaxCLIPPreTrainedModel,
														    FlaxCLIPTextModel,
														    FlaxCLIPTextPreTrainedModel,
														    FlaxCLIPVisionModel,
														    FlaxCLIPVisionPreTrainedModel,
														)


else:
							import sys

							__UpperCamelCase  :      Tuple							      =		_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)




