





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


  def __init__(							self ,__snake_case ,__snake_case=1_3 ,__snake_case=3_2 ,__snake_case=3 ,__snake_case=4 ,__snake_case=[1_0, 2_0, 3_0, 4_0] ,__snake_case=[2, 2, 3, 2] ,__snake_case=True ,__snake_case=True ,__snake_case=3_7 ,__snake_case="gelu" ,__snake_case=1_0 ,__snake_case=0.02 ,__snake_case=["stage2", "stage3", "stage4"] ,__snake_case=3 ,__snake_case=None ,):



       """simple docstring"""





       A_          =       parent
       A_          =       batch_size
       A_          =       image_size
       A_          =       num_channels
       A_          =       num_stages
       A_          =       hidden_sizes
       A_          =       depths
       A_          =       is_training
       A_          =       use_labels
       A_          =       intermediate_size
       A_          =       hidden_act
       A_          =       type_sequence_label_size
       A_          =       initializer_range
       A_          =       out_features
       A_          =       num_labels
       A_          =       scope
       A_          =       num_stages


  def 						__UpperCAmelCase     (							self ):



       """simple docstring"""





       A_          =       floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] )

       A_          =       None
       if self.use_labels:
            A_          =       ids_tensor([self.batch_size] ,self.type_sequence_label_size )

       A_          =       self.get_config()

       return config, pixel_values, labels


  def 						__UpperCAmelCase     (							self ):



       """simple docstring"""





       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 						__UpperCAmelCase     (							self ):



       """simple docstring"""





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


  def 						__UpperCAmelCase     (							self ,__snake_case ,__snake_case ,__snake_case ):



       """simple docstring"""





       A_          =       UperNetForSemanticSegmentation(config=snake_case_ )
       model.to(snake_case_ )
       model.eval()
       A_          =       model(snake_case_ )
       self.parent.assertEqual(
           result.logits.shape ,(self.batch_size, self.num_labels, self.image_size, self.image_size) )




  def 						__UpperCAmelCase     (							self ):



       """simple docstring"""





       A_          =       self.prepare_config_and_inputs()
       (
           (
           A_						
       )   ,    (
           A_						
       )   ,    (
           A_						
       )   ,    						
       )          =       config_and_inputs
       A_          =       {'''pixel_values''': pixel_values}
       return config, inputs_dict





