






"""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   UpperCamelCase_ :



  def __init__(      self ,		snake_case__ ,		snake_case__=13 ,		snake_case__=32 ,		snake_case__=3 ,		snake_case__=4 ,		snake_case__=[10, 20, 30, 40] ,		snake_case__=[2, 2, 3, 2] ,		snake_case__=True ,		snake_case__=True ,		snake_case__=37 ,		snake_case__="gelu" ,		snake_case__=10 ,		snake_case__=0.02 ,		snake_case__=["stage2", "stage3", "stage4"] ,		snake_case__=3 ,		snake_case__=None ,		)			->			Optional[int]:



         """simple docstring"""





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



  def 		UpperCamelCase_				(      self				)			->			int:



         """simple docstring"""





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

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

         UpperCAmelCase	    =			self.get_config()

         return config, pixel_values, labels



  def 		UpperCamelCase_				(      self				)			->			Tuple:



         """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				)			->			Tuple:



         """simple docstring"""





         return UperNetConfig(
             backbone_config=self.get_backbone_config() ,		hidden_size=5_12 ,		pool_scales=[1, 2, 3, 6] ,		use_auxiliary_head=snake_case__ ,		auxiliary_loss_weight=0.4 ,		auxiliary_in_channels=40 ,		auxiliary_channels=2_56 ,		auxiliary_num_convs=1 ,		auxiliary_concat_input=snake_case__ ,		loss_ignore_index=2_55 ,		num_labels=self.num_labels ,		)



  def 		UpperCamelCase_				(      self ,		snake_case__ ,		snake_case__ ,		snake_case__				)			->			Optional[Any]:



         """simple docstring"""





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



  def 		UpperCamelCase_				(      self				)			->			Union[str, Any]:



         """simple docstring"""





         UpperCAmelCase	    =			self.prepare_config_and_inputs()
         (
             (
             UpperCAmelCase
         )    ,       (
             UpperCAmelCase
         )    ,       (
             UpperCAmelCase
         )    ,       
         )	    =			config_and_inputs
         UpperCAmelCase	    =			{"""pixel_values""": pixel_values}
         return config, inputs_dict





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



  def 		UpperCamelCase_				(      self				)			->			Optional[Any]:



         """simple docstring"""





         UpperCAmelCase	    =			UperNetModelTester(self				)
         UpperCAmelCase	    =			ConfigTester(self ,		config_class=snake_case__ ,		has_text_modality=snake_case__ ,		hidden_size=37				)



  def 		UpperCamelCase_				(      self				)			->			List[Any]:



         """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				)			->			List[Any]:



         """simple docstring"""





         return



  def 		UpperCamelCase_				(      self				)			->			Any:



         """simple docstring"""





         UpperCAmelCase    ,       UpperCAmelCase	    =			self.model_tester.prepare_config_and_inputs_for_common()

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

                UpperCAmelCase	    =			["""pixel_values"""]
                self.assertListEqual(arg_names[:1] ,		snake_case__				)



  def 		UpperCamelCase_				(      self				)			->			Tuple:



         """simple docstring"""





         UpperCAmelCase	    =			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				)			->			Optional[Any]:



         """simple docstring"""





         pass



  @unittest.skip(reason="""UperNet does not support input and output embeddings"""				)
  def 		UpperCamelCase_				(      self				)			->			Any:



         """simple docstring"""





         pass



  @unittest.skip(reason="""UperNet does not have a base model"""				)
  def 		UpperCamelCase_				(      self				)			->			Any:



         """simple docstring"""





         pass



  @unittest.skip(reason="""UperNet does not have a base model"""				)
  def 		UpperCamelCase_				(      self				)			->			List[Any]:



         """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				)			->			Union[str, Any]:



         """simple docstring"""





         pass



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



         """simple docstring"""





         pass



  def 		UpperCamelCase_				(      self				)			->			int:



         """simple docstring"""





         def check_hidden_states_output(snake_case__ ,		snake_case__ ,		snake_case__				):
                UpperCAmelCase	    =			model_class(snake_case__				)
                model.to(snake_case__				)
                model.eval()

                with torch.no_grad():
                       UpperCAmelCase	    =			model(**self._prepare_for_class(snake_case__ ,		snake_case__				)				)

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

                UpperCAmelCase	    =			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] ,		)

         UpperCAmelCase    ,       UpperCAmelCase	    =			self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
                UpperCAmelCase	    =			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"]
                UpperCAmelCase	    =			True

                check_hidden_states_output(snake_case__ ,		snake_case__ ,		snake_case__				)



  def 		UpperCamelCase_				(      self				)			->			List[str]:



         """simple docstring"""





         UpperCAmelCase    ,       UpperCAmelCase	    =			self.model_tester.prepare_config_and_inputs_for_common()

         UpperCAmelCase	    =			_config_zero_init(snake_case__				)
         UpperCAmelCase	    =			_config_zero_init(configs_no_init.backbone_config				)
         for model_class in self.all_model_classes:
                UpperCAmelCase	    =			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				)			->			List[str]:



         """simple docstring"""





         pass



  @slow
  def 		UpperCamelCase_				(      self				)			->			Any:



         """simple docstring"""





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