@require_torch
class UpperCAmelCase_				( _SCREAMING_SNAKE_CASE     ,		_SCREAMING_SNAKE_CASE     ,		unittest.TestCase ):
  __lowerCAmelCase   :						List[Any]							=   (UperNetForSemanticSegmentation,) if is_torch_available() else ()
  __lowerCAmelCase   :						Optional[Any]							=   {"""image-segmentation""": UperNetForSemanticSegmentation} if is_torch_available() else {}
  __lowerCAmelCase   :						int							=   False
  __lowerCAmelCase   :						Optional[int]							=   False
  __lowerCAmelCase   :						List[Any]							=   False
  __lowerCAmelCase   :						Any							=   False
  __lowerCAmelCase   :						Tuple							=   False
  __lowerCAmelCase   :						Optional[int]							=   False


  def 						__UpperCAmelCase     (							self ):



       """simple docstring"""





       A_          =       UperNetModelTester(self )
       A_          =       ConfigTester(self ,config_class=snake_case_ ,has_text_modality=snake_case_ ,hidden_size=3_7 )


  def 						__UpperCAmelCase     (							self ):



       """simple docstring"""





       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 						__UpperCAmelCase     (							self ):



       """simple docstring"""





       return


  def 						__UpperCAmelCase     (							self ):



       """simple docstring"""





       A_   ,    A_          =       self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
            A_          =       model_class(snake_case_ )
            A_          =       inspect.signature(model.forward )
            # signature.parameters is an OrderedDict => so arg_names order is deterministic
            A_          =       [*signature.parameters.keys()]

            A_          =       ['''pixel_values''']
            self.assertListEqual(arg_names[:1] ,snake_case_ )


  def 						__UpperCAmelCase     (							self ):



       """simple docstring"""





       A_          =       self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_semantic_segmentation(*snake_case_ )


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



       """simple docstring"""





       pass


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



       """simple docstring"""





       pass


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



       """simple docstring"""





       pass


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



       """simple docstring"""





       pass


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



       """simple docstring"""





       pass


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



       """simple docstring"""





       pass


  def 						__UpperCAmelCase     (							self ):



       """simple docstring"""





       def check_hidden_states_output(__snake_case ,__snake_case ,__snake_case ):
            A_          =       model_class(snake_case_ )
            model.to(snake_case_ )
            model.eval()

            with torch.no_grad():
                 A_          =       model(**self._prepare_for_class(snake_case_ ,snake_case_ ) )

            A_          =       outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

            A_          =       self.model_tester.num_stages
            self.assertEqual(len(snake_case_ ) ,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] ,)

       A_   ,    A_          =       self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
            A_          =       True
            check_hidden_states_output(snake_case_ ,snake_case_ ,snake_case_ )

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

            check_hidden_states_output(snake_case_ ,snake_case_ ,snake_case_ )


  def 						__UpperCAmelCase     (							self ):



       """simple docstring"""





       A_   ,    A_          =       self.model_tester.prepare_config_and_inputs_for_common()

       A_          =       _config_zero_init(snake_case_ )
       A_          =       _config_zero_init(configs_no_init.backbone_config )
       for model_class in self.all_model_classes:
            A_          =       model_class(config=snake_case_ )
            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 						__UpperCAmelCase     (							self ):



       """simple docstring"""





       pass




  @slow
  def 						__UpperCAmelCase     (							self ):



       """simple docstring"""





       for model_name in UPERNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
            A_          =       UperNetForSemanticSegmentation.from_pretrained(snake_case_ )
            self.assertIsNotNone(snake_case_ )




def      UpperCAmelCase_    (    )	->				int:
     '''simple docstring'''


     A_          =       hf_hub_download(
         repo_id='''hf-internal-testing/fixtures_ade20k'''  ,						repo_type='''dataset'''  ,						filename='''ADE_val_00000001.jpg'''				)
     A_          =       Image.open(__lowerCamelCase				).convert('''RGB'''				)
     return image





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


  def 						__UpperCAmelCase     (							self ):



       """simple docstring"""





       A_          =       AutoImageProcessor.from_pretrained('''openmmlab/upernet-swin-tiny''' )
       A_          =       UperNetForSemanticSegmentation.from_pretrained('''openmmlab/upernet-swin-tiny''' ).to(snake_case_ )

       A_          =       prepare_img()
       A_          =       processor(images=snake_case_ ,return_tensors='''pt''' ).to(snake_case_ )

       with torch.no_grad():
            A_          =       model(**snake_case_ )

       A_          =       torch.Size((1, model.config.num_labels, 5_1_2, 5_1_2) )
       self.assertEqual(outputs.logits.shape ,snake_case_ )

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




  def 						__UpperCAmelCase     (							self ):



       """simple docstring"""





       A_          =       AutoImageProcessor.from_pretrained('''openmmlab/upernet-convnext-tiny''' )
       A_          =       UperNetForSemanticSegmentation.from_pretrained('''openmmlab/upernet-convnext-tiny''' ).to(snake_case_ )

       A_          =       prepare_img()
       A_          =       processor(images=snake_case_ ,return_tensors='''pt''' ).to(snake_case_ )

       with torch.no_grad():
            A_          =       model(**snake_case_ )

       A_          =       torch.Size((1, model.config.num_labels, 5_1_2, 5_1_2) )
       self.assertEqual(outputs.logits.shape ,snake_case_ )

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








from collections import OrderedDict
from typing import Any, Mapping, Optional

from ... import PreTrainedTokenizer, TensorType, is_torch_available
from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfigWithPast
from ...utils import logging


lowercase_													=      logging.get_logger(__name__)

lowercase_													=      {
    '''EleutherAI/gpt-neo-1.3B''': '''https://huggingface.co/EleutherAI/gpt-neo-1.3B/resolve/main/config.json''',
    # See all GPTNeo models at https://huggingface.co/models?filter=gpt_neo
}






class     __a			(  SCREAMING_SNAKE_CASE					):
	SCREAMING_SNAKE_CASE  	=							"gpt_neo"
	SCREAMING_SNAKE_CASE  	=							["past_key_values"]
	SCREAMING_SNAKE_CASE  	=							{"num_attention_heads": "num_heads", "num_hidden_layers": "num_layers"}



	def __init__(    self				:     Optional[int]					,						snake_case_				:     int=5_02_57					,						snake_case_				:     Tuple=20_48					,						snake_case_				:     Optional[int]=20_48					,						snake_case_				:     int=24					,						snake_case_				:     List[Any]=[[["global", "local"], 12]]					,						snake_case_				:     Tuple=16					,						snake_case_				:     Optional[int]=None					,						snake_case_				:     Dict=2_56					,						snake_case_				:     List[str]="gelu_new"					,						snake_case_				:     List[Any]=0.0					,						snake_case_				:     Tuple=0.0					,						snake_case_				:     Tuple=0.0					,						snake_case_				:     str=0.1					,						snake_case_				:     Union[str, Any]=1e-5					,						snake_case_				:     str=0.0_2					,						snake_case_				:     Union[str, Any]=True					,						snake_case_				:     Optional[int]=5_02_56					,						snake_case_				:     Tuple=5_02_56					,						**snake_case_				:     Optional[int]					,						)->				Tuple:
						__lowerCAmelCase						 =vocab_size
						__lowerCAmelCase						 =max_position_embeddings
						__lowerCAmelCase						 =hidden_size
						__lowerCAmelCase						 =num_layers
						__lowerCAmelCase						 =num_heads
						__lowerCAmelCase						 =intermediate_size
						__lowerCAmelCase						 =window_size
						__lowerCAmelCase						 =activation_function
						__lowerCAmelCase						 =resid_dropout
						__lowerCAmelCase						 =embed_dropout
						__lowerCAmelCase						 =attention_dropout
						__lowerCAmelCase						 =classifier_dropout
						__lowerCAmelCase						 =layer_norm_epsilon
						__lowerCAmelCase						 =initializer_range
						__lowerCAmelCase						 =use_cache

						__lowerCAmelCase						 =bos_token_id
						__lowerCAmelCase						 =eos_token_id

						__lowerCAmelCase						 =attention_types
						__lowerCAmelCase						 =self.expand_attention_types_params(snake_case_)

						if len(self.attention_layers) != self.num_layers:
											raise ValueError(
											    """Configuration for convolutional module is incorrect. """
											    """It is required that `len(config.attention_layers)` == `config.num_layers` """
											    F"""but is `len(config.attention_layers) = {len(self.attention_layers)}`, """
											    F"""`config.num_layers = {self.num_layers}`. """
											    """`config.attention_layers` is prepared using `config.attention_types`. """
											    """Please verify the value of `config.attention_types` argument.""")

						super().__init__(bos_token_id=snake_case_					,						eos_token_id=snake_case_					,						**snake_case_)



	@staticmethod
	def 		UpperCamelCase				(    snake_case_				:     Optional[int])->				Any:
						__lowerCAmelCase						 =[]
						for item in attention_types:
											for _ in range(item[1]):
																attentions.extend(item[0])
						return attentions