def       _lowerCAmelCase       (      ):







       '''simple docstring'''
       UpperCAmelCase	    =			hf_hub_download(
           repo_id="""hf-internal-testing/fixtures_ade20k"""	,      repo_type="""dataset"""	,      filename="""ADE_val_00000001.jpg"""		)
       UpperCAmelCase	    =			Image.open(lowerCAmelCase		).convert("""RGB"""		)
       return image





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



  def 		UpperCamelCase_				(      self				)			->			List[str]:



         """simple docstring"""





         UpperCAmelCase	    =			AutoImageProcessor.from_pretrained("""openmmlab/upernet-swin-tiny"""				)
         UpperCAmelCase	    =			UperNetForSemanticSegmentation.from_pretrained("""openmmlab/upernet-swin-tiny"""				).to(snake_case__				)

         UpperCAmelCase	    =			prepare_img()
         UpperCAmelCase	    =			processor(images=snake_case__ ,		return_tensors="""pt"""				).to(snake_case__				)

         with torch.no_grad():
                UpperCAmelCase	    =			model(**snake_case__				)

         UpperCAmelCase	    =			torch.Size((1, model.config.num_labels, 5_12, 5_12)				)
         self.assertEqual(outputs.logits.shape ,		snake_case__				)

         UpperCAmelCase	    =			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(snake_case__				)
         self.assertTrue(torch.allclose(outputs.logits[0, 0, :3, :3] ,		snake_case__ ,		atol=1e-4				)				)



  def 		UpperCamelCase_				(      self				)			->			Any:



         """simple docstring"""





         UpperCAmelCase	    =			AutoImageProcessor.from_pretrained("""openmmlab/upernet-convnext-tiny"""				)
         UpperCAmelCase	    =			UperNetForSemanticSegmentation.from_pretrained("""openmmlab/upernet-convnext-tiny"""				).to(snake_case__				)

         UpperCAmelCase	    =			prepare_img()
         UpperCAmelCase	    =			processor(images=snake_case__ ,		return_tensors="""pt"""				).to(snake_case__				)

         with torch.no_grad():
                UpperCAmelCase	    =			model(**snake_case__				)

         UpperCAmelCase	    =			torch.Size((1, model.config.num_labels, 5_12, 5_12)				)
         self.assertEqual(outputs.logits.shape ,		snake_case__				)

         UpperCAmelCase	    =			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(snake_case__				)
         self.assertTrue(torch.allclose(outputs.logits[0, 0, :3, :3] ,		snake_case__ ,		atol=1e-4				)				)








"""simple docstring"""







from typing import List, Optional, Union

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


lowerCAmelCase_	:    str            =      logging.get_logger(__name__)

lowerCAmelCase_	:    Optional[int]            =      {
    '''huggingface/time-series-transformer-tourism-monthly''': (
        '''https://huggingface.co/huggingface/time-series-transformer-tourism-monthly/resolve/main/config.json'''
    ),
    # See all TimeSeriesTransformer models at https://huggingface.co/models?filter=time_series_transformer
}





class   UpperCamelCase_ (       a_  ):
  _A       :					int				=	'time_series_transformer'
  _A       :					List[Any]				=	{
      'hidden_size': 'd_model',
      'num_attention_heads': 'encoder_attention_heads',
      'num_hidden_layers': 'encoder_layers',
  }



  def __init__(      self ,		snake_case__ = None ,		snake_case__ = None ,		snake_case__ = "student_t" ,		snake_case__ = "nll" ,		snake_case__ = 1 ,		snake_case__ = [1, 2, 3, 4, 5, 6, 7] ,		snake_case__ = "mean" ,		snake_case__ = 0 ,		snake_case__ = 0 ,		snake_case__ = 0 ,		snake_case__ = 0 ,		snake_case__ = None ,		snake_case__ = None ,		snake_case__ = 32 ,		snake_case__ = 32 ,		snake_case__ = 2 ,		snake_case__ = 2 ,		snake_case__ = 2 ,		snake_case__ = 2 ,		snake_case__ = True ,		snake_case__ = "gelu" ,		snake_case__ = 64 ,		snake_case__ = 0.1 ,		snake_case__ = 0.1 ,		snake_case__ = 0.1 ,		snake_case__ = 0.1 ,		snake_case__ = 0.1 ,		snake_case__ = 1_00 ,		snake_case__ = 0.02 ,		snake_case__=True ,		**snake_case__ ,		)			->			Union[str, Any]:



         """simple docstring"""





         UpperCAmelCase	    =			prediction_length
         UpperCAmelCase	    =			context_length or prediction_length
         UpperCAmelCase	    =			distribution_output
         UpperCAmelCase	    =			loss
         UpperCAmelCase	    =			input_size
         UpperCAmelCase	    =			num_time_features
         UpperCAmelCase	    =			lags_sequence
         UpperCAmelCase	    =			scaling
         UpperCAmelCase	    =			num_dynamic_real_features
         UpperCAmelCase	    =			num_static_real_features
         UpperCAmelCase	    =			num_static_categorical_features
         if cardinality and num_static_categorical_features > 0:
                if len(snake_case__				) != num_static_categorical_features:
                       raise ValueError(
                           """The cardinality should be a list of the same length as `num_static_categorical_features`"""				)
                UpperCAmelCase	    =			cardinality
         else:
                UpperCAmelCase	    =			[0]
         if embedding_dimension and num_static_categorical_features > 0:
                if len(snake_case__				) != num_static_categorical_features:
                       raise ValueError(
                           """The embedding dimension should be a list of the same length as `num_static_categorical_features`"""				)
                UpperCAmelCase	    =			embedding_dimension
         else:
                UpperCAmelCase	    =			[min(50 ,		(cat + 1) // 2				) for cat in self.cardinality]
         UpperCAmelCase	    =			num_parallel_samples

         # Transformer architecture configuration
         UpperCAmelCase	    =			input_size * len(snake_case__				) + self._number_of_features
         UpperCAmelCase	    =			d_model
         UpperCAmelCase	    =			encoder_attention_heads
         UpperCAmelCase	    =			decoder_attention_heads
         UpperCAmelCase	    =			encoder_ffn_dim
         UpperCAmelCase	    =			decoder_ffn_dim
         UpperCAmelCase	    =			encoder_layers
         UpperCAmelCase	    =			decoder_layers

         UpperCAmelCase	    =			dropout
         UpperCAmelCase	    =			attention_dropout
         UpperCAmelCase	    =			activation_dropout
         UpperCAmelCase	    =			encoder_layerdrop
         UpperCAmelCase	    =			decoder_layerdrop

         UpperCAmelCase	    =			activation_function
         UpperCAmelCase	    =			init_std

         UpperCAmelCase	    =			use_cache

         super().__init__(is_encoder_decoder=snake_case__ ,		**snake_case__				)



  @property
  def 		UpperCamelCase_				(      self				)			->			int:



         """simple docstring"""





         return (
             sum(self.embedding_dimension				)
             + self.num_dynamic_real_features
             + self.num_time_features
             + self.num_static_real_features
             + self.input_size * 2  # the log1p(abs(loc)) and log(scale) features
         )