def        __lowerCAmelCase		(	__lowerCamelCase						:     Any					, __lowerCamelCase						:     List[str]					, __lowerCamelCase						:     Optional[Any]					, __lowerCamelCase						:     int						)	->    int:
					import torch

					__lowerCAmelCase						 =input.size()
					__lowerCAmelCase						 =len(__lowerCamelCase						)
					__lowerCAmelCase						 =shape[dimension]

					__lowerCAmelCase						 =torch.arange(0					, __lowerCamelCase					, __lowerCamelCase						)
					__lowerCAmelCase						 =torch.div(sizedim - size					, __lowerCamelCase					, rounding_mode="""floor"""						) + 1
					__lowerCAmelCase						 =torch.arange(__lowerCamelCase						) + low_indices[:min_length][:, None]

					__lowerCAmelCase						 =[slice(__lowerCamelCase						)] * rank
					__lowerCAmelCase						 =indices
					__lowerCAmelCase						 =input[s]

					__lowerCAmelCase						 =list(range(0					, rank + 1						)						)
					perm.append(perm.pop(dimension + 1						)						)

					return sliced.permute(__lowerCamelCase						)






def        __lowerCAmelCase		(	__lowerCamelCase						:     Optional[Any]					, __lowerCamelCase						:     Any						)	->    List[str]:
					import torch

					__lowerCAmelCase						 =torch.arange(1					, __lowerCamelCase						)
					__lowerCAmelCase						 =torch.remainder(__lowerCamelCase					, __lowerCamelCase						)
					__lowerCAmelCase						 =remainders == 0
					__lowerCAmelCase						 =candidates[divisor_indices]
					__lowerCAmelCase						 =torch.max(__lowerCamelCase						)
					return largest_divisor, torch.div(__lowerCamelCase					, __lowerCamelCase					, rounding_mode="""floor"""						)






class     __a			(  SCREAMING_SNAKE_CASE					):



	@property
	def 		UpperCamelCase				(    self				:     Union[str, Any])->				Mapping[str, Mapping[int, str]]:
						__lowerCAmelCase						 =OrderedDict({"""input_ids""": {0: """batch""", 1: """sequence"""}})
						if self.use_past:
											self.fill_with_past_key_values_(snake_case_					,						direction="""inputs""")
											__lowerCAmelCase						 ={0: """batch""", 1: """past_sequence + sequence"""}
						else:
											__lowerCAmelCase						 ={0: """batch""", 1: """sequence"""}

						return common_inputs



	@property
	def 		UpperCamelCase				(    self				:     Optional[int])->				int:
						return self._config.num_heads



	def 		UpperCamelCase				(    self				:     int					,						snake_case_				:     PreTrainedTokenizer					,						snake_case_				:     int = -1					,						snake_case_				:     int = -1					,						snake_case_				:     bool = False					,						snake_case_				:     Optional[TensorType] = None					,						)->				Mapping[str, Any]:
						__lowerCAmelCase						 =super(snake_case_					,						self).generate_dummy_inputs(
						    snake_case_					,						batch_size=snake_case_					,						seq_length=snake_case_					,						is_pair=snake_case_					,						framework=snake_case_)

						# We need to order the input in the way they appears in the forward()
						__lowerCAmelCase						 =OrderedDict({"""input_ids""": common_inputs["""input_ids"""]})

						# Need to add the past_keys
						if self.use_past:
											if not is_torch_available():
																raise ValueError("""Cannot generate dummy past_keys inputs without PyTorch installed.""")
											else:
																import torch

																__lowerCAmelCase ,						__lowerCAmelCase						 =common_inputs["""input_ids"""].shape
																# Not using the same length for past_key_values
																__lowerCAmelCase						 =seqlen + 2
																__lowerCAmelCase						 =(
																    batch,
																    self.num_attention_heads,
																    past_key_values_length,
																    self._config.hidden_size // self.num_attention_heads,
																)
																__lowerCAmelCase						 =[
																    (torch.zeros(snake_case_), torch.zeros(snake_case_)) for _ in range(self.num_layers)
																]

						__lowerCAmelCase						 =common_inputs["""attention_mask"""]
						if self.use_past:
											__lowerCAmelCase						 =ordered_inputs["""attention_mask"""].dtype
											__lowerCAmelCase						 =torch.cat(
											    [ordered_inputs["""attention_mask"""], torch.ones(snake_case_					,						snake_case_					,						dtype=snake_case_)]					,						dim=1)

						return ordered_inputs



	@property
	def 		UpperCamelCase				(    self				:     Union[str, Any])->				int:
						return 13




